Skip to main content

magi/
graph.rs

1//! The competition graph.
2//!
3//! ```text
4//! prep ──► implement ×N ──► judge ×M (blind) ──► split? ──► deliberate ──► vote (private)
5//!                                                   │                          │
6//!                                                   └──── unanimous ───────────┤
7//!                                                                              ▼
8//!   merge ◄── gate ◄── review ×R + E2E, fix, repeat ◄── fold losers ◄──────── tally
9//! ```
10//!
11//! Every node persists before the next one starts, so a run can be resumed
12//! after a crash, a rate limit, or a reboot without re-spending the work that
13//! already landed.
14//!
15//! The design decision that matters most is *where the facilitator lives*.
16//! There is no moderator agent: magi assigns the labels, decides the
17//! presentation order, relays the transcript, and collects the final votes
18//! one-to-one. A moderator that never learns an author cannot leak one.
19use std::collections::{BTreeMap, BTreeSet};
20use std::path::{Path, PathBuf};
21use std::sync::atomic::{AtomicBool, Ordering};
22use std::sync::{Arc, Mutex};
23use std::time::{Duration, Instant};
24
25use anyhow::{Context as _, Result, bail};
26use jiff::Timestamp;
27use tokio::sync::Semaphore;
28
29use crate::advise;
30use crate::agent::{self, AgentOutput, Invocation, SeatState};
31use crate::ask;
32use crate::blind;
33use crate::bump;
34use crate::config::{
35    AgentSpec, Config, IncompleteReviewPolicy, LeakPolicy, MergeMode, MergeStyle, Prompts,
36    ResolvedRoles,
37};
38use crate::fixer;
39use crate::git;
40use crate::graft;
41use crate::land;
42use crate::proc::Quiet as _;
43use crate::prompt::{
44    self, CandidateView, Lens, ReviewPatch, ReviewReconsiderCtx, ReviewSeatReport, Turn,
45};
46use crate::queue;
47use crate::refs;
48use crate::run::{
49    BaseSync, Candidate, CommandOutcome, ContinuationOutcome, ContinuationRecord,
50    DeliberationRound, DeliberationTurn, E2eStatus, FailClass, FixRecord, GateFixRecord,
51    GithubTextChoice, GraftOutcome, GraftRecord, Handover, JobRecord, JobStatus, Judgement,
52    MergeOutcome, OperatorFixFinding, OperatorFixOutcome, OperatorFixRequest, Origin, QuotaLoss,
53    ReviewRecord, ReviewRevoteRecord, ReviewRound, RunState, RunStatus, SeatHistory, Tally,
54    VoteRecord, tail, write_artifact,
55};
56use crate::verdict::{
57    self, FinalVote, Finding, FixReport, Position, Proposal, Ranking, Review, ReviewRevote,
58    ReviewVote, Severity,
59};
60use crate::worktree_setup;
61
62/// How much verification output is kept and fed back to the fixer.
63const OUTPUT_TAIL: usize = 8_000;
64
65/// Bytes of a failing command's output kept in an event, so the reason a run
66/// stopped is readable from the report without opening `run.json`.
67const EVENT_OUTPUT_TAIL: usize = 2_000;
68
69/// How often [`wait_for_timed_out_children_to_die`] re-checks a timed-out
70/// command's pid before releasing the build cache's lease.
71const LEASE_RELEASE_POLL: Duration = Duration::from_secs(1);
72
73/// The most [`wait_for_timed_out_children_to_die`] will wait for a timed-out
74/// command's pid to actually exit before giving up and releasing anyway.
75///
76/// A timeout means the process was asked to die (`kill_on_drop`,
77/// `start_kill`), not that it already has — on Windows in particular that can
78/// take a moment, the same reason `agent`'s own `PIPE_GRACE` exists. Releasing
79/// the instant the command returns would let the very next acquirer (this
80/// run's own next round, another run's verification, the janitor's prune)
81/// start touching the same directory while it might still be writing to it,
82/// so this polls the actual pid — real confirmation, not a fixed guess —
83/// until it is gone or this ceiling is reached. It is still not full
84/// process-tree reaping: a grandchild the timed-out process spawned and that
85/// outlives it independently is invisible to a pid check, and continuing to
86/// observe and collect *that* stays a different piece of work with its own
87/// owner. Set generously because the common case returns early the moment
88/// the pid is confirmed gone, not because every timeout pays this in full.
89const LEASE_RELEASE_MAX_WAIT: Duration = Duration::from_secs(30);
90
91/// Consecutive review rounds with no tree progress (see
92/// [`crate::run::ReviewRound::progressed`]) before `review_loop` hands off
93/// instead of spending the rest of the round budget.
94///
95/// Not 1: a single non-progressing round is not yet a pattern — a fixer that
96/// legitimately finds nothing left to change (its previous round's fix already
97/// covered it, and this round's reviewers re-raised only nits) looks the same
98/// as one that is spinning, for exactly one round. Two in a row is where the
99/// two stop being distinguishable, and a review round on this workload has
100/// been measured at 30-45 minutes of reviewer-plus-fixer agent time, so a
101/// third attempt at a tree that has not moved twice running is pure cost.
102/// This does not touch `review_rounds` itself, which stays the operator's
103/// call.
104pub(crate) const STAGNANT_LIMIT: usize = 2;
105
106/// How many times [`Runner::sync_to_base`] will re-land the winner's tree on
107/// a base that moved before giving up and leaving the run `Blocked` for a
108/// person.
109///
110/// Mirrors `land::Step::Rebase`'s budget and the reasoning behind it: a base
111/// that keeps moving faster than a run can catch it is not something more
112/// rebasing fixes, it is a person's call. Not the same *number as*
113/// `land_rounds` - this budget is spent before a pull request exists, land's
114/// after - but bounded for the identical reason, so it uses the same
115/// default. Counted across both call sites in [`Runner::finish_after_tally`]
116/// (once before review, once before the gate), because either one finding
117/// the base still moving is the same signal.
118const BASE_SYNC_ROUNDS: usize = 4;
119
120/// How many times [`Runner::continue_fix_report`] will resume the fixer's own
121/// seat when its CLI turn ended cleanly — usable, non-empty, exit 0 — but the
122/// reply held no [`FixReport`].
123///
124/// The shape this recovers: run 20260912-114326-d3b8's fix-2 came back
125/// `subtype=success`/`is_error=false`/`stop_reason=end_turn` with the reply
126/// "I'll pause here until the `cargo make check` background run reports
127/// back." — a CLI turn that ended cleanly while the fixer's own job had not.
128/// No `FixReport` was ever collected from that seat, and the run moved on to
129/// the next review round regardless.
130///
131/// Bounded independently of `review_rounds` and `graph.retries`: this
132/// recovers one seat's missing report mid-round, not a new round of review or
133/// an ordinary parse retry, and must not itself become the unbounded wait the
134/// rest of this module exists to avoid.
135const MAX_FIX_CONTINUATIONS: usize = 2;
136
137/// One queued agent invocation.
138///
139/// `Clone` so a node can keep the jobs it sent and re-send one: a seat whose
140/// CLI hung up on its own stream is asked again from the same job rather than
141/// rebuilt from scratch. See [`Runner::resume_undelivered`].
142#[derive(Clone)]
143struct SeatJob {
144    spec: AgentSpec,
145    seat: SeatState,
146    cwd: PathBuf,
147    prompt: String,
148    timeout: Duration,
149    allow_write: bool,
150    sessions: bool,
151    artifacts: PathBuf,
152    stem: String,
153    /// The prompt for a seat that has been handed to another roster agent
154    /// (a fresh session): everything the original seat would have
155    /// remembered. `None` when `prompt` already carries it, as the first
156    /// ranking and the implement prompt do. Never a resume-style prompt.
157    handover: Option<String>,
158}
159
160/// How the graph reads one agent invocation.
161///
162/// Quota is split out from an ordinary failure on purpose: a rate-limited call
163/// is known to fail again if retried now, so the retry loop must not spend an
164/// attempt on it. `Dropped` is split out for the opposite reason: unlike
165/// `Failed`, it is worth re-asking, and unlike `Ok`, its text is the CLI's raw
166/// error JSON, never the agent's answer — a caller that matched only
167/// `Ok`/`Quota`/`Failed` before `Dropped` existed must be updated rather than
168/// left to read that JSON as if it were usable output. `resume_undelivered`
169/// is the only caller that acts on it; everywhere else it is reported like an
170/// ordinary failure.
171enum AgentOutcome {
172    /// A usable output.
173    Ok(AgentOutput),
174    /// The CLI ran out of quota / rate limit. Retrying now is pointless.
175    Quota(AgentOutput),
176    /// The CLI hung up on its own stream after billed work. See
177    /// [`agent::AgentOutput::work_undelivered`].
178    Dropped(AgentOutput),
179    /// Any other failure: a timeout, a bad exit code, an empty reply.
180    Failed(String),
181}
182
183/// A request to park the run at its next node boundary.
184///
185/// Cloning is how the request travels: the loop keeps one handle and hands a
186/// clone to each [`Runner`], and every clone points at the same flag. There
187/// is no channel because there is nothing to send - the only message is
188/// "park", it is idempotent, and a flag cannot be missed by a receiver that
189/// was not listening yet.
190///
191/// The boundary is what makes this cheap. Every node writes the run's state
192/// before the next one starts, and every node skips what is already recorded:
193/// `prep` returns early once candidates exist, `implement` asks only the seats
194/// with nothing on disk, `judge` returns early once judgements exist. So a
195/// parked run resumes into exactly the node it stopped before, and no agent
196/// work is thrown away. Killing the process mid-node, by contrast, loses
197/// whatever the seats in flight had not yet written - which for an implement
198/// wave is an hour of paid work.
199///
200/// A [`Runner`] watches two independent handles of this type - see
201/// [`Runner::on_pause`] and [`Runner::watch_interrupt`] - never one shared
202/// between them. `magi serve`'s own shutdown (`Stop::park`) hands out one
203/// clone covering the whole daemon's lifetime and is never asked to un-park,
204/// which is correct exactly because nothing is dispatched after it fires.
205/// `magi serve`'s interrupt scheduler needs the opposite lifetime - a run
206/// that parks for an interrupted task must go on to run other tasks
207/// afterward - so it mints a fresh, unshared [`Pause`] per run instead of
208/// reusing the daemon-wide one.
209#[derive(Debug, Clone, Default)]
210pub struct Pause(Arc<AtomicBool>, Arc<Mutex<Option<String>>>);
211
212impl Pause {
213    /// A pause nobody has asked for yet.
214    #[must_use]
215    pub fn new() -> Self {
216        Self::default()
217    }
218
219    /// Ask the run to park at its next node boundary. Idempotent.
220    pub fn park(&self) {
221        self.0.store(true, Ordering::SeqCst);
222    }
223
224    /// Same as [`Pause::park`], but records why, for [`Runner::park_here`] to
225    /// fold into the run's own `park` event - so an operator reading the run
226    /// later knows this was a deliberate interrupt rather than a shutdown or
227    /// a binary swap. The first reason recorded wins; a park already in
228    /// flight is not relabelled by a second, unrelated request.
229    pub fn park_because(&self, reason: impl Into<String>) {
230        let mut reason_guard = self
231            .1
232            .lock()
233            .unwrap_or_else(std::sync::PoisonError::into_inner);
234        if reason_guard.is_none() {
235            *reason_guard = Some(reason.into());
236        }
237        drop(reason_guard);
238        self.park();
239    }
240
241    /// Has a park been asked for?
242    #[must_use]
243    pub fn parked(&self) -> bool {
244        self.0.load(Ordering::SeqCst)
245    }
246
247    /// Why the park was asked for, when the caller used [`Pause::park_because`].
248    #[must_use]
249    pub fn reason(&self) -> Option<String> {
250        self.1
251            .lock()
252            .unwrap_or_else(std::sync::PoisonError::into_inner)
253            .clone()
254    }
255}
256
257/// Drives one run.
258pub struct Runner {
259    /// Run state; public so the CLI can report on it.
260    pub state: RunState,
261    roles: ResolvedRoles,
262    sem: Arc<Semaphore>,
263    /// Set when the daemon's own shutdown (Ctrl-C, a binary swap) wants the
264    /// run parked at its next node boundary. See [`Pause`]'s own doc for why
265    /// this is never the same handle as `interrupt`.
266    pause: Pause,
267    /// Set when `magi serve`'s interrupt scheduler wants this specific run
268    /// parked at its next node boundary, to let a task marked
269    /// [`crate::queue::Task::interrupt`] run alone before this one carries
270    /// on. Unlike `pause`, a fresh, unshared handle per run - see
271    /// [`Runner::watch_interrupt`].
272    interrupt: Pause,
273}
274
275/// Where the branch's own commits start: its merge base with the base branch
276/// as the remote has it now, else with the recorded `base_commit`. A branch
277/// rebased onto a base that moved past `base_commit` would otherwise count
278/// the base's commits as its own under `base_commit..branch`.
279async fn review_base(
280    repo: &Path,
281    remote: &str,
282    base_branch: &str,
283    base_commit: &str,
284    branch: &str,
285) -> String {
286    review_base_checked(repo, remote, base_branch, base_commit, branch)
287        .await
288        .0
289}
290
291/// [`review_base`] plus whether the base was read from a freshly fetched
292/// tracking ref. A failed fetch still uses whatever tracking ref exists (it is
293/// never older than `base_commit`'s view of the base), but the answer is then
294/// not trusted to rewrite a pull request's title.
295async fn review_base_checked(
296    repo: &Path,
297    remote: &str,
298    base_branch: &str,
299    base_commit: &str,
300    branch: &str,
301) -> (String, bool) {
302    let tracking = format!("{remote}/{base_branch}");
303    let fresh = matches!(git::fetch(repo, remote, base_branch).await, Ok(o) if o.ok());
304    if git::rev_exists(repo, &tracking).await
305        && let Ok(mb) = git::merge_base(repo, &tracking, branch).await
306        && !mb.is_empty()
307    {
308        return (mb, fresh);
309    }
310    let mb = git::merge_base(repo, base_commit, branch)
311        .await
312        .ok()
313        .filter(|mb| !mb.is_empty())
314        .unwrap_or_else(|| base_commit.to_owned());
315    (mb, false)
316}
317
318/// Recompute `reviewed_commits` from the branch's own commits. Left as it was
319/// when git cannot say or finds nothing: a stale list is better than a wrong
320/// or empty one.
321pub(crate) async fn refresh_reviewed_commits(state: &mut RunState, branch: &str) {
322    if !is_review_run(state) {
323        return;
324    }
325    let base = review_base(
326        &state.repo,
327        &state.config.merge.remote,
328        &state.base_branch,
329        &state.base_commit,
330        branch,
331    )
332    .await;
333    if let Ok(subjects) = git::subjects(&state.repo, &base, branch).await
334        && !subjects.is_empty()
335        && state.reviewed_commits.as_ref() != Some(&subjects)
336    {
337        state.reviewed_commits = Some(subjects);
338        state.save().ok();
339    }
340}
341
342/// Subjects of the base's commits between the recorded start and the branch's
343/// merge base: what a stale `base_commit..branch` would have mistaken for the
344/// branch's own work.
345async fn leaked_subjects(state: &RunState, branch: &str) -> Option<Vec<String>> {
346    let (base, trusted) = review_base_checked(
347        &state.repo,
348        &state.config.merge.remote,
349        &state.base_branch,
350        &state.base_commit,
351        branch,
352    )
353    .await;
354    if !trusted {
355        return None;
356    }
357    git::subjects(&state.repo, &state.base_commit, &base)
358        .await
359        .ok()
360}
361
362/// May an adopted pull request's title be replaced with `computed`? Only when
363/// it is empty, magi's own shape, or a base commit's subject that leaked in;
364/// a title a person wrote stays. Never when `computed` is itself a leak.
365pub(crate) fn should_retitle(current: &str, computed: &str, leaked: &[String]) -> bool {
366    let is_leak = |t: &str| leaked.iter().any(|l| l.trim() == t.trim());
367    if is_leak(computed) {
368        return false;
369    }
370    let cur = current.trim();
371    cur.is_empty()
372        || cur.starts_with(REVIEW_PROMPT_OPENING)
373        || cur.starts_with("chore: land candidate")
374        || cur.starts_with("magi: candidate")
375        || is_leak(cur)
376}
377
378/// The commit a run branches from: the base branch as the remote has it.
379///
380/// Two failures this replaces. A run used to branch off `HEAD` and so refused
381/// to start on a dirty tree, which made `magi serve` decline every task for as
382/// long as the operator had work in progress - most of the time. Branching off
383/// the *local* base branch fixed that and introduced a worse one: `land` merges
384/// the winner on GitHub, nothing updates the local ref, and the next run
385/// branches off a base missing everything the previous runs landed. Two tasks
386/// in a row from a phone would have had the second silently re-implementing
387/// against stale code and opening a pull request that reverted the first.
388///
389/// Only refs move here - no checkout, no local branch, no merge - so it is safe
390/// with uncommitted work in the tree. A machine with no network still starts:
391/// the fetch may fail and the local tip is used with a warning, because
392/// refusing to run offline is a worse failure than running against a base the
393/// operator can see for themselves.
394///
395/// One function, called by both entry points. Two answers to "where does a run
396/// branch from" is the kind of drift nobody notices until a diff is wrong.
397/// Bring the local `branch` in line with `<remote>/<branch>` before a review
398/// checks it out.
399///
400/// `git worktree add <branch>` resolves the *local* ref, and a branch pushed by
401/// anything other than plain `git push` from this checkout (a jj colocated
402/// workspace, another clone) moves only the remote-tracking ref - so the local
403/// one can be a stale placeholder. It moves only when local is behind the remote or is an
404/// empty placeholder that diverged from it; unpushed local work is kept, and a real
405/// divergence is refused rather than guessed at.
406async fn sync_review_branch(repo: &Path, branch: &str, remote: &str, base: &str) -> Result<()> {
407    let tracking = format!("{remote}/{branch}");
408    let fetched = git::fetch(repo, remote, branch).await;
409    let fresh = matches!(&fetched, Ok(o) if o.ok()) && git::rev_exists(repo, &tracking).await;
410    let local_exists = git::branch_exists(repo, branch).await?;
411    if !fresh {
412        if !local_exists {
413            bail!("no branch `{branch}` in {} or on {remote}", repo.display());
414        }
415        tracing::warn!(
416            "could not read {tracking}; reviewing the local `{branch}`, which may be stale"
417        );
418        return Ok(());
419    }
420    let remote_sha = git::rev_parse(repo, &tracking).await?;
421    if !local_exists {
422        git::git(repo, &["branch", branch, &tracking]).await?;
423        return Ok(());
424    }
425    let local_sha = git::rev_parse(repo, &format!("refs/heads/{branch}")).await?;
426    if local_sha == remote_sha || git::is_ancestor(repo, &remote_sha, &local_sha).await {
427        return Ok(());
428    }
429    if !git::is_ancestor(repo, &local_sha, &remote_sha).await {
430        // Diverged. `reconcile` settles it only when it can prove nothing is
431        // lost: a local tip that is the remote's change rebased is pushed over
432        // it (lease pinned to the tip read here), a tip whose every extra
433        // commit is empty is a placeholder the remote's work replaced, and
434        // anything else is two different changes - a question for a person.
435        match crate::reconcile::reconcile(repo, remote, branch, &local_sha, &remote_sha, base)
436            .await?
437        {
438            crate::reconcile::Reconciliation::Pushed => {
439                tracing::warn!(
440                    "local `{branch}` ({}) is {tracking} ({}) rebased; pushed it over",
441                    short(&local_sha),
442                    short(&remote_sha)
443                );
444                return Ok(());
445            }
446            crate::reconcile::Reconciliation::Placeholder => {}
447            crate::reconcile::Reconciliation::Genuine(d) => return Err((*d).into()),
448        }
449    }
450    let out = git::git_raw(repo, &["branch", "-f", branch, &tracking]).await?;
451    if !out.ok() {
452        bail!(
453            "local `{branch}` ({}) is stale against {tracking} ({}) but git will not move it: {}",
454            short(&local_sha),
455            short(&remote_sha),
456            out.stderr
457        );
458    }
459    tracing::warn!(
460        "local `{branch}` was stale: fast-forwarded {} -> {}",
461        short(&local_sha),
462        short(&remote_sha)
463    );
464    Ok(())
465}
466
467async fn resolve_base(repo: &Path, base_branch: &str, remote: &str) -> Result<String> {
468    let tracking = format!("{remote}/{base_branch}");
469    let fetched = git::fetch(repo, remote, base_branch).await;
470    if let Ok(out) = &fetched
471        && out.ok()
472        && git::rev_exists(repo, &tracking).await
473    {
474        return git::rev_parse(repo, &tracking).await;
475    }
476    let why = match &fetched {
477        Ok(out) if !out.ok() => out.stderr.lines().next().unwrap_or("").to_owned(),
478        Ok(_) => format!("{remote} has no {base_branch}"),
479        Err(e) => e.to_string(),
480    };
481    // No fallback to the local branch: it may be behind, and branching off an
482    // old commit is the stale-checkout bug this check exists to prevent.
483    bail!(
484        "cannot read {tracking} ({why}); refusing to branch off the local \
485         `{base_branch}`, which may be behind. Fix the remote, or set [merge] \
486         base / remote in magi.toml"
487    )
488}
489
490/// Exclusive claim on one run's `magi fix` step, released on drop — including
491/// on an early return or a panic.
492///
493/// `daemon::is_working_on` only sees a heartbeat-publishing daemon; two
494/// manual `magi fix` invocations against the same run are otherwise
495/// invisible to each other and would race to remove and recreate the same
496/// worktree (see [`Runner::fix_selected`]). The lock file itself is the same
497/// `create_new` shape as `queue::Claim`, but unlike a queued task's lock —
498/// which is only ever reclaimed later, out of band, by
499/// `daemon::sweep_stale_claims` running inside `magi serve`/`magi web` — a
500/// `magi fix` invocation is not necessarily running under either of those, so
501/// nothing would ever sweep a lock a killed or crashed process left behind.
502/// [`Self::acquire`] therefore reclaims a stale lock itself, on the same
503/// conservative PID-liveness policy `sweep_stale_claims` and `cache`'s own
504/// lease use: an unreadable or unparsable pid, or a liveness query the
505/// platform cannot answer, reads as alive and the lock is left in place.
506struct FixClaim {
507    path: PathBuf,
508}
509
510impl FixClaim {
511    fn acquire(dir: &Path) -> Result<Self> {
512        std::fs::create_dir_all(dir).with_context(|| format!("create {}", dir.display()))?;
513        let path = dir.join("fix.lock");
514        match Self::create(&path) {
515            Ok(claim) => Ok(claim),
516            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
517                if Self::reclaim_if_dead(&path) {
518                    Self::create(&path).with_context(|| format!("lock {}", path.display()))
519                } else {
520                    bail!(
521                        "another `magi fix` is already running for this run ({} exists)",
522                        path.display()
523                    )
524                }
525            }
526            Err(e) => Err(e).with_context(|| format!("lock {}", path.display())),
527        }
528    }
529
530    fn create(path: &Path) -> std::io::Result<Self> {
531        let mut f = std::fs::OpenOptions::new()
532            .write(true)
533            .create_new(true)
534            .open(path)?;
535        use std::io::Write as _;
536        // Read back by `reclaim_if_dead` on a later, stuck invocation.
537        writeln!(f, "{}", std::process::id())?;
538        Ok(Self {
539            path: path.to_owned(),
540        })
541    }
542
543    /// True if the lock named a process confirmed dead, in which case it was
544    /// also removed. Never true on an unreadable file, an unparsable pid, or
545    /// a liveness query the platform cannot answer — see this type's own doc.
546    fn reclaim_if_dead(path: &Path) -> bool {
547        let dead = std::fs::read_to_string(path)
548            .ok()
549            .and_then(|body| body.trim().parse::<u32>().ok())
550            .is_some_and(|pid| !crate::proc::pid_alive(pid));
551        dead && std::fs::remove_file(path).is_ok()
552    }
553}
554
555impl Drop for FixClaim {
556    fn drop(&mut self) {
557        let _ = std::fs::remove_file(&self.path);
558    }
559}
560
561impl Runner {
562    /// Start a fresh run against `repo`.
563    pub async fn start(
564        repo: &Path,
565        instruction: String,
566        config: Config,
567        origin: Origin,
568    ) -> Result<Self> {
569        Self::start_naming(repo, instruction, "", config, origin).await
570    }
571
572    /// [`Runner::start`] for a queued task: `also_scan` (the task's title) is
573    /// searched for branch and commit references along with the instruction,
574    /// since a task may name the work it is about only in its title.
575    pub async fn start_naming(
576        repo: &Path,
577        instruction: String,
578        also_scan: &str,
579        config: Config,
580        origin: Origin,
581    ) -> Result<Self> {
582        let repo = git::toplevel(repo).await?;
583        let missing = agent::missing_programs(&config.agents);
584        if !missing.is_empty() {
585            bail!(
586                "these agent programs are not on PATH: {}. Fix the roster in \
587                 magi.toml or install them.",
588                missing.join(", ")
589            );
590        }
591        let base_branch =
592            git::merge_base_branch(&repo, &config.merge.remote, config.merge.base.as_deref())
593                .await?;
594        let base_commit = resolve_base(&repo, &base_branch, &config.merge.remote).await?;
595        let roles = config.resolve_roles()?;
596        let max_parallel = config.graph.max_parallel.max(1);
597        // A task that points at work already in the repository starts from
598        // it; what the repository says about each reference is recorded.
599        let seeds = refs::resolve(
600            &repo,
601            &base_commit,
602            &config.merge.remote,
603            &format!("{also_scan}\n{instruction}"),
604        )
605        .await;
606        refs::plan(&repo, &seeds).await?;
607        let mut state = RunState::new(repo, base_branch, base_commit, instruction, config);
608        // Recorded before the first save, so a crash right after minting
609        // cannot leave a run with no origin. Legibility only: nothing reads it
610        // to decide anything.
611        state.origin = Some(origin);
612        for seed in &seeds {
613            state.event(
614                "seed",
615                refs::describe(std::slice::from_ref(seed)).unwrap_or_default(),
616            );
617        }
618        state.seeds = seeds;
619        state.event("start", format!("run {} created", state.id));
620        state.save()?;
621        Ok(Self {
622            state,
623            roles,
624            sem: Arc::new(Semaphore::new(max_parallel)),
625            pause: Pause::new(),
626            interrupt: Pause::new(),
627        })
628    }
629
630    /// Open a review-only run against work that already exists on `branch`.
631    ///
632    /// The expensive half of the graph is the implement wave — measured at
633    /// 111 and 134 internal tool-loop turns on this repository, against a
634    /// handful for a judge or a reviewer. The cheap half is worth running on
635    /// hand-written work too, and there was no way to reach it.
636    ///
637    /// No new state and no schema change are needed: a run with **one** viable
638    /// candidate and a tally already decided degrades `execute` to exactly
639    /// review → gate → merge, because `judge` skips a single-candidate field,
640    /// `deliberate` has fewer than two first choices to reconcile, `vote`
641    /// returns early, `tally` is already present and `fold_losers` has no
642    /// losers. Resuming such a run therefore does the right thing as well.
643    pub async fn review(repo: &Path, branch: &str, config: Config, origin: Origin) -> Result<Self> {
644        Self::review_taking_over(repo, branch, config, None, origin).await
645    }
646
647    /// [`Runner::review`] for a queued task's retry: when an earlier attempt
648    /// at the same task still has `branch` checked out, its worktree is
649    /// released first if that is safe (see [`crate::handover`]), and the
650    /// review refuses with the reason if it is not. `None` is a hand-run
651    /// review: it has no earlier attempts, so only a worktree of a dead run
652    /// magi recorded itself can be released.
653    pub async fn review_taking_over(
654        repo: &Path,
655        branch: &str,
656        config: Config,
657        takeover: Option<crate::handover::Takeover>,
658        origin: Origin,
659    ) -> Result<Self> {
660        let repo = git::toplevel(repo).await?;
661        let missing = agent::missing_programs(&config.agents);
662        if !missing.is_empty() {
663            bail!(
664                "these agent programs are not on PATH: {}. Fix the roster in \
665                 magi.toml or install them.",
666                missing.join(", ")
667            );
668        }
669        let base_branch =
670            git::merge_base_branch(&repo, &config.merge.remote, config.merge.base.as_deref())
671                .await?;
672        if base_branch == branch {
673            bail!("`{branch}` is the base branch; there is nothing to review against");
674        }
675        let base_commit = resolve_base(&repo, &base_branch, &config.merge.remote).await?;
676
677        let roles = config.resolve_roles()?;
678        let max_parallel = config.graph.max_parallel.max(1);
679        let mut state = RunState::new(
680            repo.clone(),
681            base_branch,
682            base_commit.clone(),
683            String::new(),
684            config,
685        );
686        state.origin = Some(origin);
687
688        // Released before anything else touches the branch: a stale local
689        // branch is moved with `git branch -f`, which git refuses while an
690        // earlier attempt's worktree still has it checked out. Everything
691        // after this point that can fail puts the old run back.
692        // A hand-run review has no task, hence no earlier attempts, but a
693        // worktree of a dead run magi made may still be released.
694        let takeover = takeover.unwrap_or_else(|| crate::handover::Takeover {
695            earlier: Vec::new(),
696            home: crate::run::home(),
697            choice: None,
698        });
699        let released = crate::handover::release(&repo, branch, &state.id, &takeover).await?;
700        if let Some(released) = &released {
701            state.event(
702                "release",
703                format!(
704                    "took `{branch}` over from run {}: its worktree was released: {}",
705                    crate::run::short_of(&released.old_id),
706                    released.audit
707                ),
708            );
709        }
710        // The owner's answer to an earlier divergence question is applied
711        // here: after the release (git will not move a checked-out branch)
712        // and before the sync that would otherwise ask again.
713        if let Some(choice) = takeover.choice.as_ref()
714            && let Err(e) =
715                crate::reconcile::apply_choice(&repo, &state.config.merge.remote, branch, choice)
716                    .await
717        {
718            if let Some(released) = &released {
719                released.restore(&repo, branch).await;
720            }
721            return Err(e.context("applying the owner's answer about the diverged branch"));
722        }
723        // An earlier attempt of this task that published the task head, and
724        // whose work is the branch under review, hands that head over: the
725        // review run updates the same pull request. Without one (a legacy
726        // `magi/<run>/<label>` PR, a hand-named branch) the branch keeps its
727        // own head and its own open pull request is adopted as before.
728        for id in takeover.earlier.iter().rev() {
729            if let Ok(old) = RunState::load_under(id, &takeover.home)
730                && let Some(head) = old.pr_head.clone()
731                && (head == branch || old.winner().is_some_and(|w| w.branch == branch))
732            {
733                state.pr_head = Some(head);
734                break;
735            }
736        }
737        let opened =
738            Self::open_review(&repo, branch, state, roles, max_parallel, base_commit).await;
739        if opened.is_err()
740            && let Some(released) = &released
741        {
742            released.restore(&repo, branch).await;
743        }
744        opened
745    }
746
747    /// The half of [`Runner::review_taking_over`] that can fail after an
748    /// earlier attempt's worktree was released.
749    async fn open_review(
750        repo: &Path,
751        branch: &str,
752        mut state: RunState,
753        roles: ResolvedRoles,
754        max_parallel: usize,
755        base_commit: String,
756    ) -> Result<Self> {
757        sync_review_branch(repo, branch, &state.config.merge.remote, &base_commit).await?;
758        // The commit subjects are the closest thing to a task statement that
759        // existing work carries, and the reviewers are told as much.
760        let start = review_base(
761            repo,
762            &state.config.merge.remote,
763            &state.base_branch,
764            &base_commit,
765            branch,
766        )
767        .await;
768        let log = git::log_oneline(repo, &start, branch)
769            .await
770            .unwrap_or_default();
771        let instruction = format!(
772            "Review the work already on branch `{branch}`. There is no task \
773             statement: what the change claims to do is whatever its commits \
774             say.\n\n{}",
775            if log.trim().is_empty() {
776                "(no commit messages)"
777            } else {
778                log.trim()
779            }
780        );
781        state.instruction = instruction;
782        state.reviewed_commits = Some(
783            git::subjects(repo, &start, branch)
784                .await
785                .unwrap_or_default(),
786        );
787
788        // An attached worktree, so the fixer's commits land on the branch under
789        // review rather than on a detached head nobody will look at again.
790        let worktree = state.worktree_root().join("under-review");
791        if let Some(parent) = worktree.parent() {
792            tokio::fs::create_dir_all(parent).await.ok();
793        }
794        let path = worktree.to_string_lossy().to_string();
795        git::git(repo, &["worktree", "add", &path, branch])
796            .await
797            .with_context(|| {
798                format!("checking out `{branch}` at {path} (is it checked out elsewhere?)")
799            })?;
800
801        let setup = match ensure_setup_config(&mut state, repo).await {
802            Ok(()) => worktree_setup::prepare(&state.config, repo, &worktree).await,
803            Err(e) => Err(e),
804        };
805        if let Err(e) = setup {
806            git::worktree_remove(repo, &worktree).await.ok();
807            return Err(e);
808        }
809        let commits = git::commits_ahead(&worktree, &base_commit, "HEAD")
810            .await
811            .unwrap_or(0);
812        if commits == 0 {
813            git::worktree_remove(repo, &worktree).await.ok();
814            bail!("`{branch}` has no commits beyond {}", short(&base_commit));
815        }
816        let files = git::changed_files(&worktree, &base_commit, "HEAD")
817            .await
818            .map(|f| f.len())
819            .unwrap_or(0);
820        if files == 0
821            && let (Ok(head_tree), Ok(base_tree)) = (
822                git::tree_of(&worktree, "HEAD").await,
823                git::tree_of(&worktree, &base_commit).await,
824            )
825            && head_tree == base_tree
826        {
827            let head = git::rev_parse(&worktree, "HEAD").await.unwrap_or_default();
828            git::worktree_remove(repo, &worktree).await.ok();
829            bail!(
830                "`{branch}` at {} has a tree identical to base {}; this usually means \
831                 the branch ref is stale (check `git rev-parse refs/heads/{branch}` \
832                 against `{}/{branch}`) rather than an empty change",
833                short(&head),
834                short(&base_commit),
835                state.config.merge.remote
836            );
837        }
838        let stat = git::diff_stat(&worktree, &base_commit, "HEAD")
839            .await
840            .unwrap_or_default();
841
842        state.candidates.push(Candidate {
843            index: 0,
844            label: 'A',
845            // Not an agent id on purpose: nothing in the roster wrote this, and
846            // the stats tables must not credit anyone with a win for it.
847            agent: EXISTING_BRANCH.to_owned(),
848            branch: branch.to_owned(),
849            worktree,
850            summary: String::new(),
851            stat,
852            files,
853            commits,
854            empty: false,
855            failed: None,
856            verified_noop: None,
857            duration_ms: 0,
858            folded: false,
859        });
860        state.tally = Some(Tally {
861            first_choice: BTreeMap::from([('A', 0)]),
862            borda: BTreeMap::new(),
863            winner: 'A',
864            rankings: 0,
865            unanimous_initial: false,
866            deliberated: false,
867            changed_votes: 0,
868            unanimous_final: false,
869            tie_break: None,
870            // No panel sat, so no quorum applies. Zero judges is the correct
871            // number for work that never competed, and must not be reported as
872            // a collapsed panel.
873            judges: 0,
874            present: 0,
875            quorum: 0,
876            met_quorum: true,
877            uncontested: Some("review-only run: nothing competed".to_owned()),
878        });
879        state.status = RunStatus::Reviewing;
880        state.event(
881            "start",
882            format!(
883                "review-only run {} on `{branch}` ({files} files, {commits} commits)",
884                state.id
885            ),
886        );
887        state.save()?;
888        Ok(Self {
889            state,
890            roles,
891            sem: Arc::new(Semaphore::new(max_parallel)),
892            pause: Pause::new(),
893            interrupt: Pause::new(),
894        })
895    }
896
897    /// Reopen an existing run.
898    pub fn resume(id: &str) -> Result<Self> {
899        let state = RunState::load(id)?;
900        if let Some(to) = &state.released_to {
901            bail!(
902                "run {} cannot be resumed: its worktree was released to run {}",
903                state.short(),
904                crate::run::short_of(to)
905            );
906        }
907        let roles = state.config.resolve_roles()?;
908        let max_parallel = state.config.graph.max_parallel.max(1);
909        Ok(Self {
910            state,
911            roles,
912            sem: Arc::new(Semaphore::new(max_parallel)),
913            pause: Pause::new(),
914            interrupt: Pause::new(),
915        })
916    }
917
918    /// Walk the graph to a terminal state, skipping nodes already recorded.
919    ///
920    /// Every way a run is driven - the queue loop, `magi run`, a resume from
921    /// the phone - ends here, so this is the one place a run that ended
922    /// Blocked / Stalled / Failed, or died with an error, is announced to the
923    /// notification centre. Best-effort: see [`crate::notices::raise`].
924    pub async fn execute(&mut self) -> Result<()> {
925        let result = self.execute_graph().await;
926        self.mark_driver_exited();
927        let ended = if result.is_err() {
928            Some(crate::notices::run_stopped(&self.state.id, &self.state))
929        } else {
930            crate::notices::run_ended(&self.state)
931        };
932        if let Some(notice) = ended {
933            crate::notices::raise(notice);
934        }
935        result
936    }
937
938    /// Record that this process no longer drives the run, so its pid (a
939    /// daemon's outlives the run) is not read as a live driver.
940    ///
941    /// Written onto the record as it is on disk, never this copy: another
942    /// process may have resumed the run (recording its own pid and clearing
943    /// the flag) or released its worktree since this copy was read, and
944    /// saving over that would mark a running driver dead. Only a record still
945    /// naming this process as the driver is touched.
946    fn mark_driver_exited(&mut self) {
947        self.state.driver_exited = true;
948        let pid = std::process::id();
949        let Ok(_lock) = crate::pr_head::owner_lock(&crate::run::home(), &self.state.id) else {
950            return;
951        };
952        let Ok(mut disk) = RunState::load(&self.state.id) else {
953            return;
954        };
955        if disk.released_to.is_some() || disk.driver_pid != Some(pid) || disk.driver_exited {
956            return;
957        }
958        disk.driver_exited = true;
959        if let Err(e) = disk.save() {
960            tracing::warn!("could not record that run {} stopped: {e:#}", self.state.id);
961        }
962    }
963
964    /// Validate ownership and persist the driver claim in one critical section.
965    fn claim_driver(&mut self) -> Result<()> {
966        let _lock = crate::pr_head::owner_lock(&crate::run::home(), &self.state.id)?;
967        let disk = RunState::load(&self.state.id)?;
968        crate::pr_head::ensure_owner(&disk)?;
969        // Moving again, so it is no longer parked. Set before the walk rather
970        // than in `resume`, so every way of re-entering the graph clears it
971        // and a card cannot claim a run is waiting to be resumed while the
972        // agents are already working.
973        self.state.parked = false;
974        // Any seat this state still lists as answering belongs to whatever
975        // process last drove this run — this one included, if it crashed
976        // mid-wave. Cleared and flushed immediately, before anything else
977        // runs, so a resume can never show a seat as live when nothing is
978        // asking it anything yet; the node that actually dispatches the next
979        // wave repopulates it.
980        self.state.clear_active();
981        // Recorded in the same spot, and flushed together with the clear
982        // above: this is the pid a reader checks (`RunState::liveness`) when
983        // no daemon claim exists to answer "is a process still driving this
984        // run" — a plain `magi run` / `magi review` typed into a terminal
985        // claims nothing there. Always overwritten, never only-if-absent, so
986        // a resumed run's stale pid from a previous, possibly-dead process
987        // can never survive into this one's own report. Unlike
988        // `clear_active`, this changes on every single `execute()` call, so
989        // the save below is now unconditional rather than only-if-cleared.
990        //
991        // `driver_started_at` is recorded in the same breath, from this same
992        // pid, so `liveness` can tell a live pid that is genuinely still us
993        // apart from one the OS has since handed to an unrelated process —
994        // see that field's own doc for why the pid alone is not enough.
995        // A resume that raced a takeover: the record on disk says the worktree
996        // was handed to a later run after this copy was read. Saving over it
997        // would erase that and drive a run with nothing to run in.
998        if let Some(to) = &disk.released_to {
999            bail!(
1000                "run {} cannot continue: its worktree was released to run {}",
1001                self.state.short(),
1002                crate::run::short_of(to)
1003            );
1004        }
1005        let pid = std::process::id();
1006        self.state.driver_pid = Some(pid);
1007        self.state.driver_started_at = crate::proc::process_started_at(pid);
1008        self.state.driver_exited = false;
1009        self.state.save()?;
1010        Ok(())
1011    }
1012
1013    async fn execute_graph(&mut self) -> Result<()> {
1014        self.claim_driver()?;
1015        // Publication already passed review and gate. Resume only that node:
1016        // syncing to a newer base here could change the verified commit.
1017        if self.state.pr_publication_pending {
1018            self.merge().await?;
1019            self.state.save()?;
1020            self.settle_questions();
1021            return Ok(());
1022        }
1023        // A run that already lost its quorum never resumes into the verdict
1024        // machinery: `deliberate` and `vote` would otherwise clobber the
1025        // stalled marker back to Voting and the run would keep going past a
1026        // verdict that is no longer trustworthy. Everything already recorded is
1027        // kept, so the run stays resumable (or foldable) for a human to pick up.
1028        //
1029        // On --resume the run gets one chance to repair itself: the seats a
1030        // rate limit took out are re-asked. If their quota has since reset and
1031        // the quorum is restored, the run picks up and finishes; otherwise it
1032        // stays stale and still-resumable for a later retry. If it does not
1033        // recover, the returned status stays `Stalled` and nothing was
1034        // clobbered (the recovery only mutates entries for the lost seats).
1035        if self.state.status == RunStatus::Stalled {
1036            if self.recover_stall().await? {
1037                self.finish_after_tally().await?;
1038            } else {
1039                // Still below quorum: persist the marker and stay resumable.
1040                self.state.save()?;
1041            }
1042            return Ok(());
1043        }
1044        // A run parked inside `land` - watching CI, mid fix-round, or
1045        // waiting on the owner's merge approval - resumes directly into it,
1046        // never back through `prep`. Everything before `merge` already
1047        // concluded; that is the only way `status` reaches `Landing` in the
1048        // first place. Re-walking `review_loop` first would also be actively
1049        // wrong: its own status recomputation (see its doc) treats any
1050        // clean round as reason to set `status` to `Gating`, which would
1051        // clobber this marker before `merge` ever ran, and this run would
1052        // never find its way back into `land` at all.
1053        if self.state.status == RunStatus::Landing {
1054            self.run_land().await?;
1055            // `run_land` may have settled the run right here - CI came back
1056            // green and the PR merged, say - without ever passing back
1057            // through `merge`'s own trailing call. Whatever it left `status`
1058            // as is what this has to read.
1059            self.settle_questions();
1060            return Ok(());
1061        }
1062        // A run parked at `merge` on the owner's word about a withheld PR text
1063        // resumes straight into `merge`. Re-walking the tail would sync to the
1064        // base again (possibly rebasing, possibly spending the fixer) after the
1065        // gate had passed, and then push a tree nobody reviewed or gated.
1066        if self.state.github_text.is_some()
1067            && self.state.merge.is_none()
1068            && self.state.status == RunStatus::Gating
1069        {
1070            self.merge().await?;
1071            self.state.save()?;
1072            return Ok(());
1073        }
1074        self.prep().await?;
1075        if self.park_here()? {
1076            return Ok(());
1077        }
1078        self.advise().await?;
1079        if self.park_here()? {
1080            return Ok(());
1081        }
1082        self.implement().await?;
1083        if self.park_here()? {
1084            return Ok(());
1085        }
1086        // `after_implement` already saved the state and settled any open
1087        // questions when it set this; nothing later in the graph has
1088        // anything to judge.
1089        if self.state.status == RunStatus::VerifiedNoop {
1090            return Ok(());
1091        }
1092        self.judge().await?;
1093        if self.park_here()? {
1094            return Ok(());
1095        }
1096        self.deliberate().await?;
1097        if self.park_here()? {
1098            return Ok(());
1099        }
1100        self.vote().await?;
1101        if self.park_here()? {
1102            return Ok(());
1103        }
1104        self.tally()?;
1105        // A verdict that lost its quorum is not trustworthy: do not review,
1106        // gate, or merge on it. Everything already done is kept, so the run
1107        // stays resumable (or foldable); the human can replace the agent that
1108        // ran out of quota and pick it up.
1109        if self.state.status == RunStatus::Stalled {
1110            // Persist the stalled marker now — the normal end-of-execute save
1111            // below is below this early return, and without it a resumed run
1112            // would reload a pre-tally status and keep going.
1113            self.state.save()?;
1114            return Ok(());
1115        }
1116        self.finish_after_tally().await?;
1117        Ok(())
1118    }
1119
1120    /// Park here if asked to, recording it in the run's own timeline.
1121    ///
1122    /// Returns whether the caller should stop walking the graph. The state is
1123    /// saved either way by the node that just finished; this adds the event so
1124    /// the operator's card says why a run that is neither finished nor moving
1125    /// is sitting where it is.
1126    fn park_here(&mut self) -> Result<bool> {
1127        // Either handle asking is enough - see `Pause`'s own doc for why
1128        // they are never the same one. `interrupt` is checked second so a
1129        // reason it carries is preferred in the message below over a plain
1130        // shutdown park racing it at the same boundary.
1131        if !self.pause.parked() && !self.interrupt.parked() {
1132            return Ok(false);
1133        }
1134        let why = match self.interrupt.reason().or_else(|| self.pause.reason()) {
1135            Some(reason) => format!(
1136                "parked after `{}` ({reason}) — resume to carry on from here",
1137                self.state.status.as_str()
1138            ),
1139            None => format!(
1140                "parked after `{}` — resume to carry on from here",
1141                self.state.status.as_str()
1142            ),
1143        };
1144        self.state.event("park", why);
1145        self.state.parked = true;
1146        self.state.save()?;
1147        Ok(true)
1148    }
1149
1150    /// Hand the runner the pause `magi serve`'s own shutdown watches.
1151    pub fn on_pause(&mut self, pause: Pause) {
1152        self.pause = pause;
1153    }
1154
1155    /// Hand the runner a second, independent pause: `magi serve`'s interrupt
1156    /// scheduler asking this one run - and no other - to park so a task
1157    /// marked [`crate::queue::Task::interrupt`] can run alone. See
1158    /// [`Pause`]'s own doc for why this is never [`Runner::on_pause`]'s
1159    /// handle.
1160    pub fn watch_interrupt(&mut self, pause: Pause) {
1161        self.interrupt = pause;
1162    }
1163
1164    /// Abandon this run's own open questions, once `status` has actually
1165    /// settled rather than merely paused.
1166    ///
1167    /// `Blocked` and `Stalled` are `RunStatus::resumable` — a human can pick
1168    /// either back up with the candidates, the review round and the seat
1169    /// sessions already on disk, so a question an implementer asked mid-round
1170    /// may still get a real answer read by a real resume. Only the statuses
1171    /// `resumable` excludes are actually final: the run merged, it reached
1172    /// `Ready` with nothing left to do, it failed outright with no
1173    /// established point to continue from, or every candidate agreed, with
1174    /// evidence, that nothing belonged in the worktree (`VerifiedNoop`). In
1175    /// every one of those the seat that asked is gone for good, exactly like
1176    /// the run being deleted under `magi run rm` - so the same cleanup
1177    /// applies, worded for what actually happened instead of "the run was
1178    /// deleted".
1179    ///
1180    /// Best-effort and silent on success: called from every place `status`
1181    /// can land on one of those three, including ones a resumed run revisits,
1182    /// so it must cost nothing when there was nothing open to begin with.
1183    fn settle_questions(&mut self) {
1184        if let Err(e) = ask::Questions::open().settle_run(&self.state.id, self.state.status) {
1185            tracing::warn!("abandon questions for {}: {e:#}", self.state.id);
1186        }
1187    }
1188
1189    /// The tail of the graph after a trustworthy tally: fold losers, review,
1190    /// gate, merge, and persist.
1191    async fn finish_after_tally(&mut self) -> Result<()> {
1192        // Before the losers are folded: their branches are what the graft
1193        // reads from.
1194        self.graft().await?;
1195        self.fold_losers().await?;
1196        // Before review starts, and again right before the gate: a run's
1197        // review rounds can themselves take long enough for the base to move
1198        // a second time, and the gate is the one node whose "green" gets
1199        // acted on.
1200        self.sync_to_base().await?;
1201        if self.state.status == RunStatus::AlreadyInBase {
1202            return Ok(());
1203        }
1204        self.review_loop().await?;
1205        self.sync_to_base().await?;
1206        if self.state.status == RunStatus::AlreadyInBase {
1207            return Ok(());
1208        }
1209        self.gate().await?;
1210        self.merge().await?;
1211        self.state.save()?;
1212        Ok(())
1213    }
1214
1215    // ---------------------------------------------------------------- prep
1216
1217    /// Run `[worktree] setup` in a seat's fresh worktree. A failure blocks the
1218    /// run with the step and its output in the event; it is never swallowed.
1219    /// During `prep` (`in_prep`) the whole preparation is rolled back, so a
1220    /// resume starts over instead of finding candidates and missing seats.
1221    async fn setup_seat_worktree(&mut self, repo: &Path, wt: &Path, in_prep: bool) -> Result<()> {
1222        let result = match ensure_setup_config(&mut self.state, repo).await {
1223            Ok(()) => worktree_setup::prepare(&self.state.config, repo, wt).await,
1224            Err(e) => Err(e),
1225        };
1226        if let Err(e) = result {
1227            git::worktree_remove(repo, wt).await.ok();
1228            if in_prep {
1229                self.rollback_prep(repo).await;
1230            }
1231            self.state.status = RunStatus::Blocked;
1232            self.state.event(
1233                "setup",
1234                format!("worktree setup failed in {}: {e:#}", wt.display()),
1235            );
1236            self.state.save()?;
1237            return Err(e);
1238        }
1239        Ok(())
1240    }
1241
1242    /// Undo everything `prep` created, so a resume re-runs it whole.
1243    async fn rollback_prep(&mut self, repo: &Path) {
1244        let root = self.state.worktree_root();
1245        for c in std::mem::take(&mut self.state.candidates) {
1246            git::worktree_remove(repo, &c.worktree).await.ok();
1247            git::branch_delete(repo, &c.branch).await.ok();
1248        }
1249        let seats = self.roles.judges.len().max(self.state.config.graph.judges);
1250        for j in 1..=seats {
1251            git::worktree_remove(repo, &root.join(format!("judge-{j}")))
1252                .await
1253                .ok();
1254        }
1255        for k in 1..=self.state.config.graph.advisors {
1256            git::worktree_remove(repo, &root.join(format!("advisor-{k}")))
1257                .await
1258                .ok();
1259        }
1260    }
1261
1262    async fn prep(&mut self) -> Result<()> {
1263        if !self.state.candidates.is_empty() {
1264            return Ok(());
1265        }
1266        self.state.status = RunStatus::Prep;
1267        let repo = self.state.repo.clone();
1268        let base = self.state.base_commit.clone();
1269        let plan = refs::plan(&repo, &self.state.seeds).await?;
1270        let start = plan.start.clone().unwrap_or_else(|| base.clone());
1271        let root = self.state.worktree_root();
1272        let labels = blind::assign_labels(self.roles.implementers.len(), self.state.seed);
1273
1274        // The hook is the write-time half of the blindness contract; the
1275        // presentation filter in `blind` is the half that cannot be bypassed.
1276        let hooks_dir = self.state.dir().join("hooks");
1277        if self.state.config.blind.commit_msg_hook {
1278            std::fs::create_dir_all(&hooks_dir)
1279                .with_context(|| format!("create {}", hooks_dir.display()))?;
1280            let script = blind::commit_msg_hook(&self.state.config.blind.strip_lines);
1281            let path = hooks_dir.join("commit-msg");
1282            std::fs::write(&path, script).with_context(|| format!("write {}", path.display()))?;
1283            make_executable(&path)?;
1284            // Ref-counted rather than a plain idempotent set: with more than
1285            // one run able to be in flight in the same repository at once
1286            // (see `Config::daemon.max_concurrent_runs`), a bare "already
1287            // true?" check cannot tell "another run of mine still needs
1288            // this" from "nobody does", and the run that happens to finish
1289            // first would disable the hook out from under a sibling still
1290            // relying on it.
1291            git::acquire_worktree_config(&repo).await?;
1292            self.state.enabled_worktree_config = true;
1293        }
1294
1295        // `[worktree] setup` hides its products per worktree, which needs the
1296        // same per-worktree config the hook does. Held until fold, like it.
1297        ensure_setup_config(&mut self.state, &repo).await?;
1298
1299        for (index, (spec, label)) in self
1300            .roles
1301            .implementers
1302            .clone()
1303            .into_iter()
1304            .zip(labels)
1305            .enumerate()
1306        {
1307            let branch = self.state.branch_for(label);
1308            let worktree = root.join(format!("cand-{label}"));
1309            git::worktree_add_branch(&repo, &worktree, &branch, &start).await?;
1310            if self.state.config.blind.commit_msg_hook {
1311                git::set_worktree_hooks_path(&worktree, &hooks_dir).await?;
1312            }
1313            git::local_exclude(&worktree, "/.magi/").await?;
1314            if let Err(e) = worktree_setup::prepare(&self.state.config, &repo, &worktree).await {
1315                // Nothing of this prep may survive: a resume must start over,
1316                // not find half the candidates and skip the rest.
1317                git::worktree_remove(&repo, &worktree).await.ok();
1318                git::branch_delete(&repo, &branch).await.ok();
1319                self.rollback_prep(&repo).await;
1320                self.state.status = RunStatus::Blocked;
1321                self.state
1322                    .event("prep", format!("worktree setup failed: {e:#}"));
1323                self.state.save()?;
1324                return Err(e);
1325            }
1326            for pick in &plan.picks {
1327                if let Err(e) = git::cherry_pick(&worktree, pick).await {
1328                    self.state.status = RunStatus::Blocked;
1329                    self.state
1330                        .event("prep", format!("cannot apply referenced commit: {e}"));
1331                    self.state.save()?;
1332                    return Err(e);
1333                }
1334            }
1335            self.state.candidates.push(Candidate {
1336                index,
1337                label,
1338                agent: spec.id.clone(),
1339                branch,
1340                worktree,
1341                summary: String::new(),
1342                stat: String::new(),
1343                files: 0,
1344                commits: 0,
1345                empty: false,
1346                failed: None,
1347                verified_noop: None,
1348                duration_ms: 0,
1349                folded: false,
1350            });
1351        }
1352
1353        for j in 1..=self.roles.judges.len() {
1354            let wt = root.join(format!("judge-{j}"));
1355            if !wt.exists() {
1356                git::worktree_add_detached(&repo, &wt, &base).await?;
1357                self.setup_seat_worktree(&repo, &wt, true).await?;
1358            }
1359        }
1360
1361        // Disposable, detached checkouts for the design-deliberation stage's
1362        // advisor seats — the same shape as the judges' above, at the same
1363        // base commit, since advisors also only ever read. Sized off the
1364        // configured count directly rather than a resolved roster: unlike
1365        // `implementers`/`judges`/`reviewers`, advisor seats are resolved
1366        // lazily inside `advise` itself (see `Config::advisors`'s doc), so
1367        // `prep` has no `ResolvedRoles` field to read a count from here.
1368        if self.state.config.graph.advise {
1369            for k in 1..=self.state.config.graph.advisors {
1370                let wt = root.join(format!("advisor-{k}"));
1371                if !wt.exists() {
1372                    git::worktree_add_detached(&repo, &wt, &base).await?;
1373                    self.setup_seat_worktree(&repo, &wt, true).await?;
1374                }
1375            }
1376        }
1377
1378        // A judge cannot tell it is looking at its own patch — the seats keep
1379        // separate conversations — but a panel that shares agents with the
1380        // field is less independent than it looks, and that is worth saying out
1381        // loud once per run rather than leaving it in the config.
1382        let authors: Vec<&str> = self
1383            .roles
1384            .implementers
1385            .iter()
1386            .map(|a| a.id.as_str())
1387            .collect();
1388        let overlap: Vec<String> = self
1389            .roles
1390            .judges
1391            .iter()
1392            .enumerate()
1393            .filter(|(_, j)| authors.contains(&j.id.as_str()))
1394            .map(|(i, j)| format!("judge {} = {}", i + 1, j.id))
1395            .collect();
1396        if !overlap.is_empty() {
1397            let note = format!(
1398                "{} also authored a candidate; blind, but the panel is less \
1399                 independent than {} distinct agents would be",
1400                overlap.join(", "),
1401                self.roles.judges.len()
1402            );
1403            self.state.event("prep", note);
1404        }
1405
1406        self.state.event(
1407            "prep",
1408            format!(
1409                "{} candidates, {} judges, base {} ({})",
1410                self.state.candidates.len(),
1411                self.roles.judges.len(),
1412                &self.state.base_commit[..7.min(self.state.base_commit.len())],
1413                self.state.base_branch
1414            ),
1415        );
1416        self.state.status = RunStatus::Implementing;
1417        self.state.save()?;
1418        Ok(())
1419    }
1420
1421    // -------------------------------------------------------------- advise
1422
1423    /// The design-deliberation stage: independent, read-only advisor seats
1424    /// each sketch a design before any implementer touches the repository,
1425    /// and (when at least one produced a usable proposal) a synthesis seat
1426    /// blends them into a brief `implement` carries in every candidate's
1427    /// prompt.
1428    ///
1429    /// `[graph] advise` is the on/off switch, on by default; `[graph]
1430    /// advisors` is the proposal count. Everything here is best-effort and
1431    /// non-fatal to the run: a misconfigured `[roles] advisors`, a roster
1432    /// that cannot reach quota, or a synthesis seat that produced nothing
1433    /// usable all leave `implement` exactly as it was before this stage
1434    /// existed — the task instruction alone — rather than failing the whole
1435    /// competition over an enrichment stage. Every outcome is still recorded
1436    /// as an event, so a run that got nothing from this stage says why.
1437    ///
1438    /// [`RunState::advise_attempted`] is this node's idempotency marker, the
1439    /// same role [`RunState::judge_skipped`] plays for `judge`: without it a
1440    /// resumed run whose stage failed would re-run it, and re-spend the
1441    /// agent calls, on every reentry before `implement`.
1442    ///
1443    /// Also skipped once any candidate shows implementation progress — the
1444    /// exact predicate `implement` itself uses to decide a candidate is no
1445    /// longer "todo" (see its own `todo` filter). `advise_attempted` alone
1446    /// is not enough: a run created by an older binary that predates this
1447    /// field deserializes it as `false` (`#[serde(default)]`), so resuming
1448    /// an already-`Implementing`-or-later run under this build would
1449    /// otherwise walk straight back through `prep` (a no-op once candidates
1450    /// exist) into this node and spawn every advisor seat against worktrees
1451    /// `prep` never recreated — after implementation has already started,
1452    /// which is exactly the invariant this stage exists to guarantee.
1453    async fn advise(&mut self) -> Result<()> {
1454        let implement_untouched = self
1455            .state
1456            .candidates
1457            .iter()
1458            .all(|c| c.commits == 0 && c.failed.is_none() && !c.empty);
1459        if !self.state.config.graph.advise || self.state.advise_attempted {
1460            return Ok(());
1461        }
1462        if !implement_untouched {
1463            self.state.event(
1464                "advise",
1465                "skipping the design-deliberation stage: at least one \
1466                 candidate already shows implementation progress, so this \
1467                 run is past the point the stage exists to run before"
1468                    .to_owned(),
1469            );
1470            self.state.advise_attempted = true;
1471            self.state.save()?;
1472            return Ok(());
1473        }
1474        let run_id = self.state.id.clone();
1475        let prompts = self.state.config.prompts.clone();
1476        let instruction = self.state.instruction.clone();
1477        let language = self.state.config.graph.language.clone();
1478        let root = self.state.worktree_root();
1479        let n = self.state.config.graph.advisors;
1480        let where_recorded = self.state.dir().join("run.json");
1481
1482        let seats = match self.state.config.advisors() {
1483            Ok(seats) if !seats.is_empty() => seats,
1484            Ok(_) => {
1485                self.state.event(
1486                    "advise",
1487                    format!(
1488                        "[graph] advisors is 0; skipping the design-deliberation \
1489                         stage and continuing without a synthesis brief (see {})",
1490                        where_recorded.display()
1491                    ),
1492                );
1493                self.state.advise_attempted = true;
1494                self.state.save()?;
1495                return Ok(());
1496            }
1497            Err(e) => {
1498                self.state.event(
1499                    "advise",
1500                    format!(
1501                        "could not resolve advisor seats ({e:#}); continuing \
1502                         without a design-deliberation brief (see {})",
1503                        where_recorded.display()
1504                    ),
1505                );
1506                self.state.advise_attempted = true;
1507                self.state.save()?;
1508                return Ok(());
1509            }
1510        };
1511
1512        let timeout = Duration::from_secs(self.state.config.graph.timeout_judge.max(1));
1513        let artifacts = agent::artifacts_dir(&self.state.dir());
1514        let worktrees: Vec<PathBuf> = (1..=n).map(|k| root.join(format!("advisor-{k}"))).collect();
1515
1516        let mut jobs = Vec::new();
1517        for (i, spec) in seats.iter().cloned().enumerate() {
1518            let seat_key = format!("advisor-{}", i + 1);
1519            let seat = self.seat(&seat_key, &spec.id);
1520            jobs.push(SeatJob {
1521                prompt: prompt::advisor(&instruction, i + 1, seats.len(), &language),
1522                spec,
1523                seat,
1524                cwd: worktrees[i % worktrees.len()].clone(),
1525                timeout,
1526                allow_write: false,
1527                sessions: false,
1528                artifacts: artifacts.clone(),
1529                stem: seat_key,
1530                handover: None,
1531            });
1532        }
1533
1534        self.state.event(
1535            "advise",
1536            format!(
1537                "{} advisor seat(s) sketching a design in parallel",
1538                jobs.len()
1539            ),
1540        );
1541        let mut quota_losses = Vec::new();
1542        let cache = self.state.config.cache_dir();
1543        let ctx = WaveCtx {
1544            carry_seats: false,
1545            run: &run_id,
1546            node: "advise",
1547            prompts: &prompts,
1548            cache: cache.as_deref(),
1549            round: None,
1550        };
1551        let advisor_roster = self.state.config.advisor_roster().unwrap_or_default();
1552        let results = ask_json_wave::<Proposal>(
1553            jobs,
1554            Arc::clone(&self.sem),
1555            self.state.config.graph.retries,
1556            &advisor_roster,
1557            &ctx,
1558            &mut quota_losses,
1559            &mut self.state,
1560            &|p: &Proposal| p.validate(),
1561        )
1562        .await;
1563        self.state.quota.extend(quota_losses);
1564
1565        let mut records = Vec::with_capacity(results.len());
1566        for (i, (seat, res, _attempts)) in results.into_iter().enumerate() {
1567            let agent_id = seat.agent.clone();
1568            self.state.seats.insert(seat.key.clone(), seat);
1569            match res {
1570                Ok((proposal, out)) => {
1571                    self.state
1572                        .event("advise", format!("advisor-{} proposed a design", i + 1));
1573                    records.push(advise::AdvisorRecord::proposed(
1574                        i + 1,
1575                        agent_id,
1576                        proposal,
1577                        out.duration_ms,
1578                    ));
1579                }
1580                Err(e) => {
1581                    self.state.event(
1582                        "advise",
1583                        format!("advisor-{} produced no usable proposal: {e:#}", i + 1),
1584                    );
1585                    records.push(advise::AdvisorRecord::failed(
1586                        i + 1,
1587                        agent_id,
1588                        e.to_string(),
1589                    ));
1590                }
1591            }
1592        }
1593
1594        let mut advice = advise::Advice {
1595            records,
1596            synthesis: None,
1597        };
1598        if advice.proposals().is_empty() {
1599            self.state.event(
1600                "advise",
1601                "no advisor produced a usable proposal; continuing without a \
1602                 synthesis brief"
1603                    .to_owned(),
1604            );
1605        } else {
1606            match self
1607                .synthesize_brief(
1608                    &advice,
1609                    &instruction,
1610                    &language,
1611                    &worktrees[0],
1612                    &artifacts,
1613                    &run_id,
1614                    &prompts,
1615                    cache.as_deref(),
1616                )
1617                .await
1618            {
1619                Ok(Some(text)) => {
1620                    self.state.event(
1621                        "advise",
1622                        "synthesized a design brief for the implementer".to_owned(),
1623                    );
1624                    advice.synthesis = Some(text);
1625                }
1626                Ok(None) => {
1627                    self.state.event(
1628                        "advise",
1629                        "the synthesis seat produced nothing usable; continuing \
1630                         without a design brief"
1631                            .to_owned(),
1632                    );
1633                }
1634                Err(e) => {
1635                    self.state.event(
1636                        "advise",
1637                        format!("could not synthesize a design brief: {e:#}"),
1638                    );
1639                }
1640            }
1641        }
1642        advise::apply_reflection(&mut advice);
1643
1644        self.state.advice = Some(advice);
1645        self.state.advise_attempted = true;
1646        self.state.save()?;
1647        Ok(())
1648    }
1649
1650    /// The synthesis seat: reads every advisor's proposal and blends them
1651    /// into the design brief `advise` stores on [`RunState::advice`]. Split
1652    /// out of [`Runner::advise`] only for readability — it is not called
1653    /// anywhere else.
1654    ///
1655    /// Picked the same way [`crate::talk`]'s standing conversation and
1656    /// [`crate::bump`]'s release-bump decision are: [`agent::pick`], with
1657    /// `[roles] synthesizer` checked first and [`agent::pick`]'s own default
1658    /// order (a claude seat, else the first runnable agent in roster order)
1659    /// used when that field is unset — see `[roles] synthesizer`'s own doc
1660    /// in [`crate::config`] for why a dedicated field exists here at all.
1661    #[allow(clippy::too_many_arguments)]
1662    async fn synthesize_brief(
1663        &mut self,
1664        advice: &advise::Advice,
1665        instruction: &str,
1666        language: &str,
1667        cwd: &Path,
1668        artifacts: &Path,
1669        run_id: &str,
1670        prompts: &Prompts,
1671        cache: Option<&Path>,
1672    ) -> Result<Option<String>> {
1673        let chain = agent::pick_chain(
1674            &self.state.config.agents,
1675            self.state.config.roles.synthesizer.as_ref(),
1676            &agent::installed,
1677            "synthesizer",
1678        )?;
1679        let proposals = advice.proposals();
1680        let mut prompt = prompt::with_overlay(
1681            prompt::synthesize_brief(instruction, &proposals, language),
1682            prompts.overlay("advise"),
1683        );
1684        if cache.is_some() {
1685            // This seat never writes, so it is never handed `CARGO_TARGET_DIR`
1686            // below — see `prompt::build_cache_note`'s doc for why telling a
1687            // read-only seat to build through the shared cache is exactly how
1688            // a sandbox's write refusal gets misread as a defect.
1689            prompt.push('\n');
1690            prompt.push_str(&prompt::build_cache_note("advise", false));
1691        }
1692        let timeout = Duration::from_secs(self.state.config.graph.timeout_judge.max(1));
1693        // Each id is tried once, in order; a quota hit, error or unusable
1694        // answer moves to the next. The seat is single-turn (`sessions:
1695        // false`) and the prompt is the whole context, so a fallback agent
1696        // needs nothing carried over.
1697        let mut last = None;
1698        for (n, spec) in chain.iter().enumerate() {
1699            if n > 0 {
1700                self.state
1701                    .event("advise", format!("synthesis falling back to {}", spec.id));
1702            }
1703            let mut seat = self.seat("advise-synthesis", &spec.id);
1704            let outcome = agent::invoke(
1705                spec,
1706                &mut seat,
1707                &Invocation {
1708                    cwd,
1709                    prompt: &prompt,
1710                    timeout,
1711                    allow_write: false,
1712                    unsandboxed: false,
1713                    sessions: false,
1714                    artifacts,
1715                    stem: &if n == 0 {
1716                        "advise-synthesis".to_owned()
1717                    } else {
1718                        format!("advise-synthesis-{}", spec.id)
1719                    },
1720                    run: run_id,
1721                    node: "advise",
1722                    cache_dir: None,
1723                    attachments: &[],
1724                    writable: &[],
1725                },
1726            )
1727            .await;
1728            if outcome.is_ok() {
1729                self.state.seats.insert(seat.key.clone(), seat);
1730            }
1731            let advance = agent::chain_advances(&outcome);
1732            last = Some(outcome);
1733            if !advance {
1734                break;
1735            }
1736        }
1737        // Exhausted: the last attempt's result is what a single failed seat
1738        // would have produced.
1739        let out = last.expect("a chain holds at least one agent")?;
1740        if !out.usable() {
1741            return Ok(None);
1742        }
1743        let text =
1744            verdict::section(&out.text, "synthesis").unwrap_or_else(|| out.text.trim().to_owned());
1745        Ok((!text.trim().is_empty()).then_some(text))
1746    }
1747
1748    // ----------------------------------------------------------- implement
1749
1750    async fn implement(&mut self) -> Result<()> {
1751        // Attribution for every agent this node spawns: `MAGI_RUN` lets a task the
1752        // agent files with `magi task add` name the run that paid for it. The
1753        // prompt overlay is cloned alongside it because the waves borrow it
1754        // while `self` is mutably borrowed by the node's own bookkeeping.
1755        let run_id = self.state.id.clone();
1756        let prompts = self.state.config.prompts.clone();
1757        let todo: Vec<usize> = self
1758            .state
1759            .candidates
1760            .iter()
1761            .enumerate()
1762            .filter(|(_, c)| c.commits == 0 && c.failed.is_none() && !c.empty)
1763            .map(|(i, _)| i)
1764            .collect();
1765        if todo.is_empty() {
1766            return self.after_implement();
1767        }
1768        self.state.status = RunStatus::Implementing;
1769
1770        let language = self.state.config.graph.language.clone();
1771        let timeout = Duration::from_secs(self.state.config.graph.timeout_implement);
1772        let sessions = self.state.config.graph.sessions;
1773        let artifacts = agent::artifacts_dir(&self.state.dir());
1774        // The design-deliberation stage's blended brief, when `advise` found
1775        // one — carried into every implementer's prompt the same way
1776        // regardless of which candidate it is.
1777        let brief = self
1778            .state
1779            .advice
1780            .as_ref()
1781            .and_then(|a| a.synthesis.as_deref())
1782            .map(str::to_owned);
1783        let attachments = self.state.attachments.clone();
1784
1785        let mut jobs = Vec::new();
1786        for &i in &todo {
1787            let (index, label, worktree) = {
1788                let c = &self.state.candidates[i];
1789                (c.index, c.label, c.worktree.clone())
1790            };
1791            let spec = self.roles.implementers[index].clone();
1792            let seat_key = format!("impl-{label}");
1793            let seat = self.seat(&seat_key, &spec.id);
1794            let instruction = seeded_instruction(&self.state);
1795            jobs.push(SeatJob {
1796                spec,
1797                seat,
1798                prompt: prompt::implement(
1799                    &instruction,
1800                    &worktree.to_string_lossy(),
1801                    &language,
1802                    brief.as_deref(),
1803                    &attachments,
1804                ),
1805                cwd: worktree,
1806                timeout,
1807                allow_write: true,
1808                sessions,
1809                artifacts: artifacts.clone(),
1810                stem: format!("impl-{label}"),
1811                handover: None,
1812            });
1813        }
1814
1815        self.state.event(
1816            "implement",
1817            format!("{} candidates in parallel", jobs.len()),
1818        );
1819        // Kept so a seat whose CLI hung up can be asked again from the same
1820        // job: `wave` consumes what it is given. Mutable so `resume_seat_handovers`
1821        // can update a seat's own entry once a fallback agent takes it over —
1822        // `resume_unconfirmed_commands`, which reads `sent` afterward, must see
1823        // whichever agent actually answered, not the one that quota'd out.
1824        let mut sent = jobs.clone();
1825        let cache = self.state.config.cache_dir();
1826        let ctx = WaveCtx {
1827            carry_seats: false,
1828            run: &run_id,
1829            node: "implement",
1830            prompts: &prompts,
1831            cache: cache.as_deref(),
1832            round: None,
1833        };
1834        let mut results = wave(jobs, Arc::clone(&self.sem), &ctx, &mut self.state, 0).await;
1835        self.resume_undelivered(&mut results, &sent, &prompts, &run_id)
1836            .await;
1837        self.resume_seat_handovers(&mut results, &mut sent, &prompts, &run_id)
1838            .await;
1839        self.resume_unconfirmed_commands(&mut results, &sent, &prompts, &run_id)
1840            .await;
1841
1842        for (&i, (_wi, seat, out)) in todo.iter().zip(results) {
1843            let seat_key = seat.key.clone();
1844            // A quota fallback (`resume_seat_handovers`) may have handed this
1845            // seat to a different agent than the one `prep` recorded on the
1846            // candidate; the stats tables and any later fixer-defaults-to-
1847            // winner's-author lookup must credit whoever actually answered —
1848            // unless every fallback also quota'd out, in which case nobody
1849            // actually answered and crediting the last agent tried would
1850            // erase every earlier agent's own quota loss from the stats
1851            // tables instead of just this one seat's.
1852            let agent = seat.agent.clone();
1853            let exhausted_the_fallback_chain = FailClass::of(&out).is_some();
1854            self.state.seats.insert(seat.key.clone(), seat);
1855            let label = self.state.candidates[i].label;
1856            let worktree = self.state.candidates[i].worktree.clone();
1857            let base = self.state.base_commit.clone();
1858
1859            let (summary, duration, failed, verified_claim) = match out {
1860                AgentOutcome::Ok(o) => {
1861                    let text = verdict::section(&o.text, "summary").unwrap_or(o.text.clone());
1862                    let failed = (!o.usable()).then(|| {
1863                        if o.timed_out {
1864                            "agent timed out".to_owned()
1865                        } else {
1866                            format!("agent exited with {:?}", o.exit_code)
1867                        }
1868                    });
1869                    let verified_claim = verified_noop_claim(failed.is_none(), &o.commands, &text);
1870                    (text, o.duration_ms, failed, verified_claim)
1871                }
1872                // Left un-resumed by `resume_undelivered` (a dirty tree
1873                // already rescues the work, or there was no session left to
1874                // resume into) — reported like the ordinary failure it is,
1875                // never as if `o.text` (the CLI's raw error JSON) were an
1876                // answer.
1877                AgentOutcome::Dropped(o) => {
1878                    let why = o
1879                        .dropped
1880                        .as_ref()
1881                        .map(|d| d.why.as_str())
1882                        .unwrap_or("the CLI ended the stream without delivering its answer");
1883                    (
1884                        String::new(),
1885                        o.duration_ms,
1886                        Some(format!("the CLI dropped the stream ({why})")),
1887                        None,
1888                    )
1889                }
1890                AgentOutcome::Quota(o) => {
1891                    self.state.quota.push(QuotaLoss {
1892                        seat: seat_key,
1893                        node: "implement".to_owned(),
1894                        at: Timestamp::now(),
1895                        reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
1896                    });
1897                    (
1898                        String::new(),
1899                        o.duration_ms,
1900                        Some("rate limited (quota); produced no change".to_owned()),
1901                        None,
1902                    )
1903                }
1904                AgentOutcome::Failed(e) => (String::new(), 0, Some(e), None),
1905            };
1906
1907            // Rescue anything the agent edited but never committed: an
1908            // uncommitted candidate would silently be an empty one.
1909            let rescued = match git::rescue_commit(
1910                &worktree,
1911                &format!("magi: candidate {label} (uncommitted work)"),
1912            )
1913            .await
1914            {
1915                Ok(r) => {
1916                    self.state.note_withheld("implement", &r.withheld);
1917                    r.committed
1918                }
1919                Err(_) => false,
1920            };
1921            let commits = git::commits_ahead(&worktree, &base, "HEAD")
1922                .await
1923                .unwrap_or(0);
1924            let patch = git::diff(&worktree, &base, "HEAD")
1925                .await
1926                .unwrap_or_default();
1927            let stat = git::diff_stat(&worktree, &base, "HEAD")
1928                .await
1929                .unwrap_or_default();
1930            let files = git::changed_files(&worktree, &base, "HEAD")
1931                .await
1932                .map(|f| f.len())
1933                .unwrap_or(0);
1934            write_artifact(&self.state, &format!("cand-{label}.patch"), &patch)?;
1935
1936            let c = &mut self.state.candidates[i];
1937            if !exhausted_the_fallback_chain {
1938                c.agent = agent;
1939            }
1940            c.summary = blind::sanitize_prose(&summary, &self.state.config.blind);
1941            c.stat = stat;
1942            c.files = files;
1943            c.commits = commits;
1944            c.duration_ms = duration;
1945            c.empty = commits == 0 || patch.trim().is_empty();
1946            // An agent that failed but still produced a committed change stays
1947            // in the running: the patch is what gets judged, not the exit code.
1948            c.failed = match failed {
1949                Some(_) if c.empty => failed,
1950                _ => None,
1951            };
1952            // Only an empty candidate can be a verified no-op: a claim next
1953            // to a real patch is not what the marker is for, and `c.failed`
1954            // being `Some` here already implies `verified_claim` was never
1955            // set (see the guard above the match that produced it).
1956            c.verified_noop = if c.empty { verified_claim } else { None };
1957            let note = match (&c.failed, c.empty, &c.verified_noop, rescued) {
1958                (Some(e), _, _, _) => format!("candidate {label}: {e}"),
1959                (None, true, Some(_), _) => {
1960                    format!("candidate {label}: no change produced (agent-verified no-op)")
1961                }
1962                (None, true, None, _) => format!("candidate {label}: no change produced"),
1963                (None, false, _, true) => {
1964                    format!(
1965                        "candidate {label}: {files} files, {commits} commits (rescued an uncommitted tree)"
1966                    )
1967                }
1968                (None, false, _, false) => {
1969                    format!("candidate {label}: {files} files, {commits} commits")
1970                }
1971            };
1972            self.state.event("implement", note);
1973            self.state.save()?;
1974        }
1975
1976        self.after_implement()
1977    }
1978
1979    /// Ask again, once, for work a CLI did and then failed to hand over.
1980    ///
1981    /// [`agent::dropped_stream`] recognises the one shape observed: an error
1982    /// status with an empty response and a usage report showing output tokens,
1983    /// i.e. **billed work with nothing delivered**. Run 26c7's candidate B was
1984    /// seven minutes and 14,267 output tokens that arrived as an empty
1985    /// candidate, because `agy`'s own subscriber fell behind and hung up.
1986    ///
1987    /// Two conditions, and both matter:
1988    ///
1989    /// - **Only when the tree is untouched.** Often the agent has already
1990    ///   written its files and only the closing message was lost; the rescue
1991    ///   commit below picks that up and there is nothing to ask for. Re-asking
1992    ///   then would pay for a second implementation of work already on disk.
1993    /// - **Once.** A CLI that drops one stream can drop the next, and this
1994    ///   node is the most expensive in the graph.
1995    ///
1996    /// The re-ask is a resume, not a re-run: `has_context` is true because the
1997    /// dropped reply still carried its `conversation_id`, so the seat is asked
1998    /// to finish what it was doing rather than sent the whole task again. It
1999    /// therefore gets a nudge's budget ([`retry_budget`]) - a quarter of the
2000    /// node's - for the same reason a re-ranked judge does: restating finished
2001    /// work is not the work.
2002    ///
2003    /// Unlike a quota this is worth retrying at all: a rate limit fails the
2004    /// same way until it resets, while an abandoned conversation is still
2005    /// there to be picked up.
2006    async fn resume_undelivered(
2007        &mut self,
2008        results: &mut [(usize, SeatState, AgentOutcome)],
2009        sent: &[SeatJob],
2010        prompts: &Prompts,
2011        run_id: &str,
2012    ) {
2013        for (wi, seat, out) in results.iter_mut() {
2014            let Some(dropped) = (match &*out {
2015                AgentOutcome::Dropped(o) => o.dropped.clone(),
2016                _ => None,
2017            }) else {
2018                continue;
2019            };
2020            let Some(job) = sent.get(*wi) else { continue };
2021            // Already on disk? Then only the closing message was lost.
2022            if !git::is_clean(&job.cwd).await.unwrap_or(true) {
2023                self.state.event(
2024                    "implement",
2025                    format!(
2026                        "{}: the CLI dropped the stream after {} output tokens ({}), but the \
2027                         work is in the tree",
2028                        seat.key, dropped.output_tokens, dropped.why
2029                    ),
2030                );
2031                continue;
2032            }
2033            // The re-ask only makes sense as a resume: `resume_after_drop`
2034            // says nothing about the task, trusting the seat to still hold it.
2035            // Without a session to resume — sessions disabled, or this CLI's
2036            // drop shape happened not to carry a session id — that prompt
2037            // would open a brand-new conversation with no context at all,
2038            // which is worse than leaving this as the ordinary failure it
2039            // already is.
2040            if !has_context(&job.spec, seat, job.sessions) {
2041                self.state.event(
2042                    "implement",
2043                    format!(
2044                        "{}: the CLI dropped the stream after {} output tokens ({}), but there \
2045                         is no session left to resume",
2046                        seat.key, dropped.output_tokens, dropped.why
2047                    ),
2048                );
2049                continue;
2050            }
2051            self.state.event(
2052                "implement",
2053                format!(
2054                    "{}: the CLI dropped the stream after {} output tokens ({}); resuming the \
2055                     conversation",
2056                    seat.key, dropped.output_tokens, dropped.why
2057                ),
2058            );
2059            let mut retry = job.clone();
2060            retry.seat = seat.clone();
2061            retry.prompt = prompt::resume_after_drop(&dropped.why);
2062            retry.timeout = retry_budget(job.timeout, true);
2063            retry.stem = format!("{}-resume", job.stem);
2064            let cache = self.state.config.cache_dir();
2065            let ctx = WaveCtx {
2066                carry_seats: false,
2067                run: run_id,
2068                node: "implement",
2069                prompts,
2070                cache: cache.as_deref(),
2071                round: None,
2072            };
2073            let (resumed_seat, resumed) =
2074                run_one(retry, Arc::clone(&self.sem), &ctx, &mut self.state, 1).await;
2075            *seat = resumed_seat;
2076            *out = resumed;
2077        }
2078    }
2079
2080    /// Fall an implement seat through to the next untried agent in the
2081    /// implementer roster when it lost to quota — or, since the handover was
2082    /// generalised, to a timeout or an ordinary failure (see [`FailClass`] and
2083    /// [`should_hand_over`] for when a non-quota failure stops the chain), the
2084    /// quota path itself being unchanged — instead of leaving the
2085    /// seat's loss final the moment one agent's account runs dry.
2086    ///
2087    /// Solo runs (`graph.implementers = 1`, `daemon::apply_solo`'s forced shape)
2088    /// are the motivating case: `Config::resolve_roles`'s `implementers`
2089    /// truncates to the single slot rotation picked, so a solo task whose one
2090    /// implementer hits quota mid-run used to have nothing else to try. This
2091    /// walks [`ResolvedRoles::implementer_roster`] instead — the untruncated,
2092    /// unrotated roster — which is the only place the *other* candidates in
2093    /// the machine's roster still exist once `implementers` has been cut down
2094    /// to size.
2095    ///
2096    /// Walks forward from just past the seat's own original position in the
2097    /// roster, never wrapping back to the front: a later candidate slot (say
2098    /// `beta`, the roster's second entry) must fall through to the *next*
2099    /// entry (`gamma`) on its own quota loss, not back to `alpha`, which is
2100    /// almost certainly a different candidate's own agent already — and once
2101    /// the roster's tail is exhausted there is nothing left to fall through
2102    /// to for *this* seat, wrapping or not. Tried by `spec.id`, never the
2103    /// whole [`AgentSpec`]: a roster with the same id named twice must not
2104    /// let this retry that id forever. The loop keeps falling through until
2105    /// an attempt lands something other than `Quota` or the roster's tail
2106    /// runs out of untried ids, at which point the seat is left exactly as
2107    /// `implement`'s own `AgentOutcome::Quota` arm already handles it: one
2108    /// `QuotaLoss` recorded, the candidate failed/empty.
2109    ///
2110    /// `sent` is taken mutably and updated with the fallback agent's spec:
2111    /// `resume_unconfirmed_commands`, which runs after this and also reads
2112    /// `sent`, must see whichever agent actually ended up answering the seat
2113    /// — reading the stale, original spec there would check session
2114    /// eligibility against the wrong CLI and could hand a fallback agent's
2115    /// session id to the agent that just lost the seat to quota.
2116    ///
2117    /// Every fallback gets a fresh [`SeatState`], never the quota'd seat's own
2118    /// — `self.seat` only reuses state when the agent id is unchanged, so
2119    /// handing it a different id already gets this for free. Reusing the old
2120    /// seat would resume a different CLI's session as if it were a
2121    /// continuation of this one.
2122    ///
2123    /// Unlike [`Runner::resume_undelivered`], not gated on a clean worktree:
2124    /// a quota loss cuts an agent off mid-turn, so anything already in the
2125    /// tree is unfinished work, not a completed candidate a re-ask would pay
2126    /// for twice. A dirty tree is rescued into a commit first (the same
2127    /// neutral-identity rescue `implement`'s own outcome loop gives every
2128    /// candidate) so the next agent starts clean.
2129    ///
2130    /// The new agent gets the implementer's full prompt and full
2131    /// `timeout_implement` budget, not `resume_after_drop`'s nudge-sized one:
2132    /// it has no session and no context, and is implementing the task from
2133    /// nothing, unlike a resumed drop which is only restating work already
2134    /// done.
2135    ///
2136    /// Every intermediate `Quota` this loop absorbs is folded into a plain
2137    /// `implement` event, never into `self.state.quota` — that is what
2138    /// `daemon.rs`'s own backoff reads to decide a run's task attempt should
2139    /// go unspent, and a seat that ultimately recovered on its second or
2140    /// third agent is not the stalled panel that check exists to catch. Only
2141    /// the final, unrecovered `Quota` (once the roster runs out) ever reaches
2142    /// `self.state.quota`, via the ordinary `AgentOutcome::Quota` arm the
2143    /// outcome loop already has — this helper never pushes to it itself.
2144    async fn resume_seat_handovers(
2145        &mut self,
2146        results: &mut [(usize, SeatState, AgentOutcome)],
2147        sent: &mut [SeatJob],
2148        prompts: &Prompts,
2149        run_id: &str,
2150    ) {
2151        let instruction = seeded_instruction(&self.state);
2152        let language = self.state.config.graph.language.clone();
2153        let brief = self
2154            .state
2155            .advice
2156            .as_ref()
2157            .and_then(|a| a.synthesis.as_deref())
2158            .map(str::to_owned);
2159        let attachments = self.state.attachments.clone();
2160        for (wi, seat, out) in results.iter_mut() {
2161            // Who holds the other candidate seats of this wave right now
2162            // (earlier handovers already written back to `sent`).
2163            let others: BTreeSet<String> = sent
2164                .iter()
2165                .enumerate()
2166                .filter(|(j, _)| j != wi)
2167                .map(|(_, j)| j.spec.id.clone())
2168                .collect();
2169            let Some(job) = sent.get_mut(*wi) else {
2170                continue;
2171            };
2172            // Where the seat's own original agent sits in the roster — the
2173            // fallback walk starts just past here, never at the front, so a
2174            // later candidate slot's quota loss does not fall back onto an
2175            // earlier slot's own agent.
2176            let start = self
2177                .roles
2178                .implementer_roster
2179                .iter()
2180                .position(|s| s.id == job.spec.id)
2181                .unwrap_or(0);
2182            let mut tried: BTreeSet<String> = BTreeSet::from([job.spec.id.clone()]);
2183            let mut fallback_attempt = 0usize;
2184            let mut prev: Option<FailClass> = None;
2185            while let Some(cur) = FailClass::of(&*out) {
2186                if !should_hand_over(prev.as_ref(), &cur) {
2187                    break;
2188                }
2189                let Some(next) =
2190                    pick_successor(&self.roles.implementer_roster, start, &tried, None, &others)
2191                        .cloned()
2192                else {
2193                    break;
2194                };
2195                tried.insert(next.id.clone());
2196                fallback_attempt += 1;
2197
2198                if let Ok(r) = git::rescue_commit(
2199                    &job.cwd,
2200                    &format!(
2201                        "magi: candidate {} (uncommitted work before {} fallback)",
2202                        seat.key,
2203                        if cur == FailClass::Quota {
2204                            "quota"
2205                        } else {
2206                            "handover"
2207                        }
2208                    ),
2209                )
2210                .await
2211                {
2212                    self.state.note_withheld("implement", &r.withheld);
2213                }
2214
2215                record_handover(
2216                    &mut self.state,
2217                    "implement",
2218                    &seat.key,
2219                    &seat.agent,
2220                    &next.id,
2221                    &cur,
2222                    &fail_reason(&*out),
2223                );
2224                prev = Some(cur.clone());
2225
2226                let new_seat = handover_seat(&seat.key, &next.id, self.state.next_seat_seed());
2227                self.state.seats.insert(seat.key.clone(), new_seat.clone());
2228                // Kept in sync on `sent` itself, not just the local retry: a
2229                // later helper (`resume_unconfirmed_commands`) reads `sent`
2230                // after this one returns and must see whichever agent is now
2231                // occupying the seat, not the one that just quota'd out —
2232                // otherwise it would judge session/continuation eligibility
2233                // by the wrong CLI and could resend a fallback's session id
2234                // to the agent that lost it the seat in the first place.
2235                job.spec = next.clone();
2236                let mut retry = job.clone();
2237                retry.seat = new_seat;
2238                retry.prompt = prompt::implement(
2239                    &instruction,
2240                    &job.cwd.to_string_lossy(),
2241                    &language,
2242                    brief.as_deref(),
2243                    &attachments,
2244                );
2245                retry.stem = format!("{}-{}-{}", job.stem, cur.stem_word(), next.id);
2246                let cache = self.state.config.cache_dir();
2247                let ctx = WaveCtx {
2248                    carry_seats: false,
2249                    run: run_id,
2250                    node: "implement",
2251                    prompts,
2252                    cache: cache.as_deref(),
2253                    round: None,
2254                };
2255                let (fallback_seat, fallback_out) = run_one(
2256                    retry,
2257                    Arc::clone(&self.sem),
2258                    &ctx,
2259                    &mut self.state,
2260                    fallback_attempt,
2261                )
2262                .await;
2263                *seat = fallback_seat;
2264                *out = fallback_out;
2265            }
2266        }
2267    }
2268
2269    /// Ask an implement seat's own CLI to confirm what it started, once, when
2270    /// its reply reported a command whose completion status it never
2271    /// confirmed — see [`has_unconfirmed_command`]'s own doc for exactly what
2272    /// that does and does not mean.
2273    ///
2274    /// The completion contract this task asks for, extended to `implement`
2275    /// with the same signal `continue_fix_report` reads for the fixer,
2276    /// rather than a keyword search over the reply or a hard requirement on
2277    /// `## SUMMARY`'s presence — the shape behind fb35, 9566 and e185, where
2278    /// a candidate's CLI turn ended cleanly while a test run it had started
2279    /// had not. A short, ordinary reply with no `## SUMMARY` and no commands
2280    /// named in it at all is untouched by this: `commands` is empty, so
2281    /// there is nothing to be unconfirmed.
2282    ///
2283    /// Unlike `resume_undelivered`, not gated on the tree being untouched:
2284    /// this is not about recovering edits that might already be on disk, it
2285    /// is about a result the seat itself never vouched for, which resuming
2286    /// asks for regardless of what the tree already holds. Bounded to one
2287    /// attempt for the same reason `resume_undelivered` is — this is the
2288    /// most expensive node in the graph — and a seat that still cannot
2289    /// confirm on that attempt is left as whatever its (possibly still
2290    /// unconfirmed) reply says; this does not invent a new "failed" reason
2291    /// for a candidate that otherwise produced a real, committed change.
2292    async fn resume_unconfirmed_commands(
2293        &mut self,
2294        results: &mut [(usize, SeatState, AgentOutcome)],
2295        sent: &[SeatJob],
2296        prompts: &Prompts,
2297        run_id: &str,
2298    ) {
2299        for (wi, seat, out) in results.iter_mut() {
2300            let AgentOutcome::Ok(o) = &*out else {
2301                continue;
2302            };
2303            if !has_unconfirmed_command(&o.commands) {
2304                continue;
2305            }
2306            let Some(job) = sent.get(*wi) else { continue };
2307            if !has_context(&job.spec, seat, job.sessions) {
2308                self.state.event(
2309                    "implement",
2310                    format!(
2311                        "{}: the reply named a command whose own CLI never confirmed the exit \
2312                         status of, but there is no session left to resume",
2313                        seat.key
2314                    ),
2315                );
2316                continue;
2317            }
2318            self.state.event(
2319                "implement",
2320                format!(
2321                    "{}: the reply named a command whose own CLI never confirmed the exit \
2322                     status of; resuming the conversation",
2323                    seat.key
2324                ),
2325            );
2326            let mut retry = job.clone();
2327            retry.seat = seat.clone();
2328            retry.prompt = prompt::resume_incomplete(
2329                "a command in your last reply had no confirmed exit status",
2330            );
2331            retry.timeout = retry_budget(job.timeout, true);
2332            retry.stem = format!("{}-confirm", job.stem);
2333            let cache = self.state.config.cache_dir();
2334            let ctx = WaveCtx {
2335                carry_seats: false,
2336                run: run_id,
2337                node: "implement",
2338                prompts,
2339                cache: cache.as_deref(),
2340                round: None,
2341            };
2342            let (resumed_seat, resumed) =
2343                run_one(retry, Arc::clone(&self.sem), &ctx, &mut self.state, 1).await;
2344            *seat = resumed_seat;
2345            *out = resumed;
2346        }
2347    }
2348
2349    /// Ask the fixer's own seat again, up to [`MAX_FIX_CONTINUATIONS`] times,
2350    /// when its CLI turn ended cleanly (`AgentOutcome::Ok`) but the reply held
2351    /// no [`FixReport`] — see [`MAX_FIX_CONTINUATIONS`]'s own doc for the run
2352    /// that motivated this.
2353    ///
2354    /// Not the same gap as an unparsable *shape*, which [`ask_json_wave`]'s
2355    /// own nudge loop already covers for judge/review/vote seats, and not a
2356    /// dropped stream, which [`Runner::resume_undelivered`] covers for
2357    /// implement seats: here the CLI turn genuinely finished while the node's
2358    /// own work — the fixer's account of what it did — had not. Gated purely
2359    /// on `extract_json::<FixReport>` having failed on an otherwise-usable
2360    /// reply, never on any wording in it, so a fixer whose valid, first-try
2361    /// `FixReport` happens to mention having waited on a background test is
2362    /// never resumed — the `Ok(report)` branch at the call site returns
2363    /// before this is ever invoked.
2364    ///
2365    /// Same discipline as `resume_undelivered`: a nudge-sized timeout per
2366    /// attempt ([`retry_budget`]), nothing attempted once the session is
2367    /// gone, and a quota hit ends the loop immediately rather than retrying a
2368    /// rate limit that fails the same way again.
2369    async fn continue_fix_report(
2370        &mut self,
2371        mut seat: SeatState,
2372        parse_err: String,
2373        job: &SeatJob,
2374        prompts: &Prompts,
2375        run_id: &str,
2376        round: usize,
2377    ) -> (
2378        SeatState,
2379        Option<FixReport>,
2380        Option<String>,
2381        ContinuationRecord,
2382    ) {
2383        let mut last_err = parse_err;
2384        let mut cumulative_wait_ms = 0u64;
2385        let mut attempts = 0usize;
2386        loop {
2387            if !has_context(&job.spec, &seat, job.sessions) {
2388                self.state.event(
2389                    "fix",
2390                    format!(
2391                        "round {round}: fixer's reply had no adoption report ({last_err}); no \
2392                         session left to resume into"
2393                    ),
2394                );
2395                let outcome = if attempts == 0 {
2396                    ContinuationOutcome::NoSession
2397                } else {
2398                    ContinuationOutcome::Exhausted
2399                };
2400                return (
2401                    seat,
2402                    None,
2403                    Some(format!("unparsable fix report: {last_err}")),
2404                    ContinuationRecord {
2405                        attempts,
2406                        cumulative_wait_ms,
2407                        outcome,
2408                    },
2409                );
2410            }
2411            if attempts >= MAX_FIX_CONTINUATIONS {
2412                self.state.event(
2413                    "fix",
2414                    format!(
2415                        "round {round}: fixer's reply still had no adoption report after \
2416                         {attempts} continuation(s) ({last_err}); giving up"
2417                    ),
2418                );
2419                return (
2420                    seat,
2421                    None,
2422                    Some(format!(
2423                        "unparsable fix report after {attempts} continuation(s): {last_err}"
2424                    )),
2425                    ContinuationRecord {
2426                        attempts,
2427                        cumulative_wait_ms,
2428                        outcome: ContinuationOutcome::Exhausted,
2429                    },
2430                );
2431            }
2432            attempts += 1;
2433            self.state.event(
2434                "fix",
2435                format!(
2436                    "round {round}: fixer's reply had no adoption report ({last_err}); resuming \
2437                     the conversation (attempt {attempts}/{MAX_FIX_CONTINUATIONS})"
2438                ),
2439            );
2440            let mut retry = job.clone();
2441            retry.seat = seat.clone();
2442            retry.prompt = prompt::resume_incomplete(&last_err);
2443            retry.timeout = retry_budget(job.timeout, true);
2444            retry.stem = format!("{}-continue{attempts}", job.stem);
2445            let cache = self.state.config.cache_dir();
2446            let ctx = WaveCtx {
2447                carry_seats: false,
2448                run: run_id,
2449                node: "fix",
2450                prompts,
2451                cache: cache.as_deref(),
2452                round: Some(round),
2453            };
2454            let (resumed_seat, resumed_out) = run_one(
2455                retry,
2456                Arc::clone(&self.sem),
2457                &ctx,
2458                &mut self.state,
2459                attempts,
2460            )
2461            .await;
2462            seat = resumed_seat;
2463            match resumed_out {
2464                AgentOutcome::Ok(o) => {
2465                    cumulative_wait_ms += o.duration_ms;
2466                    match verdict::extract_json::<FixReport>(&o.text) {
2467                        Ok(report) if !has_unconfirmed_command(&o.commands) => {
2468                            self.state.event(
2469                                "fix",
2470                                format!(
2471                                    "round {round}: fixer's adoption report recovered after \
2472                                     {attempts} continuation(s)"
2473                                ),
2474                            );
2475                            return (
2476                                seat,
2477                                Some(report),
2478                                None,
2479                                ContinuationRecord {
2480                                    attempts,
2481                                    cumulative_wait_ms,
2482                                    outcome: ContinuationOutcome::Resumed,
2483                                },
2484                            );
2485                        }
2486                        // The report parsed, but this same reply's own
2487                        // CommandEvidence — the identical record `state.jobs`
2488                        // renders — names a command whose CLI never
2489                        // confirmed an exit status. Read together, that is
2490                        // not a resolved answer: keep nudging rather than
2491                        // accept a report standing next to a command the
2492                        // seat's own CLI cannot vouch for.
2493                        Ok(_) => {
2494                            last_err = "the reply parsed, but it reported a command whose own CLI \
2495                                 never confirmed an exit status"
2496                                .to_owned();
2497                        }
2498                        Err(e) => last_err = e.to_string(),
2499                    }
2500                }
2501                AgentOutcome::Quota(o) => {
2502                    cumulative_wait_ms += o.duration_ms;
2503                    self.state.quota.push(QuotaLoss {
2504                        seat: seat.key.clone(),
2505                        node: "fix".to_owned(),
2506                        at: Timestamp::now(),
2507                        reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
2508                    });
2509                    self.state.event(
2510                        "fix",
2511                        format!(
2512                            "round {round}: continuation rate limited (quota); not retrying now"
2513                        ),
2514                    );
2515                    return (
2516                        seat,
2517                        None,
2518                        Some("rate limited (quota) while recovering the fix report".to_owned()),
2519                        ContinuationRecord {
2520                            attempts,
2521                            cumulative_wait_ms,
2522                            outcome: ContinuationOutcome::QuotaLost,
2523                        },
2524                    );
2525                }
2526                AgentOutcome::Dropped(o) => {
2527                    cumulative_wait_ms += o.duration_ms;
2528                    let why = o
2529                        .dropped
2530                        .as_ref()
2531                        .map(|d| d.why.as_str())
2532                        .unwrap_or("the CLI ended the stream without delivering its answer");
2533                    last_err = format!("the CLI dropped the stream ({why})");
2534                }
2535                AgentOutcome::Failed(e) => last_err = e,
2536            }
2537        }
2538    }
2539
2540    fn after_implement(&mut self) -> Result<()> {
2541        // Scan every candidate patch once the set is complete.
2542        if self.state.leaks.is_empty() {
2543            let cfg = self.state.config.blind.clone();
2544            let mut leaks = Vec::new();
2545            for c in &self.state.candidates {
2546                let Some(patch) =
2547                    crate::run::read_artifact(&self.state, &format!("cand-{}.patch", c.label))
2548                else {
2549                    continue;
2550                };
2551                leaks.extend(blind::scan(
2552                    &format!("candidate {} patch", c.label),
2553                    &patch,
2554                    &cfg.vendor_tokens,
2555                ));
2556            }
2557            if !leaks.is_empty() {
2558                let summary = leaks
2559                    .iter()
2560                    .map(|l| format!("{}×{} in {}", l.token, l.count, l.site))
2561                    .collect::<Vec<_>>()
2562                    .join(", ");
2563                match cfg.on_leak {
2564                    LeakPolicy::Fail => {
2565                        self.state.status = RunStatus::Failed;
2566                        self.state
2567                            .event("blind", format!("vendor text in a patch: {summary}"));
2568                        self.state.leaks = leaks;
2569                        self.state.save()?;
2570                        self.settle_questions();
2571                        bail!(
2572                            "blind.on_leak = \"fail\" and vendor text reached a \
2573                             judged patch: {summary}"
2574                        );
2575                    }
2576                    LeakPolicy::Redact => self.state.event(
2577                        "blind",
2578                        format!("redacting vendor text for judging: {summary}"),
2579                    ),
2580                    LeakPolicy::Warn => self.state.event(
2581                        "blind",
2582                        format!("vendor text present in a judged patch (shown as-is): {summary}"),
2583                    ),
2584                }
2585                self.state.leaks = leaks;
2586            }
2587        }
2588
2589        if self.state.viable().is_empty() {
2590            if self.state.all_candidates_verified_noop() {
2591                // Every candidate agreed, with evidence the adoption guard
2592                // accepted, that nothing belongs in this worktree. That is
2593                // not the same fact as a candidate that simply failed to
2594                // write anything, and settling it as an ordinary `Failed`
2595                // (see `SCHEMA`'s doc for schema 10) is what let two of
2596                // task 391f's attempts burn a retry each re-discovering the
2597                // same already-landed fix. Terminal either way, so `judge`
2598                // must never run over an empty candidate set — unlike the
2599                // `Failed` branch below this returns `Ok`, not an error:
2600                // nothing here failed.
2601                self.state.status = RunStatus::VerifiedNoop;
2602                self.state.save()?;
2603                self.settle_questions();
2604                return Ok(());
2605            }
2606            self.state.status = RunStatus::Failed;
2607            self.state.save()?;
2608            self.settle_questions();
2609            bail!("no candidate produced a change; nothing to judge");
2610        }
2611        self.state.status = RunStatus::Judging;
2612        self.state.save()?;
2613        Ok(())
2614    }
2615
2616    // --------------------------------------------------------------- judge
2617
2618    async fn judge(&mut self) -> Result<()> {
2619        // Attribution for every agent this node spawns: `MAGI_RUN` lets a task the
2620        // agent files with `magi task add` name the run that paid for it. The
2621        // prompt overlay is cloned alongside it because the waves borrow it
2622        // while `self` is mutably borrowed by the node's own bookkeeping.
2623        let run_id = self.state.id.clone();
2624        let prompts = self.state.config.prompts.clone();
2625        if !self.state.judgements.is_empty() || self.state.judge_skipped {
2626            return Ok(());
2627        }
2628        let viable: Vec<Candidate> = self.state.viable().into_iter().cloned().collect();
2629        if viable.len() == 1 {
2630            // Recorded so this is a one-time event: `judgements` stays empty
2631            // either way, which without this flag is indistinguishable from
2632            // "not yet judged" on the next reentry — and status is left
2633            // untouched, so a later node's conclusion (e.g. `Blocked` after
2634            // the review budget ran out) survives a resume instead of being
2635            // clobbered back to `Judging` by this node running again.
2636            self.state.judge_skipped = true;
2637            self.state.event(
2638                "judge",
2639                format!(
2640                    "only candidate {} produced a change; judging skipped",
2641                    viable[0].label
2642                ),
2643            );
2644            self.state.save()?;
2645            return Ok(());
2646        }
2647        self.state.status = RunStatus::Judging;
2648
2649        let labels: Vec<char> = viable.iter().map(|c| c.label).collect();
2650        let language = self.state.config.graph.language.clone();
2651        let timeout = Duration::from_secs(self.state.config.graph.timeout_judge);
2652        let sessions = self.state.config.graph.sessions;
2653        let artifacts = agent::artifacts_dir(&self.state.dir());
2654        let root = self.state.worktree_root();
2655        let base_short = short(&self.state.base_commit);
2656
2657        let mut jobs = Vec::new();
2658        let mut orders = Vec::new();
2659        for (j, spec) in self.roles.judges.clone().into_iter().enumerate() {
2660            let order = blind::presentation_order(viable.len(), j, self.state.seed);
2661            let views: Vec<CandidateView> = order.iter().map(|&k| self.view(&viable[k])).collect();
2662            orders.push(order.iter().map(|&k| viable[k].index).collect::<Vec<_>>());
2663            let seat_key = format!("judge-{}", j + 1);
2664            let seat = self.seat(&seat_key, &spec.id);
2665            jobs.push(SeatJob {
2666                prompt: prompt::judge(
2667                    &self.state.instruction,
2668                    &views,
2669                    self.roles.judges.len(),
2670                    &base_short,
2671                    &language,
2672                ),
2673                spec,
2674                seat,
2675                cwd: root.join(format!("judge-{}", j + 1)),
2676                timeout,
2677                allow_write: false,
2678                sessions,
2679                artifacts: artifacts.clone(),
2680                stem: format!("judge-{}", j + 1),
2681                handover: None,
2682            });
2683        }
2684
2685        self.state.event(
2686            "judge",
2687            format!(
2688                "{} judges ranking {} candidates blind",
2689                jobs.len(),
2690                viable.len()
2691            ),
2692        );
2693        let labels_for_check = labels.clone();
2694        let mut quota_losses = Vec::new();
2695        let cache = self.state.config.cache_dir();
2696        let ctx = WaveCtx {
2697            carry_seats: false,
2698            run: &run_id,
2699            node: "judge",
2700            prompts: &prompts,
2701            cache: cache.as_deref(),
2702            round: None,
2703        };
2704        let results = ask_json_wave::<Ranking>(
2705            jobs,
2706            Arc::clone(&self.sem),
2707            self.state.config.graph.retries,
2708            &self.roles.judge_roster,
2709            &ctx,
2710            &mut quota_losses,
2711            &mut self.state,
2712            &move |r: &Ranking| r.validate(&labels_for_check),
2713        )
2714        .await;
2715        self.state.quota.extend(quota_losses);
2716
2717        for (j, (seat, res, _attempts)) in results.into_iter().enumerate() {
2718            let agent_id = seat.agent.clone();
2719            self.state.seats.insert(seat.key.clone(), seat);
2720            let mut record = Judgement {
2721                judge: j + 1,
2722                seat: format!("judge-{}", j + 1),
2723                agent: agent_id,
2724                ranking: Vec::new(),
2725                reasons: BTreeMap::new(),
2726                confidence: None,
2727                strengths: BTreeMap::new(),
2728                order: orders[j].clone(),
2729                failed: None,
2730                duration_ms: 0,
2731            };
2732            match res {
2733                Ok((ranking, out)) => {
2734                    record.ranking = ranking.normalized();
2735                    record.reasons = ranking.reasons;
2736                    record.strengths = ranking.strengths;
2737                    record.confidence = ranking.confidence;
2738                    record.duration_ms = out.duration_ms;
2739                    self.state.event(
2740                        "judge",
2741                        format!(
2742                            "judge {} ranked {}",
2743                            j + 1,
2744                            record.ranking.iter().collect::<String>()
2745                        ),
2746                    );
2747                }
2748                Err(e) => {
2749                    record.failed = Some(e.to_string());
2750                    self.state
2751                        .event("judge", format!("judge {} produced no ranking: {e}", j + 1));
2752                }
2753            }
2754            self.state.judgements.push(record);
2755            self.state.save()?;
2756        }
2757        Ok(())
2758    }
2759
2760    // ---------------------------------------------------------- deliberate
2761
2762    async fn deliberate(&mut self) -> Result<()> {
2763        // Attribution for every agent this node spawns: `MAGI_RUN` lets a task the
2764        // agent files with `magi task add` name the run that paid for it. The
2765        // prompt overlay is cloned alongside it because the waves borrow it
2766        // while `self` is mutably borrowed by the node's own bookkeeping.
2767        let run_id = self.state.id.clone();
2768        let prompts = self.state.config.prompts.clone();
2769        if !self.state.deliberation.is_empty() {
2770            return Ok(());
2771        }
2772        let tops: Vec<char> = self
2773            .state
2774            .judgements
2775            .iter()
2776            .filter_map(|j| j.ranking.first().copied())
2777            .collect();
2778        let rounds = self.state.config.graph.deliberate_rounds;
2779        if tops.len() < 2 || tops.iter().all(|t| *t == tops[0]) || rounds == 0 {
2780            if tops.len() >= 2 && tops.iter().all(|t| *t == tops[0]) {
2781                self.state.event(
2782                    "deliberate",
2783                    format!("judges agreed on {} outright; no deliberation", tops[0]),
2784                );
2785            }
2786            self.state.status = RunStatus::Voting;
2787            self.state.save()?;
2788            return Ok(());
2789        }
2790
2791        self.state.status = RunStatus::Deliberating;
2792        self.state.event(
2793            "deliberate",
2794            format!(
2795                "split: first choices were {} — opening {rounds} round(s)",
2796                tops.iter().collect::<String>()
2797            ),
2798        );
2799
2800        let viable: Vec<Candidate> = self.state.viable().into_iter().cloned().collect();
2801        let language = self.state.config.graph.language.clone();
2802        let timeout = Duration::from_secs(self.state.config.graph.timeout_judge);
2803        let sessions = self.state.config.graph.sessions;
2804        let artifacts = agent::artifacts_dir(&self.state.dir());
2805        let root = self.state.worktree_root();
2806        let base_short = short(&self.state.base_commit);
2807
2808        // Judges argue in sequence so that a turn can answer the one before it;
2809        // that is the difference between deliberation and three parallel
2810        // monologues.
2811        for round in 1..=rounds {
2812            let mut turns: Vec<DeliberationTurn> = Vec::new();
2813            for (j, spec) in self.roles.judges.clone().into_iter().enumerate() {
2814                if self.state.judgements[j].failed.is_some() {
2815                    continue;
2816                }
2817                let seat_key = format!("judge-{}", j + 1);
2818                let spec = self.occupant(&seat_key, spec);
2819                let mut seat = self.seat(&seat_key, &spec.id);
2820                let transcript = self.transcript(&turns, j);
2821                let build = |context: Option<&str>| {
2822                    prompt::deliberate(
2823                        &self.state.instruction,
2824                        context,
2825                        &transcript,
2826                        round,
2827                        rounds,
2828                        &language,
2829                    )
2830                };
2831                let block = self.candidate_block(&viable, &base_short);
2832                let full = build(Some(&block));
2833                let text = if has_context(&spec, &seat, sessions) {
2834                    build(None)
2835                } else {
2836                    full.clone()
2837                };
2838                let job = SeatJob {
2839                    spec,
2840                    seat: seat.clone(),
2841                    prompt: text,
2842                    cwd: root.join(format!("judge-{}", j + 1)),
2843                    timeout,
2844                    allow_write: false,
2845                    sessions,
2846                    artifacts: artifacts.clone(),
2847                    stem: format!("delib-{round}-judge-{}", j + 1),
2848                    handover: Some(full),
2849                };
2850                let cache = self.state.config.cache_dir();
2851                let ctx = WaveCtx {
2852                    carry_seats: false,
2853                    run: &run_id,
2854                    node: "deliberate",
2855                    prompts: &prompts,
2856                    cache: cache.as_deref(),
2857                    round: None,
2858                };
2859                // A turn is never nudged (`retries` 0): a failed seat is
2860                // handed to the next roster agent, which gets the full
2861                // context. An empty answer is a turn, not a failure.
2862                let mut losses = Vec::new();
2863                let mut results = ask_wave_with::<String>(
2864                    vec![job],
2865                    Arc::clone(&self.sem),
2866                    0,
2867                    &self.roles.judge_roster,
2868                    &ctx,
2869                    &mut losses,
2870                    &mut self.state,
2871                    &|text: &str| {
2872                        Ok(verdict::section(text, "position").unwrap_or_else(|| text.to_owned()))
2873                    },
2874                )
2875                .await;
2876                self.state.quota.extend(losses);
2877                let (updated, res, _) = results.pop().expect("one job in, one result out");
2878                seat = updated;
2879                let agent_id = seat.agent.clone();
2880                self.state.seats.insert(seat.key.clone(), seat);
2881                let body = match res {
2882                    Ok((body, _)) => body,
2883                    // Skip the seat; a CLI's raw error JSON is never read as
2884                    // this judge's position.
2885                    Err(e) => {
2886                        self.state
2887                            .event("deliberate", format!("judge {} skipped: {e}", j + 1));
2888                        continue;
2889                    }
2890                };
2891                let tentative = verdict::extract_json::<Position>(&body)
2892                    .ok()
2893                    .and_then(|p| p.tentative)
2894                    .and_then(|s| s.trim().chars().next())
2895                    .map(|c| c.to_ascii_uppercase());
2896                self.state.event(
2897                    "deliberate",
2898                    format!(
2899                        "round {round}: judge {} now favours {}",
2900                        j + 1,
2901                        tentative.map_or("—".to_owned(), |c| c.to_string())
2902                    ),
2903                );
2904                turns.push(DeliberationTurn {
2905                    judge: j + 1,
2906                    agent: agent_id,
2907                    body: blind::sanitize_prose(&body, &self.state.config.blind),
2908                    tentative,
2909                });
2910            }
2911            self.state
2912                .deliberation
2913                .push(DeliberationRound { round, turns });
2914            self.state.save()?;
2915        }
2916
2917        self.state.status = RunStatus::Voting;
2918        self.state.save()?;
2919        Ok(())
2920    }
2921
2922    // ---------------------------------------------------------------- vote
2923
2924    async fn vote(&mut self) -> Result<()> {
2925        // Attribution for every agent this node spawns: `MAGI_RUN` lets a task the
2926        // agent files with `magi task add` name the run that paid for it. The
2927        // prompt overlay is cloned alongside it because the waves borrow it
2928        // while `self` is mutably borrowed by the node's own bookkeeping.
2929        let run_id = self.state.id.clone();
2930        let prompts = self.state.config.prompts.clone();
2931        if !self.state.votes.is_empty() {
2932            return Ok(());
2933        }
2934        let viable: Vec<char> = self.state.viable().into_iter().map(|c| c.label).collect();
2935        if viable.len() == 1 {
2936            return Ok(());
2937        }
2938        self.state.status = RunStatus::Voting;
2939
2940        let language = self.state.config.graph.language.clone();
2941        let timeout = Duration::from_secs(self.state.config.graph.timeout_judge);
2942        let sessions = self.state.config.graph.sessions;
2943        let artifacts = agent::artifacts_dir(&self.state.dir());
2944        let root = self.state.worktree_root();
2945        let base_short = short(&self.state.base_commit);
2946        let candidates: Vec<Candidate> = self.state.viable().into_iter().cloned().collect();
2947
2948        let mut jobs = Vec::new();
2949        let mut seats_at = Vec::new();
2950        for (j, spec) in self.roles.judges.clone().into_iter().enumerate() {
2951            if self
2952                .state
2953                .judgements
2954                .get(j)
2955                .is_some_and(|r| r.failed.is_some())
2956            {
2957                continue;
2958            }
2959            let seat_key = format!("judge-{}", j + 1);
2960            let spec = self.occupant(&seat_key, spec);
2961            let seat = self.seat(&seat_key, &spec.id);
2962            let full = self.vote_prompt_full(j, &viable, &language, &candidates, &base_short);
2963            let text = if has_context(&spec, &seat, sessions) {
2964                prompt::final_vote(&viable, &language)
2965            } else {
2966                full.clone()
2967            };
2968            jobs.push(SeatJob {
2969                spec,
2970                seat,
2971                prompt: text,
2972                cwd: root.join(format!("judge-{}", j + 1)),
2973                timeout,
2974                allow_write: false,
2975                sessions,
2976                artifacts: artifacts.clone(),
2977                stem: format!("vote-judge-{}", j + 1),
2978                handover: Some(full),
2979            });
2980            seats_at.push(j);
2981        }
2982
2983        self.state.event(
2984            "vote",
2985            format!(
2986                "collecting {} final votes one by one, privately",
2987                jobs.len()
2988            ),
2989        );
2990        let allowed = viable.clone();
2991        let mut quota_losses = Vec::new();
2992        let cache = self.state.config.cache_dir();
2993        let ctx = WaveCtx {
2994            carry_seats: false,
2995            run: &run_id,
2996            node: "vote",
2997            prompts: &prompts,
2998            cache: cache.as_deref(),
2999            round: None,
3000        };
3001        let results = ask_json_wave::<FinalVote>(
3002            jobs,
3003            Arc::clone(&self.sem),
3004            self.state.config.graph.retries,
3005            &self.roles.judge_roster,
3006            &ctx,
3007            &mut quota_losses,
3008            &mut self.state,
3009            &move |v: &FinalVote| match v.label() {
3010                Some(c) if allowed.contains(&c) => Ok(()),
3011                other => bail!("vote {other:?} is not one of {allowed:?}"),
3012            },
3013        )
3014        .await;
3015        self.state.quota.extend(quota_losses);
3016
3017        for (&j, (seat, res, _attempts)) in seats_at.iter().zip(results) {
3018            let agent_id = seat.agent.clone();
3019            self.state.seats.insert(seat.key.clone(), seat);
3020            let initial = self
3021                .state
3022                .judgements
3023                .get(j)
3024                .and_then(|r| r.ranking.first().copied());
3025            let mut record = VoteRecord {
3026                judge: j + 1,
3027                agent: agent_id,
3028                vote: None,
3029                reason: String::new(),
3030                changed: false,
3031            };
3032            match res {
3033                Ok((v, _)) => {
3034                    record.vote = v.label();
3035                    record.reason = blind::sanitize_prose(&v.reason, &self.state.config.blind);
3036                    record.changed = matches!((record.vote, initial), (Some(a), Some(b)) if a != b);
3037                    self.state.event(
3038                        "vote",
3039                        format!(
3040                            "judge {} voted {}{}",
3041                            j + 1,
3042                            record.vote.unwrap_or('?'),
3043                            if record.changed { " (changed)" } else { "" }
3044                        ),
3045                    );
3046                }
3047                Err(e) => {
3048                    self.state
3049                        .event("vote", format!("judge {} cast no vote: {e}", j + 1));
3050                }
3051            }
3052            self.state.votes.push(record);
3053            self.state.save()?;
3054        }
3055        Ok(())
3056    }
3057
3058    // --------------------------------------------------------------- tally
3059
3060    fn tally(&mut self) -> Result<()> {
3061        if self.state.tally.is_some() {
3062            return Ok(());
3063        }
3064        let viable: Vec<char> = self.state.viable().into_iter().map(|c| c.label).collect();
3065        let tops: Vec<char> = self
3066            .state
3067            .judgements
3068            .iter()
3069            .filter_map(|j| j.ranking.first().copied())
3070            .collect();
3071        let unanimous_initial = tops.len() > 1 && tops.iter().all(|t| *t == tops[0]);
3072
3073        // A judge whose private vote failed still counted once, in the initial
3074        // ranking; using it beats discarding a whole seat.
3075        let mut first_choice: BTreeMap<char, usize> = viable.iter().map(|l| (*l, 0)).collect();
3076        let mut cast: Vec<char> = Vec::new();
3077        for (i, j) in self.state.judgements.iter().enumerate() {
3078            let vote = self
3079                .state
3080                .votes
3081                .iter()
3082                .find(|v| v.judge == i + 1)
3083                .and_then(|v| v.vote)
3084                .or_else(|| j.ranking.first().copied());
3085            if let Some(v) = vote {
3086                *first_choice.entry(v).or_insert(0) += 1;
3087                cast.push(v);
3088            }
3089        }
3090
3091        let mut borda: BTreeMap<char, usize> = viable.iter().map(|l| (*l, 0)).collect();
3092        for j in &self.state.judgements {
3093            let n = j.ranking.len();
3094            for (pos, label) in j.ranking.iter().enumerate() {
3095                *borda.entry(*label).or_insert(0) += n.saturating_sub(pos + 1);
3096            }
3097        }
3098
3099        let best = first_choice.values().copied().max().unwrap_or(0);
3100        let mut leaders: Vec<char> = first_choice
3101            .iter()
3102            .filter(|(_, v)| **v == best)
3103            .map(|(k, _)| *k)
3104            .collect();
3105        let mut tie_break = None;
3106        if leaders.len() > 1 {
3107            let top_borda = leaders.iter().map(|l| borda[l]).max().unwrap_or(0);
3108            let borda_leaders: Vec<char> = leaders
3109                .iter()
3110                .copied()
3111                .filter(|l| borda[l] == top_borda)
3112                .collect();
3113            tie_break = Some(if borda_leaders.len() == 1 {
3114                format!(
3115                    "{} way tie on first-choice votes, broken by Borda points from the initial rankings",
3116                    leaders.len()
3117                )
3118            } else {
3119                format!(
3120                    "{} way tie on both first-choice votes and Borda points, broken by label order",
3121                    leaders.len()
3122                )
3123            });
3124            leaders = borda_leaders;
3125            leaders.sort_unstable();
3126        }
3127        let winner = *leaders
3128            .first()
3129            .or(viable.first())
3130            .context("no candidate to declare a winner from")?;
3131
3132        let changed_votes = self.state.votes.iter().filter(|v| v.changed).count();
3133        let unanimous_final = !cast.is_empty() && cast.iter().all(|c| *c == cast[0]);
3134        let deliberated = !self.state.deliberation.is_empty();
3135
3136        // Whose verdict is this? A rate-limited seat is absent even if it
3137        // ranked before the limit hit, so presence is measured against the
3138        // recorded losses, not just "did a ranking ever appear".
3139        let quota_seats: std::collections::BTreeSet<&str> =
3140            self.state.quota.iter().map(|q| q.seat.as_str()).collect();
3141        let mut present = 0usize;
3142        for (i, j) in self.state.judgements.iter().enumerate() {
3143            if quota_seats.contains(j.seat.as_str()) {
3144                continue;
3145            }
3146            let ranked = !j.ranking.is_empty() && j.failed.is_none();
3147            let voted = self
3148                .state
3149                .votes
3150                .iter()
3151                .any(|v| v.judge == i + 1 && v.vote.is_some());
3152            if ranked || voted {
3153                present += 1;
3154            }
3155        }
3156        // Strict majority of the configured panel. A bare majority is real
3157        // signal we can act on, while a minority verdict must never stand in
3158        // for a healthy one. A one-candidate run needs no panel at all, and
3159        // `judges` stays `0` rather than the roster size a panel that never
3160        // sat would otherwise be credited with.
3161        let needs_quorum = viable.len() > 1;
3162        let judges_total = if needs_quorum {
3163            self.roles.judges.len()
3164        } else {
3165            0
3166        };
3167        let quorum = if needs_quorum {
3168            judges_total / 2 + 1
3169        } else {
3170            0
3171        };
3172        let met_quorum = !needs_quorum || present >= quorum;
3173        let uncontested = (!needs_quorum).then(|| {
3174            format!("only one candidate ({winner}) produced a usable change; no panel was asked")
3175        });
3176
3177        self.state.event(
3178            "tally",
3179            match &uncontested {
3180                Some(reason) => format!("winner {winner} — {reason}"),
3181                None => format!(
3182                    "winner {winner} — votes {} | initial {} | {} changed | \
3183                     {present}/{judges_total} judges{}",
3184                    first_choice
3185                        .iter()
3186                        .map(|(k, v)| format!("{k}:{v}"))
3187                        .collect::<Vec<_>>()
3188                        .join(" "),
3189                    if unanimous_initial {
3190                        "unanimous"
3191                    } else {
3192                        "split"
3193                    },
3194                    changed_votes,
3195                    if met_quorum {
3196                        String::new()
3197                    } else {
3198                        format!(" — below quorum ({quorum} required)")
3199                    },
3200                ),
3201            },
3202        );
3203        if !met_quorum {
3204            self.state.event(
3205                "stall",
3206                format!(
3207                    "verdict rests on {present} of {judges_total} judges (quorum {quorum}); \
3208                     the run stops here, resumable"
3209                ),
3210            );
3211        }
3212        self.state.tally = Some(Tally {
3213            first_choice,
3214            borda,
3215            winner,
3216            rankings: tops.len(),
3217            unanimous_initial,
3218            deliberated,
3219            changed_votes,
3220            unanimous_final,
3221            tie_break,
3222            judges: judges_total,
3223            present,
3224            quorum,
3225            met_quorum,
3226            uncontested,
3227        });
3228        self.state.status = if met_quorum {
3229            RunStatus::Reviewing
3230        } else {
3231            RunStatus::Stalled
3232        };
3233        self.state.save()?;
3234        Ok(())
3235    }
3236
3237    // ------------------------------------------------------------- recover
3238
3239    /// Re-ask the judge seats `tally` counts as absent, so a `Stalled` run can be
3240    /// resumed toward completion once the transient cause clears.
3241    ///
3242    /// A seat is absent — and therefore re-asked — when `tally` refuses to count
3243    /// it toward the quorum, which is exactly the set of seats whose absence
3244    /// collapsed the panel: struck by a rate limit at *any* node (the quorum must
3245    /// not depend on which node happened to hit the limit), or an ordinary
3246    /// failure (`failed = Some`) that never produced a usable ranking. A healthy
3247    /// seat is never disturbed.
3248    ///
3249    /// A seat that now answers with a usable ranking is "recovered": its
3250    /// `Judgement` is refreshed, its `QuotaLoss`/`failed` state cleared (so
3251    /// `tally` counts it present again), and its vote re-collected. A seat that
3252    /// still fails keeps its loss and stays absent.
3253    ///
3254    /// Returns `true` when the re-tally restores the quorum (the run may proceed
3255    /// to review/gate/merge), `false` when it is still below quorum (the run
3256    /// stays `Stalled`, still resumable for a later retry).
3257    #[allow(clippy::too_many_lines)]
3258    async fn recover_stall(&mut self) -> Result<bool> {
3259        // Attribution for every agent this node spawns: `MAGI_RUN` lets a task the
3260        // agent files with `magi task add` name the run that paid for it. The
3261        // prompt overlay is cloned alongside it because the waves borrow it
3262        // while `self` is mutably borrowed by the node's own bookkeeping.
3263        let run_id = self.state.id.clone();
3264        let prompts = self.state.config.prompts.clone();
3265        // Absent seats = quota-lost at any node, or failed outright. Mirroring
3266        // `tally`'s presence test (rather than the old quota-judge/vote filter)
3267        // is what keeps a non-quota collapse — or a quota loss recorded at the
3268        // deliberate node — from being a permanent dead-end on `--resume`.
3269        let quota_seats: BTreeSet<&str> =
3270            self.state.quota.iter().map(|q| q.seat.as_str()).collect();
3271        let absent: Vec<String> = self
3272            .state
3273            .judgements
3274            .iter()
3275            .filter(|j| quota_seats.contains(j.seat.as_str()) || j.failed.is_some())
3276            .map(|j| j.seat.clone())
3277            .collect();
3278        if absent.is_empty() {
3279            return Ok(false);
3280        }
3281        let viable: Vec<Candidate> = self.state.viable().into_iter().cloned().collect();
3282        if viable.len() <= 1 {
3283            return Ok(false);
3284        }
3285        let labels: Vec<char> = viable.iter().map(|c| c.label).collect();
3286        let language = self.state.config.graph.language.clone();
3287        let timeout = Duration::from_secs(self.state.config.graph.timeout_judge);
3288        let sessions = self.state.config.graph.sessions;
3289        let artifacts = agent::artifacts_dir(&self.state.dir());
3290        let root = self.state.worktree_root();
3291        let base_short = short(&self.state.base_commit);
3292        let candidates: Vec<Candidate> = viable.clone();
3293
3294        // Map each absent seat key to its 0-based position in `roles.judges`.
3295        let mut positions: Vec<usize> = absent
3296            .iter()
3297            .filter_map(|k| self.state.judgements.iter().position(|r| &r.seat == k))
3298            .collect();
3299        if positions.is_empty() {
3300            return Ok(false);
3301        }
3302        positions.sort_unstable();
3303        positions.dedup();
3304
3305        // Re-rank the lost seats, one blind prompt each.
3306        let mut judge_jobs = Vec::new();
3307        for &j in &positions {
3308            let order = blind::presentation_order(viable.len(), j, self.state.seed);
3309            let views: Vec<CandidateView> = order.iter().map(|&k| self.view(&viable[k])).collect();
3310            let seat_key = format!("judge-{}", j + 1);
3311            let spec = self.occupant(&seat_key, self.roles.judges[j].clone());
3312            let seat = self.seat(&seat_key, &spec.id);
3313            judge_jobs.push(SeatJob {
3314                spec,
3315                seat,
3316                prompt: prompt::judge(
3317                    &self.state.instruction,
3318                    &views,
3319                    self.roles.judges.len(),
3320                    &base_short,
3321                    &language,
3322                ),
3323                cwd: root.join(seat_key),
3324                timeout,
3325                allow_write: false,
3326                sessions,
3327                artifacts: artifacts.clone(),
3328                stem: format!("judge-{}-recover", j + 1),
3329                handover: None,
3330            });
3331        }
3332
3333        let labels_for_check = labels.clone();
3334        let mut judge_losses = Vec::new();
3335        let retries = self.state.config.graph.retries;
3336        let cache = self.state.config.cache_dir();
3337        let ctx = WaveCtx {
3338            carry_seats: false,
3339            run: &run_id,
3340            node: "judge",
3341            prompts: &prompts,
3342            cache: cache.as_deref(),
3343            round: None,
3344        };
3345        let results = ask_json_wave::<Ranking>(
3346            judge_jobs,
3347            Arc::clone(&self.sem),
3348            retries,
3349            &self.roles.judge_roster,
3350            &ctx,
3351            &mut judge_losses,
3352            &mut self.state,
3353            &move |r: &Ranking| r.validate(&labels_for_check),
3354        )
3355        .await;
3356
3357        // Refresh the judgement of every seat that ranked again.
3358        let mut recovered: BTreeSet<usize> = BTreeSet::new();
3359        for (&j, (seat, res, _attempts)) in positions.iter().zip(results) {
3360            let agent_id = seat.agent.clone();
3361            self.state.seats.insert(seat.key.clone(), seat);
3362            let record = &mut self.state.judgements[j];
3363            match res {
3364                Ok((ranking, out)) => {
3365                    record.agent = agent_id;
3366                    record.ranking = ranking.normalized();
3367                    record.reasons = ranking.reasons;
3368                    record.strengths = ranking.strengths;
3369                    record.confidence = ranking.confidence;
3370                    record.failed = None;
3371                    record.duration_ms = out.duration_ms;
3372                    recovered.insert(j);
3373                    self.state.event(
3374                        "recover",
3375                        format!("judge {} ranked again after the limit", j + 1),
3376                    );
3377                }
3378                Err(e) => {
3379                    self.state
3380                        .event("recover", format!("judge {} still cannot rank: {e}", j + 1));
3381                }
3382            }
3383        }
3384
3385        // Re-ask the votes of the seats that recovered a ranking.
3386        let mut vote_jobs = Vec::new();
3387        let mut vote_pos: Vec<usize> = Vec::new();
3388        for &j in &recovered {
3389            let seat_key = format!("judge-{}", j + 1);
3390            let spec = self.occupant(&seat_key, self.roles.judges[j].clone());
3391            let seat = self.seat(&seat_key, &spec.id);
3392            let full = self.vote_prompt_full(j, &labels, &language, &candidates, &base_short);
3393            let text = if has_context(&spec, &seat, sessions) {
3394                prompt::final_vote(&labels, &language)
3395            } else {
3396                full.clone()
3397            };
3398            vote_jobs.push(SeatJob {
3399                spec,
3400                seat,
3401                prompt: text,
3402                cwd: root.join(seat_key),
3403                timeout,
3404                allow_write: false,
3405                sessions,
3406                artifacts: artifacts.clone(),
3407                stem: format!("vote-judge-{}-recover", j + 1),
3408                handover: Some(full),
3409            });
3410            vote_pos.push(j);
3411        }
3412        let allowed = labels.clone();
3413        let mut vote_losses = Vec::new();
3414        let vote_retries = self.state.config.graph.retries;
3415        let vote_cache = self.state.config.cache_dir();
3416        let ctx = WaveCtx {
3417            carry_seats: false,
3418            run: &run_id,
3419            node: "vote",
3420            prompts: &prompts,
3421            cache: vote_cache.as_deref(),
3422            round: None,
3423        };
3424        let votes = ask_json_wave::<FinalVote>(
3425            vote_jobs,
3426            Arc::clone(&self.sem),
3427            vote_retries,
3428            &self.roles.judge_roster,
3429            &ctx,
3430            &mut vote_losses,
3431            &mut self.state,
3432            &move |v: &FinalVote| match v.label() {
3433                Some(c) if allowed.contains(&c) => Ok(()),
3434                other => bail!("vote {other:?} is not one of {allowed:?}"),
3435            },
3436        )
3437        .await;
3438        for (&j, (seat, res, _attempts)) in vote_pos.iter().zip(votes) {
3439            let agent_id = seat.agent.clone();
3440            self.state.seats.insert(seat.key.clone(), seat);
3441            match res {
3442                Ok((v, _)) => {
3443                    if let Some(rec) = self.state.votes.iter_mut().find(|r| r.judge == j + 1) {
3444                        rec.vote = v.label();
3445                        rec.reason = blind::sanitize_prose(&v.reason, &self.state.config.blind);
3446                    } else {
3447                        self.state.votes.push(VoteRecord {
3448                            judge: j + 1,
3449                            agent: agent_id,
3450                            vote: v.label(),
3451                            reason: blind::sanitize_prose(&v.reason, &self.state.config.blind),
3452                            changed: false,
3453                        });
3454                    }
3455                    self.state.event(
3456                        "recover",
3457                        format!("judge {} voted again after the limit", j + 1),
3458                    );
3459                }
3460                Err(e) => {
3461                    self.state
3462                        .event("recover", format!("judge {} still cannot vote: {e}", j + 1));
3463                }
3464            }
3465        }
3466
3467        // A seat that ranked again is present even if its re-vote failed —
3468        // `tally` falls back to the initial ranking's first choice — so clear
3469        // its quota loss. Seats that still fail keep theirs and stay absent.
3470        let recovered_keys: BTreeSet<String> = recovered
3471            .iter()
3472            .map(|&j| format!("judge-{}", j + 1))
3473            .collect();
3474        self.state
3475            .quota
3476            .retain(|q| !recovered_keys.contains(&q.seat));
3477        // A seat that hit the limit again is a fresh loss, not the old one:
3478        // replace the stale entry so the history stays one-per-seat and the
3479        // daemon can tell this attempt's loss from a previous session's.
3480        for loss in judge_losses.into_iter().chain(vote_losses) {
3481            if recovered_keys.contains(&loss.seat) {
3482                continue;
3483            }
3484            self.state.quota.retain(|q| q.seat != loss.seat);
3485            self.state.quota.push(loss);
3486        }
3487
3488        // Recompute the verdict from the refreshed panel.
3489        self.state.tally = None;
3490        self.tally()?;
3491        Ok(self
3492            .state
3493            .tally
3494            .as_ref()
3495            .map(|t| t.met_quorum)
3496            .unwrap_or(false))
3497    }
3498
3499    // ---------------------------------------------------------------- graft
3500
3501    /// Port the strengths the judges attributed to the losing candidates onto
3502    /// the winner, once, between the tally and the review loop.
3503    ///
3504    /// Best-effort by construction: it returns nothing and fails nothing. A
3505    /// skip, a fixer that failed or hit quota, a round that changed nothing
3506    /// and a result over the size limit ([`graft::limit`]) all leave the
3507    /// winner exactly at the commit it had before (`GraftRecord::pre`) and the
3508    /// run carries on as if the phase were off. See [`graft`] for the
3509    /// decisions and `AGENTS.md` for the invariants.
3510    ///
3511    /// The one exception is a rollback that could not be confirmed: the
3512    /// rejected commit may still be the winner's tip, so the run is blocked
3513    /// (resumable; the record stays `Running`, which makes a resume retry the
3514    /// restore) instead of reviewing, gating and merging it.
3515    async fn graft(&mut self) -> Result<()> {
3516        let Err(e) = self.graft_inner().await else {
3517            return Ok(());
3518        };
3519        if e.downcast_ref::<GraftRestoreFailed>().is_some() {
3520            self.state.status = RunStatus::Blocked;
3521            self.state.event("graft", format!("blocked: {e:#}"));
3522            self.state.save()?;
3523            return Err(e);
3524        }
3525        // An error after the `Running` record was saved may have left the
3526        // fixer's work in the tree: the record must not stay unsettled
3527        // (a later resume would reset past the review fixes), so confirm the
3528        // restore now, or block.
3529        if let Some(rec) = self.state.graft.clone()
3530            && !rec.outcome.settled()
3531        {
3532            if let Some(winner) = self.state.winner().cloned() {
3533                if let Err(re) = Self::graft_restore(&winner.worktree, &rec.pre).await {
3534                    self.state.status = RunStatus::Blocked;
3535                    self.state.event("graft", format!("blocked: {re:#}"));
3536                    self.state.save()?;
3537                    return Err(re);
3538                }
3539            }
3540            if let Some(g) = self.state.graft.as_mut() {
3541                g.outcome = GraftOutcome::RolledBack(format!("{e:#}"));
3542            }
3543        }
3544        self.state.event("graft", format!("skipped: {e:#}"));
3545        if let Err(e) = self.state.save() {
3546            tracing::warn!("save after graft: {e:#}");
3547        }
3548        Ok(())
3549    }
3550
3551    /// Put the winner back on `pre` and confirm it: a failed `reset` (a stale
3552    /// `index.lock`, say) or a tip that is not `pre` afterwards is an error.
3553    async fn graft_restore(wt: &Path, pre: &str) -> Result<()> {
3554        let restored = git::restore_to(wt, pre).await;
3555        let tip = git::rev_parse(wt, "HEAD").await;
3556        match (restored, tip) {
3557            (Ok(()), Ok(tip)) if tip == pre => Ok(()),
3558            (Err(e), _) => Err(GraftRestoreFailed(format!("{e:#}")).into()),
3559            (Ok(()), Ok(tip)) => {
3560                Err(GraftRestoreFailed(format!("the winner is at {tip}, not {pre}")).into())
3561            }
3562            (Ok(()), Err(e)) => Err(GraftRestoreFailed(format!("{e:#}")).into()),
3563        }
3564    }
3565
3566    /// A skip is a run event, recorded once however often the node is re-entered.
3567    fn graft_skipped(&mut self, why: &str) {
3568        let message = format!("skipped: {why}");
3569        if !self
3570            .state
3571            .events
3572            .iter()
3573            .any(|e| e.node == "graft" && e.message == message)
3574        {
3575            self.state.event("graft", message);
3576        }
3577    }
3578
3579    async fn graft_inner(&mut self) -> Result<()> {
3580        // A round that was started and never finished (crash, kill): the
3581        // fixer may have left anything in the tree, so put the winner back.
3582        if let Some(rec) = self.state.graft.clone()
3583            && !rec.outcome.settled()
3584        {
3585            // Reviews exist only if the graft ended without its record being
3586            // settled (a swallowed error): the tree has moved on since, and
3587            // a reset to `pre` would discard the review fixes.
3588            if self.state.reviews.is_empty()
3589                && let Some(winner) = self.state.winner().cloned()
3590            {
3591                Self::graft_restore(&winner.worktree, &rec.pre).await?;
3592            }
3593            if let Some(g) = self.state.graft.as_mut() {
3594                g.outcome = GraftOutcome::RolledBack(graft::INTERRUPTED.to_owned());
3595            }
3596            self.state.event(
3597                "graft",
3598                "rolled back: the previous round was interrupted; the winner is untouched",
3599            );
3600            self.state.save()?;
3601            return Ok(());
3602        }
3603        let plan = match graft::plan(&self.state) {
3604            Ok(plan) => plan,
3605            Err(skip) => {
3606                if skip != graft::Skip::AlreadyDone {
3607                    self.graft_skipped(skip.reason());
3608                    self.state.save()?;
3609                }
3610                return Ok(());
3611            }
3612        };
3613        let Some(winner) = self.state.winner().cloned() else {
3614            return Ok(());
3615        };
3616        let wt = winner.worktree.clone();
3617        if !git::is_clean(&wt).await? {
3618            self.graft_skipped("the winner's worktree has uncommitted changes");
3619            self.state.save()?;
3620            return Ok(());
3621        }
3622        let pre = git::rev_parse(&wt, "HEAD").await?;
3623        let Some(winner_lines) = git::changed_lines(&wt, &self.state.base_commit, &pre).await
3624        else {
3625            self.graft_skipped("the winner's diff could not be measured");
3626            self.state.save()?;
3627            return Ok(());
3628        };
3629        let limit = graft::limit(winner_lines);
3630        // Saved before the fixer is called: a stop from here on is repaired
3631        // by the branch above on the next entry.
3632        self.state.graft = Some(GraftRecord {
3633            pre: pre.clone(),
3634            post: None,
3635            lines: None,
3636            limit: Some(limit),
3637            agent: None,
3638            outcome: GraftOutcome::Running,
3639        });
3640        self.state.event(
3641            "graft",
3642            format!(
3643                "porting strengths of candidate(s) {} onto {} (limit {limit} changed lines)",
3644                plan.strengths
3645                    .iter()
3646                    .map(|(l, _, _)| l.to_string())
3647                    .collect::<Vec<_>>()
3648                    .join(", "),
3649                plan.winner
3650            ),
3651        );
3652        self.state.save()?;
3653
3654        let fix_prompt = prompt::graft(
3655            &self.state.instruction,
3656            plan.winner,
3657            &plan.strengths,
3658            limit,
3659            &self.state.config.graph.language,
3660        );
3661        let timeout = Duration::from_secs(self.state.config.graph.timeout_fix);
3662        let sessions = self.state.config.graph.sessions;
3663        let artifacts = agent::artifacts_dir(&self.state.dir());
3664        let attempts = fixer::graft_attempts(&self.state, &self.roles, &winner);
3665        let (_, seat, out) = self
3666            .ask_fixer_among(attempts, "graft", None, Some((&wt, &pre)), |spec, seat| {
3667                SeatJob {
3668                    prompt: fix_prompt.clone(),
3669                    spec,
3670                    seat,
3671                    cwd: wt.clone(),
3672                    timeout,
3673                    allow_write: true,
3674                    sessions,
3675                    artifacts: artifacts.clone(),
3676                    stem: "graft".to_owned(),
3677                    handover: None,
3678                }
3679            })
3680            .await;
3681        let mut why: Option<String> = None;
3682        match &out {
3683            AgentOutcome::Ok(o) if agent::output_advances(o) => {
3684                why = Some("the fixer produced nothing usable".to_owned());
3685            }
3686            AgentOutcome::Ok(_) => {}
3687            AgentOutcome::Dropped(_) => why = Some("the CLI dropped the stream".to_owned()),
3688            AgentOutcome::Quota(o) => {
3689                self.state.quota.push(QuotaLoss {
3690                    seat: seat.key.clone(),
3691                    node: "graft".to_owned(),
3692                    at: Timestamp::now(),
3693                    reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
3694                });
3695                why = Some("rate limited (quota)".to_owned());
3696            }
3697            AgentOutcome::Failed(e) => {
3698                why = Some(format!(
3699                    "the fixer failed: {}",
3700                    e.lines()
3701                        .next()
3702                        .unwrap_or("")
3703                        .chars()
3704                        .take(160)
3705                        .collect::<String>()
3706                ));
3707            }
3708        }
3709        let agent_id = seat.agent.clone();
3710        self.state.seats.insert(seat.key.clone(), seat);
3711
3712        let mut post = None;
3713        let mut lines = None;
3714        if why.is_none() {
3715            let rescued = git::rescue_commit(&wt, "magi: graft (uncommitted work)").await;
3716            if let Ok(r) = &rescued {
3717                self.state.note_withheld("graft", &r.withheld);
3718            }
3719            let head = git::rev_parse(&wt, "HEAD").await?;
3720            // What is measured must be what gets reviewed and gated: a failed
3721            // rescue, or anything still uncommitted (withheld lockfiles
3722            // aside, which are never part of the tree), rejects the graft.
3723            let dirty = match &rescued {
3724                Ok(r) => git::status_porcelain(&wt)
3725                    .await
3726                    .map(|st| {
3727                        st.lines().any(|l| {
3728                            !r.withheld
3729                                .iter()
3730                                .any(|w| l.trim_end().ends_with(w.path.as_str()))
3731                        })
3732                    })
3733                    .unwrap_or(true),
3734                Err(_) => true,
3735            };
3736            if dirty {
3737                why = Some("the fixer left changes that could not be committed".to_owned());
3738            } else if head == pre {
3739                why = Some("the fixer changed nothing".to_owned());
3740            } else {
3741                lines = git::changed_lines(&wt, &pre, &head).await;
3742                if graft::within_budget(lines, limit) {
3743                    post = Some(head);
3744                } else {
3745                    why = Some(format!(
3746                        "{} changed lines against a limit of {limit}",
3747                        lines.map_or_else(
3748                            || "an unmeasurable number of".to_owned(),
3749                            |l| l.to_string()
3750                        )
3751                    ));
3752                }
3753            }
3754        }
3755        let outcome = match why {
3756            None => {
3757                self.state.event(
3758                    "graft",
3759                    format!(
3760                        "applied: {} changed lines (limit {limit})",
3761                        lines.unwrap_or(0)
3762                    ),
3763                );
3764                GraftOutcome::Applied
3765            }
3766            Some(why) => {
3767                // Not settled until the restore is confirmed: the record
3768                // stays `Running`, so a resume retries it.
3769                Self::graft_restore(&wt, &pre).await?;
3770                self.state.event(
3771                    "graft",
3772                    format!("rolled back: {why}; the winner is untouched"),
3773                );
3774                GraftOutcome::RolledBack(why)
3775            }
3776        };
3777        self.state.graft = Some(GraftRecord {
3778            pre,
3779            post,
3780            lines,
3781            limit: Some(limit),
3782            agent: Some(agent_id),
3783            outcome,
3784        });
3785        self.state.save()?;
3786        Ok(())
3787    }
3788
3789    // ----------------------------------------------------------------- fold
3790
3791    async fn fold_losers(&mut self) -> Result<()> {
3792        let Some(winner) = self.state.tally.as_ref().map(|t| t.winner) else {
3793            return Ok(());
3794        };
3795        let repo = self.state.repo.clone();
3796        let mut folded = Vec::new();
3797        for i in 0..self.state.candidates.len() {
3798            let c = &self.state.candidates[i];
3799            if c.label == winner || c.folded {
3800                continue;
3801            }
3802            let (wt, branch, label) = (c.worktree.clone(), c.branch.clone(), c.label);
3803            git::worktree_remove(&repo, &wt).await.ok();
3804            git::branch_delete(&repo, &branch).await.ok();
3805            self.state.candidates[i].folded = true;
3806            folded.push(label.to_string());
3807        }
3808        // The judges are finished; their checkouts are pure cost from here.
3809        let root = self.state.worktree_root();
3810        for j in 1..=self.roles.judges.len() {
3811            let wt = root.join(format!("judge-{j}"));
3812            if wt.exists() {
3813                git::worktree_remove(&repo, &wt).await.ok();
3814            }
3815        }
3816        // The design-deliberation stage is finished by the time a tally
3817        // exists — same reasoning as the judges above.
3818        if self.state.config.graph.advise {
3819            for k in 1..=self.state.config.graph.advisors {
3820                let wt = root.join(format!("advisor-{k}"));
3821                if wt.exists() {
3822                    git::worktree_remove(&repo, &wt).await.ok();
3823                }
3824            }
3825        }
3826        if !folded.is_empty() {
3827            self.state
3828                .event("fold", format!("folded candidates {}", folded.join(", ")));
3829            self.state.save()?;
3830        }
3831        Ok(())
3832    }
3833
3834    // ------------------------------------------------------------ base sync
3835
3836    /// Land the winner's tree on the current tip of `<remote>/<base>` before
3837    /// anything verifies it.
3838    ///
3839    /// `verify.e2e`, `verify.gate` and every reviewer in [`Self::review_loop`]
3840    /// read whatever is checked out in the winner's worktree. Left alone that
3841    /// tree stays rooted at `base_commit` - the base as [`resolve_base`] saw
3842    /// it when the run *branched* - and a run takes long enough that the base
3843    /// has usually moved by the time it gets here. A gate that ran there
3844    /// answers "green on the commit this run started from", not "green on
3845    /// what is about to land", and the difference showed up three times in
3846    /// one day as a green run whose merge would have reverted a file another
3847    /// pull request had already landed.
3848    ///
3849    /// Reuses [`crate::rebase::rebase_with_fixer`], the same routine
3850    /// `land::Step::Rebase` calls, rather than a second implementation of the
3851    /// same idea: a throwaway worktree, nothing runs in the primary tree, and
3852    /// a second rebase path is exactly the kind of drift `resolve_base`'s own
3853    /// doc warns about ("two answers to a question nobody notices until a
3854    /// diff is wrong").
3855    ///
3856    /// A conflict is not the end of the road: the standing rebase is handed
3857    /// to the fixer seat, at most `graph.review_rounds` times, counted in
3858    /// `state.rebase_fixes` (so it survives a park/resume and is shared with
3859    /// land). Once it finishes, review and the gate run as usual on the
3860    /// rebased tree, which is where a breakage the new base caused is caught
3861    /// by the ordinary gate-fix round. magi resolves nothing itself.
3862    ///
3863    /// Two different bounds, easy to confuse: [`BASE_SYNC_ROUNDS`], counted in
3864    /// `state.base_sync.attempts`, is how many times the base is *rebased
3865    /// onto* (a base that keeps moving); `rebase_fixes` is how many times a
3866    /// *conflict* was given to a fixer. When the fixer cannot finish the
3867    /// rebase the branch is restored, `state.base_sync.conflict` is set with
3868    /// what was tried (rounds spent, paths still conflicted) and the branch
3869    /// and worktree stay exactly as they were - untouched, for a person to
3870    /// look at - which is also what makes re-entering this function
3871    /// afterwards a no-op instead of a second attempt at the same wall. A
3872    /// push failure ends the same way.
3873    async fn sync_to_base(&mut self) -> Result<()> {
3874        if self.state.status == RunStatus::AlreadyInBase {
3875            return Ok(());
3876        }
3877        let conflicted = self
3878            .state
3879            .base_sync
3880            .as_ref()
3881            .is_some_and(|s| s.conflict.is_some());
3882        let Some(winner) = self.state.winner().cloned() else {
3883            return Ok(());
3884        };
3885
3886        let repo = self.state.repo.clone();
3887        let remote = self.state.config.merge.remote.clone();
3888        let base_branch = self.state.base_branch.clone();
3889        let tracking = format!("{remote}/{base_branch}");
3890
3891        git::fetch(&repo, &remote, &base_branch).await.ok();
3892        // No network, or the remote never had this branch: the run already
3893        // started from a fetched `<remote>/<base>` (`resolve_base` refuses
3894        // otherwise), and one that got this far is not blocked by a fetch
3895        // that fails now. It just has no newer tip to compare against.
3896        let Ok(tip) = git::rev_parse(&repo, &tracking).await else {
3897            return Ok(());
3898        };
3899
3900        let head = git::rev_parse(&winner.worktree, "HEAD").await?;
3901        let behind = git::commits_ahead(&repo, &head, &tip).await.unwrap_or(0);
3902        let attempts = self.state.base_sync.as_ref().map_or(0, |s| s.attempts);
3903
3904        // Before any rebase, and before a recorded conflict is honoured: a
3905        // branch whose change reached the base under other commit ids has
3906        // nothing to rebase and nothing to conflict with, and a run that
3907        // already stopped on that phantom conflict recovers here on resume.
3908        // `behind == 0` with head == tip is a branch the base has since taken
3909        // in whole, whether or not a conflict was ever recorded: the ancestry
3910        // proof must still run (`classify` ignores a head still on the start
3911        // commit).
3912        if (behind > 0 || conflicted || head == tip)
3913            && self
3914                .settle_already_in(&winner.branch, &tip, &head, attempts, behind)
3915                .await?
3916        {
3917            return Ok(());
3918        }
3919        if conflicted {
3920            return Ok(());
3921        }
3922
3923        if behind == 0 {
3924            // A fixer-finished rebase moves the branch ref before the
3925            // winner's worktree is told (`sync_to_head` below). A run that
3926            // died in between resumes here with `behind == 0` and a tree still
3927            // holding the pre-rebase files, which review and the gate would
3928            // then read. That state is exactly: HEAD moved off the tip the
3929            // rebase started from, yet the tree is still identical to that
3930            // tip. A tree with edits of its own differs from it, so nothing
3931            // is thrown away.
3932            if let Some(from) = self
3933                .state
3934                .rebase_fixes
3935                .iter()
3936                .rev()
3937                .find_map(|r| r.from.clone())
3938                && from != head
3939                && git::git_raw(&winner.worktree, &["diff", "--quiet", &from])
3940                    .await
3941                    .is_ok_and(|o| o.ok())
3942            {
3943                git::sync_to_head(&winner.worktree).await?;
3944            }
3945            // An earlier attempt may have rebased the branch locally and died
3946            // before pushing it (only the fresh-rebase arm below pushes).
3947            // Publish it now, so the plain push at PR time is not refused as
3948            // a non-fast-forward. A run already holding a recorded conflict
3949            // never reaches here; that case is out of scope.
3950            let conflict = self.publish_resumed_rebase(&winner.branch, &head).await;
3951            if let Some(why) = &conflict {
3952                self.state.status = RunStatus::Blocked;
3953                self.state.event("land", why.clone());
3954            }
3955            self.state.base_sync = Some(BaseSync {
3956                tip,
3957                behind: 0,
3958                attempts,
3959                conflict,
3960                already_in: None,
3961            });
3962            self.state.save()?;
3963            return Ok(());
3964        }
3965
3966        if attempts >= BASE_SYNC_ROUNDS {
3967            let why = format!(
3968                "{base_branch} moved {behind} commit(s) ahead of {} after {BASE_SYNC_ROUNDS} \
3969                 rebase(s); rebasing again would only race it",
3970                winner.branch
3971            );
3972            self.state.status = RunStatus::Blocked;
3973            self.state.base_sync = Some(BaseSync {
3974                tip,
3975                behind,
3976                attempts,
3977                conflict: Some(why.clone()),
3978                already_in: None,
3979            });
3980            self.state.event("land", why);
3981            self.state.save()?;
3982            return Ok(());
3983        }
3984
3985        self.state.event(
3986            "land",
3987            format!(
3988                "{base_branch} moved {behind} commit(s) ahead of {}; rebasing before verifying",
3989                winner.branch
3990            ),
3991        );
3992        self.state.save()?;
3993
3994        // The remote's copy of the branch, read now and only if the fetch
3995        // really succeeded (a stale tracking ref must never pin a lease). It is
3996        // pushed over after a rebase only when it is a commit this branch
3997        // already contains, by ancestry or by patch (an earlier rebase of ours
3998        // that never reached the remote): anything else is somebody else's work.
3999        let branch_tracking = format!("{remote}/{}", winner.branch);
4000        let fetched_branch = git::fetch(&repo, &remote, &winner.branch).await;
4001        let remote_tip = if self.state.task_pr_branch().is_none()
4002            && matches!(&fetched_branch, Ok(o) if o.ok())
4003        {
4004            git::rev_parse(&repo, &branch_tracking).await.ok()
4005        } else {
4006            None
4007        };
4008        // A remote tip this branch does not contain is somebody else's work:
4009        // rebasing would leave a local tip that can never be pushed. Stop
4010        // before touching anything and say so.
4011        if let Some(theirs) = &remote_tip
4012            && !git::is_ancestor(&repo, theirs, &head).await
4013            && !crate::reconcile::origin_missing(&repo, &head, theirs)
4014                .await
4015                .is_ok_and(|missing| missing.is_empty())
4016        {
4017            let why = format!(
4018                "{branch_tracking} ({}) has commits {} does not contain; not rebasing over \
4019                 them",
4020                short(theirs),
4021                winner.branch
4022            );
4023            self.state.status = RunStatus::Blocked;
4024            self.state.base_sync = Some(BaseSync {
4025                tip,
4026                behind,
4027                attempts,
4028                conflict: Some(why.clone()),
4029                already_in: None,
4030            });
4031            self.state.event("land", why);
4032            self.state.save()?;
4033            return Ok(());
4034        }
4035
4036        let scratch = self.state.dir().join("base-sync");
4037        let rebased = match crate::rebase::rebase_with_fixer(
4038            &mut self.state,
4039            &scratch,
4040            &winner.branch,
4041            &tracking,
4042        )
4043        .await
4044        {
4045            Ok(crate::rebase::Rebased::Applied) => Ok(None),
4046            Ok(crate::rebase::Rebased::Stopped(why)) => Ok(Some(why)),
4047            Err(e) => Err(e),
4048        };
4049        let attempts = attempts + 1;
4050        match rebased {
4051            Ok(None) => {
4052                // The branch ref moved, but a worktree that already had it
4053                // checked out (the winner's) was not told; sync its index and
4054                // files before anything reads them.
4055                git::sync_to_head(&winner.worktree).await?;
4056                refresh_reviewed_commits(&mut self.state, &winner.branch).await;
4057                let mut conflict = None;
4058                if let Some(pinned) = &remote_tip {
4059                    let pushed = git::push_pinned(&repo, &remote, &winner.branch, pinned).await;
4060                    match pushed {
4061                        Ok(o) if o.ok() => self.state.event(
4062                            "land",
4063                            format!("pushed rebased {} to {remote}", winner.branch),
4064                        ),
4065                        Ok(o) => {
4066                            conflict = Some(format!(
4067                                "rebased {} locally but {remote} refused the push (it moved                                  since {}; someone may have pushed): {}",
4068                                winner.branch,
4069                                short(pinned),
4070                                o.stderr.chars().take(600).collect::<String>()
4071                            ));
4072                        }
4073                        Err(e) => {
4074                            conflict = Some(format!(
4075                                "rebased {} locally but could not push it: {e:#}",
4076                                winner.branch
4077                            ));
4078                        }
4079                    }
4080                }
4081                if let Some(why) = &conflict {
4082                    self.state.status = RunStatus::Blocked;
4083                    self.state.event("land", why.clone());
4084                }
4085                self.state.base_sync = Some(BaseSync {
4086                    tip: tip.clone(),
4087                    behind: 0,
4088                    attempts,
4089                    conflict,
4090                    already_in: None,
4091                });
4092                self.state
4093                    .event("land", format!("rebased {} onto {tracking}", winner.branch));
4094            }
4095            Ok(Some(conflict)) => {
4096                let why = format!(
4097                    "{} conflicts with {tracking} and did not rebase: {}",
4098                    winner.branch,
4099                    conflict.chars().take(600).collect::<String>()
4100                );
4101                self.state.status = RunStatus::Blocked;
4102                self.state.base_sync = Some(BaseSync {
4103                    tip,
4104                    behind,
4105                    attempts,
4106                    conflict: Some(why.clone()),
4107                    already_in: None,
4108                });
4109                self.state.event("land", why);
4110            }
4111            Err(e) => {
4112                let why = format!("could not rebase {} onto {tracking}: {e:#}", winner.branch);
4113                self.state.status = RunStatus::Blocked;
4114                self.state.base_sync = Some(BaseSync {
4115                    tip,
4116                    behind,
4117                    attempts,
4118                    conflict: Some(why.clone()),
4119                    already_in: None,
4120                });
4121                self.state.event("land", why);
4122            }
4123        }
4124        self.state.save()?;
4125        Ok(())
4126    }
4127
4128    /// Push a branch an earlier attempt rebased locally but never published,
4129    /// pinned to the remote tip read right after a successful fetch. Returns
4130    /// the reason when the run must stop; `None` when there was nothing to do
4131    /// (no remote copy, the same tip, or a remote copy this branch already
4132    /// contains, which the PR-time push fast-forwards) or the push succeeded.
4133    async fn publish_resumed_rebase(&mut self, branch: &str, head: &str) -> Option<String> {
4134        // Task competitions publish only after the PR text gate, to their
4135        // public head; never resurrect a candidate branch on the remote.
4136        if self.state.task_pr_branch().is_some() {
4137            return None;
4138        }
4139        let repo = self.state.repo.clone();
4140        let remote = self.state.config.merge.remote.clone();
4141        let fetched = git::fetch(&repo, &remote, branch).await;
4142        if !matches!(&fetched, Ok(o) if o.ok()) {
4143            return None;
4144        }
4145        let branch_tracking = format!("{remote}/{branch}");
4146        let theirs = git::rev_parse(&repo, &branch_tracking).await.ok()?;
4147        if theirs == head || git::is_ancestor(&repo, &theirs, head).await {
4148            return None;
4149        }
4150        if !crate::reconcile::origin_missing(&repo, head, &theirs)
4151            .await
4152            .is_ok_and(|missing| missing.is_empty())
4153        {
4154            return Some(format!(
4155                "{branch_tracking} ({}) has commits {branch} does not contain; not pushing over \
4156                 them",
4157                short(&theirs)
4158            ));
4159        }
4160        match git::push_pinned(&repo, &remote, branch, &theirs).await {
4161            Ok(o) if o.ok() => {
4162                self.state
4163                    .event("land", format!("pushed rebased {branch} to {remote}"));
4164                None
4165            }
4166            Ok(o) => Some(format!(
4167                "{branch} is rebased locally but {remote} refused the push (it moved since {}; \
4168                 someone may have pushed): {}",
4169                short(&theirs),
4170                o.stderr.chars().take(600).collect::<String>()
4171            )),
4172            Err(e) => Some(format!(
4173                "{branch} is rebased locally but could not be pushed: {e:#}"
4174            )),
4175        }
4176    }
4177
4178    /// End the run as [`RunStatus::AlreadyInBase`] when `head`'s whole change
4179    /// is already on `tip` under other commit ids ([`crate::already`]); returns
4180    /// whether it did.
4181    ///
4182    /// Checked only when the base is ahead of the branch. A failing check is
4183    /// "not proven" - the ordinary rebase path then decides - never a reason to
4184    /// stop the run.
4185    ///
4186    /// The remote copy of the branch is held to the same standard as the local
4187    /// one: if it carries a tip this worktree does not, that tip must itself be
4188    /// proven in the base, or nothing is settled (a pull request would
4189    /// otherwise be closed over commits nobody checked). The pull request is
4190    /// closed *before* the terminal status is saved; if that fails for a
4191    /// reason other than a refusal (no network, a `gh` error) the run is left
4192    /// `Blocked` with the reason as its conflict, which a resume retries -
4193    /// the same recovery a phantom conflict gets.
4194    async fn settle_already_in(
4195        &mut self,
4196        branch: &str,
4197        tip: &str,
4198        head: &str,
4199        attempts: usize,
4200        behind: usize,
4201    ) -> Result<bool> {
4202        let repo = self.state.repo.clone();
4203        let remote = self.state.config.merge.remote.clone();
4204        let start = self.state.base_commit.clone();
4205        let evidence = match crate::already::classify(&repo, tip, head, Some(&start)).await {
4206            Ok(Some(e)) => e,
4207            Ok(None) => return Ok(false),
4208            Err(e) => {
4209                tracing::warn!("already-in-base check for {branch}: {e:#}");
4210                return Ok(false);
4211            }
4212        };
4213        let mut verified = vec![head.to_owned()];
4214        let public_branch = self.state.pr_branch().unwrap_or(branch).to_owned();
4215        let fetched = git::fetch(&repo, &remote, &public_branch).await;
4216        if matches!(&fetched, Ok(o) if o.ok())
4217            && let Ok(theirs) = git::rev_parse(&repo, &format!("{remote}/{public_branch}")).await
4218            && theirs != head
4219        {
4220            match crate::already::classify(&repo, tip, &theirs, Some(&start)).await {
4221                Ok(Some(_)) => verified.push(theirs),
4222                _ => return Ok(false),
4223            }
4224        }
4225        let base_branch = self.state.base_branch.clone();
4226        let message = format!(
4227            "{branch} is already in {remote}/{base_branch} as {} ({} match); nothing left to \
4228             land",
4229            evidence.names(),
4230            evidence.proof.as_str()
4231        );
4232        let closed =
4233            crate::land::close_superseded_pr(&mut self.state, &public_branch, &evidence, &verified)
4234                .await;
4235        match closed {
4236            Ok(Ok(url)) => self
4237                .state
4238                .event("land", format!("closed {url}: superseded on {base_branch}")),
4239            Ok(Err(why)) => self
4240                .state
4241                .event("land", format!("did not close a pull request: {why}")),
4242            Err(e) => {
4243                let why = format!(
4244                    "{branch} is already in {remote}/{base_branch}, but its pull request could \
4245                     not be closed ({e:#}); resume to retry"
4246                );
4247                self.state.status = RunStatus::Blocked;
4248                self.state.base_sync = Some(BaseSync {
4249                    tip: tip.to_owned(),
4250                    behind,
4251                    attempts,
4252                    conflict: Some(why.clone()),
4253                    already_in: None,
4254                });
4255                self.state.event("land", why);
4256                self.state.save()?;
4257                return Ok(true);
4258            }
4259        }
4260        self.state.status = RunStatus::AlreadyInBase;
4261        self.state.base_sync = Some(BaseSync {
4262            tip: tip.to_owned(),
4263            behind,
4264            attempts,
4265            conflict: None,
4266            already_in: Some(evidence),
4267        });
4268        self.state.event("land", message);
4269        self.state.save()?;
4270        self.settle_questions();
4271        Ok(true)
4272    }
4273
4274    /// The commit review and gate diff against: the tip [`Self::sync_to_base`]
4275    /// last landed the winner on, once it has run, else the commit the run
4276    /// branched from.
4277    ///
4278    /// Only [`Self::review_loop`] reads this. `prep`, `judge`, `deliberate`
4279    /// and `vote` all happen before there is a winner to rebase, so they
4280    /// compare every candidate against the branch point on purpose, and a
4281    /// base that moves after they are already done cannot change an answer
4282    /// they already gave.
4283    fn landing_base(&self) -> String {
4284        self.state
4285            .base_sync
4286            .as_ref()
4287            .map_or_else(|| self.state.base_commit.clone(), |s| s.tip.clone())
4288    }
4289
4290    // ------------------------------------------------------- operator fix
4291
4292    /// Route specific, already-recorded review findings to a fixer for a
4293    /// targeted, out-of-band fix on the winning branch — `magi fix`'s own
4294    /// entry point.
4295    ///
4296    /// Distinct from `review_loop`'s own fix step in three ways: it never
4297    /// runs a reviewer wave, it never spends review-round budget, and what
4298    /// happened is recorded as an [`OperatorFixRequest`] appended to
4299    /// [`RunState::operator_fixes`], never folded into a [`ReviewRound`] —
4300    /// see `run::SCHEMA`'s doc for schema 9 on why a reviewer's own severity
4301    /// and vote must never be rewritten to look like a manufactured blocking
4302    /// verdict.
4303    ///
4304    /// Only meaningful once review has actually concluded: `Ready` (handed
4305    /// off with findings still open, or simply concluded clean while minor
4306    /// findings sat unaddressed) or `Blocked` (round budget spent, or the
4307    /// gate failed). Everything else is refused: a run still in progress
4308    /// should simply be resumed, and a `Merged` run's branch has already
4309    /// landed — reopening *this* run's own record cannot change that, so the
4310    /// answer there is a fresh `magi review <branch>`.
4311    ///
4312    /// A real commit here re-verifies through a fresh, ordinary review-only
4313    /// run on the same branch ([`Self::review`]) rather than reopening this
4314    /// run's own `review_loop`: once any round in this run's history went
4315    /// clean, `review_conclusion` treats that as permanent by design (the
4316    /// same purity `gate`/`merge` rely on for safe reentry), so there is no
4317    /// way to force one more genuine reviewer wave out of *this* run without
4318    /// either rewriting history or weakening that guarantee for every other
4319    /// caller. A review-only run costs nothing extra — no implementation, no
4320    /// judging, no vote — and exercises the exact same review → verify →
4321    /// gate → (human) merge path, unmodified.
4322    pub async fn fix_selected(
4323        &mut self,
4324        ids: &[String],
4325        reason: &str,
4326        allow_stale: bool,
4327    ) -> Result<()> {
4328        let reason = reason.trim();
4329        if reason.is_empty() {
4330            bail!("a fix request needs a reason — that is the operator's own record of why");
4331        }
4332        if ids.is_empty() {
4333            bail!("no finding id given");
4334        }
4335        if !matches!(self.state.status, RunStatus::Ready | RunStatus::Blocked) {
4336            bail!(
4337                "run {} is `{}`; only a `ready` or `blocked` run — one whose review \
4338                 has already concluded — can be given a targeted fix. A run still \
4339                 in progress should simply be resumed; a `merged` run's branch has \
4340                 already landed, so its answer is a fresh `magi review <branch>`, \
4341                 not reopening this run's own record",
4342                self.state.id,
4343                self.state.status.as_str()
4344            );
4345        }
4346        let Some(winner) = self.state.winner().cloned() else {
4347            bail!("run {} has no winning candidate to fix", self.state.id);
4348        };
4349        if !git::branch_exists(&self.state.repo, &winner.branch).await? {
4350            bail!(
4351                "branch `{}` no longer exists; this run cannot be extended",
4352                winner.branch
4353            );
4354        }
4355        let home = crate::run::home();
4356        if crate::daemon::is_working_on(&home, &self.state.id, Timestamp::now()) {
4357            bail!(
4358                "run {} is currently being worked on by another magi process",
4359                self.state.id
4360            );
4361        }
4362        // Held for the rest of this call, including the follow-up review
4363        // below: two `magi fix` invocations against the same run must not
4364        // both reach the worktree manipulation further down, which would
4365        // otherwise race to remove and recreate the same directory — see
4366        // [`FixClaim`]'s own doc.
4367        let _claim = FixClaim::acquire(&self.state.dir())?;
4368
4369        // Resolve every id before spending anything — an unknown id refuses
4370        // the whole request rather than silently dropping it — and dedup
4371        // while keeping the operator's own order.
4372        let mut seen = BTreeSet::new();
4373        let mut findings = Vec::new();
4374        let mut missing = Vec::new();
4375        for id in ids {
4376            if !seen.insert(id.clone()) {
4377                continue;
4378            }
4379            match self.state.finding(id) {
4380                Some((round, rec, f)) => findings.push(OperatorFixFinding {
4381                    id: f.id.clone(),
4382                    severity: f.severity,
4383                    reviewer_vote: rec.vote,
4384                    round: round.round,
4385                    round_head: round.head.clone(),
4386                    reviewer: rec.reviewer,
4387                    agent: rec.agent.clone(),
4388                    file: f.file.clone(),
4389                    line: f.line,
4390                    title: f.title.clone(),
4391                    detail: f.detail.clone(),
4392                    outcome: OperatorFixOutcome::Pending,
4393                }),
4394                None => missing.push(id.clone()),
4395            }
4396        }
4397        if !missing.is_empty() {
4398            bail!(
4399                "unknown finding id(s): {}; nothing was changed",
4400                missing.join(", ")
4401            );
4402        }
4403
4404        let head_at_request = git::rev_parse(&self.state.repo, &winner.branch).await?;
4405        let stale_details: Vec<(String, String)> = findings
4406            .iter()
4407            .filter(|f| f.round_head != head_at_request)
4408            .map(|f| (f.id.clone(), f.round_head.clone()))
4409            .collect();
4410        let stale = !stale_details.is_empty();
4411        if stale && !allow_stale {
4412            bail!(
4413                "the branch has moved since some finding(s) were raised — {} — now \
4414                 at {}; pass --allow-stale to fix anyway, or re-run review first",
4415                stale_details
4416                    .iter()
4417                    .map(|(id, head)| format!("{id} (raised against {})", short(head)))
4418                    .collect::<Vec<_>>()
4419                    .join(", "),
4420                short(&head_at_request)
4421            );
4422        }
4423
4424        let request = OperatorFixRequest {
4425            requested_at: Timestamp::now(),
4426            reason: reason.to_owned(),
4427            findings,
4428            head_at_request: head_at_request.clone(),
4429            allow_stale,
4430            stale,
4431            fix: None,
4432            result_head: None,
4433            follow_up_review_run: None,
4434        };
4435        self.state.event(
4436            "fix",
4437            format!(
4438                "operator requested a targeted fix on {} finding(s) ({}): {reason}",
4439                request.findings.len(),
4440                request
4441                    .findings
4442                    .iter()
4443                    .map(|f| f.id.as_str())
4444                    .collect::<Vec<_>>()
4445                    .join(", "),
4446            ),
4447        );
4448        // Recorded now, before any worktree work or the fixer call itself —
4449        // and re-saved at each checkpoint below: a crash at any point after
4450        // this (mid fixer call, mid follow-up review) must not lose the fact
4451        // that this was requested, for which findings, and why. Everything
4452        // past this point reads and writes through `request_index` rather
4453        // than a local variable, since `request` itself is moved here.
4454        self.state.operator_fixes.push(request);
4455        self.state.save()?;
4456        let request_index = self.state.operator_fixes.len() - 1;
4457
4458        // A fresh, dedicated worktree for this one call, never the winner's
4459        // own worktree in place: that one may already be gone (folded away),
4460        // and reusing it in place would leave the branch checked out there
4461        // when the follow-up review below tries to check it out again. Freed
4462        // immediately after, either way — but only once confirmed clean:
4463        // `worktree_remove` is a `git worktree remove --force`, which would
4464        // otherwise discard uncommitted work left there by the operator or
4465        // another process before this had a chance to even look at it.
4466        if winner.worktree.exists() {
4467            // Lockfiles a rescue commit withheld stay untracked on purpose and
4468            // are already recorded; they are not the operator's work to protect.
4469            let dirty = git::git(
4470                &winner.worktree,
4471                &["status", "--porcelain", "--untracked-files=all"],
4472            )
4473            .await?;
4474            let only_withheld = dirty.lines().all(|l| {
4475                l.strip_prefix("?? ")
4476                    .is_some_and(|p| self.state.withheld.iter().any(|w| w.path == p))
4477            });
4478            if !only_withheld {
4479                bail!(
4480                    "`{}` has uncommitted changes; refusing to touch it — commit or \
4481                     discard them first",
4482                    winner.worktree.display()
4483                );
4484            }
4485            git::worktree_remove(&self.state.repo, &winner.worktree)
4486                .await
4487                .ok();
4488        }
4489        let fix_worktree = self.state.worktree_root().join("operator-fix");
4490        let fix_worktree_s = fix_worktree.to_string_lossy().to_string();
4491        git::git(
4492            &self.state.repo,
4493            &["worktree", "add", &fix_worktree_s, winner.branch.as_str()],
4494        )
4495        .await
4496        .with_context(|| format!("checking out `{}` for the fix", winner.branch))?;
4497        if !git::is_clean(&fix_worktree).await? {
4498            git::worktree_remove(&self.state.repo, &fix_worktree)
4499                .await
4500                .ok();
4501            bail!(
4502                "`{}` has uncommitted changes; refusing to start a fix on a dirty tree",
4503                winner.branch
4504            );
4505        }
4506        let repo_for_setup = self.state.repo.clone();
4507        let setup = match ensure_setup_config(&mut self.state, &repo_for_setup).await {
4508            Ok(()) => {
4509                worktree_setup::prepare(&self.state.config, &repo_for_setup, &fix_worktree).await
4510            }
4511            Err(e) => Err(e),
4512        };
4513        if let Err(e) = setup {
4514            git::worktree_remove(&self.state.repo, &fix_worktree)
4515                .await
4516                .ok();
4517            return Err(e);
4518        }
4519
4520        let run_id = self.state.id.clone();
4521        let prompts = self.state.config.prompts.clone();
4522        let language = self.state.config.graph.language.clone();
4523        let sessions = self.state.config.graph.sessions;
4524        let artifacts = agent::artifacts_dir(&self.state.dir());
4525        let finding_list: Vec<Finding> = self.state.operator_fixes[request_index]
4526            .findings
4527            .iter()
4528            .map(|f| Finding {
4529                id: f.id.clone(),
4530                severity: f.severity,
4531                file: f.file.clone(),
4532                line: f.line,
4533                title: f.title.clone(),
4534                detail: f.detail.clone(),
4535            })
4536            .collect();
4537        let fix_prompt = prompt::operator_fix(
4538            &self.state.instruction,
4539            &finding_list,
4540            reason,
4541            &stale_details,
4542            &head_at_request,
4543            &language,
4544        );
4545        let timeout = Duration::from_secs(self.state.config.graph.timeout_fix);
4546        let (job, seat, out) = self
4547            .ask_fixer(&winner, "fix", None, |spec, seat| SeatJob {
4548                prompt: fix_prompt.clone(),
4549                spec,
4550                seat,
4551                cwd: fix_worktree.clone(),
4552                timeout,
4553                allow_write: true,
4554                sessions,
4555                artifacts: artifacts.clone(),
4556                stem: "operator-fix".to_owned(),
4557                handover: None,
4558            })
4559            .await;
4560        let agent_id = seat.agent.clone();
4561
4562        let mut fix = FixRecord {
4563            agent: agent_id,
4564            addressed: Vec::new(),
4565            rejected: Vec::new(),
4566            notes: String::new(),
4567            committed: false,
4568            failed: None,
4569            duration_ms: 0,
4570            continuation: None,
4571        };
4572        let mut final_seat = seat.clone();
4573        match out {
4574            AgentOutcome::Ok(o) => {
4575                fix.duration_ms = o.duration_ms;
4576                let parsed = verdict::extract_json::<FixReport>(&o.text);
4577                let incomplete_reason = match &parsed {
4578                    Ok(_) if has_unconfirmed_command(&o.commands) => Some(
4579                        "the reply parsed, but it reported a command whose own CLI \
4580                         never confirmed an exit status"
4581                            .to_owned(),
4582                    ),
4583                    Ok(_) => None,
4584                    Err(e) => Some(e.to_string()),
4585                };
4586                match incomplete_reason {
4587                    None => {
4588                        let report = parsed.expect("checked Ok above");
4589                        fix.addressed = report.addressed;
4590                        fix.rejected = report.rejected;
4591                        fix.notes = blind::sanitize_prose(&report.notes, &self.state.config.blind);
4592                    }
4593                    Some(reason) => {
4594                        let (resumed_seat, resolved, failure, cont) = self
4595                            .continue_fix_report(seat, reason, &job, &prompts, &run_id, 0)
4596                            .await;
4597                        fix.duration_ms += cont.cumulative_wait_ms;
4598                        fix.continuation = Some(cont);
4599                        final_seat = resumed_seat;
4600                        match resolved {
4601                            Some(report) => {
4602                                fix.addressed = report.addressed;
4603                                fix.rejected = report.rejected;
4604                                fix.notes =
4605                                    blind::sanitize_prose(&report.notes, &self.state.config.blind);
4606                            }
4607                            None => fix.failed = failure,
4608                        }
4609                    }
4610                }
4611            }
4612            AgentOutcome::Dropped(o) => {
4613                fix.duration_ms = o.duration_ms;
4614                let why = o
4615                    .dropped
4616                    .as_ref()
4617                    .map(|d| d.why.as_str())
4618                    .unwrap_or("the CLI ended the stream without delivering its answer");
4619                fix.failed = Some(format!("the CLI dropped the stream ({why})"));
4620            }
4621            AgentOutcome::Quota(o) => {
4622                self.state.quota.push(QuotaLoss {
4623                    seat: final_seat.key.clone(),
4624                    node: "fix".to_owned(),
4625                    at: Timestamp::now(),
4626                    reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
4627                });
4628                fix.failed = Some("rate limited (quota); fixer could not run".to_owned());
4629            }
4630            AgentOutcome::Failed(e) => fix.failed = Some(e),
4631        }
4632        if fix.continuation.is_none() {
4633            fix.continuation = Some(ContinuationRecord::not_needed());
4634        }
4635        self.state.seats.insert(final_seat.key.clone(), final_seat);
4636
4637        let rescue_message = format!(
4638            "magi: operator-selected fix ({}) (uncommitted work)",
4639            self.state.operator_fixes[request_index]
4640                .findings
4641                .iter()
4642                .map(|f| f.id.as_str())
4643                .collect::<Vec<_>>()
4644                .join(", ")
4645        );
4646        if let Ok(r) = git::rescue_commit(&fix_worktree, &rescue_message).await {
4647            self.state.note_withheld("fix", &r.withheld);
4648        }
4649        let after = git::rev_parse(&fix_worktree, "HEAD").await?;
4650        fix.committed = after != head_at_request;
4651        git::worktree_remove(&self.state.repo, &fix_worktree)
4652            .await
4653            .ok();
4654
4655        self.state.event(
4656            "fix",
4657            match &fix.failed {
4658                Some(reason) => format!(
4659                    "operator fix: adoption report was lost ({reason}); {}",
4660                    if fix.committed {
4661                        "committed"
4662                    } else {
4663                        "NO new commit"
4664                    }
4665                ),
4666                None => format!(
4667                    "operator fix: {} addressed, {} rejected, {}",
4668                    fix.addressed.len(),
4669                    fix.rejected.len(),
4670                    if fix.committed {
4671                        "committed"
4672                    } else {
4673                        "NO new commit"
4674                    }
4675                ),
4676            },
4677        );
4678
4679        // Every selected finding gets an outcome — never left `Pending` once
4680        // the fixer's own turn is over. A report that never came back at all
4681        // marks every one of them `Unreported`, not silently "not addressed":
4682        // quota, a dropped stream, or an exhausted continuation are gaps in
4683        // the report, not evidence about the finding itself (see [`SCHEMA`]'s
4684        // doc for schema 9 and [`OperatorFixOutcome::Unreported`]).
4685        for f in &mut self.state.operator_fixes[request_index].findings {
4686            f.outcome = if fix.failed.is_some() {
4687                OperatorFixOutcome::Unreported
4688            } else if fix.addressed.contains(&f.id) {
4689                OperatorFixOutcome::Addressed
4690            } else if let Some(r) = fix.rejected.iter().find(|r| r.id == f.id) {
4691                OperatorFixOutcome::Rejected { why: r.why.clone() }
4692            } else {
4693                OperatorFixOutcome::Unreported
4694            };
4695        }
4696
4697        let committed = fix.committed;
4698        if committed {
4699            self.state.operator_fixes[request_index].result_head = Some(after.clone());
4700        }
4701        self.state.operator_fixes[request_index].fix = Some(fix);
4702        // Saved again now that the fixer's own outcome is final, on top of
4703        // the save right after the request was first pushed above.
4704        self.state.save()?;
4705
4706        if committed {
4707            self.state.event(
4708                "fix",
4709                format!(
4710                    "operator fix committed {}; opening a follow-up review-only run",
4711                    short(&after)
4712                ),
4713            );
4714            // The operator asked for the fix, and the follow-up serves whatever
4715            // task the run it follows served.
4716            let origin =
4717                Origin::operator().serving(self.state.origin.as_ref().and_then(|o| o.task.clone()));
4718            match Self::review(
4719                &self.state.repo,
4720                &winner.branch,
4721                self.state.config.clone(),
4722                origin,
4723            )
4724            .await
4725            {
4726                Ok(mut follow_up) => {
4727                    follow_up.state.event(
4728                        "start",
4729                        format!(
4730                            "requested by an operator fix on run {} for finding(s) {}",
4731                            self.state.id,
4732                            self.state.operator_fixes[request_index]
4733                                .findings
4734                                .iter()
4735                                .map(|f| f.id.as_str())
4736                                .collect::<Vec<_>>()
4737                                .join(", "),
4738                        ),
4739                    );
4740                    follow_up.state.save()?;
4741                    let follow_up_id = follow_up.state.id.clone();
4742                    // The follow-up is a run like any other: it belongs to the
4743                    // task of the run it follows, or to one filed for it.
4744                    let adopted = match crate::direct::adopt(
4745                        &follow_up.state,
4746                        self.state.origin.as_ref().and_then(|o| o.task.as_deref()),
4747                    ) {
4748                        Ok(a) => a,
4749                        Err(e) => {
4750                            // An ownerless run must not spend agent calls; it
4751                            // stays saved, and `magi run --resume` adopts it.
4752                            self.state.event(
4753                                "fix",
4754                                format!(
4755                                    "follow-up review {follow_up_id} got no owning task and was not executed: {e:#}"
4756                                ),
4757                            );
4758                            self.state.save()?;
4759                            return Ok(());
4760                        }
4761                    };
4762                    let executed = follow_up.execute().await;
4763                    let failure = executed.as_ref().err().map(|e| format!("{e:#}"));
4764                    if let Some(a) = adopted {
4765                        a.finish(&follow_up.state, executed);
4766                    }
4767                    if let Some(e) = failure {
4768                        self.state.event(
4769                            "fix",
4770                            format!(
4771                                "follow-up review {follow_up_id} did not complete cleanly: {e:#}"
4772                            ),
4773                        );
4774                    }
4775                    self.state.operator_fixes[request_index].follow_up_review_run =
4776                        Some(follow_up_id);
4777                }
4778                Err(e) => {
4779                    self.state.event(
4780                        "fix",
4781                        format!("committed the fix but could not open a follow-up review: {e:#}"),
4782                    );
4783                }
4784            }
4785            self.state.save()?;
4786        }
4787
4788        Ok(())
4789    }
4790
4791    // --------------------------------------------------------------- review
4792
4793    /// Ask the fixer chain once for one fix call: the agents [`fixer::attempts`]
4794    /// names, in order, each at most once, moving on only when the call
4795    /// advances (an error, a quota hit or nothing usable - the same decision
4796    /// point `agent::chain_advances` is for the other chained roles).
4797    ///
4798    /// Each agent gets its own seat from `Runner::seat`: the same agent
4799    /// continues its conversation (the winner's own implementer seat when it
4800    /// is the winner's author, now that the competition is over), another
4801    /// takes a fresh one so the full prompt is sent again. A handover is
4802    /// recorded under `node`, which is what makes the fallback stick for the
4803    /// rest of the run (see `crate::fixer`). Only the last attempt's outcome
4804    /// is returned, so an exhausted chain reads exactly like a single failed
4805    /// fixer: one quota loss, the same wording, the same refund. An earlier
4806    /// attempt's edits are left in the tree and judged, with the final
4807    /// attempt's, by what git says afterwards.
4808    ///
4809    /// Returns the job that produced the outcome, for `continue_fix_report`.
4810    async fn ask_fixer(
4811        &mut self,
4812        winner: &Candidate,
4813        node: &'static str,
4814        round: Option<usize>,
4815        build: impl Fn(AgentSpec, SeatState) -> SeatJob,
4816    ) -> (SeatJob, SeatState, AgentOutcome) {
4817        let attempts = fixer::attempts(&self.state, &self.roles, winner);
4818        self.ask_fixer_among(attempts, node, round, None, build)
4819            .await
4820    }
4821
4822    /// [`Self::ask_fixer`] over an explicit attempt list. `restore` is a
4823    /// worktree and a commit: before the next agent is asked after one that
4824    /// did not satisfy the call, the worktree is put back on that commit, so
4825    /// a failed agent's partial edits are not carried into the next one.
4826    async fn ask_fixer_among(
4827        &mut self,
4828        attempts: Vec<(AgentSpec, String)>,
4829        node: &'static str,
4830        round: Option<usize>,
4831        restore: Option<(&Path, &str)>,
4832        build: impl Fn(AgentSpec, SeatState) -> SeatJob,
4833    ) -> (SeatJob, SeatState, AgentOutcome) {
4834        let run_id = self.state.id.clone();
4835        let prompts = self.state.config.prompts.clone();
4836        let cache = self.state.config.cache_dir();
4837        let ids: Vec<String> = attempts.iter().map(|(s, _)| s.id.clone()).collect();
4838        let mut last = None;
4839        for (i, (spec, key)) in attempts.into_iter().enumerate() {
4840            let seat = self.seat(&key, &spec.id);
4841            let mut job = build(spec.clone(), seat);
4842            if i > 0 {
4843                job.stem = format!("{}-{}", job.stem, spec.id);
4844            }
4845            let ctx = WaveCtx {
4846                carry_seats: false,
4847                run: &run_id,
4848                node,
4849                prompts: &prompts,
4850                cache: cache.as_deref(),
4851                round,
4852            };
4853            let (seat, out) =
4854                run_one(job.clone(), Arc::clone(&self.sem), &ctx, &mut self.state, 0).await;
4855            let advances = match &out {
4856                AgentOutcome::Ok(o) => agent::output_advances(o),
4857                _ => true,
4858            };
4859            if let Some(next) = ids.get(i + 1)
4860                && advances
4861            {
4862                self.state.seats.insert(seat.key.clone(), seat.clone());
4863                let class =
4864                    FailClass::of(&out).unwrap_or_else(|| FailClass::Other("unusable".to_owned()));
4865                let reason = match &out {
4866                    AgentOutcome::Ok(_) => "nothing usable".to_owned(),
4867                    other => fail_reason(other),
4868                };
4869                record_handover(
4870                    &mut self.state,
4871                    node,
4872                    &seat.key,
4873                    &spec.id,
4874                    next,
4875                    &class,
4876                    &reason,
4877                );
4878                // A failed agent's edits must not reach the next one, and a
4879                // restore that did not work ends the chain: the call fails
4880                // and the caller's own (confirmed) restore decides.
4881                if let Some((wt, rev)) = restore
4882                    && let Err(e) = git::restore_to(wt, rev).await
4883                {
4884                    let why =
4885                        format!("could not restore the worktree before the next fixer: {e:#}");
4886                    tracing::warn!("{why}");
4887                    last = Some((job, seat, AgentOutcome::Failed(why)));
4888                    break;
4889                }
4890                continue;
4891            }
4892            last = Some((job, seat, out));
4893            break;
4894        }
4895        last.expect("the fixer chain always has an entry")
4896    }
4897
4898    async fn review_loop(&mut self) -> Result<()> {
4899        // A base that would not rebase is a person's decision, not a review
4900        // round: nothing here would change the answer, and reviewers and a
4901        // fixer would be spending real budget on a tree that cannot land
4902        // regardless of what they find.
4903        if self
4904            .state
4905            .base_sync
4906            .as_ref()
4907            .is_some_and(|s| s.conflict.is_some())
4908        {
4909            return Ok(());
4910        }
4911        // Attribution for every agent this node spawns: `MAGI_RUN` lets a task the
4912        // agent files with `magi task add` name the run that paid for it. The
4913        // prompt overlay is cloned alongside it because the waves borrow it
4914        // while `self` is mutably borrowed by the node's own bookkeeping.
4915        let run_id = self.state.id.clone();
4916        let prompts = self.state.config.prompts.clone();
4917        let Some(winner) = self.state.winner().cloned() else {
4918            return Ok(());
4919        };
4920        let max_rounds = self.state.config.graph.review_rounds;
4921        // A clean round, an exhausted round budget, or a stalled tree (see
4922        // `STAGNANT_LIMIT`) are all already-decided conclusions the moment
4923        // they are recorded — recomputed here, not read off `status`, so a
4924        // reentry into a run that already stopped restates the identical
4925        // verdict instead of silently handing back whatever an earlier node
4926        // in this same walk clobbered `status` to (a solo-candidate
4927        // `judge`/`deliberate` skip rewrites it on every reentry). The loop
4928        // below runs an empty range once the budget is spent, and would
4929        // otherwise fall through without touching `status` at all.
4930        if let Some(status) = review_conclusion(&self.state.reviews, max_rounds) {
4931            // A reentry after a crash between the last round's save and
4932            // `stop_reviewing` reaches the hand-off here, not there.
4933            if status == RunStatus::Gating {
4934                self.record_contested_handoff();
4935            }
4936            self.state.status = status;
4937            self.state.save()?;
4938            return Ok(());
4939        }
4940        self.state.status = RunStatus::Reviewing;
4941        // A last recorded round whose own verification never resolved
4942        // (`ResourceBlocked` — the shared build cache, not the patch) is
4943        // never a concluded round, whatever the round budget says: starting
4944        // a fresh round on top of it would spend a whole new reviewer wave
4945        // re-reading an unchanged patch instead of just retrying the one
4946        // check that actually needs it, and once the budget is spent the
4947        // loop below has nothing left to do at all (its range is empty).
4948        // Retry that check directly instead, exactly the same retry
4949        // `stop_reviewing` already does for its own catch-up case.
4950        if self
4951            .state
4952            .reviews
4953            .last()
4954            .is_some_and(|r| r.e2e_status() == E2eStatus::ResourceBlocked)
4955        {
4956            let shell = self.state.config.shell();
4957            return self
4958                .stop_reviewing(
4959                    "the last round's own verification never resolved",
4960                    &shell,
4961                    &winner.worktree,
4962                )
4963                .await;
4964        }
4965
4966        let repo = self.state.repo.clone();
4967        let root = self.state.worktree_root();
4968        let language = self.state.config.graph.language.clone();
4969        let sessions = self.state.config.graph.sessions;
4970        let artifacts = agent::artifacts_dir(&self.state.dir());
4971        let base = self.landing_base();
4972        let base_short = short(&base);
4973        let reviewers = self.roles.reviewers.clone();
4974        let shell = self.state.config.shell();
4975
4976        for round in (self.state.reviews.len() + 1)..=max_rounds {
4977            let head = git::rev_parse(&winner.worktree, "HEAD").await?;
4978            let patch = git::diff(&winner.worktree, &base, "HEAD").await?;
4979            let stat = git::diff_stat(&winner.worktree, &base, "HEAD").await?;
4980            // The prior round's own record, already persisted — never a
4981            // hand-carried variable of just its failing output: that is
4982            // exactly what let a round's e2e result drift out of sync with
4983            // which commit it was actually about (see `SCHEMA`'s doc for
4984            // schema 8). Judged against `head`, the commit reviewers are
4985            // about to look at now, so the summary always reads as "an
4986            // earlier head" here — this round's own patch has not been
4987            // checked yet.
4988            let prev_verification = self
4989                .state
4990                .reviews
4991                .last()
4992                .and_then(|r| r.verification_summary(&head));
4993
4994            // Each reviewer gets its own detached checkout of exactly this
4995            // commit: nobody can perturb the winner's tree, and the fixer can
4996            // keep working without racing a reviewer.
4997            let mut jobs = Vec::new();
4998            for (r, spec) in reviewers.iter().cloned().enumerate() {
4999                let wt = root.join(format!("review-{}", r + 1));
5000                // `clean -fdx` in a reset wipes setup's products, so every
5001                // round sets the seat up again.
5002                if wt.exists() {
5003                    git::reset_detached(&wt, &head).await?;
5004                } else {
5005                    git::worktree_add_detached(&repo, &wt, &head).await?;
5006                }
5007                self.setup_seat_worktree(&repo, &wt, false).await?;
5008                let seat_key = format!("review-{}", r + 1);
5009                // The seat starts the round on whoever answered it last, not
5010                // on the agent the spec names, so a failure is not re-paid.
5011                let spec = pick_start_spec(
5012                    &self.roles.reviewer_roster,
5013                    spec,
5014                    self.state.seat_history.get(&seat_key),
5015                );
5016                let seat = self.seat(&seat_key, &spec.id);
5017                jobs.push(SeatJob {
5018                    prompt: prompt::review(&prompt::ReviewCtx {
5019                        instruction: &self.state.instruction,
5020                        branch: &winner.branch,
5021                        base_short: &base_short,
5022                        stat: &stat,
5023                        patch: &patch,
5024                        verification: prev_verification.as_ref(),
5025                        reviewers: reviewers.len(),
5026                        round,
5027                        rounds: max_rounds,
5028                        // A review-only run has no rankings, so nothing
5029                        // competed for this patch and the reviewer is told so.
5030                        competed: self.state.tally.as_ref().is_some_and(|t| t.rankings > 0),
5031                        lens: Lens::for_seat(r),
5032                        language: &language,
5033                    }),
5034                    spec,
5035                    seat,
5036                    cwd: wt,
5037                    timeout: Duration::from_secs(self.state.config.graph.timeout_review),
5038                    allow_write: false,
5039                    sessions,
5040                    artifacts: artifacts.clone(),
5041                    stem: format!("review-{round}-{}", r + 1),
5042                    handover: None,
5043                });
5044            }
5045
5046            self.state.event(
5047                "review",
5048                format!(
5049                    "round {round}: {} reviewers on {}",
5050                    jobs.len(),
5051                    short(&head)
5052                ),
5053            );
5054            let mut quota_losses = Vec::new();
5055            let review_retries = self.state.config.graph.retries;
5056            let review_cache = self.state.config.cache_dir();
5057            let ctx = WaveCtx {
5058                carry_seats: true,
5059                run: &run_id,
5060                node: "review",
5061                prompts: &prompts,
5062                cache: review_cache.as_deref(),
5063                round: Some(round),
5064            };
5065            let results = ask_json_wave::<Review>(
5066                jobs,
5067                Arc::clone(&self.sem),
5068                review_retries,
5069                &self.roles.reviewer_roster,
5070                &ctx,
5071                &mut quota_losses,
5072                &mut self.state,
5073                &|_: &Review| Ok(()),
5074            )
5075            .await;
5076            // Counted before the move below: how many of *this* round's
5077            // reviewer seats were lost to their own rate limit, as opposed to
5078            // a crash, a timeout, or unparsable output — see `round_is_clean`.
5079            let round_quota_missing = quota_losses.len();
5080            self.state.quota.extend(quota_losses);
5081
5082            let mut records = Vec::new();
5083            let mut all_findings = Vec::new();
5084            for (r, (seat, res, attempts)) in results.into_iter().enumerate() {
5085                let agent_id = seat.agent.clone();
5086                self.state.seats.insert(seat.key.clone(), seat);
5087                let mut record = ReviewRecord {
5088                    reviewer: r + 1,
5089                    agent: agent_id,
5090                    summary: String::new(),
5091                    findings: Vec::new(),
5092                    vote: None,
5093                    failed: None,
5094                    duration_ms: 0,
5095                    // Set for both outcomes: `failed: Some(_)` with
5096                    // `attempts > 0` is a seat every retry still lost, not a
5097                    // recovered one — only `failed: None` with `attempts > 0`
5098                    // reads as "answered after a nudge" (see this field's own
5099                    // doc).
5100                    attempts,
5101                };
5102                match res {
5103                    Ok((review, out)) => {
5104                        // Sanitized here, at the point every other piece of
5105                        // agent prose in this file is (candidate summaries,
5106                        // deliberation turns, vote reasons): a reviewer's own
5107                        // words are the one thing about it that could name
5108                        // it, and reconsideration below broadcasts this same
5109                        // summary and these same findings to every other
5110                        // seat on the panel.
5111                        record.summary =
5112                            blind::sanitize_prose(&review.summary, &self.state.config.blind);
5113                        record.vote = Some(review.vote);
5114                        record.duration_ms = out.duration_ms;
5115                        for (n, mut f) in review.findings.into_iter().enumerate() {
5116                            // ids are magi's, never the agent's: the fixer's
5117                            // adoption report is keyed by them.
5118                            f.id = format!("R{round}-{}-{}", r + 1, n + 1);
5119                            f.title = blind::sanitize_prose(&f.title, &self.state.config.blind);
5120                            f.detail = blind::sanitize_prose(&f.detail, &self.state.config.blind);
5121                            // `file` is agent-supplied prose too, never
5122                            // checked against the real tree — the same
5123                            // exposure `title`/`detail` above have, just in
5124                            // a field easy to forget because it looks like a
5125                            // path rather than free text.
5126                            f.file = f
5127                                .file
5128                                .map(|file| blind::sanitize_prose(&file, &self.state.config.blind));
5129                            all_findings.push(f.clone());
5130                            record.findings.push(f);
5131                        }
5132                        self.state.event(
5133                            "review",
5134                            format!(
5135                                "round {round}: reviewer {} voted {} with {} finding(s)",
5136                                r + 1,
5137                                review.vote.label(),
5138                                record.findings.len()
5139                            ),
5140                        );
5141                    }
5142                    Err(e) => {
5143                        record.failed = Some(e.to_string());
5144                        self.state.event(
5145                            "review",
5146                            format!("round {round}: reviewer {} produced nothing: {e}", r + 1),
5147                        );
5148                    }
5149                }
5150                records.push(record);
5151            }
5152
5153            // Tally the round's votes and, if they split, spend the one
5154            // round of reconsideration the split -> deliberate -> revote
5155            // shape `judge`/`vote` use for the panel, sized down to what a
5156            // read-only review round can afford: one round, and a revote
5157            // rather than an argument, because the panel already wrote its
5158            // reasoning down as findings the first time around.
5159            let initial_votes: Vec<ReviewVote> = records.iter().filter_map(|r| r.vote).collect();
5160            let vote_split =
5161                initial_votes.len() > 1 && !initial_votes.iter().all(|v| *v == initial_votes[0]);
5162            let mut reconsideration: Vec<ReviewRevoteRecord> = Vec::new();
5163            if vote_split {
5164                self.state.event(
5165                    "review",
5166                    format!(
5167                        "round {round}: votes split ({}) — one round of reconsideration",
5168                        initial_votes
5169                            .iter()
5170                            .map(|v| v.label())
5171                            .collect::<Vec<_>>()
5172                            .join(", ")
5173                    ),
5174                );
5175                // Seats read every seat's findings and votes, still numbered
5176                // and never named — the same anonymity `review` itself keeps.
5177                let panel: Vec<ReviewSeatReport<'_>> = records
5178                    .iter()
5179                    .filter_map(|r| {
5180                        r.vote.map(|vote| ReviewSeatReport {
5181                            reviewer: r.reviewer,
5182                            vote,
5183                            summary: &r.summary,
5184                            findings: &r.findings,
5185                        })
5186                    })
5187                    .collect();
5188
5189                let mut jobs = Vec::new();
5190                let mut seats_at = Vec::new();
5191                for (r, spec) in reviewers.iter().cloned().enumerate() {
5192                    // A seat with no initial vote has nothing to reconsider
5193                    // from and stays absent, the same as it stayed absent
5194                    // from `panel` above.
5195                    if records[r].vote.is_none() {
5196                        continue;
5197                    }
5198                    let wt = root.join(format!("review-{}", r + 1));
5199                    let seat_key = format!("review-{}", r + 1);
5200                    let spec = self.occupant(&seat_key, spec);
5201                    let seat = self.seat(&seat_key, &spec.id);
5202                    // A seat with no live session has already forgotten the
5203                    // initial review's prompt — restate the patch it is
5204                    // voting on, the same as `deliberate`/`vote` do for a
5205                    // judge in the same position.
5206                    // The panel already carries this seat's own review and
5207                    // vote, so restating the patch makes the prompt whole for
5208                    // a seat handed to another agent.
5209                    let build = |with_patch: bool| {
5210                        prompt::review_reconsider(&ReviewReconsiderCtx {
5211                            instruction: &self.state.instruction,
5212                            reviewer: r + 1,
5213                            lens: Lens::for_seat(r),
5214                            panel: &panel,
5215                            patch: with_patch.then_some(ReviewPatch {
5216                                branch: &winner.branch,
5217                                base_short: &base_short,
5218                                stat: &stat,
5219                                patch: &patch,
5220                            }),
5221                            round,
5222                            rounds: max_rounds,
5223                            language: &language,
5224                        })
5225                    };
5226                    let full = build(true);
5227                    let prompt = if has_context(&spec, &seat, sessions) {
5228                        build(false)
5229                    } else {
5230                        full.clone()
5231                    };
5232                    jobs.push(SeatJob {
5233                        prompt,
5234                        spec,
5235                        seat,
5236                        cwd: wt,
5237                        timeout: Duration::from_secs(self.state.config.graph.timeout_review),
5238                        allow_write: false,
5239                        sessions,
5240                        artifacts: artifacts.clone(),
5241                        stem: format!("review-{round}-reconsider-{}", r + 1),
5242                        handover: Some(full),
5243                    });
5244                    seats_at.push(r);
5245                }
5246
5247                let mut recon_quota_losses = Vec::new();
5248                let recon_cache = self.state.config.cache_dir();
5249                let recon_ctx = WaveCtx {
5250                    carry_seats: true,
5251                    run: &run_id,
5252                    node: "review",
5253                    prompts: &prompts,
5254                    cache: recon_cache.as_deref(),
5255                    round: Some(round),
5256                };
5257                let recon_results = ask_json_wave::<ReviewRevote>(
5258                    jobs,
5259                    Arc::clone(&self.sem),
5260                    review_retries,
5261                    &self.roles.reviewer_roster,
5262                    &recon_ctx,
5263                    &mut recon_quota_losses,
5264                    &mut self.state,
5265                    &|_: &ReviewRevote| Ok(()),
5266                )
5267                .await;
5268                self.state.quota.extend(recon_quota_losses);
5269
5270                for (&r, (seat, res, _attempts)) in seats_at.iter().zip(recon_results) {
5271                    let agent_id = seat.agent.clone();
5272                    self.state.seats.insert(seat.key.clone(), seat);
5273                    let mut rec = ReviewRevoteRecord {
5274                        reviewer: r + 1,
5275                        agent: agent_id,
5276                        vote: None,
5277                        reason: String::new(),
5278                        failed: None,
5279                    };
5280                    match res {
5281                        Ok((rv, _)) => {
5282                            rec.vote = Some(rv.vote);
5283                            rec.reason =
5284                                blind::sanitize_prose(&rv.reason, &self.state.config.blind);
5285                            self.state.event(
5286                                "review",
5287                                format!(
5288                                    "round {round}: reviewer {} revoted {}",
5289                                    r + 1,
5290                                    rv.vote.label()
5291                                ),
5292                            );
5293                        }
5294                        Err(e) => {
5295                            rec.failed = Some(e.to_string());
5296                            self.state.event(
5297                                "review",
5298                                format!("round {round}: reviewer {} did not revote: {e}", r + 1),
5299                            );
5300                        }
5301                    }
5302                    reconsideration.push(rec);
5303                }
5304            } else if initial_votes.len() > 1 {
5305                self.state.event(
5306                    "review",
5307                    format!(
5308                        "round {round}: votes agreed ({}) — no reconsideration",
5309                        initial_votes[0].label()
5310                    ),
5311                );
5312            }
5313
5314            let blocking = all_findings.iter().filter(|f| f.severity.blocks()).count();
5315            let verify_timeout = Duration::from_secs(self.state.config.graph.verify_timeout());
5316            // A round that already has a blocking finding and a round left to
5317            // try is going back to the fixer no matter what `verify.e2e`
5318            // says, so running it first only spends the loop's slowest step
5319            // (minutes, for a Rust repo's full test suite) on a head about
5320            // to be rewritten. Deferred, never skipped: `verify.e2e` still
5321            // runs once a round has no blocking findings left (see
5322            // `round_is_clean`, which a deferred — empty — `e2e` can never
5323            // satisfy since `blocking` is nonzero whenever this branch is
5324            // taken), and `stop_reviewing` forces a real run before it will
5325            // ever read a deferred round as green.
5326            let defer_e2e =
5327                blocking > 0 && round < max_rounds && !self.state.config.graph.e2e_every_round;
5328            let (e2e, verify_retried, e2e_deferred, e2e_defer_reason) = if defer_e2e {
5329                let reason =
5330                    format!("{blocking} blocking finding(s) already required a fix this round");
5331                self.state.event(
5332                    "verify",
5333                    format!(
5334                        "round {round}: {reason} — e2e deferred to the fixer (reviewed head \
5335                         {}); it will run once a round has none left",
5336                        short(&head)
5337                    ),
5338                );
5339                (Vec::new(), false, true, Some(reason))
5340            } else {
5341                let e2e_commands = self.state.config.verify.e2e.clone();
5342                let cache_dir = self.state.config.cache_dir();
5343                let context = format!("round {round}");
5344                let (e2e, verify_retried) = with_cache_lease(
5345                    &mut self.state,
5346                    cache_dir.as_deref(),
5347                    "e2e",
5348                    "e2e",
5349                    &winner.worktree,
5350                    &head,
5351                    verify_timeout,
5352                    &context,
5353                    |state, budget| {
5354                        let shell = shell.clone();
5355                        let e2e_commands = e2e_commands.clone();
5356                        let worktree = winner.worktree.clone();
5357                        let context = context.clone();
5358                        async move {
5359                            run_e2e_with_retry(
5360                                state,
5361                                &shell,
5362                                &e2e_commands,
5363                                &worktree,
5364                                budget,
5365                                &context,
5366                            )
5367                            .await
5368                        }
5369                    },
5370                )
5371                .await;
5372                (e2e, verify_retried, false, None)
5373            };
5374
5375            let expected = records.len();
5376            let answered = records.iter().filter(|r| r.failed.is_none()).count();
5377            let incomplete = answered < expected;
5378            let e2e_ok = e2e.iter().all(CommandOutcome::ok);
5379            let policy = self.state.config.graph.incomplete_review;
5380            let clean = round_is_clean(
5381                blocking,
5382                e2e_ok,
5383                answered,
5384                expected,
5385                round_quota_missing,
5386                policy,
5387            );
5388
5389            let mut round_record = ReviewRound {
5390                round,
5391                head: head.clone(),
5392                verified_head: None,
5393                verified_at: None,
5394                reviews: records,
5395                e2e,
5396                verify_retried,
5397                e2e_deferred,
5398                e2e_defer_reason,
5399                fix: None,
5400                blocking,
5401                answered,
5402                expected,
5403                clean,
5404                progressed: false,
5405                vote_split,
5406                reconsideration,
5407                verdict: None,
5408            };
5409            // The final vote per seat is its revote where reconsideration
5410            // ran and answered, its initial vote otherwise — the same
5411            // fallback `tally` uses for a judge whose private vote failed.
5412            round_record.verdict = ReviewVote::worst(
5413                round_record
5414                    .final_votes()
5415                    .into_iter()
5416                    .map(|(_, _, vote)| vote),
5417            );
5418            // Which commit and when magi actually attempted to check —
5419            // known the moment a command was dispatched against `head`,
5420            // whether or not it finished: a resource-blocked attempt still
5421            // targeted a specific commit at a specific time, and leaving
5422            // that unrecorded is exactly what made `verification_summary`
5423            // report a fresh attempt as "commit unknown ... recorded before
5424            // this was tracked", indistinguishable from a genuinely old,
5425            // untracked record. Only a deferred or unconfigured round never
5426            // ran at all and has nothing to record — see
5427            // `ReviewRound::verified_head`'s own doc.
5428            if !matches!(
5429                round_record.e2e_status(),
5430                E2eStatus::Deferred | E2eStatus::NotConfigured
5431            ) {
5432                round_record.verified_head = Some(head.clone());
5433                round_record.verified_at = Some(Timestamp::now());
5434            }
5435            let this_round_verification = round_record.verification_summary(&head);
5436
5437            if incomplete {
5438                let missing: Vec<String> = round_record
5439                    .reviews
5440                    .iter()
5441                    .filter(|r| r.failed.is_some())
5442                    .map(|r| format!("review-{}", r.reviewer))
5443                    .collect();
5444                self.state.event(
5445                    "review",
5446                    format!(
5447                        "round {round}: {answered}/{expected} reviewer(s) answered ({} never answered)",
5448                        missing.join(", ")
5449                    ),
5450                );
5451            }
5452
5453            if clean {
5454                self.state.event(
5455                    "review",
5456                    if incomplete && policy == IncompleteReviewPolicy::Warn {
5457                        format!(
5458                            "round {round}: clean (warn policy, incomplete panel) — no \
5459                             blocking findings from the seats that answered, verification green"
5460                        )
5461                    } else if incomplete {
5462                        format!(
5463                            "round {round}: clean ({} rate-limited reviewer(s) excluded from \
5464                             quorum) — no blocking findings from the seats that answered, \
5465                             verification green",
5466                            expected - answered
5467                        )
5468                    } else {
5469                        format!("round {round}: clean — no blocking findings, verification green")
5470                    },
5471                );
5472                self.state.reviews.push(round_record);
5473                self.state.status = RunStatus::Gating;
5474                self.state.save()?;
5475                return Ok(());
5476            }
5477
5478            // Nothing was raised and verification passed, but not every seat
5479            // answered and `round_is_clean` still refused to call it clean —
5480            // either a seat is missing for a reason other than its own quota
5481            // (a crash, a timeout, unparsable output — worth another try), or
5482            // every seat that could have answered lost its quota and nobody
5483            // is left to decide on: re-review rather than send the fixer
5484            // after a round with nothing to fix.
5485            if incomplete && blocking == 0 && e2e_ok {
5486                self.state.reviews.push(round_record);
5487                self.state.save()?;
5488                if round == max_rounds {
5489                    self.state.status = RunStatus::Blocked;
5490                    self.state.event(
5491                        "review",
5492                        format!(
5493                            "{} reviewer seat(s) never answered after {max_rounds} rounds; \
5494                             refusing to call it clean",
5495                            expected - answered
5496                        ),
5497                    );
5498                    return Ok(());
5499                }
5500                continue;
5501            }
5502
5503            // Nothing for the fixer to act on (`blocking == 0`) and the only
5504            // reason this round is not clean is that magi itself never got
5505            // a command to run — the shared build cache, not the patch (see
5506            // `CommandOutcome::resource_blocked`'s own doc). Sending that to
5507            // the fixer would invite a change to appease contention that has
5508            // nothing to do with the diff, and would leave this attempt
5509            // sitting in the next round's prompt as if it were about an
5510            // earlier, superseded commit rather than what it actually is:
5511            // the same head, still waiting to be checked. Wait for it the
5512            // same way the final round's own contention is already handled,
5513            // whatever round this happens to be.
5514            if blocking == 0 && round_record.e2e_status() == E2eStatus::ResourceBlocked {
5515                self.state.reviews.push(round_record);
5516                return self
5517                    .stop_reviewing(
5518                        "the round's own verification could not run",
5519                        &shell,
5520                        &winner.worktree,
5521                    )
5522                    .await;
5523            }
5524
5525            if round == max_rounds {
5526                self.state.reviews.push(round_record);
5527                return self
5528                    .stop_reviewing(
5529                        &format!(
5530                            "{blocking} blocking finding(s) still open after {max_rounds} round(s)"
5531                        ),
5532                        &shell,
5533                        &winner.worktree,
5534                    )
5535                    .await;
5536            }
5537
5538            // Fix. The winner's own implementer seat continues its conversation:
5539            // the competition is over, so context is pure benefit now.
5540            let blocking_findings: Vec<_> = all_findings
5541                .iter()
5542                .filter(|f| f.severity.blocks())
5543                .cloned()
5544                .collect();
5545            let fix_prompt = prompt::fix(
5546                &self.state.instruction,
5547                &blocking_findings,
5548                this_round_verification.as_ref(),
5549                round,
5550                max_rounds,
5551                &language,
5552            );
5553            let timeout = Duration::from_secs(self.state.config.graph.timeout_fix);
5554            let before = git::rev_parse(&winner.worktree, "HEAD").await?;
5555            let (job, seat, out) = self
5556                .ask_fixer(&winner, "fix", Some(round), |spec, seat| SeatJob {
5557                    prompt: fix_prompt.clone(),
5558                    spec,
5559                    seat,
5560                    cwd: winner.worktree.clone(),
5561                    timeout,
5562                    allow_write: true,
5563                    sessions,
5564                    artifacts: artifacts.clone(),
5565                    stem: format!("fix-{round}"),
5566                    handover: None,
5567                })
5568                .await;
5569            let agent_id = seat.agent.clone();
5570
5571            let mut fix = FixRecord {
5572                agent: agent_id,
5573                addressed: Vec::new(),
5574                rejected: Vec::new(),
5575                notes: String::new(),
5576                committed: false,
5577                failed: None,
5578                duration_ms: 0,
5579                continuation: None,
5580            };
5581            let mut continuation = ContinuationRecord::not_needed();
5582            let mut final_seat = seat.clone();
5583            match out {
5584                AgentOutcome::Ok(o) => {
5585                    fix.duration_ms = o.duration_ms;
5586                    let parsed = verdict::extract_json::<FixReport>(&o.text);
5587                    // A parsed report standing next to a command this same
5588                    // reply's own CLI never confirmed the exit status of is
5589                    // not a resolved answer — the identical `CommandEvidence`
5590                    // `state.jobs` renders, read here instead of only on
5591                    // display, per the completion judgment and the shown
5592                    // record needing to agree.
5593                    let incomplete_reason = match &parsed {
5594                        Ok(_) if has_unconfirmed_command(&o.commands) => Some(
5595                            "the reply parsed, but it reported a command whose own CLI never \
5596                             confirmed an exit status"
5597                                .to_owned(),
5598                        ),
5599                        Ok(_) => None,
5600                        Err(e) => Some(e.to_string()),
5601                    };
5602                    match incomplete_reason {
5603                        None => {
5604                            let report = parsed.expect("checked Ok above");
5605                            fix.addressed = report.addressed;
5606                            fix.rejected = report.rejected;
5607                            fix.notes =
5608                                blind::sanitize_prose(&report.notes, &self.state.config.blind);
5609                        }
5610                        Some(reason) => {
5611                            let (resumed_seat, resolved, failure, cont) = self
5612                                .continue_fix_report(seat, reason, &job, &prompts, &run_id, round)
5613                                .await;
5614                            fix.duration_ms += cont.cumulative_wait_ms;
5615                            continuation = cont;
5616                            final_seat = resumed_seat;
5617                            match resolved {
5618                                Some(report) => {
5619                                    fix.addressed = report.addressed;
5620                                    fix.rejected = report.rejected;
5621                                    fix.notes = blind::sanitize_prose(
5622                                        &report.notes,
5623                                        &self.state.config.blind,
5624                                    );
5625                                }
5626                                None => fix.failed = failure,
5627                            }
5628                        }
5629                    }
5630                }
5631                // The CLI's raw error JSON is not a fix report to parse.
5632                AgentOutcome::Dropped(o) => {
5633                    fix.duration_ms = o.duration_ms;
5634                    let why = o
5635                        .dropped
5636                        .as_ref()
5637                        .map(|d| d.why.as_str())
5638                        .unwrap_or("the CLI ended the stream without delivering its answer");
5639                    fix.failed = Some(format!("the CLI dropped the stream ({why})"));
5640                }
5641                AgentOutcome::Quota(o) => {
5642                    self.state.quota.push(QuotaLoss {
5643                        seat: final_seat.key.clone(),
5644                        node: "fix".to_owned(),
5645                        at: Timestamp::now(),
5646                        reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
5647                    });
5648                    fix.failed = Some("rate limited (quota); fixer could not run".to_owned());
5649                }
5650                AgentOutcome::Failed(e) => fix.failed = Some(e),
5651            }
5652            fix.continuation = Some(continuation);
5653            self.state.seats.insert(final_seat.key.clone(), final_seat);
5654            if let Ok(r) = git::rescue_commit(
5655                &winner.worktree,
5656                &format!("magi: review round {round} fixes (uncommitted work)"),
5657            )
5658            .await
5659            {
5660                self.state.note_withheld("fix", &r.withheld);
5661            }
5662            let after = git::rev_parse(&winner.worktree, "HEAD").await?;
5663            fix.committed = after != before;
5664            // Judged by what `git` says moved against base, never by the
5665            // fixer's own `addressed`/`rejected` count — see
5666            // `ReviewRound::progressed`. Propagated with `?`, the same as the
5667            // `patch` snapshot above: swallowing this error would default
5668            // `diff_after` to empty, which almost always differs from a
5669            // non-empty `patch` and reads as "progressed" — exactly backwards
5670            // for a `git` failure the stagnation check cannot see through.
5671            let diff_after = git::diff(&winner.worktree, &base, "HEAD").await?;
5672            let progressed = diff_after != patch;
5673            let commit_note = if fix.committed {
5674                "committed"
5675            } else {
5676                "NO new commit"
5677            };
5678            let tree_note = if progressed {
5679                "changed vs base"
5680            } else {
5681                "unchanged vs base"
5682            };
5683            self.state.event(
5684                "fix",
5685                match &fix.failed {
5686                    // Distinct on purpose from "0 addressed, 0 rejected": the
5687                    // fixer's own diff still landed (blocking counts do keep
5688                    // falling round over round), only its adoption report did
5689                    // not come back, so this must never read like every
5690                    // finding was reviewed and declined.
5691                    Some(reason) => {
5692                        format!(
5693                            "round {round}: fixer's adoption report was lost ({reason}); \
5694                             {commit_note}, tree {tree_note}"
5695                        )
5696                    }
5697                    None => format!(
5698                        "round {round}: {} addressed, {} rejected, {commit_note}, tree \
5699                         {tree_note}{}",
5700                        fix.addressed.len(),
5701                        fix.rejected.len(),
5702                        if continuation.outcome == ContinuationOutcome::Resumed {
5703                            format!(
5704                                " (adoption report recovered after {} continuation(s))",
5705                                continuation.attempts
5706                            )
5707                        } else {
5708                            String::new()
5709                        },
5710                    ),
5711                },
5712            );
5713            round_record.fix = Some(fix);
5714            round_record.progressed = progressed;
5715            self.state.reviews.push(round_record);
5716            self.state.save()?;
5717
5718            // The fixer's own report never came back this round, even after
5719            // `continue_fix_report`'s own budget was spent on it — not an
5720            // ordinary "no report" (dropped stream, quota, plain failure),
5721            // which already reads that way and is left to the existing round
5722            // budget. Stopping here, rather than opening another round, is
5723            // what keeps a next reviewer/fixer wave from ever being
5724            // dispatched onto `winner.worktree` while whatever the seat's
5725            // last call may still have running there is unaccounted for: no
5726            // process liveness check exists (and none is being added — see
5727            // AGENTS.md/this task's own scope), so the only way to honour
5728            // "nothing starts before a valid report returns" is to not start
5729            // anything further on this worktree from this run at all.
5730            if matches!(
5731                continuation.outcome,
5732                ContinuationOutcome::Exhausted
5733                    | ContinuationOutcome::QuotaLost
5734                    | ContinuationOutcome::NoSession
5735            ) {
5736                return self
5737                    .stop_reviewing(
5738                        "the fixer's adoption report never came back, even after resuming its \
5739                         own seat; refusing to start another round against the same worktree \
5740                         while that is unresolved",
5741                        &shell,
5742                        &winner.worktree,
5743                    )
5744                    .await;
5745            }
5746
5747            let streak = self
5748                .state
5749                .reviews
5750                .iter()
5751                .rev()
5752                .take_while(|r| !r.progressed)
5753                .count();
5754            if streak >= STAGNANT_LIMIT {
5755                return self
5756                    .stop_reviewing(
5757                        &format!(
5758                            "the tree has not moved against base for {streak} round(s) in a row"
5759                        ),
5760                        &shell,
5761                        &winner.worktree,
5762                    )
5763                    .await;
5764            }
5765        }
5766        Ok(())
5767    }
5768
5769    /// Decide, from the last recorded round's own verification, whether
5770    /// stopping the review loop is a hand-off or a genuine block.
5771    ///
5772    /// Called once the loop has given up trying — the round budget is spent,
5773    /// or the tree stopped moving (see [`STAGNANT_LIMIT`]) — with blocking
5774    /// findings still open, never while a round is still clean or the
5775    /// incomplete-panel case handled inline above. Gate and e2e are facts
5776    /// about the tree; a lingering review finding is an opinion, and this
5777    /// workload's own `magi stats` puts reviewer precision low enough
5778    /// (12-33%, 0.18-0.29 adopted per round) that a panel of open findings
5779    /// must not by itself stand between a green, verified change and the
5780    /// human who decides what to do with it. A red e2e is not an opinion, so
5781    /// that case still blocks, with the failing command and a tail of its
5782    /// output recorded here rather than left in `run.json` for someone to go
5783    /// find.
5784    ///
5785    /// A round that deferred its own e2e (see [`Config::graph`]'s
5786    /// `e2e_every_round`) is never read as that green: its `e2e` is empty
5787    /// only because nothing ran, and treating an empty list as a passing one
5788    /// here is exactly the "deferred painted green" bug this function exists
5789    /// to not have. When the last round's own verification never resolved —
5790    /// deferred on purpose, or a real attempt the shared build cache blocked
5791    /// — this makes (or retries) the real run, on the actual worktree this
5792    /// loop is about to stop touching, before deciding anything. A
5793    /// resource-blocked attempt is likewise never read as either green or
5794    /// red: it is evidence about the machine, not the patch (see
5795    /// [`CommandOutcome::resource_blocked`]'s own doc), so a persistently
5796    /// blocked cache leaves this call without deciding rather than guessing
5797    /// — the caller retries on a later reentry.
5798    /// Record, once, that the review loop handed off over a blocking finding
5799    /// a reviewer rejected on (see [`ReviewRound::contested_handoff`]), so
5800    /// `land` asks the owner even with `land_approval` off. Called from every
5801    /// path that concludes `Gating`; a reentry keeps the first record.
5802    fn record_contested_handoff(&mut self) {
5803        if self.state.contested_handoff.is_some() {
5804            return;
5805        }
5806        let Some(contested) = self
5807            .state
5808            .reviews
5809            .last()
5810            .and_then(ReviewRound::contested_handoff)
5811        else {
5812            return;
5813        };
5814        self.state.event(
5815            "review",
5816            format!(
5817                "{} blocking finding(s) open and {} reviewer(s) rejecting — the merge will \
5818                 wait for the owner's approval",
5819                contested.findings.len(),
5820                contested.rejecters.len()
5821            ),
5822        );
5823        self.state.contested_handoff = Some(contested);
5824    }
5825
5826    async fn stop_reviewing(&mut self, why: &str, shell: &[String], worktree: &Path) -> Result<()> {
5827        let round_idx = self.state.reviews.len() - 1;
5828        // A deferred round and a resource-blocked one are the same shape
5829        // here: neither has a real result yet, and both get one more
5830        // attempt. Read off `e2e_status` — the single source for this —
5831        // rather than `e2e.is_empty()` alone, so a resource-blocked attempt
5832        // (whose `e2e` is *not* empty; see `CommandOutcome::resource_blocked`)
5833        // still retries instead of being read as a settled result the
5834        // instant it stops being empty.
5835        let needs_catchup_run = matches!(
5836            self.state.reviews[round_idx].e2e_status(),
5837            E2eStatus::Deferred | E2eStatus::ResourceBlocked
5838        );
5839        if needs_catchup_run {
5840            let round = self.state.reviews[round_idx].round;
5841            let timeout = Duration::from_secs(self.state.config.graph.verify_timeout());
5842            let commands = self.state.config.verify.e2e.clone();
5843            let attempted_head = git::rev_parse(worktree, "HEAD").await?;
5844            let cache_dir = self.state.config.cache_dir();
5845            let context = format!(
5846                "round {round}: verification unresolved, catching up before the final decision"
5847            );
5848            let (outcomes, verify_retried) = with_cache_lease(
5849                &mut self.state,
5850                cache_dir.as_deref(),
5851                "e2e",
5852                "e2e",
5853                worktree,
5854                &attempted_head,
5855                timeout,
5856                &context,
5857                |state, budget| {
5858                    let shell = shell.to_vec();
5859                    let commands = commands.clone();
5860                    let context = context.clone();
5861                    async move {
5862                        run_e2e_with_retry(state, &shell, &commands, worktree, budget, &context)
5863                            .await
5864                    }
5865                },
5866            )
5867            .await;
5868            let last = &mut self.state.reviews[round_idx];
5869            last.e2e = outcomes;
5870            last.verify_retried = verify_retried;
5871            // Always the commit and time this attempt actually targeted,
5872            // whether or not it happens to equal the reviewed `head` and
5873            // whether or not a command finished — see
5874            // `ReviewRound::verified_head`'s own doc. A still-inconclusive
5875            // attempt is recorded too, so a later reader sees "attempted
5876            // again at T2" rather than silence.
5877            last.verified_head = Some(attempted_head);
5878            last.verified_at = Some(Timestamp::now());
5879            if verify_inconclusive(&last.e2e) {
5880                // Still not a real result: `e2e_deferred` is left exactly
5881                // as it was, so `needs_catchup_run` above reads
5882                // `ResourceBlocked` (via `e2e_status`, which checks
5883                // `resource_blocked` before `e2e_deferred`) and retries
5884                // again on the next reentry, rather than recording
5885                // contention as a red e2e and blocking the run on it.
5886                self.state.save()?;
5887                return Ok(());
5888            }
5889            last.e2e_deferred = false;
5890        }
5891        let last = &self.state.reviews[round_idx];
5892        let open: usize = last.reviews.iter().map(|r| r.findings.len()).sum();
5893
5894        match last.e2e_status() {
5895            E2eStatus::Failed => {
5896                let red: Vec<String> = last
5897                    .e2e
5898                    .iter()
5899                    .filter(|o| !o.ok())
5900                    .map(|o| {
5901                        format!(
5902                            "`{}` -> {:?}\n{}",
5903                            o.command,
5904                            o.code,
5905                            tail(&o.output_tail, EVENT_OUTPUT_TAIL)
5906                        )
5907                    })
5908                    .collect();
5909                self.state
5910                    .event("review", format!("{why}; e2e failed:\n{}", red.join("\n")));
5911                self.state.status = RunStatus::Blocked;
5912            }
5913            // `needs_catchup_run` above already retried once this call; if
5914            // it is still blocked, this is magi's own admission it could
5915            // not get a command to run, never a verdict on the patch — the
5916            // run is left exactly where a later reentry can retry again.
5917            E2eStatus::ResourceBlocked => {
5918                self.state.event(
5919                    "review",
5920                    format!(
5921                        "{why}; e2e could not run (shared build cache unavailable); not \
5922                         deciding yet"
5923                    ),
5924                );
5925            }
5926            E2eStatus::Passed | E2eStatus::Deferred | E2eStatus::NotConfigured => {
5927                self.state.event(
5928                    "review",
5929                    format!("{why}; e2e is green — handing off with {open} finding(s) still open"),
5930                );
5931                self.record_contested_handoff();
5932                self.state.status = RunStatus::Gating;
5933            }
5934        }
5935        self.state.save()?;
5936        Ok(())
5937    }
5938
5939    // ----------------------------------------------------------------- gate
5940
5941    async fn gate(&mut self) -> Result<()> {
5942        // Judged by the review record itself, not by `status`: a solo
5943        // candidate's `judge`/`deliberate` skip rewrites `status` on every
5944        // reentry (see `judge`), and trusting it here is exactly how a run
5945        // that exhausted its review budget got gated and merged a second
5946        // time around. `review_conclusion` recomputes the review loop's own
5947        // verdict from the round records themselves — `Gating` for a clean
5948        // round or a hand-off (see `stop_reviewing`), anything else means the
5949        // loop is still going or genuinely blocked.
5950        // A base the winner could not be replayed onto is a decision, not a
5951        // round: there is no landing tree to gate. Read as its own record for
5952        // the same reason the review verdict is.
5953        if self.state.status == RunStatus::Failed
5954            || self
5955                .state
5956                .base_sync
5957                .as_ref()
5958                .is_some_and(|s| s.conflict.is_some())
5959            || review_conclusion(&self.state.reviews, self.state.config.graph.review_rounds)
5960                != Some(RunStatus::Gating)
5961        {
5962            return Ok(());
5963        }
5964        if self.state.gate_ran {
5965            // `review_loop` derives its conclusion from the clean review
5966            // record on every reentry and therefore puts a completed run back
5967            // in `Gating`. A recorded gate is a stronger, terminal fact:
5968            // retain its original command output (or lack of any, for a repo
5969            // with no `verify.gate` commands — see `RunState::gate_ran`'s own
5970            // doc) and restore `Blocked` on a real failure rather than
5971            // pretending the command is still running or running it a second
5972            // time. `gate_ran == false` remains the only shape — unattempted,
5973            // or a resource-blocked retry — that may still need to execute a
5974            // command.
5975            if self.state.gate.iter().any(|outcome| !outcome.ok()) {
5976                self.state.status = RunStatus::Blocked;
5977                self.state.save()?;
5978            }
5979            return Ok(());
5980        }
5981        let Some(winner) = self.state.winner().cloned() else {
5982            return Ok(());
5983        };
5984        self.state.status = RunStatus::Gating;
5985        let mut outcomes = self.run_gate(&winner).await?;
5986        loop {
5987            // A resource-blocked outcome means the gate command never actually
5988            // ran - the shared build cache could not be acquired or confirmed
5989            // fresh in time - which is evidence about the machine, not about
5990            // the tree (see `CommandOutcome::resource_blocked`'s own doc).
5991            // Recording it as a red gate would mark a run `Blocked` on nothing
5992            // but contention magi has already logged; leaving `self.state.gate`
5993            // empty and `self.state.gate_ran` false instead keeps the shape
5994            // this function already treats as "still needs to run" (see the
5995            // early-return above), so the next call retries the command
5996            // rather than concluding anything.
5997            if verify_inconclusive(&outcomes) {
5998                self.state.save()?;
5999                return Ok(());
6000            }
6001            if outcomes.iter().all(CommandOutcome::ok) {
6002                break;
6003            }
6004            match self.gate_fix_round(&winner, &outcomes).await? {
6005                GateFix::Retry => outcomes = self.run_gate(&winner).await?,
6006                GateFix::Stop => break,
6007                GateFix::Defer => {
6008                    self.state.save()?;
6009                    return Ok(());
6010                }
6011            }
6012        }
6013        let passed = outcomes.iter().all(CommandOutcome::ok);
6014        self.state.gate = outcomes;
6015        self.state.gate_ran = true;
6016        if !passed {
6017            self.state.status = RunStatus::Blocked;
6018            let spent = self.state.gate_fixes.len();
6019            self.state.event(
6020                "gate",
6021                if spent == 0 {
6022                    "gate failed; not merging".to_owned()
6023                } else {
6024                    format!("gate failed after {spent} gate-fix round(s); not merging")
6025                },
6026            );
6027        }
6028        self.state.save()?;
6029        Ok(())
6030    }
6031
6032    /// Run `verify.pre_gate` in the winner's worktree, then fold whatever it
6033    /// changed into one commit. Reached only from [`Self::run_gate`], i.e.
6034    /// after review is clean and never on a candidate awaiting judging.
6035    ///
6036    /// Never fails the run: a non-zero exit or timeout is a warning and a
6037    /// recorded outcome, and the gate remains the single arbiter. Nothing
6038    /// configured means nothing happens - no event, no commit. `commit_all`
6039    /// commits any leftover change under the neutral identity and returns
6040    /// `false` when the tree is clean, so no empty commit is ever made.
6041    async fn run_pre_gate(&mut self, winner: &Candidate) {
6042        let commands = self.state.config.verify.pre_gate.clone();
6043        if commands.is_empty() {
6044            return;
6045        }
6046        let shell = self.state.config.shell();
6047        let timeout = Duration::from_secs(self.state.config.graph.verify_timeout());
6048        let (outcomes, _) = run_commands(
6049            &mut self.state,
6050            "pre_gate",
6051            "pre_gate",
6052            0,
6053            &shell,
6054            &commands,
6055            &winner.worktree,
6056            timeout,
6057        )
6058        .await;
6059        for o in &outcomes {
6060            if !o.ok() {
6061                tracing::warn!(
6062                    "pre_gate `{}` failed ({:?}); the gate decides",
6063                    o.command,
6064                    o.code
6065                );
6066            }
6067            self.state.event(
6068                "pre_gate",
6069                format!(
6070                    "`{}` -> {}",
6071                    o.command,
6072                    if o.ok() {
6073                        "pass".to_owned()
6074                    } else {
6075                        format!(
6076                            "FAIL ({:?})\n{}",
6077                            o.code,
6078                            tail(&o.output_tail, EVENT_OUTPUT_TAIL)
6079                        )
6080                    }
6081                ),
6082            );
6083        }
6084        self.state.pre_gate = outcomes;
6085        match git::commit_all(&winner.worktree, "magi: pre_gate (mechanical fixes)").await {
6086            Ok(true) => match git::rev_parse(&winner.worktree, "HEAD").await {
6087                Ok(head) => {
6088                    self.state
6089                        .event("pre_gate", format!("committed mechanical fixes ({head})"));
6090                    self.state.pre_gate_commit = Some(head);
6091                }
6092                Err(e) => tracing::warn!("pre_gate committed but HEAD unreadable: {e:#}"),
6093            },
6094            Ok(false) => {}
6095            Err(e) => tracing::warn!("pre_gate could not commit its changes: {e:#}"),
6096        }
6097        if let Err(e) = self.state.save() {
6098            tracing::warn!("could not persist the pre_gate record: {e:#}");
6099        }
6100    }
6101
6102    /// Run `verify.gate` once against the winner's current tree, logging one
6103    /// event per command. Empty when nothing is configured.
6104    async fn run_gate(&mut self, winner: &Candidate) -> Result<Vec<CommandOutcome>> {
6105        self.run_pre_gate(winner).await;
6106        let shell = self.state.config.shell();
6107        let gate_commands = self.state.config.verify.gate.clone();
6108        // Zero commands has nothing to run and nothing that could touch the
6109        // shared build cache, so it never needs a lease: `Config::cache_dir`
6110        // is derived from `verify.e2e` too, so a repo with no `verify.gate`
6111        // commands but a `CARGO_TARGET_DIR`-using `verify.e2e` would
6112        // otherwise queue behind an unrelated run's lease and come back
6113        // resource-blocked - `gate_ran` would stay false on nothing but
6114        // cache contention, for a step that had nothing to check in the
6115        // first place.
6116        let outcomes = if gate_commands.is_empty() {
6117            Vec::new()
6118        } else {
6119            let timeout = Duration::from_secs(self.state.config.graph.verify_timeout());
6120            let cache_dir = self.state.config.cache_dir();
6121            let head = git::rev_parse(&winner.worktree, "HEAD").await?;
6122            let (outcomes, _) = with_cache_lease(
6123                &mut self.state,
6124                cache_dir.as_deref(),
6125                "gate",
6126                "gate",
6127                &winner.worktree,
6128                &head,
6129                timeout,
6130                "final gate",
6131                |state, budget| {
6132                    let shell = shell.clone();
6133                    let gate_commands = gate_commands.clone();
6134                    let worktree = winner.worktree.clone();
6135                    async move {
6136                        let (outcomes, timed_out_pids) = run_commands(
6137                            state,
6138                            "gate",
6139                            "gate",
6140                            0,
6141                            &shell,
6142                            &gate_commands,
6143                            &worktree,
6144                            budget,
6145                        )
6146                        .await;
6147                        (outcomes, false, timed_out_pids)
6148                    }
6149                },
6150            )
6151            .await;
6152            outcomes
6153        };
6154        if outcomes.is_empty() {
6155            // Nothing configured to check — distinct from every other
6156            // silence in this run's event log, since an empty `gate` alone
6157            // no longer says whether the gate ran at all (see
6158            // `RunState::gate_ran`'s own doc).
6159            self.state.event(
6160                "gate",
6161                "no gate commands configured; nothing to check, passing",
6162            );
6163        }
6164        for o in &outcomes {
6165            self.state.event(
6166                "gate",
6167                format!(
6168                    "`{}` -> {}",
6169                    o.command,
6170                    if o.ok() {
6171                        "pass".to_owned()
6172                    } else {
6173                        format!(
6174                            "FAIL ({:?})\n{}",
6175                            o.code,
6176                            tail(&o.output_tail, EVENT_OUTPUT_TAIL)
6177                        )
6178                    }
6179                ),
6180            );
6181        }
6182        Ok(outcomes)
6183    }
6184
6185    /// One bounded fix round for a failing gate.
6186    ///
6187    /// The fixer is told the failure came from the gate itself, not from a
6188    /// reviewer, and is shown the failed commands, their exit codes and a tail
6189    /// of their output - whatever `[verify].gate` holds, nothing here knows
6190    /// what those commands run. Only a normal non-zero exit that printed
6191    /// something earns a round (see [`gate_fixable`]): a timeout, a missing
6192    /// command or a full disk says nothing about the code, and a fixer sent
6193    /// after it can only appease the machine. The round is judged by what git
6194    /// says moved, never by the fixer's own report, and `verify.e2e` runs
6195    /// again before the gate does, so a fix cannot trade a green gate for a
6196    /// red e2e unnoticed.
6197    async fn gate_fix_round(
6198        &mut self,
6199        winner: &Candidate,
6200        outcomes: &[CommandOutcome],
6201    ) -> Result<GateFix> {
6202        let cap = self.state.config.graph.gate_fix_rounds;
6203        let spent = self.state.gate_fixes.len();
6204        if spent >= cap {
6205            if cap > 0 {
6206                self.state.event(
6207                    "gate",
6208                    format!("{spent} gate-fix round(s) spent and the gate still fails"),
6209                );
6210            }
6211            return Ok(GateFix::Stop);
6212        }
6213        if !gate_fixable(outcomes) {
6214            self.state.event(
6215                "gate",
6216                "gate failure is not an ordinary non-zero exit with output (timeout, missing \
6217                 command or similar); not spending a fix round on it",
6218            );
6219            return Ok(GateFix::Stop);
6220        }
6221        let min_free = self.state.config.disk.min_free_bytes;
6222        if min_free > 0 {
6223            match crate::disk::free_bytes(&winner.worktree) {
6224                Ok(free) if crate::disk::enough_space(free, min_free) => {}
6225                Ok(free) => {
6226                    self.state.event(
6227                        "gate",
6228                        format!(
6229                            "only {free} bytes free ({min_free} required by `[disk] \
6230                             min_free_bytes`); not spending a fix round on a failure the disk \
6231                             may explain"
6232                        ),
6233                    );
6234                    return Ok(GateFix::Stop);
6235                }
6236                Err(e) => {
6237                    self.state.event(
6238                        "gate",
6239                        format!("free disk space could not be measured ({e:#}); no fix round"),
6240                    );
6241                    return Ok(GateFix::Stop);
6242                }
6243            }
6244        }
6245
6246        let attempt = spent + 1;
6247        let failed: Vec<CommandOutcome> = outcomes.iter().filter(|o| !o.ok()).cloned().collect();
6248        let base = self.landing_base();
6249        let fix_prompt = prompt::gate_fix(
6250            &self.state.instruction,
6251            &failed,
6252            attempt,
6253            cap,
6254            &self.state.config.graph.language,
6255        );
6256        let timeout = Duration::from_secs(self.state.config.graph.timeout_fix);
6257        let sessions = self.state.config.graph.sessions;
6258        let artifacts = agent::artifacts_dir(&self.state.dir());
6259        self.state.event(
6260            "gate",
6261            format!("gate failed; gate-fix round {attempt} of {cap}"),
6262        );
6263        let before = git::rev_parse(&winner.worktree, "HEAD").await?;
6264        let patch = git::diff(&winner.worktree, &base, "HEAD").await?;
6265        let (_, seat, out) = self
6266            .ask_fixer(winner, "gate-fix", None, |spec, seat| SeatJob {
6267                prompt: fix_prompt.clone(),
6268                spec,
6269                seat,
6270                cwd: winner.worktree.clone(),
6271                timeout,
6272                allow_write: true,
6273                sessions,
6274                artifacts: artifacts.clone(),
6275                stem: format!("gate-fix-{attempt}"),
6276                handover: None,
6277            })
6278            .await;
6279        let mut record = GateFixRecord {
6280            agent: seat.agent.clone(),
6281            failed,
6282            notes: String::new(),
6283            committed: false,
6284            error: None,
6285        };
6286        match out {
6287            AgentOutcome::Ok(o) => {
6288                // A missing report is not a failed fix: the round is judged
6289                // by the tree below, and the report only carries prose.
6290                if let Ok(report) = verdict::extract_json::<FixReport>(&o.text) {
6291                    record.notes = blind::sanitize_prose(&report.notes, &self.state.config.blind);
6292                }
6293            }
6294            AgentOutcome::Dropped(_) => {
6295                record.error = Some("the CLI dropped the stream".to_owned());
6296            }
6297            AgentOutcome::Quota(o) => {
6298                self.state.quota.push(QuotaLoss {
6299                    seat: seat.key.clone(),
6300                    node: "gate-fix".to_owned(),
6301                    at: Timestamp::now(),
6302                    reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
6303                });
6304                record.error = Some("rate limited (quota); fixer could not run".to_owned());
6305            }
6306            AgentOutcome::Failed(e) => record.error = Some(e),
6307        }
6308        self.state.seats.insert(seat.key.clone(), seat);
6309        if let Ok(r) = git::rescue_commit(
6310            &winner.worktree,
6311            &format!("magi: gate fix {attempt} (uncommitted work)"),
6312        )
6313        .await
6314        {
6315            self.state.note_withheld("gate-fix", &r.withheld);
6316        }
6317        let after = git::rev_parse(&winner.worktree, "HEAD").await?;
6318        record.committed = after != before;
6319        let changed = git::diff(&winner.worktree, &base, "HEAD").await? != patch;
6320        let note = record.error.clone();
6321        self.state.gate_fixes.push(record);
6322        self.state.save()?;
6323        if !changed {
6324            self.state.event(
6325                "gate",
6326                match note {
6327                    Some(why) => format!("gate-fix round {attempt}: fixer failed ({why})"),
6328                    None => format!("gate-fix round {attempt}: the tree did not change"),
6329                },
6330            );
6331            return Ok(GateFix::Stop);
6332        }
6333        self.state.event(
6334            "gate",
6335            format!("gate-fix round {attempt}: tree changed vs base; re-running verify.e2e"),
6336        );
6337
6338        let commands = self.state.config.verify.e2e.clone();
6339        if !commands.is_empty() {
6340            let shell = self.state.config.shell();
6341            let timeout = Duration::from_secs(self.state.config.graph.verify_timeout());
6342            let cache_dir = self.state.config.cache_dir();
6343            let context = format!("gate-fix round {attempt}");
6344            let (e2e, _) = with_cache_lease(
6345                &mut self.state,
6346                cache_dir.as_deref(),
6347                "e2e",
6348                "e2e",
6349                &winner.worktree,
6350                &after,
6351                timeout,
6352                &context,
6353                |state, budget| {
6354                    let shell = shell.clone();
6355                    let commands = commands.clone();
6356                    let context = context.clone();
6357                    let worktree = winner.worktree.clone();
6358                    async move {
6359                        run_e2e_with_retry(state, &shell, &commands, &worktree, budget, &context)
6360                            .await
6361                    }
6362                },
6363            )
6364            .await;
6365            if verify_inconclusive(&e2e) {
6366                return Ok(GateFix::Defer);
6367            }
6368            if e2e.iter().any(|o| !o.ok()) {
6369                self.state.event(
6370                    "gate",
6371                    format!("gate-fix round {attempt}: verify.e2e failed after the fix"),
6372                );
6373                return Ok(GateFix::Stop);
6374            }
6375        }
6376        Ok(GateFix::Retry)
6377    }
6378
6379    // ---------------------------------------------------------------- merge
6380
6381    /// One read-only rewrite by the summary's author; a text the gate still
6382    /// withholds goes to the owner (see `github_text`) and the fixed fallback
6383    /// applies on their word or on silence. `None` means the run is parked on
6384    /// that question and nothing may be pushed or opened yet.
6385    async fn guarded_pr_message(
6386        &mut self,
6387        winner: &Candidate,
6388        facts: Option<&BranchFacts>,
6389        posting: bool,
6390    ) -> Result<Option<PrMessage>> {
6391        let mut pr = pr_message_raw(&self.state, winner.label, facts);
6392        if !posting {
6393            let identity = crate::scrub::Identity::current();
6394            return Ok(Some(PrMessage {
6395                title: crate::scrub::scrub(&pr.title, &identity),
6396                body: crate::scrub::scrub(&pr.body, &identity),
6397            }));
6398        }
6399        // This very text was already rejected and asked about: no second
6400        // judge call, rewrite or question, just carry the record on.
6401        let raw_fp = crate::github_text::fingerprint(&pr.title, &pr.body);
6402        if let Some(known) = self
6403            .state
6404            .github_text
6405            .clone()
6406            .filter(|g| g.fingerprint == raw_fp)
6407        {
6408            return self.settle_withheld(winner, &pr, known).await;
6409        }
6410        let mut decision = self.judge_pr_language(&winner.worktree, &pr).await;
6411        let mut violations = crate::github_text::check_with(&pr.title, &pr.body, decision);
6412        let identity = crate::scrub::Identity::current();
6413        if (crate::scrub::scrub(&pr.title, &identity) != pr.title
6414            || crate::scrub::scrub(&pr.body, &identity) != pr.body)
6415            && !violations.contains(&crate::github_text::Violation::SensitiveData)
6416        {
6417            violations.push(crate::github_text::Violation::SensitiveData);
6418        }
6419        // A rewrite cannot fix sensitive data (e.g. a quoted original task), so
6420        // only a language violation starts one; sensitive data alone goes
6421        // straight to the owner, with the redacted text as the fallback.
6422        let has_language = violations
6423            .iter()
6424            .any(|v| *v != crate::github_text::Violation::SensitiveData);
6425        if self.state.config.graph.github_text_guard && !violations.is_empty() {
6426            self.state.event(
6427                "github-text",
6428                if has_language {
6429                    format!("description rejected: {violations:?}; requesting one rewrite")
6430                } else {
6431                    format!("description rejected: {violations:?}; asking the owner")
6432                },
6433            );
6434            let mut rewritten = false;
6435            if has_language
6436                && let Some(spec) = self
6437                    .state
6438                    .config
6439                    .agents
6440                    .iter()
6441                    .find(|a| a.id == winner.agent)
6442                    .cloned()
6443            {
6444                let key = format!("impl-{}", winner.label);
6445                let seat = self.seat(&key, &spec.id);
6446                let prompt = format!(
6447                    "Rewrite only the following pull request description. The posting gate reported {violations:?}. Write English prose and remove all machine or operator identifying data and secrets. Do not edit files or run commands. Return TITLE: followed by the title, then the complete Markdown body. Preserve the magi run marker.\n\nTITLE: {}\n{}",
6448                    pr.title, pr.body
6449                );
6450                let job = SeatJob {
6451                    spec,
6452                    seat,
6453                    cwd: winner.worktree.clone(),
6454                    prompt,
6455                    timeout: retry_budget(
6456                        Duration::from_secs(self.state.config.graph.timeout_implement),
6457                        true,
6458                    ),
6459                    allow_write: false,
6460                    sessions: self.state.config.graph.sessions,
6461                    artifacts: agent::artifacts_dir(&self.state.dir()),
6462                    stem: "github-text-rewrite".to_owned(),
6463                    handover: None,
6464                };
6465                let prompts = self.state.config.prompts.clone();
6466                let cache = self.state.config.cache_dir();
6467                let run = self.state.id.clone();
6468                let ctx = WaveCtx {
6469                    run: &run,
6470                    node: "github-text",
6471                    prompts: &prompts,
6472                    cache: cache.as_deref(),
6473                    round: None,
6474                    carry_seats: false,
6475                };
6476                let (seat, outcome) =
6477                    run_one(job, Arc::clone(&self.sem), &ctx, &mut self.state, 1).await;
6478                self.state.seats.insert(seat.key.clone(), seat);
6479                if let AgentOutcome::Ok(output) = outcome
6480                    && let Some(title) = summary_title(&output.text)
6481                {
6482                    let mut body = summary_without_title(&output.text);
6483                    let rewrite = PrMessage {
6484                        title: title.clone(),
6485                        body: body.clone(),
6486                    };
6487                    let redecision = self.judge_pr_language(&winner.worktree, &rewrite).await;
6488                    if !body.trim().is_empty()
6489                        && crate::github_text::check_with(&title, &body, redecision).is_empty()
6490                        && crate::github_text::shareable(&title)
6491                        && crate::github_text::shareable(&body)
6492                    {
6493                        decision = redecision;
6494                        let marker = format!("magi:run/{}", self.state.id);
6495                        if !body.contains(&marker) {
6496                            body.push_str(&format!("\n\n{marker}"));
6497                        }
6498                        pr = PrMessage { title, body };
6499                        rewritten = true;
6500                        self.state
6501                            .event("github-text", "description rewrite passed");
6502                    }
6503                }
6504            }
6505            if !rewritten {
6506                self.state.event(
6507                    "github-text",
6508                    "description rewrite unavailable or rejected; asking the owner",
6509                );
6510                if let Some(w) =
6511                    crate::github_text::Withheld::from_check(&violations, &pr.title, &pr.body)
6512                {
6513                    // A different text than the one asked about before: that
6514                    // question no longer describes anything.
6515                    self.retire_github_text_question("the withheld text changed");
6516                    let record = crate::run::GithubTextAsk {
6517                        fingerprint: raw_fp,
6518                        title: w.title,
6519                        body: w.body,
6520                        categories: w.categories.iter().map(|c| (*c).to_owned()).collect(),
6521                        question: None,
6522                        asks: 0,
6523                        resolved: false,
6524                        chosen_title: None,
6525                        choice: None,
6526                    };
6527                    return self.settle_withheld(winner, &pr, record).await;
6528                }
6529            }
6530        }
6531        let (title, body) =
6532            crate::github_text::prepare_with(&mut self.state, &pr.title, &pr.body, decision);
6533        Ok(Some(PrMessage { title, body }))
6534    }
6535
6536    /// Abandon the standing posting-gate question, if any.
6537    fn retire_github_text_question(&mut self, why: &str) {
6538        let Some(id) = self
6539            .state
6540            .github_text
6541            .as_ref()
6542            .and_then(|g| g.question.clone())
6543        else {
6544            return;
6545        };
6546        let _ = ask::Questions::open().update(&id, |q| {
6547            q.abandon(why);
6548            Ok(())
6549        });
6550    }
6551
6552    /// Carry a withheld text to its end: file the question, park while it is
6553    /// open, and on an answer, silence or timeout pick the text to post. The
6554    /// decision is saved before it is used, so it applies once.
6555    async fn settle_withheld(
6556        &mut self,
6557        winner: &Candidate,
6558        pr: &PrMessage,
6559        mut g: crate::run::GithubTextAsk,
6560    ) -> Result<Option<PrMessage>> {
6561        use crate::github_text as gt;
6562        let store = ask::Questions::open();
6563        let timeout = self.state.config.graph.answer_timeout;
6564        if !g.resolved {
6565            let mut standing = g
6566                .question
6567                .as_ref()
6568                .and_then(|id| store.list().into_iter().find(|q| &q.id == id));
6569            if let Some(q) = standing.as_ref()
6570                && q.status.open()
6571                && gt::expired(q, timeout)
6572            {
6573                let why = format!("no answer within {}s of asking", timeout.max(1));
6574                standing = store
6575                    .update(&q.id, |q| {
6576                        q.abandon(&why);
6577                        Ok(())
6578                    })
6579                    .ok()
6580                    .map(|(q, ())| q);
6581            }
6582            let mut ask_again: Option<Option<Vec<String>>> = None;
6583            match standing {
6584                // Recorded but never written (a stop between the two saves),
6585                // or removed: ask again without spending the retry.
6586                None if g.asks <= 1 => {
6587                    g.asks = 0;
6588                    ask_again = Some(None);
6589                }
6590                None => {
6591                    g.resolved = true;
6592                    g.chosen_title = None;
6593                    g.choice = Some(GithubTextChoice::Fallback);
6594                }
6595                Some(q) if q.status.open() => {
6596                    self.park_on_github_text(&g)?;
6597                    return Ok(None);
6598                }
6599                Some(q) => match gt::read_reply(&q) {
6600                    gt::Reply::Fallback => {
6601                        g.resolved = true;
6602                        g.chosen_title = None;
6603                        g.choice = Some(GithubTextChoice::Fallback);
6604                    }
6605                    gt::Reply::Original => {
6606                        g.resolved = true;
6607                        g.chosen_title = None;
6608                        g.choice = Some(GithubTextChoice::Original);
6609                    }
6610                    gt::Reply::Title(text) => {
6611                        let first = text
6612                            .lines()
6613                            .map(str::trim)
6614                            .find(|l| !l.is_empty())
6615                            .unwrap_or("");
6616                        let decision = if first.is_empty() {
6617                            None
6618                        } else {
6619                            let candidate = PrMessage {
6620                                title: first.to_owned(),
6621                                body: String::new(),
6622                            };
6623                            self.judge_pr_language(&winner.worktree, &candidate).await
6624                        };
6625                        match gt::vet_title(&text, decision) {
6626                            Ok(title) => {
6627                                g.resolved = true;
6628                                g.chosen_title = Some(title);
6629                                g.choice = Some(GithubTextChoice::MyTitle);
6630                            }
6631                            Err(cats) if g.asks < 2 => {
6632                                ask_again = Some(Some(
6633                                    cats.into_iter().map(str::to_owned).collect::<Vec<_>>(),
6634                                ))
6635                            }
6636                            Err(_) => {
6637                                g.resolved = true;
6638                                g.chosen_title = None;
6639                                g.choice = Some(GithubTextChoice::Fallback);
6640                            }
6641                        }
6642                    }
6643                },
6644            }
6645            if let Some(rejected) = ask_again {
6646                // A retry (after a rejected replacement title) is still about
6647                // the original text: the same ORIGINAL / CHANGED comparison.
6648                let w = gt::Withheld {
6649                    title: g.title,
6650                    body: g.body,
6651                    categories: g.categories.clone(),
6652                };
6653                // Named by the original text's fingerprint alone, so a resume
6654                // rewrites the same file and the detail stays identical.
6655                let artifact = match crate::run::write_artifact(
6656                    &self.state,
6657                    &gt::artifact_name(&gt::fingerprint(&pr.title, &pr.body)),
6658                    &gt::artifact_text(&w, &pr.title, &pr.body, &self.state.id),
6659                ) {
6660                    Ok(p) => Some(p),
6661                    Err(e) => {
6662                        tracing::warn!("could not write the withheld-text artifact: {e:#}");
6663                        None
6664                    }
6665                };
6666                let mut q = ask::Question::new(
6667                    self.state.id.clone(),
6668                    gt::ASK_NODE.to_owned(),
6669                    gt::ASK_SEAT.to_owned(),
6670                    gt::question_summary(&self.state.config.graph.language, &w),
6671                    gt::question_detail(
6672                        &w,
6673                        &pr.title,
6674                        &pr.body,
6675                        &self.state.id,
6676                        rejected.as_deref(),
6677                        artifact.as_deref(),
6678                    ),
6679                    gt::question_choices(&w),
6680                );
6681                q.answer_timeout = timeout;
6682                // Recorded before it is written: a stop in between leaves a
6683                // record naming a question the resume then files again.
6684                g.question = Some(q.id.clone());
6685                g.asks += 1;
6686                self.state.github_text = Some(g.clone());
6687                self.state.event(
6688                    "github-text",
6689                    format!("asking the owner about the withheld text ({})", q.short()),
6690                );
6691                self.state.save()?;
6692                store
6693                    .put(&mut q)
6694                    .context("file the posting-gate question")?;
6695                if let Err(e) = ask::notify(&self.state.config.notify, &q).await {
6696                    tracing::warn!(
6697                        "could not notify about posting-gate question {}: {e:#}",
6698                        q.short()
6699                    );
6700                }
6701                self.park_on_github_text(&g)?;
6702                return Ok(None);
6703            }
6704        }
6705        let resolved = g.chosen_title.clone();
6706        let original = g.choice == Some(GithubTextChoice::Original);
6707        // Field names only: the event log never records the text.
6708        self.state.event(
6709            "github-text",
6710            if original {
6711                format!(
6712                    "posting the original text for: {}",
6713                    match (g.title, g.body) {
6714                        (true, true) => "title, description",
6715                        (true, false) => "title",
6716                        _ => "description",
6717                    }
6718                )
6719            } else if resolved.is_some() {
6720                "posting the owner's replacement title".to_owned()
6721            } else {
6722                "posting the neutral text for the withheld fields".to_owned()
6723            },
6724        );
6725        self.state.github_text = Some(g.clone());
6726        self.state.save()?;
6727        let w = gt::Withheld {
6728            title: g.title,
6729            body: g.body,
6730            categories: g.categories.clone(),
6731        };
6732        let (mut title, body) = gt::fallback_text(&w, &pr.title, &pr.body, &self.state.id);
6733        if let Some(t) = resolved {
6734            title = t;
6735        }
6736        // Every withheld field is now neutral or vetted English.
6737        let english = gt::LanguageDecision {
6738            title_english: true,
6739            body_english: true,
6740            body_complete: true,
6741        };
6742        let (mut title, mut body) = gt::prepare_with(&mut self.state, &title, &body, Some(english));
6743        if original {
6744            // The owner's explicit choice overrides the gate for the fields it
6745            // was made for; the others keep what the gate produced.
6746            if g.title {
6747                title = pr.title.clone();
6748            }
6749            if g.body {
6750                body = pr.body.clone();
6751            }
6752        }
6753        Ok(Some(PrMessage { title, body }))
6754    }
6755
6756    fn park_on_github_text(&mut self, g: &crate::run::GithubTextAsk) -> Result<()> {
6757        self.state.github_text = Some(g.clone());
6758        self.state.parked = true;
6759        self.state.event(
6760            "github-text",
6761            "parked awaiting the owner's word on the withheld pull request text - resumes once answered",
6762        );
6763        self.state.save()
6764    }
6765
6766    /// Ask `[roles] language_judge` about this text and record which source
6767    /// decided; the text itself is never recorded.
6768    async fn judge_pr_language(
6769        &mut self,
6770        cwd: &Path,
6771        pr: &PrMessage,
6772    ) -> Option<crate::github_text::LanguageDecision> {
6773        self.state.config.roles.language_judge.as_ref()?;
6774        let decision =
6775            crate::github_text::judge_language(&self.state.config, cwd, &pr.title, &pr.body).await;
6776        self.state.event(
6777            "github-text",
6778            if decision.is_some() {
6779                "language decided by the language judge"
6780            } else {
6781                "language judge unavailable; using built-in heuristics"
6782            },
6783        );
6784        decision
6785    }
6786
6787    async fn merge(&mut self) -> Result<()> {
6788        // Same reasoning as `gate`: ask the review and gate records directly
6789        // rather than `status`, which a solo-candidate `judge`/`deliberate`
6790        // skip can rewrite on reentry to something that no longer says
6791        // `Blocked`. `review_conclusion` is the same derivation `gate` uses,
6792        // so a hand-off (open findings, green verification) reaches merge
6793        // exactly like a genuinely clean round does.
6794        //
6795        // A run resumed mid-`land` never reaches here at all: `execute`
6796        // recognises `RunStatus::Landing` before it even calls `prep`, and
6797        // routes straight to `run_land` instead. That has to happen a level
6798        // up from this function, not with a check in here, because
6799        // `review_loop`'s own status recomputation (see its doc) runs
6800        // *before* `merge` on every reentry and would otherwise overwrite
6801        // the `Landing` marker with `Gating` before this node ever saw it.
6802        if self
6803            .state
6804            .base_sync
6805            .as_ref()
6806            .is_some_and(|s| s.conflict.is_some())
6807            || review_conclusion(&self.state.reviews, self.state.config.graph.review_rounds)
6808                != Some(RunStatus::Gating)
6809            // `gate_ran == false` is not "passed" - `gate` leaves it false
6810            // both before it has ever run and when its last attempt was
6811            // resource-blocked (see `Runner::gate`'s own doc), and neither is
6812            // permission to merge on nothing but the review record. Only a
6813            // gate that actually ran - zero commands configured and
6814            // vacuously passed, or one or more that all exited 0 - may
6815            // proceed; `RunState::gate_status` is the single place that
6816            // reading is computed.
6817            || !self.state.gate_status().ok()
6818        {
6819            return Ok(());
6820        }
6821        // This node's own record, not `status`: `status == Ready` is not
6822        // unique to the harmless `MergeMode::None` path this line was
6823        // written for. `land` (below) sets it too, when a `MergeMode::Pr`
6824        // run's PR was closed without merging — and on that run `mode` is
6825        // still `Pr`, so a reentry that fell through here would push and
6826        // open a second pull request. `self.state.merge` is set exactly once
6827        // this node (or `land`) has already produced a verdict, under every
6828        // mode, which is what "already done" actually means here.
6829        if self.state.merge.is_some() && !self.state.pr_publication_pending {
6830            return Ok(());
6831        }
6832        let Some(winner) = self.state.winner().cloned() else {
6833            return Ok(());
6834        };
6835        let repo = self.state.repo.clone();
6836        let base = self.state.base_branch.clone();
6837        let mode = self.state.config.merge.mode;
6838        let style = self.state.config.merge.style;
6839        let facts = if is_review_run(&self.state) {
6840            refresh_reviewed_commits(&mut self.state, &winner.branch).await;
6841            let start = review_base(
6842                &repo,
6843                &self.state.config.merge.remote,
6844                &base,
6845                &self.state.base_commit,
6846                &winner.branch,
6847            )
6848            .await;
6849            branch_facts(&repo, &start, &winner.branch).await
6850        } else {
6851            None
6852        };
6853        // `None` when the base could not be freshly read: then an adopted
6854        // pull request's title is left alone.
6855        let leaked = if is_review_run(&self.state) {
6856            leaked_subjects(&self.state, &winner.branch).await
6857        } else {
6858            Some(Vec::new())
6859        };
6860        // `None`: parked on the owner's word about a withheld title or body.
6861        // Nothing has been pushed or opened, and `merge` stays unrecorded so
6862        // the resume arrives here again.
6863        let Some(pr) = self
6864            .guarded_pr_message(&winner, facts.as_ref(), mode == MergeMode::Pr)
6865            .await?
6866        else {
6867            return Ok(());
6868        };
6869        let message = pr.commit_message();
6870
6871        let outcome = match mode {
6872            MergeMode::None => MergeOutcome {
6873                mode,
6874                ok: true,
6875                detail: manual_merge_command(style, &repo, &winner.branch, &message),
6876                empty: false,
6877            },
6878            MergeMode::Pr | MergeMode::Local
6879                if merge_is_empty(&repo, &self.state, &winner.branch, mode).await =>
6880            {
6881                MergeOutcome {
6882                    mode,
6883                    ok: false,
6884                    detail: empty_candidate_detail(&self.state, &base),
6885                    empty: true,
6886                }
6887            }
6888            MergeMode::Local => {
6889                let on = git::current_branch(&repo).await?;
6890                if on.as_deref() != Some(base.as_str()) {
6891                    MergeOutcome {
6892                        mode,
6893                        ok: false,
6894                        detail: format!(
6895                            "{} has {} checked out, not the base branch {base}",
6896                            repo.display(),
6897                            on.unwrap_or_else(|| "a detached HEAD".to_owned())
6898                        ),
6899                        empty: false,
6900                    }
6901                } else if !git::is_clean(&repo).await? {
6902                    MergeOutcome {
6903                        mode,
6904                        ok: false,
6905                        detail: format!("{} is dirty; refusing to merge", repo.display()),
6906                        empty: false,
6907                    }
6908                } else {
6909                    let out = match style {
6910                        MergeStyle::Merge => {
6911                            git::merge_no_ff(&repo, &winner.branch, &message).await?
6912                        }
6913                        MergeStyle::Squash => {
6914                            git::merge_squash(&repo, &winner.branch, &message).await?
6915                        }
6916                        MergeStyle::Rebase => git::merge_ff_only(&repo, &winner.branch).await?,
6917                    };
6918                    MergeOutcome {
6919                        mode,
6920                        ok: out.ok(),
6921                        detail: if out.ok() { out.stdout } else { out.stderr },
6922                        empty: false,
6923                    }
6924                }
6925            }
6926            MergeMode::Pr if self.state.task_pr_branch().is_some() => {
6927                self.state.pr_publication_pending = true;
6928                self.state.save()?;
6929                let out =
6930                    crate::pr_head::publish(&mut self.state, &winner, &pr.title, &pr.body).await;
6931                self.state.pr_publication_pending = out.is_err();
6932                MergeOutcome {
6933                    mode,
6934                    ok: out.is_ok(),
6935                    detail: match out {
6936                        Ok(url) => url,
6937                        Err(e) => format!("{e:#}"),
6938                    },
6939                    empty: false,
6940                }
6941            }
6942            MergeMode::Pr => {
6943                let remote = self.state.config.merge.remote.clone();
6944                let pushed = git::push(&winner.worktree, &remote, &winner.branch).await?;
6945                if !pushed.ok() {
6946                    MergeOutcome {
6947                        mode,
6948                        ok: false,
6949                        detail: pushed.stderr,
6950                        empty: false,
6951                    }
6952                } else {
6953                    // A retry or resume of a run whose branch already has an
6954                    // open pull request adopts it rather than failing on a
6955                    // duplicate. Only this winner branch into this base:
6956                    // `branch_for` derives the name from the run id, so a
6957                    // different run's pull request never matches.
6958                    let found = land::find_open_pr(&winner.worktree, &winner.branch, &base).await;
6959                    let out = match pr_merge_plan(found) {
6960                        PrPlan::Create => {
6961                            gh_pr_create(
6962                                &winner.worktree,
6963                                &base,
6964                                &winner.branch,
6965                                &pr.title,
6966                                &pr.body,
6967                            )
6968                            .await
6969                        }
6970                        PrPlan::Adopt { url, title } => {
6971                            self.state
6972                                .event("merge", format!("Pr: adopted open pull request {url}"));
6973                            if title != pr.title
6974                                && (!is_review_run(&self.state)
6975                                    || leaked
6976                                        .as_deref()
6977                                        .is_some_and(|l| should_retitle(&title, &pr.title, l)))
6978                                && let Err(e) =
6979                                    if self.state.github_text.as_ref().is_some_and(|g| {
6980                                        g.resolved
6981                                            && g.title
6982                                            && g.choice == Some(GithubTextChoice::Original)
6983                                    }) {
6984                                        // The owner chose to post the original title.
6985                                        land::set_pr_title_verbatim(
6986                                            &winner.worktree,
6987                                            &url,
6988                                            &pr.title,
6989                                        )
6990                                        .await
6991                                    } else {
6992                                        land::set_pr_title(
6993                                            &mut self.state,
6994                                            &winner.worktree,
6995                                            &url,
6996                                            &pr.title,
6997                                        )
6998                                        .await
6999                                    }
7000                            {
7001                                tracing::warn!("could not refresh title of {url}: {e:#}");
7002                                self.state
7003                                    .event("merge", format!("Pr: title refresh failed: {e:#}"));
7004                            }
7005                            Ok(url)
7006                        }
7007                        PrPlan::Stop(why) => Err(anyhow::anyhow!(why)),
7008                    };
7009                    match out {
7010                        Ok(url) => MergeOutcome {
7011                            mode,
7012                            ok: true,
7013                            detail: url,
7014                            empty: false,
7015                        },
7016                        Err(e) => MergeOutcome {
7017                            mode,
7018                            ok: false,
7019                            detail: e.to_string(),
7020                            empty: false,
7021                        },
7022                    }
7023                }
7024            }
7025        };
7026
7027        self.state.status = match (mode, outcome.ok) {
7028            (MergeMode::None, _) => RunStatus::Ready,
7029            (_, true) => RunStatus::Merged,
7030            (_, false) => RunStatus::Blocked,
7031        };
7032        self.state.event(
7033            "merge",
7034            format!(
7035                "{:?}: {}",
7036                mode,
7037                outcome.detail.lines().next().unwrap_or("")
7038            ),
7039        );
7040        self.state.merge = Some(outcome);
7041        self.state.save()?;
7042
7043        // The PR is open and the run would historically stop here, leaving the
7044        // operator to watch checks, feed review comments back to a fixer, and
7045        // merge. That was done by hand six times in one session before this
7046        // existed. Opt-in, because merging is the one irreversible thing magi
7047        // can do to a repository.
7048        if self.state.config.graph.land
7049            && mode == MergeMode::Pr
7050            && self.state.status == RunStatus::Merged
7051        {
7052            self.run_land().await?;
7053        }
7054        // `run_land` may have left `status` at `Landing` - still waiting on
7055        // CI or the owner's approval, not actually settled - so this has to
7056        // read whatever `status` ended up as here, not the `Merged` this
7057        // function set a few lines up.
7058        self.settle_questions();
7059        Ok(())
7060    }
7061
7062    /// Enter `land`.
7063    ///
7064    /// Shared between a fresh run's first pass through [`Runner::merge`] and
7065    /// a resumed run's re-entry. `land::land` itself is what serialises the
7066    /// two git-mutating moments inside the loop — the rebase push and
7067    /// `gh pr merge` — per repository (see its own doc); nothing here needs
7068    /// to hold a lock across the whole call, and doing so would serialise
7069    /// this run's CI wait against a *different* run's land-approval resume
7070    /// in the same repository, which is exactly the "must not wait on
7071    /// another task" property the daemon's slot-freeing exists to give.
7072    async fn run_land(&mut self) -> Result<()> {
7073        crate::pr_head::ensure_owner(&self.state)?;
7074        let url = self
7075            .state
7076            .merge
7077            .as_ref()
7078            .map(|m| m.detail.clone())
7079            .unwrap_or_default();
7080        let url = url.lines().next().unwrap_or("").trim().to_owned();
7081        if !url.starts_with("http") {
7082            return Ok(());
7083        }
7084        // A land failure is not a lost run: the work is on a branch and the
7085        // pull request is open, which is exactly where a human takes over.
7086        match land::land(&mut self.state, &url).await {
7087            Ok(pr) if self.state.parked => {
7088                // `land` already saved the parked marker; nothing here
7089                // overrides `status` back to a terminal value while an
7090                // approval is still outstanding.
7091                let _ = pr;
7092            }
7093            Ok(pr) => {
7094                self.state.status = match pr.state {
7095                    land::PrLifecycle::Merged => RunStatus::Merged,
7096                    _ => RunStatus::Blocked,
7097                };
7098                // Downstream of a confirmed merge only - see
7099                // `bump::should_release_bump`'s own doc for why this one
7100                // check covers all three of `land`'s success paths.
7101                // Best-effort: the run already landed, so a failure here
7102                // (the decision call, `gh`, `cargo`) is recorded and never
7103                // turns a landed run into a failed one.
7104                if bump::should_release_bump(self.state.status)
7105                    && let Err(e) = bump::after_merge(&mut self.state, &pr.url).await
7106                {
7107                    // The event is the run's own record. Not-eligible cases
7108                    // (disabled, no `Cargo.toml`, ...) return `Ok`, so an
7109                    // `Err` is a bump that was tried and failed:
7110                    // `after_merge` itself raises the operator notice for
7111                    // that, whether or not a release PR exists yet.
7112                    self.state
7113                        .event("bump", format!("release bump skipped: {e:#}"));
7114                }
7115                // Independent of the bump, and best-effort in the same way:
7116                // findings the merge left open become follow-up tasks.
7117                if self.state.status == RunStatus::Merged {
7118                    crate::followup::after_merge(&mut self.state, &pr.url).await;
7119                }
7120                self.state.save()?;
7121            }
7122            Err(e) => {
7123                self.state.status = RunStatus::Blocked;
7124                self.state.event("land", format!("gave up: {e}"));
7125                self.state.save()?;
7126            }
7127        }
7128        Ok(())
7129    }
7130
7131    // -------------------------------------------------------------- helpers
7132
7133    /// Fetch or create a seat, keeping its conversation across nodes.
7134    fn seat(&mut self, key: &str, agent: &str) -> SeatState {
7135        if let Some(existing) = self.state.seats.get(key)
7136            && existing.agent == agent
7137        {
7138            return existing.clone();
7139        }
7140        // A seat that changes agent mints its session id from the agent too,
7141        // like a handover: the old agent's uuid is already taken by the CLI.
7142        let fresh = if self.state.seats.contains_key(key) {
7143            handover_seat(key, agent, self.state.next_seat_seed())
7144        } else {
7145            SeatState::new(key, agent, self.state.seed)
7146        };
7147        self.state.seats.insert(key.to_owned(), fresh.clone());
7148        fresh
7149    }
7150
7151    /// The agent now holding seat `key`: `spec`, unless a handover moved the
7152    /// seat to another roster agent, in which case that agent. Nodes that
7153    /// continue a seat's conversation (deliberation, the votes, a reviewer's
7154    /// reconsideration) must keep talking to whoever answered it, not slip
7155    /// back to the agent that failed it.
7156    fn occupant(&self, key: &str, spec: AgentSpec) -> AgentSpec {
7157        match self.state.seats.get(key) {
7158            Some(s) if s.agent != spec.id => {
7159                self.state.config.agent(&s.agent).cloned().unwrap_or(spec)
7160            }
7161            _ => spec,
7162        }
7163    }
7164
7165    /// A candidate rendered for judging, with the leak policy applied.
7166    fn view(&self, c: &Candidate) -> CandidateView {
7167        let raw = crate::run::read_artifact(&self.state, &format!("cand-{}.patch", c.label))
7168            .unwrap_or_default();
7169        let (patch, _) = blind::sanitize_patch(
7170            &format!("candidate {} patch", c.label),
7171            &raw,
7172            &self.state.config.blind,
7173        );
7174        CandidateView {
7175            label: c.label,
7176            branch: c.branch.clone(),
7177            summary: c.summary.clone(),
7178            stat: c.stat.clone(),
7179            patch,
7180        }
7181    }
7182
7183    /// The full candidate set as prompt text, for seats with no live session.
7184    fn candidate_block(&self, candidates: &[Candidate], base_short: &str) -> String {
7185        let views: Vec<CandidateView> = candidates.iter().map(|c| self.view(c)).collect();
7186        prompt::judge(
7187            "(see above)",
7188            &views,
7189            self.roles.judges.len(),
7190            base_short,
7191            "en",
7192        )
7193    }
7194
7195    /// The final-vote prompt with everything a seat that has no session of its
7196    /// own needs: the candidates, the seat's own ranking and reasons, and the
7197    /// anonymised deliberation it took part in (only when there was one, so a
7198    /// handed-over seat never sees more than the seat it replaces did). The
7199    /// `Final vote` heading stays first.
7200    fn vote_prompt_full(
7201        &self,
7202        j: usize,
7203        labels: &[char],
7204        language: &str,
7205        candidates: &[Candidate],
7206        base_short: &str,
7207    ) -> String {
7208        let mut text = format!(
7209            "{}\n\n# The task the candidates were given\n\n{}\n\n# Candidates\n\n{}",
7210            prompt::final_vote(labels, language),
7211            self.state.instruction,
7212            self.candidate_block(candidates, base_short)
7213        );
7214        if let Some(own) = self
7215            .state
7216            .judgements
7217            .get(j)
7218            .filter(|r| !r.ranking.is_empty())
7219        {
7220            let reasons = own
7221                .reasons
7222                .iter()
7223                .map(|(k, v)| format!("- {k}: {v}"))
7224                .collect::<Vec<_>>()
7225                .join("\n");
7226            text.push_str(&format!(
7227                "\n\n# Your own earlier ranking\n\nYou ranked {}{}{reasons}\n",
7228                own.ranking.iter().collect::<String>(),
7229                if reasons.is_empty() {
7230                    ""
7231                } else {
7232                    ", because:\n"
7233                }
7234            ));
7235        }
7236        if !self.state.deliberation.is_empty() {
7237            text.push_str("\n# What was argued before this vote\n");
7238            for t in self.transcript(&[], j) {
7239                text.push_str(&format!(
7240                    "\n## {}{}\n\n{}\n",
7241                    t.who,
7242                    if t.is_self { " (you)" } else { "" },
7243                    t.body.trim()
7244                ));
7245            }
7246        }
7247        text
7248    }
7249
7250    /// Anonymised transcript for judge `self_idx`.
7251    ///
7252    /// The initial rankings are always the opening statements. Seeding them
7253    /// only when no turn had been taken yet meant every judge after the first
7254    /// argued against a single voice instead of against the actual split — the
7255    /// disagreement is the information, so it is always on the table.
7256    fn transcript(&self, current: &[DeliberationTurn], self_idx: usize) -> Vec<Turn> {
7257        let mut turns = Vec::new();
7258        for j in &self.state.judgements {
7259            if j.ranking.is_empty() {
7260                continue;
7261            }
7262            let reasons = j
7263                .reasons
7264                .iter()
7265                .map(|(k, v)| format!("- {k}: {v}"))
7266                .collect::<Vec<_>>()
7267                .join("\n");
7268            turns.push(Turn {
7269                who: format!("Judge {} (opening ranking)", j.judge),
7270                is_self: j.judge == self_idx + 1,
7271                body: format!(
7272                    "Ranked {}{}{reasons}",
7273                    j.ranking.iter().collect::<String>(),
7274                    if reasons.is_empty() {
7275                        ""
7276                    } else {
7277                        ", because:\n"
7278                    }
7279                ),
7280            });
7281        }
7282        for t in self
7283            .state
7284            .deliberation
7285            .iter()
7286            .flat_map(|r| r.turns.iter())
7287            .chain(current)
7288        {
7289            turns.push(Turn {
7290                who: format!("Judge {}", t.judge),
7291                is_self: t.judge == self_idx + 1,
7292                body: t.body.clone(),
7293            });
7294        }
7295        turns
7296    }
7297}
7298
7299/// Does this seat still hold the context a follow-up prompt would rely on?
7300fn has_context(spec: &AgentSpec, seat: &SeatState, sessions: bool) -> bool {
7301    agent::has_session(spec.kind, seat, sessions)
7302}
7303
7304/// The next entry in `roster` after `start`, never wrapping back to the
7305/// front, whose id is not in `tried` yet.
7306///
7307/// Starts one past `start` rather than at the front of `roster`: `start` is
7308/// the seat's own original position, and a seat whose candidate slot already
7309/// sits on the roster's second entry must fall through to the third next, not
7310/// restart at the first — which is very likely a different candidate's own
7311/// agent already. Never wraps back past `start`, for the same reason: an
7312/// entry earlier in the roster than the seat's own position is almost
7313/// certainly some *other* candidate slot's own agent, and once the tail of
7314/// the roster is exhausted there are no more untried agents for *this* seat
7315/// to fall through to — the caller's fallback chain ends there, exactly as
7316/// "no further untried agents remain in the list for that seat" asks for.
7317///
7318/// Matched by [`AgentSpec::id`], never the whole spec: a roster that names
7319/// the same id twice (an operator's `roles.implementers` typo, or a
7320/// `[[agents]]` list reused across roles) must not let
7321/// [`Runner::resume_seat_handovers`] retry that id forever — one forward pass
7322/// over `roster` either finds an untried id or runs out, so this always
7323/// terminates regardless of duplicates.
7324fn next_untried_in_roster<'a>(
7325    roster: &'a [AgentSpec],
7326    start: usize,
7327    tried: &BTreeSet<String>,
7328) -> Option<&'a AgentSpec> {
7329    roster
7330        .get(start + 1..)?
7331        .iter()
7332        .find(|s| !tried.contains(&s.id))
7333}
7334
7335/// The successor for a seat, shared by all four seat kinds (implement, judge,
7336/// review, advise). `others` holds the ids that *currently* occupy the other
7337/// seats of the same wave (after any earlier handover, as [`Runner::occupant`]
7338/// sees them), so roster entries beyond the seat count act as spares: a failed
7339/// seat goes to an agent no other seat holds whenever the roster permits.
7340///
7341/// `carried` is `Some` only for the review loop's carried failure history.
7342/// Order: (1) forward from `start`, never wrapping, untried, not a carried
7343/// failure, not another seat's occupant; (2) with `carried`, a rescue over the
7344/// whole roster: untried and not another seat's occupant; (3) the plain walk
7345/// ([`next_untried_in_roster`] / [`next_for_seat`]) that ignores `others`.
7346/// Step 3 is deliberate: a duplicate agent on two seats is a worse panel but
7347/// a better outcome than an empty seat, and it keeps the answer to "is there
7348/// a successor at all" exactly what it was before occupants were considered,
7349/// so no handover rule (`should_hand_over`, nudges, the tried-once bound)
7350/// moves. The rescue excludes occupants too, on the same reasoning: retrying a
7351/// carried failure is a cheaper bet than doubling an agent on the panel, and
7352/// if it fails again `tried` bounds it and step 3 takes over. Seats failing in
7353/// the same round are handled one at a time, so a seat later in the batch
7354/// sees an earlier one's new occupant but not yet its own freed agent.
7355fn pick_successor<'a>(
7356    roster: &'a [AgentSpec],
7357    start: usize,
7358    tried: &BTreeSet<String>,
7359    carried: Option<&BTreeSet<String>>,
7360    others: &BTreeSet<String>,
7361) -> Option<&'a AgentSpec> {
7362    let free = |s: &&AgentSpec| !tried.contains(&s.id) && !others.contains(&s.id);
7363    roster
7364        .get(start + 1..)
7365        .and_then(|tail| {
7366            tail.iter()
7367                .filter(free)
7368                .find(|s| carried.is_none_or(|c| !c.contains(&s.id)))
7369        })
7370        .or_else(|| {
7371            carried
7372                .is_some()
7373                .then(|| roster.iter().find(free))
7374                .flatten()
7375        })
7376        .or_else(|| match carried {
7377            Some(c) => next_for_seat(roster, start, tried, c),
7378            None => next_untried_in_roster(roster, start, tried),
7379        })
7380}
7381
7382/// The next agent for a seat that carries its failure history across rounds
7383/// (the review loop). `round_tried` is this round's own bound and starts
7384/// empty every round; `carried_failed` only decides priority.
7385///
7386/// First: an id walking forward from `start`, never wrapping, that is neither
7387/// tried this round nor failed in an earlier one. Only when that is exhausted
7388/// does it rescue: the first roster id (in roster order, so this one *does*
7389/// look before `start`) not yet tried this round, which is by then a carried
7390/// failure. Each id is rescued at most once per round, so it cannot loop.
7391fn next_for_seat<'a>(
7392    roster: &'a [AgentSpec],
7393    start: usize,
7394    round_tried: &BTreeSet<String>,
7395    carried_failed: &BTreeSet<String>,
7396) -> Option<&'a AgentSpec> {
7397    roster
7398        .get(start + 1..)?
7399        .iter()
7400        .find(|s| !round_tried.contains(&s.id) && !carried_failed.contains(&s.id))
7401        .or_else(|| roster.iter().find(|s| !round_tried.contains(&s.id)))
7402}
7403
7404/// Where a reviewer seat starts a round: the agent that last answered it when
7405/// it is still on the roster and not marked failed, else the spec's own agent
7406/// unless it failed, else the next roster agent that has not failed, else the
7407/// spec's own agent again (the whole roster failed). Ids no longer on the
7408/// roster are ignored. An empty roster has no handover, so the spec stands.
7409fn pick_start_spec(roster: &[AgentSpec], spec: AgentSpec, hist: Option<&SeatHistory>) -> AgentSpec {
7410    let Some(h) = hist.filter(|_| !roster.is_empty()) else {
7411        return spec;
7412    };
7413    let ok = |id: &str| !h.failed.contains(id);
7414    if let Some(last) = h.last_ok.as_deref()
7415        && ok(last)
7416        && let Some(s) = roster.iter().find(|s| s.id == last)
7417    {
7418        return s.clone();
7419    }
7420    if ok(&spec.id) {
7421        return spec;
7422    }
7423    let from = roster.iter().position(|s| s.id == spec.id).unwrap_or(0);
7424    roster
7425        .get(from + 1..)
7426        .into_iter()
7427        .flatten()
7428        .chain(roster.iter())
7429        .find(|s| ok(&s.id))
7430        .cloned()
7431        .unwrap_or(spec)
7432}
7433
7434/// A fresh seat for the agent taking over `key`. Mixes the agent id into the
7435/// seed so a CLI that mints its session id up front (`--session-id`) never
7436/// reuses the uuid the previous agent already opened under the same seat key.
7437pub(crate) fn handover_seat(key: &str, agent: &str, run_seed: u64) -> SeatState {
7438    SeatState::new(key, agent, run_seed ^ crate::rng::fnv1a(agent))
7439}
7440
7441/// What an agent's turn timed out as, in [`AgentOutcome::Failed`]. One const
7442/// so the classifier below and the code that builds the message cannot drift.
7443const TIMED_OUT: &str = "timed out";
7444
7445impl FailClass {
7446    /// `None` for an answer; otherwise how the turn failed.
7447    fn of(out: &AgentOutcome) -> Option<Self> {
7448        match out {
7449            AgentOutcome::Ok(_) => None,
7450            AgentOutcome::Quota(_) => Some(Self::Quota),
7451            AgentOutcome::Dropped(_) => Some(Self::Other("dropped".to_owned())),
7452            AgentOutcome::Failed(e) if e == TIMED_OUT => Some(Self::Timeout),
7453            AgentOutcome::Failed(e) => Some(Self::Other(failure_signature(e))),
7454        }
7455    }
7456
7457    /// The word in a handover's artifact stem (`impl-A-quota-beta`).
7458    fn stem_word(&self) -> &'static str {
7459        match self {
7460            Self::Quota => "quota",
7461            _ => "handover",
7462        }
7463    }
7464}
7465
7466/// The message's first line with its variable parts removed — digit runs and
7467/// path-like tokens — so "exited with Some(2)" and "exited with Some(7)" read
7468/// as one kind of failure.
7469fn failure_signature(msg: &str) -> String {
7470    let line = msg.lines().next().unwrap_or("").trim().to_lowercase();
7471    let mut out = Vec::new();
7472    for word in line.split_whitespace() {
7473        if word.contains('/') || word.contains('\\') {
7474            out.push("<path>".to_owned());
7475            continue;
7476        }
7477        let mut w = String::new();
7478        let mut in_digits = false;
7479        for c in word.chars() {
7480            if c.is_ascii_digit() {
7481                if !in_digits {
7482                    w.push('#');
7483                }
7484                in_digits = true;
7485            } else {
7486                in_digits = false;
7487                w.push(c);
7488            }
7489        }
7490        out.push(w);
7491    }
7492    out.join(" ").chars().take(120).collect()
7493}
7494
7495/// Whether a seat that just failed with `cur` may go to the next roster agent.
7496/// A quota or a timeout always may. Any other failure may not when the agent
7497/// before it failed the same way: an error the prompt causes would otherwise
7498/// walk the whole roster. `prev` is the class of the immediately preceding
7499/// agent's failure, so a quota or timeout in between breaks the run of
7500/// identical failures by itself.
7501fn should_hand_over(prev: Option<&FailClass>, cur: &FailClass) -> bool {
7502    match cur {
7503        FailClass::Quota | FailClass::Timeout => true,
7504        FailClass::Other(_) => prev != Some(cur),
7505    }
7506}
7507
7508/// A short human reason for a failed outcome, for the handover record.
7509fn fail_reason(out: &AgentOutcome) -> String {
7510    match out {
7511        AgentOutcome::Ok(_) => String::new(),
7512        AgentOutcome::Quota(_) => "rate limited (quota)".to_owned(),
7513        AgentOutcome::Dropped(o) => format!(
7514            "the CLI dropped the stream ({})",
7515            o.dropped
7516                .as_ref()
7517                .map(|d| d.why.as_str())
7518                .unwrap_or("it ended without delivering its answer")
7519        ),
7520        AgentOutcome::Failed(e) => e.lines().next().unwrap_or("").chars().take(160).collect(),
7521    }
7522}
7523
7524/// Note one handover in the run: the structured record and, in the timeline,
7525/// the sentence a person reads. A quota keeps the wording it always had.
7526pub(crate) fn record_handover(
7527    state: &mut RunState,
7528    node: &str,
7529    seat: &str,
7530    from: &str,
7531    to: &str,
7532    class: &FailClass,
7533    reason: &str,
7534) {
7535    let message = if *class == FailClass::Quota {
7536        format!("{seat}: rate limited (quota) on {from}; retrying with {to}")
7537    } else {
7538        format!("{seat}: handed over {from} -> {to} ({reason})")
7539    };
7540    state.event(node, message);
7541    state.handovers.push(Handover {
7542        at: Timestamp::now(),
7543        node: node.to_owned(),
7544        seat: seat.to_owned(),
7545        from: from.to_owned(),
7546        to: to.to_owned(),
7547        reason: reason.to_owned(),
7548    });
7549}
7550
7551/// Did this reply report running a command whose own CLI never confirmed an
7552/// exit status?
7553///
7554/// An [`agent::CommandEvidence`] only ever exists when the CLI reported the
7555/// command *finished* (see that type's own doc), so this can only be `true`
7556/// for a command whose completion event carried no readable exit code — not
7557/// for one that simply is not mentioned at all. That is the one signal this
7558/// crate can read, from the same record `state.jobs` renders, about a reply
7559/// standing next to work its own CLI cannot vouch for finishing; it is
7560/// deliberately not a check on the exit code's *value* (a fixer legitimately
7561/// runs a command that fails mid-iteration before it succeeds) and not a
7562/// guess at a command still running in the background (which emits no event
7563/// at all, and so leaves no evidence here to find).
7564fn has_unconfirmed_command(commands: &[agent::CommandEvidence]) -> bool {
7565    commands.iter().any(|c| c.exit_code.is_none())
7566}
7567
7568/// Whether a `NO CHANGE NEEDED` marker in an implementer's reply should be
7569/// trusted as a verified no-op — the adoption guard's own text-level half.
7570///
7571/// `usable` is the caller's `AgentOutput::usable()` (a clean CLI exit, not
7572/// timed out): a marker only earns the benefit of the doubt from a turn the
7573/// CLI itself vouches for finishing properly, the same house style
7574/// `resume_unconfirmed_commands` and `continue_fix_report` already hold a
7575/// *fix* report to for `commands`. A candidate that timed out, exited
7576/// non-zero, or left a command unconfirmed is read as the ordinary loss it
7577/// is, whatever prose it wrote — this returns `None` before it ever looks at
7578/// `text`. The remaining guards (the tree really is empty, the evidence is
7579/// non-empty) are the caller's: this only reads what the reply *claimed*.
7580fn verified_noop_claim(
7581    usable: bool,
7582    commands: &[agent::CommandEvidence],
7583    text: &str,
7584) -> Option<String> {
7585    (usable && !has_unconfirmed_command(commands))
7586        .then(|| verdict::verified_noop(text))
7587        .flatten()
7588}
7589
7590fn short(commit: &str) -> String {
7591    commit.chars().take(7).collect()
7592}
7593
7594fn make_executable(path: &Path) -> Result<()> {
7595    #[cfg(unix)]
7596    {
7597        use std::os::unix::fs::PermissionsExt as _;
7598        let mut perms = std::fs::metadata(path)?.permissions();
7599        perms.set_mode(0o755);
7600        std::fs::set_permissions(path, perms)?;
7601    }
7602    #[cfg(not(unix))]
7603    {
7604        let _ = path;
7605    }
7606    Ok(())
7607}
7608
7609/// What every seat in one batch shares: where the answers are attributed, the
7610/// prompt overlay they inherit, and the build cache they are told to use.
7611///
7612/// A struct rather than four more parameters: `wave` also needs the run's
7613/// state (to record who is answering right now) and the attempt number, and
7614/// eight positional arguments is both unreadable and a clippy error.
7615struct WaveCtx<'a> {
7616    /// Exported as `MAGI_RUN`, so a task an agent files names the run that
7617    /// paid for it.
7618    run: &'a str,
7619    /// Exported as `MAGI_NODE`, and the key the prompt overlay is chosen by.
7620    node: &'a str,
7621    prompts: &'a Prompts,
7622    /// The shared `CARGO_TARGET_DIR`, when the config declares one.
7623    cache: Option<&'a Path>,
7624    /// The review round this wave belongs to, for `"review"`/`"fix"` — see
7625    /// `JobRecord::round`. `None` for every other node.
7626    round: Option<usize>,
7627    /// Carry each seat's failed-agent history across waves (the review loop
7628    /// only): start-of-round priority and handover choice read
7629    /// [`RunState::seat_history`], and every answer or failure writes it.
7630    carry_seats: bool,
7631}
7632
7633/// Run one job, honouring the parallelism budget.
7634async fn run_one(
7635    job: SeatJob,
7636    sem: Arc<Semaphore>,
7637    ctx: &WaveCtx<'_>,
7638    state: &mut RunState,
7639    attempt: usize,
7640) -> (SeatState, AgentOutcome) {
7641    let (_, seat, out) = wave(vec![job], sem, ctx, state, attempt)
7642        .await
7643        .pop()
7644        .expect("one job in, one result out");
7645    (seat, out)
7646}
7647
7648/// Run every job concurrently, capped by the semaphore, preserving order.
7649///
7650/// Every seat in the batch is recorded into [`RunState::active`] before the
7651/// wave starts and cleared as each answer lands, so the run's own record says
7652/// who is still being waited on rather than only who finished.
7653async fn wave(
7654    jobs: Vec<SeatJob>,
7655    sem: Arc<Semaphore>,
7656    ctx: &WaveCtx<'_>,
7657    state: &mut RunState,
7658    attempt: usize,
7659) -> Vec<(usize, SeatState, AgentOutcome)> {
7660    let WaveCtx {
7661        run,
7662        node,
7663        prompts,
7664        cache,
7665        round,
7666        carry_seats: _,
7667    } = *ctx;
7668    for job in &jobs {
7669        state.seat_started(node, &job.seat.key, job.timeout, attempt);
7670    }
7671    if let Err(e) = state.save() {
7672        // A failed persist of "who is answering right now" must not abort the
7673        // wave: the seats are already being asked, and the alternative is
7674        // losing the answers to save a status line nobody may even be
7675        // watching.
7676        tracing::warn!("could not persist in-progress seats: {e:#}");
7677    }
7678    // Hold the shared build cache's lease for the whole batch, not per job:
7679    // several candidates (an implement wave) or a fixer legitimately share
7680    // one cache concurrently within this run, and that stays untouched — a
7681    // single lease taken once for the whole wave and released once it is
7682    // done is what stops a *different* borrower (another run's own wave, its
7683    // e2e/gate, a human's `magi review`) from interleaving a build into the
7684    // same directory while this one is in flight. Best-effort, not
7685    // all-or-nothing: a wave that cannot get the lease within its own
7686    // longest job's budget still runs — an hour of paid implementer calls is
7687    // not thrown away over cache contention — but every write-allowed seat
7688    // then goes without `CARGO_TARGET_DIR` for this wave too (see the filter
7689    // below), the same fallback a read-only seat always gets, rather than
7690    // building into a directory this run was never granted. The identity
7691    // record is still invalidated below either way, so the next tracked
7692    // caller (`e2e`/`gate`) never trusts a match it cannot vouch for.
7693    let jobs_had_a_writer = jobs.iter().any(|j| j.allow_write);
7694    let wait_started = Instant::now();
7695    let cache_guard = if let Some(cache_dir) = cache {
7696        if jobs_had_a_writer {
7697            let owner = crate::cache::Owner::here(run, node, "*", Path::new("(wave)"), "");
7698            let budget = jobs
7699                .iter()
7700                .map(|j| j.timeout)
7701                .max()
7702                .unwrap_or(Duration::from_secs(60));
7703            acquire_cache_lease(state, cache_dir, &owner, budget, node)
7704                .await
7705                .ok()
7706        } else {
7707            None
7708        }
7709    } else {
7710        None
7711    };
7712    // Carved out of each job's own budget, not added on top of it: a seat
7713    // that waited behind the lease must not also get its full timeout
7714    // afterward, or a run contended on the cache could double the time it
7715    // spends per wave. `saturating_sub` floors at zero rather than
7716    // wrapping - a job whose whole budget was spent waiting starts with
7717    // none left, which is the honest number, not a free minimum.
7718    let waited_for_lease = wait_started.elapsed();
7719    let mut set = tokio::task::JoinSet::new();
7720    let overlay = prompts.overlay(node);
7721    for (i, mut job) in jobs.into_iter().enumerate() {
7722        job.timeout = job.timeout.saturating_sub(waited_for_lease);
7723        job.prompt = prompt::with_overlay(job.prompt, overlay.clone());
7724        if cache.is_some() {
7725            job.prompt.push('\n');
7726            job.prompt
7727                .push_str(&prompt::build_cache_note(node, job.allow_write));
7728        }
7729        let sem = Arc::clone(&sem);
7730        let run = run.to_owned();
7731        let node = node.to_owned();
7732        // Only implementers were told about the task's attachments, so only
7733        // their seats get the directory widened for reading.
7734        let attachments = if node == "implement" {
7735            state.attachments.clone()
7736        } else {
7737            Vec::new()
7738        };
7739        // A read-only seat is never handed `CARGO_TARGET_DIR` — see
7740        // `prompt::build_cache_note`'s doc for why setting it anyway is
7741        // exactly how a sandboxed reviewer's write refusal got reported as a
7742        // defect in the patch, not a property of its own seat. And a
7743        // write-allowed one is handed it only when the lease above was
7744        // actually acquired: a wave that could not get it (`cache_guard` is
7745        // `None`, see its own comment) must not send seats to build into a
7746        // directory this run does not hold - that is the exact concurrent,
7747        // unmanaged-write race this module exists to prevent, not something
7748        // "proceeding anyway" is allowed to reintroduce.
7749        let cache = cache
7750            .filter(|_| job.allow_write && cache_guard.is_some())
7751            .map(Path::to_path_buf);
7752        set.spawn(async move {
7753            let _permit = sem.acquire().await;
7754            let mut seat = job.seat;
7755            let out = agent::invoke(
7756                &job.spec,
7757                &mut seat,
7758                &Invocation {
7759                    cwd: &job.cwd,
7760                    prompt: &job.prompt,
7761                    timeout: job.timeout,
7762                    allow_write: job.allow_write,
7763                    unsandboxed: false,
7764                    sessions: job.sessions,
7765                    artifacts: &job.artifacts,
7766                    stem: &job.stem,
7767                    run: &run,
7768                    node: &node,
7769                    cache_dir: cache.as_deref(),
7770                    attachments: &attachments,
7771                    writable: &[],
7772                },
7773            )
7774            .await;
7775            let out = match out {
7776                Ok(o) if o.usable() => AgentOutcome::Ok(o),
7777                Ok(o) if o.quota_exhausted() => AgentOutcome::Quota(o),
7778                // Billed work the CLI failed to hand over is not an ordinary
7779                // failure, but its text is the CLI's raw error JSON, not an
7780                // answer — `Dropped` keeps it out of `Ok` so a caller cannot
7781                // read it as one by forgetting to check. `usable()` is always
7782                // false here (dropped implies an empty response), so this has
7783                // to be checked before the catch-all `Failed` below or the
7784                // one shape this exists for is lost with the rest.
7785                Ok(o) if o.work_undelivered() => AgentOutcome::Dropped(o),
7786                Ok(o) if o.timed_out => AgentOutcome::Failed(TIMED_OUT.to_owned()),
7787                Ok(o) => AgentOutcome::Failed(format!(
7788                    "exited with {:?} and no usable output",
7789                    o.exit_code
7790                )),
7791                Err(e) => AgentOutcome::Failed(e.to_string()),
7792            };
7793            (i, seat, out)
7794        });
7795    }
7796    let mut collected: Vec<Option<(usize, SeatState, AgentOutcome)>> = Vec::new();
7797    while let Some(joined) = set.join_next().await {
7798        let (i, seat, out) = match joined {
7799            Ok(v) => v,
7800            // No seat to clear: a panicked task never reported which one it
7801            // was. The defensive sweep below this loop is what stops that
7802            // seat's `active` entry from surviving forever.
7803            Err(e) => {
7804                tracing::error!("agent task panicked: {e}");
7805                continue;
7806            }
7807        };
7808        state.seat_finished(&seat.key);
7809        record_jobs(state, node, round, &seat.key, &out);
7810        if let Err(e) = state.save() {
7811            tracing::warn!("could not persist a seat's completion: {e:#}");
7812        }
7813        if collected.len() <= i {
7814            collected.resize_with(i + 1, || None);
7815        }
7816        collected[i] = Some((i, seat, out));
7817    }
7818    // Belt-and-braces for the panic branch above: every seat this exact batch
7819    // started shares this `(node, attempt)` pair, and every seat that finished
7820    // normally already cleared itself, so anything left tagged with it here
7821    // can only be a panicked task's leftover. Cleared unconditionally rather
7822    // than left to read as still answering forever.
7823    if state
7824        .active
7825        .values()
7826        .any(|a| a.node == node && a.attempt == attempt)
7827    {
7828        state
7829            .active
7830            .retain(|_, a| !(a.node == node && a.attempt == attempt));
7831        if let Err(e) = state.save() {
7832            tracing::warn!("could not persist the end of a wave: {e:#}");
7833        }
7834    }
7835    // Whether or not the lease above was actually held, several worktrees
7836    // may just have built into the cache with nothing here able to name one
7837    // coherent (worktree, head) for it - see `cache::invalidate_identity`'s
7838    // own doc. Forgetting the old record costs the next `e2e`/`gate` one
7839    // clean it might not have strictly needed; trusting a stale match would
7840    // cost it a wrong answer.
7841    if let Some(cache_dir) = cache
7842        && jobs_had_a_writer
7843    {
7844        crate::cache::invalidate_identity(&crate::run::home(), cache_dir);
7845    }
7846    if let Some(guard) = cache_guard {
7847        guard.release();
7848    }
7849    collected.into_iter().flatten().collect()
7850}
7851
7852/// Fold one seat's [`agent::CommandEvidence`] (if its outcome carries any)
7853/// into the run's [`JobRecord`] log — every node, every seat, uniformly:
7854/// this is data collection, not the fix-specific completion contract in
7855/// [`Runner::continue_fix_report`], and applies regardless of which node
7856/// asked.
7857///
7858/// Only `AgentOutcome::Ok`/`Quota`/`Dropped` carry an [`AgentOutput`] to read
7859/// evidence from; `Failed` does not, and correctly contributes nothing — a
7860/// timeout or crash is not itself evidence about a command the seat may have
7861/// started.
7862fn record_jobs(
7863    state: &mut RunState,
7864    node: &str,
7865    round: Option<usize>,
7866    seat: &str,
7867    out: &AgentOutcome,
7868) {
7869    let commands: &[agent::CommandEvidence] = match out {
7870        AgentOutcome::Ok(o) | AgentOutcome::Quota(o) | AgentOutcome::Dropped(o) => &o.commands,
7871        AgentOutcome::Failed(_) => &[],
7872    };
7873    let checked_at = Timestamp::now();
7874    for c in commands {
7875        state.jobs.push(JobRecord {
7876            node: node.to_owned(),
7877            round,
7878            seat: seat.to_owned(),
7879            id: c.id.clone(),
7880            description: c.description.clone(),
7881            checked_at,
7882            status: match c.exit_code {
7883                Some(0) => JobStatus::Completed,
7884                Some(_) => JobStatus::Failed,
7885                None => JobStatus::Unknown,
7886            },
7887            exit_code: c.exit_code,
7888            result_summary: c.result_summary.clone(),
7889            source: c.source.clone(),
7890        });
7891    }
7892}
7893
7894/// Is a review round clean, given how many reviewer seats answered against
7895/// how many the round expected?
7896///
7897/// A seat that never answered (timeout, crash, unparsable output) is not a
7898/// seat that read the patch and found nothing — treating it as such is
7899/// exactly the bug this function exists to close. Under the default `block`
7900/// policy a missing seat can never be clean; `warn` still requires the seats
7901/// that *did* answer to have found nothing blocking and verification to be
7902/// green.
7903///
7904/// `quota_missing` narrows that `block` default for exactly one cause of
7905/// absence: a seat lost to its own rate limit this round. Re-reviewing hoping
7906/// a session limit lifts by the very next round buys nothing — the seat is
7907/// asked again with the same quota — so once every missing seat is accounted
7908/// for by a quota loss (and at least one seat *did* answer, so a decision has
7909/// something to rest on) the round is decided on the panel that could answer,
7910/// same as `warn` would. A panel that lost every seat to quota is not
7911/// decided here: `answered == 0` falls through to the existing `block`
7912/// fallback so a fully collapsed panel still waits rather than landing on no
7913/// review at all.
7914fn round_is_clean(
7915    blocking: usize,
7916    e2e_ok: bool,
7917    answered: usize,
7918    expected: usize,
7919    quota_missing: usize,
7920    policy: IncompleteReviewPolicy,
7921) -> bool {
7922    if blocking != 0 || !e2e_ok {
7923        return false;
7924    }
7925    if answered == expected || policy == IncompleteReviewPolicy::Warn {
7926        return true;
7927    }
7928    answered > 0 && expected - answered <= quota_missing
7929}
7930
7931/// The review loop's own conclusion, derived entirely from its persisted
7932/// round records and the round budget that produced them — never from
7933/// `status`, so a reentry (or `gate`/`merge` reading it independently)
7934/// recomputes the identical answer regardless of what an earlier node in the
7935/// same walk, or a previous walk, did to `status`.
7936///
7937/// `None` while more rounds remain to try, including when review never ran
7938/// at all (`review_rounds = 0`, or nothing yet recorded). Once a round has
7939/// gone clean, or the budget is spent, or the tree has stopped moving (see
7940/// [`STAGNANT_LIMIT`]), the answer is one of two things:
7941///
7942/// - An incomplete panel that raised nothing is missing input, not a
7943///   verified tree — never a hand-off candidate, whatever verification said
7944///   (see [`ReviewRound::incomplete`], `IncompleteReviewPolicy`).
7945/// - Otherwise, green e2e on the last round hands off (see
7946///   [`Runner::stop_reviewing`]); red e2e blocks.
7947///
7948/// A last round whose own verification is still `ResourceBlocked` — magi
7949/// itself never got a command to run, not evidence the patch is broken —
7950/// is neither: this returns `None` for it too, the same as "more rounds
7951/// remain", so a reentry retries the check (see `Runner::review_loop`'s own
7952/// handling of that shape) instead of this cheap recomputation guessing a
7953/// verdict a real attempt never produced.
7954fn review_conclusion(reviews: &[ReviewRound], max_rounds: usize) -> Option<RunStatus> {
7955    if max_rounds == 0 || reviews.iter().any(|r| r.clean) {
7956        return Some(RunStatus::Gating);
7957    }
7958    let last = reviews.last()?;
7959    let stagnant = reviews.iter().rev().take_while(|r| !r.progressed).count() >= STAGNANT_LIMIT;
7960    if reviews.len() < max_rounds && !stagnant {
7961        return None;
7962    }
7963    if last.incomplete() && last.blocking == 0 {
7964        return Some(RunStatus::Blocked);
7965    }
7966    if last.e2e_status() == E2eStatus::ResourceBlocked {
7967        return None;
7968    }
7969    Some(if last.e2e.iter().all(CommandOutcome::ok) {
7970        RunStatus::Gating
7971    } else {
7972        RunStatus::Blocked
7973    })
7974}
7975
7976/// How long a re-ask may take, given the budget the first attempt had.
7977///
7978/// A `nudged` retry is a request to restate an answer the seat has already
7979/// worked out: it carries no new work, so it does not deserve the original
7980/// budget. Measured on run 01c2, two judges restated their ranking in 41 and
7981/// 133 seconds while a third sat for over ten minutes on a resumed session
7982/// holding 410 KB of prior output - and because the retry had inherited the
7983/// full 1200s judge timeout, one stuck nudge nearly doubled the wall time of a
7984/// judging round whose other seats were long finished.
7985///
7986/// A quarter of the budget, with a floor so that a deliberately short timeout
7987/// does not collapse to nothing. A retry that re-sends the whole prompt
7988/// (because the seat kept no context) is the original job again, and keeps the
7989/// original budget.
7990pub(crate) fn retry_budget(full: Duration, nudged: bool) -> Duration {
7991    if nudged {
7992        (full / 4).max(Duration::from_secs(120)).min(full)
7993    } else {
7994        full
7995    }
7996}
7997
7998/// Run a wave and parse each reply, re-asking the seats whose reply was
7999/// unusable.
8000///
8001/// The re-ask is a nudge rather than the whole prompt again when the seat still
8002/// holds its conversation, which is the difference between a cheap retry and
8003/// paying for the entire candidate set twice.
8004///
8005/// A seat whose agent *fails* (rate limit, timeout, any other error) and has a
8006/// successor in `roster` is handed to it instead of being re-asked: the
8007/// handover is the retry. A seat with no successor left (a single-agent
8008/// roster, the roster's tail) is nudged as before, up to `retries` times. So
8009/// the asks to one seat in one node number at most
8010/// `roster.len().max(1) * (1 + retries)`; an agent that still has a successor
8011/// is asked once (a dropped stream is nudged first), and only the last agent
8012/// of the chain gets the `retries` same-agent nudges. Each roster agent is
8013/// tried at most once per seat, walking forward from the seat's own position and never wrapping
8014/// ([`next_untried_in_roster`]); a quota or timeout always hands over, any
8015/// other failure stops the chain when the previous agent failed the same way
8016/// ([`should_hand_over`]). The new agent takes a fresh [`SeatState`], so
8017/// [`has_context`] is false and the job's own full prompt and full budget are
8018/// sent. A seat whose chain ends on a quota records one [`QuotaLoss`] (the
8019/// intermediate ones are not losses) and is returned as a failure like any
8020/// other absent seat — the caller decides whether the panel still has a
8021/// quorum. An empty `roster` disables handover: failures are nudged as they
8022/// always were, and a quota is simply lost. A reply that fails to parse or
8023/// validate is the prompt's doing and is only ever nudged, never handed over.
8024///
8025/// The returned [`SeatState`] names the agent that answered (or tried last).
8026#[allow(clippy::too_many_arguments)]
8027async fn ask_json_wave<T>(
8028    jobs: Vec<SeatJob>,
8029    sem: Arc<Semaphore>,
8030    retries: usize,
8031    roster: &[AgentSpec],
8032    ctx: &WaveCtx<'_>,
8033    losses: &mut Vec<QuotaLoss>,
8034    state: &mut RunState,
8035    validate: &(dyn Fn(&T) -> Result<()> + Send + Sync),
8036) -> Vec<(SeatState, Result<(T, AgentOutput)>, usize)>
8037where
8038    T: serde::de::DeserializeOwned + Send + 'static,
8039{
8040    ask_wave_with(
8041        jobs,
8042        sem,
8043        retries,
8044        roster,
8045        ctx,
8046        losses,
8047        state,
8048        &|text: &str| {
8049            let v = verdict::extract_json::<T>(text)?;
8050            validate(&v)?;
8051            Ok(v)
8052        },
8053    )
8054    .await
8055}
8056
8057/// [`ask_json_wave`] with the reading of an answer supplied by the caller, so
8058/// a node whose answer is prose (deliberation) shares the same handover,
8059/// failure classification, quota bookkeeping and bounds instead of a copy.
8060///
8061/// A seat handed to another roster agent is sent the job's `handover` prompt
8062/// (when it has one) rather than `prompt`: the new agent has no session, so a
8063/// resume-style prompt would be incomplete. That holds for the handover ask
8064/// and for every nudge to that agent whose `has_context` is false.
8065#[allow(clippy::too_many_arguments)]
8066async fn ask_wave_with<T>(
8067    jobs: Vec<SeatJob>,
8068    sem: Arc<Semaphore>,
8069    retries: usize,
8070    roster: &[AgentSpec],
8071    ctx: &WaveCtx<'_>,
8072    losses: &mut Vec<QuotaLoss>,
8073    state: &mut RunState,
8074    parse: &(dyn Fn(&str) -> Result<T> + Send + Sync),
8075) -> Vec<(SeatState, Result<(T, AgentOutput)>, usize)>
8076where
8077    T: Send + 'static,
8078{
8079    let n = jobs.len();
8080    let originals: Vec<SeatJob> = jobs;
8081    let mut seats: Vec<SeatState> = originals.iter().map(|j| j.seat.clone()).collect();
8082    let mut done: Vec<Option<Result<(T, AgentOutput)>>> = (0..n).map(|_| None).collect();
8083    // Nudges each seat's *current* agent has taken — 0 for a first-ask
8084    // answer, N once it has gone through N nudges. Read back once this
8085    // returns, so a caller building a history record (`ReviewRecord`) can
8086    // tell "never answered" (`failed: Some(_)`, `attempts == 0`) apart from
8087    // "recovered after a nudge" (`failed: None`, `attempts > 0`) — see that
8088    // field's own doc.
8089    let mut nudges: Vec<usize> = vec![0; n];
8090    // The agent now occupying each seat, the ids it has already been through,
8091    // where in the roster the walk began, the class of the last failure, and
8092    // the stem word of a handover not yet asked (full prompt, full budget).
8093    let mut specs: Vec<AgentSpec> = originals.iter().map(|j| j.spec.clone()).collect();
8094    let mut tried: Vec<BTreeSet<String>> = specs
8095        .iter()
8096        .map(|s| BTreeSet::from([s.id.clone()]))
8097        .collect();
8098    let starts: Vec<usize> = specs
8099        .iter()
8100        .map(|s| roster.iter().position(|r| r.id == s.id).unwrap_or(0))
8101        .collect();
8102    let carry = ctx.carry_seats && !roster.is_empty();
8103    // Carried across rounds: ids that failed the seat earlier, and how the
8104    // last failure went (so a repeat of it is not handed over again).
8105    let carried: Vec<BTreeSet<String>> = originals
8106        .iter()
8107        .map(|j| {
8108            state
8109                .seat_history
8110                .get(&j.seat.key)
8111                .filter(|_| carry)
8112                .map(|h| h.failed.clone())
8113                .unwrap_or_default()
8114        })
8115        .collect();
8116    let mut prev: Vec<Option<FailClass>> = originals
8117        .iter()
8118        .map(|j| {
8119            state
8120                .seat_history
8121                .get(&j.seat.key)
8122                .filter(|_| carry)
8123                .and_then(|h| h.last_fail.clone())
8124        })
8125        .collect();
8126    let next_agent =
8127        |i: usize, tried: &BTreeSet<String>, specs: &[AgentSpec]| -> Option<AgentSpec> {
8128            let others: BTreeSet<String> = specs
8129                .iter()
8130                .enumerate()
8131                .filter(|(j, _)| *j != i)
8132                .map(|(_, s)| s.id.clone())
8133                .collect();
8134            pick_successor(
8135                roster,
8136                starts[i],
8137                tried,
8138                carry.then(|| &carried[i]),
8139                &others,
8140            )
8141            .cloned()
8142        };
8143    let mut fresh: Vec<Option<String>> = vec![None; n];
8144    let mut last_quota: Vec<Option<Option<String>>> = vec![None; n];
8145    let mut pending: Vec<usize> = (0..n).collect();
8146
8147    // Per seat the work is bounded by `roster.len().max(1) * (1 + retries)`
8148    // asks: an agent with a successor is asked once and handed over, and only
8149    // a seat with no successor spends `retries` nudges on the same agent. This
8150    // only guarantees the loop's own termination whatever those say.
8151    let max_rounds = (retries + 1) * roster.len().max(1) + 1;
8152    for round in 0..max_rounds {
8153        if pending.is_empty() {
8154            break;
8155        }
8156        let mut batch = Vec::with_capacity(pending.len());
8157        let mut renudged: Vec<&str> = Vec::new();
8158        for &i in &pending {
8159            let src = &originals[i];
8160            // A seat now held by another agent than the job named has no
8161            // session of its own: it gets the full-context prompt whenever
8162            // it is asked in full (the handover ask, a nudge it cannot
8163            // resume).
8164            let full: &str = match &src.handover {
8165                Some(h) if specs[i].id != src.spec.id => h,
8166                _ => &src.prompt,
8167            };
8168            // The prompt and the budget are one decision: a nudge restates
8169            // finished work, a re-sent prompt redoes it.
8170            let (prompt, timeout, stem) = if let Some(word) = fresh[i].take() {
8171                (
8172                    full.to_owned(),
8173                    src.timeout,
8174                    format!("{}-{word}-{}", src.stem, specs[i].id),
8175                )
8176            } else if nudges[i] == 0 {
8177                (full.to_owned(), src.timeout, src.stem.clone())
8178            } else {
8179                renudged.push(src.seat.key.as_str());
8180                let why = done[i]
8181                    .as_ref()
8182                    .and_then(|r| r.as_ref().err().map(ToString::to_string))
8183                    .unwrap_or_else(|| "no parsable answer".to_owned());
8184                let nudge = prompt::nudge(&why);
8185                let nudged = has_context(&specs[i], &seats[i], src.sessions);
8186                let prompt = if nudged {
8187                    nudge
8188                } else {
8189                    format!("{full}\n\n---\n\n{nudge}")
8190                };
8191                (
8192                    prompt,
8193                    retry_budget(src.timeout, nudged),
8194                    format!("{}-retry{}", src.stem, nudges[i]),
8195                )
8196            };
8197            batch.push(SeatJob {
8198                spec: specs[i].clone(),
8199                seat: seats[i].clone(),
8200                cwd: src.cwd.clone(),
8201                prompt,
8202                timeout,
8203                allow_write: src.allow_write,
8204                sessions: src.sessions,
8205                artifacts: src.artifacts.clone(),
8206                stem,
8207                handover: None,
8208            });
8209        }
8210
8211        if !renudged.is_empty() {
8212            state.event(
8213                ctx.node,
8214                format!("retry {round}: re-asking {}", renudged.join(", ")),
8215            );
8216        }
8217        let results = wave(batch, Arc::clone(&sem), ctx, state, round).await;
8218        let mut still = Vec::new();
8219        for (&i, (_wi, seat, out)) in pending.iter().zip(results) {
8220            seats[i] = seat;
8221            let class = FailClass::of(&out);
8222            // A dropped stream is nudged first (the conversation is still
8223            // there to pick up); only a seat whose nudges are spent hands over.
8224            let nudge_first = matches!(out, AgentOutcome::Dropped(_))
8225                && nudges[i] < retries
8226                && !roster.is_empty();
8227            if let Some(cur) = class.clone().filter(|_| !roster.is_empty() && !nudge_first) {
8228                let next = should_hand_over(prev[i].as_ref(), &cur)
8229                    .then(|| next_agent(i, &tried[i], &specs))
8230                    .flatten();
8231                if carry {
8232                    let h = state
8233                        .seat_history
8234                        .entry(originals[i].seat.key.clone())
8235                        .or_default();
8236                    h.failed.insert(specs[i].id.clone());
8237                    h.last_fail = Some(cur.clone());
8238                    if h.last_ok.as_deref() == Some(specs[i].id.as_str()) {
8239                        h.last_ok = None;
8240                    }
8241                    // Saved before the next agent is asked, so a restart in
8242                    // between does not forget who failed.
8243                    if let Err(e) = state.save() {
8244                        tracing::warn!("could not persist a seat's failure history: {e:#}");
8245                    }
8246                }
8247                if let Some(next) = next {
8248                    record_handover(
8249                        state,
8250                        ctx.node,
8251                        &originals[i].seat.key,
8252                        &specs[i].id,
8253                        &next.id,
8254                        &cur,
8255                        &fail_reason(&out),
8256                    );
8257                    tried[i].insert(next.id.clone());
8258                    prev[i] = Some(cur.clone());
8259                    seats[i] =
8260                        handover_seat(&originals[i].seat.key, &next.id, state.next_seat_seed());
8261                    specs[i] = next;
8262                    fresh[i] = Some(cur.stem_word().to_owned());
8263                    nudges[i] = 0;
8264                    done[i] = Some(Err(anyhow::anyhow!(
8265                        "handed over after: {}",
8266                        fail_reason(&out)
8267                    )));
8268                    still.push(i);
8269                    continue;
8270                }
8271            }
8272            if carry
8273                && !nudge_first
8274                && let Some(cur) = class.clone()
8275            {
8276                // The chain ended here (no successor, or a repeated failure).
8277                let h = state
8278                    .seat_history
8279                    .entry(originals[i].seat.key.clone())
8280                    .or_default();
8281                h.failed.insert(specs[i].id.clone());
8282                h.last_fail = Some(cur);
8283            }
8284            let parsed = match out {
8285                AgentOutcome::Ok(o) => parse(&o.text).map(|v| (v, o)),
8286                AgentOutcome::Quota(o) => {
8287                    last_quota[i] = Some(o.quota.as_ref().and_then(|q| q.reset.clone()));
8288                    Err(anyhow::anyhow!("rate limited (quota); not retrying now"))
8289                }
8290                // Not a parseable answer: the nudge loop re-asks it, which is
8291                // exactly what a dropped stream needs. Just don't hand its raw
8292                // error JSON to `extract_json`.
8293                AgentOutcome::Dropped(o) => {
8294                    let why = o
8295                        .dropped
8296                        .as_ref()
8297                        .map(|d| d.why.as_str())
8298                        .unwrap_or("the CLI ended the stream without delivering its answer");
8299                    Err(anyhow::anyhow!("the CLI dropped the stream ({why})"))
8300                }
8301                AgentOutcome::Failed(e) => Err(anyhow::anyhow!(e)),
8302            };
8303            let quota = class == Some(FailClass::Quota);
8304            let failed = parsed.is_err();
8305            done[i] = Some(parsed);
8306            if carry && !failed {
8307                let h = state
8308                    .seat_history
8309                    .entry(originals[i].seat.key.clone())
8310                    .or_default();
8311                h.failed.remove(&specs[i].id);
8312                h.last_ok = Some(specs[i].id.clone());
8313                h.last_fail = None;
8314            }
8315            // Do not re-ask a rate-limited seat (quota) — a retry is known to
8316            // fail the same way; and never re-ask a seat that already parsed.
8317            // A failed agent that still has a successor is not re-asked
8318            // either: the handover was its remedy and has just been refused
8319            // (the chain stops on a repeated failure class). A seat with no
8320            // successor left (a single-agent roster, the roster's tail, or an
8321            // empty roster) keeps the same-agent nudge, bounded by `retries`.
8322            let agent_failure = class.is_some()
8323                && !nudge_first
8324                && !roster.is_empty()
8325                && next_agent(i, &tried[i], &specs).is_some();
8326            if failed && !quota && !agent_failure && nudges[i] < retries {
8327                nudges[i] += 1;
8328                still.push(i);
8329            }
8330        }
8331        pending = still;
8332    }
8333
8334    // One loss per seat whose chain ended on a quota: the intermediate ones
8335    // were absorbed by a handover and are not losses.
8336    for (i, q) in last_quota.into_iter().enumerate() {
8337        if let Some(reset) = q {
8338            losses.push(QuotaLoss {
8339                seat: originals[i].seat.key.clone(),
8340                node: ctx.node.to_owned(),
8341                at: Timestamp::now(),
8342                reset,
8343            });
8344        }
8345    }
8346
8347    seats
8348        .into_iter()
8349        .zip(done)
8350        .zip(nudges)
8351        .map(|((seat, res), attempts)| {
8352            (
8353                seat,
8354                res.unwrap_or_else(|| Err(anyhow::anyhow!("no attempt was made"))),
8355                attempts,
8356            )
8357        })
8358        .collect()
8359}
8360
8361/// Acquire the shared build cache's lease, waiting out contention within
8362/// `budget` (never past it — see AGENTS.md's build-cache section on why an
8363/// unbounded wait is never acceptable).
8364///
8365/// A first, non-blocking check happens before ever waiting; if it finds the
8366/// lease busy, that fact is logged as a `verify` event *and* flushed with
8367/// [`RunState::save`] immediately — not only once the wait finally succeeds
8368/// or gives up — so a `magi show` run by a different process while this one
8369/// is still waiting reads a `run.json` that says so, rather than whatever it
8370/// looked like before the wait started. The same applies to the terminal
8371/// failure: logged and saved before this returns `Err`, so a caller that
8372/// could not get the lease at all still leaves a legible record of why.
8373async fn acquire_cache_lease(
8374    state: &mut RunState,
8375    cache_dir: &Path,
8376    owner: &crate::cache::Owner,
8377    budget: Duration,
8378    context: &str,
8379) -> Result<crate::cache::Guard> {
8380    let home = crate::run::home();
8381    let started = Instant::now();
8382    let busy = match crate::cache::try_acquire(&home, cache_dir, owner) {
8383        Ok(crate::cache::AcquireOutcome::Acquired(g)) => return Ok(g),
8384        Ok(crate::cache::AcquireOutcome::Busy(busy)) => busy,
8385        Err(e) => {
8386            state.event(
8387                "verify",
8388                format!("{context}: could not check the shared build cache: {e:#}"),
8389            );
8390            if let Err(e2) = state.save() {
8391                tracing::warn!("could not persist a cache-check failure: {e2:#}");
8392            }
8393            return Err(e);
8394        }
8395    };
8396    state.event(
8397        "verify",
8398        format!(
8399            "{context}: waiting for the shared build cache at {} ({})",
8400            cache_dir.display(),
8401            busy.describe()
8402        ),
8403    );
8404    if let Err(e) = state.save() {
8405        tracing::warn!("could not persist a cache wait: {e:#}");
8406    }
8407    let remaining = budget.saturating_sub(started.elapsed());
8408    match crate::cache::wait_for(&home, cache_dir, owner, remaining, Duration::from_secs(5)).await {
8409        Ok(g) => Ok(g),
8410        Err(e) => {
8411            state.event("verify", format!("{context}: {e:#}"));
8412            if let Err(e2) = state.save() {
8413                tracing::warn!("could not persist a cache wait timeout: {e2:#}");
8414            }
8415            Err(e)
8416        }
8417    }
8418}
8419
8420/// Run `body` — a verify command batch — while holding the shared build
8421/// cache's lease, so this run's own full verification (`e2e`, `gate`) can
8422/// never interleave with another borrower's build against the same
8423/// `CARGO_TARGET_DIR`: a different run, a lingering reviewer past its
8424/// timeout, or a human's own `magi review`. See the `cache` module doc for
8425/// why this matters more than Cargo's own per-target locking covers — two
8426/// *different* worktrees building the same package name/version into one
8427/// cache directory is a staleness bug, not a lock contention one.
8428///
8429/// The wait for the lease is carved out of `budget`, never on top of it —
8430/// `body` is handed whatever is left, so a caller's own node timeout is the
8431/// only clock involved, exactly what AGENTS.md's build-cache section asks
8432/// for ("never an unbounded wait"). When `cache_dir` is `None` — no shared
8433/// cache configured at all — this is a pass-through: `body` runs with the
8434/// full budget and nothing is leased.
8435///
8436/// A lease that cannot be acquired within `budget` is reported as a single
8437/// synthetic [`CommandOutcome`] (`code: None`) rather than silently skipping
8438/// verification — the same shape a spawn failure already takes in
8439/// [`run_commands`], so a caller need not special-case it.
8440#[allow(clippy::too_many_arguments)]
8441async fn with_cache_lease<'s, F, Fut>(
8442    state: &'s mut RunState,
8443    cache_dir: Option<&Path>,
8444    node: &str,
8445    seat: &str,
8446    worktree: &Path,
8447    head: &str,
8448    budget: Duration,
8449    context: &str,
8450    body: F,
8451) -> (Vec<CommandOutcome>, bool)
8452where
8453    F: FnOnce(&'s mut RunState, Duration) -> Fut,
8454    Fut: std::future::Future<Output = (Vec<CommandOutcome>, bool, Vec<u32>)>,
8455{
8456    let Some(cache_dir) = cache_dir else {
8457        let (outcomes, retried, _timed_out_pids) = body(state, budget).await;
8458        return (outcomes, retried);
8459    };
8460    let home = crate::run::home();
8461    let owner = crate::cache::Owner::here(&state.id, node, seat, worktree, head);
8462    let started = Instant::now();
8463    let guard = match acquire_cache_lease(state, cache_dir, &owner, budget, context).await {
8464        Ok(g) => g,
8465        Err(e) => {
8466            return (
8467                vec![CommandOutcome {
8468                    command: "(waiting for the shared build cache)".to_owned(),
8469                    code: None,
8470                    output_tail: e.to_string(),
8471                    duration_ms: started.elapsed().as_millis() as u64,
8472                    resource_blocked: true,
8473                }],
8474                false,
8475            );
8476        }
8477    };
8478    let identity = crate::cache::Identity::new(worktree, head);
8479    if let Err(e) = crate::cache::ensure_fresh(
8480        &home,
8481        cache_dir,
8482        &identity,
8483        &state.config.verify.cargo_manifests(),
8484    ) {
8485        // A failed freshness check means this process cannot vouch for what
8486        // is sitting in the cache right now - on Windows this is exactly the
8487        // "a stale test executable is still locked, `cargo clean -p` cannot
8488        // remove it" case the evidence log records. Running verify anyway
8489        // and reporting whatever it says would let a result nobody can trust
8490        // stand for the tree it claims to have checked; fail the step
8491        // instead of the patch.
8492        state.event(
8493            "verify",
8494            format!(
8495                "{context}: could not confirm the shared build cache matches {} at {}: {e:#}",
8496                worktree.display(),
8497                short(head)
8498            ),
8499        );
8500        guard.release();
8501        return (
8502            vec![CommandOutcome {
8503                command: "(confirming the shared build cache is fresh)".to_owned(),
8504                code: None,
8505                output_tail: e.to_string(),
8506                duration_ms: started.elapsed().as_millis() as u64,
8507                resource_blocked: true,
8508            }],
8509            false,
8510        );
8511    }
8512    let remaining = budget.saturating_sub(started.elapsed());
8513    let (outcomes, retried, timed_out_pids) = body(state, remaining).await;
8514    // A timed-out command's process was only *asked* to die (`kill_on_drop`,
8515    // `start_kill`); confirm it actually has before handing the directory to
8516    // the next acquirer. See `wait_for_timed_out_children_to_die`'s own doc
8517    // for what this can and cannot see.
8518    if !timed_out_pids.is_empty() {
8519        wait_for_timed_out_children_to_die(&timed_out_pids).await;
8520    }
8521    guard.release();
8522    (outcomes, retried)
8523}
8524
8525/// Poll `pids` — commands [`run_commands`] reports as still running when its
8526/// own timeout elapsed — until every one is confirmed gone, or
8527/// [`LEASE_RELEASE_MAX_WAIT`] passes, whichever comes first.
8528///
8529/// Real confirmation where confirmation is possible, not a substitute for
8530/// full process-tree observation: a grandchild the timed-out process spawned
8531/// and that survives independently of it is invisible to a pid check the
8532/// same way it always was, and continuing to observe and collect *that*
8533/// stays a different piece of work with its own owner. This only narrows a
8534/// fixed blind wait into an actual check of the pids this process does know
8535/// about.
8536async fn wait_for_timed_out_children_to_die(pids: &[u32]) {
8537    wait_for_pids_with(
8538        pids,
8539        crate::proc::pid_alive,
8540        LEASE_RELEASE_POLL,
8541        LEASE_RELEASE_MAX_WAIT,
8542    )
8543    .await;
8544}
8545
8546/// [`wait_for_timed_out_children_to_die`] with its liveness query, poll
8547/// interval and ceiling supplied by the caller, so the polling *logic* -
8548/// returns as soon as every pid reports dead, gives up at the ceiling
8549/// otherwise - is testable on millisecond durations without asking the real
8550/// OS about a pid at all.
8551async fn wait_for_pids_with<F: Fn(u32) -> bool>(
8552    pids: &[u32],
8553    alive: F,
8554    poll: Duration,
8555    max_wait: Duration,
8556) {
8557    let deadline = Instant::now() + max_wait;
8558    loop {
8559        if pids.iter().all(|&pid| !alive(pid)) {
8560            return;
8561        }
8562        if Instant::now() >= deadline {
8563            return;
8564        }
8565        tokio::time::sleep(poll).await;
8566    }
8567}
8568
8569/// Are any of `outcomes` [`CommandOutcome::resource_blocked`] - magi's own
8570/// admission that it could not even get a verify command to run, as opposed
8571/// to evidence the command actually produced? A caller that would otherwise
8572/// read a resource-blocked outcome as a red command must check this first:
8573/// see [`Runner::gate`], which retries rather than records `Blocked` when
8574/// this is true.
8575fn verify_inconclusive(outcomes: &[CommandOutcome]) -> bool {
8576    outcomes.iter().any(|o| o.resource_blocked)
8577}
8578
8579/// What [`Runner::gate_fix_round`] decided.
8580enum GateFix {
8581    /// The tree changed and `verify.e2e` is still green: run the gate again.
8582    Retry,
8583    /// No more rounds, nothing to fix, or the fix did not hold: the gate's
8584    /// last failure stands and the run ends blocked.
8585    Stop,
8586    /// `verify.e2e` could not run after the fix (magi's own contention):
8587    /// decide nothing now, a later reentry retries.
8588    Defer,
8589}
8590
8591/// Is every red command in `outcomes` an ordinary failure the code could
8592/// explain: it ran, exited non-zero, and said something?
8593///
8594/// A timeout, a spawn failure and a killed process all leave `code` `None`;
8595/// 126 / 127 are the POSIX shell's "cannot execute" / "not found". Output-free
8596/// exits carry nothing for a fixer to act on. Language-agnostic on purpose:
8597/// what the command is stays the gate's business.
8598fn gate_fixable(outcomes: &[CommandOutcome]) -> bool {
8599    let mut red = outcomes.iter().filter(|o| !o.ok()).peekable();
8600    red.peek().is_some()
8601        && red.all(|o| {
8602            !o.resource_blocked
8603                && matches!(o.code, Some(c) if c != 0 && c != 126 && c != 127)
8604                && !o.output_tail.trim().is_empty()
8605        })
8606}
8607
8608/// Describe one verify command's outcome for the event log, distinguishing a
8609/// build/link failure — the toolchain never produced a binary to run — from
8610/// an actual test failure, since only the latter is a verdict on the patch.
8611fn e2e_outcome_label(o: &CommandOutcome) -> String {
8612    if o.ok() {
8613        return "pass".to_owned();
8614    }
8615    let reason = if o.build_failed() {
8616        format!("COULD NOT RUN ({:?}, build/link failure)", o.code)
8617    } else {
8618        format!("FAIL ({:?})", o.code)
8619    };
8620    format!("{reason}\n{}", tail(&o.output_tail, EVENT_OUTPUT_TAIL))
8621}
8622
8623/// Run `verify.e2e`, retrying once if the first attempt could not build or
8624/// link — a build/link failure is frequently a race against a shared
8625/// `CARGO_TARGET_DIR` (see AGENTS.md), not a verdict on the patch. Emits one
8626/// `verify` event per command, tagged with `context` (normally `"round N"`)
8627/// so the two call sites that need this — the ordinary per-round leg in
8628/// `review_loop`, and the deferred catch-up run `stop_reviewing` makes before
8629/// it will ever call a round green — read identically in the event log.
8630async fn run_e2e_with_retry(
8631    state: &mut RunState,
8632    shell: &[String],
8633    commands: &[String],
8634    worktree: &Path,
8635    timeout: Duration,
8636    context: &str,
8637) -> (Vec<CommandOutcome>, bool, Vec<u32>) {
8638    let (mut e2e, mut timed_out_pids) = run_commands(
8639        state, "verify", "e2e", 0, shell, commands, worktree, timeout,
8640    )
8641    .await;
8642    for o in &e2e {
8643        state.event(
8644            "verify",
8645            format!("{context}: `{}` -> {}", o.command, e2e_outcome_label(o)),
8646        );
8647    }
8648    // A build/link failure is not a verdict on the patch — it is frequently a
8649    // race against a shared `CARGO_TARGET_DIR` (see AGENTS.md). Give verify
8650    // one retry before letting a red like that decide the round.
8651    let verify_retried = e2e.iter().any(CommandOutcome::build_failed);
8652    if verify_retried {
8653        state.event(
8654            "verify",
8655            format!(
8656                "{context}: verify could not build/link, not a test result — retrying once \
8657                 before concluding"
8658            ),
8659        );
8660        let retried = run_commands(
8661            state, "verify", "e2e", 1, shell, commands, worktree, timeout,
8662        )
8663        .await;
8664        e2e = retried.0;
8665        // Both attempts' timeouts matter, not just the last one: the first
8666        // attempt's descendants may still be alive alongside the retry's.
8667        timed_out_pids.extend(retried.1);
8668        for o in &e2e {
8669            state.event(
8670                "verify",
8671                format!(
8672                    "{context}: retry `{}` -> {}",
8673                    o.command,
8674                    e2e_outcome_label(o)
8675                ),
8676            );
8677        }
8678    }
8679    (e2e, verify_retried, timed_out_pids)
8680}
8681
8682/// Run configured shell commands in `cwd`, in order. The second element is
8683/// the pid of every command that hit `timeout` and was still running when
8684/// this stopped waiting on it (best-effort: `None` when the platform did not
8685/// hand one back) — see [`with_cache_lease`]'s use of it for why a caller
8686/// that releases a shared resource afterward needs to know.
8687///
8688/// Records `task` into [`RunState::active`] at every command boundary
8689/// (`RunState::task_command`) and clears it once the whole list has run
8690/// (`RunState::task_finished`) — a `verify.e2e` / `verify.gate` list can run
8691/// for minutes with no seat and no output of its own to show for it (see
8692/// `CommandOutcome`'s doc on why an empty `e2e`/`gate` alone cannot be told
8693/// apart from "not yet run" without this), and this is the only place that
8694/// knows which command is running right now and how many are left. Three
8695/// saves per command — start, not per second — matching the same "only at a
8696/// boundary" rule [`wave`] already follows for seats.
8697#[allow(clippy::too_many_arguments)]
8698async fn run_commands(
8699    state: &mut RunState,
8700    node: &str,
8701    task: &str,
8702    attempt: usize,
8703    shell: &[String],
8704    commands: &[String],
8705    cwd: &Path,
8706    timeout: Duration,
8707) -> (Vec<CommandOutcome>, Vec<u32>) {
8708    if commands.is_empty() {
8709        // Nothing to mark as running and nothing to clear — an empty list
8710        // means "not configured", and touching `active` (or the disk) over
8711        // that would be a write for every round of a repo with no
8712        // `verify.e2e` / `verify.gate` commands at all.
8713        return (Vec::new(), Vec::new());
8714    }
8715    let mut out = Vec::new();
8716    let mut timed_out_pids = Vec::new();
8717    let total = commands.len();
8718    for (idx, command) in commands.iter().enumerate() {
8719        state.task_command(task, node, attempt, command, idx + 1, total, timeout);
8720        if let Err(e) = state.save() {
8721            tracing::warn!("could not persist an in-progress {task} command: {e:#}");
8722        }
8723        let started = Instant::now();
8724        let mut cmd = tokio::process::Command::new(&shell[0]);
8725        cmd.quiet();
8726        cmd.args(&shell[1..])
8727            .arg(command)
8728            .current_dir(cwd)
8729            .stdin(std::process::Stdio::null())
8730            .stdout(std::process::Stdio::piped())
8731            .stderr(std::process::Stdio::piped())
8732            .kill_on_drop(true);
8733        let spawned = cmd.spawn();
8734        let (code, body) = match spawned {
8735            Ok(child) => {
8736                // Captured before the child is consumed below: `kill_on_drop`
8737                // only *asks* the process to die when the timeout branch
8738                // drops it, and the pid is the only way anyone downstream can
8739                // later check whether that request actually took.
8740                let pid = child.id();
8741                match tokio::time::timeout(timeout, child.wait_with_output()).await {
8742                    Ok(Ok(o)) => {
8743                        let mut body = String::from_utf8_lossy(&o.stdout).into_owned();
8744                        body.push_str(&String::from_utf8_lossy(&o.stderr));
8745                        (o.status.code(), body)
8746                    }
8747                    Ok(Err(e)) => (None, format!("failed to run: {e}")),
8748                    Err(_) => {
8749                        if let Some(pid) = pid {
8750                            timed_out_pids.push(pid);
8751                        }
8752                        (None, format!("timed out after {}s", timeout.as_secs()))
8753                    }
8754                }
8755            }
8756            Err(e) => (None, format!("failed to spawn `{}`: {e}", shell[0])),
8757        };
8758        out.push(CommandOutcome {
8759            command: command.clone(),
8760            code,
8761            output_tail: tail(&body, OUTPUT_TAIL),
8762            duration_ms: started.elapsed().as_millis() as u64,
8763            resource_blocked: false,
8764        });
8765    }
8766    state.task_finished(task);
8767    if let Err(e) = state.save() {
8768        tracing::warn!("could not persist the end of {task}: {e:#}");
8769    }
8770    (out, timed_out_pids)
8771}
8772
8773/// The shell command line `mode = "none"` prints — in `magi show`'s `merge`
8774/// section (`report::run`) and in the `merge` event this node records — for
8775/// the operator to run by hand.
8776///
8777/// Built from [`MergeStyle`] rather than always `git merge --no-ff`: a base
8778/// branch whose ruleset forbids merge commits (GitHub's "must not contain
8779/// merge commits", or "require linear history") rejects the push a `--no-ff`
8780/// merge would produce, which is exactly the guidance this function replaces.
8781/// `message`'s first line becomes the squash commit's subject, matching the
8782/// note `report::run` prints alongside this command — see that function for
8783/// why an explicit subject is not optional there.
8784fn manual_merge_command(style: MergeStyle, repo: &Path, branch: &str, message: &str) -> String {
8785    let repo = repo.display();
8786    match style {
8787        MergeStyle::Merge => format!("git -C {repo} merge --no-ff {branch}"),
8788        MergeStyle::Squash => {
8789            // The subject sits inside double quotes, and a title an agent
8790            // wrote may carry the characters that break out of them.
8791            let subject = message
8792                .lines()
8793                .next()
8794                .unwrap_or(branch)
8795                .replace(['\\', '"', '$', '`'], "");
8796            format!(
8797                "git -C {repo} merge --squash {branch} && git -C {repo} commit -m \"{subject}\""
8798            )
8799        }
8800        MergeStyle::Rebase => format!("git -C {repo} merge --ff-only {branch}"),
8801    }
8802}
8803
8804/// GitHub's `createPullRequest` GraphQL mutation, which `gh pr create` calls
8805/// under the hood, rejects a `title` over 256 characters and the whole
8806/// command fails — no PR at all, for a run whose body was otherwise fine
8807/// (this is what happened to run 2963; see AGENTS.md). 240 leaves room below
8808/// that limit: titles are counted in `chars()` (Unicode scalars), which is not
8809/// always how GitHub counts. [`english_title`] keeps its trailing `...` inside
8810/// this bound. It is a margin, not a guarantee — a title packed
8811/// with multi-unit characters could still in principle land close to the
8812/// edge, but a real task title's occasional emoji or accented letter fits
8813/// comfortably inside it.
8814const PR_TITLE_MAX: usize = 240;
8815
8816/// A pull request title from the opening line of `text`, or `None` when that
8817/// line is not English (GitHub text is) or has no letters.
8818///
8819/// A line within `max` is kept as is. A longer one is cut at the end of its
8820/// first sentence when that falls inside `max`, else at a word boundary with a
8821/// plain `...` (ASCII, unlike the `…` `queue::title_from` appends, which would
8822/// make every merely-truncated title look non-English). The language check
8823/// runs on the kept text before any mark is added, so only what GitHub will
8824/// show is judged: an English opening followed by non-ASCII far past the cut
8825/// still passes.
8826fn english_title(text: &str, max: usize) -> Option<String> {
8827    let line = queue::first_line(text)?;
8828    let chars: Vec<char> = line.chars().collect();
8829    let (kept, mark) = if chars.len() <= max {
8830        (line.to_owned(), "")
8831    } else if let Some(end) = sentence_end(&chars, max) {
8832        (chars[..end].iter().collect::<String>(), "")
8833    } else {
8834        let room = max.saturating_sub(3);
8835        // Cut at the last space inside the room; when the char just past the
8836        // room is a space the room already ends on a word.
8837        let cut = if chars[room].is_whitespace() {
8838            room
8839        } else {
8840            chars[..room]
8841                .iter()
8842                .rposition(|c| c.is_whitespace())
8843                .unwrap_or(room)
8844        };
8845        let head: String = chars[..cut].iter().collect();
8846        let head = head.trim_end_matches(|c: char| c.is_whitespace() || ",;:-".contains(c));
8847        (head.to_owned(), "...")
8848    };
8849    if kept.is_empty() || !kept.is_ascii() || !kept.chars().any(|c| c.is_ascii_alphabetic()) {
8850        return None;
8851    }
8852    Some(format!("{kept}{mark}"))
8853}
8854
8855/// The char length of the first sentence of `chars` when it ends within `max`
8856/// (the closing `.`/`!`/`?` dropped), skipping very short stubs and common
8857/// abbreviations so `e.g. foo` does not end a title early.
8858fn sentence_end(chars: &[char], max: usize) -> Option<usize> {
8859    const MIN: usize = 20;
8860    for i in MIN..max.min(chars.len()) {
8861        if !matches!(chars[i], '.' | '!' | '?') {
8862            continue;
8863        }
8864        let Some(&next) = chars.get(i + 1) else {
8865            continue;
8866        };
8867        if !next.is_whitespace() {
8868            continue;
8869        }
8870        let after = chars[i + 1..].iter().find(|c| !c.is_whitespace());
8871        if after.is_some_and(|c| c.is_ascii_lowercase()) {
8872            continue;
8873        }
8874        let word: String = chars[..i]
8875            .iter()
8876            .rev()
8877            .take_while(|c| !c.is_whitespace())
8878            .collect::<Vec<_>>()
8879            .into_iter()
8880            .rev()
8881            .collect();
8882        let word = word.to_ascii_lowercase();
8883        if matches!(word.as_str(), "e.g" | "i.e" | "etc" | "vs" | "cf") {
8884            continue;
8885        }
8886        let end = chars[..i]
8887            .iter()
8888            .rposition(|c| !c.is_whitespace())
8889            .map_or(i, |p| p + 1);
8890        return Some(end);
8891    }
8892    None
8893}
8894
8895/// What `merge = "pr"` (and the merge commit of the other modes) says about a
8896/// change: a title and a body describing what was *implemented*, not the task
8897/// that asked for it. A task reads as a request; a reader of the merged
8898/// history wants the change.
8899struct PrMessage {
8900    title: String,
8901    body: String,
8902}
8903
8904impl PrMessage {
8905    /// Title, blank line, body. The first line is the squash/merge commit
8906    /// subject (`manual_merge_command` takes it via `lines().next()`), so it
8907    /// has to stay one sensible line.
8908    fn commit_message(&self) -> String {
8909        format!("{}\n\n{}", self.title, self.body)
8910    }
8911}
8912
8913/// The text after a leading `TITLE:` (any case) on `line`.
8914fn title_marker(line: &str) -> Option<&str> {
8915    let line = line.trim();
8916    let head = line.get(..6)?;
8917    head.eq_ignore_ascii_case("title:")
8918        .then(|| line[6..].trim())
8919}
8920
8921/// The implementer's own one-line title: the `TITLE:` line the implement
8922/// prompt asks for at the top of its SUMMARY. Candidate commits are all
8923/// `magi: candidate X (uncommitted work)`, so a commit subject is never a
8924/// source, and a title that says as much is refused here too.
8925fn summary_title(summary: &str) -> Option<String> {
8926    let first = summary.lines().find(|l| !l.trim().is_empty())?;
8927    let raw = title_marker(first)?;
8928    if raw.is_empty() {
8929        return None;
8930    }
8931    let title = queue::title_from(raw, PR_TITLE_MAX);
8932    let lower = title.to_ascii_lowercase();
8933    if lower.starts_with("magi:") || lower.contains("(uncommitted work)") {
8934        return None;
8935    }
8936    Some(title)
8937}
8938
8939/// How `open_review`'s instruction begins; see [`landing_title`].
8940const REVIEW_PROMPT_OPENING: &str = "Review the work already on branch";
8941
8942/// Marker `open_review` gives a candidate that nothing in the roster wrote.
8943const EXISTING_BRANCH: &str = "(existing branch)";
8944
8945/// Does this run review work that already existed, rather than implement a
8946/// task? Runs recorded before `reviewed_commits` existed carry only the
8947/// candidate marker.
8948fn is_review_run(state: &RunState) -> bool {
8949    state.reviewed_commits.is_some() || state.candidates.iter().any(|c| c.agent == EXISTING_BRANCH)
8950}
8951
8952/// The title of a review-only run: the subject of the oldest commit under
8953/// review. Later commits are usually fixups, and `instruction` is the review
8954/// prompt, which says nothing about the change. GitHub text is English, so a
8955/// non-ASCII or blank subject yields `None` and the caller's neutral title.
8956fn review_title(state: &RunState) -> Option<String> {
8957    english_subject(state.reviewed_commits.as_ref()?.first()?)
8958}
8959
8960/// `raw` as a pull request title, or `None` when it is blank, not English
8961/// (GitHub text is), or one of magi's own candidate commit subjects.
8962fn english_subject(raw: &str) -> Option<String> {
8963    let raw = raw.trim();
8964    if raw.is_empty() || !raw.is_ascii() || !raw.chars().any(|c| c.is_ascii_alphabetic()) {
8965        return None;
8966    }
8967    let title = queue::title_from(raw, PR_TITLE_MAX);
8968    let lower = title.to_ascii_lowercase();
8969    if lower.starts_with("magi:") || lower.contains("(uncommitted work)") {
8970        return None;
8971    }
8972    Some(title)
8973}
8974
8975/// What a review-only run's branch says about itself, read at the moment the
8976/// pull request is opened.
8977#[derive(Debug, Clone, PartialEq, Eq)]
8978struct BranchFacts {
8979    /// `(subject, body)` of each commit, oldest first.
8980    commits: Vec<(String, String)>,
8981    /// Trimmed `git diff --stat`.
8982    stat: String,
8983}
8984
8985/// Longest diff stat shown: this many file lines plus the summary line.
8986const STAT_FILE_LINES: usize = 25;
8987/// Cap on the commit list, well inside GitHub's 65536-character body limit.
8988const COMMITS_MAX_CHARS: usize = 20_000;
8989
8990/// Read the commits and diff stat of `base..branch`. `None` when git cannot
8991/// say or finds nothing, so the caller falls back to what the run recorded.
8992async fn branch_facts(repo: &Path, base: &str, branch: &str) -> Option<BranchFacts> {
8993    let commits = git::commit_log(repo, base, branch).await.ok()?;
8994    if commits.is_empty() {
8995        return None;
8996    }
8997    let stat = git::diff_stat(repo, base, branch).await.unwrap_or_default();
8998    let lines: Vec<&str> = stat.lines().collect();
8999    let stat = if lines.len() > STAT_FILE_LINES + 1 {
9000        let omitted = lines.len() - 1 - STAT_FILE_LINES;
9001        let more = format!(" ... {omitted} more file(s)");
9002        let mut kept: Vec<&str> = lines[..STAT_FILE_LINES].to_vec();
9003        kept.push(&more);
9004        kept.push(lines[lines.len() - 1]);
9005        kept.join("\n")
9006    } else {
9007        lines.join("\n")
9008    };
9009    Some(BranchFacts { commits, stat })
9010}
9011
9012/// Defang what would break the surrounding markdown: a closing `</details>`
9013/// and a code fence.
9014fn markdown_safe(text: &str) -> String {
9015    text.replace("</details>", "&lt;/details&gt;")
9016        .replace("\x60\x60\x60", "~~~")
9017}
9018
9019fn neutral_title(state: &RunState, winner: char) -> String {
9020    format!(
9021        "chore: land candidate {} of run {}",
9022        winner.to_ascii_uppercase(),
9023        state.id
9024    )
9025}
9026
9027/// The pull request title to hand to `land::merge_subject`. A review-only run
9028/// opened by an earlier build titled its pull request with the review prompt;
9029/// that title is dropped (empty, so the fallback applies) rather than landed.
9030/// Any other title, including an operator's rename, passes through untouched,
9031/// and so does every title of a run that implements a task.
9032pub fn landing_title<'a>(state: &RunState, pr_title: &'a str) -> &'a str {
9033    if is_review_run(state) && pr_title.trim_start().starts_with(REVIEW_PROMPT_OPENING) {
9034        ""
9035    } else {
9036        pr_title
9037    }
9038}
9039
9040/// What the squash subject falls back to when the pull request title is empty
9041/// or candidate-shaped: for a review-only run the derived title, never the
9042/// review prompt held in `instruction`.
9043pub fn landing_subject_source(state: &RunState) -> String {
9044    if is_review_run(state) {
9045        let winner = state.candidates.first().map_or('A', |c| c.label);
9046        return review_title(state).unwrap_or_else(|| neutral_title(state, winner));
9047    }
9048    state.instruction.clone()
9049}
9050
9051/// `summary` without its `TITLE:` line, which the pull request title already
9052/// carries.
9053fn summary_without_title(summary: &str) -> String {
9054    let mut lines = summary.trim().lines().peekable();
9055    if lines.peek().is_some_and(|l| title_marker(l).is_some()) {
9056        lines.next();
9057    }
9058    lines.collect::<Vec<_>>().join("\n").trim().to_owned()
9059}
9060
9061/// The pull request title and body for the winning candidate.
9062///
9063/// Title: the implementer's `TITLE:` line ([`summary_title`]), falling back to
9064/// the task's own opening line via [`queue::title_from`] when there is none.
9065/// `state.instruction` can open with blank lines (`task_text` only rejects a
9066/// body that is blank *entirely*), which `title_from` skips.
9067///
9068/// Body: the implementer's summary and the fixer's notes, then — when the
9069/// winning review round was not clean — the findings still open and whatever
9070/// the fixer declined, so `merge = "pr"` hands the reader the same material
9071/// `magi show` does. The task follows inside a collapsed block, and the
9072/// footer repeats the run and candidate as plain tags for a reader holding
9073/// only the merged commit or the PR body.
9074#[cfg(test)]
9075fn pr_message(state: &RunState, winner: char) -> PrMessage {
9076    pr_message_with(state, winner, None)
9077}
9078
9079/// [`pr_message`] with what the branch of a review-only run says about itself.
9080/// `facts` is ignored for a run that implements a task.
9081#[cfg(test)]
9082fn pr_message_with(state: &RunState, winner: char, facts: Option<&BranchFacts>) -> PrMessage {
9083    let raw = pr_message_raw(state, winner, facts);
9084    let id = crate::scrub::Identity::current();
9085    PrMessage {
9086        title: crate::scrub::scrub(&raw.title, &id),
9087        body: crate::scrub::scrub(&raw.body, &id),
9088    }
9089}
9090
9091fn pr_message_raw(state: &RunState, winner: char, facts: Option<&BranchFacts>) -> PrMessage {
9092    let summary = state
9093        .candidates
9094        .iter()
9095        .find(|c| c.label == winner)
9096        .map(|c| c.summary.as_str())
9097        .unwrap_or_default();
9098    // The fallback is the operator's own words and may not be English; GitHub
9099    // text always is, so a non-English task gets a neutral title instead.
9100    let review = is_review_run(state);
9101    let title = if review {
9102        facts
9103            .and_then(|f| english_subject(&f.commits.first()?.0))
9104            .or_else(|| review_title(state))
9105            .or_else(|| {
9106                state
9107                    .candidates
9108                    .iter()
9109                    .find(|c| c.label == winner)
9110                    .filter(|c| !c.branch.starts_with("magi/"))
9111                    .and_then(|c| english_subject(&c.branch))
9112            })
9113            .unwrap_or_else(|| neutral_title(state, winner))
9114    } else {
9115        summary_title(summary).unwrap_or_else(|| {
9116            english_title(&state.instruction, PR_TITLE_MAX)
9117                .unwrap_or_else(|| neutral_title(state, winner))
9118        })
9119    };
9120
9121    let mut body = String::new();
9122    let what = summary_without_title(summary);
9123    if !what.is_empty() {
9124        body.push_str("## Summary\n\n");
9125        body.push_str(&what);
9126        body.push_str("\n\n");
9127    }
9128
9129    // The last round is usually a clean verification pass with no fix of its
9130    // own, so every round's notes are read, not just the final one's.
9131    let notes: Vec<(usize, &str)> = state
9132        .reviews
9133        .iter()
9134        .filter_map(|r| {
9135            let n = r.fix.as_ref()?.notes.trim();
9136            (!n.is_empty()).then_some((r.round, n))
9137        })
9138        .collect();
9139    if !notes.is_empty() {
9140        body.push_str("## Review fixes\n\n");
9141        if let [(_, only)] = notes.as_slice() {
9142            body.push_str(only);
9143            body.push_str("\n\n");
9144        } else {
9145            for (round, n) in &notes {
9146                body.push_str(&format!("### Round {round}\n\n{n}\n\n"));
9147            }
9148        }
9149    }
9150    let fix = state.reviews.iter().rev().find_map(|r| r.fix.as_ref());
9151
9152    let open = state.open_findings();
9153    if !open.is_empty() {
9154        body.push_str("## Open review findings\n\n");
9155        for f in &open {
9156            body.push_str(&format!("- `{}` [{:?}] {}\n", f.id, f.severity, f.title));
9157        }
9158        body.push('\n');
9159    }
9160
9161    if let Some(fix) = fix
9162        && !fix.rejected.is_empty()
9163    {
9164        body.push_str("## Declined by the fixer\n\n");
9165        for r in &fix.rejected {
9166            body.push_str(&format!("- `{}`: {}\n", r.id, r.why));
9167        }
9168        body.push('\n');
9169    }
9170
9171    if review {
9172        // The review prompt is not the task; list what the branch carries.
9173        body.push_str("## Commits under review\n\n");
9174        if let Some(facts) = facts {
9175            let mut left = COMMITS_MAX_CHARS;
9176            for (i, (subject, text)) in facts.commits.iter().enumerate() {
9177                let mut entry = format!("- {}\n", markdown_safe(subject));
9178                for l in markdown_safe(text).lines() {
9179                    entry.push_str(format!("  {l}\n").trim_end_matches(' '));
9180                }
9181                if left == 0 {
9182                    body.push_str(&format!(
9183                        "- ... {} more commit(s)\n",
9184                        facts.commits.len() - i
9185                    ));
9186                    break;
9187                }
9188                if entry.len() > left {
9189                    // Even the first commit is cut: one huge body must not
9190                    // push the whole description past GitHub's limit.
9191                    let mut end = left;
9192                    while !entry.is_char_boundary(end) {
9193                        end -= 1;
9194                    }
9195                    entry.truncate(end);
9196                    entry.push_str("\n  ... (truncated)\n");
9197                    left = 0;
9198                } else {
9199                    left -= entry.len();
9200                }
9201                body.push_str(&entry);
9202            }
9203            if !facts.stat.trim().is_empty() {
9204                body.push_str(&format!(
9205                    "\n## Diff stat\n\n```\n{}\n```\n",
9206                    markdown_safe(facts.stat.trim())
9207                ));
9208            }
9209        } else {
9210            match &state.reviewed_commits {
9211                Some(subjects) => {
9212                    for s in subjects {
9213                        body.push_str(&format!("- {}\n", s.trim()));
9214                    }
9215                }
9216                None => {
9217                    // An older run kept only the prompt, with the commit list
9218                    // after its first paragraph.
9219                    let rest = state.instruction.split_once("\n\n").map_or("", |(_, r)| r);
9220                    body.push_str(rest.trim());
9221                    body.push('\n');
9222                }
9223            }
9224        }
9225    } else {
9226        let task = state.instruction.trim();
9227        let task = if task.is_empty() {
9228            "(empty task)"
9229        } else {
9230            task
9231        };
9232        body.push_str(&format!(
9233            "<details>\n<summary>Original task</summary>\n\n{}\n\n</details>\n",
9234            task.replace("</details>", "&lt;/details&gt;")
9235        ));
9236    }
9237
9238    body.push_str(&format!(
9239        "\n---\nmagi:run/{} magi:candidate-{}\n",
9240        state.id,
9241        winner.to_ascii_lowercase()
9242    ));
9243
9244    PrMessage { title, body }
9245}
9246
9247/// The task with what the repository says about the existing work it names
9248/// appended, so an implementer knows what it started from and what it must
9249/// not redo. Unchanged when the task names nothing.
9250fn seeded_instruction(state: &RunState) -> String {
9251    match refs::describe(&state.seeds) {
9252        Some(facts) => format!(
9253            "{}\n\n# Existing work the task refers to\n\n{facts}\n\n\
9254             Candidates start from the unmerged branch named above, when there \
9255             is one, and carry any unmerged commit named by sha as a \
9256             cherry-pick. Check that this is what the task meant before \
9257             building on it.",
9258            state.instruction
9259        ),
9260        None => state.instruction.clone(),
9261    }
9262}
9263
9264/// Does the winner have no commits ahead of the base it would land on?
9265/// Any failure to find out reads as "not empty": the merge then behaves as it
9266/// always did rather than refusing on a guess.
9267async fn merge_is_empty(repo: &Path, state: &RunState, branch: &str, mode: MergeMode) -> bool {
9268    let base = &state.base_branch;
9269    let mut against = base.clone();
9270    if mode == MergeMode::Pr {
9271        // A pull request lands on the remote's base, never the local branch
9272        // of the same name: if that cannot be read, "not empty" is the safe
9273        // answer.
9274        let remote = &state.config.merge.remote;
9275        let tracking = format!("{remote}/{base}");
9276        let fetched = git::fetch(repo, remote, base).await;
9277        if fetched.is_ok_and(|o| o.ok()) && git::rev_exists(repo, &tracking).await {
9278            against = tracking;
9279        } else {
9280            return false;
9281        }
9282    }
9283    matches!(git::commits_ahead(repo, &against, branch).await, Ok(0))
9284}
9285
9286/// Why nothing was opened for an empty winner, with what the task's own
9287/// references resolved to.
9288fn empty_candidate_detail(state: &RunState, base: &str) -> String {
9289    let mut detail = format!(
9290        "empty candidate: the winning branch has 0 commits ahead of {base}, so there is \
9291         nothing to open a pull request for"
9292    );
9293    match refs::describe(&state.seeds) {
9294        Some(facts) => detail.push_str(&format!("\nReferences in the task:\n{facts}")),
9295        None => detail.push_str(
9296            "\nThe task names no existing branch or commit; if it means to land work \
9297             that lives elsewhere, name the branch (magi/<run>/<label>) or the sha.",
9298        ),
9299    }
9300    detail
9301}
9302
9303/// What the `Pr` merge does once it knows whether the branch already has an
9304/// open pull request.
9305#[derive(Debug, PartialEq, Eq)]
9306enum PrPlan {
9307    Create,
9308    Adopt { url: String, title: String },
9309    Stop(String),
9310}
9311
9312/// Pure decision behind the `Pr` merge: none -> create, one -> adopt, many or
9313/// a failed lookup -> stop with the real reason. Never guesses.
9314fn pr_merge_plan(found: Result<land::OpenPr>) -> PrPlan {
9315    match found {
9316        Ok(land::OpenPr::None) => PrPlan::Create,
9317        Ok(land::OpenPr::One { url, title }) => PrPlan::Adopt { url, title },
9318        Ok(land::OpenPr::Many(urls)) => PrPlan::Stop(format!(
9319            "several open pull requests exist for this branch, not picking one: {}",
9320            urls.join(" ")
9321        )),
9322        Err(e) => PrPlan::Stop(format!("could not look up open pull requests: {e:#}")),
9323    }
9324}
9325
9326/// `gh pr create`, returning the PR url.
9327pub(crate) async fn gh_pr_create(
9328    cwd: &Path,
9329    base: &str,
9330    head: &str,
9331    title: &str,
9332    body: &str,
9333) -> Result<String> {
9334    let out = tokio::process::Command::new("gh")
9335        .args([
9336            "pr", "create", "--base", base, "--head", head, "--title", title, "--body", body,
9337        ])
9338        .current_dir(cwd)
9339        .quiet()
9340        .stdin(std::process::Stdio::null())
9341        .output()
9342        .await
9343        .context("spawn gh")?;
9344    if out.status.success() {
9345        Ok(String::from_utf8_lossy(&out.stdout).trim().to_owned())
9346    } else {
9347        bail!("{}", String::from_utf8_lossy(&out.stderr).trim().to_owned())
9348    }
9349}
9350
9351/// Tear a run's worktrees and branches down.
9352///
9353/// `home` is where the updated `run.json` is saved (via
9354/// [`RunState::save_under`]), never the process-global [`crate::run::home`]:
9355/// a housekeeping pass already has its own honest `home` handed to it, and
9356/// falling through to the global here would write back through whichever
9357/// directory some other process or test pinned into that `OnceLock` first,
9358/// not the one the caller actually resolved its `runs` and `state` from.
9359pub async fn fold_run(state: &mut RunState, drop_winner: bool, home: &Path) -> Result<Vec<String>> {
9360    let repo = state.repo.clone();
9361    let root = state.worktree_root();
9362    let winner = state.tally.as_ref().map(|t| t.winner);
9363    let mut removed = Vec::new();
9364
9365    for i in 0..state.candidates.len() {
9366        let c = state.candidates[i].clone();
9367        let is_winner = Some(c.label) == winner;
9368        if is_winner && !drop_winner {
9369            continue;
9370        }
9371        if c.worktree.exists() {
9372            git::worktree_remove(&repo, &c.worktree).await.ok();
9373            removed.push(c.worktree.to_string_lossy().into_owned());
9374        }
9375        // A branch handed to a later run (and its pull request) is not this
9376        // run's to delete.
9377        let handed_over = state.released_branches.contains(&c.branch);
9378        if !handed_over && git::branch_exists(&repo, &c.branch).await.unwrap_or(false) {
9379            git::branch_delete(&repo, &c.branch).await.ok();
9380            removed.push(c.branch.clone());
9381        }
9382        state.candidates[i].folded = true;
9383    }
9384
9385    for name in std::fs::read_dir(&root).into_iter().flatten().flatten() {
9386        let path = name.path();
9387        let keep = !drop_winner
9388            && winner.is_some_and(|w| {
9389                path.file_name()
9390                    .is_some_and(|n| n == format!("cand-{w}").as_str())
9391            });
9392        if keep {
9393            continue;
9394        }
9395        git::worktree_remove(&repo, &path).await.ok();
9396        removed.push(path.to_string_lossy().into_owned());
9397    }
9398
9399    // `root` (`wt/<...>/<short>/`) held nothing but this run's candidate and
9400    // judge worktrees, so once the loop above has cleared all of them out,
9401    // the parent is a bare directory nobody else was ever going to remove -
9402    // git only ever managed what was inside it. Left alone, one of these
9403    // accumulates per fully-folded run; the operator's own machine had 74.
9404    // `remove_if_empty` re-checks rather than assuming: a run whose winner
9405    // was kept (`!drop_winner`) leaves its directory behind on purpose, and
9406    // so does anything a run never claimed that happens to share the bay.
9407    remove_if_empty(&root);
9408
9409    if state.enabled_worktree_config && drop_winner {
9410        // A release, not a raw disable: some sibling run in this repository
9411        // may still hold its own reference (see `git::acquire_worktree_config`),
9412        // and only the last release actually turns the setting back off.
9413        git::release_worktree_config(&repo).await.ok();
9414        state.enabled_worktree_config = false;
9415    }
9416    state.save_under(home)?;
9417    Ok(removed)
9418}
9419
9420/// Remove `dir` if it exists and has nothing in it.
9421///
9422/// Best-effort and silent by design: a directory that is not empty (a run
9423/// whose winner is still parked there, a stray file some other process left)
9424/// is exactly the case this must refuse, and a directory that is already gone
9425/// is not a failure worth reporting either. `std::fs::remove_dir` itself
9426/// already refuses a non-empty directory, so the emptiness check below is
9427/// belt, not suspenders - it is what keeps this from ever attempting the
9428/// removal in the case that matters, rather than trusting `remove_dir`'s
9429/// error path to have no side effects if it ever changed.
9430fn remove_if_empty(dir: &Path) {
9431    if dir.is_dir() && std::fs::read_dir(dir).is_ok_and(|mut entries| entries.next().is_none()) {
9432        std::fs::remove_dir(dir).ok();
9433    }
9434}
9435
9436/// Severity of the worst open finding in the last review round, for reporting.
9437pub fn worst_open(state: &RunState) -> Option<Severity> {
9438    state
9439        .reviews
9440        .last()?
9441        .reviews
9442        .iter()
9443        .flat_map(|r| r.findings.iter())
9444        .map(|f| f.severity)
9445        .max()
9446}
9447
9448/// `[worktree] setup` hides its products per worktree, which needs
9449/// `extensions.worktreeConfig`. Taken once per run and released with the run's
9450/// worktrees, like the hook's; every place that runs setup calls this first.
9451async fn ensure_setup_config(state: &mut RunState, repo: &Path) -> Result<()> {
9452    if !state.config.worktree.setup.is_empty() && !state.enabled_worktree_config {
9453        git::acquire_worktree_config(repo).await?;
9454        state.enabled_worktree_config = true;
9455    }
9456    Ok(())
9457}
9458
9459#[cfg(test)]
9460mod tests {
9461    #[test]
9462    fn should_retitle_only_replaces_magi_shaped_or_leaked_titles() {
9463        let leaked = vec!["chore(deps): update a crate".to_owned()];
9464        let own = "fix(daemon): apply a chosen action";
9465        assert!(should_retitle("", own, &leaked));
9466        assert!(should_retitle(
9467            &format!("{REVIEW_PROMPT_OPENING} `x`"),
9468            own,
9469            &leaked
9470        ));
9471        assert!(should_retitle(
9472            "chore: land candidate A of run 1",
9473            own,
9474            &leaked
9475        ));
9476        assert!(should_retitle(
9477            "magi: candidate A (uncommitted work)",
9478            own,
9479            &leaked
9480        ));
9481        assert!(should_retitle("chore(deps): update a crate", own, &leaked));
9482        assert!(!should_retitle("feat: renamed by hand", own, &leaked));
9483        assert!(!should_retitle("", "chore(deps): update a crate", &leaked));
9484    }
9485
9486    #[test]
9487    fn pr_merge_plan_creates_adopts_or_stops() {
9488        assert_eq!(pr_merge_plan(Ok(land::OpenPr::None)), PrPlan::Create);
9489        assert_eq!(
9490            pr_merge_plan(Ok(land::OpenPr::One {
9491                url: "u".into(),
9492                title: "t".into()
9493            })),
9494            PrPlan::Adopt {
9495                url: "u".into(),
9496                title: "t".into()
9497            }
9498        );
9499        let PrPlan::Stop(many) =
9500            pr_merge_plan(Ok(land::OpenPr::Many(vec!["a".into(), "b".into()])))
9501        else {
9502            panic!("many must stop");
9503        };
9504        assert!(many.contains('a') && many.contains('b'));
9505        let PrPlan::Stop(err) = pr_merge_plan(Err(anyhow::anyhow!("bad token"))) else {
9506            panic!("a failed lookup must stop");
9507        };
9508        assert!(err.contains("bad token"));
9509    }
9510
9511    use super::*;
9512    use crate::run::GateStatus;
9513    use std::collections::BTreeMap;
9514    use std::time::Duration;
9515
9516    fn conductor() -> AgentSpec {
9517        AgentSpec {
9518            id: "conductor".to_owned(),
9519            kind: crate::config::AgentKind::Command,
9520            model: None,
9521            command: vec!["true".to_owned()],
9522            extra_args: Vec::new(),
9523            env: BTreeMap::new(),
9524            prompt_delivery: None,
9525        }
9526    }
9527
9528    fn spec(id: &str) -> AgentSpec {
9529        AgentSpec {
9530            id: id.to_owned(),
9531            kind: crate::config::AgentKind::Command,
9532            model: None,
9533            command: vec!["true".to_owned()],
9534            extra_args: Vec::new(),
9535            env: BTreeMap::new(),
9536            prompt_delivery: None,
9537        }
9538    }
9539
9540    fn ids(xs: &[&str]) -> BTreeSet<String> {
9541        xs.iter().map(|s| (*s).to_owned()).collect()
9542    }
9543
9544    #[test]
9545    fn pick_successor_skips_an_agent_another_seat_holds() {
9546        // Seats a and b of roster [a, b, c]; a fails, b holds the other seat.
9547        let roster = [spec("a"), spec("b"), spec("c")];
9548        let next = pick_successor(&roster, 0, &ids(&["a"]), None, &ids(&["b"]));
9549        assert_eq!(next.map(|s| s.id.as_str()), Some("c"));
9550    }
9551
9552    #[test]
9553    fn pick_successor_respects_the_occupant_after_an_earlier_handover() {
9554        // The other seat started on c but was handed to d; c is free again.
9555        let roster = [spec("a"), spec("b"), spec("c"), spec("d")];
9556        let next = pick_successor(&roster, 0, &ids(&["a"]), None, &ids(&["b"]));
9557        assert_eq!(next.map(|s| s.id.as_str()), Some("c"));
9558        let next = pick_successor(&roster, 0, &ids(&["a"]), None, &ids(&["b", "c"]));
9559        assert_eq!(next.map(|s| s.id.as_str()), Some("d"));
9560    }
9561
9562    #[test]
9563    fn pick_successor_falls_back_to_a_duplicate_when_no_distinct_agent_remains() {
9564        let roster = [spec("a"), spec("b")];
9565        let next = pick_successor(&roster, 0, &ids(&["a"]), None, &ids(&["b"]));
9566        assert_eq!(next.map(|s| s.id.as_str()), Some("b"));
9567        let carried = ids(&["b"]);
9568        let next = pick_successor(&roster, 0, &ids(&["a"]), Some(&carried), &ids(&["b"]));
9569        assert_eq!(next.map(|s| s.id.as_str()), Some("b"));
9570    }
9571
9572    #[test]
9573    fn pick_successor_returns_none_without_any_untried_successor() {
9574        let roster = [spec("a"), spec("b")];
9575        assert!(pick_successor(&roster, 1, &ids(&["b"]), None, &ids(&["a"])).is_none());
9576        assert!(pick_successor(&roster, 0, &ids(&["a", "b"]), None, &ids(&[])).is_none());
9577        let carried = ids(&["a"]);
9578        assert!(pick_successor(&roster, 0, &ids(&["a", "b"]), Some(&carried), &ids(&[])).is_none());
9579    }
9580
9581    #[test]
9582    fn pick_successor_rescue_avoids_another_seats_occupant() {
9583        // b is a carried failure and free; a is held by the other seat.
9584        let roster = [spec("a"), spec("b"), spec("c")];
9585        let carried = ids(&["b", "c"]);
9586        let next = pick_successor(&roster, 2, &ids(&["c"]), Some(&carried), &ids(&["a"]));
9587        assert_eq!(next.map(|s| s.id.as_str()), Some("b"));
9588    }
9589
9590    #[test]
9591    fn next_for_seat_prefers_an_agent_that_has_not_failed() {
9592        let roster = [spec("a"), spec("b"), spec("c")];
9593        let next = next_for_seat(&roster, 0, &ids(&["a"]), &ids(&["b"]));
9594        assert_eq!(next.map(|s| s.id.as_str()), Some("c"));
9595    }
9596
9597    #[test]
9598    fn next_for_seat_rescues_a_failed_agent_only_when_nothing_else_is_left() {
9599        let roster = [spec("a"), spec("b"), spec("c")];
9600        let failed = ids(&["a", "b", "c"]);
9601        // Rescue looks at the whole roster, once per id, then runs out.
9602        let mut tried = ids(&["b"]);
9603        let first = next_for_seat(&roster, 1, &tried, &failed).expect("rescue");
9604        assert_eq!(first.id, "a");
9605        tried.insert(first.id.clone());
9606        let second = next_for_seat(&roster, 1, &tried, &failed).expect("rescue");
9607        assert_eq!(second.id, "c");
9608        tried.insert(second.id.clone());
9609        assert!(next_for_seat(&roster, 1, &tried, &failed).is_none());
9610    }
9611
9612    #[test]
9613    fn next_for_seat_ignores_failed_ids_no_longer_on_the_roster() {
9614        let roster = [spec("a"), spec("b")];
9615        let next = next_for_seat(&roster, 0, &ids(&["a"]), &ids(&["gone"]));
9616        assert_eq!(next.map(|s| s.id.as_str()), Some("b"));
9617    }
9618
9619    #[test]
9620    fn pick_start_spec_starts_on_the_last_answerer_when_still_eligible() {
9621        let roster = [spec("a"), spec("b"), spec("c")];
9622        let h = SeatHistory {
9623            failed: ids(&["a"]),
9624            last_ok: Some("b".to_owned()),
9625            last_fail: None,
9626        };
9627        assert_eq!(pick_start_spec(&roster, spec("a"), Some(&h)).id, "b");
9628        // A last answerer that left the roster, or later failed, is ignored.
9629        let gone = SeatHistory {
9630            last_ok: Some("zzz".to_owned()),
9631            ..h.clone()
9632        };
9633        assert_eq!(pick_start_spec(&roster, spec("a"), Some(&gone)).id, "b");
9634        let failed = SeatHistory {
9635            failed: ids(&["a", "b"]),
9636            last_ok: Some("b".to_owned()),
9637            last_fail: None,
9638        };
9639        assert_eq!(pick_start_spec(&roster, spec("a"), Some(&failed)).id, "c");
9640    }
9641
9642    #[test]
9643    fn handover_seat_mints_a_session_id_distinct_from_the_previous_agents() {
9644        let first = SeatState::new("review-1", "alpha", 7);
9645        let next = handover_seat("review-1", "gamma", 7);
9646        assert_ne!(first.claude_session, next.claude_session);
9647    }
9648
9649    #[test]
9650    fn re_handing_a_seat_to_the_same_agent_mints_a_new_session_id() {
9651        let mut state = state_with_summary("x", "y");
9652        let a = handover_seat("review-1", "beta", state.next_seat_seed());
9653        let b = handover_seat("review-1", "beta", state.next_seat_seed());
9654        assert_ne!(a.claude_session, b.claude_session);
9655    }
9656
9657    #[test]
9658    fn pick_start_spec_falls_back_to_the_spec_when_the_whole_roster_failed() {
9659        let roster = [spec("a"), spec("b")];
9660        let h = SeatHistory {
9661            failed: ids(&["a", "b"]),
9662            ..SeatHistory::default()
9663        };
9664        assert_eq!(pick_start_spec(&roster, spec("b"), Some(&h)).id, "b");
9665        assert_eq!(pick_start_spec(&roster, spec("b"), None).id, "b");
9666        assert_eq!(pick_start_spec(&[], spec("b"), Some(&h)).id, "b");
9667    }
9668
9669    // `next_untried_in_roster` is the property `resume_seat_handovers`'s own
9670    // fallback loop depends on to terminate: it must walk forward from the
9671    // seat's own position, never restart at the front of the roster, and it
9672    // must never hand back an id already tried, however many times that id
9673    // happens to appear.
9674
9675    #[test]
9676    fn failure_signature_ignores_numbers_and_paths() {
9677        assert_eq!(
9678            failure_signature("exited with Some(2) and no usable output"),
9679            failure_signature("exited with Some(137) and no usable output")
9680        );
9681        assert_eq!(
9682            failure_signature("cannot open /tmp/a/b.txt: denied\nsecond line"),
9683            failure_signature("cannot open /var/x.txt: denied")
9684        );
9685        assert_ne!(failure_signature("boom"), failure_signature("bang"));
9686    }
9687
9688    #[test]
9689    fn quota_and_timeout_always_hand_over_other_failures_stop_on_a_repeat() {
9690        let other = FailClass::Other("x".into());
9691        assert!(should_hand_over(None, &FailClass::Quota));
9692        assert!(should_hand_over(Some(&other), &FailClass::Quota));
9693        assert!(should_hand_over(
9694            Some(&FailClass::Timeout),
9695            &FailClass::Timeout
9696        ));
9697        assert!(should_hand_over(None, &other));
9698        assert!(!should_hand_over(Some(&other), &other));
9699        assert!(should_hand_over(
9700            Some(&other),
9701            &FailClass::Other("y".into())
9702        ));
9703        // A quota or timeout in between ends the run of identical failures.
9704        assert!(should_hand_over(Some(&FailClass::Timeout), &other));
9705        assert!(should_hand_over(Some(&FailClass::Quota), &other));
9706    }
9707
9708    #[test]
9709    fn a_handover_seat_never_reuses_the_previous_agents_session_id() {
9710        let a = SeatState::new("judge-1", "alpha", 7);
9711        let b = handover_seat("judge-1", "beta", 7);
9712        assert_ne!(a.claude_session, b.claude_session);
9713        assert_eq!(b.turns, 0);
9714    }
9715
9716    #[test]
9717    fn a_timeout_is_classified_apart_from_other_failures() {
9718        assert_eq!(
9719            FailClass::of(&AgentOutcome::Failed(TIMED_OUT.to_owned())),
9720            Some(FailClass::Timeout)
9721        );
9722        assert!(matches!(
9723            FailClass::of(&AgentOutcome::Failed("boom".to_owned())),
9724            Some(FailClass::Other(_))
9725        ));
9726    }
9727
9728    #[test]
9729    fn next_untried_in_roster_walks_forward_from_the_seats_own_position() {
9730        let roster = vec![spec("alpha"), spec("beta"), spec("gamma")];
9731        let tried = BTreeSet::from(["beta".to_owned()]);
9732        // beta sits at index 1; the next candidate is gamma, never alpha —
9733        // which is very likely a different candidate slot's own agent.
9734        let next = next_untried_in_roster(&roster, 1, &tried);
9735        assert_eq!(next.map(|s| s.id.as_str()), Some("gamma"));
9736    }
9737
9738    #[test]
9739    fn next_untried_in_roster_does_not_wrap_back_past_its_own_start() {
9740        let roster = vec![spec("alpha"), spec("beta")];
9741        let tried = BTreeSet::from(["beta".to_owned()]);
9742        // beta is the roster's last entry: nothing follows it, and alpha —
9743        // earlier in the roster, almost certainly a different candidate
9744        // slot's own agent — must not be reached by wrapping back to it.
9745        assert!(next_untried_in_roster(&roster, 1, &tried).is_none());
9746    }
9747
9748    #[test]
9749    fn next_untried_in_roster_stops_once_the_tail_is_exhausted_even_if_earlier_ids_are_untried() {
9750        let roster = vec![spec("alpha"), spec("beta"), spec("gamma")];
9751        let tried = BTreeSet::from(["beta".to_owned(), "gamma".to_owned()]);
9752        // beta (index 1) and gamma (index 2, the only entry after it) have
9753        // both been tried; alpha (index 0) never has, but it comes before
9754        // beta's own position, so there is nothing further for this seat.
9755        assert!(next_untried_in_roster(&roster, 1, &tried).is_none());
9756    }
9757
9758    #[test]
9759    fn next_untried_in_roster_skips_ids_already_tried_even_when_duplicated() {
9760        let roster = vec![spec("a"), spec("a"), spec("b")];
9761        let tried = BTreeSet::from(["a".to_owned()]);
9762        let next = next_untried_in_roster(&roster, 0, &tried);
9763        assert_eq!(next.map(|s| s.id.as_str()), Some("b"));
9764    }
9765
9766    #[test]
9767    fn next_untried_in_roster_returns_none_once_every_id_is_tried() {
9768        let roster = vec![spec("a"), spec("b")];
9769        let tried = BTreeSet::from(["a".to_owned(), "b".to_owned()]);
9770        assert!(next_untried_in_roster(&roster, 0, &tried).is_none());
9771    }
9772
9773    #[test]
9774    fn remove_if_empty_only_ever_takes_a_bare_directory() {
9775        let dir = tempfile::tempdir().unwrap();
9776        let bay = dir.path().join("ffff");
9777
9778        // Not there yet: nothing to do, nothing to panic on.
9779        remove_if_empty(&bay);
9780        assert!(!bay.exists());
9781
9782        // Something still inside - the winner's worktree, or a stray file -
9783        // keeps the directory standing.
9784        std::fs::create_dir_all(bay.join("cand-A")).unwrap();
9785        remove_if_empty(&bay);
9786        assert!(bay.exists(), "non-empty directory must survive");
9787
9788        // Once the last entry is gone, so is the directory itself.
9789        std::fs::remove_dir(bay.join("cand-A")).unwrap();
9790        remove_if_empty(&bay);
9791        assert!(!bay.exists(), "an empty bay is a leftover, not a record");
9792    }
9793
9794    // `round_is_clean` is the exact decision this task fixed: a round with a
9795    // seat that never answered must not read the same as a round every seat
9796    // actually reviewed. These are deterministic and process-free by design —
9797    // the equivalent end-to-end check (a real reviewer timing out under a
9798    // live graph run) is a genuine race against wall-clock contention, and a
9799    // spawn slow enough to blow even a generous budget under a loaded test
9800    // run must not turn this specific regression check flaky.
9801
9802    #[test]
9803    fn a_full_panel_that_found_nothing_is_clean() {
9804        assert!(round_is_clean(
9805            0,
9806            true,
9807            2,
9808            2,
9809            0,
9810            IncompleteReviewPolicy::Block
9811        ));
9812    }
9813
9814    #[test]
9815    fn a_missing_seat_is_never_clean_under_the_default_policy() {
9816        assert!(!round_is_clean(
9817            0,
9818            true,
9819            1,
9820            2,
9821            0,
9822            IncompleteReviewPolicy::Block
9823        ));
9824    }
9825
9826    #[test]
9827    fn warn_policy_still_refuses_a_missing_seat_with_open_findings() {
9828        assert!(!round_is_clean(
9829            1,
9830            true,
9831            1,
9832            2,
9833            0,
9834            IncompleteReviewPolicy::Warn
9835        ));
9836    }
9837
9838    #[test]
9839    fn warn_policy_gates_a_missing_seat_once_what_answered_is_clean() {
9840        assert!(round_is_clean(
9841            0,
9842            true,
9843            1,
9844            2,
9845            0,
9846            IncompleteReviewPolicy::Warn
9847        ));
9848    }
9849
9850    #[test]
9851    fn a_full_panel_with_an_open_finding_is_not_clean() {
9852        assert!(!round_is_clean(
9853            1,
9854            true,
9855            2,
9856            2,
9857            0,
9858            IncompleteReviewPolicy::Block
9859        ));
9860    }
9861
9862    #[test]
9863    fn a_full_panel_with_a_red_e2e_is_not_clean() {
9864        assert!(!round_is_clean(
9865            0,
9866            false,
9867            2,
9868            2,
9869            0,
9870            IncompleteReviewPolicy::Block
9871        ));
9872    }
9873
9874    // The stall this task closes: under the default `block` policy, a seat
9875    // missing only because it was rate limited must not force a wait for a
9876    // session limit that will not lift by the next round. `round_is_clean`
9877    // is where that quorum carve-out lives; the review loop around it never
9878    // changes what a reviewer's vote or a finding's severity means.
9879
9880    #[test]
9881    fn a_seat_missing_only_to_its_own_quota_is_clean_under_the_default_policy() {
9882        // 1 of 2 answered, and the one missing was quota'd — the exact
9883        // "review-2 rate limited (quota)" shape from the field report.
9884        assert!(round_is_clean(
9885            0,
9886            true,
9887            1,
9888            2,
9889            1,
9890            IncompleteReviewPolicy::Block
9891        ));
9892    }
9893
9894    #[test]
9895    fn a_seat_missing_for_a_reason_other_than_quota_still_waits() {
9896        // 1 of 2 answered, but the miss was a crash/timeout/parse failure,
9897        // not a quota loss (`quota_missing` stays 0) — worth another try.
9898        assert!(!round_is_clean(
9899            0,
9900            true,
9901            1,
9902            2,
9903            0,
9904            IncompleteReviewPolicy::Block
9905        ));
9906    }
9907
9908    #[test]
9909    fn a_quota_loss_does_not_excuse_an_open_finding_or_a_red_e2e() {
9910        assert!(!round_is_clean(
9911            1,
9912            true,
9913            1,
9914            2,
9915            1,
9916            IncompleteReviewPolicy::Block
9917        ));
9918        assert!(!round_is_clean(
9919            0,
9920            false,
9921            1,
9922            2,
9923            1,
9924            IncompleteReviewPolicy::Block
9925        ));
9926    }
9927
9928    #[test]
9929    fn a_panel_lost_entirely_to_quota_still_waits_rather_than_deciding_on_nobody() {
9930        // Every seat quota'd, nobody answered: there is no panel to decide
9931        // on, so this must fall through to the existing block-and-retry
9932        // fallback rather than call an unreviewed patch clean.
9933        assert!(!round_is_clean(
9934            0,
9935            true,
9936            0,
9937            2,
9938            2,
9939            IncompleteReviewPolicy::Block
9940        ));
9941    }
9942
9943    fn outcome(code: Option<i32>, resource_blocked: bool) -> CommandOutcome {
9944        CommandOutcome {
9945            command: "test".to_owned(),
9946            code,
9947            output_tail: String::new(),
9948            duration_ms: 0,
9949            resource_blocked,
9950        }
9951    }
9952
9953    #[test]
9954    fn verify_is_inconclusive_only_when_a_resource_blocked_outcome_is_present() {
9955        assert!(!verify_inconclusive(&[outcome(Some(0), false)]));
9956        assert!(
9957            !verify_inconclusive(&[outcome(Some(1), false)]),
9958            "an ordinary failure is still evidence about the patch"
9959        );
9960        assert!(verify_inconclusive(&[outcome(None, true)]));
9961        assert!(
9962            verify_inconclusive(&[outcome(Some(0), false), outcome(None, true)]),
9963            "one inconclusive outcome taints the whole batch"
9964        );
9965        assert!(!verify_inconclusive(&[]));
9966    }
9967
9968    #[tokio::test]
9969    async fn timed_out_pid_waiting_returns_as_soon_as_every_pid_is_confirmed_dead() {
9970        // Alive for the first two checks, then dead - confirms the loop
9971        // actually re-polls rather than deciding once and sleeping out the
9972        // ceiling regardless.
9973        let calls = std::sync::atomic::AtomicUsize::new(0);
9974        let started = Instant::now();
9975        wait_for_pids_with(
9976            &[123],
9977            |_| calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst) < 2,
9978            Duration::from_millis(5),
9979            Duration::from_secs(5),
9980        )
9981        .await;
9982        assert!(
9983            calls.load(std::sync::atomic::Ordering::SeqCst) >= 3,
9984            "must keep checking rather than deciding on the first answer"
9985        );
9986        assert!(
9987            started.elapsed() < Duration::from_secs(1),
9988            "must return the moment it is confirmed dead, not wait out the ceiling"
9989        );
9990    }
9991
9992    #[tokio::test]
9993    async fn timed_out_pid_waiting_gives_up_at_its_ceiling_if_never_confirmed_dead() {
9994        let started = Instant::now();
9995        wait_for_pids_with(
9996            &[123],
9997            |_| true, // never reports dead
9998            Duration::from_millis(5),
9999            Duration::from_millis(30),
10000        )
10001        .await;
10002        let elapsed = started.elapsed();
10003        assert!(
10004            elapsed >= Duration::from_millis(30),
10005            "must not give up before its own ceiling: {elapsed:?}"
10006        );
10007        assert!(
10008            elapsed < Duration::from_secs(1),
10009            "must not wait past its own ceiling either: {elapsed:?}"
10010        );
10011    }
10012
10013    #[tokio::test]
10014    async fn timed_out_pid_waiting_is_a_no_op_when_nothing_was_still_running() {
10015        let started = Instant::now();
10016        wait_for_pids_with(
10017            &[],
10018            |_| true,
10019            Duration::from_secs(5),
10020            Duration::from_secs(5),
10021        )
10022        .await;
10023        assert!(
10024            started.elapsed() < Duration::from_millis(200),
10025            "an empty pid list has nothing to confirm"
10026        );
10027    }
10028
10029    // `review_conclusion` is the exact decision the review hand-off task
10030    // fixed: a round budget spent (or a tree that stopped moving) must not
10031    // collapse into `Blocked` regardless of what verification actually
10032    // said. Deterministic and process-free for the same reason the
10033    // `round_is_clean` family above is.
10034    fn review_round(
10035        clean: bool,
10036        blocking: usize,
10037        answered: usize,
10038        expected: usize,
10039        progressed: bool,
10040        e2e_ok: bool,
10041    ) -> ReviewRound {
10042        ReviewRound {
10043            round: 1,
10044            head: "h".to_owned(),
10045            verified_head: None,
10046            verified_at: None,
10047            reviews: Vec::new(),
10048            e2e: vec![CommandOutcome {
10049                command: "test".to_owned(),
10050                code: Some(if e2e_ok { 0 } else { 1 }),
10051                output_tail: String::new(),
10052                duration_ms: 0,
10053                resource_blocked: false,
10054            }],
10055            verify_retried: false,
10056            e2e_deferred: false,
10057            e2e_defer_reason: None,
10058            fix: None,
10059            blocking,
10060            answered,
10061            expected,
10062            clean,
10063            progressed,
10064            vote_split: false,
10065            reconsideration: Vec::new(),
10066            verdict: None,
10067        }
10068    }
10069
10070    #[test]
10071    fn review_conclusion_is_none_when_nothing_has_run() {
10072        assert_eq!(review_conclusion(&[], 3), None);
10073    }
10074
10075    #[test]
10076    fn review_conclusion_is_none_while_rounds_remain() {
10077        let rounds = vec![review_round(false, 1, 2, 2, true, true)];
10078        assert_eq!(review_conclusion(&rounds, 3), None);
10079    }
10080
10081    #[test]
10082    fn review_conclusion_is_gating_once_a_round_is_clean() {
10083        let rounds = vec![review_round(true, 0, 2, 2, false, true)];
10084        assert_eq!(review_conclusion(&rounds, 3), Some(RunStatus::Gating));
10085    }
10086
10087    #[test]
10088    fn review_conclusion_hands_off_when_the_budget_is_spent_and_e2e_is_green() {
10089        let rounds = vec![
10090            review_round(false, 1, 2, 2, true, true),
10091            review_round(false, 1, 2, 2, true, true),
10092        ];
10093        assert_eq!(review_conclusion(&rounds, 2), Some(RunStatus::Gating));
10094    }
10095
10096    #[test]
10097    fn review_conclusion_blocks_when_the_budget_is_spent_and_e2e_is_red() {
10098        let rounds = vec![
10099            review_round(false, 1, 2, 2, true, true),
10100            review_round(false, 1, 2, 2, true, false),
10101        ];
10102        assert_eq!(review_conclusion(&rounds, 2), Some(RunStatus::Blocked));
10103    }
10104
10105    #[test]
10106    fn review_conclusion_stays_none_when_the_budget_is_spent_but_the_last_round_could_not_run() {
10107        // Magi never got a command to run against this round's own head — a
10108        // resource-blocked attempt, not a red one — so this must never
10109        // settle on `Blocked` the way a genuine e2e failure would. `None`
10110        // here is what tells `Runner::review_loop` to retry the check
10111        // itself rather than trust this cheap recomputation with a verdict
10112        // it cannot actually produce.
10113        let mut blocked = review_round(false, 1, 2, 2, true, false);
10114        blocked.e2e[0].resource_blocked = true;
10115        let rounds = vec![review_round(false, 1, 2, 2, true, true), blocked];
10116        assert_eq!(review_conclusion(&rounds, 2), None);
10117    }
10118
10119    #[test]
10120    fn review_conclusion_blocks_an_incomplete_panel_that_raised_nothing_even_with_green_e2e() {
10121        // Missing input, not a verified tree — never a hand-off candidate.
10122        let rounds = vec![review_round(false, 0, 1, 2, false, true)];
10123        assert_eq!(review_conclusion(&rounds, 1), Some(RunStatus::Blocked));
10124    }
10125
10126    #[test]
10127    fn review_conclusion_hands_off_when_the_tree_stagnates_before_the_budget_is_spent() {
10128        let rounds = vec![
10129            review_round(false, 1, 2, 2, false, true),
10130            review_round(false, 1, 2, 2, false, true),
10131        ];
10132        assert_eq!(review_conclusion(&rounds, 10), Some(RunStatus::Gating));
10133    }
10134
10135    fn secs(n: u64) -> Duration {
10136        Duration::from_secs(n)
10137    }
10138
10139    /// A throwaway repo with one commit on `main`, for tests that need `merge`
10140    /// to make real (and, if it runs at all, real*ly fail*) git calls.
10141    fn init_repo(dir: &Path) {
10142        let run = |args: &[&str]| {
10143            let out = std::process::Command::new("git")
10144                .args(args)
10145                .current_dir(dir)
10146                .quiet()
10147                .output()
10148                .expect("spawn git");
10149            assert!(
10150                out.status.success(),
10151                "git {args:?} failed: {}",
10152                String::from_utf8_lossy(&out.stderr)
10153            );
10154        };
10155        run(&["init", "-b", "main"]);
10156        run(&["config", "user.name", "magi test"]);
10157        run(&["config", "user.email", "magi@example.com"]);
10158        std::fs::write(dir.join("README.md"), "# fixture\n").unwrap();
10159        run(&["add", "-A"]);
10160        run(&["commit", "-m", "init"]);
10161    }
10162
10163    // `settle_questions` is what closes the ghost the phone showed: a run's
10164    // seat asked something, the run then ended, and nothing was left to
10165    // abandon the question it left `open`. `HOME` is a process-wide
10166    // `OnceLock` (see `run::set_home`'s doc), so this only wins the race the
10167    // first time it runs in the binary — every test below still reaches the
10168    // same directory whichever call won, and each gets its own run id from
10169    // `RunState::new`, so they never collide there.
10170    fn ask_test_home() {
10171        crate::run::pin_test_home();
10172    }
10173
10174    /// A minimal, git-free `Runner` at a given status — `settle_questions`
10175    /// reads nothing else off it.
10176    fn runner_at(status: RunStatus) -> Runner {
10177        let mut state = RunState::new(
10178            PathBuf::from("/nonexistent/repo"),
10179            "main".to_owned(),
10180            "deadbeef".to_owned(),
10181            "task".to_owned(),
10182            Config::default(),
10183        );
10184        state.status = status;
10185        Runner {
10186            state,
10187            roles: ResolvedRoles {
10188                implementers: Vec::new(),
10189                judges: Vec::new(),
10190                reviewers: Vec::new(),
10191                fixer: None,
10192                conductor: conductor(),
10193                implementer_roster: Vec::new(),
10194                judge_roster: Vec::new(),
10195                reviewer_roster: Vec::new(),
10196            },
10197            sem: Arc::new(Semaphore::new(1)),
10198            pause: Pause::new(),
10199            interrupt: Pause::new(),
10200        }
10201    }
10202
10203    /// `park_here` folding in the reason `Pause::park_because` recorded -
10204    /// this is what lets an operator reading a run's events tell an
10205    /// interrupt-driven park from an ordinary shutdown park.
10206    #[test]
10207    fn park_here_folds_the_interrupt_reason_into_the_park_event() {
10208        crate::run::pin_test_home();
10209        let mut runner = runner_at(RunStatus::Implementing);
10210        let interrupt = Pause::new();
10211        runner.watch_interrupt(interrupt.clone());
10212
10213        interrupt.park_because("task a1b2 asked to run first");
10214
10215        assert!(runner.park_here().expect("park_here"));
10216        assert!(runner.state.parked);
10217        let last = runner.state.events.last().expect("a park event");
10218        assert_eq!(last.node, "park");
10219        assert!(
10220            last.message.contains("task a1b2 asked to run first"),
10221            "expected the interrupt reason in {:?}",
10222            last.message
10223        );
10224    }
10225
10226    /// `watch_interrupt` and `on_pause` are genuinely independent: an ordinary
10227    /// shutdown `Pause` (what `Stop::park` hands every run, shared and never
10228    /// cleared) must not make a *different* run - one only watching its own,
10229    /// unshared interrupt `Pause` - see itself as parked. If a future change
10230    /// ever collapsed these back into one handle, the interrupt scheduler
10231    /// would park every run for the rest of the daemon's life, not just the
10232    /// one it meant to interrupt.
10233    #[test]
10234    fn the_stop_level_pause_and_a_runs_interrupt_pause_do_not_leak_into_each_other() {
10235        crate::run::pin_test_home();
10236        let mut runner = runner_at(RunStatus::Implementing);
10237        let shutdown = Pause::new();
10238        runner.on_pause(shutdown.clone());
10239        let interrupt = Pause::new();
10240        runner.watch_interrupt(interrupt.clone());
10241
10242        // Nobody has asked for anything yet.
10243        assert!(!runner.park_here().expect("park_here"));
10244        assert!(!runner.state.parked);
10245
10246        // Only the interrupt handle fires; the shutdown handle stays clear.
10247        interrupt.park_because("test");
10248        assert!(!shutdown.parked());
10249        assert!(runner.park_here().expect("park_here"));
10250    }
10251
10252    /// The property every prior attempt at this feature failed to pin down:
10253    /// asking a run to park while one of its nodes has a real, in-flight
10254    /// async operation running (an agent call, in production) must not cut
10255    /// that operation short. `park_here` is only ever consulted *between*
10256    /// `execute`'s node calls - see its own doc - so nothing inside a node
10257    /// can observe a park request until the node itself returns. This proves
10258    /// that structurally, with real `tokio` concurrency and a channel
10259    /// handshake (never a sleep, which would only prove "usually", not
10260    /// "cannot"): the "node" below reports that it has genuinely started,
10261    /// and only then is the park requested; the node still has to be told to
10262    /// finish before `park_here` is ever called, exactly mirroring every
10263    /// `self.some_node().await; if self.park_here()? { return Ok(()); }` pair
10264    /// in `execute`.
10265    #[tokio::test]
10266    async fn a_park_request_made_mid_node_only_takes_effect_at_the_next_boundary() {
10267        crate::run::pin_test_home();
10268        let mut runner = runner_at(RunStatus::Implementing);
10269        let interrupt = Pause::new();
10270        runner.watch_interrupt(interrupt.clone());
10271
10272        let (started_tx, started_rx) = tokio::sync::oneshot::channel::<()>();
10273        let (finish_tx, finish_rx) = tokio::sync::oneshot::channel::<()>();
10274
10275        // Stands in for one node's in-flight agent call: it proves it has
10276        // genuinely started, then blocks - exactly as a spawned CLI process
10277        // does - until told to finish.
10278        let node = async move {
10279            started_tx.send(()).expect("send started");
10280            finish_rx.await.expect("recv finish");
10281            "node finished"
10282        };
10283
10284        let interrupter = async move {
10285            started_rx.await.expect("recv started");
10286            // The call is now genuinely in flight. Ask it to park.
10287            interrupt.park_because("higher-priority task waiting");
10288            // Nothing the node does can observe this yet - there is no
10289            // check inside it, by construction - so let the executor run
10290            // anything pending and then let the node finish on its own.
10291            tokio::task::yield_now().await;
10292            finish_tx.send(()).expect("send finish");
10293        };
10294
10295        let (node_result, ()) = tokio::join!(node, interrupter);
10296        assert_eq!(
10297            node_result, "node finished",
10298            "the in-flight call ran to completion"
10299        );
10300
10301        // Only now, at the boundary the real `execute` would check right
10302        // after this node, does the park take effect.
10303        assert!(runner.park_here().expect("park_here"));
10304        assert!(runner.state.parked);
10305    }
10306
10307    /// A run parked mid-competition carries every field it had accumulated
10308    /// through the exact same disk round-trip an ordinary resume uses -
10309    /// `RunState::save`/`RunState::load`, which is all `Runner::resume` is.
10310    /// Nothing about parking for an interrupt is a special case of that path;
10311    /// this is what proves it rather than assuming it.
10312    #[test]
10313    fn a_run_parked_for_an_interrupt_resumes_with_nothing_lost() {
10314        crate::run::pin_test_home();
10315        let mut runner = runner_at(RunStatus::Judging);
10316        // `Runner::resume` re-resolves roles from the saved config, which
10317        // refuses an empty roster - give it the same minimal one `conductor`
10318        // itself uses.
10319        runner.state.config.agents = vec![conductor()];
10320        runner.state.candidates = vec![Candidate {
10321            index: 0,
10322            label: 'A',
10323            agent: "alpha".to_owned(),
10324            branch: "magi/x/A".to_owned(),
10325            worktree: PathBuf::from("/nonexistent/worktree"),
10326            summary: "did the thing".to_owned(),
10327            stat: "1 file changed".to_owned(),
10328            files: 1,
10329            commits: 1,
10330            empty: false,
10331            failed: None,
10332            verified_noop: None,
10333            duration_ms: 1234,
10334            folded: false,
10335        }];
10336        let run_id = runner.state.id.clone();
10337
10338        let interrupt = Pause::new();
10339        runner.watch_interrupt(interrupt.clone());
10340        interrupt.park_because("task c3d4 asked to run first");
10341        assert!(runner.park_here().expect("park_here"));
10342
10343        let resumed = Runner::resume(&run_id).expect("resume");
10344        assert_eq!(resumed.state.candidates.len(), 1);
10345        assert_eq!(resumed.state.candidates[0].summary, "did the thing");
10346        assert_eq!(resumed.state.candidates[0].branch, "magi/x/A");
10347        assert_eq!(resumed.state.status, runner.state.status);
10348        assert!(
10349            resumed.state.parked,
10350            "still parked until `execute` actually walks the graph again"
10351        );
10352        assert!(resumed.state.events.iter().any(|e| e.node == "park"));
10353    }
10354
10355    /// A fresh open question on `run`, stored and handed back for assertions.
10356    fn ask_open_question(store: &ask::Questions, run: &str) -> ask::Question {
10357        let mut q = ask::Question::new(
10358            run.to_owned(),
10359            "implement".to_owned(),
10360            "impl-A".to_owned(),
10361            "Which storage backend should the cache use?".to_owned(),
10362            String::new(),
10363            vec!["SQLite".to_owned(), "Redis".to_owned()],
10364        );
10365        store.put(&mut q).unwrap();
10366        q
10367    }
10368
10369    #[test]
10370    fn a_failed_runs_open_question_is_abandoned() {
10371        ask_test_home();
10372        let store = ask::Questions::open();
10373        let mut runner = runner_at(RunStatus::Failed);
10374        let run = runner.state.id.clone();
10375        let q = ask_open_question(&store, &run);
10376
10377        runner.settle_questions();
10378
10379        let back = store.get(&q.id).unwrap();
10380        assert!(
10381            !back.status.open(),
10382            "the seat that asked died with the run; nobody is left to read an answer"
10383        );
10384        assert!(
10385            back.detail.contains(&run) && back.detail.contains("failed"),
10386            "the reason names what the run became, not just that it is gone: {}",
10387            back.detail
10388        );
10389    }
10390
10391    #[test]
10392    fn a_merged_runs_open_question_is_abandoned_too() {
10393        ask_test_home();
10394        let store = ask::Questions::open();
10395        // A run that finishes cleanly still leaves nobody to read an answer -
10396        // this is not only a failure-path cleanup.
10397        for status in [RunStatus::Merged, RunStatus::Ready] {
10398            let mut runner = runner_at(status);
10399            let run = runner.state.id.clone();
10400            let q = ask_open_question(&store, &run);
10401
10402            runner.settle_questions();
10403
10404            let back = store.get(&q.id).unwrap();
10405            assert!(
10406                !back.status.open(),
10407                "{status:?} run's question must not outlive the run"
10408            );
10409        }
10410    }
10411
10412    #[test]
10413    fn a_still_resumable_runs_open_question_is_left_alone() {
10414        ask_test_home();
10415        let store = ask::Questions::open();
10416        // `Blocked` and `Stalled` can still be resumed — the candidates, the
10417        // review round and the seat sessions are all still on disk — so a
10418        // question asked mid-round may yet get a real answer from a real
10419        // resume. Sweeping it here would be exactly the failure mode this
10420        // whole feature exists to avoid on the other side.
10421        for status in [RunStatus::Blocked, RunStatus::Stalled] {
10422            let mut runner = runner_at(status);
10423            let run = runner.state.id.clone();
10424            let q = ask_open_question(&store, &run);
10425
10426            runner.settle_questions();
10427
10428            let back = store.get(&q.id).unwrap();
10429            assert!(
10430                back.status.open(),
10431                "{status:?} is still alive; the question must still be waiting"
10432            );
10433        }
10434    }
10435
10436    #[test]
10437    fn settle_questions_never_touches_an_already_answered_question() {
10438        ask_test_home();
10439        let store = ask::Questions::open();
10440        let mut runner = runner_at(RunStatus::Failed);
10441        let run = runner.state.id.clone();
10442        let mut q = ask_open_question(&store, &run);
10443        q.answer(crate::ask::Answer::Choice("SQLite".to_owned()))
10444            .unwrap();
10445        store.put(&mut q).unwrap();
10446
10447        // Called twice, the way a crash-recovered daemon reclaim and the
10448        // graph's own cleanup both can for the same run — `abandon_for_run`
10449        // only ever touches what is still open, so this must be inert both
10450        // times, not merely the second.
10451        runner.settle_questions();
10452        runner.settle_questions();
10453
10454        let back = store.get(&q.id).unwrap();
10455        assert_eq!(
10456            back.status,
10457            ask::QuestionStatus::Answered,
10458            "a real answer is a decision on record, never overwritten by a sweep"
10459        );
10460    }
10461
10462    /// `fold_run(&mut state, drop_winner = false)` is exactly the call
10463    /// `clean::fold_due` makes for a `Ready`/`Failed` run - one that finished
10464    /// without merging, whose winner is still the operator's answer to read.
10465    /// Nothing previously called `fold_run` itself with a real `tally`, so
10466    /// this is the first test to pin down the one distinction the whole
10467    /// automatic-fold feature depends on: the winner's worktree and branch
10468    /// must survive, everything else sharing the run's worktree bay - a
10469    /// loser, standing in for a judge/review worktree too, since `fold_run`'s
10470    /// second sweep treats every non-winner directory under the bay alike -
10471    /// must not.
10472    #[tokio::test]
10473    async fn fold_run_keeps_only_the_winner_when_the_winner_is_not_dropped() {
10474        crate::run::pin_test_home();
10475        let tmp = tempfile::tempdir().expect("tempdir");
10476        let repo = tmp.path().join("repo");
10477        std::fs::create_dir_all(&repo).unwrap();
10478        init_repo(&repo);
10479
10480        let mut config = Config::default();
10481        config.graph.worktree_root = Some(tmp.path().join("wt"));
10482
10483        let mut state = RunState::new(
10484            repo.clone(),
10485            "main".to_owned(),
10486            "deadbeef".to_owned(),
10487            "task".to_owned(),
10488            config,
10489        );
10490        let root = state.worktree_root();
10491        let wt_a = root.join("cand-A");
10492        let wt_b = root.join("cand-B");
10493        git::worktree_add_branch(&repo, &wt_a, "magi/x/A", "main")
10494            .await
10495            .expect("worktree A");
10496        git::worktree_add_branch(&repo, &wt_b, "magi/x/B", "main")
10497            .await
10498            .expect("worktree B");
10499
10500        state.candidates = vec![
10501            Candidate {
10502                index: 0,
10503                label: 'A',
10504                agent: "alpha".to_owned(),
10505                branch: "magi/x/A".to_owned(),
10506                worktree: wt_a.clone(),
10507                summary: String::new(),
10508                stat: String::new(),
10509                files: 0,
10510                commits: 0,
10511                empty: false,
10512                failed: None,
10513                verified_noop: None,
10514                duration_ms: 0,
10515                folded: false,
10516            },
10517            Candidate {
10518                index: 1,
10519                label: 'B',
10520                agent: "beta".to_owned(),
10521                branch: "magi/x/B".to_owned(),
10522                worktree: wt_b.clone(),
10523                summary: String::new(),
10524                stat: String::new(),
10525                files: 0,
10526                commits: 0,
10527                empty: false,
10528                failed: None,
10529                verified_noop: None,
10530                duration_ms: 0,
10531                folded: false,
10532            },
10533        ];
10534        state.tally = Some(Tally {
10535            first_choice: BTreeMap::from([('A', 1)]),
10536            borda: BTreeMap::new(),
10537            winner: 'A',
10538            rankings: 1,
10539            unanimous_initial: true,
10540            deliberated: false,
10541            changed_votes: 0,
10542            unanimous_final: true,
10543            tie_break: None,
10544            judges: 1,
10545            present: 1,
10546            quorum: 1,
10547            met_quorum: true,
10548            uncontested: None,
10549        });
10550        state.status = RunStatus::Ready;
10551
10552        fold_run(&mut state, false, &crate::run::home())
10553            .await
10554            .expect("fold_run");
10555
10556        assert!(wt_a.exists(), "the unmerged winner's worktree survives");
10557        assert!(
10558            git::branch_exists(&repo, "magi/x/A").await.unwrap(),
10559            "the unmerged winner's branch survives"
10560        );
10561        assert!(
10562            !state.candidates[0].folded,
10563            "the winner is not marked folded"
10564        );
10565
10566        assert!(!wt_b.exists(), "the loser's worktree is removed");
10567        assert!(
10568            !git::branch_exists(&repo, "magi/x/B").await.unwrap(),
10569            "the loser's branch is removed"
10570        );
10571        assert!(state.candidates[1].folded, "the loser is marked folded");
10572    }
10573
10574    /// A branch handed to a later run is that run's (and its pull request's):
10575    /// folding the run that released it must not delete it.
10576    #[tokio::test]
10577    async fn fold_run_keeps_a_branch_that_was_handed_to_a_later_run() {
10578        let tmp = tempfile::tempdir().expect("tempdir");
10579        let repo = tmp.path().join("repo");
10580        std::fs::create_dir_all(&repo).unwrap();
10581        init_repo(&repo);
10582        let home = tmp.path().join("home");
10583
10584        let mut config = Config::default();
10585        config.graph.worktree_root = Some(tmp.path().join("wt"));
10586        let mut state = RunState::new(
10587            repo.clone(),
10588            "main".to_owned(),
10589            "deadbeef".to_owned(),
10590            "task".to_owned(),
10591            config,
10592        );
10593        // The worktree is already gone (released); the branch survives.
10594        git::git(&repo, &["branch", "magi/x/A", "main"])
10595            .await
10596            .expect("branch");
10597        state.candidates = vec![Candidate {
10598            index: 0,
10599            label: 'A',
10600            agent: "alpha".to_owned(),
10601            branch: "magi/x/A".to_owned(),
10602            worktree: state.worktree_root().join("cand-A"),
10603            summary: String::new(),
10604            stat: String::new(),
10605            files: 0,
10606            commits: 0,
10607            empty: false,
10608            failed: None,
10609            verified_noop: None,
10610            duration_ms: 0,
10611            folded: true,
10612        }];
10613        state.released_to = Some("20260901-000000-new1".to_owned());
10614        state.released_branches = vec!["magi/x/A".to_owned()];
10615
10616        fold_run(&mut state, true, &home).await.expect("fold_run");
10617
10618        assert!(
10619            git::branch_exists(&repo, "magi/x/A").await.unwrap(),
10620            "the handed-over branch survives a fold"
10621        );
10622    }
10623
10624    /// A winner with nothing ahead of the base is caught before `gh` is ever
10625    /// asked for a pull request, and the message carries what the task's
10626    /// references resolved to.
10627    #[tokio::test]
10628    async fn an_empty_winner_is_detected_before_a_pull_request_is_attempted() {
10629        let tmp = tempfile::tempdir().expect("tempdir");
10630        let repo = tmp.path().join("repo");
10631        std::fs::create_dir_all(&repo).unwrap();
10632        init_repo(&repo);
10633        let run = |args: &[&str]| {
10634            let out = std::process::Command::new("git")
10635                .quiet()
10636                .args(args)
10637                .current_dir(&repo)
10638                .output()
10639                .expect("spawn git");
10640            assert!(out.status.success(), "git {args:?}");
10641        };
10642        run(&["branch", "magi/x/A"]);
10643        run(&["checkout", "-q", "-b", "magi/x/B"]);
10644        std::fs::write(repo.join("f.txt"), "x\n").unwrap();
10645        run(&["add", "-A"]);
10646        run(&["commit", "-q", "-m", "work"]);
10647        run(&["checkout", "-q", "main"]);
10648
10649        // A pull request is compared against the remote's base, so the
10650        // fixture needs one. Before it exists nothing can be read, and the
10651        // answer must be "not empty".
10652        let probe = RunState::new(
10653            repo.clone(),
10654            "main".to_owned(),
10655            "deadbeef".to_owned(),
10656            "task".to_owned(),
10657            Config::default(),
10658        );
10659        assert!(!merge_is_empty(&repo, &probe, "magi/x/A", MergeMode::Pr).await);
10660        let bare = tmp.path().join("origin.git");
10661        let out = std::process::Command::new("git")
10662            .quiet()
10663            .args(["init", "-q", "--bare"])
10664            .arg(&bare)
10665            .output()
10666            .expect("spawn git");
10667        assert!(out.status.success(), "git init --bare");
10668        run(&["remote", "add", "origin", bare.to_str().unwrap()]);
10669        run(&["push", "-q", "origin", "main"]);
10670
10671        let mut state = RunState::new(
10672            repo.clone(),
10673            "main".to_owned(),
10674            "deadbeef".to_owned(),
10675            "task".to_owned(),
10676            Config::default(),
10677        );
10678        state.seeds = vec![refs::Seed {
10679            token: "magi/27b2/A".to_owned(),
10680            kind: refs::SeedKind::Unresolved,
10681            sha: String::new(),
10682            branch: true,
10683            detail: "no branch or commit named magi/27b2/A".to_owned(),
10684        }];
10685
10686        assert!(merge_is_empty(&repo, &state, "magi/x/A", MergeMode::Pr).await);
10687        assert!(merge_is_empty(&repo, &state, "magi/x/A", MergeMode::Local).await);
10688        assert!(!merge_is_empty(&repo, &state, "magi/x/B", MergeMode::Pr).await);
10689        let detail = empty_candidate_detail(&state, "main");
10690        assert!(detail.starts_with("empty candidate"), "{detail}");
10691        assert!(detail.contains("magi/27b2/A"), "{detail}");
10692    }
10693
10694    /// `status == Ready` used to be read as "this is the harmless
10695    /// `MergeMode::None` no-op path, nothing to guard" (graph.rs, prior to
10696    /// this test). But `land` sets the very same status when a `MergeMode::Pr`
10697    /// run's PR was closed without merging — and reentering `merge` with
10698    /// `mode` still `Pr` does not know the difference, so it pushed and
10699    /// opened a second pull request. `mode == Local` reproduces the same
10700    /// blind spot without a network call: reentry must not attempt another
10701    /// git merge once this node has already recorded an outcome.
10702    #[tokio::test]
10703    async fn merge_does_not_reattempt_once_a_run_has_concluded() {
10704        let tmp = tempfile::tempdir().expect("tempdir");
10705        let repo = tmp.path().join("repo");
10706        std::fs::create_dir_all(&repo).unwrap();
10707        init_repo(&repo);
10708
10709        let mut config = Config::default();
10710        config.merge.mode = MergeMode::Local;
10711
10712        let mut state = RunState::new(
10713            repo.clone(),
10714            "main".to_owned(),
10715            "deadbeef".to_owned(),
10716            "task".to_owned(),
10717            config,
10718        );
10719        state.candidates = vec![Candidate {
10720            index: 0,
10721            label: 'A',
10722            agent: "alpha".to_owned(),
10723            branch: "does-not-exist".to_owned(),
10724            worktree: repo.clone(),
10725            summary: String::new(),
10726            stat: String::new(),
10727            files: 0,
10728            commits: 0,
10729            empty: false,
10730            failed: None,
10731            verified_noop: None,
10732            duration_ms: 0,
10733            folded: false,
10734        }];
10735        state.tally = Some(Tally {
10736            first_choice: BTreeMap::from([('A', 1)]),
10737            borda: BTreeMap::new(),
10738            winner: 'A',
10739            rankings: 1,
10740            unanimous_initial: true,
10741            deliberated: false,
10742            changed_votes: 0,
10743            unanimous_final: true,
10744            tie_break: None,
10745            judges: 0,
10746            present: 0,
10747            quorum: 0,
10748            met_quorum: true,
10749            uncontested: Some("only candidate A produced a change".to_owned()),
10750        });
10751        state.reviews = vec![ReviewRound {
10752            round: 1,
10753            head: "deadbeef".to_owned(),
10754            verified_head: None,
10755            verified_at: None,
10756            reviews: Vec::new(),
10757            e2e: Vec::new(),
10758            fix: None,
10759            blocking: 0,
10760            answered: 0,
10761            expected: 0,
10762            clean: true,
10763            verify_retried: false,
10764            e2e_deferred: false,
10765            e2e_defer_reason: None,
10766            progressed: false,
10767            vote_split: false,
10768            reconsideration: Vec::new(),
10769            verdict: None,
10770        }];
10771        state.gate = vec![CommandOutcome {
10772            command: "test".to_owned(),
10773            code: Some(0),
10774            output_tail: String::new(),
10775            duration_ms: 0,
10776            resource_blocked: false,
10777        }];
10778        state.gate_ran = true;
10779        // Reached its conclusion already — e.g. `land` closing the PR without
10780        // merging it, which (like the honest `MergeMode::None` path) leaves
10781        // `status` at `Ready`. The recorded outcome is what actually marks
10782        // this node done.
10783        state.status = RunStatus::Ready;
10784        state.merge = Some(MergeOutcome {
10785            mode: MergeMode::Local,
10786            ok: false,
10787            detail: "already concluded".to_owned(),
10788            empty: false,
10789        });
10790
10791        let mut runner = Runner {
10792            state,
10793            roles: ResolvedRoles {
10794                implementers: Vec::new(),
10795                judges: Vec::new(),
10796                reviewers: Vec::new(),
10797                fixer: None,
10798                conductor: conductor(),
10799                implementer_roster: Vec::new(),
10800                judge_roster: Vec::new(),
10801                reviewer_roster: Vec::new(),
10802            },
10803            sem: Arc::new(Semaphore::new(1)),
10804            pause: Pause::new(),
10805            interrupt: Pause::new(),
10806        };
10807
10808        runner.merge().await.expect("merge");
10809
10810        assert_eq!(
10811            runner.state.status,
10812            RunStatus::Ready,
10813            "a concluded run's status must not change on reentry"
10814        );
10815        assert_eq!(
10816            runner.state.merge.as_ref().map(|m| m.detail.as_str()),
10817            Some("already concluded"),
10818            "merge must not run again once the node already recorded an outcome"
10819        );
10820    }
10821
10822    /// `gate` leaves `state.gate_ran` false both before it has ever run and
10823    /// when its last attempt was resource-blocked (the shared build cache
10824    /// could not be acquired or confirmed fresh in time - see
10825    /// `CommandOutcome::resource_blocked`'s own doc). Trusting the empty
10826    /// `Vec` this also leaves behind used to read as "nothing failed" and let
10827    /// a run merge a tree the gate never actually checked - exactly the case
10828    /// a contended cache produces on every retry until it clears. `merge`
10829    /// must refuse until `gate` has actually recorded an attempt.
10830    #[tokio::test]
10831    async fn merge_refuses_a_gate_that_has_not_actually_run() {
10832        let tmp = tempfile::tempdir().expect("tempdir");
10833        let repo = tmp.path().join("repo");
10834        std::fs::create_dir_all(&repo).unwrap();
10835        init_repo(&repo);
10836
10837        let mut config = Config::default();
10838        config.merge.mode = MergeMode::Local;
10839
10840        let mut state = RunState::new(
10841            repo.clone(),
10842            "main".to_owned(),
10843            "deadbeef".to_owned(),
10844            "task".to_owned(),
10845            config,
10846        );
10847        state.candidates = vec![Candidate {
10848            index: 0,
10849            label: 'A',
10850            agent: "alpha".to_owned(),
10851            branch: "does-not-exist".to_owned(),
10852            worktree: repo.clone(),
10853            summary: String::new(),
10854            stat: String::new(),
10855            files: 0,
10856            commits: 0,
10857            empty: false,
10858            failed: None,
10859            verified_noop: None,
10860            duration_ms: 0,
10861            folded: false,
10862        }];
10863        state.tally = Some(Tally {
10864            first_choice: BTreeMap::from([('A', 1)]),
10865            borda: BTreeMap::new(),
10866            winner: 'A',
10867            rankings: 1,
10868            unanimous_initial: true,
10869            deliberated: false,
10870            changed_votes: 0,
10871            unanimous_final: true,
10872            tie_break: None,
10873            judges: 0,
10874            present: 0,
10875            quorum: 0,
10876            met_quorum: true,
10877            uncontested: Some("only candidate A produced a change".to_owned()),
10878        });
10879        state.reviews = vec![ReviewRound {
10880            round: 1,
10881            head: "deadbeef".to_owned(),
10882            verified_head: None,
10883            verified_at: None,
10884            reviews: Vec::new(),
10885            e2e: Vec::new(),
10886            fix: None,
10887            blocking: 0,
10888            answered: 0,
10889            expected: 0,
10890            clean: true,
10891            verify_retried: false,
10892            e2e_deferred: false,
10893            e2e_defer_reason: None,
10894            progressed: false,
10895            vote_split: false,
10896            reconsideration: Vec::new(),
10897            verdict: None,
10898        }];
10899        // The point: `gate` has not recorded anything yet.
10900        state.gate = Vec::new();
10901        state.gate_ran = false;
10902        state.status = RunStatus::Gating;
10903
10904        let mut runner = Runner {
10905            state,
10906            roles: ResolvedRoles {
10907                implementers: Vec::new(),
10908                judges: Vec::new(),
10909                reviewers: Vec::new(),
10910                fixer: None,
10911                conductor: conductor(),
10912                implementer_roster: Vec::new(),
10913                judge_roster: Vec::new(),
10914                reviewer_roster: Vec::new(),
10915            },
10916            sem: Arc::new(Semaphore::new(1)),
10917            pause: Pause::new(),
10918            interrupt: Pause::new(),
10919        };
10920
10921        runner.merge().await.expect("merge");
10922
10923        assert!(
10924            runner.state.merge.is_none(),
10925            "an empty gate must never be read as a passing one: {:?}",
10926            runner.state.merge
10927        );
10928    }
10929
10930    /// The `shoka` repro this schema bump exists for: `verify.gate` has no
10931    /// commands configured and `merge.mode` is `none` (a review-only run).
10932    /// `gate` must still record a real attempt — zero commands, vacuously
10933    /// passed — rather than leaving `state.gate` empty in a way `merge`
10934    /// cannot tell apart from "never ran"; otherwise the run reaches
10935    /// `Gating` and can never leave it. See `RunState::gate_ran`'s own doc.
10936    #[tokio::test]
10937    async fn gate_and_merge_reach_ready_when_no_gate_commands_are_configured() {
10938        let tmp = tempfile::tempdir().expect("tempdir");
10939        let repo = tmp.path().join("repo");
10940        std::fs::create_dir_all(&repo).unwrap();
10941        init_repo(&repo);
10942
10943        // Default config: `verify.gate` empty, `merge.mode` is `none`.
10944        let config = Config::default();
10945
10946        let mut state = RunState::new(
10947            repo.clone(),
10948            "main".to_owned(),
10949            "deadbeef".to_owned(),
10950            "task".to_owned(),
10951            config,
10952        );
10953        state.candidates = vec![Candidate {
10954            index: 0,
10955            label: 'A',
10956            agent: "alpha".to_owned(),
10957            branch: "does-not-exist".to_owned(),
10958            worktree: repo.clone(),
10959            summary: String::new(),
10960            stat: String::new(),
10961            files: 0,
10962            commits: 0,
10963            empty: false,
10964            failed: None,
10965            verified_noop: None,
10966            duration_ms: 0,
10967            folded: false,
10968        }];
10969        state.tally = Some(Tally {
10970            first_choice: BTreeMap::from([('A', 1)]),
10971            borda: BTreeMap::new(),
10972            winner: 'A',
10973            rankings: 1,
10974            unanimous_initial: true,
10975            deliberated: false,
10976            changed_votes: 0,
10977            unanimous_final: true,
10978            tie_break: None,
10979            judges: 0,
10980            present: 0,
10981            quorum: 0,
10982            met_quorum: true,
10983            uncontested: Some("only candidate A produced a change".to_owned()),
10984        });
10985        state.reviews = vec![ReviewRound {
10986            round: 1,
10987            head: "deadbeef".to_owned(),
10988            verified_head: None,
10989            verified_at: None,
10990            reviews: Vec::new(),
10991            e2e: Vec::new(),
10992            fix: None,
10993            blocking: 0,
10994            answered: 0,
10995            expected: 0,
10996            clean: true,
10997            verify_retried: false,
10998            e2e_deferred: false,
10999            e2e_defer_reason: None,
11000            progressed: false,
11001            vote_split: false,
11002            reconsideration: Vec::new(),
11003            verdict: None,
11004        }];
11005
11006        let mut runner = Runner {
11007            state,
11008            roles: ResolvedRoles {
11009                implementers: Vec::new(),
11010                judges: Vec::new(),
11011                reviewers: Vec::new(),
11012                fixer: None,
11013                conductor: conductor(),
11014                implementer_roster: Vec::new(),
11015                judge_roster: Vec::new(),
11016                reviewer_roster: Vec::new(),
11017            },
11018            sem: Arc::new(Semaphore::new(1)),
11019            pause: Pause::new(),
11020            interrupt: Pause::new(),
11021        };
11022
11023        runner.gate().await.expect("gate");
11024        assert!(
11025            runner.state.gate_ran,
11026            "zero configured commands is still a real attempt, not an unrun gate"
11027        );
11028        assert!(runner.state.gate.is_empty());
11029        assert_eq!(runner.state.gate_status(), GateStatus::PassedWithNoCommands);
11030        assert_ne!(
11031            runner.state.status,
11032            RunStatus::Blocked,
11033            "a gate with nothing to check must not read as failed"
11034        );
11035
11036        runner.merge().await.expect("merge");
11037        assert_eq!(
11038            runner.state.status,
11039            RunStatus::Ready,
11040            "a clean review-only run with no gate commands must reach Ready, not stay stuck in Gating"
11041        );
11042    }
11043
11044    /// `Config::cache_dir` is derived from `verify.e2e` as well as
11045    /// `verify.gate` (so the e2e leg and the final gate never build against
11046    /// different directories). With zero `verify.gate` commands but a
11047    /// `CARGO_TARGET_DIR`-using `verify.e2e`, `gate` used to still queue for
11048    /// that lease before discovering it had nothing to run - so a repo with
11049    /// no gate commands could come back `resource_blocked` (and therefore
11050    /// still `gate_ran == false`) on nothing but an unrelated run holding the
11051    /// cache, exactly the contention this run's own zero commands could
11052    /// never have touched. `gate` must recognise there is nothing to check
11053    /// before it ever asks for the lease.
11054    #[tokio::test]
11055    async fn gate_never_asks_for_the_cache_lease_when_it_has_no_commands_to_run() {
11056        crate::run::pin_test_home();
11057        let home = crate::run::home();
11058
11059        let tmp = tempfile::tempdir().expect("tempdir");
11060        let repo = tmp.path().join("repo");
11061        std::fs::create_dir_all(&repo).unwrap();
11062        init_repo(&repo);
11063        // Unique to this test, so holding its lease cannot collide with
11064        // another test sharing the same process-wide `home`.
11065        let cache_dir = tmp.path().join("target");
11066
11067        let mut config = Config::default();
11068        config.verify.e2e = vec![format!("CARGO_TARGET_DIR='{}' true", cache_dir.display())];
11069        // `verify.gate` stays empty (the default). Bounded so a regression
11070        // that does start waiting fails the test in seconds, not hangs it.
11071        config.graph.timeout_verify = Some(2);
11072
11073        let other = crate::cache::Owner::here("other-run", "e2e", "e2e", &repo, "deadbeef");
11074        let _held = match crate::cache::try_acquire(&home, &cache_dir, &other)
11075            .expect("no io error acquiring directly")
11076        {
11077            crate::cache::AcquireOutcome::Acquired(g) => g,
11078            crate::cache::AcquireOutcome::Busy(b) => {
11079                panic!("expected the direct acquire to win the lease first: {b:?}")
11080            }
11081        };
11082
11083        let mut state = RunState::new(
11084            repo.clone(),
11085            "main".to_owned(),
11086            "deadbeef".to_owned(),
11087            "task".to_owned(),
11088            config,
11089        );
11090        state.candidates = vec![Candidate {
11091            index: 0,
11092            label: 'A',
11093            agent: "alpha".to_owned(),
11094            branch: "does-not-exist".to_owned(),
11095            worktree: repo.clone(),
11096            summary: String::new(),
11097            stat: String::new(),
11098            files: 0,
11099            commits: 0,
11100            empty: false,
11101            failed: None,
11102            verified_noop: None,
11103            duration_ms: 0,
11104            folded: false,
11105        }];
11106        state.tally = Some(Tally {
11107            first_choice: BTreeMap::from([('A', 1)]),
11108            borda: BTreeMap::new(),
11109            winner: 'A',
11110            rankings: 1,
11111            unanimous_initial: true,
11112            deliberated: false,
11113            changed_votes: 0,
11114            unanimous_final: true,
11115            tie_break: None,
11116            judges: 0,
11117            present: 0,
11118            quorum: 0,
11119            met_quorum: true,
11120            uncontested: Some("only candidate A produced a change".to_owned()),
11121        });
11122        state.reviews = vec![ReviewRound {
11123            round: 1,
11124            head: "deadbeef".to_owned(),
11125            verified_head: None,
11126            verified_at: None,
11127            reviews: Vec::new(),
11128            e2e: Vec::new(),
11129            fix: None,
11130            blocking: 0,
11131            answered: 0,
11132            expected: 0,
11133            clean: true,
11134            verify_retried: false,
11135            e2e_deferred: false,
11136            e2e_defer_reason: None,
11137            progressed: false,
11138            vote_split: false,
11139            reconsideration: Vec::new(),
11140            verdict: None,
11141        }];
11142
11143        let mut runner = Runner {
11144            state,
11145            roles: ResolvedRoles {
11146                implementers: Vec::new(),
11147                judges: Vec::new(),
11148                reviewers: Vec::new(),
11149                fixer: None,
11150                conductor: conductor(),
11151                implementer_roster: Vec::new(),
11152                judge_roster: Vec::new(),
11153                reviewer_roster: Vec::new(),
11154            },
11155            sem: Arc::new(Semaphore::new(1)),
11156            pause: Pause::new(),
11157            interrupt: Pause::new(),
11158        };
11159
11160        let started = std::time::Instant::now();
11161        runner.gate().await.expect("gate");
11162        assert!(
11163            started.elapsed() < Duration::from_secs(1),
11164            "a gate with nothing to run must never wait on a lease it never needed"
11165        );
11166        assert!(
11167            runner.state.gate_ran,
11168            "zero commands is still a real, immediate attempt"
11169        );
11170        assert!(runner.state.gate.is_empty());
11171        assert_ne!(
11172            runner.state.status,
11173            RunStatus::Blocked,
11174            "must not read as resource-blocked on a lease it never asked for"
11175        );
11176    }
11177
11178    /// The addendum's second gap: a `verify.gate` command running for real
11179    /// wall-clock time had nothing at all to show for it in `active` before
11180    /// `run_commands` learned to record it — a run could sit in `Gating` for
11181    /// minutes with `magi show` and `GET /api/runs/{id}` both silent about
11182    /// what was actually happening. Proven with a genuinely still-running
11183    /// command, not just a before/after check on the final state: a poller
11184    /// task reads the same `run.json` `gate()` is writing, the same way the
11185    /// phone or `magi show` would, while the shell command is still blocked
11186    /// on its own release marker.
11187    #[tokio::test]
11188    async fn gate_records_a_running_task_entry_while_its_command_is_still_in_flight() {
11189        crate::run::pin_test_home();
11190
11191        let tmp = tempfile::tempdir().expect("tempdir");
11192        let repo = tmp.path().join("repo");
11193        std::fs::create_dir_all(&repo).unwrap();
11194        init_repo(&repo);
11195
11196        let mut config = Config::default();
11197        config.verify.gate = vec![
11198            "printf started > started.marker; i=0; while [ ! -f release.marker ] && \
11199             [ \"$i\" -lt 100 ]; do i=$((i+1)); sleep 0.05; done"
11200                .to_owned(),
11201        ];
11202
11203        let mut state = RunState::new(
11204            repo.clone(),
11205            "main".to_owned(),
11206            "deadbeef".to_owned(),
11207            "task".to_owned(),
11208            config,
11209        );
11210        let run_id = state.id.clone();
11211        state.candidates = vec![Candidate {
11212            index: 0,
11213            label: 'A',
11214            agent: "alpha".to_owned(),
11215            branch: "does-not-exist".to_owned(),
11216            worktree: repo.clone(),
11217            summary: String::new(),
11218            stat: String::new(),
11219            files: 0,
11220            commits: 0,
11221            empty: false,
11222            failed: None,
11223            verified_noop: None,
11224            duration_ms: 0,
11225            folded: false,
11226        }];
11227        state.tally = Some(Tally {
11228            first_choice: BTreeMap::from([('A', 1)]),
11229            borda: BTreeMap::new(),
11230            winner: 'A',
11231            rankings: 1,
11232            unanimous_initial: true,
11233            deliberated: false,
11234            changed_votes: 0,
11235            unanimous_final: true,
11236            tie_break: None,
11237            judges: 0,
11238            present: 0,
11239            quorum: 0,
11240            met_quorum: true,
11241            uncontested: Some("only candidate A produced a change".to_owned()),
11242        });
11243        state.reviews = vec![ReviewRound {
11244            round: 1,
11245            head: "deadbeef".to_owned(),
11246            verified_head: None,
11247            verified_at: None,
11248            reviews: Vec::new(),
11249            e2e: Vec::new(),
11250            fix: None,
11251            blocking: 0,
11252            answered: 0,
11253            expected: 0,
11254            clean: true,
11255            verify_retried: false,
11256            e2e_deferred: false,
11257            e2e_defer_reason: None,
11258            progressed: false,
11259            vote_split: false,
11260            reconsideration: Vec::new(),
11261            verdict: None,
11262        }];
11263
11264        let mut runner = Runner {
11265            state,
11266            roles: ResolvedRoles {
11267                implementers: Vec::new(),
11268                judges: Vec::new(),
11269                reviewers: Vec::new(),
11270                fixer: None,
11271                conductor: conductor(),
11272                implementer_roster: Vec::new(),
11273                judge_roster: Vec::new(),
11274                reviewer_roster: Vec::new(),
11275            },
11276            sem: Arc::new(Semaphore::new(1)),
11277            pause: Pause::new(),
11278            interrupt: Pause::new(),
11279        };
11280
11281        let started_marker = repo.join("started.marker");
11282        let release_marker = repo.join("release.marker");
11283        let poller = tokio::spawn(async move {
11284            // Bounded so a regression that never records the task entry
11285            // fails this test in seconds instead of hanging the suite —
11286            // the same shape `a_park_requested_while_a_seat_is_mid_call_
11287            // does_not_cut_it_short` uses for the same reason.
11288            for _ in 0..100 {
11289                if started_marker.exists()
11290                    && let Ok(s) = crate::run::RunState::load(&run_id)
11291                    && let Some(a) = s.active.get("gate")
11292                {
11293                    std::fs::write(&release_marker, b"go").expect("release marker");
11294                    return Some(a.clone());
11295                }
11296                tokio::time::sleep(Duration::from_millis(50)).await;
11297            }
11298            None
11299        });
11300
11301        runner.gate().await.expect("gate");
11302        let captured = poller.await.expect("poller task");
11303        let captured = captured.expect(
11304            "the poller never saw a `gate` task entry in run.json while the command was \
11305             still blocked on its own release marker",
11306        );
11307
11308        assert_eq!(captured.task.as_deref(), Some("gate"));
11309        assert_eq!(captured.node, "gate");
11310        assert_eq!(captured.index, Some(1));
11311        assert_eq!(captured.total, Some(1));
11312        assert!(
11313            captured
11314                .command
11315                .as_deref()
11316                .is_some_and(|c| c.contains("started.marker")),
11317            "{captured:?}"
11318        );
11319
11320        assert!(
11321            runner.state.active.is_empty(),
11322            "the entry must be cleared once the command actually finished: {:?}",
11323            runner.state.active
11324        );
11325        assert!(runner.state.gate_ran);
11326        assert!(runner.state.gate.iter().all(CommandOutcome::ok));
11327    }
11328
11329    /// The hand-off over a blocking finding a reviewer rejected on leaves a
11330    /// record for `land`; one with only a Minor, or no reject, leaves none.
11331    #[tokio::test]
11332    async fn stop_reviewing_records_a_contested_hand_off_only_for_major_plus_reject() {
11333        use crate::verdict::{Finding, ReviewVote, Severity};
11334        crate::run::pin_test_home();
11335        let tmp = tempfile::tempdir().expect("tempdir");
11336        let repo = tmp.path().join("repo");
11337        std::fs::create_dir_all(&repo).unwrap();
11338        init_repo(&repo);
11339
11340        for (severity, vote, expect) in [
11341            (Severity::Major, ReviewVote::Reject, true),
11342            (Severity::Minor, ReviewVote::Reject, false),
11343            (Severity::Major, ReviewVote::Approve, false),
11344        ] {
11345            let mut round = review_round(false, 1, 1, 1, false, true);
11346            round.reviews = vec![ReviewRecord {
11347                reviewer: 1,
11348                agent: "alpha".to_owned(),
11349                summary: String::new(),
11350                findings: vec![Finding {
11351                    id: "R1-1-1".to_owned(),
11352                    severity,
11353                    file: None,
11354                    line: None,
11355                    title: "t".to_owned(),
11356                    detail: String::new(),
11357                }],
11358                vote: Some(vote),
11359                failed: None,
11360                duration_ms: 0,
11361                attempts: 0,
11362            }];
11363            let mut state = RunState::new(
11364                repo.clone(),
11365                "main".to_owned(),
11366                "deadbeef".to_owned(),
11367                "task".to_owned(),
11368                Config::default(),
11369            );
11370            state.reviews = vec![round];
11371            let mut runner = Runner {
11372                state,
11373                roles: ResolvedRoles {
11374                    implementers: Vec::new(),
11375                    judges: Vec::new(),
11376                    reviewers: Vec::new(),
11377                    fixer: None,
11378                    conductor: conductor(),
11379                    implementer_roster: Vec::new(),
11380                    judge_roster: Vec::new(),
11381                    reviewer_roster: Vec::new(),
11382                },
11383                sem: Arc::new(Semaphore::new(1)),
11384                pause: Pause::new(),
11385                interrupt: Pause::new(),
11386            };
11387            let shell = runner.state.config.shell();
11388            runner
11389                .stop_reviewing("round budget spent", &shell, &repo)
11390                .await
11391                .expect("stop_reviewing");
11392            assert_eq!(runner.state.status, RunStatus::Gating);
11393            assert_eq!(
11394                runner.state.contested_handoff.is_some(),
11395                expect,
11396                "{severity:?} + {vote:?}"
11397            );
11398        }
11399    }
11400
11401    /// The shape the incident this whole fix responds to actually had: the
11402    /// round budget spent, the last round's own e2e blocked on the shared
11403    /// build cache (held here by a live pid — this test process — exactly
11404    /// `cache`'s own unit tests' pattern for "another owner, still alive"
11405    /// without forking a process). `stop_reviewing` must retry it — not
11406    /// silently leave the round looking untouched (the catch-up-only half of
11407    /// the bug), and not read the contention as a red `e2e` and block the
11408    /// run on it (the other half). Called directly, the same way
11409    /// `gate_never_asks_for_the_cache_lease_when_it_has_no_commands_to_run`
11410    /// above exercises `gate`, so this never needs a real cargo build to
11411    /// reach: the lease is never released, so `with_cache_lease` never gets
11412    /// past acquiring it into anything that would need a real workspace.
11413    #[tokio::test]
11414    async fn stop_reviewing_retries_a_resource_blocked_e2e_instead_of_reading_it_as_red() {
11415        crate::run::pin_test_home();
11416        let home = crate::run::home();
11417
11418        let tmp = tempfile::tempdir().expect("tempdir");
11419        let repo = tmp.path().join("repo");
11420        std::fs::create_dir_all(&repo).unwrap();
11421        init_repo(&repo);
11422        let head = crate::git::rev_parse(&repo, "HEAD")
11423            .await
11424            .expect("rev-parse");
11425        // Unique to this test, so holding its lease cannot collide with
11426        // another test sharing the same process-wide `home`.
11427        let cache_dir = tmp.path().join("target");
11428
11429        let mut config = Config::default();
11430        config.verify.e2e = vec![format!(
11431            "CARGO_TARGET_DIR='{}' test -f README.md",
11432            cache_dir.display()
11433        )];
11434        config.graph.review_rounds = 1;
11435        // Bounded so a regression that does start waiting fails the test in
11436        // seconds, not hangs it.
11437        config.graph.timeout_verify = Some(2);
11438
11439        let other = crate::cache::Owner::here("other-run", "e2e", "e2e", &repo, "deadbeef");
11440        let held = match crate::cache::try_acquire(&home, &cache_dir, &other)
11441            .expect("no io error acquiring directly")
11442        {
11443            crate::cache::AcquireOutcome::Acquired(g) => g,
11444            crate::cache::AcquireOutcome::Busy(b) => {
11445                panic!("expected the direct acquire to win the lease first: {b:?}")
11446            }
11447        };
11448
11449        let mut state = RunState::new(
11450            repo.clone(),
11451            "main".to_owned(),
11452            head.clone(),
11453            "task".to_owned(),
11454            config,
11455        );
11456        state.candidates = vec![Candidate {
11457            index: 0,
11458            label: 'A',
11459            agent: "alpha".to_owned(),
11460            branch: "does-not-exist".to_owned(),
11461            worktree: repo.clone(),
11462            summary: String::new(),
11463            stat: String::new(),
11464            files: 0,
11465            commits: 0,
11466            empty: false,
11467            failed: None,
11468            verified_noop: None,
11469            duration_ms: 0,
11470            folded: false,
11471        }];
11472        state.tally = Some(Tally {
11473            first_choice: BTreeMap::from([('A', 1)]),
11474            borda: BTreeMap::new(),
11475            winner: 'A',
11476            rankings: 1,
11477            unanimous_initial: true,
11478            deliberated: false,
11479            changed_votes: 0,
11480            unanimous_final: true,
11481            tie_break: None,
11482            judges: 0,
11483            present: 0,
11484            quorum: 0,
11485            met_quorum: true,
11486            uncontested: Some("only candidate A produced a change".to_owned()),
11487        });
11488        // The round budget's last round, deferred: `needs_catchup_run`'s
11489        // other trigger. `stop_reviewing`'s retry machinery must treat this
11490        // exactly like a resource-blocked attempt once it actually runs.
11491        state.reviews = vec![ReviewRound {
11492            round: 1,
11493            head: head.clone(),
11494            verified_head: None,
11495            verified_at: None,
11496            reviews: Vec::new(),
11497            e2e: Vec::new(),
11498            fix: None,
11499            blocking: 1,
11500            answered: 1,
11501            expected: 1,
11502            clean: false,
11503            verify_retried: false,
11504            e2e_deferred: true,
11505            e2e_defer_reason: Some("1 blocking finding(s) already required a fix".to_owned()),
11506            progressed: false,
11507            vote_split: false,
11508            reconsideration: Vec::new(),
11509            verdict: None,
11510        }];
11511
11512        let mut runner = Runner {
11513            state,
11514            roles: ResolvedRoles {
11515                implementers: Vec::new(),
11516                judges: Vec::new(),
11517                reviewers: Vec::new(),
11518                fixer: None,
11519                conductor: conductor(),
11520                implementer_roster: Vec::new(),
11521                judge_roster: Vec::new(),
11522                reviewer_roster: Vec::new(),
11523            },
11524            sem: Arc::new(Semaphore::new(1)),
11525            pause: Pause::new(),
11526            interrupt: Pause::new(),
11527        };
11528
11529        let shell = runner.state.config.shell();
11530        runner
11531            .stop_reviewing("round budget spent", &shell, &repo)
11532            .await
11533            .expect("stop_reviewing");
11534
11535        let last = runner.state.reviews.last().expect("round record");
11536        assert_eq!(
11537            last.e2e_status(),
11538            E2eStatus::ResourceBlocked,
11539            "the shared cache is still held; the attempt must read as blocked, not deferred or \
11540             failed: {last:?}"
11541        );
11542        assert_eq!(
11543            last.verified_head.as_deref(),
11544            Some(head.as_str()),
11545            "which commit this attempt targeted is known even though nothing finished checking \
11546             it"
11547        );
11548        let first_attempt_at = last
11549            .verified_at
11550            .expect("when this attempt ran is known too");
11551        assert_ne!(
11552            runner.state.status,
11553            RunStatus::Blocked,
11554            "contention is evidence about the machine, not the patch — it must not settle the \
11555             run as blocked: {:?}",
11556            runner.state.status
11557        );
11558        assert!(
11559            !runner
11560                .state
11561                .events
11562                .iter()
11563                .any(|e| e.node == "review" && e.message.contains("e2e failed")),
11564            "a resource-blocked attempt must never be logged as a failed e2e: {:?}",
11565            runner.state.events
11566        );
11567
11568        // The cache is still held: a later reentry must retry the same
11569        // round's verification again — not leave it looking exactly as
11570        // untouched as the first blocked attempt, which is indistinguishable
11571        // from never having tried again at all.
11572        runner
11573            .stop_reviewing("round budget spent", &shell, &repo)
11574            .await
11575            .expect("stop_reviewing retry");
11576        assert_eq!(
11577            runner.state.reviews.len(),
11578            1,
11579            "no new round was started: {:?}",
11580            runner.state.reviews
11581        );
11582        let last = runner.state.reviews.last().expect("round record");
11583        assert_eq!(last.e2e_status(), E2eStatus::ResourceBlocked, "{last:?}");
11584        assert!(
11585            last.verified_at.expect("still known") > first_attempt_at,
11586            "a second reentry must be a fresh attempt, not a stale copy of the first"
11587        );
11588        assert_ne!(runner.state.status, RunStatus::Blocked);
11589
11590        held.release();
11591    }
11592
11593    /// A resumed run — a fresh `Runner`, `self.state.reviews` already
11594    /// holding the round `stop_reviewing` left `ResourceBlocked` from a
11595    /// prior process — must not sit at `Reviewing` forever: `review_loop`'s
11596    /// own top-of-function fast path (`review_conclusion`) correctly reads
11597    /// this shape as `None` rather than guessing `Blocked`, and the loop's
11598    /// own `for` range is empty once the round budget is spent, so
11599    /// `review_loop` must retry the check itself rather than silently doing
11600    /// nothing. Reaches the exact same retry `stop_reviewing_retries_a_*`
11601    /// above exercises directly, but through `review_loop`'s own entry point
11602    /// this time, proving the wiring between the two rather than just the
11603    /// retry logic in isolation.
11604    #[tokio::test]
11605    async fn a_resumed_review_loop_retries_a_last_round_left_resource_blocked() {
11606        crate::run::pin_test_home();
11607        let home = crate::run::home();
11608
11609        let tmp = tempfile::tempdir().expect("tempdir");
11610        let repo = tmp.path().join("repo");
11611        std::fs::create_dir_all(&repo).unwrap();
11612        init_repo(&repo);
11613        let head = crate::git::rev_parse(&repo, "HEAD")
11614            .await
11615            .expect("rev-parse");
11616        let cache_dir = tmp.path().join("target");
11617
11618        let mut config = Config::default();
11619        config.verify.e2e = vec![format!(
11620            "CARGO_TARGET_DIR='{}' test -f README.md",
11621            cache_dir.display()
11622        )];
11623        config.graph.review_rounds = 1;
11624        config.graph.timeout_verify = Some(2);
11625
11626        let other = crate::cache::Owner::here("other-run", "e2e", "e2e", &repo, "deadbeef");
11627        let held = match crate::cache::try_acquire(&home, &cache_dir, &other)
11628            .expect("no io error acquiring directly")
11629        {
11630            crate::cache::AcquireOutcome::Acquired(g) => g,
11631            crate::cache::AcquireOutcome::Busy(b) => {
11632                panic!("expected the direct acquire to win the lease first: {b:?}")
11633            }
11634        };
11635
11636        let mut state = RunState::new(
11637            repo.clone(),
11638            "main".to_owned(),
11639            head.clone(),
11640            "task".to_owned(),
11641            config,
11642        );
11643        state.candidates = vec![Candidate {
11644            index: 0,
11645            label: 'A',
11646            agent: "alpha".to_owned(),
11647            branch: "does-not-exist".to_owned(),
11648            worktree: repo.clone(),
11649            summary: String::new(),
11650            stat: String::new(),
11651            files: 0,
11652            commits: 0,
11653            empty: false,
11654            failed: None,
11655            verified_noop: None,
11656            duration_ms: 0,
11657            folded: false,
11658        }];
11659        state.tally = Some(Tally {
11660            first_choice: BTreeMap::from([('A', 1)]),
11661            borda: BTreeMap::new(),
11662            winner: 'A',
11663            rankings: 1,
11664            unanimous_initial: true,
11665            deliberated: false,
11666            changed_votes: 0,
11667            unanimous_final: true,
11668            tie_break: None,
11669            judges: 0,
11670            present: 0,
11671            quorum: 0,
11672            met_quorum: true,
11673            uncontested: Some("only candidate A produced a change".to_owned()),
11674        });
11675        // The exact shape a prior process's `stop_reviewing` would have left
11676        // on disk: the round budget's last round, a real attempt already
11677        // made and already resource-blocked.
11678        state.reviews = vec![ReviewRound {
11679            round: 1,
11680            head: head.clone(),
11681            verified_head: Some(head.clone()),
11682            verified_at: Some(jiff::Timestamp::now()),
11683            reviews: Vec::new(),
11684            e2e: vec![CommandOutcome {
11685                command: format!(
11686                    "CARGO_TARGET_DIR='{}' test -f README.md",
11687                    cache_dir.display()
11688                ),
11689                code: None,
11690                output_tail: "waiting for the shared build cache".to_owned(),
11691                duration_ms: 0,
11692                resource_blocked: true,
11693            }],
11694            fix: None,
11695            blocking: 1,
11696            answered: 1,
11697            expected: 1,
11698            clean: false,
11699            verify_retried: false,
11700            e2e_deferred: false,
11701            e2e_defer_reason: None,
11702            progressed: false,
11703            vote_split: false,
11704            reconsideration: Vec::new(),
11705            verdict: None,
11706        }];
11707
11708        let first_attempt_at = state.reviews[0].verified_at.expect("set above");
11709        let mut runner = Runner {
11710            state,
11711            roles: ResolvedRoles {
11712                implementers: Vec::new(),
11713                judges: Vec::new(),
11714                reviewers: Vec::new(),
11715                fixer: None,
11716                conductor: conductor(),
11717                implementer_roster: Vec::new(),
11718                judge_roster: Vec::new(),
11719                reviewer_roster: Vec::new(),
11720            },
11721            sem: Arc::new(Semaphore::new(1)),
11722            pause: Pause::new(),
11723            interrupt: Pause::new(),
11724        };
11725
11726        // The lease is still held throughout, so this reentry's own retry is
11727        // also contended — proving `review_loop` actually tried again (not
11728        // that it happened to succeed) is what the timestamp comparison
11729        // below is for.
11730        runner.review_loop().await.expect("review_loop");
11731
11732        assert_eq!(
11733            runner.state.reviews.len(),
11734            1,
11735            "no new round was started on top of the unresolved one: {:?}",
11736            runner.state.reviews
11737        );
11738        let last = &runner.state.reviews[0];
11739        assert_eq!(
11740            last.e2e_status(),
11741            E2eStatus::ResourceBlocked,
11742            "still contended: {last:?}"
11743        );
11744        assert!(
11745            last.verified_at.expect("still known") > first_attempt_at,
11746            "review_loop must have actually retried the check, not left it exactly as found"
11747        );
11748        assert_ne!(
11749            runner.state.status,
11750            RunStatus::Blocked,
11751            "a resumed run must not read leftover contention as a verdict on the patch: {:?}",
11752            runner.state.status
11753        );
11754
11755        held.release();
11756    }
11757
11758    #[tokio::test]
11759    async fn a_run_resumed_mid_landing_reenters_land_instead_of_opening_a_second_pull_request() {
11760        crate::run::pin_test_home();
11761        let tmp = tempfile::tempdir().expect("tempdir");
11762        let repo = tmp.path().join("repo");
11763        std::fs::create_dir_all(&repo).unwrap();
11764        init_repo(&repo);
11765
11766        let mut config = Config::default();
11767        config.merge.mode = MergeMode::Pr;
11768        config.graph.land = true;
11769        config.graph.land_approval = false;
11770
11771        let mut state = RunState::new(
11772            repo.clone(),
11773            "main".to_owned(),
11774            "deadbeef".to_owned(),
11775            "task".to_owned(),
11776            config,
11777        );
11778        state.candidates = vec![Candidate {
11779            index: 0,
11780            label: 'A',
11781            agent: "alpha".to_owned(),
11782            branch: "does-not-exist".to_owned(),
11783            worktree: repo.clone(),
11784            summary: String::new(),
11785            stat: String::new(),
11786            files: 0,
11787            commits: 0,
11788            empty: false,
11789            failed: None,
11790            verified_noop: None,
11791            duration_ms: 0,
11792            folded: false,
11793        }];
11794        state.tally = Some(Tally {
11795            first_choice: BTreeMap::from([('A', 1)]),
11796            borda: BTreeMap::new(),
11797            winner: 'A',
11798            rankings: 1,
11799            unanimous_initial: true,
11800            deliberated: false,
11801            changed_votes: 0,
11802            unanimous_final: true,
11803            tie_break: None,
11804            judges: 0,
11805            present: 0,
11806            quorum: 0,
11807            met_quorum: true,
11808            uncontested: Some("only candidate A produced a change".to_owned()),
11809        });
11810        state.reviews = vec![ReviewRound {
11811            round: 1,
11812            head: "deadbeef".to_owned(),
11813            verified_head: None,
11814            verified_at: None,
11815            reviews: Vec::new(),
11816            e2e: Vec::new(),
11817            fix: None,
11818            blocking: 0,
11819            answered: 0,
11820            expected: 0,
11821            clean: true,
11822            verify_retried: false,
11823            e2e_deferred: false,
11824            e2e_defer_reason: None,
11825            progressed: false,
11826            vote_split: false,
11827            reconsideration: Vec::new(),
11828            verdict: None,
11829        }];
11830        state.gate = vec![CommandOutcome {
11831            command: "test".to_owned(),
11832            code: Some(0),
11833            output_tail: String::new(),
11834            duration_ms: 0,
11835            resource_blocked: false,
11836        }];
11837        state.gate_ran = true;
11838        // A first pass through `merge` already pushed and opened this pull
11839        // request; `status` is `Landing` because a previous call into `land`
11840        // parked or was interrupted before it reached a terminal outcome.
11841        state.status = RunStatus::Landing;
11842        state.merge = Some(MergeOutcome {
11843            mode: MergeMode::Pr,
11844            ok: true,
11845            detail: "https://example.invalid/x/y/pull/1".to_owned(),
11846            empty: false,
11847        });
11848
11849        // The Landing-resume shortcut calls `run_land` directly rather than
11850        // through `merge`, which is exactly the call site that used to skip
11851        // `settle_questions` - see the fixture below.
11852        ask_test_home();
11853        // A real resume loads a persisted run; driver ownership is checked
11854        // against that record before the Landing shortcut is entered.
11855        state.save().expect("save landing run");
11856        let store = ask::Questions::open();
11857        let q = ask_open_question(&store, &state.id);
11858
11859        let mut runner = Runner {
11860            state,
11861            roles: ResolvedRoles {
11862                implementers: Vec::new(),
11863                judges: Vec::new(),
11864                reviewers: Vec::new(),
11865                fixer: None,
11866                conductor: conductor(),
11867                implementer_roster: Vec::new(),
11868                judge_roster: Vec::new(),
11869                reviewer_roster: Vec::new(),
11870            },
11871            sem: Arc::new(Semaphore::new(1)),
11872            pause: Pause::new(),
11873            interrupt: Pause::new(),
11874        };
11875
11876        // `execute`, not `merge` directly: the Landing-resume shortcut lives
11877        // at the top of `execute`, not inside `merge` (see `execute`'s doc)
11878        // exactly because `review_loop` would otherwise clobber the marker
11879        // first.
11880        runner.execute().await.expect("execute");
11881
11882        assert_eq!(
11883            runner.state.merge.as_ref().map(|m| m.detail.as_str()),
11884            Some("https://example.invalid/x/y/pull/1"),
11885            "reentry must not push again or open a second pull request over the \
11886             one `land` is already watching"
11887        );
11888        assert_ne!(
11889            runner.state.status,
11890            RunStatus::Landing,
11891            "land could not actually reach the fake pull request, so it must \
11892             have given up rather than left the run silently parked forever"
11893        );
11894        // `land` could not reach the fake pull request, so it gave up into
11895        // `Blocked` - still resumable, so the question must not have been
11896        // swept just because this branch now also calls `settle_questions`.
11897        assert_eq!(runner.state.status, RunStatus::Blocked);
11898        assert!(
11899            store.get(&q.id).unwrap().status.open(),
11900            "Blocked is still alive; settle_questions must have been a no-op here"
11901        );
11902    }
11903
11904    fn state_with_round(round: ReviewRound) -> RunState {
11905        let mut s = RunState::new(
11906            PathBuf::from("/repo"),
11907            "main".to_owned(),
11908            "abc1234".to_owned(),
11909            "add retries".to_owned(),
11910            Config::default(),
11911        );
11912        s.reviews = vec![round];
11913        s
11914    }
11915
11916    fn finding(id: &str, severity: Severity, title: &str) -> crate::verdict::Finding {
11917        crate::verdict::Finding {
11918            id: id.to_owned(),
11919            severity,
11920            file: None,
11921            line: None,
11922            title: title.to_owned(),
11923            detail: String::new(),
11924        }
11925    }
11926
11927    #[test]
11928    fn pr_body_names_open_findings_and_declined_ones() {
11929        let round = ReviewRound {
11930            round: 2,
11931            head: "deadbee".to_owned(),
11932            verified_head: None,
11933            verified_at: None,
11934            reviews: vec![ReviewRecord {
11935                attempts: 0,
11936                reviewer: 1,
11937                agent: "alpha".to_owned(),
11938                summary: String::new(),
11939                findings: vec![finding("R2-1-1", Severity::Minor, "unused import")],
11940                vote: None,
11941                failed: None,
11942                duration_ms: 0,
11943            }],
11944            e2e: vec![CommandOutcome {
11945                command: "cargo test".to_owned(),
11946                code: Some(0),
11947                output_tail: String::new(),
11948                duration_ms: 0,
11949                resource_blocked: false,
11950            }],
11951            verify_retried: false,
11952            e2e_deferred: false,
11953            e2e_defer_reason: None,
11954            fix: Some(FixRecord {
11955                agent: "alpha".to_owned(),
11956                addressed: Vec::new(),
11957                rejected: vec![crate::verdict::Rejection {
11958                    id: "R1-1-1".to_owned(),
11959                    why: "not reachable from any caller".to_owned(),
11960                }],
11961                notes: String::new(),
11962                committed: true,
11963                failed: None,
11964                duration_ms: 0,
11965                continuation: None,
11966            }),
11967            blocking: 0,
11968            answered: 1,
11969            expected: 1,
11970            clean: false,
11971            progressed: true,
11972            vote_split: false,
11973            reconsideration: Vec::new(),
11974            verdict: None,
11975        };
11976        let state = state_with_round(round);
11977        let body = pr_message(&state, 'A').body;
11978
11979        assert!(body.contains("add retries"), "the task must still be there");
11980        assert!(body.contains("R2-1-1"), "{body}");
11981        assert!(body.contains("unused import"), "{body}");
11982        assert!(body.contains("R1-1-1"), "the declined finding: {body}");
11983        assert!(
11984            body.contains("not reachable from any caller"),
11985            "the reason it was declined: {body}"
11986        );
11987    }
11988
11989    #[test]
11990    fn pr_body_says_nothing_extra_when_the_round_was_clean() {
11991        let round = ReviewRound {
11992            round: 1,
11993            head: "deadbee".to_owned(),
11994            verified_head: None,
11995            verified_at: None,
11996            reviews: vec![ReviewRecord {
11997                attempts: 0,
11998                reviewer: 1,
11999                agent: "alpha".to_owned(),
12000                summary: String::new(),
12001                findings: Vec::new(),
12002                vote: None,
12003                failed: None,
12004                duration_ms: 0,
12005            }],
12006            e2e: Vec::new(),
12007            verify_retried: false,
12008            e2e_deferred: false,
12009            e2e_defer_reason: None,
12010            fix: None,
12011            blocking: 0,
12012            answered: 1,
12013            expected: 1,
12014            clean: true,
12015            progressed: false,
12016            vote_split: false,
12017            reconsideration: Vec::new(),
12018            verdict: None,
12019        };
12020        let state = state_with_round(round);
12021        let body = pr_message(&state, 'A').body;
12022        assert!(!body.contains("Open review findings"), "{body}");
12023        assert!(!body.contains("Declined"), "{body}");
12024    }
12025
12026    fn state_with_summary(instruction: &str, summary: &str) -> RunState {
12027        let mut state = RunState::new(
12028            PathBuf::from("/repo"),
12029            "main".to_owned(),
12030            "abc1234".to_owned(),
12031            instruction.to_owned(),
12032            Config::default(),
12033        );
12034        state.candidates.push(Candidate {
12035            index: 0,
12036            label: 'A',
12037            agent: "alpha".to_owned(),
12038            branch: "magi/x/A".to_owned(),
12039            worktree: PathBuf::from("/wt"),
12040            summary: summary.to_owned(),
12041            stat: String::new(),
12042            files: 1,
12043            commits: 1,
12044            empty: false,
12045            failed: None,
12046            verified_noop: None,
12047            folded: false,
12048            duration_ms: 0,
12049        });
12050        state
12051    }
12052
12053    fn review_state(subjects: &[&str]) -> RunState {
12054        let mut state = state_with_summary(
12055            "Review the work already on branch `magi/x/A`. There is no task statement: what the change claims to do is whatever its commits say.\n\nfirst\nsecond",
12056            "",
12057        );
12058        state.candidates[0].agent = EXISTING_BRANCH.to_owned();
12059        state.reviewed_commits = Some(subjects.iter().map(|s| (*s).to_owned()).collect());
12060        state
12061    }
12062
12063    /// A branch rebased onto a main that moved past the recorded
12064    /// `base_commit` is titled from its own first commit, never main's.
12065    #[tokio::test]
12066    async fn a_rebased_review_branch_is_titled_from_its_own_commits() {
12067        ask_test_home();
12068        let tmp = tempfile::tempdir().unwrap();
12069        let repo = tmp.path().join("repo");
12070        std::fs::create_dir_all(&repo).unwrap();
12071        init_repo(&repo);
12072        let origin = tmp.path().join("origin.git");
12073        let g = |dir: &Path, args: &[&str]| {
12074            let out = std::process::Command::new("git")
12075                .args(args)
12076                .current_dir(dir)
12077                .quiet()
12078                .output()
12079                .expect("spawn git");
12080            assert!(
12081                out.status.success(),
12082                "git {args:?}: {}",
12083                String::from_utf8_lossy(&out.stderr)
12084            );
12085        };
12086        g(
12087            tmp.path(),
12088            &[
12089                "clone",
12090                "--bare",
12091                "-q",
12092                repo.to_str().unwrap(),
12093                origin.to_str().unwrap(),
12094            ],
12095        );
12096        g(
12097            &repo,
12098            &["remote", "add", "origin", origin.to_str().unwrap()],
12099        );
12100        let c1 = git::rev_parse(&repo, "main").await.unwrap();
12101
12102        // Main moves on; the branch is built on top of the new main.
12103        std::fs::write(repo.join("dep.txt"), "bump\n").unwrap();
12104        g(&repo, &["add", "-A"]);
12105        g(
12106            &repo,
12107            &["commit", "-q", "-m", "chore(deps): update a crate"],
12108        );
12109        g(&repo, &["push", "-q", "origin", "main"]);
12110        g(&repo, &["checkout", "-q", "-b", "feat/own"]);
12111        std::fs::write(repo.join("own.txt"), "own\n").unwrap();
12112        g(&repo, &["add", "-A"]);
12113        g(
12114            &repo,
12115            &["commit", "-q", "-m", "fix(daemon): apply a chosen action"],
12116        );
12117        g(&repo, &["checkout", "-q", "main"]);
12118
12119        let start = review_base(&repo, "origin", "main", &c1, "feat/own").await;
12120        assert_eq!(start, git::rev_parse(&repo, "main").await.unwrap());
12121        // Without a readable tracking ref the recorded base's merge base is used.
12122        let fallback = review_base(&repo, "nowhere", "main", &c1, "feat/own").await;
12123        assert_eq!(fallback, c1);
12124
12125        let mut state = review_state(&[
12126            "chore(deps): update a crate",
12127            "fix(daemon): apply a chosen action",
12128        ]);
12129        state.repo = repo.clone();
12130        state.base_branch = "main".to_owned();
12131        state.base_commit = c1;
12132        refresh_reviewed_commits(&mut state, "feat/own").await;
12133        assert_eq!(
12134            state.reviewed_commits,
12135            Some(vec!["fix(daemon): apply a chosen action".to_owned()])
12136        );
12137        assert_eq!(
12138            review_title(&state).as_deref(),
12139            Some("fix(daemon): apply a chosen action")
12140        );
12141        assert_eq!(
12142            leaked_subjects(&state, "feat/own").await,
12143            Some(vec!["chore(deps): update a crate".to_owned()])
12144        );
12145        // A stale tracking ref is still used when the fetch fails, but the
12146        // leak list is withheld.
12147        state.config.merge.remote = "nowhere".to_owned();
12148        assert_eq!(leaked_subjects(&state, "feat/own").await, None);
12149    }
12150
12151    #[test]
12152    fn pr_message_review_single_commit_uses_its_subject() {
12153        let state = review_state(&["feat(nats): per-role user"]);
12154        let m = pr_message(&state, 'A');
12155        assert_eq!(m.title, "feat(nats): per-role user");
12156        assert!(!m.body.contains("Review the work already"), "{}", m.body);
12157        assert!(m.body.contains("## Commits under review"), "{}", m.body);
12158    }
12159
12160    #[test]
12161    fn pr_message_review_multi_commit_takes_the_oldest() {
12162        let state = review_state(&["feat: the change", "fix: typo", "fix: again"]);
12163        let m = pr_message(&state, 'A');
12164        assert_eq!(m.title, "feat: the change");
12165        for s in ["feat: the change", "fix: typo", "fix: again"] {
12166            assert!(m.body.contains(&format!("- {s}\n")), "{}", m.body);
12167        }
12168    }
12169
12170    #[test]
12171    fn pr_message_review_without_a_usable_first_subject_is_neutral() {
12172        for first in ["日本語の件名", "", "magi: candidate A (uncommitted work)"] {
12173            let mut state = review_state(&[first, "fix: later fixup"]);
12174            state.candidates[0].branch = "機能/ログイン".to_owned();
12175            let m = pr_message(&state, 'A');
12176            assert!(
12177                m.title.starts_with("chore: land candidate A of run"),
12178                "{}",
12179                m.title
12180            );
12181        }
12182    }
12183
12184    fn facts(commits: &[(&str, &str)], stat: &str) -> BranchFacts {
12185        BranchFacts {
12186            commits: commits
12187                .iter()
12188                .map(|(s, b)| ((*s).to_owned(), (*b).to_owned()))
12189                .collect(),
12190            stat: stat.to_owned(),
12191        }
12192    }
12193
12194    fn round_with_notes(round: usize, notes: Option<&str>) -> ReviewRound {
12195        let mut r = review_round(true, 0, 1, 1, true, true);
12196        r.round = round;
12197        r.fix = notes.map(|n| FixRecord {
12198            agent: "fixer".to_owned(),
12199            addressed: Vec::new(),
12200            rejected: Vec::new(),
12201            notes: n.to_owned(),
12202            committed: true,
12203            failed: None,
12204            duration_ms: 0,
12205            continuation: None,
12206        });
12207        r
12208    }
12209
12210    #[test]
12211    fn pr_message_review_with_branch_facts_uses_commits_and_stat() {
12212        let state = review_state(&["ignored"]);
12213        let f = facts(
12214            &[
12215                (
12216                    "fix(login): resolve PATH on macOS",
12217                    "Login shells skip rc files.",
12218                ),
12219                ("fix: address review", ""),
12220            ],
12221            " src/a.rs | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)",
12222        );
12223        let m = pr_message_with(&state, 'A', Some(&f));
12224        assert_eq!(m.title, "fix(login): resolve PATH on macOS");
12225        assert!(
12226            m.body
12227                .contains("- fix(login): resolve PATH on macOS\n  Login shells skip rc files.\n"),
12228            "{}",
12229            m.body
12230        );
12231        assert!(m.body.contains("- fix: address review\n"), "{}", m.body);
12232        assert!(m.body.contains("## Diff stat"), "{}", m.body);
12233        assert!(m.body.contains("src/a.rs | 2 +-"), "{}", m.body);
12234        for banned in [
12235            "Review the work already",
12236            "no task statement",
12237            "Original task",
12238        ] {
12239            assert!(!m.body.contains(banned), "{banned}: {}", m.body);
12240        }
12241        assert!(m.body.ends_with("magi:candidate-a\n"), "{}", m.body);
12242    }
12243
12244    #[test]
12245    fn pr_message_review_truncates_a_huge_first_commit_body() {
12246        let state = review_state(&["ignored"]);
12247        let f = facts(
12248            &[("feat: big", &"x".repeat(70_000)), ("fix: later", "")],
12249            "s",
12250        );
12251        let m = pr_message_with(&state, 'A', Some(&f));
12252        assert!(m.body.len() < 30_000, "{}", m.body.len());
12253        assert!(m.body.contains("(truncated)"), "{}", m.body.len());
12254        assert!(
12255            m.body.contains("- ... 1 more commit(s)"),
12256            "{}",
12257            m.body.len()
12258        );
12259        assert!(m.body.ends_with("magi:candidate-a\n"));
12260    }
12261
12262    #[test]
12263    fn pr_message_review_without_facts_falls_back_to_recorded_subjects() {
12264        let m = pr_message_with(&review_state(&["feat: x", "fix: y"]), 'A', None);
12265        assert_eq!(m.title, "feat: x");
12266        assert!(m.body.contains("- fix: y\n"), "{}", m.body);
12267        assert!(!m.body.contains("Diff stat"), "{}", m.body);
12268        assert!(!m.body.contains("no task statement"), "{}", m.body);
12269    }
12270
12271    #[test]
12272    fn pr_message_review_titles_from_the_branch_name_when_subjects_are_unusable() {
12273        let mut state = review_state(&["日本語の件名"]);
12274        state.candidates[0].branch = "fix/macos-login-path".to_owned();
12275        assert_eq!(pr_message(&state, 'A').title, "fix/macos-login-path");
12276        state.candidates[0].branch = "機能/ログイン".to_owned();
12277        assert!(
12278            pr_message(&state, 'A')
12279                .title
12280                .starts_with("chore: land candidate A")
12281        );
12282    }
12283
12284    #[test]
12285    fn pr_message_review_fixes_survive_a_clean_final_round() {
12286        let mut state = review_state(&["feat: x"]);
12287        state.reviews = vec![
12288            round_with_notes(1, Some("handled the PATH case")),
12289            round_with_notes(2, None),
12290        ];
12291        let body = pr_message(&state, 'A').body;
12292        assert!(
12293            body.contains("## Review fixes\n\nhandled the PATH case\n"),
12294            "{body}"
12295        );
12296        assert!(!body.contains("### Round"), "{body}");
12297
12298        state.reviews = vec![
12299            round_with_notes(1, Some("first fix")),
12300            round_with_notes(2, Some("")),
12301            round_with_notes(3, Some("second fix")),
12302            round_with_notes(4, None),
12303        ];
12304        let body = pr_message(&state, 'A').body;
12305        assert!(body.contains("### Round 1\n\nfirst fix"), "{body}");
12306        assert!(body.contains("### Round 3\n\nsecond fix"), "{body}");
12307        assert!(!body.contains("### Round 2"), "{body}");
12308    }
12309
12310    #[test]
12311    fn pr_message_implementation_run_keeps_its_shape_and_marker() {
12312        let state = state_with_summary(
12313            "add retries to the client",
12314            "TITLE: feat: retries\n\nDid it.",
12315        );
12316        let m = pr_message_with(&state, 'A', Some(&facts(&[("x", "")], "s")));
12317        assert_eq!(m.title, "feat: retries");
12318        assert!(
12319            m.body.contains("<summary>Original task</summary>"),
12320            "{}",
12321            m.body
12322        );
12323        assert!(!m.body.contains("Commits under review"), "{}", m.body);
12324        assert!(
12325            m.body
12326                .ends_with(&format!("magi:run/{} magi:candidate-a\n", state.id)),
12327            "{}",
12328            m.body
12329        );
12330    }
12331
12332    #[test]
12333    fn pr_message_review_bounds_a_long_english_subject() {
12334        let long = format!("feat: {}", "word ".repeat(100));
12335        let m = pr_message(&review_state(&[&long]), 'A');
12336        assert!(m.title.starts_with("feat: word"), "{}", m.title);
12337        assert!(m.title.chars().count() <= PR_TITLE_MAX, "{}", m.title);
12338    }
12339
12340    #[test]
12341    fn pr_message_implementation_run_is_unchanged_by_review_support() {
12342        let state = state_with_summary("add retries\n\ndetails", "- did some things");
12343        let m = pr_message(&state, 'A');
12344        assert_eq!(m.title, "add retries");
12345        assert!(m.body.contains("<summary>Original task</summary>"));
12346        assert!(!m.body.contains("Commits under review"));
12347        assert_eq!(landing_subject_source(&state), state.instruction);
12348    }
12349
12350    #[test]
12351    fn review_run_squash_subject_is_the_change_not_the_prompt() {
12352        let state = review_state(&["feat: the change", "fix: typo"]);
12353        let source = landing_subject_source(&state);
12354        assert_eq!(land::merge_subject("", &source), "feat: the change");
12355        assert_eq!(
12356            land::merge_subject("magi: candidate A (uncommitted work)", &source),
12357            "feat: the change"
12358        );
12359        // An operator's rename still wins.
12360        assert_eq!(
12361            land::merge_subject("feat: renamed by hand", &source),
12362            "feat: renamed by hand"
12363        );
12364        let blank = review_state(&["日本語"]);
12365        assert!(
12366            land::merge_subject("", &landing_subject_source(&blank)).starts_with("chore: land")
12367        );
12368    }
12369
12370    #[test]
12371    fn review_run_drops_a_prompt_shaped_pr_title_at_landing() {
12372        let state = review_state(&["feat: the change"]);
12373        let source = landing_subject_source(&state);
12374        let old = "Review the work already on branch `magi/x/A`. There is no task statement";
12375        assert_eq!(
12376            land::merge_subject(landing_title(&state, old), &source),
12377            "feat: the change"
12378        );
12379        assert_eq!(landing_title(&state, "feat: renamed"), "feat: renamed");
12380        let task = state_with_summary("add retries", "");
12381        assert_eq!(landing_title(&task, old), old);
12382    }
12383
12384    #[test]
12385    fn pr_message_describes_the_change_not_the_task() {
12386        let state = state_with_summary(
12387            "今回やってほしいこと: results projector を直す",
12388            "TITLE: fix(web): batch the runs list reads\n- reads run.json once\n- risk: none",
12389        );
12390        let m = pr_message(&state, 'A');
12391        assert_eq!(m.title, "fix(web): batch the runs list reads");
12392        assert!(
12393            m.body.starts_with("## Summary\n\n- reads run.json once"),
12394            "{}",
12395            m.body
12396        );
12397        assert!(!m.body.contains("TITLE:"), "{}", m.body);
12398        let task_at = m.body.find("今回やってほしいこと").unwrap();
12399        let details_at = m.body.find("<details>").unwrap();
12400        assert!(
12401            details_at < task_at,
12402            "the task lives inside <details>: {}",
12403            m.body
12404        );
12405        assert!(m.body.contains(&format!("magi:run/{}", state.id)));
12406        assert!(m.body.contains("magi:candidate-a"));
12407    }
12408
12409    #[test]
12410    fn pr_message_falls_back_to_the_task_without_a_title_line() {
12411        let state = state_with_summary("\n\nadd retries\n\ndetails", "- did some things");
12412        let m = pr_message(&state, 'A');
12413        assert_eq!(m.title, "add retries");
12414        assert!(
12415            m.body.contains("## Summary\n\n- did some things"),
12416            "{}",
12417            m.body
12418        );
12419
12420        let none = RunState::new(
12421            PathBuf::from("/repo"),
12422            "main".to_owned(),
12423            "abc1234".to_owned(),
12424            "add retries".to_owned(),
12425            Config::default(),
12426        );
12427        let m = pr_message(&none, 'A');
12428        assert_eq!(m.title, "add retries");
12429        assert!(!m.body.contains("## Summary"), "{}", m.body);
12430    }
12431
12432    #[test]
12433    fn pr_message_refuses_the_candidate_commit_subject() {
12434        for bad in [
12435            "TITLE: magi: candidate A (uncommitted work)",
12436            "TITLE: chore: stuff (uncommitted work)",
12437            "TITLE:   ",
12438        ] {
12439            let state = state_with_summary("add retries", bad);
12440            assert_eq!(pr_message(&state, 'A').title, "add retries", "{bad}");
12441        }
12442    }
12443
12444    #[test]
12445    fn pr_message_bounds_a_very_long_task_and_title() {
12446        let long = format!("fix the thing 🎉 {}", "x".repeat(5000));
12447        let state = state_with_summary(&long, "- nothing");
12448        let m = pr_message(&state, 'A');
12449        assert!(m.title.chars().count() <= PR_TITLE_MAX, "{}", m.title);
12450        assert!(!m.title.contains('\n'));
12451
12452        let state = state_with_summary("task", &format!("TITLE: feat: {}", "y".repeat(5000)));
12453        let m = pr_message(&state, 'A');
12454        assert!(m.title.starts_with("feat: "));
12455        assert!(m.title.chars().count() <= PR_TITLE_MAX, "{}", m.title);
12456        assert_eq!(m.commit_message().lines().next(), Some(m.title.as_str()));
12457    }
12458
12459    fn long_title_of(instruction: &str) -> String {
12460        pr_message(&state_with_summary(instruction, "- nothing"), 'A').title
12461    }
12462
12463    #[test]
12464    fn pr_message_cuts_a_long_english_line_at_its_first_sentence() {
12465        let first = "Make the landing path keep a readable title for long tasks";
12466        let line = format!(
12467            "{first}. {}",
12468            "And then keep going with more words ".repeat(20)
12469        );
12470        let t = long_title_of(&line);
12471        assert_eq!(t, first);
12472        assert!(!t.starts_with("chore: land"));
12473    }
12474
12475    #[test]
12476    fn pr_message_cuts_a_sentenceless_long_line_at_a_word() {
12477        let line = "word ".repeat(200);
12478        let t = long_title_of(&line);
12479        assert!(t.ends_with("word..."), "{t}");
12480        assert!(t.is_ascii() && t.chars().count() <= PR_TITLE_MAX, "{t}");
12481    }
12482
12483    #[test]
12484    fn pr_message_long_non_english_or_letterless_line_is_neutral() {
12485        for line in ["日本語のタスク ".repeat(80), "1234 ".repeat(100)] {
12486            assert!(long_title_of(&line).starts_with("chore: land"), "{line}");
12487        }
12488    }
12489
12490    #[test]
12491    fn pr_message_title_limit_is_exact() {
12492        let at = "a".repeat(PR_TITLE_MAX);
12493        assert_eq!(long_title_of(&at), at);
12494        let over = long_title_of(&"a".repeat(PR_TITLE_MAX + 1));
12495        assert!(over.ends_with("..."), "{over}");
12496        assert_eq!(over.chars().count(), PR_TITLE_MAX);
12497    }
12498
12499    #[test]
12500    fn pr_message_judges_the_kept_text_not_what_follows_the_cut() {
12501        let line = format!("{} \u{2014} tail", "alpha beta ".repeat(40));
12502        let t = long_title_of(&line);
12503        assert!(t.ends_with("..."), "{t}");
12504        assert!(t.is_ascii(), "{t}");
12505    }
12506
12507    #[test]
12508    fn pr_message_sentence_cut_skips_abbreviations_and_decimals() {
12509        let line = format!(
12510            "Support several shells, e.g. bash and zsh, at version 1.5 or newer when it matters {}",
12511            "plus more filler words ".repeat(20)
12512        );
12513        let t = long_title_of(&line);
12514        assert!(t.contains("e.g. bash") && t.contains("1.5 or newer"), "{t}");
12515    }
12516
12517    #[test]
12518    fn pr_message_long_title_survives_a_blank_first_line_and_the_squash_subject() {
12519        let line = format!("\n\n# {}", "title words ".repeat(40));
12520        let state = state_with_summary(&line, "- nothing");
12521        let m = pr_message(&state, 'A');
12522        assert!(m.title.starts_with("title words"), "{}", m.title);
12523        assert_eq!(
12524            land::merge_subject(&m.title, &landing_subject_source(&state)),
12525            m.title
12526        );
12527        // An operator's rename wins untouched.
12528        assert_eq!(
12529            land::merge_subject("feat: renamed by hand", &landing_subject_source(&state)),
12530            "feat: renamed by hand"
12531        );
12532    }
12533
12534    #[tokio::test]
12535    async fn github_text_rewrite_is_bounded_and_falls_back() {
12536        crate::run::pin_test_home();
12537        for (reply, accepted) in [
12538            (
12539                "TITLE: fix: retries\nAdd retries for failed requests.",
12540                true,
12541            ),
12542            (
12543                "TITLE: fix: retries\n日本語の説明をもう一度書きます。",
12544                false,
12545            ),
12546            ("TITLE: fix: retries\nUse token=secret", false),
12547        ] {
12548            let dir = tempfile::tempdir().unwrap();
12549            let mut runner = runner_at(RunStatus::Gating);
12550            runner.state = state_with_summary(
12551                "add retries",
12552                "TITLE: fix: retries\n日本語の説明を書きます。",
12553            );
12554            runner.state.repo = dir.path().to_owned();
12555            runner.state.candidates[0].worktree = dir.path().to_owned();
12556            let mut author = spec("alpha");
12557            author.command = vec![
12558                "sh".into(),
12559                "-c".into(),
12560                "printf '%s' \"$REWRITE_REPLY\"".into(),
12561            ];
12562            author.env.insert("REWRITE_REPLY".into(), reply.into());
12563            runner.state.config.agents = vec![author];
12564            let winner = runner.state.candidates[0].clone();
12565            let first = runner
12566                .guarded_pr_message(&winner, None, true)
12567                .await
12568                .unwrap();
12569            let message = if accepted {
12570                first.expect("a passing rewrite never asks")
12571            } else {
12572                // Withheld: the run parks on a question and posts nothing yet.
12573                assert!(first.is_none());
12574                assert!(runner.state.parked);
12575                let store = ask::Questions::open();
12576                let asked: Vec<_> = store
12577                    .list()
12578                    .into_iter()
12579                    .filter(|q| q.run == runner.state.id && q.node == crate::github_text::ASK_NODE)
12580                    .collect();
12581                assert_eq!(asked.len(), 1);
12582                assert!(!asked[0].detail.contains("secret"));
12583                // Silence: the question lapses and the neutral text is used,
12584                // without a second rewrite call or a second question.
12585                store
12586                    .update(&asked[0].id, |q| {
12587                        q.abandon("test");
12588                        Ok(())
12589                    })
12590                    .unwrap();
12591                let again = runner
12592                    .guarded_pr_message(&winner, None, true)
12593                    .await
12594                    .unwrap()
12595                    .expect("an abandoned question falls back");
12596                let n = store
12597                    .list()
12598                    .into_iter()
12599                    .filter(|q| q.run == runner.state.id && q.node == crate::github_text::ASK_NODE)
12600                    .count();
12601                assert_eq!(n, 1, "asked once per rejected text");
12602                again
12603            };
12604            assert!(crate::github_text::check(&message.title, &message.body).is_empty());
12605            assert_eq!(
12606                message.body.contains("Add retries for failed requests."),
12607                accepted
12608            );
12609            assert_eq!(
12610                runner.state.seats["impl-A"].turns, 1,
12611                "only one rewrite invocation"
12612            );
12613            assert!(runner.state.events.iter().any(|e| e.node == "github-text"));
12614            if !accepted {
12615                assert!(message.body.contains(crate::github_text::NEUTRAL_BODY));
12616                assert!(
12617                    message
12618                        .body
12619                        .contains(&format!("magi:run/{}", runner.state.id))
12620                );
12621            }
12622        }
12623    }
12624
12625    #[tokio::test]
12626    async fn withheld_title_takes_the_owner_replacement_and_rechecks_it() {
12627        crate::run::pin_test_home();
12628        let dir = tempfile::tempdir().unwrap();
12629        let mut runner = runner_at(RunStatus::Gating);
12630        runner.state = state_with_summary("add retries", "TITLE: 日本語のタイトル\nAdd retries.");
12631        runner.state.repo = dir.path().to_owned();
12632        runner.state.candidates[0].worktree = dir.path().to_owned();
12633        let winner = runner.state.candidates[0].clone();
12634        assert!(
12635            runner
12636                .guarded_pr_message(&winner, None, true)
12637                .await
12638                .unwrap()
12639                .is_none()
12640        );
12641        let store = ask::Questions::open();
12642        let find = |run: &str| {
12643            store
12644                .list()
12645                .into_iter()
12646                .find(|q| q.run == run && q.node == crate::github_text::ASK_NODE && q.status.open())
12647                .unwrap()
12648        };
12649        let run = runner.state.id.clone();
12650        let q = find(&run);
12651        let fp = runner
12652            .state
12653            .github_text
12654            .as_ref()
12655            .unwrap()
12656            .fingerprint
12657            .clone();
12658        let name = crate::github_text::artifact_name(&fp);
12659        let path = crate::run::artifact_path(&runner.state, &name);
12660        assert!(q.detail.contains(&path.display().to_string()));
12661        assert!(q.detail.contains("title-language: title"));
12662        let saved = std::fs::read_to_string(&path).expect("artifact written");
12663        assert!(saved.contains("日本語のタイトル"));
12664        assert!(q.cwd.is_none());
12665        assert_eq!(q.choices, ["use fallback", "post original", "use my text"]);
12666        // A bad replacement is refused and asked about once more.
12667        let say = |id: &str, text: &str, choice: &str| {
12668            store
12669                .update(id, |q| {
12670                    q.thread.push(ask::Turn {
12671                        who: ask::Who::Operator,
12672                        body: text.to_owned(),
12673                        at: jiff::Timestamp::now(),
12674                        note: None,
12675                    });
12676                    q.answer(ask::Answer::Choice(choice.to_owned()))
12677                })
12678                .unwrap();
12679        };
12680        say(&q.id, "まだ日本語", "use my text");
12681        assert!(
12682            runner
12683                .guarded_pr_message(&winner, None, true)
12684                .await
12685                .unwrap()
12686                .is_none()
12687        );
12688        let second = find(&run);
12689        assert_ne!(second.id, q.id);
12690        assert!(second.detail.contains("did not pass"));
12691        // The retry still compares the original text; the rejected
12692        // replacement is named by category only.
12693        assert!(second.detail.contains("日本語のタイトル"));
12694        assert!(!second.detail.contains("まだ日本語"));
12695        assert!(second.detail.contains(&path.display().to_string()));
12696        say(&second.id, "fix: retry failed requests", "use my text");
12697        let message = runner
12698            .guarded_pr_message(&winner, None, true)
12699            .await
12700            .unwrap()
12701            .expect("a vetted replacement is posted");
12702        assert_eq!(message.title, "fix: retry failed requests");
12703        assert_eq!(
12704            runner
12705                .state
12706                .github_text
12707                .as_ref()
12708                .unwrap()
12709                .chosen_title
12710                .as_deref(),
12711            Some("fix: retry failed requests")
12712        );
12713        assert!(runner.state.github_text.as_ref().unwrap().resolved);
12714        // Resumed again: the saved decision is applied, nothing is re-asked.
12715        let again = runner
12716            .guarded_pr_message(&winner, None, true)
12717            .await
12718            .unwrap()
12719            .unwrap();
12720        assert_eq!(again.title, message.title);
12721        assert_eq!(
12722            store
12723                .list()
12724                .into_iter()
12725                .filter(|q| q.run == run && q.node == crate::github_text::ASK_NODE)
12726                .count(),
12727            2
12728        );
12729    }
12730
12731    #[test]
12732    fn pr_message_magi_text_is_english_and_the_task_is_verbatim() {
12733        // What magi itself writes stays English under any configured language,
12734        // so a future localisation of these headings fails here. (The agents'
12735        // own text is also checked by the posting gate.)
12736        let mut state = state_with_summary(
12737            "add retries",
12738            "TITLE: fix(web): batch reads\n- reads run.json once",
12739        );
12740        state.config.graph.language = "ja".to_owned();
12741        let m = pr_message(&state, 'A');
12742        assert!(crate::github_text::check(&m.title, &m.body).is_empty());
12743        assert!(m.title.is_ascii() && m.body.is_ascii(), "{}", m.body);
12744
12745        // The task is the operator's own text: it goes in untouched, and the
12746        // fallback title (no summary) may be in its language too.
12747        let task = "今回やってほしいこと: results projector を直す";
12748        let mut state = state_with_summary(task, "- no title line");
12749        state.config.graph.language = "ja".to_owned();
12750        let m = pr_message(&state, 'A');
12751        assert_eq!(
12752            m.title,
12753            format!("chore: land candidate A of run {}", state.id)
12754        );
12755        assert!(
12756            m.body.contains(&format!(
12757                "<summary>Original task</summary>\n\n{task}\n\n</details>"
12758            )),
12759            "{}",
12760            m.body
12761        );
12762    }
12763
12764    #[test]
12765    fn pr_message_scrubs_home_paths_and_addresses() {
12766        let state = state_with_summary(
12767            "fix it in /Users/someone/src/x",
12768            "TITLE: fix(x): y\n- edited /home/someone/repo/src/a.rs on 10.1.2.3",
12769        );
12770        let m = pr_message(&state, 'A');
12771        for leak in ["/Users/someone", "/home/someone", "10.1.2.3"] {
12772            assert!(!m.body.contains(leak), "{}", m.body);
12773        }
12774        assert!(m.body.contains("~/repo/src/a.rs"), "{}", m.body);
12775    }
12776
12777    #[test]
12778    fn pr_message_survives_a_task_that_closes_details() {
12779        let state = state_with_summary("a </details> b", "TITLE: fix: x");
12780        let m = pr_message(&state, 'A');
12781        assert_eq!(m.body.matches("</details>").count(), 1, "{}", m.body);
12782    }
12783
12784    #[test]
12785    fn manual_squash_subject_cannot_break_out_of_its_quotes() {
12786        let cmd = manual_merge_command(
12787            MergeStyle::Squash,
12788            Path::new("/repo"),
12789            "b",
12790            "fix: \"quoted\" $(x) `y`\n\nbody",
12791        );
12792        assert!(cmd.ends_with("commit -m \"fix: quoted (x) y\""), "{cmd}");
12793    }
12794
12795    #[test]
12796    fn manual_merge_command_matches_the_configured_style() {
12797        let repo = Path::new("/repo");
12798        let message = "Merge magi run 0832 (candidate A)\n\nadd retries";
12799
12800        let merge = manual_merge_command(MergeStyle::Merge, repo, "magi/0832/A", message);
12801        assert_eq!(merge, "git -C /repo merge --no-ff magi/0832/A");
12802
12803        let squash = manual_merge_command(MergeStyle::Squash, repo, "magi/0832/A", message);
12804        assert_eq!(
12805            squash,
12806            "git -C /repo merge --squash magi/0832/A && git -C /repo commit -m \
12807             \"Merge magi run 0832 (candidate A)\""
12808        );
12809
12810        let rebase = manual_merge_command(MergeStyle::Rebase, repo, "magi/0832/A", message);
12811        assert_eq!(rebase, "git -C /repo merge --ff-only magi/0832/A");
12812    }
12813
12814    #[test]
12815    fn a_nudge_gets_a_quarter_of_the_budget() {
12816        // The judge and implement budgets magi ships with.
12817        assert_eq!(retry_budget(secs(1200), true), secs(300));
12818        assert_eq!(retry_budget(secs(3600), true), secs(900));
12819    }
12820
12821    #[test]
12822    fn a_resent_prompt_keeps_the_whole_budget() {
12823        // The seat kept no context, so the retry is the original job again and
12824        // shortening it would only guarantee a second failure.
12825        assert_eq!(retry_budget(secs(1200), false), secs(1200));
12826        assert_eq!(retry_budget(secs(60), false), secs(60));
12827    }
12828
12829    #[test]
12830    fn the_floor_never_exceeds_the_original_budget() {
12831        // A short configured timeout must not be *raised* by the floor: the
12832        // operator asked for a bound, and a retry may not outlast the attempt
12833        // it is retrying.
12834        assert_eq!(retry_budget(secs(60), true), secs(60));
12835        assert_eq!(retry_budget(secs(480), true), secs(120));
12836        assert_eq!(retry_budget(secs(0), true), secs(0));
12837    }
12838
12839    fn evidence(exit_code: Option<i32>) -> agent::CommandEvidence {
12840        agent::CommandEvidence {
12841            id: "item1".to_owned(),
12842            description: "cargo test".to_owned(),
12843            exit_code,
12844            result_summary: String::new(),
12845            source: "codex".to_owned(),
12846        }
12847    }
12848
12849    #[test]
12850    fn a_reply_with_no_commands_at_all_is_not_unconfirmed() {
12851        // No evidence is not the same fact as unconfirmed evidence: a
12852        // backend with no adapter, or a reply that ran no commands at all,
12853        // must not be misread as carrying a dangling job.
12854        assert!(!has_unconfirmed_command(&[]));
12855    }
12856
12857    #[test]
12858    fn a_command_with_a_real_exit_code_is_confirmed_whatever_its_value() {
12859        // Deliberately not a check on the exit code's *value*: a fixer
12860        // legitimately runs something that fails mid-iteration before it
12861        // succeeds, and that must never by itself reopen a valid report.
12862        assert!(!has_unconfirmed_command(&[evidence(Some(0))]));
12863        assert!(!has_unconfirmed_command(&[evidence(Some(1))]));
12864        assert!(!has_unconfirmed_command(&[
12865            evidence(Some(0)),
12866            evidence(Some(101))
12867        ]));
12868    }
12869
12870    #[test]
12871    fn one_command_with_no_readable_exit_code_is_enough_to_flag_the_reply() {
12872        assert!(has_unconfirmed_command(&[
12873            evidence(Some(0)),
12874            evidence(None)
12875        ]));
12876    }
12877
12878    #[test]
12879    fn a_clean_usable_reply_with_the_marker_is_a_verified_claim() {
12880        let text = "NO CHANGE NEEDED: already fixed by b32cfc4, on main.";
12881        assert_eq!(
12882            verified_noop_claim(true, &[], text).as_deref(),
12883            Some("already fixed by b32cfc4, on main.")
12884        );
12885    }
12886
12887    #[test]
12888    fn an_unusable_reply_never_earns_the_benefit_of_the_doubt() {
12889        // A timeout or a bad exit code reads as the ordinary loss it is,
12890        // whatever the reply's own prose claims.
12891        let text = "NO CHANGE NEEDED: already fixed by b32cfc4, on main.";
12892        assert!(verified_noop_claim(false, &[], text).is_none());
12893    }
12894
12895    #[test]
12896    fn an_unconfirmed_command_disqualifies_the_claim_even_on_a_usable_reply() {
12897        let text = "NO CHANGE NEEDED: already fixed by b32cfc4, on main.";
12898        assert!(verified_noop_claim(true, &[evidence(None)], text).is_none());
12899        // A confirmed command alongside the marker is fine.
12900        assert!(verified_noop_claim(true, &[evidence(Some(0))], text).is_some());
12901    }
12902
12903    #[test]
12904    fn an_ordinary_reply_with_no_marker_is_never_a_claim() {
12905        assert!(verified_noop_claim(true, &[], "- did the thing\n- tested it").is_none());
12906    }
12907
12908    /// Sets `runner.state.candidates` to one candidate per `(empty, verified)`
12909    /// pair, in order, labelled A, B, C, ...
12910    fn set_candidates(runner: &mut Runner, shape: &[(bool, Option<&str>)]) {
12911        runner.state.candidates = shape
12912            .iter()
12913            .enumerate()
12914            .map(|(i, &(empty, verified))| Candidate {
12915                index: i,
12916                label: (b'A' + i as u8) as char,
12917                agent: "sonnet".to_owned(),
12918                branch: format!("magi/x/{}", (b'A' + i as u8) as char),
12919                worktree: PathBuf::from(format!("/wt/{i}")),
12920                summary: String::new(),
12921                stat: String::new(),
12922                files: 0,
12923                commits: 0,
12924                empty,
12925                failed: None,
12926                verified_noop: verified.map(str::to_owned),
12927                duration_ms: 0,
12928                folded: false,
12929            })
12930            .collect();
12931    }
12932
12933    #[test]
12934    fn after_implement_reads_all_candidates_verified_as_a_noop_not_a_failure() {
12935        ask_test_home();
12936        let mut runner = runner_at(RunStatus::Implementing);
12937        set_candidates(
12938            &mut runner,
12939            &[
12940                (true, Some("already on main at b32cfc4")),
12941                (true, Some("same fix, see the existing test")),
12942            ],
12943        );
12944
12945        runner
12946            .after_implement()
12947            .expect("a verified no-op is not an error");
12948
12949        assert_eq!(runner.state.status, RunStatus::VerifiedNoop);
12950    }
12951
12952    #[test]
12953    fn after_implement_does_not_accept_one_candidates_claim_next_to_an_ordinary_loss() {
12954        ask_test_home();
12955        let mut runner = runner_at(RunStatus::Implementing);
12956        // Candidate A declares a verified no-op; candidate B simply wrote
12957        // nothing and said nothing about why. One candidate's claim is not
12958        // the whole run's agreement.
12959        set_candidates(
12960            &mut runner,
12961            &[(true, Some("already on main at b32cfc4")), (true, None)],
12962        );
12963
12964        let err = runner
12965            .after_implement()
12966            .expect_err("an unverified empty candidate must still fail the run");
12967
12968        assert!(
12969            err.to_string().contains("no candidate produced a change"),
12970            "{err}"
12971        );
12972        assert_eq!(runner.state.status, RunStatus::Failed);
12973    }
12974
12975    #[test]
12976    fn after_implement_still_fails_an_ordinary_all_empty_run() {
12977        ask_test_home();
12978        let mut runner = runner_at(RunStatus::Implementing);
12979        set_candidates(&mut runner, &[(true, None), (true, None)]);
12980
12981        let err = runner
12982            .after_implement()
12983            .expect_err("no candidate declared anything; this is an ordinary failure");
12984
12985        assert!(
12986            err.to_string().contains("no candidate produced a change"),
12987            "{err}"
12988        );
12989        assert_eq!(runner.state.status, RunStatus::Failed);
12990    }
12991}
12992
12993/// A graft rollback whose restore could not be confirmed (see
12994/// [`Runner::graft`]).
12995#[derive(Debug)]
12996struct GraftRestoreFailed(String);
12997
12998impl std::fmt::Display for GraftRestoreFailed {
12999    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13000        write!(
13001            f,
13002            "could not restore the winner to its pre-graft commit: {}",
13003            self.0
13004        )
13005    }
13006}
13007
13008impl std::error::Error for GraftRestoreFailed {}