Skip to main content

orch/engine/runner/
mod.rs

1//! Task runner — executes tasks using AI agents.
2//!
3//! Replaces `run_task.sh` with pure Rust. The runner:
4//! 1. Sets up a git worktree for isolation
5//! 2. Builds context (project instructions, skills, comments)
6//! 3. Renders prompts and spawns the agent in tmux
7//! 4. Waits for completion, collects and classifies the response
8//! 5. Handles errors (reroute on limits, fallback agents)
9//! 6. Auto-commits, pushes, and creates PRs
10//! 7. Updates status labels and posts result comments
11//!
12//! Submodules handle the heavy lifting:
13//! - [`task_init`] — guard checks, worktree setup, and invocation building
14//! - [`session`] — tmux session lifecycle and output collection
15//! - [`response_handler`] — success path: commit, push, PR, token storage
16//! - [`fallback`] — error classification and recovery strategies
17
18pub mod agent;
19pub mod agents;
20pub mod commit;
21pub mod context;
22pub mod cow;
23pub mod diff;
24pub mod direct;
25pub mod fallback;
26pub mod git_ops;
27#[cfg(test)]
28mod recorded_fixtures;
29pub mod response;
30pub mod response_handler;
31pub mod sandbox;
32pub mod session;
33pub mod task_init;
34pub mod worktree;
35
36use crate::backends::{ExternalBackend, ExternalTask, Status};
37use crate::config;
38use crate::engine;
39use crate::engine::cleanup;
40use crate::engine::cooldown;
41use crate::engine::router::RouteResult;
42use crate::engine::tasks;
43use crate::engine::tasks::is_internal_id;
44#[cfg(not(test))]
45use crate::github::http;
46use crate::github::http::GhHttp;
47use crate::home;
48use crate::parser;
49use crate::parser::AgentResponse;
50use crate::parser::Delegation;
51use crate::status_heuristics;
52use crate::store;
53use crate::store::InsertTaskMetric;
54use crate::store::RunTokenUsage;
55use crate::store::TaskStatus;
56use crate::store::TaskStore;
57use crate::tmux::TmuxManager;
58use anyhow::Context;
59use chrono::Utc;
60pub use response::WeightSignal;
61use serde_json::Value;
62use std::collections::HashSet;
63use std::env;
64use std::error;
65use std::fmt;
66use std::fmt::Debug;
67use std::fmt::Formatter;
68use std::path::PathBuf;
69use std::slice;
70use std::sync::Arc;
71use tokio::fs;
72use tokio::task;
73
74/// Fully materialized task-run audit data.
75#[derive(Debug, Clone)]
76pub struct RunAudit {
77    pub stdout: String,
78    pub stderr: String,
79    pub parsed_response: String,
80    pub outcome: String,
81    pub error: String,
82    pub input_tokens: u64,
83    pub output_tokens: u64,
84    pub total_cost_usd: f64,
85    pub duration_secs: f64,
86}
87
88/// Result returned by `run()` so callers can persist the audit trail.
89#[derive(Debug, Clone)]
90pub struct RunExecution {
91    pub status: String,
92    pub exit_code: Option<i32>,
93    pub audit: RunAudit,
94}
95
96fn serialize_parsed_response(
97    parse_result: &Result<agents::ParsedResponse, agents::AgentError>,
98    raw_stdout: &str,
99) -> String {
100    match parse_result {
101        Ok(parsed) => serde_json::json!({
102            "response": parsed.response,
103            "input_tokens": parsed.input_tokens,
104            "output_tokens": parsed.output_tokens,
105            "cost_usd": parsed.cost_usd,
106            "duration_ms": parsed.duration_ms,
107        })
108        .to_string(),
109        Err(agents::AgentError::InvalidResponse { raw }) => {
110            serde_json::json!({"raw": raw}).to_string()
111        }
112        Err(_) => serde_json::json!({"raw": raw_stdout}).to_string(),
113    }
114}
115
116fn classify_run_outcome(
117    status: &str,
118    parse_result: &Result<agents::ParsedResponse, agents::AgentError>,
119    push_failed: bool,
120) -> &'static str {
121    if push_failed {
122        return "push_failed";
123    }
124    match parse_result {
125        Err(agents::AgentError::Timeout { .. }) => "timeout",
126        Err(agents::AgentError::Interrupted { .. }) => "interrupted",
127        // Generic rate-limit patterns (e.g. "usage limit") also match billing-cycle
128        // exhaustion messages, so consult the credit-exhaustion detector first —
129        // otherwise Kimi-style "usage limit for this billing cycle" errors are
130        // stored as a plain "rate_limit" outcome (issue #3529).
131        Err(agents::AgentError::RateLimit { message }) => {
132            cooldown::detect_credit_exhaustion_outcome(message).unwrap_or("rate_limit")
133        }
134        Err(agents::AgentError::Auth { message }) => {
135            cooldown::detect_credit_exhaustion_outcome(message).unwrap_or("failed")
136        }
137        Err(agents::AgentError::InvalidResponse { .. }) => "parse_error",
138        Ok(_)
139            if matches!(
140                status,
141                "done" | "completed" | "in_progress" | "in_review" | "needs_review" | "routed"
142            ) =>
143        {
144            "success"
145        }
146        Ok(_) if status == "blocked" => "blocked",
147        // Non-canonical status that clearly reads as completion — success, not
148        // "unrecognized status" (#3627).
149        Ok(_) if status_heuristics::status_looks_like_descriptive_completion(status) => "success",
150        Err(_) | Ok(_) => "failed",
151    }
152}
153
154fn classify_run_error_type(last_error: &str) -> &'static str {
155    if last_error.is_empty() {
156        // No error: agent completed successfully and created a PR waiting for review
157        "success"
158    } else if last_error.contains("timeout") {
159        "timeout"
160    } else if last_error.contains("interrupted") {
161        "interrupted"
162    } else if last_error.contains("billing cycle") || last_error.contains("monthly quota") {
163        "billing_cycle_exhausted"
164    } else if last_error.contains("rate limit") || last_error.contains("usage limit") {
165        "rate_limit"
166    } else if last_error.contains("auth") {
167        "auth_error"
168    } else if last_error.contains("push failed") {
169        "push_failed"
170    } else if last_error.contains("No commits between") || last_error.contains("no commits") {
171        "no_commits"
172    } else if last_error.contains("create PR failed")
173        || last_error.contains("failed to create pull request")
174        || last_error.contains("pull request creation failed")
175    {
176        "pr_failed"
177    } else if last_error.contains("invalid response") || last_error.contains("parse error") {
178        "parse_error"
179    } else if last_error.contains("exceeded max attempts") {
180        "max_attempts"
181    } else {
182        let lower = last_error.to_ascii_lowercase();
183        if lower.contains("build failed")
184            || lower.contains("tests failed")
185            || lower.contains("test failed")
186            || lower.contains("test suite failed")
187            || lower.contains("lint failed")
188        {
189            "ci_failure"
190        } else {
191            "failed"
192        }
193    }
194}
195
196fn extract_run_tokens(
197    parse_result: &Result<agents::ParsedResponse, agents::AgentError>,
198) -> (u64, u64) {
199    match parse_result {
200        Ok(parsed) => (
201            parsed
202                .input_tokens
203                .or(parsed.response.input_tokens)
204                .unwrap_or(0),
205            parsed
206                .output_tokens
207                .or(parsed.response.output_tokens)
208                .unwrap_or(0),
209        ),
210        Err(_) => (0, 0),
211    }
212}
213
214#[expect(clippy::cast_precision_loss, reason = "a run duration in seconds")]
215fn run_duration_secs(started_at: &chrono::DateTime<Utc>) -> f64 {
216    (Utc::now() - *started_at).num_milliseconds() as f64 / 1000.0
217}
218
219struct RunAuditInput<'a> {
220    task_id: &'a str,
221    status: &'a str,
222    parse_result: &'a Result<agents::ParsedResponse, agents::AgentError>,
223    raw_stdout: &'a str,
224    raw_stderr: &'a str,
225    started_at: &'a chrono::DateTime<Utc>,
226    error_override: Option<String>,
227    elapsed_secs: Option<u64>,
228    push_failed: bool,
229}
230
231fn parse_success_output(
232    task_id: &str,
233    agent_name: &str,
234    agent_runner: &dyn agents::AgentRunner,
235    raw_stdout: &str,
236) -> Result<agents::ParsedResponse, agents::AgentError> {
237    // The last result envelope is the agent's final answer and carries the run's tokens and cost.
238    // Claude writes terminal_reason:completed on every run, so this goes before that shortcut
239    let agent_result = agents::find_agent_result(agent_name, raw_stdout);
240    if let Some(result) = &agent_result
241        && !result.is_error
242        && let Ok(response) = parser::parse(&result.result_text)
243    {
244        return Ok(agents::ParsedResponse {
245            response,
246            input_tokens: result.input_tokens,
247            output_tokens: result.output_tokens,
248            cost_usd: result.cost_usd,
249            duration_ms: result.duration_ms,
250        });
251    }
252
253    // Quick NDJSON terminal_reason:completed shortcut: when the agent
254    // emitted a session envelope that explicitly signals completion we
255    // prefer to interpret that as a successful run (or at least a
256    // soft/empty success) rather than immediately classifying an error
257    // from the trailing process output.  Do not override explicit
258    // in-envelope `is_error: true` signals.
259    if raw_stdout.contains("\"terminal_reason\":\"completed\"")
260        && !raw_stdout.contains("\"is_error\":true")
261    {
262        // Try to recover a structured response from assistant messages first
263        let assistant_text = agents::collect_assistant_messages_text(agent_name, raw_stdout);
264        if !assistant_text.is_empty() {
265            if let Ok(response) = parser::parse(&assistant_text) {
266                tracing::debug!(
267                    task_id,
268                    agent = agent_name,
269                    "found AgentResponse JSON in earlier assistant message (terminal_reason:completed)"
270                );
271                return Ok(agents::ParsedResponse {
272                    response,
273                    input_tokens: None,
274                    output_tokens: None,
275                    cost_usd: None,
276                    duration_ms: None,
277                });
278            }
279            if let Some(response) = agents::synthesize_response_from_text(&assistant_text) {
280                tracing::warn!(
281                    task_id,
282                    agent = agent_name,
283                    "synthesizing response from assistant message due to terminal_reason:completed"
284                );
285                return Ok(agents::ParsedResponse {
286                    response,
287                    input_tokens: None,
288                    output_tokens: None,
289                    cost_usd: None,
290                    duration_ms: None,
291                });
292            }
293        }
294        // If we couldn't recover structured data from earlier assistant
295        // messages, fall through and let the regular NDJSON envelope parsing
296        // / agent-specific parsers handle the terminal_reason case. This
297        // preserves the existing behavior where an empty `result` field
298        // combined with exit != 0 becomes an InvalidResponse that is then
299        // interpreted by parse_session_output (see tests).
300    }
301
302    // 1. Try to use the unified agent result extractor first
303    if let Some(agent_result) = agent_result {
304        // Treat is_error as authoritative for the success path
305        if agent_result.is_error {
306            // Classify the error from the agent's result text rather than
307            // blindly returning AgentFailed — this preserves rate-limit, auth,
308            // and other actionable error types for proper cooldown/reroute.
309            // Without this, e.g. a claude session-limit error with is_error=true
310            // would be downgraded to AgentFailed, losing the RateLimit signal
311            // that the fallback chain needs for UsageLimit recovery (issue #3292).
312            if let Some(e) = agents::patterns::detect_rate_limit(&agent_result.result_text) {
313                return Err(e);
314            }
315            if let Some(e) = agents::patterns::detect_auth_error(&agent_result.result_text) {
316                return Err(e);
317            }
318            if let Some(e) = agents::patterns::detect_model_unavailable(&agent_result.result_text) {
319                return Err(e);
320            }
321            return Err(agents::AgentError::AgentFailed {
322                message: agent_result.result_text,
323            });
324        }
325
326        // Strategy 2: scan all earlier assistant messages in the NDJSON
327        // stream for valid AgentResponse JSON. Agents often emit the
328        // JSON in an intermediate turn and then summarise in prose as
329        // their final output, so the `type:result` text may be pure
330        // narrative while a prior assistant turn contains the JSON.
331        let assistant_text = agents::collect_assistant_messages_text(agent_name, raw_stdout);
332        if !assistant_text.is_empty()
333            && let Ok(response) = parser::parse(&assistant_text)
334        {
335            tracing::debug!(
336                task_id,
337                agent = agent_name,
338                "found AgentResponse JSON in earlier assistant message"
339            );
340            return Ok(agents::ParsedResponse {
341                response,
342                input_tokens: agent_result.input_tokens,
343                output_tokens: agent_result.output_tokens,
344                cost_usd: agent_result.cost_usd,
345                duration_ms: agent_result.duration_ms,
346            });
347        }
348
349        // Only fall back to text synthesis when parsing fails
350        if let Some(response) = agents::synthesize_response_from_text(&agent_result.result_text) {
351            tracing::warn!(
352                task_id,
353                agent = agent_name,
354                "parse failed on agent result, synthesizing response from plain text"
355            );
356            return Ok(agents::ParsedResponse {
357                response,
358                input_tokens: agent_result.input_tokens,
359                output_tokens: agent_result.output_tokens,
360                cost_usd: agent_result.cost_usd,
361                duration_ms: agent_result.duration_ms,
362            });
363        }
364
365        // NDJSON envelope fallback: the envelope explicitly reports
366        // success (is_error=false) but the result content is domain
367        // JSON or plain prose that doesn't match AgentResponse schema
368        // and doesn't contain completion keywords.  Rather than
369        // returning InvalidResponse (which causes the task to re-run),
370        // synthesize a minimal "done" response from the result text.
371        //
372        // Guard against kimi/minimax edge case: when terminal_reason=completed
373        // but result text is empty, synthesize returns None, parse_success_output
374        // returns InvalidResponse, and parse_session_output can intercept it
375        // before classify_error. Requiring non-empty result here means the
376        // envelope fallback only fires when we have actual text to report.
377        if !agent_result.is_error
378            && result_envelope_completed(raw_stdout)
379            && !agent_result.result_text.is_empty()
380        {
381            let summary_end = agents::truncate_at_char_boundary(&agent_result.result_text, 500);
382            let summary = agent_result.result_text[..summary_end].to_string();
383            tracing::warn!(
384                task_id,
385                agent = agent_name,
386                "NDJSON envelope indicates success but result lacks AgentResponse \
387                 schema — synthesizing done response"
388            );
389            return Ok(agents::ParsedResponse {
390                response: AgentResponse {
391                    status: "done".to_string(),
392                    summary,
393                    ..Default::default()
394                },
395                input_tokens: agent_result.input_tokens,
396                output_tokens: agent_result.output_tokens,
397                cost_usd: agent_result.cost_usd,
398                duration_ms: agent_result.duration_ms,
399            });
400        }
401    }
402
403    // 2. Fall back to generic agent-specific parser if extraction failed
404    match agent_runner.parse_response(raw_stdout) {
405        Ok(parsed) => Ok(parsed),
406        Err(agents::AgentError::InvalidResponse { raw }) => {
407            if let Some(response) = agents::synthesize_response_from_text(&raw) {
408                tracing::warn!(
409                    task_id,
410                    agent = agent_name,
411                    "parse failed, synthesizing response from plain text"
412                );
413                // Try to extract token metadata from the original raw NDJSON
414                // output (not the truncated/extracted text from the error), so
415                // cost tracking isn't lost for agents that return free-form text.
416                let agent_result = agents::find_agent_result(agent_name, raw_stdout);
417                let input_tokens = agent_result.as_ref().and_then(|r| r.input_tokens);
418                let output_tokens = agent_result.as_ref().and_then(|r| r.output_tokens);
419                let cost_usd = agent_result.as_ref().and_then(|r| r.cost_usd);
420                let duration_ms = agent_result.as_ref().and_then(|r| r.duration_ms);
421                Ok(agents::ParsedResponse {
422                    response,
423                    input_tokens,
424                    output_tokens,
425                    cost_usd,
426                    duration_ms,
427                })
428            } else {
429                Err(agents::AgentError::InvalidResponse { raw })
430            }
431        }
432        Err(err) => Err(err),
433    }
434}
435
436/// Whether the last `type: result` line reports `subtype: success` or
437/// `terminal_reason: completed`. Grok omits `terminal_reason`.
438fn result_envelope_line(raw_stdout: &str) -> Option<serde_json::Value> {
439    raw_stdout
440        .lines()
441        .rev()
442        .filter_map(|l| serde_json::from_str::<serde_json::Value>(l.trim()).ok())
443        .find(|v| v.get("type").and_then(|t| t.as_str()) == Some("result"))
444}
445
446fn has_result_envelope(raw_stdout: &str) -> bool {
447    result_envelope_line(raw_stdout).is_some()
448}
449
450fn result_envelope_completed(raw_stdout: &str) -> bool {
451    result_envelope_line(raw_stdout).is_some_and(|v| {
452        v.get("subtype").and_then(|t| t.as_str()) == Some("success")
453            || v.get("terminal_reason").and_then(|t| t.as_str()) == Some("completed")
454    })
455}
456
457// A timeout kill has no result envelope. Salvage only the wrap-up shape: a successful
458// `git commit`, then only shell calls that all returned ok. Returns the last assistant text
459fn salvage_wrapup(raw: &str) -> Option<String> {
460    // (tool name, command, finished ok) in call order
461    let mut calls: Vec<(String, String, Option<bool>)> = Vec::new();
462    let mut last_text = String::new();
463    for v in raw
464        .lines()
465        .filter_map(|l| serde_json::from_str::<Value>(l.trim()).ok())
466    {
467        let blocks = v.pointer("/message/content").and_then(|c| c.as_array());
468        for b in blocks.into_iter().flatten() {
469            match b.get("type").and_then(|t| t.as_str()) {
470                Some("tool_use") => calls.push((
471                    b["name"].as_str().unwrap_or("").to_string(),
472                    b.pointer("/input/command")
473                        .and_then(|c| c.as_str())
474                        .unwrap_or("")
475                        .to_string(),
476                    None,
477                )),
478                Some("text") if v["type"] == "assistant" => {
479                    last_text = b["text"].as_str().unwrap_or("").to_string();
480                }
481                Some("tool_result") => {
482                    let ok = b.get("is_error").and_then(serde_json::Value::as_bool) != Some(true);
483                    // results arrive in call order, fill the first unanswered call
484                    if let Some(c) = calls.iter_mut().find(|c| c.2.is_none()) {
485                        c.2 = Some(ok);
486                    }
487                }
488                _ => {}
489            }
490        }
491    }
492    let commit = calls
493        .iter()
494        .rposition(|(_, cmd, ok)| cmd.contains("git commit") && *ok == Some(true))?;
495    let tail_ok = calls[commit..].iter().all(|(name, _, ok)| {
496        matches!(name.as_str(), "Bash" | "run_terminal_cmd") && *ok == Some(true)
497    });
498    let lower = last_text.to_lowercase();
499    if !tail_ok || lower.contains("still running") || lower.contains("in progress") {
500        return None;
501    }
502    Some(last_text.chars().take(500).collect())
503}
504
505/**
506 * Tries `parse_success_output` first when stdout is non-empty. If that succeeds,
507 * returns success regardless of exit code. If it fails and `exit_code != 0`,
508 * checks for `"terminal_reason":"completed"` or cost-telemetry-only stdout
509 * (`"costUSD":`) before falling back to `classify_error`, so valid sessions that
510 * exit 1 produce `InvalidResponse` (soft error, re-route) instead of a garbled
511 * `Unknown`/`Auth` error (hard failure with spurious cooldown)
512 **/
513pub fn parse_session_output(
514    task_id: &str,
515    agent_name: &str,
516    agent_runner: &dyn agents::AgentRunner,
517    session_output: &session::SessionOutput,
518) -> Result<agents::ParsedResponse, agents::AgentError> {
519    // Some agents may emit NDJSON/telemetry to stderr instead of stdout. Combine
520    // both streams so parse_success_output can inspect either place for the
521    // terminal_reason/cost telemetry envelope. Only skip parsing when both are
522    // empty so we still attempt recovery when stderr contains the envelope.
523    let combined_output = format!("{}{}", session_output.raw_stdout, session_output.raw_stderr);
524    if !(session_output.raw_stdout.is_empty() && session_output.raw_stderr.is_empty()) {
525        match parse_success_output(task_id, agent_name, agent_runner, &combined_output) {
526            Ok(parsed) => return Ok(parsed),
527            Err(err) => {
528                if session_output.exit_code == 124
529                    && let Some(summary) = salvage_wrapup(&combined_output)
530                {
531                    tracing::warn!(
532                        task_id,
533                        agent = agent_name,
534                        "timed out after commit during wrap-up, salvaging as done"
535                    );
536                    return Ok(agents::ParsedResponse {
537                        response: AgentResponse {
538                            status: "done".to_string(),
539                            summary,
540                            ..Default::default()
541                        },
542                        input_tokens: None,
543                        output_tokens: None,
544                        cost_usd: None,
545                        duration_ms: None,
546                    });
547                }
548                // If parse_success_output already returned a classified error
549                // (RateLimit, AgentFailed, Timeout, Auth, etc.), propagate it
550                // directly — only InvalidResponse (unparseable output) should
551                // be intercepted by the terminal_reason:completed / cost-telemetry
552                // guard below. Without this, e.g. a claude session-limit error
553                // with terminal_reason=completed would be downgraded from RateLimit
554                // to InvalidResponse → Failed, losing the correct cooldown/reroute.
555                if !matches!(err, agents::AgentError::InvalidResponse { .. }) {
556                    return Err(err);
557                }
558                if session_output.exit_code != 0 {
559                    // kimi/minimax/glm sometimes emit valid NDJSON with
560                    // "terminal_reason":"completed" or cost-only telemetry
561                    // ("costUSD":) but exit 1 due to a post-session hook or
562                    // minor CLI issue. Scanning the tail would just grab JSON
563                    // metadata, producing a garbled error. Detect either
564                    // completion signal and treat as a soft parse error so the
565                    // task re-routes instead of hitting a false cooldown from
566                    // Unknown or Auth error variants. Use the combined output
567                    // (stdout+stderr) so envelopes written to stderr are
568                    // observed.
569                    let combined = &combined_output;
570                    let is_completed_signal =
571                        combined.contains("\"terminal_reason\":\"completed\"");
572                    // GLM emits a cost-telemetry JSON object (containing
573                    // "costUSD":) as its final stdout/stderr line on clean
574                    // exit even when the process exits with code 1. If the
575                    // only content is such telemetry (no is_error=true in the
576                    // envelope), treat it the same as terminal_reason:completed.
577                    let is_cost_telemetry_only = combined.contains("\"costUSD\":")
578                        && !combined.contains("\"is_error\":true");
579                    if is_completed_signal || is_cost_telemetry_only {
580                        let raw_tail = agents::patterns::safe_tail(combined, 300);
581                        return Err(agents::AgentError::InvalidResponse {
582                            raw: raw_tail.to_string(),
583                        });
584                    }
585                    return Err(agent_runner.classify_error_with_elapsed(
586                        session_output.exit_code,
587                        &session_output.raw_stdout,
588                        &session_output.raw_stderr,
589                        session_output.elapsed_secs,
590                    ));
591                }
592                if !has_result_envelope(&combined_output) {
593                    return Err(agents::AgentError::Interrupted {
594                        message: "exited without a result envelope".to_string(),
595                    });
596                }
597                return Err(err);
598            }
599        }
600    }
601
602    if session_output.exit_code != 0 {
603        return Err(agent_runner.classify_error_with_elapsed(
604            session_output.exit_code,
605            &session_output.raw_stdout,
606            &session_output.raw_stderr,
607            session_output.elapsed_secs,
608        ));
609    }
610
611    Err(agents::AgentError::Unknown {
612        exit_code: session_output.exit_code,
613        message: "empty-output-exit0: opencode returned exit 0 with empty stdout".to_string(),
614    })
615}
616
617/// The repo has no project directory: not in the active config, no legacy
618/// `project_dir`, no bare clone. It fails before any agent starts
619#[derive(Debug)]
620pub(crate) struct NoProjectDir {
621    pub(crate) repo: String,
622    pub(crate) bare_clone: PathBuf,
623}
624
625impl NoProjectDir {
626    /// Block reason for a task of this repo
627    pub(crate) const BLOCK_REASON: &str = "project is not configured and has no bare clone";
628}
629
630impl fmt::Display for NoProjectDir {
631    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
632        write!(
633            f,
634            "no resolvable project directory for repo {} — not in active config, no bare clone at {}, no legacy project_dir set",
635            self.repo,
636            self.bare_clone.display()
637        )
638    }
639}
640
641impl error::Error for NoProjectDir {}
642
643/// Task runner configuration.
644pub struct TaskRunner {
645    /// Repository slug (owner/repo)
646    repo: String,
647    /// Path to the orch home directory
648    orch_home: PathBuf,
649    /// Unified task store for run audit trail
650    store: Option<Arc<TaskStore>>,
651}
652
653impl Debug for TaskRunner {
654    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
655        f.debug_struct("TaskRunner")
656            .field("repo", &self.repo)
657            .field("orch_home", &self.orch_home)
658            .finish_non_exhaustive()
659    }
660}
661
662impl TaskRunner {
663    pub fn new(repo: String) -> Self {
664        let orch_home = home::orch_home().unwrap_or_else(|e| {
665            tracing::warn!(error = %e, "orch_home() failed — falling back to /tmp/.orch");
666            PathBuf::from("/tmp").join(".orch")
667        });
668
669        Self {
670            repo,
671            orch_home,
672            store: None,
673        }
674    }
675
676    /// Set the unified task store for run audit trail.
677    #[must_use]
678    pub fn with_store(mut self, store: Arc<TaskStore>) -> Self {
679        self.store = Some(store);
680        self
681    }
682
683    /// Read a field from the task store.
684    async fn get_field(&self, task_id: &str, field: &str) -> Option<String> {
685        let task = store::opt_store_get_task(&self.store, &self.repo, task_id).await?;
686        match field {
687            "route_reason" => Some(task.route_reason),
688            "worktree" => Some(task.worktree),
689            "attempts" => Some(task.attempts.to_string()),
690            "delegations" => serde_json::to_string(&task.delegations).ok(),
691            "summary" => Some(task.summary),
692            "last_error" => Some(task.last_error),
693            "agent" => task.agent,
694            _ => None,
695        }
696    }
697
698    async fn build_run_audit(&self, input: RunAuditInput<'_>) -> RunAudit {
699        // Filter empty strings so that a blank last_error does not shadow the
700        // actual parse_result error.  Without this, `Option::or(Some(""))` would
701        // return `Some("")` and prevent the `unwrap_or_else` fallback from ever
702        // running, causing task_runs.error to be written as "" even when the
703        // agent returned a classifiable error (fixes issue #2527).
704        let last_error = self
705            .get_field(input.task_id, "last_error")
706            .await
707            .filter(|s| !s.is_empty());
708        let outcome =
709            classify_run_outcome(input.status, input.parse_result, input.push_failed).to_string();
710
711        let error = if outcome == "success" {
712            String::new()
713        } else {
714            // Prefer this run's parsed error over a last_error left by a previous
715            // agent's failover. last_error stays the Err/EarlyReturn fallback (#3851)
716            let error = input
717                .error_override
718                .as_deref()
719                .filter(|s| !s.is_empty())
720                .map(str::to_string)
721                .or_else(|| match input.parse_result {
722                    Ok(parsed) => parsed
723                        .response
724                        .error
725                        .as_deref()
726                        .filter(|s| !s.is_empty())
727                        .map(str::to_string),
728                    Err(_) => None,
729                })
730                .or(last_error)
731                .unwrap_or_else(|| match input.parse_result {
732                    Ok(_) => String::new(),
733                    Err(err) => err.to_string(),
734                });
735
736            if error.is_empty() {
737                if input.status == "blocked" {
738                    match input.parse_result {
739                        Ok(parsed) if !parsed.response.summary.is_empty() => {
740                            parsed.response.summary.clone()
741                        }
742                        _ => "task blocked without explicit error".to_string(),
743                    }
744                } else if matches!(
745                    input.status,
746                    "done"
747                        | "completed"
748                        | "in_progress"
749                        | "in_review"
750                        | "needs_review"
751                        | "routed"
752                        | "new"
753                ) {
754                    // Canonical non-success statuses have no error message (e.g. "routed").
755                    String::new()
756                } else {
757                    // Non-canonical status with no error message — surface the status
758                    // explicitly so operators can identify which statuses need to be
759                    // added to the normalization map in parser.rs.
760                    format!("unrecognized status: {}", input.status)
761                }
762            } else {
763                error
764            }
765        };
766
767        let total_cost_usd = input
768            .parse_result
769            .as_ref()
770            .ok()
771            .and_then(|p| p.cost_usd)
772            .unwrap_or(0.0);
773
774        let (input_tokens, output_tokens) = extract_run_tokens(input.parse_result);
775
776        #[expect(clippy::cast_precision_loss, reason = "a run duration in seconds")]
777        let duration_secs = input
778            .elapsed_secs
779            .map_or_else(|| run_duration_secs(input.started_at), |s| s as f64);
780
781        RunAudit {
782            stdout: input.raw_stdout.to_string(),
783            stderr: input.raw_stderr.to_string(),
784            parsed_response: serialize_parsed_response(input.parse_result, input.raw_stdout),
785            outcome,
786            error,
787            input_tokens,
788            output_tokens,
789            total_cost_usd,
790            duration_secs,
791        }
792    }
793
794    /// The blocked outcome for a sandbox that cannot start. No agent ran, so none is cooled down
795    async fn sandbox_blocked(&self, task_id: &str, reason: String) -> RunExecution {
796        tracing::error!(task_id, error = %reason, "blocking task");
797        if let Err(e) = store::store_set_result(
798            &self.store,
799            &self.repo,
800            task_id,
801            &[
802                ("last_error", serde_json::json!(reason)),
803                ("block_reason", serde_json::json!(reason)),
804            ],
805        )
806        .await
807        {
808            tracing::warn!(task_id, err = %e, "failed to write the sandbox block_reason");
809        }
810        RunExecution {
811            status: "blocked".to_string(),
812            exit_code: None,
813            audit: RunAudit {
814                stdout: String::new(),
815                stderr: String::new(),
816                parsed_response: String::new(),
817                outcome: "failed".to_string(),
818                error: reason,
819                input_tokens: 0,
820                output_tokens: 0,
821                total_cost_usd: 0.0,
822                duration_secs: 0.0,
823            },
824        }
825    }
826
827    /// Run a task through the full execution pipeline.
828    ///
829    /// Returns `Ok(None)` if the runner guard skipped the task (caller should not
830    /// post stale results). Returns `Ok(Some(status))` with the final task status.
831    pub async fn run(
832        &self,
833        task_id: &str,
834        agent: Option<&str>,
835        model: Option<&str>,
836        backend: Option<&dyn ExternalBackend>,
837        started_at: &chrono::DateTime<Utc>,
838    ) -> anyhow::Result<Option<RunExecution>> {
839        tracing::info!(
840            task_id,
841            agent = agent.unwrap_or("default"),
842            model = model.unwrap_or("default"),
843            "starting task execution"
844        );
845
846        // Check task guards; returns outcome indicating whether to proceed.
847        let attempts = match task_init::check_guards(task_id, &self.repo, &self.store).await {
848            Ok(task_init::GuardOutcome::Proceed(a)) => a,
849            Ok(task_init::GuardOutcome::Skip) => {
850                return Ok(None);
851            }
852            Ok(task_init::GuardOutcome::MaxAttempts) => {
853                // The caller marks the task blocked from the returned status, a task that ran out
854                // of attempts did not finish, so it must never reach done
855                if backend.is_some() {
856                    // If this was label-forced to a specific agent, remove the agent label
857                    // so that /retry can route to a different agent instead of looping.
858                    let route_reason = self
859                        .get_field(task_id, "route_reason")
860                        .await
861                        .unwrap_or_default();
862                    if route_reason.starts_with("label agent:") {
863                        let agent_label = route_reason.trim_start_matches("label ");
864                        let gh = GhHttp::new()?;
865                        match gh.remove_label(&self.repo, task_id, agent_label).await {
866                            Err(e) => {
867                                tracing::warn!(task_id, label = agent_label, err = %e, "failed to remove agent label after max attempts");
868                            }
869                            _ => {
870                                tracing::info!(
871                                    task_id,
872                                    label = agent_label,
873                                    "removed forced agent label after max attempts — /retry will use free routing"
874                                );
875                            }
876                        }
877                    }
878                }
879                return Ok(Some(RunExecution {
880                    status: "blocked".to_string(),
881                    exit_code: None,
882                    audit: RunAudit {
883                        stdout: String::new(),
884                        stderr: String::new(),
885                        parsed_response: String::new(),
886                        outcome: "failed".to_string(),
887                        error: "max attempts reached".to_string(),
888                        input_tokens: 0,
889                        output_tokens: 0,
890                        total_cost_usd: 0.0,
891                        duration_secs: 0.0,
892                    },
893                }));
894            }
895            Err(e) => return Err(e),
896        };
897
898        // Resolve project directory
899        let project_dir = self.resolve_project_dir().await?;
900
901        // Prepare task: worktree, context, prompts, and agent invocation
902        let init = task_init::prepare_task(
903            task_id,
904            agent,
905            model,
906            backend,
907            &self.repo,
908            &project_dir,
909            attempts,
910            &self.store,
911        )
912        .await?;
913
914        // A sandbox that cannot start blocks the task, which never runs without it. It is the
915        // operator's setup that fails, so no agent or model is cooled down
916        if init.invocation.sandbox
917            && let Err(e) = sandbox::argv(
918                &init.invocation,
919                &env::temp_dir().join("orch-sandbox-preflight"),
920            )
921            .await
922        {
923            return Ok(Some(self.sandbox_blocked(task_id, e.to_string()).await));
924        }
925
926        // Run agent session: spawn in tmux, wait for completion, collect output
927        let (tmux, tmux_session, session_output) = session::run_agent_session(
928            task_id,
929            &init.invocation,
930            &init.attempt_dir,
931            &self.orch_home,
932        )
933        .await;
934
935        // The pre-flight policy passed but the one built at spawn did not: block the same way
936        if let Some(reason) = session_output.sandbox_error.clone() {
937            return Ok(Some(self.sandbox_blocked(task_id, reason).await));
938        }
939
940        // Touch updated_at immediately after session exit so the stuck-task recovery
941        // timer resets before response_handler post-processing (git push, PR creation,
942        // status update) begins.  Without this, a session that ran longer than
943        // no_session_stuck_timeout (default 10 min) would be re-dispatched by the tick
944        // while the runner is still writing results.
945        store::store_touch_updated_at(&self.store, &self.repo, task_id).await;
946
947        // If silence detection (tick phase1b) already rerouted this task while the
948        // session was running, skip all fallback/review processing. Without this check the
949        // runner would call `fallback::handle_error` on the empty output, exhaust fallback
950        // agents, and mark the task `needs_review` — overwriting the rerouted status and
951        // triggering a spurious review cycle.
952        // Silence detection now sets Routed (with fallback agent) or NeedsReview (no fallback).
953        // Check for both, plus New for backward compatibility.
954        if let Some(stored) = store::opt_store_get_task(&self.store, &self.repo, task_id).await {
955            let silence_reset = matches!(
956                stored.status,
957                TaskStatus::New | TaskStatus::Routed | TaskStatus::NeedsReview
958            );
959            if silence_reset {
960                let status_str = stored.status.as_str();
961                tracing::info!(
962                    task_id,
963                    status = status_str,
964                    "task already reset by the engine — skipping fallback processing"
965                );
966                session::cleanup_session(task_id, &tmux, &tmux_session).await;
967                let silence_err = agents::patterns::classify_from_text(0, "silence detected");
968                let silence_parse: Result<agents::ParsedResponse, agents::AgentError> =
969                    Err(silence_err);
970                let audit = self
971                    .build_run_audit(RunAuditInput {
972                        task_id,
973                        status: status_str,
974                        parse_result: &silence_parse,
975                        raw_stdout: &session_output.raw_stdout,
976                        raw_stderr: &session_output.raw_stderr,
977                        started_at,
978                        error_override: Some(format!("task was reset by the engine while the session ran (status {status_str})")),
979                        elapsed_secs: session_output.elapsed_secs,
980                        push_failed: false,
981                    })
982                    .await;
983                return Ok(Some(RunExecution {
984                    status: status_str.to_string(),
985                    exit_code: Some(session_output.exit_code),
986                    audit,
987                }));
988            }
989        }
990
991        // Log raw output for debugging agent failures
992        let stdout_len = session_output.raw_stdout.len();
993        let stderr_len = session_output.raw_stderr.len();
994        let stdout_tail: String = session_output
995            .raw_stdout
996            .chars()
997            .rev()
998            .take(500)
999            .collect::<String>()
1000            .chars()
1001            .rev()
1002            .collect();
1003        let stderr_tail: String = session_output
1004            .raw_stderr
1005            .chars()
1006            .rev()
1007            .take(500)
1008            .collect::<String>()
1009            .chars()
1010            .rev()
1011            .collect();
1012        tracing::info!(
1013            task_id,
1014            exit_code = session_output.exit_code,
1015            stdout_len,
1016            stderr_len,
1017            stdout_tail = %stdout_tail,
1018            stderr_tail = %stderr_tail,
1019            "agent raw output"
1020        );
1021
1022        // Use agent-specific parsing where possible. Previously we only attempted
1023        // parse_success_output when exit_code == 0 which caused runs that emitted
1024        // valid NDJSON but exited non-zero (kimi / minimax) to be classed as
1025        // failures. Try parsing success output first when stdout is present; if
1026        // parsing succeeds treat as success regardless of exit code. Only fall
1027        // back to error classification when parsing fails and the process exit
1028        // code indicates an error.
1029        for window in agents::claude::rate_limit_windows(&session_output.raw_stdout) {
1030            cooldown::record_rate_limit_window(&init.agent_name, &window).await;
1031        }
1032        let agent_runner = agents::get_runner(&init.agent_name);
1033        let parse_result =
1034            parse_session_output(task_id, &init.agent_name, &*agent_runner, &session_output);
1035
1036        // Write structured result.json for deterministic testing and debugging
1037        response_handler::write_result_json(
1038            &init.attempt_dir,
1039            task_id,
1040            &init.agent_name,
1041            init.model_name.as_deref(),
1042            session_output.exit_code,
1043            init.new_attempts,
1044            &parse_result,
1045            &session_output.raw_stdout,
1046            &session_output.raw_stderr,
1047        )
1048        .await;
1049
1050        // Handle outcome: success or error recovery
1051        // `fallback_error` carries the attributed error message from `handle_error()` when
1052        // the Continue path is taken.  It is passed as `error_override` to `build_run_audit`
1053        // so that stale `last_error` values from a previous agent's run are never written
1054        // into the current run's audit record (fixes misattribution bug: task_run shows
1055        // agent=kimi but error="minimax timed out...").
1056        let (final_status, push_failed, fallback_error) = match parse_result {
1057            Ok(ref parsed) => {
1058                let (status, push_failed) = response_handler::handle_success(
1059                    task_id,
1060                    parsed.clone(),
1061                    &init.wt,
1062                    &init.task_title,
1063                    &init.agent_name,
1064                    init.model_name.as_deref(),
1065                    init.new_attempts,
1066                    &self.repo,
1067                    &self.store,
1068                    &session_output.raw_stdout,
1069                )
1070                .await?;
1071                (status, push_failed, None)
1072            }
1073            Err(ref agent_err) => {
1074                match fallback::handle_error(
1075                    task_id,
1076                    agent_err,
1077                    &init.agent_name,
1078                    &*agent_runner,
1079                    init.model_name.as_deref(),
1080                    init.complexity.as_deref(),
1081                    init.new_attempts,
1082                    &self.store,
1083                    &self.repo,
1084                )
1085                .await?
1086                {
1087                    fallback::ErrorHandleResult::EarlyReturn { status } => {
1088                        // Task rerouted — clean up tmux session and env secrets
1089                        session::cleanup_session(task_id, &tmux, &tmux_session).await;
1090                        // The fallback handler already writes a sanitized `last_error`
1091                        // into the store for EarlyReturn paths (reroutes / needs_review).
1092                        // Passing the raw agent_err here would record noisy provider
1093                        // payloads (e.g. full api_retry NDJSON). Leave error_override
1094                        // as None so build_run_audit will prefer the store's last_error
1095                        // (see src/engine/runner/fallback.rs which persists a compact
1096                        // summary for rate-limit and reroute cases).
1097                        let audit = self
1098                            .build_run_audit(RunAuditInput {
1099                                task_id,
1100                                status: &status,
1101                                parse_result: &parse_result,
1102                                raw_stdout: &session_output.raw_stdout,
1103                                raw_stderr: &session_output.raw_stderr,
1104                                started_at,
1105                                error_override: None,
1106                                elapsed_secs: session_output.elapsed_secs,
1107                                push_failed: false,
1108                            })
1109                            .await;
1110                        return Ok(Some(RunExecution {
1111                            status,
1112                            exit_code: Some(session_output.exit_code),
1113                            audit,
1114                        }));
1115                    }
1116                    fallback::ErrorHandleResult::Continue { status, error } => {
1117                        (status, false, Some(error))
1118                    }
1119                }
1120            }
1121        };
1122
1123        // Kill tmux session if still alive
1124        session::cleanup_session(task_id, &tmux, &tmux_session).await;
1125
1126        // For no-op done tasks (agent finished with no code changes and no PR),
1127        // clean up the worktree and branch immediately — tmux session is already
1128        // dead so the tmux guard won't block removal.
1129        if final_status.as_str() == "done"
1130            && let Some(ref st) = self.store
1131            && let Err(e) = cleanup::cleanup_task_worktree(task_id, &self.repo, st).await
1132        {
1133            tracing::warn!(task_id, error = ?e, "worktree cleanup failed after no-op done");
1134        }
1135
1136        // Return final status and the session exit code so callers can record it.
1137        // Use `fallback_error` as error_override when available: it carries the error message
1138        // attributed to the current agent (set by handle_error), preventing stale last_error
1139        // values from a prior run from contaminating this run's audit record.
1140        let audit = self
1141            .build_run_audit(RunAuditInput {
1142                task_id,
1143                status: &final_status,
1144                parse_result: &parse_result,
1145                raw_stdout: &session_output.raw_stdout,
1146                raw_stderr: &session_output.raw_stderr,
1147                started_at,
1148                error_override: fallback_error,
1149                elapsed_secs: session_output.elapsed_secs,
1150                push_failed,
1151            })
1152            .await;
1153
1154        Ok(Some(RunExecution {
1155            status: final_status.clone(),
1156            exit_code: Some(session_output.exit_code),
1157            audit,
1158        }))
1159    }
1160
1161    /// Record task execution metrics to the database.
1162    #[expect(
1163        clippy::too_many_arguments,
1164        reason = "a metric row needs the agent, model, route, timing and outcome together"
1165    )]
1166    async fn record_metrics(
1167        &self,
1168        task_id: &str,
1169        agent_name: &str,
1170        model_name: Option<&str>,
1171        route_result: Option<&RouteResult>,
1172        started_at: &chrono::DateTime<Utc>,
1173        attempts: u32,
1174        final_status: &str,
1175        error_type: Option<&str>,
1176    ) {
1177        let completed_at = Utc::now();
1178        #[expect(clippy::cast_precision_loss, reason = "a run duration in seconds")]
1179        let duration_seconds = (completed_at - *started_at).num_milliseconds() as f64 / 1000.0;
1180
1181        // Classify outcome based on the final status and optional error_type/last_error
1182        let outcome = match final_status {
1183            "done" | "in_progress" | "in_review" => "success",
1184            "new" => "rerouted",
1185            "needs_review" => classify_run_error_type(error_type.unwrap_or("")),
1186            _ => "unknown",
1187        };
1188
1189        let complexity = route_result.map(|r| r.complexity.clone());
1190        let files_changed = git_ops::count_changed_files(&PathBuf::from(
1191            self.get_field(task_id, "worktree")
1192                .await
1193                .unwrap_or_default(),
1194        ))
1195        .await
1196        .unwrap_or(0);
1197
1198        if let Some(ref store) = self.store {
1199            // Only set error_type for non-success outcomes
1200            let db_error_type: Option<String> = if outcome == "success" {
1201                None
1202            } else {
1203                Some(outcome.to_string())
1204            };
1205
1206            let usage = store::get_token_usage(&self.store, &self.repo, task_id).await;
1207            let total_cost = store::get_reported_cost(&self.store, &self.repo, task_id).await;
1208            let input_tokens = if usage.input_tokens > 0 {
1209                Some(i64::try_from(usage.input_tokens).unwrap_or(i64::MAX))
1210            } else {
1211                None
1212            };
1213            let output_tokens = if usage.output_tokens > 0 {
1214                Some(i64::try_from(usage.output_tokens).unwrap_or(i64::MAX))
1215            } else {
1216                None
1217            };
1218
1219            let metric = InsertTaskMetric {
1220                repo: &self.repo,
1221                task_id,
1222                agent: agent_name,
1223                model: model_name,
1224                complexity: complexity.as_deref(),
1225                outcome,
1226                duration_seconds,
1227                started_at,
1228                completed_at: &completed_at,
1229                attempts: i32::try_from(attempts.saturating_add(1)).unwrap_or(i32::MAX),
1230                files_changed: i32::try_from(files_changed).unwrap_or(i32::MAX),
1231                error_type: db_error_type.as_deref(),
1232                input_tokens,
1233                output_tokens,
1234                total_cost_usd: total_cost,
1235            };
1236
1237            if let Err(e) = store.insert_task_metric(&metric).await {
1238                tracing::error!(task_id, ?e, "failed to record task metrics");
1239            }
1240        }
1241    }
1242
1243    /// Run a task with full engine context (backend, tmux, capture).
1244    ///
1245    /// Called by the engine dispatch loop with richer context.
1246    /// Returns a `WeightSignal` for the engine to feed back to the router.
1247    pub async fn run_with_context(
1248        &self,
1249        task: &ExternalTask,
1250        backend: &Arc<dyn ExternalBackend>,
1251        _tmux: &Arc<TmuxManager>,
1252        route_result: Option<&RouteResult>,
1253    ) -> anyhow::Result<WeightSignal> {
1254        let task_id = &task.id.0;
1255        let agent = route_result.map(|r| r.agent.as_str());
1256        let agent_name = agent.unwrap_or("claude").to_string();
1257        let model = route_result.and_then(|r| r.model.as_deref());
1258
1259        // Record start time for metrics (before any work begins)
1260        let started_at = Utc::now();
1261
1262        // Store task info for prompt building (title/body already in store tasks table)
1263
1264        // Record run start in task_runs audit trail
1265        let run_audit_id = if let Some(ref store) = self.store {
1266            let attempt: i32 = self
1267                .get_field(task_id, "attempts")
1268                .await
1269                .and_then(|s| s.parse().ok())
1270                .unwrap_or(0)
1271                + 1;
1272            match store.resolve_task_id(&self.repo, task_id).await {
1273                Ok(Some(store_id)) => {
1274                    match store
1275                        .start_run(&crate::store::StartRun {
1276                            task_id: store_id,
1277                            attempt,
1278                            run_type: "agent",
1279                            agent: &agent_name,
1280                            model: model.unwrap_or(""),
1281                            command: &format!(
1282                                "{} --model {}",
1283                                agent_name,
1284                                model.unwrap_or("default")
1285                            ),
1286                            prompt: &task.body,
1287                        })
1288                        .await
1289                    {
1290                        Ok(run_id) => Some(run_id),
1291                        Err(e) => {
1292                            tracing::warn!(
1293                                task_id,
1294                                error = %e,
1295                                "failed to record run start in audit trail"
1296                            );
1297                            None
1298                        }
1299                    }
1300                }
1301                _ => None,
1302            }
1303        } else {
1304            None
1305        };
1306
1307        // Run the task.
1308        // Always finalize the run audit row, even on early-return/error paths.
1309        let run_result = match self
1310            .run(task_id, agent, model, Some(&**backend), &started_at)
1311            .await
1312        {
1313            Ok(run_result) => run_result,
1314            Err(e) => {
1315                if let Some(run_id) = run_audit_id
1316                    && let Some(ref store) = self.store
1317                    && let Err(complete_err) = store
1318                        .complete_run(&crate::store::CompleteRun {
1319                            run_id,
1320                            exit_code: Some(-1),
1321                            stdout: "",
1322                            stderr: "",
1323                            parsed: "",
1324                            outcome: "failed",
1325                            error: &e.to_string(),
1326                            tokens: RunTokenUsage::default(),
1327                        })
1328                        .await
1329                {
1330                    tracing::warn!(
1331                        task_id,
1332                        run_id,
1333                        error = %complete_err,
1334                        "failed to record run completion in audit trail after runner error"
1335                    );
1336                }
1337                return Err(e);
1338            }
1339        };
1340
1341        // If the runner guard skipped the task, do not re-post stale data as a new comment.
1342        let (status, exit_code_opt, run_audit) = if let Some(result) = run_result {
1343            (result.status, result.exit_code, result.audit)
1344        } else {
1345            if let Some(run_id) = run_audit_id
1346                && let Some(ref store) = self.store
1347                && let Err(e) = store
1348                    .complete_run(&crate::store::CompleteRun {
1349                        run_id,
1350                        exit_code: Some(-1),
1351                        stdout: "",
1352                        stderr: "",
1353                        parsed: "",
1354                        outcome: "aborted",
1355                        error: "run skipped by guard",
1356                        tokens: RunTokenUsage::default(),
1357                    })
1358                    .await
1359            {
1360                tracing::warn!(
1361                    task_id,
1362                    run_id,
1363                    error = %e,
1364                    "failed to record skipped run completion in audit trail"
1365                );
1366            }
1367            tracing::info!(task_id, "guard skipped task — not posting stale result");
1368            return Ok(WeightSignal::None);
1369        };
1370
1371        // Process delegations if the agent requested subtasks
1372        let delegations_raw = self
1373            .get_field(task_id, "delegations")
1374            .await
1375            .unwrap_or_default();
1376        if !delegations_raw.is_empty() {
1377            match serde_json::from_str::<Vec<Delegation>>(&delegations_raw) {
1378                Ok(delegations) if !delegations.is_empty() => {
1379                    self.process_delegations(task, &delegations, backend)
1380                        .await?;
1381                    if let Err(e) = store::store_set_result(
1382                        &self.store,
1383                        &self.repo,
1384                        task_id,
1385                        &[("delegations", serde_json::json!([]))],
1386                    )
1387                    .await
1388                    {
1389                        tracing::warn!(task_id, err = %e, "failed to clear delegations in store");
1390                    }
1391                }
1392                Ok(_) => {} // empty list, nothing to do
1393                Err(e) => {
1394                    tracing::error!(task_id, error = %e, "corrupt delegations JSON — clearing");
1395                    if let Err(e2) = store::store_set_result(
1396                        &self.store,
1397                        &self.repo,
1398                        task_id,
1399                        &[("delegations", serde_json::json!([]))],
1400                    )
1401                    .await
1402                    {
1403                        tracing::warn!(task_id, err = %e2, "failed to clear corrupt delegations in store");
1404                    }
1405                    if let Err(e2) = store::store_set_result(
1406                        &self.store,
1407                        &self.repo,
1408                        task_id,
1409                        &[(
1410                            "last_error",
1411                            serde_json::json!(format!("delegation parse failed: {e}")),
1412                        )],
1413                    )
1414                    .await
1415                    {
1416                        tracing::warn!(task_id, err = %e2, "failed to write delegation_parse_failed last_error to store");
1417                    }
1418                }
1419            }
1420        }
1421
1422        // Post result to GitHub
1423        let summary = self.get_field(task_id, "summary").await.unwrap_or_default();
1424        let last_error = self
1425            .get_field(task_id, "last_error")
1426            .await
1427            .unwrap_or_default();
1428
1429        // Determine weight signal based on outcome.
1430        // Any reroute ("new"/"routed") should avoid review-gate and go back through
1431        // normal dispatch, regardless of why it was rerouted.
1432        let is_rerouted = status == "new" || status == "routed";
1433        if is_rerouted {
1434            // Check if silence detection (tick phase1b) already re-routed this task while
1435            // the session was running. Silence detection sets status to Routed/New and
1436            // stores the fallback agent — we must NOT overwrite those fields here.
1437            // If the stored status is still InProgress, silence detection did not fire
1438            // and we can safely clear agent/model for normal reroute.
1439            let silence_already_reset = store::opt_store_get_task(&self.store, &self.repo, task_id)
1440                .await
1441                .is_some_and(|t| !matches!(t.status, TaskStatus::InProgress));
1442
1443            if silence_already_reset {
1444                tracing::debug!(
1445                    task_id,
1446                    "silence detection already set fallback agent — skipping agent/model clear"
1447                );
1448            } else if let Err(e) = store::store_set_result(
1449                &self.store,
1450                &self.repo,
1451                task_id,
1452                &[
1453                    ("agent", serde_json::json!("")),
1454                    ("model", serde_json::json!("")),
1455                ],
1456            )
1457            .await
1458            {
1459                tracing::warn!(task_id, err = %e, "failed to clear agent/model for reroute in store");
1460            }
1461        }
1462        // Determine weight signal based on outcome.
1463        // Reroutes should only emit RateLimited when the underlying error is genuinely
1464        // a rate limit (not silence detection, timeouts, parse errors, etc.). This prevents
1465        // false cooldown cascades where non-rate-limit failures are treated as rate limits.
1466        let is_rate_limit_error = classify_run_error_type(&last_error) == "rate_limit";
1467        let weight_signal = if is_rerouted {
1468            if is_rate_limit_error {
1469                WeightSignal::RateLimited {
1470                    agent: agent_name.clone(),
1471                }
1472            } else {
1473                // Non-rate-limit reroutes (silence detection, timeout, parse error): signal
1474                // the engine to keep the task in "new" for re-routing. Using Rerouted (not
1475                // None) prevents tick from converting the silence-reset back to needs_review,
1476                // which would trigger the no-code review shortcut and falsely mark it done.
1477                WeightSignal::Rerouted
1478            }
1479        } else if status == "needs_review" {
1480            // Rate limit errors that escalate to needs_review must NOT reset backoff
1481            // counters (issue #2153).  These errors indicate the agent is genuinely
1482            // unavailable — resetting would wipe the cooldown and cause the next dispatch
1483            // to retry the same rate-limited agent immediately.
1484            //
1485            // Detection: classify_run_error_type() confirms "rate limit" in last_error.
1486            // Reuse the same classifier as `is_rate_limit_error` above rather than a raw
1487            // substring check — billing-cycle exhaustion messages (e.g. Kimi's "usage
1488            // limit for this billing cycle") also contain "usage limit"/"rate limit", so
1489            // a bare substring check would record a rate-limit event into the router's
1490            // health signal for a condition that isn't a genuine rate limit (issue #3529).
1491            let signal = needs_review_signal(&last_error, &agent_name);
1492            match &signal {
1493                WeightSignal::RateLimited { .. } => {
1494                    // Record in the metrics store so the router's health check detects it.
1495                    if let Some(ref store) = self.store
1496                        && let Err(e) = store
1497                            .record_rate_limit(&agent_name, "rate", Some(task_id))
1498                            .await
1499                    {
1500                        tracing::warn!(task_id, agent = %agent_name, err = %e, "failed to persist rate_limit event — router health check may miss this signal");
1501                    }
1502                    // Also record via the cooldown system so model_for_complexity() skips
1503                    // this model.
1504                    cooldown::record_rate_limit(&agent_name, model, &last_error).await;
1505                }
1506                WeightSignal::Success { .. } => {
1507                    // Reset backoff so the next failure starts fresh.
1508                    cooldown::record_agent_success(&agent_name, model.unwrap_or("")).await;
1509                }
1510                _ => {}
1511            }
1512            signal
1513        } else if status == "done" || status == "in_progress" || status == "in_review" {
1514            // Reset exponential backoff counters so the next failure starts fresh.
1515            cooldown::record_agent_success(&agent_name, model.unwrap_or("")).await;
1516            WeightSignal::Success {
1517                agent: agent_name.clone(),
1518            }
1519        } else if status == "blocked" {
1520            WeightSignal::Blocked
1521        } else {
1522            WeightSignal::None
1523        };
1524
1525        // Record metrics
1526        {
1527            let attempts: u32 = self
1528                .get_field(task_id, "attempts")
1529                .await
1530                .and_then(|s| s.parse().ok())
1531                .unwrap_or(0);
1532            self.record_metrics(
1533                task_id,
1534                &agent_name,
1535                model,
1536                route_result,
1537                &started_at,
1538                attempts,
1539                &status,
1540                if last_error.is_empty() {
1541                    None
1542                } else {
1543                    Some(&last_error)
1544                },
1545            )
1546            .await;
1547        }
1548
1549        // Complete run in task_runs audit trail (include exit code from runner when available)
1550        if let Some(run_id) = run_audit_id
1551            && let Some(ref store) = self.store
1552            && let Err(e) = store
1553                .complete_run(&crate::store::CompleteRun {
1554                    run_id,
1555                    exit_code: exit_code_opt,
1556                    stdout: &run_audit.stdout,
1557                    stderr: &run_audit.stderr,
1558                    parsed: &run_audit.parsed_response,
1559                    outcome: &run_audit.outcome,
1560                    error: &run_audit.error,
1561                    tokens: RunTokenUsage {
1562                        input_tokens: run_audit.input_tokens,
1563                        output_tokens: run_audit.output_tokens,
1564                        total_cost_usd: run_audit.total_cost_usd,
1565                        duration_secs: run_audit.duration_secs,
1566                    },
1567                })
1568                .await
1569        {
1570            tracing::warn!(
1571                task_id,
1572                run_id,
1573                error = %e,
1574                "failed to record run completion in audit trail"
1575            );
1576        }
1577
1578        // If task was rerouted (status=routed or new after run), update GitHub agent label
1579        // so the router doesn't re-route back to the same failed agent.
1580        if is_rerouted && !is_internal_id(task_id) {
1581            let new_agent = self.get_field(task_id, "agent").await.unwrap_or_default();
1582            if !new_agent.is_empty() && new_agent != agent_name {
1583                update_agent_label_after_reroute(
1584                    &self.repo,
1585                    task,
1586                    backend,
1587                    &agent_name,
1588                    &new_agent,
1589                )
1590                .await;
1591            }
1592        }
1593
1594        // Internal tasks: skip GitHub status/comment updates — dispatch phase handles status.
1595        if is_internal_id(task_id) {
1596            return Ok(weight_signal);
1597        }
1598
1599        // Update GitHub status
1600        let new_status = match status.as_str() {
1601            "done" => Status::Done,
1602            "in_progress" => Status::InProgress,
1603            "in_review" => Status::InReview,
1604            "blocked" => Status::Blocked,
1605            "routed" => Status::Routed, // Rerouted (agent/model already set)
1606            "new" => Status::New,       // Legacy reroute path
1607            _ => Status::NeedsReview,
1608        };
1609        // Store-first: update SQLite (must succeed), then mirror to backend.
1610        if let Some(ref store) = self.store
1611            && let Ok(Some(store_id)) = store.resolve_task_id(&self.repo, &task.id.0).await
1612        {
1613            store
1614                .update_status(store_id, tasks::status_to_task_status(new_status))
1615                .await
1616                .context("store-first status update failed in runner")?;
1617        }
1618        if let Err(e) = backend.update_status(&task.id, new_status).await {
1619            tracing::warn!(
1620                task_id,
1621                ?new_status,
1622                err = %e,
1623                "failed to mirror runner status to backend — store is authoritative"
1624            );
1625        }
1626
1627        let now = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ");
1628        let mut comment = if !summary.is_empty() {
1629            format!("[{now}] {status}: {summary}")
1630        } else if !last_error.is_empty() {
1631            format!("[{now}] {status}: {last_error}")
1632        } else {
1633            format!("[{now}] {status}")
1634        };
1635
1636        // Append visible attribution footer (matches PR body style)
1637        let footer = engine::attribution_footer("Created", &agent_name, model);
1638        comment.push_str(&footer);
1639
1640        backend.post_comment(&task.id, &comment).await?;
1641
1642        Ok(weight_signal)
1643    }
1644
1645    /// Process delegations from an agent response.
1646    ///
1647    /// Creates child GitHub issues for each delegation and marks the parent
1648    /// as blocked. The engine's Phase 4 unblock mechanism will re-activate
1649    /// the parent when all children are done.
1650    async fn process_delegations(
1651        &self,
1652        parent_task: &ExternalTask,
1653        delegations: &[Delegation],
1654        backend: &Arc<dyn ExternalBackend>,
1655    ) -> anyhow::Result<()> {
1656        let parent_id = &parent_task.id;
1657
1658        // Fetch existing open tasks once for dedup — prevents creating duplicate
1659        // GitHub issues when the same delegations are produced on repeated runs.
1660        let existing_titles: HashSet<String> = match backend.list_all_tasks().await {
1661            Ok(tasks) => tasks.into_iter().map(|t| t.title).collect(),
1662            Err(e) => {
1663                tracing::warn!(err = %e, "failed to fetch tasks for dedup — will create all");
1664                HashSet::new()
1665            }
1666        };
1667
1668        for delegation in delegations {
1669            // Skip if an issue with the same title already exists
1670            if existing_titles.contains(&delegation.title) {
1671                tracing::info!(
1672                    parent = parent_id.0,
1673                    title = delegation.title,
1674                    "skipping delegation — issue with same title already exists"
1675                );
1676                continue;
1677            }
1678
1679            // Build labels: status:new + any labels from the delegation
1680            let mut labels = delegation.labels.clone();
1681            labels.push("status:new".to_string());
1682
1683            // Build child body with delegation reference
1684            let child_body = format!(
1685                "{}\n\n---\n_Delegated from #{}_{}",
1686                delegation.body,
1687                parent_id.0,
1688                engine::orch_footer()
1689            );
1690
1691            match backend
1692                .create_sub_task(parent_id, &delegation.title, &child_body, &labels)
1693                .await
1694            {
1695                Ok(child_id) => {
1696                    tracing::info!(
1697                        parent = parent_id.0,
1698                        child = child_id.0,
1699                        title = delegation.title,
1700                        "created delegated subtask"
1701                    );
1702                }
1703                Err(e) => {
1704                    tracing::error!(
1705                        parent = parent_id.0,
1706                        title = delegation.title,
1707                        err = %e,
1708                        "failed to create delegated subtask"
1709                    );
1710                }
1711            }
1712        }
1713
1714        // Mark parent as blocked — store-first, then mirror to backend.
1715        if let Some(ref store) = self.store
1716            && let Ok(Some(store_id)) = store.resolve_task_id(&self.repo, &parent_id.0).await
1717        {
1718            store
1719                .update_status(store_id, TaskStatus::Blocked)
1720                .await
1721                .context("store-first: failed to block parent in store")?;
1722        }
1723        if let Err(e) = backend.update_status(parent_id, Status::Blocked).await {
1724            tracing::warn!(
1725                parent = parent_id.0,
1726                err = %e,
1727                "failed to mirror blocked status to backend — store is authoritative"
1728            );
1729        }
1730
1731        // Post summary comment on parent
1732        let summary = delegations
1733            .iter()
1734            .enumerate()
1735            .map(|(i, d)| format!("{}. {}", i + 1, d.title))
1736            .collect::<Vec<_>>()
1737            .join("\n");
1738
1739        backend
1740            .post_comment(
1741                parent_id,
1742                &format!(
1743                    "Delegated {} subtask(s):\n\n{}\n\nParent task is blocked until all subtasks complete.{}",
1744                    delegations.len(),
1745                    summary,
1746                    engine::orch_footer()
1747                ),
1748            )
1749            .await?;
1750
1751        Ok(())
1752    }
1753
1754    /// Resolve the project directory for this repo.
1755    pub(crate) async fn resolve_project_dir(&self) -> anyhow::Result<PathBuf> {
1756        // Explicit env var always wins
1757        if let Ok(dir) = env::var("PROJECT_DIR")
1758            && !dir.is_empty()
1759        {
1760            return Ok(PathBuf::from(dir));
1761        }
1762
1763        // Check projects list in global config — find path for this repo
1764        if let Ok(paths) = config::get_project_paths() {
1765            for path_str in &paths {
1766                let path = PathBuf::from(path_str);
1767                // Check if this project's .orch.yml has matching repo
1768                if let Ok(repo) = config::get_repo_for_project(&path) {
1769                    // Use tokio::fs::metadata for async-safe existence check
1770                    let path_for_check = path.clone();
1771                    if repo == self.repo && fs::metadata(&path_for_check).await.is_ok() {
1772                        return Ok(path);
1773                    }
1774                }
1775            }
1776        }
1777
1778        // Legacy: check config project_dir
1779        if let Ok(dir) = config::get("project_dir")
1780            && !dir.is_empty()
1781        {
1782            let path = PathBuf::from(&dir);
1783            let path_for_check = path.clone();
1784            if fs::metadata(&path_for_check).await.is_ok() {
1785                return Ok(path);
1786            }
1787        }
1788
1789        // Check for bare clone
1790        let parts: Vec<&str> = self.repo.split('/').collect();
1791        if parts.len() == 2 {
1792            let bare = self
1793                .orch_home
1794                .join("projects")
1795                .join(parts[0])
1796                .join(format!("{}.git", parts[1]));
1797            let bare_for_check = bare.clone();
1798            if task::spawn_blocking(move || bare_for_check.exists())
1799                .await
1800                .unwrap_or(false)
1801            {
1802                return Ok(bare);
1803            }
1804        }
1805
1806        // No resolvable project directory: not in active config, no legacy
1807        // project_dir, no bare clone. Falling back to the service's own CWD
1808        // here would silently point worktree creation at an unrelated
1809        // directory (see issue #3416) — surface a clear error instead.
1810        Err(NoProjectDir {
1811            repo: self.repo.clone(),
1812            bare_clone: self
1813                .orch_home
1814                .join("projects")
1815                .join(self.repo.split('/').next().unwrap_or_default())
1816                .join(format!(
1817                    "{}.git",
1818                    self.repo.split('/').nth(1).unwrap_or_default()
1819                )),
1820        }
1821        .into())
1822    }
1823}
1824
1825/// Update the GitHub agent label on an external task when it is rerouted to a new agent.
1826///
1827/// Removes the old `agent:<old_agent>` label, ensures the new `agent:<new_agent>` label exists
1828/// in the repo, then applies it. Returns `true` only when both `ensure_label` (repo label creation)
1829/// and `set_labels` (issue label assignment) succeed. `remove_label` failures are non-fatal.
1830async fn update_agent_label_after_reroute(
1831    #[cfg_attr(
1832        test,
1833        expect(unused_variables, reason = "only the label call outside tests uses it")
1834    )]
1835    repo: &str,
1836    task: &ExternalTask,
1837    backend: &Arc<dyn ExternalBackend>,
1838    old_agent: &str,
1839    new_agent: &str,
1840) -> bool {
1841    let old_label = format!("agent:{old_agent}");
1842    if let Err(e) = backend.remove_label(&task.id, &old_label).await {
1843        tracing::warn!(
1844            task_id = ?task.id,
1845            label = old_label,
1846            error = %e,
1847            "failed to remove old agent label during re-route"
1848        );
1849    }
1850    let new_label = format!("agent:{new_agent}");
1851
1852    // ensure_label creates the repo-level label if missing (best-effort).
1853    // Skipped under #[cfg(test)] to avoid real GitHub API calls.
1854    #[cfg(not(test))]
1855    {
1856        match GhHttp::new() {
1857            Ok(gh) => {
1858                if gh
1859                    .ensure_label(
1860                        repo,
1861                        &new_label,
1862                        http::status_label_color(&new_label),
1863                        &format!("Agent: {new_agent}"),
1864                    )
1865                    .await
1866                    .is_err()
1867                {
1868                    tracing::warn!(
1869                        task_id = ?task.id,
1870                        label = %new_label,
1871                        "ensure_label failed during agent label update after failover"
1872                    );
1873                }
1874            }
1875            Err(e) => {
1876                tracing::warn!(
1877                    task_id = ?task.id,
1878                    err = %e,
1879                    "GhHttp::new() failed — skipping ensure_label"
1880                );
1881            }
1882        }
1883    }
1884
1885    if let Err(e) = backend
1886        .set_labels(&task.id, slice::from_ref(&new_label))
1887        .await
1888    {
1889        tracing::warn!(
1890            task_id = ?task.id,
1891            label = %new_label,
1892            error = %e,
1893            "set_labels failed during agent label update after failover"
1894        );
1895        return false;
1896    }
1897    true
1898}
1899
1900/// Weight signal for a run that ended in `needs_review`. `prepare_task` clears `last_error` at run
1901/// start, so a non-empty value means `handle_error` ended this run
1902fn needs_review_signal(last_error: &str, agent: &str) -> WeightSignal {
1903    let agent = agent.to_string();
1904    if classify_run_error_type(last_error) == "rate_limit" {
1905        WeightSignal::RateLimited { agent }
1906    } else if !last_error.is_empty() {
1907        WeightSignal::Failed { agent }
1908    } else {
1909        WeightSignal::Success { agent }
1910    }
1911}
1912
1913#[cfg(test)]
1914mod tests {
1915    use super::*;
1916    use crate::backends::{ExternalId, ExternalTask, Mention, Status};
1917    use crate::engine::runner::agents::patterns::safe_tail as safe_utf8_tail;
1918    use crate::parser::AgentResponse;
1919    use crate::store::{NewTask, TaskStore};
1920    use async_trait::async_trait;
1921    use chrono::Utc;
1922    use std::sync::{Arc, Mutex};
1923    use tempfile::TempDir;
1924    use tokio::process::Command;
1925
1926    static ENV_LOCK: Mutex<()> = Mutex::new(());
1927
1928    fn ok_parse_result(status: &str) -> agents::ParsedResponse {
1929        agents::ParsedResponse {
1930            response: AgentResponse {
1931                status: status.to_string(),
1932                summary: String::new(),
1933                accomplished: vec![],
1934                remaining: vec![],
1935                files: vec![],
1936                error: None,
1937                input_tokens: None,
1938                output_tokens: None,
1939                learnings: vec![],
1940                delegations: vec![],
1941            },
1942            input_tokens: None,
1943            output_tokens: None,
1944            cost_usd: None,
1945            duration_ms: None,
1946        }
1947    }
1948
1949    // ── safe_utf8_tail ───────────────────────────────────────────────────────
1950
1951    #[test]
1952    fn safe_utf8_tail_short_string_returned_as_is() {
1953        assert_eq!(safe_utf8_tail("hello", 100), "hello");
1954        assert_eq!(safe_utf8_tail("", 10), "");
1955    }
1956
1957    #[test]
1958    fn safe_utf8_tail_truncates_ascii() {
1959        let s = "abcdefghij"; // 10 bytes
1960        assert_eq!(safe_utf8_tail(s, 5), "fghij");
1961        assert_eq!(safe_utf8_tail(s, 10), "abcdefghij");
1962    }
1963
1964    #[test]
1965    fn safe_utf8_tail_handles_multibyte_boundary() {
1966        // "日本語" is 3 chars × 3 bytes each = 9 bytes total
1967        let s = "日本語";
1968        assert_eq!(s.len(), 9);
1969        // Cutting at 8 bytes lands in the middle of "語" (3 bytes at 6..9).
1970        // safe_utf8_tail should walk forward to the next boundary at 9 (end) rather
1971        // than panicking on an invalid slice.
1972        let tail = safe_utf8_tail(s, 8);
1973        assert!(
1974            s.ends_with(tail),
1975            "tail must be a valid suffix of the original"
1976        );
1977        assert!(
1978            str::from_utf8(tail.as_bytes()).is_ok(),
1979            "tail must be valid UTF-8"
1980        );
1981    }
1982
1983    #[test]
1984    fn safe_utf8_tail_handles_mixed_ascii_and_multibyte() {
1985        let s = "hello日本語world"; // ascii + CJK + ascii
1986        for max_bytes in [1, 3, 5, 7, 11, 13, 20, 100] {
1987            let tail = safe_utf8_tail(s, max_bytes);
1988            assert!(
1989                str::from_utf8(tail.as_bytes()).is_ok(),
1990                "max_bytes={max_bytes}: tail must be valid UTF-8"
1991            );
1992            assert!(
1993                s.ends_with(tail),
1994                "max_bytes={max_bytes}: tail must be a suffix of original"
1995            );
1996        }
1997    }
1998
1999    #[test]
2000    fn safe_utf8_tail_exact_boundary() {
2001        let s = "abcdef"; // 6 bytes
2002        assert_eq!(safe_utf8_tail(s, 6), "abcdef"); // exactly fits
2003        assert_eq!(safe_utf8_tail(s, 0), ""); // empty tail
2004    }
2005
2006    #[test]
2007    fn parse_success_output_synthesizes_done_from_plain_text() {
2008        let runner = agents::get_runner("claude");
2009        let parsed = parse_success_output(
2010            "123",
2011            "claude",
2012            &*runner,
2013            "No open positions, no trade executions, no conditions change.",
2014        )
2015        .unwrap();
2016
2017        assert_eq!(parsed.response.status, "done");
2018        assert_eq!(
2019            parsed.response.summary,
2020            "No open positions, no trade executions, no conditions change."
2021        );
2022    }
2023
2024    #[test]
2025    fn parse_success_output_synthesizes_needs_review_from_error_text() {
2026        let runner = agents::get_runner("claude");
2027        let parsed =
2028            parse_success_output("123", "claude", &*runner, "Failed to update branch").unwrap();
2029
2030        assert_eq!(parsed.response.status, "needs_review");
2031        assert_eq!(
2032            parsed.response.error.as_deref(),
2033            Some("agent returned plain text instead of JSON")
2034        );
2035    }
2036
2037    #[test]
2038    fn parse_success_output_keeps_empty_text_invalid() {
2039        let runner = agents::get_runner("claude");
2040        let err = parse_success_output("123", "claude", &*runner, "").unwrap_err();
2041
2042        assert!(matches!(err, agents::AgentError::InvalidResponse { .. }));
2043    }
2044
2045    #[test]
2046    fn parse_success_output_preserves_tokens_from_ndjson() {
2047        // Simulates NDJSON output from --output-format stream-json where
2048        // the result event contains token usage. parse_response fails on
2049        // NDJSON (expects single JSON blob), but find_agent_result extracts
2050        // tokens from the NDJSON envelope so cost tracking is preserved.
2051        let ndjson = r#"{"type":"system","subtype":"init","session_id":"abc"}
2052{"type":"assistant","message":{"content":[{"type":"text","text":"Done."}],"usage":{"input_tokens":100,"output_tokens":50}}}
2053{"type":"result","subtype":"success","is_error":false,"duration_ms":5000,"result":"All changes committed successfully","usage":{"input_tokens":1234,"output_tokens":567}}"#;
2054        let runner = agents::get_runner("claude");
2055        let parsed = parse_success_output("456", "claude", &*runner, ndjson).unwrap();
2056
2057        assert_eq!(parsed.response.status, "done");
2058        assert_eq!(parsed.input_tokens, Some(1234));
2059        assert_eq!(parsed.output_tokens, Some(567));
2060        assert_eq!(parsed.duration_ms, Some(5000));
2061    }
2062
2063    #[test]
2064    fn parse_success_output_preserves_tokens_from_minimax_ndjson() {
2065        // MiniMax/Kimi use assistant event fallback when there's no result event.
2066        let ndjson = r#"{"type":"system","subtype":"init"}
2067{"type":"assistant","message":{"content":[{"type":"text","text":"No open positions."}],"usage":{"input_tokens":2000,"output_tokens":300}}}"#;
2068        let runner = agents::get_runner("minimax");
2069        let parsed = parse_success_output("789", "minimax", &*runner, ndjson).unwrap();
2070
2071        assert_eq!(parsed.response.status, "done");
2072        assert_eq!(parsed.input_tokens, Some(2000));
2073        assert_eq!(parsed.output_tokens, Some(300));
2074    }
2075
2076    #[test]
2077    fn classify_run_error_type_recognizes_specific_patterns() {
2078        let cases = [
2079            ("push failed: remote rejected", "push_failed"),
2080            ("No commits between main and branch", "no_commits"),
2081            ("create PR failed: 422", "pr_failed"),
2082            ("invalid response from agent", "parse_error"),
2083            ("parse error: missing json", "parse_error"),
2084            ("exceeded max attempts while rerouting", "max_attempts"),
2085            ("unit test failed", "ci_failure"),
2086            ("build failed: type mismatch", "ci_failure"),
2087            ("lint failed on warning", "ci_failure"),
2088            ("3 tests failed", "ci_failure"),
2089            ("test suite failed: 3 tests failed", "ci_failure"),
2090            (
2091                "claude interrupted: exited without a result envelope",
2092                "interrupted",
2093            ),
2094        ];
2095
2096        for (input, expected) in cases {
2097            assert_eq!(classify_run_error_type(input), expected, "input={input}");
2098        }
2099    }
2100
2101    #[test]
2102    fn classify_run_error_type_falls_back_for_unknown_errors() {
2103        assert_eq!(classify_run_error_type("something unexpected"), "failed");
2104    }
2105
2106    #[test]
2107    fn classify_run_error_type_connection_error_mentioning_pull_request_is_not_pr_failed() {
2108        // A generic GitHub API or connection error whose message happens to contain the
2109        // substring "pull request" must NOT be classified as pr_failed.
2110        assert_eq!(
2111            classify_run_error_type("failed to fetch pull request details from GitHub"),
2112            "failed"
2113        );
2114        assert_eq!(
2115            classify_run_error_type("connection reset while reading pull request data"),
2116            "failed"
2117        );
2118    }
2119
2120    #[test]
2121    fn classify_run_error_type_url_with_test_substring_is_not_ci_failure() {
2122        // A connection error whose URL path contains "/test-org/" must NOT be ci_failure.
2123        assert_eq!(
2124            classify_run_error_type(
2125                "failed to connect to api.github.com/repos/test-org/repo/pulls/123"
2126            ),
2127            "failed"
2128        );
2129        // Bare "test" word in an unrelated error must not trigger ci_failure.
2130        assert_eq!(
2131            classify_run_error_type("context deadline exceeded testing connection"),
2132            "failed"
2133        );
2134    }
2135
2136    #[test]
2137    fn classify_run_error_type_usage_without_limit_is_not_rate_limit() {
2138        // "usage" alone (e.g. token usage reporting) must NOT be rate_limit.
2139        assert_eq!(
2140            classify_run_error_type("token usage: 1245 tokens"),
2141            "failed"
2142        );
2143        assert_eq!(
2144            classify_run_error_type("incorrect usage of git command"),
2145            "failed"
2146        );
2147        // Only "usage limit" should trigger rate_limit.
2148        assert_eq!(
2149            classify_run_error_type("exceeded usage limit for this billing period"),
2150            "rate_limit"
2151        );
2152    }
2153
2154    #[test]
2155    fn classify_run_error_type_billing_cycle_is_not_auth_error() {
2156        // "billing cycle" quota-exhaustion messages must be classified as billing_cycle_exhausted,
2157        // not auth_error — aligning with cooldown.rs CreditExhaustionReason::BillingCycleExhausted.
2158        assert_eq!(
2159            classify_run_error_type(
2160                "You've reached your usage limit for this billing cycle. \
2161                 Your quota will be refreshed in the next cycle."
2162            ),
2163            "billing_cycle_exhausted"
2164        );
2165        assert_eq!(
2166            classify_run_error_type("quota exhausted for billing cycle"),
2167            "billing_cycle_exhausted"
2168        );
2169        // Plain "billing" without "cycle" that used to map to auth_error now falls through to "failed".
2170        assert_eq!(
2171            classify_run_error_type("billing account suspended"),
2172            "failed"
2173        );
2174    }
2175
2176    #[test]
2177    fn classify_run_error_type_monthly_quota_is_billing_cycle() {
2178        // Regression test for issue #3302: GitHub Copilot "You have exceeded your
2179        // monthly quota" must map to billing_cycle_exhausted, not "failed".
2180        assert_eq!(
2181            classify_run_error_type(
2182                "opencode failed: agent failed: You have exceeded your monthly quota"
2183            ),
2184            "billing_cycle_exhausted"
2185        );
2186        assert_eq!(
2187            classify_run_error_type("You have exceeded your monthly quota"),
2188            "billing_cycle_exhausted"
2189        );
2190    }
2191
2192    #[test]
2193    fn classify_run_outcome_routed_is_success() {
2194        // "routed" is a canonical non-success status (task will be re-dispatched).
2195        // It should be classified as "success" so the audit trail distinguishes it
2196        // from genuine failures (#2801).
2197        let parse_result = Ok(ok_parse_result("routed"));
2198        assert_eq!(
2199            classify_run_outcome("routed", &parse_result, false),
2200            "success"
2201        );
2202    }
2203
2204    #[test]
2205    fn classify_run_outcome_blocked_is_blocked() {
2206        let parse_result = Ok(ok_parse_result("blocked"));
2207        assert_eq!(
2208            classify_run_outcome("blocked", &parse_result, false),
2209            "blocked"
2210        );
2211    }
2212
2213    #[test]
2214    fn classify_run_outcome_non_canonical_is_failed() {
2215        // Non-canonical statuses should be classified as "failed" in the audit.
2216        // The explicit error message is provided by build_run_audit.
2217        let parse_result = Ok(ok_parse_result("fix_deployed"));
2218        assert_eq!(
2219            classify_run_outcome("fix_deployed", &parse_result, false),
2220            "failed"
2221        );
2222    }
2223
2224    #[test]
2225    fn classify_run_outcome_kimi_billing_cycle_is_not_rate_limit() {
2226        // Regression test for issue #3529: Kimi's exact billing-cycle exhaustion
2227        // message was stored as outcome="rate_limit" because the generic "usage
2228        // limit" pattern in detect_rate_limit() matched before credit-exhaustion
2229        // detection ever ran. The outcome must reflect the actual failure mode.
2230        let parse_result: Result<agents::ParsedResponse, agents::AgentError> =
2231            Err(agents::AgentError::RateLimit {
2232                message: "Failed to authenticate. API Error: 403 You've reached your usage \
2233                          limit for this billing cycle. Your quota will be refreshed soon."
2234                    .to_string(),
2235            });
2236        assert_eq!(
2237            classify_run_outcome("needs_review", &parse_result, false),
2238            "billing_cycle_exhausted"
2239        );
2240    }
2241
2242    #[test]
2243    fn classify_run_outcome_generic_rate_limit_is_unaffected() {
2244        // A genuine rate limit (no billing-cycle/credit-exhaustion markers) must
2245        // still classify as "rate_limit".
2246        let parse_result: Result<agents::ParsedResponse, agents::AgentError> =
2247            Err(agents::AgentError::RateLimit {
2248                message: "429 Too Many Requests".to_string(),
2249            });
2250        assert_eq!(
2251            classify_run_outcome("needs_review", &parse_result, false),
2252            "rate_limit"
2253        );
2254    }
2255
2256    #[test]
2257    fn classify_run_outcome_auth_billing_cycle_is_not_failed() {
2258        // Auth-classified billing-cycle exhaustion (e.g. "billing cycle exhausted"
2259        // matches detect_auth_error's pattern list) must also be distinguished from
2260        // a generic auth failure.
2261        let parse_result: Result<agents::ParsedResponse, agents::AgentError> =
2262            Err(agents::AgentError::Auth {
2263                message: "billing cycle exhausted for this account".to_string(),
2264            });
2265        assert_eq!(
2266            classify_run_outcome("needs_review", &parse_result, false),
2267            "billing_cycle_exhausted"
2268        );
2269    }
2270
2271    #[test]
2272    fn classify_run_outcome_generic_auth_error_is_unaffected() {
2273        let parse_result: Result<agents::ParsedResponse, agents::AgentError> =
2274            Err(agents::AgentError::Auth {
2275                message: "401 Unauthorized".to_string(),
2276            });
2277        assert_eq!(
2278            classify_run_outcome("needs_review", &parse_result, false),
2279            "failed"
2280        );
2281    }
2282
2283    #[test]
2284    fn classify_run_outcome_interrupted() {
2285        let parse_result: Result<agents::ParsedResponse, agents::AgentError> =
2286            Err(agents::AgentError::Interrupted {
2287                message: "exited without a result envelope".to_string(),
2288            });
2289        assert_eq!(
2290            classify_run_outcome("new", &parse_result, false),
2291            "interrupted"
2292        );
2293    }
2294
2295    #[tokio::test]
2296    async fn build_run_audit_success_ignores_stale_last_error() {
2297        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2298        let task_id = store
2299            .create(&NewTask {
2300                repo: "owner/repo".to_string(),
2301                origin: "internal".to_string(),
2302                title: "Test".to_string(),
2303                ..Default::default()
2304            })
2305            .await
2306            .unwrap();
2307        store
2308            .set_fields(
2309                task_id,
2310                &[("last_error", serde_json::json!("no error info available"))],
2311            )
2312            .await
2313            .unwrap();
2314
2315        let runner = TaskRunner::new("owner/repo".to_string()).with_store(store);
2316        let parse_result = Ok(ok_parse_result("done"));
2317        let started_at = Utc::now();
2318        let audit = runner
2319            .build_run_audit(RunAuditInput {
2320                task_id: &task_id.to_string(),
2321                status: "done",
2322                parse_result: &parse_result,
2323                raw_stdout: "",
2324                raw_stderr: "",
2325                started_at: &started_at,
2326                error_override: None,
2327                elapsed_secs: Some(1),
2328                push_failed: false,
2329            })
2330            .await;
2331
2332        assert_eq!(audit.outcome, "success");
2333        assert_eq!(audit.error, "");
2334    }
2335
2336    #[tokio::test]
2337    async fn build_run_audit_ok_parse_prefers_parsed_error_over_stale_last_error() {
2338        // needs_review classifies as success and wipes the error. The #3851
2339        // misattribution is an Ok parse whose outcome is failed or blocked
2340        let cases = [("needs_action", "failed"), ("blocked", "blocked")];
2341        let parsed_error = "passbox Touch ID prompt timed out for cf/read-token";
2342        let stale = "minimax auth error: API Error: 402 insufficient balance (1008), clearing agent/model for re-route";
2343        for (status, expected_outcome) in cases {
2344            let store = Arc::new(TaskStore::open_memory().await.unwrap());
2345            let task_id = store
2346                .create(&NewTask {
2347                    repo: "owner/repo".to_string(),
2348                    origin: "internal".to_string(),
2349                    title: "Test".to_string(),
2350                    ..Default::default()
2351                })
2352                .await
2353                .unwrap();
2354            store
2355                .set_fields(task_id, &[("last_error", serde_json::json!(stale))])
2356                .await
2357                .unwrap();
2358
2359            let runner = TaskRunner::new("owner/repo".to_string()).with_store(store);
2360            let parse_result = Ok(agents::ParsedResponse {
2361                response: AgentResponse {
2362                    status: status.to_string(),
2363                    summary: "cache_stats.py did not run".to_string(),
2364                    accomplished: vec![],
2365                    remaining: vec![],
2366                    files: vec![],
2367                    error: Some(parsed_error.to_string()),
2368                    input_tokens: None,
2369                    output_tokens: None,
2370                    learnings: vec![],
2371                    delegations: vec![],
2372                },
2373                input_tokens: None,
2374                output_tokens: None,
2375                cost_usd: None,
2376                duration_ms: None,
2377            });
2378            let started_at = Utc::now();
2379            let audit = runner
2380                .build_run_audit(RunAuditInput {
2381                    task_id: &task_id.to_string(),
2382                    status,
2383                    parse_result: &parse_result,
2384                    raw_stdout: "",
2385                    raw_stderr: "",
2386                    started_at: &started_at,
2387                    error_override: None,
2388                    elapsed_secs: Some(261),
2389                    push_failed: false,
2390                })
2391                .await;
2392
2393            assert_eq!(audit.outcome, expected_outcome, "status={status}");
2394            assert_eq!(audit.error, parsed_error, "status={status}");
2395        }
2396    }
2397
2398    #[tokio::test]
2399    async fn build_run_audit_non_success_uses_fallback_error_when_empty() {
2400        let runner = TaskRunner::new("owner/repo".to_string());
2401        let parse_result = Ok(ok_parse_result("new"));
2402        let started_at = Utc::now();
2403        let audit = runner
2404            .build_run_audit(RunAuditInput {
2405                task_id: "1",
2406                status: "new",
2407                parse_result: &parse_result,
2408                raw_stdout: "",
2409                raw_stderr: "",
2410                started_at: &started_at,
2411                error_override: None,
2412                elapsed_secs: Some(1),
2413                push_failed: false,
2414            })
2415            .await;
2416
2417        assert_eq!(audit.outcome, "failed");
2418        // "new" is a canonical non-success status — no error message needed.
2419        assert_eq!(audit.error, "");
2420    }
2421
2422    #[tokio::test]
2423    async fn build_run_audit_non_canonical_status_reports_explicit_error() {
2424        // Non-canonical statuses (not in the allowlist) with no parse error
2425        // should get an explicit "unrecognized status: X" message instead of
2426        // the opaque "no error info available" placeholder (#2801).
2427        let runner = TaskRunner::new("owner/repo".to_string());
2428        let parse_result = Ok(ok_parse_result("fix_deployed"));
2429        let started_at = Utc::now();
2430        let audit = runner
2431            .build_run_audit(RunAuditInput {
2432                task_id: "1",
2433                status: "fix_deployed",
2434                parse_result: &parse_result,
2435                raw_stdout: "",
2436                raw_stderr: "",
2437                started_at: &started_at,
2438                error_override: None,
2439                elapsed_secs: Some(1),
2440                push_failed: false,
2441            })
2442            .await;
2443
2444        assert_eq!(audit.outcome, "failed");
2445        assert_eq!(audit.error, "unrecognized status: fix_deployed");
2446    }
2447
2448    #[tokio::test]
2449    async fn build_run_audit_canonical_non_success_status_no_error_message() {
2450        // Canonical non-success statuses (e.g. "routed") should not produce
2451        // an error message — they're expected outcomes, not failures.
2452        for status in &["routed"] {
2453            let runner = TaskRunner::new("owner/repo".to_string());
2454            let parse_result = Ok(ok_parse_result(status));
2455            let started_at = Utc::now();
2456            let audit = runner
2457                .build_run_audit(RunAuditInput {
2458                    task_id: "1",
2459                    status,
2460                    parse_result: &parse_result,
2461                    raw_stdout: "",
2462                    raw_stderr: "",
2463                    started_at: &started_at,
2464                    error_override: None,
2465                    elapsed_secs: Some(1),
2466                    push_failed: false,
2467                })
2468                .await;
2469
2470            assert_eq!(audit.outcome, "success", "status={status}");
2471            assert_eq!(
2472                audit.error, "",
2473                "status={status}: no error for canonical status"
2474            );
2475        }
2476    }
2477
2478    #[tokio::test]
2479    async fn build_run_audit_blocked_uses_summary_when_error_missing() {
2480        let runner = TaskRunner::new("owner/repo".to_string());
2481        let parse_result = Ok(agents::ParsedResponse {
2482            response: AgentResponse {
2483                status: "blocked".to_string(),
2484                summary: "Could not proceed due to missing credentials".to_string(),
2485                accomplished: vec![],
2486                remaining: vec![],
2487                files: vec![],
2488                error: None,
2489                input_tokens: None,
2490                output_tokens: None,
2491                learnings: vec![],
2492                delegations: vec![],
2493            },
2494            input_tokens: None,
2495            output_tokens: None,
2496            cost_usd: None,
2497            duration_ms: None,
2498        });
2499        let started_at = Utc::now();
2500        let audit = runner
2501            .build_run_audit(RunAuditInput {
2502                task_id: "1",
2503                status: "blocked",
2504                parse_result: &parse_result,
2505                raw_stdout: "",
2506                raw_stderr: "",
2507                started_at: &started_at,
2508                error_override: None,
2509                elapsed_secs: Some(1),
2510                push_failed: false,
2511            })
2512            .await;
2513
2514        assert_eq!(audit.outcome, "blocked");
2515        assert_eq!(audit.error, "Could not proceed due to missing credentials");
2516    }
2517
2518    // ── resolve_project_dir ──────────────────────────────────────────────────
2519
2520    #[tokio::test]
2521    async fn resolve_project_dir_uses_project_dir_env() {
2522        // When PROJECT_DIR env var is set, it should always win.
2523        let runner = TaskRunner::new("owner/repo".to_string());
2524
2525        // Use a temp dir so the path exists
2526        let dir = tempfile::tempdir().unwrap();
2527        let dir_str = dir.path().to_string_lossy().to_string();
2528
2529        // Temporarily set the env var — note: tests run in parallel so we use a
2530        // dedicated key that only this test touches. std::env::set_var is not
2531        // thread-safe in general, but the subsequent call is synchronous and
2532        // isolated to the temp dir created above.
2533        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2534        unsafe { env::set_var("PROJECT_DIR", &dir_str) };
2535        let result = runner.resolve_project_dir().await;
2536        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2537        unsafe { env::remove_var("PROJECT_DIR") };
2538
2539        assert!(
2540            result.is_ok(),
2541            "resolve_project_dir should succeed with PROJECT_DIR set"
2542        );
2543        assert_eq!(result.unwrap(), dir.path());
2544    }
2545
2546    #[tokio::test]
2547    async fn resolve_project_dir_empty_project_dir_env_falls_through() {
2548        // An empty PROJECT_DIR should be ignored and fall through to other logic.
2549        let runner = TaskRunner::new("owner/testrepo-nonexistent".to_string());
2550        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2551        unsafe { env::set_var("PROJECT_DIR", "") };
2552        let result = runner.resolve_project_dir().await;
2553        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2554        unsafe { env::remove_var("PROJECT_DIR") };
2555
2556        // No active config entry, no legacy project_dir, no bare clone for this
2557        // repo: should return an explicit error rather than falling back to the
2558        // service's own CWD (see issue #3416).
2559        assert!(
2560            result.is_err(),
2561            "resolve_project_dir should error when no project dir can be resolved"
2562        );
2563        let err = result.unwrap_err().to_string();
2564        assert!(
2565            err.contains("owner/testrepo-nonexistent"),
2566            "error should name the unresolvable repo: {err}"
2567        );
2568    }
2569
2570    // ── mock backend for process_delegations ─────────────────────────────────
2571
2572    struct TrackingBackend {
2573        sub_tasks_created: Arc<Mutex<Vec<(String, String)>>>, // (title, parent_id)
2574        status_updates: Arc<Mutex<Vec<(String, Status)>>>,
2575        comments: Arc<Mutex<Vec<(String, String)>>>, // (id, body)
2576    }
2577
2578    impl TrackingBackend {
2579        fn new() -> Self {
2580            Self {
2581                sub_tasks_created: Arc::new(Mutex::new(vec![])),
2582                status_updates: Arc::new(Mutex::new(vec![])),
2583                comments: Arc::new(Mutex::new(vec![])),
2584            }
2585        }
2586    }
2587
2588    #[async_trait]
2589    impl ExternalBackend for TrackingBackend {
2590        fn name(&self) -> &'static str {
2591            "tracking"
2592        }
2593        async fn create_task(
2594            &self,
2595            _t: &str,
2596            _b: &str,
2597            _l: &[String],
2598        ) -> anyhow::Result<ExternalId> {
2599            Ok(ExternalId("new".to_string()))
2600        }
2601        async fn get_task(&self, id: &ExternalId) -> anyhow::Result<ExternalTask> {
2602            Ok(ExternalTask {
2603                id: id.clone(),
2604                title: String::new(),
2605                body: String::new(),
2606                state: "open".to_string(),
2607                labels: vec![],
2608                author: String::new(),
2609                created_at: "2026-01-01T00:00:00Z".to_string(),
2610                updated_at: "2026-01-01T00:00:00Z".to_string(),
2611                url: String::new(),
2612            })
2613        }
2614        async fn list_by_status(&self, _s: Status) -> anyhow::Result<Vec<ExternalTask>> {
2615            Ok(vec![])
2616        }
2617        async fn list_routable(&self) -> anyhow::Result<Vec<ExternalTask>> {
2618            Ok(vec![])
2619        }
2620        async fn post_comment(&self, id: &ExternalId, body: &str) -> anyhow::Result<()> {
2621            self.comments
2622                .lock()
2623                .unwrap()
2624                .push((id.0.clone(), body.to_string()));
2625            Ok(())
2626        }
2627        async fn set_labels(&self, _id: &ExternalId, _l: &[String]) -> anyhow::Result<()> {
2628            Ok(())
2629        }
2630        async fn remove_label(&self, _id: &ExternalId, _l: &str) -> anyhow::Result<()> {
2631            Ok(())
2632        }
2633        async fn get_sub_issues(&self, _id: &ExternalId) -> anyhow::Result<Vec<ExternalId>> {
2634            Ok(vec![])
2635        }
2636        async fn create_sub_task(
2637            &self,
2638            parent: &ExternalId,
2639            title: &str,
2640            _body: &str,
2641            _l: &[String],
2642        ) -> anyhow::Result<ExternalId> {
2643            self.sub_tasks_created
2644                .lock()
2645                .unwrap()
2646                .push((title.to_string(), parent.0.clone()));
2647            Ok(ExternalId(format!("child-{title}")))
2648        }
2649        async fn ensure_status_label(&self, _l: &str) -> anyhow::Result<()> {
2650            Ok(())
2651        }
2652        async fn has_open_issue_with_title(&self, _t: &str, _l: &str) -> anyhow::Result<bool> {
2653            Ok(false)
2654        }
2655        async fn health_check(&self) -> anyhow::Result<()> {
2656            Ok(())
2657        }
2658        async fn is_pr_merged(&self, _b: &str) -> anyhow::Result<bool> {
2659            Ok(false)
2660        }
2661        async fn get_authenticated_user(&self) -> anyhow::Result<Option<String>> {
2662            Ok(None)
2663        }
2664        async fn get_mentions(&self, _s: &str) -> anyhow::Result<Vec<Mention>> {
2665            Ok(vec![])
2666        }
2667        async fn update_status(&self, id: &ExternalId, status: Status) -> anyhow::Result<()> {
2668            self.status_updates
2669                .lock()
2670                .unwrap()
2671                .push((id.0.clone(), status));
2672            Ok(())
2673        }
2674    }
2675
2676    fn make_task(id: &str) -> ExternalTask {
2677        ExternalTask {
2678            id: ExternalId(id.to_string()),
2679            title: format!("Task {id}"),
2680            body: String::new(),
2681            state: "open".to_string(),
2682            labels: vec![],
2683            author: "bot".to_string(),
2684            created_at: "2026-01-01T00:00:00Z".to_string(),
2685            updated_at: "2026-01-01T00:00:00Z".to_string(),
2686            url: String::new(),
2687        }
2688    }
2689
2690    // ── process_delegations ───────────────────────────────────────────────────
2691
2692    #[expect(
2693        clippy::await_holding_lock,
2694        reason = "the env lock must stay held across the awaited calls"
2695    )]
2696    #[tokio::test]
2697    async fn process_delegations_creates_subtasks_and_blocks_parent() {
2698        let _guard = ENV_LOCK.lock().unwrap();
2699        let temp_home = TempDir::new().unwrap();
2700        let orch_home = temp_home.path().join(".orch");
2701        fs::create_dir_all(&orch_home).await.unwrap();
2702        let old_orch_home = env::var("ORCH_HOME").ok();
2703        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2704        unsafe { env::set_var("ORCH_HOME", &orch_home) };
2705
2706        let runner = TaskRunner::new("owner/repo".to_string());
2707        let parent = make_task("99");
2708        let backend = Arc::new(TrackingBackend::new());
2709        let backend_dyn: Arc<dyn ExternalBackend> = Arc::<TrackingBackend>::clone(&backend);
2710
2711        let delegations = vec![
2712            Delegation {
2713                title: "Subtask A".to_string(),
2714                body: "Do A".to_string(),
2715                labels: vec!["enhancement".to_string()],
2716            },
2717            Delegation {
2718                title: "Subtask B".to_string(),
2719                body: "Do B".to_string(),
2720                labels: vec![],
2721            },
2722        ];
2723
2724        runner
2725            .process_delegations(&parent, &delegations, &backend_dyn)
2726            .await
2727            .unwrap();
2728
2729        // Two sub-tasks should have been created
2730        let created = backend.sub_tasks_created.lock().unwrap();
2731        assert_eq!(created.len(), 2);
2732        assert_eq!(created[0].1, "99", "parent id should be 99");
2733        assert!(
2734            created.iter().any(|(t, _)| t == "Subtask A"),
2735            "Subtask A should be created"
2736        );
2737        assert!(
2738            created.iter().any(|(t, _)| t == "Subtask B"),
2739            "Subtask B should be created"
2740        );
2741        drop(created);
2742
2743        // Parent should be marked blocked
2744        let updates = backend.status_updates.lock().unwrap();
2745        assert!(
2746            updates
2747                .iter()
2748                .any(|(id, s)| id == "99" && *s == Status::Blocked),
2749            "parent should be blocked"
2750        );
2751        drop(updates);
2752
2753        // A comment summarising the delegations should be posted
2754        let comments = backend.comments.lock().unwrap();
2755        assert!(
2756            !comments.is_empty(),
2757            "a delegation summary comment should be posted"
2758        );
2759        let comment_body = &comments[0].1;
2760        assert!(
2761            comment_body.contains("Subtask A"),
2762            "summary should mention Subtask A"
2763        );
2764        assert!(
2765            comment_body.contains("Subtask B"),
2766            "summary should mention Subtask B"
2767        );
2768
2769        if let Some(old) = old_orch_home {
2770            // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2771            unsafe { env::set_var("ORCH_HOME", old) };
2772        } else {
2773            // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2774            unsafe { env::remove_var("ORCH_HOME") };
2775        }
2776    }
2777
2778    #[expect(
2779        clippy::await_holding_lock,
2780        reason = "the env lock must stay held across the awaited calls"
2781    )]
2782    #[tokio::test]
2783    async fn process_delegations_single_subtask() {
2784        let _guard = ENV_LOCK.lock().unwrap();
2785        let temp_home = TempDir::new().unwrap();
2786        let orch_home = temp_home.path().join(".orch");
2787        fs::create_dir_all(&orch_home).await.unwrap();
2788        let old_orch_home = env::var("ORCH_HOME").ok();
2789        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2790        unsafe { env::set_var("ORCH_HOME", &orch_home) };
2791
2792        let runner = TaskRunner::new("owner/repo".to_string());
2793        let parent = make_task("101");
2794        let backend = Arc::new(TrackingBackend::new());
2795        let backend_dyn: Arc<dyn ExternalBackend> = Arc::<TrackingBackend>::clone(&backend);
2796
2797        let delegations = vec![Delegation {
2798            title: "Only Child".to_string(),
2799            body: "Do only child".to_string(),
2800            labels: vec!["feature".to_string()],
2801        }];
2802
2803        runner
2804            .process_delegations(&parent, &delegations, &backend_dyn)
2805            .await
2806            .unwrap();
2807
2808        let created = backend.sub_tasks_created.lock().unwrap();
2809        assert_eq!(created.len(), 1);
2810        assert_eq!(created[0].0, "Only Child");
2811        drop(created);
2812
2813        let updates = backend.status_updates.lock().unwrap();
2814        assert!(
2815            updates
2816                .iter()
2817                .any(|(id, s)| id == "101" && *s == Status::Blocked),
2818            "parent should be blocked"
2819        );
2820        drop(updates);
2821
2822        let comments = backend.comments.lock().unwrap();
2823        let body = &comments[0].1;
2824        assert!(
2825            body.contains("1 subtask"),
2826            "comment should count one subtask"
2827        );
2828
2829        if let Some(old) = old_orch_home {
2830            // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2831            unsafe { env::set_var("ORCH_HOME", old) };
2832        } else {
2833            // SAFETY: nextest runs each test in its own process, so no other test touches the environment
2834            unsafe { env::remove_var("ORCH_HOME") };
2835        }
2836    }
2837
2838    // ── weight signal logic ───────────────────────────────────────────────────
2839
2840    /// Test helper that mirrors the weight signal logic from `run_with_context`.
2841    /// Reroutes only emit `RateLimited` when the error type is genuinely "rate_limit".
2842    fn weight_signal_for(status: &str, error_type: &str, agent: &str) -> WeightSignal {
2843        let is_rerouted = status == "new" || status == "routed";
2844        let is_rate_limit_error = error_type == "rate_limit";
2845        if is_rerouted {
2846            if is_rate_limit_error {
2847                WeightSignal::RateLimited {
2848                    agent: agent.to_string(),
2849                }
2850            } else {
2851                WeightSignal::Rerouted
2852            }
2853        } else if status == "done"
2854            || status == "needs_review"
2855            || status == "in_progress"
2856            || status == "in_review"
2857        {
2858            WeightSignal::Success {
2859                agent: agent.to_string(),
2860            }
2861        } else if status == "blocked" {
2862            WeightSignal::Blocked
2863        } else {
2864            WeightSignal::None
2865        }
2866    }
2867
2868    #[test]
2869    fn weight_signal_success_includes_needs_review() {
2870        // The normal happy-path for code tasks: agent reports done, has a PR,
2871        // so handle_success returns "needs_review". This must map to Success.
2872        let signal = weight_signal_for("needs_review", "success", "claude");
2873        assert!(
2874            matches!(signal, WeightSignal::Success { agent } if agent == "claude"),
2875            "needs_review should produce WeightSignal::Success"
2876        );
2877    }
2878
2879    #[test]
2880    fn needs_review_signal_success_for_empty_error() {
2881        let signal = needs_review_signal("", "claude");
2882        assert!(matches!(signal, WeightSignal::Success { agent } if agent == "claude"));
2883    }
2884
2885    #[test]
2886    fn needs_review_signal_failed_after_timeout_with_no_fallback() {
2887        let signal =
2888            needs_review_signal("claude timed out after 1804s, no fallback agents", "claude");
2889        assert!(matches!(signal, WeightSignal::Failed { agent } if agent == "claude"));
2890    }
2891
2892    #[test]
2893    fn needs_review_signal_rate_limited_for_rate_limit() {
2894        let signal = needs_review_signal("rate limit exceeded, try again later", "claude");
2895        assert!(matches!(signal, WeightSignal::RateLimited { agent } if agent == "claude"));
2896    }
2897
2898    #[test]
2899    fn weight_signal_success_for_all_done_statuses() {
2900        for status in ["done", "needs_review", "in_progress", "in_review"] {
2901            let signal = weight_signal_for(status, "success", "codex");
2902            assert!(
2903                matches!(signal, WeightSignal::Success { agent } if agent == "codex"),
2904                "{status} should produce WeightSignal::Success"
2905            );
2906        }
2907    }
2908
2909    #[test]
2910    fn weight_signal_rate_limited_for_reroute_with_rate_limit_error() {
2911        // Reroute due to rate limit should emit RateLimited.
2912        let signal = weight_signal_for("new", "rate_limit", "claude");
2913        assert!(matches!(signal, WeightSignal::RateLimited { agent } if agent == "claude"));
2914    }
2915
2916    #[test]
2917    fn weight_signal_rate_limited_for_routed_status_with_rate_limit_error() {
2918        let signal = weight_signal_for("routed", "rate_limit", "claude");
2919        assert!(matches!(signal, WeightSignal::RateLimited { agent } if agent == "claude"));
2920    }
2921
2922    #[test]
2923    fn weight_signal_rerouted_for_non_rate_limit_reroute() {
2924        // Reroute due to silence detection, timeout, or other non-rate-limit errors
2925        // should emit Rerouted (not RateLimited and not None).
2926        // Rerouted maps to "new" in tick so the task stays retryable and is never
2927        // converted to needs_review → done via the no-code review shortcut.
2928        for error_type in ["timeout", "silence detected", "parse_error", "failed"] {
2929            for status in ["new", "routed"] {
2930                let signal = weight_signal_for(status, error_type, "claude");
2931                assert!(
2932                    matches!(signal, WeightSignal::Rerouted),
2933                    "{status} with error_type={error_type} should produce WeightSignal::Rerouted, not RateLimited or None"
2934                );
2935            }
2936        }
2937    }
2938
2939    #[test]
2940    fn silence_reset_rerouted_never_becomes_needs_review() {
2941        // Regression test for: silence detection sets task to new, runner returns
2942        // WeightSignal::Rerouted, tick must map that to "new" — not "needs_review".
2943        // If it mapped to needs_review the no-code shortcut would close the task as
2944        // done with no successful agent run in the audit trail.
2945        let signal = weight_signal_for("new", "silence detected", "claude");
2946        assert!(
2947            matches!(signal, WeightSignal::Rerouted),
2948            "silence-reset reroute must produce Rerouted, never None/needs_review"
2949        );
2950        // Verify tick mapping: Rerouted => "new" (not needs_review or done)
2951        let display_status = match &signal {
2952            WeightSignal::Rerouted => "new",
2953            WeightSignal::None => "needs_review", // the old (buggy) path
2954            _ => "other",
2955        };
2956        assert_eq!(
2957            display_status, "new",
2958            "Rerouted must map to 'new' in tick, not 'needs_review'"
2959        );
2960    }
2961
2962    #[test]
2963    fn weight_signal_blocked_status() {
2964        let signal = weight_signal_for("blocked", "success", "claude");
2965        assert!(matches!(signal, WeightSignal::Blocked));
2966    }
2967
2968    // ── parse_success_output token extraction ────────────────────────────────
2969
2970    /// Mock runner that always returns InvalidResponse with truncated text,
2971    /// simulating what minimax/codex do when they can't parse structured JSON.
2972    struct FailingMockRunner;
2973
2974    impl agents::AgentRunner for FailingMockRunner {
2975        #[cfg(test)]
2976        fn name(&self) -> &'static str {
2977            "opencode"
2978        }
2979
2980        fn stream_events(&self, _event: &serde_json::Value) -> Vec<agents::StreamEvent> {
2981            vec![]
2982        }
2983
2984        fn build_command(
2985            &self,
2986            _model: Option<&str>,
2987            _timeout_cmd: &str,
2988            _sys_file: &str,
2989            _msg_file: &str,
2990            _permissions: &agents::PermissionRules,
2991        ) -> String {
2992            String::new()
2993        }
2994
2995        fn parse_response(&self, raw: &str) -> Result<agents::ParsedResponse, agents::AgentError> {
2996            // Simulate what minimax does: truncate to 300 chars
2997            Err(agents::AgentError::InvalidResponse {
2998                raw: raw.chars().take(30).collect(),
2999            })
3000        }
3001
3002        fn extract_text(&self, raw: &str) -> Result<String, agents::AgentError> {
3003            Err(agents::AgentError::InvalidResponse {
3004                raw: raw.to_string(),
3005            })
3006        }
3007
3008        fn classify_error(
3009            &self,
3010            exit_code: i32,
3011            _stdout: &str,
3012            _stderr: &str,
3013        ) -> agents::AgentError {
3014            agents::AgentError::Unknown {
3015                exit_code,
3016                message: "mock error".into(),
3017            }
3018        }
3019
3020        fn router_command(&self, _prompt: &str, _model: Option<&str>) -> anyhow::Result<Command> {
3021            anyhow::bail!("not implemented for mock")
3022        }
3023    }
3024
3025    #[test]
3026    fn parse_success_output_extracts_tokens_from_raw_stdout_not_error_raw() {
3027        // OpenCode NDJSON output with token metadata in step_finish
3028        let ndjson = r#"{"type":"text","timestamp":1,"part":{"type":"text","text":"Here is my response: {\"status\":\"done\",\"summary\":\"fixed\"}}"}}
3029{"type":"step_finish","timestamp":2,"part":{"type":"step-finish","reason":"stop","cost":0,"tokens":{"total":95177,"input":94920,"output":257}}}"#;
3030
3031        let runner = FailingMockRunner;
3032        let result = parse_success_output("1550", "opencode", &runner, ndjson);
3033
3034        // Should succeed (synthesized from text) with tokens preserved
3035        let parsed = result.expect("should synthesize response from text");
3036        assert_eq!(parsed.input_tokens, Some(94920));
3037        assert_eq!(parsed.output_tokens, Some(257));
3038    }
3039
3040    #[test]
3041    fn parse_success_output_no_tokens_when_ndjson_has_none() {
3042        // Plain text with an explicit completion signal but no token metadata.
3043        // Uses "No open positions" which is in the explicit_done list.
3044        let text = "No open positions, no trade executions, no conditions change.";
3045
3046        let runner = FailingMockRunner;
3047        let result = parse_success_output("1551", "opencode", &runner, text);
3048
3049        // Should synthesize a "done" response, but without tokens (no NDJSON envelope).
3050        let parsed = result.expect("should synthesize response from explicit-done text");
3051        assert_eq!(parsed.input_tokens, None);
3052        assert_eq!(parsed.output_tokens, None);
3053    }
3054
3055    #[test]
3056    fn parse_success_output_uses_agent_result_directly() {
3057        // Here we test that if find_agent_result succeeds and returns valid JSON, we use it directly
3058        // WITHOUT calling agent_runner.parse_response at all.
3059        let ndjson = r#"{"type":"result","subtype":"success","is_error":false,"result":"{\"status\":\"done\",\"summary\":\"my summary\",\"accomplished\":[],\"remaining\":[],\"files\":[]}"}"#;
3060
3061        // FailingMockRunner always returns InvalidResponse, so if we called it, we'd synthesize "done" instead of getting "my summary".
3062        let runner = FailingMockRunner;
3063        let result = parse_success_output("999", "claude", &runner, ndjson);
3064
3065        let parsed = result.expect("should parse successfully via find_agent_result");
3066        assert_eq!(parsed.response.status, "done");
3067        assert_eq!(parsed.response.summary, "my summary");
3068    }
3069
3070    #[test]
3071    fn parse_success_output_treats_agent_result_is_error_as_authoritative() {
3072        // Here we test that if find_agent_result returns is_error=true, we return AgentFailed
3073        // instead of falling back to synthesis or agent_runner.parse_response.
3074        let ndjson = r#"{"type":"result","subtype":"success","is_error":true,"result":"I failed because of reasons"}"#;
3075
3076        let runner = FailingMockRunner;
3077        let result = parse_success_output("999", "claude", &runner, ndjson);
3078
3079        let err = result.expect_err("should return error");
3080        match err {
3081            agents::AgentError::AgentFailed { message } => {
3082                assert_eq!(message, "I failed because of reasons");
3083            }
3084            _ => panic!("expected AgentFailed, got {err:?}"),
3085        }
3086    }
3087
3088    #[test]
3089    fn parse_success_output_classifies_model_unavailable_from_is_error_result() {
3090        // Regression test for issue #3607: the is_error=true NDJSON path
3091        // (used by codex's find_codex_result, which stringifies its own
3092        // classified AgentError into result_text) must re-classify a
3093        // model-not-found message as ModelUnavailable so it gets
3094        // model-scoped cooldown treatment instead of degrading to a
3095        // generic AgentFailed, which triggers an agent-wide cooldown.
3096        let ndjson = r#"{"type":"result","subtype":"success","is_error":true,"result":"model unavailable (gpt-5.5): Reconnecting... 2/5 (unexpected status 404 Not Found: The model `gpt-5.5` does not exist or you do not have access to it.)"}"#;
3097
3098        let runner = FailingMockRunner;
3099        let result = parse_success_output("999", "claude", &runner, ndjson);
3100
3101        let err = result.expect_err("should return error");
3102        match err {
3103            agents::AgentError::ModelUnavailable { model, .. } => {
3104                assert_eq!(model, "gpt-5.5");
3105            }
3106            _ => panic!("expected ModelUnavailable, got {err:?}"),
3107        }
3108    }
3109
3110    #[test]
3111    fn nonzero_exit_with_ndjson_success_is_parsed_as_success() {
3112        // Simulate an agent that exits with non-zero code but emits NDJSON result
3113        // with is_error=false and a JSON result payload. The runner should accept
3114        // this as success because the NDJSON result is authoritative.
3115        let ndjson = r#"{"type":"result","subtype":"success","is_error":false,"duration_ms":1234,"result":"{\"status\":\"done\",\"summary\":\"All good\"}","usage":{"input_tokens":10,"output_tokens":5}}"#;
3116
3117        // Use a runner that would normally fail parsing to ensure we're using
3118        // the find_agent_result path. FailingMockRunner.parse_response returns
3119        // InvalidResponse, but parse_success_output should short-circuit.
3120        let runner = FailingMockRunner;
3121
3122        // Construct a fake SessionOutput
3123        let session_output = session::SessionOutput {
3124            exit_code: 1,
3125            raw_stdout: ndjson.to_string(),
3126            raw_stderr: String::new(),
3127            elapsed_secs: Some(1),
3128            sandbox_error: None,
3129        };
3130
3131        // Call the helper we added via the task runner logic by reusing the
3132        // parse_success_output directly since it encapsulates the same behavior.
3133        let parsed =
3134            parse_success_output("task-ndjson", "claude", &runner, &session_output.raw_stdout)
3135                .expect("should parse NDJSON success");
3136        assert_eq!(parsed.response.status, "done");
3137        assert_eq!(parsed.response.summary, "All good");
3138        assert_eq!(parsed.input_tokens, Some(10));
3139        assert_eq!(parsed.output_tokens, Some(5));
3140    }
3141
3142    // ── agent label update after reroute ─────────────────────────────────────
3143
3144    /// Backend that wraps TrackingBackend but can be configured to fail label ops.
3145    struct LabelFailingBackend {
3146        inner: TrackingBackend,
3147        /// If set, set_labels returns this error.
3148        set_labels_err: Option<String>,
3149    }
3150
3151    impl LabelFailingBackend {
3152        fn new() -> Self {
3153            Self {
3154                inner: TrackingBackend::new(),
3155                set_labels_err: None,
3156            }
3157        }
3158        fn with_set_labels_err(mut self, err: impl Into<String>) -> Self {
3159            self.set_labels_err = Some(err.into());
3160            self
3161        }
3162    }
3163
3164    impl Default for LabelFailingBackend {
3165        fn default() -> Self {
3166            Self::new()
3167        }
3168    }
3169
3170    #[async_trait]
3171    impl ExternalBackend for LabelFailingBackend {
3172        fn name(&self) -> &'static str {
3173            "label_failing"
3174        }
3175        async fn create_task(
3176            &self,
3177            _t: &str,
3178            _b: &str,
3179            _l: &[String],
3180        ) -> anyhow::Result<ExternalId> {
3181            Ok(ExternalId("new".to_string()))
3182        }
3183        async fn get_task(&self, id: &ExternalId) -> anyhow::Result<ExternalTask> {
3184            self.inner.get_task(id).await
3185        }
3186        async fn list_by_status(&self, _s: Status) -> anyhow::Result<Vec<ExternalTask>> {
3187            Ok(vec![])
3188        }
3189        async fn list_routable(&self) -> anyhow::Result<Vec<ExternalTask>> {
3190            Ok(vec![])
3191        }
3192        async fn post_comment(&self, id: &ExternalId, body: &str) -> anyhow::Result<()> {
3193            self.inner.post_comment(id, body).await
3194        }
3195        async fn set_labels(&self, id: &ExternalId, labels: &[String]) -> anyhow::Result<()> {
3196            if let Some(ref err) = self.set_labels_err {
3197                return Err(anyhow::anyhow!("{err}"));
3198            }
3199            self.inner.set_labels(id, labels).await
3200        }
3201        async fn remove_label(&self, id: &ExternalId, label: &str) -> anyhow::Result<()> {
3202            self.inner.remove_label(id, label).await
3203        }
3204        async fn get_sub_issues(&self, _id: &ExternalId) -> anyhow::Result<Vec<ExternalId>> {
3205            Ok(vec![])
3206        }
3207        async fn create_sub_task(
3208            &self,
3209            parent: &ExternalId,
3210            title: &str,
3211            body: &str,
3212            labels: &[String],
3213        ) -> anyhow::Result<ExternalId> {
3214            self.inner
3215                .create_sub_task(parent, title, body, labels)
3216                .await
3217        }
3218        async fn ensure_status_label(&self, label: &str) -> anyhow::Result<()> {
3219            self.inner.ensure_status_label(label).await
3220        }
3221        async fn update_status(&self, id: &ExternalId, status: Status) -> anyhow::Result<()> {
3222            self.inner.update_status(id, status).await
3223        }
3224        async fn get_authenticated_user(&self) -> anyhow::Result<Option<String>> {
3225            self.inner.get_authenticated_user().await
3226        }
3227        async fn get_mentions(&self, _s: &str) -> anyhow::Result<Vec<Mention>> {
3228            Ok(vec![])
3229        }
3230        async fn health_check(&self) -> anyhow::Result<()> {
3231            self.inner.health_check().await
3232        }
3233    }
3234
3235    /// `update_agent_label_after_reroute` returns `false` when `set_labels` fails.
3236    ///
3237    /// This tests the extracted helper directly, bypassing the full `run_with_context`
3238    /// path (which would fail earlier on worktree setup in a unit-test environment).
3239    #[tokio::test]
3240    async fn update_agent_label_returns_false_on_set_labels_failure() {
3241        let task = ExternalTask {
3242            id: ExternalId("test-reroute-1".to_string()),
3243            title: "Test".to_string(),
3244            body: "body".to_string(),
3245            state: "open".to_string(),
3246            labels: vec![],
3247            author: "bot".to_string(),
3248            created_at: "2026-01-01T00:00:00Z".to_string(),
3249            updated_at: "2026-01-01T00:00:00Z".to_string(),
3250            url: String::new(),
3251        };
3252        let backend: Arc<dyn ExternalBackend> =
3253            Arc::new(LabelFailingBackend::new().with_set_labels_err("network error"));
3254
3255        let result =
3256            update_agent_label_after_reroute("owner/repo", &task, &backend, "claude", "codex")
3257                .await;
3258
3259        assert!(
3260            !result,
3261            "update_agent_label_after_reroute should return false when set_labels fails"
3262        );
3263    }
3264
3265    /// `update_agent_label_after_reroute` returns `true` when `set_labels` succeeds.
3266    #[tokio::test]
3267    async fn update_agent_label_returns_true_on_success() {
3268        let task = ExternalTask {
3269            id: ExternalId("test-reroute-2".to_string()),
3270            title: "Test".to_string(),
3271            body: "body".to_string(),
3272            state: "open".to_string(),
3273            labels: vec![],
3274            author: "bot".to_string(),
3275            created_at: "2026-01-01T00:00:00Z".to_string(),
3276            updated_at: "2026-01-01T00:00:00Z".to_string(),
3277            url: String::new(),
3278        };
3279        // LabelFailingBackend with no injected error delegates to TrackingBackend (always Ok).
3280        let backend: Arc<dyn ExternalBackend> = Arc::new(LabelFailingBackend::new());
3281
3282        let result =
3283            update_agent_label_after_reroute("owner/repo", &task, &backend, "claude", "codex")
3284                .await;
3285
3286        // ensure_label is skipped in #[cfg(test)], set_labels succeeds via mock.
3287        assert!(
3288            result,
3289            "update_agent_label_after_reroute should return true when set_labels succeeds"
3290        );
3291    }
3292
3293    // ── NDJSON envelope fallback (issue #3078) ────────────────────────────────
3294
3295    /// Pattern A: agent emits domain JSON as result (no `status` field).
3296    /// NDJSON envelope says is_error=false, subtype=success.
3297    /// Was previously returning InvalidResponse → task re-ran.
3298    #[test]
3299    fn parse_success_output_ndjson_envelope_fallback_domain_json_result() {
3300        let domain_result = r#"{"file_path":"md/journal/2026-05-08-morning-briefing.md","summary":"Morning briefing filed","health":{"status":"ok"},"priorities":[]}"#;
3301        let ndjson = format!(
3302            r#"{{"type":"result","subtype":"success","is_error":false,"terminal_reason":"completed","result":{domain_result:?},"usage":{{"input_tokens":3000,"output_tokens":400}}}}"#
3303        );
3304
3305        let runner = agents::get_runner("glm");
3306        let parsed = parse_success_output("149200", "glm", &*runner, &ndjson)
3307            .expect("should synthesize done from NDJSON envelope when result is domain JSON");
3308
3309        assert_eq!(parsed.response.status, "done");
3310        assert!(
3311            parsed.response.summary.contains("file_path") || !parsed.response.summary.is_empty(),
3312            "summary should be populated from domain JSON result text"
3313        );
3314        assert_eq!(parsed.input_tokens, Some(3000));
3315        assert_eq!(parsed.output_tokens, Some(400));
3316    }
3317
3318    /// Grok-style envelope: no terminal_reason, subtype=success.
3319    #[test]
3320    fn parse_success_output_envelope_fallback_without_terminal_reason() {
3321        let receipt = r#"{"ok":true,"report":"notes.md","commit":"abc"}"#;
3322        let ndjson = format!(
3323            r#"{{"type":"result","subtype":"success","is_error":false,"stop_reason":"end_turn","result":{receipt:?},"usage":{{"input_tokens":3000,"output_tokens":400}}}}"#
3324        );
3325        let runner = agents::get_runner("grok");
3326        let parsed = parse_success_output("1", "grok", &*runner, &ndjson).expect("done");
3327        assert_eq!(parsed.response.status, "done");
3328        assert_eq!(parsed.input_tokens, Some(3000));
3329    }
3330
3331    /// Pattern B: agent emits plain prose as result, no completion keywords.
3332    /// NDJSON envelope says is_error=false, subtype=success.
3333    /// Was previously returning InvalidResponse → task re-ran.
3334    #[test]
3335    fn parse_success_output_ndjson_envelope_fallback_plain_prose_result() {
3336        let prose = "PR #1008 now has zero diff vs main. The reverted ledger changes are gone and the branch is clean.";
3337        let ndjson = format!(
3338            r#"{{"type":"result","subtype":"success","is_error":false,"terminal_reason":"completed","result":{prose:?},"usage":{{"input_tokens":2500,"output_tokens":300}}}}"#
3339        );
3340
3341        let runner = agents::get_runner("claude");
3342        let parsed = parse_success_output("149164", "claude", &*runner, &ndjson)
3343            .expect("should synthesize done from NDJSON envelope when result is plain prose");
3344
3345        assert_eq!(parsed.response.status, "done");
3346        assert_eq!(parsed.response.summary, prose);
3347        assert_eq!(parsed.input_tokens, Some(2500));
3348        assert_eq!(parsed.output_tokens, Some(300));
3349    }
3350
3351    /// Verify that is_error=true in NDJSON envelope still returns AgentFailed,
3352    /// not "done" — the envelope fallback must not override explicit errors.
3353    #[test]
3354    fn parse_success_output_ndjson_envelope_fallback_skipped_when_is_error_true() {
3355        let ndjson = r#"{"type":"result","subtype":"error","is_error":true,"result":"Something went wrong with no status"}"#;
3356
3357        let runner = agents::get_runner("claude");
3358        let err = parse_success_output("149999", "claude", &*runner, ndjson)
3359            .expect_err("is_error=true should return AgentFailed");
3360
3361        assert!(matches!(err, agents::AgentError::AgentFailed { .. }));
3362    }
3363
3364    fn tool_pair(id: &str, name: &str, cmd: &str, is_error: bool) -> String {
3365        format!(
3366            "{}\n{}\n",
3367            serde_json::json!({"type":"assistant","message":{"content":[{"type":"tool_use","id":id,"name":name,"input":{"command":cmd}}]}}),
3368            serde_json::json!({"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":id,"is_error":is_error,"content":"x"}]}}),
3369        )
3370    }
3371
3372    #[test]
3373    fn salvage_wrapup_accepts_commit_then_cleanup() {
3374        let raw = format!(
3375            "{}{}",
3376            tool_pair("1", "run_terminal_cmd", "git commit -m x", false),
3377            tool_pair("2", "run_terminal_cmd", "git status", false)
3378        );
3379        assert!(salvage_wrapup(&raw).is_some());
3380    }
3381
3382    #[test]
3383    fn salvage_wrapup_rejects_edit_after_commit_or_unanswered_call() {
3384        let edit = format!(
3385            "{}{}",
3386            tool_pair("1", "run_terminal_cmd", "git commit -m x", false),
3387            tool_pair("2", "search_replace", "", false)
3388        );
3389        assert!(salvage_wrapup(&edit).is_none());
3390        let pending = format!(
3391            "{}{}",
3392            tool_pair("1", "run_terminal_cmd", "git commit -m x", false),
3393            serde_json::json!({"type":"assistant","message":{"content":[{"type":"tool_use","id":"2","name":"run_terminal_cmd","input":{"command":"cargo test"}}]}})
3394        );
3395        assert!(salvage_wrapup(&pending).is_none());
3396        assert!(salvage_wrapup(&tool_pair("1", "run_terminal_cmd", "ls", false)).is_none());
3397    }
3398
3399    // ── terminal_reason:completed + exit 1 detection (issue #3087) ─────────────
3400
3401    /// kimi/minimax sometimes emit valid NDJSON with terminal_reason=completed
3402    /// but exit 1. In that case parse_success_output may fail (e.g. empty result
3403    /// text that synthesize can't parse), then parse_session_output would
3404    /// normally fall back to classify_error — grabbing a garbled JSON tail and
3405    /// producing a spurious Unknown/Auth error. The fix intercepts this and
3406    /// returns InvalidResponse (soft error, re-route) instead.
3407    #[test]
3408    fn parse_session_output_exit0_without_envelope_is_interrupted() {
3409        let session = session::SessionOutput {
3410            exit_code: 0,
3411            raw_stdout: "\u{4}\u{4}\u{4} truncated pane junk".to_string(),
3412            raw_stderr: String::new(),
3413            elapsed_secs: Some(180),
3414            sandbox_error: None,
3415        };
3416
3417        let runner = agents::get_runner("claude");
3418        let err = parse_session_output("3876", "claude", &*runner, &session)
3419            .expect_err("missing envelope should be an error");
3420        assert!(
3421            matches!(err, agents::AgentError::Interrupted { .. }),
3422            "exit 0 with no result envelope should be Interrupted, got {err:?}"
3423        );
3424    }
3425
3426    #[test]
3427    fn parse_session_output_exit1_with_terminal_reason_completed_returns_invalid_response() {
3428        // Empty result text causes synthesize_response_from_text to return None
3429        // (empty/whitespace-only input), so parse_success_output returns
3430        // InvalidResponse. With the fix, parse_session_output intercepts the
3431        // "terminal_reason":"completed" signal before classify_error and
3432        // returns InvalidResponse (re-route) instead of a garbled error.
3433        let ndjson = r#"{"type":"result","subtype":"success","is_error":false,"terminal_reason":"completed","result":"","usage":{"input_tokens":1000,"output_tokens":100}}"#;
3434        let session = session::SessionOutput {
3435            exit_code: 1,
3436            raw_stdout: ndjson.to_string(),
3437            raw_stderr: String::new(),
3438            elapsed_secs: Some(30),
3439            sandbox_error: None,
3440        };
3441
3442        let runner = agents::get_runner("claude");
3443        let result = parse_session_output("149298", "claude", &*runner, &session);
3444
3445        let err = result.expect_err("should be an error, not Ok");
3446        assert!(
3447            matches!(err, agents::AgentError::InvalidResponse { .. }),
3448            "should be InvalidResponse, got {err:?}"
3449        );
3450    }
3451
3452    // ── GLM cost-telemetry exit-1 detection (issue #3094) ──────────────────────
3453
3454    /// GLM exits with code 1 but stdout is only cost telemetry JSON (contains
3455    /// "costUSD":). This must return InvalidResponse (soft re-route) instead of
3456    /// a hard Failed classification with spurious agent cooldown.
3457    #[test]
3458    fn parse_session_output_exit1_with_cost_telemetry_returns_invalid_response() {
3459        // GLM emits cost/usage JSON on clean exit; exit code 1 is a false failure.
3460        let telemetry = r#"{"type":"result","subtype":"success","is_error":false,"result":"","costUSD":0.042,"usage":{"input_tokens":5000,"output_tokens":800}}"#;
3461        let session = session::SessionOutput {
3462            exit_code: 1,
3463            raw_stdout: telemetry.to_string(),
3464            raw_stderr: String::new(),
3465            elapsed_secs: Some(120),
3466            sandbox_error: None,
3467        };
3468
3469        let runner = agents::get_runner("glm");
3470        let result = parse_session_output("149347", "glm", &*runner, &session);
3471
3472        let err = result.expect_err("should be an error, not Ok");
3473        assert!(
3474            matches!(err, agents::AgentError::InvalidResponse { .. }),
3475            "glm cost-telemetry exit-1 should return InvalidResponse, got {err:?}"
3476        );
3477    }
3478
3479    /// GLM with costUSD AND is_error:true — must propagate AgentFailed, not
3480    /// silently swallow the error via the cost-telemetry guard.
3481    #[test]
3482    fn parse_session_output_exit1_cost_telemetry_with_is_error_true_propagates_failure() {
3483        let telemetry = r#"{"type":"result","subtype":"error","is_error":true,"result":"tool failed","costUSD":0.01,"usage":{"input_tokens":100,"output_tokens":10}}"#;
3484        let session = session::SessionOutput {
3485            exit_code: 1,
3486            raw_stdout: telemetry.to_string(),
3487            raw_stderr: String::new(),
3488            elapsed_secs: Some(15),
3489            sandbox_error: None,
3490        };
3491
3492        let runner = agents::get_runner("glm");
3493        let result = parse_session_output("149348", "glm", &*runner, &session);
3494
3495        let err = result.expect_err("should be an error");
3496        assert!(
3497            matches!(err, agents::AgentError::AgentFailed { .. }),
3498            "is_error=true with costUSD must still return AgentFailed, got {err:?}"
3499        );
3500    }
3501
3502    /// terminal_reason=completed must NOT override actual error conditions.
3503    /// When is_error=true in the NDJSON envelope, AgentFailed must be returned
3504    /// even if terminal_reason=completed is also present. Only kimi/minimax
3505    /// agents use find_claude_result which extracts is_error, so use "kimi"
3506    /// as agent_name.
3507    #[test]
3508    fn parse_session_output_exit1_preserves_error_when_is_error_true() {
3509        let ndjson = r#"{"type":"result","subtype":"error","is_error":true,"terminal_reason":"completed","result":"failed"}"#;
3510        let session = session::SessionOutput {
3511            exit_code: 1,
3512            raw_stdout: ndjson.to_string(),
3513            raw_stderr: String::new(),
3514            elapsed_secs: Some(30),
3515            sandbox_error: None,
3516        };
3517
3518        let runner = agents::get_runner("kimi");
3519        let result = parse_session_output("149299", "kimi", &*runner, &session);
3520
3521        let err = result.expect_err("should be an error");
3522        assert!(
3523            matches!(err, agents::AgentError::AgentFailed { .. }),
3524            "is_error=true should return AgentFailed, got {err:?}"
3525        );
3526    }
3527
3528    /// RateLimit with terminal_reason=completed must be propagated as RateLimit,
3529    /// not downgraded to InvalidResponse. This covers claude session-limit errors
3530    /// that include "terminal_reason":"completed" in their NDJSON envelope — the
3531    /// terminal_reason guard was designed for kimi/minimax/glm false-positive
3532    /// exit codes but was intercepting classified errors like RateLimit.
3533    #[test]
3534    fn parse_session_output_exit1_with_rate_limit_and_terminal_reason_completed() {
3535        let ndjson = r#"{"type":"result","is_error":true,"terminal_reason":"completed","result":"You've hit your session limit · resets 2:20pm (America/Sao_Paulo)","usage":{"input_tokens":0,"output_tokens":0}}"#;
3536        let session = session::SessionOutput {
3537            exit_code: 1,
3538            raw_stdout: ndjson.to_string(),
3539            raw_stderr: String::new(),
3540            elapsed_secs: Some(120),
3541            sandbox_error: None,
3542        };
3543
3544        let runner = agents::get_runner("claude");
3545        let result = parse_session_output("152895", "claude", &*runner, &session);
3546
3547        let err = result.expect_err("rate-limit should be an error");
3548        assert!(
3549            matches!(err, agents::AgentError::RateLimit { .. }),
3550            "session limit should return RateLimit, not downgraded, got {err:?}"
3551        );
3552    }
3553}