Skip to main content

orch/engine/runner/response_handler/
mod.rs

1//! Success-path response handler — commit, push, PR, token storage.
2//!
3//! Extracted from `runner/mod.rs`. Handles the `Ok(parsed)` arm of the parse
4//! result, including git operations and delegation storage. Also owns
5//! `write_result_json`.
6
7use crate::config;
8use crate::parser::AgentResponse;
9use crate::store;
10use crate::store::TaskStore;
11use std::io;
12use std::io::ErrorKind;
13use std::path::Path;
14use std::sync::Arc;
15
16use tokio::fs;
17
18use super::{agents, git_ops, response, screenshots, worktree};
19use crate::engine::cooldown;
20use crate::engine::push_retry;
21use crate::engine::review;
22use crate::github::http::GhHttp;
23use crate::status_heuristics;
24use crate::store::Task;
25
26/// Write a structured `result.json` to the attempt directory for debugging.
27#[expect(
28    clippy::too_many_arguments,
29    reason = "each field of result.json is a separate argument"
30)]
31pub async fn write_result_json(
32    attempt_dir: &Path,
33    task_id: &str,
34    agent_name: &str,
35    model_name: Option<&str>,
36    exit_code: i32,
37    new_attempts: u32,
38    parse_result: &Result<agents::ParsedResponse, agents::AgentError>,
39    raw_stdout: &str,
40    raw_stderr: &str,
41) {
42    let result_json = match parse_result {
43        Ok(parsed) => {
44            serde_json::json!({
45                "outcome": "success",
46                "agent": agent_name,
47                "model": model_name.unwrap_or("default"),
48                "exit_code": exit_code,
49                "attempt": new_attempts,
50                "status": parsed.response.status,
51                "summary": parsed.response.summary,
52                "input_tokens": parsed.input_tokens,
53                "output_tokens": parsed.output_tokens,
54                "duration_ms": parsed.duration_ms,
55                "files": parsed.response.files,
56                "accomplished": parsed.response.accomplished,
57                "remaining": parsed.response.remaining,
58                "error": parsed.response.error,
59                "learnings": parsed.response.learnings,
60                "delegations": parsed.response.delegations.iter()
61                    .map(|d| serde_json::json!({"title": d.title, "body": d.body}))
62                    .collect::<Vec<_>>(),
63            })
64        }
65        Err(agent_err) => {
66            serde_json::json!({
67                "outcome": "error",
68                "agent": agent_name,
69                "model": model_name.unwrap_or("default"),
70                "exit_code": exit_code,
71                "attempt": new_attempts,
72                "error_class": agents::error_class_name(agent_err),
73                "error_message": agent_err.to_string(),
74                "stderr_tail": agents::patterns::safe_tail(raw_stderr, 2000),
75                "stdout_tail": agents::patterns::safe_tail(raw_stdout, 2000),
76            })
77        }
78    };
79
80    if let Err(e) = fs::write(
81        attempt_dir.join("result.json"),
82        serde_json::to_string_pretty(&result_json).unwrap_or_default(),
83    )
84    .await
85    {
86        tracing::debug!(task_id, ?e, "failed to write result.json");
87    }
88}
89
90/// Input to the pure status-decision function [`classify_final_status`].
91///
92/// The caller is responsible for pre-computing counter values (via store
93/// increments) and populating them here.  No I/O is performed inside the
94/// decision function itself, making it trivially unit-testable.
95#[derive(Default)]
96#[expect(
97    clippy::struct_excessive_bools,
98    reason = "each flag is an independent input to the status decision"
99)]
100struct DecisionInput<'a> {
101    /// Agent-reported status (e.g. `"done"`, `"in_progress"`).
102    agent_status: &'a str,
103    /// Push was attempted and commits existed, but the push error contained a
104    /// `workflow` scope complaint — non-retryable.
105    is_workflow_scope_failure: bool,
106    /// Push failure recovery attempted a rebase but failed (for example due to
107    /// conflicts). This is non-retryable without human intervention.
108    is_rebase_conflict_failure: bool,
109    /// Push was attempted (commits existed) but failed.
110    push_failed: bool,
111    /// Persistent push-failure counter *after* this run's increment.
112    /// Only meaningful when `push_failed` is `true`.
113    push_failures: u64,
114    /// The push failed on a GitHub or network error. A new agent run cannot change that, so the
115    /// engine retries the push with a backoff instead (#4025).
116    is_transient_push_failure: bool,
117    /// A PR already exists (or was just created) for this task.
118    has_pr: bool,
119    /// The agent created sub-task delegations.
120    has_delegations: bool,
121    /// Commits were pushed to the remote branch successfully.
122    has_pushed: bool,
123    /// PR creation failed because the base branch was invalid (even after
124    /// GitHub API fallback). This is a terminal error — the task should be
125    /// blocked rather than re-dispatched.
126    is_pr_base_invalid: bool,
127    /// The task is external and requires a PR to be marked done.
128    /// Always false for internal tasks.
129    requires_pr: bool,
130    /// Persistent no-code-reroute counter *after* this run's increment.
131    no_code_reroutes: u64,
132    /// Maximum no-code reroutes before blocking (from config).
133    max_reroutes: u32,
134    /// True if the agent that just ran is the same as the one that produced
135    /// the previous no-code result. Same-agent loops should be blocked immediately.
136    is_same_agent: bool,
137    /// True when agent_status is "completed". Used to distinguish internal
138    /// completed tasks (mark done) from "done" tasks where the git ops pipeline
139    /// would have detected no-code reroutes. Pre-computed from agent_status
140    /// by the caller so this function stays pure.
141    is_completed_status: bool,
142    /// Agent explicitly returned blocked, but reason appears transient and retryable.
143    is_retryable_blocked: bool,
144    /// The task was re-dispatched to resolve a merge conflict and came back with
145    /// nothing ahead of the default branch, meaning the fix already landed and
146    /// the open PR is superseded. Mark done instead of counting a no-code run.
147    is_superseded_conflict: bool,
148}
149
150/// Determine the final task status from pre-computed state.
151///
152/// This is a pure function — it performs no I/O.  All store increments must be
153/// done by the caller **before** calling this function (so counters reflect the
154/// current run), and all store side-effects (clearing agent/model, writing
155/// last_error) must be applied **after** based on the returned status.
156fn classify_final_status(input: &DecisionInput<'_>) -> String {
157    if input.is_workflow_scope_failure || input.is_rebase_conflict_failure {
158        "blocked".to_string()
159    } else if input.push_failed {
160        if input.is_transient_push_failure && input.push_failures < push_retry::MAX_PUSH_FAILURES {
161            "needs_review".to_string()
162        } else if input.push_failures >= 3 {
163            "blocked".to_string()
164        } else {
165            "new".to_string()
166        }
167    } else if input.agent_status == "done" && input.has_pr {
168        "needs_review".to_string()
169    } else if input.agent_status == "done" && !input.has_pr && input.has_delegations {
170        "blocked".to_string()
171    } else if input.agent_status == "done"
172        && !input.has_pr
173        && input.has_pushed
174        && input.is_pr_base_invalid
175    {
176        // PR creation failed with an invalid base branch (even after GitHub API
177        // fallback).  This is a terminal configuration error — re-dispatching
178        // would just burn tokens on the same failure.
179        "blocked".to_string()
180    } else if input.agent_status == "done" && !input.has_pr && input.has_pushed {
181        "routed".to_string()
182    } else if input.is_superseded_conflict {
183        "done".to_string()
184    } else if input.agent_status == "done" && input.requires_pr {
185        // Same-agent loop detection: if this agent is the same as the one that
186        // produced the previous no-code result, block immediately (#2410, #2686).
187        // Otherwise, block if max reroutes exhausted, else reroute.
188        if input.is_same_agent || input.no_code_reroutes >= u64::from(input.max_reroutes) {
189            "blocked".to_string()
190        } else {
191            "new".to_string()
192        }
193    } else if input.agent_status == "done" && !input.has_pushed {
194        // Only internal tasks reach here — external done+!has_pr+!has_pushed is
195        // always caught by branch 6 (requires_pr = true for external tasks with
196        // !has_pr and done status), so is_external can never be true here.
197        // Internal tasks may legitimately produce no git-visible changes.
198        "done".to_string()
199    } else if input.is_completed_status && input.has_pr {
200        // Agent said "completed" and a PR was created — send to review.
201        "needs_review".to_string()
202    } else if input.is_completed_status && !input.has_pr && input.has_delegations {
203        // Agent said "completed" with delegations but no PR — blocked on children.
204        "blocked".to_string()
205    } else if input.is_completed_status
206        && !input.has_pr
207        && input.has_pushed
208        && input.is_pr_base_invalid
209    {
210        "blocked".to_string()
211    } else if input.is_completed_status && !input.has_pr && input.has_pushed {
212        // Agent said "completed", commits pushed but no PR — re-dispatch for PR.
213        "routed".to_string()
214    } else if input.is_completed_status && input.requires_pr {
215        // Agent said "completed" on external task requiring PR without a PR or pushes.
216        // Same-agent loop detection applies, then reroute or block.
217        if input.is_same_agent || input.no_code_reroutes >= u64::from(input.max_reroutes) {
218            "blocked".to_string()
219        } else {
220            "new".to_string()
221        }
222    } else if input.is_completed_status {
223        // Agent said "completed" on internal task — mark done.
224        // Git ops already ran and detected no commits, so this is a legitimate
225        // completion without code changes.
226        "done".to_string()
227    } else if input.agent_status == "blocked" && input.is_retryable_blocked {
228        "new".to_string()
229    } else if status_heuristics::status_looks_like_descriptive_completion(input.agent_status) {
230        "done".to_string()
231    } else {
232        input.agent_status.to_string()
233    }
234}
235
236// ── Internal context ──────────────────────────────────────────────────────────
237
238/// Bundles store references so helper functions don't repeat four parameters.
239struct StoreCtx<'a> {
240    store: &'a Option<Arc<TaskStore>>,
241    store_id_opt: Option<i64>,
242    repo: &'a str,
243    task_id: &'a str,
244}
245
246impl StoreCtx<'_> {
247    /// Write fields, using the pre-resolved `store_id` when available.
248    async fn set(&self, fields: &[(&str, serde_json::Value)]) {
249        if let Some(store_id) = self.store_id_opt {
250            let _ = store::store_set_by_id(self.store.as_ref(), store_id, fields).await;
251        } else {
252            store::store_set(self.store, self.repo, self.task_id, fields).await;
253        }
254    }
255
256    /// A successful run ends the streak of network errors and timeouts
257    async fn reset_retry_streaks(&self) {
258        self.set(&[
259            ("network_retries", serde_json::json!(0)),
260            ("timeout_retries", serde_json::json!(0)),
261        ])
262        .await;
263    }
264
265    /// Load the full task record.
266    async fn get_task(&self) -> Option<Task> {
267        if let Some(store_id) = self.store_id_opt {
268            store::opt_store_get_task_by_id(self.store, store_id).await
269        } else {
270            store::opt_store_get_task(self.store, self.repo, self.task_id).await
271        }
272    }
273
274    /// Atomically increment a counter field.
275    async fn increment(&self, field: &str) -> anyhow::Result<u64> {
276        if let Some(store_id) = self.store_id_opt {
277            store::store_increment_by_id(self.store, store_id, field).await
278        } else {
279            store::store_increment(self.store, self.repo, self.task_id, field).await
280        }
281    }
282
283    /// Append an activity event to the task timeline.
284    async fn append_activity(
285        &self,
286        event_type: &str,
287        agent: Option<&str>,
288        model: Option<&str>,
289        details: Option<&serde_json::Value>,
290    ) {
291        if let Some(store_id) = self.store_id_opt {
292            if let Some(s) = self.store
293                && let Err(e) = s
294                    .append_activity(store_id, event_type, None, None, agent, model, details)
295                    .await
296            {
297                tracing::warn!(
298                    task_id = self.task_id,
299                    event_type,
300                    error = %e,
301                    "store append_activity failed"
302                );
303            }
304        } else {
305            store::store_log_activity(
306                self.store,
307                self.repo,
308                self.task_id,
309                event_type,
310                None,
311                None,
312                agent,
313                model,
314                details,
315            )
316            .await;
317        }
318    }
319}
320
321// ── Git operation result types ────────────────────────────────────────────────
322
323/// Outcome of the auto-commit → push → PR pipeline.
324#[derive(Default)]
325#[expect(
326    clippy::struct_excessive_bools,
327    reason = "each flag is an independent outcome of the git pipeline"
328)]
329struct GitOpsResult {
330    has_pr: bool,
331    has_pushed: bool,
332    has_commits: bool,
333    /// PR creation failed with an invalid base branch (after all retries
334    /// and the GitHub API fallback).  Used to block the task instead of
335    /// re-dispatching it indefinitely.
336    pr_base_invalid: bool,
337}
338
339/// Push-failure detection result.
340#[expect(
341    clippy::struct_excessive_bools,
342    reason = "each flag is an independent kind of push failure"
343)]
344struct PushFailureState {
345    push_failed: bool,
346    is_workflow_scope_failure: bool,
347    is_rebase_conflict_failure: bool,
348    /// GitHub or network error that the engine retries with a backoff, without an agent run.
349    is_transient_failure: bool,
350    /// Post-increment failure counter (0 when no push was attempted or failed).
351    push_failures: u64,
352    /// Last error stored in the task record (used for workflow-scope detection).
353    stored_last_error: String,
354}
355
356// ── Private git-ops helpers ───────────────────────────────────────────────────
357
358/// Attempt auto-commit; record any error in the store.
359async fn auto_commit_changes(
360    ctx: &StoreCtx<'_>,
361    work_dir: &Path,
362    task_title: &str,
363    agent_name: &str,
364    new_attempts: u32,
365) {
366    if let Err(e) = git_ops::auto_commit(
367        work_dir,
368        ctx.task_id,
369        task_title,
370        agent_name,
371        new_attempts,
372        super::commit::When::Always,
373    )
374    .await
375    {
376        tracing::error!(task_id = ctx.task_id, error = ?e, "auto commit failed");
377        ctx.set(&[(
378            "last_error",
379            serde_json::json!(format!("auto commit failed: {e}")),
380        )])
381        .await;
382    }
383}
384
385/// Check whether commits exist ahead of the default branch.
386///
387/// When there are no commits, clears any stale push-failure error so that the
388/// review gate does not block an otherwise-approved task.
389async fn check_commits_and_clear_stale_errors(
390    ctx: &StoreCtx<'_>,
391    work_dir: &Path,
392    default_branch: &str,
393) -> bool {
394    let has_commits = git_ops::has_commits_ahead(work_dir, default_branch).await;
395    if !has_commits {
396        tracing::info!(
397            task_id = ctx.task_id,
398            "no commits ahead of default branch, skipping push + PR"
399        );
400        // Clear stale push failure from previous runs.
401        let last_err = ctx
402            .get_task()
403            .await
404            .map(|t| t.last_error)
405            .unwrap_or_default();
406        if last_err.contains("push failed") {
407            ctx.set(&[("last_error", serde_json::json!(""))]).await;
408        }
409    }
410    has_commits
411}
412
413/// Push the branch to the remote and log the activity.
414///
415/// On push failure due to diverged history (non-fast-forward), fetches the remote
416/// branch and rebases local commits on top, then pushes again. If rebase fails
417/// (conflicts), the task is blocked for human intervention.
418///
419/// Returns `PushResult::Success` on success, `PushResult::Failed` if push failed
420/// and recovery was not possible, `PushResult::Skipped` if the push was skipped,
421/// or `PushResult::NoCommits` if the branch has no commits ahead of the default branch
422enum PushResult {
423    Success,
424    Failed,
425    Skipped,         // push_branch returned Ok(false)
426    NoCommits,       // agent made no code changes
427    WorktreeMissing, // retry after rebuilding missing worktree
428}
429
430fn is_missing_path_error(err: &anyhow::Error) -> bool {
431    err.chain().any(|cause| {
432        cause
433            .downcast_ref::<io::Error>()
434            .is_some_and(|ioe| ioe.kind() == ErrorKind::NotFound)
435    }) || err
436        .to_string()
437        .to_ascii_lowercase()
438        .contains("no such file or directory")
439}
440
441async fn recover_missing_worktree_for_push(
442    ctx: &StoreCtx<'_>,
443    wt: &worktree::WorktreeSetup,
444    task_title: &str,
445    agent_name: &str,
446    model_name: Option<&str>,
447) -> anyhow::Result<worktree::WorktreeSetup> {
448    tracing::warn!(
449        task_id = ctx.task_id,
450        worktree = %wt.work_dir.display(),
451        branch = wt.branch,
452        "worktree missing during push; attempting one-time recovery"
453    );
454    ctx.append_activity(
455        "push_recover_worktree",
456        Some(agent_name),
457        model_name,
458        Some(&serde_json::json!({
459            "status": "start",
460            "worktree": wt.work_dir.display().to_string(),
461            "branch": wt.branch,
462        })),
463    )
464    .await;
465
466    let recovered = worktree::setup_worktree(
467        ctx.task_id,
468        task_title,
469        &wt.main_project_dir,
470        ctx.store,
471        ctx.repo,
472    )
473    .await?;
474
475    if !recovered.work_dir.exists() {
476        anyhow::bail!(
477            "recovered worktree path is still missing: {}",
478            recovered.work_dir.display()
479        );
480    }
481
482    ctx.append_activity(
483        "push_recover_worktree",
484        Some(agent_name),
485        model_name,
486        Some(&serde_json::json!({
487            "status": "ok",
488            "worktree": recovered.work_dir.display().to_string(),
489            "branch": recovered.branch,
490        })),
491    )
492    .await;
493
494    Ok(recovered)
495}
496
497/// Push a branch that was just rebased onto its moved remote counterpart.
498///
499/// Separate from the first push attempt so a failure here is reported as orch's own
500/// push failure rather than re-entering the diverged-branch recovery
501async fn push_after_rebase(
502    ctx: &StoreCtx<'_>,
503    work_dir: &Path,
504    branch: &str,
505    default_branch: &str,
506    agent_name: &str,
507    model_name: Option<&str>,
508) -> PushResult {
509    match git_ops::push_branch(work_dir, branch, default_branch).await {
510        Ok(true) => {
511            ctx.append_activity(
512                "push",
513                Some(agent_name),
514                model_name,
515                Some(&serde_json::json!({
516                    "status": "ok",
517                    "branch": branch,
518                    "default_branch": default_branch,
519                    "reason": "pushed_after_rebase",
520                })),
521            )
522            .await;
523            ctx.set(&[
524                ("last_error", serde_json::json!("")),
525                ("push_failures", serde_json::json!(0)),
526            ])
527            .await;
528            PushResult::Success
529        }
530        Ok(false) => {
531            tracing::info!(
532                task_id = ctx.task_id,
533                branch,
534                default_branch,
535                "push skipped after rebase"
536            );
537            ctx.append_activity(
538                "push",
539                Some(agent_name),
540                model_name,
541                Some(&serde_json::json!({
542                    "status": "skipped",
543                    "branch": branch,
544                    "default_branch": default_branch,
545                    "reason": "pushed_after_rebase",
546                })),
547            )
548            .await;
549            PushResult::Skipped
550        }
551        Err(e) => {
552            tracing::error!(
553                task_id = ctx.task_id,
554                branch = branch,
555                error = ?e,
556                "push failed again after rebasing on the remote branch"
557            );
558            ctx.append_activity(
559                "push",
560                Some(agent_name),
561                model_name,
562                Some(&serde_json::json!({
563                    "status": "error",
564                    "branch": branch,
565                    "default_branch": default_branch,
566                    "reason": "pushed_after_rebase",
567                    "error": e.to_string(),
568                })),
569            )
570            .await;
571            ctx.set(&[(
572                "last_error",
573                serde_json::json!(format!("push failed after rebase on remote branch: {e}")),
574            )])
575            .await;
576            PushResult::Failed
577        }
578    }
579}
580
581async fn push_branch_with_log(
582    ctx: &StoreCtx<'_>,
583    work_dir: &Path,
584    branch: &str,
585    default_branch: &str,
586    agent_name: &str,
587    model_name: Option<&str>,
588) -> PushResult {
589    match git_ops::push_branch(work_dir, branch, default_branch).await {
590        Ok(true) => {
591            ctx.append_activity(
592                "push",
593                Some(agent_name),
594                model_name,
595                Some(&serde_json::json!({
596                    "status": "ok",
597                    "branch": branch,
598                    "default_branch": default_branch,
599                })),
600            )
601            .await;
602            // Clear any stale push failure from a previous run so review_and_merge
603            // does not incorrectly block an approved task.
604            ctx.set(&[
605                ("last_error", serde_json::json!("")),
606                ("push_failures", serde_json::json!(0)),
607            ])
608            .await;
609            PushResult::Success
610        }
611        Ok(false) => {
612            tracing::info!(
613                task_id = ctx.task_id,
614                branch,
615                default_branch,
616                "push skipped"
617            );
618            ctx.append_activity(
619                "push",
620                Some(agent_name),
621                model_name,
622                Some(&serde_json::json!({
623                    "status": "skipped",
624                    "branch": branch,
625                    "default_branch": default_branch,
626                })),
627            )
628            .await;
629            PushResult::Skipped
630        }
631        Err(e) => {
632            let err_str = e.to_string();
633            tracing::error!(task_id = ctx.task_id, error = ?e, "push failed");
634
635            if is_missing_path_error(&e) && !work_dir.exists() {
636                tracing::warn!(
637                    task_id = ctx.task_id,
638                    worktree = %work_dir.display(),
639                    "push failed because worktree path disappeared; will attempt recovery"
640                );
641                ctx.append_activity(
642                    "push",
643                    Some(agent_name),
644                    model_name,
645                    Some(&serde_json::json!({
646                        "status": "retry",
647                        "branch": branch,
648                        "default_branch": default_branch,
649                        "reason": "worktree_missing",
650                        "error": err_str,
651                    })),
652                )
653                .await;
654                return PushResult::WorktreeMissing;
655            }
656
657            // Check if this is a non-fast-forward error that we can recover from.
658            if push_error_is_diverged(&err_str) {
659                tracing::info!(
660                    task_id = ctx.task_id,
661                    branch = branch,
662                    "attempting fetch and rebase to recover from diverged branch"
663                );
664
665                // Attempt to fetch and rebase on origin/{branch}.
666                match git_ops::rebase_on_branch(work_dir, branch).await {
667                    Ok(true) => {
668                        // Pushing is orch's step, not the agent's: the commits are already
669                        // written, so push them onto the moved remote branch here. Re-routing
670                        // instead re-ran a finished task until it hit the attempt limit (#3783)
671                        tracing::info!(
672                            task_id = ctx.task_id,
673                            branch = branch,
674                            "rebase succeeded — retrying the push"
675                        );
676                        ctx.append_activity(
677                            "push_rebased",
678                            Some(agent_name),
679                            model_name,
680                            Some(&serde_json::json!({
681                                "status": "rebased",
682                                "branch": branch,
683                                "reason": "diverged_from_remote",
684                            })),
685                        )
686                        .await;
687                        return push_after_rebase(
688                            ctx,
689                            work_dir,
690                            branch,
691                            default_branch,
692                            agent_name,
693                            model_name,
694                        )
695                        .await;
696                    }
697                    Ok(false) => {
698                        // The remote branch already holds every local commit, nothing to push
699                        tracing::info!(
700                            task_id = ctx.task_id,
701                            branch = branch,
702                            "no local commits after fetch — marking as success"
703                        );
704                        ctx.append_activity(
705                            "push",
706                            Some(agent_name),
707                            model_name,
708                            Some(&serde_json::json!({
709                                "status": "ok",
710                                "branch": branch,
711                                "reason": "no_commits_after_fetch",
712                            })),
713                        )
714                        .await;
715                        ctx.set(&[
716                            ("last_error", serde_json::json!("")),
717                            ("push_failures", serde_json::json!(0)),
718                        ])
719                        .await;
720                        return PushResult::Success;
721                    }
722                    Err(rebase_err) if git_ops::is_missing_remote_ref_error(&rebase_err) => {
723                        // The branch never reached the remote, so there is nothing to rebase onto.
724                        // Take the counted retry path instead of the rebase conflict block
725                        tracing::warn!(
726                            task_id = ctx.task_id,
727                            branch = branch,
728                            "remote branch does not exist, skipping rebase recovery"
729                        );
730                        ctx.append_activity(
731                            "push",
732                            Some(agent_name),
733                            model_name,
734                            Some(&serde_json::json!({
735                                "status": "error",
736                                "branch": branch,
737                                "default_branch": default_branch,
738                                "error": err_str,
739                            })),
740                        )
741                        .await;
742                        ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
743                            .await;
744                        return PushResult::Failed;
745                    }
746                    Err(rebase_err) => {
747                        // Rebase failed (likely conflicts) — block for human intervention.
748                        tracing::error!(
749                            task_id = ctx.task_id,
750                            branch = branch,
751                            error = ?rebase_err,
752                            "rebase failed — blocking task for human intervention"
753                        );
754                        ctx.append_activity(
755                            "push_rebased",
756                            Some(agent_name),
757                            model_name,
758                            Some(&serde_json::json!({
759                                "status": "error",
760                                "branch": branch,
761                                "error": rebase_err.to_string(),
762                            })),
763                        )
764                        .await;
765                        ctx.set(&[(
766                            "last_error",
767                            serde_json::json!(format!(
768                                "push failed and rebase failed: {rebase_err} — manual resolution required"
769                            )),
770                        )])
771                        .await;
772                        return PushResult::Failed;
773                    }
774                }
775            }
776
777            // Non-recoverable push error — log and return failure.
778            ctx.append_activity(
779                "push",
780                Some(agent_name),
781                model_name,
782                Some(&serde_json::json!({
783                    "status": "error",
784                    "branch": branch,
785                    "default_branch": default_branch,
786                    "error": err_str,
787                })),
788            )
789            .await;
790            ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
791                .await;
792            PushResult::Failed
793        }
794    }
795}
796
797/// True when a push error means the remote branch moved. `[remote rejected]` is a server side
798/// failure, only `! [rejected]` marks a divergence
799fn push_error_is_diverged(err_str: &str) -> bool {
800    err_str.contains("non-fast-forward")
801        || err_str.contains("! [rejected]")
802        || err_str.contains("fetch first")
803        || err_str.contains("behind")
804}
805
806/// True when the GitHub client refused the call locally because its rate-limit window is active.
807fn is_local_rate_limit_skip(err_str: &str) -> bool {
808    err_str.contains("rate-limited, skipping call")
809}
810
811/// Create (or find an existing) PR and log the activity.
812///
813/// Returns `(has_pr, has_pushed, pr_base_invalid)`. `has_pushed` may be set to
814/// `false` when a 422 "no commits" GitHub error indicates the branch was
815/// already merged. `pr_base_invalid` is set when the base branch is rejected
816/// by GitHub (even after the API fallback), which is a terminal condition.
817async fn create_pr_with_log(
818    ctx: &StoreCtx<'_>,
819    wt: &worktree::WorktreeSetup,
820    task_title: &str,
821    resp: &AgentResponse,
822    agent_name: &str,
823    model_name: Option<&str>,
824    mut has_pushed: bool,
825) -> (bool, bool, bool) {
826    match git_ops::create_pr_if_needed(
827        &wt.work_dir,
828        &wt.branch,
829        task_title,
830        &resp.summary,
831        &resp.accomplished,
832        &resp.remaining,
833        &resp.files,
834        ctx.task_id,
835        agent_name,
836        model_name,
837        ctx.repo,
838        &wt.default_branch,
839    )
840    .await
841    {
842        Ok(ref url) => {
843            // Save pr_number to the store so the review gate can find it
844            // immediately without racing GitHub's list-API cache (~300 ms lag).
845            // This is set for both newly-created and pre-existing PRs.
846            match review::parse_pr_number_from_url(url) {
847                Ok(pr_num) => {
848                    if let Err(e) = store::store_set_result(
849                        ctx.store,
850                        ctx.repo,
851                        ctx.task_id,
852                        &[("pr_number", serde_json::json!(pr_num))],
853                    )
854                    .await
855                    {
856                        tracing::error!(
857                            task_id = ctx.task_id,
858                            pr_url = %url,
859                            err = %e,
860                            "CRITICAL: failed to save pr_number to store — downstream review gate may trigger duplicate gh pr create"
861                        );
862                    }
863                }
864                Err(e) => {
865                    tracing::warn!(
866                        task_id = ctx.task_id,
867                        pr_url = %url,
868                        error = %e,
869                        "PR created but failed to parse PR number from URL"
870                    );
871                }
872            }
873            ctx.append_activity(
874                "pr_create",
875                Some(agent_name),
876                model_name,
877                Some(&serde_json::json!({"status": "created", "url": url})),
878            )
879            .await;
880            (true, has_pushed, false)
881        }
882        Err(e) => {
883            let err_str = format!("{e}");
884            tracing::error!(task_id = ctx.task_id, error = ?e, "create PR failed");
885            ctx.append_activity(
886                "pr_create",
887                Some(agent_name),
888                model_name,
889                Some(&serde_json::json!({
890                    "status": "error",
891                    "branch": wt.branch,
892                    "error": err_str.clone(),
893                })),
894            )
895            .await;
896            ctx.set(&[(
897                "last_error",
898                serde_json::json!(format!("create PR failed: {e}")),
899            )])
900            .await;
901            // Distinguish between terminal 422 errors and recoverable ones.
902            // - "No commits between" / "head invalid" → agent made no code changes
903            //   or the branch was already merged.  Clearing has_pushed lets the
904            //   task fall through to the "done" path.
905            // - "base invalid" → the base branch name is wrong.  After the
906            //   GitHub API fallback in create_pr_if_needed also fails, this is a
907            //   terminal configuration error.  We set pr_base_invalid so the
908            //   classifier blocks the task instead of re-dispatching forever.
909            // A GitHub auth error is terminal too: the commits are pushed, so only a token fix helps
910            let mut pr_base_invalid = false;
911            if is_local_rate_limit_skip(&err_str) {
912                // No request was sent and the branch is pushed. Re-running the agent cannot help,
913                // so report the PR as pending and let the review gate create it once calls resume
914                tracing::warn!(
915                    task_id = ctx.task_id,
916                    "PR creation skipped by local GitHub rate limit — review gate will create the PR"
917                );
918                return (true, has_pushed, false);
919            }
920            if agents::patterns::detect_auth_error(&err_str).is_some() {
921                tracing::error!(
922                    task_id = ctx.task_id,
923                    "PR creation failed with GitHub auth error — blocking task"
924                );
925                pr_base_invalid = true;
926            } else if err_str.contains("422") {
927                if err_str.contains("No commits between") || err_str.contains("head") {
928                    tracing::info!(
929                        task_id = ctx.task_id,
930                        "PR creation returned 422/no-commits/head-invalid — agent made no code changes or branch merged, marking done"
931                    );
932                    has_pushed = false;
933                } else if git_ops::is_invalid_base_error(&err_str) {
934                    tracing::error!(
935                        task_id = ctx.task_id,
936                        "PR creation failed with invalid base branch — blocking task"
937                    );
938                    pr_base_invalid = true;
939                }
940            }
941            (false, has_pushed, pr_base_invalid)
942        }
943    }
944}
945
946/// Run the full auto-commit → push → PR pipeline.
947async fn run_git_ops(
948    ctx: &StoreCtx<'_>,
949    wt: &worktree::WorktreeSetup,
950    task_title: &str,
951    resp: &AgentResponse,
952    agent_name: &str,
953    model_name: Option<&str>,
954    new_attempts: u32,
955) -> GitOpsResult {
956    auto_commit_changes(ctx, &wt.work_dir, task_title, agent_name, new_attempts).await;
957
958    let mut has_commits =
959        check_commits_and_clear_stale_errors(ctx, &wt.work_dir, &wt.default_branch).await;
960
961    // Skip push + PR if there are no commits ahead of the default branch.
962    // No-op tasks (e.g. "nothing to execute") produce no commits, so pushing
963    // and creating a PR would just waste API calls and trigger 422 errors.
964    let push_result = if has_commits {
965        push_branch_with_log(
966            ctx,
967            &wt.work_dir,
968            &wt.branch,
969            &wt.default_branch,
970            agent_name,
971            model_name,
972        )
973        .await
974    } else {
975        store::store_log_activity(
976            ctx.store,
977            ctx.repo,
978            ctx.task_id,
979            "push",
980            None,
981            None,
982            Some(agent_name),
983            model_name,
984            Some(&serde_json::json!({
985                "status": "skipped",
986                "reason": "no_commits_ahead",
987                "branch": wt.branch,
988                "default_branch": wt.default_branch,
989            })),
990        )
991        .await;
992        PushResult::NoCommits
993    };
994    let push_result = if matches!(push_result, PushResult::WorktreeMissing) {
995        match recover_missing_worktree_for_push(ctx, wt, task_title, agent_name, model_name).await {
996            Ok(recovered) => {
997                let wt_for_ops = &recovered;
998                has_commits = check_commits_and_clear_stale_errors(
999                    ctx,
1000                    &wt_for_ops.work_dir,
1001                    &wt_for_ops.default_branch,
1002                )
1003                .await;
1004                if has_commits {
1005                    push_branch_with_log(
1006                        ctx,
1007                        &wt_for_ops.work_dir,
1008                        &wt_for_ops.branch,
1009                        &wt_for_ops.default_branch,
1010                        agent_name,
1011                        model_name,
1012                    )
1013                    .await
1014                } else {
1015                    PushResult::NoCommits
1016                }
1017            }
1018            Err(e) => {
1019                tracing::error!(
1020                    task_id = ctx.task_id,
1021                    err = %e,
1022                    "worktree recovery before push failed"
1023                );
1024                ctx.append_activity(
1025                    "push_recover_worktree",
1026                    Some(agent_name),
1027                    model_name,
1028                    Some(&serde_json::json!({
1029                        "status": "error",
1030                        "worktree": wt.work_dir.display().to_string(),
1031                        "branch": wt.branch,
1032                        "error": e.to_string(),
1033                    })),
1034                )
1035                .await;
1036                ctx.set(&[(
1037                    "last_error",
1038                    serde_json::json!(format!(
1039                        "push failed: worktree disappeared and recovery failed: {e}"
1040                    )),
1041                )])
1042                .await;
1043                PushResult::Failed
1044            }
1045        }
1046    } else {
1047        push_result
1048    };
1049
1050    let mut has_pushed = matches!(push_result, PushResult::Success);
1051    let mut has_pr = false;
1052
1053    // Create PR (skip if push failed or repo is unknown)
1054    if has_commits && matches!(push_result, PushResult::Failed) {
1055        tracing::warn!(
1056            task_id = ctx.task_id,
1057            "skipping PR creation due to push failure"
1058        );
1059    } else if matches!(push_result, PushResult::Skipped) {
1060        tracing::info!(task_id = ctx.task_id, "push skipped, skipping PR creation");
1061    } else if matches!(push_result, PushResult::NoCommits) {
1062        tracing::info!(
1063            task_id = ctx.task_id,
1064            "no commits ahead of default branch, skipping push + PR"
1065        );
1066    } else if !has_pushed {
1067        // no commits — already logged "no commits ahead, skipping push + PR" at INFO level above
1068    } else if ctx.repo.is_empty() {
1069        tracing::warn!(
1070            task_id = ctx.task_id,
1071            "skipping PR creation — repo is empty (internal task?)"
1072        );
1073    } else {
1074        let (pr, updated_pushed, pr_base_invalid) = create_pr_with_log(
1075            ctx, wt, task_title, resp, agent_name, model_name, has_pushed,
1076        )
1077        .await;
1078        has_pr = pr;
1079        has_pushed = updated_pushed;
1080        return GitOpsResult {
1081            has_pr,
1082            has_pushed,
1083            has_commits,
1084            pr_base_invalid,
1085        };
1086    }
1087
1088    GitOpsResult {
1089        has_pr,
1090        has_pushed,
1091        has_commits,
1092        pr_base_invalid: false,
1093    }
1094}
1095
1096// ── Private status-computation helpers ───────────────────────────────────────
1097
1098/// Comment the agent's screenshots on the PR. Returns the note for the task summary and channels
1099async fn post_screenshots(
1100    ctx: &StoreCtx<'_>,
1101    work_dir: &Path,
1102    resp: &AgentResponse,
1103) -> Option<String> {
1104    let note = resp.screenshot_note.as_deref().map(str::trim);
1105    let note = note.filter(|n| !n.is_empty());
1106    if resp.screenshots.is_empty() && note.is_none() {
1107        return None;
1108    }
1109    let pr_number = ctx.get_task().await.and_then(|t| t.pr_number);
1110    let (Some(pr_number), false) = (pr_number, ctx.repo.is_empty()) else {
1111        return note.map(String::from);
1112    };
1113    let Ok(pr) = u64::try_from(pr_number) else {
1114        return note.map(String::from);
1115    };
1116
1117    let roots = screenshots::allowed_roots(work_dir).await;
1118    let (images, skipped) = screenshots::prepare(&resp.screenshots, work_dir, &roots, pr);
1119    let commit = if images.is_empty() {
1120        String::new()
1121    } else {
1122        match screenshots::publish(work_dir, &images, pr).await {
1123            Ok(commit) => commit,
1124            Err(e) => {
1125                tracing::warn!(task_id = ctx.task_id, err = %e, "failed to push screenshots");
1126                let body = screenshots::build_unpublished_comment(note, &skipped);
1127                comment_screenshots(ctx, pr, &body).await;
1128                return note.map(String::from);
1129            }
1130        }
1131    };
1132    let body = screenshots::build_comment(ctx.repo, &commit, &images, note, &skipped);
1133    comment_screenshots(ctx, pr, &body).await;
1134    note.map(String::from)
1135}
1136
1137async fn comment_screenshots(ctx: &StoreCtx<'_>, pr: u64, body: &str) {
1138    match GhHttp::new() {
1139        Ok(gh) => {
1140            if let Err(e) = gh.add_comment(ctx.repo, &pr.to_string(), body).await {
1141                tracing::warn!(task_id = ctx.task_id, pr, err = %e, "failed to comment screenshots");
1142            }
1143        }
1144        Err(e) => tracing::warn!(task_id = ctx.task_id, err = %e, "cannot comment screenshots"),
1145    }
1146}
1147
1148/// Comment on and close a PR whose change already landed elsewhere.
1149async fn close_superseded_pr(ctx: &StoreCtx<'_>, pr_number: u64) {
1150    if ctx.repo.is_empty() {
1151        return;
1152    }
1153    match GhHttp::new() {
1154        Ok(gh) => {
1155            let note = "Closing as superseded: the change is already on the default branch, so this branch has no commits ahead of it.";
1156            if let Err(e) = gh.add_comment(ctx.repo, &pr_number.to_string(), note).await {
1157                tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to comment on superseded PR");
1158            }
1159            if let Err(e) = gh.close_pr(ctx.repo, pr_number).await {
1160                tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to close superseded PR");
1161            }
1162        }
1163        Err(e) => {
1164            tracing::warn!(task_id = ctx.task_id, err = %e, "cannot close superseded PR");
1165        }
1166    }
1167    // Dropping the PR lets the blocker refresh read the closed, completed issue with no PR
1168    ctx.set(&[
1169        (
1170            "last_error",
1171            serde_json::json!(
1172                "PR closed as superseded: the fix already landed on the default branch"
1173            ),
1174        ),
1175        ("pr_number", serde_json::json!(null)),
1176    ])
1177    .await;
1178}
1179
1180/// The PR to close when a merge-conflict reroute produced no commits.
1181///
1182/// `merge_conflict_retries > 0` means review_poll sent the task back to resolve a
1183/// conflict. Coming back with nothing ahead of the default branch means another
1184/// PR already landed the same fix, so the open PR is superseded (#3780)
1185fn superseded_conflict_pr(
1186    merge_conflict_retries: i32,
1187    pr_number: Option<i32>,
1188    has_commits: bool,
1189) -> Option<u64> {
1190    if has_commits || merge_conflict_retries <= 0 {
1191        return None;
1192    }
1193    pr_number
1194        .filter(|n| *n > 0)
1195        .and_then(|n| u64::try_from(n).ok())
1196}
1197
1198/// Read push-failure state from the store and increment `push_failures` if needed.
1199async fn detect_push_failure_state(
1200    ctx: &StoreCtx<'_>,
1201    has_commits: bool,
1202    has_pushed: bool,
1203) -> PushFailureState {
1204    // Load stored task once to inspect last_error (avoid repeated DB reads).
1205    let stored_last_error = ctx
1206        .get_task()
1207        .await
1208        .map(|t| t.last_error)
1209        .unwrap_or_default();
1210
1211    let push_failed = !has_pushed && has_commits && stored_last_error.contains("push failed");
1212
1213    // Detect workflow scope errors — these are non-retryable token permission issues.
1214    // Rerouting to a different agent won't help since the same token is used.
1215    let is_workflow_scope_failure = push_failed
1216        && (stored_last_error.contains("workflow` scope")
1217            || stored_last_error.contains("workflow' scope")
1218            || (stored_last_error.contains("refusing to allow")
1219                && stored_last_error.contains("workflow")));
1220
1221    let is_rebase_conflict_failure =
1222        push_failed && stored_last_error.contains("push failed and rebase failed:");
1223
1224    let is_transient_failure = push_failed
1225        && !is_workflow_scope_failure
1226        && !is_rebase_conflict_failure
1227        && git_ops::is_transient_network_error(&stored_last_error);
1228
1229    // Track push failures — block after 3 consecutive failures.
1230    // Workflow-scope failures are non-retryable and blocked immediately; do not
1231    // increment this retry counter for them so that later normal push failures
1232    // are not prematurely blocked.
1233    let push_failures = if push_failed && !is_workflow_scope_failure && !is_rebase_conflict_failure
1234    {
1235        match ctx.increment("push_failures").await {
1236            Ok(v) => v,
1237            Err(e) => {
1238                tracing::warn!(
1239                    task_id = ctx.task_id,
1240                    err = %e,
1241                    "failed to increment push_failures — reading current value from store"
1242                );
1243                // On DB error, read current value to ensure blocking threshold is still reachable
1244                ctx.get_task()
1245                    .await
1246                    .map_or(1, |t| u64::try_from(t.push_failures).unwrap_or(0) + 1)
1247                // assume at least 1 if we can't read either
1248            }
1249        }
1250    } else {
1251        0
1252    };
1253
1254    PushFailureState {
1255        push_failed,
1256        is_workflow_scope_failure,
1257        is_rebase_conflict_failure,
1258        is_transient_failure,
1259        push_failures,
1260        stored_last_error,
1261    }
1262}
1263
1264/// Increment `no_code_reroutes` and log a warning when the agent completed
1265/// without producing code changes on an external task requiring a PR.
1266async fn detect_no_code_reroutes(
1267    ctx: &StoreCtx<'_>,
1268    is_no_code_reroute: bool,
1269    new_attempts: u32,
1270    max_reroutes: u32,
1271) -> u64 {
1272    if !is_no_code_reroute {
1273        return 0;
1274    }
1275    tracing::warn!(
1276        task_id = ctx.task_id,
1277        attempts = new_attempts,
1278        max_reroutes,
1279        "agent reported done but produced no code changes on external task requiring PR"
1280    );
1281    match ctx.increment("no_code_reroutes").await {
1282        Ok(v) => v,
1283        Err(e) => {
1284            tracing::warn!(
1285                task_id = ctx.task_id,
1286                err = %e,
1287                "failed to increment no_code_reroutes — reading current value from store"
1288            );
1289            // On DB error, read current value to ensure blocking threshold is still reachable
1290            ctx.get_task()
1291                .await
1292                .map_or(1, |t| u64::try_from(t.no_code_reroutes).unwrap_or(0) + 1)
1293            // assume at least 1 if we can't read either
1294        }
1295    }
1296}
1297
1298// ── Post-decision side-effect helpers ─────────────────────────────────────────
1299
1300/// Build a human-readable reason string from a "blocked" agent response.
1301///
1302/// Priority: explicit `error` field → `summary` → fallback.
1303/// Appends `remaining` items if present so operators know what was left to do.
1304fn agent_blocked_reason(resp: &AgentResponse) -> String {
1305    let summary_opt = if resp.summary.is_empty() {
1306        None
1307    } else {
1308        Some(resp.summary.as_str())
1309    };
1310    let base = resp
1311        .error
1312        .as_deref()
1313        .filter(|s| !s.is_empty())
1314        .or(summary_opt)
1315        .unwrap_or("agent returned blocked status without a reason");
1316    if resp.remaining.is_empty() {
1317        base.to_string()
1318    } else {
1319        format!("{base}. Remaining: {}", resp.remaining.join("; "))
1320    }
1321}
1322
1323/// True when a blocked response names a denied `git push` and nothing else as the blocker.
1324fn is_denied_push_block(resp: &AgentResponse) -> bool {
1325    let r = agent_blocked_reason(resp).to_lowercase();
1326    r.contains("push")
1327        && [
1328            "denied",
1329            "not allowed",
1330            "permission",
1331            "disallowed",
1332            "approval",
1333        ]
1334        .iter()
1335        .any(|p| r.contains(p))
1336}
1337
1338/// Heuristic classifier for agent-returned blocked reasons.
1339///
1340/// Returns true when the reason looks transient/retryable (network/provider/
1341/// auth-temporary/environment availability issues), and false for likely
1342/// permanent/input/configuration issues.
1343fn is_retryable_blocked_reason(reason: &str) -> bool {
1344    let r = reason.to_lowercase();
1345
1346    let permanent_patterns = [
1347        "not found",
1348        "does not exist",
1349        "invalid",
1350        "malformed",
1351        "syntax error",
1352        "unsupported",
1353        "unimplemented",
1354        "manual intervention required",
1355        "human intervention required",
1356    ];
1357    if permanent_patterns.iter().any(|p| r.contains(p)) {
1358        return false;
1359    }
1360
1361    let transient_patterns = [
1362        "unavailable",
1363        "temporarily unavailable",
1364        "timeout",
1365        "timed out",
1366        "connection reset",
1367        "connection refused",
1368        "network error",
1369        "rate limit",
1370        "try again",
1371        "service down",
1372        "permission denied",
1373        "worktree lock",
1374        "git lock",
1375        "lockfile permission",
1376        "lock permission",
1377        "resource busy",
1378        "could not resolve host",
1379        // Credential providers in headless/tmux sessions can fail transiently
1380        // when GPG agent/passphrase is unavailable.
1381        "gpg decryption fails",
1382        "gpg decryption failed",
1383        "gpg agent",
1384        "no passphrase available",
1385        "passphrase unavailable",
1386        "pass store",
1387        "passwordstoreprovider",
1388        "agentvaultprovider",
1389        "onepasswordprovider",
1390        "decryption failed in this agent session",
1391    ];
1392    transient_patterns.iter().any(|p| r.contains(p))
1393}
1394
1395/// Apply tracing and store writes that depend on the final status decision.
1396#[expect(
1397    clippy::too_many_arguments,
1398    reason = "the decision and its context arrive as separate values"
1399)]
1400async fn apply_post_decision_effects(
1401    ctx: &StoreCtx<'_>,
1402    final_status: &str,
1403    resp_status: &str,
1404    push_state: &PushFailureState,
1405    is_no_code_reroute: bool,
1406    has_pr: bool,
1407    has_pushed: bool,
1408    is_pr_base_invalid: bool,
1409    has_delegations: bool,
1410    is_external: bool,
1411    no_code_reroutes: u64,
1412    max_reroutes: u32,
1413    agent_name: &str,
1414) {
1415    if push_state.is_workflow_scope_failure {
1416        // Non-retryable — block immediately with actionable guidance.
1417        tracing::error!(
1418            task_id = ctx.task_id,
1419            "push failed: token lacks `workflow` OAuth scope — blocking immediately \
1420             (rerouting would not help)"
1421        );
1422        ctx.set(&[(
1423            "last_error",
1424            serde_json::json!(format!(
1425                "push failed: GitHub token lacks `workflow` OAuth scope. \
1426                 The agent modified .github/workflows/ files but the token cannot push them. \
1427                 Fix: add `workflow` scope to your GitHub token, or use a GitHub App for auth. \
1428                 Original error: {}",
1429                push_state.stored_last_error
1430            )),
1431        )])
1432        .await;
1433    } else if push_state.is_rebase_conflict_failure {
1434        tracing::error!(
1435            task_id = ctx.task_id,
1436            "push failed and automatic rebase recovery failed — blocking for human intervention"
1437        );
1438        ctx.set(&[
1439            ("agent", serde_json::json!(null)),
1440            ("model", serde_json::json!(null)),
1441        ])
1442        .await;
1443    } else if push_state.push_failed && final_status == "needs_review" {
1444        // The engine retries the push from the review gate. Keep agent and model, since the
1445        // task never goes back to the router
1446        tracing::warn!(
1447            task_id = ctx.task_id,
1448            push_failures = push_state.push_failures,
1449            "agent done but push failed on a GitHub or network error — engine retries the push"
1450        );
1451    } else if push_state.push_failed {
1452        // Clear agent and model so router picks a different one on reroute (#1604)
1453        ctx.set(&[
1454            ("agent", serde_json::json!(null)),
1455            ("model", serde_json::json!(null)),
1456        ])
1457        .await;
1458        let push_failures = push_state.push_failures;
1459        if push_failures >= 3 {
1460            tracing::error!(
1461                task_id = ctx.task_id,
1462                push_failures,
1463                "push failed {push_failures} times — blocking for human intervention"
1464            );
1465        } else {
1466            tracing::warn!(
1467                task_id = ctx.task_id,
1468                push_failures,
1469                "agent done but push failed ({push_failures}/3) — rerouting to different agent"
1470            );
1471        }
1472    } else if resp_status == "done" && !has_pr && has_delegations {
1473        tracing::info!(
1474            task_id = ctx.task_id,
1475            "agent reported done with delegations but no PR — setting blocked"
1476        );
1477    } else if (resp_status == "done" || resp_status == "completed")
1478        && !has_pr
1479        && has_pushed
1480        && is_pr_base_invalid
1481    {
1482        tracing::error!(
1483            task_id = ctx.task_id,
1484            "PR creation failed with invalid base branch — blocking for human intervention"
1485        );
1486        ctx.set(&[(
1487            "last_error",
1488            serde_json::json!(
1489                "PR creation failed terminally (invalid base branch or GitHub auth error). The agent's commits were pushed successfully. \
1490                 Check the repository default branch and the GitHub token, then create the PR manually or unblock the task."
1491            ),
1492        )])
1493        .await;
1494    } else if resp_status == "done" && !has_pr && has_pushed {
1495        // Push succeeded but PR creation failed after retries.
1496        tracing::warn!(
1497            task_id = ctx.task_id,
1498            "agent done, commits pushed, but PR creation failed — re-dispatching as routed"
1499        );
1500    } else if is_no_code_reroute {
1501        // Record which agent produced this no-code result so the same-agent
1502        // detection on the next run can prevent loop-backs (#2410, #2686).
1503        // Also clear agent/model to force re-routing.
1504
1505        if final_status == "blocked" {
1506            // final_status is "blocked" either because max_reroutes was exhausted
1507            // or because is_same_agent was detected (both cases are handled by
1508            // classify_final_status). Log accordingly.
1509            let prev_no_code_agent = ctx
1510                .get_task()
1511                .await
1512                .map(|t| t.no_code_last_agent)
1513                .unwrap_or_default();
1514            let is_same_agent = !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name;
1515
1516            let msg = if is_same_agent {
1517                tracing::error!(
1518                    task_id = ctx.task_id,
1519                    agent = %agent_name,
1520                    "blocking same-agent no-code loop: agent {} would be selected again",
1521                    agent_name
1522                );
1523                format!(
1524                    "agent {agent_name} completed without code changes twice — same-agent loop detected, blocking for human review"
1525                )
1526            } else {
1527                tracing::error!(
1528                    task_id = ctx.task_id,
1529                    no_code_reroutes,
1530                    max_reroutes,
1531                    "blocking no-code reroute: reached max reroute attempts ({}/{}) for no-code-result",
1532                    no_code_reroutes,
1533                    max_reroutes
1534                );
1535                format!(
1536                    "agent completed without code changes after {no_code_reroutes}/{max_reroutes} reroute attempts on external task requiring PR"
1537                )
1538            };
1539            ctx.set(&[("last_error", serde_json::json!(msg))]).await;
1540        } else {
1541            // Rerouting — no blocking error to record yet.
1542            ctx.set(&[(
1543                "last_error",
1544                serde_json::json!(
1545                    "agent completed without code changes on external task requiring PR"
1546                ),
1547            )])
1548            .await;
1549        }
1550
1551        // Always record which agent produced this no-code result so the router can
1552        // skip it on the next attempt. Also clear agent/model to force re-routing
1553        // (the router will pick a fresh agent, which will be caught by the same-agent
1554        // check on the next round if it's the same agent).
1555        ctx.set(&[
1556            ("no_code_last_agent", serde_json::json!(agent_name)),
1557            ("agent", serde_json::json!(null)),
1558            ("model", serde_json::json!(null)),
1559        ])
1560        .await;
1561    } else if resp_status == "done" && !has_pr && is_external {
1562        // External task with non-code labels (e.g. documentation, research) —
1563        // allowed to be marked done without a PR.
1564        tracing::info!(
1565            task_id = ctx.task_id,
1566            "external task with non-code labels reported done — marking done without PR"
1567        );
1568    } else if resp_status == "done" && !has_pr {
1569        tracing::info!(
1570            task_id = ctx.task_id,
1571            "internal task reported done with no PR — marking done"
1572        );
1573    }
1574}
1575
1576/// Store token usage if both input and output token counts are known, with the cost the agent
1577/// reported for the run.
1578async fn store_token_usage(
1579    ctx: &StoreCtx<'_>,
1580    input_tokens: Option<u64>,
1581    output_tokens: Option<u64>,
1582    cost_usd: Option<f64>,
1583) {
1584    let (Some(input), Some(output)) = (input_tokens, output_tokens) else {
1585        return;
1586    };
1587    if let Some(st) = ctx.store {
1588        if let Some(store_id) = ctx.store_id_opt {
1589            if let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await {
1590                tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1591            }
1592        } else if let Ok(Some(store_id)) = st.resolve_task_id(ctx.repo, ctx.task_id).await
1593            && let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await
1594        {
1595            tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1596        }
1597    }
1598}
1599
1600/// Handle a successful agent response: commit, push, PR, delegations, tokens.
1601///
1602/// Returns `Ok((status, push_failed))` where `status` is the final task status
1603/// string and `push_failed` is `true` when a push was attempted but failed (for
1604/// audit trail classification).
1605#[expect(
1606    clippy::too_many_arguments,
1607    reason = "the runner passes each part of the run result"
1608)]
1609pub async fn handle_success(
1610    task_id: &str,
1611    parsed: agents::ParsedResponse,
1612    wt: &worktree::WorktreeSetup,
1613    task_title: &str,
1614    agent_name: &str,
1615    model_name: Option<&str>,
1616    new_attempts: u32,
1617    repo: &str,
1618    store: &Option<Arc<TaskStore>>,
1619    raw_stdout: &str,
1620) -> anyhow::Result<(String, bool)> {
1621    // Extract token counts before consuming parsed.
1622    let input_tokens = parsed.input_tokens;
1623    let output_tokens = parsed.output_tokens;
1624    let cost_usd = parsed.cost_usd;
1625    let mut resp = parsed.response;
1626
1627    tracing::info!(
1628        task_id,
1629        status = resp.status,
1630        summary = resp.summary,
1631        "agent completed successfully"
1632    );
1633
1634    let push_denied = resp.status == "blocked" && is_denied_push_block(&resp);
1635
1636    // Resolve numeric store_id once so we can reuse it for multiple store ops
1637    // in this hot path and avoid repeated external_id -> store_id SQL lookups.
1638    let store_id_opt = store::resolve_store_id(store, repo, task_id).await;
1639    let ctx = StoreCtx {
1640        store,
1641        store_id_opt,
1642        repo,
1643        task_id,
1644    };
1645
1646    ctx.reset_retry_streaks().await;
1647
1648    // Auto-commit, push, create PR
1649    let git = if resp.status == "done"
1650        || resp.status == "completed"
1651        || resp.status == "in_progress"
1652        || resp.status == "needs_review"
1653        || push_denied
1654    {
1655        run_git_ops(
1656            &ctx,
1657            wt,
1658            task_title,
1659            &resp,
1660            agent_name,
1661            model_name,
1662            new_attempts,
1663        )
1664        .await
1665    } else {
1666        GitOpsResult::default()
1667    };
1668
1669    // The agent's only blocker was the denied push and orch pushed for it, so
1670    // the task continues as if the agent had returned done
1671    if push_denied && (git.has_pr || git.has_pushed) {
1672        tracing::info!(
1673            task_id,
1674            "agent blocked on denied push, orch pushed the branch"
1675        );
1676        resp.status = "done".to_string();
1677    }
1678
1679    let screenshot_note = if git.has_pr {
1680        post_screenshots(&ctx, &wt.work_dir, &resp).await
1681    } else {
1682        None
1683    };
1684
1685    // Store delegations in store if present (processed by run_with_context)
1686    if !resp.delegations.is_empty() {
1687        ctx.set(&[("delegations", serde_json::json!(resp.delegations))])
1688            .await;
1689    }
1690
1691    // Store result in task store
1692    // If agent said "done" and a PR exists, send to review before merge.
1693    // If agent said "done", pushed commits, but PR creation failed — review gate creates PR.
1694    // If agent said "done", no PR, and no delegations — work is complete
1695    // (e.g., review/analysis jobs that create issues but no code changes).
1696    // If agent said "done", no PR, but has delegations — blocked on children.
1697    let has_delegations = !resp.delegations.is_empty();
1698
1699    // Detect push failure, increment counters.
1700    let push_state = detect_push_failure_state(&ctx, git.has_commits, git.has_pushed).await;
1701
1702    // Determine whether this external task can be marked done without a PR.
1703    // External tasks always require a PR before reaching `done` — unless
1704    // commits were successfully pushed (in which case the `has_pushed` branch
1705    // handles routing). The `is_non_code_task` heuristic was removed because
1706    // it relied on agent output and could be fooled: an agent claiming
1707    // "already implemented" or "config-only" would match non-code keywords
1708    // and close the issue without verification.
1709    let is_external = !task_id.starts_with("internal:");
1710    let requires_pr = is_external && !git.has_pr && !resp.status.starts_with("needs_review");
1711
1712    // Preferred config key `workflow.max_reroute_attempts`; fall back to
1713    // `workflow.max_attempts` for backwards compatibility.
1714    let max_reroutes: u32 = config::get("workflow.max_reroute_attempts")
1715        .ok()
1716        .and_then(|s| s.parse().ok())
1717        .or_else(|| {
1718            config::get("workflow.max_attempts")
1719                .ok()
1720                .and_then(|s| s.parse().ok())
1721        })
1722        .unwrap_or(3);
1723
1724    // Determine whether we are entering the no-code-reroute branch (all
1725    // earlier chain conditions are false + done + requires_pr).
1726    let is_no_code_reroute = !push_state.is_workflow_scope_failure
1727        && !push_state.push_failed
1728        && (resp.status == "done" || resp.status == "completed")
1729        && !has_delegations
1730        && !git.has_pushed
1731        && requires_pr;
1732
1733    // A conflict reroute with nothing ahead of the default branch is a landed
1734    // fix, not a no-code run: close the superseded PR and mark the task done.
1735    let superseded_pr = match (is_no_code_reroute, ctx.get_task().await) {
1736        (true, Some(task)) => {
1737            superseded_conflict_pr(task.merge_conflict_retries, task.pr_number, git.has_commits)
1738        }
1739        _ => None,
1740    };
1741    let is_no_code_reroute = is_no_code_reroute && superseded_pr.is_none();
1742
1743    let no_code_reroutes =
1744        detect_no_code_reroutes(&ctx, is_no_code_reroute, new_attempts, max_reroutes).await;
1745
1746    let blocked_reason = if resp.status == "blocked" {
1747        Some(agent_blocked_reason(&resp))
1748    } else {
1749        None
1750    };
1751    let is_retryable_blocked = blocked_reason
1752        .as_deref()
1753        .is_some_and(is_retryable_blocked_reason);
1754
1755    // ── Detect same-agent loop ───────────────────────────────────────────────
1756    //
1757    // If this is a no-code reroute and the agent is the same as the one that
1758    // produced the previous no-code result, flag it for blocking (#2410, #2686).
1759    let is_same_agent = if is_no_code_reroute {
1760        let prev_no_code_agent = ctx
1761            .get_task()
1762            .await
1763            .map(|t| t.no_code_last_agent)
1764            .unwrap_or_default();
1765        !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name
1766    } else {
1767        false
1768    };
1769
1770    // ── Pure status decision ─────────────────────────────────────────────────
1771
1772    let final_status_owned = classify_final_status(&DecisionInput {
1773        agent_status: &resp.status,
1774        is_workflow_scope_failure: push_state.is_workflow_scope_failure,
1775        is_rebase_conflict_failure: push_state.is_rebase_conflict_failure,
1776        push_failed: push_state.push_failed,
1777        push_failures: push_state.push_failures,
1778        is_transient_push_failure: push_state.is_transient_failure,
1779        has_pr: git.has_pr,
1780        has_delegations,
1781        has_pushed: git.has_pushed,
1782        is_pr_base_invalid: git.pr_base_invalid,
1783        requires_pr,
1784        no_code_reroutes,
1785        max_reroutes,
1786        is_same_agent,
1787        is_completed_status: resp.status == "completed",
1788        is_retryable_blocked,
1789        is_superseded_conflict: superseded_pr.is_some(),
1790    });
1791    let final_status = final_status_owned.as_str();
1792
1793    // ── Post-decision side effects (tracing + store updates) ─────────────────
1794
1795    apply_post_decision_effects(
1796        &ctx,
1797        final_status,
1798        &resp.status,
1799        &push_state,
1800        is_no_code_reroute,
1801        git.has_pr,
1802        git.has_pushed,
1803        git.pr_base_invalid,
1804        has_delegations,
1805        is_external,
1806        no_code_reroutes,
1807        max_reroutes,
1808        agent_name,
1809    )
1810    .await;
1811
1812    // When the agent explicitly returned "blocked", persist its explanation into
1813    // last_error so `orch task get` surfaces the reason without requiring manual
1814    // inspection of task_runs.parsed_response.  The apply_post_decision_effects
1815    // branches above only fire on push/no-code scenarios; a raw agent-blocked
1816    // response falls through all of them and would leave last_error empty.
1817    if resp.status == "blocked" {
1818        let reason = blocked_reason.unwrap_or_else(|| agent_blocked_reason(&resp));
1819        if final_status == "new" {
1820            // Retryable external block: cooldown failed agent/model, clear selection,
1821            // and reroute after backoff instead of permanently blocking.
1822            cooldown::record_agent_failure_with_message(agent_name, &reason).await;
1823            if let Some(m) = model_name {
1824                cooldown::record_model_failure(agent_name, m).await;
1825            }
1826            ctx.set(&[
1827                (
1828                    "last_error",
1829                    serde_json::json!(format!(
1830                        "transient blocker (auto-retry scheduled): {reason}"
1831                    )),
1832                ),
1833                ("agent", serde_json::json!(null)),
1834                ("model", serde_json::json!(null)),
1835            ])
1836            .await;
1837            tracing::warn!(
1838                task_id = ctx.task_id,
1839                reason = %reason,
1840                "agent returned retryable blocked status — rerouting as new with cooldown"
1841            );
1842        } else if final_status == "blocked" {
1843            ctx.set(&[("last_error", serde_json::json!(reason))]).await;
1844        }
1845    }
1846
1847    if let Some(pr_number) = superseded_pr {
1848        tracing::info!(
1849            task_id = ctx.task_id,
1850            pr_number,
1851            "conflict reroute found the fix already on the default branch — closing superseded PR"
1852        );
1853        close_superseded_pr(&ctx, pr_number).await;
1854    }
1855
1856    // The summary reaches the channels, so a missing skill or browser goes with it
1857    let summary = match &screenshot_note {
1858        Some(note) => format!("{}\n\nScreenshots: {note}", resp.summary),
1859        None => resp.summary.clone(),
1860    };
1861    ctx.set(&[("summary", serde_json::json!(summary))]).await;
1862
1863    // Store token usage — prefer agent-parsed tokens, fall back to response
1864    store_token_usage(
1865        &ctx,
1866        input_tokens.or(resp.input_tokens),
1867        output_tokens.or(resp.output_tokens),
1868        cost_usd,
1869    )
1870    .await;
1871
1872    // Store learnings for memory (for future retries)
1873    response::store_learnings_from_response(
1874        task_id,
1875        new_attempts,
1876        agent_name,
1877        model_name,
1878        &resp,
1879        resp.error.as_deref(),
1880        store,
1881        repo,
1882        raw_stdout,
1883    )
1884    .await;
1885
1886    // Note: done → in_review transition is handled by the engine
1887    // after triggering the review agent (engine/mod.rs)
1888    Ok((final_status.to_string(), push_state.push_failed))
1889}
1890
1891/// Labels that indicate a task is non-code and can be marked done without a PR.
1892#[cfg(test)]
1893mod tests;