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