|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands\Support; |
| 4 | + |
| 5 | +use App\Models\Support\SupportApproval; |
| 6 | +use App\Models\Support\SupportCase; |
| 7 | +use App\Services\Support\Cursor\CursorAgentService; |
| 8 | +use App\Services\Support\Cursor\GitHubPullRequestService; |
| 9 | +use App\Services\Support\SupportActionLogger; |
| 10 | +use App\Services\Support\SupportApprovalEmailService; |
| 11 | +use App\Services\Support\SupportJson; |
| 12 | +use Illuminate\Console\Command; |
| 13 | + |
| 14 | +class AiPollAgentsCommand extends Command |
| 15 | +{ |
| 16 | + protected $signature = 'support:ai:poll-agents {--json}'; |
| 17 | + |
| 18 | + protected $description = 'Poll in-flight Cursor code-change agents, capture PR links, report results, open dev->live PR.'; |
| 19 | + |
| 20 | + public function handle( |
| 21 | + CursorAgentService $cursorAgent, |
| 22 | + GitHubPullRequestService $github, |
| 23 | + SupportApprovalEmailService $approvalEmail, |
| 24 | + SupportActionLogger $logger, |
| 25 | + ): int { |
| 26 | + if (!$cursorAgent->enabled()) { |
| 27 | + $this->maybeJson(['ok' => true, 'skipped' => 'code_change_disabled']); |
| 28 | + |
| 29 | + return self::SUCCESS; |
| 30 | + } |
| 31 | + |
| 32 | + $cases = SupportCase::query() |
| 33 | + ->where('status', 'action_executed') |
| 34 | + ->whereNotNull('cursor_agent_id') |
| 35 | + ->where('case_type', 'code_change') |
| 36 | + ->limit(25) |
| 37 | + ->get(); |
| 38 | + |
| 39 | + $checked = 0; |
| 40 | + $finished = 0; |
| 41 | + |
| 42 | + foreach ($cases as $case) { |
| 43 | + $checked++; |
| 44 | + $status = $cursorAgent->getAgent((string) $case->cursor_agent_id); |
| 45 | + $inner = is_array($status['result'] ?? null) ? $status['result'] : []; |
| 46 | + |
| 47 | + if (!($status['ok'] ?? false)) { |
| 48 | + if ($this->timedOut($case)) { |
| 49 | + $this->closeOut($case, $approvalEmail, $logger, false, ['errors' => ['agent_poll_timeout']]); |
| 50 | + $finished++; |
| 51 | + } |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + $agentStatus = $inner['status'] ?? null; |
| 56 | + $prUrl = $inner['pr_url'] ?? $case->cursor_pr_url; |
| 57 | + |
| 58 | + $case->update([ |
| 59 | + 'cursor_agent_status' => $agentStatus, |
| 60 | + 'cursor_pr_url' => $prUrl, |
| 61 | + ]); |
| 62 | + |
| 63 | + if (!$cursorAgent->isFinished($agentStatus)) { |
| 64 | + if ($this->timedOut($case)) { |
| 65 | + $this->closeOut($case, $approvalEmail, $logger, false, ['errors' => ['agent_poll_timeout'], 'result' => $inner]); |
| 66 | + $finished++; |
| 67 | + } |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + $succeeded = $cursorAgent->isSuccessful($agentStatus); |
| 72 | + $resultInner = $inner; |
| 73 | + |
| 74 | + if ($succeeded && $prUrl && (string) config('support_ai.live_promotion', 'pr_only') === 'pr_only') { |
| 75 | + $promotion = $github->openDevToLivePr( |
| 76 | + title: 'Promote dev → '.config('support_ai.live_branch', 'master').' (support copilot)', |
| 77 | + body: "Automated release PR opened by the support copilot.\n\nIncludes the fix from case #{$case->id} once merged into dev.\nA developer must review and merge to deploy.", |
| 78 | + ); |
| 79 | + if (($promotion['ok'] ?? false)) { |
| 80 | + $promoUrl = $promotion['result']['pr_url'] ?? null; |
| 81 | + $case->update(['live_promotion_pr_url' => $promoUrl]); |
| 82 | + $resultInner['promotion_pr_url'] = $promoUrl; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $this->closeOut($case, $approvalEmail, $logger, $succeeded, [ |
| 87 | + 'ok' => $succeeded, |
| 88 | + 'result' => $resultInner, |
| 89 | + 'errors' => $succeeded ? [] : ['agent_failed'], |
| 90 | + ]); |
| 91 | + $finished++; |
| 92 | + } |
| 93 | + |
| 94 | + $this->maybeJson(['ok' => true, 'checked' => $checked, 'finished' => $finished]); |
| 95 | + |
| 96 | + return self::SUCCESS; |
| 97 | + } |
| 98 | + |
| 99 | + private function timedOut(SupportCase $case): bool |
| 100 | + { |
| 101 | + $maxMinutes = (int) config('support_ai.code_change.max_poll_minutes', 30); |
| 102 | + |
| 103 | + return $case->updated_at !== null && $case->updated_at->diffInMinutes(now()) > $maxMinutes; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param array<string, mixed> $result |
| 108 | + */ |
| 109 | + private function closeOut( |
| 110 | + SupportCase $case, |
| 111 | + SupportApprovalEmailService $approvalEmail, |
| 112 | + SupportActionLogger $logger, |
| 113 | + bool $succeeded, |
| 114 | + array $result, |
| 115 | + ): void { |
| 116 | + $case->update(['status' => $succeeded ? 'verified' : 'escalated']); |
| 117 | + |
| 118 | + $approval = SupportApproval::query() |
| 119 | + ->where('support_case_id', $case->id) |
| 120 | + ->where('requested_action', 'code_change') |
| 121 | + ->where('status', 'approved') |
| 122 | + ->latest('id') |
| 123 | + ->first(); |
| 124 | + |
| 125 | + $envelope = SupportJson::ok('code_change', ['case_id' => $case->id], (array) ($result['result'] ?? [])); |
| 126 | + if (!$succeeded) { |
| 127 | + $envelope = SupportJson::fail('code_change', ['case_id' => $case->id], (array) ($result['errors'] ?? ['agent_failed'])); |
| 128 | + $envelope['result'] = (array) ($result['result'] ?? []); |
| 129 | + } |
| 130 | + |
| 131 | + $logger->log( |
| 132 | + case: $case, |
| 133 | + actionName: 'code_change_completed', |
| 134 | + actionType: 'write', |
| 135 | + input: ['case_id' => $case->id, 'agent_id' => $case->cursor_agent_id], |
| 136 | + output: $envelope, |
| 137 | + succeeded: $succeeded, |
| 138 | + executedBy: 'system', |
| 139 | + correlationId: $case->correlation_id, |
| 140 | + errorMessage: $succeeded ? null : implode(';', (array) ($result['errors'] ?? [])), |
| 141 | + ); |
| 142 | + |
| 143 | + if ($approval !== null) { |
| 144 | + try { |
| 145 | + $approvalEmail->sendActionCompletion($case, $approval, 'code_change', $envelope, $succeeded); |
| 146 | + } catch (\Throwable $e) { |
| 147 | + $logger->log( |
| 148 | + case: $case, |
| 149 | + actionName: 'support_completion_email', |
| 150 | + actionType: 'notify', |
| 151 | + input: ['case_id' => $case->id], |
| 152 | + output: ['ok' => false, 'error' => $e->getMessage()], |
| 153 | + succeeded: false, |
| 154 | + executedBy: 'system', |
| 155 | + correlationId: $case->correlation_id, |
| 156 | + errorMessage: $e->getMessage(), |
| 157 | + ); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @param array<string, mixed> $payload |
| 164 | + */ |
| 165 | + private function maybeJson(array $payload): void |
| 166 | + { |
| 167 | + if ($this->option('json')) { |
| 168 | + $this->line(json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments