Skip to main content

magi/
run.rs

1//! Run state: what happened, where it is stored, and how a run is resumed.
2//!
3//! Every node writes its result into [`RunState`] and the whole struct is
4//! flushed to `run.json` before the next node starts. That is what makes a run
5//! resumable: a competition can take an hour, and dying in review round four
6//! should not throw away three implementations, nine judge reads and a
7//! deliberation.
8//!
9//! Patches and raw agent transcripts are *not* in `run.json` — they live beside
10//! it under `artifacts/`, so the state file stays small enough to read by hand.
11use std::collections::BTreeMap;
12use std::path::{Path, PathBuf};
13
14use anyhow::{Context as _, Result, bail};
15use jiff::{Timestamp, Zoned};
16use serde::{Deserialize, Serialize};
17
18use crate::agent::SeatState;
19use crate::blind::Leak;
20use crate::config::{Config, MergeMode};
21use crate::verdict::{Finding, Rejection, ReviewVote};
22
23/// On-disk format version. Bumped when a field changes meaning, so a resumed
24/// run never half-reads a state file written by a different magi.
25///
26/// 2: added `RunStatus::Stalled`, `RunState::quota` (rate-limit losses), and
27/// the quorum fields on `Tally`. `RunState::load` already fails loudly and
28/// clearly on a schema mismatch; an old `run.json` from schema 1 now says so
29/// instead of silently half-reading.
30///
31/// 3: added `RunState::judge_skipped`. A solo candidate makes `judge` write
32/// only an event, leaving `judgements` empty forever — indistinguishable from
33/// "not yet judged" on every later reentry, which is what let `judge` re-run
34/// on a finished run and clobber its status back to `Judging`. The flag is
35/// the missing record of the fact that judging was skipped on purpose.
36///
37/// Also 3: a single-viable-candidate tally records `Tally::judges` as `0` and
38/// fills `Tally::uncontested`, instead of leaving the full roster size sitting
39/// next to a panel that never sat. A schema-2 record keeps reading as "0 of 3
40/// judges present" forever, because a tally is computed once and never
41/// recomputed on resume; the bump keeps that stale reading from being mixed
42/// with the new meaning.
43///
44/// 4: added `ReviewRound::progressed`. `graph::STAGNANT_LIMIT` counts
45/// consecutive rounds with `progressed == false` to decide whether the
46/// review loop should give up early, and a schema-3 record's default
47/// `false` would misreport a round that, at the time, actually committed a
48/// real diff — the field simply did not exist yet to say so. Without the
49/// bump, resuming an old multi-round review could spuriously trip the
50/// stagnation check on rounds that were never stagnant.
51///
52/// 5: added `RunStatus::Landing`. A run inside [`crate::land`]'s post-merge
53/// loop used to carry whatever status `merge` set before calling it forward
54/// unchanged - `Merged`, even while still watching CI or waiting on the
55/// owner's approval - which is also the one status [`RunStatus::resumable`]
56/// treats as finished. A daemon that gave this run's slot back to poll
57/// something else while an approval was outstanding, or one that simply
58/// crashed mid-land, had no way to tell "still landing" from "actually
59/// merged" and would either restart the whole competition or leave the run
60/// stuck reading as done. A schema-4 record has no notion of `Landing` at
61/// all, so this is a meaning a resumed old run cannot be guessed into rather
62/// than a value it can default to - hence the bump, not a `#[serde(default)]`.
63///
64/// 6: a deferred e2e is represented by an empty outcome list plus
65/// `ReviewRound::e2e_deferred`. Schema 5 treated that same empty list as an
66/// unconfigured, successful check, so schema-5 records are migrated with the
67/// old (not-deferred) meaning while older binaries reject schema-6 records.
68///
69/// 7: added `RunState::gate_ran`. An empty `RunState::gate` used to carry two
70/// meanings at once — "never attempted, or the last attempt was
71/// resource-blocked" (`graph::Runner::gate`'s retry case) and "attempted,
72/// zero commands configured, vacuously passed" (a repo with no
73/// `verify.gate`) — and nothing told them apart. `graph::Runner::merge`
74/// therefore read the second case as the first and refused forever: a
75/// review-only run with no gate commands configured reached `Gating` and
76/// then could never leave it. A schema-6 record's non-empty `gate` is
77/// migrated to `gate_ran = true` (a recorded attempt, real or historical,
78/// should not be spent again); an empty one migrates to `gate_ran = false`
79/// and is simply re-attempted by the next `gate()` call, which self-heals
80/// instantly for the zero-commands case.
81pub const SCHEMA: u32 = 7;
82
83/// Where a run got to.
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
85#[serde(rename_all = "snake_case")]
86pub enum RunStatus {
87    /// Worktrees being prepared.
88    Prep,
89    /// Candidates being implemented.
90    Implementing,
91    /// Judges ranking blind.
92    Judging,
93    /// Judges deliberating after a split.
94    Deliberating,
95    /// Final votes being collected privately.
96    Voting,
97    /// Winner in the review + verification loop.
98    Reviewing,
99    /// Gate commands running. Transient: `graph::Runner::gate` and
100    /// `graph::Runner::merge` always move a run on from here, whether or not
101    /// any gate commands are configured — see [`RunState::gate_status`] and
102    /// `SCHEMA`'s doc for schema 7, which fixed a repo with an empty
103    /// `verify.gate` stranding a review-only run in `Gating` forever.
104    Gating,
105    /// Inside [`crate::land`]'s post-merge loop: watching CI, running a fix
106    /// round, rebasing onto a moved base, or waiting on the owner's merge
107    /// approval. A run parked here while an approval is outstanding has
108    /// handed its daemon slot back — see [`crate::daemon`] — and resumes
109    /// through exactly this status, not a fresh competition.
110    Landing,
111    /// Winner merged.
112    Merged,
113    /// Winner passed the gate; merge was not requested.
114    Ready,
115    /// The judgement did not gather enough judges (e.g. rate limiting took out
116    /// seats), so the verdict is not trustworthy. The run stopped and kept its
117    /// work so it can be resumed or folded — it must never be confused with a
118    /// healthy `Ready`.
119    Stalled,
120    /// Review rounds exhausted with findings still open, or the gate failed.
121    Blocked,
122    /// The graph could not complete.
123    Failed,
124}
125
126impl RunStatus {
127    /// Is this a terminal state?
128    pub fn done(self) -> bool {
129        matches!(
130            self,
131            Self::Merged | Self::Ready | Self::Stalled | Self::Blocked | Self::Failed
132        )
133    }
134
135    /// The name this status is written and shown under, matching the
136    /// `snake_case` serde spelling so a log line, an error message and the
137    /// JSON a phone reads all say the same word.
138    pub fn as_str(self) -> &'static str {
139        match self {
140            Self::Prep => "prep",
141            Self::Implementing => "implementing",
142            Self::Judging => "judging",
143            Self::Deliberating => "deliberating",
144            Self::Voting => "voting",
145            Self::Reviewing => "reviewing",
146            Self::Gating => "gating",
147            Self::Landing => "landing",
148            Self::Merged => "merged",
149            Self::Ready => "ready",
150            Self::Stalled => "stalled",
151            Self::Blocked => "blocked",
152            Self::Failed => "failed",
153        }
154    }
155
156    /// Can this run be carried on from where it stopped?
157    ///
158    /// Everything except a finished run and a failed one. `execute` skips
159    /// nodes already recorded, so re-entering is cheap wherever the run
160    /// stopped, and the alternative is always a fresh competition against
161    /// work that already exists.
162    ///
163    /// - `Stalled` re-asks only the seats whose absence collapsed the panel,
164    ///   keeping the candidates that were already paid for.
165    /// - `Blocked` re-enters the review loop against a branch that is built.
166    /// - **A non-terminal status** means the run was interrupted: a parked
167    ///   run waiting for its upgrade, or one whose daemon was killed. This
168    ///   used to be excluded, which left run 4043 stuck at `reviewing` with
169    ///   the deck telling the operator it could not be resumed - the one
170    ///   state where resuming is the only sensible answer.
171    ///
172    /// `Failed` does not qualify: the graph could not complete and there is
173    /// no established point to continue from. Nor does a finished run, whose
174    /// answer is a new competition.
175    ///
176    /// Whether anything is *already* driving the run is a separate question,
177    /// answered by `daemon::is_working_on` at the callers that need it.
178    pub fn resumable(self) -> bool {
179        !matches!(self, Self::Merged | Self::Ready | Self::Failed)
180    }
181}
182
183/// One candidate implementation.
184#[derive(Debug, Clone, Serialize, Deserialize)]
185pub struct Candidate {
186    /// Position in the implementer list.
187    pub index: usize,
188    /// Blind label as presented to judges.
189    pub label: char,
190    /// Which agent wrote it. Recorded for the stats tables, never shown to a
191    /// judge.
192    pub agent: String,
193    /// Branch, named after the label so judges can inspect it without learning
194    /// the author.
195    pub branch: String,
196    /// Worktree path.
197    pub worktree: PathBuf,
198    /// Sanitized author summary.
199    #[serde(default)]
200    pub summary: String,
201    /// `git diff --stat`.
202    #[serde(default)]
203    pub stat: String,
204    /// Files touched.
205    #[serde(default)]
206    pub files: usize,
207    /// Commits ahead of base.
208    #[serde(default)]
209    pub commits: usize,
210    /// True when the agent produced no change at all.
211    #[serde(default)]
212    pub empty: bool,
213    /// Why this candidate is not in the running.
214    #[serde(default)]
215    pub failed: Option<String>,
216    /// Wall-clock time for the implementation.
217    #[serde(default)]
218    pub duration_ms: u64,
219    /// Whether the worktree has been folded away.
220    #[serde(default)]
221    pub folded: bool,
222}
223
224impl Candidate {
225    /// Can this candidate be judged?
226    pub fn viable(&self) -> bool {
227        self.failed.is_none() && !self.empty
228    }
229}
230
231/// One judge's independent ranking.
232#[derive(Debug, Clone, Serialize, Deserialize)]
233pub struct Judgement {
234    /// Judge seat number, 1-based.
235    pub judge: usize,
236    /// Seat key.
237    pub seat: String,
238    /// Agent occupying the seat.
239    pub agent: String,
240    /// Best-first labels.
241    #[serde(default)]
242    pub ranking: Vec<char>,
243    /// Per-label justification.
244    #[serde(default)]
245    pub reasons: BTreeMap<String, String>,
246    /// Self-reported confidence.
247    #[serde(default)]
248    pub confidence: Option<u8>,
249    /// Order the candidates were presented in, as candidate indices.
250    #[serde(default)]
251    pub order: Vec<usize>,
252    /// Why this judge has no ranking.
253    #[serde(default)]
254    pub failed: Option<String>,
255    /// Wall-clock time.
256    #[serde(default)]
257    pub duration_ms: u64,
258}
259
260/// One judge's turn in a deliberation round.
261#[derive(Debug, Clone, Serialize, Deserialize)]
262pub struct DeliberationTurn {
263    /// Judge seat number, 1-based.
264    pub judge: usize,
265    /// Agent occupying the seat.
266    pub agent: String,
267    /// The argument, as written.
268    pub body: String,
269    /// Where the judge stood at the end of the turn.
270    #[serde(default)]
271    pub tentative: Option<char>,
272}
273
274/// A deliberation round.
275#[derive(Debug, Clone, Serialize, Deserialize)]
276pub struct DeliberationRound {
277    /// 1-based round number.
278    pub round: usize,
279    /// Turns, in the order they were taken.
280    pub turns: Vec<DeliberationTurn>,
281}
282
283/// A final vote, collected privately.
284#[derive(Debug, Clone, Serialize, Deserialize)]
285pub struct VoteRecord {
286    /// Judge seat number, 1-based.
287    pub judge: usize,
288    /// Agent occupying the seat.
289    pub agent: String,
290    /// The vote.
291    #[serde(default)]
292    pub vote: Option<char>,
293    /// Why.
294    #[serde(default)]
295    pub reason: String,
296    /// Did this judge move from its initial first choice?
297    #[serde(default)]
298    pub changed: bool,
299}
300
301/// A seat that was taken out by a CLI rate limit / quota, recorded so a run
302/// whose panel collapsed does not masquerade as a healthy one.
303#[derive(Debug, Clone, Serialize, Deserialize)]
304pub struct QuotaLoss {
305    /// Seat key, e.g. `judge-1` or `review-2`.
306    pub seat: String,
307    /// Node that was running, e.g. `judge`, `vote`, `review`.
308    pub node: String,
309    /// When the CLI reported the limit.
310    pub at: Timestamp,
311    /// Reset hint if the CLI printed one, free text.
312    #[serde(default)]
313    pub reset: Option<String>,
314}
315
316/// The mechanical count.
317#[derive(Debug, Clone, Serialize, Deserialize)]
318pub struct Tally {
319    /// First-choice votes per label.
320    pub first_choice: BTreeMap<char, usize>,
321    /// Borda points from the initial rankings, used only to break a tie.
322    pub borda: BTreeMap<char, usize>,
323    /// The winning label.
324    pub winner: char,
325    /// How many judges produced a usable ranking. A panel of one is not a
326    /// consensus and must not be reported as a split.
327    #[serde(default)]
328    pub rankings: usize,
329    /// Did every judge's *initial* first choice agree?
330    pub unanimous_initial: bool,
331    /// Was deliberation run?
332    pub deliberated: bool,
333    /// Judges who moved between their initial ranking and their final vote.
334    pub changed_votes: usize,
335    /// Did the final votes agree?
336    pub unanimous_final: bool,
337    /// How the tie was broken, when it had to be.
338    #[serde(default)]
339    pub tie_break: Option<String>,
340    /// Configured judge count — the size of the full panel. `0` when no
341    /// panel was asked (see `uncontested`), not the roster size a panel that
342    /// never sat would have had.
343    #[serde(default)]
344    pub judges: usize,
345    /// Judges who actually contributed to the decision (not taken out by a
346    /// rate limit and producing a usable rank or vote).
347    #[serde(default)]
348    pub present: usize,
349    /// How many judges are required for a trustworthy verdict. Chosen as a
350    /// strict majority (`judges / 2 + 1`): a verdict backed by a minority must
351    /// never be presented as a healthy one, while a bare majority is still
352    /// real signal. A one-candidate run needs no quorum.
353    #[serde(default)]
354    pub quorum: usize,
355    /// `present >= quorum`, or no quorum was required.
356    #[serde(default)]
357    pub met_quorum: bool,
358    /// Why no panel was asked, when none was: a single viable candidate, or
359    /// a review-only run that never competed. `None` when judges actually
360    /// ranked and voted — including when too few of them survived to reach
361    /// quorum, which is a collapse and must keep reading as one.
362    #[serde(default)]
363    pub uncontested: Option<String>,
364}
365
366/// One reviewer's report in a round.
367#[derive(Debug, Clone, Serialize, Deserialize)]
368pub struct ReviewRecord {
369    /// Reviewer seat number, 1-based.
370    pub reviewer: usize,
371    /// Agent occupying the seat.
372    pub agent: String,
373    /// Reviewer prose.
374    #[serde(default)]
375    pub summary: String,
376    /// Findings, with magi-assigned ids.
377    #[serde(default)]
378    pub findings: Vec<Finding>,
379    /// This seat's initial vote. `None` on a record predating votes, exactly
380    /// like a round that genuinely had none cast — never a stand-in for a
381    /// vote that was lost.
382    #[serde(default)]
383    pub vote: Option<ReviewVote>,
384    /// Why this reviewer produced nothing.
385    #[serde(default)]
386    pub failed: Option<String>,
387    /// Wall-clock time.
388    #[serde(default)]
389    pub duration_ms: u64,
390}
391
392/// One seat's revote during a round's reconsideration (see
393/// [`ReviewRound::reconsideration`]).
394#[derive(Debug, Clone, Serialize, Deserialize)]
395pub struct ReviewRevoteRecord {
396    /// Reviewer seat number, 1-based.
397    pub reviewer: usize,
398    /// Agent occupying the seat.
399    pub agent: String,
400    /// The revote. `None` when the seat did not answer.
401    #[serde(default)]
402    pub vote: Option<ReviewVote>,
403    /// Why.
404    #[serde(default)]
405    pub reason: String,
406    /// Why this seat produced no revote.
407    #[serde(default)]
408    pub failed: Option<String>,
409}
410
411/// The fixer's response to a round.
412#[derive(Debug, Clone, Serialize, Deserialize)]
413pub struct FixRecord {
414    /// Agent that applied the fixes.
415    pub agent: String,
416    /// Finding ids acted on.
417    #[serde(default)]
418    pub addressed: Vec<String>,
419    /// Findings declined, with reasons.
420    #[serde(default)]
421    pub rejected: Vec<Rejection>,
422    /// What changed.
423    #[serde(default)]
424    pub notes: String,
425    /// Did the fix produce a commit?
426    #[serde(default)]
427    pub committed: bool,
428    /// Why the fix step produced nothing.
429    #[serde(default)]
430    pub failed: Option<String>,
431    /// Wall-clock time.
432    #[serde(default)]
433    pub duration_ms: u64,
434    /// How the fixer's own seat was made to answer when its CLI turn ended
435    /// cleanly but without an addressed/rejected report — see
436    /// [`graph::Runner::continue_fix_report`]. `None` for a record written
437    /// before this existed, which must read as "unknown", not as
438    /// [`ContinuationOutcome::NotNeeded`]: an old run really may have hit
439    /// this exact gap and simply had no mechanism to say so.
440    #[serde(default)]
441    pub continuation: Option<ContinuationRecord>,
442}
443
444/// How a node recovered — or failed to recover — a structured report after
445/// the CLI's own turn ended cleanly (a usable, non-empty, exit-0 reply)
446/// without it. A clean CLI turn is not the same fact as the node's own work
447/// being done — see the `fix` node's `continue_fix_report`, which is what
448/// produces this.
449#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
450pub enum ContinuationOutcome {
451    /// The first reply already carried the report; nothing was resumed.
452    NotNeeded,
453    /// A follow-up call in the same session recovered the report.
454    Resumed,
455    /// The continuation budget was spent without ever recovering it.
456    Exhausted,
457    /// A continuation attempt hit the CLI's rate limit; not retried further
458    /// — a quota fails the same way again immediately.
459    QuotaLost,
460    /// No session was left to resume into, so nothing was attempted.
461    NoSession,
462}
463
464/// Cost and outcome of one node's attempt to recover a missing report by
465/// resuming its own seat.
466#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
467pub struct ContinuationRecord {
468    /// Follow-up calls made to the same seat. `0` when the outcome is
469    /// [`ContinuationOutcome::NotNeeded`] or [`ContinuationOutcome::NoSession`].
470    pub attempts: usize,
471    /// Wall-clock time spent on those follow-up calls, summed — not counting
472    /// the original call whose reply this is recovering from.
473    pub cumulative_wait_ms: u64,
474    /// What ended the loop.
475    pub outcome: ContinuationOutcome,
476}
477
478impl ContinuationRecord {
479    /// The report was already there on the first try.
480    pub fn not_needed() -> Self {
481        Self {
482            attempts: 0,
483            cumulative_wait_ms: 0,
484            outcome: ContinuationOutcome::NotNeeded,
485        }
486    }
487}
488
489/// A command a seat's own CLI reported running, kept for `magi show` and for
490/// telling "this seat's turn ended" apart from "the process it started is
491/// done" — see `agent::CommandEvidence`, which is the only source this is
492/// ever built from. Never something magi polled or supervised; a command the
493/// CLI never reported finishing (or a CLI this crate has no adapter for at
494/// all) simply has no entry here, which must read as "unknown", not as
495/// "nothing ran".
496#[derive(Debug, Clone, Serialize, Deserialize)]
497pub struct JobRecord {
498    /// Graph node the seat belongs to, e.g. `"implement"`, `"fix"`.
499    pub node: String,
500    /// Seat key, e.g. `"impl-A"`.
501    pub seat: String,
502    /// The CLI's own id for this command.
503    pub id: String,
504    /// The command itself, as the CLI reported it.
505    pub description: String,
506    /// When this evidence was captured — the moment this seat's reply
507    /// carrying it was read, not the command's own start time, which no
508    /// adapter here currently has. A lower bound on staleness only.
509    pub checked_at: Timestamp,
510    /// What the CLI reported for it.
511    pub status: JobStatus,
512    /// Exit code the CLI reported.
513    pub exit_code: Option<i32>,
514    /// Tail of the command's own output, when reported.
515    #[serde(default)]
516    pub result_summary: String,
517    /// Which CLI/event stream this came from, e.g. `"codex"`.
518    pub source: String,
519}
520
521/// What a [`JobRecord`]'s own CLI reported for it. There is no `Running`
522/// variant: nothing here is ever polled live, so "still running" and
523/// "finished but never reported" are the same absence of evidence, not a
524/// state this type can name — see [`JobRecord`]'s own doc.
525#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
526pub enum JobStatus {
527    /// The command's own reported exit code was `0`.
528    Completed,
529    /// The command's own reported exit code was non-zero.
530    Failed,
531    /// The CLI reported this command but not a readable exit code.
532    Unknown,
533}
534
535/// Outcome of one shell command.
536#[derive(Debug, Clone, Serialize, Deserialize)]
537pub struct CommandOutcome {
538    /// The command, as configured.
539    pub command: String,
540    /// Exit code, `None` on timeout or signal.
541    pub code: Option<i32>,
542    /// Tail of the combined output, for the report and the fix prompt.
543    #[serde(default)]
544    pub output_tail: String,
545    /// Wall-clock time.
546    #[serde(default)]
547    pub duration_ms: u64,
548    /// Set only by magi itself, never inferred from `output_tail`: the
549    /// configured command was never actually run because a resource it
550    /// needs — right now, only the shared build cache's lease or the
551    /// freshness check that must precede using it — was not available
552    /// within budget. Distinct from an ordinary failure or timeout (both of
553    /// which *did* run something and are evidence about the patch); this is
554    /// evidence about the machine, and must never be read as a verdict on
555    /// the tree it named. `#[serde(default)]` so every record written
556    /// before this field existed keeps reading as `false` — exactly what it
557    /// was.
558    #[serde(default)]
559    pub resource_blocked: bool,
560}
561
562/// Substrings that mark a Cargo/rustc/link failure: the toolchain could not
563/// produce a binary to run at all, as opposed to producing one that ran and
564/// failed. A Windows link race against a shared `CARGO_TARGET_DIR` (see
565/// AGENTS.md, "Running magi on magi") looks exactly like a red command
566/// otherwise, and a run has concluded `Blocked` on nothing but that race.
567const BUILD_FAILURE_MARKERS: &[&str] = &[
568    "error: could not compile",
569    "error: linking with",
570    "LINK : fatal error",
571    "fatal error LNK",
572];
573
574impl CommandOutcome {
575    /// Did it pass?
576    pub fn ok(&self) -> bool {
577        self.code == Some(0)
578    }
579
580    /// Did this command fail because the code could not be built or linked,
581    /// rather than because it ran and produced a wrong result? A failure here
582    /// is not a verdict on the patch under review.
583    pub fn build_failed(&self) -> bool {
584        !self.ok()
585            && BUILD_FAILURE_MARKERS
586                .iter()
587                .any(|m| self.output_tail.contains(m))
588    }
589}
590
591/// One review + verify + fix round.
592#[derive(Debug, Clone, Serialize, Deserialize)]
593pub struct ReviewRound {
594    /// 1-based round number.
595    pub round: usize,
596    /// Commit the round reviewed.
597    pub head: String,
598    /// Commit actually checked by a catch-up e2e, when it differs from the
599    /// reviewed commit. Kept separate so reports never attribute a command
600    /// result to a review target the command did not inspect.
601    #[serde(default)]
602    pub verified_head: Option<String>,
603    /// Reviewer reports.
604    pub reviews: Vec<ReviewRecord>,
605    /// E2E command outcomes for this round.
606    #[serde(default)]
607    pub e2e: Vec<CommandOutcome>,
608    /// True when the first verify attempt this round could not build or
609    /// link, and `e2e` above holds a second attempt run before concluding.
610    /// A run must never be decided on a red it could not tell from an
611    /// unrelated build race.
612    #[serde(default)]
613    pub verify_retried: bool,
614    /// True when `e2e` was intentionally left empty this round: the round
615    /// already had blocking findings and another round was available, so
616    /// `graph::Runner::review_loop` sent the fixer straight at them instead
617    /// of spending a full verify run on a head it already knew would need
618    /// another fix. Distinct from an `e2e` that is simply empty because
619    /// `verify.e2e` has no commands configured — `e2e.is_empty()` alone
620    /// cannot tell those apart, and conflating them is exactly how a
621    /// deferred check would get painted green. A record written before this
622    /// field existed defaults to `false`, which is the truth for it: every
623    /// round used to run e2e unconditionally.
624    #[serde(default)]
625    pub e2e_deferred: bool,
626    /// Why `e2e` was deferred, set only when [`Self::e2e_deferred`] is true.
627    /// Carried to the fixer's prompt and shown in the report so "deferred"
628    /// never reads as silence.
629    #[serde(default)]
630    pub e2e_defer_reason: Option<String>,
631    /// Fixer response, absent when the round was already clean.
632    #[serde(default)]
633    pub fix: Option<FixRecord>,
634    /// Findings that hold the merge.
635    #[serde(default)]
636    pub blocking: usize,
637    /// Reviewer seats that answered (did not time out, crash, or return
638    /// something unparsable).
639    #[serde(default)]
640    pub answered: usize,
641    /// Reviewer seats the round expected an answer from — normally
642    /// `graph.reviewers`, but recorded per round so a config change between
643    /// runs never has to be inferred from history.
644    #[serde(default)]
645    pub expected: usize,
646    /// Round ended with no blocking findings and green verification, judged
647    /// against the seats that answered. See [`Self::incomplete`] for whether
648    /// that verdict is missing input.
649    #[serde(default)]
650    pub clean: bool,
651    /// Did the tree actually move against `base` this round, comparing the
652    /// diff after the fix to the diff the reviewers saw at the start of the
653    /// round?
654    ///
655    /// Never derived from the fixer's own `addressed`/`rejected` count: that
656    /// self-report has been caught lying twice on this workload (runs `b455`
657    /// and `6218`, both of which committed a real, substantial diff while
658    /// reporting `0 addressed`). `git` does not lie about whether the tree
659    /// changed, so this is what `graph::Runner::review_loop` counts rounds of
660    /// no progress against. Absent on a round with no fix attempt (already
661    /// clean, or the round the budget ran out on), where it defaults to
662    /// `false` and is not consulted.
663    #[serde(default)]
664    pub progressed: bool,
665    /// Did the seats' initial votes ([`ReviewRecord::vote`]) disagree?
666    #[serde(default)]
667    pub vote_split: bool,
668    /// One round of revoting, run only when `vote_split`: each seat that cast
669    /// an initial vote reads every seat's findings and votes, then revotes.
670    /// Empty when the initial votes already agreed, the same as a solo
671    /// candidate leaving `deliberation` empty.
672    #[serde(default)]
673    pub reconsideration: Vec<ReviewRevoteRecord>,
674    /// The round's verdict: the most cautious vote among the seats that
675    /// answered, using each seat's revote where reconsideration ran and its
676    /// initial vote otherwise. `None` when no seat produced a usable vote —
677    /// including every record written before votes existed, which is the
678    /// truth for those rounds, not a gap in this one.
679    #[serde(default)]
680    pub verdict: Option<ReviewVote>,
681}
682
683impl ReviewRound {
684    /// Did at least one reviewer seat fail to answer this round?
685    pub fn incomplete(&self) -> bool {
686        self.answered < self.expected
687    }
688
689    /// The honest state of this round's e2e leg.
690    ///
691    /// Never derive this from `e2e.is_empty()` alone anywhere else in the
692    /// codebase — `NotConfigured` and `Deferred` both leave it empty, and
693    /// only this method (backed by [`Self::e2e_deferred`]) tells them apart.
694    pub fn e2e_status(&self) -> E2eStatus {
695        if !self.e2e.is_empty() {
696            if self.e2e.iter().all(CommandOutcome::ok) {
697                E2eStatus::Passed
698            } else {
699                E2eStatus::Failed
700            }
701        } else if self.e2e_deferred {
702            E2eStatus::Deferred
703        } else {
704            E2eStatus::NotConfigured
705        }
706    }
707}
708
709/// The honest state of a round's e2e leg. See [`ReviewRound::e2e_status`].
710#[derive(Debug, Clone, Copy, PartialEq, Eq)]
711pub enum E2eStatus {
712    /// `verify.e2e` has no commands configured.
713    NotConfigured,
714    /// Skipped this round on purpose: blocking findings already required a
715    /// fix, so the round went straight to the fixer instead of spending a
716    /// full verify run on a head it already knew would need another pass.
717    Deferred,
718    /// Ran, and every command exited 0.
719    Passed,
720    /// Ran, and at least one command did not exit 0.
721    Failed,
722}
723
724/// The honest state of a run's final gate. See [`RunState::gate_status`].
725#[derive(Debug, Clone, Copy, PartialEq, Eq)]
726pub enum GateStatus {
727    /// Never attempted, or the last attempt was resource-blocked (the shared
728    /// build cache could not be acquired or confirmed fresh in time) and
729    /// needs a retry.
730    NotRun,
731    /// Ran with zero commands configured (`verify.gate` is empty) and
732    /// therefore vacuously passed — there was nothing to check.
733    PassedWithNoCommands,
734    /// Ran one or more commands, and every one of them exited 0.
735    Passed,
736    /// Ran one or more commands, and at least one did not exit 0.
737    Failed,
738}
739
740impl GateStatus {
741    /// May a run in this state proceed to merge?
742    pub fn ok(self) -> bool {
743        matches!(self, Self::PassedWithNoCommands | Self::Passed)
744    }
745}
746
747/// What happened to the winning branch.
748#[derive(Debug, Clone, Serialize, Deserialize)]
749pub struct MergeOutcome {
750    /// Requested mode.
751    pub mode: MergeMode,
752    /// Did it land?
753    pub ok: bool,
754    /// Command output, or the command the operator should run.
755    #[serde(default)]
756    pub detail: String,
757}
758
759/// A seat currently mid-answer: a prompt was sent and no reply has landed yet.
760///
761/// This is not the whole story of "is it alive" — a daemon killed mid-wave
762/// leaves its last wave's entries here forever, since nothing ran to clear
763/// them. A reader must cross-check a live daemon's heartbeat
764/// (`daemon::is_working_on`) before trusting one of these as "still running"
765/// rather than "abandoned". [`RunState::clear_active`] is what keeps that
766/// leftover from surviving into the next attempt at this run: `execute` calls
767/// it before doing anything else, so a resumed run never carries a stale
768/// entry into its own report before the next wave repopulates it.
769///
770/// Deliberately carries no agent id: an implementer's agent is no secret, but
771/// a judge or reviewer seat is blind (`SeatState::key` is keyed by seat, never
772/// agent, for exactly this reason), and this struct has no way to tell which
773/// kind of seat it describes. The seat key alone — already in the map this
774/// lives under — is what every caller needs to say which seat is running.
775#[derive(Debug, Clone, Serialize, Deserialize)]
776pub struct ActiveSeat {
777    /// Node the seat is answering for, e.g. `implement`, `judge`, `review`.
778    pub node: String,
779    /// When this attempt was sent.
780    pub started_at: Timestamp,
781    /// The CLI's wall-clock budget for this attempt.
782    pub timeout_secs: u64,
783    /// 0 for the first ask, N for the Nth nudge or resume.
784    #[serde(default)]
785    pub attempt: usize,
786}
787
788impl ActiveSeat {
789    /// Seconds since this attempt was sent.
790    #[must_use]
791    pub fn elapsed_secs(&self, now: Timestamp) -> i64 {
792        (now.as_second() - self.started_at.as_second()).max(0)
793    }
794
795    /// Seconds left before this attempt's own timeout fires, floored at zero
796    /// rather than going negative once the CLI has overrun its budget.
797    #[must_use]
798    pub fn remaining_secs(&self, now: Timestamp) -> i64 {
799        (self.timeout_secs as i64 - self.elapsed_secs(now)).max(0)
800    }
801}
802
803/// A timestamped note about a node.
804#[derive(Debug, Clone, Serialize, Deserialize)]
805pub struct Event {
806    /// When.
807    pub at: Timestamp,
808    /// Node name.
809    pub node: String,
810    /// What happened.
811    pub message: String,
812}
813
814/// How far the winner's tree trailed the landing base, last time it was
815/// checked, and what came of trying to close that gap.
816///
817/// Set by `graph::Runner::sync_to_base`, which runs before the review loop and
818/// again before the gate: verifying against a tree that does not yet contain
819/// the base's tip answers "green on the commit this run branched from", not
820/// "green on what is about to land", and a merge on that answer can revert
821/// whatever landed elsewhere while the run was thinking.
822#[derive(Debug, Clone, Serialize, Deserialize)]
823pub struct BaseSync {
824    /// `<remote>/<base>` tip the tree was last checked against.
825    pub tip: String,
826    /// Commits `tip` was ahead of the tree at that check, before any rebase
827    /// this round tried to close the gap. Zero means the tree already
828    /// contained `tip`.
829    pub behind: usize,
830    /// Rebase attempts spent so far this run, bounded by
831    /// `graph::BASE_SYNC_ROUNDS`.
832    pub attempts: usize,
833    /// What git said, if the most recent rebase attempt conflicted or could
834    /// not be pushed. `Some` here is what makes a `Blocked` run read as
835    /// "stopped on the base, not on review or the gate" - the rebase is not
836    /// retried again while this is set; a person has to look.
837    #[serde(default)]
838    pub conflict: Option<String>,
839}
840
841/// What the land loop saw last time it looked at the pull request.
842///
843/// Strings for `state` and `checks` on purpose: they are `gh`'s vocabulary, and
844/// pinning them into an enum here would mean a new GitHub check conclusion
845/// turns a readable status into a deserialisation error on a run someone is
846/// trying to look at.
847#[derive(Debug, Clone, Serialize, Deserialize)]
848pub struct PrRecord {
849    /// Pull request url.
850    pub url: String,
851    /// Pull request number.
852    pub number: u64,
853    /// `open`, `merged` or `closed`.
854    pub state: String,
855    /// `pending`, `green`, `red` or `unknown`.
856    pub checks: String,
857    /// Land round, 1-based, or 0 before the first fix.
858    pub round: usize,
859    /// Land round budget.
860    pub rounds: usize,
861}
862
863/// The whole run.
864#[derive(Debug, Clone, Serialize, Deserialize)]
865pub struct RunState {
866    /// On-disk format version.
867    pub schema: u32,
868    /// Run id, e.g. `20260830-153012-a1b2`.
869    pub id: String,
870    /// Repository the run operates on.
871    pub repo: PathBuf,
872    /// Branch the run started from.
873    pub base_branch: String,
874    /// Commit the run started from.
875    pub base_commit: String,
876    /// The task, verbatim.
877    pub instruction: String,
878    /// When the run was created.
879    pub created_at: Timestamp,
880    /// Last state flush.
881    pub updated_at: Timestamp,
882    /// Current status.
883    pub status: RunStatus,
884    /// Seed for labels and session ids.
885    pub seed: u64,
886    /// Config snapshot, so a resumed run behaves like the original.
887    pub config: Config,
888    /// Did this run take a reference on `extensions.worktreeConfig` being on
889    /// (see [`crate::git::acquire_worktree_config`])? If so, cleanup releases
890    /// it - which only actually turns the setting back off once every other
891    /// run sharing this repository has released its own reference too.
892    #[serde(default)]
893    pub enabled_worktree_config: bool,
894    /// Candidates.
895    #[serde(default)]
896    pub candidates: Vec<Candidate>,
897    /// Initial blind rankings.
898    #[serde(default)]
899    pub judgements: Vec<Judgement>,
900    /// `judge` decided a solo candidate needs no panel and only logged it.
901    ///
902    /// `judgements` stays empty in that case — nothing to distinguish from
903    /// "not yet judged" — so this is the record that makes the skip
904    /// idempotent: without it, every reentry re-ran `judge`, re-logged the
905    /// same event, and rewrote `status` to `Judging` over whatever a later
906    /// node had already concluded.
907    #[serde(default)]
908    pub judge_skipped: bool,
909    /// Deliberation, if it happened.
910    #[serde(default)]
911    pub deliberation: Vec<DeliberationRound>,
912    /// Private final votes.
913    #[serde(default)]
914    pub votes: Vec<VoteRecord>,
915    /// The count.
916    #[serde(default)]
917    pub tally: Option<Tally>,
918    /// Review rounds.
919    #[serde(default)]
920    pub reviews: Vec<ReviewRound>,
921    /// Final gate.
922    ///
923    /// Never derive whether the gate has run from `gate.is_empty()` alone —
924    /// use [`Self::gate_status`] instead. An empty list is ambiguous on its
925    /// own: it is what an unattempted gate looks like, what a
926    /// resource-blocked attempt leaves behind (see `graph::Runner::gate`'s
927    /// own doc), and also what a repo with no `verify.gate` commands
928    /// configured produces once it *has* run. [`Self::gate_ran`] is what
929    /// tells the third case apart from the first two.
930    #[serde(default)]
931    pub gate: Vec<CommandOutcome>,
932    /// Did `gate()` actually record an attempt — zero commands configured
933    /// and vacuously passed, or one or more commands that ran to
934    /// completion — as opposed to never having run, or having last hit a
935    /// resource-blocked retry?
936    ///
937    /// `gate.is_empty()` cannot tell those apart by itself: a repo with no
938    /// `verify.gate` commands leaves `gate` empty exactly like an
939    /// unattempted or resource-blocked one does, and reading that empty list
940    /// as "not yet run" is what stranded a review-only run in
941    /// `RunStatus::Gating` forever on such a repo — see `SCHEMA`'s doc for
942    /// schema 7. A record written before this field existed defaults to
943    /// `false` and is migrated in [`migrate_schema`].
944    #[serde(default)]
945    pub gate_ran: bool,
946    /// Merge outcome.
947    #[serde(default)]
948    pub merge: Option<MergeOutcome>,
949    /// Vendor tokens seen in judged material.
950    #[serde(default)]
951    pub leaks: Vec<Leak>,
952    /// Seats lost to a CLI rate limit / quota, in the order they hit.
953    #[serde(default)]
954    pub quota: Vec<QuotaLoss>,
955    /// Parked at a node boundary, waiting to be resumed.
956    ///
957    /// A run that is neither finished nor being worked on is otherwise
958    /// indistinguishable from one whose daemon was killed, and the two want
959    /// opposite things from an operator: the first is expected to be resumed,
960    /// the second is a leftover. Cleared by the resume that carries it on.
961    #[serde(default)]
962    pub parked: bool,
963    /// Per-seat conversation state.
964    #[serde(default)]
965    pub seats: BTreeMap<String, SeatState>,
966    /// Seats currently mid-answer, keyed by seat.
967    ///
968    /// An entry exists from the moment a prompt is sent until a reply (of any
969    /// kind — success, failure, quota, drop) comes back, so its keys are
970    /// exactly "who hasn't answered yet" for whichever node populated it. See
971    /// [`ActiveSeat`] for why a reader still has to check a live daemon
972    /// before trusting one of these as "running" rather than "abandoned".
973    #[serde(default)]
974    pub active: BTreeMap<String, ActiveSeat>,
975    /// Last observation of the winner's pull request, when a land loop ran.
976    ///
977    /// Persisted rather than derived from the event log because the phone asks
978    /// two questions about a run that has opened a PR - how are its checks and
979    /// which round is it on - and parsing prose out of events to answer them
980    /// would break the first time an event message was reworded.
981    #[serde(default)]
982    pub pr: Option<PrRecord>,
983    /// The last look at how far the winner's tree trailed the landing base,
984    /// and the rebase(s) tried to close that gap. `None` until the tree has a
985    /// winner to check.
986    #[serde(default)]
987    pub base_sync: Option<BaseSync>,
988    /// The design-deliberation stage's output, when `[graph] advise` ran it:
989    /// one record per advisor seat, plus the synthesis blended into the
990    /// implementer's prompt. `None` when the stage is off, has not run yet,
991    /// or could not even resolve its seats - see
992    /// [`crate::graph::Runner::advise`].
993    #[serde(default)]
994    pub advice: Option<crate::advise::Advice>,
995    /// Whether the design-deliberation stage has already been attempted this
996    /// run, whatever it produced. The idempotency marker `Runner::advise`
997    /// checks on reentry, the same role [`Self::judge_skipped`] plays for
998    /// `judge` - without it a resumed run whose stage failed (a misconfigured
999    /// `[roles] advisors`, every seat quota'd) would re-run it, and re-spend
1000    /// the agent calls, on every single reentry before `implement`.
1001    #[serde(default)]
1002    pub advise_attempted: bool,
1003    /// Node log.
1004    #[serde(default)]
1005    pub events: Vec<Event>,
1006    /// Commands seats' own CLIs reported running, across every node — see
1007    /// [`JobRecord`]. Populated in [`crate::graph::wave`] as each seat
1008    /// answers, so a resumed run keeps what earlier waves already collected
1009    /// rather than losing it to a reentry. Empty on a record written before
1010    /// this existed, or wherever no adapter reads structured job events for
1011    /// the backend a seat used — both read as "no evidence", not "nothing
1012    /// ran".
1013    #[serde(default)]
1014    pub jobs: Vec<JobRecord>,
1015}
1016
1017impl RunState {
1018    /// A fresh run.
1019    pub fn new(
1020        repo: PathBuf,
1021        base_branch: String,
1022        base_commit: String,
1023        instruction: String,
1024        config: Config,
1025    ) -> Self {
1026        let now = Timestamp::now();
1027        let seed = config.blind.seed.unwrap_or_else(crate::rng::entropy);
1028        Self {
1029            schema: SCHEMA,
1030            id: new_id(),
1031            repo,
1032            base_branch,
1033            base_commit,
1034            instruction,
1035            created_at: now,
1036            updated_at: now,
1037            status: RunStatus::Prep,
1038            seed,
1039            config,
1040            enabled_worktree_config: false,
1041            candidates: Vec::new(),
1042            judgements: Vec::new(),
1043            judge_skipped: false,
1044            deliberation: Vec::new(),
1045            votes: Vec::new(),
1046            tally: None,
1047            reviews: Vec::new(),
1048            gate: Vec::new(),
1049            gate_ran: false,
1050            merge: None,
1051            leaks: Vec::new(),
1052            quota: Vec::new(),
1053            parked: false,
1054            seats: BTreeMap::new(),
1055            active: BTreeMap::new(),
1056            pr: None,
1057            base_sync: None,
1058            advice: None,
1059            advise_attempted: false,
1060            events: Vec::new(),
1061            jobs: Vec::new(),
1062        }
1063    }
1064
1065    /// Directory holding this run's state and artifacts.
1066    pub fn dir(&self) -> PathBuf {
1067        run_dir(&self.id)
1068    }
1069
1070    /// Short form used in branch names and reports.
1071    pub fn short(&self) -> &str {
1072        short_of(&self.id)
1073    }
1074
1075    /// Branch name for a label.
1076    pub fn branch_for(&self, label: char) -> String {
1077        format!("magi/{}/{}", self.short(), label)
1078    }
1079
1080    /// Root of this run's worktrees.
1081    pub fn worktree_root(&self) -> PathBuf {
1082        self.config
1083            .graph
1084            .worktree_root
1085            .clone()
1086            .unwrap_or_else(default_worktree_root)
1087            .join(self.short())
1088    }
1089
1090    /// Note something in the run log and on the tracing stream.
1091    pub fn event(&mut self, node: &str, message: impl Into<String>) {
1092        let message = message.into();
1093        tracing::info!(node, "{message}");
1094        self.events.push(Event {
1095            at: Timestamp::now(),
1096            node: node.to_owned(),
1097            message,
1098        });
1099    }
1100
1101    /// The honest state of the final gate.
1102    ///
1103    /// Never derive this from `gate.is_empty()` alone anywhere else in the
1104    /// codebase — `NotRun` and `PassedWithNoCommands` both leave `gate`
1105    /// empty, and only this method (backed by [`Self::gate_ran`]) tells them
1106    /// apart. See `SCHEMA`'s doc for schema 7 for what conflating them used
1107    /// to do.
1108    pub fn gate_status(&self) -> GateStatus {
1109        if !self.gate_ran {
1110            GateStatus::NotRun
1111        } else if self.gate.is_empty() {
1112            GateStatus::PassedWithNoCommands
1113        } else if self.gate.iter().all(CommandOutcome::ok) {
1114            GateStatus::Passed
1115        } else {
1116            GateStatus::Failed
1117        }
1118    }
1119
1120    /// Record that `seat` was just sent a prompt for `node`, with the given
1121    /// wall-clock budget. `attempt` is 0 for the first ask and N for the Nth
1122    /// nudge or resume, purely for display — it does not change how the seat
1123    /// is treated.
1124    pub fn seat_started(
1125        &mut self,
1126        node: &str,
1127        seat: &str,
1128        timeout: std::time::Duration,
1129        attempt: usize,
1130    ) {
1131        self.active.insert(
1132            seat.to_owned(),
1133            ActiveSeat {
1134                node: node.to_owned(),
1135                started_at: Timestamp::now(),
1136                timeout_secs: timeout.as_secs(),
1137                attempt,
1138            },
1139        );
1140    }
1141
1142    /// Record that `seat` has answered, whatever the answer was.
1143    pub fn seat_finished(&mut self, seat: &str) {
1144        self.active.remove(seat);
1145    }
1146
1147    /// Drop every seat this state still lists as answering, reporting whether
1148    /// anything was dropped.
1149    ///
1150    /// Called first thing in `execute`, on every entry — fresh, resumed, or
1151    /// recovering a stall — because an entry here only means something while
1152    /// the process that wrote it is still asking that seat something. A
1153    /// process killed mid-wave leaves its last batch of seats here with
1154    /// nobody left to clear them, and the next process to touch this run must
1155    /// not let that leftover read as "still going" before it has asked
1156    /// anyone anything.
1157    pub fn clear_active(&mut self) -> bool {
1158        if self.active.is_empty() {
1159            return false;
1160        }
1161        self.active.clear();
1162        true
1163    }
1164
1165    /// Does every seat this run still lists as [`Self::active`] sit past its
1166    /// own [`ActiveSeat::timeout_secs`]? `false` when nothing is active at
1167    /// all — an empty map is not evidence of anything overrunning.
1168    ///
1169    /// This alone is not proof the run is dead: a seat's own attempt can
1170    /// legitimately run a little past its budget while the process driving it
1171    /// is still tearing the attempt down. Every caller pairs this with its own
1172    /// `!live` reading (`daemon::is_working_on`) before treating the run as
1173    /// abandoned — this module cannot check that itself without depending on
1174    /// `crate::daemon`, and callers already have to ask that question anyway.
1175    #[must_use]
1176    pub fn active_all_overrun(&self, now: Timestamp) -> bool {
1177        !self.active.is_empty()
1178            && self
1179                .active
1180                .values()
1181                .all(|a| a.elapsed_secs(now) > a.timeout_secs as i64)
1182    }
1183
1184    /// Clear every seat this run still lists as active and fail it, unless it
1185    /// had already reached a terminal status some other way.
1186    ///
1187    /// Callers must already have proven this run is dead — [`Self::active_all_overrun`]
1188    /// plus their own `!live` reading — before calling this; it does not
1189    /// check either itself. Unlike [`Self::clear_active`] (dropping a resumed
1190    /// run's own stale wave before repopulating it, called unconditionally at
1191    /// the top of every `execute()`), this is a verdict: a run left this way
1192    /// has nothing left to repopulate the wave, ever, and must stop reading as
1193    /// `implementing` (or whichever node) forever.
1194    pub fn abandon(&mut self, by: &str) {
1195        let seats: Vec<String> = self.active.keys().cloned().collect();
1196        self.clear_active();
1197        if !self.status.done() {
1198            self.status = RunStatus::Failed;
1199        }
1200        self.event(
1201            by,
1202            format!(
1203                "abandoned: seat(s) {} left behind by a killed process, past their own \
1204                 timeout with no live daemon claiming this run",
1205                seats.join(", ")
1206            ),
1207        );
1208    }
1209
1210    /// Flush to `run.json`, atomically, under the process-global [`home`].
1211    pub fn save(&mut self) -> Result<()> {
1212        let home = home();
1213        self.save_under(&home)
1214    }
1215
1216    /// [`Self::save`], rooted at an explicit `home` instead of the
1217    /// process-global one.
1218    ///
1219    /// For a caller that was already handed its own `home` explicitly — a
1220    /// housekeeping pass, mainly, for the same reason `Queue::at` and the
1221    /// daemon status path are parameters rather than resolved here (see
1222    /// `daemon::drive`'s own doc) — falling through to the global would write
1223    /// back through whichever directory some *other* process or test pinned
1224    /// into that `OnceLock` first, not the one this call was actually handed.
1225    pub fn save_under(&mut self, home: &Path) -> Result<()> {
1226        self.updated_at = Timestamp::now();
1227        let dir = home.join("runs").join(&self.id);
1228        std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
1229        let body = serde_json::to_string_pretty(self).context("serialize run state")?;
1230        let tmp = dir.join("run.json.tmp");
1231        std::fs::write(&tmp, &body).with_context(|| format!("write {}", tmp.display()))?;
1232        std::fs::rename(&tmp, dir.join("run.json")).with_context(|| "replace run.json")?;
1233        Ok(())
1234    }
1235
1236    /// Load a run by id or unambiguous id prefix.
1237    pub fn load(id: &str) -> Result<Self> {
1238        let resolved = resolve_id(id)?;
1239        let path = run_dir(&resolved).join("run.json");
1240        let body =
1241            std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
1242        let state: Self =
1243            serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
1244        migrate_schema(state)
1245    }
1246}
1247
1248fn migrate_schema(mut state: RunState) -> Result<RunState> {
1249    // Schema 5 predates deferred e2e. Its empty e2e lists therefore mean
1250    // "not configured", never "deferred"; serde's field defaults retain
1251    // exactly that representation while this migration permits resumes.
1252    if state.schema == 5 {
1253        state.schema = 6;
1254    }
1255    // Schema 6 predates `gate_ran` and could not tell "never attempted or
1256    // resource-blocked" apart from "ran with zero commands configured" — see
1257    // `SCHEMA`'s doc for schema 7. A non-empty `gate` is a real recorded
1258    // attempt either way, so it is trusted as `gate_ran = true` rather than
1259    // spent again; an empty one is simply handed back to the next `gate()`
1260    // call, which re-attempts it and, for the zero-commands case, resolves
1261    // instantly.
1262    if state.schema == 6 {
1263        state.gate_ran = !state.gate.is_empty();
1264        state.schema = SCHEMA;
1265    }
1266    if state.schema != SCHEMA {
1267        bail!(
1268            "run {} was written by a different magi (schema {}, this build \
1269                 speaks {SCHEMA})",
1270            state.id,
1271            state.schema
1272        );
1273    }
1274    Ok(state)
1275}
1276
1277impl RunState {
1278    /// The winning candidate, once the tally has run.
1279    pub fn winner(&self) -> Option<&Candidate> {
1280        let label = self.tally.as_ref()?.winner;
1281        self.candidates.iter().find(|c| c.label == label)
1282    }
1283
1284    /// Candidates eligible for judging.
1285    pub fn viable(&self) -> Vec<&Candidate> {
1286        self.candidates.iter().filter(|c| c.viable()).collect()
1287    }
1288
1289    /// Findings still open when the review loop stopped trying: the last
1290    /// round's, exactly when that round was not clean. Empty on a run that
1291    /// never reviewed, or whose last round was clean.
1292    ///
1293    /// This is the last round's findings regardless of what the fixer claims
1294    /// to have addressed in that same round: a round that stopped the loop
1295    /// (round budget spent, or no tree progress for
1296    /// [`crate::graph::STAGNANT_LIMIT`] rounds) never had a *following* round
1297    /// to confirm the fix actually landed, and the self-reported adoption
1298    /// count is not trusted for that judgement either — see
1299    /// [`ReviewRound::progressed`].
1300    pub fn open_findings(&self) -> Vec<&Finding> {
1301        match self.reviews.last() {
1302            Some(r) if !r.clean => r
1303                .reviews
1304                .iter()
1305                .flat_map(|rec| rec.findings.iter())
1306                .collect(),
1307            _ => Vec::new(),
1308        }
1309    }
1310
1311    /// Did this run reach a mergeable status (`Ready` or `Merged`) with
1312    /// review findings still open?
1313    ///
1314    /// That combination is the point of the review hand-off: the review
1315    /// round budget (or an unproductive round, see [`ReviewRound::progressed`])
1316    /// was spent while gate and e2e stayed green, so the run was handed off
1317    /// rather than blocked — but the findings did not disappear, and whoever
1318    /// reads the result should be told they are still there.
1319    pub fn handed_off_with_open_findings(&self) -> bool {
1320        matches!(self.status, RunStatus::Ready | RunStatus::Merged)
1321            && self.reviews.last().is_some_and(|r| !r.clean)
1322    }
1323
1324    /// Reached `Ready` because `[merge] mode = "none"` left it there by
1325    /// design, never to be picked up by the PR-polling merge watcher — as
1326    /// opposed to a `Ready` that is still a plausible landing candidate (a
1327    /// PR closed without merging, or a re-entry onto an already-concluded
1328    /// node). Both leave `status` at `Ready`; only this one leaves the
1329    /// winning branch permanently unwatched, which is what a caller needs to
1330    /// know before labelling the run in a listing.
1331    pub fn unmerged_by_design(&self) -> bool {
1332        self.status == RunStatus::Ready
1333            && self
1334                .merge
1335                .as_ref()
1336                .is_some_and(|m| m.mode == MergeMode::None)
1337    }
1338
1339    /// Local-time creation stamp for reports.
1340    pub fn created_local(&self) -> String {
1341        self.created_at
1342            .to_zoned(jiff::tz::TimeZone::system())
1343            .strftime("%Y-%m-%d %H:%M:%S")
1344            .to_string()
1345    }
1346
1347    /// Assert that this run is safe to delete.
1348    ///
1349    /// Refuses a run a live daemon is working on, and refuses any run whose
1350    /// candidate worktrees and branches have not been folded away with `magi
1351    /// fold`. The fold requirement is the real protection: it is what makes
1352    /// "delete" mean "remove a record" rather than "throw away a worktree
1353    /// somebody may still be editing".
1354    ///
1355    /// `in_flight` has to come from the caller, because a run's own status
1356    /// cannot answer the question. A daemon killed mid-run leaves its status at
1357    /// `implementing` forever, and a guard that trusted that would make every
1358    /// interrupted run permanently undeletable - the operator's only recourse
1359    /// being to edit `run.json` by hand, which is exactly the sort of thing
1360    /// this command exists to avoid. The queue already treats an orphaned
1361    /// `.lock` from a `SIGKILL`ed daemon the same way; this is that rule for
1362    /// runs.
1363    pub fn ensure_can_delete(&self, in_flight: bool) -> Result<()> {
1364        if in_flight {
1365            bail!(
1366                "run {} is being worked on by a live daemon right now",
1367                self.short()
1368            );
1369        }
1370        if self.candidates.iter().any(|c| !c.folded) {
1371            bail!(
1372                "run {} has unfolded candidates; fold first with `magi fold`",
1373                self.short()
1374            );
1375        }
1376        Ok(())
1377    }
1378}
1379
1380/// The short form of a run id: the trailing block after the last `-`.
1381///
1382/// A free function as well as [`RunState::short`], because callers that have
1383/// only an id - an error message, a daemon status, a route handler - were
1384/// otherwise reimplementing the split, and two spellings of "short id" is one
1385/// rename away from branch names that no longer match their run.
1386pub fn short_of(id: &str) -> &str {
1387    id.split('-').next_back().unwrap_or(id)
1388}
1389
1390/// Where magi keeps its runs.
1391///
1392/// `MAGI_HOME` overrides the default, and [`set_home`] overrides both — which
1393/// is what lets the integration tests drive a whole graph without writing into
1394/// the operator's real history.
1395///
1396/// In a unit test build (`cfg(test)`), falling through to the real
1397/// `<data_local>/magi` is not a fallback worth having: it is exactly how
1398/// three broken fixture runs ended up in the operator's actual history and
1399/// were counted as `unreadable` by the deck. A test that reaches this point
1400/// forgot to call [`set_home`] (or set `MAGI_HOME`) - that is a bug in the
1401/// test, not a case to serve, so it panics instead of writing anywhere.
1402pub fn home() -> PathBuf {
1403    resolve_home(HOME.get().cloned(), std::env::var_os("MAGI_HOME"))
1404}
1405
1406/// The decision `home` makes, taking its two overrides as plain values
1407/// instead of reading the `OnceLock` and the environment itself.
1408///
1409/// Pulled out so the `cfg(test)` panic is asserted directly against a
1410/// `None, None` input, rather than racing every other unit test in the
1411/// binary for who touches the process-global `HOME` first.
1412fn resolve_home(pinned: Option<PathBuf>, magi_home_env: Option<std::ffi::OsString>) -> PathBuf {
1413    if let Some(dir) = pinned {
1414        return dir;
1415    }
1416    if let Some(dir) = magi_home_env {
1417        return PathBuf::from(dir);
1418    }
1419    #[cfg(test)]
1420    {
1421        panic!(
1422            "run::home() was reached in a test without run::set_home() or \
1423             MAGI_HOME; this would write into the operator's real \
1424             <data_local>/magi. Call `run::set_home(temp_dir)` before any \
1425             code path that touches a RunState."
1426        );
1427    }
1428    #[cfg(not(test))]
1429    {
1430        dirs::data_local_dir()
1431            .unwrap_or_else(|| PathBuf::from("."))
1432            .join("magi")
1433    }
1434}
1435
1436/// Pin the run home for this process. The first call wins.
1437pub fn set_home(dir: PathBuf) {
1438    let _ = HOME.set(dir);
1439}
1440
1441static HOME: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
1442
1443/// `<home>/runs`.
1444pub fn runs_root() -> PathBuf {
1445    home().join("runs")
1446}
1447
1448/// The worktree root a run uses when the config sets none: `~/wt/magi`.
1449///
1450/// One definition of the default, so the folder the janitor folds and the
1451/// folder the health view sizes cannot drift apart: a run with no configured
1452/// [`crate::config::Graph::worktree_root`] lays its worktrees exactly here.
1453pub fn default_worktree_root() -> PathBuf {
1454    dirs::home_dir()
1455        .unwrap_or_else(|| PathBuf::from("."))
1456        .join("wt")
1457        .join("magi")
1458}
1459
1460/// Directory for one run id.
1461pub fn run_dir(id: &str) -> PathBuf {
1462    runs_root().join(id)
1463}
1464
1465/// Every run id on disk, newest first.
1466///
1467/// A directory is a run because of its **name**, not because it holds a
1468/// readable `run.json`. A run whose very first save lost the machine's last
1469/// free bytes leaves `<id>/run.json.tmp` and nothing else, and filtering on
1470/// `run.json` made that run invisible everywhere: not in `magi list`, not in
1471/// `runs_unreadable`, not on the phone, so nothing could report it and no
1472/// route could clear it. `88c0` sat like that for two days. Unreadable is
1473/// counted, never hidden - the readers already say why each one cannot be
1474/// read, and `fold_unreadable` is how a record like this leaves.
1475pub fn list_ids() -> Vec<String> {
1476    let mut ids: Vec<String> = std::fs::read_dir(runs_root())
1477        .into_iter()
1478        .flatten()
1479        .flatten()
1480        .filter(|e| e.path().is_dir())
1481        .map(|e| e.file_name().to_string_lossy().into_owned())
1482        .filter(|name| is_run_id(name))
1483        .collect();
1484    // Ids start with a sortable timestamp.
1485    ids.sort_unstable_by(|a, b| b.cmp(a));
1486    ids
1487}
1488
1489/// Does `name` have the shape [`new_id`] mints: `YYYYMMDD-HHMMSS-xxxx`?
1490///
1491/// The test for "this directory is a run", so a stray folder under
1492/// `<home>/runs` is not reported as a broken run.
1493///
1494/// The tag is checked for length and for being alphanumeric, not for being
1495/// hex: real ids are hex, but fixtures across this crate name runs
1496/// `...-dead` / `...-gone` / `...-once`, and a predicate that disowned those
1497/// would be asserting the fixtures' spelling rather than the shape.
1498pub fn is_run_id(name: &str) -> bool {
1499    let mut parts = name.split('-');
1500    let (Some(day), Some(time), Some(tag), None) =
1501        (parts.next(), parts.next(), parts.next(), parts.next())
1502    else {
1503        return false;
1504    };
1505    day.len() == 8
1506        && day.bytes().all(|b| b.is_ascii_digit())
1507        && time.len() == 6
1508        && time.bytes().all(|b| b.is_ascii_digit())
1509        && tag.len() == 4
1510        && tag.bytes().all(|b| b.is_ascii_alphanumeric())
1511}
1512
1513/// Expand an id prefix to exactly one run id.
1514pub fn resolve_id(prefix: &str) -> Result<String> {
1515    // A whole id names its directory, readable state or not: the run whose
1516    // `run.json` never landed still has to be reachable by `magi show` and
1517    // by the fold route, which is the only way its record ever leaves.
1518    if is_run_id(prefix) && run_dir(prefix).is_dir() {
1519        return Ok(prefix.to_owned());
1520    }
1521    let hits: Vec<String> = list_ids()
1522        .into_iter()
1523        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
1524        .collect();
1525    match hits.len() {
1526        1 => Ok(hits.into_iter().next().expect("exactly one hit")),
1527        0 => bail!("no run matches `{prefix}`"),
1528        _ => bail!(
1529            "`{prefix}` matches {} runs: {}",
1530            hits.len(),
1531            hits.join(", ")
1532        ),
1533    }
1534}
1535
1536/// The most recent run, if any.
1537pub fn latest_id() -> Option<String> {
1538    list_ids().into_iter().next()
1539}
1540
1541/// `YYYYMMDD-HHMMSS-xxxx`, sortable and short enough for a branch name.
1542///
1543/// The four hex digits are fresh entropy, **not** `blind.seed`. They were the
1544/// seed, and a pinned seed then made the whole id a function of the second it
1545/// started in: two runs a second apart were distinguishable, two in the same
1546/// second were not. Everything keyed on the id collided with them - the run
1547/// directory, `artifacts/`, and the candidate worktrees under
1548/// `wt/magi/<short>/`.
1549///
1550/// `tests/common` pins the seed on purpose, so its integration tests all share
1551/// one suffix. On Windows the suite is slow enough that the seconds differ and
1552/// nothing showed; on Linux `graph_dropped_stream`'s three tests run inside
1553/// 16s, so two of them shared a run directory and the second read an artifact
1554/// the first had written (`impl-B-resume.out`) - a failure that looked like the
1555/// resume logic misbehaving and was really two runs in one directory.
1556///
1557/// A seed exists to make the *blind* decisions reproducible: label assignment
1558/// and per-judge presentation order. It was never meant to name the run, and
1559/// `RunState::seed` still carries it for what it is for.
1560fn new_id() -> String {
1561    let stamp = Zoned::now().strftime("%Y%m%d-%H%M%S");
1562    let entropy = crate::rng::entropy();
1563    format!("{stamp}-{:04x}", (entropy ^ (entropy >> 32)) & 0xffff)
1564}
1565
1566/// Keep the last `max` bytes of `text`, on a line boundary.
1567pub fn tail(text: &str, max: usize) -> String {
1568    if text.len() <= max {
1569        return text.to_owned();
1570    }
1571    let mut cut = text.len() - max;
1572    while cut < text.len() && !text.is_char_boundary(cut) {
1573        cut += 1;
1574    }
1575    let slice = &text[cut..];
1576    let start = slice.find('\n').map_or(0, |i| i + 1);
1577    format!(
1578        "[... {} earlier bytes omitted ...]\n{}",
1579        cut,
1580        &slice[start..]
1581    )
1582}
1583
1584/// Path of a run artifact.
1585pub fn artifact_path(run: &RunState, name: &str) -> PathBuf {
1586    run.dir().join("artifacts").join(name)
1587}
1588
1589/// Write an artifact, creating the directory if needed.
1590pub fn write_artifact(run: &RunState, name: &str, body: &str) -> Result<PathBuf> {
1591    let path = artifact_path(run, name);
1592    if let Some(parent) = path.parent() {
1593        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
1594    }
1595    std::fs::write(&path, body).with_context(|| format!("write {}", path.display()))?;
1596    Ok(path)
1597}
1598
1599/// Read an artifact back, e.g. a stored patch on resume.
1600pub fn read_artifact(run: &RunState, name: &str) -> Option<String> {
1601    std::fs::read_to_string(artifact_path(run, name)).ok()
1602}
1603
1604#[cfg(test)]
1605mod tests {
1606    use super::*;
1607
1608    fn state() -> RunState {
1609        RunState::new(
1610            PathBuf::from("/repo"),
1611            "main".to_owned(),
1612            "abc1234def".to_owned(),
1613            "add retries".to_owned(),
1614            Config::default(),
1615        )
1616    }
1617
1618    #[test]
1619    fn resolve_home_prefers_the_pin_then_the_env_var() {
1620        let pinned = PathBuf::from("/pinned");
1621        assert_eq!(
1622            resolve_home(Some(pinned.clone()), Some("/env".into())),
1623            pinned,
1624            "a pin wins even over MAGI_HOME"
1625        );
1626        assert_eq!(
1627            resolve_home(None, Some("/env".into())),
1628            PathBuf::from("/env")
1629        );
1630    }
1631
1632    #[test]
1633    #[should_panic(expected = "run::set_home()")]
1634    fn resolve_home_refuses_to_fall_back_to_the_operators_real_home() {
1635        // Neither override present is exactly the state a test reaches by
1636        // forgetting `set_home`/`MAGI_HOME` - the accident that put three
1637        // broken fixture runs into the operator's real history. Asserted
1638        // against the pure decision directly, not `home()` itself, because
1639        // `HOME` is a process-wide `OnceLock` another test may have already
1640        // set - this must not depend on test execution order.
1641        resolve_home(None, None);
1642    }
1643
1644    #[test]
1645    fn a_run_is_named_by_shape_so_a_state_less_directory_is_still_a_run() {
1646        // The shape `new_id` mints. A directory answering to it is a run even
1647        // with no readable `run.json`: that is how a save that ran out of
1648        // disk stays visible instead of vanishing from every listing.
1649        assert!(is_run_id(&new_id()));
1650        assert!(is_run_id("20260904-014540-88c0"));
1651        // Not runs: a stray folder, a truncated id, a non-hex tag, and an id
1652        // with an extra segment (a worktree label, say).
1653        assert!(!is_run_id("scratch"));
1654        assert!(!is_run_id("20260904-014540"));
1655        assert!(!is_run_id("20260904-014540-88c0f"));
1656        assert!(!is_run_id("2026090x-014540-88c0"));
1657        assert!(!is_run_id("20260904-014540-88c0-A"));
1658    }
1659
1660    #[test]
1661    fn ids_are_sortable_and_short_suffixed() {
1662        let s = state();
1663        let parts: Vec<&str> = s.id.split('-').collect();
1664        assert_eq!(parts.len(), 3);
1665        assert_eq!(parts[0].len(), 8);
1666        assert_eq!(parts[1].len(), 6);
1667        assert_eq!(parts[2].len(), 4);
1668        assert_eq!(s.short(), parts[2]);
1669    }
1670
1671    #[test]
1672    fn branch_names_carry_the_label_not_the_author() {
1673        let s = state();
1674        let b = s.branch_for('B');
1675        assert_eq!(b, format!("magi/{}/B", s.short()));
1676        assert!(!b.contains("claude"));
1677    }
1678
1679    /// A pinned seed reproduces the blind decisions. It must **not** reproduce
1680    /// the run's identity.
1681    ///
1682    /// `assert_eq!(a.short(), b.short())` used to stand where the last
1683    /// assertion is now, and it was pinning the defect: with the id's suffix
1684    /// derived from the seed, two runs started in the same second were the
1685    /// same run as far as the filesystem was concerned - one directory, one
1686    /// `artifacts/`, one set of candidate worktrees. `tests/common` pins a
1687    /// seed for every integration test, so on Linux, where the suite is fast,
1688    /// two tests in `graph_dropped_stream` shared a directory and one read the
1689    /// other's artifact.
1690    #[test]
1691    fn a_pinned_seed_is_reproducible_but_never_the_run_id() {
1692        let mut cfg = Config::default();
1693        cfg.blind.seed = Some(1234);
1694        let a = RunState::new(
1695            PathBuf::from("/r"),
1696            "main".to_owned(),
1697            "c".to_owned(),
1698            "t".to_owned(),
1699            cfg.clone(),
1700        );
1701        let b = RunState::new(
1702            PathBuf::from("/r"),
1703            "main".to_owned(),
1704            "c".to_owned(),
1705            "t".to_owned(),
1706            cfg,
1707        );
1708        // What the seed is for: the same shuffles, run after run.
1709        assert_eq!(a.seed, 1234);
1710        assert_eq!(a.seed, b.seed);
1711        // What it is not for. Two runs are two runs, in the same second or
1712        // not, and everything keyed on the id depends on that.
1713        assert_ne!(
1714            a.id, b.id,
1715            "two runs sharing an id share a directory, artifacts and worktrees"
1716        );
1717    }
1718
1719    #[test]
1720    fn status_terminality() {
1721        assert!(RunStatus::Merged.done());
1722        assert!(RunStatus::Blocked.done());
1723        assert!(!RunStatus::Reviewing.done());
1724    }
1725
1726    fn overrun_seat(now: Timestamp, elapsed_secs: i64, timeout_secs: u64) -> ActiveSeat {
1727        ActiveSeat {
1728            node: "implement".to_owned(),
1729            started_at: now - jiff::SignedDuration::new(elapsed_secs, 0),
1730            timeout_secs,
1731            attempt: 0,
1732        }
1733    }
1734
1735    #[test]
1736    fn active_all_overrun_requires_every_seat_past_its_own_timeout() {
1737        let mut s = state();
1738        let now = Timestamp::now();
1739        assert!(
1740            !s.active_all_overrun(now),
1741            "nothing active is not evidence of anything"
1742        );
1743
1744        s.active
1745            .insert("impl-A".to_owned(), overrun_seat(now, 21_000, 3_600));
1746        assert!(
1747            s.active_all_overrun(now),
1748            "21000s elapsed against a 3600s budget"
1749        );
1750
1751        // A seat still well within its own budget means the run is not
1752        // provably dead, however far its sibling has overrun.
1753        s.active
1754            .insert("impl-B".to_owned(), overrun_seat(now, 0, 3_600));
1755        assert!(!s.active_all_overrun(now));
1756    }
1757
1758    #[test]
1759    fn abandon_clears_active_and_fails_a_non_terminal_run() {
1760        let mut s = state();
1761        s.status = RunStatus::Implementing;
1762        let now = Timestamp::now();
1763        s.active
1764            .insert("impl-A".to_owned(), overrun_seat(now, 21_000, 3_600));
1765
1766        s.abandon("daemon");
1767
1768        assert!(s.active.is_empty());
1769        assert_eq!(s.status, RunStatus::Failed);
1770        assert!(
1771            s.events
1772                .last()
1773                .expect("an event was logged")
1774                .message
1775                .contains("impl-A"),
1776            "the event names the abandoned seat"
1777        );
1778    }
1779
1780    #[test]
1781    fn abandon_never_overwrites_a_status_already_terminal() {
1782        let mut s = state();
1783        s.status = RunStatus::Ready;
1784        let now = Timestamp::now();
1785        s.active
1786            .insert("impl-A".to_owned(), overrun_seat(now, 21_000, 3_600));
1787
1788        s.abandon("daemon");
1789
1790        assert!(s.active.is_empty());
1791        assert_eq!(
1792            s.status,
1793            RunStatus::Ready,
1794            "a run already done must not be relabelled Failed"
1795        );
1796    }
1797
1798    #[test]
1799    fn candidate_viability_excludes_empty_and_failed() {
1800        let mut c = Candidate {
1801            index: 0,
1802            label: 'A',
1803            agent: "a".to_owned(),
1804            branch: "b".to_owned(),
1805            worktree: PathBuf::from("/w"),
1806            summary: String::new(),
1807            stat: String::new(),
1808            files: 1,
1809            commits: 1,
1810            empty: false,
1811            failed: None,
1812            duration_ms: 0,
1813            folded: false,
1814        };
1815        assert!(c.viable());
1816        c.empty = true;
1817        assert!(!c.viable());
1818        c.empty = false;
1819        c.failed = Some("timeout".to_owned());
1820        assert!(!c.viable());
1821    }
1822
1823    #[test]
1824    fn build_failure_is_distinguished_from_a_failing_test() {
1825        let link_race = CommandOutcome {
1826            command: "cargo test".to_owned(),
1827            code: Some(1),
1828            output_tail: "LINK : fatal error LNK1104: cannot open file \
1829                          'graph_dirty_tree-71d4dc8e.exe'\n\
1830                          error: could not compile `magi-cli` (test \"graph_dirty_tree\")"
1831                .to_owned(),
1832            duration_ms: 500,
1833            resource_blocked: false,
1834        };
1835        assert!(!link_race.ok());
1836        assert!(link_race.build_failed());
1837
1838        let failing_test = CommandOutcome {
1839            command: "cargo test".to_owned(),
1840            code: Some(101),
1841            output_tail: "thread 'it_works' panicked at 'assertion failed'".to_owned(),
1842            duration_ms: 500,
1843            resource_blocked: false,
1844        };
1845        assert!(!failing_test.ok());
1846        assert!(
1847            !failing_test.build_failed(),
1848            "a real test failure must not be classed as a build failure"
1849        );
1850
1851        let passing = CommandOutcome {
1852            command: "cargo test".to_owned(),
1853            code: Some(0),
1854            output_tail: String::new(),
1855            duration_ms: 500,
1856            resource_blocked: false,
1857        };
1858        assert!(passing.ok());
1859        assert!(!passing.build_failed());
1860    }
1861
1862    #[test]
1863    fn tail_keeps_the_end_on_a_line_boundary() {
1864        let text = (0..100).map(|i| format!("line {i}\n")).collect::<String>();
1865        let t = tail(&text, 40);
1866        assert!(t.starts_with("[..."));
1867        assert!(t.ends_with("line 99\n"));
1868        assert!(t.len() < 120);
1869        assert_eq!(tail("short", 40), "short");
1870    }
1871
1872    #[test]
1873    fn tail_survives_multibyte_cuts() {
1874        let text = "あ".repeat(50);
1875        let t = tail(&text, 10);
1876        assert!(t.contains("earlier bytes omitted"));
1877        assert!(t.ends_with('あ'));
1878    }
1879
1880    fn finding(id: &str, severity: crate::verdict::Severity) -> crate::verdict::Finding {
1881        crate::verdict::Finding {
1882            id: id.to_owned(),
1883            severity,
1884            file: None,
1885            line: None,
1886            title: "x".to_owned(),
1887            detail: String::new(),
1888        }
1889    }
1890
1891    fn round(clean: bool, findings: Vec<crate::verdict::Finding>) -> ReviewRound {
1892        ReviewRound {
1893            round: 1,
1894            head: "h".to_owned(),
1895            verified_head: None,
1896            reviews: vec![ReviewRecord {
1897                reviewer: 1,
1898                agent: "a".to_owned(),
1899                summary: String::new(),
1900                findings,
1901                vote: None,
1902                failed: None,
1903                duration_ms: 0,
1904            }],
1905            e2e: Vec::new(),
1906            verify_retried: false,
1907            e2e_deferred: false,
1908            e2e_defer_reason: None,
1909            fix: None,
1910            blocking: 0,
1911            answered: 1,
1912            expected: 1,
1913            clean,
1914            progressed: false,
1915            vote_split: false,
1916            reconsideration: Vec::new(),
1917            verdict: None,
1918        }
1919    }
1920
1921    #[test]
1922    fn e2e_status_tells_deferred_apart_from_not_configured() {
1923        let mut r = round(false, Vec::new());
1924        assert_eq!(r.e2e_status(), E2eStatus::NotConfigured);
1925
1926        r.e2e_deferred = true;
1927        assert_eq!(
1928            r.e2e_status(),
1929            E2eStatus::Deferred,
1930            "an empty e2e must not read as unconfigured once it was deferred on purpose"
1931        );
1932
1933        r.e2e = vec![CommandOutcome {
1934            command: "test".to_owned(),
1935            code: Some(0),
1936            output_tail: String::new(),
1937            duration_ms: 0,
1938            resource_blocked: false,
1939        }];
1940        assert_eq!(
1941            r.e2e_status(),
1942            E2eStatus::Passed,
1943            "a round with real outcomes is never read as deferred, even if the flag is still set"
1944        );
1945    }
1946
1947    #[test]
1948    fn e2e_status_reports_a_real_failure_as_failed_not_deferred() {
1949        let mut r = round(false, Vec::new());
1950        r.e2e = vec![CommandOutcome {
1951            command: "test".to_owned(),
1952            code: Some(1),
1953            output_tail: "boom".to_owned(),
1954            duration_ms: 0,
1955            resource_blocked: false,
1956        }];
1957        assert_eq!(r.e2e_status(), E2eStatus::Failed);
1958    }
1959
1960    #[test]
1961    fn gate_status_tells_not_run_apart_from_passed_with_no_commands() {
1962        let mut s = state();
1963        assert_eq!(s.gate_status(), GateStatus::NotRun);
1964
1965        s.gate_ran = true;
1966        assert_eq!(
1967            s.gate_status(),
1968            GateStatus::PassedWithNoCommands,
1969            "an empty gate must read as a real pass once gate_ran says it actually ran"
1970        );
1971
1972        s.gate = vec![CommandOutcome {
1973            command: "cargo make check".to_owned(),
1974            code: Some(0),
1975            output_tail: String::new(),
1976            duration_ms: 0,
1977            resource_blocked: false,
1978        }];
1979        assert_eq!(s.gate_status(), GateStatus::Passed);
1980
1981        s.gate[0].code = Some(1);
1982        assert_eq!(s.gate_status(), GateStatus::Failed);
1983
1984        s.gate_ran = false;
1985        assert_eq!(
1986            s.gate_status(),
1987            GateStatus::NotRun,
1988            "gate_ran false must win even over a non-empty gate left from a stale record"
1989        );
1990    }
1991
1992    #[test]
1993    fn open_findings_is_empty_when_the_last_round_was_clean() {
1994        let mut s = state();
1995        s.reviews = vec![round(
1996            true,
1997            vec![finding("R1-1-1", crate::verdict::Severity::Minor)],
1998        )];
1999        assert!(s.open_findings().is_empty());
2000    }
2001
2002    #[test]
2003    fn open_findings_reads_the_last_non_clean_round() {
2004        let mut s = state();
2005        s.reviews = vec![round(
2006            false,
2007            vec![finding("R1-1-1", crate::verdict::Severity::Major)],
2008        )];
2009        let open = s.open_findings();
2010        assert_eq!(open.len(), 1);
2011        assert_eq!(open[0].id, "R1-1-1");
2012    }
2013
2014    #[test]
2015    fn handed_off_with_open_findings_needs_a_mergeable_status_and_an_open_round() {
2016        let mut s = state();
2017        s.reviews = vec![round(
2018            false,
2019            vec![finding("R1-1-1", crate::verdict::Severity::Major)],
2020        )];
2021
2022        s.status = RunStatus::Blocked;
2023        assert!(
2024            !s.handed_off_with_open_findings(),
2025            "a blocked run is not a hand-off"
2026        );
2027
2028        s.status = RunStatus::Ready;
2029        assert!(s.handed_off_with_open_findings());
2030
2031        s.reviews = vec![round(true, Vec::new())];
2032        assert!(
2033            !s.handed_off_with_open_findings(),
2034            "a clean last round has nothing to hand off"
2035        );
2036    }
2037
2038    #[test]
2039    fn unmerged_by_design_is_only_ready_reached_via_merge_mode_none() {
2040        let mut s = state();
2041
2042        s.status = RunStatus::Ready;
2043        assert!(
2044            !s.unmerged_by_design(),
2045            "no merge outcome recorded at all must not be flagged"
2046        );
2047
2048        s.merge = Some(MergeOutcome {
2049            mode: MergeMode::None,
2050            ok: true,
2051            detail: "git merge --no-ff magi/x/A".to_owned(),
2052        });
2053        assert!(
2054            s.unmerged_by_design(),
2055            "Ready reached through mode none is the case this exists to flag"
2056        );
2057
2058        // A PR closed without merging also leaves `status` at `Ready`, but
2059        // through `mode = "pr"` — a run that may still have been landable by
2060        // a person watching the PR, unlike the honest mode-none no-op.
2061        s.merge = Some(MergeOutcome {
2062            mode: MergeMode::Pr,
2063            ok: false,
2064            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
2065        });
2066        assert!(
2067            !s.unmerged_by_design(),
2068            "a closed pull request is a different Ready and must not be relabelled"
2069        );
2070
2071        // Same signal must not fire before the run actually got there.
2072        s.status = RunStatus::Gating;
2073        s.merge = Some(MergeOutcome {
2074            mode: MergeMode::None,
2075            ok: true,
2076            detail: "git merge --no-ff magi/x/A".to_owned(),
2077        });
2078        assert!(
2079            !s.unmerged_by_design(),
2080            "status must actually be Ready, not merely have a stale mode-none merge record"
2081        );
2082    }
2083
2084    #[test]
2085    fn state_round_trips_through_json() {
2086        let s = state();
2087        let body = serde_json::to_string(&s).unwrap();
2088        let back: RunState = serde_json::from_str(&body).unwrap();
2089        assert_eq!(back.id, s.id);
2090        assert_eq!(back.instruction, "add retries");
2091        assert_eq!(back.status, RunStatus::Prep);
2092    }
2093
2094    #[test]
2095    fn a_round_recorded_before_e2e_deferral_existed_still_loads() {
2096        // Exactly the shape a pre-existing `run.json` has for a round: no
2097        // `e2e_deferred`, no `e2e_defer_reason`. Every round used to run e2e
2098        // unconditionally, so the honest reading of an old record's silence
2099        // on this is "it was not deferred" — `false`/`None`, not a load
2100        // failure and not a schema bump (see the `SCHEMA` doc comment: a
2101        // purely additive field whose absence has one unambiguous meaning
2102        // does not need one).
2103        let body = r#"{
2104            "round": 1,
2105            "head": "deadbeef",
2106            "reviews": [],
2107            "e2e": [],
2108            "verify_retried": false,
2109            "fix": null,
2110            "blocking": 0,
2111            "answered": 1,
2112            "expected": 1,
2113            "clean": true
2114        }"#;
2115        let r: ReviewRound = serde_json::from_str(body).expect("an old-shaped round must load");
2116        assert!(!r.e2e_deferred);
2117        assert!(r.e2e_defer_reason.is_none());
2118        assert_eq!(r.e2e_status(), E2eStatus::NotConfigured);
2119    }
2120
2121    #[test]
2122    fn schema_five_state_migrates_old_empty_e2e_and_legacy_verify_budget() {
2123        let mut value = serde_json::to_value(state()).expect("serialize state");
2124        let object = value.as_object_mut().expect("state object");
2125        object.insert("schema".to_owned(), serde_json::json!(5));
2126        let graph = object["config"]["graph"]
2127            .as_object_mut()
2128            .expect("graph object");
2129        graph.insert("timeout_review".to_owned(), serde_json::json!(3600));
2130        graph.remove("timeout_verify");
2131        let review = object["reviews"].as_array_mut().expect("reviews");
2132        review.push(serde_json::json!({
2133            "round": 1, "head": "old", "reviews": [], "e2e": [],
2134            "verify_retried": false, "blocking": 0, "answered": 1,
2135            "expected": 1, "clean": true
2136        }));
2137        let old: RunState = serde_json::from_value(value).expect("schema-5 shape parses");
2138        let migrated = migrate_schema(old).expect("schema 5 migrates");
2139        assert_eq!(migrated.schema, SCHEMA);
2140        assert_eq!(migrated.config.graph.verify_timeout(), 3600);
2141        assert_eq!(migrated.reviews[0].e2e_status(), E2eStatus::NotConfigured);
2142    }
2143
2144    #[test]
2145    fn schema_six_state_with_a_recorded_gate_migrates_to_gate_ran_true() {
2146        let mut value = serde_json::to_value(state()).expect("serialize state");
2147        let object = value.as_object_mut().expect("state object");
2148        object.insert("schema".to_owned(), serde_json::json!(6));
2149        object.insert(
2150            "gate".to_owned(),
2151            serde_json::json!([{
2152                "command": "cargo make check",
2153                "code": 0,
2154                "output_tail": "",
2155                "duration_ms": 0,
2156                "resource_blocked": false
2157            }]),
2158        );
2159        let old: RunState = serde_json::from_value(value).expect("schema-6 shape parses");
2160        let migrated = migrate_schema(old).expect("schema 6 migrates");
2161        assert_eq!(migrated.schema, SCHEMA);
2162        assert!(
2163            migrated.gate_ran,
2164            "a non-empty recorded gate is a real attempt, not an unrun one"
2165        );
2166        assert_eq!(migrated.gate_status(), GateStatus::Passed);
2167    }
2168
2169    #[test]
2170    fn schema_six_state_with_an_empty_gate_migrates_to_gate_ran_false_and_is_retried() {
2171        // The exact shape of the stuck `shoka` run this schema bump fixes:
2172        // `verify.gate` empty, `gate` empty, schema 6. It must come back as
2173        // "not yet run" so the next `gate()` call re-attempts it — and for a
2174        // repo with no gate commands configured, that resolves instantly to
2175        // `PassedWithNoCommands` instead of staying stuck forever.
2176        let mut value = serde_json::to_value(state()).expect("serialize state");
2177        let object = value.as_object_mut().expect("state object");
2178        object.insert("schema".to_owned(), serde_json::json!(6));
2179        object.insert("gate".to_owned(), serde_json::json!([]));
2180        let old: RunState = serde_json::from_value(value).expect("schema-6 shape parses");
2181        let migrated = migrate_schema(old).expect("schema 6 migrates");
2182        assert_eq!(migrated.schema, SCHEMA);
2183        assert!(
2184            !migrated.gate_ran,
2185            "an empty gate on schema 6 is ambiguous and must be treated as unrun"
2186        );
2187        assert_eq!(migrated.gate_status(), GateStatus::NotRun);
2188    }
2189
2190    #[test]
2191    fn schema_six_serialization_is_rejected_by_a_schema_five_reader() {
2192        let body = serde_json::to_value(state()).expect("serialize state");
2193        assert_eq!(body["schema"], serde_json::json!(SCHEMA));
2194        assert_ne!(body["schema"], serde_json::json!(5));
2195    }
2196
2197    #[test]
2198    fn seat_started_and_finished_track_who_has_not_answered_yet() {
2199        let mut s = state();
2200        s.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
2201        s.seat_started("judge", "judge-2", std::time::Duration::from_secs(60), 0);
2202        assert_eq!(s.active.len(), 2, "both seats are still out");
2203
2204        s.seat_finished("judge-1");
2205        assert_eq!(
2206            s.active.keys().collect::<Vec<_>>(),
2207            vec!["judge-2"],
2208            "only the seat that answered drops out; judge-2 is still waited on"
2209        );
2210    }
2211
2212    #[test]
2213    fn a_retry_is_recorded_as_a_later_attempt_on_the_same_seat() {
2214        let mut s = state();
2215        s.seat_started("review", "review-2", std::time::Duration::from_secs(30), 0);
2216        s.seat_finished("review-2");
2217        // A nudge re-asks the same seat; attempt says this is not the first
2218        // time, which is the only trace a nudge otherwise leaves behind.
2219        s.seat_started("review", "review-2", std::time::Duration::from_secs(30), 1);
2220        assert_eq!(s.active["review-2"].attempt, 1);
2221    }
2222
2223    #[test]
2224    fn active_seat_reports_elapsed_and_remaining_time() {
2225        let now = Timestamp::now();
2226        let started = now - jiff::SignedDuration::from_secs(30);
2227        let seat = ActiveSeat {
2228            node: "judge".to_owned(),
2229            started_at: started,
2230            timeout_secs: 100,
2231            attempt: 0,
2232        };
2233        assert_eq!(seat.elapsed_secs(now), 30);
2234        assert_eq!(seat.remaining_secs(now), 70);
2235    }
2236
2237    #[test]
2238    fn remaining_time_never_goes_negative_past_the_timeout() {
2239        // `agy`'s own print-timeout occasionally overruns by a hair before the
2240        // kill lands; a naive subtraction would print a negative "time left".
2241        let now = Timestamp::now();
2242        let started = now - jiff::SignedDuration::from_secs(200);
2243        let seat = ActiveSeat {
2244            node: "implement".to_owned(),
2245            started_at: started,
2246            timeout_secs: 100,
2247            attempt: 1,
2248        };
2249        assert_eq!(seat.remaining_secs(now), 0);
2250    }
2251
2252    #[test]
2253    fn clear_active_drops_stale_seats_and_reports_whether_it_did() {
2254        let mut s = state();
2255        assert!(!s.clear_active(), "nothing to clear on a fresh run");
2256        s.seat_started(
2257            "implement",
2258            "impl-B",
2259            std::time::Duration::from_secs(3600),
2260            0,
2261        );
2262        assert!(s.clear_active(), "a leftover entry is reported as cleared");
2263        assert!(s.active.is_empty());
2264    }
2265
2266    #[test]
2267    fn active_seat_carries_nothing_that_could_be_read_as_output_bytes() {
2268        // `agy` prints exactly one JSON object, at the very end (see
2269        // `agent::dropped_stream`'s doc comment) — a seat can sit at zero
2270        // captured bytes for its whole timeout while working normally. So
2271        // `ActiveSeat` records only the wall-clock facts (when it started,
2272        // its budget, which attempt), never a byte count, which is what
2273        // keeps a reader from being able to build "0 bytes => dead" out of
2274        // it even by accident.
2275        let seat = ActiveSeat {
2276            node: "implement".to_owned(),
2277            started_at: Timestamp::now(),
2278            timeout_secs: 60,
2279            attempt: 0,
2280        };
2281        let value = serde_json::to_value(&seat).unwrap();
2282        let keys: std::collections::BTreeSet<String> =
2283            value.as_object().unwrap().keys().cloned().collect();
2284        assert_eq!(
2285            keys,
2286            std::collections::BTreeSet::from([
2287                "node".to_owned(),
2288                "started_at".to_owned(),
2289                "timeout_secs".to_owned(),
2290                "attempt".to_owned(),
2291            ]),
2292            "a byte count here would be a lever to declare a silent-but-healthy seat dead"
2293        );
2294    }
2295
2296    #[test]
2297    fn an_old_run_json_without_active_seats_still_loads() {
2298        // Schema did not bump for this field: an already-written run.json
2299        // simply lacks the key, and `#[serde(default)]` must fill it in
2300        // rather than fail the whole read.
2301        let s = state();
2302        let mut value = serde_json::to_value(&s).unwrap();
2303        value.as_object_mut().unwrap().remove("active");
2304        let back: RunState = serde_json::from_value(value).unwrap();
2305        assert!(back.active.is_empty());
2306        assert_eq!(back.schema, SCHEMA);
2307    }
2308
2309    #[test]
2310    fn an_old_run_json_without_jobs_still_loads() {
2311        // No schema bump for this field either, for the same reason: an
2312        // empty `jobs` list on an old record means exactly what it always
2313        // meant for that record — no adapter existed yet to report one —
2314        // and `#[serde(default)]` fills it in rather than failing the read.
2315        let s = state();
2316        let mut value = serde_json::to_value(&s).unwrap();
2317        value.as_object_mut().unwrap().remove("jobs");
2318        let back: RunState = serde_json::from_value(value).unwrap();
2319        assert!(back.jobs.is_empty());
2320        assert_eq!(back.schema, SCHEMA);
2321    }
2322
2323    #[test]
2324    fn ensure_can_delete_guards_live_and_unfolded_runs() {
2325        let mut s = state();
2326        // 1. A daemon is working on it right now.
2327        s.status = RunStatus::Prep;
2328        let err = s.ensure_can_delete(true).unwrap_err().to_string();
2329        assert!(err.contains("live daemon"), "{err}");
2330
2331        // 2. The same unfinished run with no daemon behind it is a leftover
2332        // from a killed process, and deletable. Without this an interrupted
2333        // run could never be removed: its status stays `prep` forever.
2334        assert!(s.ensure_can_delete(false).is_ok());
2335
2336        // 3. Unfolded candidates are refused either way — that is the guard
2337        // that stops a delete from discarding a worktree.
2338        s.status = RunStatus::Merged;
2339        s.candidates.push(Candidate {
2340            index: 0,
2341            label: 'A',
2342            agent: "a".to_owned(),
2343            branch: "b".to_owned(),
2344            worktree: PathBuf::from("/w"),
2345            summary: String::new(),
2346            stat: String::new(),
2347            files: 1,
2348            commits: 1,
2349            empty: false,
2350            failed: None,
2351            duration_ms: 0,
2352            folded: false,
2353        });
2354        let err = s.ensure_can_delete(false).unwrap_err().to_string();
2355        assert!(
2356            err.contains("magi fold"),
2357            "error must suggest `magi fold`: {err}"
2358        );
2359
2360        // 4. Folded and nobody working on it.
2361        s.candidates[0].folded = true;
2362        assert!(s.ensure_can_delete(false).is_ok());
2363    }
2364}