Skip to main content

magi/
daemon.rs

1//! The unattended loop: take the next task, run the graph, record what
2//! happened, take the next one.
3//!
4//! This is what turns magi from a command a human types into something an
5//! agent can hand work to. [`crate::queue`] is the mailbox; this module is the
6//! thing that empties it. Nothing here decides *how* a task is implemented —
7//! that is [`crate::graph`] — it only decides which task runs next, and what a
8//! finished run means for the task that produced it.
9//!
10//! # One run at a time, on purpose
11//!
12//! There is no `--jobs` flag and there will not be one. A single run is
13//! already internally parallel: candidates implement concurrently and judges
14//! rank concurrently, so the machine is not idle while one task is in flight.
15//! The real constraint is not CPU but the agent CLIs' quota, and two graphs at
16//! once doubles the burn rate on exactly the resource whose exhaustion produces
17//! [`RunStatus::Stalled`]. Serialising the loop is what keeps a full backlog
18//! from converting the whole day's quota into a pile of untrustworthy verdicts.
19//!
20//! # A crash is legible, and the loop notices on its own
21//!
22//! The task is written as [`crate::queue::TaskStatus::Running`], with its run
23//! id, *before* the graph starts, and is only rewritten once the run reaches a
24//! terminal status. A daemon killed mid-run therefore leaves the task
25//! `Running` and pointing at the run that was in flight. The alternative —
26//! reverting the task to `Queued` on the way out — would hide the abandoned
27//! run and re-spend its quota on the next poll.
28//!
29//! A task left `Running` forever is not the point, though:
30//! [`crate::queue::TaskStatus::runnable`] never offers it again, so a daemon
31//! that died mid-run would otherwise strand its task for good.
32//! [`reclaim_orphaned_running`] runs on every poll and settles exactly the
33//! tasks no live process is actually driving — proven by [`Queue::claim`]
34//! succeeding rather than by a staleness guess — against whatever their last
35//! run actually became, through the same [`settle`] a live finish uses. A run
36//! that genuinely cannot be read still holds its task for a human; the run's
37//! own report explains how far it got.
38//!
39//! # Retries are bounded
40//!
41//! Every attempt at a task consumes one of [`Opts::max_attempts`], after which
42//! the task is [`crate::queue::TaskStatus::Held`] for a human. The one
43//! exception is a run that ended `Stalled`: the panel collapsed because the
44//! agent CLIs hit their quota, which is a fact about the machine and not about
45//! the task, so it must not spend an attempt. Without that exception a quota
46//! outage would quietly hold the entire backlog, and the operator would come
47//! back to a reset quota and nothing left that the loop is willing to run.
48
49use std::path::{Path, PathBuf};
50use std::sync::Arc;
51use std::sync::atomic::{AtomicBool, Ordering};
52use std::sync::{Mutex, MutexGuard};
53use std::time::Duration;
54
55use anyhow::{Context, Result, bail};
56use jiff::Timestamp;
57use serde::{Deserialize, Serialize};
58use tokio::sync::Notify;
59
60use crate::ask::{self, Questions};
61use crate::clean;
62use crate::conduct::Conductor;
63use crate::config::{Config, MergeMode};
64use crate::graph::Runner;
65use crate::land;
66use crate::notices::{self, Link, Notice};
67use crate::queue::{Queue, Task, TaskStatus};
68use crate::run::{Liveness, QuotaLoss, RunState, RunStatus};
69use crate::triage;
70
71/// On-disk format for [`Status`]. Bumped when a field's meaning changes.
72pub const SCHEMA: u32 = 1;
73
74/// How often the status file is refreshed. A reader treats a status file older
75/// than [`STALE_SECS`] as "no daemon", so the heartbeat has to be brisk enough
76/// that a busy daemon is never mistaken for a dead one.
77pub const HEARTBEAT: Duration = Duration::from_secs(5);
78
79/// How old a heartbeat may be before a reader calls the daemon dead. Six
80/// missed beats: long enough to survive a slow filesystem, short enough that
81/// a crashed daemon is not still reported as running a task.
82///
83/// The single threshold every reader shares — the web UI's `/api/health` and
84/// `magi doctor` both call [`Reading::running`] rather than each comparing
85/// against their own copy of this number, so a crashed daemon cannot look
86/// alive on one screen and dead on another.
87pub const STALE_SECS: i64 = 30;
88
89/// Default queue poll interval.
90pub const POLL: Duration = Duration::from_secs(5);
91
92/// How old a claim has to be before startup sweeps it. Longer than any run
93/// this graph plausibly takes, so a sweep cannot pull a task out from under a
94/// daemon that is merely slow.
95pub const STALE_CLAIM: Duration = Duration::from_secs(6 * 60 * 60);
96
97/// How long a task may sit [`TaskStatus::Running`] with no live daemon's
98/// heartbeat naming it before [`crate::conduct`] is shown it as stalled.
99///
100/// [`reclaim_orphaned_running`] settles most crashes immediately, on every
101/// poll, by attempting the task's own claim: a dead pid is proof enough for
102/// [`sweep_stale_claims`] to drop the lock the same tick, and the very next
103/// claim attempt succeeds. But a lock whose pid cannot be parsed at all — an
104/// empty or corrupt `.lock` file — falls back to [`STALE_CLAIM`]'s six-hour
105/// age instead, since there is nothing else to check (see
106/// [`sweep_stale_claims`]'s own doc). For as long as that lock survives, the
107/// claim keeps failing and `reclaim_orphaned_running` correctly leaves the
108/// task `running` — see
109/// `stalled_tasks_still_reaches_a_task_reclaim_could_not_claim_yet` for
110/// exactly this ordering. `stalled_tasks` is what surfaces that task to the
111/// conductor well before the mechanical six-hour sweep would, and thirty
112/// minutes is comfortably below `STALE_CLAIM` while still being generous
113/// enough that a task merely late to publish its first [`HEARTBEAT`] is
114/// never mistaken for abandoned.
115pub const STALLED_RUNNING: Duration = Duration::from_secs(30 * 60);
116
117/// What the loop is working on, for the status file.
118#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
119#[serde(default)]
120pub struct Current {
121    /// Task id being run.
122    pub task: String,
123    /// Run id the task produced.
124    pub run: String,
125}
126
127/// The daemon's liveness, published to `<home>/daemon.json`.
128///
129/// This is the only interface between the loop and the web UI, which is why it
130/// carries `updated_at` as well as `started_at`: a reader cannot tell a
131/// running daemon from a `SIGKILL`ed one by the file's existence alone, but it
132/// can compare the heartbeat against the clock.
133#[derive(Debug, Clone, Serialize, Deserialize)]
134pub struct Status {
135    /// On-disk format version.
136    pub schema: u32,
137    /// Process id, so a human can find or kill the daemon.
138    pub pid: u32,
139    /// When this process started.
140    pub started_at: Timestamp,
141    /// Last heartbeat.
142    pub updated_at: Timestamp,
143    /// True when the queue has nothing runnable.
144    pub idle: bool,
145    /// Every task and run currently in flight. More than one entry means the
146    /// loop is driving more than one run at once — see
147    /// [`crate::config::Daemon::max_concurrent_runs`]. Empty, not absent, when
148    /// nothing is running, so a reader never has to treat "no field" and "an
149    /// empty list" as two different kinds of idle.
150    pub current: Vec<Current>,
151    /// Tasks that reached a terminal status in this process.
152    pub completed: usize,
153    /// Queue polls since start, so a wedged loop shows up as a frozen count.
154    pub polls: u64,
155}
156
157impl Status {
158    /// A fresh, idle status for this process.
159    #[must_use]
160    pub fn new() -> Self {
161        let now = Timestamp::now();
162        Self {
163            schema: SCHEMA,
164            pid: std::process::id(),
165            started_at: now,
166            updated_at: now,
167            idle: true,
168            current: Vec::new(),
169            completed: 0,
170            polls: 0,
171        }
172    }
173}
174
175impl Default for Status {
176    fn default() -> Self {
177        Self::new()
178    }
179}
180
181/// How the loop should behave.
182#[derive(Debug, Clone)]
183pub struct Opts {
184    /// Repository used by tasks that name none.
185    pub repo: PathBuf,
186    /// Explicit `magi.toml`, instead of the discovered layer stack.
187    pub config: Option<PathBuf>,
188    /// Queue poll interval.
189    pub poll: Duration,
190    /// Attempts a task gets before it is held for a human.
191    pub max_attempts: usize,
192    /// Drain what is runnable now, then return, instead of waiting for more.
193    pub once: bool,
194    /// Merge mode override (`none`, `local`, `pr`); `None` keeps the config's.
195    pub merge: Option<String>,
196    /// Where the janitor's [`crate::clean::fold_orphaned_worktrees`] and
197    /// [`crate::git::worktree_prune`] look for and reclaim worktrees.
198    /// `None` resolves to [`crate::run::default_worktree_root`] - the
199    /// operator's real `~/wt/<repo>` - the same way a run with no
200    /// [`crate::config::Graph::worktree_root`] resolves its own. A caller
201    /// that does not own that directory (a test, an embedding that manages
202    /// worktrees itself) must set this, or every idle tick reclaims worktrees
203    /// out from under whoever actually does.
204    pub worktrees_root: Option<PathBuf>,
205}
206
207impl Default for Opts {
208    fn default() -> Self {
209        Self {
210            repo: PathBuf::from("."),
211            config: None,
212            poll: POLL,
213            max_attempts: 2,
214            once: false,
215            merge: None,
216            worktrees_root: None,
217        }
218    }
219}
220
221/// How many runs a plain `usize` from config may drive concurrently, floored
222/// at one. A `0` in a config file would otherwise stall the loop entirely -
223/// no runnable task could ever start - which is never what an operator who
224/// wrote `0` meant.
225fn max_concurrent(n: usize) -> usize {
226    n.max(1)
227}
228
229/// Where the status file lives.
230#[must_use]
231pub fn status_path() -> PathBuf {
232    crate::run::home().join("daemon.json")
233}
234
235/// Publish the status file for this process.
236pub fn write_status(status: &Status) -> Result<()> {
237    write_status_to(&status_path(), status)
238}
239
240/// Publish a status to an explicit path.
241///
242/// Written to a sibling `.tmp` and renamed, because the web UI reads this file
243/// on every health poll and must never see a half-written one.
244pub fn write_status_to(path: &Path, status: &Status) -> Result<()> {
245    if let Some(parent) = path.parent() {
246        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
247    }
248    let body = serde_json::to_string_pretty(status).context("serialize daemon status")?;
249    let tmp = path.with_extension("json.tmp");
250    std::fs::write(&tmp, &body).with_context(|| format!("write {}", tmp.display()))?;
251    std::fs::rename(&tmp, path).with_context(|| format!("replace {}", path.display()))?;
252    Ok(())
253}
254
255/// Delete the status file. Called on the way out so a clean exit reads as
256/// "no daemon" rather than as a daemon whose heartbeat merely stopped.
257pub fn clear_status() {
258    clear_status_at(&status_path());
259}
260
261/// Delete a status file at an explicit path, so the loop's teardown and
262/// [`clear_status`] cannot drift apart: the loop is handed the path it
263/// published to, and a test can watch a temp file disappear.
264fn clear_status_at(path: &Path) {
265    let _ = std::fs::remove_file(path);
266}
267
268/// A cooperative stop, shared with whoever asked the loop to run.
269///
270/// Cloning is how the request travels: [`serve_until`] keeps one handle, the
271/// Ctrl-C listener and the web UI keep others, and every clone points at the
272/// same flag. There is no channel because there is nothing to send — the only
273/// message is "stop", it is idempotent, and a flag cannot be missed by a
274/// receiver that was not listening yet.
275///
276/// The handle also answers the question the operator's screen asks next: a
277/// stop does not take effect until the run in flight has finished, so
278/// [`Stop::finishing`] reports "asked to stop, still working" rather than
279/// leaving a caller to infer it from a heartbeat and hope.
280#[derive(Debug, Clone, Default)]
281pub struct Stop {
282    /// Set once, never cleared: a stop is not something an operator takes back
283    /// half way through, and a clearable flag would let a start racing a stop
284    /// resurrect a loop that is already unwinding.
285    stopped: Arc<AtomicBool>,
286    /// How many runs are in flight, so `finishing` can distinguish a stop
287    /// that has landed from one that is waiting on `execute`. A count, not a
288    /// flag, because more than one run can be in flight at once - see
289    /// [`crate::config::Daemon::max_concurrent_runs`] - and the last one to
290    /// finish is the one that should turn "finishing" off.
291    busy: Arc<std::sync::atomic::AtomicUsize>,
292    /// Wakes the idle wait. Without this a stop would not be seen until the
293    /// poll interval elapsed, and an operator tapping stop on a phone would
294    /// watch a button do nothing for five seconds.
295    wake: Arc<Notify>,
296    /// Handed to the run in flight, so a stop can also mean "park at the next
297    /// node boundary" instead of "finish the whole competition first".
298    pause: crate::graph::Pause,
299}
300
301impl Stop {
302    /// A stop nobody has asked for yet.
303    #[must_use]
304    pub fn new() -> Self {
305        Self::default()
306    }
307
308    /// Ask the loop to stop. Idempotent, and safe to call before the loop
309    /// starts: the flag is checked before the first poll.
310    pub fn stop(&self) {
311        self.stopped.store(true, Ordering::SeqCst);
312        // `notify_one` rather than `notify_waiters` because the loop may not be
313        // parked yet: this stores a permit, so a wait that registers a moment
314        // later returns at once instead of sleeping out the whole interval.
315        self.wake.notify_one();
316    }
317
318    /// Has a stop been asked for?
319    #[must_use]
320    pub fn stopped(&self) -> bool {
321        self.stopped.load(Ordering::SeqCst)
322    }
323
324    /// Has a stop been asked for that has not taken effect yet, because a run
325    /// is still in flight?
326    ///
327    /// This is the state a screen has to be able to show. A stop never abandons
328    /// a run — see [`serve_until`] — so between the tap and the loop's return
329    /// there is a window of tens of minutes in which "running" and "stopped"
330    /// are both misleading answers.
331    #[must_use]
332    pub fn finishing(&self) -> bool {
333        self.stopped() && self.busy_now()
334    }
335
336    /// Ask the loop to stop *and* the run in flight to park at its next node
337    /// boundary.
338    ///
339    /// The plain [`Stop::stop`] never abandons a run, which is right when the
340    /// operator only wants the queue to drain: a competition is tens of
341    /// minutes and its worktrees are paid for. But an operator who wants to
342    /// replace the binary cannot wait out a run that has an hour left, and
343    /// killing the process loses whatever the seats in flight had not written.
344    /// Parking costs at most the node in progress and leaves the run
345    /// resumable.
346    pub fn park(&self, cause: crate::run::ParkCause) {
347        self.pause.park_for(cause);
348        self.stop();
349    }
350
351    /// Has a park been asked for?
352    #[must_use]
353    pub fn parking(&self) -> bool {
354        self.pause.parked()
355    }
356
357    /// The pause handle to give a runner.
358    #[must_use]
359    pub fn pause(&self) -> crate::graph::Pause {
360        self.pause.clone()
361    }
362
363    /// Is any run in flight right now?
364    ///
365    /// `finishing` answers "a stop is waiting on a run", which is false until
366    /// someone asks to stop. An upgrade needs the plain question, because it
367    /// is about to be the one asking.
368    #[must_use]
369    pub fn busy_now(&self) -> bool {
370        self.busy.load(Ordering::SeqCst) > 0
371    }
372
373    /// Mark one more run as in flight, for [`Stop::finishing`].
374    fn enter(&self) {
375        self.busy.fetch_add(1, Ordering::SeqCst);
376    }
377
378    /// Mark one run as finished. The last one out is what makes
379    /// [`Stop::busy_now`] false again.
380    fn exit(&self) {
381        self.busy.fetch_sub(1, Ordering::SeqCst);
382    }
383
384    /// Wait out one poll interval, returning early once a stop is asked for.
385    async fn idle(&self, poll: Duration) {
386        tokio::select! {
387            () = tokio::time::sleep(poll) => {}
388            () = self.wake.notified() => {}
389        }
390    }
391}
392
393/// The daemon's published state, read permissively.
394///
395/// This mirrors [`Status`], but is a separate declaration on purpose: every
396/// field defaults, so a status file from an older or newer magi still yields
397/// a usable reading — one this build has never heard of — instead of a parse
398/// error that hides the daemon entirely.
399#[derive(Debug, Clone, Default, Deserialize)]
400#[serde(default)]
401pub struct Reading {
402    /// Format version the daemon claims.
403    pub schema: u32,
404    /// Daemon process id, for an operator who wants to stop it.
405    pub pid: Option<u32>,
406    /// When that process started.
407    pub started_at: Option<Timestamp>,
408    /// Last heartbeat. Absent means the file is unusable, hence not running.
409    pub updated_at: Option<Timestamp>,
410    /// True when the queue had nothing runnable at the last poll.
411    pub idle: bool,
412    /// What the daemon is working on. Empty means idle; more than one entry
413    /// means more than one run is in flight at once.
414    ///
415    /// `deserialize_with` rather than the plain derive: a daemon started
416    /// before this field became a list is still out there writing the old
417    /// shape — a single `{"task":...,"run":...}` object, or its absence —
418    /// on every heartbeat until it is restarted, and a live process reading
419    /// that file during the rollout must still see it as running rather than
420    /// as absent. A bare type change here would fail the whole struct's
421    /// deserialization on a type mismatch, defeating the permissiveness this
422    /// type exists for.
423    #[serde(deserialize_with = "de_current")]
424    pub current: Vec<Current>,
425    /// Tasks this daemon process has finished.
426    pub completed: u64,
427    /// Queue polls this daemon process has made.
428    pub polls: u64,
429}
430
431/// Accept the old single-`Current`-or-absent shape as well as the current
432/// list, so a reader never has to know which build wrote the file.
433fn de_current<'de, D>(deserializer: D) -> std::result::Result<Vec<Current>, D::Error>
434where
435    D: serde::Deserializer<'de>,
436{
437    #[derive(Deserialize)]
438    #[serde(untagged)]
439    enum Shape {
440        Many(Vec<Current>),
441        One(Current),
442    }
443    Ok(
444        Option::<Shape>::deserialize(deserializer)?.map_or_else(Vec::new, |shape| match shape {
445            Shape::Many(v) => v,
446            Shape::One(c) => vec![c],
447        }),
448    )
449}
450
451impl Reading {
452    /// Seconds since the last heartbeat, or `None` when there has never been
453    /// one.
454    #[must_use]
455    pub fn age_secs(&self, now: Timestamp) -> Option<i64> {
456        self.updated_at
457            .map(|at| (now.as_second() - at.as_second()).max(0))
458    }
459
460    /// Whether the loop counts as running: a heartbeat no older than
461    /// [`STALE_SECS`]. The alternative is a reader that claims a task is in
462    /// progress hours after the daemon that owned it was killed.
463    #[must_use]
464    pub fn running(&self, now: Timestamp) -> bool {
465        self.age_secs(now).is_some_and(|secs| secs <= STALE_SECS)
466    }
467}
468
469/// Read `<home>/daemon.json` permissively, or `None` when there is nothing
470/// usable there.
471///
472/// Missing, half-written and unparseable all collapse to `None`, because the
473/// only question a reader asks is whether a daemon is alive, and a file it
474/// cannot read is not evidence that one is.
475#[must_use]
476pub fn read_status(home: &Path) -> Option<Reading> {
477    let body = std::fs::read_to_string(home.join("daemon.json")).ok()?;
478    serde_json::from_str(&body).ok()
479}
480
481/// Every run a live daemon is working on right now.
482///
483/// One definition of liveness, because deleting a task and deleting a run are
484/// both gated on it from both the CLI and the web UI - four callers that must
485/// never disagree about whether the same thing is in flight. A stale heartbeat
486/// reads as "no daemon": that is [`Reading::running`]'s judgement, and a task
487/// left at `running` or a run left at `implementing` by a killed daemon is a
488/// leftover record rather than work in progress. More than one entry once
489/// [`crate::config::Daemon::max_concurrent_runs`] is more than one - a caller
490/// after "the one thing in flight" wants [`is_working_on`] or
491/// [`is_working_on_task`], not this directly.
492#[must_use]
493pub fn current_work(home: &Path, now: Timestamp) -> Vec<Current> {
494    read_status(home)
495        .filter(|reading| reading.running(now))
496        .map(|reading| reading.current)
497        .unwrap_or_default()
498}
499
500/// Whether a live daemon is working on this run at this moment.
501#[must_use]
502pub fn is_working_on(home: &Path, run: &str, now: Timestamp) -> bool {
503    current_work(home, now).iter().any(|c| c.run == run)
504}
505
506/// Whether a live daemon is working on a run whose short id is this one.
507///
508/// For a worktree that has no run record to compare against at all -
509/// [`crate::clean::fold_orphaned_worktrees`]'s whole reason to exist - a full
510/// id is not available to hand to [`is_working_on`]. The short id is: a run's
511/// worktree bay is named after it (see [`crate::run::RunState::worktree_root`]),
512/// and it is exactly the gap between the daemon claiming a task and
513/// `RunState::new` saving the first `run.json` that this exists to protect -
514/// a run genuinely in flight but invisible to a scan of `runs/`.
515#[must_use]
516pub fn is_working_on_short(home: &Path, short: &str, now: Timestamp) -> bool {
517    current_work(home, now)
518        .iter()
519        .any(|c| crate::run::short_of(&c.run) == short)
520}
521
522/// Whether a live daemon is working on this task at this moment.
523#[must_use]
524pub fn is_working_on_task(home: &Path, task: &str, now: Timestamp) -> bool {
525    current_work(home, now).iter().any(|c| c.task == task)
526}
527
528/// Remove claim files whose owner is provably dead, or that have simply
529/// outlived `older_than`, and return the task ids swept.
530///
531/// A daemon killed with `SIGKILL` never runs [`crate::queue::Claim`]'s
532/// destructor, and the orphaned `.lock` file would make its task permanently
533/// unclaimable — the backlog would stop for good at exactly the task that was
534/// in flight when the machine went down.
535///
536/// The pid recorded in the lock is the authority whenever it can be read at
537/// all; age is only a fallback for when it cannot be.
538///
539/// - **A parseable pid wins outright.** [`crate::proc::pid_alive`] decides,
540///   full stop — dead sweeps the lock immediately, regardless of age; alive
541///   protects it, regardless of age. This is what lets a lock be reclaimed in
542///   seconds instead of waiting out [`STALE_CLAIM`]: a lock made 33 minutes
543///   before this daemon even started, next to a `queued` task, no longer has
544///   to sit for six hours before anything notices its owner is gone.
545/// - **A pid that cannot be parsed at all** — an empty or corrupt lock file —
546///   falls back to `older_than`, since there is nothing else to check.
547///
548/// Age must never override a *positive* liveness confirmation. `sweep`
549/// [`poll`]s concurrently with every attempt this daemon itself has spawned —
550/// see [`InFlightGuard`] — not only between them the way a single sequential
551/// loop once did, so a run that legitimately runs longer than `older_than`
552/// (a multi-round review, a long land wait carried across several resumed
553/// attempts) still has this very process's own live pid sitting in its own
554/// lock file on every later sweep. Deciding by age alone in that case would
555/// delete this daemon's own still-valid claim on its own in-flight task,
556/// which [`reclaim_orphaned_running`] would then read as abandoned and hand
557/// to a second attempt — two `Runner`s writing the same `run.json` and the
558/// same worktree at once. `pid_alive` answering "alive" for anything it
559/// cannot determine (a live process, a pid this build cannot check, one
560/// under another account) is exactly what keeps that path from ever
561/// firing on a guess.
562///
563/// [`STALE_CLAIM`] itself stays large: a helper program missing or its
564/// output unreadable must not be license to guess, and the risk of an
565/// unparseable lock outliving a genuinely dead owner is bounded by an order
566/// of magnitude above any plausible run rather than by a positive check.
567///
568/// Runs on every poll, not only at startup — a daemon up for days must keep
569/// noticing a lock some other, now-dead, daemon left behind just as readily
570/// as one it trips over on the way up.
571pub fn sweep_stale_claims(queue: &Queue, older_than: Duration) -> Vec<String> {
572    sweep_stale_claims_with(queue, older_than, crate::proc::pid_alive)
573}
574
575/// [`sweep_stale_claims`] with its process-query boundary supplied by the
576/// caller. This keeps the lock policy testable where process listing is
577/// unavailable, while production still uses the platform query above.
578fn sweep_stale_claims_with<F>(queue: &Queue, older_than: Duration, pid_alive: F) -> Vec<String>
579where
580    F: Fn(u32) -> bool,
581{
582    let this_process = std::process::id();
583    let mut swept: Vec<String> = std::fs::read_dir(queue.root())
584        .into_iter()
585        .flatten()
586        .flatten()
587        .map(|e| e.path())
588        .filter(|p| p.extension().is_some_and(|x| x == "lock"))
589        .filter(|p| {
590            match std::fs::read_to_string(p)
591                .ok()
592                .and_then(|body| body.trim().parse::<u32>().ok())
593            {
594                // This process wrote it and is asking the question right
595                // now, so it is definitionally still alive - settled without
596                // spawning a helper process at all.
597                Some(pid) if pid == this_process => false,
598                Some(pid) => !pid_alive(pid),
599                None => p
600                    .metadata()
601                    .and_then(|m| m.modified())
602                    .and_then(|t| t.elapsed().map_err(std::io::Error::other))
603                    .is_ok_and(|age| age >= older_than),
604            }
605        })
606        .filter(|p| std::fs::remove_file(p).is_ok())
607        .filter_map(|p| {
608            p.file_stem()
609                .and_then(|s| s.to_str())
610                .map(std::borrow::ToOwned::to_owned)
611        })
612        .collect();
613    swept.sort_unstable();
614    swept
615}
616
617/// Is `task` stalled: [`TaskStatus::Running`], past [`STALLED_RUNNING`], with
618/// no live daemon's heartbeat naming it? Deterministic — no model call, and
619/// the exact test [`stalled_tasks`] uses to decide what `crate::conduct` is
620/// shown.
621fn is_stalled(task: &Task, home: &Path, now: Timestamp) -> bool {
622    task.status == TaskStatus::Running
623        && (now.as_second() - task.updated_at.as_second()) >= STALLED_RUNNING.as_secs() as i64
624        && !is_working_on_task(home, &task.id, now)
625}
626
627/// Every task [`is_stalled`] right now — "止まったタスク" in
628/// `crate::conduct`'s vocabulary.
629fn stalled_tasks(queue: &Queue, home: &Path, now: Timestamp) -> Vec<Task> {
630    queue
631        .list()
632        .into_iter()
633        .filter(|t| is_stalled(t, home, now))
634        .collect()
635}
636
637/// Runnable tasks a dependency can still be set on — "runnable なタスク" in
638/// `crate::conduct`'s vocabulary. Deliberately `Queued` only, not
639/// `Failed`-and-so-also-runnable: a task that already attempted and lost
640/// belongs in [`finished_tasks`], where the question is a recovery, not a
641/// dependency.
642fn queued_tasks(queue: &Queue) -> Vec<Task> {
643    queue
644        .list()
645        .into_iter()
646        .filter(|t| t.status == TaskStatus::Queued)
647        .collect()
648}
649
650/// `Failed`/`Held` tasks nobody has decided a recovery for yet — "終わった
651/// タスク" in `crate::conduct`'s vocabulary.
652fn finished_tasks(queue: &Queue) -> Vec<Task> {
653    queue
654        .list()
655        .into_iter()
656        .filter(|t| matches!(t.status, TaskStatus::Failed | TaskStatus::Held))
657        .collect()
658}
659
660/// Deterministically resolve `Task::blocked_by`: a dependency task that
661/// reached `Done`, or a question that was answered, is removed — no model
662/// involved, on every poll. An answered question's content is copied onto
663/// the task ([`Task::record_answer`]) before its id is dropped, so it
664/// reaches the next `crate::conduct` prompt and the next run's instruction
665/// (see [`instruction_for`]) rather than only clearing the block.
666///
667/// A `blocked_by` id that names neither an existing task nor an existing
668/// question - `magi task rm` (or an operator by hand) deleted it while this
669/// task was still waiting - is caught before any of that: it can never
670/// become `Done` or `Answered`, so the ordinary loop below would otherwise
671/// leave the task `blocked` forever with nothing to notice. Such a task is
672/// quarantined to a machine hold instead ([`crate::queue::missing_blocker_hold_reason`]),
673/// which puts it in front of `crate::triage::run_once`'s own walk the next
674/// time it runs - see that module's doc for why the choice is "ask a human",
675/// never "assume the missing dependency was satisfied and unblock anyway".
676fn resolve_blockers(queue: &Queue, questions: &Questions) {
677    for listed in queue.list() {
678        if listed.status != TaskStatus::Blocked || listed.blocked_by.is_empty() {
679            continue;
680        }
681        let Ok(_claim) = queue.claim(&listed.id) else {
682            continue;
683        };
684        let Ok(mut task) = queue.get(&listed.id) else {
685            continue;
686        };
687        if task.status != TaskStatus::Blocked {
688            continue;
689        }
690        // A dependency deleted on purpose is released, not held - and before
691        // the missing check, so a pass that races the delete (or a claim that
692        // made `Queue::remove` skip this task) settles it quietly.
693        let deleted = queue.apply_deleted_blockers(&mut task);
694        if !deleted.is_empty() {
695            record(queue, &mut task);
696            for id in &deleted {
697                queue.note_dependency_deleted(&task, id);
698            }
699            if task.status != TaskStatus::Blocked {
700                continue;
701            }
702        }
703        let missing = crate::queue::missing_blockers(queue, questions, &task.blocked_by);
704        if !missing.is_empty() {
705            let language = language_of(&task, Path::new("."));
706            task.hold_machine(Some(crate::queue::missing_blocker_hold_reason_in(
707                &task.blocked_by,
708                &missing,
709                &language,
710            )));
711            record(queue, &mut task);
712            continue;
713        }
714        // A conductor question nobody answered in time is retired by the
715        // waiter, and nothing else would ever move the task it blocks.
716        if let Some(q) = task.blocked_by.iter().find_map(|id| {
717            questions.get(id).ok().filter(|q| {
718                q.node == crate::conduct::NODE && q.status == ask::QuestionStatus::Abandoned
719            })
720        }) {
721            let language = language_of(&task, Path::new("."));
722            task.hold_machine(Some(unanswered_question_hold_reason(&q, &language)));
723            record(queue, &mut task);
724            continue;
725        }
726        let mut changed = false;
727        for id in task.blocked_by.clone() {
728            if let Ok(dep) = queue.get(&id) {
729                if dep.status == TaskStatus::Done {
730                    task.unblock(&id);
731                    changed = true;
732                }
733                continue;
734            }
735            if let Ok(q) = questions.get(&id)
736                && q.status == ask::QuestionStatus::Answered
737            {
738                let answer = match &q.answer {
739                    Some(ask::Answer::Choice(c) | ask::Answer::Text(c)) => c.clone(),
740                    None => String::new(),
741                };
742                task.record_answer(q.summary.clone(), answer);
743                task.unblock(&id);
744                changed = true;
745            }
746        }
747        if changed {
748            record(queue, &mut task);
749        }
750    }
751}
752
753/// The `hold_reason` of a task whose conductor question went unanswered.
754fn unanswered_question_hold_reason(q: &ask::Question, language: &str) -> String {
755    if crate::lang::is_japanese(language) {
756        format!(
757            "質問 {} 「{}」 に期限内の回答がなく、取り下げられました - `magi task triage` を参照",
758            q.short(),
759            q.summary
760        )
761    } else {
762        format!(
763            "question {} \"{}\" went unanswered and was abandoned - see `magi task triage`",
764            q.short(),
765            q.summary
766        )
767    }
768}
769
770/// Where `q`'s chosen action stands for `task`. `daemon_will_act` is false
771/// for several unrelated reasons, and the waiter must not read them alike.
772#[derive(Debug, Clone, Copy, PartialEq, Eq)]
773pub(crate) enum ActionStanding {
774    /// The answer carries no action: the waiter delivers it as before.
775    NoAction,
776    /// The daemon will apply it (not applied, task idle, current attempt).
777    Pending,
778    /// Already applied (`Task::actions_applied`): the daemon has acted, so
779    /// delivering the word to the old seat would run it beside the new work.
780    Applied,
781    /// The question is about an earlier attempt than the task's latest.
782    Stale,
783    /// The task is running / blocked / done: the daemon defers for now.
784    Busy,
785}
786
787impl ActionStanding {
788    /// The daemon has acted or will act: the waiter must not deliver.
789    pub(crate) fn daemon_owns(self) -> bool {
790        matches!(self, Self::Pending | Self::Applied | Self::Stale)
791    }
792}
793
794/// Classify `q`'s chosen action against `task`. `Stale` is judged before
795/// `Busy`: a question about an earlier attempt must be stopped even while a
796/// newer competition is running.
797pub(crate) fn action_standing(task: &Task, q: &ask::Question) -> ActionStanding {
798    if q.chosen_action().is_none() {
799        ActionStanding::NoAction
800    } else if task.action_applied(&q.id) {
801        ActionStanding::Applied
802    } else if q.node != crate::conduct::NODE && task.runs.last() != Some(&q.run) {
803        ActionStanding::Stale
804    } else if matches!(
805        task.status,
806        TaskStatus::Running | TaskStatus::Blocked | TaskStatus::Done
807    ) {
808        ActionStanding::Busy
809    } else {
810        ActionStanding::Pending
811    }
812}
813
814/// What [`decide_action`] concluded about one answered question.
815#[derive(Debug, Clone, PartialEq, Eq)]
816enum ActionDecision {
817    /// Nothing to do now (no action, already applied, or the task is busy).
818    Skip,
819    /// Continue the named run instead of competing again.
820    Resume(String),
821    /// Requeue as a fresh competition.
822    Requeue,
823    /// Close the task as done.
824    Done,
825    /// The question is about an earlier attempt than the task's latest; it
826    /// is recorded as handled and changes nothing.
827    Stale,
828    /// The action cannot be carried out; hold the task and say why. Never
829    /// falls back to something else, because the operator chose this one.
830    Refuse(String),
831}
832
833/// Decide what the choice answered on `q` means for `task`. Pure: `load`
834/// reads a run's state, as with [`unfinished_run_with`].
835///
836/// `Resume` only fires on the task's *latest* run, and only while that run
837/// could actually make progress ([`RunStatus::resumable`], not released, review
838/// budget left); an answer naming anything else is refused rather than
839/// reinterpreted as a fresh competition.
840fn decide_action<F>(task: &Task, q: &ask::Question, p: &Phrases, load: F) -> ActionDecision
841where
842    F: FnOnce(&str) -> Result<RunState>,
843{
844    let Some(action) = q.chosen_action() else {
845        return ActionDecision::Skip;
846    };
847    if task.action_applied(&q.id)
848        || matches!(
849            task.status,
850            TaskStatus::Running | TaskStatus::Blocked | TaskStatus::Done
851        )
852    {
853        return ActionDecision::Skip;
854    }
855    // A question about an earlier attempt must not act on a newer one. A
856    // conductor question is keyed by the task id, not a run, so it is exempt.
857    if q.node != crate::conduct::NODE && task.runs.last() != Some(&q.run) {
858        return ActionDecision::Stale;
859    }
860    let repair = q.node == land::APPROVAL_NODE && q.resolution().as_deref() == Some(land::REPAIR);
861    // Repair is an explicit new decision on an existing approval card. A
862    // manual hold before that answer must not discard it just because the
863    // card predates the hold. Keep the older policy for other choice actions.
864    let decision_at = if repair {
865        q.answered_at.unwrap_or(q.asked_at)
866    } else {
867        q.asked_at
868    };
869    if task.operator_decided_at.is_some_and(|at| at > decision_at) {
870        return if repair {
871            ActionDecision::Refuse(format!(
872                "repair answer {} was superseded by a later operator decision; the task stays held. Inspect `magi show {}`; if repair is still wanted, explicitly release the existing owner with `magi task release {}`",
873                q.id, q.run, task.id
874            ))
875        } else {
876            ActionDecision::Stale
877        };
878    }
879    match action {
880        ask::ChoiceAction::Requeue => ActionDecision::Requeue,
881        ask::ChoiceAction::Done => ActionDecision::Done,
882        ask::ChoiceAction::Resume { run } => {
883            if task.runs.last() != Some(run) {
884                return ActionDecision::Refuse((p.resume_not_latest)(
885                    q.short(),
886                    ask::short_id(run),
887                ));
888            }
889            match load(run) {
890                Ok(s)
891                    if q.node == land::APPROVAL_NODE
892                        && q.resolution().as_deref() == Some(land::REPAIR) =>
893                {
894                    if s.merge_repairs.iter().any(|r| r.question == q.id)
895                        || s.land_approval.as_ref().is_some_and(|a| a.question != q.id)
896                    {
897                        // A live/manual driver already consumed or superseded
898                        // this answer. Do not hold its fresh approval wait.
899                        ActionDecision::Stale
900                    } else if s.liveness(false) == Liveness::Live {
901                        ActionDecision::Skip
902                    } else if s.status != RunStatus::Landing
903                        || s.released()
904                        || !land::repair_available(&s)
905                        || !implementers_match(task, &s)
906                        || !s
907                            .land_approval
908                            .as_ref()
909                            .is_some_and(|a| a.question == q.id && !a.head.is_empty())
910                    {
911                        ActionDecision::Refuse(format!(
912                            "repair question {} no longer names a supported current approval on run {}; inspect `magi show {}` and restore the existing owner's approval before retrying; do not file another task",
913                            q.id, run, run
914                        ))
915                    } else {
916                        ActionDecision::Resume(run.clone())
917                    }
918                }
919                Ok(s) if s.status.resumable() && !s.released() && !exhausted_review_budget(&s) => {
920                    ActionDecision::Resume(run.clone())
921                }
922                Ok(_) => ActionDecision::Refuse((p.resume_cannot_progress)(
923                    q.short(),
924                    ask::short_id(run),
925                )),
926                Err(e) => ActionDecision::Refuse((p.resume_unreadable)(
927                    q.short(),
928                    ask::short_id(run),
929                    &format!("{e:#}"),
930                )),
931            }
932        }
933    }
934}
935
936/// The fixed prose the daemon writes into [`Task::last_error`] and
937/// [`Task::hold_reason`], in the language of `[graph] language`. Same shape as
938/// `triage::Wording` and `land::Words`: only English and Japanese are
939/// translated, anything else falls back to English.
940///
941/// Rendered when the reason is *generated*, so a reason keeps the language it
942/// was written in if the setting changes later. The notification frame
943/// (`Task <id> is held:`) is not here and stays English.
944///
945/// Deliberately **not** translated, because other code reads them back by
946/// string: the disk gate prefixes (`triage::is_disk_hold`), `out of attempts`
947/// and `gate red`. IDs, node names, paths and external error text are embedded
948/// as they are.
949struct Phrases {
950    /// `{}` = status label, then the detail.
951    graph_stopped: fn(&str, &str) -> String,
952    quorum_lost: &'static str,
953    /// Followed by the seats' names.
954    quota_took_out: &'static str,
955    /// Followed by the status label.
956    run_ended: &'static str,
957    /// Followed by the question's short id.
958    waiting_for_answer: &'static str,
959    recovered_running: &'static str,
960    no_run_to_recover: &'static str,
961    could_not_start: &'static str,
962    resume_not_latest: fn(&str, &str) -> String,
963    resume_cannot_progress: fn(&str, &str) -> String,
964    resume_unreadable: fn(&str, &str, &str) -> String,
965    /// A takeover refusal: the operator-facing text. English is the error's own
966    /// `Display`; a translation keeps the machine detail apart in parentheses.
967    handover_refused: fn(&crate::handover::Refused) -> String,
968    /// Appended to a refused handover's hold reason (leading space included).
969    handover_hint: &'static str,
970    /// (seats that never answered, review rounds) for the review loop's
971    /// "refusing to call it clean" stop.
972    reviewers_never_answered: fn(usize, usize) -> String,
973}
974
975const PHRASES_EN: Phrases = Phrases {
976    graph_stopped: |status, detail| {
977        format!("the graph stopped at `{status}` without reaching a terminal status: {detail}")
978    },
979    quorum_lost: "the judging panel lost its quorum",
980    quota_took_out: "; quota took out ",
981    run_ended: "run ended ",
982    waiting_for_answer: " - waiting for operator answer to question ",
983    recovered_running: "recovered a `running` task whose daemon never recorded the outcome: ",
984    no_run_to_recover: "task was `running` with no live daemon and no readable \
985                        run to recover; held for a human to check what happened",
986    could_not_start: "could not start the run: ",
987    resume_not_latest: |q, run| {
988        format!("question {q} asked to resume run {run}, which is not this task's latest run")
989    },
990    resume_cannot_progress: |q, run| {
991        format!("question {q} asked to resume run {run}, which cannot make progress")
992    },
993    resume_unreadable: |q, run, e| {
994        format!("question {q} asked to resume run {run}, which could not be read: {e}")
995    },
996    handover_refused: |r| r.to_string(),
997    handover_hint: " (clean up the other worktree, then release the task from the queue)",
998    reviewers_never_answered: |missing, rounds| {
999        format!(
1000            "{missing} reviewer seat(s) never answered after {rounds} rounds; \
1001             refusing to call it clean"
1002        )
1003    },
1004};
1005
1006const PHRASES_JA: Phrases = Phrases {
1007    graph_stopped: |status, detail| {
1008        format!("グラフが終端状態に達しないまま `{status}` で停止しました: {detail}")
1009    },
1010    quorum_lost: "審査パネルが定足数を失いました",
1011    quota_took_out: "。クォータで脱落: ",
1012    run_ended: "run 終了: ",
1013    waiting_for_answer: " - オペレーターの回答待ち: 質問 ",
1014    recovered_running: "daemon が結果を記録しないまま `running` だったタスクを回収しました: ",
1015    no_run_to_recover: "タスクは `running` でしたが、生きた daemon も回収できる run も見つかりません。\
1016                        何が起きたか人が確認するため保留にしました",
1017    could_not_start: "run を開始できませんでした: ",
1018    resume_not_latest: |q, run| {
1019        format!(
1020            "質問 {q} は run {run} の再開を求めましたが、これはタスクの最新の run ではありません"
1021        )
1022    },
1023    resume_cannot_progress: |q, run| {
1024        format!("質問 {q} は run {run} の再開を求めましたが、これは進行できません")
1025    },
1026    resume_unreadable: |q, run, e| {
1027        format!("質問 {q} は run {run} の再開を求めましたが、読み込めませんでした: {e}")
1028    },
1029    handover_refused: |r| {
1030        use crate::handover::Refused;
1031        match r {
1032            Refused::Foreign { branch, path, why } => format!(
1033                "ブランチ `{branch}` は {path} にチェックアウトされており、magi は自動では\
1034                 削除しません。不要なら `git worktree remove` でその worktree を削除して\
1035                 から、やり直してください(詳細: {why})"
1036            ),
1037            Refused::Unsafe { branch, path, why } => format!(
1038                "ブランチ `{branch}` は {path} にチェックアウトされています。そこの作業を\
1039                 コミットか破棄したうえで `git worktree remove` で worktree を削除する\
1040                 か、run を破棄してよいと伝えてから、やり直してください(詳細: {why})"
1041            ),
1042            Refused::ReleaseFailed { branch, path, run } => format!(
1043                "ブランチ `{branch}` は {path} で run {run} がチェックアウトしており、その\
1044                 worktree の解放に失敗したか、変更が見つかりました(未コミットの変更が\
1045                 ある worktree は git が削除を拒否します)。worktree はそのまま残しました"
1046            ),
1047        }
1048    },
1049    handover_hint: "(他の worktree を片付けてから、タスクをキューから解放してください)",
1050    reviewers_never_answered: |missing, rounds| {
1051        format!(
1052            "{missing} 席のレビュアーが {rounds} ラウンドの間に一度も回答しなかったため、\
1053             クリーンとは認めません"
1054        )
1055    },
1056};
1057
1058fn phrases(language: &str) -> &'static Phrases {
1059    if crate::lang::is_japanese(language) {
1060        &PHRASES_JA
1061    } else {
1062        &PHRASES_EN
1063    }
1064}
1065
1066/// The language of the repository a task belongs to, read best-effort for the
1067/// paths that have no run (and so no config of their own) to ask. An unreadable
1068/// config is English, not an error: a hold reason must still get written.
1069fn language_of(task: &Task, fallback: &Path) -> String {
1070    crate::lang::of_repo(&repo_for(task, fallback))
1071}
1072
1073/// The task an answered question speaks for: the one whose runs include the
1074/// question's run, or - for a conductor question, filed under the task's own
1075/// id - the task with that id.
1076pub(crate) fn task_of_question<'a>(tasks: &'a [Task], q: &ask::Question) -> Option<&'a Task> {
1077    if q.node == crate::conduct::NODE {
1078        return tasks.iter().find(|t| t.id == q.run);
1079    }
1080    tasks.iter().find(|t| t.runs.contains(&q.run))
1081}
1082
1083/// Carry out the [`ask::ChoiceAction`] attached to each answered choice, once.
1084///
1085/// Runs on every poll before the conductor looks at the queue. The task is
1086/// re-read under its claim, so a task somebody completed or held by hand in
1087/// the meantime is judged on what it is now. `Resume` releases the task and
1088/// records a pinned [`crate::queue::OperatorResume`], which `crate::conduct`
1089/// honours (no re-hold, no requeue) until the task runs and [`attempt`]'s
1090/// ordinary `unfinished_run` logic resumes that same run.
1091pub(crate) fn apply_choice_actions(queue: &Queue, questions: &Questions, home: &Path) {
1092    let tasks = queue.list();
1093    for q in questions.list() {
1094        if q.chosen_action().is_none() {
1095            continue;
1096        }
1097        let Some(listed) = task_of_question(&tasks, &q) else {
1098            if q.node == land::APPROVAL_NODE && q.resolution().as_deref() == Some(land::REPAIR) {
1099                notices::raise_in(
1100                    home,
1101                    Notice::warn(
1102                        &format!("repair-owner:{}", q.id),
1103                        format!(
1104                            "Repair is waiting: no task owns run {}. Inspect `magi show {}` and restore its original queue owner; do not file a competing repair task.",
1105                            q.run, q.run
1106                        ),
1107                    ),
1108                );
1109            }
1110            continue;
1111        };
1112        if listed.action_applied(&q.id) {
1113            continue;
1114        }
1115        let Ok(_claim) = queue.claim(&listed.id) else {
1116            continue;
1117        };
1118        let Ok(mut task) = queue.get(&listed.id) else {
1119            continue;
1120        };
1121        let language = language_of(&task, Path::new("."));
1122        let decision = decide_action(&task, &q, phrases(&language), |id| {
1123            RunState::load_under(id, home)
1124        });
1125        let ran = matches!(
1126            decision,
1127            ActionDecision::Resume(_) | ActionDecision::Requeue | ActionDecision::Done
1128        );
1129        if ran && asker_may_still_read(questions, &q, home) {
1130            // The asking process or its seat can still take the word; act
1131            // once it has gone quiet. Bound on the remaining window: the
1132            // check is not atomic with the write below, so an asker that
1133            // takes the word in between (at most one poll, never more than
1134            // `ask::LEASE_TTL`) only prints it - it never touches the task,
1135            // and `Task::actions_applied` under the claim keeps the action to
1136            // one application. The waiter does not deliver an answer that
1137            // carries an action (`waiter::decide_owned`), so it cannot race.
1138            continue;
1139        }
1140        let done = matches!(decision, ActionDecision::Done);
1141        match decision {
1142            ActionDecision::Skip => continue,
1143            ActionDecision::Resume(run) => {
1144                task.release_operator("choice", Some(&q.id));
1145                task.resume_override = Some(crate::queue::OperatorResume {
1146                    question_id: q.id.clone(),
1147                    at: Timestamp::now(),
1148                    conductor_rehold: None,
1149                    forced: true,
1150                    pinned_run: Some(run),
1151                });
1152            }
1153            ActionDecision::Stale => {}
1154            ActionDecision::Requeue => {
1155                task.release_operator("choice-requeue", Some(&q.id));
1156                task.requeue();
1157            }
1158            ActionDecision::Done => task.succeed(),
1159            ActionDecision::Refuse(why) => {
1160                if task.operator_held() {
1161                    // A rejected answer does not release the operator's hold
1162                    // or reclassify it as recoverable by the conductor.
1163                    task.hold_reason = Some(match task.hold_reason.take() {
1164                        Some(reason) => format!("{reason}\n\n{why}"),
1165                        None => why,
1166                    });
1167                } else {
1168                    task.hold_machine(Some(why));
1169                }
1170                notices::raise(
1171                    Notice::warn(
1172                        &format!("action:{}", q.id),
1173                        "An answer asked the daemon to resume a run that cannot be resumed; the task stays held.",
1174                    )
1175                    .link(Link::Task {
1176                        id: task.id.clone(),
1177                    }),
1178                );
1179            }
1180        }
1181        task.mark_action_applied(&q.id);
1182        // The applied id travels with the transition, so a failed save loses
1183        // neither and the answer is acted on at the next poll.
1184        record(queue, &mut task);
1185        if done && queue.get(&task.id).is_ok_and(|t| t.action_applied(&q.id)) {
1186            supersede_prior_runs(&task, home);
1187        }
1188        // Delivery is only a note of who has read the answer; the task's own
1189        // mark above is the authority. Best effort.
1190        let _ = questions.update(&q.id, |r| {
1191            r.answer_delivered = true;
1192            Ok(())
1193        });
1194    }
1195}
1196
1197/// Whether something may still read the answer to `q`: a fresh lease, or the
1198/// asking seat still listed as active in its run (its CLI can outlive the
1199/// `magi ask` that was killed).
1200fn asker_may_still_read(questions: &Questions, q: &ask::Question, home: &Path) -> bool {
1201    let now = Timestamp::now();
1202    if questions.read_lease(&q.id).is_some_and(|l| l.fresh(now)) {
1203        return true;
1204    }
1205    q.node != crate::conduct::NODE
1206        && RunState::load_under(&q.run, home).is_ok_and(|s| {
1207            s.seats_active()
1208                .any(|(k, a)| *k == q.seat && a.remaining_secs(now) > 0)
1209        })
1210}
1211
1212/// Retire an unanswered conductor question after its task no longer refers to
1213/// it. Conductor questions use the task id in `Question::run`, so run-based
1214/// cleanup cannot observe a manual release or completion.
1215///
1216/// Restricted to `Question::node == crate::conduct::NODE`: an ordinary run's
1217/// own question also carries a `run`, and a run id that happens to collide
1218/// with some task's id is not this loop's business — only a conductor
1219/// question actually uses the task id that way. One `Questions::list()` scan
1220/// is taken up front and matched against the in-memory task set, rather than
1221/// calling `Questions::open_for` (a full disk scan on its own) once per task.
1222fn reconcile_task_questions(queue: &Queue, questions: &Questions) {
1223    let tasks = queue.list();
1224    let by_id: std::collections::BTreeMap<&str, &Task> =
1225        tasks.iter().map(|t| (t.id.as_str(), t)).collect();
1226    let referenced: std::collections::BTreeSet<&str> = tasks
1227        .iter()
1228        .flat_map(|task| task.blocked_by.iter().map(String::as_str))
1229        .collect();
1230
1231    for mut question in questions.list() {
1232        if !question.status.open() || question.node != crate::conduct::NODE {
1233            continue;
1234        }
1235        // Keep questions a task still names, including when the reference
1236        // moved to a dependent task.
1237        if referenced.contains(question.id.as_str()) {
1238            continue;
1239        }
1240        let Some(task) = by_id.get(question.run.as_str()) else {
1241            continue;
1242        };
1243        question.abandon(format!(
1244            "task {} no longer waits for this answer",
1245            task.short()
1246        ));
1247        if let Err(e) = questions.put(&mut question) {
1248            tracing::warn!(
1249                "could not retire question {} for task {}: {e:#}",
1250                question.short(),
1251                task.short()
1252            );
1253        }
1254    }
1255}
1256
1257/// What a finished run tells the queue about the task it came from.
1258///
1259/// A struct rather than a fourth and fifth boolean argument: the two flags
1260/// answer different questions about the same run, and a call site passing
1261/// `(…, true, false)` is one transposition away from refunding attempts
1262/// forever.
1263#[derive(Debug, Clone, Copy)]
1264pub struct Verdict {
1265    /// Where the graph stopped.
1266    pub status: RunStatus,
1267    /// The run opened a pull request.
1268    pub left_pr: bool,
1269    /// At least one seat was lost to a rate limit.
1270    pub quota_hit: bool,
1271    /// The run parked at a node boundary because it was asked to.
1272    pub parked: bool,
1273    /// The run never produced a single candidate a judge could look at.
1274    ///
1275    /// Distinct from `quota_hit`: a run can lose a seat to a rate limit and
1276    /// still have another candidate worth judging, in which case the loss was
1277    /// not the reason nothing came of the run. This is `true` only when the
1278    /// implement wave ended with nothing viable at all.
1279    pub no_viable_candidates: bool,
1280}
1281
1282/// Record a finished run against the task it came from.
1283///
1284/// Kept pure and separate from the loop because this mapping *is* the retry
1285/// policy, and a policy that can only be exercised by spawning a graph is a
1286/// policy nobody checks. The table:
1287///
1288/// | run status                           | task becomes        | attempt spent |
1289/// |---------------------------------------|---------------------|---------------|
1290/// | parked at a boundary                  | `Parked` (requeued) | **no**        |
1291/// | `Merged`, `Ready`                      | `Done`               | yes          |
1292/// | `AlreadyInBase`                        | `Done`, with a note  | **no**        |
1293/// | `Stalled`, quota hit                   | `Failed` (requeued) | **no**        |
1294/// | `Failed`, quota hit, no viable cand.   | `Failed` (requeued) | **no**        |
1295/// | `Stalled`, no quota                    | `Failed`, or `Held`  | yes          |
1296/// | `Blocked` with a PR                    | `Held`               | yes          |
1297/// | `Blocked`, `Failed` otherwise          | `Failed`, or `Held`  | yes          |
1298/// | `VerifiedNoop`                          | `Held`               | yes          |
1299/// | anything non-terminal                  | `Failed`, or `Held`  | yes          |
1300///
1301/// The `VerifiedNoop` row is independent of the `Failed`-quota row above it,
1302/// deliberately: every candidate agreeing there is nothing to write is not a
1303/// machine fact about a rate limit, it is an unverified claim about the
1304/// *task* that a human still has to check — see [`RunStatus::VerifiedNoop`]'s
1305/// own doc and [`Task::handed_off`]. `Held` rather than `Done` on purpose: the
1306/// claim could be wrong (a misread instruction, a stale check), and closing
1307/// the task automatically on an implementer's say-so would be the exact
1308/// failure mode task 391f's own audit was raised to avoid. `attempt spent` is
1309/// `yes` here for the same reason it is on the `Blocked`-with-a-PR row just
1310/// above, which settles through the same [`Task::handed_off`]: `Held` is not
1311/// `Failed`-and-requeued, so nothing retries this task on the same unverified
1312/// claim regardless of whether the one already-spent attempt is refunded, and
1313/// [`Task::release`] resets the count to zero anyway the moment a human looks
1314/// at the evidence and lets it run again.
1315///
1316/// The `Stalled`-quota and `Failed`-quota rows are the ones worth reading
1317/// twice, together. A quorum lost to rate limits is a property of the machine
1318/// and not of the task, so the attempt is refunded and a reset quota picks
1319/// the work up where it stopped — and that is just as true when every
1320/// implement seat lost the same race and `after_implement` bails with nothing
1321/// to judge, which surfaces as `Failed` rather than `Stalled` but is the same
1322/// machine fact. The `no_viable_candidates` guard is what keeps that row
1323/// narrow: a `Failed` run that produced a real candidate which then lost for
1324/// some other reason still spends the attempt, exactly like the quorum lost
1325/// to judges that answered with the wrong shape is ordinary flakiness, and
1326/// refunding *that* takes the bound off the retry loop entirely: run e633
1327/// stalled with `quota: []` after two judges wrote unusable JSON, was
1328/// refunded, and the next attempt paid for a fresh hour-long implement wave
1329/// before it could fail the same way. `max_attempts` exists precisely so
1330/// that cannot repeat forever.
1331///
1332/// A non-terminal status means `execute` returned while the graph was still
1333/// mid-flight, which is a bug rather than a verdict; it is treated as a
1334/// failure so that a task cannot loop on it either.
1335///
1336/// `left_pr` splits the `Blocked` row, and it is the difference between a run
1337/// that failed and a run that finished into a gate. See [`Task::handed_off`].
1338pub fn settle(task: &mut Task, verdict: Verdict, detail: &str, max_attempts: usize) {
1339    settle_in(task, verdict, detail, max_attempts, &PHRASES_EN)
1340}
1341
1342/// [`settle`], with the daemon's own fixed prose in `p`'s language.
1343fn settle_in(task: &mut Task, verdict: Verdict, detail: &str, max_attempts: usize, p: &Phrases) {
1344    // A parked run is the operator's own doing, and its work is intact on
1345    // disk. The task goes back in line with its attempt refunded so the next
1346    // loop resumes the same run - which `one_task` prefers over competing
1347    // again - and so that swapping the binary a few times cannot exhaust a
1348    // budget meant for agents that actually misbehaved.
1349    if verdict.parked {
1350        task.park(detail);
1351        return;
1352    }
1353    match verdict.status {
1354        RunStatus::Merged | RunStatus::Ready => task.succeed(),
1355        RunStatus::AlreadyInBase => task.already_landed(detail),
1356        RunStatus::Stalled if verdict.quota_hit => task.stall(detail),
1357        RunStatus::Failed if verdict.quota_hit && verdict.no_viable_candidates => {
1358            task.stall(detail)
1359        }
1360        RunStatus::Stalled | RunStatus::Failed => task.fail(detail, max_attempts),
1361        RunStatus::Blocked if verdict.left_pr => task.handed_off(detail),
1362        RunStatus::Blocked => task.fail(detail, max_attempts),
1363        RunStatus::VerifiedNoop => task.handed_off(detail),
1364        other => task.fail((p.graph_stopped)(label(other), detail), max_attempts),
1365    }
1366}
1367
1368/// Rewrite this task's now-moot earlier attempts — [`Task::superseded_attempts`]
1369/// — from `Blocked`/`Stalled` to [`RunStatus::Superseded`], once the task
1370/// itself has actually finished *and* its own last attempt is provably why:
1371/// this loads that run and requires `Merged`/`Ready` before touching
1372/// anything earlier in `runs`, since `Task::succeed` can also be called
1373/// directly on a task whose last attempt never landed at all.
1374///
1375/// Called right after whatever call set `task.status` to
1376/// [`TaskStatus::Done`] — this module's own `settle`'s callers below and
1377/// `reclaim`'s, but also every other place a task can be closed by hand
1378/// without the loop ever settling it itself: `magi task done`
1379/// (`main::TaskCmd::Done`) and `POST /api/queue/{id}/done`
1380/// (`web::queue_done`). Closing a task by hand — after confirming a manual
1381/// GitHub merge, say, exactly the workflow this repository's own
1382/// "Landing a run's winner by hand" documents — is just as much "this task's
1383/// story is over" as a daemon-driven `Merged`/`Ready` is, and skipping it
1384/// there would leave every earlier attempt stuck at `Blocked`/`Stalled`
1385/// forever, which is the exact backlog this status exists to clear. `pub`
1386/// for those two out-of-module callers; everything else about this stays
1387/// internal bookkeeping.
1388///
1389/// A run something is still actually driving is left alone: a
1390/// task cannot be `Done` while one of its *own* attempts is still in flight,
1391/// so a live one at this point can only be an unrelated process — a manual
1392/// `--resume` of this old, now-moot run — and rewriting under it would just
1393/// be undone (back to `Blocked`/`Stalled`, or worse) the next time that
1394/// process itself saves. This is the same `daemon_claims` + [`RunState::liveness`]
1395/// pair `run_report`/`run_detail` use for the same question elsewhere:
1396/// `is_working_on` alone only ever proves a *daemon* claim, not a `magi run
1397/// --resume` invoked by hand outside it, which `liveness` also checks via
1398/// the run's own recorded `driver_pid`. Anything not `Blocked` or `Stalled`
1399/// (already terminal in some other way, or itself mid-run) is left untouched
1400/// too.
1401///
1402/// Best-effort, like the rest of this module's bookkeeping: a load or save
1403/// failure here is a `tracing::warn`, not a failed settle, and the next time
1404/// this same task's completion is recorded — or a daemon restart replays
1405/// `reclaim_orphaned_running` — is another chance to catch up. There is no
1406/// separate reconciliation pass; if that gap ever matters in practice, one
1407/// can be added then.
1408///
1409/// `home` is a parameter, not the process-global [`crate::run::home`], for
1410/// the same reason [`RunState::save_under`] takes one: `web::queue_done`
1411/// calls this with `ui.home`, which is `crate::run::home()` in a real
1412/// process but a fixture's own directory under test — falling through to
1413/// the global there would read and write through whichever home some other
1414/// test in the same binary happened to pin into that `OnceLock` first, not
1415/// the run this call actually means.
1416pub fn supersede_prior_runs(task: &Task, home: &Path) {
1417    let now = Timestamp::now();
1418    // `Task::superseded_attempts` cannot tell a loop-driven `Done` (always
1419    // right behind a `Merged`/`Ready` verdict) from one set directly by
1420    // `magi task done`/its web equivalent/the conductor on a task whose last
1421    // recorded attempt never actually landed - that requires reading the
1422    // run itself, which is exactly what this does before trusting anything
1423    // in `runs` is safe to relabel.
1424    let last_run_succeeded = task
1425        .runs
1426        .last()
1427        .and_then(|id| RunState::load_under(id, home).ok())
1428        .is_some_and(|s| matches!(s.status, RunStatus::Merged | RunStatus::Ready));
1429    for id in task.superseded_attempts(last_run_succeeded) {
1430        let mut state = match RunState::load_under(id, home) {
1431            Ok(s) => s,
1432            Err(e) => {
1433                tracing::warn!("could not load run {id} to mark it superseded: {e:#}");
1434                continue;
1435            }
1436        };
1437        if !matches!(state.status, RunStatus::Blocked | RunStatus::Stalled) {
1438            continue;
1439        }
1440        let daemon_claims = is_working_on(home, id, now);
1441        if state.liveness(daemon_claims) == Liveness::Live {
1442            continue;
1443        }
1444        state.status = RunStatus::Superseded;
1445        if let Err(e) = state.save_under(home) {
1446            tracing::warn!("could not mark run {id} superseded: {e:#}");
1447        }
1448    }
1449}
1450
1451/// Catch up every `Done` task [`supersede_prior_runs`] could not fully
1452/// finish the moment it was called, because one of its earlier attempts was
1453/// still live then - a `magi run --resume` invoked by hand on an old run,
1454/// most likely.
1455///
1456/// That skip is correct at the time (rewriting under a run something is
1457/// still actively saving would just be undone), but a task only ever
1458/// reaches `Done` once, so without this nothing would ever look at it
1459/// again: if that manual process later ends anywhere other than
1460/// `Merged`/`Ready` - abandoned, or landed as another `Blocked` - the
1461/// skipped attempt would sit at `Blocked`/`Stalled` forever, exactly the
1462/// backlog this status exists to clear. Called on the same idle-only
1463/// cadence as [`janitor`], next to it rather than folded into it: this
1464/// walks `queue` and `janitor` never otherwise needs one.
1465///
1466/// Simply re-running [`supersede_prior_runs`] over every `Done` task with
1467/// more than one attempt, every pass: each call is a handful of local file
1468/// reads bounded by that task's own attempt count, the same order of cost
1469/// [`clean::fold_due`] already pays scanning every run on disk on the same
1470/// cadence, and a task already fully resolved costs one failed liveness
1471/// check turned no-op the moment its lingering attempt is no longer live.
1472fn resweep_superseded_attempts(queue: &Queue, home: &Path) {
1473    for task in queue.list() {
1474        if task.status != TaskStatus::Done || task.runs.len() < 2 {
1475            continue;
1476        }
1477        supersede_prior_runs(&task, home);
1478    }
1479}
1480
1481/// [`settle`], plus attaching the run's own [`diagnostic`] excerpt once the
1482/// task ends up held.
1483///
1484/// The one place [`attempt`] (a live finish) and [`reclaim`] (recovering one a
1485/// dead daemon never got back to) share this, so the two cannot drift into
1486/// disagreeing about which held tasks get a diagnostic.
1487fn settle_and_diagnose(
1488    task: &mut Task,
1489    verdict: Verdict,
1490    detail: &str,
1491    max_attempts: usize,
1492    state: &RunState,
1493) {
1494    let p = phrases(&state.config.graph.language);
1495    let attempts_before = task.attempts;
1496    // A run that stopped because a shared resource stayed busy was never a
1497    // failure of the task or of any agent: nothing was asked. The attempt is
1498    // refunded and the task is held by the machine, under a reason `triage`
1499    // can re-check, so the same run resumes with the same agents once the
1500    // resource is free. A park request still wins (it is the operator's).
1501    if let Some(block) = state.resource_block.as_ref().filter(|_| !verdict.parked) {
1502        task.last_error = Some(detail.to_owned());
1503        task.diagnostic = None;
1504        task.attempts = task.attempts.saturating_sub(1);
1505        task.hold_machine(Some(format!(
1506            "{}{} stayed busy past the resource-wait limit before `{}` could start; \
1507             no agent was asked and no attempt was spent",
1508            crate::cache::RESOURCE_HOLD,
1509            block.kind,
1510            block.node
1511        )));
1512        if let Some(pass) = state.passes.last() {
1513            task.record_pass(&state.id, pass.index, attempts_before, None);
1514        }
1515        return;
1516    }
1517    settle_in(task, verdict, detail, max_attempts, p);
1518    // Written here, where the refund (or not) is decided, so the history is a
1519    // fact and not a later guess from the run's status.
1520    if let Some(pass) = state.passes.last() {
1521        let parked = match &pass.end {
1522            Some(crate::run::PassEnd::Parked { cause }) => Some(cause.clone()),
1523            _ => None,
1524        };
1525        task.record_pass(&state.id, pass.index, attempts_before, parked);
1526    }
1527    if task.status == TaskStatus::Held {
1528        task.diagnostic = diagnostic(state);
1529        note_open_question(task, &state.id, p);
1530    }
1531}
1532
1533/// If `task` is held with a `magi ask` question still open on `run`, name the
1534/// question in [`Task::hold_reason`] rather than leaving the hold reading
1535/// only [`describe`]'s run summary.
1536///
1537/// This is the shape task 20260928-191407-05fd left underspecified: an
1538/// implementer settled a verified no-op and filed a question for the
1539/// operator, and the hold that followed said nothing about the question
1540/// waiting on them. Appended, not substituted - [`crate::triage`] parses the
1541/// existing text ([`is_disk_hold`](crate::triage), the answered-question
1542/// marker) and must keep seeing it unchanged.
1543///
1544/// Best-effort: an unreadable question store must not stop a run from
1545/// settling, so this is a no-op rather than a propagated error when
1546/// [`crate::run::try_home`] has nothing to offer or the store is empty.
1547fn note_open_question(task: &mut Task, run: &str, p: &Phrases) {
1548    let Some(home) = crate::run::try_home() else {
1549        return;
1550    };
1551    let open = Questions::at(home.join("questions")).open_for(run);
1552    let Some(q) = open.first() else {
1553        return;
1554    };
1555    let base = task.hold_reason.clone().unwrap_or_default();
1556    task.hold_reason = Some(format!("{base}{}{}", p.waiting_for_answer, q.short()));
1557}
1558
1559/// Reconcile a task left at [`TaskStatus::Running`] by a daemon that never
1560/// got back to [`settle`] for it — a crash, a `SIGKILL`, or a run carried on
1561/// by some other means entirely, like a manual `magi run` resume that
1562/// finishes the graph outside the queue's bookkeeping.
1563///
1564/// Pure and separate from [`reclaim_orphaned_running`] for the same reason
1565/// `settle` is separate from `attempt`: a task recovered this way must land
1566/// exactly where a live daemon would have put it — the same policy table,
1567/// not a second one that quietly drifts from it — and that is only checkable
1568/// without spawning a real run.
1569fn reclaim(task: &mut Task, last_run: Option<RunState>, max_attempts: usize, language: &str) {
1570    match last_run {
1571        Some(state) => {
1572            let verdict = Verdict {
1573                status: state.status,
1574                left_pr: state.pr.is_some(),
1575                quota_hit: !state.quota.is_empty(),
1576                parked: state.parked,
1577                no_viable_candidates: state.viable().is_empty(),
1578            };
1579            let detail = format!(
1580                "{}{}",
1581                phrases(&state.config.graph.language).recovered_running,
1582                describe(&state)
1583            );
1584            settle_and_diagnose(task, verdict, &detail, max_attempts, &state);
1585        }
1586        None => {
1587            // No run, so no config of its own: `language` is the task's
1588            // repository's setting, read by the caller.
1589            let why = phrases(language).no_run_to_recover;
1590            task.last_error = Some(why.to_owned());
1591            // The phone shows `hold_reason`, so a task held by the machine
1592            // says why there too and not only in `last_error`.
1593            task.hold_machine(Some(why.to_owned()));
1594        }
1595    }
1596}
1597
1598/// Find every task left at `running` that no live process is actually
1599/// driving, and settle each one against whatever its last run became.
1600///
1601/// # Why a claim is proof, not a guess
1602///
1603/// [`poll`] takes a task's [`Queue::claim`] *before* [`Task::start`] writes
1604/// `running`, and the guard is held for the task's whole time in that status:
1605/// `attempt` does not return, and the loop does not move past the scope
1606/// holding the claim, until the run has settled. So a `running` task whose
1607/// lock is gone cannot have a live owner — this process or any other —
1608/// without needing a staleness threshold or a pid check the way
1609/// [`sweep_stale_claims`] does for the narrower case of a lock left next to a
1610/// task that never got as far as `running` at all. Taking the claim here is
1611/// the whole test: it either fails, because something really does hold it
1612/// and the task is left alone, or it succeeds, which is the proof — and it is
1613/// kept for the rest of the decision so nothing else can start a competing
1614/// run while this one is being written.
1615///
1616/// Called on every poll, not only at startup, for the reason
1617/// [`sweep_stale_claims`] now is too: a daemon that has been up for days must
1618/// keep noticing this, not only on the one morning it happened to restart.
1619fn reclaim_orphaned_running(queue: &Queue, max_attempts: usize) -> Vec<String> {
1620    let mut reclaimed = Vec::new();
1621    for listed in queue.list() {
1622        if listed.status != TaskStatus::Running {
1623            continue;
1624        }
1625        let Ok(_claim) = queue.claim(&listed.id) else {
1626            continue;
1627        };
1628        // Re-read under the claim: a release or an edit landed by a human
1629        // between the listing above and the claim just taken must not be
1630        // clobbered by a decision based on the stale copy.
1631        let Ok(mut task) = queue.get(&listed.id) else {
1632            continue;
1633        };
1634        if task.status != TaskStatus::Running {
1635            continue;
1636        }
1637        let last_run = task.runs.last().and_then(|id| RunState::load(id).ok());
1638        // `execute` normally abandons a run's own open questions the moment
1639        // `status` lands somewhere non-resumable (see `graph::Runner::settle_questions`),
1640        // but a daemon that crashed *inside* that path - mid `land`'s CI wait,
1641        // say - can leave a `run.json` already at `Merged`/`Ready`/`Failed`
1642        // with the question still `open`, because the process died before
1643        // reaching that call. `reclaim` itself stays pure on purpose (see its
1644        // own doc), so the same cleanup runs here instead, against the run
1645        // this reclaim is already reading. `settle_run` costs nothing when
1646        // `execute` already got there first.
1647        if let Some(state) = &last_run
1648            && let Err(e) = ask::Questions::open().settle_run(&state.id, state.status)
1649        {
1650            tracing::warn!("abandon questions for {}: {e:#}", state.id);
1651        }
1652        let language = if last_run.is_none() {
1653            language_of(&task, Path::new("."))
1654        } else {
1655            String::new()
1656        };
1657        reclaim(&mut task, last_run, max_attempts, &language);
1658        if task.status == TaskStatus::Done {
1659            supersede_prior_runs(&task, &crate::run::home());
1660        }
1661        record(queue, &mut task);
1662        reclaimed.push(task.id.clone());
1663    }
1664    reclaimed
1665}
1666
1667/// Find every run whose `run.json` is provably dead — every seat it still
1668/// lists as [`crate::run::RunState::active`] has overrun its own timeout, and
1669/// [`crate::run::RunState::liveness`] reads [`crate::run::Liveness::Dead`],
1670/// not merely "no daemon claims it" — and fail it, clearing the leftover
1671/// active seats so the run stops reading as `implementing` (or whichever
1672/// node) forever.
1673///
1674/// [`reclaim_orphaned_running`] settles the *task* a dead daemon left
1675/// `running`, using whatever `run.json` already says — but nothing in that
1676/// path, nor in [`reclaim`], ever writes back to the run itself (`reclaim`
1677/// stays pure on purpose, see its own doc), so a `run.json` a killed process
1678/// never got back to sits exactly where it was left: `active` full of seats
1679/// nobody will ever answer for, `status` stuck on whatever node was in
1680/// flight. `magi show` already tells an operator this in prose (`no live
1681/// daemon claims this run right now`); this is what makes that fact durable
1682/// on disk, the same way a task's own `TaskStatus::Running` does not get to
1683/// stay stuck once nothing is driving it.
1684///
1685/// Runs on every poll, not only at startup, for the reason
1686/// [`sweep_stale_claims`] and [`reclaim_orphaned_running`] already are: a
1687/// daemon up for days must keep noticing a run some other, now-dead, daemon
1688/// left behind just as readily as one it trips over on the way up.
1689///
1690/// Walks `home.join("runs")` directly and reads each `run.json` on its own,
1691/// rather than the process-global [`RunState::load`] / [`crate::run::list_ids`] —
1692/// the same reason [`crate::clean`]'s housekeeping passes take an explicit
1693/// `runs` directory instead: `home` here is a parameter precisely so a test
1694/// can point it away from the operator's real history (see [`drive`]'s own
1695/// doc), and a scan that fell through to the global home anyway would walk
1696/// whichever directory some *other* process or test pinned into that
1697/// `OnceLock` first — mutating runs this call was never handed.
1698fn reclaim_abandoned_runs(home: &Path, now: Timestamp) -> Vec<String> {
1699    reclaim_abandoned_runs_with(
1700        home,
1701        now,
1702        crate::proc::pid_status,
1703        crate::proc::process_started_at,
1704    )
1705}
1706
1707/// [`reclaim_abandoned_runs`] with its `driver_pid` liveness/identity queries
1708/// supplied by the caller — mirrors [`sweep_stale_claims_with`], which exists
1709/// for the identical reason: this sweep's real damage (wiping a run's active
1710/// seats and failing it) has to be provable against an injected answer in a
1711/// test, not just the real process table.
1712fn reclaim_abandoned_runs_with<F, G>(
1713    home: &Path,
1714    now: Timestamp,
1715    query: F,
1716    identity: G,
1717) -> Vec<String>
1718where
1719    F: Fn(u32) -> Option<bool>,
1720    G: Fn(u32) -> Option<String>,
1721{
1722    let mut abandoned = Vec::new();
1723    for entry in std::fs::read_dir(home.join("runs"))
1724        .into_iter()
1725        .flatten()
1726        .flatten()
1727    {
1728        let id = entry.file_name().to_string_lossy().into_owned();
1729        if !crate::run::is_run_id(&id) {
1730            continue;
1731        }
1732        // Unreadable is `clean::fold_due`'s problem, not this one's — see
1733        // that module's docs for why a run this cannot parse is left alone
1734        // rather than guessed at. A different schema number is not that: this
1735        // touches only `status` and `active`, never a field whose meaning a
1736        // schema bump changed, so an old record's values serve this exactly
1737        // as well as a current one's (see `clean::read_state`'s own doc for
1738        // the same reasoning applied to folding).
1739        let Ok(body) = std::fs::read_to_string(entry.path().join("run.json")) else {
1740            continue;
1741        };
1742        let Ok(mut state) = serde_json::from_str::<RunState>(&body) else {
1743            continue;
1744        };
1745        if state.status.done() || !state.active_all_overrun(now) {
1746            continue;
1747        }
1748        // Not `!is_working_on(..)` alone: that is only "no *daemon* claims
1749        // it", which is also the normal, healthy shape of a manual `magi
1750        // run` / `magi review` sharing this same home — this scan walks
1751        // every run on disk, not only ones this daemon itself started. Such
1752        // a run's active seats can legitimately sit past their own timeout
1753        // for a little while (the CLI finishing up, its result still being
1754        // collected) without the process driving it having died. `liveness`
1755        // is what actually tells the two apart, by corroborating
1756        // `driver_pid` against the process it names — see its own doc. Only
1757        // its strongest, provable answer licenses wiping this run's active
1758        // seats and failing it out from under whatever is still running it.
1759        let daemon_claims = is_working_on(home, &id, now);
1760        if state.liveness_with(daemon_claims, &query, &identity) != crate::run::Liveness::Dead {
1761            continue;
1762        }
1763        state.abandon("daemon");
1764        if let Err(e) = state.save_under(home) {
1765            tracing::warn!("could not persist abandoned run {id}: {e:#}");
1766            continue;
1767        }
1768        // This run never passes through `Runner::execute` again, so its end
1769        // is announced here.
1770        if let Some(notice) = notices::run_ended(&state) {
1771            notices::raise_in(home, notice);
1772        }
1773        // The seat that asked is gone for good now, exactly like any other
1774        // door `graph::Runner::settle_questions` closes the moment `status`
1775        // lands somewhere non-resumable - see that method's own doc. Nothing
1776        // else reaches this one before the next `janitor()` startup pass
1777        // (`clean::abandon_settled_questions`), and a daemon that stays up
1778        // for days must not leave an open question badging the operator
1779        // until it happens to restart.
1780        if let Err(e) = Questions::at(home.join("questions")).settle_run(&id, state.status) {
1781            tracing::warn!("abandon questions for {id}: {e:#}");
1782        }
1783        abandoned.push(id);
1784    }
1785    abandoned
1786}
1787
1788/// Run the loop until Ctrl-C, or until the queue drains with [`Opts::once`].
1789///
1790/// A thin wrapper over [`serve_until`] with a stop nothing but Ctrl-C ever
1791/// sets, so there is one loop body rather than two that drift apart the first
1792/// time the retry policy changes on only one of them.
1793pub async fn serve(opts: Opts) -> Result<()> {
1794    serve_until(opts, Stop::new()).await
1795}
1796
1797/// [`serve`], but stopping when `stop` is set as well as on Ctrl-C.
1798///
1799/// Neither a signal nor a `stop` abandons a run in flight. Killing the graph
1800/// mid-node leaves worktrees, branches and agent sessions behind, and every
1801/// agent call already paid for is lost; finishing the run costs the operator a
1802/// wait and saves them a cleanup. A stop therefore only sets a flag: the
1803/// current `execute` runs to its terminal status, the task's outcome is
1804/// recorded, and only then does the loop return. That window is what
1805/// [`Stop::finishing`] is for. An operator who genuinely wants the run dead
1806/// still has a second Ctrl-C, which the runtime turns into a process kill —
1807/// and the task left `Running` then tells the next daemon, and the next human,
1808/// where to look.
1809///
1810/// While the queue is empty the stop is honoured within one wakeup rather than
1811/// one poll interval: the wait is a `select!` against [`Stop`]'s notify, so a
1812/// caller that taps stop does not sit through the remainder of a sleep.
1813pub async fn serve_until(opts: Opts, stop: Stop) -> Result<()> {
1814    let signal = {
1815        let stop = stop.clone();
1816        tokio::spawn(async move {
1817            if tokio::signal::ctrl_c().await.is_ok() {
1818                stop.stop();
1819                tracing::info!("shutdown requested; a run in flight will be finished first");
1820            }
1821        })
1822    };
1823
1824    let worktrees_root = opts
1825        .worktrees_root
1826        .clone()
1827        .unwrap_or_else(crate::run::default_worktree_root);
1828    let outcome = drive(
1829        &opts,
1830        &Queue::open(),
1831        &status_path(),
1832        &crate::run::home(),
1833        &worktrees_root,
1834        &stop,
1835    )
1836    .await;
1837
1838    signal.abort();
1839    outcome
1840}
1841
1842/// The loop proper: setup, poll, teardown, with the queue and the status file
1843/// supplied rather than discovered.
1844///
1845/// All three of `home`, `worktrees_root` and the queue/status paths are
1846/// parameters rather than resolved here, for the same reason:
1847/// [`crate::run::home`] is process-global and its override is a `OnceLock`,
1848/// so a unit test that pinned it would fight every other test in the binary,
1849/// and a loop that resolved its own worktree bay could only be exercised
1850/// against the operator's real `~/wt/<repo>` - publishing over a live
1851/// daemon's status file, claiming tasks out of a live backlog, and, since
1852/// [`janitor`] runs on every idle tick, reclaiming worktrees out from under
1853/// whatever the operator actually has on disk.
1854async fn drive(
1855    opts: &Opts,
1856    queue: &Queue,
1857    status_file: &Path,
1858    home: &Path,
1859    worktrees_root: &Path,
1860    stop: &Stop,
1861) -> Result<()> {
1862    // The status file is a *snapshot*, not a stream of events: a reader only
1863    // ever wants the latest values, and every tick rewrites the whole file
1864    // anyway. A shared `Mutex<Status>` therefore says exactly what is meant,
1865    // while an mpsc channel would force the loop to re-send unchanged fields on
1866    // every heartbeat — or the heartbeat to keep its own shadow copy of them —
1867    // for no gain. The lock is only ever held across a field assignment, never
1868    // across an await.
1869    let status = Arc::new(Mutex::new(Status::new()));
1870    write_status_to(status_file, &lock(&status)).context("publish the daemon status file")?;
1871    let beat = tokio::spawn(heartbeat(Arc::clone(&status), status_file.to_path_buf()));
1872
1873    // Read once at startup, not per task: how many runs this loop drives at
1874    // once is a property of the machine running it, not of whichever
1875    // repository a given task happens to name - see
1876    // `Config::daemon.max_concurrent_runs`'s doc for why that is a machine
1877    // fact in the same sense the agent roster is.
1878    let daemon_cfg = prepare(&opts.repo, opts)
1879        .map(|c| c.daemon)
1880        .unwrap_or_default();
1881    let concurrency = max_concurrent(daemon_cfg.max_concurrent_runs);
1882
1883    // Something has to be waiting on every question for as long as it is open,
1884    // whether or not the agent's own `magi ask` survived. Its own task rather
1885    // than a step of the poll loop: the poll loop is busy for the whole of a
1886    // run, and an owner's reply must not queue behind one.
1887    let waiter = tokio::spawn(crate::waiter::run(
1888        crate::waiter::Waiter::new(
1889            crate::ask::Questions::at(home.join("questions")),
1890            home.to_path_buf(),
1891            prepare(&opts.repo, opts).ok(),
1892        ),
1893        stop.clone(),
1894    ));
1895
1896    // The follow-up seats for the conductor's questions: the conductor files a
1897    // question and moves on, and each open one gets a deputy that reads the
1898    // owner's reply. Its own task for the same reason the waiter's is.
1899    let deputies = tokio::spawn(crate::deputy::run(
1900        crate::deputy::Deputies::new(
1901            crate::ask::Questions::at(home.join("questions")),
1902            home.to_path_buf(),
1903            prepare(&opts.repo, opts).ok(),
1904            opts.repo.clone(),
1905            daemon_cfg.max_deputies,
1906            {
1907                let stop = stop.clone();
1908                Arc::new(move || stop.parking())
1909            },
1910        ),
1911        stop.clone(),
1912    ));
1913
1914    // Keeps every checkout's origin/main fresh; its own task so a slow remote
1915    // never holds up the poll loop.
1916    let fetcher = tokio::spawn(fetch_loop(opts.repo.clone(), opts.clone(), stop.clone()));
1917
1918    // Watches the release-bump pull requests until they land; its own task, not
1919    // tied to any run, because a pending bump outlives the run that opened it.
1920    let release_watch = tokio::spawn(crate::release_watch::run(
1921        crate::release_watch::Watcher::new(
1922            Box::new(crate::release_watch::GhForge),
1923            home.to_path_buf(),
1924        ),
1925        {
1926            let (repo, opts) = (opts.repo.clone(), opts.clone());
1927            move || release_watch_settings(&repo, &opts)
1928        },
1929        stop.clone(),
1930    ));
1931
1932    tracing::info!(
1933        "magi serve: queue {} (poll {}s, {} attempts per task, {} run(s) at once{})",
1934        queue.root().display(),
1935        opts.poll.as_secs(),
1936        opts.max_attempts,
1937        concurrency,
1938        if daemon_cfg.pause_for_interrupts {
1939            ", interrupts enabled"
1940        } else {
1941            ""
1942        }
1943    );
1944
1945    // `--once` drains an already-idle queue without reaching the idle wait,
1946    // but must still perform the startup cleanup.
1947    janitor(&opts.repo, opts, home, worktrees_root).await;
1948    resweep_superseded_attempts(queue, home);
1949
1950    let outcome = poll(
1951        opts,
1952        queue,
1953        &status,
1954        home,
1955        worktrees_root,
1956        stop,
1957        DispatchLimits {
1958            max_concurrent: concurrency,
1959            pause_for_interrupts: daemon_cfg.pause_for_interrupts,
1960        },
1961    )
1962    .await;
1963
1964    beat.abort();
1965    waiter.abort();
1966    deputies.abort();
1967    fetcher.abort();
1968    release_watch.abort();
1969    clear_status_at(status_file);
1970    outcome
1971}
1972
1973/// The checkouts the release watcher covers and `[daemon] release_stall_minutes`,
1974/// re-read each lap. An unreadable config yields `0` minutes: the watcher idles.
1975fn release_watch_settings(repo: &Path, opts: &Opts) -> (Vec<PathBuf>, u64) {
1976    let Ok(cfg) = prepare(repo, opts) else {
1977        return (Vec::new(), 0);
1978    };
1979    let mut paths: Vec<PathBuf> = crate::repos::scan(&cfg.repos.roots)
1980        .into_iter()
1981        .map(|r| r.path)
1982        .collect();
1983    // Repositories the daemon has worked in, wherever they live: a task may
1984    // name one outside `[repos] roots`, and its release PR must be watched too.
1985    let mut known: Vec<PathBuf> = Queue::open().list().into_iter().map(|t| t.repo).collect();
1986    let runs = crate::run::home().join("runs");
1987    for id in crate::run::list_ids_in(&runs) {
1988        let repo = std::fs::read_to_string(runs.join(&id).join("run.json"))
1989            .ok()
1990            .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
1991            .and_then(|v| v.get("repo")?.as_str().map(PathBuf::from));
1992        known.extend(repo);
1993    }
1994    for k in known {
1995        let k = k.canonicalize().unwrap_or(k);
1996        if k.join(".git").exists() && !paths.contains(&k) {
1997            paths.push(k);
1998        }
1999    }
2000    let own = repo.canonicalize().unwrap_or_else(|_| repo.to_path_buf());
2001    if !paths.contains(&own) {
2002        paths.push(own);
2003    }
2004    (paths, cfg.daemon.release_stall_minutes)
2005}
2006
2007/// Per-repo bound on one `git fetch`, so a dead remote cannot stall the pass.
2008const FETCH_TIMEOUT: Duration = Duration::from_secs(30);
2009
2010/// Run [`crate::clean::fetch_origins`] every `[repos] fetch_interval` seconds
2011/// until `stop`. The config is re-read each lap; `0` (or an unreadable config)
2012/// disables the fetch for that lap. Never fails.
2013async fn fetch_loop(repo: PathBuf, opts: Opts, stop: Stop) {
2014    while !stop.stopped() {
2015        let (interval, roots) = match prepare(&repo, &opts) {
2016            Ok(c) => (c.repos.fetch_interval, c.repos.roots),
2017            Err(_) => (0, Vec::new()),
2018        };
2019        if interval > 0 && !roots.is_empty() {
2020            let r = crate::clean::fetch_origins(&roots, FETCH_TIMEOUT, || stop.stopped()).await;
2021            tracing::debug!("fetch origins: {r:?}");
2022        }
2023        // Sleep in slices so a stop or a config change is noticed promptly.
2024        let wait = if interval > 0 { interval } else { 60 };
2025        let mut slept = 0;
2026        while slept < wait && !stop.stopped() {
2027            tokio::time::sleep(Duration::from_secs(1)).await;
2028            slept += 1;
2029        }
2030    }
2031}
2032
2033/// Refresh the status file on a fixed tick.
2034///
2035/// Separate from the loop because a run takes tens of minutes: a status file
2036/// written only between tasks would look stale for the whole of every run, and
2037/// a reader would report the daemon dead exactly while it was busiest.
2038async fn heartbeat(status: Arc<Mutex<Status>>, path: PathBuf) {
2039    loop {
2040        tokio::time::sleep(HEARTBEAT).await;
2041        let snapshot = {
2042            let mut guard = lock(&status);
2043            guard.updated_at = Timestamp::now();
2044            guard.clone()
2045        };
2046        if let Err(e) = write_status_to(&path, &snapshot) {
2047            // A failed heartbeat must not take the daemon down: the loop is the
2048            // product, the status file is only the window onto it.
2049            tracing::warn!("could not refresh the daemon status file: {e:#}");
2050        }
2051    }
2052}
2053
2054/// Whether a task's last run is sitting in `land`'s merge-approval wait, and
2055/// if so, whether that wait is over.
2056#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2057enum LandResume {
2058    /// The task's last run is not parked on a land approval; schedule it
2059    /// like any other candidate.
2060    NotLanding,
2061    /// Parked in `land`, waiting on a question nobody has answered yet.
2062    /// Left alone: attempting it now would only re-observe the same pull
2063    /// request and park again, spending a `gh` call on a decision that has
2064    /// not changed since the last time this was checked.
2065    StillWaiting,
2066    /// Parked in `land`, and the question is settled - answered or
2067    /// abandoned. Resuming this is the one kind of candidate that must not
2068    /// wait on a free [`Config::daemon`] concurrency slot: see [`poll`].
2069    Ready,
2070}
2071
2072/// Classify a runnable candidate by whether it is parked on a land-merge
2073/// approval. Read-only - no claim taken, nothing written - so it is cheap
2074/// enough to call on every candidate, every poll.
2075fn land_resume_state(task: &Task) -> LandResume {
2076    let Some(run_id) = task.runs.last() else {
2077        return LandResume::NotLanding;
2078    };
2079    let Ok(state) = RunState::load(run_id) else {
2080        return LandResume::NotLanding;
2081    };
2082    if !implementers_match(task, &state) || !state.parked {
2083        return LandResume::NotLanding;
2084    }
2085    // Parked in `land` on the merge approval, or at `merge` on the owner's
2086    // word about a title/body the posting gate withheld. Told apart by the
2087    // run's own record, not by a status both can share.
2088    let node = if state.status == RunStatus::Landing {
2089        land::APPROVAL_NODE
2090    } else if state.github_text.as_ref().is_some_and(|g| !g.resolved) {
2091        crate::github_text::ASK_NODE
2092    } else {
2093        return LandResume::NotLanding;
2094    };
2095    let store = ask::Questions::open();
2096    let waiting = store
2097        .list()
2098        .into_iter()
2099        .filter(|q| &q.run == run_id && q.node == node)
2100        .max_by(|a, b| a.id.cmp(&b.id));
2101    let Some(mut q) = waiting else {
2102        return LandResume::Ready;
2103    };
2104    if !q.status.open() {
2105        // Repair spends agent resources and must obey concurrency and quota
2106        // waits; only a merge/hold decision may use the decision-only lane.
2107        if q.resolution().as_deref() == Some(land::REPAIR) {
2108            return if state.liveness(false) == Liveness::Live {
2109                LandResume::StillWaiting
2110            } else {
2111                LandResume::NotLanding
2112            };
2113        }
2114        return LandResume::Ready;
2115    }
2116    // `ask::ask_and_wait`'s own deadline is what used to retire a question
2117    // nobody ever answered; land's approval bypasses that wait entirely (see
2118    // `land::approval_gate`), so the same deadline has to be enforced here
2119    // instead, or `graph.answer_timeout` silently stops meaning anything for
2120    // a land approval and a run can sit `StillWaiting` forever with nobody
2121    // told to look at it.
2122    let timeout = Duration::from_secs(state.config.graph.answer_timeout);
2123    let elapsed = Timestamp::now().as_second() - q.asked_at.as_second();
2124    if elapsed >= 0 && elapsed as u64 >= timeout.as_secs() {
2125        q.abandon(format!(
2126            "no answer within {}s of asking",
2127            timeout.as_secs().max(1)
2128        ));
2129        // If this can't be persisted, do not treat the wait as settled on a
2130        // guess: fall through and try again next poll.
2131        if store.put(&mut q).is_ok() {
2132            return LandResume::Ready;
2133        }
2134    }
2135    LandResume::StillWaiting
2136}
2137
2138/// How often the loop rechecks for new work while something it already
2139/// started is still running, rather than sleeping out the whole
2140/// [`Opts::poll`] interval.
2141///
2142/// Short on purpose: this is what lets a land-merge approval that comes back
2143/// while another task is mid-competition be noticed and resumed within a
2144/// fraction of a second, not within the next multi-second poll.
2145const RECHECK_WHILE_BUSY: Duration = Duration::from_millis(200);
2146
2147/// How often [`poll`] rechecks the shared build cache against its cap at a
2148/// boundary between runs (see [`maybe_prune_cache_between_runs`]), instead of
2149/// waiting for the queue to run dry.
2150///
2151/// A queue that never empties means the `janitor` call at the bottom of this
2152/// loop's fully-idle branch can go unreached for as long as the backlog
2153/// lasts. Five minutes is far below a single gate's own 1200s timeout, so a
2154/// cache that started the day at its 10 GiB cap cannot grow anywhere near the
2155/// 81.8 GiB an idle-only check let it reach before this existed, and it is
2156/// well above the cost of a `dir_size` walk over a multi-gigabyte cache, so a
2157/// backlog of short tasks does not pay for that walk on every poll.
2158const CACHE_CHECK_INTERVAL_SECS: u64 = 5 * 60;
2159
2160/// Frees one attempt's concurrency slot - `Stop`'s busy count and its entry
2161/// in `Status::current` - on drop, so both are released even if the attempt
2162/// panics rather than returning.
2163///
2164/// A `Drop` impl rather than statements written after the `.await` it
2165/// guards: a panic unwinds straight past code placed "after" a call, and
2166/// `Runner::execute`'s chain reaches deep enough into agent-output parsing
2167/// that ruling a panic out there is not a bet this loop can make. Without
2168/// this, one panicking run would leave [`Stop::busy_now`] stuck `true`
2169/// forever - the idle branch in [`poll`], and with it the janitor, would
2170/// never run again - and a ghost entry in `Status::current` naming a task
2171/// nothing is still working on.
2172struct InFlightGuard<'a> {
2173    status: &'a Arc<Mutex<Status>>,
2174    stop: &'a Stop,
2175    task_id: &'a str,
2176}
2177
2178impl Drop for InFlightGuard<'_> {
2179    fn drop(&mut self) {
2180        lock(self.status).current.retain(|c| c.task != self.task_id);
2181        self.stop.exit();
2182    }
2183}
2184
2185/// State of [`poll`]'s own interrupt-scheduling sequence - see
2186/// [`crate::config::Daemon::pause_for_interrupts`]. Advanced once per tick by
2187/// [`advance_interrupt`] and consulted by [`interrupt_gate`], both pure and
2188/// both kept free of `Task`'s non-identity fields on purpose: every decision
2189/// here turns only on task ids and which ones are in flight, so the "never
2190/// more than one run at once" and "exactly one resume" invariants can be
2191/// pinned down with a plain `#[test]`, no `Runner`, no tokio, no fixture
2192/// queue - which is exactly the coverage this feature's first two attempts
2193/// were missing.
2194///
2195/// Deliberately in-memory only, not written to disk anywhere: a daemon
2196/// restart mid-sequence loses track of which run it had asked to park and
2197/// which task was meant to run first, and simply falls back to `Idle` -
2198/// see [`drive`]'s own setup. The parked run itself is not lost - it is
2199/// sitting in the queue exactly like any other resumable, interrupted task,
2200/// `RunStatus::resumable` and [`Task::interrupt`] both intact on disk - it
2201/// just resumes on the ordinary priority order rather than guaranteed to go
2202/// first. Giving that guarantee a crash-proof memory would mean a new queue
2203/// field and a recovery ordering to go with it, which is exactly the
2204/// complexity this feature's constraints rule out for the one property that
2205/// actually matters: at most one run, ever, at once.
2206#[derive(Debug, Clone, PartialEq, Eq)]
2207enum Interrupt {
2208    /// No interrupt sequence in progress. Ordinary dispatch applies.
2209    Idle,
2210    /// `interrupt_task` is runnable and exactly one other run is in flight;
2211    /// `parked` names that one task's id. Dispatch is withheld from
2212    /// everyone, including `interrupt_task` itself, until it has left
2213    /// flight - only then is `interrupt_task` let through.
2214    ///
2215    /// `parked` is a `Vec` rather than a bare id for symmetry with
2216    /// [`Interrupt::Running`] and [`Interrupt::Resuming`], but
2217    /// [`advance_interrupt`]'s own `Idle` branch only ever starts a sequence
2218    /// when exactly one run is in flight, so it is guaranteed to hold
2219    /// exactly one entry in practice - see that branch's own doc for why
2220    /// more than one is deliberately never attempted.
2221    Parking {
2222        parked: Vec<String>,
2223        interrupt_task: String,
2224    },
2225    /// `interrupt_task` is dispatched and in flight alone. Dispatch is
2226    /// withheld from everyone until it leaves flight - merged, failed, held,
2227    /// it makes no difference - at which point the sequence moves to
2228    /// [`Interrupt::Resuming`], never straight back to [`Interrupt::Idle`]:
2229    /// going straight to `Idle` would hand `parked` back to ordinary
2230    /// priority-order dispatch, where a higher-priority task filed in the
2231    /// meantime could start ahead of it.
2232    Running {
2233        parked: Vec<String>,
2234        interrupt_task: String,
2235    },
2236    /// `interrupt_task` left flight; `parked` still names the one run this
2237    /// sequence owes a resume. Dispatch is withheld from everyone except
2238    /// that task - see [`interrupt_gate`] - so the resume this feature
2239    /// promises is never raced by, or run alongside, an unrelated candidate.
2240    /// Ends the moment it is seen in flight, or - see `advance_interrupt`'s
2241    /// own doc on abandonment - the moment it is no longer runnable at all.
2242    Resuming { parked: Vec<String> },
2243}
2244
2245/// One tick of the interrupt scheduler's own state machine. Pure: `in_flight`
2246/// and `runnable` are read-only snapshots of this tick's reality, and the
2247/// only side effect the caller still owes the world is asking whichever
2248/// `Pause` handles `parked` names to actually park - see [`poll`]'s own call
2249/// site.
2250///
2251/// `runnable` only has to carry `id` and `interrupt`; the whole [`Task`] is
2252/// accepted rather than a narrower type because that is what [`poll`] already
2253/// has on hand from [`runnable`], and building a second, smaller list on
2254/// every tick just to satisfy this signature would cost more than it proves.
2255///
2256/// Abandonment: [`Interrupt::Parking`] and [`Interrupt::Resuming`] both fall
2257/// back to a task they are waiting on no longer being [`runnable`] - held,
2258/// blocked, deleted, or finished by some other means entirely, all of which
2259/// an operator can do to a task sitting in the queue with no claim on it at
2260/// all, at any moment, interrupt sequence or not. Without this check the
2261/// sequence would wait forever for a dispatch that can never come, and
2262/// `interrupt_gate` would withhold every other task in the queue right along
2263/// with it - a single `magi task hold` on the wrong id turning into a
2264/// daemon that never dispatches anything again.
2265fn advance_interrupt(state: Interrupt, in_flight: &[String], runnable: &[Task]) -> Interrupt {
2266    match state {
2267        Interrupt::Idle => {
2268            // Not just "something to interrupt": exactly one thing. More
2269            // than one run in flight only happens above the default
2270            // `max_concurrent_runs = 1`, and `parked` guarantees "exactly
2271            // one resume, never run alongside anything else" only because
2272            // it is only ever seeded with exactly one id - see
2273            // `Interrupt::Resuming`'s own doc on why releasing more than one
2274            // parked id back to ordinary dispatch cannot be made safe
2275            // against that same setting's own extra concurrency slots.
2276            // Waiting here for the herd to settle to one is the
2277            // simplification this feature's own constraints ask for rather
2278            // than a second concurrency model to reconcile with the first.
2279            if in_flight.len() != 1 {
2280                return Interrupt::Idle;
2281            }
2282            match runnable.iter().find(|t| t.interrupt) {
2283                Some(t) => Interrupt::Parking {
2284                    parked: in_flight.to_vec(),
2285                    interrupt_task: t.id.clone(),
2286                },
2287                None => Interrupt::Idle,
2288            }
2289        }
2290        Interrupt::Parking {
2291            parked,
2292            interrupt_task,
2293        } => {
2294            if in_flight.iter().any(|id| parked.contains(id)) {
2295                // Still waiting for what was in flight to actually stop.
2296                Interrupt::Parking {
2297                    parked,
2298                    interrupt_task,
2299                }
2300            } else if in_flight.contains(&interrupt_task) {
2301                Interrupt::Running {
2302                    parked,
2303                    interrupt_task,
2304                }
2305            } else if runnable.iter().any(|t| t.id == interrupt_task) {
2306                // The parked run(s) are gone, but the interrupt task has not
2307                // been dispatched yet on this tick - `interrupt_gate` is
2308                // what lets it through next.
2309                Interrupt::Parking {
2310                    parked,
2311                    interrupt_task,
2312                }
2313            } else {
2314                // The interrupt task itself is no longer runnable - see this
2315                // function's own doc on abandonment. The parked run(s) still
2316                // get their guaranteed resume; there is simply no interrupt
2317                // to run ahead of them any longer.
2318                Interrupt::Resuming { parked }
2319            }
2320        }
2321        Interrupt::Running {
2322            parked,
2323            interrupt_task,
2324        } => {
2325            if in_flight.contains(&interrupt_task) {
2326                Interrupt::Running {
2327                    parked,
2328                    interrupt_task,
2329                }
2330            } else {
2331                // The interrupt task's own run reached a terminal status,
2332                // whichever one - this is the *only* trigger that moves the
2333                // sequence on, driven straight off the same in-flight
2334                // bookkeeping `poll` already reaps every tick, not a second,
2335                // independent poll of anything.
2336                Interrupt::Resuming { parked }
2337            }
2338        }
2339        Interrupt::Resuming { parked } => {
2340            if in_flight.iter().any(|id| parked.contains(id)) {
2341                // One of the parked runs has been dispatched - the resume
2342                // this sequence owed is fulfilled. Whatever else is left in
2343                // `parked` (ordinarily nothing, at the default concurrency
2344                // of one) rejoins ordinary priority-order dispatch, same as
2345                // any other runnable task.
2346                Interrupt::Idle
2347            } else if runnable.iter().any(|t| parked.contains(&t.id)) {
2348                Interrupt::Resuming { parked }
2349            } else {
2350                // Abandonment (see this function's own doc): nothing left in
2351                // `parked` is even runnable any longer.
2352                Interrupt::Idle
2353            }
2354        }
2355    }
2356}
2357
2358/// [`Interrupt`], but with [`crate::config::Daemon::pause_for_interrupts`]
2359/// folded in: disabled, the sequence can never leave [`Interrupt::Idle`], so
2360/// a task marked [`Task::interrupt`] on a daemon that has not opted in is
2361/// indistinguishable from any other runnable task - exactly the "off does
2362/// nothing" this feature promises.
2363fn advance_interrupt_tick(
2364    enabled: bool,
2365    state: Interrupt,
2366    in_flight: &[String],
2367    runnable: &[Task],
2368) -> Interrupt {
2369    if !enabled {
2370        return Interrupt::Idle;
2371    }
2372    advance_interrupt(state, in_flight, runnable)
2373}
2374
2375/// Which of this tick's runnable candidates the interrupt sequence actually
2376/// allows to be dispatched. Pure, and separate from [`advance_interrupt`] so
2377/// each half is assertable on its own: this is the half that keeps a
2378/// competition and an interrupt from ever running at the same moment.
2379fn interrupt_gate(state: &Interrupt, in_flight: &[String], candidates: Vec<Task>) -> Vec<Task> {
2380    match state {
2381        Interrupt::Idle => candidates,
2382        Interrupt::Parking {
2383            parked,
2384            interrupt_task,
2385        } => {
2386            if in_flight.iter().any(|id| parked.contains(id)) {
2387                Vec::new()
2388            } else {
2389                candidates
2390                    .into_iter()
2391                    .filter(|t| &t.id == interrupt_task)
2392                    .collect()
2393            }
2394        }
2395        Interrupt::Running { .. } => Vec::new(),
2396        // At most one: even if `parked` names more than one id (more than
2397        // one run was in flight when the sequence began, only possible
2398        // above the default `max_concurrent_runs = 1`), only the first match
2399        // is offered. Capping this to a single candidate - not merely to
2400        // `parked`'s own ids - is what makes "exactly one resume, never two
2401        // dispatched together" true regardless of how many ordinary slots
2402        // happen to be free this tick.
2403        Interrupt::Resuming { parked } => candidates
2404            .into_iter()
2405            .find(|t| parked.contains(&t.id))
2406            .into_iter()
2407            .collect(),
2408    }
2409}
2410
2411/// The daemon-loop knobs [`poll`] needs from [`crate::config::Daemon`],
2412/// bundled into one parameter so `poll`'s own signature stays readable -
2413/// see [`drive`]'s call site for where these are actually read.
2414struct DispatchLimits {
2415    /// How many *ordinary* candidates run at once. See
2416    /// [`crate::config::Daemon::max_concurrent_runs`].
2417    max_concurrent: usize,
2418    /// See [`crate::config::Daemon::pause_for_interrupts`].
2419    pause_for_interrupts: bool,
2420}
2421
2422/// Which semaphore, if any, dispatching a candidate should draw its permit
2423/// from.
2424///
2425/// Pure and separate from [`poll`]'s loop body for the same reason
2426/// [`advance_interrupt`] is: the choice between "skip the ordinary pool
2427/// entirely", "spend the one urgent slot" and "spend an ordinary slot" is
2428/// exactly the policy this feature adds, and a policy only exercisable by
2429/// running the whole loop is a policy nobody checks.
2430#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2431enum PermitKind {
2432    /// A land-merge resume (see [`LandResume::Ready`]): bypasses every slot.
2433    /// Checked first - a task can be both a land resume and marked
2434    /// [`Task::urgent`], and the resume's own "must not queue behind
2435    /// anything" guarantee takes precedence.
2436    None,
2437    /// [`Task::urgent`]: the one extra slot in `urgent_sem`, spent instead of
2438    /// (never in addition to trying) the ordinary pool. This is what lets
2439    /// an urgent task dispatch while every ordinary slot is already checked
2440    /// out, and what stops a second urgent task from opening a third run: it
2441    /// waits on this same one-slot semaphore rather than falling through to
2442    /// the ordinary one.
2443    Urgent,
2444    /// The ordinary `max_concurrent_runs` pool - unaffected by either of the
2445    /// above.
2446    Ordinary,
2447}
2448
2449/// [`PermitKind`] for one candidate. `priority` is [`LandResume::Ready`]'s
2450/// own boolean, already computed by the caller from [`land_resume_state`].
2451fn permit_kind(priority: bool, urgent: bool) -> PermitKind {
2452    if priority {
2453        PermitKind::None
2454    } else if urgent {
2455        PermitKind::Urgent
2456    } else {
2457        PermitKind::Ordinary
2458    }
2459}
2460
2461struct DispatchClaim {
2462    task: Task,
2463    claim: crate::queue::Claim,
2464    permit: Option<tokio::sync::OwnedSemaphorePermit>,
2465}
2466
2467/// Claim and re-read before choosing a lane: a queue listing may predate an
2468/// urgent toggle. All unsuccessful paths release the claim without an attempt.
2469fn claim_dispatch(
2470    queue: &Queue,
2471    candidate: &Task,
2472    cooling_down: bool,
2473    ordinary: &Arc<tokio::sync::Semaphore>,
2474    urgent: &Arc<tokio::sync::Semaphore>,
2475) -> Option<DispatchClaim> {
2476    let Ok(claim) = queue.claim(&candidate.id) else {
2477        tracing::info!("task {} is claimed elsewhere; skipping", candidate.short());
2478        return None;
2479    };
2480    let task = match queue.get(&candidate.id) {
2481        Ok(t) if t.status.runnable() => t,
2482        Ok(_) => return None,
2483        Err(e) => {
2484            tracing::warn!("could not re-read task {}: {e:#}", candidate.short());
2485            return None;
2486        }
2487    };
2488    let resume = land_resume_state(&task);
2489    if resume == LandResume::StillWaiting {
2490        return None;
2491    }
2492    let priority = resume == LandResume::Ready;
2493    if !priority && cooling_down {
2494        return None;
2495    }
2496    // Never borrow an ordinary slot when the single urgent slot is busy.
2497    // Keep scanning other candidates when either pool has no free slot.
2498    let permit = match permit_kind(priority, task.urgent) {
2499        PermitKind::None => None,
2500        PermitKind::Urgent => Some(Arc::clone(urgent).try_acquire_owned().ok()?),
2501        PermitKind::Ordinary => Some(Arc::clone(ordinary).try_acquire_owned().ok()?),
2502    };
2503    Some(DispatchClaim {
2504        task,
2505        claim,
2506        permit,
2507    })
2508}
2509
2510/// Poll the queue until stopped, factored out so [`drive`] owns only setup and
2511/// teardown and cannot skip the teardown on an early return.
2512///
2513/// `limits.max_concurrent` bounds how many *ordinary* candidates run at once,
2514/// see [`crate::config::Daemon::max_concurrent_runs`]. A run parked on a
2515/// land approval that has since been answered is dispatched outside that
2516/// bound the moment [`land_resume_state`] reports it [`LandResume::Ready`]:
2517/// the whole point of parking there is that it must not queue behind
2518/// whatever else the loop happens to be running, even at the default of one.
2519/// A [`Task::urgent`] candidate is exempt from the same bound the same way,
2520/// through its own one-slot `urgent_sem` (see [`permit_kind`]). Every one of
2521/// these exemptions is still subject to the interrupt gate below, urgent
2522/// included: a land-resume or urgent candidate is exactly as much "something
2523/// else running" as an ordinary one from the interrupt sequence's point of
2524/// view, and letting either slip through while a run is being parked, or
2525/// while the interrupt task itself has the floor, is precisely the second
2526/// run 75dd's own "at most one run, ever, at once" guarantee must never
2527/// allow - see [`Interrupt`]'s own doc.
2528async fn poll(
2529    opts: &Opts,
2530    queue: &Queue,
2531    status: &Arc<Mutex<Status>>,
2532    home: &Path,
2533    worktrees_root: &Path,
2534    stop: &Stop,
2535    limits: DispatchLimits,
2536) -> Result<()> {
2537    let DispatchLimits {
2538        max_concurrent,
2539        pause_for_interrupts,
2540    } = limits;
2541    // Only consulted by `once`, where a task that just failed is still
2542    // `runnable` and would otherwise be picked up again inside the same drain.
2543    // In the long-running mode a later poll retrying a failed task is the point,
2544    // and the attempt counter is what bounds it.
2545    let mut attempted: Vec<String> = Vec::new();
2546    let sem = Arc::new(tokio::sync::Semaphore::new(max_concurrent));
2547    // One extra, permanent slot for `--urgent` tasks (see `Task::urgent`),
2548    // entirely separate from `sem`: an urgent candidate must dispatch
2549    // alongside whatever `sem` already has checked out, never by waiting for
2550    // one of those ordinary slots to free up and never by growing
2551    // `max_concurrent_runs` itself. Sized at one, not unbounded - see
2552    // `permit_kind`'s own doc - so a second `--urgent` task queues behind the
2553    // first on this same slot rather than opening a third run.
2554    let urgent_sem = Arc::new(tokio::sync::Semaphore::new(1));
2555    // A quota hit is a fact about the machine, not the task that happened to
2556    // surface it, and every other *ordinary* candidate is no less likely to
2557    // hit the same wall - see the warning below. A land-merge resume is
2558    // exempt: it is a human decision finishing, not a fresh competition, and
2559    // must not sit out a quota cooldown it did not cause.
2560    let quota_cooldown_until: Arc<Mutex<Option<Timestamp>>> = Arc::new(Mutex::new(None));
2561    let mut inflight: tokio::task::JoinSet<()> = tokio::task::JoinSet::new();
2562    let mut conductor = Conductor::new();
2563    // See `maybe_prune_cache_between_runs`'s own doc: this is the cache check
2564    // a congested queue would otherwise starve of the fully-idle branch below.
2565    let mut cache_last_checked: Option<Timestamp> = None;
2566    // See `Interrupt`'s own doc: in-memory only, advanced once per tick.
2567    let mut interrupt = Interrupt::Idle;
2568    // The `Pause` handed to each dispatched candidate's own `Runner` - see
2569    // `attempt`'s new parameter - kept here so the tick that decides to park
2570    // a run for an interrupt can reach that specific run's handle and no
2571    // other's. Pruned to whatever is still in flight at the top of every
2572    // tick, so a finished attempt's handle does not linger.
2573    let mut interrupt_pauses: std::collections::HashMap<String, crate::graph::Pause> =
2574        std::collections::HashMap::new();
2575
2576    while !stop.stopped() {
2577        lock(status).polls += 1;
2578
2579        // Reap whatever finished since the last tick without blocking on
2580        // anything still running. `InFlightGuard` already released the slot
2581        // even if the spawned attempt panicked; this only surfaces that it
2582        // happened, since a panic swallowed here otherwise leaves no trace.
2583        while let Some(result) = inflight.try_join_next() {
2584            if let Err(e) = result {
2585                tracing::error!("a spawned attempt did not finish cleanly: {e}");
2586                notices::raise(Notice::error(
2587                    "loop:attempt",
2588                    "A queued attempt ended abnormally; check the task it was running.",
2589                ));
2590            }
2591        }
2592
2593        let swept = sweep_stale_claims(queue, STALE_CLAIM);
2594        if !swept.is_empty() {
2595            tracing::warn!(
2596                "swept {} stale claim(s) left behind by an earlier daemon: {}",
2597                swept.len(),
2598                swept.join(", ")
2599            );
2600        }
2601        // Capture stalled work before reclaiming it. A dead daemon's ordinary
2602        // lock is swept and reclaimed in this same poll, but the conductor
2603        // must still see that it was stranded rather than only its mechanical
2604        // terminal state.
2605        let now = Timestamp::now();
2606
2607        // No run this daemon spawned is mid-compile right now, whether or
2608        // not another candidate is about to start - see
2609        // `maybe_prune_cache_between_runs`'s own doc for why this cannot
2610        // wait for the queue to run dry.
2611        if !stop.busy_now() {
2612            maybe_prune_cache_between_runs(
2613                &opts.repo,
2614                opts,
2615                home,
2616                stop,
2617                &mut cache_last_checked,
2618                now,
2619            )
2620            .await;
2621        }
2622
2623        let stalled = stalled_tasks(queue, home, now);
2624        let stalled_ids: std::collections::BTreeSet<_> =
2625            stalled.iter().map(|task| task.id.clone()).collect();
2626        let reclaimed = reclaim_orphaned_running(queue, opts.max_attempts);
2627        if !reclaimed.is_empty() {
2628            tracing::warn!(
2629                "reclaimed {} task(s) left `running` by a daemon that never \
2630                 recorded the outcome: {}",
2631                reclaimed.len(),
2632                reclaimed.join(", ")
2633            );
2634        }
2635        let abandoned_runs = reclaim_abandoned_runs(home, now);
2636        if !abandoned_runs.is_empty() {
2637            tracing::warn!(
2638                "failed {} run(s) left behind by a killed process, past every \
2639                 active seat's own timeout: {}",
2640                abandoned_runs.len(),
2641                abandoned_runs.join(", ")
2642            );
2643        }
2644
2645        // `home`, not `ask::Questions::open()`'s own process-global default:
2646        // `poll` is handed its home explicitly precisely so a test can point
2647        // it elsewhere, the same reason `Queue::at` and the status file path
2648        // are parameters rather than resolved here - see `drive`'s own doc.
2649        let questions = Questions::at(home.join("questions"));
2650
2651        // Deterministic: no model, run before the conductor sees anything so
2652        // its input reflects the queue's current, already-resolved state.
2653        resolve_blockers(queue, &questions);
2654        apply_choice_actions(queue, &questions, home);
2655        triage::apply_answers(queue, &questions);
2656        reconcile_task_questions(queue, &questions);
2657        // Tell the chat a filed task or watched command has ended. Reconciled
2658        // here regardless of how it ended, and started only when no
2659        // upgrade is parking (the draft then waits for a manual resume).
2660        {
2661            let talks = crate::talk::Talks::at(home.join("talks"));
2662            crate::chat_notify::sweep(queue, &talks, home);
2663            crate::chat_watch::sweep(home, &talks);
2664            crate::chat_notify::start_turns(&talks, stop.parking(), &mut inflight);
2665        }
2666
2667        // The conductor gets one look per cycle, right before the loop takes
2668        // its next task, and only when there is something new to look at -
2669        // see `Conductor::worth_a_look`'s own doc for why "stalled is
2670        // non-empty" is the wrong test. Checked before `prepare` so an
2671        // unchanged cycle never pays for a synchronous config load.
2672        let finished: Vec<Task> = finished_tasks(queue)
2673            .into_iter()
2674            .filter(|task| !stalled_ids.contains(&task.id))
2675            // A parked run is waiting for this loop, not for a decision: the
2676            // conductor must not get to hold it or requeue it (a requeue
2677            // sets `fresh_start` and re-competes) before `attempt` resumes.
2678            .filter(|task| !awaiting_resume_with(task, |id| RunState::load_under(id, home)))
2679            .collect();
2680        let queued = queued_tasks(queue);
2681        // An empty queue has nothing to arrange. In particular, do not let
2682        // the conductor's initial snapshot cause synchronous config I/O
2683        // between the caller's stop notification and the idle wait below.
2684        if !(queued.is_empty() && stalled.is_empty() && finished.is_empty())
2685            && conductor.worth_a_look(queue, &stalled, &finished)
2686        {
2687            match prepare(&opts.repo, opts) {
2688                Ok(cfg) => {
2689                    conductor
2690                        .maybe_run(
2691                            &cfg,
2692                            &opts.repo,
2693                            queue,
2694                            &questions,
2695                            home,
2696                            &queued,
2697                            &stalled,
2698                            &finished,
2699                            opts.max_attempts,
2700                        )
2701                        .await;
2702                }
2703                Err(e) => {
2704                    tracing::warn!("conductor: no config: {e:#}");
2705                    notices::raise(Notice::warn(
2706                        "loop:no-config",
2707                        "The loop could not read this repository's config, so held tasks are not being triaged.",
2708                    ));
2709                }
2710            }
2711        }
2712
2713        let candidates: Vec<Task> = runnable(queue)
2714            .into_iter()
2715            .filter(|t| !opts.once || !attempted.contains(&t.id))
2716            .collect();
2717
2718        // A task id only stays a key here while its attempt is genuinely in
2719        // flight; `status.current` is the same liveness fact `InFlightGuard`
2720        // maintains for the phone's own status file, so this piggybacks on
2721        // it rather than tracking a second copy of the same thing.
2722        let in_flight: Vec<String> = lock(status)
2723            .current
2724            .iter()
2725            .map(|c| c.task.clone())
2726            .collect();
2727        interrupt_pauses.retain(|id, _| in_flight.contains(id));
2728
2729        interrupt =
2730            advance_interrupt_tick(pause_for_interrupts, interrupt, &in_flight, &candidates);
2731        if let Interrupt::Parking {
2732            parked,
2733            interrupt_task,
2734        } = &interrupt
2735        {
2736            let reason = format!(
2737                "task {} asked to run first",
2738                crate::run::short_of(interrupt_task)
2739            );
2740            for id in parked {
2741                if let Some(pause) = interrupt_pauses.get(id) {
2742                    pause.park_because(reason.clone());
2743                }
2744            }
2745        }
2746        let candidates = interrupt_gate(&interrupt, &in_flight, candidates);
2747
2748        let cooling_down =
2749            lock(&quota_cooldown_until).is_some_and(|until| Timestamp::now() < until);
2750
2751        let mut started_any = false;
2752        for candidate in candidates {
2753            if stop.stopped() {
2754                break;
2755            }
2756
2757            let Some(DispatchClaim {
2758                mut task,
2759                claim,
2760                permit,
2761            }) = claim_dispatch(queue, &candidate, cooling_down, &sem, &urgent_sem)
2762            else {
2763                continue;
2764            };
2765
2766            let task_id = task.id.clone();
2767            attempted.push(task_id.clone());
2768            lock(status).idle = false;
2769            // A stop asked for from here on is "finishing", not "stopped": the
2770            // run gets to reach a terminal status before the loop returns.
2771            stop.enter();
2772            started_any = true;
2773
2774            // A fresh, unshared handle - never `stop.pause()` - so parking
2775            // this run for an interrupt cannot leak into any other run this
2776            // loop ever drives. See `Pause`'s own doc.
2777            let run_pause = crate::graph::Pause::new();
2778            interrupt_pauses.insert(task_id.clone(), run_pause.clone());
2779
2780            let opts = opts.clone();
2781            let queue = queue.clone();
2782            let status = Arc::clone(status);
2783            let stop = stop.clone();
2784            let quota_cooldown_until = Arc::clone(&quota_cooldown_until);
2785            inflight.spawn(async move {
2786                // Held for the whole attempt: dropping either at the end of
2787                // this task is what releases the claim and, for an ordinary
2788                // candidate, frees its concurrency slot back to the loop.
2789                let _claim = claim;
2790                let _permit = permit;
2791                // See `InFlightGuard`: this must survive a panic inside `attempt`.
2792                let _inflight = InFlightGuard {
2793                    status: &status,
2794                    stop: &stop,
2795                    task_id: &task_id,
2796                };
2797                let quota = attempt(&opts, &queue, &status, &stop, run_pause, &mut task).await;
2798                lock(&status).completed += 1;
2799                // A quota loss is a fact about the machine, not this task, and
2800                // the next ordinary candidate the loop offers is no less
2801                // likely to hit the same wall: without a cooldown here a
2802                // whole backlog can be run - and failed - in the seconds it
2803                // takes each attempt to notice the CLI is out of quota.
2804                let now = Timestamp::now();
2805                if let Some(until) = cooldown_until(&quota, now) {
2806                    let wait = until.as_second() - now.as_second();
2807                    *lock(&quota_cooldown_until) = Some(until);
2808                    let hint = quota
2809                        .iter()
2810                        .find(|q| q.reset.is_some())
2811                        .and_then(|q| q.reset.as_deref());
2812                    match hint {
2813                        Some(h) => tracing::warn!(
2814                            "quota hit; waiting {wait}s before taking another ordinary task \
2815                             (CLI reported reset: {h})"
2816                        ),
2817                        None => tracing::warn!(
2818                            "quota hit; waiting {wait}s before taking another ordinary task \
2819                             (no reset hint reported)"
2820                        ),
2821                    }
2822                }
2823            });
2824        }
2825
2826        if started_any {
2827            continue;
2828        }
2829
2830        if stop.busy_now() {
2831            // Something started on an earlier tick is still running. Recheck
2832            // soon rather than sleeping out the whole poll interval - a freed
2833            // slot, or a land approval answered mid-run, must not sit idle
2834            // for it.
2835            stop.idle(RECHECK_WHILE_BUSY.min(opts.poll)).await;
2836            continue;
2837        }
2838
2839        // Truly idle: nothing new to start and nothing still running.
2840        lock(status).idle = true;
2841        if opts.once {
2842            // A one-shot drain must perform the same post-work cleanup as a
2843            // daemon that reached a normal idle interval. The startup pass
2844            // cannot see runs or cache files produced by this drain.
2845            janitor(&opts.repo, opts, home, worktrees_root).await;
2846            resweep_superseded_attempts(queue, home);
2847            triage_held(queue, home, opts).await;
2848            break;
2849        }
2850        stop.idle(opts.poll).await;
2851        if stop.stopped() {
2852            continue;
2853        }
2854        // Housekeeping only after a full quiet interval. Running it before
2855        // the first idle wait can block the executor while an operator's
2856        // stop request is waiting to be scheduled, defeating Stop's retained
2857        // wake permit. No run can start while this branch is active, so the
2858        // janitor still never races an in-flight compile.
2859        janitor(&opts.repo, opts, home, worktrees_root).await;
2860        resweep_superseded_attempts(queue, home);
2861        triage_held(queue, home, opts).await;
2862    }
2863
2864    // Never return while a run is still in flight, whichever way the loop
2865    // above exited: a stop only sets a flag - see `serve_until` - and
2866    // returning here while `inflight` still holds spawned work would abandon
2867    // it exactly as a mid-node kill would.
2868    while let Some(result) = inflight.join_next().await {
2869        if let Err(e) = result {
2870            tracing::error!("a spawned attempt did not finish cleanly: {e}");
2871            notices::raise(Notice::error(
2872                "loop:attempt",
2873                "A queued attempt ended abnormally; check the task it was running.",
2874            ));
2875        }
2876    }
2877    Ok(())
2878}
2879
2880/// Run one claimed task to a terminal status and record the outcome.
2881///
2882/// Every transition is flushed to the queue as it happens, so the state on disk
2883/// is what actually occurred rather than what this process still intends to
2884/// write.
2885async fn attempt(
2886    opts: &Opts,
2887    queue: &Queue,
2888    status: &Arc<Mutex<Status>>,
2889    stop: &Stop,
2890    interrupt_pause: crate::graph::Pause,
2891    task: &mut Task,
2892) -> Vec<QuotaLoss> {
2893    let repo = repo_for(task, &opts.repo);
2894    tracing::info!(
2895        "task {} — {} (repo {})",
2896        task.short(),
2897        task.title,
2898        repo.display()
2899    );
2900
2901    // The repository's magi.toml comes from `<remote>/<base>`, fetched now. A
2902    // failure here is the network's or the operator's, not the task's: hold
2903    // without spending an attempt, never fall back to a possibly stale copy.
2904    let explicit = match &task.overrides {
2905        Some(o) if !o.inherit_daemon_options => o.config.as_deref(),
2906        Some(_) => opts.config.as_deref(),
2907        None => opts.config.as_deref(),
2908    };
2909    if let Err(e) = Config::discover_fetched(&repo, explicit).await {
2910        let reason = format!("[config] {e:#}");
2911        task.last_error = Some(reason.clone());
2912        task.hold_machine(Some(reason.clone()));
2913        record(queue, task);
2914        tracing::warn!("holding {}: {reason}", task.short());
2915        return Vec::new();
2916    }
2917    let mut config = match prepare_for(&repo, opts, task) {
2918        Ok(c) => c,
2919        Err(e) => {
2920            // A setup failure spends an attempt even though no run was minted.
2921            // Without that, a task naming a repository that does not exist
2922            // would be retried at every poll for as long as the daemon lives.
2923            task.attempts += 1;
2924            task.fail(format!("config: {e:#}"), opts.max_attempts);
2925            record(queue, task);
2926            return Vec::new();
2927        }
2928    };
2929    apply_task_implementers(&mut config, task);
2930    let p = phrases(&config.graph.language);
2931    let start_failed = p.could_not_start;
2932
2933    // The free-space gate, checked *before* anything is minted: a task that
2934    // waits out a full disk costs nothing yet, and must not spend an attempt
2935    // or start a run the machine cannot finish. Held tasks stay in the list
2936    // for the human to see, and `magi task release` re-queues them when space
2937    // comes back - the same recovery as any other hold. A volume whose free
2938    // space cannot be measured closes the gate too: starting a run blind on a
2939    // disk that may be full is how the machine ends up with 6.7 GB free.
2940    if let Some(reason) = disk_gate(&repo, &config) {
2941        task.last_error = Some(reason.clone());
2942        task.hold_machine(Some(reason.clone()));
2943        record(queue, task);
2944        tracing::warn!("holding {} for want of disk space: {reason}", task.short());
2945        // Stable wording - the free-space figures live in the task's hold
2946        // reason, and changing numbers would relight the bell every poll.
2947        notices::raise(
2948            Notice::warn(
2949                &format!("disk:{}", repo.display()),
2950                "A task was held for want of free disk space; free some, then release it from the queue.",
2951            )
2952            .link(Link::Task {
2953                id: task.id.clone(),
2954            }),
2955        );
2956        return Vec::new();
2957    }
2958
2959    // A resumable run of this task is carried on, never re-competed. The
2960    // candidates are built and paid for, and a fresh competition races a
2961    // second implementation against them.
2962    //
2963    // Two runs paid for that lesson. Run 01c2 was blocked and the loop
2964    // started 3cbf on the same task a moment later, duplicating two and a
2965    // half hours of agent work. Then b25f stalled on a judge that timed out
2966    // and one that answered with no JSON - `quota: 0`, so nothing the machine
2967    // was to blame for - and 4043 started **one second** later, buying three
2968    // fresh implementations to reach the same panel. `RunStatus::resumable`
2969    // rather than `!done()` is what catches the second case: a stall is
2970    // terminal, and its cheap recovery re-asks only the absent seats.
2971    //
2972    // A load failure is warned about rather than silently read as "not
2973    // resumable": the alternative is exactly what let a schema mismatch on
2974    // run `eba2` fall through to a full re-competition with nobody told why.
2975    // `crate::conduct` is what actually offers a better answer than
2976    // `Runner::start` here (see `Recovery::Review`), once this task's next
2977    // failure shows it up as `held`/`failed` with the run state unreadable.
2978    let unfinished = (!task.fresh_start)
2979        .then(|| unfinished_run(&task.runs, task.short()))
2980        .flatten()
2981        .filter(|id| RunState::load(id).is_ok_and(|run| may_resume_run(&run, task)));
2982    // `crate::conduct` chose `Review` for this task on an earlier cycle: its
2983    // branch survived, and this reopens exactly that branch as a
2984    // review-only pass rather than resuming or competing again. Consumed
2985    // (cleared) here whichever way this goes, so it never outlives this one
2986    // attempt - see `queue::Task::review_branch`.
2987    let previous = task.runs.last().and_then(|id| RunState::load(id).ok());
2988    let review_branch = take_review_branch(task, previous.as_ref());
2989    let branch_exists = match &review_branch {
2990        Some(branch) => crate::git::branch_exists(&repo, branch)
2991            .await
2992            .unwrap_or(false),
2993        None => false,
2994    };
2995    // An explicit `magi review <branch>` is reviewed whatever happens to the
2996    // branch unless an explicit implementer change requires fresh work. An
2997    // unfinished run of the same task still wins, so a retry resumes.
2998    let proposed = match (&review_branch, &unfinished, &task.review_of) {
2999        (None, None, Some(branch)) => Starter::Review(branch.clone()),
3000        _ => choose_starter(
3001            review_branch.as_deref(),
3002            branch_exists,
3003            unfinished.as_deref(),
3004        ),
3005    };
3006    let starter = if let Some(selected) = &task.pr_recovery {
3007        if selected.check_task(&task.id, &task.instruction).is_ok() {
3008            selected_starter(selected, previous.as_ref(), unfinished.as_deref())
3009        } else {
3010            Starter::Review(selected.branch.clone())
3011        }
3012    } else {
3013        starter_with_implementers(task, previous.as_ref(), proposed)
3014    };
3015    // A repair pin must never degrade into a new competition or takeover if
3016    // the owner disappears between the answer and dispatch.
3017    if let Some(pin) = &task.resume_override
3018        && let Some(run) = &pin.pinned_run
3019        && Questions::open().get(&pin.question_id).map_or(true, |q| {
3020            q.node == land::APPROVAL_NODE && q.resolution().as_deref() == Some(land::REPAIR)
3021        })
3022        && starter != Starter::Resume(run.clone())
3023    {
3024        task.hold_machine(Some(format!(
3025            "repair owner cannot resume; inspect `magi show {run}` and restore the existing run before `magi task release {}`; no replacement task was started",
3026            task.id
3027        )));
3028        record(queue, task);
3029        return Vec::new();
3030    }
3031    // Absolute paths of the files the task was filed with, re-read from the
3032    // task now so a file attached while it was held reaches a resumed run
3033    // too. A stored name whose file has gone is an error before any agent is
3034    // paid for, never a silent omission the implementer cannot notice.
3035    let attachments = match task_attachments(queue, task) {
3036        Ok(a) => a,
3037        Err(e) => {
3038            task.attempts += 1;
3039            task.fail(format!("{start_failed}{e:#}"), opts.max_attempts);
3040            record(queue, task);
3041            return Vec::new();
3042        }
3043    };
3044    let started = match &starter {
3045        Starter::Review(_) if task.pr_recovery.is_some() => {
3046            let selected = task.pr_recovery.clone().expect("selected recovery");
3047            match selected.check_task(&task.id, &task.instruction) {
3048                Ok(()) => Runner::recover_pr(&repo, &task.id, selected, config)
3049                    .await
3050                    .map(|mut r| {
3051                        r.state.attachments = attachments.clone();
3052                        r
3053                    }),
3054                Err(e) => Err(e),
3055            }
3056        }
3057        Starter::Review(branch) => {
3058            tracing::info!(
3059                "task {} reopens `{branch}` as a review-only pass",
3060                task.short()
3061            );
3062            // Every earlier attempt is superseded by the one being minted
3063            // now, so a branch one of them still holds may be taken over.
3064            let takeover = crate::handover::Takeover {
3065                earlier: task.earlier_attempts().to_vec(),
3066                home: crate::run::home(),
3067                choice: take_divergence_answer(branch, &config.merge.remote, task),
3068                task_instruction: Some(instruction_for(task)),
3069            };
3070            Runner::review_taking_over(
3071                &repo,
3072                branch,
3073                config,
3074                Some(takeover),
3075                crate::run::Origin::queue(&task.id),
3076            )
3077            .await
3078        }
3079        Starter::Resume(id) => {
3080            tracing::info!("resuming run {id} rather than competing again");
3081            Runner::resume(id).and_then(|mut r| {
3082                if r.state.prep_failure.is_some() {
3083                    let approval = task
3084                        .resumption
3085                        .as_ref()
3086                        .context("preparation retry requires an explicit task release")?;
3087                    r.retry_preparation(approval.at)?;
3088                }
3089                if let Some(instruction) =
3090                    prepare_instruction(&starter, Some(&r.state.instruction), task)
3091                {
3092                    r.state.instruction = instruction;
3093                }
3094                r.state.attachments = attachments.clone();
3095                // The merge choice of a hand-started task still holds when the
3096                // run it resumes was started under another one.
3097                if let Some(mode) = task
3098                    .overrides
3099                    .as_ref()
3100                    .and_then(|o| o.merge.as_deref())
3101                    .and_then(|m| merge_mode(m).ok())
3102                {
3103                    r.state.config.merge.mode = mode;
3104                }
3105                Ok(r)
3106            })
3107        }
3108        Starter::Start => {
3109            if let Some(branch) = &review_branch {
3110                tracing::warn!(
3111                    "conductor chose review for task {} but branch `{branch}` no longer \
3112                     exists; requeuing as a fresh competition instead",
3113                    task.short()
3114                );
3115            }
3116            let instruction = prepare_instruction(&starter, None, task)
3117                .unwrap_or_else(|| task.instruction.clone());
3118            Runner::start_naming(
3119                &repo,
3120                instruction,
3121                &task.title,
3122                config,
3123                crate::run::Origin::queue(&task.id),
3124            )
3125            .await
3126            .map(|mut r| {
3127                r.state.attachments = attachments.clone();
3128                r
3129            })
3130        }
3131    };
3132    let mut runner = match started {
3133        Ok(r) => r,
3134        // A branch an earlier attempt still holds and magi will not release
3135        // by itself is the operator's call, not a failed attempt: hold the
3136        // task with the reason, spend nothing, and say so on the bell.
3137        Err(e) if task.pr_recovery.is_some() => {
3138            task.attempts += 1;
3139            if let Some(partial) = e.downcast_ref::<crate::pr_recovery::StartupFailed>() {
3140                task.link_run(&partial.run);
3141                task.record_pass(&partial.run, 0, task.attempts, None);
3142            }
3143            task.last_error = Some(format!("{start_failed}{e:#}"));
3144            task.hold_unrequested_retry(Some(format!(
3145                "Selected public-head recovery could not start: {e:#}. Correct the cause and use `magi task recover-pr` with the approved public head; automatic recovery is stopped."
3146            )));
3147            record(queue, task);
3148            return Vec::new();
3149        }
3150        Err(e) if e.downcast_ref::<crate::handover::Refused>().is_some() => {
3151            let detail = match e.downcast_ref::<crate::handover::Refused>() {
3152                Some(r) => (p.handover_refused)(r),
3153                None => format!("{e:#}"),
3154            };
3155            let reason = format!("{start_failed}{detail}");
3156            task.last_error = Some(reason.clone());
3157            // Consumed above by `take`; keep it so the released task retries
3158            // as a review of this same branch.
3159            let branch = match &starter {
3160                Starter::Review(branch) => Some(branch.clone()),
3161                _ => None,
3162            };
3163            // The hold's own `task:<id>` notice (raised by `Queue::put`) is the
3164            // one page; the guidance rides in the reason, fixed per state so a
3165            // re-put never changes the wording and relights it.
3166            task.hold_for_handover(branch, format!("{reason}{}", p.handover_hint));
3167            record(queue, task);
3168            tracing::warn!(
3169                "holding {} for a branch it cannot take over: {e:#}",
3170                task.short()
3171            );
3172            return Vec::new();
3173        }
3174        // A branch that differs between here and the remote in ways magi
3175        // cannot prove are the same change is a decision for the owner, not a
3176        // failed attempt: ask with both sides laid out, spend no attempt, and
3177        // let the answer requeue the task.
3178        Err(e) if e.downcast_ref::<crate::reconcile::Diverged>().is_some() => {
3179            let d = e
3180                .downcast_ref::<crate::reconcile::Diverged>()
3181                .expect("checked by the guard");
3182            let mut q = ask::Question::new(
3183                task.id.clone(),
3184                crate::reconcile::NODE.to_owned(),
3185                crate::reconcile::SEAT.to_owned(),
3186                d.summary(),
3187                d.detail(),
3188                d.choices(),
3189            );
3190            match Questions::open().put(&mut q) {
3191                Ok(()) => {
3192                    task.last_error = Some(format!("{e:#}"));
3193                    // Consumed above by `take`; the answer has to reach a review
3194                    // of this same branch, so keep pointing the task at it.
3195                    task.review_branch = Some(d.branch.clone());
3196                    task.review_instruction = Some(task.instruction.clone());
3197                    task.block(vec![q.id.clone()], Some(d.summary()));
3198                }
3199                Err(put) => {
3200                    tracing::warn!("could not file the divergence question: {put:#}");
3201                    task.attempts += 1;
3202                    task.fail(format!("{start_failed}{e:#}"), opts.max_attempts);
3203                }
3204            }
3205            record(queue, task);
3206            return Vec::new();
3207        }
3208        Err(e) => {
3209            // The answer was stale and nothing was applied: keep pointing at
3210            // the branch so the next attempt asks again against its new tips.
3211            // Nothing went wrong with the task, so no attempt is spent: a
3212            // budget one short of its limit must not hold it instead of
3213            // letting the question be asked again.
3214            if e.downcast_ref::<crate::reconcile::Stale>().is_some()
3215                && let Starter::Review(branch) = &starter
3216            {
3217                task.review_branch = Some(branch.clone());
3218                task.review_instruction = Some(task.instruction.clone());
3219                task.last_error = Some(format!("{start_failed}{e:#}"));
3220                task.status = crate::queue::TaskStatus::Failed;
3221                record(queue, task);
3222                return Vec::new();
3223            }
3224            task.attempts += 1;
3225            task.last_error = Some(format!("{start_failed}{e:#}"));
3226            task.hold_unrequested_retry(Some(format!(
3227                "Startup failed: {e:#}. Correct the startup source or configuration, then explicitly release the task; automatic recovery is stopped."
3228            )));
3229            record(queue, task);
3230            return Vec::new();
3231        }
3232    };
3233    runner.state.task_instruction = Some(task.instruction.clone());
3234    // The follow-up depth is fixed now, while the task certainly exists:
3235    // deleting it later must not reset the cap (`crate::followup`).
3236    runner.state.followup_generation = Some(task.followup.as_ref().map_or(0, |f| f.generation));
3237    if runner.state.origin_chat.is_none() {
3238        runner.state.origin_chat = task.chat_talk().map(str::to_owned);
3239    }
3240    // A stop that means "park" reaches the graph through this handle.
3241    runner.on_pause(stop.pause());
3242    // `poll`'s interrupt scheduler reaches this one run - and no other -
3243    // through this handle. See `Pause`'s own doc for why these are never
3244    // the same one.
3245    runner.watch_interrupt(interrupt_pause);
3246
3247    // `start` has minted the run, so the task can now point at it. Persisting
3248    // `Running` before `execute` is what makes a crash mid-run legible.
3249    let run = runner.state.id.clone();
3250    task.start(run.clone());
3251    record(queue, task);
3252    lock(status).current.push(Current {
3253        task: task.id.clone(),
3254        run,
3255    });
3256
3257    // `RunState::quota` is the run's whole history across every resume, so
3258    // only what this execution added may arm the cooldown or earn a refund.
3259    let quota_before = runner.state.quota.clone();
3260    runner.state.next_pass_task_entry = true;
3261    let result = runner.execute().await;
3262    finish_attempt(
3263        opts.max_attempts,
3264        queue,
3265        task,
3266        &runner.state,
3267        &quota_before,
3268        result,
3269    )
3270}
3271
3272/// Settle `task` after its run executed: the verdict, the diagnosis, the
3273/// supersession of earlier attempts and the save. Shared by the loop and by
3274/// `magi run --resume`, so an attempt is counted one way wherever it ran.
3275/// Returns the quota losses this execution added to the run.
3276pub fn finish_attempt(
3277    max_attempts: usize,
3278    queue: &Queue,
3279    task: &mut Task,
3280    state: &RunState,
3281    quota_before: &[QuotaLoss],
3282    result: Result<()>,
3283) -> Vec<QuotaLoss> {
3284    let detail = match result {
3285        Ok(()) => describe(state),
3286        Err(e) => format!("{e:#}"),
3287    };
3288    let fresh = losses_this_attempt(quota_before, &state.quota);
3289    let verdict = Verdict {
3290        status: state.status,
3291        // A run that opened a pull request handed its work over, whatever the
3292        // gate then decided about merging it.
3293        left_pr: state.pr.is_some(),
3294        // Only a rate limit earns the task its attempt back - and only one
3295        // suffered now: a refund justified by a previous session's loss is
3296        // the same mistake as re-arming the cooldown from it. A stalled run
3297        // resumed without hitting quota again therefore spends its attempt,
3298        // like any other failure of the task's own.
3299        quota_hit: !fresh.is_empty(),
3300        // A run that parked was asked to stop; that is not a failure and must
3301        // not spend an attempt, or replacing the binary a few times would
3302        // exhaust a task's budget without an agent ever misbehaving.
3303        parked: state.parked,
3304        // A quota loss that left nothing viable is the same machine fact as a
3305        // `Stalled` quota loss; see `settle`'s doc table.
3306        no_viable_candidates: state.viable().is_empty(),
3307    };
3308    settle_and_diagnose(task, verdict, &detail, max_attempts, state);
3309    if state.status == RunStatus::Blocked && state.prep_failure.is_some() {
3310        task.hold_unrequested_retry(Some(format!(
3311            "Candidate setup failed: {detail}. Select a complete implementation with `magi task recover-pr`, or correct the referenced source before explicitly releasing the task."
3312        )));
3313    }
3314    if task.status == TaskStatus::Done {
3315        supersede_prior_runs(task, &crate::run::home());
3316    }
3317    record(queue, task);
3318    tracing::info!(
3319        "task {} is {} after run {} ({})",
3320        task.short(),
3321        task.status.as_str(),
3322        state.short(),
3323        label(state.status)
3324    );
3325    fresh
3326}
3327
3328/// A task a hand-started run leaves runnable (failed with attempts to spare,
3329/// or requeued) is **held**: nobody asked for a retry, and a loop starting
3330/// later must not spend agent calls on it. `magi task release` retries.
3331pub fn hold_if_runnable(queue: &Queue, task: &mut Task) {
3332    // A run parked on the owner's word about a withheld PR title/body is not a
3333    // failure nobody asked to retry: it resumes (answer, or the neutral text on
3334    // the deadline) the moment a loop serves the queue, via `land_resume_state`.
3335    // Holding it would keep it from ever being a candidate.
3336    let waits_on_owner = withheld_text_wait(task).is_some();
3337    if task.status.runnable() && !waits_on_owner {
3338        let why = task.last_error.clone().map_or_else(
3339            || "the run did not finish".to_owned(),
3340            |e| format!("the run did not finish: {e}"),
3341        );
3342        task.hold_unrequested_retry(Some(format!(
3343            "{why}. It was started by hand, so it is not retried \
3344             automatically; `magi task release` retries it."
3345        )));
3346        record(queue, task);
3347    }
3348}
3349
3350/// Whether an attempt at a task whose newest run is `run` would resume that
3351/// run rather than start a fresh competition. `magi run --resume` hands a run
3352/// to a live loop only when this holds.
3353#[must_use]
3354pub fn loop_would_resume(run: &str) -> bool {
3355    unfinished_run(&[run.to_owned()], crate::run::short_of(run)).is_some()
3356}
3357
3358/// A loop in another process that is alive right now: a fresh heartbeat in
3359/// `<home>/daemon.json` published by a pid that is not `own_pid`. `Some(pid)`
3360/// carries the pid it published (`None` inside when it published none - a
3361/// fresh heartbeat is still evidence of a live loop).
3362///
3363/// The one judgement of "is somebody else serving this queue", shared by the
3364/// web UI's refusal to start a second loop and by `magi run` / `magi review`
3365/// deciding whether to hand a task to the loop. A claim file proves nothing
3366/// here: whether a claim succeeds says nothing about whether its owner lives.
3367#[must_use]
3368pub fn foreign_loop(
3369    reading: Option<&Reading>,
3370    now: Timestamp,
3371    own_pid: u32,
3372) -> Option<Option<u32>> {
3373    let reading = reading.filter(|r| r.running(now))?;
3374    match reading.pid {
3375        Some(pid) if pid == own_pid => None,
3376        pid => Some(pid),
3377    }
3378}
3379
3380/// Run one task this process has claimed, in this process, through exactly the
3381/// attempt a daemon would make: the same starter choice (start, resume,
3382/// review), the same `Task::start` / `settle` transitions, attempt counting,
3383/// quota refund and parking. No second dispatcher - `magi run` and `magi
3384/// review` call this when no daemon is alive.
3385///
3386/// The caller holds the [`Queue::claim`] for the whole call, which is what
3387/// keeps a daemon that starts meanwhile from taking the task.
3388///
3389/// The one deliberate difference from the loop: a task the attempt leaves
3390/// runnable (failed with attempts to spare, or requeued) is **held** instead.
3391/// Nobody asked for a retry, and a daemon starting later must not spend agent
3392/// calls re-running a hand-started task behind the operator's back;
3393/// `magi task release` is the explicit retry.
3394pub async fn run_claimed(opts: &Opts, queue: &Queue, task: &mut Task) {
3395    let status = Arc::new(Mutex::new(Status::new()));
3396    let stop = Stop::new();
3397    attempt(
3398        opts,
3399        queue,
3400        &status,
3401        &stop,
3402        crate::graph::Pause::new(),
3403        task,
3404    )
3405    .await;
3406    // A run parked on the owner's word about a withheld PR title/body has no
3407    // daemon to finish it. Nobody else will enforce `answer_timeout`, so wait
3408    // here with the claim still held, and resume once the question is settled
3409    // (answered, or abandoned at its deadline -> the neutral text).
3410    // A rejected reply files one more question (the gate asks at most twice),
3411    // so after a resume the run may be waiting again: keep going, with a fresh
3412    // limit for the new question. `resumes` bounds it even if a resume parks
3413    // without asking anything new.
3414    let mut resumes = 0;
3415    while resumes < MAX_WITHHELD_RESUMES {
3416        let Some(timeout) = withheld_text_wait(task) else {
3417            break;
3418        };
3419        eprintln!(
3420            "waiting for the owner's answer on a withheld pull request text \
3421             (up to {timeout}s; Ctrl-C leaves the run parked for a later `magi serve`)"
3422        );
3423        // The deadline abandons the question itself; the extra margin only
3424        // covers an abandon that could not be saved.
3425        let limit = std::time::Instant::now() + Duration::from_secs(timeout + 60);
3426        let ready = loop {
3427            match land_resume_state(task) {
3428                LandResume::StillWaiting if std::time::Instant::now() < limit => {
3429                    tokio::time::sleep(WITHHELD_POLL).await;
3430                }
3431                LandResume::Ready => break true,
3432                _ => break false,
3433            }
3434        };
3435        if !ready {
3436            break;
3437        }
3438        resumes += 1;
3439        attempt(
3440            opts,
3441            queue,
3442            &status,
3443            &stop,
3444            crate::graph::Pause::new(),
3445            task,
3446        )
3447        .await;
3448    }
3449    hold_if_runnable(queue, task);
3450}
3451
3452/// Resumes an in-process run makes for withheld-text questions: the gate asks
3453/// at most twice, plus one for a resume that asks nothing new.
3454const MAX_WITHHELD_RESUMES: u32 = 3;
3455
3456/// How often an in-process run rechecks its withheld-text question.
3457const WITHHELD_POLL: Duration = Duration::from_secs(15);
3458
3459/// The `answer_timeout` (seconds) of the task's last run when that run is
3460/// parked on an unresolved withheld-text question; `None` otherwise.
3461fn withheld_text_wait(task: &Task) -> Option<u64> {
3462    let id = task.runs.last()?;
3463    let s = RunState::load(id).ok()?;
3464    (s.parked && s.github_text.as_ref().is_some_and(|g| !g.resolved))
3465        .then_some(s.config.graph.answer_timeout)
3466}
3467
3468/// The quota losses `after` holds that `before` did not: what one execution
3469/// suffered, as opposed to the run's history.
3470///
3471/// Compared by value rather than by length or position because
3472/// `Runner::recover_stall` drops a `QuotaLoss` when its seat ranks again, so
3473/// the vector can shrink and shift under a resume. A retried seat that hits
3474/// quota again is a `push` with a new `at`, so it shows up as new here.
3475/// `reclaim` deliberately keeps reading the whole history: after a crash there
3476/// is no attempt boundary to diff against.
3477fn losses_this_attempt(before: &[QuotaLoss], after: &[QuotaLoss]) -> Vec<QuotaLoss> {
3478    after
3479        .iter()
3480        .filter(|q| !before.contains(q))
3481        .cloned()
3482        .collect()
3483}
3484
3485/// When the loop-wide quota cooldown should end, given this attempt's losses;
3486/// `None` when there were none. Reset-hint parsing and the cap live here.
3487fn cooldown_until(quota: &[QuotaLoss], now: Timestamp) -> Option<Timestamp> {
3488    if quota.is_empty() {
3489        return None;
3490    }
3491    let with_hint = quota.iter().find(|q| q.reset.is_some());
3492    let reset_at = with_hint.and_then(|q| parse_reset_hint(q.reset.as_deref()?, now, q.at));
3493    let wait = quota_wait(reset_at, now, QUOTA_WAIT_FALLBACK, QUOTA_WAIT_CAP);
3494    let secs = i64::try_from(wait.as_secs()).unwrap_or(i64::MAX);
3495    Some(
3496        now.checked_add(jiff::SignedDuration::from_secs(secs))
3497            .unwrap_or(Timestamp::MAX),
3498    )
3499}
3500
3501/// An explicit task count must agree with a run before any resume.
3502fn implementers_match(task: &Task, state: &RunState) -> bool {
3503    task.implementers()
3504        .is_none_or(|n| n == state.config.graph.implementers)
3505}
3506
3507/// Never resume or take over candidate work built for a different seat count.
3508fn starter_with_implementers(
3509    task: &Task,
3510    previous: Option<&RunState>,
3511    proposed: Starter,
3512) -> Starter {
3513    if previous.is_some_and(|s| !implementers_match(task, s)) {
3514        task.review_of
3515            .as_ref()
3516            .map_or(Starter::Start, |branch| Starter::Review(branch.clone()))
3517    } else {
3518        proposed
3519    }
3520}
3521
3522/// Apply this task's explicit count to the new attempt, without changing defaults.
3523fn apply_task_implementers(config: &mut Config, task: &Task) {
3524    if let Some(n) = task.implementers() {
3525        config.graph.implementers = n;
3526    }
3527}
3528
3529/// [`prepare`] for one task: its own `--config` and command-line overrides
3530/// (`Task::overrides`, filed by `magi run` / `magi review`) take precedence
3531/// over the loop's, so handing a hand-started task to a daemon changes nothing
3532/// about what it does.
3533fn prepare_for(repo: &Path, opts: &Opts, task: &Task) -> Result<Config> {
3534    let Some(o) = &task.overrides else {
3535        return prepare(repo, opts);
3536    };
3537    // A task filed by `magi run` / `magi review` was configured by that
3538    // command line alone: the loop's own `--config` / `--merge` must not leak
3539    // in, or the same command would behave differently with a loop alive.
3540    let own = Opts {
3541        config: o.config.clone(),
3542        merge: None,
3543        ..opts.clone()
3544    };
3545    let mut config = prepare(repo, if o.inherit_daemon_options { opts } else { &own })?;
3546    o.apply(&mut config);
3547    Ok(config)
3548}
3549
3550/// Load the config for a task's repository, with the merge override applied.
3551fn prepare(repo: &Path, opts: &Opts) -> Result<Config> {
3552    let (mut config, _layers) = Config::discover(repo, opts.config.as_deref())?;
3553    if let Some(mode) = &opts.merge {
3554        config.merge.mode = merge_mode(mode)?;
3555    }
3556    Ok(config)
3557}
3558
3559/// Prune the shared build cache back under its cap at a safe boundary
3560/// between runs, so a queue that never empties - and so never reaches
3561/// [`poll`]'s fully-idle branch, where the ordinary [`janitor`] pass lives -
3562/// does not leave the cache to grow unchecked for as long as the backlog
3563/// lasts.
3564///
3565/// Called from [`poll`] only when `stop.busy_now()` is already `false`: the
3566/// same liveness fact the idle branch's own janitor call rests on - no run
3567/// this daemon spawned is still mid-compile - so pruning here races nothing.
3568/// The caller must not call this while a run is in flight; there is no
3569/// second `busy_now()` check inside this function, on purpose, because there
3570/// is nothing left to check that `busy_now()` has not already answered.
3571///
3572/// A stop that has already been asked for *is* checked here, for a different
3573/// reason. [`clean::prune_cache_if_over_limit`] walks the whole cache
3574/// synchronously before it decides anything, so the poll loop cannot get back
3575/// to its own `stopped()` test until that walk is over — and a loop already
3576/// on its way out must not make the operator wait out housekeeping it is
3577/// about to stop needing. This is the same call the idle branch makes when it
3578/// rechecks `stop.stopped()` after its wait before reaching [`janitor`], and
3579/// it matters more here: `busy_now()` is false throughout, so
3580/// [`Stop::finishing`] would report a stop as already landed while the walk
3581/// still held the loop. Nothing is lost by skipping — the cap is a standing
3582/// policy, and the next daemon's startup pass measures the same cache.
3583///
3584/// Rate-limited by [`CACHE_CHECK_INTERVAL_SECS`] rather than run on every
3585/// poll: a busy loop reaches this the instant one run's `InFlightGuard` drops
3586/// and the next has not yet claimed a task, which can be every few
3587/// milliseconds, and re-walking a multi-gigabyte cache that often would cost
3588/// more than the growth it is guarding against.
3589async fn maybe_prune_cache_between_runs(
3590    repo: &Path,
3591    opts: &Opts,
3592    home: &Path,
3593    stop: &Stop,
3594    last_checked: &mut Option<Timestamp>,
3595    now: Timestamp,
3596) {
3597    if stop.stopped() || !cache_check_due(*last_checked, now, CACHE_CHECK_INTERVAL_SECS) {
3598        return;
3599    }
3600    *last_checked = Some(now);
3601    let cfg = match prepare(repo, opts) {
3602        Ok(cfg) => cfg,
3603        Err(e) => {
3604            tracing::warn!("cache check: no config: {e:#}");
3605            return;
3606        }
3607    };
3608    match clean::prune_cache_if_over_limit(&cfg, home) {
3609        Ok(Some(pruned)) if pruned.files > 0 => tracing::info!(
3610            "housekeep: pruned {} file(s) ({} bytes) from the shared cache between runs",
3611            pruned.files,
3612            pruned.freed
3613        ),
3614        Ok(_) => {}
3615        Err(e) => {
3616            tracing::warn!("housekeep: prune cache: {e:#}");
3617            notices::raise_in(
3618                home,
3619                Notice::warn(
3620                    "housekeep:cache",
3621                    "Pruning the shared build cache failed; disk usage may keep growing.",
3622                ),
3623            );
3624        }
3625    }
3626}
3627
3628/// Whether [`maybe_prune_cache_between_runs`] should re-measure the cache
3629/// now, given when it last did (if ever). Pure, so the cadence is asserted
3630/// directly rather than by waiting out real minutes in a test.
3631fn cache_check_due(last_checked: Option<Timestamp>, now: Timestamp, interval_secs: u64) -> bool {
3632    last_checked.is_none_or(|last| clean::due(now, last, interval_secs))
3633}
3634
3635/// The disk janitor, with its housekeeping logged rather than fatal.
3636///
3637/// Called only at the loop's idle points, for the reason the caller documents:
3638/// a prune racing a live compile would delete files mid-build. The config is
3639/// re-read on every call because the repository that just ran may not be the
3640/// daemon's own default, and the cache directory is a repository fact.
3641///
3642/// `home` and `worktrees_root` are parameters rather than [`crate::run::home`]
3643/// and [`crate::run::default_worktree_root`] read here, for the same reason
3644/// [`drive`] takes its queue and status file rather than resolving them: a
3645/// test driving the loop must not reach through to the operator's real home
3646/// or worktree bay just because the janitor runs on every idle tick.
3647/// `worktrees_root` staying unread by [`clean::fold_due`] once made this easy
3648/// to get wrong silently - a test's `home` was already isolated, but nothing
3649/// exercised the parameter next to it, so a real worktree bay stayed wired in
3650/// underneath. The moment [`clean::fold_orphaned_worktrees`] started reading
3651/// it for real, every test in this file that drives the loop at all started
3652/// sweeping the operator's actual `~/wt/<repo>` instead of a fixture's.
3653async fn janitor(repo: &Path, opts: &Opts, home: &Path, worktrees_root: &Path) {
3654    let cfg = match prepare(repo, opts) {
3655        Ok(cfg) => cfg,
3656        Err(e) => {
3657            tracing::warn!("housekeep: no config: {e:#}");
3658            return;
3659        }
3660    };
3661    // A run's own worktree lives under `config.graph.worktree_root` when the
3662    // repository sets one - the same precedence `RunState::worktree_root`
3663    // uses - and `worktrees_root` only stands in for the *default* an
3664    // unconfigured repository resolves to (see this function's own
3665    // parameter, or the test fixture wiring one to a fake path). Housekeeping
3666    // that always swept the default regardless of this override would never
3667    // see, and so never reclaim, a single worktree for a repository that
3668    // relocated them elsewhere.
3669    let worktrees_root = cfg.graph.worktree_root.as_deref().unwrap_or(worktrees_root);
3670    let out = clean::housekeep(&cfg, home, worktrees_root, repo, Timestamp::now()).await;
3671    // Reported whenever there is anything to say, not only when `folded > 0`:
3672    // the incident this exists to prevent was 90 of 93 runs skipped and 0
3673    // folded, on every single pass, for months - a report gated on `folded`
3674    // would have stayed silent through every one of them.
3675    if out.folded > 0 || out.unreadable > 0 || out.orphaned_worktrees > 0 {
3676        let mut extra = Vec::new();
3677        if out.unreadable > 0 {
3678            extra.push(format!("{} unreadable", out.unreadable));
3679        }
3680        if out.orphaned_worktrees > 0 {
3681            extra.push(format!("{} orphaned worktree(s)", out.orphaned_worktrees));
3682        }
3683        let detail = if extra.is_empty() {
3684            String::new()
3685        } else {
3686            format!(" ({})", extra.join(", "))
3687        };
3688        tracing::info!("housekeep: folded {} run(s){detail}", out.folded);
3689    }
3690    if out.external_merges_recorded > 0 {
3691        tracing::info!(
3692            "housekeep: recorded {} run(s) as merged externally",
3693            out.external_merges_recorded
3694        );
3695    }
3696    if out.stale_pr_states_repaired > 0 {
3697        tracing::info!(
3698            "housekeep: rewrote {} run record(s) whose pull request had already settled",
3699            out.stale_pr_states_repaired
3700        );
3701    }
3702    if out.cache_files > 0 {
3703        tracing::info!(
3704            "housekeep: pruned {} file(s) ({} bytes) from the shared cache",
3705            out.cache_files,
3706            out.cache_freed
3707        );
3708    }
3709    if out.questions_abandoned > 0 {
3710        tracing::info!(
3711            "housekeep: abandoned {} question(s) left open by a finished run",
3712            out.questions_abandoned
3713        );
3714    }
3715}
3716
3717/// Run [`triage::run_once`] and log whatever it did, the same "only when
3718/// there is something to say" rule [`janitor`] follows for its own report.
3719///
3720/// Called at the same idle points as [`janitor`] - once per full poll
3721/// interval, never mid-attempt - for the same reason: it is not liveness
3722/// critical, and a task's own `hold_reason` string is the one thing this
3723/// would otherwise re-check (via [`crate::disk::free_bytes`]) on every busy
3724/// tick for no benefit.
3725async fn triage_held(queue: &Queue, home: &Path, opts: &Opts) {
3726    let questions = Questions::at(home.join("questions"));
3727    let report = triage::run_once(queue, &questions, opts.config.as_deref(), Timestamp::now());
3728    if report.is_empty() {
3729        return;
3730    }
3731    if !report.quarantined.is_empty() {
3732        tracing::info!(
3733            "triage: held {} blocked task(s) whose blocked-on task or \
3734             question no longer exists: {}",
3735            report.quarantined.len(),
3736            report.quarantined.join(", ")
3737        );
3738    }
3739    if !report.resumed.is_empty() {
3740        tracing::info!(
3741            "triage: resumed {} held task(s) whose machine hold had resolved: {}",
3742            report.resumed.len(),
3743            report.resumed.join(", ")
3744        );
3745    }
3746    if !report.asked.is_empty() {
3747        tracing::info!(
3748            "triage: asked about {} held task(s): {}",
3749            report.asked.len(),
3750            report.asked.join(", ")
3751        );
3752    }
3753    if !report.answered.is_empty() {
3754        tracing::info!(
3755            "triage: applied {} operator answer(s): {}",
3756            report.answered.len(),
3757            report.answered.join(", ")
3758        );
3759    }
3760}
3761
3762/// The free-space gate: what stands between this task and a new run, if
3763/// anything. `Some(reason)` holds the task; `None` lets it start.
3764///
3765/// A zero [`Config::disk::min_free_bytes`] opens the gate unconditionally -
3766/// the operator opted out. A measurement failure is a gate, not a pass: both
3767/// sides of "cannot tell" are served by not starting.
3768fn disk_gate(repo: &Path, config: &Config) -> Option<String> {
3769    disk_gate_with(repo, config, crate::disk::free_bytes)
3770}
3771
3772/// [`disk_gate`] with its free-space measurement supplied by the caller, so a
3773/// test can assert the exact wiring `attempt` runs - config's threshold in,
3774/// task-holding reason out - without asking the real machine's disk anything.
3775fn disk_gate_with<F: Fn(&Path) -> Result<u64>>(
3776    repo: &Path,
3777    config: &Config,
3778    free_bytes: F,
3779) -> Option<String> {
3780    let min = config.disk.min_free_bytes;
3781    if min == 0 {
3782        return None;
3783    }
3784    match free_bytes(repo) {
3785        Ok(free) => crate::disk::gate_in(free, min, &config.graph.language),
3786        Err(e) => Some(crate::disk::unmeasured_in(repo, &e, &config.graph.language)),
3787    }
3788}
3789
3790/// How long to wait before offering another task when a run lost a seat to a
3791/// rate limit and its [`QuotaLoss::reset`] carried no hint [`parse_reset_hint`]
3792/// could read, or carried nothing at all. Long enough that a quota outage
3793/// cannot burn through a whole backlog in the few seconds each doomed attempt
3794/// takes to fail; short enough that a quota which clears early is not left
3795/// idle for the fallback's sake.
3796const QUOTA_WAIT_FALLBACK: Duration = Duration::from_secs(5 * 60);
3797
3798/// Longest a parsed reset hint may push the wait out to. The hint comes from
3799/// the CLI's own words, not a contract, so a parsing slip that lands a day
3800/// away must not leave the loop asleep for a day.
3801const QUOTA_WAIT_CAP: Duration = Duration::from_secs(30 * 60);
3802
3803/// How long [`poll`] should wait before offering the next task, after a run
3804/// lost at least one seat to a rate limit.
3805///
3806/// Pure and separate from the loop so the policy can be exercised without a
3807/// real quota outage. `reset_at` is the time [`parse_reset_hint`] made of the
3808/// CLI's free-text hint, if it could; `fallback` is what to wait when there is
3809/// nothing to parse, or the parsed time has already passed; `cap` bounds how
3810/// far a parsed hint is trusted to push the wait out.
3811fn quota_wait(
3812    reset_at: Option<Timestamp>,
3813    now: Timestamp,
3814    fallback: Duration,
3815    cap: Duration,
3816) -> Duration {
3817    match reset_at {
3818        Some(at) if at > now => {
3819            let secs = u64::try_from(at.as_second() - now.as_second()).unwrap_or(0);
3820            Duration::from_secs(secs).min(cap)
3821        }
3822        _ => fallback,
3823    }
3824}
3825
3826/// Best-effort reading of a [`QuotaLoss::reset`] hint into a concrete time.
3827///
3828/// `reset` is deliberately free text — see [`crate::agent::Quota`], which
3829/// explains why parsing it exactly "would be a bug factory" — so this only
3830/// recognises the shapes actually observed in the wild, and returns `None`
3831/// for anything else rather than guess at a format nobody has seen.
3832///
3833/// `recorded` is when the loss was noted ([`QuotaLoss::at`]). It anchors the
3834/// relative shape (`"in 1h2m49s"`, agy's), which counts from the moment the CLI
3835/// said it, not from whenever this loop happens to read it: anchoring on `now`
3836/// would push the reset later on every read and would read an already-elapsed
3837/// reset as still in the future. (A long hint is still clamped by
3838/// [`QUOTA_WAIT_CAP`]; the anchor matters for short hints and elapsed ones.)
3839fn parse_reset_hint(text: &str, now: Timestamp, recorded: Timestamp) -> Option<Timestamp> {
3840    parse_reset_hint_zoned(text, now)
3841        .or_else(|| parse_reset_hint_dated(text))
3842        .or_else(|| parse_reset_hint_relative(text, recorded))
3843}
3844
3845/// agy's shape: `"in 1h2m49s"` - `in`, then hours/minutes/seconds, each unit
3846/// optional but at least one required, in that order. A bare number or any
3847/// unknown unit is refused.
3848fn parse_reset_hint_relative(text: &str, recorded: Timestamp) -> Option<Timestamp> {
3849    let rest = text.trim().trim_end_matches('.').strip_prefix("in ")?;
3850    let mut rest = rest.trim();
3851    if rest.is_empty() {
3852        return None;
3853    }
3854    let mut total: i64 = 0;
3855    let mut matched = false;
3856    for (unit, secs) in [('h', 3600), ('m', 60), ('s', 1)] {
3857        if let Some((digits, tail)) = rest.split_once(unit)
3858            && !digits.is_empty()
3859            && digits.bytes().all(|b| b.is_ascii_digit())
3860        {
3861            total += digits.parse::<i64>().ok()?.checked_mul(secs)?;
3862            rest = tail;
3863            matched = true;
3864        }
3865    }
3866    if !rest.is_empty() || !matched {
3867        return None;
3868    }
3869    recorded
3870        .checked_add(jiff::SignedDuration::from_secs(total))
3871        .ok()
3872}
3873
3874/// Reads a 12-hour `"H:MMam/pm"` clock reading (whitespace trimmed,
3875/// case-insensitive) into a 24-hour hour and minute. Shared by every
3876/// reset-hint shape below.
3877fn parse_12h_clock(clock: &str) -> Option<(i8, i8)> {
3878    let clock = clock.trim().to_lowercase();
3879    let (digits, pm) = clock
3880        .strip_suffix("am")
3881        .map(|d| (d, false))
3882        .or_else(|| clock.strip_suffix("pm").map(|d| (d, true)))?;
3883    let (h, m) = digits.trim().split_once(':')?;
3884    let mut hour: i8 = h.trim().parse().ok()?;
3885    let minute: i8 = m.trim().parse().ok()?;
3886    if !(1..=12).contains(&hour) || !(0..=59).contains(&minute) {
3887        return None;
3888    }
3889    if pm && hour != 12 {
3890        hour += 12;
3891    } else if !pm && hour == 12 {
3892        hour = 0;
3893    }
3894    Some((hour, minute))
3895}
3896
3897/// The Claude CLI's shape: `"H:MMam/pm (Zone)"`, naming only a clock reading
3898/// and a zone, never a date. A clock reading already past today is read as
3899/// tomorrow's: a CLI naming a same-day reset that has already gone by means
3900/// the window rolled over while nothing was watching.
3901fn parse_reset_hint_zoned(text: &str, now: Timestamp) -> Option<Timestamp> {
3902    let open = text.find('(')?;
3903    let close = text.rfind(')')?;
3904    if close <= open {
3905        return None;
3906    }
3907    let zone = text[open + 1..close].trim();
3908    let (hour, minute) = parse_12h_clock(&text[..open])?;
3909    let tz = jiff::tz::TimeZone::get(zone).ok()?;
3910    let candidate = now
3911        .to_zoned(tz)
3912        .with()
3913        .hour(hour)
3914        .minute(minute)
3915        .second(0)
3916        .millisecond(0)
3917        .microsecond(0)
3918        .nanosecond(0)
3919        .build()
3920        .ok()?;
3921    let mut at = candidate.timestamp();
3922    if at <= now {
3923        at += jiff::SignedDuration::from_hours(24);
3924    }
3925    Some(at)
3926}
3927
3928/// The Codex CLI's shape: `"Mon DDth, YYYY H:MMam/pm"` (English month
3929/// abbreviation, an ordinal day, a 4-digit year, a 12-hour clock reading),
3930/// with no zone at all — unlike [`parse_reset_hint_zoned`], so there is no
3931/// "already past today" correction to make: the year already disambiguates
3932/// it. Scanned as a five-word window so it can be pulled out of the middle
3933/// of a full sentence, e.g. Codex's actual wording: "...or try again at Sep
3934/// 19th, 2026 5:10 PM." The result is read as UTC, same as this crate reads
3935/// any other timestamp with no zone attached.
3936fn parse_reset_hint_dated(text: &str) -> Option<Timestamp> {
3937    let words: Vec<&str> = text.split_whitespace().collect();
3938    if words.len() < 5 {
3939        return None;
3940    }
3941    (0..=words.len() - 5)
3942        .find_map(|start| parse_dated_window(&words[start..start + 5], words.get(start + 5)))
3943}
3944
3945/// One five-word window: month, `"DDth,"`, `"YYYY"`, `"H:MM"`, `"am/pm"`. A
3946/// parenthesis right after the window is refused rather than ignored — it
3947/// reads as an explicit zone annotation on a shape that otherwise carries
3948/// none, and guessing UTC anyway would be exactly the silent misread this
3949/// module's parsing otherwise avoids.
3950fn parse_dated_window(window: &[&str], trailing: Option<&&str>) -> Option<Timestamp> {
3951    if trailing.is_some_and(|next| next.starts_with('(')) {
3952        return None;
3953    }
3954    let month = month_number(window[0])?;
3955    let day_token = window[1].strip_suffix(',')?.to_lowercase();
3956    let day_digits = ["st", "nd", "rd", "th"]
3957        .iter()
3958        .find_map(|suffix| day_token.strip_suffix(*suffix))?;
3959    let day: i8 = day_digits.parse().ok()?;
3960    let year_token = window[2];
3961    if year_token.len() != 4 || !year_token.bytes().all(|b| b.is_ascii_digit()) {
3962        return None;
3963    }
3964    let year: i16 = year_token.parse().ok()?;
3965    // The am/pm word carries the sentence's own trailing punctuation, e.g.
3966    // the period ending "...at Sep 19th, 2026 5:10 PM." — strip it before
3967    // reusing the same 12-hour clock reader the bracketed shape uses.
3968    let ampm = window[4].trim_matches(|c: char| !c.is_ascii_alphabetic());
3969    let (hour, minute) = parse_12h_clock(&format!("{}{}", window[3], ampm))?;
3970    let date = jiff::civil::Date::new(year, month, day).ok()?;
3971    let candidate = date
3972        .at(hour, minute, 0, 0)
3973        .to_zoned(jiff::tz::TimeZone::UTC)
3974        .ok()?;
3975    Some(candidate.timestamp())
3976}
3977
3978/// The 3-letter English month abbreviation [`parse_reset_hint_dated`] reads,
3979/// case-insensitively, into a 1-based month number.
3980fn month_number(name: &str) -> Option<i8> {
3981    const NAMES: [&str; 12] = [
3982        "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
3983    ];
3984    let lower = name.to_lowercase();
3985    NAMES
3986        .iter()
3987        .position(|n| *n == lower.as_str())
3988        .map(|i| i as i8 + 1)
3989}
3990
3991/// Resuming a `Blocked` run that already spent every review round its own
3992/// config allowed cannot make progress: `graph::Runner`'s review loop walks
3993/// `(reviews.len()+1)..=max_rounds`, which is empty once `reviews.len()` has
3994/// reached `max_rounds`, so `execute` would settle straight back to
3995/// `Blocked` without asking anyone anything. Read-only against a state this
3996/// build never mutates — `src/graph.rs` stays untouched — but without this
3997/// check, [`unfinished_run`] would keep reporting such a run as still
3998/// "unfinished", and `crate::conduct::Recovery::Requeue` (whose whole
3999/// promise is a fresh competition when a design needs to change) would
4000/// silently resume the exhausted run instead, spending an attempt on a
4001/// cycle that cannot change anything.
4002fn exhausted_review_budget(state: &RunState) -> bool {
4003    // A run whose task-head publication failed has nothing left to review:
4004    // resuming it only retries the publication, so it is never "exhausted".
4005    state.status == RunStatus::Blocked
4006        && !state.pr_publication_pending
4007        && state.reviews.len() >= state.config.graph.review_rounds
4008}
4009
4010/// This task's *most recent* run, if resuming it would actually make
4011/// progress. `short` is only for the warning's own message.
4012///
4013/// Only ever `runs.last()` — never a search back through older history.
4014/// `runs` accumulates one entry per fresh `Runner::start`/`Runner::review`
4015/// mint, oldest first, and every entry before the last one was already
4016/// superseded at the moment it was minted: the daemon only ever starts a new
4017/// run when the previous one was not worth resuming (unresumable, exhausted,
4018/// or unreadable), or when `crate::conduct::Recovery::Review` deliberately
4019/// opens a fresh review-only run alongside an older, already-failed
4020/// competition. Searching further back would let an old run that merely
4021/// *looks* resumable — a `Stalled` competition an earlier `Review` pass left
4022/// behind, say — get resumed instead of the fresh competition
4023/// `crate::conduct::Recovery::Requeue` actually promised, reviving history
4024/// nothing asked to revisit.
4025///
4026/// Two runs paid for the "prefer resuming over restarting" half of this
4027/// lesson, which is why this still checks `runs.last()` rather than always
4028/// restarting. Run 01c2 was blocked and the loop started 3cbf on the same
4029/// task a moment later, duplicating two and a half hours of agent work. Then
4030/// b25f stalled on a judge that timed out and one that answered with no JSON
4031/// — `quota: 0`, so nothing the machine was to blame for — and 4043 started
4032/// **one second** later, buying three fresh implementations to reach the
4033/// same panel. `RunStatus::resumable` rather than `!done()` is what catches
4034/// the second case: a stall is terminal, and its cheap recovery re-asks only
4035/// the absent seats. [`exhausted_review_budget`] is the other half: a run
4036/// that is technically `resumable()` but provably cannot progress must not
4037/// count as "unfinished" either, or `Recovery::Requeue` becomes a silent
4038/// no-op instead of the fresh competition it promises.
4039///
4040/// A load failure is warned about rather than silently read as "not
4041/// resumable": the alternative is exactly what let a schema mismatch on run
4042/// `eba2` fall through to a full re-competition with nobody told why.
4043/// `crate::conduct` is what actually offers a better answer than
4044/// `Runner::start` here (see `Recovery::Review`), once this task's next
4045/// failure shows it up as `held`/`failed` with the run state unreadable.
4046fn unfinished_run(runs: &[String], short: &str) -> Option<String> {
4047    unfinished_run_with(runs, short, RunState::load)
4048}
4049
4050/// [`unfinished_run`] with an injected state reader. Tests provide their
4051/// fixtures directly rather than touching the process-global run home.
4052fn unfinished_run_with<F>(runs: &[String], short: &str, load: F) -> Option<String>
4053where
4054    F: FnOnce(&str) -> Result<RunState>,
4055{
4056    let id = runs.last()?;
4057    match load(id) {
4058        // A run some process is driving right now is not "unfinished" in the
4059        // sense of waiting to be picked up: a run started by hand is in
4060        // `runs` while it is still executing, and resuming it here
4061        // would put a second driver on the same worktrees.
4062        Ok(s)
4063            if s.status.resumable()
4064                && !s.released()
4065                && !exhausted_review_budget(&s)
4066                && s.liveness(false) != crate::run::Liveness::Live =>
4067        {
4068            Some(id.clone())
4069        }
4070        Ok(_) => None,
4071        Err(e) => {
4072            tracing::warn!("could not read run {id} for task {short}: {e:#}");
4073            None
4074        }
4075    }
4076}
4077
4078/// Is `task` a parked (or, from an older build, failed) task whose last run was parked at a node boundary, so
4079/// that [`attempt`] will resume it on its own?
4080///
4081/// Mirrors [`unfinished_run_with`]'s own conditions (plus `parked`, and no
4082/// pending review choice, which [`choose_starter`] ranks first). Pure, with an
4083/// injected reader like its sibling. An unreadable run is not awaiting
4084/// anything: it keeps going to the conductor as before.
4085fn awaiting_resume_with<F>(task: &Task, load: F) -> bool
4086where
4087    F: FnOnce(&str) -> Result<RunState>,
4088{
4089    // `Failed` stays: a record an older build wrote for a parked run.
4090    if !matches!(task.status, TaskStatus::Parked | TaskStatus::Failed)
4091        || task.fresh_start
4092        || task.review_branch.is_some()
4093    {
4094        return false;
4095    }
4096    let Some(id) = task.runs.last() else {
4097        return false;
4098    };
4099    load(id).is_ok_and(|s| {
4100        s.parked && s.status.resumable() && !s.released() && !exhausted_review_budget(&s)
4101    })
4102}
4103
4104/// Which of the three ways [`attempt`] can mint or continue a run this task
4105/// should use.
4106#[derive(Debug, Clone, PartialEq, Eq)]
4107enum Starter {
4108    /// `crate::graph::Runner::review` against a branch `crate::conduct` chose
4109    /// and that still exists.
4110    Review(String),
4111    /// `crate::graph::Runner::resume` on an unfinished run of this task.
4112    Resume(String),
4113    /// `crate::graph::Runner::start`: a fresh competition.
4114    Start,
4115}
4116
4117/// A selected public implementation overrides every historical candidate and pin.
4118fn selected_starter(
4119    selected: &crate::pr_recovery::Selection,
4120    previous: Option<&RunState>,
4121    unfinished: Option<&str>,
4122) -> Starter {
4123    if let Some(id) = unfinished
4124        && previous.is_some_and(|r| {
4125            r.id == id && r.pr_recovery.as_ref() == Some(selected) && r.reviewed_commits.is_some()
4126        })
4127    {
4128        Starter::Resume(id.to_owned())
4129    } else {
4130        Starter::Review(selected.branch.clone())
4131    }
4132}
4133
4134/// The owner's answer to this branch's divergence question, consumed from the
4135/// task so a later, different divergence asks again. The runner applies it
4136/// (see `Runner::review_taking_over`), after the earlier worktree is released.
4137fn take_divergence_answer(
4138    branch: &str,
4139    remote: &str,
4140    task: &mut Task,
4141) -> Option<crate::reconcile::Choice> {
4142    let summary = crate::reconcile::summary_for(branch, remote);
4143    let (idx, choice) = task.answers.iter().enumerate().rev().find_map(|(i, a)| {
4144        (a.question == summary)
4145            .then(|| crate::reconcile::Choice::from_answer(&a.answer))
4146            .flatten()
4147            .map(|c| (i, c))
4148    })?;
4149    task.answers.remove(idx);
4150    Some(choice)
4151}
4152
4153/// Decide which of [`Runner::review`], [`Runner::resume`] or [`Runner::start`]
4154/// this attempt should use. Pure, and separate from [`attempt`], so the
4155/// routing itself is assertable without spawning a real graph or a git
4156/// process: `attempt`'s own `crate::git::branch_exists` call has already
4157/// happened by the time this is called.
4158///
4159/// `review_branch` wins whenever `branch_exists` confirms it; a `review_branch`
4160/// whose branch is gone falls all the way through to [`Starter::Start`], not
4161/// to [`Starter::Resume`] — `crate::conduct` chose review over resuming the
4162/// old (likely `Blocked`) run in the first place, and a branch that vanished
4163/// out from under that choice is not evidence resuming it would fare better.
4164fn choose_starter(
4165    review_branch: Option<&str>,
4166    branch_exists: bool,
4167    unfinished: Option<&str>,
4168) -> Starter {
4169    match review_branch {
4170        Some(branch) if branch_exists => Starter::Review(branch.to_owned()),
4171        Some(_) => Starter::Start,
4172        None => match unfinished {
4173            Some(id) => Starter::Resume(id.to_owned()),
4174            None => Starter::Start,
4175        },
4176    }
4177}
4178
4179/// Consume a retained review only while its authored criteria still match.
4180fn take_review_branch(task: &mut Task, previous: Option<&RunState>) -> Option<String> {
4181    let branch = task.review_branch.take();
4182    let instruction = task.review_instruction.take();
4183    branch.filter(|branch| match &instruction {
4184        Some(authored) => *authored == task.instruction,
4185        // Legacy retained branches must agree with their prior run; only an
4186        // explicitly pinned review can proceed without recoverable provenance.
4187        None => previous.map_or_else(
4188            || task.review_of.as_ref() == Some(branch),
4189            |run| compatible_run_instruction(run, task),
4190        ),
4191    })
4192}
4193
4194/// A current explicit pin can select an otherwise incompatible run. Edits
4195/// and subsequent approvals invalidate pins selected for earlier criteria.
4196fn may_resume_run(run: &RunState, task: &Task) -> bool {
4197    task.resume_override
4198        .as_ref()
4199        .is_some_and(|r| r.pinned_run.as_ref() == Some(&run.id))
4200        || compatible_run_instruction(run, task)
4201}
4202
4203/// Review scaffolding is execution context, not evidence of an operator edit.
4204pub(crate) fn compatible_run_instruction(run: &RunState, task: &Task) -> bool {
4205    if let Some(authored) = &run.task_instruction {
4206        return compatible_instruction(authored, task);
4207    }
4208    if compatible_instruction(&run.instruction, task) {
4209        return true;
4210    }
4211    // Recognize only the existing generated review shapes on legacy runs.
4212    // Ordinary authored text with a similar heading is left intact.
4213    if run.reviewed_commits.is_some() {
4214        if let Some((authored, _)) = run.instruction.split_once("\n\n# Work already on branch `") {
4215            return strip_answers_block(authored, task).trim() == task.instruction.trim();
4216        }
4217        if let Some(branch) = &task.review_of {
4218            let canonical = format!(
4219                "Review the branch `{branch}` (review-only: no implementation, no judging)."
4220            );
4221            let generated = format!(
4222                "Review the work already on branch `{branch}`. There is no task statement: what the change claims to do is whatever its commits say.\n\n"
4223            );
4224            return task.instruction == canonical && run.instruction.starts_with(&generated);
4225        }
4226    }
4227    false
4228}
4229
4230/// Compare authored instructions, excluding answer blocks appended by the daemon.
4231pub(crate) fn compatible_instruction(old: &str, task: &Task) -> bool {
4232    strip_answers_block(old, task) == task.instruction
4233}
4234
4235/// Which repository a task runs in. A task that names none — the normal case
4236/// for one filed from a phone — runs in the daemon's own default.
4237fn repo_for(task: &Task, fallback: &Path) -> PathBuf {
4238    if task.repo.as_os_str().is_empty() || task.repo == Path::new(".") {
4239        return fallback.to_path_buf();
4240    }
4241    task.repo.clone()
4242}
4243
4244/// The header [`append_answers`] appends operator answers under. Shared with
4245/// [`strip_answers_block`] so a resumed run's instruction can be refreshed
4246/// rather than grown a new block on every resume.
4247const ANSWERS_HEADER: &str = "\n\n# Operator answers\n\n";
4248
4249/// Render the first `count` answers in the block appended to an instruction.
4250fn answers_block(task: &Task, count: usize) -> String {
4251    let mut s = ANSWERS_HEADER.to_owned();
4252    for a in &task.answers[..count] {
4253        s.push_str(&format!("- {}: {}\n", a.question, a.answer));
4254    }
4255    s
4256}
4257
4258/// Render the approval independently of transient task state, so historical
4259/// suffixes remain recognizable after another explicit release.
4260fn resumption_block(r: &crate::queue::Resumption) -> String {
4261    format!(
4262        "\n\n# Explicit operator resumption\n\nGeneration {} at {} approved this instruction:\n{}\n\
4263         Earlier answers (first {}) are audit history; their waiting premises are superseded by this approval.\n",
4264        r.generation, r.at, r.instruction, r.answers_through,
4265    )
4266}
4267
4268/// Append audit answers and the latest explicit approval to the instruction.
4269fn append_answers(base: &str, task: &Task) -> String {
4270    let mut s = base.to_owned();
4271    if !task.answers.is_empty() {
4272        s.push_str(&answers_block(task, task.answers.len()));
4273    }
4274    if let Some(r) = &task.resumption {
4275        s.push_str(&resumption_block(r));
4276    }
4277    s
4278}
4279
4280/// Drop the prior answer block only when it is exactly the suffix this task
4281/// could have appended on an earlier resume. An `ANSWERS_HEADER` written by
4282/// the task author is ordinary instruction text, not a block to remove.
4283fn strip_answers_block<'a>(instruction: &'a str, task: &Task) -> &'a str {
4284    let mut instruction = instruction;
4285    for approval in task
4286        .resumption
4287        .iter()
4288        .chain(task.resumption_history.iter().rev())
4289    {
4290        if let Some(base) = instruction.strip_suffix(&resumption_block(approval)) {
4291            instruction = base;
4292            break;
4293        }
4294    }
4295    for count in (1..=task.answers.len()).rev() {
4296        let block = answers_block(task, count);
4297        if let Some(base) = instruction.strip_suffix(&block) {
4298            return base;
4299        }
4300    }
4301    instruction
4302}
4303
4304/// The instruction handed to `Runner::start`: the task's own text, plus any
4305/// operator answers `crate::conduct` collected for it (see
4306/// [`Task::answers`]), so a decision the operator actually made reaches the
4307/// implementers rather than only clearing the block that was waiting on it.
4308///
4309/// Appended rather than merged into [`Task::instruction`] itself, so the
4310/// task's own record stays exactly what its author wrote.
4311fn instruction_for(task: &Task) -> String {
4312    append_answers(&task.instruction, task)
4313}
4314
4315/// The instruction a resumed run should carry on with: whatever it already
4316/// had, refreshed with the task's *current* operator answers.
4317///
4318/// A resumable run's own `RunState::instruction` predates any answer
4319/// `crate::conduct` collects after the run parks, so resuming it unchanged —
4320/// the behaviour before this function existed — silently drops the very
4321/// decision the operator made to unblock it. Re-stripping any block this
4322/// function appended on an earlier resume before re-appending the current
4323/// list (rather than blindly appending again) is what keeps a task resumed
4324/// three times over three answered questions from carrying the same answer
4325/// three times.
4326fn resumed_instruction(old_instruction: &str, task: &Task) -> String {
4327    append_answers(strip_answers_block(old_instruction, task), task)
4328}
4329
4330/// The task's attachments as absolute paths, each checked to still exist.
4331fn task_attachments(queue: &Queue, task: &Task) -> Result<Vec<PathBuf>> {
4332    let paths = queue.attachment_paths(task);
4333    for (name, path) in task.attachments.iter().zip(&paths) {
4334        if !path.is_file() {
4335            bail!(
4336                "attachment `{name}` is recorded on the task but {} is missing",
4337                path.display()
4338            );
4339        }
4340    }
4341    Ok(paths)
4342}
4343
4344/// What [`attempt`] should tell a [`Starter`] about `task`'s current operator
4345/// answers before handing it to `Runner` — the actual boundary between
4346/// [`choose_starter`]'s routing and the graph, factored out so it is
4347/// assertable without a real repository, git branch, or agent CLI.
4348///
4349/// `Starter::Review` deliberately answers `None`: `Runner::review` builds its
4350/// instruction from the reviewed branch's own commit log because there is no
4351/// task statement to speak of for hand-written work, and splicing operator
4352/// answers into that text would contradict the very message it sends
4353/// reviewers ("there is no task statement").
4354fn prepare_instruction(
4355    starter: &Starter,
4356    old_instruction: Option<&str>,
4357    task: &Task,
4358) -> Option<String> {
4359    match starter {
4360        Starter::Start => Some(instruction_for(task)),
4361        Starter::Resume(_) => Some(resumed_instruction(
4362            old_instruction.expect("a resumed run always has a prior instruction"),
4363            task,
4364        )),
4365        Starter::Review(_) => None,
4366    }
4367}
4368
4369/// Persist a transition. A queue write failure is logged rather than fatal: the
4370/// run already happened, and taking the daemon down would only add a lost
4371/// backlog to a full disk.
4372fn record(queue: &Queue, task: &mut Task) {
4373    if let Err(e) = queue.put(task) {
4374        tracing::error!("could not record task {}: {e:#}", task.short());
4375        let notice = Notice::error(
4376            "loop:record",
4377            "The loop could not save a task's state; check the disk.",
4378        )
4379        .link(Link::Task {
4380            id: task.id.clone(),
4381        });
4382        // The queue lives at `<home>/queue`, so its parent is the home.
4383        match queue.root().parent().filter(|p| !p.as_os_str().is_empty()) {
4384            Some(home) => notices::raise_in(home, notice),
4385            None => notices::raise(notice),
4386        }
4387    }
4388}
4389
4390/// Every runnable task, in the order the loop should try them.
4391///
4392/// The head of this list is exactly what [`Queue::next_runnable`] offers; the
4393/// tail exists so that a claim somebody else holds costs the loop the next
4394/// candidate rather than a whole poll interval of idleness.
4395fn runnable(queue: &Queue) -> Vec<Task> {
4396    let mut tasks: Vec<Task> = queue
4397        .list()
4398        .into_iter()
4399        .filter(|t| t.status.runnable())
4400        .collect();
4401    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then(a.id.cmp(&b.id)));
4402    tasks
4403}
4404
4405/// Why a run ended where it did, in one line, for [`Task::last_error`].
4406///
4407/// A stalled run names the seats the quota took out: "out of quota" is not
4408/// actionable, while "judge-2, judge-3 hit a limit" tells the operator which
4409/// agent to replace or which plan to top up.
4410///
4411/// Uses [`RunStatus::display_label`] rather than [`label`]/`as_str` on
4412/// purpose: unlike `label`'s other callers (an internal log line, an
4413/// already-a-bug fallback message), this string becomes `Task::last_error`
4414/// verbatim, which the phone renders in the same alarm-styled box an
4415/// ordinary failure gets — see `web::tests` and `assets/ui/app.js`'s
4416/// `.err` styling. A bare `verified_noop` there would read exactly like the
4417/// failure this whole feature exists to tell apart from one.
4418fn describe(state: &RunState) -> String {
4419    let p = phrases(&state.config.graph.language);
4420    let mut detail = if state.status == RunStatus::Stalled {
4421        let mut seats: Vec<&str> = state.quota.iter().map(|q| q.seat.as_str()).collect();
4422        seats.sort_unstable();
4423        seats.dedup();
4424        if seats.is_empty() {
4425            p.quorum_lost.to_owned()
4426        } else {
4427            format!("{}{}{}", p.quorum_lost, p.quota_took_out, seats.join(", "))
4428        }
4429    } else {
4430        format!("{}{}", p.run_ended, state.status.display_label())
4431    };
4432    if let Some(last) = state.events.last() {
4433        // The review loop's "never answered" stop is the one event worth
4434        // translating; it is recognised from structured state, not by
4435        // matching the English event text (which stays as recorded).
4436        let unanswered = state
4437            .reviews
4438            .last()
4439            .filter(|r| {
4440                state.status == RunStatus::Blocked
4441                    && last.node == "review"
4442                    && r.incomplete()
4443                    && r.blocking == 0
4444                    && r.round == state.config.graph.review_rounds
4445                    && r.e2e.iter().all(crate::run::CommandOutcome::ok)
4446            })
4447            .map(|r| (r.expected - r.answered, r.round));
4448        match unanswered {
4449            Some((missing, rounds)) if crate::lang::is_japanese(&state.config.graph.language) => {
4450                detail.push_str(&format!(
4451                    " ({}: {})",
4452                    last.node,
4453                    (p.reviewers_never_answered)(missing, rounds)
4454                ));
4455            }
4456            _ => detail.push_str(&format!(" ({}: {})", last.node, last.message)),
4457        }
4458    }
4459    detail.push_str(&format!(" [run {}]", state.id));
4460    detail
4461}
4462
4463/// Upper bound on [`Task::diagnostic`]'s length, in bytes.
4464///
4465/// The task file lives in the backlog indefinitely; a diagnostic is an
4466/// excerpt of the run's own `artifacts/`, not a copy of them, so this has to
4467/// stay small regardless of how much a gate command or a candidate printed.
4468const DIAGNOSTIC_MAX: usize = 4_000;
4469
4470/// Tail kept from a single failing command's output inside a diagnostic.
4471/// Smaller than [`crate::graph`]'s own `OUTPUT_TAIL` on purpose: this is a
4472/// pointer for a human deciding whether to go read the full artifact by hand,
4473/// not a replacement for reading it.
4474const DIAGNOSTIC_OUTPUT_TAIL: usize = 800;
4475
4476/// Assemble a bounded diagnostic excerpt from a held task's own run, so
4477/// `magi task show` says more than the one-line reason in [`describe`].
4478///
4479/// The one-liner answers "where did the run stop"; this answers "what would a
4480/// human have found opening `artifacts/` by hand" — the point of the whole
4481/// feature is the case that one-liner actively misleads on: a run held as "no
4482/// candidate produced a change" can mean the implementer actually finished
4483/// the task (opened a PR, merged it, tagged a release) and only left a clean
4484/// local worktree behind, which reads as "nothing happened" unless someone
4485/// goes and reads what the agent actually said. `None` when the run carries
4486/// none of the three shapes this recognises — an ordinary run held for
4487/// something not diagnosable from `RunState` alone still explains itself
4488/// through `Task::last_error`.
4489fn diagnostic(state: &RunState) -> Option<String> {
4490    let mut parts: Vec<String> = Vec::new();
4491
4492    // Gate failure: which check(s), and the tail of what each printed.
4493    for o in state.gate.iter().filter(|o| !o.ok()) {
4494        parts.push(format!(
4495            "gate `{}` failed ({:?}):\n{}",
4496            o.command,
4497            o.code,
4498            crate::run::tail(&o.output_tail, DIAGNOSTIC_OUTPUT_TAIL)
4499        ));
4500    }
4501
4502    // The land loop gave up because the fixer declined while checks were
4503    // still red: the message already names them (see `land::run`).
4504    if let Some(last) = state
4505        .events
4506        .iter()
4507        .rev()
4508        .find(|e| e.node == "land" && e.message.contains("fixer produced no commit"))
4509    {
4510        parts.push(last.message.clone());
4511    }
4512
4513    // No viable candidate: every implementer's own final word, sanitized the
4514    // same way a judge would have read it, so a run that actually finished
4515    // the job does not read as an unexplained failure. A verified no-op is
4516    // called out ahead of its own summary and apart from an ordinary
4517    // failure's `why` — this is the one candidate shape whose diagnostic a
4518    // human is expected to actually judge, not just skim.
4519    if state.viable().is_empty() {
4520        for c in &state.candidates {
4521            if let Some(evidence) = &c.verified_noop {
4522                parts.push(format!(
4523                    "candidate {} (agent-verified no-op, unconfirmed by magi): {evidence}",
4524                    c.label
4525                ));
4526            } else if !c.summary.trim().is_empty() {
4527                parts.push(format!("candidate {}: {}", c.label, c.summary.trim()));
4528            } else if let Some(why) = &c.failed {
4529                parts.push(format!("candidate {}: {why}", c.label));
4530            }
4531        }
4532    }
4533
4534    if parts.is_empty() {
4535        return None;
4536    }
4537    // `run::tail` prefixes an "N earlier bytes omitted" marker whose own
4538    // length depends on N, so asking it for exactly `DIAGNOSTIC_MAX` can come
4539    // back slightly over. Leave it enough room to always land under the
4540    // limit.
4541    Some(crate::run::tail(
4542        &parts.join("\n\n"),
4543        DIAGNOSTIC_MAX.saturating_sub(100),
4544    ))
4545}
4546
4547/// Stable lower-case name for a run status, for an internal log line and the
4548/// "graph stopped without reaching a terminal status" bug message in
4549/// [`settle`] — never for [`Task::last_error`] itself; see [`describe`]'s own
4550/// doc for why that one reads [`RunStatus::display_label`] instead. One
4551/// definition of a status's name, on the type that owns it: this table used
4552/// to live here as a second copy, and a status renamed in one place would
4553/// have gone on reading correctly in the other.
4554fn label(status: RunStatus) -> &'static str {
4555    status.as_str()
4556}
4557
4558/// Parse a merge mode override.
4559pub(crate) fn merge_mode(mode: &str) -> Result<MergeMode> {
4560    match mode {
4561        "none" => Ok(MergeMode::None),
4562        "local" => Ok(MergeMode::Local),
4563        "pr" => Ok(MergeMode::Pr),
4564        other => bail!("unknown merge mode `{other}`; expected none, local or pr"),
4565    }
4566}
4567
4568/// Take the status lock, recovering from a poisoned one.
4569///
4570/// A panic elsewhere must not silently stop the heartbeat: the status is plain
4571/// data, and the worst a poisoned lock can hold is a stale timestamp.
4572fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
4573    mutex
4574        .lock()
4575        .unwrap_or_else(std::sync::PoisonError::into_inner)
4576}
4577
4578#[cfg(test)]
4579mod tests {
4580    use super::*;
4581    use crate::queue::{Source, TaskStatus};
4582    use crate::run::{Candidate, CommandOutcome};
4583    use pretty_assertions::assert_eq;
4584
4585    fn task() -> Task {
4586        Task::new(
4587            "add retries".to_owned(),
4588            "add retries".to_owned(),
4589            PathBuf::from("/repo"),
4590            Source::Human,
4591        )
4592    }
4593
4594    #[test]
4595    fn selected_public_head_overrides_historical_resume_and_pin() {
4596        let mut t = task();
4597        let mut old = run_state(RunStatus::Blocked);
4598        let selection = crate::pr_recovery::Selection {
4599            url: "url".into(),
4600            branch: format!("magi/task-{}", t.id),
4601            head: "a".repeat(40),
4602            instruction: t.instruction.clone(),
4603            approved_at: Timestamp::now(),
4604        };
4605        t.resume_override = Some(crate::queue::OperatorResume {
4606            pinned_run: Some(old.id.clone()),
4607            question_id: "choice".into(),
4608            at: Timestamp::now(),
4609            conductor_rehold: None,
4610            forced: false,
4611        });
4612        assert_eq!(
4613            selected_starter(&selection, Some(&old), Some(&old.id)),
4614            Starter::Review(selection.branch.clone())
4615        );
4616        old.pr_recovery = Some(selection.clone());
4617        assert_eq!(
4618            selected_starter(&selection, Some(&old), Some(&old.id)),
4619            Starter::Review(selection.branch.clone())
4620        );
4621        old.reviewed_commits = Some(vec!["implementation".into()]);
4622        assert_eq!(
4623            selected_starter(&selection, Some(&old), Some(&old.id)),
4624            Starter::Resume(old.id.clone())
4625        );
4626        let mut changed = selection.clone();
4627        changed.head = "b".repeat(40);
4628        assert_eq!(
4629            selected_starter(&changed, Some(&old), Some(&old.id)),
4630            Starter::Review(changed.branch)
4631        );
4632    }
4633
4634    #[test]
4635    fn candidate_transplant_failure_is_one_actionable_hold() {
4636        let dir = tempfile::tempdir().unwrap();
4637        let q = Queue::at(dir.path().join("queue"));
4638        let mut t = task();
4639        let mut state = run_state(RunStatus::Blocked);
4640        state.event("prep", "cannot apply referenced commit: conflict");
4641        state.prep_failure = Some("conflict".into());
4642        t.start(state.id.clone());
4643        finish_attempt(3, &q, &mut t, &state, &[], Err(anyhow::anyhow!("conflict")));
4644        assert_eq!(t.attempts, 1);
4645        assert_eq!(t.status, TaskStatus::Held);
4646        assert!(t.operator_held());
4647        assert!(!t.status.runnable());
4648        assert!(t.hold_reason.as_deref().unwrap().contains("recover-pr"));
4649    }
4650
4651    #[test]
4652    fn revised_resumption_invalidates_old_pins_and_preserves_unchanged_pins() {
4653        for source in ["cli", "web"] {
4654            let mut t = task();
4655            let mut run = run_state(RunStatus::Blocked);
4656            run.instruction = "Run A original criteria".into();
4657            run.task_instruction = Some(run.instruction.clone());
4658            t.start(run.id.clone());
4659            t.release_operator("choice", Some("resume-question"));
4660            t.resume_override = Some(crate::queue::OperatorResume {
4661                question_id: "resume-question".into(),
4662                at: Timestamp::now(),
4663                conductor_rehold: None,
4664                forced: true,
4665                pinned_run: Some(run.id.clone()),
4666            });
4667            t.hold_machine(Some("not enough free space to start a run".into()));
4668            let mut unchanged = t.clone();
4669            unchanged
4670                .edit("title changed".into(), unchanged.instruction.clone())
4671                .unwrap();
4672            unchanged.release_operator(source, None);
4673            assert_eq!(
4674                unchanged
4675                    .resume_override
4676                    .as_ref()
4677                    .unwrap()
4678                    .pinned_run
4679                    .as_deref(),
4680                Some(run.id.as_str())
4681            );
4682            assert!(!compatible_run_instruction(&run, &unchanged));
4683            assert!(may_resume_run(&run, &unchanged));
4684
4685            t.edit(
4686                "approved recovery".into(),
4687                "Reuse measured PR head B; fix Clippy; review".into(),
4688            )
4689            .unwrap();
4690            assert!(t.resume_override.is_none());
4691            t.release_operator(source, None);
4692            let reloaded: Task = serde_json::from_str(&serde_json::to_string(&t).unwrap()).unwrap();
4693            assert!(reloaded.resume_override.is_none());
4694            assert!(!may_resume_run(&run, &reloaded));
4695            let unfinished =
4696                unfinished_run_with(&reloaded.runs, reloaded.short(), |_| Ok(run.clone()))
4697                    .filter(|_| may_resume_run(&run, &reloaded));
4698            assert_eq!(
4699                choose_starter(None, false, unfinished.as_deref()),
4700                Starter::Start
4701            );
4702            assert!(instruction_for(&reloaded).starts_with("Reuse measured PR head B"));
4703            assert_eq!(
4704                reloaded.resumption_history[0].question_id.as_deref(),
4705                Some("resume-question")
4706            );
4707
4708            // A revision already persisted with the old approval/pin is also
4709            // invalidated when the new operator release is recorded.
4710            unchanged.instruction = "Reuse measured PR head B".into();
4711            unchanged.hold_machine(Some("disk hold".into()));
4712            unchanged.release_operator(source, None);
4713            assert!(unchanged.resume_override.is_none());
4714            assert!(!may_resume_run(&run, &unchanged));
4715        }
4716    }
4717
4718    #[test]
4719    fn resumption_preserves_generated_review_runs_and_rejects_revised_targets() {
4720        let mut t = task();
4721        let mut run = run_state(RunStatus::Blocked);
4722        t.review_of = Some("approved-head".into());
4723        t.instruction =
4724            "Review the branch `approved-head` (review-only: no implementation, no judging)."
4725                .into();
4726        run.instruction = "Review the work already on branch `approved-head`. There is no task statement: what the change claims to do is whatever its commits say.\n\nCommit log".into();
4727        run.reviewed_commits = Some(vec!["Commit log".into()]);
4728        t.start(run.id.clone());
4729        t.hold_machine(Some("interrupted review".into()));
4730        t.release_operator("cli", None);
4731        assert!(
4732            compatible_run_instruction(&run, &t),
4733            "legacy explicit review remains resumable"
4734        );
4735        let unfinished = unfinished_run_with(&t.runs, t.short(), |_| Ok(run.clone()))
4736            .filter(|_| compatible_run_instruction(&run, &t));
4737        assert_eq!(
4738            choose_starter(None, false, unfinished.as_deref()),
4739            Starter::Resume(run.id.clone())
4740        );
4741        run.task_instruction = Some(t.instruction.clone());
4742        assert!(compatible_run_instruction(&run, &t));
4743        let reloaded: RunState =
4744            serde_json::from_str(&serde_json::to_string(&run).unwrap()).unwrap();
4745        assert!(compatible_run_instruction(&reloaded, &t));
4746        t.edit(
4747            "new approved head".into(),
4748            "Reuse measured PR head B".into(),
4749        )
4750        .unwrap();
4751        assert!(!compatible_run_instruction(&run, &t));
4752
4753        // Legacy recovery reviews retain the task criteria ahead of the log.
4754        t.review_of = None;
4755        t.instruction = "Fix Clippy on approved head".into();
4756        run.task_instruction = None;
4757        run.instruction = format!(
4758            "{}\n\n# Work already on branch `approved-head` (commit subjects)\n\nCommit log",
4759            t.instruction
4760        );
4761        assert!(compatible_run_instruction(&run, &t));
4762        t.instruction.push('\n');
4763        assert!(
4764            compatible_run_instruction(&run, &t),
4765            "legacy generated review trimmed the original criteria"
4766        );
4767        t.hold_for_handover(Some("approved-head".into()), "refused".into());
4768        let mut unchanged = t.clone();
4769        assert_eq!(
4770            take_review_branch(&mut unchanged, Some(&run)).as_deref(),
4771            Some("approved-head")
4772        );
4773        t.edit("new plan".into(), "Reuse measured PR head B".into())
4774            .unwrap();
4775        assert!(take_review_branch(&mut t, Some(&run)).is_none());
4776        assert!(!compatible_run_instruction(&run, &t));
4777        t.review_branch = Some("approved-head".into());
4778        t.review_instruction = None;
4779        assert!(
4780            take_review_branch(&mut t, Some(&run)).is_none(),
4781            "legacy retained candidate cannot replace a revised approved head"
4782        );
4783    }
4784
4785    #[test]
4786    fn resumption_actions_survive_automatic_holds_of_hand_started_runs() {
4787        crate::run::pin_test_home();
4788        let dir = tempfile::tempdir().unwrap();
4789        let queue = Queue::at(dir.path().join("queue"));
4790        for resume in [true, false] {
4791            let state = run_state(RunStatus::Blocked);
4792            let mut t = task();
4793            t.start(state.id.clone());
4794            let action = if resume {
4795                ask::ChoiceAction::Resume {
4796                    run: state.id.clone(),
4797                }
4798            } else {
4799                ask::ChoiceAction::Requeue
4800            };
4801            let mut q = action_question(&state.id, action);
4802            q.status = ask::QuestionStatus::Open;
4803            q.answer = None;
4804            q.answered_at = None;
4805            t.fail("run stopped with a question", 3);
4806            hold_if_runnable(&queue, &mut t);
4807            assert!(t.operator_decided_at.is_none());
4808            assert!(t.operator_held());
4809            q.answer(ask::Answer::Choice(q.choices[0].clone())).unwrap();
4810            let expected = if resume {
4811                ActionDecision::Resume(state.id.clone())
4812            } else {
4813                ActionDecision::Requeue
4814            };
4815            assert_eq!(
4816                decide_action(&t, &q, phrases("en"), |_| Ok(state.clone())),
4817                expected
4818            );
4819            t.hold_manual(Some("later explicit hold".into()));
4820            assert_eq!(
4821                decide_action(&t, &q, phrases("en"), |_| Ok(state.clone())),
4822                ActionDecision::Stale
4823            );
4824        }
4825    }
4826
4827    #[test]
4828    fn explicit_resumption_keeps_revised_instruction_and_audit_answers() {
4829        let mut t = task();
4830        t.answers.push(crate::queue::AnsweredQuestion {
4831            question: "Inspect the remote?".into(),
4832            answer: "Inspect then wait".into(),
4833        });
4834        let old = instruction_for(&t);
4835        assert!(compatible_instruction(&old, &t));
4836        t.instruction = "Reuse approved PR head and review".into();
4837        t.release_operator("cli", None);
4838        assert!(!compatible_instruction(&old, &t));
4839        let approved = instruction_for(&t);
4840        assert!(approved.contains("waiting premises are superseded"));
4841        assert!(compatible_instruction(&approved, &t));
4842        assert_eq!(resumed_instruction(&approved, &t), approved);
4843        t.release_operator("web", None);
4844        assert!(compatible_instruction(&approved, &t));
4845        assert_eq!(t.resumption_history.len(), 1);
4846        t.start("run".into());
4847        assert!(instruction_for(&t).contains("Explicit operator resumption"));
4848    }
4849
4850    /// A run that stopped on an infrastructure wait asked no agent anything:
4851    /// the task is held by the machine under a `[resource]` reason (resumable,
4852    /// re-checked by triage), and the attempt it started is given back.
4853    #[test]
4854    fn a_resource_blocked_run_holds_its_task_and_spends_no_attempt() {
4855        let mut state = RunState::new(
4856            PathBuf::from("/repo"),
4857            "main".to_owned(),
4858            "abc1234".to_owned(),
4859            "task".to_owned(),
4860            Config::default(),
4861        );
4862        state.status = RunStatus::Blocked;
4863        state.resource_block = Some(crate::run::ResourceBlock {
4864            node: "graft".to_owned(),
4865            kind: "shared build cache".to_owned(),
4866            detail: "timed out".to_owned(),
4867            at: jiff::Timestamp::now(),
4868        });
4869        let mut t = task();
4870        t.start("20260902-000000-aaaa".to_owned());
4871        assert_eq!(t.attempts, 1);
4872        let verdict = Verdict {
4873            status: RunStatus::Blocked,
4874            left_pr: false,
4875            quota_hit: false,
4876            parked: false,
4877            no_viable_candidates: false,
4878        };
4879        settle_and_diagnose(&mut t, verdict, "blocked", 2, &state);
4880        assert_eq!(t.status, TaskStatus::Held);
4881        assert_eq!(t.attempts, 0, "the attempt is refunded");
4882        assert!(t.hold_reason.as_deref().unwrap().starts_with("[resource] "));
4883        assert!(
4884            !t.operator_held(),
4885            "a machine hold, released without a human"
4886        );
4887
4888        // A park request outranks it: still the operator's doing.
4889        let mut p = task();
4890        p.start("20260902-000000-bbbb".to_owned());
4891        settle_and_diagnose(
4892            &mut p,
4893            Verdict {
4894                parked: true,
4895                ..verdict
4896            },
4897            "parked",
4898            2,
4899            &state,
4900        );
4901        assert_eq!(p.status, TaskStatus::Parked);
4902    }
4903
4904    #[test]
4905    fn record_failure_notice_links_the_task() {
4906        let dir = tempfile::tempdir().unwrap();
4907        // A regular file where the queue directory should be makes every
4908        // `put` fail, on every platform and whoever runs the test.
4909        std::fs::write(dir.path().join("queue"), b"not a directory").unwrap();
4910        let queue = Queue::at(dir.path().join("queue"));
4911        let mut t = interrupt_task("rec-fail-1");
4912        record(&queue, &mut t);
4913
4914        let found = crate::notices::Notices::at(dir.path().join("notifications")).list();
4915        assert_eq!(found.len(), 1);
4916        assert_eq!(found[0].key, "loop:record");
4917        assert_eq!(
4918            found[0].link,
4919            Some(Link::Task {
4920                id: "rec-fail-1".to_owned()
4921            })
4922        );
4923    }
4924
4925    /// A runnable task marked to interrupt, with an id fixed for assertions
4926    /// rather than the random one [`Task::new`] mints.
4927    fn interrupt_task(id: &str) -> Task {
4928        let mut t = task();
4929        t.id = id.to_owned();
4930        t.interrupt = true;
4931        t
4932    }
4933
4934    /// An ordinary runnable task with an id fixed for assertions.
4935    fn task_with_id(id: &str) -> Task {
4936        let mut t = task();
4937        t.id = id.to_owned();
4938        t
4939    }
4940
4941    /// A runnable task marked `--urgent`, with an id fixed for assertions.
4942    fn urgent_task(id: &str) -> Task {
4943        let mut t = task();
4944        t.id = id.to_owned();
4945        t.urgent = true;
4946        t
4947    }
4948
4949    #[test]
4950    fn urgent_dispatch_has_one_extra_slot_and_never_uses_a_stale_flag() {
4951        let dir = tempfile::tempdir().unwrap();
4952        let queue = Queue::at(dir.path().join("queue"));
4953        let ordinary = Arc::new(tokio::sync::Semaphore::new(1));
4954        let urgent = Arc::new(tokio::sync::Semaphore::new(1));
4955        let occupied = Arc::clone(&ordinary).try_acquire_owned().unwrap();
4956        let mut first = task();
4957        queue.put(&mut first).unwrap();
4958        let stale_plain = queue.get(&first.id).unwrap();
4959        first.set_urgent(true).unwrap();
4960        queue.put(&mut first).unwrap();
4961        let dispatched = claim_dispatch(&queue, &stale_plain, false, &ordinary, &urgent).unwrap();
4962        let mut second = urgent_task("second");
4963        queue.put(&mut second).unwrap();
4964        assert!(claim_dispatch(&queue, &second, false, &ordinary, &urgent).is_none());
4965        drop(queue.claim(&second.id).unwrap());
4966        assert_eq!(queue.get(&second.id).unwrap().attempts, 0);
4967        drop(dispatched);
4968        let stale_urgent = queue.get(&first.id).unwrap();
4969        first.set_urgent(false).unwrap();
4970        queue.put(&mut first).unwrap();
4971        assert!(claim_dispatch(&queue, &stale_urgent, false, &ordinary, &urgent).is_none());
4972        assert_eq!(urgent.available_permits(), 1);
4973        drop(queue.claim(&first.id).unwrap());
4974        drop(occupied);
4975    }
4976
4977    #[test]
4978    fn urgent_dispatch_uses_saved_lane_and_releases_unsuccessful_claims() {
4979        let dir = tempfile::tempdir().unwrap();
4980        let queue = Queue::at(dir.path().join("queue"));
4981        let ordinary = Arc::new(tokio::sync::Semaphore::new(1));
4982        let urgent = Arc::new(tokio::sync::Semaphore::new(1));
4983        let mut t = task();
4984        queue.put(&mut t).unwrap();
4985        for enabled in [true, false] {
4986            let stale = queue.get(&t.id).unwrap();
4987            t.set_urgent(enabled).unwrap();
4988            queue.put(&mut t).unwrap();
4989            assert_ne!(stale.urgent, enabled);
4990            let DispatchClaim {
4991                task: fresh,
4992                claim,
4993                permit,
4994            } = claim_dispatch(&queue, &stale, false, &ordinary, &urgent).unwrap();
4995            assert_eq!(fresh.urgent, enabled);
4996            assert_eq!(ordinary.available_permits(), usize::from(enabled));
4997            assert_eq!(urgent.available_permits(), usize::from(!enabled));
4998            assert!(queue.claim(&t.id).is_err());
4999            assert_eq!(fresh.attempts, stale.attempts);
5000            drop((claim, permit));
5001            assert_eq!(ordinary.available_permits(), 1);
5002            assert_eq!(urgent.available_permits(), 1);
5003        }
5004        t.set_urgent(true).unwrap();
5005        queue.put(&mut t).unwrap();
5006        let occupied = Arc::clone(&urgent).try_acquire_owned().unwrap();
5007        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
5008        assert_eq!(ordinary.available_permits(), 1, "no ordinary fallback");
5009        drop(queue.claim(&t.id).unwrap());
5010        assert_eq!(queue.get(&t.id).unwrap().attempts, t.attempts);
5011        drop(occupied);
5012        assert!(claim_dispatch(&queue, &t, true, &ordinary, &urgent).is_none());
5013        drop(queue.claim(&t.id).unwrap());
5014        let held = queue.claim(&t.id).unwrap();
5015        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
5016        drop(held);
5017        t.status = TaskStatus::Held;
5018        queue.put(&mut t).unwrap();
5019        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
5020        drop(queue.claim(&t.id).unwrap());
5021        std::fs::remove_file(queue.path_of(&t.id)).unwrap();
5022        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
5023        drop(queue.claim(&t.id).unwrap());
5024        assert_eq!(urgent.available_permits(), 1);
5025    }
5026
5027    /// The land-resume exemption wins outright, whether or not the candidate
5028    /// also happens to be marked [`Task::urgent`]: a resume's own "must not
5029    /// queue behind anything" guarantee cannot be weaker just because the
5030    /// same task was also filed with `--urgent`.
5031    #[test]
5032    fn permit_kind_prefers_a_land_resume_over_the_urgent_slot() {
5033        assert_eq!(permit_kind(true, false), PermitKind::None);
5034        assert_eq!(permit_kind(true, true), PermitKind::None);
5035    }
5036
5037    /// The one property this whole feature exists for: `--urgent` draws from
5038    /// its own slot, never the ordinary `max_concurrent_runs` pool - and an
5039    /// ordinary candidate draws from the ordinary pool exactly as before,
5040    /// untouched by the urgent slot's existence.
5041    #[test]
5042    fn permit_kind_separates_urgent_from_ordinary() {
5043        assert_eq!(permit_kind(false, true), PermitKind::Urgent);
5044        assert_eq!(permit_kind(false, false), PermitKind::Ordinary);
5045    }
5046
5047    /// The exact wiring `attempt` runs before minting anything: a config's
5048    /// `min_free_bytes` in, a task-holding reason naming both numbers out.
5049    /// Free space is injected rather than asked of the real disk - the point
5050    /// of [`disk_gate_with`] existing separately from [`disk_gate`] - so this
5051    /// is deterministic on every machine this test runs on, never dependent
5052    /// on how full the CI runner's own disk happens to be.
5053    #[test]
5054    fn disk_gate_with_holds_a_task_below_the_threshold_and_names_both_numbers() {
5055        let cfg = Config::default();
5056        let repo = Path::new("/any/repo/path");
5057
5058        let reason =
5059            disk_gate_with(repo, &cfg, |_| Ok(1024)).expect("must hold below the threshold");
5060        assert!(reason.contains("1024"), "{reason}");
5061        assert!(
5062            reason.contains(&cfg.disk.min_free_bytes.to_string()),
5063            "{reason}"
5064        );
5065
5066        assert_eq!(
5067            disk_gate_with(repo, &cfg, |_| Ok(cfg.disk.min_free_bytes)),
5068            None,
5069            "exactly at the floor is open"
5070        );
5071        assert_eq!(
5072            disk_gate_with(repo, &cfg, |_| Ok(cfg.disk.min_free_bytes + 1)),
5073            None,
5074            "comfortably above the floor is open"
5075        );
5076    }
5077
5078    #[test]
5079    fn disk_gate_with_opens_unconditionally_when_the_operator_opted_out() {
5080        let mut cfg = Config::default();
5081        cfg.disk.min_free_bytes = 0;
5082        let repo = Path::new("/any/repo/path");
5083        assert_eq!(
5084            disk_gate_with(repo, &cfg, |_| Ok(0)),
5085            None,
5086            "a zero floor never measures at all"
5087        );
5088    }
5089
5090    #[test]
5091    fn disk_gate_with_closes_rather_than_starts_blind_when_it_cannot_measure() {
5092        let cfg = Config::default();
5093        let repo = Path::new("/any/repo/path");
5094        let reason = disk_gate_with(repo, &cfg, |_| Err(anyhow::anyhow!("no df on this box")))
5095            .expect("a measurement failure must close the gate, not open it");
5096        assert!(reason.contains("could not measure"), "{reason}");
5097    }
5098
5099    #[test]
5100    fn no_interrupt_task_leaves_the_sequence_idle_even_with_something_in_flight() {
5101        let ordinary = task();
5102        let next = advance_interrupt(
5103            Interrupt::Idle,
5104            std::slice::from_ref(&ordinary.id),
5105            std::slice::from_ref(&ordinary),
5106        );
5107        assert_eq!(next, Interrupt::Idle);
5108    }
5109
5110    #[test]
5111    fn an_interrupt_task_with_nothing_in_flight_never_starts_a_sequence() {
5112        // Nothing to interrupt - this is just an ordinary candidate, and the
5113        // loop's normal dispatch will pick it up like any other.
5114        let marked = interrupt_task("marked");
5115        let next = advance_interrupt(Interrupt::Idle, &[], std::slice::from_ref(&marked));
5116        assert_eq!(next, Interrupt::Idle);
5117    }
5118
5119    #[test]
5120    fn an_interrupt_task_with_something_in_flight_starts_parking_it() {
5121        let marked = interrupt_task("marked");
5122        let next = advance_interrupt(
5123            Interrupt::Idle,
5124            &["running".to_owned()],
5125            std::slice::from_ref(&marked),
5126        );
5127        assert_eq!(
5128            next,
5129            Interrupt::Parking {
5130                parked: vec!["running".to_owned()],
5131                interrupt_task: "marked".to_owned(),
5132            }
5133        );
5134    }
5135
5136    /// R1-1-2 / R2-1-2: above the default `max_concurrent_runs`, more than
5137    /// one run can be in flight when a task becomes runnable and marked.
5138    /// Parking all of them would mean `Resuming` later has more than one id
5139    /// to release back to ordinary dispatch, which cannot be made safe
5140    /// against that same setting's own extra concurrency slots letting two
5141    /// of them start together - see `advance_interrupt`'s own `Idle` branch.
5142    /// The simplification the task's own constraints ask for: do not begin
5143    /// a sequence at all until the herd settles back to exactly one.
5144    #[test]
5145    fn more_than_one_run_in_flight_never_starts_an_interrupt_sequence() {
5146        let marked = interrupt_task("marked");
5147
5148        let two = advance_interrupt(
5149            Interrupt::Idle,
5150            &["a".to_owned(), "b".to_owned()],
5151            std::slice::from_ref(&marked),
5152        );
5153        assert_eq!(two, Interrupt::Idle);
5154
5155        let none = advance_interrupt(Interrupt::Idle, &[], std::slice::from_ref(&marked));
5156        assert_eq!(none, Interrupt::Idle, "nothing to interrupt either");
5157    }
5158
5159    #[test]
5160    fn parking_holds_until_every_parked_id_has_actually_left_flight() {
5161        let state = Interrupt::Parking {
5162            parked: vec!["running".to_owned()],
5163            interrupt_task: "marked".to_owned(),
5164        };
5165        // Still in flight: no change.
5166        let still_going = advance_interrupt(state.clone(), &["running".to_owned()], &[]);
5167        assert_eq!(still_going, state);
5168
5169        // Left flight, but the interrupt task has not been dispatched yet on
5170        // this tick - stays `Parking` so `interrupt_gate` can let it through,
5171        // as long as it is still runnable.
5172        let stopped_but_not_yet_dispatched =
5173            advance_interrupt(state.clone(), &[], &[interrupt_task("marked")]);
5174        assert_eq!(stopped_but_not_yet_dispatched, state);
5175
5176        // Left flight, and the interrupt task is now in flight itself.
5177        let dispatched = advance_interrupt(state, &["marked".to_owned()], &[]);
5178        assert_eq!(
5179            dispatched,
5180            Interrupt::Running {
5181                parked: vec!["running".to_owned()],
5182                interrupt_task: "marked".to_owned(),
5183            }
5184        );
5185    }
5186
5187    #[test]
5188    fn the_sequence_moves_to_resuming_the_instant_the_interrupt_tasks_own_run_leaves_flight() {
5189        let state = Interrupt::Running {
5190            parked: vec!["running".to_owned()],
5191            interrupt_task: "marked".to_owned(),
5192        };
5193        let still_running = advance_interrupt(state.clone(), &["marked".to_owned()], &[]);
5194        assert_eq!(still_running, state);
5195
5196        // Whatever it ended as - merged, failed, held - is not this
5197        // function's concern: leaving flight is the only trigger, driven
5198        // straight off the same in-flight list `poll` already reaps. It does
5199        // not go straight to `Idle`: see `Interrupt::Running`'s own doc for
5200        // why that would let an unrelated task start ahead of, or alongside,
5201        // the guaranteed resume.
5202        let ended = advance_interrupt(state, &[], &[task_with_id("running")]);
5203        assert_eq!(
5204            ended,
5205            Interrupt::Resuming {
5206                parked: vec!["running".to_owned()]
5207            }
5208        );
5209    }
5210
5211    #[test]
5212    fn resuming_ends_the_instant_a_parked_task_is_seen_in_flight() {
5213        let state = Interrupt::Resuming {
5214            parked: vec!["running".to_owned()],
5215        };
5216        let still_waiting = advance_interrupt(state.clone(), &[], &[task_with_id("running")]);
5217        assert_eq!(still_waiting, state);
5218
5219        let dispatched = advance_interrupt(state, &["running".to_owned()], &[]);
5220        assert_eq!(dispatched, Interrupt::Idle);
5221    }
5222
5223    /// R1-2-1: an interrupt task that stops being runnable - held, blocked,
5224    /// or otherwise moved on by an operator with no claim standing in the
5225    /// way - must not wedge the sequence (and so the whole loop's dispatch,
5226    /// via `interrupt_gate`) waiting forever for a dispatch that can never
5227    /// come. The parked run still gets its resume.
5228    #[test]
5229    fn an_interrupt_task_that_stops_being_runnable_abandons_the_wait_without_losing_the_parked_run()
5230    {
5231        let state = Interrupt::Parking {
5232            parked: vec!["running".to_owned()],
5233            interrupt_task: "marked".to_owned(),
5234        };
5235        // `marked` has been held/blocked/deleted since the sequence began:
5236        // it no longer appears in `runnable` at all.
5237        let next = advance_interrupt(state, &[], &[]);
5238        assert_eq!(
5239            next,
5240            Interrupt::Resuming {
5241                parked: vec!["running".to_owned()]
5242            },
5243            "abandoning the interrupt must not abandon the resume it owes"
5244        );
5245    }
5246
5247    /// The same abandonment, one step later: `Resuming` itself must not wait
5248    /// forever for a parked task that has since become unrunnable.
5249    #[test]
5250    fn resuming_abandons_a_parked_task_that_stops_being_runnable() {
5251        let state = Interrupt::Resuming {
5252            parked: vec!["running".to_owned()],
5253        };
5254        let next = advance_interrupt(state, &[], &[]);
5255        assert_eq!(
5256            next,
5257            Interrupt::Idle,
5258            "nothing is left to wait for; the loop must not stay wedged"
5259        );
5260    }
5261
5262    #[test]
5263    fn disabled_by_config_the_sequence_can_never_leave_idle() {
5264        let marked = interrupt_task("marked");
5265        let next = advance_interrupt_tick(
5266            false,
5267            Interrupt::Idle,
5268            &["running".to_owned()],
5269            std::slice::from_ref(&marked),
5270        );
5271        assert_eq!(
5272            next,
5273            Interrupt::Idle,
5274            "an unmarked, unconfigured daemon must behave exactly as before"
5275        );
5276    }
5277
5278    #[test]
5279    fn the_gate_blocks_everyone_while_something_parked_is_still_in_flight() {
5280        let state = Interrupt::Parking {
5281            parked: vec!["running".to_owned()],
5282            interrupt_task: "marked".to_owned(),
5283        };
5284        let candidates = vec![interrupt_task("marked"), task()];
5285        let allowed = interrupt_gate(&state, &["running".to_owned()], candidates);
5286        assert!(
5287            allowed.is_empty(),
5288            "nothing may dispatch - not even the interrupt task itself - \
5289             until the parked run has actually stopped"
5290        );
5291    }
5292
5293    /// `interrupt_gate` knows nothing about [`Task::urgent`] and must not
5294    /// grow a special case for it: an urgent candidate that is not the
5295    /// interrupt task withholds exactly like an ordinary one for as long as
5296    /// the parked run it is racing has not actually left flight. A version
5297    /// of this feature once bypassed the gate for urgent candidates, on the
5298    /// theory that a genuinely separate concurrency lane could not collide
5299    /// with 75dd's own; it could, and did - the bypassed task dispatched
5300    /// alongside a run 75dd's own state machine had only *asked* to park,
5301    /// not yet confirmed gone, which is exactly the second run
5302    /// `Interrupt::Parking`'s own doc says must never happen. There is no
5303    /// tick during Parking/Running/Resuming where letting an extra
5304    /// candidate through is safe, urgent or not - the parked run may still
5305    /// be genuinely mid-call.
5306    #[test]
5307    fn urgent_gains_no_exemption_from_an_active_interrupt_sequence() {
5308        for state in [
5309            Interrupt::Parking {
5310                parked: vec!["running".to_owned()],
5311                interrupt_task: "marked".to_owned(),
5312            },
5313            Interrupt::Running {
5314                parked: vec!["running".to_owned()],
5315                interrupt_task: "marked".to_owned(),
5316            },
5317            Interrupt::Resuming {
5318                parked: vec!["running".to_owned()],
5319            },
5320        ] {
5321            let candidates = vec![
5322                interrupt_task("marked"),
5323                urgent_task("hot"),
5324                task_with_id("ordinary"),
5325            ];
5326            let allowed = interrupt_gate(&state, &["running".to_owned()], candidates);
5327            assert!(
5328                !allowed.iter().any(|t| t.id == "hot"),
5329                "an urgent candidate must wait out the same gate as anything \
5330                 else while the run it would run alongside has not actually \
5331                 left flight, for state {state:?}: {allowed:?}"
5332            );
5333        }
5334    }
5335
5336    /// The one case where marking a task `--urgent` and `interrupt` at once
5337    /// is not redundant: once the interrupt sequence's own guaranteed
5338    /// resume/dispatch actually admits the task, it is unaffected by also
5339    /// carrying `urgent` - `interrupt_gate` decides purely on identity,
5340    /// never on the urgent flag.
5341    #[test]
5342    fn a_task_marked_both_urgent_and_interrupt_is_admitted_once_the_gate_itself_says_so() {
5343        let state = Interrupt::Resuming {
5344            parked: vec!["hot".to_owned()],
5345        };
5346        let candidates = vec![urgent_task("hot"), task()];
5347        let allowed = interrupt_gate(&state, &[], candidates);
5348        assert_eq!(
5349            allowed.iter().filter(|t| t.id == "hot").count(),
5350            1,
5351            "the gate's own decision is unaffected by the urgent flag: {allowed:?}"
5352        );
5353    }
5354
5355    #[test]
5356    fn the_gate_lets_only_the_interrupt_task_through_once_parked_work_has_stopped() {
5357        let state = Interrupt::Parking {
5358            parked: vec!["running".to_owned()],
5359            interrupt_task: "marked".to_owned(),
5360        };
5361        let other = task();
5362        let candidates = vec![interrupt_task("marked"), other.clone()];
5363        let allowed = interrupt_gate(&state, &[], candidates);
5364        assert_eq!(allowed.len(), 1);
5365        assert_eq!(allowed[0].id, "marked");
5366    }
5367
5368    #[test]
5369    fn the_gate_blocks_everyone_while_the_interrupt_task_itself_is_in_flight() {
5370        let state = Interrupt::Running {
5371            parked: vec!["running".to_owned()],
5372            interrupt_task: "marked".to_owned(),
5373        };
5374        let candidates = vec![task(), task()];
5375        let allowed = interrupt_gate(&state, &["marked".to_owned()], candidates);
5376        assert!(allowed.is_empty());
5377    }
5378
5379    /// R1-1-1 / R1-1-2: even when more than one task was in flight when the
5380    /// sequence began (only reachable above the default
5381    /// `max_concurrent_runs = 1`), `Resuming` offers at most one of them -
5382    /// never both in the same tick, which is what "exactly one resume, no
5383    /// simultaneous run" actually requires structurally rather than by
5384    /// coincidence of how many ordinary slots happen to be free.
5385    #[test]
5386    fn the_gate_offers_at_most_one_candidate_while_resuming_even_with_two_parked() {
5387        let state = Interrupt::Resuming {
5388            parked: vec!["a".to_owned(), "c".to_owned()],
5389        };
5390        let candidates = vec![task_with_id("a"), task_with_id("c"), task_with_id("other")];
5391        let allowed = interrupt_gate(&state, &[], candidates);
5392        assert_eq!(
5393            allowed.len(),
5394            1,
5395            "at most one candidate may be offered while resuming: {allowed:?}"
5396        );
5397        assert_eq!(allowed[0].id, "a");
5398    }
5399
5400    #[test]
5401    fn the_gate_offers_nothing_while_resuming_if_no_parked_task_is_runnable() {
5402        let state = Interrupt::Resuming {
5403            parked: vec!["a".to_owned()],
5404        };
5405        let allowed = interrupt_gate(&state, &[], vec![task_with_id("other")]);
5406        assert!(allowed.is_empty());
5407    }
5408
5409    /// The invariant the completion criteria ask for by name: across a whole
5410    /// simulated sequence, there is never a tick where the gate would let
5411    /// through both the parked run's resume and the interrupt task, and
5412    /// exactly one candidate resumes the instant the interrupt task's run
5413    /// ends - never zero, never more than one.
5414    #[test]
5415    fn a_full_sequence_never_gates_two_runs_through_at_once_and_resumes_exactly_one() {
5416        let running = task(); // id: whatever `Task::new` minted
5417        let marked = interrupt_task("marked");
5418
5419        let mut state = Interrupt::Idle;
5420        // Tick 1: `running` is in flight, `marked` becomes runnable.
5421        let in_flight = vec![running.id.clone()];
5422        state = advance_interrupt_tick(true, state, &in_flight, std::slice::from_ref(&marked));
5423        let gated = interrupt_gate(&state, &in_flight, vec![marked.clone(), running.clone()]);
5424        assert!(gated.is_empty(), "still waiting on `running` to park");
5425
5426        // Tick 2: `running` parked and left flight; nothing dispatched yet.
5427        state = advance_interrupt_tick(true, state, &[], &[marked.clone(), running.clone()]);
5428        let gated = interrupt_gate(&state, &[], vec![marked.clone(), running.clone()]);
5429        assert_eq!(
5430            gated.iter().map(|t| t.id.as_str()).collect::<Vec<_>>(),
5431            vec!["marked"],
5432            "only the interrupt task may be offered to the dispatcher now"
5433        );
5434
5435        // Tick 3: `marked` is now in flight (dispatched from tick 2's gate).
5436        state = advance_interrupt_tick(
5437            true,
5438            state,
5439            &["marked".to_owned()],
5440            std::slice::from_ref(&running),
5441        );
5442        let gated = interrupt_gate(
5443            &state,
5444            &["marked".to_owned()],
5445            vec![marked.clone(), running.clone()],
5446        );
5447        assert!(
5448            gated.is_empty(),
5449            "the parked run must not be offered back while the interrupt \
5450             task is still running"
5451        );
5452
5453        // Tick 4: `marked`'s run reached a terminal status and left flight.
5454        // A higher-priority ordinary task `other` is also runnable now - it
5455        // must not be let through instead of, or alongside, `running`.
5456        let other = task_with_id("other");
5457        state = advance_interrupt_tick(true, state, &[], &[running.clone(), other.clone()]);
5458        assert_eq!(
5459            state,
5460            Interrupt::Resuming {
5461                parked: vec![running.id.clone()]
5462            }
5463        );
5464        let gated = interrupt_gate(&state, &[], vec![other.clone(), running.clone()]);
5465        assert_eq!(
5466            gated.iter().map(|t| t.id.as_str()).collect::<Vec<_>>(),
5467            vec![running.id.as_str()],
5468            "exactly the parked run resumes - not the unrelated task, even \
5469             though it was offered first"
5470        );
5471
5472        // Tick 5: `running` is now in flight (dispatched from tick 4's
5473        // gate). Only now does the sequence end and ordinary dispatch fully
5474        // resume.
5475        state = advance_interrupt_tick(
5476            true,
5477            state,
5478            std::slice::from_ref(&running.id),
5479            std::slice::from_ref(&other),
5480        );
5481        assert_eq!(state, Interrupt::Idle);
5482        let gated = interrupt_gate(
5483            &state,
5484            std::slice::from_ref(&running.id),
5485            vec![other.clone()],
5486        );
5487        assert_eq!(
5488            gated.iter().map(|t| t.id.as_str()).collect::<Vec<_>>(),
5489            vec![other.id.as_str()],
5490            "ordinary dispatch is unrestricted again"
5491        );
5492    }
5493
5494    #[test]
5495    fn every_run_status_settles_the_task_it_came_from() {
5496        // run status, resulting task status, attempts still standing after one
5497        let table = [
5498            (RunStatus::Merged, TaskStatus::Done, 1),
5499            (RunStatus::Ready, TaskStatus::Done, 1),
5500            (RunStatus::Stalled, TaskStatus::Failed, 0),
5501            (RunStatus::Blocked, TaskStatus::Failed, 1),
5502            (RunStatus::Failed, TaskStatus::Failed, 1),
5503            (RunStatus::VerifiedNoop, TaskStatus::Held, 1),
5504            (RunStatus::Prep, TaskStatus::Failed, 1),
5505            (RunStatus::Implementing, TaskStatus::Failed, 1),
5506            (RunStatus::Judging, TaskStatus::Failed, 1),
5507            (RunStatus::Deliberating, TaskStatus::Failed, 1),
5508            (RunStatus::Voting, TaskStatus::Failed, 1),
5509            (RunStatus::Reviewing, TaskStatus::Failed, 1),
5510            (RunStatus::Gating, TaskStatus::Failed, 1),
5511        ];
5512        for (run, want, attempts) in table {
5513            let mut t = task();
5514            t.start("20260902-000000-aaaa".to_owned());
5515            settle(
5516                &mut t,
5517                Verdict {
5518                    status: run,
5519                    left_pr: false,
5520                    parked: false,
5521                    quota_hit: matches!(run, RunStatus::Stalled),
5522                    no_viable_candidates: false,
5523                },
5524                "why",
5525                2,
5526            );
5527            assert_eq!(t.status, want, "task status after {}", label(run));
5528            assert_eq!(t.attempts, attempts, "attempts after {}", label(run));
5529        }
5530    }
5531
5532    #[test]
5533    fn a_quota_stall_costs_the_task_no_attempt_but_a_block_does() {
5534        let mut stalled = task();
5535        stalled.start("20260902-000000-aaaa".to_owned());
5536        settle(
5537            &mut stalled,
5538            Verdict {
5539                status: RunStatus::Stalled,
5540                left_pr: false,
5541                parked: false,
5542                quota_hit: true,
5543                no_viable_candidates: false,
5544            },
5545            "quota",
5546            1,
5547        );
5548        assert_eq!(stalled.attempts, 0);
5549        assert!(
5550            stalled.status.runnable(),
5551            "a machine problem must leave the task in line"
5552        );
5553
5554        let mut blocked = task();
5555        blocked.start("20260902-000000-aaaa".to_owned());
5556        settle(
5557            &mut blocked,
5558            Verdict {
5559                status: RunStatus::Blocked,
5560                left_pr: false,
5561                parked: false,
5562                quota_hit: false,
5563                no_viable_candidates: false,
5564            },
5565            "findings open",
5566            1,
5567        );
5568        assert_eq!(blocked.attempts, 1);
5569        assert_eq!(
5570            blocked.status,
5571            TaskStatus::Held,
5572            "the last attempt hands the task to a human"
5573        );
5574    }
5575
5576    #[test]
5577    fn a_run_that_opened_a_pull_request_is_never_re_competed() {
5578        // Attempts to spare: without the pull request this task would go
5579        // straight back in line and run the whole competition again.
5580        let mut delivered = task();
5581        delivered.start("20260903-080619-01c2".to_owned());
5582        settle(
5583            &mut delivered,
5584            Verdict {
5585                status: RunStatus::Blocked,
5586                left_pr: true,
5587                parked: false,
5588                quota_hit: false,
5589                no_viable_candidates: false,
5590            },
5591            "no check status",
5592            4,
5593        );
5594        assert_eq!(
5595            delivered.status,
5596            TaskStatus::Held,
5597            "a pull request waiting on CI or a person is not a retryable failure"
5598        );
5599        assert!(
5600            !delivered.status.runnable(),
5601            "the loop must not pick this task up again"
5602        );
5603        assert_eq!(
5604            delivered.last_error.as_deref(),
5605            Some("no check status"),
5606            "the operator needs to be told what the gate was waiting for"
5607        );
5608
5609        // The same status without a pull request is a plain failure, and with
5610        // attempts left it is retried.
5611        let mut empty_handed = task();
5612        empty_handed.start("20260903-080619-01c2".to_owned());
5613        settle(
5614            &mut empty_handed,
5615            Verdict {
5616                status: RunStatus::Blocked,
5617                left_pr: false,
5618                parked: false,
5619                quota_hit: false,
5620                no_viable_candidates: false,
5621            },
5622            "findings open",
5623            4,
5624        );
5625        assert_eq!(empty_handed.status, TaskStatus::Failed);
5626        assert!(empty_handed.status.runnable());
5627    }
5628
5629    #[test]
5630    fn a_verified_noop_run_hands_off_rather_than_closing_or_auto_retrying() {
5631        // Every candidate agreed, with evidence, that nothing belonged in the
5632        // worktree. That is not a confirmed success to close automatically -
5633        // a human still has to check the claim - and it is not an ordinary
5634        // failure either, so this settles exactly like a pull request nobody
5635        // merged yet: `Held`, same as `Blocked` with a PR.
5636        let mut noop = task();
5637        noop.start("20260912-131304-391f".to_owned());
5638        settle(
5639            &mut noop,
5640            Verdict {
5641                status: RunStatus::VerifiedNoop,
5642                left_pr: false,
5643                parked: false,
5644                quota_hit: false,
5645                no_viable_candidates: true,
5646            },
5647            "candidate A: already fixed by b32cfc4, on main",
5648            4,
5649        );
5650        assert_eq!(
5651            noop.status,
5652            TaskStatus::Held,
5653            "an unverified claim is a request for a human, not a failure"
5654        );
5655        assert!(
5656            !noop.status.runnable(),
5657            "the loop must not requeue this on the same unverified claim"
5658        );
5659        // `Task::release` resets attempts to zero the moment a human looks at
5660        // the evidence and lets it run again, so it does not matter here
5661        // whether the one attempt already spent stays spent - what matters is
5662        // that nothing retries this task unattended in the meantime.
5663        assert_eq!(noop.attempts, 1);
5664    }
5665
5666    #[test]
5667    fn parking_costs_the_task_no_attempt_and_leaves_it_in_line() {
5668        // Parking is the operator asking for the process back - to replace the
5669        // binary, most of all. The run's work is intact on disk, so this is
5670        // not a failed attempt, and charging for it would mean a few upgrades
5671        // could exhaust a budget meant for agents that misbehaved.
5672        let mut parked = task();
5673        parked.start("20260903-183634-2d98".to_owned());
5674        settle(
5675            &mut parked,
5676            Verdict {
5677                status: RunStatus::Implementing,
5678                left_pr: false,
5679                quota_hit: false,
5680                parked: true,
5681                no_viable_candidates: false,
5682            },
5683            "parked after `implementing`",
5684            2,
5685        );
5686        assert_eq!(parked.attempts, 0, "a park is refunded");
5687        assert!(
5688            parked.status.runnable(),
5689            "and the task stays in line so the next loop resumes its run"
5690        );
5691        assert_eq!(parked.status, TaskStatus::Parked);
5692        assert_eq!(
5693            parked.park_reason.as_deref(),
5694            Some("parked after `implementing`"),
5695            "the card says where it stopped"
5696        );
5697        assert_eq!(parked.last_error, None, "nothing failed");
5698
5699        // Without the park flag the same non-terminal status is what it always
5700        // was: `execute` returning mid-flight, which is a bug and spends an
5701        // attempt so a task cannot loop on it forever.
5702        let mut broken = task();
5703        broken.start("20260903-183634-2d98".to_owned());
5704        settle(
5705            &mut broken,
5706            Verdict {
5707                status: RunStatus::Implementing,
5708                left_pr: false,
5709                quota_hit: false,
5710                parked: false,
5711                no_viable_candidates: false,
5712            },
5713            "returned mid-flight",
5714            2,
5715        );
5716        assert_eq!(broken.attempts, 1);
5717    }
5718
5719    #[test]
5720    fn only_a_rate_limit_buys_the_task_its_attempt_back() {
5721        // Run e633: quorum lost because two judges answered with the wrong
5722        // JSON shape, `quota: []`. Refunding that takes the bound off the
5723        // retry loop, and each retry pays for a fresh hour-long implement
5724        // wave before it can fail the same way.
5725        let mut flaky = task();
5726        flaky.start("20260903-123023-e633".to_owned());
5727        settle(
5728            &mut flaky,
5729            Verdict {
5730                status: RunStatus::Stalled,
5731                left_pr: false,
5732                parked: false,
5733                quota_hit: false,
5734                no_viable_candidates: false,
5735            },
5736            "verdict rests on 1 of 3 judges",
5737            2,
5738        );
5739        assert_eq!(
5740            flaky.attempts, 1,
5741            "flakiness spends an attempt, so `max_attempts` still bounds it"
5742        );
5743        assert!(flaky.status.runnable(), "and it is still worth retrying");
5744
5745        // The same status, lost to a rate limit, is the machine's fault.
5746        let mut limited = task();
5747        limited.start("20260903-123023-e633".to_owned());
5748        settle(
5749            &mut limited,
5750            Verdict {
5751                status: RunStatus::Stalled,
5752                left_pr: false,
5753                parked: false,
5754                quota_hit: true,
5755                no_viable_candidates: false,
5756            },
5757            "judge-2, judge-3 out of quota",
5758            2,
5759        );
5760        assert_eq!(limited.attempts, 0, "a quota window is refunded");
5761        assert!(limited.status.runnable());
5762
5763        // And the bound really binds: a task that keeps stalling on flakiness
5764        // reaches a human instead of running the roster forever.
5765        let mut worn = task();
5766        for _ in 0..2 {
5767            worn.release();
5768        }
5769        worn.start("20260903-123023-e633".to_owned());
5770        worn.attempts = 2;
5771        settle(
5772            &mut worn,
5773            Verdict {
5774                status: RunStatus::Stalled,
5775                left_pr: false,
5776                parked: false,
5777                quota_hit: false,
5778                no_viable_candidates: false,
5779            },
5780            "no quorum again",
5781            2,
5782        );
5783        assert_eq!(worn.status, TaskStatus::Held);
5784        assert!(!worn.status.runnable());
5785    }
5786
5787    #[test]
5788    fn a_quota_wipeout_that_leaves_nothing_to_judge_also_costs_no_attempt() {
5789        // The implement wave loses every seat to the same rate limit and
5790        // `after_implement` bails with nothing viable, which surfaces as
5791        // `Failed` rather than `Stalled`. That is the same machine fact the
5792        // `Stalled`-quota row already refunds, and must be refunded the same
5793        // way, or a quota outage quietly holds every task it touches instead
5794        // of leaving them in line for the reset.
5795        let mut wiped_out = task();
5796        wiped_out.start("20260907-025000-a1b2".to_owned());
5797        settle(
5798            &mut wiped_out,
5799            Verdict {
5800                status: RunStatus::Failed,
5801                left_pr: false,
5802                parked: false,
5803                quota_hit: true,
5804                no_viable_candidates: true,
5805            },
5806            "no candidate produced a change; nothing to judge",
5807            2,
5808        );
5809        assert_eq!(wiped_out.attempts, 0, "a total quota wipeout is refunded");
5810        assert!(
5811            wiped_out.status.runnable(),
5812            "a machine problem must leave the task in line"
5813        );
5814
5815        // This is the exemption that must stay narrow: a candidate that did
5816        // produce a change, and then failed for some other reason, still
5817        // spends the attempt even though a seat elsewhere hit its quota.
5818        // Otherwise every ordinary failure that happens to share a run with
5819        // an unrelated rate limit would be refunded for free.
5820        let mut partial_progress = task();
5821        partial_progress.start("20260907-025500-c3d4".to_owned());
5822        settle(
5823            &mut partial_progress,
5824            Verdict {
5825                status: RunStatus::Failed,
5826                left_pr: false,
5827                parked: false,
5828                quota_hit: true,
5829                no_viable_candidates: false,
5830            },
5831            "gate failed on the winning candidate",
5832            2,
5833        );
5834        assert_eq!(
5835            partial_progress.attempts, 1,
5836            "a candidate that actually produced a change spends the attempt \
5837             even though some other seat hit its quota"
5838        );
5839        assert!(partial_progress.status.runnable());
5840    }
5841
5842    #[test]
5843    fn reclaim_refunds_a_recovered_quota_wipeout_the_same_way_a_live_settle_does() {
5844        // `reclaim` builds its own `Verdict` from a `RunState` it loads off
5845        // disk, and that construction must reach the same conclusion as the
5846        // one `attempt` builds from a live run, or a crash at exactly the
5847        // wrong moment gives a recovered task a different policy than one a
5848        // daemon finished settling itself.
5849        let mut t = task();
5850        t.start("20260907-025000-a1b2".to_owned());
5851        let mut state = run_state(RunStatus::Failed);
5852        state.quota.push(QuotaLoss {
5853            seat: "cand-a".to_owned(),
5854            node: "implement".to_owned(),
5855            at: Timestamp::now(),
5856            reset: None,
5857        });
5858        assert!(
5859            state.viable().is_empty(),
5860            "no candidate was added, so nothing is viable"
5861        );
5862        reclaim(&mut t, Some(state), 2, "en");
5863        assert_eq!(t.attempts, 0, "a recovered quota wipeout is refunded");
5864        assert!(t.status.runnable());
5865    }
5866
5867    #[test]
5868    fn a_held_task_is_never_offered_to_the_loop() {
5869        let dir = tempfile::tempdir().unwrap();
5870        let queue = Queue::at(dir.path().to_path_buf());
5871        for (n, priority) in [(1, 0), (2, 5), (3, 5)] {
5872            let mut t = task();
5873            t.id = format!("2026090{n}-000000-000{n}");
5874            t.priority = priority;
5875            queue.put(&mut t).unwrap();
5876        }
5877        let mut held = task();
5878        held.id = "20260909-000000-9999".to_owned();
5879        held.priority = 99;
5880        held.hold_machine(None);
5881        queue.put(&mut held).unwrap();
5882
5883        let order: Vec<String> = runnable(&queue).into_iter().map(|t| t.id).collect();
5884        assert_eq!(order.len(), 3);
5885        assert!(!order.contains(&held.id));
5886        assert_eq!(
5887            order.first().cloned(),
5888            queue.next_runnable().map(|t| t.id),
5889            "the loop's first candidate is exactly what the queue offers"
5890        );
5891        assert_eq!(
5892            order,
5893            vec![
5894                "20260902-000000-0002".to_owned(),
5895                "20260903-000000-0003".to_owned(),
5896                "20260901-000000-0001".to_owned(),
5897            ],
5898            "priority first, then oldest, so nothing starves"
5899        );
5900    }
5901
5902    #[test]
5903    fn sweep_removes_an_old_unparseable_lock_and_keeps_a_live_one() {
5904        let dir = tempfile::tempdir().unwrap();
5905        let queue = Queue::at(dir.path().to_path_buf());
5906        let mut old = task();
5907        old.id = "20260101-000000-old0".to_owned();
5908        queue.put(&mut old).unwrap();
5909        let mut fresh = task();
5910        fresh.id = "20260101-000000-new0".to_owned();
5911        queue.put(&mut fresh).unwrap();
5912
5913        // No parseable pid at all, so age is the only signal there is to
5914        // check - unlike a real `Queue::claim`, which always names a real,
5915        // and therefore alive, pid this test cannot fake as dead.
5916        std::fs::write(dir.path().join(format!("{}.lock", old.id)), "not a pid").unwrap();
5917        std::thread::sleep(Duration::from_millis(60));
5918        let live = queue.claim(&fresh.id).unwrap();
5919
5920        let swept = sweep_stale_claims(&queue, Duration::from_millis(50));
5921        assert_eq!(swept, vec![old.id.clone()]);
5922        assert!(
5923            queue.claim(&old.id).is_ok(),
5924            "an unparseable lock older than the threshold is swept"
5925        );
5926        assert!(
5927            queue.claim(&fresh.id).is_err(),
5928            "a live pid protects its lock regardless of age"
5929        );
5930        drop(live);
5931    }
5932
5933    #[test]
5934    fn an_old_lock_whose_pid_is_still_alive_is_never_swept_by_age_alone() {
5935        // The regression this guards: `sweep` now runs concurrently with
5936        // every attempt this daemon itself has spawned (see
5937        // `InFlightGuard`), not only between them the way a single
5938        // sequential loop once did. A run that legitimately outlives
5939        // `older_than` still has this very process's own live pid sitting in
5940        // its own lock file on every later sweep, and deciding by age alone
5941        // would delete that still-valid claim out from under the attempt
5942        // that holds it - which `reclaim_orphaned_running` would then read
5943        // as abandoned and hand to a second, competing attempt.
5944        let dir = tempfile::tempdir().unwrap();
5945        let queue = Queue::at(dir.path().to_path_buf());
5946        let mut t = task();
5947        t.id = "20260101-000000-live".to_owned();
5948        queue.put(&mut t).unwrap();
5949
5950        let claim = queue.claim(&t.id).unwrap();
5951        std::thread::sleep(Duration::from_millis(60));
5952
5953        let swept = sweep_stale_claims(&queue, Duration::from_millis(50));
5954        assert!(
5955            swept.is_empty(),
5956            "a lock naming a live pid must never be swept by age, no matter how old: {swept:?}"
5957        );
5958        assert!(
5959            queue.claim(&t.id).is_err(),
5960            "the lock still protects its task"
5961        );
5962        drop(claim);
5963    }
5964
5965    /// このテストプロセスにはなり得ない決定的なフィクスチャ PID。
5966    /// OS 上の状態は意図的に無関係で、各利用箇所が方針問い合わせを注入する。
5967    fn injected_dead_pid() -> u32 {
5968        std::process::id().checked_add(1).unwrap_or(1)
5969    }
5970
5971    #[test]
5972    fn a_lock_naming_a_dead_pid_is_swept_at_once_regardless_of_age() {
5973        let dir = tempfile::tempdir().unwrap();
5974        let queue = Queue::at(dir.path().to_path_buf());
5975        let mut t = task();
5976        t.id = "20260101-000000-dead".to_owned();
5977        queue.put(&mut t).unwrap();
5978        let dead_pid = injected_dead_pid();
5979
5980        // Written directly rather than through `Queue::claim`, which would
5981        // stamp this test process's own very much alive pid and defeat the
5982        // point: this is what a `.lock` left by a `SIGKILL`ed daemon looks
5983        // like moments after it died, not six hours later.
5984        std::fs::write(
5985            dir.path().join(format!("{}.lock", t.id)),
5986            dead_pid.to_string(),
5987        )
5988        .unwrap();
5989
5990        let swept = sweep_stale_claims_with(&queue, Duration::from_secs(6 * 60 * 60), |pid| {
5991            pid != dead_pid
5992        });
5993        assert_eq!(
5994            swept,
5995            vec![t.id.clone()],
5996            "a dead owner is reclaimed immediately, not after STALE_CLAIM"
5997        );
5998        assert!(queue.claim(&t.id).is_ok(), "the task is claimable again");
5999    }
6000
6001    #[test]
6002    fn sweeping_on_every_poll_catches_a_lock_that_appears_after_the_first_sweep() {
6003        let dir = tempfile::tempdir().unwrap();
6004        let queue = Queue::at(dir.path().to_path_buf());
6005        let mut t = task();
6006        t.id = "20260101-000000-late".to_owned();
6007        queue.put(&mut t).unwrap();
6008        let dead_pid = injected_dead_pid();
6009
6010        // Tick one, standing in for the sweep `poll` already runs at
6011        // startup: nothing to find yet.
6012        assert!(
6013            sweep_stale_claims(&queue, Duration::from_secs(6 * 60 * 60)).is_empty(),
6014            "nothing has claimed the task yet"
6015        );
6016
6017        // A second daemon claims the task and dies before it ever writes
6018        // `running`, well after this loop's own startup sweep already ran.
6019        std::fs::write(
6020            dir.path().join(format!("{}.lock", t.id)),
6021            dead_pid.to_string(),
6022        )
6023        .unwrap();
6024
6025        // Tick two, standing in for a poll long into this daemon's uptime:
6026        // the same function, called again, notices what only just appeared -
6027        // proving the sweep is not a one-shot startup check.
6028        let swept = sweep_stale_claims_with(&queue, Duration::from_secs(6 * 60 * 60), |pid| {
6029            pid != dead_pid
6030        });
6031        assert_eq!(swept, vec![t.id.clone()]);
6032    }
6033
6034    #[test]
6035    fn a_running_task_behind_a_dead_daemons_lock_recovers_once_swept_and_keeps_its_history() {
6036        // `reclaim_orphaned_running` looks up the task's last run, which
6037        // touches `run::home()`; the first call anywhere in this binary wins,
6038        // so this is a no-op if another test already pinned one, and either
6039        // way the run id below is never written under it.
6040        crate::run::pin_test_home();
6041        let dir = tempfile::tempdir().unwrap();
6042        let queue = Queue::at(dir.path().to_path_buf());
6043        let mut t = task();
6044        t.id = "20260101-000000-crsh".to_owned();
6045        t.status = TaskStatus::Running;
6046        t.attempts = 1;
6047        // No `run.json` behind this id: standing in for a run this test does
6048        // not need to make readable, since the point is the lock, not the
6049        // recovery table `reclaim` already has its own tests for.
6050        t.runs.push("20260904-000000-4043".to_owned());
6051        queue.put(&mut t).unwrap();
6052        let dead_pid = injected_dead_pid();
6053
6054        // The crashed daemon's own claim, naming a pid nothing on the
6055        // machine holds anymore.
6056        std::fs::write(
6057            dir.path().join(format!("{}.lock", t.id)),
6058            dead_pid.to_string(),
6059        )
6060        .unwrap();
6061
6062        // Before the lock is swept the task looks claimed, and
6063        // `reclaim_orphaned_running` must leave it alone - this is exactly
6064        // the bug: a `running` task stranded behind a dead daemon's lock,
6065        // invisible to the claim-as-proof check because the lock outlived
6066        // the process that wrote it.
6067        assert!(reclaim_orphaned_running(&queue, 2).is_empty());
6068        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Running);
6069
6070        let swept = sweep_stale_claims_with(&queue, Duration::from_secs(6 * 60 * 60), |pid| {
6071            pid != dead_pid
6072        });
6073        assert_eq!(swept, vec![t.id.clone()]);
6074
6075        let reclaimed = reclaim_orphaned_running(&queue, 2);
6076        assert_eq!(reclaimed, vec![t.id.clone()]);
6077        let after = queue.get(&t.id).unwrap();
6078        assert_eq!(
6079            after.status,
6080            TaskStatus::Held,
6081            "no run.json to recover from, so a human is asked"
6082        );
6083        assert_eq!(
6084            after.runs,
6085            vec!["20260904-000000-4043".to_owned()],
6086            "the crashed run's id is kept as evidence, not discarded"
6087        );
6088    }
6089
6090    #[test]
6091    fn a_lock_is_kept_when_the_process_query_is_unavailable() {
6092        let dir = tempfile::tempdir().unwrap();
6093        let queue = Queue::at(dir.path().to_path_buf());
6094        let mut t = task();
6095        t.id = "20260101-000000-unknown".to_owned();
6096        queue.put(&mut t).unwrap();
6097        let dead_pid = injected_dead_pid();
6098        std::fs::write(
6099            dir.path().join(format!("{}.lock", t.id)),
6100            dead_pid.to_string(),
6101        )
6102        .unwrap();
6103
6104        let swept = sweep_stale_claims_with(&queue, Duration::ZERO, |_| true);
6105        assert!(swept.is_empty(), "an unknown pid must keep its lock");
6106        assert!(queue.claim(&t.id).is_err(), "the lock remains protective");
6107    }
6108
6109    fn run_state_in(status: RunStatus, language: &str) -> RunState {
6110        let mut s = run_state(status);
6111        s.config.graph.language = language.to_owned();
6112        s
6113    }
6114
6115    fn unstarted_verdict(status: RunStatus) -> Verdict {
6116        Verdict {
6117            status,
6118            left_pr: false,
6119            quota_hit: false,
6120            parked: false,
6121            no_viable_candidates: false,
6122        }
6123    }
6124
6125    #[test]
6126    fn an_already_in_base_run_finishes_the_task_without_spending_an_attempt() {
6127        let mut t = task();
6128        t.attempts = 1;
6129        settle_in(
6130            &mut t,
6131            unstarted_verdict(RunStatus::AlreadyInBase),
6132            "already in main",
6133            1,
6134            phrases("en"),
6135        );
6136        assert_eq!(t.status, TaskStatus::Done);
6137        assert_eq!(t.attempts, 0);
6138    }
6139
6140    #[test]
6141    fn settle_renders_the_non_terminal_reason_in_the_configured_language() {
6142        let reason = |language: &str| {
6143            let mut t = task();
6144            settle_in(
6145                &mut t,
6146                unstarted_verdict(RunStatus::Judging),
6147                "boom",
6148                1,
6149                phrases(language),
6150            );
6151            t.last_error.or(t.hold_reason).unwrap_or_default()
6152        };
6153        assert!(
6154            reason("en").starts_with("the graph stopped at `"),
6155            "{}",
6156            reason("en")
6157        );
6158        assert!(reason("ja").starts_with("グラフが終端状態に達しないまま"));
6159        assert!(reason("日本語").contains("boom"));
6160        assert_eq!(reason("fr"), reason("en"));
6161    }
6162
6163    #[test]
6164    fn describe_follows_the_run_language_and_keeps_the_run_id() {
6165        let en = describe(&run_state_in(RunStatus::Stalled, "en"));
6166        assert!(en.starts_with("the judging panel lost its quorum"), "{en}");
6167        let ja = describe(&run_state_in(RunStatus::Stalled, "ja"));
6168        assert!(ja.starts_with("審査パネルが定足数を失いました"), "{ja}");
6169        assert!(ja.contains("[run "), "{ja}");
6170        let ended = describe(&run_state_in(RunStatus::Failed, "jp"));
6171        assert!(ended.starts_with("run 終了: "), "{ended}");
6172        let mut de = run_state_in(RunStatus::Failed, "de");
6173        let mut en = run_state_in(RunStatus::Failed, "en");
6174        de.id = "same".to_owned();
6175        en.id = "same".to_owned();
6176        assert_eq!(describe(&de), describe(&en));
6177    }
6178
6179    #[test]
6180    fn handover_refusals_follow_the_language_and_keep_the_detail_apart() {
6181        use crate::handover::Refused;
6182        let cases = [
6183            Refused::Foreign {
6184                branch: "b".into(),
6185                path: "/w/x".into(),
6186                why: "made by hand".into(),
6187            },
6188            Refused::Unsafe {
6189                branch: "b".into(),
6190                path: "/w/x".into(),
6191                why: "its worktree has uncommitted changes (a.rs)".into(),
6192            },
6193            Refused::ReleaseFailed {
6194                branch: "b".into(),
6195                path: "/w/x".into(),
6196                run: "ab12".into(),
6197            },
6198        ];
6199        for r in &cases {
6200            let en = (phrases("en").handover_refused)(r);
6201            assert_eq!(en, r.to_string());
6202            let ja = (phrases("ja").handover_refused)(r);
6203            assert!(
6204                !ja.contains("is checked out") && !ja.contains("try again"),
6205                "{ja}"
6206            );
6207            assert!(ja.contains("`b`") && ja.contains("/w/x"), "{ja}");
6208            if let Refused::Foreign { why, .. } | Refused::Unsafe { why, .. } = r {
6209                assert!(ja.contains(&format!("(詳細: {why})")), "{ja}");
6210            }
6211        }
6212        assert!(phrases("en").handover_hint.contains("release the task"));
6213        assert!(phrases("ja").handover_hint.contains("解放"));
6214    }
6215
6216    #[test]
6217    fn describe_translates_the_unanswered_reviewer_stop_only_in_ja() {
6218        let build = |lang: &str| {
6219            let mut s = run_state_in(RunStatus::Blocked, lang);
6220            s.id = "same".to_owned();
6221            s.config.graph.review_rounds = 3;
6222            let mut r = review_round(3);
6223            r.expected = 3;
6224            r.answered = 1;
6225            s.reviews.push(r);
6226            s.event(
6227                "review",
6228                "2 reviewer seat(s) never answered after 3 rounds; refusing to call it clean",
6229            );
6230            s
6231        };
6232        let en = describe(&build("en"));
6233        assert!(en.contains("(review: 2 reviewer seat(s) never answered after 3 rounds; refusing to call it clean)"), "{en}");
6234        let ja = describe(&build("ja"));
6235        assert!(ja.contains("2 席のレビュアーが 3 ラウンド"), "{ja}");
6236        assert!(!ja.contains("never answered"), "{ja}");
6237        assert!(ja.contains("[run "), "{ja}");
6238
6239        // An incomplete panel whose verification failed is a different stop:
6240        // the real event must survive in ja too.
6241        let mut failed = build("ja");
6242        failed.reviews[0].e2e.push(crate::run::CommandOutcome {
6243            command: "cargo test".to_owned(),
6244            code: Some(1),
6245            output_tail: String::new(),
6246            duration_ms: 0,
6247            resource_blocked: false,
6248        });
6249        failed.event("review", "stopped; e2e failed: cargo test");
6250        let ja = describe(&failed);
6251        assert!(ja.contains("stopped; e2e failed: cargo test"), "{ja}");
6252        assert!(!ja.contains("席のレビュアー"), "{ja}");
6253    }
6254
6255    #[test]
6256    fn refusals_and_recovery_prose_follow_the_language() {
6257        let t = held_task_with("r1");
6258        let q = action_question(
6259            "r1",
6260            ask::ChoiceAction::Resume {
6261                run: "r1".to_owned(),
6262            },
6263        );
6264        let refuse = |p: &Phrases| match decide_action(&t, &q, p, |_: &str| bail!("gone")) {
6265            ActionDecision::Refuse(s) => s,
6266            other => panic!("{other:?}"),
6267        };
6268        assert!(refuse(phrases("en")).contains("could not be read: gone"));
6269        assert!(refuse(phrases("ja")).contains("読み込めませんでした: gone"));
6270        assert_eq!(refuse(phrases("fr")), refuse(phrases("en")));
6271
6272        let mut held = task();
6273        reclaim(&mut held, None, 2, "ja");
6274        assert!(held.hold_reason.unwrap().contains("保留にしました"));
6275        let mut held = task();
6276        reclaim(&mut held, None, 2, "xx");
6277        assert!(held.hold_reason.unwrap().contains("held for a human"));
6278    }
6279
6280    fn run_state(status: RunStatus) -> RunState {
6281        let mut state = RunState::new(
6282            PathBuf::from("/repo"),
6283            "main".to_owned(),
6284            "abc1234def".to_owned(),
6285            "add retries".to_owned(),
6286            Config::default(),
6287        );
6288        state.status = status;
6289        state
6290    }
6291
6292    fn candidate(label: char, summary: &str, empty: bool, failed: Option<&str>) -> Candidate {
6293        Candidate {
6294            index: 0,
6295            label,
6296            agent: "claude".to_owned(),
6297            branch: format!("magi/x/{label}"),
6298            worktree: PathBuf::from("/repo"),
6299            summary: summary.to_owned(),
6300            stat: String::new(),
6301            files: 0,
6302            commits: usize::from(!empty),
6303            empty,
6304            failed: failed.map(str::to_owned),
6305            verified_noop: None,
6306            duration_ms: 0,
6307            folded: false,
6308        }
6309    }
6310
6311    #[test]
6312    fn diagnostic_names_the_failing_gate_checks_and_their_output() {
6313        let mut state = run_state(RunStatus::Blocked);
6314        state.gate = vec![
6315            CommandOutcome {
6316                command: "cargo make check".to_owned(),
6317                code: Some(0),
6318                output_tail: "ok".to_owned(),
6319                duration_ms: 0,
6320                resource_blocked: false,
6321            },
6322            CommandOutcome {
6323                command: "cargo test".to_owned(),
6324                code: Some(101),
6325                output_tail: "thread 'x' panicked: assertion failed".to_owned(),
6326                duration_ms: 0,
6327                resource_blocked: false,
6328            },
6329        ];
6330        let d = diagnostic(&state).expect("a failing gate must produce a diagnostic");
6331        assert!(d.contains("cargo test"), "{d}");
6332        assert!(
6333            !d.contains("cargo make check"),
6334            "a passing check is not a diagnostic: {d}"
6335        );
6336        assert!(d.contains("assertion failed"), "{d}");
6337    }
6338
6339    #[test]
6340    fn diagnostic_names_the_checks_the_fixer_gave_up_in_front_of() {
6341        let mut state = run_state(RunStatus::Blocked);
6342        state.event(
6343            "land",
6344            "stopped: the fixer produced no commit while 2 check(s) were failing \
6345             (build, lint); stopping instead of looping on an unchanged tree",
6346        );
6347        let d = diagnostic(&state).expect("a stalled land loop must produce a diagnostic");
6348        assert!(d.contains("build"), "{d}");
6349        assert!(d.contains("lint"), "{d}");
6350        assert!(d.contains("fixer produced no commit"), "{d}");
6351    }
6352
6353    #[test]
6354    fn describe_never_leaves_a_verified_noop_reading_as_a_bare_status_code() {
6355        // `describe`'s output becomes `Task::last_error` verbatim, and the
6356        // phone renders that in the same alarm-styled box an ordinary
6357        // failure gets. A bare `verified_noop` there would read exactly like
6358        // the failure this status exists to be told apart from.
6359        let state = run_state(RunStatus::VerifiedNoop);
6360        let d = describe(&state);
6361        assert!(
6362            d.contains("agent-verified no-op"),
6363            "expected the display label, not the wire spelling: {d}"
6364        );
6365        assert!(!d.contains("verified_noop"), "{d}");
6366    }
6367
6368    #[test]
6369    fn diagnostic_carries_a_candidates_own_final_word_when_none_was_viable() {
6370        // The whole point of the feature: a run held as "no candidate produced
6371        // a change" can mean the implementer actually finished the task and
6372        // only left a clean local tree behind - see AGENTS.md on this exact
6373        // failure mode. The diagnostic has to carry what the agent actually
6374        // said, not just the fact that nothing was there to judge.
6375        let mut state = run_state(RunStatus::Failed);
6376        state.candidates = vec![candidate(
6377            'A',
6378            "opened pull request #42, merged it, tagged v1.2.3 and published the release",
6379            true,
6380            None,
6381        )];
6382        let d = diagnostic(&state).expect("an empty candidate with a summary must be surfaced");
6383        assert!(d.contains("candidate A"), "{d}");
6384        assert!(d.contains("tagged v1.2.3"), "{d}");
6385    }
6386
6387    #[test]
6388    fn diagnostic_falls_back_to_a_candidates_failure_reason_when_it_has_no_summary() {
6389        let mut state = run_state(RunStatus::Failed);
6390        state.candidates = vec![candidate('A', "", true, Some("agent timed out"))];
6391        let d = diagnostic(&state).expect("a candidate's own failure reason must be surfaced");
6392        assert!(d.contains("candidate A"), "{d}");
6393        assert!(d.contains("agent timed out"), "{d}");
6394    }
6395
6396    #[test]
6397    fn diagnostic_is_none_when_nothing_recognisable_explains_the_hold() {
6398        // A viable candidate existed, the gate never ran, and nothing land
6399        // said matches - `Task::last_error` is left to explain this one alone.
6400        let mut state = run_state(RunStatus::Failed);
6401        state.candidates = vec![candidate('A', "did the work", false, None)];
6402        assert!(diagnostic(&state).is_none());
6403    }
6404
6405    #[test]
6406    fn diagnostic_is_bounded_however_much_a_run_printed() {
6407        let mut state = run_state(RunStatus::Blocked);
6408        state.gate = vec![
6409            CommandOutcome {
6410                command: "cargo test".to_owned(),
6411                code: Some(101),
6412                output_tail: "x".repeat(50_000),
6413                duration_ms: 0,
6414                resource_blocked: false,
6415            },
6416            CommandOutcome {
6417                command: "cargo clippy".to_owned(),
6418                code: Some(1),
6419                output_tail: "y".repeat(50_000),
6420                duration_ms: 0,
6421                resource_blocked: false,
6422            },
6423        ];
6424        state.candidates = vec![
6425            candidate('A', &"z".repeat(50_000), true, None),
6426            candidate('B', &"w".repeat(50_000), true, None),
6427        ];
6428        let d = diagnostic(&state).expect("plenty here to diagnose");
6429        assert!(
6430            d.len() <= DIAGNOSTIC_MAX,
6431            "diagnostic grew to {} bytes, unbounded",
6432            d.len()
6433        );
6434    }
6435
6436    #[test]
6437    fn settle_and_diagnose_attaches_a_diagnostic_only_once_the_task_is_held() {
6438        let mut state = run_state(RunStatus::Blocked);
6439        state.gate = vec![CommandOutcome {
6440            command: "cargo test".to_owned(),
6441            code: Some(101),
6442            output_tail: "assertion failed".to_owned(),
6443            duration_ms: 0,
6444            resource_blocked: false,
6445        }];
6446        let verdict = Verdict {
6447            status: RunStatus::Blocked,
6448            left_pr: false,
6449            quota_hit: false,
6450            parked: false,
6451            no_viable_candidates: false,
6452        };
6453
6454        // Attempt one of two still has a retry coming: no diagnostic yet, the
6455        // task is going to run again and this run's evidence would go stale.
6456        let mut t = task();
6457        t.start("run-1".to_owned());
6458        settle_and_diagnose(&mut t, verdict, "gate failed", 2, &state);
6459        assert_eq!(t.status, TaskStatus::Failed);
6460        assert!(t.diagnostic.is_none());
6461
6462        // Attempt two exhausts the budget: now it is held, and the
6463        // diagnostic is what `magi task show` has to say more than one line.
6464        t.start("run-2".to_owned());
6465        settle_and_diagnose(&mut t, verdict, "gate failed", 2, &state);
6466        assert_eq!(t.status, TaskStatus::Held);
6467        let d = t.diagnostic.expect("a held task must carry its diagnostic");
6468        assert!(d.contains("cargo test"), "{d}");
6469    }
6470
6471    #[test]
6472    fn a_held_task_names_the_open_question_it_is_waiting_on() {
6473        // Task 20260928-191407-05fd's own bug: a verified no-op that filed a
6474        // `magi ask` question settled Held with `hold_reason: null`, so the
6475        // notification said nothing actionable. `note_open_question` should
6476        // append the question id to whatever `handed_off` already wrote.
6477        crate::run::pin_test_home();
6478        let home = crate::run::home();
6479        let state = run_state(RunStatus::VerifiedNoop);
6480        let mut q = ask::Question::new(
6481            state.id.clone(),
6482            "implement".to_owned(),
6483            "impl-A".to_owned(),
6484            "is this really a no-op?".to_owned(),
6485            String::new(),
6486            Vec::new(),
6487        );
6488        Questions::at(home.join("questions")).put(&mut q).unwrap();
6489
6490        let verdict = Verdict {
6491            status: RunStatus::VerifiedNoop,
6492            left_pr: false,
6493            quota_hit: false,
6494            parked: false,
6495            no_viable_candidates: false,
6496        };
6497        let mut t = task();
6498        t.start(state.id.clone());
6499        settle_and_diagnose(&mut t, verdict, "run ended agent-verified no-op", 5, &state);
6500
6501        assert_eq!(t.status, TaskStatus::Held);
6502        let reason = t.hold_reason.expect("a held task must record why");
6503        assert!(
6504            reason.starts_with("run ended agent-verified no-op"),
6505            "the original settle reason must survive unchanged: {reason}"
6506        );
6507        assert!(
6508            reason.contains(q.short()),
6509            "the open question's id must be named so the notice is actionable: {reason}"
6510        );
6511    }
6512
6513    #[test]
6514    fn a_held_task_with_no_open_question_keeps_its_plain_reason() {
6515        crate::run::pin_test_home();
6516        let state = run_state(RunStatus::VerifiedNoop);
6517
6518        let verdict = Verdict {
6519            status: RunStatus::VerifiedNoop,
6520            left_pr: false,
6521            quota_hit: false,
6522            parked: false,
6523            no_viable_candidates: false,
6524        };
6525        let mut t = task();
6526        t.start(state.id.clone());
6527        settle_and_diagnose(&mut t, verdict, "run ended agent-verified no-op", 5, &state);
6528
6529        assert_eq!(t.status, TaskStatus::Held);
6530        assert_eq!(
6531            t.hold_reason.as_deref(),
6532            Some("run ended agent-verified no-op"),
6533            "nothing to append when the question was already answered or never asked"
6534        );
6535    }
6536
6537    #[test]
6538    fn supersede_prior_runs_rewrites_an_earlier_blocked_attempt_once_a_later_one_lands() {
6539        crate::run::pin_test_home();
6540        let mut first = run_state(RunStatus::Blocked);
6541        first.id = "20260101-000000-sup1".to_owned();
6542        first.save().unwrap();
6543        let mut second = run_state(RunStatus::Merged);
6544        second.id = "20260101-000000-sup2".to_owned();
6545        second.save().unwrap();
6546
6547        let mut t = task();
6548        t.runs = vec![first.id.clone(), second.id.clone()];
6549        t.status = TaskStatus::Done;
6550
6551        supersede_prior_runs(&t, &crate::run::home());
6552
6553        assert_eq!(
6554            RunState::load(&first.id).unwrap().status,
6555            RunStatus::Superseded,
6556            "the first attempt's Blocked no longer needs anyone's attention"
6557        );
6558        assert_eq!(
6559            RunState::load(&second.id).unwrap().status,
6560            RunStatus::Merged,
6561            "the run that actually succeeded is left exactly as it was"
6562        );
6563    }
6564
6565    #[test]
6566    fn supersede_prior_runs_leaves_a_manually_resumed_attempt_alone() {
6567        // `is_working_on` alone only proves a *daemon* claim; a `magi run
6568        // --resume` invoked by hand outside the daemon never touches
6569        // `daemon.json` at all, so it would look identical to a genuinely
6570        // idle run without also consulting `RunState::liveness`, which reads
6571        // this run's own recorded `driver_pid` instead.
6572        crate::run::pin_test_home();
6573        let mut first = run_state(RunStatus::Blocked);
6574        first.id = "20260101-000000-sup9".to_owned();
6575        // This test process's own pid: guaranteed alive without needing a
6576        // real second process or daemon.
6577        first.driver_pid = Some(std::process::id());
6578        first.driver_started_at = Some(
6579            crate::proc::process_started_at(std::process::id())
6580                .expect("this test process's own start time must be queryable"),
6581        );
6582        first.save().unwrap();
6583        let mut second = run_state(RunStatus::Merged);
6584        second.id = "20260101-000000-supa".to_owned();
6585        second.save().unwrap();
6586
6587        let mut t = task();
6588        t.runs = vec![first.id.clone(), second.id.clone()];
6589        t.status = TaskStatus::Done;
6590
6591        supersede_prior_runs(&t, &crate::run::home());
6592
6593        assert_eq!(
6594            RunState::load(&first.id).unwrap().status,
6595            RunStatus::Blocked,
6596            "a live driver_pid means something is still actually working this run, \
6597             even though no daemon claims it - rewriting under it would just be \
6598             undone the next time that process saves"
6599        );
6600    }
6601
6602    #[test]
6603    fn resweep_catches_up_a_run_left_live_once_its_manual_process_is_no_longer_driving_it() {
6604        // The first resweep must skip a still-live attempt exactly like
6605        // `supersede_prior_runs` itself does; a later resweep, once that
6606        // process is no longer actually driving it, is what closes the gap
6607        // `supersede_prior_runs` alone leaves - a task only ever reaches
6608        // `Done` once, so nothing else would ever look at this run again.
6609        let dir = tempfile::tempdir().unwrap();
6610        let home = dir.path().to_path_buf();
6611        let queue = Queue::at(dir.path().join("queue"));
6612
6613        let mut first = run_state(RunStatus::Blocked);
6614        first.id = "20260101-000000-supd".to_owned();
6615        first.driver_pid = Some(std::process::id());
6616        first.driver_started_at = Some(
6617            crate::proc::process_started_at(std::process::id())
6618                .expect("this test process's own start time must be queryable"),
6619        );
6620        first.save_under(&home).unwrap();
6621        let mut second = run_state(RunStatus::Merged);
6622        second.id = "20260101-000000-supe".to_owned();
6623        second.save_under(&home).unwrap();
6624
6625        let mut t = task();
6626        t.runs = vec![first.id.clone(), second.id.clone()];
6627        t.status = TaskStatus::Done;
6628        queue.put(&mut t).unwrap();
6629
6630        resweep_superseded_attempts(&queue, &home);
6631        assert_eq!(
6632            RunState::load_under(&first.id, &home).unwrap().status,
6633            RunStatus::Blocked,
6634            "still live on the first pass, so still untouched"
6635        );
6636
6637        // Stand in for the manual process no longer being the one driving
6638        // this run: same pid (this test process really is still alive), but
6639        // an identity marker that no longer matches it - see
6640        // `RunState::liveness`'s own doc for why a mismatched
6641        // `driver_started_at` reads as `Dead`, not merely `Unknown`.
6642        let mut stale = RunState::load_under(&first.id, &home).unwrap();
6643        stale.driver_started_at = Some("1".to_owned());
6644        stale.save_under(&home).unwrap();
6645
6646        resweep_superseded_attempts(&queue, &home);
6647        assert_eq!(
6648            RunState::load_under(&first.id, &home).unwrap().status,
6649            RunStatus::Superseded,
6650            "the second pass catches up what the first one correctly skipped"
6651        );
6652    }
6653
6654    #[test]
6655    fn supersede_prior_runs_leaves_concurrent_blocked_attempts_alone_while_the_task_is_not_done() {
6656        crate::run::pin_test_home();
6657        let mut first = run_state(RunStatus::Blocked);
6658        first.id = "20260101-000000-sup3".to_owned();
6659        first.save().unwrap();
6660        let mut second = run_state(RunStatus::Blocked);
6661        second.id = "20260101-000000-sup4".to_owned();
6662        second.save().unwrap();
6663
6664        let mut t = task();
6665        t.runs = vec![first.id.clone(), second.id.clone()];
6666        // Still retrying: nothing about this task's story is settled yet, so
6667        // neither Blocked run may be relabelled, even though a later attempt
6668        // already exists.
6669        t.status = TaskStatus::Failed;
6670
6671        supersede_prior_runs(&t, &crate::run::home());
6672
6673        assert_eq!(
6674            RunState::load(&first.id).unwrap().status,
6675            RunStatus::Blocked
6676        );
6677        assert_eq!(
6678            RunState::load(&second.id).unwrap().status,
6679            RunStatus::Blocked
6680        );
6681    }
6682
6683    #[test]
6684    fn supersede_prior_runs_does_nothing_when_the_task_was_closed_by_hand() {
6685        // `magi task done` (or the API's equivalent) can close a task with no
6686        // run of its own ever having succeeded - there is nothing here that
6687        // counts as "the attempt that finished it", so nothing is rewritten.
6688        crate::run::pin_test_home();
6689        let mut first = run_state(RunStatus::Blocked);
6690        first.id = "20260101-000000-sup5".to_owned();
6691        first.save().unwrap();
6692
6693        let mut t = task();
6694        t.runs = vec![first.id.clone()];
6695        t.status = TaskStatus::Done;
6696
6697        supersede_prior_runs(&t, &crate::run::home());
6698
6699        assert_eq!(
6700            RunState::load(&first.id).unwrap().status,
6701            RunStatus::Blocked,
6702            "a single-attempt task has no earlier run to supersede"
6703        );
6704    }
6705
6706    #[test]
6707    fn supersede_prior_runs_does_nothing_when_the_last_recorded_attempt_never_landed() {
6708        // `magi task done` (or the web/conductor equivalents) can close a
6709        // task in any status, including one whose *last* recorded attempt is
6710        // itself still `Blocked`/`Failed` - a manual merge magi's own loop
6711        // never watched, say. `runs.last()` being `Done`-adjacent is not
6712        // proof it actually succeeded, so nothing earlier may be relabelled
6713        // on its say-so alone.
6714        crate::run::pin_test_home();
6715        let mut first = run_state(RunStatus::Blocked);
6716        first.id = "20260101-000000-supb".to_owned();
6717        first.save().unwrap();
6718        let mut second = run_state(RunStatus::Failed);
6719        second.id = "20260101-000000-supc".to_owned();
6720        second.save().unwrap();
6721
6722        let mut t = task();
6723        t.runs = vec![first.id.clone(), second.id.clone()];
6724        t.status = TaskStatus::Done;
6725
6726        supersede_prior_runs(&t, &crate::run::home());
6727
6728        assert_eq!(
6729            RunState::load(&first.id).unwrap().status,
6730            RunStatus::Blocked,
6731            "the task's last attempt never landed, so there is nothing here \
6732             actually superseding it"
6733        );
6734    }
6735
6736    #[test]
6737    fn supersede_prior_runs_leaves_a_failed_or_verified_noop_attempt_as_is() {
6738        // Only `Blocked`/`Stalled` mean "sitting there waiting for a human
6739        // to look" - `Failed` and `VerifiedNoop` are already their own
6740        // terminal answers and must not be relabelled into a third one.
6741        crate::run::pin_test_home();
6742        let mut failed = run_state(RunStatus::Failed);
6743        failed.id = "20260101-000000-sup6".to_owned();
6744        failed.save().unwrap();
6745        let mut noop = run_state(RunStatus::VerifiedNoop);
6746        noop.id = "20260101-000000-sup7".to_owned();
6747        noop.save().unwrap();
6748        let mut winner = run_state(RunStatus::Ready);
6749        winner.id = "20260101-000000-sup8".to_owned();
6750        winner.save().unwrap();
6751
6752        let mut t = task();
6753        t.runs = vec![failed.id.clone(), noop.id.clone(), winner.id.clone()];
6754        t.status = TaskStatus::Done;
6755
6756        supersede_prior_runs(&t, &crate::run::home());
6757
6758        assert_eq!(
6759            RunState::load(&failed.id).unwrap().status,
6760            RunStatus::Failed
6761        );
6762        assert_eq!(
6763            RunState::load(&noop.id).unwrap().status,
6764            RunStatus::VerifiedNoop
6765        );
6766    }
6767
6768    fn approval_question(run: &str) -> ask::Question {
6769        ask::Question::new(
6770            run.to_owned(),
6771            land::APPROVAL_NODE.to_owned(),
6772            "land".to_owned(),
6773            "merge?".to_owned(),
6774            String::new(),
6775            vec!["merge".to_owned(), "hold".to_owned()],
6776        )
6777    }
6778
6779    #[test]
6780    fn land_resume_state_leaves_a_fresh_open_question_waiting() {
6781        crate::run::pin_test_home();
6782        let mut state = run_state(RunStatus::Landing);
6783        state.id = "20260101-000000-fre1".to_owned();
6784        state.parked = true;
6785        state.save().unwrap();
6786        ask::Questions::open()
6787            .put(&mut approval_question(&state.id))
6788            .unwrap();
6789
6790        let mut t = task();
6791        t.runs.push(state.id.clone());
6792        assert_eq!(
6793            land_resume_state(&t),
6794            LandResume::StillWaiting,
6795            "nobody has answered and the timeout has not passed"
6796        );
6797    }
6798
6799    #[test]
6800    fn land_resume_state_waits_on_a_withheld_text_question_then_resumes() {
6801        crate::run::pin_test_home();
6802        let mut state = run_state(RunStatus::Gating);
6803        state.id = "20260101-000000-gt01".to_owned();
6804        state.parked = true;
6805        state.config.graph.answer_timeout = 60;
6806        state.github_text = Some(crate::run::GithubTextAsk {
6807            rewrite: None,
6808            fingerprint: "f".into(),
6809            title: true,
6810            body: false,
6811            categories: vec!["title-language".into()],
6812            question: None,
6813            asks: 1,
6814            resolved: false,
6815            chosen_title: None,
6816            choice: None,
6817        });
6818        state.save().unwrap();
6819        let store = ask::Questions::open();
6820        let mut q = ask::Question::new(
6821            state.id.clone(),
6822            crate::github_text::ASK_NODE.to_owned(),
6823            crate::github_text::ASK_SEAT.to_owned(),
6824            "s".into(),
6825            String::new(),
6826            vec!["use fallback".into()],
6827        );
6828        store.put(&mut q).unwrap();
6829        let mut t = task();
6830        t.runs.push(state.id.clone());
6831        assert_eq!(land_resume_state(&t), LandResume::StillWaiting);
6832        q.asked_at = Timestamp::now() - jiff::SignedDuration::from_secs(120);
6833        store.put(&mut q).unwrap();
6834        assert_eq!(land_resume_state(&t), LandResume::Ready);
6835        assert!(!store.get(&q.id).unwrap().status.open());
6836        // Once decided, a parked run is an ordinary candidate again.
6837        state.github_text.as_mut().unwrap().resolved = true;
6838        state.save().unwrap();
6839        assert_eq!(land_resume_state(&t), LandResume::NotLanding);
6840    }
6841
6842    #[test]
6843    fn land_resume_state_abandons_a_question_that_outlived_answer_timeout() {
6844        // `ask::ask_and_wait`'s own deadline used to retire a question
6845        // nobody answered; land's approval bypasses that wait (see
6846        // `land::approval_gate`), so this is now the only place
6847        // `graph.answer_timeout` is enforced for a land approval at all.
6848        crate::run::pin_test_home();
6849        let mut state = run_state(RunStatus::Landing);
6850        state.id = "20260101-000000-exp1".to_owned();
6851        state.parked = true;
6852        state.config.graph.answer_timeout = 60;
6853        state.save().unwrap();
6854
6855        let store = ask::Questions::open();
6856        let mut q = approval_question(&state.id);
6857        q.asked_at = Timestamp::now() - jiff::SignedDuration::from_secs(120);
6858        store.put(&mut q).unwrap();
6859
6860        let mut t = task();
6861        t.runs.push(state.id.clone());
6862        assert_eq!(
6863            land_resume_state(&t),
6864            LandResume::Ready,
6865            "an expired question must not be waited on forever"
6866        );
6867
6868        let after = store.get(&q.id).unwrap();
6869        assert!(
6870            !after.status.open(),
6871            "the question is abandoned, not silently ignored"
6872        );
6873        assert!(
6874            after.resolution().is_none(),
6875            "an abandoned question is not read as a decision"
6876        );
6877    }
6878
6879    #[test]
6880    fn reclaim_settles_a_running_task_against_its_last_run() {
6881        let mut t = task();
6882        t.start("20260904-000000-4043".to_owned());
6883        reclaim(&mut t, Some(run_state(RunStatus::Ready)), 2, "en");
6884        assert_eq!(
6885            t.status,
6886            TaskStatus::Done,
6887            "a run that actually finished must not stay `running` forever"
6888        );
6889    }
6890
6891    #[test]
6892    fn reclaim_maps_a_parked_run_to_parked_without_touching_last_error() {
6893        let mut t = task();
6894        t.start("20260904-000000-4043".to_owned());
6895        t.last_error = Some("earlier trouble".to_owned());
6896        let mut state = run_state(RunStatus::Judging);
6897        state.parked = true;
6898        reclaim(&mut t, Some(state), 1, "en");
6899        assert_eq!(t.status, TaskStatus::Parked);
6900        assert_eq!(t.attempts, 0, "the park is refunded, even at max_attempts");
6901        assert_eq!(t.last_error.as_deref(), Some("earlier trouble"));
6902        assert!(t.park_reason.is_some());
6903        assert_eq!(t.runs, ["20260904-000000-4043"], "the same run is kept");
6904        assert!(!t.fresh_start);
6905        assert!(t.status.runnable());
6906    }
6907
6908    #[test]
6909    fn settle_parks_a_task_without_a_failure_and_a_new_start_clears_the_reason() {
6910        let mut t = task();
6911        t.start("20260904-000000-4043".to_owned());
6912        t.last_error = Some("earlier trouble".to_owned());
6913        settle(
6914            &mut t,
6915            Verdict {
6916                status: RunStatus::Judging,
6917                left_pr: false,
6918                quota_hit: false,
6919                parked: true,
6920                no_viable_candidates: false,
6921            },
6922            "parked after `judging`",
6923            1,
6924        );
6925        assert_eq!(t.status, TaskStatus::Parked);
6926        assert_eq!(t.attempts, 0);
6927        assert_eq!(t.last_error.as_deref(), Some("earlier trouble"));
6928        assert_eq!(t.park_reason.as_deref(), Some("parked after `judging`"));
6929        assert_eq!(t.schema, crate::queue::SCHEMA);
6930        assert!(
6931            crate::notices::task_held(&t).is_none(),
6932            "a park is not news"
6933        );
6934        t.start("20260904-000000-4043".to_owned());
6935        assert_eq!(t.park_reason, None);
6936    }
6937
6938    #[test]
6939    fn reclaim_reuses_the_same_retry_policy_as_a_live_settle() {
6940        // A blocked run with attempts left goes back to `Failed`, exactly as
6941        // it would from `attempt` itself - `reclaim` must not invent a second
6942        // policy for a task a daemon merely stopped without reporting.
6943        let mut t = task();
6944        t.start("20260904-000000-4043".to_owned());
6945        reclaim(&mut t, Some(run_state(RunStatus::Blocked)), 2, "en");
6946        assert_eq!(t.status, TaskStatus::Failed);
6947        assert!(t.status.runnable());
6948    }
6949
6950    #[test]
6951    fn a_settled_parked_pass_records_its_cause_and_the_refund() {
6952        let mut t = task();
6953        t.start("20260904-000000-4043".to_owned());
6954        assert_eq!(t.attempts, 1);
6955        let mut run = run_state(RunStatus::Implementing);
6956        run.id = "20260904-000000-4043".to_owned();
6957        run.begin_pass();
6958        let cause = crate::run::ParkCause::Upgrade {
6959            from: "0.121.0".to_owned(),
6960            to: Some("0.121.1".to_owned()),
6961        };
6962        run.end_pass_parked(cause.clone());
6963        run.parked = true;
6964        let verdict = Verdict {
6965            status: run.status,
6966            left_pr: false,
6967            quota_hit: false,
6968            parked: true,
6969            no_viable_candidates: false,
6970        };
6971        settle_and_diagnose(&mut t, verdict, "parked", 2, &run);
6972        assert_eq!(t.attempts, 0);
6973        assert_eq!(t.passes.len(), 1);
6974        assert_eq!(t.passes[0].cost, crate::queue::PassCost::Refunded);
6975        assert_eq!(t.passes[0].parked, Some(cause));
6976        assert_eq!(t.passes[0].pass_index, 0);
6977
6978        // A resumed pass that then fails spends its attempt, and the earlier
6979        // pass's entry is untouched.
6980        t.start("20260904-000000-4043".to_owned());
6981        run.begin_pass();
6982        run.status = RunStatus::Failed;
6983        let verdict = Verdict {
6984            status: RunStatus::Failed,
6985            left_pr: false,
6986            quota_hit: false,
6987            parked: false,
6988            no_viable_candidates: false,
6989        };
6990        settle_and_diagnose(&mut t, verdict, "failed", 2, &run);
6991        assert_eq!(t.passes.len(), 2);
6992        assert_eq!(t.passes[0].cost, crate::queue::PassCost::Refunded);
6993        assert_eq!(t.passes[1].cost, crate::queue::PassCost::Spent);
6994        assert_eq!(t.passes[1].pass_index, 1);
6995        assert_eq!(t.attempts, 1);
6996    }
6997
6998    #[test]
6999    fn reclaim_holds_a_running_task_whose_run_cannot_be_found() {
7000        let mut t = task();
7001        t.start("20260904-000000-4043".to_owned());
7002        reclaim(&mut t, None, 2, "en");
7003        assert_eq!(t.status, TaskStatus::Held);
7004        assert!(
7005            t.last_error
7006                .as_deref()
7007                .is_some_and(|e| e.contains("running")),
7008            "the operator needs to know why this task was held"
7009        );
7010    }
7011
7012    #[test]
7013    fn orphaned_running_tasks_are_reclaimed_but_live_ones_are_left_alone() {
7014        let dir = tempfile::tempdir().unwrap();
7015        let queue = Queue::at(dir.path().to_path_buf());
7016
7017        // No run recorded, so this never has to touch `RunState::load`.
7018        let mut orphaned = task();
7019        orphaned.id = "20260904-000000-orph".to_owned();
7020        orphaned.status = TaskStatus::Running;
7021        orphaned.attempts = 1;
7022        queue.put(&mut orphaned).unwrap();
7023
7024        let mut alive = task();
7025        alive.id = "20260904-000000-live".to_owned();
7026        alive.status = TaskStatus::Running;
7027        alive.attempts = 1;
7028        queue.put(&mut alive).unwrap();
7029        let _held_by_a_live_daemon = queue.claim(&alive.id).unwrap();
7030
7031        let mut queued = task();
7032        queued.id = "20260904-000000-wait".to_owned();
7033        queue.put(&mut queued).unwrap();
7034
7035        let reclaimed = reclaim_orphaned_running(&queue, 2);
7036        assert_eq!(reclaimed, vec![orphaned.id.clone()]);
7037
7038        assert_eq!(
7039            queue.get(&orphaned.id).unwrap().status,
7040            TaskStatus::Held,
7041            "nothing was driving it and there was no run to recover"
7042        );
7043        assert_eq!(
7044            queue.get(&alive.id).unwrap().status,
7045            TaskStatus::Running,
7046            "a live claim must protect the task it belongs to"
7047        );
7048        assert_eq!(queue.get(&queued.id).unwrap().status, TaskStatus::Queued);
7049    }
7050
7051    /// Read a run.json back from an explicit `home`, the same way
7052    /// `reclaim_abandoned_runs` itself does - never through the
7053    /// process-global `RunState::load`, which this test's own `home` (an
7054    /// isolated tempdir, never pinned into the shared `OnceLock`) does not
7055    /// use at all.
7056    fn read_run_under(home: &Path, id: &str) -> RunState {
7057        let body = std::fs::read_to_string(home.join("runs").join(id).join("run.json")).unwrap();
7058        serde_json::from_str(&body).unwrap()
7059    }
7060
7061    #[test]
7062    fn reclaim_abandoned_runs_fails_a_run_whose_active_seats_are_all_provably_dead() {
7063        let dir = tempfile::tempdir().unwrap();
7064        let home = dir.path().to_path_buf();
7065        let now = Timestamp::now();
7066        let overrun_seat = || crate::run::ActiveSeat {
7067            node: "implement".to_owned(),
7068            started_at: now - jiff::SignedDuration::new(21_000, 0),
7069            timeout_secs: 3_600,
7070            attempt: 0,
7071            task: None,
7072            command: None,
7073            index: None,
7074            total: None,
7075        };
7076
7077        let mut dead = run_state(RunStatus::Implementing);
7078        dead.id = "20260101-000000-dead".to_owned();
7079        dead.active.insert("impl-A".to_owned(), overrun_seat());
7080        // A `driver_pid` the injected query below confirms gone outright —
7081        // `liveness` reads this as `Dead`, not merely "no daemon claims it".
7082        dead.driver_pid = Some(4242);
7083        dead.save_under(&home).unwrap();
7084
7085        // Same shape, but a live daemon's heartbeat names it: must be left
7086        // exactly alone, however far past its own timeout the seat sits.
7087        let mut alive = run_state(RunStatus::Implementing);
7088        alive.id = "20260101-000000-aliv".to_owned();
7089        alive.active.insert("impl-A".to_owned(), overrun_seat());
7090        alive.save_under(&home).unwrap();
7091        let mut status = Status::new();
7092        status.current = vec![Current {
7093            task: "20260101-000000-task".to_owned(),
7094            run: alive.id.clone(),
7095        }];
7096        write_status_to(&home.join("daemon.json"), &status).unwrap();
7097
7098        // The abandoned seat left an open question behind: nobody is left to
7099        // read an answer once the run is failed, and this must not wait for
7100        // some later daemon startup's own sweep to notice that.
7101        let questions = Questions::at(home.join("questions"));
7102        let mut q = ask::Question::new(
7103            dead.id.clone(),
7104            "implement".to_owned(),
7105            "impl-A".to_owned(),
7106            "Which storage backend?".to_owned(),
7107            String::new(),
7108            vec!["SQLite".to_owned(), "Redis".to_owned()],
7109        );
7110        questions.put(&mut q).unwrap();
7111
7112        let abandoned = reclaim_abandoned_runs_with(
7113            &home,
7114            now,
7115            |pid| if pid == 4242 { Some(false) } else { None },
7116            |_| panic!("a query answering Dead outright needs no identity corroboration"),
7117        );
7118        assert_eq!(abandoned, vec![dead.id.clone()]);
7119
7120        let reloaded = read_run_under(&home, &dead.id);
7121        assert_eq!(reloaded.status, RunStatus::Failed);
7122        assert!(reloaded.active.is_empty());
7123        assert!(
7124            !questions.get(&q.id).unwrap().status.open(),
7125            "the failed run's own open question must be settled in the same pass"
7126        );
7127
7128        let still_alive = read_run_under(&home, &alive.id);
7129        assert_eq!(
7130            still_alive.status,
7131            RunStatus::Implementing,
7132            "a live daemon's claim protects it"
7133        );
7134        assert!(!still_alive.active.is_empty());
7135    }
7136
7137    /// The exact shape a review round flagged as broken: `magi serve` running
7138    /// in this same `home` scans *every* run on disk, including a manual
7139    /// `magi review` / `magi run` this daemon never started and that
7140    /// therefore claims no heartbeat of its own. Before this scan asked
7141    /// `liveness` rather than just `is_working_on`, a manual run whose active
7142    /// seat merely ran a little past its own timeout — the CLI finishing up,
7143    /// its result still being collected — got wiped and failed by a daemon
7144    /// that had nothing to do with it, out from under a process that was
7145    /// still very much running.
7146    #[test]
7147    fn reclaim_abandoned_runs_leaves_a_live_manual_run_alone_even_though_no_daemon_claims_it() {
7148        let dir = tempfile::tempdir().unwrap();
7149        let home = dir.path().to_path_buf();
7150        let now = Timestamp::now();
7151
7152        let mut manual = run_state(RunStatus::Reviewing);
7153        manual.id = "20260101-000000-manl".to_owned();
7154        manual.active.insert(
7155            "review-1".to_owned(),
7156            crate::run::ActiveSeat {
7157                node: "review".to_owned(),
7158                started_at: now - jiff::SignedDuration::new(21_000, 0),
7159                timeout_secs: 3_600,
7160                attempt: 0,
7161                task: None,
7162                command: None,
7163                index: None,
7164                total: None,
7165            },
7166        );
7167        // Not claimed by any daemon (no `daemon.json` at all in this `home`),
7168        // but a real, still-running process: `liveness` must corroborate this
7169        // as `Live`, not read the missing daemon claim as death.
7170        manual.driver_pid = Some(4242);
7171        manual.driver_started_at = Some("1790000000".to_owned());
7172        manual.save_under(&home).unwrap();
7173
7174        let abandoned = reclaim_abandoned_runs_with(
7175            &home,
7176            now,
7177            |pid| if pid == 4242 { Some(true) } else { None },
7178            |pid| {
7179                if pid == 4242 {
7180                    Some("1790000000".to_owned())
7181                } else {
7182                    None
7183                }
7184            },
7185        );
7186        assert!(
7187            abandoned.is_empty(),
7188            "a manual run a real process is still driving must never be reclaimed: {abandoned:?}"
7189        );
7190
7191        let reloaded = read_run_under(&home, &manual.id);
7192        assert_eq!(reloaded.status, RunStatus::Reviewing);
7193        assert!(!reloaded.active.is_empty());
7194    }
7195
7196    #[test]
7197    fn an_already_claimed_task_is_skipped_rather_than_failed() {
7198        let dir = tempfile::tempdir().unwrap();
7199        let queue = Queue::at(dir.path().to_path_buf());
7200        let mut only = task();
7201        queue.put(&mut only).unwrap();
7202
7203        let _elsewhere = queue.claim(&only.id).unwrap();
7204        let candidates = runnable(&queue);
7205        assert_eq!(candidates.len(), 1, "the task is still runnable");
7206        assert!(
7207            queue.claim(&candidates[0].id).is_err(),
7208            "the loop cannot take a claim somebody else holds"
7209        );
7210
7211        let after = queue.get(&only.id).unwrap();
7212        assert_eq!(after.status, TaskStatus::Queued);
7213        assert_eq!(
7214            after.attempts, 0,
7215            "losing the race is not an attempt at the task"
7216        );
7217        assert_eq!(after.last_error, None);
7218    }
7219
7220    #[test]
7221    fn the_status_file_round_trips_and_its_heartbeat_advances() {
7222        let dir = tempfile::tempdir().unwrap();
7223        let path = dir.path().join("daemon.json");
7224
7225        let mut status = Status::new();
7226        status.idle = false;
7227        status.completed = 7;
7228        status.current = vec![Current {
7229            task: "20260902-000000-t111".to_owned(),
7230            run: "20260902-000001-r111".to_owned(),
7231        }];
7232        write_status_to(&path, &status).unwrap();
7233        let first: Status = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
7234        assert_eq!(first.schema, SCHEMA);
7235        assert_eq!(first.pid, std::process::id());
7236        assert!(!first.idle);
7237        assert_eq!(first.completed, 7);
7238        assert_eq!(first.current, status.current);
7239        assert!(
7240            !path.with_extension("json.tmp").exists(),
7241            "the temp file is renamed, not left behind"
7242        );
7243
7244        std::thread::sleep(Duration::from_millis(5));
7245        status.updated_at = Timestamp::now();
7246        status.polls = 3;
7247        write_status_to(&path, &status).unwrap();
7248        let second: Status =
7249            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
7250        assert!(
7251            second.updated_at > first.updated_at,
7252            "a reader can only detect staleness if the heartbeat moves"
7253        );
7254        assert_eq!(
7255            second.started_at, first.started_at,
7256            "the start time is not a heartbeat"
7257        );
7258        assert_eq!(second.polls, 3);
7259    }
7260
7261    #[test]
7262    fn reading_counts_as_running_only_while_its_heartbeat_is_fresh() {
7263        let dir = tempfile::tempdir().unwrap();
7264
7265        assert!(read_status(dir.path()).is_none(), "no file, no daemon");
7266
7267        let mut status = Status::new();
7268        status.updated_at = Timestamp::now() - jiff::SignedDuration::from_secs(60);
7269        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
7270        let stale = read_status(dir.path()).unwrap();
7271        assert!(
7272            !stale.running(Timestamp::now()),
7273            "a minute without a heartbeat is a dead daemon, not a busy one"
7274        );
7275        assert!(stale.age_secs(Timestamp::now()).is_some_and(|s| s >= 55));
7276
7277        status.updated_at = Timestamp::now();
7278        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
7279        let fresh = read_status(dir.path()).unwrap();
7280        assert!(fresh.running(Timestamp::now()));
7281    }
7282
7283    #[test]
7284    fn only_a_live_daemon_on_this_very_run_counts_as_working_on_it() {
7285        let dir = tempfile::tempdir().unwrap();
7286        let now = Timestamp::now();
7287        let mine = "20260903-080619-01c2";
7288
7289        assert!(
7290            !is_working_on(dir.path(), mine, now),
7291            "no status file means nobody is working on anything"
7292        );
7293
7294        let mut status = Status::new();
7295        status.current = vec![Current {
7296            task: "20260903-080340-0167".to_owned(),
7297            run: mine.to_owned(),
7298        }];
7299        status.updated_at = now;
7300        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
7301        assert!(is_working_on(dir.path(), mine, now));
7302        assert!(
7303            !is_working_on(dir.path(), "20260903-105039-3cbf", now),
7304            "a daemon busy with one run is not working on another"
7305        );
7306
7307        // A killed daemon stops writing heartbeats but leaves the file behind
7308        // naming the run it died in. That run must not be undeletable forever.
7309        status.updated_at = now - jiff::SignedDuration::from_secs(600);
7310        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
7311        assert!(
7312            !is_working_on(dir.path(), mine, now),
7313            "a stale heartbeat is a dead daemon, so its run is a leftover"
7314        );
7315    }
7316
7317    #[test]
7318    fn is_working_on_short_matches_by_the_worktree_bays_own_name() {
7319        let dir = tempfile::tempdir().unwrap();
7320        let now = Timestamp::now();
7321
7322        assert!(
7323            !is_working_on_short(dir.path(), "01c2", now),
7324            "no status file means nobody is working on anything"
7325        );
7326
7327        let mut status = Status::new();
7328        status.current = vec![Current {
7329            task: "20260903-080340-0167".to_owned(),
7330            run: "20260903-080619-01c2".to_owned(),
7331        }];
7332        status.updated_at = now;
7333        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
7334        assert!(
7335            is_working_on_short(dir.path(), "01c2", now),
7336            "the run's short id is the last block of its full id"
7337        );
7338        assert!(
7339            !is_working_on_short(dir.path(), "3cbf", now),
7340            "a daemon busy with one worktree bay is not working on another"
7341        );
7342    }
7343
7344    #[test]
7345    fn a_newer_status_file_still_yields_a_reading() {
7346        let dir = tempfile::tempdir().unwrap();
7347        // A field this build has never heard of must not turn the reading into
7348        // nothing at all; that is the whole reason the reader is permissive.
7349        std::fs::write(
7350            dir.path().join("daemon.json"),
7351            serde_json::json!({
7352                "schema": 2,
7353                "updated_at": Timestamp::now().to_string(),
7354                "idle": true,
7355                "surprise": { "nested": [1, 2, 3] },
7356            })
7357            .to_string(),
7358        )
7359        .unwrap();
7360
7361        let reading = read_status(dir.path()).expect("a forward-compatible read");
7362        assert!(reading.running(Timestamp::now()));
7363        assert!(reading.idle);
7364        assert!(reading.current.is_empty());
7365    }
7366
7367    #[test]
7368    fn an_older_daemons_single_object_current_still_reads_as_a_one_item_list() {
7369        // A daemon started before `current` became a list keeps writing this
7370        // shape on every heartbeat until it is restarted. A rolling upgrade
7371        // - a newer `magi web` or `magi doctor` reading an older `magi
7372        // serve`'s heartbeat - must still see the run it is on, not "no
7373        // daemon" from a type mismatch failing the whole struct.
7374        let dir = tempfile::tempdir().unwrap();
7375        std::fs::write(
7376            dir.path().join("daemon.json"),
7377            serde_json::json!({
7378                "schema": 1,
7379                "pid": 4242,
7380                "updated_at": Timestamp::now().to_string(),
7381                "idle": false,
7382                "current": {"task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb"},
7383                "completed": 3,
7384                "polls": 9,
7385            })
7386            .to_string(),
7387        )
7388        .unwrap();
7389
7390        let reading = read_status(dir.path()).expect("an older shape must still parse");
7391        assert!(reading.running(Timestamp::now()));
7392        assert_eq!(
7393            reading.current,
7394            vec![Current {
7395                task: "20260902-140501-aaaa".to_owned(),
7396                run: "20260902-140502-bbbb".to_owned(),
7397            }]
7398        );
7399    }
7400
7401    #[test]
7402    fn an_absent_or_null_current_reads_as_idle_not_a_parse_failure() {
7403        let dir = tempfile::tempdir().unwrap();
7404        std::fs::write(
7405            dir.path().join("daemon.json"),
7406            serde_json::json!({
7407                "schema": 1,
7408                "updated_at": Timestamp::now().to_string(),
7409                "idle": true,
7410                "current": null,
7411            })
7412            .to_string(),
7413        )
7414        .unwrap();
7415        let with_null = read_status(dir.path()).expect("null must still parse");
7416        assert!(with_null.current.is_empty());
7417
7418        std::fs::write(
7419            dir.path().join("daemon.json"),
7420            serde_json::json!({
7421                "schema": 1,
7422                "updated_at": Timestamp::now().to_string(),
7423                "idle": true,
7424            })
7425            .to_string(),
7426        )
7427        .unwrap();
7428        let absent = read_status(dir.path()).expect("a missing field must still parse");
7429        assert!(absent.current.is_empty());
7430    }
7431
7432    #[test]
7433    fn a_task_without_a_repository_runs_in_the_daemons_default() {
7434        let fallback = Path::new("/default");
7435        let mut blank = task();
7436        blank.repo = PathBuf::new();
7437        assert_eq!(repo_for(&blank, fallback), PathBuf::from("/default"));
7438        let mut dot = task();
7439        dot.repo = PathBuf::from(".");
7440        assert_eq!(repo_for(&dot, fallback), PathBuf::from("/default"));
7441        assert_eq!(
7442            repo_for(&task(), fallback),
7443            PathBuf::from("/repo"),
7444            "a task that names a repository keeps it"
7445        );
7446    }
7447
7448    #[test]
7449    fn task_implementers_edit_inherits_daemon_config_and_merge() {
7450        let dir = tempfile::tempdir().unwrap();
7451        let path = dir.path().join("explicit.toml");
7452        std::fs::write(&path, "[graph]\nimplementers = 3\njudges = 1\n").unwrap();
7453        let opts = Opts {
7454            config: Some(path),
7455            merge: Some("none".into()),
7456            ..Default::default()
7457        };
7458        let mut t = task();
7459        t.set_implementers(
7460            2,
7461            &toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
7462                .unwrap(),
7463        )
7464        .unwrap();
7465        let cfg = prepare_for(dir.path(), &opts, &t).unwrap();
7466        assert_eq!(cfg.graph.implementers, 2);
7467        assert_eq!(cfg.graph.judges, 1);
7468        assert_eq!(cfg.merge.mode, MergeMode::None);
7469        t.set_implementers(1, &cfg).unwrap();
7470        let mut cfg = prepare_for(dir.path(), &opts, &t).unwrap();
7471        apply_task_implementers(&mut cfg, &t);
7472        assert_eq!(cfg.graph.implementers, 1);
7473        assert_eq!(cfg.graph.judges, 1);
7474        assert_eq!(cfg.merge.mode, MergeMode::None);
7475    }
7476
7477    #[test]
7478    fn task_implementers_direct_overrides_stay_isolated_after_edits() {
7479        let dir = tempfile::tempdir().unwrap();
7480        let repo = dir.path().join("repo");
7481        std::fs::create_dir(&repo).unwrap();
7482        std::fs::write(
7483            repo.join("magi.toml"),
7484            "[graph]\njudges = 2\n[merge]\nmode = 'none'\n",
7485        )
7486        .unwrap();
7487        let daemon_config = dir.path().join("daemon.toml");
7488        std::fs::write(
7489            &daemon_config,
7490            "[graph]\njudges = 1\n[merge]\nmode = 'pr'\n",
7491        )
7492        .unwrap();
7493        let opts = Opts {
7494            config: Some(daemon_config),
7495            merge: Some("pr".into()),
7496            ..Default::default()
7497        };
7498        let q = Queue::at(dir.path().join("queue"));
7499        let validation: Config =
7500            toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
7501                .unwrap();
7502        // No provenance field on legacy direct tasks: both shapes stay isolated.
7503        for record in ["{}", r#"{"candidates":2}"#] {
7504            let overrides = serde_json::from_str(record).unwrap();
7505            let filing = crate::direct::Filing {
7506                instruction: "work".into(),
7507                title: "direct".into(),
7508                repo: repo.clone(),
7509                source: crate::queue::Source::Human,
7510                solo: false,
7511                overrides,
7512                review_of: None,
7513            };
7514            let filed =
7515                crate::direct::file(&q, dir.path(), Timestamp::now(), 1, filing, true).unwrap();
7516            let crate::direct::Filed::Standalone {
7517                mut task,
7518                claim: _claim,
7519            } = filed
7520            else {
7521                panic!("local filing")
7522            };
7523            let before = prepare_for(&repo, &opts, &task).unwrap();
7524            assert_eq!(before.graph.judges, 2, "daemon --config must not leak");
7525            assert_eq!(
7526                before.merge.mode,
7527                MergeMode::None,
7528                "daemon --merge must not leak"
7529            );
7530            for n in [2, 1, 2] {
7531                task.set_implementers(n, &validation).unwrap();
7532                q.put(&mut task).unwrap();
7533                task = q.get(&task.id).unwrap();
7534                assert!(!task.overrides.as_ref().unwrap().inherit_daemon_options);
7535                let mut cfg = prepare_for(&repo, &opts, &task).unwrap();
7536                apply_task_implementers(&mut cfg, &task);
7537                assert_eq!(cfg.graph.implementers, usize::from(n));
7538                assert_eq!(cfg.graph.judges, 2);
7539                assert_eq!(cfg.merge.mode, MergeMode::None);
7540            }
7541        }
7542    }
7543
7544    #[test]
7545    fn task_implementers_mismatch_keeps_explicit_reviews_review_only() {
7546        let cfg: Config =
7547            toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
7548                .unwrap();
7549        let mut t = task();
7550        t.review_of = Some("review-target".into());
7551        t.hold_manual(Some("retry".into()));
7552        let mut previous = RunState::new(
7553            PathBuf::from("/repo"),
7554            "main".into(),
7555            "abc1234".into(),
7556            "review".into(),
7557            cfg.clone(),
7558        );
7559        previous.config.graph.implementers = 2;
7560        for count in [1, 3] {
7561            t.set_implementers(count, &cfg).unwrap();
7562            t.release();
7563            assert_eq!(t.review_of.as_deref(), Some("review-target"));
7564            for proposed in [
7565                Starter::Resume(previous.id.clone()),
7566                Starter::Review("recovery-target".into()),
7567            ] {
7568                assert_eq!(
7569                    starter_with_implementers(&t, Some(&previous), proposed),
7570                    Starter::Review("review-target".into())
7571                );
7572            }
7573            let mut configured = cfg.clone();
7574            apply_task_implementers(&mut configured, &t);
7575            assert_eq!(configured.graph.implementers, usize::from(count));
7576            assert_eq!(previous.config.graph.implementers, 2);
7577        }
7578    }
7579
7580    #[test]
7581    fn task_implementers_dispatch_and_resume_agree() {
7582        let mut t = task();
7583        let cfg: crate::config::Config =
7584            toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
7585                .unwrap();
7586        let mut run = RunState::new(
7587            PathBuf::from("/repo"),
7588            "main".into(),
7589            "abc1234".into(),
7590            "work".into(),
7591            cfg.clone(),
7592        );
7593        run.config.graph.implementers = 2;
7594        for n in [2, 1, 3, 2] {
7595            t.set_implementers(n, &cfg).unwrap();
7596            let mut dispatched = cfg.clone();
7597            apply_task_implementers(&mut dispatched, &t);
7598            assert_eq!(dispatched.graph.implementers, usize::from(n));
7599            assert_eq!(implementers_match(&t, &run), n == 2);
7600            for proposed in [
7601                Starter::Resume(run.id.clone()),
7602                Starter::Review("old-branch".into()),
7603            ] {
7604                let expected = if n == 2 {
7605                    proposed.clone()
7606                } else {
7607                    Starter::Start
7608                };
7609                assert_eq!(
7610                    starter_with_implementers(&t, Some(&run), proposed),
7611                    expected
7612                );
7613            }
7614            assert_eq!(
7615                run.config.graph.implementers, 2,
7616                "history must not be rewritten"
7617            );
7618        }
7619        t.solo = false;
7620        t.overrides = None;
7621        assert!(implementers_match(&t, &run));
7622    }
7623
7624    #[test]
7625    fn a_solo_task_runs_with_one_candidate_and_a_plain_task_keeps_the_configs() {
7626        // Three seats said out loud. What `solo` promises is one candidate
7627        // *whatever the config asks for*, so the contrast has to be a number
7628        // this test owns - it used to be `Config::default()`'s, which became
7629        // 1 when one implementation became the default and left the two
7630        // halves of this test asserting the same thing.
7631        let mut solo_cfg = Config::default();
7632        solo_cfg.graph.implementers = 3;
7633        let mut solo_task = task();
7634        solo_task.solo = true;
7635        apply_task_implementers(&mut solo_cfg, &solo_task);
7636        assert_eq!(solo_cfg.graph.implementers, 1);
7637
7638        let mut plain_cfg = Config::default();
7639        plain_cfg.graph.implementers = 3;
7640        let plain_task = task();
7641        assert!(!plain_task.solo);
7642        apply_task_implementers(&mut plain_cfg, &plain_task);
7643        assert_eq!(
7644            plain_cfg.graph.implementers, 3,
7645            "a task that did not ask to run alone keeps the config's candidates"
7646        );
7647    }
7648
7649    fn loss(seat: &str, at: &str, reset: Option<&str>) -> QuotaLoss {
7650        QuotaLoss {
7651            seat: seat.into(),
7652            node: "judge".into(),
7653            at: at.parse().unwrap(),
7654            reset: reset.map(str::to_string),
7655        }
7656    }
7657
7658    #[test]
7659    fn a_resumed_run_with_only_old_quota_losses_arms_no_cooldown() {
7660        let old: Vec<QuotaLoss> = (1..=4)
7661            .map(|i| {
7662                loss(
7663                    &format!("judge-{i}"),
7664                    "2026-09-23T05:23:00Z",
7665                    Some("2:40pm (Asia/Tokyo)"),
7666                )
7667            })
7668            .collect();
7669        let fresh = losses_this_attempt(&old, &old);
7670        assert!(fresh.is_empty());
7671        assert_eq!(cooldown_until(&fresh, Timestamp::now()), None);
7672        // And it is not a `quota_hit` either: that is `!fresh.is_empty()`.
7673    }
7674
7675    #[test]
7676    fn a_new_quota_loss_during_the_attempt_still_arms_the_cooldown() {
7677        let old = vec![loss("judge-1", "2026-09-23T05:23:00Z", None)];
7678        let now = Timestamp::now();
7679        let mut after = old.clone();
7680        after.push(loss("judge-2", &now.to_string(), None));
7681        let fresh = losses_this_attempt(&old, &after);
7682        assert_eq!(fresh, vec![after[1].clone()]);
7683        let until = cooldown_until(&fresh, now).expect("a fresh loss arms the cooldown");
7684        assert_eq!(
7685            until,
7686            now + jiff::SignedDuration::from_secs(QUOTA_WAIT_FALLBACK.as_secs() as i64)
7687        );
7688    }
7689
7690    #[test]
7691    fn a_recovered_seat_dropping_out_of_the_history_does_not_hide_a_new_loss() {
7692        // `recover_stall` removes judge-1's loss and the retry then hits quota
7693        // again: the vector is the same length, so an index diff sees nothing.
7694        let before = vec![
7695            loss("judge-1", "2026-09-23T05:23:00Z", None),
7696            loss("judge-2", "2026-09-23T05:24:00Z", None),
7697        ];
7698        let after = vec![
7699            loss("judge-2", "2026-09-23T05:24:00Z", None),
7700            loss("judge-1", "2026-09-24T01:00:00Z", None),
7701        ];
7702        assert_eq!(losses_this_attempt(&before, &after), vec![after[1].clone()]);
7703    }
7704
7705    #[test]
7706    fn merge_overrides_are_parsed_or_refused() {
7707        assert_eq!(merge_mode("none").unwrap(), MergeMode::None);
7708        assert_eq!(merge_mode("local").unwrap(), MergeMode::Local);
7709        assert_eq!(merge_mode("pr").unwrap(), MergeMode::Pr);
7710        assert!(merge_mode("squash").is_err());
7711    }
7712
7713    #[test]
7714    fn quota_wait_uses_a_future_reset_time_capped_and_falls_back_otherwise() {
7715        let now = Timestamp::now();
7716        let fallback = Duration::from_secs(300);
7717        let cap = Duration::from_secs(1800);
7718
7719        // No reset hint at all: the fallback.
7720        assert_eq!(quota_wait(None, now, fallback, cap), fallback);
7721
7722        // A reset ten minutes out, well inside the cap: waited for exactly.
7723        let soon = now + jiff::SignedDuration::from_secs(600);
7724        assert_eq!(
7725            quota_wait(Some(soon), now, fallback, cap),
7726            Duration::from_secs(600)
7727        );
7728
7729        // A reset already in the past is not trusted: the fallback, not a
7730        // zero or negative wait that would spin the loop right back around.
7731        let past = now - jiff::SignedDuration::from_secs(60);
7732        assert_eq!(quota_wait(Some(past), now, fallback, cap), fallback);
7733
7734        // A reset further out than the cap is trusted for direction but not
7735        // for magnitude: a parsing slip must not sleep the loop for a day.
7736        let far = now + jiff::SignedDuration::from_secs(3 * 3600);
7737        assert_eq!(quota_wait(Some(far), now, fallback, cap), cap);
7738    }
7739
7740    #[test]
7741    fn parse_reset_hint_reads_the_claude_cli_shape_and_rolls_a_past_clock_to_tomorrow() {
7742        let now = "2026-09-07T02:50:00Z".parse::<Timestamp>().unwrap();
7743
7744        let at = parse_reset_hint("4:50am (UTC)", now, now).expect("a recognised shape parses");
7745        assert_eq!(at.to_string(), "2026-09-07T04:50:00Z");
7746
7747        // Same clock reading, but it has already gone by today: read as
7748        // tomorrow's, since the CLI would not still be reporting a limit past
7749        // its own stated reset.
7750        let already_past =
7751            parse_reset_hint("1:00am (UTC)", now, now).expect("a recognised shape parses");
7752        assert_eq!(already_past.to_string(), "2026-09-08T01:00:00Z");
7753
7754        assert!(
7755            parse_reset_hint("session limit reached", now, now).is_none(),
7756            "free text with no recognised shape is not guessed at"
7757        );
7758        assert!(
7759            parse_reset_hint("4:50am (Nowhere/Fake)", now, now).is_none(),
7760            "an unresolvable zone name is not guessed at either"
7761        );
7762    }
7763
7764    #[test]
7765    fn parse_reset_hint_reads_the_codex_cli_shape_with_no_year_rollover_needed() {
7766        let now = "2026-09-07T02:50:00Z".parse::<Timestamp>().unwrap();
7767
7768        let at = parse_reset_hint(
7769            "You've hit your usage limit. Visit \
7770             https://chatgpt.com/codex/settings/usage to purchase more \
7771             credits or try again at Sep 19th, 2026 5:10 PM.",
7772            now,
7773            now,
7774        )
7775        .expect("the codex reset wording is a recognised shape");
7776        assert_eq!(at.to_string(), "2026-09-19T17:10:00Z");
7777
7778        // The month is explicit, so a date already earlier in the same
7779        // sentence-implied year than `now` is trusted as written rather than
7780        // rolled forward a year the way the bracketed shape rolls a
7781        // same-day clock reading to tomorrow.
7782        let earlier = parse_reset_hint("try again at Jan 2nd, 2026 1:00 AM.", now, now)
7783            .expect("an explicit year needs no rollover");
7784        assert_eq!(earlier.to_string(), "2026-01-02T01:00:00Z");
7785
7786        assert!(
7787            parse_reset_hint("try again at Sep 19th, 26 5:10 PM.", now, now).is_none(),
7788            "a two-digit year is not the documented shape and is not guessed at"
7789        );
7790        assert!(
7791            parse_reset_hint("try again at Sept 19th, 2026 5:10 PM.", now, now).is_none(),
7792            "a four-letter month name is not the documented three-letter abbreviation"
7793        );
7794        assert!(
7795            parse_reset_hint("try again at Sep 19th, 2026 5:10 PM (UTC).", now, now).is_none(),
7796            "an explicit zone on the dated shape is a format nobody has \
7797             documented, and is refused rather than guessed at as UTC"
7798        );
7799    }
7800
7801    #[test]
7802    fn parse_reset_hint_reads_agys_relative_shape_from_when_the_loss_was_recorded() {
7803        let now = "2026-09-24T12:00:00Z".parse::<Timestamp>().unwrap();
7804        let recorded = "2026-09-24T08:00:00Z".parse::<Timestamp>().unwrap();
7805
7806        let at = parse_reset_hint("in 1h2m49s", now, recorded).expect("agy's shape parses");
7807        assert_eq!(at.as_second() - recorded.as_second(), 3769);
7808
7809        let partial = parse_reset_hint("in 45m", now, recorded).expect("units are optional");
7810        assert_eq!(partial.as_second() - recorded.as_second(), 45 * 60);
7811
7812        for bad in ["in ", "in 45", "in 3x", "in m", "in 1h junk", "1h2m"] {
7813            assert!(
7814                parse_reset_hint(bad, now, recorded).is_none(),
7815                "{bad:?} must not be guessed at"
7816            );
7817        }
7818    }
7819
7820    /// A loop whose queue lives in a temp tree and whose poll interval is far
7821    /// longer than the test's patience, so anything that waits out a poll
7822    /// instead of noticing the stop fails rather than merely being slow.
7823    fn idle_loop(dir: &Path) -> (Opts, Queue, PathBuf, PathBuf, PathBuf) {
7824        let config = dir.join("magi.toml");
7825        std::fs::write(
7826            &config,
7827            "[disk]\nmin_free_bytes = 0\nauto_fold = false\ncache_limit_bytes = 0\n",
7828        )
7829        .unwrap();
7830        let opts = Opts {
7831            poll: Duration::from_secs(30),
7832            config: Some(config),
7833            // The explicit fixture config keeps startup cleanup from reading
7834            // machine configuration. This fictional repository likewise
7835            // keeps any best-effort git cleanup away from this checkout.
7836            repo: dir.join("repo"),
7837            ..Opts::default()
7838        };
7839        // The status file goes in a directory that does not exist yet, so its
7840        // creation is itself evidence the loop published one. `worktrees`
7841        // must be just as fictional: the janitor reclaims worktrees under it
7842        // for real, and a test that let it fall through to
7843        // `crate::run::default_worktree_root()` would have it reclaim
7844        // worktrees out of the operator's real `~/wt/<repo>`, not a fixture -
7845        // which is exactly what happened before this function took the
7846        // parameter at all.
7847        let home = dir.join("home");
7848        let worktrees = dir.join("wt");
7849        (
7850            opts,
7851            Queue::at(dir.join("queue")),
7852            home.join("daemon.json"),
7853            home,
7854            worktrees,
7855        )
7856    }
7857
7858    #[test]
7859    fn a_stop_is_idempotent_and_once_set_stays_set() {
7860        let stop = Stop::new();
7861        assert!(!stop.stopped());
7862
7863        stop.stop();
7864        assert!(stop.stopped());
7865        stop.stop();
7866        assert!(stop.stopped(), "a second stop is not a toggle");
7867
7868        let shared = stop.clone();
7869        assert!(
7870            shared.stopped(),
7871            "a clone is the same stop; that is how the loop and its caller share one"
7872        );
7873    }
7874
7875    #[test]
7876    fn only_a_stop_with_a_run_in_flight_reads_as_finishing() {
7877        let stop = Stop::new();
7878        stop.enter();
7879        assert!(
7880            !stop.finishing(),
7881            "a busy loop nobody has asked to stop is just running"
7882        );
7883
7884        stop.stop();
7885        assert!(
7886            stop.finishing(),
7887            "a stop asked for mid-run has not landed until the run is settled"
7888        );
7889
7890        stop.exit();
7891        assert!(
7892            !stop.finishing(),
7893            "once the run is settled the stop has landed and there is nothing to finish"
7894        );
7895    }
7896
7897    #[test]
7898    fn finishing_stays_true_until_the_last_of_several_runs_exits() {
7899        let stop = Stop::new();
7900        stop.enter();
7901        stop.enter();
7902        stop.stop();
7903        assert!(stop.finishing(), "two runs still in flight");
7904
7905        stop.exit();
7906        assert!(
7907            stop.finishing(),
7908            "one run finished, but a sibling is still working"
7909        );
7910
7911        stop.exit();
7912        assert!(
7913            !stop.finishing(),
7914            "the last run out is what actually lands the stop"
7915        );
7916    }
7917
7918    #[tokio::test]
7919    async fn a_loop_already_asked_to_stop_returns_without_waiting_out_a_poll() {
7920        let dir = tempfile::tempdir().unwrap();
7921        let (opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
7922        let stop = Stop::new();
7923        stop.stop();
7924
7925        let began = std::time::Instant::now();
7926        tokio::time::timeout(
7927            Duration::from_secs(2),
7928            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
7929        )
7930        .await
7931        .expect("a stopped loop must return, not sit out its poll interval")
7932        .expect("the loop's own setup and teardown must not fail");
7933        assert!(
7934            began.elapsed() < opts.poll,
7935            "returned only after {:?}, which is a poll interval, not a stop",
7936            began.elapsed()
7937        );
7938    }
7939
7940    #[tokio::test]
7941    async fn a_stop_while_idle_wakes_the_wait_instead_of_sleeping_it_out() {
7942        let dir = tempfile::tempdir().unwrap();
7943        let (opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
7944        let stop = Stop::new();
7945
7946        // Asked for after the loop is already parked on its empty queue, which
7947        // is the case an operator tapping stop on a phone actually hits.
7948        let asker = {
7949            let stop = stop.clone();
7950            tokio::spawn(async move {
7951                tokio::time::sleep(Duration::from_millis(20)).await;
7952                stop.stop();
7953            })
7954        };
7955
7956        let began = std::time::Instant::now();
7957        tokio::time::timeout(
7958            Duration::from_secs(2),
7959            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
7960        )
7961        .await
7962        .expect("a stop asked for while idle must wake the wait")
7963        .expect("the loop's own setup and teardown must not fail");
7964        asker.await.unwrap();
7965        assert!(
7966            began.elapsed() < opts.poll,
7967            "returned only after {:?}, so the stop waited on the sleep",
7968            began.elapsed()
7969        );
7970    }
7971
7972    #[tokio::test]
7973    async fn a_stopped_loop_leaves_no_status_file_claiming_it_is_running() {
7974        let dir = tempfile::tempdir().unwrap();
7975        let (opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
7976        let stop = Stop::new();
7977        stop.stop();
7978
7979        tokio::time::timeout(
7980            Duration::from_secs(2),
7981            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
7982        )
7983        .await
7984        .expect("a stopped loop must return")
7985        .expect("the loop's own setup and teardown must not fail");
7986
7987        assert!(
7988            home.is_dir(),
7989            "the loop did publish a status file, so its removal is the teardown and not an absence"
7990        );
7991        assert!(
7992            !status_file.exists(),
7993            "a stopped loop clears its status file"
7994        );
7995        assert!(
7996            read_status(&home).is_none(),
7997            "a reader must see no daemon at all, not a heartbeat that merely stopped"
7998        );
7999    }
8000
8001    #[tokio::test]
8002    async fn once_runs_startup_housekeeping_before_an_empty_queue_exits() {
8003        let dir = tempfile::tempdir().unwrap();
8004        let (mut opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
8005        opts.once = true;
8006
8007        let mut settled = RunState::new(
8008            dir.path().join("repo"),
8009            "main".to_owned(),
8010            "abc1234".to_owned(),
8011            "fixture".to_owned(),
8012            Config::default(),
8013        );
8014        settled.status = RunStatus::Ready;
8015        let run_dir = home.join("runs").join(&settled.id);
8016        std::fs::create_dir_all(&run_dir).unwrap();
8017        std::fs::write(
8018            run_dir.join("run.json"),
8019            serde_json::to_string_pretty(&settled).unwrap(),
8020        )
8021        .unwrap();
8022        let questions = Questions::at(home.join("questions"));
8023        let mut question = ask::Question::new(
8024            settled.id.clone(),
8025            "review".to_owned(),
8026            "reviewer-1".to_owned(),
8027            "Continue?".to_owned(),
8028            String::new(),
8029            Vec::new(),
8030        );
8031        questions.put(&mut question).unwrap();
8032
8033        drive(&opts, &queue, &status_file, &home, &worktrees, &Stop::new())
8034            .await
8035            .unwrap();
8036
8037        assert_eq!(
8038            questions.get(&question.id).unwrap().status,
8039            ask::QuestionStatus::Abandoned,
8040            "an empty --once drain still performs startup question cleanup"
8041        );
8042    }
8043
8044    #[test]
8045    fn cache_check_due_fires_immediately_then_waits_out_its_own_interval() {
8046        let t0 = "2026-09-15T00:00:00Z".parse::<Timestamp>().unwrap();
8047
8048        assert!(
8049            cache_check_due(None, t0, CACHE_CHECK_INTERVAL_SECS),
8050            "never checked before: due at once"
8051        );
8052
8053        let one_sec_later = t0 + jiff::SignedDuration::from_secs(1);
8054        assert!(
8055            !cache_check_due(Some(t0), one_sec_later, CACHE_CHECK_INTERVAL_SECS),
8056            "well inside the interval: not due yet"
8057        );
8058
8059        let at_the_edge = t0 + jiff::SignedDuration::from_secs(CACHE_CHECK_INTERVAL_SECS as i64);
8060        assert!(
8061            !cache_check_due(Some(t0), at_the_edge, CACHE_CHECK_INTERVAL_SECS),
8062            "exactly at the edge: not yet due, same convention as `clean::due`"
8063        );
8064
8065        let past_it = t0 + jiff::SignedDuration::from_secs(CACHE_CHECK_INTERVAL_SECS as i64 + 1);
8066        assert!(
8067            cache_check_due(Some(t0), past_it, CACHE_CHECK_INTERVAL_SECS),
8068            "past the interval: due again"
8069        );
8070    }
8071
8072    /// A `magi.toml` whose `[verify] gate` names `cache_dir` as its shared
8073    /// `CARGO_TARGET_DIR`, capped at `limit_bytes`, plus a repository path
8074    /// that is never created - the fixtures [`maybe_prune_cache_between_runs`]
8075    /// and the congestion test below both need, and must not drift apart.
8076    fn cache_check_opts(dir: &Path, cache_dir: &Path, limit_bytes: u64) -> Opts {
8077        let config = dir.join("magi.toml");
8078        // A literal (single-quoted) TOML string, not a basic one: the cache
8079        // path is a Windows path full of backslashes, and a basic string
8080        // would have TOML try to interpret `\U` (from `\Users\...`) as a
8081        // Unicode escape and fail to parse - the same trap `magi.toml`'s own
8082        // `{{ vars.cache }}` rendering documents.
8083        std::fs::write(
8084            &config,
8085            format!(
8086                "[disk]\nmin_free_bytes = 0\nauto_fold = false\ncache_limit_bytes = {limit_bytes}\n\n\
8087                 [verify]\ngate = ['CARGO_TARGET_DIR={} cargo make check']\n",
8088                cache_dir.display()
8089            ),
8090        )
8091        .unwrap();
8092        Opts {
8093            config: Some(config),
8094            repo: dir.join("repo"),
8095            ..Opts::default()
8096        }
8097    }
8098
8099    #[tokio::test]
8100    async fn maybe_prune_cache_between_runs_reprunes_only_once_its_own_interval_elapses() {
8101        let dir = tempfile::tempdir().unwrap();
8102        let home = dir.path().join("home");
8103        let cache_dir = dir.path().join("cache");
8104        std::fs::create_dir_all(&cache_dir).unwrap();
8105        std::fs::write(cache_dir.join("a"), vec![0u8; 10]).unwrap();
8106        let opts = cache_check_opts(dir.path(), &cache_dir, 1);
8107
8108        // Nobody has asked this daemon to stop, which is the ordinary case;
8109        // the skip that a stop buys is asserted by its own test below.
8110        let running = Stop::new();
8111        let mut last_checked = None;
8112        let t0 = "2026-09-15T00:00:00Z".parse::<Timestamp>().unwrap();
8113        maybe_prune_cache_between_runs(&opts.repo, &opts, &home, &running, &mut last_checked, t0)
8114            .await;
8115        assert_eq!(
8116            crate::disk::dir_size(&cache_dir),
8117            0,
8118            "over the cap on the first check ever: pruned at once, no idle queue required"
8119        );
8120        assert_eq!(last_checked, Some(t0));
8121
8122        // A fresh oversized file lands, but the next check is not due yet.
8123        std::fs::write(cache_dir.join("b"), vec![0u8; 10]).unwrap();
8124        let too_soon = t0 + jiff::SignedDuration::from_secs(1);
8125        maybe_prune_cache_between_runs(
8126            &opts.repo,
8127            &opts,
8128            &home,
8129            &running,
8130            &mut last_checked,
8131            too_soon,
8132        )
8133        .await;
8134        assert_eq!(
8135            crate::disk::dir_size(&cache_dir),
8136            10,
8137            "too soon since the last check: left alone rather than rescanned every call"
8138        );
8139        assert_eq!(
8140            last_checked,
8141            Some(t0),
8142            "an idle check does not reset the clock"
8143        );
8144
8145        // Once the interval elapses, the same oversized cache is caught again.
8146        let due_again = t0 + jiff::SignedDuration::from_secs(CACHE_CHECK_INTERVAL_SECS as i64 + 1);
8147        maybe_prune_cache_between_runs(
8148            &opts.repo,
8149            &opts,
8150            &home,
8151            &running,
8152            &mut last_checked,
8153            due_again,
8154        )
8155        .await;
8156        assert_eq!(
8157            crate::disk::dir_size(&cache_dir),
8158            0,
8159            "due again: pruned back under the cap"
8160        );
8161    }
8162
8163    /// A stop must not queue behind housekeeping. The prune below is a
8164    /// synchronous walk of the whole cache with no await point in it, so a
8165    /// loop that entered it could not get back to its own `stopped()` test
8166    /// until the walk finished - and because no run is in flight at this
8167    /// boundary, `Stop::finishing` would meanwhile tell the operator's screen
8168    /// the stop had already landed. The idle branch has always made this same
8169    /// check before reaching `janitor`; the between-runs path makes it too.
8170    #[tokio::test]
8171    async fn a_stop_already_asked_for_skips_the_between_runs_cache_walk() {
8172        let dir = tempfile::tempdir().unwrap();
8173        let home = dir.path().join("home");
8174        let cache_dir = dir.path().join("cache");
8175        std::fs::create_dir_all(&cache_dir).unwrap();
8176        std::fs::write(cache_dir.join("a"), vec![0u8; 10]).unwrap();
8177        let opts = cache_check_opts(dir.path(), &cache_dir, 1);
8178
8179        let stop = Stop::new();
8180        stop.stop();
8181        assert!(
8182            !stop.finishing(),
8183            "no run is in flight at a between-runs boundary, so nothing else \
8184             would tell the operator this stop had not taken effect yet"
8185        );
8186
8187        let mut last_checked = None;
8188        let t0 = "2026-09-15T00:00:00Z".parse::<Timestamp>().unwrap();
8189        maybe_prune_cache_between_runs(&opts.repo, &opts, &home, &stop, &mut last_checked, t0)
8190            .await;
8191        assert_eq!(
8192            crate::disk::dir_size(&cache_dir),
8193            10,
8194            "over its cap, and due for the first check ever, but a stop outranks \
8195             it: the cap is a standing policy the next start measures again"
8196        );
8197        assert_eq!(
8198            last_checked, None,
8199            "a check that never happened must not claim the interval"
8200        );
8201    }
8202
8203    /// The regression this whole change exists for: gate timeouts on runs
8204    /// 52da/2f7f/5991/0915 traced back to the shared cache sitting at 81.8
8205    /// GiB against a 10 GiB cap, because the operator's queue never had a
8206    /// quiet moment for `poll`'s fully-idle branch to reach the ordinary
8207    /// `janitor` pass.
8208    ///
8209    /// Reproduced with a backlog whose repository is never created:
8210    /// `Runner::start` fails at `git::toplevel`, spawning no agent CLI.
8211    /// Each task is held after one startup failure, but the remaining tasks
8212    /// keep the queue busy until an external stop ends the loop.
8213    #[tokio::test]
8214    async fn cache_prune_reaches_a_queue_that_never_goes_idle() {
8215        let dir = tempfile::tempdir().unwrap();
8216        let cache_dir = dir.path().join("cache");
8217        std::fs::create_dir_all(&cache_dir).unwrap();
8218        std::fs::write(cache_dir.join("stale"), vec![0u8; 4096]).unwrap();
8219
8220        let mut opts = cache_check_opts(dir.path(), &cache_dir, 1);
8221        opts.poll = Duration::from_millis(20);
8222        opts.max_attempts = 1_000;
8223
8224        let queue = Queue::at(dir.path().join("queue"));
8225        for i in 0..128 {
8226            let mut task = Task::new(
8227                format!("queued work {i}"),
8228                format!("queued work {i}"),
8229                opts.repo.clone(),
8230                Source::Human,
8231            );
8232            queue.put(&mut task).unwrap();
8233        }
8234
8235        let home = dir.path().join("home");
8236        let worktrees = dir.path().join("wt");
8237        let status_file = home.join("daemon.json");
8238        let stop = Stop::new();
8239        let stopper = {
8240            let stop = stop.clone();
8241            let queue = Queue::at(dir.path().join("queue"));
8242            tokio::spawn(async move {
8243                // Observe completed work rather than rely on a wall-clock
8244                // delay. Every startup failure holds only its own task; the
8245                // backlog keeps offering work at the next boundary.
8246                for _ in 0..2_000 {
8247                    if queue.list().iter().filter(|t| t.attempts == 1).count() >= 2 {
8248                        break;
8249                    }
8250                    tokio::time::sleep(Duration::from_millis(5)).await;
8251                }
8252                stop.stop();
8253            })
8254        };
8255
8256        tokio::time::timeout(
8257            Duration::from_secs(20),
8258            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
8259        )
8260        .await
8261        .expect("the loop must not hang on a queue that keeps producing failing work")
8262        .expect("the loop's own setup and teardown must not fail");
8263        stopper.await.unwrap();
8264
8265        let after = queue.list();
8266        let completed = after.iter().filter(|t| t.attempts == 1).count();
8267        assert!(
8268            completed >= 2,
8269            "the harness must have processed multiple tasks to exercise a busy queue \
8270             (got {completed} completed task(s))"
8271        );
8272        assert!(
8273            after.iter().any(|t| t.status.runnable()),
8274            "unprocessed tasks must remain: the external stop, rather than an \
8275             exhausted backlog, must end the test"
8276        );
8277        assert!(
8278            after
8279                .iter()
8280                .filter(|t| t.attempts > 0)
8281                .all(|t| { t.attempts == 1 && t.status == TaskStatus::Held }),
8282            "a busy backlog must not retry a task's unchanged startup failure"
8283        );
8284
8285        assert_eq!(
8286            crate::disk::dir_size(&cache_dir),
8287            0,
8288            "an oversized cache must not be left to grow unboundedly just because the \
8289             queue kept the loop busy the whole time"
8290        );
8291    }
8292
8293    #[test]
8294    fn task_question_reconciliation_keeps_references_and_retires_manual_releases() {
8295        let dir = tempfile::tempdir().unwrap();
8296        let queue = Queue::at(dir.path().join("queue"));
8297        let questions = Questions::at(dir.path().join("questions"));
8298        let mut task = task();
8299        queue.put(&mut task).unwrap();
8300
8301        let mut task_question = ask::Question::new(
8302            task.id.clone(),
8303            crate::conduct::NODE.to_owned(),
8304            "conduct".to_owned(),
8305            "Which backend?".to_owned(),
8306            String::new(),
8307            Vec::new(),
8308        );
8309        questions.put(&mut task_question).unwrap();
8310        task.block(vec![task_question.id.clone()], None);
8311        queue.put(&mut task).unwrap();
8312
8313        let mut run_question = ask::Question::new(
8314            "20260101-000000-run1".to_owned(),
8315            "review".to_owned(),
8316            "reviewer-1".to_owned(),
8317            "Run question".to_owned(),
8318            String::new(),
8319            Vec::new(),
8320        );
8321        questions.put(&mut run_question).unwrap();
8322
8323        // A question from another node whose `run` happens to equal this
8324        // task's id — the same field, filled in for an unrelated reason. Only
8325        // `crate::conduct::NODE` questions use `run` as a task id; this one
8326        // must never be touched by this reconciliation, even after release.
8327        let mut coincidental = ask::Question::new(
8328            task.id.clone(),
8329            "review".to_owned(),
8330            "reviewer-1".to_owned(),
8331            "Unrelated review question".to_owned(),
8332            String::new(),
8333            Vec::new(),
8334        );
8335        questions.put(&mut coincidental).unwrap();
8336
8337        reconcile_task_questions(&queue, &questions);
8338        assert!(questions.get(&task_question.id).unwrap().status.open());
8339        assert!(questions.get(&run_question.id).unwrap().status.open());
8340        assert!(questions.get(&coincidental.id).unwrap().status.open());
8341
8342        task.release();
8343        queue.put(&mut task).unwrap();
8344        reconcile_task_questions(&queue, &questions);
8345        assert_eq!(
8346            questions.get(&task_question.id).unwrap().status,
8347            ask::QuestionStatus::Abandoned
8348        );
8349        assert!(
8350            questions.get(&run_question.id).unwrap().status.open(),
8351            "run questions remain the run janitor's responsibility"
8352        );
8353        assert!(
8354            questions.get(&coincidental.id).unwrap().status.open(),
8355            "a non-conductor question must not be abandoned just because its \
8356             run id coincides with a task id"
8357        );
8358    }
8359
8360    #[test]
8361    fn a_freshly_started_running_task_is_never_stalled() {
8362        let dir = tempfile::tempdir().unwrap();
8363        let mut t = task();
8364        t.start("run-1".to_owned());
8365        // `updated_at` is `Timestamp::now()`, left alone: no live daemon
8366        // named in `dir`, but nowhere near `STALLED_RUNNING` yet.
8367        assert!(!is_stalled(&t, dir.path(), Timestamp::now()));
8368    }
8369
8370    #[test]
8371    fn a_long_running_task_with_no_live_daemon_is_stalled() {
8372        let dir = tempfile::tempdir().unwrap();
8373        let mut t = task();
8374        t.start("run-1".to_owned());
8375        t.updated_at = Timestamp::now()
8376            - jiff::SignedDuration::from_secs(STALLED_RUNNING.as_secs() as i64 + 60);
8377        assert!(is_stalled(&t, dir.path(), Timestamp::now()));
8378        assert_eq!(
8379            stalled_tasks(
8380                &Queue::at(dir.path().join("q")),
8381                dir.path(),
8382                Timestamp::now()
8383            )
8384            .len(),
8385            0,
8386            "the task was never written to this queue"
8387        );
8388    }
8389
8390    #[test]
8391    fn a_long_running_task_a_live_daemon_still_names_is_not_stalled() {
8392        let dir = tempfile::tempdir().unwrap();
8393        let mut t = task();
8394        t.id = "20260903-080340-0167".to_owned();
8395        t.start("20260903-080619-01c2".to_owned());
8396        t.updated_at = Timestamp::now()
8397            - jiff::SignedDuration::from_secs(STALLED_RUNNING.as_secs() as i64 + 60);
8398
8399        let mut status = Status::new();
8400        status.current = vec![Current {
8401            task: t.id.clone(),
8402            run: "20260903-080619-01c2".to_owned(),
8403        }];
8404        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
8405
8406        assert!(
8407            !is_stalled(&t, dir.path(), Timestamp::now()),
8408            "a live daemon's own heartbeat rules out stalled, however long the task has run"
8409        );
8410    }
8411
8412    /// Rewrite a task's `updated_at` on disk directly, bypassing
8413    /// `Queue::put`'s own `Timestamp::now()` stamping - the only way to make
8414    /// a fixture look like it has genuinely been `running` for a while.
8415    fn backdate_task(queue: &Queue, id: &str, seconds_ago: i64) {
8416        let path = queue.path_of(id);
8417        let body = std::fs::read_to_string(&path).unwrap();
8418        let mut v: serde_json::Value = serde_json::from_str(&body).unwrap();
8419        let old = Timestamp::now() - jiff::SignedDuration::from_secs(seconds_ago);
8420        v["updated_at"] = serde_json::Value::String(old.to_string());
8421        std::fs::write(&path, serde_json::to_string_pretty(&v).unwrap()).unwrap();
8422    }
8423
8424    #[test]
8425    fn stalled_tasks_still_reaches_a_task_reclaim_could_not_claim_yet() {
8426        // The realistic `poll()` ordering, not `is_stalled` in isolation:
8427        // `reclaim_orphaned_running` runs first, on every poll, and settles
8428        // any `running` task whose claim it can actually take. For most
8429        // crashes that is immediate - a dead pid is proof enough for
8430        // `sweep_stale_claims` to drop the lock the same tick, and the very
8431        // next claim attempt succeeds. But a lock whose pid cannot be parsed
8432        // at all falls back to `STALE_CLAIM`'s six-hour age instead (see
8433        // `sweep_stale_claims`'s own doc), so the lock - and the claim
8434        // failure behind it - can legitimately outlive many polls. This is
8435        // exactly the gap `stalled_tasks` exists to surface well before that
8436        // six-hour sweep would: reclaim leaves the task `running`, and it
8437        // must still reach the conductor as stalled.
8438        let dir = tempfile::tempdir().unwrap();
8439        let queue = Queue::at(dir.path().join("queue"));
8440        let home = dir.path().join("home");
8441
8442        let mut t = task();
8443        t.id = "20260101-000001-lock".to_owned();
8444        t.start("run-1".to_owned());
8445        queue.put(&mut t).unwrap();
8446        backdate_task(&queue, &t.id, STALLED_RUNNING.as_secs() as i64 + 60);
8447        std::fs::write(
8448            dir.path().join("queue").join(format!("{}.lock", t.id)),
8449            "not a pid",
8450        )
8451        .unwrap();
8452
8453        let now = Timestamp::now();
8454        assert!(
8455            reclaim_orphaned_running(&queue, 2).is_empty(),
8456            "the unparseable lock is still well within STALE_CLAIM, so the claim fails \
8457             and reclaim must leave the task alone"
8458        );
8459        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Running);
8460
8461        let stalled = stalled_tasks(&queue, &home, now);
8462        assert_eq!(
8463            stalled.len(),
8464            1,
8465            "reclaim's inability to claim it yet must not hide it from the conductor"
8466        );
8467        assert_eq!(stalled[0].id, t.id);
8468    }
8469
8470    #[test]
8471    fn ordinary_dead_daemon_task_is_shown_stalled_before_reclaim_and_can_be_requeued() {
8472        let dir = tempfile::tempdir().unwrap();
8473        crate::run::set_home(dir.path().join("run-home"));
8474        let queue = Queue::at(dir.path().join("queue"));
8475        let home = dir.path().join("home");
8476        let questions = Questions::at(dir.path().join("questions"));
8477
8478        let mut t = task();
8479        t.id = "20260101-000003-dead".to_owned();
8480        t.start("missing-run".to_owned());
8481        queue.put(&mut t).unwrap();
8482        backdate_task(&queue, &t.id, STALLED_RUNNING.as_secs() as i64 + 60);
8483
8484        // This is the real poll ordering: retain the deterministic stalled
8485        // input before a claim proves the owner is gone and reclaims it.
8486        let stalled = stalled_tasks(&queue, &home, Timestamp::now());
8487        assert_eq!(
8488            stalled.iter().map(|task| &task.id).collect::<Vec<_>>(),
8489            [&t.id]
8490        );
8491        assert_eq!(reclaim_orphaned_running(&queue, 2), [t.id.clone()]);
8492        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
8493
8494        // Reclaim drops its guard before conductor decisions are applied, so
8495        // the decision for the captured stalled input has a real write path.
8496        crate::conduct::apply(
8497            &queue,
8498            &questions,
8499            &crate::conduct::Verdict {
8500                decisions: vec![crate::conduct::Decision {
8501                    id: t.id.clone(),
8502                    recovery: Some(crate::conduct::Recovery::Requeue),
8503                    ..crate::conduct::Decision::default()
8504                }],
8505            },
8506        )
8507        .unwrap();
8508        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Queued);
8509    }
8510
8511    #[test]
8512    fn stalled_tasks_reports_exactly_the_tasks_is_stalled_agrees_on() {
8513        let dir = tempfile::tempdir().unwrap();
8514        let queue = Queue::at(dir.path().join("queue"));
8515        let home = dir.path().join("home");
8516
8517        let mut fresh = task();
8518        fresh.id = "20260101-000001-aaaa".to_owned();
8519        fresh.start("run-1".to_owned());
8520        queue.put(&mut fresh).unwrap();
8521
8522        let mut old = task();
8523        old.id = "20260101-000002-bbbb".to_owned();
8524        old.start("run-2".to_owned());
8525        queue.put(&mut old).unwrap();
8526        backdate_task(&queue, &old.id, STALLED_RUNNING.as_secs() as i64 + 60);
8527
8528        let stalled = stalled_tasks(&queue, &home, Timestamp::now());
8529        assert_eq!(stalled.len(), 1);
8530        assert_eq!(stalled[0].id, old.id);
8531    }
8532
8533    #[test]
8534    fn queued_and_finished_task_views_partition_by_status() {
8535        let dir = tempfile::tempdir().unwrap();
8536        let queue = Queue::at(dir.path().join("queue"));
8537
8538        let mut queued = task();
8539        queued.id = "20260101-000001-aaaa".to_owned();
8540        queue.put(&mut queued).unwrap();
8541
8542        let mut failed = task();
8543        failed.id = "20260101-000002-bbbb".to_owned();
8544        failed.start("run-1".to_owned());
8545        failed.fail("gate red", 5);
8546        queue.put(&mut failed).unwrap();
8547
8548        let mut held = task();
8549        held.id = "20260101-000003-cccc".to_owned();
8550        held.hold_machine(None);
8551        queue.put(&mut held).unwrap();
8552
8553        let mut running = task();
8554        running.id = "20260101-000004-dddd".to_owned();
8555        running.start("run-2".to_owned());
8556        queue.put(&mut running).unwrap();
8557
8558        let queued_ids: Vec<String> = queued_tasks(&queue).into_iter().map(|t| t.id).collect();
8559        assert_eq!(queued_ids, [queued.id.clone()]);
8560
8561        let mut finished_ids: Vec<String> =
8562            finished_tasks(&queue).into_iter().map(|t| t.id).collect();
8563        finished_ids.sort_unstable();
8564        let mut want = vec![failed.id.clone(), held.id.clone()];
8565        want.sort_unstable();
8566        assert_eq!(finished_ids, want);
8567    }
8568
8569    #[test]
8570    fn resolve_blockers_clears_a_done_dependency_and_keeps_an_unresolved_one() {
8571        let dir = tempfile::tempdir().unwrap();
8572        let queue = Queue::at(dir.path().join("queue"));
8573        let questions = ask::Questions::at(dir.path().join("questions"));
8574
8575        let mut dep = task();
8576        dep.id = "20260101-000001-dep0".to_owned();
8577        dep.succeed();
8578        queue.put(&mut dep).unwrap();
8579
8580        let mut still_going = task();
8581        still_going.id = "20260101-000002-dep1".to_owned();
8582        queue.put(&mut still_going).unwrap();
8583
8584        let mut blocked = task();
8585        blocked.id = "20260101-000003-main".to_owned();
8586        blocked.block(
8587            vec![dep.id.clone(), still_going.id.clone()],
8588            Some("waits on both".to_owned()),
8589        );
8590        queue.put(&mut blocked).unwrap();
8591
8592        resolve_blockers(&queue, &questions);
8593
8594        let after = queue.get(&blocked.id).unwrap();
8595        assert_eq!(
8596            after.status,
8597            TaskStatus::Blocked,
8598            "one dependency is still outstanding"
8599        );
8600        assert_eq!(after.blocked_by, [still_going.id.clone()]);
8601    }
8602
8603    #[test]
8604    fn resolve_blockers_carries_an_answers_content_onto_the_task_and_unblocks_it() {
8605        let dir = tempfile::tempdir().unwrap();
8606        let queue = Queue::at(dir.path().join("queue"));
8607        let questions = ask::Questions::at(dir.path().join("questions"));
8608
8609        let mut q = crate::ask::Question::new(
8610            "20260101-000001-main".to_owned(),
8611            crate::conduct::NODE.to_owned(),
8612            "conduct".to_owned(),
8613            "Which backend?".to_owned(),
8614            String::new(),
8615            Vec::new(),
8616        );
8617        questions.put(&mut q).unwrap();
8618        q.answer(crate::ask::Answer::Text("SQLite".to_owned()))
8619            .unwrap();
8620        questions.put(&mut q).unwrap();
8621
8622        let mut blocked = task();
8623        blocked.id = "20260101-000001-main".to_owned();
8624        blocked.block(vec![q.id.clone()], Some("which backend?".to_owned()));
8625        queue.put(&mut blocked).unwrap();
8626
8627        resolve_blockers(&queue, &questions);
8628
8629        let after = queue.get(&blocked.id).unwrap();
8630        assert_eq!(
8631            after.status,
8632            TaskStatus::Queued,
8633            "the only blocker resolved"
8634        );
8635        assert_eq!(after.answers.len(), 1);
8636        assert_eq!(after.answers[0].question, "Which backend?");
8637        assert_eq!(after.answers[0].answer, "SQLite");
8638
8639        // And the run this task starts next is told about it.
8640        let instruction = instruction_for(&after);
8641        assert!(instruction.contains("Which backend?"));
8642        assert!(instruction.contains("SQLite"));
8643    }
8644
8645    #[test]
8646    fn resolve_blockers_holds_a_task_whose_conductor_question_was_abandoned() {
8647        let dir = tempfile::tempdir().unwrap();
8648        let queue = Queue::at(dir.path().join("queue"));
8649        let questions = ask::Questions::at(dir.path().join("questions"));
8650
8651        let mut q = crate::ask::Question::new(
8652            "20260101-000001-main".to_owned(),
8653            crate::conduct::NODE.to_owned(),
8654            "conduct".to_owned(),
8655            "Is the setup done?".to_owned(),
8656            String::new(),
8657            Vec::new(),
8658        );
8659        q.abandon("no answer within 60s of asking");
8660        questions.put(&mut q).unwrap();
8661
8662        let mut blocked = task();
8663        blocked.id = "20260101-000001-main".to_owned();
8664        blocked.block(vec![q.id.clone()], Some("setup?".to_owned()));
8665        queue.put(&mut blocked).unwrap();
8666
8667        resolve_blockers(&queue, &questions);
8668
8669        let after = queue.get(&blocked.id).unwrap();
8670        assert_eq!(
8671            after.status,
8672            TaskStatus::Held,
8673            "never left blocked on nothing"
8674        );
8675        assert!(!after.operator_held(), "a machine hold, for triage");
8676        assert!(
8677            after
8678                .hold_reason
8679                .as_deref()
8680                .unwrap_or_default()
8681                .contains("went unanswered")
8682        );
8683    }
8684
8685    #[test]
8686    fn resolve_blockers_restores_a_held_task_to_held_instead_of_queuing_it() {
8687        // Reproduces the reported bug (task 3958): a task held out of
8688        // attempts, blocked on a `crate::conduct` follow-up question, must
8689        // come back `held` once that question is answered - never `queued`,
8690        // whatever the answer said - or it silently re-enters the
8691        // competition queue with its attempts already exhausted.
8692        let dir = tempfile::tempdir().unwrap();
8693        let queue = Queue::at(dir.path().join("queue"));
8694        let questions = ask::Questions::at(dir.path().join("questions"));
8695
8696        let mut q = crate::ask::Question::new(
8697            "20260101-000001-main".to_owned(),
8698            crate::conduct::NODE.to_owned(),
8699            "conduct".to_owned(),
8700            "How should this be handled?".to_owned(),
8701            String::new(),
8702            Vec::new(),
8703        );
8704        questions.put(&mut q).unwrap();
8705        q.answer(crate::ask::Answer::Text(
8706            "leave it held, a human will look at it later".to_owned(),
8707        ))
8708        .unwrap();
8709        questions.put(&mut q).unwrap();
8710
8711        let mut held = task();
8712        held.id = "20260101-000001-main".to_owned();
8713        held.hold_machine(Some("out of attempts".to_owned()));
8714        held.block(vec![q.id.clone()], Some("what now?".to_owned()));
8715        queue.put(&mut held).unwrap();
8716
8717        resolve_blockers(&queue, &questions);
8718
8719        let after = queue.get(&held.id).unwrap();
8720        assert_eq!(after.status, TaskStatus::Held);
8721        assert_eq!(after.hold_reason.as_deref(), Some("out of attempts"));
8722        assert_eq!(
8723            after.answers[0].answer,
8724            "leave it held, a human will look at it later"
8725        );
8726    }
8727
8728    #[test]
8729    fn resolve_blockers_releases_a_task_whose_dependency_was_deleted_on_purpose() {
8730        let dir = tempfile::tempdir().unwrap();
8731        let queue = Queue::at(dir.path().join("queue"));
8732        let questions = ask::Questions::at(dir.path().join("questions"));
8733        let mut dep = task();
8734        dep.id = "20260101-000001-gone".to_owned();
8735        queue.put(&mut dep).unwrap();
8736        let mut blocked = task();
8737        blocked.id = "20260101-000003-main".to_owned();
8738        blocked.block(vec![dep.id.clone()], None);
8739        queue.put(&mut blocked).unwrap();
8740
8741        // Claimed, so `remove` cannot rewrite it and the resolver must.
8742        let claim = queue.claim(&blocked.id).unwrap();
8743        queue.remove(&dep.id, false, &questions).unwrap();
8744        drop(claim);
8745        resolve_blockers(&queue, &questions);
8746
8747        let after = queue.get(&blocked.id).unwrap();
8748        assert_eq!(after.status, TaskStatus::Queued);
8749        assert!(after.blocked_by.is_empty());
8750    }
8751
8752    #[test]
8753    fn resolve_blockers_holds_a_task_whose_dependency_was_deleted() {
8754        // Reproduces the reported bug: a task blocked on a task id that was
8755        // removed (`magi task rm`, or deleted by hand) can never see that id
8756        // reach `Done`, so the ordinary per-id loop has nothing to notice and
8757        // would otherwise leave the task `blocked` forever with no way for an
8758        // operator to find out why.
8759        let dir = tempfile::tempdir().unwrap();
8760        let queue = Queue::at(dir.path().join("queue"));
8761        let questions = ask::Questions::at(dir.path().join("questions"));
8762
8763        let mut still_going = task();
8764        still_going.id = "20260101-000002-dep1".to_owned();
8765        queue.put(&mut still_going).unwrap();
8766
8767        let mut blocked = task();
8768        blocked.id = "20260101-000003-main".to_owned();
8769        blocked.block(
8770            vec!["20260101-000001-gone".to_owned(), still_going.id.clone()],
8771            Some("waits on both".to_owned()),
8772        );
8773        queue.put(&mut blocked).unwrap();
8774
8775        resolve_blockers(&queue, &questions);
8776
8777        let after = queue.get(&blocked.id).unwrap();
8778        assert_eq!(
8779            after.status,
8780            TaskStatus::Held,
8781            "a missing dependency must not leave the task blocked forever"
8782        );
8783        assert_eq!(after.hold_source, Some(crate::queue::HoldSource::Machine));
8784        assert!(after.blocked_by.is_empty());
8785        let reason = after.hold_reason.as_deref().unwrap_or_default();
8786        assert!(
8787            reason.contains("20260101-000001-gone"),
8788            "the missing id must be named so an operator can tell what happened: {reason}"
8789        );
8790        assert!(
8791            reason.contains(&still_going.id),
8792            "the still-valid dependency must not silently vanish from the record: {reason}"
8793        );
8794    }
8795
8796    #[test]
8797    fn instruction_for_is_unchanged_without_any_answers() {
8798        let t = task();
8799        assert_eq!(instruction_for(&t), t.instruction);
8800    }
8801
8802    #[test]
8803    fn task_attachments_are_absolute_and_a_missing_file_is_an_error() {
8804        let dir = tempfile::tempdir().unwrap();
8805        let q = Queue::at(dir.path().join("queue"));
8806        let src = dir.path().join("shot.png");
8807        std::fs::write(&src, "x").unwrap();
8808        let mut t = task();
8809        q.attach(&mut t, &[src]).unwrap();
8810        let paths = task_attachments(&q, &t).unwrap();
8811        assert_eq!(paths.len(), 1);
8812        assert!(paths[0].is_absolute() && paths[0].is_file());
8813        std::fs::remove_file(&paths[0]).unwrap();
8814        let err = task_attachments(&q, &t).unwrap_err().to_string();
8815        assert!(err.contains("shot.png"), "{err}");
8816    }
8817
8818    #[test]
8819    fn resumed_instruction_is_unchanged_without_any_answers() {
8820        let t = task();
8821        assert_eq!(resumed_instruction(&t.instruction, &t), t.instruction);
8822    }
8823
8824    #[test]
8825    fn resumed_instruction_carries_a_new_answer_onto_the_old_run() {
8826        let mut t = task();
8827        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
8828        // The run's own instruction on disk predates the answer: it is the
8829        // plain original text `Runner::start` saved before the operator was
8830        // ever asked anything.
8831        let old = t.instruction.clone();
8832
8833        let refreshed = resumed_instruction(&old, &t);
8834        assert!(refreshed.starts_with(&old), "the original text is kept");
8835        assert!(refreshed.contains("Which backend?"));
8836        assert!(refreshed.contains("SQLite"));
8837    }
8838
8839    #[test]
8840    fn resumed_instruction_keeps_an_original_answers_heading() {
8841        let mut t = task();
8842        t.instruction = "Context\n\n# Operator answers\n\nThis is part of the task.".to_owned();
8843        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
8844
8845        let refreshed = resumed_instruction(&t.instruction, &t);
8846
8847        assert!(
8848            refreshed.starts_with(&t.instruction),
8849            "an answers heading in the original instruction is not the appended block"
8850        );
8851        assert_eq!(refreshed.matches(ANSWERS_HEADER).count(), 2);
8852        assert!(refreshed.contains("Which backend?"));
8853        assert!(refreshed.contains("SQLite"));
8854
8855        let repeated = resumed_instruction(&refreshed, &t);
8856        assert_eq!(
8857            repeated, refreshed,
8858            "only the final appended block is refreshed"
8859        );
8860    }
8861
8862    #[test]
8863    fn resumed_instruction_does_not_duplicate_across_repeated_resumes() {
8864        let mut t = task();
8865        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
8866
8867        // A first resume appends the block; a second resume of the same run,
8868        // with no new answer in between, must reproduce exactly the same
8869        // text rather than appending the block a second time.
8870        let once = resumed_instruction(&t.instruction, &t);
8871        let twice = resumed_instruction(&once, &t);
8872        assert_eq!(once, twice);
8873        assert_eq!(once.matches("Which backend?").count(), 1);
8874
8875        // A later answer replaces the block wholesale rather than growing it.
8876        t.record_answer("Which cache?".to_owned(), "Redis".to_owned());
8877        let refreshed = resumed_instruction(&once, &t);
8878        assert_eq!(refreshed.matches(ANSWERS_HEADER).count(), 1);
8879        assert!(refreshed.contains("Which backend?"));
8880        assert!(refreshed.contains("Which cache?"));
8881    }
8882
8883    #[test]
8884    fn prepare_instruction_covers_all_three_starters() {
8885        let mut t = task();
8886        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
8887
8888        // Start: a fresh run gets the task text plus every answer so far —
8889        // exactly `instruction_for`.
8890        assert_eq!(
8891            prepare_instruction(&Starter::Start, None, &t),
8892            Some(instruction_for(&t))
8893        );
8894
8895        // Resume: the run's prior instruction is refreshed with the answer,
8896        // not discarded and not left stale.
8897        let old = t.instruction.clone();
8898        assert_eq!(
8899            prepare_instruction(&Starter::Resume("some-run".to_owned()), Some(&old), &t),
8900            Some(resumed_instruction(&old, &t))
8901        );
8902
8903        // Review: a review-only pass builds its own instruction from the
8904        // branch's history in `crate::graph`, with no task statement at all -
8905        // this boundary must leave it alone.
8906        assert_eq!(
8907            prepare_instruction(&Starter::Review("magi/eba2/A".to_owned()), Some(&old), &t),
8908            None
8909        );
8910    }
8911
8912    #[test]
8913    fn choose_starter_prefers_review_over_resume_when_the_branch_survived() {
8914        assert_eq!(
8915            choose_starter(Some("magi/eba2/A"), true, Some("some-run")),
8916            Starter::Review("magi/eba2/A".to_owned())
8917        );
8918    }
8919
8920    #[test]
8921    fn choose_starter_falls_back_to_start_when_the_review_branch_is_gone() {
8922        assert_eq!(
8923            choose_starter(Some("magi/eba2/A"), false, Some("some-run")),
8924            Starter::Start,
8925            "a vanished review branch must not fall back to resuming the old run either"
8926        );
8927    }
8928
8929    #[test]
8930    fn a_refused_handover_retries_as_a_review_of_the_same_branch() {
8931        let mut t = task();
8932        t.start("old-run".to_owned());
8933        t.hold_for_handover(Some("magi/eba2/A".to_owned()), "checked out".to_owned());
8934        t.release();
8935        let branch = t.review_branch.take();
8936        assert_eq!(
8937            choose_starter(branch.as_deref(), true, Some("old-run")),
8938            Starter::Review("magi/eba2/A".to_owned()),
8939            "a review wins over resuming the old run"
8940        );
8941    }
8942
8943    #[test]
8944    fn choose_starter_resumes_or_starts_when_there_is_no_review_choice_at_all() {
8945        assert_eq!(
8946            choose_starter(None, false, Some("some-run")),
8947            Starter::Resume("some-run".to_owned())
8948        );
8949        assert_eq!(choose_starter(None, false, None), Starter::Start);
8950    }
8951
8952    #[test]
8953    fn an_explicit_release_forces_a_fresh_competition_even_with_a_resumable_run() {
8954        let mut released = task();
8955        released.start("stalled-run".to_owned());
8956        released.requeue();
8957        let unfinished = (!released.fresh_start)
8958            .then(|| Some("stalled-run".to_owned()))
8959            .flatten();
8960        assert_eq!(
8961            choose_starter(None, false, unfinished.as_deref()),
8962            Starter::Start,
8963            "release keeps run history but must not resume it"
8964        );
8965        assert_eq!(released.runs, ["stalled-run"]);
8966    }
8967
8968    #[test]
8969    fn an_ordinary_release_keeps_a_resumable_run_available() {
8970        let mut released = task();
8971        released.start("stalled-run".to_owned());
8972        released.release();
8973        let unfinished = (!released.fresh_start)
8974            .then(|| Some("stalled-run".to_owned()))
8975            .flatten();
8976        assert_eq!(
8977            choose_starter(None, false, unfinished.as_deref()),
8978            Starter::Resume("stalled-run".to_owned()),
8979            "manual release must preserve the normal resume path"
8980        );
8981    }
8982
8983    #[test]
8984    fn a_blocked_run_that_spent_every_review_round_has_exhausted_its_budget() {
8985        let mut state = run_state(RunStatus::Blocked);
8986        state.config.graph.review_rounds = 3;
8987        state.reviews = vec![review_round(1), review_round(2), review_round(3)];
8988        assert!(exhausted_review_budget(&state));
8989
8990        // One round still unused: resuming can still ask a reviewer something.
8991        state.reviews.pop();
8992        assert!(!exhausted_review_budget(&state));
8993
8994        // Exhausted rounds on a non-`Blocked` status (a stall, say) do not
8995        // count: only a `Blocked` run re-enters the review loop on resume.
8996        let mut stalled = run_state(RunStatus::Stalled);
8997        stalled.config.graph.review_rounds = 1;
8998        stalled.reviews = vec![review_round(1)];
8999        assert!(!exhausted_review_budget(&stalled));
9000    }
9001
9002    fn review_round(round: usize) -> crate::run::ReviewRound {
9003        crate::run::ReviewRound {
9004            round,
9005            head: "deadbeef".to_owned(),
9006            verified_head: None,
9007            verified_at: None,
9008            reviews: Vec::new(),
9009            e2e: Vec::new(),
9010            verify_retried: false,
9011            e2e_deferred: false,
9012            e2e_defer_reason: None,
9013            fix: None,
9014            blocking: 0,
9015            answered: 1,
9016            expected: 1,
9017            clean: false,
9018            progressed: true,
9019            vote_split: false,
9020            reconsideration: Vec::new(),
9021            verdict: None,
9022        }
9023    }
9024
9025    #[test]
9026    fn awaiting_resume_is_a_failed_task_whose_last_run_parked_and_can_be_resumed() {
9027        let mut run = RunState::new(
9028            PathBuf::from("/repo"),
9029            "main".to_owned(),
9030            "abc1234def".to_owned(),
9031            "add retries".to_owned(),
9032            Config::default(),
9033        );
9034        run.status = RunStatus::Judging;
9035        run.parked = true;
9036        let mut task = Task::new(
9037            "add retries".to_owned(),
9038            "add retries".to_owned(),
9039            PathBuf::from("/repo"),
9040            crate::queue::Source::Human,
9041        );
9042        task.status = TaskStatus::Failed;
9043        task.runs = vec![run.id.clone()];
9044        let with = |t: &Task, r: &RunState| awaiting_resume_with(t, |_| Ok(r.clone()));
9045        assert!(with(&task, &run), "parked after judging is the case");
9046        let mut parked_task = task.clone();
9047        parked_task.status = TaskStatus::Parked;
9048        assert!(with(&parked_task, &run), "the Parked status is the case");
9049
9050        let mut not_parked = run.clone();
9051        not_parked.parked = false;
9052        not_parked.status = RunStatus::Stalled;
9053        assert!(!with(&task, &not_parked), "a stall is the conductor's");
9054
9055        let mut fresh = task.clone();
9056        fresh.fresh_start = true;
9057        assert!(!with(&fresh, &run), "a requeue asked for a new competition");
9058
9059        let mut review = task.clone();
9060        review.review_branch = Some("magi/x/A".to_owned());
9061        assert!(!with(&review, &run), "review is ranked before resume");
9062
9063        let mut held = task.clone();
9064        held.status = TaskStatus::Held;
9065        assert!(!with(&held, &run), "a hold stays visible to the conductor");
9066
9067        let mut released = run.clone();
9068        released.released_to = Some("20260901-000000-new1".to_owned());
9069        assert!(!with(&task, &released), "nothing left to resume into");
9070
9071        assert!(!awaiting_resume_with(&task, |_| anyhow::bail!(
9072            "unreadable"
9073        )));
9074    }
9075
9076    #[test]
9077    fn unfinished_run_never_offers_a_run_whose_worktree_was_released() {
9078        let mut released = RunState::new(
9079            PathBuf::from("/repo"),
9080            "main".to_owned(),
9081            "abc1234def".to_owned(),
9082            "add retries".to_owned(),
9083            Config::default(),
9084        );
9085        released.status = RunStatus::Blocked;
9086        assert_eq!(
9087            unfinished_run_with(&[released.id.clone()], "t", |_| Ok(released.clone())),
9088            Some(released.id.clone())
9089        );
9090        released.released_to = Some("20260901-000000-new1".to_owned());
9091        assert_eq!(
9092            unfinished_run_with(&[released.id.clone()], "t", |_| Ok(released.clone())),
9093            None,
9094            "there is nothing left to resume it into"
9095        );
9096    }
9097
9098    #[test]
9099    fn unfinished_run_skips_a_round_exhausted_blocked_run_so_requeue_means_a_fresh_competition() {
9100        // Mirrors the failure this exists to close: a task's last run ended
9101        // `Blocked` with the review budget spent, `crate::conduct` chose
9102        // `Recovery::Requeue` (`Task::release`, which keeps `runs` as
9103        // evidence), and without this check `attempt` would go on treating
9104        // that exhausted run as "unfinished" and resume it - `graph::Runner`'s
9105        // review loop iterates zero times over an already-spent budget, so
9106        // the resumed run settles right back to `Blocked` having asked nobody
9107        // anything, and `Requeue`'s promised fresh competition never happens.
9108        let mut exhausted = RunState::new(
9109            PathBuf::from("/repo"),
9110            "main".to_owned(),
9111            "abc1234def".to_owned(),
9112            "add retries".to_owned(),
9113            Config::default(),
9114        );
9115        exhausted.status = RunStatus::Blocked;
9116        exhausted.config.graph.review_rounds = 1;
9117        exhausted.reviews = vec![review_round(1)];
9118
9119        assert_eq!(
9120            unfinished_run_with(&[exhausted.id.clone()], "t", |_| Ok(exhausted.clone())),
9121            None,
9122            "an exhausted `Blocked` run must not be offered as resumable"
9123        );
9124
9125        // A `Blocked` run with rounds still unused is genuinely worth
9126        // resuming, and must still be found.
9127        let mut has_budget_left = RunState::new(
9128            PathBuf::from("/repo"),
9129            "main".to_owned(),
9130            "abc1234def".to_owned(),
9131            "add retries".to_owned(),
9132            Config::default(),
9133        );
9134        has_budget_left.status = RunStatus::Blocked;
9135        has_budget_left.config.graph.review_rounds = 3;
9136        has_budget_left.reviews = vec![review_round(1)];
9137
9138        assert_eq!(
9139            unfinished_run_with(&[has_budget_left.id.clone()], "t", |_| {
9140                Ok(has_budget_left.clone())
9141            }),
9142            Some(has_budget_left.id.clone())
9143        );
9144    }
9145
9146    #[test]
9147    fn unfinished_run_resumes_a_run_whose_task_head_publication_is_pending() {
9148        let mut run = RunState::new(
9149            PathBuf::from("/repo"),
9150            "main".to_owned(),
9151            "abc1234def".to_owned(),
9152            "add retries".to_owned(),
9153            Config::default(),
9154        );
9155        run.status = RunStatus::Blocked;
9156        run.config.graph.review_rounds = 1;
9157        run.reviews = vec![review_round(1)];
9158        run.pr_publication_pending = true;
9159        assert_eq!(
9160            unfinished_run_with(&[run.id.clone()], "t", |_| Ok(run.clone())),
9161            Some(run.id.clone()),
9162            "a spent review budget must not turn a failed publication into a re-competition"
9163        );
9164    }
9165
9166    #[test]
9167    fn unfinished_run_never_falls_back_to_an_older_resumable_run() {
9168        // A task whose history holds an *older* run that still looks
9169        // resumable (say, a competition `Runner::review` was started
9170        // alongside after that older run went `Stalled`) and a *newest* run
9171        // that is `Blocked` with its review budget spent. `Recovery::Requeue`
9172        // on this task must mean a fresh competition — falling back to the
9173        // stale, superseded `Stalled` run instead would resurrect history
9174        // nothing asked to revisit and silently defeat the requeue.
9175        let mut older_stalled = RunState::new(
9176            PathBuf::from("/repo"),
9177            "main".to_owned(),
9178            "abc1234def".to_owned(),
9179            "add retries".to_owned(),
9180            Config::default(),
9181        );
9182        older_stalled.status = RunStatus::Stalled;
9183
9184        let mut newest_exhausted = RunState::new(
9185            PathBuf::from("/repo"),
9186            "main".to_owned(),
9187            "abc1234def".to_owned(),
9188            "add retries".to_owned(),
9189            Config::default(),
9190        );
9191        newest_exhausted.status = RunStatus::Blocked;
9192        newest_exhausted.config.graph.review_rounds = 1;
9193        newest_exhausted.reviews = vec![review_round(1)];
9194
9195        assert_eq!(
9196            unfinished_run_with(
9197                &[older_stalled.id.clone(), newest_exhausted.id.clone()],
9198                "t",
9199                |_| Ok(newest_exhausted.clone())
9200            ),
9201            None,
9202            "the newest run is exhausted, so nothing here is worth resuming - \
9203             least of all the older, already-superseded run"
9204        );
9205    }
9206
9207    #[test]
9208    fn unfinished_run_warns_and_skips_a_run_it_cannot_read() {
9209        assert_eq!(
9210            unfinished_run_with(&["20260101-000000-gone".to_owned()], "t", |_| {
9211                Err(anyhow::anyhow!("fixture is absent"))
9212            }),
9213            None
9214        );
9215    }
9216
9217    fn action_question(run: &str, action: ask::ChoiceAction) -> ask::Question {
9218        let mut q = ask::Question::new(
9219            run.to_owned(),
9220            "implement".to_owned(),
9221            "impl-A".to_owned(),
9222            "continue?".to_owned(),
9223            String::new(),
9224            vec!["resume で続行する".to_owned(), "other".to_owned()],
9225        );
9226        q.actions.insert("resume で続行する".to_owned(), action);
9227        q.answer(ask::Answer::Choice("resume で続行する".to_owned()))
9228            .unwrap();
9229        q
9230    }
9231
9232    fn held_task_with(run: &str) -> Task {
9233        let mut t = task();
9234        t.runs = vec![run.to_owned()];
9235        t.hold_machine(Some("waiting for magi resume to be executed".to_owned()));
9236        t
9237    }
9238
9239    fn resume_action(run: &str) -> ask::ChoiceAction {
9240        ask::ChoiceAction::Resume { run: run.into() }
9241    }
9242
9243    #[test]
9244    fn explicitly_resumed_older_run_remains_the_release_and_question_target() {
9245        for parked in [false, true] {
9246            let mut t = task();
9247            t.start("A".into());
9248            t.start("B".into());
9249            assert_eq!(t.runs, vec!["A", "B"]);
9250            t.start("A".into());
9251            let mut resumed = run_state(RunStatus::Blocked);
9252            resumed.id = "A".into();
9253            resumed.parked = parked;
9254            resumed.driver_exited = true;
9255            t.hold_machine(Some("resumed run stopped".into()));
9256            let q = action_question("A", resume_action("A"));
9257            assert_eq!(action_standing(&t, &q), ActionStanding::Pending);
9258            assert_eq!(
9259                decide_action(&t, &q, &PHRASES_EN, |id| {
9260                    assert_eq!(id, "A");
9261                    Ok(resumed.clone())
9262                }),
9263                ActionDecision::Resume("A".into())
9264            );
9265            assert_eq!(
9266                action_standing(&t, &action_question("B", resume_action("B"))),
9267                ActionStanding::Stale
9268            );
9269            t.release_operator("cli", None);
9270            let unfinished = unfinished_run_with(&t.runs, t.short(), |id| {
9271                assert_eq!(id, "A");
9272                Ok(resumed.clone())
9273            });
9274            assert_eq!(
9275                choose_starter(None, false, unfinished.as_deref()),
9276                Starter::Resume("A".into())
9277            );
9278            assert_eq!(t.runs, vec!["B", "A"]);
9279        }
9280    }
9281
9282    #[test]
9283    fn merge_repair_dispatch_refuses_obsolete_bindings_and_unsupported_owners() {
9284        let mut state = crate::land::tests::repair_state(true, true);
9285        state.status = RunStatus::Landing;
9286        state.parked = true;
9287        state.driver_exited = true;
9288        let mut q = ask::Question::new(
9289            state.id.clone(),
9290            land::APPROVAL_NODE.into(),
9291            "land".into(),
9292            "Merge?".into(),
9293            String::new(),
9294            vec![land::APPROVE.into(), land::HOLD.into()],
9295        );
9296        q.id = state.id.clone();
9297        state.land_approval = Some(crate::run::LandApproval {
9298            question: q.id.clone(),
9299            head: "current-head".into(),
9300        });
9301        assert!(land::upgrade_repair_choice(&mut q, &state));
9302        q.answer(ask::Answer::Choice(land::REPAIR.into())).unwrap();
9303        let mut task = held_task_with(&state.id);
9304        task.status = TaskStatus::Parked;
9305        assert_eq!(
9306            decide_action(&task, &q, &PHRASES_EN, |_| Ok(state.clone())),
9307            ActionDecision::Resume(state.id.clone())
9308        );
9309        q.asked_at = "2026-01-01T00:00:00Z".parse().unwrap();
9310        q.answered_at = Some("2026-01-01T00:00:20Z".parse().unwrap());
9311        task.operator_decided_at = Some("2026-01-01T00:00:10Z".parse().unwrap());
9312        assert_eq!(
9313            decide_action(&task, &q, &PHRASES_EN, |_| Ok(state.clone())),
9314            ActionDecision::Resume(state.id.clone()),
9315            "a repair answer newer than the manual hold resumes the existing owner"
9316        );
9317        task.operator_decided_at = Some("2026-01-01T00:00:30Z".parse().unwrap());
9318        for answered_at in [q.answered_at, None] {
9319            let mut older = q.clone();
9320            older.answered_at = answered_at;
9321            let refused = decide_action(&task, &older, &PHRASES_EN, |_| {
9322                panic!("a later manual hold must prevent dispatch")
9323            });
9324            let ActionDecision::Refuse(reason) = refused else {
9325                panic!("expected an actionable refusal, got {refused:?}");
9326            };
9327            assert!(reason.contains("later operator decision"));
9328            assert!(reason.contains(&format!("magi task release {}", task.id)));
9329        }
9330        task.operator_decided_at = None;
9331        let mut superseded = state.clone();
9332        superseded.land_approval.as_mut().unwrap().question = "fresh-decision".into();
9333        assert_eq!(
9334            decide_action(&task, &q, &PHRASES_EN, |_| Ok(superseded)),
9335            ActionDecision::Stale
9336        );
9337        for case in 1..4 {
9338            let mut changed = state.clone();
9339            match case {
9340                1 => changed.status = RunStatus::Blocked,
9341                2 => changed.released_to = Some("new-owner".into()),
9342                _ => changed.land_approval.as_mut().unwrap().head.clear(),
9343            }
9344            assert!(matches!(
9345                decide_action(&task, &q, &PHRASES_EN, |_| Ok(changed)),
9346                ActionDecision::Refuse(_)
9347            ));
9348        }
9349        assert!(matches!(
9350            decide_action(&task, &q, &PHRASES_EN, |_| anyhow::bail!("missing owner")),
9351            ActionDecision::Refuse(_)
9352        ));
9353        task.runs.push("replacement".into());
9354        task.operator_decided_at = Some("2026-01-01T00:00:30Z".parse().unwrap());
9355        assert_eq!(
9356            decide_action(&task, &q, &PHRASES_EN, |_| panic!(
9357                "old owner must not load"
9358            )),
9359            ActionDecision::Stale
9360        );
9361    }
9362
9363    #[test]
9364    fn merge_repair_dispatch_obeys_capacity_and_quota_while_hold_stays_decision_only() {
9365        let mut state = crate::land::tests::repair_state(true, true);
9366        state.status = RunStatus::Landing;
9367        state.parked = true;
9368        state.driver_exited = true;
9369        let mut q = ask::Question::new(
9370            state.id.clone(),
9371            land::APPROVAL_NODE.into(),
9372            "land".into(),
9373            "Merge?".into(),
9374            String::new(),
9375            vec![land::APPROVE.into(), land::HOLD.into()],
9376        );
9377        q.id = state.id.clone();
9378        state.land_approval = Some(crate::run::LandApproval {
9379            question: q.id.clone(),
9380            head: "current-head".into(),
9381        });
9382        state.save().unwrap();
9383        land::upgrade_repair_choice(&mut q, &state);
9384        q.answer(ask::Answer::Choice(land::REPAIR.into())).unwrap();
9385        Questions::open().put(&mut q).unwrap();
9386        let tmp = tempfile::tempdir().unwrap();
9387        let queue = Queue::at(tmp.path().join("queue"));
9388        let mut task = held_task_with(&state.id);
9389        task.status = TaskStatus::Parked;
9390        queue.put(&mut task).unwrap();
9391        let empty = Arc::new(tokio::sync::Semaphore::new(0));
9392        let free = Arc::new(tokio::sync::Semaphore::new(1));
9393        assert!(claim_dispatch(&queue, &task, false, &empty, &free).is_none());
9394        assert!(claim_dispatch(&queue, &task, true, &free, &free).is_none());
9395        let claim = claim_dispatch(&queue, &task, false, &free, &free).unwrap();
9396        assert!(claim.permit.is_some());
9397        assert!(claim_dispatch(&queue, &task, false, &free, &free).is_none());
9398        drop(claim);
9399        Questions::open()
9400            .update(&q.id, |q| {
9401                q.answer = Some(ask::Answer::Choice(land::HOLD.into()));
9402                Ok(())
9403            })
9404            .unwrap();
9405        let claim = claim_dispatch(&queue, &task, true, &empty, &empty).unwrap();
9406        assert!(claim.permit.is_none());
9407        assert!(RunState::load(&state.id).unwrap().merge_repairs.is_empty());
9408    }
9409
9410    #[test]
9411    fn decide_action_resumes_only_the_latest_resumable_run() {
9412        let t = held_task_with("r1");
9413        let q = action_question("r1", resume_action("r1"));
9414        let load = |s: RunState| move |_: &str| Ok(s);
9415        assert_eq!(
9416            decide_action(&t, &q, &PHRASES_EN, load(run_state(RunStatus::Blocked))),
9417            ActionDecision::Resume("r1".into())
9418        );
9419        // Not the latest run of the task.
9420        let q_other = action_question("r1", resume_action("r0"));
9421        assert!(matches!(
9422            decide_action(
9423                &t,
9424                &q_other,
9425                &PHRASES_EN,
9426                load(run_state(RunStatus::Blocked))
9427            ),
9428            ActionDecision::Refuse(_)
9429        ));
9430        // A finished run cannot make progress.
9431        assert!(matches!(
9432            decide_action(&t, &q, &PHRASES_EN, load(run_state(RunStatus::Ready))),
9433            ActionDecision::Refuse(_)
9434        ));
9435        // A released one either.
9436        let mut released = run_state(RunStatus::Blocked);
9437        released.released_to = Some("elsewhere".into());
9438        assert!(matches!(
9439            decide_action(&t, &q, &PHRASES_EN, load(released)),
9440            ActionDecision::Refuse(_)
9441        ));
9442        // Unreadable state is a refusal, never a fresh competition.
9443        assert!(matches!(
9444            decide_action(&t, &q, &PHRASES_EN, |_: &str| bail!("gone")),
9445            ActionDecision::Refuse(_)
9446        ));
9447    }
9448
9449    #[test]
9450    fn decide_action_ignores_a_question_about_an_earlier_run() {
9451        let mut t = held_task_with("r1");
9452        t.runs.push("r2".to_owned());
9453        let never = |_: &str| -> Result<RunState> { bail!("not read") };
9454        assert_eq!(
9455            decide_action(
9456                &t,
9457                &action_question("r1", ask::ChoiceAction::Done),
9458                &PHRASES_EN,
9459                never
9460            ),
9461            ActionDecision::Stale
9462        );
9463    }
9464
9465    #[test]
9466    fn decide_action_maps_requeue_and_done_and_never_acts_twice() {
9467        let mut t = held_task_with("r1");
9468        let never = |_: &str| -> Result<RunState> { bail!("not read") };
9469        assert_eq!(
9470            decide_action(
9471                &t,
9472                &action_question("r1", ask::ChoiceAction::Requeue),
9473                &PHRASES_EN,
9474                never
9475            ),
9476            ActionDecision::Requeue
9477        );
9478        let done_q = action_question("r1", ask::ChoiceAction::Done);
9479        assert_eq!(
9480            decide_action(&t, &done_q, &PHRASES_EN, never),
9481            ActionDecision::Done
9482        );
9483        t.mark_action_applied(&done_q.id);
9484        assert_eq!(
9485            decide_action(&t, &done_q, &PHRASES_EN, never),
9486            ActionDecision::Skip
9487        );
9488
9489        // A plain answer (no action for that label) does nothing.
9490        let mut plain = action_question("r1", ask::ChoiceAction::Done);
9491        plain.actions.clear();
9492        assert_eq!(
9493            decide_action(&held_task_with("r1"), &plain, &PHRASES_EN, never),
9494            ActionDecision::Skip
9495        );
9496        // A running task is left alone.
9497        let mut running = held_task_with("r1");
9498        running.status = TaskStatus::Running;
9499        assert_eq!(
9500            decide_action(
9501                &running,
9502                &action_question("r1", ask::ChoiceAction::Done),
9503                &PHRASES_EN,
9504                never
9505            ),
9506            ActionDecision::Skip
9507        );
9508    }
9509
9510    #[test]
9511    fn merge_repair_rejected_after_manual_hold_keeps_the_conductor_guard() {
9512        crate::run::pin_test_home();
9513        for legacy in [false, true] {
9514            let dir = tempfile::tempdir().unwrap();
9515            let queue = Queue::at(dir.path().join("queue"));
9516            let questions = Questions::at(dir.path().join("questions"));
9517            let run = "20260101-000000-hold";
9518            let mut task = held_task_with(run);
9519            task.status = TaskStatus::Parked;
9520            let mut q = ask::Question::new(
9521                run.into(),
9522                land::APPROVAL_NODE.into(),
9523                "land".into(),
9524                "Repair?".into(),
9525                String::new(),
9526                vec![land::REPAIR.into()],
9527            );
9528            q.actions.insert(land::REPAIR.into(), resume_action(run));
9529            q.answer(ask::Answer::Choice(land::REPAIR.into())).unwrap();
9530            // Deterministically order the answer before the actual manual hold.
9531            q.asked_at = "2026-01-01T00:00:00Z".parse().unwrap();
9532            q.answered_at = Some("2026-01-01T00:00:01Z".parse().unwrap());
9533            questions.put(&mut q).unwrap();
9534            task.hold_manual(Some("Keep this PR paused".into()));
9535            if legacy {
9536                task.hold_source = None;
9537            }
9538            queue.put(&mut task).unwrap();
9539
9540            apply_choice_actions(&queue, &questions, dir.path());
9541            let after = queue.get(&task.id).unwrap();
9542            assert!(after.operator_held());
9543            assert_eq!(after.hold_source, task.hold_source);
9544            assert_eq!(after.operator_decided_at, task.operator_decided_at);
9545            assert_eq!(after.runs, task.runs);
9546            assert_eq!(after.instruction, task.instruction);
9547            assert!(after.resume_override.is_none());
9548            assert!(after.action_applied(&q.id));
9549            let reason = after.hold_reason.as_deref().unwrap();
9550            assert!(reason.starts_with("Keep this PR paused"));
9551            assert!(reason.contains("later operator decision"));
9552            assert!(reason.contains(&format!("magi task release {}", task.id)));
9553
9554            for recovery in [
9555                crate::conduct::Recovery::Review,
9556                crate::conduct::Recovery::Requeue,
9557            ] {
9558                crate::conduct::apply(
9559                    &queue,
9560                    &questions,
9561                    &crate::conduct::Verdict {
9562                        decisions: vec![crate::conduct::Decision {
9563                            id: task.id.clone(),
9564                            recovery: Some(recovery),
9565                            ..Default::default()
9566                        }],
9567                    },
9568                )
9569                .unwrap();
9570            }
9571            // Reconciliation is idempotent, and automatic recovery cannot
9572            // reopen this owner after it observes the rejected repair answer.
9573            apply_choice_actions(&queue, &questions, dir.path());
9574            let still_held = queue.get(&task.id).unwrap();
9575            assert!(still_held.operator_held());
9576            assert_eq!(still_held.hold_reason, after.hold_reason);
9577            assert_eq!(still_held.runs, task.runs);
9578            assert!(still_held.review_branch.is_none());
9579            assert!(!still_held.fresh_start);
9580            assert!(queue.next_runnable().is_none());
9581        }
9582    }
9583
9584    #[test]
9585    fn apply_choice_actions_releases_the_task_pinned_to_its_run_and_only_once() {
9586        let dir = tempfile::tempdir().unwrap();
9587        let queue = Queue::at(dir.path().join("queue"));
9588        let questions = Questions::at(dir.path().join("questions"));
9589        let home = dir.path().join("home");
9590        let mut state = run_state(RunStatus::Blocked);
9591        state.id = "20260101-000000-act1".to_owned();
9592        state.save_under(&home).unwrap();
9593
9594        let mut t = held_task_with(&state.id);
9595        queue.put(&mut t).unwrap();
9596        let mut q = action_question(&state.id, resume_action(&state.id));
9597        questions.put(&mut q).unwrap();
9598
9599        apply_choice_actions(&queue, &questions, &home);
9600        let after = queue.get(&t.id).unwrap();
9601        assert_eq!(after.status, TaskStatus::Queued);
9602        assert!(!after.fresh_start);
9603        assert!(after.action_applied(&q.id));
9604        let pin = after.resume_override.clone().unwrap();
9605        assert_eq!(pin.pinned_run.as_deref(), Some(state.id.as_str()));
9606        assert!(pin.forced);
9607
9608        // Held again later: the same answer does not release it a second time.
9609        let mut again = queue.get(&t.id).unwrap();
9610        again.hold_machine(Some("later".into()));
9611        queue.put(&mut again).unwrap();
9612        apply_choice_actions(&queue, &questions, &home);
9613        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
9614    }
9615
9616    #[test]
9617    fn a_delivered_answer_is_still_acted_on_exactly_once() {
9618        let dir = tempfile::tempdir().unwrap();
9619        let queue = Queue::at(dir.path().join("queue"));
9620        let questions = Questions::at(dir.path().join("questions"));
9621        let home = dir.path().join("home");
9622        let mut state = run_state(RunStatus::Blocked);
9623        state.id = "20260101-000000-act2".to_owned();
9624        state.save_under(&home).unwrap();
9625
9626        let mut t = held_task_with(&state.id);
9627        queue.put(&mut t).unwrap();
9628        let mut q = action_question(&state.id, resume_action(&state.id));
9629        q.answer_delivered = true;
9630        questions.put(&mut q).unwrap();
9631
9632        // Delivered, and the agent then died: the task is still held, so the
9633        // chosen action applies.
9634        apply_choice_actions(&queue, &questions, &home);
9635        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Queued);
9636        assert!(queue.get(&t.id).unwrap().action_applied(&q.id));
9637
9638        // Held again: the same answer does nothing a second time.
9639        let mut again = queue.get(&t.id).unwrap();
9640        again.hold_machine(Some("later".into()));
9641        queue.put(&mut again).unwrap();
9642        apply_choice_actions(&queue, &questions, &home);
9643        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
9644    }
9645
9646    #[test]
9647    fn a_fresh_asker_defers_the_action_until_it_goes_quiet() {
9648        let dir = tempfile::tempdir().unwrap();
9649        let queue = Queue::at(dir.path().join("queue"));
9650        let questions = Questions::at(dir.path().join("questions"));
9651        let home = dir.path().join("home");
9652        let mut state = run_state(RunStatus::Blocked);
9653        state.id = "20260101-000000-act3".to_owned();
9654        state.save_under(&home).unwrap();
9655        let mut t = held_task_with(&state.id);
9656        queue.put(&mut t).unwrap();
9657        let mut q = action_question(&state.id, ask::ChoiceAction::Requeue);
9658        questions.put(&mut q).unwrap();
9659
9660        questions.beat(&q.id, crate::ask::WaiterKind::Asker);
9661        apply_choice_actions(&queue, &questions, &home);
9662        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
9663
9664        std::fs::remove_file(questions.root().join(format!("{}.lease", q.id))).unwrap();
9665        apply_choice_actions(&queue, &questions, &home);
9666        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Queued);
9667    }
9668
9669    #[test]
9670    fn action_standing_tells_the_reasons_apart() {
9671        let mut t = held_task_with("r1");
9672        let q = action_question("r1", ask::ChoiceAction::Requeue);
9673        assert_eq!(action_standing(&t, &q), ActionStanding::Pending);
9674        let mut plain = q.clone();
9675        plain.actions.clear();
9676        assert_eq!(action_standing(&t, &plain), ActionStanding::NoAction);
9677
9678        // Applied wins over everything, even a running task.
9679        t.mark_action_applied(&q.id);
9680        assert_eq!(action_standing(&t, &q), ActionStanding::Applied);
9681
9682        // A newer attempt running: the old question is Stale, not Busy.
9683        let mut t = held_task_with("r1");
9684        t.start("r2".to_owned());
9685        assert_eq!(action_standing(&t, &q), ActionStanding::Stale);
9686        // The same question about the latest run just waits.
9687        let q2 = action_question("r2", ask::ChoiceAction::Requeue);
9688        assert_eq!(action_standing(&t, &q2), ActionStanding::Busy);
9689        t.status = TaskStatus::Blocked;
9690        assert_eq!(action_standing(&t, &q2), ActionStanding::Busy);
9691
9692        // A conductor question is keyed by the task, never stale.
9693        let mut c = action_question("r0", ask::ChoiceAction::Requeue);
9694        c.node = crate::conduct::NODE.to_owned();
9695        assert_eq!(action_standing(&t, &c), ActionStanding::Busy);
9696        assert!(!ActionStanding::Busy.daemon_owns());
9697    }
9698
9699    #[test]
9700    fn a_running_task_waits_and_a_dead_asker_with_a_cwd_is_still_actioned() {
9701        let dir = tempfile::tempdir().unwrap();
9702        let queue = Queue::at(dir.path().join("queue"));
9703        let questions = Questions::at(dir.path().join("questions"));
9704        let home = dir.path().join("home");
9705        let mut state = run_state(RunStatus::Blocked);
9706        state.id = "20260101-000000-act4".to_owned();
9707        state.save_under(&home).unwrap();
9708        let mut t = held_task_with(&state.id);
9709        t.status = TaskStatus::Running;
9710        queue.put(&mut t).unwrap();
9711        let mut q = action_question(&state.id, ask::ChoiceAction::Done);
9712        q.cwd = Some(dir.path().display().to_string());
9713        questions.put(&mut q).unwrap();
9714
9715        // While the task runs the daemon will not act, so the waiter still
9716        // delivers the word to the dead asker's seat ...
9717        let running = queue.get(&t.id).unwrap();
9718        assert_eq!(action_standing(&running, &q), ActionStanding::Busy);
9719        assert!(matches!(
9720            crate::waiter::decide_owned(&q, None, false, 86_400, Timestamp::now(), false),
9721            crate::waiter::Action::Deliver(_)
9722        ));
9723        // ... and the daemon waits while the task runs, then applies once.
9724        apply_choice_actions(&queue, &questions, &home);
9725        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Running);
9726        let mut back = queue.get(&t.id).unwrap();
9727        back.hold_machine(Some("later".into()));
9728        queue.put(&mut back).unwrap();
9729        apply_choice_actions(&queue, &questions, &home);
9730        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Done);
9731        let mut again = queue.get(&t.id).unwrap();
9732        again.hold_machine(Some("again".into()));
9733        queue.put(&mut again).unwrap();
9734        apply_choice_actions(&queue, &questions, &home);
9735        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
9736    }
9737}