Skip to main content

orch/engine/runner/
response_handler.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, 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, or `PushResult::NoCommits` if the branch has no
421/// commits ahead of the default branch
422enum PushResult {
423    Success,
424    Failed,
425    NoCommits,       // agent made no code changes
426    WorktreeMissing, // retry after rebuilding missing worktree
427}
428
429fn is_missing_path_error(err: &anyhow::Error) -> bool {
430    err.chain().any(|cause| {
431        cause
432            .downcast_ref::<io::Error>()
433            .is_some_and(|ioe| ioe.kind() == ErrorKind::NotFound)
434    }) || err
435        .to_string()
436        .to_ascii_lowercase()
437        .contains("no such file or directory")
438}
439
440async fn recover_missing_worktree_for_push(
441    ctx: &StoreCtx<'_>,
442    wt: &worktree::WorktreeSetup,
443    task_title: &str,
444    agent_name: &str,
445    model_name: Option<&str>,
446) -> anyhow::Result<worktree::WorktreeSetup> {
447    tracing::warn!(
448        task_id = ctx.task_id,
449        worktree = %wt.work_dir.display(),
450        branch = wt.branch,
451        "worktree missing during push; attempting one-time recovery"
452    );
453    ctx.append_activity(
454        "push_recover_worktree",
455        Some(agent_name),
456        model_name,
457        Some(&serde_json::json!({
458            "status": "start",
459            "worktree": wt.work_dir.display().to_string(),
460            "branch": wt.branch,
461        })),
462    )
463    .await;
464
465    let recovered = worktree::setup_worktree(
466        ctx.task_id,
467        task_title,
468        &wt.main_project_dir,
469        ctx.store,
470        ctx.repo,
471    )
472    .await?;
473
474    if !recovered.work_dir.exists() {
475        anyhow::bail!(
476            "recovered worktree path is still missing: {}",
477            recovered.work_dir.display()
478        );
479    }
480
481    ctx.append_activity(
482        "push_recover_worktree",
483        Some(agent_name),
484        model_name,
485        Some(&serde_json::json!({
486            "status": "ok",
487            "worktree": recovered.work_dir.display().to_string(),
488            "branch": recovered.branch,
489        })),
490    )
491    .await;
492
493    Ok(recovered)
494}
495
496/// Push a branch that was just rebased onto its moved remote counterpart.
497///
498/// Separate from the first push attempt so a failure here is reported as orch's own
499/// push failure rather than re-entering the diverged-branch recovery
500async fn push_after_rebase(
501    ctx: &StoreCtx<'_>,
502    work_dir: &Path,
503    branch: &str,
504    default_branch: &str,
505    agent_name: &str,
506    model_name: Option<&str>,
507) -> PushResult {
508    match git_ops::push_branch(work_dir, branch, default_branch).await {
509        Ok(_) => {
510            ctx.append_activity(
511                "push",
512                Some(agent_name),
513                model_name,
514                Some(&serde_json::json!({
515                    "status": "ok",
516                    "branch": branch,
517                    "default_branch": default_branch,
518                    "reason": "pushed_after_rebase",
519                })),
520            )
521            .await;
522            ctx.set(&[
523                ("last_error", serde_json::json!("")),
524                ("push_failures", serde_json::json!(0)),
525            ])
526            .await;
527            PushResult::Success
528        }
529        Err(e) => {
530            tracing::error!(
531                task_id = ctx.task_id,
532                branch = branch,
533                error = ?e,
534                "push failed again after rebasing on the remote branch"
535            );
536            ctx.append_activity(
537                "push",
538                Some(agent_name),
539                model_name,
540                Some(&serde_json::json!({
541                    "status": "error",
542                    "branch": branch,
543                    "default_branch": default_branch,
544                    "reason": "pushed_after_rebase",
545                    "error": e.to_string(),
546                })),
547            )
548            .await;
549            ctx.set(&[(
550                "last_error",
551                serde_json::json!(format!("push failed after rebase on remote branch: {e}")),
552            )])
553            .await;
554            PushResult::Failed
555        }
556    }
557}
558
559async fn push_branch_with_log(
560    ctx: &StoreCtx<'_>,
561    work_dir: &Path,
562    branch: &str,
563    default_branch: &str,
564    agent_name: &str,
565    model_name: Option<&str>,
566) -> PushResult {
567    match git_ops::push_branch(work_dir, branch, default_branch).await {
568        Ok(_) => {
569            ctx.append_activity(
570                "push",
571                Some(agent_name),
572                model_name,
573                Some(&serde_json::json!({
574                    "status": "ok",
575                    "branch": branch,
576                    "default_branch": default_branch,
577                })),
578            )
579            .await;
580            // Clear any stale push failure from a previous run so review_and_merge
581            // does not incorrectly block an approved task.
582            ctx.set(&[
583                ("last_error", serde_json::json!("")),
584                ("push_failures", serde_json::json!(0)),
585            ])
586            .await;
587            PushResult::Success
588        }
589        Err(e) => {
590            let err_str = e.to_string();
591            tracing::error!(task_id = ctx.task_id, error = ?e, "push failed");
592
593            if is_missing_path_error(&e) && !work_dir.exists() {
594                tracing::warn!(
595                    task_id = ctx.task_id,
596                    worktree = %work_dir.display(),
597                    "push failed because worktree path disappeared; will attempt recovery"
598                );
599                ctx.append_activity(
600                    "push",
601                    Some(agent_name),
602                    model_name,
603                    Some(&serde_json::json!({
604                        "status": "retry",
605                        "branch": branch,
606                        "default_branch": default_branch,
607                        "reason": "worktree_missing",
608                        "error": err_str,
609                    })),
610                )
611                .await;
612                return PushResult::WorktreeMissing;
613            }
614
615            // Check if this is a non-fast-forward error that we can recover from.
616            if push_error_is_diverged(&err_str) {
617                tracing::info!(
618                    task_id = ctx.task_id,
619                    branch = branch,
620                    "attempting fetch and rebase to recover from diverged branch"
621                );
622
623                // Attempt to fetch and rebase on origin/{branch}.
624                match git_ops::rebase_on_branch(work_dir, branch).await {
625                    Ok(true) => {
626                        // Pushing is orch's step, not the agent's: the commits are already
627                        // written, so push them onto the moved remote branch here. Re-routing
628                        // instead re-ran a finished task until it hit the attempt limit (#3783)
629                        tracing::info!(
630                            task_id = ctx.task_id,
631                            branch = branch,
632                            "rebase succeeded — retrying the push"
633                        );
634                        ctx.append_activity(
635                            "push_rebased",
636                            Some(agent_name),
637                            model_name,
638                            Some(&serde_json::json!({
639                                "status": "rebased",
640                                "branch": branch,
641                                "reason": "diverged_from_remote",
642                            })),
643                        )
644                        .await;
645                        return push_after_rebase(
646                            ctx,
647                            work_dir,
648                            branch,
649                            default_branch,
650                            agent_name,
651                            model_name,
652                        )
653                        .await;
654                    }
655                    Ok(false) => {
656                        // The remote branch already holds every local commit, nothing to push
657                        tracing::info!(
658                            task_id = ctx.task_id,
659                            branch = branch,
660                            "no local commits after fetch — marking as success"
661                        );
662                        ctx.append_activity(
663                            "push",
664                            Some(agent_name),
665                            model_name,
666                            Some(&serde_json::json!({
667                                "status": "ok",
668                                "branch": branch,
669                                "reason": "no_commits_after_fetch",
670                            })),
671                        )
672                        .await;
673                        ctx.set(&[
674                            ("last_error", serde_json::json!("")),
675                            ("push_failures", serde_json::json!(0)),
676                        ])
677                        .await;
678                        return PushResult::Success;
679                    }
680                    Err(rebase_err) if git_ops::is_missing_remote_ref_error(&rebase_err) => {
681                        // The branch never reached the remote, so there is nothing to rebase onto.
682                        // Take the counted retry path instead of the rebase conflict block
683                        tracing::warn!(
684                            task_id = ctx.task_id,
685                            branch = branch,
686                            "remote branch does not exist, skipping rebase recovery"
687                        );
688                        ctx.append_activity(
689                            "push",
690                            Some(agent_name),
691                            model_name,
692                            Some(&serde_json::json!({
693                                "status": "error",
694                                "branch": branch,
695                                "default_branch": default_branch,
696                                "error": err_str,
697                            })),
698                        )
699                        .await;
700                        ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
701                            .await;
702                        return PushResult::Failed;
703                    }
704                    Err(rebase_err) => {
705                        // Rebase failed (likely conflicts) — block for human intervention.
706                        tracing::error!(
707                            task_id = ctx.task_id,
708                            branch = branch,
709                            error = ?rebase_err,
710                            "rebase failed — blocking task for human intervention"
711                        );
712                        ctx.append_activity(
713                            "push_rebased",
714                            Some(agent_name),
715                            model_name,
716                            Some(&serde_json::json!({
717                                "status": "error",
718                                "branch": branch,
719                                "error": rebase_err.to_string(),
720                            })),
721                        )
722                        .await;
723                        ctx.set(&[(
724                            "last_error",
725                            serde_json::json!(format!(
726                                "push failed and rebase failed: {rebase_err} — manual resolution required"
727                            )),
728                        )])
729                        .await;
730                        return PushResult::Failed;
731                    }
732                }
733            }
734
735            // Non-recoverable push error — log and return failure.
736            ctx.append_activity(
737                "push",
738                Some(agent_name),
739                model_name,
740                Some(&serde_json::json!({
741                    "status": "error",
742                    "branch": branch,
743                    "default_branch": default_branch,
744                    "error": err_str,
745                })),
746            )
747            .await;
748            ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
749                .await;
750            PushResult::Failed
751        }
752    }
753}
754
755/// True when a push error means the remote branch moved. `[remote rejected]` is a server side
756/// failure, only `! [rejected]` marks a divergence
757fn push_error_is_diverged(err_str: &str) -> bool {
758    err_str.contains("non-fast-forward")
759        || err_str.contains("! [rejected]")
760        || err_str.contains("fetch first")
761        || err_str.contains("behind")
762}
763
764/// True when the GitHub client refused the call locally because its rate-limit window is active.
765fn is_local_rate_limit_skip(err_str: &str) -> bool {
766    err_str.contains("rate-limited, skipping call")
767}
768
769/// Create (or find an existing) PR and log the activity.
770///
771/// Returns `(has_pr, has_pushed, pr_base_invalid)`. `has_pushed` may be set to
772/// `false` when a 422 "no commits" GitHub error indicates the branch was
773/// already merged. `pr_base_invalid` is set when the base branch is rejected
774/// by GitHub (even after the API fallback), which is a terminal condition.
775async fn create_pr_with_log(
776    ctx: &StoreCtx<'_>,
777    wt: &worktree::WorktreeSetup,
778    task_title: &str,
779    resp: &AgentResponse,
780    agent_name: &str,
781    model_name: Option<&str>,
782    mut has_pushed: bool,
783) -> (bool, bool, bool) {
784    match git_ops::create_pr_if_needed(
785        &wt.work_dir,
786        &wt.branch,
787        task_title,
788        &resp.summary,
789        &resp.accomplished,
790        &resp.remaining,
791        &resp.files,
792        ctx.task_id,
793        agent_name,
794        model_name,
795        ctx.repo,
796        &wt.default_branch,
797    )
798    .await
799    {
800        Ok(ref url) => {
801            // Save pr_number to the store so the review gate can find it
802            // immediately without racing GitHub's list-API cache (~300 ms lag).
803            // This is set for both newly-created and pre-existing PRs.
804            match review::parse_pr_number_from_url(url) {
805                Ok(pr_num) => {
806                    if let Err(e) = store::store_set_result(
807                        ctx.store,
808                        ctx.repo,
809                        ctx.task_id,
810                        &[("pr_number", serde_json::json!(pr_num))],
811                    )
812                    .await
813                    {
814                        tracing::error!(
815                            task_id = ctx.task_id,
816                            pr_url = %url,
817                            err = %e,
818                            "CRITICAL: failed to save pr_number to store — downstream review gate may trigger duplicate gh pr create"
819                        );
820                    }
821                }
822                Err(e) => {
823                    tracing::warn!(
824                        task_id = ctx.task_id,
825                        pr_url = %url,
826                        error = %e,
827                        "PR created but failed to parse PR number from URL"
828                    );
829                }
830            }
831            ctx.append_activity(
832                "pr_create",
833                Some(agent_name),
834                model_name,
835                Some(&serde_json::json!({"status": "created", "url": url})),
836            )
837            .await;
838            (true, has_pushed, false)
839        }
840        Err(e) => {
841            let err_str = format!("{e}");
842            tracing::error!(task_id = ctx.task_id, error = ?e, "create PR failed");
843            ctx.append_activity(
844                "pr_create",
845                Some(agent_name),
846                model_name,
847                Some(&serde_json::json!({
848                    "status": "error",
849                    "branch": wt.branch,
850                    "error": err_str.clone(),
851                })),
852            )
853            .await;
854            ctx.set(&[(
855                "last_error",
856                serde_json::json!(format!("create PR failed: {e}")),
857            )])
858            .await;
859            // Distinguish between terminal 422 errors and recoverable ones.
860            // - "No commits between" / "head invalid" → agent made no code changes
861            //   or the branch was already merged.  Clearing has_pushed lets the
862            //   task fall through to the "done" path.
863            // - "base invalid" → the base branch name is wrong.  After the
864            //   GitHub API fallback in create_pr_if_needed also fails, this is a
865            //   terminal configuration error.  We set pr_base_invalid so the
866            //   classifier blocks the task instead of re-dispatching forever.
867            // A GitHub auth error is terminal too: the commits are pushed, so only a token fix helps
868            let mut pr_base_invalid = false;
869            if is_local_rate_limit_skip(&err_str) {
870                // No request was sent and the branch is pushed. Re-running the agent cannot help,
871                // so report the PR as pending and let the review gate create it once calls resume
872                tracing::warn!(
873                    task_id = ctx.task_id,
874                    "PR creation skipped by local GitHub rate limit — review gate will create the PR"
875                );
876                return (true, has_pushed, false);
877            }
878            if agents::patterns::detect_auth_error(&err_str).is_some() {
879                tracing::error!(
880                    task_id = ctx.task_id,
881                    "PR creation failed with GitHub auth error — blocking task"
882                );
883                pr_base_invalid = true;
884            } else if err_str.contains("422") {
885                if err_str.contains("No commits between") || err_str.contains("head") {
886                    tracing::info!(
887                        task_id = ctx.task_id,
888                        "PR creation returned 422/no-commits/head-invalid — agent made no code changes or branch merged, marking done"
889                    );
890                    has_pushed = false;
891                } else if git_ops::is_invalid_base_error(&err_str) {
892                    tracing::error!(
893                        task_id = ctx.task_id,
894                        "PR creation failed with invalid base branch — blocking task"
895                    );
896                    pr_base_invalid = true;
897                }
898            }
899            (false, has_pushed, pr_base_invalid)
900        }
901    }
902}
903
904/// Run the full auto-commit → push → PR pipeline.
905async fn run_git_ops(
906    ctx: &StoreCtx<'_>,
907    wt: &worktree::WorktreeSetup,
908    task_title: &str,
909    resp: &AgentResponse,
910    agent_name: &str,
911    model_name: Option<&str>,
912    new_attempts: u32,
913) -> GitOpsResult {
914    auto_commit_changes(ctx, &wt.work_dir, task_title, agent_name, new_attempts).await;
915
916    let mut has_commits =
917        check_commits_and_clear_stale_errors(ctx, &wt.work_dir, &wt.default_branch).await;
918
919    // Skip push + PR if there are no commits ahead of the default branch.
920    // No-op tasks (e.g. "nothing to execute") produce no commits, so pushing
921    // and creating a PR would just waste API calls and trigger 422 errors.
922    let push_result = if has_commits {
923        push_branch_with_log(
924            ctx,
925            &wt.work_dir,
926            &wt.branch,
927            &wt.default_branch,
928            agent_name,
929            model_name,
930        )
931        .await
932    } else {
933        store::store_log_activity(
934            ctx.store,
935            ctx.repo,
936            ctx.task_id,
937            "push",
938            None,
939            None,
940            Some(agent_name),
941            model_name,
942            Some(&serde_json::json!({
943                "status": "skipped",
944                "reason": "no_commits_ahead",
945                "branch": wt.branch,
946                "default_branch": wt.default_branch,
947            })),
948        )
949        .await;
950        PushResult::NoCommits
951    };
952    let push_result = if matches!(push_result, PushResult::WorktreeMissing) {
953        match recover_missing_worktree_for_push(ctx, wt, task_title, agent_name, model_name).await {
954            Ok(recovered) => {
955                let wt_for_ops = &recovered;
956                has_commits = check_commits_and_clear_stale_errors(
957                    ctx,
958                    &wt_for_ops.work_dir,
959                    &wt_for_ops.default_branch,
960                )
961                .await;
962                if has_commits {
963                    push_branch_with_log(
964                        ctx,
965                        &wt_for_ops.work_dir,
966                        &wt_for_ops.branch,
967                        &wt_for_ops.default_branch,
968                        agent_name,
969                        model_name,
970                    )
971                    .await
972                } else {
973                    PushResult::NoCommits
974                }
975            }
976            Err(e) => {
977                tracing::error!(
978                    task_id = ctx.task_id,
979                    err = %e,
980                    "worktree recovery before push failed"
981                );
982                ctx.append_activity(
983                    "push_recover_worktree",
984                    Some(agent_name),
985                    model_name,
986                    Some(&serde_json::json!({
987                        "status": "error",
988                        "worktree": wt.work_dir.display().to_string(),
989                        "branch": wt.branch,
990                        "error": e.to_string(),
991                    })),
992                )
993                .await;
994                ctx.set(&[(
995                    "last_error",
996                    serde_json::json!(format!(
997                        "push failed: worktree disappeared and recovery failed: {e}"
998                    )),
999                )])
1000                .await;
1001                PushResult::Failed
1002            }
1003        }
1004    } else {
1005        push_result
1006    };
1007
1008    let mut has_pushed = matches!(push_result, PushResult::Success);
1009    let mut has_pr = false;
1010
1011    // Create PR (skip if push failed or repo is unknown)
1012    if has_commits && matches!(push_result, PushResult::Failed) {
1013        tracing::warn!(
1014            task_id = ctx.task_id,
1015            "skipping PR creation due to push failure"
1016        );
1017    } else if matches!(push_result, PushResult::NoCommits) {
1018        tracing::info!(
1019            task_id = ctx.task_id,
1020            "no commits ahead of default branch, skipping push + PR"
1021        );
1022    } else if !has_pushed {
1023        // no commits — already logged "no commits ahead, skipping push + PR" at INFO level above
1024    } else if ctx.repo.is_empty() {
1025        tracing::warn!(
1026            task_id = ctx.task_id,
1027            "skipping PR creation — repo is empty (internal task?)"
1028        );
1029    } else {
1030        let (pr, updated_pushed, pr_base_invalid) = create_pr_with_log(
1031            ctx, wt, task_title, resp, agent_name, model_name, has_pushed,
1032        )
1033        .await;
1034        has_pr = pr;
1035        has_pushed = updated_pushed;
1036        return GitOpsResult {
1037            has_pr,
1038            has_pushed,
1039            has_commits,
1040            pr_base_invalid,
1041        };
1042    }
1043
1044    GitOpsResult {
1045        has_pr,
1046        has_pushed,
1047        has_commits,
1048        pr_base_invalid: false,
1049    }
1050}
1051
1052// ── Private status-computation helpers ───────────────────────────────────────
1053
1054/// Comment on and close a PR whose change already landed elsewhere.
1055async fn close_superseded_pr(ctx: &StoreCtx<'_>, pr_number: u64) {
1056    if ctx.repo.is_empty() {
1057        return;
1058    }
1059    match GhHttp::new() {
1060        Ok(gh) => {
1061            let note = "Closing as superseded: the change is already on the default branch, so this branch has no commits ahead of it.";
1062            if let Err(e) = gh.add_comment(ctx.repo, &pr_number.to_string(), note).await {
1063                tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to comment on superseded PR");
1064            }
1065            if let Err(e) = gh.close_pr(ctx.repo, pr_number).await {
1066                tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to close superseded PR");
1067            }
1068        }
1069        Err(e) => {
1070            tracing::warn!(task_id = ctx.task_id, err = %e, "cannot close superseded PR");
1071        }
1072    }
1073    // Dropping the PR lets the blocker refresh read the closed, completed issue with no PR
1074    ctx.set(&[
1075        (
1076            "last_error",
1077            serde_json::json!(
1078                "PR closed as superseded: the fix already landed on the default branch"
1079            ),
1080        ),
1081        ("pr_number", serde_json::json!(null)),
1082    ])
1083    .await;
1084}
1085
1086/// The PR to close when a merge-conflict reroute produced no commits.
1087///
1088/// `merge_conflict_retries > 0` means review_poll sent the task back to resolve a
1089/// conflict. Coming back with nothing ahead of the default branch means another
1090/// PR already landed the same fix, so the open PR is superseded (#3780)
1091fn superseded_conflict_pr(
1092    merge_conflict_retries: i32,
1093    pr_number: Option<i32>,
1094    has_commits: bool,
1095) -> Option<u64> {
1096    if has_commits || merge_conflict_retries <= 0 {
1097        return None;
1098    }
1099    pr_number
1100        .filter(|n| *n > 0)
1101        .and_then(|n| u64::try_from(n).ok())
1102}
1103
1104/// Read push-failure state from the store and increment `push_failures` if needed.
1105async fn detect_push_failure_state(
1106    ctx: &StoreCtx<'_>,
1107    has_commits: bool,
1108    has_pushed: bool,
1109) -> PushFailureState {
1110    // Load stored task once to inspect last_error (avoid repeated DB reads).
1111    let stored_last_error = ctx
1112        .get_task()
1113        .await
1114        .map(|t| t.last_error)
1115        .unwrap_or_default();
1116
1117    let push_failed = !has_pushed && has_commits && stored_last_error.contains("push failed");
1118
1119    // Detect workflow scope errors — these are non-retryable token permission issues.
1120    // Rerouting to a different agent won't help since the same token is used.
1121    let is_workflow_scope_failure = push_failed
1122        && (stored_last_error.contains("workflow` scope")
1123            || stored_last_error.contains("workflow' scope")
1124            || (stored_last_error.contains("refusing to allow")
1125                && stored_last_error.contains("workflow")));
1126
1127    let is_rebase_conflict_failure =
1128        push_failed && stored_last_error.contains("push failed and rebase failed:");
1129
1130    let is_transient_failure = push_failed
1131        && !is_workflow_scope_failure
1132        && !is_rebase_conflict_failure
1133        && git_ops::is_transient_network_error(&stored_last_error);
1134
1135    // Track push failures — block after 3 consecutive failures.
1136    // Workflow-scope failures are non-retryable and blocked immediately; do not
1137    // increment this retry counter for them so that later normal push failures
1138    // are not prematurely blocked.
1139    let push_failures = if push_failed && !is_workflow_scope_failure && !is_rebase_conflict_failure
1140    {
1141        match ctx.increment("push_failures").await {
1142            Ok(v) => v,
1143            Err(e) => {
1144                tracing::warn!(
1145                    task_id = ctx.task_id,
1146                    err = %e,
1147                    "failed to increment push_failures — reading current value from store"
1148                );
1149                // On DB error, read current value to ensure blocking threshold is still reachable
1150                ctx.get_task()
1151                    .await
1152                    .map_or(1, |t| u64::try_from(t.push_failures).unwrap_or(0) + 1)
1153                // assume at least 1 if we can't read either
1154            }
1155        }
1156    } else {
1157        0
1158    };
1159
1160    PushFailureState {
1161        push_failed,
1162        is_workflow_scope_failure,
1163        is_rebase_conflict_failure,
1164        is_transient_failure,
1165        push_failures,
1166        stored_last_error,
1167    }
1168}
1169
1170/// Increment `no_code_reroutes` and log a warning when the agent completed
1171/// without producing code changes on an external task requiring a PR.
1172async fn detect_no_code_reroutes(
1173    ctx: &StoreCtx<'_>,
1174    is_no_code_reroute: bool,
1175    new_attempts: u32,
1176    max_reroutes: u32,
1177) -> u64 {
1178    if !is_no_code_reroute {
1179        return 0;
1180    }
1181    tracing::warn!(
1182        task_id = ctx.task_id,
1183        attempts = new_attempts,
1184        max_reroutes,
1185        "agent reported done but produced no code changes on external task requiring PR"
1186    );
1187    match ctx.increment("no_code_reroutes").await {
1188        Ok(v) => v,
1189        Err(e) => {
1190            tracing::warn!(
1191                task_id = ctx.task_id,
1192                err = %e,
1193                "failed to increment no_code_reroutes — reading current value from store"
1194            );
1195            // On DB error, read current value to ensure blocking threshold is still reachable
1196            ctx.get_task()
1197                .await
1198                .map_or(1, |t| u64::try_from(t.no_code_reroutes).unwrap_or(0) + 1)
1199            // assume at least 1 if we can't read either
1200        }
1201    }
1202}
1203
1204// ── Post-decision side-effect helpers ─────────────────────────────────────────
1205
1206/// Build a human-readable reason string from a "blocked" agent response.
1207///
1208/// Priority: explicit `error` field → `summary` → fallback.
1209/// Appends `remaining` items if present so operators know what was left to do.
1210fn agent_blocked_reason(resp: &AgentResponse) -> String {
1211    let summary_opt = if resp.summary.is_empty() {
1212        None
1213    } else {
1214        Some(resp.summary.as_str())
1215    };
1216    let base = resp
1217        .error
1218        .as_deref()
1219        .filter(|s| !s.is_empty())
1220        .or(summary_opt)
1221        .unwrap_or("agent returned blocked status without a reason");
1222    if resp.remaining.is_empty() {
1223        base.to_string()
1224    } else {
1225        format!("{base}. Remaining: {}", resp.remaining.join("; "))
1226    }
1227}
1228
1229/// True when a blocked response names a denied `git push` and nothing else as the blocker.
1230fn is_denied_push_block(resp: &AgentResponse) -> bool {
1231    let r = agent_blocked_reason(resp).to_lowercase();
1232    r.contains("push")
1233        && [
1234            "denied",
1235            "not allowed",
1236            "permission",
1237            "disallowed",
1238            "approval",
1239        ]
1240        .iter()
1241        .any(|p| r.contains(p))
1242}
1243
1244/// Heuristic classifier for agent-returned blocked reasons.
1245///
1246/// Returns true when the reason looks transient/retryable (network/provider/
1247/// auth-temporary/environment availability issues), and false for likely
1248/// permanent/input/configuration issues.
1249fn is_retryable_blocked_reason(reason: &str) -> bool {
1250    let r = reason.to_lowercase();
1251
1252    let permanent_patterns = [
1253        "not found",
1254        "does not exist",
1255        "invalid",
1256        "malformed",
1257        "syntax error",
1258        "unsupported",
1259        "unimplemented",
1260        "manual intervention required",
1261        "human intervention required",
1262    ];
1263    if permanent_patterns.iter().any(|p| r.contains(p)) {
1264        return false;
1265    }
1266
1267    let transient_patterns = [
1268        "unavailable",
1269        "temporarily unavailable",
1270        "timeout",
1271        "timed out",
1272        "connection reset",
1273        "connection refused",
1274        "network error",
1275        "rate limit",
1276        "try again",
1277        "service down",
1278        "permission denied",
1279        "worktree lock",
1280        "git lock",
1281        "lockfile permission",
1282        "lock permission",
1283        "resource busy",
1284        "could not resolve host",
1285        // Credential providers in headless/tmux sessions can fail transiently
1286        // when GPG agent/passphrase is unavailable.
1287        "gpg decryption fails",
1288        "gpg decryption failed",
1289        "gpg agent",
1290        "no passphrase available",
1291        "passphrase unavailable",
1292        "pass store",
1293        "passwordstoreprovider",
1294        "agentvaultprovider",
1295        "onepasswordprovider",
1296        "decryption failed in this agent session",
1297    ];
1298    transient_patterns.iter().any(|p| r.contains(p))
1299}
1300
1301/// Apply tracing and store writes that depend on the final status decision.
1302#[expect(
1303    clippy::too_many_arguments,
1304    reason = "the decision and its context arrive as separate values"
1305)]
1306async fn apply_post_decision_effects(
1307    ctx: &StoreCtx<'_>,
1308    final_status: &str,
1309    resp_status: &str,
1310    push_state: &PushFailureState,
1311    is_no_code_reroute: bool,
1312    has_pr: bool,
1313    has_pushed: bool,
1314    is_pr_base_invalid: bool,
1315    has_delegations: bool,
1316    is_external: bool,
1317    no_code_reroutes: u64,
1318    max_reroutes: u32,
1319    agent_name: &str,
1320) {
1321    if push_state.is_workflow_scope_failure {
1322        // Non-retryable — block immediately with actionable guidance.
1323        tracing::error!(
1324            task_id = ctx.task_id,
1325            "push failed: token lacks `workflow` OAuth scope — blocking immediately \
1326             (rerouting would not help)"
1327        );
1328        ctx.set(&[(
1329            "last_error",
1330            serde_json::json!(format!(
1331                "push failed: GitHub token lacks `workflow` OAuth scope. \
1332                 The agent modified .github/workflows/ files but the token cannot push them. \
1333                 Fix: add `workflow` scope to your GitHub token, or use a GitHub App for auth. \
1334                 Original error: {}",
1335                push_state.stored_last_error
1336            )),
1337        )])
1338        .await;
1339    } else if push_state.is_rebase_conflict_failure {
1340        tracing::error!(
1341            task_id = ctx.task_id,
1342            "push failed and automatic rebase recovery failed — blocking for human intervention"
1343        );
1344        ctx.set(&[
1345            ("agent", serde_json::json!(null)),
1346            ("model", serde_json::json!(null)),
1347        ])
1348        .await;
1349    } else if push_state.push_failed && final_status == "needs_review" {
1350        // The engine retries the push from the review gate. Keep agent and model, since the
1351        // task never goes back to the router
1352        tracing::warn!(
1353            task_id = ctx.task_id,
1354            push_failures = push_state.push_failures,
1355            "agent done but push failed on a GitHub or network error — engine retries the push"
1356        );
1357    } else if push_state.push_failed {
1358        // Clear agent and model so router picks a different one on reroute (#1604)
1359        ctx.set(&[
1360            ("agent", serde_json::json!(null)),
1361            ("model", serde_json::json!(null)),
1362        ])
1363        .await;
1364        let push_failures = push_state.push_failures;
1365        if push_failures >= 3 {
1366            tracing::error!(
1367                task_id = ctx.task_id,
1368                push_failures,
1369                "push failed {push_failures} times — blocking for human intervention"
1370            );
1371        } else {
1372            tracing::warn!(
1373                task_id = ctx.task_id,
1374                push_failures,
1375                "agent done but push failed ({push_failures}/3) — rerouting to different agent"
1376            );
1377        }
1378    } else if resp_status == "done" && !has_pr && has_delegations {
1379        tracing::info!(
1380            task_id = ctx.task_id,
1381            "agent reported done with delegations but no PR — setting blocked"
1382        );
1383    } else if (resp_status == "done" || resp_status == "completed")
1384        && !has_pr
1385        && has_pushed
1386        && is_pr_base_invalid
1387    {
1388        tracing::error!(
1389            task_id = ctx.task_id,
1390            "PR creation failed with invalid base branch — blocking for human intervention"
1391        );
1392        ctx.set(&[(
1393            "last_error",
1394            serde_json::json!(
1395                "PR creation failed terminally (invalid base branch or GitHub auth error). The agent's commits were pushed successfully. \
1396                 Check the repository default branch and the GitHub token, then create the PR manually or unblock the task."
1397            ),
1398        )])
1399        .await;
1400    } else if resp_status == "done" && !has_pr && has_pushed {
1401        // Push succeeded but PR creation failed after retries.
1402        tracing::warn!(
1403            task_id = ctx.task_id,
1404            "agent done, commits pushed, but PR creation failed — re-dispatching as routed"
1405        );
1406    } else if is_no_code_reroute {
1407        // Record which agent produced this no-code result so the same-agent
1408        // detection on the next run can prevent loop-backs (#2410, #2686).
1409        // Also clear agent/model to force re-routing.
1410
1411        if final_status == "blocked" {
1412            // final_status is "blocked" either because max_reroutes was exhausted
1413            // or because is_same_agent was detected (both cases are handled by
1414            // classify_final_status). Log accordingly.
1415            let prev_no_code_agent = ctx
1416                .get_task()
1417                .await
1418                .map(|t| t.no_code_last_agent)
1419                .unwrap_or_default();
1420            let is_same_agent = !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name;
1421
1422            let msg = if is_same_agent {
1423                tracing::error!(
1424                    task_id = ctx.task_id,
1425                    agent = %agent_name,
1426                    "blocking same-agent no-code loop: agent {} would be selected again",
1427                    agent_name
1428                );
1429                format!(
1430                    "agent {agent_name} completed without code changes twice — same-agent loop detected, blocking for human review"
1431                )
1432            } else {
1433                tracing::error!(
1434                    task_id = ctx.task_id,
1435                    no_code_reroutes,
1436                    max_reroutes,
1437                    "blocking no-code reroute: reached max reroute attempts ({}/{}) for no-code-result",
1438                    no_code_reroutes,
1439                    max_reroutes
1440                );
1441                format!(
1442                    "agent completed without code changes after {no_code_reroutes}/{max_reroutes} reroute attempts on external task requiring PR"
1443                )
1444            };
1445            ctx.set(&[("last_error", serde_json::json!(msg))]).await;
1446        } else {
1447            // Rerouting — no blocking error to record yet.
1448            ctx.set(&[(
1449                "last_error",
1450                serde_json::json!(
1451                    "agent completed without code changes on external task requiring PR"
1452                ),
1453            )])
1454            .await;
1455        }
1456
1457        // Always record which agent produced this no-code result so the router can
1458        // skip it on the next attempt. Also clear agent/model to force re-routing
1459        // (the router will pick a fresh agent, which will be caught by the same-agent
1460        // check on the next round if it's the same agent).
1461        ctx.set(&[
1462            ("no_code_last_agent", serde_json::json!(agent_name)),
1463            ("agent", serde_json::json!(null)),
1464            ("model", serde_json::json!(null)),
1465        ])
1466        .await;
1467    } else if resp_status == "done" && !has_pr && is_external {
1468        // External task with non-code labels (e.g. documentation, research) —
1469        // allowed to be marked done without a PR.
1470        tracing::info!(
1471            task_id = ctx.task_id,
1472            "external task with non-code labels reported done — marking done without PR"
1473        );
1474    } else if resp_status == "done" && !has_pr {
1475        tracing::info!(
1476            task_id = ctx.task_id,
1477            "internal task reported done with no PR — marking done"
1478        );
1479    }
1480}
1481
1482/// Store token usage if both input and output token counts are known, with the cost the agent
1483/// reported for the run.
1484async fn store_token_usage(
1485    ctx: &StoreCtx<'_>,
1486    input_tokens: Option<u64>,
1487    output_tokens: Option<u64>,
1488    cost_usd: Option<f64>,
1489) {
1490    let (Some(input), Some(output)) = (input_tokens, output_tokens) else {
1491        return;
1492    };
1493    if let Some(st) = ctx.store {
1494        if let Some(store_id) = ctx.store_id_opt {
1495            if let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await {
1496                tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1497            }
1498        } else if let Ok(Some(store_id)) = st.resolve_task_id(ctx.repo, ctx.task_id).await
1499            && let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await
1500        {
1501            tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1502        }
1503    }
1504}
1505
1506/// Handle a successful agent response: commit, push, PR, delegations, tokens.
1507///
1508/// Returns `Ok((status, push_failed))` where `status` is the final task status
1509/// string and `push_failed` is `true` when a push was attempted but failed (for
1510/// audit trail classification).
1511#[expect(
1512    clippy::too_many_arguments,
1513    reason = "the runner passes each part of the run result"
1514)]
1515pub async fn handle_success(
1516    task_id: &str,
1517    parsed: agents::ParsedResponse,
1518    wt: &worktree::WorktreeSetup,
1519    task_title: &str,
1520    agent_name: &str,
1521    model_name: Option<&str>,
1522    new_attempts: u32,
1523    repo: &str,
1524    store: &Option<Arc<TaskStore>>,
1525    raw_stdout: &str,
1526) -> anyhow::Result<(String, bool)> {
1527    // Extract token counts before consuming parsed.
1528    let input_tokens = parsed.input_tokens;
1529    let output_tokens = parsed.output_tokens;
1530    let cost_usd = parsed.cost_usd;
1531    let mut resp = parsed.response;
1532
1533    tracing::info!(
1534        task_id,
1535        status = resp.status,
1536        summary = resp.summary,
1537        "agent completed successfully"
1538    );
1539
1540    let push_denied = resp.status == "blocked" && is_denied_push_block(&resp);
1541
1542    // Resolve numeric store_id once so we can reuse it for multiple store ops
1543    // in this hot path and avoid repeated external_id -> store_id SQL lookups.
1544    let store_id_opt = store::resolve_store_id(store, repo, task_id).await;
1545    let ctx = StoreCtx {
1546        store,
1547        store_id_opt,
1548        repo,
1549        task_id,
1550    };
1551
1552    ctx.reset_retry_streaks().await;
1553
1554    // Auto-commit, push, create PR
1555    let git = if resp.status == "done"
1556        || resp.status == "completed"
1557        || resp.status == "in_progress"
1558        || resp.status == "needs_review"
1559        || push_denied
1560    {
1561        run_git_ops(
1562            &ctx,
1563            wt,
1564            task_title,
1565            &resp,
1566            agent_name,
1567            model_name,
1568            new_attempts,
1569        )
1570        .await
1571    } else {
1572        GitOpsResult::default()
1573    };
1574
1575    // The agent's only blocker was the denied push and orch pushed for it, so
1576    // the task continues as if the agent had returned done
1577    if push_denied && (git.has_pr || git.has_pushed) {
1578        tracing::info!(
1579            task_id,
1580            "agent blocked on denied push, orch pushed the branch"
1581        );
1582        resp.status = "done".to_string();
1583    }
1584
1585    // Store delegations in store if present (processed by run_with_context)
1586    if !resp.delegations.is_empty() {
1587        ctx.set(&[("delegations", serde_json::json!(resp.delegations))])
1588            .await;
1589    }
1590
1591    // Store result in task store
1592    // If agent said "done" and a PR exists, send to review before merge.
1593    // If agent said "done", pushed commits, but PR creation failed — review gate creates PR.
1594    // If agent said "done", no PR, and no delegations — work is complete
1595    // (e.g., review/analysis jobs that create issues but no code changes).
1596    // If agent said "done", no PR, but has delegations — blocked on children.
1597    let has_delegations = !resp.delegations.is_empty();
1598
1599    // Detect push failure, increment counters.
1600    let push_state = detect_push_failure_state(&ctx, git.has_commits, git.has_pushed).await;
1601
1602    // Determine whether this external task can be marked done without a PR.
1603    // External tasks always require a PR before reaching `done` — unless
1604    // commits were successfully pushed (in which case the `has_pushed` branch
1605    // handles routing). The `is_non_code_task` heuristic was removed because
1606    // it relied on agent output and could be fooled: an agent claiming
1607    // "already implemented" or "config-only" would match non-code keywords
1608    // and close the issue without verification.
1609    let is_external = !task_id.starts_with("internal:");
1610    let requires_pr = is_external && !git.has_pr && !resp.status.starts_with("needs_review");
1611
1612    // Preferred config key `workflow.max_reroute_attempts`; fall back to
1613    // `workflow.max_attempts` for backwards compatibility.
1614    let max_reroutes: u32 = config::get("workflow.max_reroute_attempts")
1615        .ok()
1616        .and_then(|s| s.parse().ok())
1617        .or_else(|| {
1618            config::get("workflow.max_attempts")
1619                .ok()
1620                .and_then(|s| s.parse().ok())
1621        })
1622        .unwrap_or(3);
1623
1624    // Determine whether we are entering the no-code-reroute branch (all
1625    // earlier chain conditions are false + done + requires_pr).
1626    let is_no_code_reroute = !push_state.is_workflow_scope_failure
1627        && !push_state.push_failed
1628        && (resp.status == "done" || resp.status == "completed")
1629        && !has_delegations
1630        && !git.has_pushed
1631        && requires_pr;
1632
1633    // A conflict reroute with nothing ahead of the default branch is a landed
1634    // fix, not a no-code run: close the superseded PR and mark the task done.
1635    let superseded_pr = match (is_no_code_reroute, ctx.get_task().await) {
1636        (true, Some(task)) => {
1637            superseded_conflict_pr(task.merge_conflict_retries, task.pr_number, git.has_commits)
1638        }
1639        _ => None,
1640    };
1641    let is_no_code_reroute = is_no_code_reroute && superseded_pr.is_none();
1642
1643    let no_code_reroutes =
1644        detect_no_code_reroutes(&ctx, is_no_code_reroute, new_attempts, max_reroutes).await;
1645
1646    let blocked_reason = if resp.status == "blocked" {
1647        Some(agent_blocked_reason(&resp))
1648    } else {
1649        None
1650    };
1651    let is_retryable_blocked = blocked_reason
1652        .as_deref()
1653        .is_some_and(is_retryable_blocked_reason);
1654
1655    // ── Detect same-agent loop ───────────────────────────────────────────────
1656    //
1657    // If this is a no-code reroute and the agent is the same as the one that
1658    // produced the previous no-code result, flag it for blocking (#2410, #2686).
1659    let is_same_agent = if is_no_code_reroute {
1660        let prev_no_code_agent = ctx
1661            .get_task()
1662            .await
1663            .map(|t| t.no_code_last_agent)
1664            .unwrap_or_default();
1665        !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name
1666    } else {
1667        false
1668    };
1669
1670    // ── Pure status decision ─────────────────────────────────────────────────
1671
1672    let final_status_owned = classify_final_status(&DecisionInput {
1673        agent_status: &resp.status,
1674        is_workflow_scope_failure: push_state.is_workflow_scope_failure,
1675        is_rebase_conflict_failure: push_state.is_rebase_conflict_failure,
1676        push_failed: push_state.push_failed,
1677        push_failures: push_state.push_failures,
1678        is_transient_push_failure: push_state.is_transient_failure,
1679        has_pr: git.has_pr,
1680        has_delegations,
1681        has_pushed: git.has_pushed,
1682        is_pr_base_invalid: git.pr_base_invalid,
1683        requires_pr,
1684        no_code_reroutes,
1685        max_reroutes,
1686        is_same_agent,
1687        is_completed_status: resp.status == "completed",
1688        is_retryable_blocked,
1689        is_superseded_conflict: superseded_pr.is_some(),
1690    });
1691    let final_status = final_status_owned.as_str();
1692
1693    // ── Post-decision side effects (tracing + store updates) ─────────────────
1694
1695    apply_post_decision_effects(
1696        &ctx,
1697        final_status,
1698        &resp.status,
1699        &push_state,
1700        is_no_code_reroute,
1701        git.has_pr,
1702        git.has_pushed,
1703        git.pr_base_invalid,
1704        has_delegations,
1705        is_external,
1706        no_code_reroutes,
1707        max_reroutes,
1708        agent_name,
1709    )
1710    .await;
1711
1712    // When the agent explicitly returned "blocked", persist its explanation into
1713    // last_error so `orch task get` surfaces the reason without requiring manual
1714    // inspection of task_runs.parsed_response.  The apply_post_decision_effects
1715    // branches above only fire on push/no-code scenarios; a raw agent-blocked
1716    // response falls through all of them and would leave last_error empty.
1717    if resp.status == "blocked" {
1718        let reason = blocked_reason.unwrap_or_else(|| agent_blocked_reason(&resp));
1719        if final_status == "new" {
1720            // Retryable external block: cooldown failed agent/model, clear selection,
1721            // and reroute after backoff instead of permanently blocking.
1722            cooldown::record_agent_failure_with_message(agent_name, &reason).await;
1723            if let Some(m) = model_name {
1724                cooldown::record_model_failure(agent_name, m).await;
1725            }
1726            ctx.set(&[
1727                (
1728                    "last_error",
1729                    serde_json::json!(format!(
1730                        "transient blocker (auto-retry scheduled): {reason}"
1731                    )),
1732                ),
1733                ("agent", serde_json::json!(null)),
1734                ("model", serde_json::json!(null)),
1735            ])
1736            .await;
1737            tracing::warn!(
1738                task_id = ctx.task_id,
1739                reason = %reason,
1740                "agent returned retryable blocked status — rerouting as new with cooldown"
1741            );
1742        } else if final_status == "blocked" {
1743            ctx.set(&[("last_error", serde_json::json!(reason))]).await;
1744        }
1745    }
1746
1747    if let Some(pr_number) = superseded_pr {
1748        tracing::info!(
1749            task_id = ctx.task_id,
1750            pr_number,
1751            "conflict reroute found the fix already on the default branch — closing superseded PR"
1752        );
1753        close_superseded_pr(&ctx, pr_number).await;
1754    }
1755
1756    ctx.set(&[("summary", serde_json::json!(resp.summary))])
1757        .await;
1758
1759    // Store token usage — prefer agent-parsed tokens, fall back to response
1760    store_token_usage(
1761        &ctx,
1762        input_tokens.or(resp.input_tokens),
1763        output_tokens.or(resp.output_tokens),
1764        cost_usd,
1765    )
1766    .await;
1767
1768    // Store learnings for memory (for future retries)
1769    response::store_learnings_from_response(
1770        task_id,
1771        new_attempts,
1772        agent_name,
1773        model_name,
1774        &resp,
1775        resp.error.as_deref(),
1776        store,
1777        repo,
1778        raw_stdout,
1779    )
1780    .await;
1781
1782    // Note: done → in_review transition is handled by the engine
1783    // after triggering the review agent (engine/mod.rs)
1784    Ok((final_status.to_string(), push_state.push_failed))
1785}
1786
1787/// Labels that indicate a task is non-code and can be marked done without a PR.
1788#[cfg(test)]
1789mod tests {
1790    use std::fs as std_fs;
1791    use std::process::Command;
1792
1793    use super::*;
1794    use crate::store::NewTask;
1795
1796    /// A successful run between timeouts restarts the count, so only timeouts in a row block a task
1797    #[tokio::test]
1798    async fn successful_run_resets_timeout_retries() {
1799        let store = Some(Arc::new(TaskStore::open_memory().await.unwrap()));
1800        let id = store
1801            .as_ref()
1802            .unwrap()
1803            .create_internal("owner/repo", "t", "b", "test", "", None)
1804            .await
1805            .unwrap();
1806        let task_id = format!("internal:{id}");
1807        let ctx = StoreCtx {
1808            store: &store,
1809            store_id_opt: None,
1810            repo: "owner/repo",
1811            task_id: &task_id,
1812        };
1813
1814        ctx.increment("timeout_retries").await.unwrap();
1815        ctx.increment("network_retries").await.unwrap();
1816        ctx.reset_retry_streaks().await;
1817        ctx.increment("timeout_retries").await.unwrap();
1818        ctx.increment("timeout_retries").await.unwrap();
1819
1820        let task = ctx.get_task().await.unwrap();
1821        assert_eq!(task.timeout_retries, 2);
1822        assert_eq!(task.network_retries, 0);
1823        assert_ne!(task.status.as_str(), "blocked");
1824    }
1825
1826    #[test]
1827    fn local_rate_limit_skip_is_detected() {
1828        assert!(is_local_rate_limit_skip(
1829            "GitHub API error: GitHub REST API rate-limited, skipping call for 236s"
1830        ));
1831        assert!(!is_local_rate_limit_skip(
1832            "GitHub API POST failed (502): Bad Gateway"
1833        ));
1834    }
1835
1836    /// Helper: run git in `dir`, panicking on failure
1837    fn git(dir: &Path, args: &[&str]) {
1838        let out = Command::new("git")
1839            .current_dir(dir)
1840            .args(args)
1841            .output()
1842            .unwrap_or_else(|e| panic!("git {args:?} failed to start: {e}"));
1843        assert!(
1844            out.status.success(),
1845            "git {args:?} failed:\nstdout: {}\nstderr: {}",
1846            String::from_utf8_lossy(&out.stdout),
1847            String::from_utf8_lossy(&out.stderr)
1848        );
1849    }
1850
1851    fn init_clone(remote: &Path, dir: &Path) {
1852        git(dir, &["clone", remote.to_str().unwrap(), "."]);
1853        git(dir, &["config", "user.email", "t@t.com"]);
1854        git(dir, &["config", "user.name", "T"]);
1855        git(dir, &["config", "commit.gpgsign", "false"]);
1856    }
1857
1858    #[test]
1859    fn remote_rejected_failure_is_not_diverged() {
1860        let err = "remote: fatal error in commit_refs\n ! [remote rejected] b -> b (failure)";
1861        assert!(!push_error_is_diverged(err));
1862        assert!(push_error_is_diverged("! [rejected] b -> b (fetch first)"));
1863        assert!(push_error_is_diverged(
1864            "! [rejected] b -> b (non-fast-forward)"
1865        ));
1866    }
1867
1868    /**
1869     * Someone rebased the PR branch from another clone and force-pushed it, so orch's
1870     * push is rejected with a stale lease. Pushing is orch's own step: it must rebase
1871     * onto the moved branch and push again, never hand finished work back to the agent
1872     * as a fresh attempt (#3783)
1873     **/
1874    #[tokio::test]
1875    async fn push_rejected_after_remote_rebase_is_pushed_not_rerouted() {
1876        let remote = tempfile::tempdir().unwrap();
1877        git(remote.path(), &["init", "--bare", "-b", "main"]);
1878
1879        // orch's worktree: main, then a feature branch with the task's commit
1880        let work = tempfile::tempdir().unwrap();
1881        init_clone(remote.path(), work.path());
1882        std_fs::write(work.path().join("README.md"), "base").unwrap();
1883        git(work.path(), &["add", "."]);
1884        git(work.path(), &["commit", "-m", "init"]);
1885        git(work.path(), &["push", "origin", "HEAD:main"]);
1886        git(work.path(), &["checkout", "-b", "feat"]);
1887        std_fs::write(work.path().join("fix.txt"), "the fix").unwrap();
1888        git(work.path(), &["add", "."]);
1889        git(work.path(), &["commit", "-m", "apply the fix"]);
1890        git(work.path(), &["push", "-u", "origin", "feat"]);
1891
1892        // Another clone rewrites the same change and force-pushes it
1893        let other = tempfile::tempdir().unwrap();
1894        init_clone(remote.path(), other.path());
1895        git(other.path(), &["checkout", "feat"]);
1896        git(
1897            other.path(),
1898            &["commit", "--amend", "-m", "apply the fix (rebased)"],
1899        );
1900        git(other.path(), &["push", "--force", "origin", "feat"]);
1901
1902        // orch now has an unpushed commit of its own on top of the stale branch
1903        std_fs::write(work.path().join("more.txt"), "follow-up").unwrap();
1904        git(work.path(), &["add", "."]);
1905        git(work.path(), &["commit", "-m", "follow-up"]);
1906
1907        let store = Arc::new(TaskStore::open_memory().await.unwrap());
1908        let store_id = store
1909            .create_internal("owner/repo", "Push recovery", "", "task", "3783", None)
1910            .await
1911            .unwrap();
1912        let task_id = format!("internal:{store_id}");
1913        let store_opt = Some(Arc::clone(&store));
1914        let ctx = StoreCtx {
1915            store: &store_opt,
1916            store_id_opt: Some(store_id),
1917            repo: "owner/repo",
1918            task_id: &task_id,
1919        };
1920
1921        let result =
1922            push_branch_with_log(&ctx, work.path(), "feat", "main", "claude", Some("opus")).await;
1923
1924        assert!(
1925            matches!(result, PushResult::Success),
1926            "orch must land the push itself after adopting the moved remote branch"
1927        );
1928
1929        let task = store.get(store_id).await.unwrap();
1930        assert!(
1931            task.last_error.is_empty(),
1932            "a recovered push must leave no error behind, got: {}",
1933            task.last_error
1934        );
1935        assert_eq!(task.push_failures, 0, "recovery is not a push failure");
1936
1937        // push_branch recovers the stale lease itself, so the log below is the proof: the
1938        // follow-up commit is on the remote, on top of the rebased history
1939        let log = Command::new("git")
1940            .current_dir(work.path())
1941            .args(["log", "--oneline", "origin/feat"])
1942            .output()
1943            .unwrap();
1944        let log = String::from_utf8_lossy(&log.stdout);
1945        assert!(
1946            log.contains("follow-up"),
1947            "remote must carry the task's commit: {log}"
1948        );
1949        assert!(
1950            log.contains("apply the fix (rebased)"),
1951            "orch must adopt the rebased remote history, not re-push its own: {log}"
1952        );
1953        assert!(
1954            !log.contains("apply the fix\n"),
1955            "the superseded commit must not come back: {log}"
1956        );
1957    }
1958
1959    #[test]
1960    fn missing_path_error_detects_io_not_found() {
1961        let err = anyhow::Error::from(io::Error::new(
1962            ErrorKind::NotFound,
1963            "simulated missing path",
1964        ));
1965        assert!(is_missing_path_error(&err));
1966    }
1967
1968    #[test]
1969    fn missing_path_error_detects_message_fallback() {
1970        let err = anyhow::anyhow!("push failed: No such file or directory (os error 2)");
1971        assert!(is_missing_path_error(&err));
1972    }
1973
1974    #[test]
1975    fn superseded_conflict_pr_requires_conflict_reroute_and_no_commits() {
1976        // Conflict reroute, nothing ahead of the default branch → close the PR.
1977        assert_eq!(superseded_conflict_pr(1, Some(42), false), Some(42));
1978        // Commits exist → the agent did work, keep the PR.
1979        assert_eq!(superseded_conflict_pr(1, Some(42), true), None);
1980        // No conflict reroute → an ordinary no-code run.
1981        assert_eq!(superseded_conflict_pr(0, Some(42), false), None);
1982        // No PR to close.
1983        assert_eq!(superseded_conflict_pr(1, None, false), None);
1984    }
1985
1986    /// End-to-end: a conflict reroute that comes back with zero commits ahead of
1987    /// the default branch closes the superseded PR and marks the task done,
1988    /// instead of blocking it as a same-agent no-code loop (#3780).
1989    #[tokio::test]
1990    async fn conflict_reroute_with_no_commits_closes_pr_and_marks_done() {
1991        fn git(dir: &Path, args: &[&str]) {
1992            let ok = Command::new("git")
1993                .args(args)
1994                .current_dir(dir)
1995                .env("GIT_AUTHOR_NAME", "t")
1996                .env("GIT_AUTHOR_EMAIL", "t@example.com")
1997                .env("GIT_COMMITTER_NAME", "t")
1998                .env("GIT_COMMITTER_EMAIL", "t@example.com")
1999                .output()
2000                .expect("git runs")
2001                .status
2002                .success();
2003            assert!(ok, "git {args:?} failed");
2004        }
2005
2006        // A worktree whose branch has nothing ahead of origin/main — the state
2007        // the agent leaves behind when the fix already landed on the base.
2008        let dir = tempfile::tempdir().unwrap();
2009        let origin = dir.path().join("origin.git");
2010        std_fs::create_dir(&origin).unwrap();
2011        git(&origin, &["init", "--bare", "-b", "main"]);
2012        let work_dir = dir.path().join("work");
2013        std_fs::create_dir(&work_dir).unwrap();
2014        git(&work_dir, &["init", "-b", "main"]);
2015        git(
2016            &work_dir,
2017            &["remote", "add", "origin", &origin.display().to_string()],
2018        );
2019        std_fs::write(work_dir.join("a.txt"), "a").unwrap();
2020        git(&work_dir, &["add", "-A"]);
2021        git(&work_dir, &["commit", "-m", "init"]);
2022        git(&work_dir, &["push", "-u", "origin", "main"]);
2023        git(&work_dir, &["checkout", "-b", "fix-branch"]);
2024
2025        let wt = worktree::WorktreeSetup {
2026            work_dir: work_dir.clone(),
2027            branch: "fix-branch".to_string(),
2028            default_branch: "main".to_string(),
2029            main_project_dir: work_dir.clone(),
2030        };
2031
2032        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2033        let store_id = store
2034            .create(&NewTask {
2035                external_id: Some("3772".to_string()),
2036                repo: "owner/repo".to_string(),
2037                origin: "github".to_string(),
2038                title: "Fix the thing".to_string(),
2039                body: String::new(),
2040                source: "manual".to_string(),
2041                source_id: String::new(),
2042                author: "bot".to_string(),
2043                url: String::new(),
2044                labels: vec![],
2045                parent_id: None,
2046            })
2047            .await
2048            .unwrap();
2049        store
2050            .set_fields(store_id, &[("pr_number", serde_json::json!(42))])
2051            .await
2052            .unwrap();
2053        // review_poll rerouted this task to resolve a merge conflict.
2054        store
2055            .increment(store_id, "merge_conflict_retries")
2056            .await
2057            .unwrap();
2058        // A previous no-code run by the same agent — without the superseded
2059        // check this run would block the task as a same-agent loop.
2060        store
2061            .set_fields(
2062                store_id,
2063                &[("no_code_last_agent", serde_json::json!("claude"))],
2064            )
2065            .await
2066            .unwrap();
2067
2068        let parsed = agents::ParsedResponse {
2069            response: AgentResponse {
2070                status: "done".to_string(),
2071                summary: "the fix is already on main".to_string(),
2072                ..Default::default()
2073            },
2074            input_tokens: None,
2075            output_tokens: None,
2076            cost_usd: None,
2077            duration_ms: None,
2078        };
2079
2080        let (final_status, push_failed) = handle_success(
2081            "3772",
2082            parsed,
2083            &wt,
2084            "Fix the thing",
2085            "claude",
2086            Some("sonnet"),
2087            1,
2088            "owner/repo",
2089            &Some(Arc::clone(&store)),
2090            "",
2091        )
2092        .await
2093        .unwrap();
2094
2095        assert_eq!(
2096            final_status, "done",
2097            "a superseded conflict reroute must end done, not blocked"
2098        );
2099        assert!(!push_failed);
2100
2101        let task = store.get(store_id).await.unwrap();
2102        assert!(
2103            task.last_error.contains("superseded"),
2104            "the close attempt must be recorded on the task: {}",
2105            task.last_error
2106        );
2107        assert_eq!(
2108            task.pr_number, None,
2109            "a superseded PR must not stay on the task, the blocker refresh would read it as closed unmerged"
2110        );
2111    }
2112
2113    /// A conflict reroute with zero commits ahead marks the task done instead of
2114    /// blocking it as a same-agent no-code loop (#3780).
2115    #[test]
2116    fn classify_superseded_conflict_marks_done() {
2117        let status = classify_final_status(&DecisionInput {
2118            agent_status: "done",
2119            requires_pr: true,
2120            is_same_agent: true,
2121            is_superseded_conflict: true,
2122            ..Default::default()
2123        });
2124        assert_eq!(status, "done");
2125    }
2126
2127    // ── classify_final_status — one test per decision branch ─────────────────
2128
2129    /// Branch 1: workflow-scope push failure → block immediately (non-retryable).
2130    #[test]
2131    fn classify_workflow_scope_failure_blocks() {
2132        let status = classify_final_status(&DecisionInput {
2133            is_workflow_scope_failure: true,
2134            push_failed: true,
2135            push_failures: 1,
2136            ..Default::default()
2137        });
2138        assert_eq!(status, "blocked");
2139    }
2140
2141    /// Branch 1b: automatic rebase recovery failed (conflicts) → block immediately.
2142    #[test]
2143    fn classify_rebase_conflict_failure_blocks() {
2144        let status = classify_final_status(&DecisionInput {
2145            is_rebase_conflict_failure: true,
2146            push_failed: true,
2147            ..Default::default()
2148        });
2149        assert_eq!(status, "blocked");
2150    }
2151
2152    /// Branch 2a: push failed, < 3 times → reroute with "new".
2153    #[test]
2154    fn classify_push_failed_under_threshold_reroutes() {
2155        let status = classify_final_status(&DecisionInput {
2156            push_failed: true,
2157            push_failures: 2, // < 3
2158            ..Default::default()
2159        });
2160        assert_eq!(status, "new");
2161    }
2162
2163    #[test]
2164    fn denied_push_block_detected() {
2165        let resp = |s: &str| AgentResponse {
2166            status: "blocked".to_string(),
2167            summary: s.to_string(),
2168            ..Default::default()
2169        };
2170        assert!(is_denied_push_block(&resp(
2171            "Work committed, but `git push -u origin HEAD` was denied"
2172        )));
2173        assert!(is_denied_push_block(&resp(
2174            "It is not pushed: `git push --force-with-lease` needs approval that this session could not get."
2175        )));
2176        assert!(is_denied_push_block(&resp(
2177            "`git push --force-with-lease` was denied because this session cannot get approval"
2178        )));
2179        assert!(is_denied_push_block(&resp("The push requires approval")));
2180        assert!(!is_denied_push_block(&resp("Missing API key")));
2181    }
2182
2183    /// Branch 2b: push failed >= 3 times → block for human intervention.
2184    #[test]
2185    fn classify_push_failed_at_threshold_blocks() {
2186        let status = classify_final_status(&DecisionInput {
2187            push_failed: true,
2188            push_failures: 3, // >= 3
2189            ..Default::default()
2190        });
2191        assert_eq!(status, "blocked");
2192    }
2193
2194    /// A GitHub-side push failure never reaches the router, the engine retries the push (#4025).
2195    #[test]
2196    fn classify_transient_push_failure_waits_for_the_engine_retry() {
2197        for push_failures in 1..push_retry::MAX_PUSH_FAILURES {
2198            let status = classify_final_status(&DecisionInput {
2199                push_failed: true,
2200                is_transient_push_failure: true,
2201                push_failures,
2202                ..Default::default()
2203            });
2204            assert_eq!(status, "needs_review", "push_failures={push_failures}");
2205        }
2206    }
2207
2208    #[test]
2209    fn classify_transient_push_failure_blocks_after_the_last_retry() {
2210        let status = classify_final_status(&DecisionInput {
2211            push_failed: true,
2212            is_transient_push_failure: true,
2213            push_failures: push_retry::MAX_PUSH_FAILURES,
2214            ..Default::default()
2215        });
2216        assert_eq!(status, "blocked");
2217    }
2218
2219    /// Branch 3: done + PR exists → send to review.
2220    #[test]
2221    fn classify_done_with_pr_goes_to_needs_review() {
2222        let status = classify_final_status(&DecisionInput {
2223            agent_status: "done",
2224            has_pr: true,
2225            ..Default::default()
2226        });
2227        assert_eq!(status, "needs_review");
2228    }
2229
2230    /// Branch 4: done + delegations, no PR → block (waiting on child tasks).
2231    #[test]
2232    fn classify_done_with_delegations_no_pr_blocks() {
2233        let status = classify_final_status(&DecisionInput {
2234            agent_status: "done",
2235            has_delegations: true,
2236            ..Default::default()
2237        });
2238        assert_eq!(status, "blocked");
2239    }
2240
2241    /// Branch 5: done + commits pushed but PR creation failed → re-dispatch.
2242    #[test]
2243    fn classify_done_pushed_no_pr_reroutes_for_pr_creation() {
2244        let status = classify_final_status(&DecisionInput {
2245            agent_status: "done",
2246            has_pushed: true,
2247            ..Default::default()
2248        });
2249        assert_eq!(status, "routed");
2250    }
2251
2252    /// Branch 5b: done + pushed + PR base invalid → block (terminal error).
2253    #[test]
2254    fn classify_done_pushed_pr_base_invalid_blocks() {
2255        let status = classify_final_status(&DecisionInput {
2256            agent_status: "done",
2257            has_pushed: true,
2258            is_pr_base_invalid: true,
2259            ..Default::default()
2260        });
2261        assert_eq!(status, "blocked");
2262    }
2263
2264    /// Branch 5c: completed + pushed + PR base invalid → block (terminal error).
2265    #[test]
2266    fn classify_completed_pushed_pr_base_invalid_blocks() {
2267        let status = classify_final_status(&DecisionInput {
2268            agent_status: "completed",
2269            is_completed_status: true,
2270            has_pushed: true,
2271            is_pr_base_invalid: true,
2272            ..Default::default()
2273        });
2274        assert_eq!(status, "blocked");
2275    }
2276
2277    /// Branch 6a: done + requires_pr, under max reroutes → reroute.
2278    #[test]
2279    fn classify_done_requires_pr_under_max_reroutes() {
2280        let status = classify_final_status(&DecisionInput {
2281            agent_status: "done",
2282            requires_pr: true,
2283            no_code_reroutes: 1,
2284            max_reroutes: 3,
2285            is_same_agent: false,
2286            ..Default::default()
2287        });
2288        assert_eq!(status, "new");
2289    }
2290
2291    /// Branch 6a edge: same agent on no-code reroute → block immediately.
2292    #[test]
2293    fn classify_done_requires_pr_same_agent_blocks_immediately() {
2294        let status = classify_final_status(&DecisionInput {
2295            agent_status: "done",
2296            requires_pr: true,
2297            no_code_reroutes: 1,
2298            max_reroutes: 3,
2299            is_same_agent: true, // Same agent would be selected again
2300            ..Default::default()
2301        });
2302        assert_eq!(status, "blocked");
2303    }
2304
2305    /// Branch 6b: done + requires_pr, at max reroutes → block.
2306    #[test]
2307    fn classify_done_requires_pr_at_max_reroutes_blocks() {
2308        let status = classify_final_status(&DecisionInput {
2309            agent_status: "done",
2310            requires_pr: true,
2311            no_code_reroutes: 3,
2312            max_reroutes: 3,
2313            is_same_agent: false,
2314            ..Default::default()
2315        });
2316        assert_eq!(status, "blocked");
2317    }
2318
2319    /// Branch 6b edge: exceeding max also blocks.
2320    #[test]
2321    fn classify_done_requires_pr_exceeds_max_reroutes_blocks() {
2322        let status = classify_final_status(&DecisionInput {
2323            agent_status: "done",
2324            requires_pr: true,
2325            no_code_reroutes: 5,
2326            max_reroutes: 3,
2327            is_same_agent: false,
2328            ..Default::default()
2329        });
2330        assert_eq!(status, "blocked");
2331    }
2332
2333    /// Same agent on external task requiring PR → block immediately (not wait for max).
2334    #[test]
2335    fn classify_done_external_no_pushed_same_agent_blocks_immediately() {
2336        let status = classify_final_status(&DecisionInput {
2337            agent_status: "done",
2338            requires_pr: true, // always true for external tasks with !has_pr + done
2339            no_code_reroutes: 0,
2340            max_reroutes: 3,
2341            is_same_agent: true,
2342            ..Default::default()
2343        });
2344        assert_eq!(status, "blocked");
2345    }
2346
2347    /// External task with no pushed commits: reroute (agent must produce commits
2348    /// before the issue can be closed). This was the source of issue #1898 — an
2349    /// agent claiming "already implemented" without any code changes would
2350    /// previously match non-code keywords and close the issue falsely.
2351    ///
2352    /// External tasks with done+!has_pr+!has_pushed always have requires_pr=true
2353    /// (see handle_success), so they are caught by branch 6, not branch 7.
2354    #[test]
2355    fn classify_done_external_no_pushed_commits_reroutes() {
2356        let status = classify_final_status(&DecisionInput {
2357            agent_status: "done",
2358            has_pushed: false,
2359            requires_pr: true, // always true for external tasks with !has_pr + done
2360            no_code_reroutes: 0,
2361            max_reroutes: 3,
2362            is_same_agent: false,
2363            ..Default::default()
2364        });
2365        assert_eq!(status, "new");
2366    }
2367
2368    /// Internal task with no pushed commits may still be marked done — internal
2369    /// tasks may produce no git-visible changes.
2370    #[test]
2371    fn classify_done_internal_task_no_pr_is_done() {
2372        let status = classify_final_status(&DecisionInput {
2373            agent_status: "done",
2374            has_pushed: false,
2375            requires_pr: false,
2376            ..Default::default()
2377        });
2378        assert_eq!(status, "done");
2379    }
2380
2381    /// Pass-through: non-"done" status is returned unchanged.
2382    #[test]
2383    fn classify_non_done_status_passes_through() {
2384        for status_str in &["in_progress", "needs_review", "blocked"] {
2385            let result = classify_final_status(&DecisionInput {
2386                agent_status: status_str,
2387                ..Default::default()
2388            });
2389            assert_eq!(
2390                result, *status_str,
2391                "status '{status_str}' should pass through unchanged"
2392            );
2393        }
2394    }
2395
2396    /// Descriptive completion text should normalize to done.
2397    #[test]
2398    fn classify_descriptive_completion_status_normalizes_to_done() {
2399        let status = classify_final_status(&DecisionInput {
2400            agent_status: "Trading scan complete. Nothing to trade.",
2401            ..Default::default()
2402        });
2403        assert_eq!(status, "done");
2404    }
2405
2406    /// Do not normalize to done when completion-like text includes failure cues.
2407    #[test]
2408    fn classify_descriptive_completion_with_failure_cues_does_not_normalize() {
2409        let status = classify_final_status(&DecisionInput {
2410            agent_status: "completed but failed to push branch due to error",
2411            ..Default::default()
2412        });
2413        assert_eq!(status, "completed but failed to push branch due to error");
2414    }
2415
2416    /// push_failed takes precedence over done+has_pr (push failure detected first).
2417    #[test]
2418    fn classify_push_failed_takes_precedence_over_has_pr() {
2419        let status = classify_final_status(&DecisionInput {
2420            agent_status: "done",
2421            has_pr: true,
2422            push_failed: true,
2423            push_failures: 1,
2424            ..Default::default()
2425        });
2426        // push_failed branch runs before done+has_pr check
2427        assert_eq!(status, "new");
2428    }
2429
2430    /// workflow_scope_failure takes precedence over push_failed < 3.
2431    #[test]
2432    fn classify_workflow_scope_failure_takes_precedence_over_push_reroute() {
2433        let status = classify_final_status(&DecisionInput {
2434            is_workflow_scope_failure: true,
2435            push_failed: true,
2436            push_failures: 1, // would normally reroute
2437            ..Default::default()
2438        });
2439        assert_eq!(status, "blocked");
2440    }
2441
2442    // ── "completed" status tests ────────────────────────────────────────────────
2443
2444    /// "completed" + has_pr → needs_review.
2445    #[test]
2446    fn classify_completed_with_pr_goes_to_needs_review() {
2447        let status = classify_final_status(&DecisionInput {
2448            agent_status: "completed",
2449            has_pr: true,
2450            is_completed_status: true,
2451            ..Default::default()
2452        });
2453        assert_eq!(status, "needs_review");
2454    }
2455
2456    /// "completed" + delegations, no PR → blocked.
2457    #[test]
2458    fn classify_completed_with_delegations_no_pr_blocks() {
2459        let status = classify_final_status(&DecisionInput {
2460            agent_status: "completed",
2461            has_delegations: true,
2462            is_completed_status: true,
2463            ..Default::default()
2464        });
2465        assert_eq!(status, "blocked");
2466    }
2467
2468    /// "completed" + pushed, no PR → routed for PR creation.
2469    #[test]
2470    fn classify_completed_pushed_no_pr_reroutes_for_pr_creation() {
2471        let status = classify_final_status(&DecisionInput {
2472            agent_status: "completed",
2473            has_pushed: true,
2474            is_completed_status: true,
2475            ..Default::default()
2476        });
2477        assert_eq!(status, "routed");
2478    }
2479
2480    /// "completed" + external task requiring PR, under max reroutes → reroute.
2481    #[test]
2482    fn classify_completed_requires_pr_under_max_reroutes() {
2483        let status = classify_final_status(&DecisionInput {
2484            agent_status: "completed",
2485            requires_pr: true,
2486            no_code_reroutes: 1,
2487            max_reroutes: 3,
2488            is_completed_status: true,
2489            is_same_agent: false,
2490            ..Default::default()
2491        });
2492        assert_eq!(status, "new");
2493    }
2494
2495    /// "completed" + external task, at max reroutes → block.
2496    #[test]
2497    fn classify_completed_requires_pr_at_max_reroutes_blocks() {
2498        let status = classify_final_status(&DecisionInput {
2499            agent_status: "completed",
2500            requires_pr: true,
2501            no_code_reroutes: 3,
2502            max_reroutes: 3,
2503            is_completed_status: true,
2504            is_same_agent: false,
2505            ..Default::default()
2506        });
2507        assert_eq!(status, "blocked");
2508    }
2509
2510    /// "completed" + external task, same agent on no-code reroute → block immediately.
2511    #[test]
2512    fn classify_completed_same_agent_blocks_immediately() {
2513        let status = classify_final_status(&DecisionInput {
2514            agent_status: "completed",
2515            requires_pr: true,
2516            no_code_reroutes: 1,
2517            max_reroutes: 3,
2518            is_completed_status: true,
2519            is_same_agent: true,
2520            ..Default::default()
2521        });
2522        assert_eq!(status, "blocked");
2523    }
2524
2525    /// "completed" on internal task (no requires_pr) → mark done.
2526    #[test]
2527    fn classify_completed_internal_task_is_done() {
2528        let status = classify_final_status(&DecisionInput {
2529            agent_status: "completed",
2530            requires_pr: false,
2531            is_completed_status: true,
2532            ..Default::default()
2533        });
2534        assert_eq!(status, "done");
2535    }
2536
2537    // ── agent_blocked_reason — regression tests for issue #3012 ──────────────
2538
2539    /// Agent explicitly returned blocked with an error field — use error as reason.
2540    #[test]
2541    fn agent_blocked_reason_prefers_error_field() {
2542        let resp = AgentResponse {
2543            status: "blocked".to_string(),
2544            error: Some("CDP endpoint not reachable".to_string()),
2545            summary: "Could not complete research".to_string(),
2546            remaining: vec!["Run x-twitter-brave with reachable endpoint".to_string()],
2547            ..Default::default()
2548        };
2549        let reason = agent_blocked_reason(&resp);
2550        assert!(
2551            reason.contains("CDP endpoint not reachable"),
2552            "reason: {reason}"
2553        );
2554        assert!(reason.contains("Remaining:"), "reason: {reason}");
2555        assert!(reason.contains("x-twitter-brave"), "reason: {reason}");
2556    }
2557
2558    /// No error field — falls back to summary.
2559    #[test]
2560    fn agent_blocked_reason_falls_back_to_summary() {
2561        let resp = AgentResponse {
2562            status: "blocked".to_string(),
2563            error: None,
2564            summary: "Could not access required data sources in sandboxed environment".to_string(),
2565            remaining: vec![],
2566            ..Default::default()
2567        };
2568        let reason = agent_blocked_reason(&resp);
2569        assert_eq!(
2570            reason,
2571            "Could not access required data sources in sandboxed environment"
2572        );
2573    }
2574
2575    /// No error or summary — returns generic fallback.
2576    #[test]
2577    fn agent_blocked_reason_generic_fallback() {
2578        let resp = AgentResponse {
2579            status: "blocked".to_string(),
2580            ..Default::default()
2581        };
2582        let reason = agent_blocked_reason(&resp);
2583        assert_eq!(reason, "agent returned blocked status without a reason");
2584    }
2585
2586    /// Agent-returned "blocked" status passes through classify_final_status unchanged.
2587    #[test]
2588    fn classify_agent_blocked_passes_through() {
2589        let status = classify_final_status(&DecisionInput {
2590            agent_status: "blocked",
2591            ..Default::default()
2592        });
2593        assert_eq!(status, "blocked");
2594    }
2595
2596    #[test]
2597    fn classify_retryable_agent_blocked_reroutes() {
2598        let status = classify_final_status(&DecisionInput {
2599            agent_status: "blocked",
2600            is_retryable_blocked: true,
2601            ..Default::default()
2602        });
2603        assert_eq!(status, "new");
2604    }
2605
2606    #[test]
2607    fn retryable_blocked_reason_detects_transient_patterns() {
2608        assert!(is_retryable_blocked_reason(
2609            "Twitter access unavailable: service timed out"
2610        ));
2611        assert!(is_retryable_blocked_reason(
2612            "final commit blocked by git worktree lock permission error"
2613        ));
2614        assert!(is_retryable_blocked_reason(
2615            "could not commit due a worktree git lock permission error"
2616        ));
2617        assert!(is_retryable_blocked_reason(
2618            "git lockfile permission restrictions in this environment"
2619        ));
2620        assert!(is_retryable_blocked_reason(
2621            "API rate limit hit, try again later"
2622        ));
2623        assert!(is_retryable_blocked_reason(
2624            "Credential bean/hyperliquid-address exists in the pass store but GPG decryption fails in this agent session — no passphrase available."
2625        ));
2626        assert!(is_retryable_blocked_reason(
2627            "credential resolution failed via PasswordStoreProvider; AgentVaultProvider unavailable"
2628        ));
2629    }
2630
2631    #[test]
2632    fn retryable_blocked_reason_rejects_permanent_patterns() {
2633        assert!(!is_retryable_blocked_reason(
2634            "invalid request payload: malformed JSON"
2635        ));
2636        assert!(!is_retryable_blocked_reason(
2637            "resource not found in repository"
2638        ));
2639    }
2640}