Skip to main content

magi/
daemon.rs

1//! The unattended scheduler and independently leased task workers.
2//!
3//! The scheduler chooses tasks and execution lanes; a detached worker owns each
4//! queue claim, graph driver and outcome settlement. Replacing the scheduler
5//! reconstructs occupied slots from live worker jobs, without touching their
6//! attempts, passes or paid agent calls. Finite maintenance jobs follow the same
7//! lifetime rule. Explicit parking is durable control, never an upgrade step.
8//!
9//! # A crashed worker is legible
10//!
11//! A task is written `Running`, with its run id, before graph execution. The
12//! worker keeps its claim until the outcome is recorded. Recovery requires both
13//! no live worker reservation and an obtainable task claim, and settles the
14//! saved run through the same policy as a live finish. A scheduler's missing
15//! heartbeat alone is never evidence that a run died.
16//!
17//! # Retries are bounded
18//!
19//! Every attempt at a task consumes one of [`Opts::max_attempts`], after which
20//! the task is [`crate::queue::TaskStatus::Held`] for a human. The one
21//! exception is a run that ended `Stalled`: the panel collapsed because the
22//! agent CLIs hit their quota, which is a fact about the machine and not about
23//! the task, so it must not spend an attempt. Without that exception a quota
24//! outage would quietly hold the entire backlog, and the operator would come
25//! back to a reset quota and nothing left that the loop is willing to run.
26
27use std::path::{Path, PathBuf};
28use std::sync::Arc;
29use std::sync::atomic::{AtomicBool, Ordering};
30use std::sync::{Mutex, MutexGuard};
31use std::time::Duration;
32
33use anyhow::{Context, Result, bail};
34use jiff::Timestamp;
35use serde::{Deserialize, Serialize};
36use tokio::sync::Notify;
37
38use crate::ask::{self, Questions};
39use crate::clean;
40use crate::conduct::Conductor;
41use crate::config::{Config, MergeMode};
42use crate::graph::Runner;
43use crate::land;
44use crate::notices::{self, Link, Notice};
45use crate::queue::{Queue, Task, TaskStatus};
46use crate::run::{Liveness, QuotaLoss, RunState, RunStatus};
47use crate::triage;
48
49/// On-disk format for [`Status`]. Bumped when a field's meaning changes.
50pub const SCHEMA: u32 = 1;
51
52/// How often the status file is refreshed. A reader treats a status file older
53/// than [`STALE_SECS`] as "no daemon", so the heartbeat has to be brisk enough
54/// that a busy daemon is never mistaken for a dead one.
55pub const HEARTBEAT: Duration = Duration::from_secs(5);
56
57/// How old a heartbeat may be before a reader calls the daemon dead. Six
58/// missed beats: long enough to survive a slow filesystem, short enough that
59/// a crashed daemon is not still reported as running a task.
60///
61/// The single threshold every reader shares — the web UI's `/api/health` and
62/// `magi doctor` both call [`Reading::running`] rather than each comparing
63/// against their own copy of this number, so a crashed daemon cannot look
64/// alive on one screen and dead on another.
65pub const STALE_SECS: i64 = 30;
66
67/// Default queue poll interval.
68pub const POLL: Duration = Duration::from_secs(5);
69
70/// How old a claim has to be before startup sweeps it. Longer than any run
71/// this graph plausibly takes, so a sweep cannot pull a task out from under a
72/// daemon that is merely slow.
73pub const STALE_CLAIM: Duration = Duration::from_secs(6 * 60 * 60);
74
75/// How long a task may sit [`TaskStatus::Running`] with no live daemon's
76/// heartbeat naming it before [`crate::conduct`] is shown it as stalled.
77///
78/// [`reclaim_orphaned_running`] settles most crashes immediately, on every
79/// poll, by attempting the task's own claim: a dead pid is proof enough for
80/// [`sweep_stale_claims`] to drop the lock the same tick, and the very next
81/// claim attempt succeeds. But a lock whose pid cannot be parsed at all — an
82/// empty or corrupt `.lock` file — falls back to [`STALE_CLAIM`]'s six-hour
83/// age instead, since there is nothing else to check (see
84/// [`sweep_stale_claims`]'s own doc). For as long as that lock survives, the
85/// claim keeps failing and `reclaim_orphaned_running` correctly leaves the
86/// task `running` — see
87/// `stalled_tasks_still_reaches_a_task_reclaim_could_not_claim_yet` for
88/// exactly this ordering. `stalled_tasks` is what surfaces that task to the
89/// conductor well before the mechanical six-hour sweep would, and thirty
90/// minutes is comfortably below `STALE_CLAIM` while still being generous
91/// enough that a task merely late to publish its first [`HEARTBEAT`] is
92/// never mistaken for abandoned.
93pub const STALLED_RUNNING: Duration = Duration::from_secs(30 * 60);
94
95/// What the loop is working on, for the status file.
96#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
97#[serde(default)]
98pub struct Current {
99    /// Task id being run.
100    pub task: String,
101    /// Run id the task produced.
102    pub run: String,
103}
104
105/// The daemon's liveness, published to `<home>/daemon.json`.
106///
107/// This is the only interface between the loop and the web UI, which is why it
108/// carries `updated_at` as well as `started_at`: a reader cannot tell a
109/// running daemon from a `SIGKILL`ed one by the file's existence alone, but it
110/// can compare the heartbeat against the clock.
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct Status {
113    /// On-disk format version.
114    pub schema: u32,
115    /// Process id, so a human can find or kill the daemon.
116    pub pid: u32,
117    /// When this process started.
118    pub started_at: Timestamp,
119    /// Last heartbeat.
120    pub updated_at: Timestamp,
121    /// True when the queue has nothing runnable.
122    pub idle: bool,
123    /// Every task and run currently in flight. More than one entry means the
124    /// loop is driving more than one run at once — see
125    /// [`crate::config::Daemon::max_concurrent_runs`]. Empty, not absent, when
126    /// nothing is running, so a reader never has to treat "no field" and "an
127    /// empty list" as two different kinds of idle.
128    pub current: Vec<Current>,
129    /// Tasks that reached a terminal status in this process.
130    pub completed: usize,
131    /// Queue polls since start, so a wedged loop shows up as a frozen count.
132    pub polls: u64,
133    /// Effective startup capabilities of the question recovery loops.
134    #[serde(default)]
135    pub question_recovery: Option<crate::question_delivery::Recovery>,
136    /// Independent-worker control protocol supported by this scheduler.
137    #[serde(default)]
138    pub worker_protocol: Option<u32>,
139}
140
141impl Status {
142    /// A fresh, idle status for this process.
143    #[must_use]
144    pub fn new() -> Self {
145        let now = Timestamp::now();
146        Self {
147            schema: SCHEMA,
148            pid: std::process::id(),
149            started_at: now,
150            updated_at: now,
151            idle: true,
152            current: Vec::new(),
153            completed: 0,
154            polls: 0,
155            question_recovery: None,
156            worker_protocol: Some(1),
157        }
158    }
159}
160
161impl Default for Status {
162    fn default() -> Self {
163        Self::new()
164    }
165}
166
167/// How the loop should behave.
168#[derive(Debug, Clone, Serialize, Deserialize)]
169#[serde(default)]
170pub struct Opts {
171    /// Repository used by tasks that name none.
172    pub repo: PathBuf,
173    /// Explicit `magi.toml`, instead of the discovered layer stack.
174    pub config: Option<PathBuf>,
175    /// Queue poll interval.
176    pub poll: Duration,
177    /// Attempts a task gets before it is held for a human.
178    pub max_attempts: usize,
179    /// Drain what is runnable now, then return, instead of waiting for more.
180    pub once: bool,
181    /// Merge mode override (`none`, `local`, `pr`); `None` keeps the config's.
182    pub merge: Option<String>,
183    /// Where the janitor's [`crate::clean::fold_orphaned_worktrees`] and
184    /// [`crate::git::worktree_prune`] look for and reclaim worktrees.
185    /// `None` resolves to [`crate::run::default_worktree_root`] - the
186    /// operator's real `~/wt/<repo>` - the same way a run with no
187    /// [`crate::config::Graph::worktree_root`] resolves its own. A caller
188    /// that does not own that directory (a test, an embedding that manages
189    /// worktrees itself) must set this, or every idle tick reclaims worktrees
190    /// out from under whoever actually does.
191    pub worktrees_root: Option<PathBuf>,
192}
193
194impl Default for Opts {
195    fn default() -> Self {
196        Self {
197            repo: PathBuf::from("."),
198            config: None,
199            poll: POLL,
200            max_attempts: 2,
201            once: false,
202            merge: None,
203            worktrees_root: None,
204        }
205    }
206}
207
208impl Opts {
209    fn absolute(&self) -> Result<Self> {
210        let mut opts = self.clone();
211        opts.repo = std::path::absolute(&opts.repo)?;
212        opts.config = opts
213            .config
214            .as_deref()
215            .map(std::path::absolute)
216            .transpose()?;
217        opts.worktrees_root = opts
218            .worktrees_root
219            .as_deref()
220            .map(std::path::absolute)
221            .transpose()?;
222        Ok(opts)
223    }
224}
225
226/// How many runs a plain `usize` from config may drive concurrently, floored
227/// at one. A `0` in a config file would otherwise stall the loop entirely -
228/// no runnable task could ever start - which is never what an operator who
229/// wrote `0` meant.
230fn max_concurrent(n: usize) -> usize {
231    n.max(1)
232}
233
234/// Where the status file lives.
235#[must_use]
236pub fn status_path() -> PathBuf {
237    crate::run::home().join("daemon.json")
238}
239
240/// Publish the status file for this process.
241pub fn write_status(status: &Status) -> Result<()> {
242    write_status_to(&status_path(), status)
243}
244
245/// Publish a status to an explicit path.
246///
247/// Written to a sibling `.tmp` and renamed, because the web UI reads this file
248/// on every health poll and must never see a half-written one.
249pub fn write_status_to(path: &Path, status: &Status) -> Result<()> {
250    if let Some(parent) = path.parent() {
251        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
252    }
253    let body = serde_json::to_string_pretty(status).context("serialize daemon status")?;
254    let tmp = path.with_extension("json.tmp");
255    std::fs::write(&tmp, &body).with_context(|| format!("write {}", tmp.display()))?;
256    std::fs::rename(&tmp, path).with_context(|| format!("replace {}", path.display()))?;
257    Ok(())
258}
259
260/// Delete the status file. Called on the way out so a clean exit reads as
261/// "no daemon" rather than as a daemon whose heartbeat merely stopped.
262pub fn clear_status() {
263    clear_status_at(&status_path());
264}
265
266/// Delete a status file at an explicit path, so the loop's teardown and
267/// [`clear_status`] cannot drift apart: the loop is handed the path it
268/// published to, and a test can watch a temp file disappear.
269fn clear_status_at(path: &Path) {
270    let _ = std::fs::remove_file(path);
271}
272
273/// A cooperative stop, shared with whoever asked the loop to run.
274///
275/// Cloning is how the request travels: [`serve_until`] keeps one handle, the
276/// Ctrl-C listener and the web UI keep others, and every clone points at the
277/// same flag. There is no channel because there is nothing to send — the only
278/// message is "stop", it is idempotent, and a flag cannot be missed by a
279/// receiver that was not listening yet.
280///
281/// A stop ends scheduler dispatch promptly. Its busy snapshot is observational:
282/// detached task workers keep their own lifetimes and ownership after this
283/// handle's scheduler exits.
284#[derive(Debug, Clone, Default)]
285pub struct Stop {
286    /// Set once, never cleared: a stop is not something an operator takes back
287    /// half way through, and a clearable flag would let a start racing a stop
288    /// resurrect a loop that is already unwinding.
289    stopped: Arc<AtomicBool>,
290    /// Live task workers observed by the scheduler's latest dispatch snapshot.
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    /// Whether a requested scheduler retirement still has an active snapshot.
325    /// Workers continue independently after the scheduler has retired.
326    #[must_use]
327    pub fn finishing(&self) -> bool {
328        self.stopped() && self.busy_now()
329    }
330
331    /// Ask the loop to stop *and* the run in flight to park at its next node
332    /// boundary.
333    ///
334    /// Unlike plain stop, explicit park asks workers to checkpoint at their
335    /// existing safe boundaries. Binary replacement never needs a park.
336    pub fn park(&self, cause: crate::run::ParkCause) {
337        self.pause.park_for(cause);
338        self.stop();
339    }
340
341    /// Has a park been asked for?
342    #[must_use]
343    pub fn parking(&self) -> bool {
344        self.pause.parked()
345    }
346
347    /// The pause handle to give a runner.
348    #[must_use]
349    pub fn pause(&self) -> crate::graph::Pause {
350        self.pause.clone()
351    }
352
353    /// Whether the scheduler's latest snapshot contains active task workers.
354    #[must_use]
355    pub fn busy_now(&self) -> bool {
356        self.busy.load(Ordering::SeqCst) > 0
357    }
358
359    /// Wait out one poll interval, returning early once a stop is asked for.
360    async fn idle(&self, poll: Duration) {
361        tokio::select! {
362            () = tokio::time::sleep(poll) => {}
363            () = self.wake.notified() => {}
364        }
365    }
366}
367
368/// The daemon's published state, read permissively.
369///
370/// This mirrors [`Status`], but is a separate declaration on purpose: every
371/// field defaults, so a status file from an older or newer magi still yields
372/// a usable reading — one this build has never heard of — instead of a parse
373/// error that hides the daemon entirely.
374#[derive(Debug, Clone, Default, Deserialize)]
375#[serde(default)]
376pub struct Reading {
377    /// Format version the daemon claims.
378    pub schema: u32,
379    /// Daemon process id, for an operator who wants to stop it.
380    pub pid: Option<u32>,
381    /// When that process started.
382    pub started_at: Option<Timestamp>,
383    /// Last heartbeat. Absent means the file is unusable, hence not running.
384    pub updated_at: Option<Timestamp>,
385    /// True when the queue had nothing runnable at the last poll.
386    pub idle: bool,
387    /// What the daemon is working on. Empty means idle; more than one entry
388    /// means more than one run is in flight at once.
389    ///
390    /// `deserialize_with` rather than the plain derive: a daemon started
391    /// before this field became a list is still out there writing the old
392    /// shape — a single `{"task":...,"run":...}` object, or its absence —
393    /// on every heartbeat until it is restarted, and a live process reading
394    /// that file during the rollout must still see it as running rather than
395    /// as absent. A bare type change here would fail the whole struct's
396    /// deserialization on a type mismatch, defeating the permissiveness this
397    /// type exists for.
398    #[serde(deserialize_with = "de_current")]
399    pub current: Vec<Current>,
400    /// Tasks this daemon process has finished.
401    pub completed: u64,
402    /// Queue polls this daemon process has made.
403    pub polls: u64,
404    /// Effective startup capabilities of the question recovery loops.
405    #[serde(default)]
406    pub question_recovery: Option<crate::question_delivery::Recovery>,
407    /// Independent-worker control protocol, absent on legacy embedded loops.
408    pub worker_protocol: Option<u32>,
409}
410
411/// Accept the old single-`Current`-or-absent shape as well as the current
412/// list, so a reader never has to know which build wrote the file.
413fn de_current<'de, D>(deserializer: D) -> std::result::Result<Vec<Current>, D::Error>
414where
415    D: serde::Deserializer<'de>,
416{
417    #[derive(Deserialize)]
418    #[serde(untagged)]
419    enum Shape {
420        Many(Vec<Current>),
421        One(Current),
422    }
423    Ok(
424        Option::<Shape>::deserialize(deserializer)?.map_or_else(Vec::new, |shape| match shape {
425            Shape::Many(v) => v,
426            Shape::One(c) => vec![c],
427        }),
428    )
429}
430
431impl Reading {
432    /// Seconds since the last heartbeat, or `None` when there has never been
433    /// one.
434    #[must_use]
435    pub fn age_secs(&self, now: Timestamp) -> Option<i64> {
436        self.updated_at
437            .map(|at| (now.as_second() - at.as_second()).max(0))
438    }
439
440    /// Whether the loop counts as running: a heartbeat no older than
441    /// [`STALE_SECS`]. The alternative is a reader that claims a task is in
442    /// progress hours after the daemon that owned it was killed.
443    #[must_use]
444    pub fn running(&self, now: Timestamp) -> bool {
445        self.age_secs(now).is_some_and(|secs| secs <= STALE_SECS)
446    }
447}
448
449/// Read `<home>/daemon.json` permissively, or `None` when there is nothing
450/// usable there.
451///
452/// Missing, half-written and unparseable all collapse to `None`, because the
453/// only question a reader asks is whether a daemon is alive, and a file it
454/// cannot read is not evidence that one is.
455#[must_use]
456pub fn read_status(home: &Path) -> Option<Reading> {
457    let body = std::fs::read_to_string(home.join("daemon.json")).ok()?;
458    serde_json::from_str(&body).ok()
459}
460
461/// Read-only evidence of runs owned by a live daemon or independent worker.
462///
463/// CLI, sandboxed conversation controls and Web cleanup share this observation.
464/// A stale daemon heartbeat does not invalidate a worker's OS-held lease.
465/// Starting reservations and unknown worker state remain protected; observing
466/// them never creates, reconciles or archives files in the worker store.
467/// Callers interested in one identity use [`is_working_on`] or
468/// [`is_working_on_task`].
469#[must_use]
470pub fn current_work(home: &Path, now: Timestamp) -> Vec<Current> {
471    let mut current = read_status(home)
472        .filter(|reading| reading.running(now))
473        .map(|reading| reading.current)
474        .unwrap_or_default();
475    let workers = crate::worker::observe_active(home).and_then(|jobs| {
476        jobs.into_iter()
477            .filter(|job| job.kind == "task")
478            .map(|job| {
479                let request: TaskWorker = serde_json::from_value(job.payload)?;
480                let run = Queue::at(request.queue)
481                    .get(&request.task)
482                    .ok()
483                    .and_then(|task| task.runs.last().cloned())
484                    .unwrap_or_default();
485                Ok(Current {
486                    task: request.task,
487                    run,
488                })
489            })
490            .collect::<Result<Vec<_>>>()
491    });
492    match workers {
493        Ok(workers) => {
494            for worker in workers {
495                if let Some(existing) = current.iter_mut().find(|c| c.task == worker.task) {
496                    *existing = worker;
497                } else {
498                    current.push(worker);
499                }
500            }
501        }
502        Err(error) => {
503            // This API gates destructive cleanup. Unknown worker ownership
504            // protects every known task/run instead of pretending it is idle.
505            tracing::error!("cannot determine active workers: {error:#}");
506            current.extend(
507                Queue::at(home.join("queue"))
508                    .list()
509                    .into_iter()
510                    .map(|task| Current {
511                        task: task.id,
512                        run: task.runs.last().cloned().unwrap_or_default(),
513                    }),
514            );
515            current.extend(
516                crate::run::list_ids_in(&home.join("runs"))
517                    .into_iter()
518                    .map(|run| Current {
519                        task: String::new(),
520                        run,
521                    }),
522            );
523        }
524    }
525    current
526}
527
528/// Whether a live daemon is working on this run at this moment.
529#[must_use]
530pub fn is_working_on(home: &Path, run: &str, now: Timestamp) -> bool {
531    current_work(home, now).iter().any(|c| c.run == run)
532}
533
534/// Whether a live daemon is working on a run whose short id is this one.
535///
536/// For a worktree that has no run record to compare against at all -
537/// [`crate::clean::fold_orphaned_worktrees`]'s whole reason to exist - a full
538/// id is not available to hand to [`is_working_on`]. The short id is: a run's
539/// worktree bay is named after it (see [`crate::run::RunState::worktree_root`]),
540/// and it is exactly the gap between the daemon claiming a task and
541/// `RunState::new` saving the first `run.json` that this exists to protect -
542/// a run genuinely in flight but invisible to a scan of `runs/`.
543#[must_use]
544pub fn is_working_on_short(home: &Path, short: &str, now: Timestamp) -> bool {
545    current_work(home, now)
546        .iter()
547        .any(|c| crate::run::short_of(&c.run) == short)
548}
549
550/// Whether a live daemon is working on this task at this moment.
551#[must_use]
552pub fn is_working_on_task(home: &Path, task: &str, now: Timestamp) -> bool {
553    current_work(home, now).iter().any(|c| c.task == task)
554}
555
556/// Remove claim files whose owner is provably dead, or that have simply
557/// outlived `older_than`, and return the task ids swept.
558///
559/// A daemon killed with `SIGKILL` never runs [`crate::queue::Claim`]'s
560/// destructor, and the orphaned `.lock` file would make its task permanently
561/// unclaimable — the backlog would stop for good at exactly the task that was
562/// in flight when the machine went down.
563///
564/// The pid recorded in the lock is the authority whenever it can be read at
565/// all; age is only a fallback for when it cannot be.
566///
567/// - **A parseable pid wins outright.** [`crate::proc::pid_alive`] decides,
568///   full stop — dead sweeps the lock immediately, regardless of age; alive
569///   protects it, regardless of age. This is what lets a lock be reclaimed in
570///   seconds instead of waiting out [`STALE_CLAIM`]: a lock made 33 minutes
571///   before this daemon even started, next to a `queued` task, no longer has
572///   to sit for six hours before anything notices its owner is gone.
573/// - **A pid that cannot be parsed at all** — an empty or corrupt lock file —
574///   falls back to `older_than`, since there is nothing else to check.
575///
576/// Age must never override a *positive* liveness confirmation. `sweep`
577/// polls concurrently with independently running task workers. A worker may
578/// legitimately run longer than `older_than`; its live pid must still protect
579/// its claim. Age alone must never turn that claim into a second task attempt,
580/// which [`reclaim_orphaned_running`] would then read as abandoned and hand
581/// to a second attempt — two `Runner`s writing the same `run.json` and the
582/// same worktree at once. `pid_alive` answering "alive" for anything it
583/// cannot determine (a live process, a pid this build cannot check, one
584/// under another account) is exactly what keeps that path from ever
585/// firing on a guess.
586///
587/// [`STALE_CLAIM`] itself stays large: a helper program missing or its
588/// output unreadable must not be license to guess, and the risk of an
589/// unparseable lock outliving a genuinely dead owner is bounded by an order
590/// of magnitude above any plausible run rather than by a positive check.
591///
592/// Runs on every poll, not only at startup — a daemon up for days must keep
593/// noticing a lock some other, now-dead, daemon left behind just as readily
594/// as one it trips over on the way up.
595pub fn sweep_stale_claims(queue: &Queue, older_than: Duration) -> Vec<String> {
596    sweep_stale_claims_with(queue, older_than, crate::proc::pid_alive)
597}
598
599/// [`sweep_stale_claims`] with its process-query boundary supplied by the
600/// caller. This keeps the lock policy testable where process listing is
601/// unavailable, while production still uses the platform query above.
602fn sweep_stale_claims_with<F>(queue: &Queue, older_than: Duration, pid_alive: F) -> Vec<String>
603where
604    F: Fn(u32) -> bool,
605{
606    let this_process = std::process::id();
607    let mut swept: Vec<String> = std::fs::read_dir(queue.root())
608        .into_iter()
609        .flatten()
610        .flatten()
611        .map(|e| e.path())
612        .filter(|p| p.extension().is_some_and(|x| x == "lock"))
613        .filter(|p| {
614            match std::fs::read_to_string(p)
615                .ok()
616                .and_then(|body| body.trim().parse::<u32>().ok())
617            {
618                // This process wrote it and is asking the question right
619                // now, so it is definitionally still alive - settled without
620                // spawning a helper process at all.
621                Some(pid) if pid == this_process => false,
622                Some(pid) => !pid_alive(pid),
623                None => p
624                    .metadata()
625                    .and_then(|m| m.modified())
626                    .and_then(|t| t.elapsed().map_err(std::io::Error::other))
627                    .is_ok_and(|age| age >= older_than),
628            }
629        })
630        .filter(|p| std::fs::remove_file(p).is_ok())
631        .filter_map(|p| {
632            p.file_stem()
633                .and_then(|s| s.to_str())
634                .map(std::borrow::ToOwned::to_owned)
635        })
636        .collect();
637    swept.sort_unstable();
638    swept
639}
640
641/// Is `task` stalled: [`TaskStatus::Running`], past [`STALLED_RUNNING`], with
642/// no live daemon's heartbeat naming it? Deterministic — no model call, and
643/// the exact test [`stalled_tasks`] uses to decide what `crate::conduct` is
644/// shown.
645fn is_stalled(task: &Task, home: &Path, now: Timestamp) -> bool {
646    task.status == TaskStatus::Running
647        && (now.as_second() - task.updated_at.as_second()) >= STALLED_RUNNING.as_secs() as i64
648        && !is_working_on_task(home, &task.id, now)
649}
650
651/// Every task [`is_stalled`] right now — "止まったタスク" in
652/// `crate::conduct`'s vocabulary.
653fn stalled_tasks(queue: &Queue, home: &Path, now: Timestamp) -> Vec<Task> {
654    queue
655        .list()
656        .into_iter()
657        .filter(|t| is_stalled(t, home, now))
658        .collect()
659}
660
661/// Runnable tasks a dependency can still be set on — "runnable なタスク" in
662/// `crate::conduct`'s vocabulary. Deliberately `Queued` only, not
663/// `Failed`-and-so-also-runnable: a task that already attempted and lost
664/// belongs in [`finished_tasks`], where the question is a recovery, not a
665/// dependency.
666fn queued_tasks(queue: &Queue) -> Vec<Task> {
667    queue
668        .list()
669        .into_iter()
670        .filter(|t| t.status == TaskStatus::Queued)
671        .collect()
672}
673
674/// `Failed`/`Held` tasks nobody has decided a recovery for yet — "終わった
675/// タスク" in `crate::conduct`'s vocabulary.
676fn finished_tasks(queue: &Queue) -> Vec<Task> {
677    queue
678        .list()
679        .into_iter()
680        .filter(|t| matches!(t.status, TaskStatus::Failed | TaskStatus::Held))
681        .collect()
682}
683
684/// Deterministically resolve `Task::blocked_by`: a dependency task that
685/// reached `Done`, or a question that was answered, is removed — no model
686/// involved, on every poll. An answered question's content is copied onto
687/// the task ([`Task::record_answer`]) before its id is dropped, so it
688/// reaches the next `crate::conduct` prompt and the next run's instruction
689/// (see [`instruction_for`]) rather than only clearing the block.
690///
691/// A `blocked_by` id that names neither an existing task nor an existing
692/// question - `magi task rm` (or an operator by hand) deleted it while this
693/// task was still waiting - is caught before any of that: it can never
694/// become `Done` or `Answered`, so the ordinary loop below would otherwise
695/// leave the task `blocked` forever with nothing to notice. Such a task is
696/// quarantined to a machine hold instead ([`crate::queue::missing_blocker_hold_reason`]),
697/// which puts it in front of `crate::triage::run_once`'s own walk the next
698/// time it runs - see that module's doc for why the choice is "ask a human",
699/// never "assume the missing dependency was satisfied and unblock anyway".
700fn resolve_blockers(queue: &Queue, questions: &Questions) {
701    for listed in queue.list() {
702        if listed.status != TaskStatus::Blocked || listed.blocked_by.is_empty() {
703            continue;
704        }
705        let Ok(_claim) = queue.claim(&listed.id) else {
706            continue;
707        };
708        let Ok(mut task) = queue.get(&listed.id) else {
709            continue;
710        };
711        if task.status != TaskStatus::Blocked {
712            continue;
713        }
714        // A dependency deleted on purpose is released, not held - and before
715        // the missing check, so a pass that races the delete (or a claim that
716        // made `Queue::remove` skip this task) settles it quietly.
717        let deleted = queue.apply_deleted_blockers(&mut task, questions);
718        if !deleted.is_empty() {
719            record(queue, &mut task);
720            for id in &deleted {
721                queue.note_dependency_deleted(&task, id);
722            }
723            if task.status != TaskStatus::Blocked {
724                continue;
725            }
726        }
727        let missing = crate::queue::missing_blockers(queue, questions, &task.blocked_by);
728        if !missing.is_empty() {
729            let language = language_of(&task, Path::new("."));
730            let mut cause = missing.clone();
731            cause.sort();
732            cause.dedup();
733            task.notice_cause(&format!("missing-blockers:{}", cause.join(",")), None, None);
734            task.hold_machine(Some(crate::queue::missing_blocker_hold_reason_in(
735                &task.blocked_by,
736                &missing,
737                &language,
738            )));
739            record(queue, &mut task);
740            continue;
741        }
742        // A conductor question nobody answered in time is retired by the
743        // waiter, and nothing else would ever move the task it blocks.
744        if let Some(q) = task.blocked_by.iter().find_map(|id| {
745            let crate::queue::Dependency::Question(full) =
746                crate::queue::dependency(queue, questions, id)
747            else {
748                return None;
749            };
750            questions.get(&full).ok().filter(|q| {
751                q.node == crate::conduct::NODE && q.status == ask::QuestionStatus::Abandoned
752            })
753        }) {
754            let language = language_of(&task, Path::new("."));
755            task.notice_cause(&format!("unanswered-question:{}", q.id), None, None);
756            task.hold_machine(Some(unanswered_question_hold_reason(&q, &language)));
757            record(queue, &mut task);
758            continue;
759        }
760        let mut changed = false;
761        for id in task.blocked_by.clone() {
762            let resolved = crate::queue::dependency(queue, questions, &id);
763            if let crate::queue::Dependency::Task(full) = &resolved {
764                if queue
765                    .get(full)
766                    .is_ok_and(|dep| dep.status == TaskStatus::Done)
767                {
768                    task.unblock(&id);
769                    changed = true;
770                }
771                continue;
772            }
773            if let crate::queue::Dependency::Question(full) = resolved
774                && let Ok(q) = questions.get(&full)
775                && q.status == ask::QuestionStatus::Answered
776            {
777                // The action sweep releases conductor blockers atomically
778                // with their selected operation. A fresh deputy lease must
779                // never leave an answered hold temporarily runnable.
780                if q.node == crate::conduct::NODE
781                    && q.chosen_action().is_some()
782                    && !task.action_applied(&q.id)
783                {
784                    continue;
785                }
786                let answer = match &q.answer {
787                    Some(ask::Answer::Choice(c) | ask::Answer::Text(c)) => c.clone(),
788                    None => String::new(),
789                };
790                task.record_answer(q.summary.clone(), answer);
791                task.unblock(&id);
792                changed = true;
793            }
794        }
795        if changed {
796            record(queue, &mut task);
797        }
798    }
799}
800
801/// The `hold_reason` of a task whose conductor question went unanswered.
802fn unanswered_question_hold_reason(q: &ask::Question, language: &str) -> String {
803    if crate::lang::is_japanese(language) {
804        format!(
805            "質問 {} 「{}」 に期限内の回答がなく、取り下げられました - `magi task triage` を参照",
806            q.short(),
807            q.summary
808        )
809    } else {
810        format!(
811            "question {} \"{}\" went unanswered and was abandoned - see `magi task triage`",
812            q.short(),
813            q.summary
814        )
815    }
816}
817
818/// Where `q`'s chosen action stands for `task`. `daemon_will_act` is false
819/// for several unrelated reasons, and the waiter must not read them alike.
820#[derive(Debug, Clone, Copy, PartialEq, Eq)]
821pub(crate) enum ActionStanding {
822    /// The answer carries no action: the waiter delivers it as before.
823    NoAction,
824    /// The daemon will apply it (not applied, task idle, current attempt).
825    Pending,
826    /// Already applied (`Task::actions_applied`): the daemon has acted, so
827    /// delivering the word to the old seat would run it beside the new work.
828    Applied,
829    /// The question is about an earlier attempt than the task's latest.
830    Stale,
831    /// The task is running / blocked / done: the daemon defers for now.
832    Busy,
833}
834
835impl ActionStanding {
836    /// The daemon has acted or will act: the waiter must not deliver.
837    pub(crate) fn daemon_owns(self) -> bool {
838        matches!(self, Self::Pending | Self::Applied | Self::Stale)
839    }
840}
841
842/// Classify `q`'s chosen action against `task`. `Stale` is judged before
843/// `Busy`: a question about an earlier attempt must be stopped even while a
844/// newer competition is running.
845pub(crate) fn action_standing(task: &Task, q: &ask::Question) -> ActionStanding {
846    if q.chosen_action().is_none() {
847        ActionStanding::NoAction
848    } else if task.action_applied(&q.id) {
849        ActionStanding::Applied
850    } else if q.node != crate::conduct::NODE && task.runs.last() != Some(&q.run) {
851        ActionStanding::Stale
852    } else if matches!(
853        task.status,
854        TaskStatus::Running | TaskStatus::Blocked | TaskStatus::Done
855    ) {
856        ActionStanding::Busy
857    } else {
858        ActionStanding::Pending
859    }
860}
861
862/// What [`decide_action`] concluded about one answered question.
863#[derive(Debug, Clone, PartialEq, Eq)]
864enum ActionDecision {
865    /// Nothing to do now (no action, already applied, or the task is busy).
866    Skip,
867    /// Continue the named run instead of competing again.
868    Resume(String),
869    /// Requeue as a fresh competition.
870    Requeue,
871    /// Hold the task by explicit operator choice.
872    Hold,
873    /// Close the task as done.
874    Done,
875    /// The question is about an earlier attempt than the task's latest; it
876    /// is recorded as handled and changes nothing.
877    Stale,
878    /// The action cannot be carried out; hold the task and say why. Never
879    /// falls back to something else, because the operator chose this one.
880    Refuse(String),
881}
882
883/// Decide what the choice answered on `q` means for `task`. Pure: `load`
884/// reads a run's state, as with [`unfinished_run_with`].
885///
886/// `Resume` only fires on the task's *latest* run, and only while that run
887/// could actually make progress ([`RunStatus::resumable`], not released, review
888/// budget left); an answer naming anything else is refused rather than
889/// reinterpreted as a fresh competition.
890fn decide_action<F>(task: &Task, q: &ask::Question, p: &Phrases, load: F) -> ActionDecision
891where
892    F: FnOnce(&str) -> Result<RunState>,
893{
894    let Some(action) = q.chosen_action() else {
895        return ActionDecision::Skip;
896    };
897    if task.action_applied(&q.id)
898        || matches!(
899            task.status,
900            TaskStatus::Running | TaskStatus::Blocked | TaskStatus::Done
901        )
902    {
903        return ActionDecision::Skip;
904    }
905    // A question about an earlier attempt must not act on a newer one. A
906    // conductor question is keyed by the task id, not a run, so it is exempt.
907    if q.node != crate::conduct::NODE && task.runs.last() != Some(&q.run) {
908        return ActionDecision::Stale;
909    }
910    let repair = q.node == land::APPROVAL_NODE && q.resolution().as_deref() == Some(land::REPAIR);
911    let stopping = q.node == land::APPROVAL_NODE
912        && q.resolution()
913            .as_deref()
914            .is_some_and(crate::merge_decision::stops);
915    // Repair and stop actions are new decisions on an existing approval card. A
916    // manual hold before that answer must not discard it just because the
917    // card predates the hold. Keep the older policy for other choice actions.
918    let decision_at = if repair || stopping {
919        q.answered_at.unwrap_or(q.asked_at)
920    } else {
921        q.asked_at
922    };
923    if task.operator_decided_at.is_some_and(|at| at > decision_at) {
924        return if repair || stopping {
925            ActionDecision::Refuse(format!(
926                "{} answer {} was superseded by a later operator decision; the task stays held. Inspect `magi show {}`; if this action is still wanted, explicitly release the existing owner with `magi task release {}`",
927                if repair { "repair" } else { "stop" },
928                q.id,
929                q.run,
930                task.id
931            ))
932        } else {
933            ActionDecision::Stale
934        };
935    }
936    match action {
937        a if a.maintenance() => ActionDecision::Skip,
938        ask::ChoiceAction::ResumeRun { .. }
939        | ask::ChoiceAction::ReleaseTask { .. }
940        | ask::ChoiceAction::RerunChecks { .. }
941        | ask::ChoiceAction::AbandonRun { .. } => unreachable!(),
942        ask::ChoiceAction::Requeue => ActionDecision::Requeue,
943        ask::ChoiceAction::Hold => ActionDecision::Hold,
944        ask::ChoiceAction::Done => ActionDecision::Done,
945        ask::ChoiceAction::Resume { run } => {
946            if task.runs.last() != Some(run) {
947                return ActionDecision::Refuse((p.resume_not_latest)(
948                    q.short(),
949                    ask::short_id(run),
950                ));
951            }
952            match load(run) {
953                Ok(s) if stopping => {
954                    if s.merge_decision.as_ref().is_some_and(|d| d.closed)
955                        || !s
956                            .land_approval
957                            .as_ref()
958                            .is_some_and(|binding| binding.question == q.id)
959                    {
960                        ActionDecision::Stale
961                    } else if s.liveness(false) == Liveness::Live {
962                        ActionDecision::Skip
963                    } else if s.status == RunStatus::Landing
964                        && !s.released()
965                        && s.retired_to.is_none()
966                    {
967                        ActionDecision::Resume(run.clone())
968                    } else {
969                        ActionDecision::Refuse("Owner stop decision cannot resume its original landing execution; inspect the existing run. No replacement or merge was started.".into())
970                    }
971                }
972                Ok(s)
973                    if q.node == land::APPROVAL_NODE
974                        && q.resolution().as_deref() == Some(land::REPAIR) =>
975                {
976                    if s.merge_repairs.iter().any(|r| r.question == q.id)
977                        || s.land_approval.as_ref().is_some_and(|a| a.question != q.id)
978                    {
979                        // A live/manual driver already consumed or superseded
980                        // this answer. Do not hold its fresh approval wait.
981                        ActionDecision::Stale
982                    } else if s.liveness(false) == Liveness::Live {
983                        ActionDecision::Skip
984                    } else if s.status != RunStatus::Landing
985                        || s.released()
986                        || s.reviews.is_empty()
987                        || !implementers_match(task, &s)
988                        || !s
989                            .land_approval
990                            .as_ref()
991                            .is_some_and(|a| a.question == q.id && !a.head.is_empty())
992                    {
993                        ActionDecision::Refuse(format!(
994                            "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",
995                            q.id, run, run
996                        ))
997                    } else {
998                        ActionDecision::Resume(run.clone())
999                    }
1000                }
1001                Ok(s) if s.liveness(false) == Liveness::Live => {
1002                    ActionDecision::Refuse("run already has a live driver".into())
1003                }
1004                Ok(s) if crate::handover::can_resume(&s) => ActionDecision::Resume(run.clone()),
1005                Ok(_) => ActionDecision::Refuse((p.resume_cannot_progress)(
1006                    q.short(),
1007                    ask::short_id(run),
1008                )),
1009                Err(e) => ActionDecision::Refuse((p.resume_unreadable)(
1010                    q.short(),
1011                    ask::short_id(run),
1012                    &format!("{e:#}"),
1013                )),
1014            }
1015        }
1016    }
1017}
1018
1019/// The fixed prose the daemon writes into [`Task::last_error`] and
1020/// [`Task::hold_reason`], in the language of `[graph] language`. Same shape as
1021/// `triage::Wording` and `land::Words`: only English and Japanese are
1022/// translated, anything else falls back to English.
1023///
1024/// Rendered when the reason is *generated*, so a reason keeps the language it
1025/// was written in if the setting changes later. The notification frame
1026/// (`Task <id> is held:`) is not here and stays English.
1027///
1028/// Deliberately **not** translated, because other code reads them back by
1029/// string: the disk gate prefixes (`triage::is_disk_hold`), `out of attempts`
1030/// and `gate red`. IDs, node names, paths and external error text are embedded
1031/// as they are.
1032struct Phrases {
1033    /// `{}` = status label, then the detail.
1034    graph_stopped: fn(&str, &str) -> String,
1035    quorum_lost: &'static str,
1036    /// Followed by the seats' names.
1037    quota_took_out: &'static str,
1038    /// Followed by the status label.
1039    run_ended: &'static str,
1040    /// Followed by the question's short id.
1041    waiting_for_answer: &'static str,
1042    recovered_running: &'static str,
1043    no_run_to_recover: &'static str,
1044    could_not_start: &'static str,
1045    resume_not_latest: fn(&str, &str) -> String,
1046    resume_cannot_progress: fn(&str, &str) -> String,
1047    resume_unreadable: fn(&str, &str, &str) -> String,
1048    /// A takeover refusal: the operator-facing text. English is the error's own
1049    /// `Display`; a translation keeps the machine detail apart in parentheses.
1050    handover_refused: fn(&crate::handover::Refused) -> String,
1051    /// Appended to a refused handover's hold reason (leading space included).
1052    handover_hint: &'static str,
1053    /// (seats that never answered, review rounds) for the review loop's
1054    /// "refusing to call it clean" stop.
1055    reviewers_never_answered: fn(usize, usize) -> String,
1056}
1057
1058const PHRASES_EN: Phrases = Phrases {
1059    graph_stopped: |status, detail| {
1060        format!("the graph stopped at `{status}` without reaching a terminal status: {detail}")
1061    },
1062    quorum_lost: "the judging panel lost its quorum",
1063    quota_took_out: "; quota took out ",
1064    run_ended: "run ended ",
1065    waiting_for_answer: " - waiting for operator answer to question ",
1066    recovered_running: "recovered a `running` task whose daemon never recorded the outcome: ",
1067    no_run_to_recover: "task was `running` with no live daemon and no readable \
1068                        run to recover; held for a human to check what happened",
1069    could_not_start: "could not start the run: ",
1070    resume_not_latest: |q, run| {
1071        format!("question {q} asked to resume run {run}, which is not this task's latest run")
1072    },
1073    resume_cannot_progress: |q, run| {
1074        format!("question {q} asked to resume run {run}, which cannot make progress")
1075    },
1076    resume_unreadable: |q, run, e| {
1077        format!("question {q} asked to resume run {run}, which could not be read: {e}")
1078    },
1079    handover_refused: |r| r.to_string(),
1080    handover_hint: " (clean up the other worktree, then release the task from the queue)",
1081    reviewers_never_answered: |missing, rounds| {
1082        format!(
1083            "{missing} reviewer seat(s) never answered after {rounds} rounds; \
1084             refusing to call it clean"
1085        )
1086    },
1087};
1088
1089const PHRASES_JA: Phrases = Phrases {
1090    graph_stopped: |status, detail| {
1091        format!("グラフが終端状態に達しないまま `{status}` で停止しました: {detail}")
1092    },
1093    quorum_lost: "審査パネルが定足数を失いました",
1094    quota_took_out: "。クォータで脱落: ",
1095    run_ended: "run 終了: ",
1096    waiting_for_answer: " - オペレーターの回答待ち: 質問 ",
1097    recovered_running: "daemon が結果を記録しないまま `running` だったタスクを回収しました: ",
1098    no_run_to_recover: "タスクは `running` でしたが、生きた daemon も回収できる run も見つかりません。\
1099                        何が起きたか人が確認するため保留にしました",
1100    could_not_start: "run を開始できませんでした: ",
1101    resume_not_latest: |q, run| {
1102        format!(
1103            "質問 {q} は run {run} の再開を求めましたが、これはタスクの最新の run ではありません"
1104        )
1105    },
1106    resume_cannot_progress: |q, run| {
1107        format!("質問 {q} は run {run} の再開を求めましたが、これは進行できません")
1108    },
1109    resume_unreadable: |q, run, e| {
1110        format!("質問 {q} は run {run} の再開を求めましたが、読み込めませんでした: {e}")
1111    },
1112    handover_refused: |r| {
1113        use crate::handover::Refused;
1114        match r {
1115            Refused::Foreign { branch, path, why } => format!(
1116                "ブランチ `{branch}` は {path} にチェックアウトされており、magi は自動では\
1117                 削除しません。不要なら `git worktree remove` でその worktree を削除して\
1118                 から、やり直してください(詳細: {why})"
1119            ),
1120            Refused::Unsafe { branch, path, why } => format!(
1121                "ブランチ `{branch}` は {path} にチェックアウトされています。そこの作業を\
1122                 コミットか破棄したうえで `git worktree remove` で worktree を削除する\
1123                 か、run を破棄してよいと伝えてから、やり直してください(詳細: {why})"
1124            ),
1125            Refused::ReleaseFailed { branch, path, run } => format!(
1126                "ブランチ `{branch}` は {path} で run {run} がチェックアウトしており、その\
1127                 worktree の解放に失敗したか、変更が見つかりました(未コミットの変更が\
1128                 ある worktree は git が削除を拒否します)。worktree はそのまま残しました"
1129            ),
1130        }
1131    },
1132    handover_hint: "(他の worktree を片付けてから、タスクをキューから解放してください)",
1133    reviewers_never_answered: |missing, rounds| {
1134        format!(
1135            "{missing} 席のレビュアーが {rounds} ラウンドの間に一度も回答しなかったため、\
1136             クリーンとは認めません"
1137        )
1138    },
1139};
1140
1141fn phrases(language: &str) -> &'static Phrases {
1142    if crate::lang::is_japanese(language) {
1143        &PHRASES_JA
1144    } else {
1145        &PHRASES_EN
1146    }
1147}
1148
1149/// The language of the repository a task belongs to, read best-effort for the
1150/// paths that have no run (and so no config of their own) to ask. An unreadable
1151/// config is English, not an error: a hold reason must still get written.
1152fn language_of(task: &Task, fallback: &Path) -> String {
1153    crate::lang::of_repo(&repo_for(task, fallback))
1154}
1155
1156/// The task an answered question speaks for: the one whose runs include the
1157/// question's run, or - for a conductor question, filed under the task's own
1158/// id - the task with that id.
1159pub(crate) fn task_of_question<'a>(tasks: &'a [Task], q: &ask::Question) -> Option<&'a Task> {
1160    if q.node == crate::conduct::NODE {
1161        return tasks.iter().find(|t| t.id == q.run);
1162    }
1163    tasks.iter().find(|t| t.runs.contains(&q.run))
1164}
1165
1166/// Legacy conductor prose never becomes a maintenance command by inference.
1167/// Replace unsupported open labels under the question lock before dispatch.
1168fn repair_choice_offers(questions: &Questions) {
1169    for q in questions.list().into_iter().filter(|q| {
1170        q.status.open()
1171            && q.node == crate::conduct::NODE
1172            && q.choices
1173                .iter()
1174                .any(|c| crate::choice_effect::describe(q, c).is_none())
1175    }) {
1176        let _ = questions.update(&q.id, |current| {
1177            if current.status.open() {
1178                crate::choice_effect::executable_conductor(current);
1179                if let Some(deputy) = &mut current.deputy {
1180                    deputy.brief = crate::deputy::brief(
1181                        &current.run,
1182                        &current.detail,
1183                        &current.choices,
1184                        &current.actions,
1185                    );
1186                }
1187            }
1188            Ok(())
1189        });
1190    }
1191}
1192
1193/// Apply answered typed choices. Only the daemon calls this in production;
1194/// deputies settle the question and never execute its actions.
1195pub async fn apply_choice_actions(queue: &Queue, questions: &Questions, home: &Path) {
1196    apply_maintenance_choices(queue, questions, home).await;
1197    apply_task_choice_actions(queue, questions, home);
1198}
1199
1200/// Apply bounded maintenance effects independently of question ownership: a
1201/// release watcher need not have a queue task, and a predecessor is another run.
1202async fn apply_maintenance_choices(queue: &Queue, questions: &Questions, home: &Path) {
1203    for q in questions.list() {
1204        let Some(action) = q.chosen_action().filter(|a| a.maintenance()) else {
1205            continue;
1206        };
1207        // Receipt paths use question IDs, whose format is independent of run
1208        // IDs. Apply the question store's filename safety rule to both eras.
1209        if asker_may_still_read(questions, &q, home) || !ask::valid_asset_name(&q.id) {
1210            continue;
1211        }
1212        let dir = home.join("choice-actions");
1213        if std::fs::create_dir_all(&dir).is_err() {
1214            continue;
1215        }
1216        let Ok(lock) = std::fs::OpenOptions::new()
1217            .create(true)
1218            .truncate(false)
1219            .write(true)
1220            .open(dir.join(format!("{}.lock", q.id)))
1221        else {
1222            continue;
1223        };
1224        if crate::pr_head::try_lock_file(&lock).is_err() {
1225            continue;
1226        }
1227        let fresh = !home
1228            .join("choice-actions")
1229            .join(format!("{}.json", q.id))
1230            .exists();
1231        let result = async {
1232            let receipt = dir.join(format!("{}.json", q.id));
1233            if receipt.exists() {
1234                let prior: serde_json::Value = serde_json::from_slice(&std::fs::read(&receipt)?)?;
1235                anyhow::ensure!(
1236                    prior.get("status").and_then(|s| s.as_str()) == Some("applied"),
1237                    "previous maintenance attempt did not confirm completion; inspect its receipt"
1238                );
1239                return Ok::<_, anyhow::Error>(());
1240            }
1241            // Never retry an uncertain external effect. The receipt is written
1242            // before calling gh, exactly like release_watch's rerun record.
1243            let record = |status: &str| -> anyhow::Result<()> {
1244                let tmp = receipt.with_extension("tmp");
1245                std::fs::write(
1246                    &tmp,
1247                    serde_json::to_vec(&serde_json::json!({
1248                        "action": action, "status": status,
1249                    }))?,
1250                )?;
1251                std::fs::rename(tmp, &receipt)?;
1252                Ok(())
1253            };
1254            let applied = async {
1255                match action {
1256                    ask::ChoiceAction::ResumeRun { run } => {
1257                        crate::resume::validate(home, run)?;
1258                        record("started")?;
1259                        if !crate::resume::pin_queued(queue, run, Some(q.asked_at), Some(&q.id))? {
1260                            crate::resume::validate_unqueued(home, run)?;
1261                            crate::resume::start(home, run.clone()).await?;
1262                        }
1263                    }
1264                    ask::ChoiceAction::ReleaseTask {
1265                        task,
1266                        held_at,
1267                        reason,
1268                    } => {
1269                        anyhow::ensure!(
1270                            queue.resolve_id(task)? == *task,
1271                            "task action requires an exact task ID"
1272                        );
1273                        let _claim = queue.claim(task)?;
1274                        let mut task = queue.get(task)?;
1275                        if task.action_applied(&q.id) {
1276                            return Ok(());
1277                        }
1278                        // Filing this conductor question temporarily wraps the
1279                        // original hold in a Blocked state. Restore only that
1280                        // exact blocker on the local copy before checking the
1281                        // hold binding; unrelated waits never authorize release.
1282                        if q.node == crate::conduct::NODE
1283                            && q.run == task.id
1284                            && task.status == TaskStatus::Blocked
1285                            && task.blocked_from == Some(TaskStatus::Held)
1286                            && task.blocked_by.as_slice() == [q.id.as_str()]
1287                        {
1288                            task.unblock(&q.id);
1289                        }
1290                        anyhow::ensure!(
1291                            task.status == TaskStatus::Held
1292                                && task.held_at == Some(*held_at)
1293                                && task.hold_reason.as_deref() == reason.as_deref()
1294                                && task.operator_decided_at.is_none_or(|at| at <= q.asked_at),
1295                            "task no longer has the hold this question names"
1296                        );
1297                        task.release_operator("choice", Some(&q.id));
1298                        task.mark_action_applied(&q.id);
1299                        queue.put(&mut task)?;
1300                    }
1301                    ask::ChoiceAction::AbandonRun { run } => {
1302                        record("started")?;
1303                        crate::driver::abandon_proven_dead(home, run)?;
1304                    }
1305                    ask::ChoiceAction::RerunChecks {
1306                        repo,
1307                        pr,
1308                        head,
1309                        runs,
1310                    } => {
1311                        rerun_checks_once(home, &q.id, repo, pr, head, runs, || record("started"))
1312                            .await?;
1313                    }
1314                    _ => unreachable!(),
1315                }
1316                Ok::<_, anyhow::Error>(())
1317            }
1318            .await;
1319            match applied {
1320                Ok(()) => record("applied"),
1321                Err(e) => {
1322                    record(&format!("refused or failed: {e:#}"))?;
1323                    Err(e)
1324                }
1325            }
1326        }
1327        .await;
1328        // Retire the originating blocker as well as the target operation.
1329        // A foreign resume leaves its issuer held instead of competing beside
1330        // the execution just started. Other successful maintenance lets the
1331        // original task retry its normal, guarded path.
1332        if home
1333            .join("choice-actions")
1334            .join(format!("{}.json", q.id))
1335            .exists()
1336            && let Some(listed) = task_of_question(&queue.list(), &q)
1337            && !listed.action_applied(&q.id)
1338            && let Ok(_claim) = queue.claim(&listed.id)
1339            && let Ok(mut origin) = queue.get(&listed.id)
1340        {
1341            if !origin.operator_held()
1342                && origin.operator_decided_at.is_none_or(|at| at <= q.asked_at)
1343                && !matches!(origin.status, TaskStatus::Running | TaskStatus::Done)
1344            {
1345                if result.is_err() || matches!(action, ask::ChoiceAction::ResumeRun { .. }) {
1346                    origin.hold_machine(Some(format!(
1347                        "Maintenance choice {} settled; inspect its target before releasing this task",
1348                        q.id
1349                    )));
1350                } else {
1351                    origin.unblock(&q.id);
1352                }
1353            }
1354            origin.mark_action_applied(&q.id);
1355            let _ = queue.put(&mut origin);
1356        }
1357        if let Err(e) = result
1358            && fresh
1359        {
1360            notices::raise_in(
1361                home,
1362                Notice::warn(
1363                    &format!("action:{}", q.id),
1364                    format!("Choice action {} was not completed: {e:#}", q.id),
1365                ),
1366            );
1367        }
1368    }
1369}
1370
1371/// Rerun the named failed workflow runs of a pull request, each at most once
1372/// per `request` (a question id, or a chat operation identity). The head,
1373/// lifecycle and failed state are re-read from the forge first; `started` is
1374/// called once those checks pass and before the first external call, and the
1375/// ledger entry is written before every `gh` call, so an uncertain result is
1376/// never repeated automatically.
1377pub(crate) async fn rerun_checks_once(
1378    home: &Path,
1379    request: &str,
1380    repo: &Path,
1381    pr: &str,
1382    head: &str,
1383    runs: &[String],
1384    started: impl FnOnce() -> anyhow::Result<()>,
1385) -> anyhow::Result<()> {
1386    use crate::release_watch::{GhForge, ReleaseForge};
1387    anyhow::ensure!(
1388        !runs.is_empty()
1389            && runs.len() <= 20
1390            && runs
1391                .iter()
1392                .all(|r| !r.is_empty() && r.bytes().all(|b| b.is_ascii_digit()))
1393            && !pr.starts_with('-'),
1394        "invalid rerun targets"
1395    );
1396    let snap = GhForge.snapshot(repo, pr).await?;
1397    anyhow::ensure!(
1398        snap.head == head && snap.state == crate::land::PrLifecycle::Open,
1399        "pull request head or lifecycle changed"
1400    );
1401    anyhow::ensure!(
1402        runs.iter().all(|r| snap
1403            .checks
1404            .iter()
1405            .any(|c| c.run.as_ref() == Some(r) && crate::release_watch::is_failed(c.verdict))),
1406        "named workflow is no longer failed on this pull request"
1407    );
1408    started()?;
1409    let dir = home.join("choice-actions");
1410    std::fs::create_dir_all(&dir)?;
1411    let rerun_lock = std::fs::OpenOptions::new()
1412        .create(true)
1413        .truncate(false)
1414        .write(true)
1415        .open(dir.join("reruns.lock"))?;
1416    crate::pr_head::try_lock_file(&rerun_lock)?;
1417    let ledger = dir.join("reruns.json");
1418    let mut recorded: std::collections::BTreeSet<String> = if ledger.exists() {
1419        serde_json::from_slice(&std::fs::read(&ledger)?)?
1420    } else {
1421        Default::default()
1422    };
1423    for run in runs {
1424        let key = serde_json::to_string(&(request, repo, pr, head, run))?;
1425        if !recorded.insert(key) {
1426            continue;
1427        }
1428        let tmp = ledger.with_extension("tmp");
1429        std::fs::write(&tmp, serde_json::to_vec(&recorded)?)?;
1430        std::fs::rename(tmp, &ledger)?;
1431        GhForge.rerun(repo, run).await?;
1432    }
1433    Ok(())
1434}
1435
1436/// Carry out the [`ask::ChoiceAction`] attached to each answered choice, once.
1437///
1438/// Runs on every poll before the conductor looks at the queue. The task is
1439/// re-read under its claim, so a task somebody completed or held by hand in
1440/// the meantime is judged on what it is now. `Resume` releases the task and
1441/// records a pinned [`crate::queue::OperatorResume`], which `crate::conduct`
1442/// honours (no re-hold, no requeue) until the task runs and [`attempt`]'s
1443/// ordinary `unfinished_run` logic resumes that same run.
1444pub(crate) fn apply_task_choice_actions(queue: &Queue, questions: &Questions, home: &Path) {
1445    let tasks = queue.list();
1446    for q in questions.list() {
1447        // Dismissal is committed by the question writer, never a resume.
1448        if q.resolution().as_deref() == Some(crate::merge_decision::DISMISS) {
1449            continue;
1450        }
1451        if q.chosen_action().is_none_or(|a| a.maintenance()) {
1452            continue;
1453        }
1454        let Some(listed) = task_of_question(&tasks, &q) else {
1455            if q.node == land::APPROVAL_NODE
1456                && q.resolution().as_deref().is_some_and(|choice| {
1457                    choice == land::REPAIR || crate::merge_decision::stops(choice)
1458                })
1459            {
1460                notices::raise_in(
1461                    home,
1462                    Notice::warn(
1463                        &format!("repair-owner:{}", q.id),
1464                        format!(
1465                            "Approval action is waiting: no task owns run {}. Inspect `magi show {}` and restore its original queue owner; do not file a competing task.",
1466                            q.run, q.run
1467                        ),
1468                    ),
1469                );
1470            }
1471            continue;
1472        };
1473        if listed.action_applied(&q.id) {
1474            continue;
1475        }
1476        let Ok(_claim) = queue.claim(&listed.id) else {
1477            continue;
1478        };
1479        let Ok(mut task) = queue.get(&listed.id) else {
1480            continue;
1481        };
1482        // Work only on this local copy until the action can be committed.
1483        // In particular, a fresh lease below leaves the persisted task blocked.
1484        if q.node == crate::conduct::NODE && task.status == TaskStatus::Blocked {
1485            let blockers: Vec<_> = task
1486                .blocked_by
1487                .iter()
1488                .filter(|id| {
1489                    crate::queue::dependency(queue, questions, id)
1490                        == crate::queue::Dependency::Question(q.id.clone())
1491                })
1492                .cloned()
1493                .collect();
1494            if !blockers.is_empty() {
1495                for id in blockers {
1496                    task.unblock(&id);
1497                }
1498                let answer = match &q.answer {
1499                    Some(ask::Answer::Choice(c) | ask::Answer::Text(c)) => c.clone(),
1500                    None => String::new(),
1501                };
1502                task.record_answer(q.summary.clone(), answer);
1503            }
1504        }
1505        let language = language_of(&task, Path::new("."));
1506        let decision = decide_action(&task, &q, phrases(&language), |id| {
1507            RunState::load_under(id, home)
1508        });
1509        let ran = matches!(
1510            decision,
1511            ActionDecision::Resume(_)
1512                | ActionDecision::Requeue
1513                | ActionDecision::Hold
1514                | ActionDecision::Done
1515        );
1516        if ran && asker_may_still_read(questions, &q, home) {
1517            // The asking process or its seat can still take the word; act
1518            // once it has gone quiet. Bound on the remaining window: the
1519            // check is not atomic with the write below, so an asker that
1520            // takes the word in between (at most one poll, never more than
1521            // `ask::LEASE_TTL`) only prints it - it never touches the task,
1522            // and `Task::actions_applied` under the claim keeps the action to
1523            // one application. The waiter does not deliver an answer that
1524            // carries an action (`waiter::decide_owned`), so it cannot race.
1525            continue;
1526        }
1527        let done = matches!(decision, ActionDecision::Done);
1528        match decision {
1529            ActionDecision::Skip => continue,
1530            ActionDecision::Resume(run) => {
1531                task.release_operator("choice", Some(&q.id));
1532                task.resume_override = Some(crate::queue::OperatorResume {
1533                    question_id: q.id.clone(),
1534                    at: Timestamp::now(),
1535                    conductor_rehold: None,
1536                    forced: true,
1537                    pinned_run: Some(run),
1538                });
1539            }
1540            ActionDecision::Stale => {}
1541            ActionDecision::Requeue => {
1542                task.release_operator("choice-requeue", Some(&q.id));
1543                task.requeue();
1544            }
1545            ActionDecision::Hold => task.hold_manual(Some(q.summary.clone())),
1546            ActionDecision::Done => task.succeed(),
1547            ActionDecision::Refuse(why) => {
1548                if task.operator_held() {
1549                    // A rejected answer does not release the operator's hold
1550                    // or reclassify it as recoverable by the conductor.
1551                    task.hold_reason = Some(match task.hold_reason.take() {
1552                        Some(reason) => format!("{reason}\n\n{why}"),
1553                        None => why,
1554                    });
1555                } else {
1556                    task.notice_cause(&format!("resume-refused:{}", q.id), None, None);
1557                    task.hold_machine(Some(why));
1558                }
1559                notices::raise(
1560                    Notice::warn(
1561                        &format!("action:{}", q.id),
1562                        "An answer asked the daemon to resume a run that cannot be resumed; the task stays held.",
1563                    )
1564                    .link(Link::Task {
1565                        id: task.id.clone(),
1566                    }),
1567                );
1568            }
1569        }
1570        task.mark_action_applied(&q.id);
1571        // The applied id travels with the transition, so a failed save loses
1572        // neither and the answer is acted on at the next poll.
1573        record(queue, &mut task);
1574        if done && queue.get(&task.id).is_ok_and(|t| t.action_applied(&q.id)) {
1575            supersede_prior_runs(&task, home);
1576        }
1577        // Delivery is only a note of who has read the answer; the task's own
1578        // mark above is the authority. Best effort.
1579        let _ = questions.update(&q.id, |r| {
1580            r.answer_delivered = true;
1581            Ok(())
1582        });
1583    }
1584}
1585
1586/// Whether something may still read the answer to `q`: a fresh lease, or the
1587/// asking seat still listed as active in its run (its CLI can outlive the
1588/// `magi ask` that was killed).
1589fn asker_may_still_read(questions: &Questions, q: &ask::Question, home: &Path) -> bool {
1590    let now = Timestamp::now();
1591    if questions.read_lease(&q.id).is_some_and(|l| l.fresh(now)) {
1592        return true;
1593    }
1594    q.node != crate::conduct::NODE
1595        && RunState::load_under(&q.run, home).is_ok_and(|s| {
1596            s.seats_active()
1597                .any(|(k, a)| *k == q.seat && a.remaining_secs(now) > 0)
1598        })
1599}
1600
1601/// Retire an unanswered conductor question after its task no longer refers to
1602/// it. Conductor questions use the task id in `Question::run`, so run-based
1603/// cleanup cannot observe a manual release or completion.
1604///
1605/// Restricted to `Question::node == crate::conduct::NODE`: an ordinary run's
1606/// own question also carries a `run`, and a run id that happens to collide
1607/// with some task's id is not this loop's business — only a conductor
1608/// question actually uses the task id that way. One `Questions::list()` scan
1609/// is taken up front and matched against the in-memory task set, rather than
1610/// calling `Questions::open_for` (a full disk scan on its own) once per task.
1611fn reconcile_task_questions(queue: &Queue, questions: &Questions) {
1612    let tasks = queue.list();
1613    let by_id: std::collections::BTreeMap<&str, &Task> =
1614        tasks.iter().map(|t| (t.id.as_str(), t)).collect();
1615    let referenced: Vec<&str> = tasks
1616        .iter()
1617        .flat_map(|task| task.blocked_by.iter().map(String::as_str))
1618        .collect();
1619
1620    for mut question in questions.list() {
1621        if !question.status.open() || question.node != crate::conduct::NODE {
1622            continue;
1623        }
1624        // Keep questions a task still names, including when the reference
1625        // moved to a dependent task.
1626        // Preserve even ambiguous references: retiring a possibly referenced
1627        // question is unsafe until the operator disambiguates it.
1628        if referenced
1629            .iter()
1630            .any(|id| question.id.starts_with(id) || question.id.ends_with(id))
1631        {
1632            continue;
1633        }
1634        let Some(task) = by_id.get(question.run.as_str()) else {
1635            continue;
1636        };
1637        question.abandon(format!(
1638            "task {} no longer waits for this answer",
1639            task.short()
1640        ));
1641        if let Err(e) = questions.put(&mut question) {
1642            tracing::warn!(
1643                "could not retire question {} for task {}: {e:#}",
1644                question.short(),
1645                task.short()
1646            );
1647        }
1648    }
1649}
1650
1651/// What a finished run tells the queue about the task it came from.
1652///
1653/// A struct rather than a fourth and fifth boolean argument: the two flags
1654/// answer different questions about the same run, and a call site passing
1655/// `(…, true, false)` is one transposition away from refunding attempts
1656/// forever.
1657#[derive(Debug, Clone, Copy)]
1658pub struct Verdict {
1659    /// Where the graph stopped.
1660    pub status: RunStatus,
1661    /// The run opened a pull request.
1662    pub left_pr: bool,
1663    /// At least one seat was lost to a rate limit.
1664    pub quota_hit: bool,
1665    /// The run parked at a node boundary because it was asked to.
1666    pub parked: bool,
1667    /// The run never produced a single candidate a judge could look at.
1668    ///
1669    /// Distinct from `quota_hit`: a run can lose a seat to a rate limit and
1670    /// still have another candidate worth judging, in which case the loss was
1671    /// not the reason nothing came of the run. This is `true` only when the
1672    /// implement wave ended with nothing viable at all.
1673    pub no_viable_candidates: bool,
1674}
1675
1676/// Record a finished run against the task it came from.
1677///
1678/// Kept pure and separate from the loop because this mapping *is* the retry
1679/// policy, and a policy that can only be exercised by spawning a graph is a
1680/// policy nobody checks. The table:
1681///
1682/// | run status                           | task becomes        | attempt spent |
1683/// |---------------------------------------|---------------------|---------------|
1684/// | parked at a boundary                  | `Parked` (requeued) | **no**        |
1685/// | `Merged`, `Ready`                      | `Done`               | yes          |
1686/// | `AlreadyInBase`                        | `Done`, with a note  | **no**        |
1687/// | `Stalled`, quota hit                   | `Failed` (requeued) | **no**        |
1688/// | `Failed`, quota hit, no viable cand.   | `Failed` (requeued) | **no**        |
1689/// | `Stalled`, no quota                    | `Failed`, or `Held`  | yes          |
1690/// | `Blocked` with a PR                    | `Held`               | yes          |
1691/// | `Blocked`, `Failed` otherwise          | `Failed`, or `Held`  | yes          |
1692/// | `VerifiedNoop`                          | `Held`               | yes          |
1693/// | anything non-terminal                  | `Failed`, or `Held`  | yes          |
1694///
1695/// The `VerifiedNoop` row is independent of the `Failed`-quota row above it,
1696/// deliberately: every candidate agreeing there is nothing to write is not a
1697/// machine fact about a rate limit, it is an unverified claim about the
1698/// *task* that a human still has to check — see [`RunStatus::VerifiedNoop`]'s
1699/// own doc and [`Task::handed_off`]. `Held` rather than `Done` on purpose: the
1700/// claim could be wrong (a misread instruction, a stale check), and closing
1701/// the task automatically on an implementer's say-so would be the exact
1702/// failure mode task 391f's own audit was raised to avoid. `attempt spent` is
1703/// `yes` here for the same reason it is on the `Blocked`-with-a-PR row just
1704/// above, which settles through the same [`Task::handed_off`]: `Held` is not
1705/// `Failed`-and-requeued, so nothing retries this task on the same unverified
1706/// claim regardless of whether the one already-spent attempt is refunded, and
1707/// [`Task::release`] resets the count to zero anyway the moment a human looks
1708/// at the evidence and lets it run again.
1709///
1710/// The `Stalled`-quota and `Failed`-quota rows are the ones worth reading
1711/// twice, together. A quorum lost to rate limits is a property of the machine
1712/// and not of the task, so the attempt is refunded and a reset quota picks
1713/// the work up where it stopped — and that is just as true when every
1714/// implement seat lost the same race and `after_implement` bails with nothing
1715/// to judge, which surfaces as `Failed` rather than `Stalled` but is the same
1716/// machine fact. The `no_viable_candidates` guard is what keeps that row
1717/// narrow: a `Failed` run that produced a real candidate which then lost for
1718/// some other reason still spends the attempt, exactly like the quorum lost
1719/// to judges that answered with the wrong shape is ordinary flakiness, and
1720/// refunding *that* takes the bound off the retry loop entirely: run e633
1721/// stalled with `quota: []` after two judges wrote unusable JSON, was
1722/// refunded, and the next attempt paid for a fresh hour-long implement wave
1723/// before it could fail the same way. `max_attempts` exists precisely so
1724/// that cannot repeat forever.
1725///
1726/// A non-terminal status means `execute` returned while the graph was still
1727/// mid-flight, which is a bug rather than a verdict; it is treated as a
1728/// failure so that a task cannot loop on it either.
1729///
1730/// `left_pr` splits the `Blocked` row, and it is the difference between a run
1731/// that failed and a run that finished into a gate. See [`Task::handed_off`].
1732pub fn settle(task: &mut Task, verdict: Verdict, detail: &str, max_attempts: usize) {
1733    settle_in(task, verdict, detail, max_attempts, &PHRASES_EN)
1734}
1735
1736/// [`settle`], with the daemon's own fixed prose in `p`'s language.
1737fn settle_in(task: &mut Task, verdict: Verdict, detail: &str, max_attempts: usize, p: &Phrases) {
1738    // A parked run is the operator's own doing, and its work is intact on
1739    // disk. The task goes back in line with its attempt refunded so the next
1740    // loop resumes the same run - which `one_task` prefers over competing
1741    // again - and so that swapping the binary a few times cannot exhaust a
1742    // budget meant for agents that actually misbehaved.
1743    if verdict.parked {
1744        task.park(detail);
1745        return;
1746    }
1747    match verdict.status {
1748        RunStatus::Merged | RunStatus::Ready
1749            if task.pr_recovery.as_ref().is_some_and(|s| {
1750                s.disposition != Some(crate::pr_recovery::Disposition::Review)
1751            }) =>
1752        {
1753            task.hold_manual(Some("Public delivery is not confirmed. Inspect the selected PR and run, then use recover-pr --disposition land with the intended source and validation configuration.".into()));
1754        }
1755        RunStatus::Merged | RunStatus::Ready => task.succeed(),
1756        RunStatus::AlreadyInBase => task.already_landed(detail),
1757        RunStatus::Stalled if verdict.quota_hit => task.stall(detail),
1758        RunStatus::Failed if verdict.quota_hit && verdict.no_viable_candidates => {
1759            task.stall(detail)
1760        }
1761        RunStatus::Stalled | RunStatus::Failed => task.fail(detail, max_attempts),
1762        RunStatus::Blocked if verdict.left_pr => task.handed_off(detail),
1763        RunStatus::Blocked => task.fail(detail, max_attempts),
1764        RunStatus::VerifiedNoop => task.handed_off(detail),
1765        other => task.fail((p.graph_stopped)(label(other), detail), max_attempts),
1766    }
1767}
1768
1769/// Rewrite this task's now-moot earlier attempts — [`Task::superseded_attempts`]
1770/// — from `Blocked`/`Stalled` to [`RunStatus::Superseded`], once the task
1771/// itself has actually finished *and* its own last attempt is provably why:
1772/// this loads that run and requires `Merged`/`Ready` before touching
1773/// anything earlier in `runs`, since `Task::succeed` can also be called
1774/// directly on a task whose last attempt never landed at all.
1775///
1776/// Called right after whatever call set `task.status` to
1777/// [`TaskStatus::Done`] — this module's own `settle`'s callers below and
1778/// `reclaim`'s, but also every other place a task can be closed by hand
1779/// without the loop ever settling it itself: `magi task done`
1780/// (`main::TaskCmd::Done`) and `POST /api/queue/{id}/done`
1781/// (`web::queue_done`). Closing a task by hand — after confirming a manual
1782/// GitHub merge, say, exactly the workflow this repository's own
1783/// "Landing a run's winner by hand" documents — is just as much "this task's
1784/// story is over" as a daemon-driven `Merged`/`Ready` is, and skipping it
1785/// there would leave every earlier attempt stuck at `Blocked`/`Stalled`
1786/// forever, which is the exact backlog this status exists to clear. `pub`
1787/// for those two out-of-module callers; everything else about this stays
1788/// internal bookkeeping.
1789///
1790/// A run something is still actually driving is left alone: a
1791/// task cannot be `Done` while one of its *own* attempts is still in flight,
1792/// so a live one at this point can only be an unrelated process — a manual
1793/// `--resume` of this old, now-moot run — and rewriting under it would just
1794/// be undone (back to `Blocked`/`Stalled`, or worse) the next time that
1795/// process itself saves. This is the same `daemon_claims` + [`RunState::liveness`]
1796/// pair `run_report`/`run_detail` use for the same question elsewhere:
1797/// `is_working_on` alone only ever proves a *daemon* claim, not a `magi run
1798/// --resume` invoked by hand outside it, which `liveness` also checks via
1799/// the run's own recorded `driver_pid`. Anything not `Blocked` or `Stalled`
1800/// (already terminal in some other way, or itself mid-run) is left untouched
1801/// too.
1802///
1803/// Best-effort, like the rest of this module's bookkeeping: a load or save
1804/// failure here is a `tracing::warn`, not a failed settle, and the next time
1805/// this same task's completion is recorded — or a daemon restart replays
1806/// `reclaim_orphaned_running` — is another chance to catch up. There is no
1807/// separate reconciliation pass; if that gap ever matters in practice, one
1808/// can be added then.
1809///
1810/// `home` is a parameter, not the process-global [`crate::run::home`], for
1811/// the same reason [`RunState::save_under`] takes one: `web::queue_done`
1812/// calls this with `ui.home`, which is `crate::run::home()` in a real
1813/// process but a fixture's own directory under test — falling through to
1814/// the global there would read and write through whichever home some other
1815/// test in the same binary happened to pin into that `OnceLock` first, not
1816/// the run this call actually means.
1817pub fn supersede_prior_runs(task: &Task, home: &Path) {
1818    let now = Timestamp::now();
1819    // `Task::superseded_attempts` cannot tell a loop-driven `Done` (always
1820    // right behind a `Merged`/`Ready` verdict) from one set directly by
1821    // `magi task done`/its web equivalent/the conductor on a task whose last
1822    // recorded attempt never actually landed - that requires reading the
1823    // run itself, which is exactly what this does before trusting anything
1824    // in `runs` is safe to relabel.
1825    let last_run_succeeded = task
1826        .runs
1827        .last()
1828        .and_then(|id| RunState::load_under(id, home).ok())
1829        .is_some_and(|s| matches!(s.status, RunStatus::Merged | RunStatus::Ready));
1830    for id in task.superseded_attempts(last_run_succeeded) {
1831        let mut state = match RunState::load_under(id, home) {
1832            Ok(s) => s,
1833            Err(e) => {
1834                tracing::warn!("could not load run {id} to mark it superseded: {e:#}");
1835                continue;
1836            }
1837        };
1838        if !matches!(state.status, RunStatus::Blocked | RunStatus::Stalled) {
1839            continue;
1840        }
1841        let daemon_claims = is_working_on(home, id, now);
1842        if state.liveness(daemon_claims) == Liveness::Live {
1843            continue;
1844        }
1845        state.status = RunStatus::Superseded;
1846        if let Err(e) = state.save_under(home) {
1847            tracing::warn!("could not mark run {id} superseded: {e:#}");
1848        }
1849    }
1850}
1851
1852/// Catch up every `Done` task [`supersede_prior_runs`] could not fully
1853/// finish the moment it was called, because one of its earlier attempts was
1854/// still live then - a `magi run --resume` invoked by hand on an old run,
1855/// most likely.
1856///
1857/// That skip is correct at the time (rewriting under a run something is
1858/// still actively saving would just be undone), but a task only ever
1859/// reaches `Done` once, so without this nothing would ever look at it
1860/// again: if that manual process later ends anywhere other than
1861/// `Merged`/`Ready` - abandoned, or landed as another `Blocked` - the
1862/// skipped attempt would sit at `Blocked`/`Stalled` forever, exactly the
1863/// backlog this status exists to clear. Called on the same idle-only
1864/// cadence as [`janitor`], next to it rather than folded into it: this
1865/// walks `queue` and `janitor` never otherwise needs one.
1866///
1867/// Simply re-running [`supersede_prior_runs`] over every `Done` task with
1868/// more than one attempt, every pass: each call is a handful of local file
1869/// reads bounded by that task's own attempt count, the same order of cost
1870/// [`clean::fold_due`] already pays scanning every run on disk on the same
1871/// cadence, and a task already fully resolved costs one failed liveness
1872/// check turned no-op the moment its lingering attempt is no longer live.
1873fn resweep_superseded_attempts(queue: &Queue, home: &Path) {
1874    for task in queue.list() {
1875        if task.status != TaskStatus::Done || task.runs.len() < 2 {
1876            continue;
1877        }
1878        supersede_prior_runs(&task, home);
1879    }
1880}
1881
1882/// [`settle`], plus attaching the run's own [`diagnostic`] excerpt once the
1883/// task ends up held.
1884///
1885/// The one place [`attempt`] (a live finish) and [`reclaim`] (recovering one a
1886/// dead daemon never got back to) share this, so the two cannot drift into
1887/// disagreeing about which held tasks get a diagnostic.
1888fn settle_and_diagnose(
1889    task: &mut Task,
1890    verdict: Verdict,
1891    detail: &str,
1892    max_attempts: usize,
1893    state: &RunState,
1894) {
1895    let p = phrases(&state.config.graph.language);
1896    // Shared by normal completion and crash recovery, under the task claim.
1897    // No new attempt can own the task until the old driver has exited.
1898    if let Some(d) = &state.merge_decision {
1899        use crate::merge_decision as md;
1900        if task.runs.last().is_some_and(|id| id != &state.id) {
1901            return;
1902        }
1903        if (d.choice == md::WITHDRAW && d.closed) || d.choice == md::DISMISS {
1904            task.succeed();
1905        } else if let Some(agent) = md::selected_agent(&d.choice).filter(|_| d.closed) {
1906            if state.liveness(false) != Liveness::Dead {
1907                task.hold_manual(Some("Previous driver is still live; replacement held until it exits. Release the task after checking the driver.".into()));
1908                return;
1909            }
1910            let replacement = md::Replacement {
1911                question: d.question.clone(),
1912                run: state.id.clone(),
1913                pr: d.target.pr.clone(),
1914                agent: agent.to_owned(),
1915                context: format!(
1916                    "Previous PR: {}. Previous run: {}. Owner requested a new implementation.\nReview evidence:\n{}",
1917                    d.target.pr,
1918                    state.id,
1919                    serde_json::to_string(&state.reviews).unwrap_or_default()
1920                ),
1921            };
1922            task.requeue();
1923            task.pr_recovery = None;
1924            task.review_of = None;
1925            task.replacement = Some(replacement);
1926        } else {
1927            task.hold_manual(Some(if d.choice.eq_ignore_ascii_case(land::HOLD) {
1928                format!("Decision held by owner; no further repair or merge work. {}", state.merge.as_ref().map_or("", |m| m.detail.as_str()))
1929            } else { format!("PR closure was not confirmed. Task held; inspect the existing run before starting replacement work. {}", state.merge.as_ref().map_or("", |m| m.detail.as_str())) }));
1930        }
1931        task.mark_action_applied(&d.question);
1932        if let Some(pass) = state.passes.last() {
1933            task.record_pass_at(
1934                &state.id,
1935                pass.index,
1936                Some(pass.started_at),
1937                task.attempts,
1938                None,
1939            );
1940        }
1941        return;
1942    }
1943    if let Some(replacement) = &task.replacement
1944        && !state.parked
1945        && state.resource_block.is_none()
1946        && (state.viable().is_empty()
1947            || (state.status == RunStatus::Blocked && state.tally.is_none()))
1948    {
1949        task.hold_manual(Some(format!("Selected implementer `{}` was unavailable, failed or hit quota before producing a viable implementation. No other implementer was dispatched. Inspect the run, restore availability, then explicitly release this task.", replacement.agent)));
1950        return;
1951    }
1952    let attempts_before = task.attempts;
1953    // A run that stopped because a shared resource stayed busy was never a
1954    // failure of the task or of any agent: nothing was asked. The attempt is
1955    // refunded and the task is held by the machine, under a reason `triage`
1956    // can re-check, so the same run resumes with the same agents once the
1957    // resource is free. A park request still wins (it is the operator's).
1958    if let Some(block) = state.resource_block.as_ref().filter(|_| !verdict.parked) {
1959        task.notice_cause(
1960            &format!("resource:{}:{}", block.kind, block.node),
1961            None,
1962            None,
1963        );
1964        task.last_error = Some(detail.to_owned());
1965        task.diagnostic = None;
1966        task.attempts = task.attempts.saturating_sub(1);
1967        if block.kind == "disk pressure" {
1968            task.hold_machine(Some(format!(
1969                "{}{}",
1970                crate::disk::RUNNING_PRESSURE,
1971                block.detail
1972            )));
1973        } else {
1974            task.hold_machine(Some(format!(
1975                "{}{} stayed busy past the resource-wait limit before `{}` could start; \
1976             no agent was asked and no attempt was spent",
1977                crate::cache::RESOURCE_HOLD,
1978                block.kind,
1979                block.node
1980            )));
1981        }
1982        if let Some(pass) = state.passes.last() {
1983            task.record_pass_at(
1984                &state.id,
1985                pass.index,
1986                Some(pass.started_at),
1987                attempts_before,
1988                None,
1989            );
1990        }
1991        return;
1992    }
1993    let unresolved = task.hold_notice_cause.clone();
1994    if !verdict.parked
1995        && task
1996            .pr_recovery
1997            .as_ref()
1998            .is_some_and(|s| s.disposition != Some(crate::pr_recovery::Disposition::Review))
1999        && matches!(
2000            verdict.status,
2001            RunStatus::Ready | RunStatus::Merged | RunStatus::AlreadyInBase
2002        )
2003    {
2004        if state.pr_recovery == task.pr_recovery && state.public_delivery_complete() {
2005            task.succeed();
2006        } else {
2007            task.hold_manual(Some("Selected PR delivery is incomplete: local review or publication is not a confirmed public merge. Inspect the run and PR; use recover-pr --disposition land with --source-head <reviewed-full-SHA> and the required verification configuration. No automatic new attempt will start.".into()));
2008        }
2009    } else if !verdict.parked
2010        && task.pr_recovery.is_some()
2011        && matches!(
2012            verdict.status,
2013            RunStatus::Blocked | RunStatus::Failed | RunStatus::Gating
2014        )
2015        && !verdict.quota_hit
2016    {
2017        task.hold_unrequested_retry(Some(format!("Selected recovery stopped: {detail}. Inspect the retained source and run; correct the cause before explicitly releasing or selecting again. No automatic replacement will start.")));
2018    } else {
2019        settle_in(task, verdict, detail, max_attempts, p);
2020    }
2021    if !verdict.parked
2022        && !verdict.quota_hit
2023        && matches!(
2024            verdict.status,
2025            RunStatus::Blocked | RunStatus::Failed | RunStatus::Stalled
2026        )
2027    {
2028        task.recovery_failed(state);
2029    }
2030    if state.prep_failure.is_some() {
2031        // Minting a run whose candidates could not be prepared is not work
2032        // on a newly approved attempt. Keep the unresolved outcome anchor.
2033        task.hold_notice_cause = unresolved;
2034        task.startup_notice_cause("startup-candidates");
2035    }
2036    // Written here, where the refund (or not) is decided, so the history is a
2037    // fact and not a later guess from the run's status.
2038    if let Some(pass) = state.passes.last() {
2039        let parked = match &pass.end {
2040            Some(crate::run::PassEnd::Parked { cause }) => Some(cause.clone()),
2041            _ => None,
2042        };
2043        task.record_pass_at(
2044            &state.id,
2045            pass.index,
2046            Some(pass.started_at),
2047            attempts_before,
2048            parked,
2049        );
2050    }
2051    if task.status == TaskStatus::Held {
2052        if state.prep_failure.is_none() {
2053            task.notice_cause(
2054                label(verdict.status),
2055                Some(&state.id),
2056                state.passes.last().map(|p| p.index),
2057            );
2058        }
2059        task.diagnostic = diagnostic(state);
2060        note_open_question(task, &state.id, p);
2061    }
2062}
2063
2064/// If `task` is held with a `magi ask` question still open on `run`, name the
2065/// question in [`Task::hold_reason`] rather than leaving the hold reading
2066/// only [`describe`]'s run summary.
2067///
2068/// This is the shape task 20260928-191407-05fd left underspecified: an
2069/// implementer settled a verified no-op and filed a question for the
2070/// operator, and the hold that followed said nothing about the question
2071/// waiting on them. Appended, not substituted - [`crate::triage`] parses the
2072/// existing text ([`is_disk_hold`](crate::triage), the answered-question
2073/// marker) and must keep seeing it unchanged.
2074///
2075/// Best-effort: an unreadable question store must not stop a run from
2076/// settling, so this is a no-op rather than a propagated error when
2077/// [`crate::run::try_home`] has nothing to offer or the store is empty.
2078fn note_open_question(task: &mut Task, run: &str, p: &Phrases) {
2079    let Some(home) = crate::run::try_home() else {
2080        return;
2081    };
2082    let open = Questions::at(home.join("questions")).open_for(run);
2083    let Some(q) = open.first() else {
2084        return;
2085    };
2086    let mut decisions: Vec<_> = open.iter().map(|q| q.id.as_str()).collect();
2087    decisions.sort_unstable();
2088    task.notice_cause(
2089        &format!("questions:{}", decisions.join(",")),
2090        Some(run),
2091        None,
2092    );
2093    let base = task.hold_reason.clone().unwrap_or_default();
2094    task.hold_reason = Some(format!("{base}{}{}", p.waiting_for_answer, q.short()));
2095}
2096
2097/// Reconcile a task left at [`TaskStatus::Running`] by a daemon that never
2098/// got back to [`settle`] for it — a crash, a `SIGKILL`, or a run carried on
2099/// by some other means entirely, like a manual `magi run` resume that
2100/// finishes the graph outside the queue's bookkeeping.
2101///
2102/// Pure and separate from [`reclaim_orphaned_running`] for the same reason
2103/// `settle` is separate from `attempt`: a task recovered this way must land
2104/// exactly where a live daemon would have put it — the same policy table,
2105/// not a second one that quietly drifts from it — and that is only checkable
2106/// without spawning a real run.
2107fn reclaim(task: &mut Task, last_run: Option<RunState>, max_attempts: usize, language: &str) {
2108    match last_run {
2109        Some(state) => {
2110            let verdict = Verdict {
2111                status: state.status,
2112                left_pr: state.pr.is_some(),
2113                quota_hit: !state.quota.is_empty(),
2114                parked: state.parked,
2115                no_viable_candidates: state.viable().is_empty(),
2116            };
2117            let detail = format!(
2118                "{}{}",
2119                phrases(&state.config.graph.language).recovered_running,
2120                describe(&state)
2121            );
2122            settle_and_diagnose(task, verdict, &detail, max_attempts, &state);
2123        }
2124        None => {
2125            // No run, so no config of its own: `language` is the task's
2126            // repository's setting, read by the caller.
2127            let why = phrases(language).no_run_to_recover;
2128            task.last_error = Some(why.to_owned());
2129            // The phone shows `hold_reason`, so a task held by the machine
2130            // says why there too and not only in `last_error`.
2131            task.notice_cause("missing-run", None, None);
2132            task.hold_machine(Some(why.to_owned()));
2133        }
2134    }
2135}
2136
2137/// Find every task left at `running` that no live process is actually
2138/// driving, and settle each one against whatever its last run became.
2139///
2140/// # Why a claim is proof, not a guess
2141///
2142/// [`poll`] takes a task's [`Queue::claim`] *before* [`Task::start`] writes
2143/// `running`, and the guard is held for the task's whole time in that status:
2144/// `attempt` does not return, and the loop does not move past the scope
2145/// holding the claim, until the run has settled. So a `running` task whose
2146/// lock is gone cannot have a live owner — this process or any other —
2147/// without needing a staleness threshold or a pid check the way
2148/// [`sweep_stale_claims`] does for the narrower case of a lock left next to a
2149/// task that never got as far as `running` at all. Taking the claim here is
2150/// the whole test: it either fails, because something really does hold it
2151/// and the task is left alone, or it succeeds, which is the proof — and it is
2152/// kept for the rest of the decision so nothing else can start a competing
2153/// run while this one is being written.
2154///
2155/// Called on every poll, not only at startup, for the reason
2156/// [`sweep_stale_claims`] now is too: a daemon that has been up for days must
2157/// keep noticing this, not only on the one morning it happened to restart.
2158fn reclaim_orphaned_running(
2159    queue: &Queue,
2160    max_attempts: usize,
2161    live_workers: &std::collections::BTreeSet<String>,
2162) -> Vec<String> {
2163    let mut reclaimed = Vec::new();
2164    for listed in queue.list() {
2165        if listed.status != TaskStatus::Running || live_workers.contains(&listed.id) {
2166            continue;
2167        }
2168        let Ok(_claim) = queue.claim(&listed.id) else {
2169            continue;
2170        };
2171        // Re-read under the claim: a release or an edit landed by a human
2172        // between the listing above and the claim just taken must not be
2173        // clobbered by a decision based on the stale copy.
2174        let Ok(mut task) = queue.get(&listed.id) else {
2175            continue;
2176        };
2177        if task.status != TaskStatus::Running {
2178            continue;
2179        }
2180        let last_run = task.runs.last().and_then(|id| RunState::load(id).ok());
2181        // `execute` normally abandons a run's own open questions the moment
2182        // `status` lands somewhere non-resumable (see `graph::Runner::settle_questions`),
2183        // but a daemon that crashed *inside* that path - mid `land`'s CI wait,
2184        // say - can leave a `run.json` already at `Merged`/`Ready`/`Failed`
2185        // with the question still `open`, because the process died before
2186        // reaching that call. `reclaim` itself stays pure on purpose (see its
2187        // own doc), so the same cleanup runs here instead, against the run
2188        // this reclaim is already reading. `settle_run` costs nothing when
2189        // `execute` already got there first.
2190        if let Some(state) = &last_run
2191            && let Err(e) = ask::Questions::open().settle_run(&state.id, state.status)
2192        {
2193            tracing::warn!("abandon questions for {}: {e:#}", state.id);
2194        }
2195        let language = if last_run.is_none() {
2196            language_of(&task, Path::new("."))
2197        } else {
2198            String::new()
2199        };
2200        reclaim(&mut task, last_run, max_attempts, &language);
2201        if task.status == TaskStatus::Done {
2202            supersede_prior_runs(&task, &crate::run::home());
2203        }
2204        record(queue, &mut task);
2205        reclaimed.push(task.id.clone());
2206    }
2207    reclaimed
2208}
2209
2210/// Find every run whose `run.json` is provably dead — every seat it still
2211/// lists as [`crate::run::RunState::active`] has overrun its own timeout, and
2212/// [`crate::run::RunState::liveness`] reads [`crate::run::Liveness::Dead`],
2213/// not merely "no daemon claims it" — and fail it, clearing the leftover
2214/// active seats so the run stops reading as `implementing` (or whichever
2215/// node) forever.
2216///
2217/// [`reclaim_orphaned_running`] settles the *task* a dead daemon left
2218/// `running`, using whatever `run.json` already says — but nothing in that
2219/// path, nor in [`reclaim`], ever writes back to the run itself (`reclaim`
2220/// stays pure on purpose, see its own doc), so a `run.json` a killed process
2221/// never got back to sits exactly where it was left: `active` full of seats
2222/// nobody will ever answer for, `status` stuck on whatever node was in
2223/// flight. `magi show` already tells an operator this in prose (`no live
2224/// daemon claims this run right now`); this is what makes that fact durable
2225/// on disk, the same way a task's own `TaskStatus::Running` does not get to
2226/// stay stuck once nothing is driving it.
2227///
2228/// Runs on every poll, not only at startup, for the reason
2229/// [`sweep_stale_claims`] and [`reclaim_orphaned_running`] already are: a
2230/// daemon up for days must keep noticing a run some other, now-dead, daemon
2231/// left behind just as readily as one it trips over on the way up.
2232///
2233/// Walks `home.join("runs")` directly and reads each `run.json` on its own,
2234/// rather than the process-global [`RunState::load`] / [`crate::run::list_ids`] —
2235/// the same reason [`crate::clean`]'s housekeeping passes take an explicit
2236/// `runs` directory instead: `home` here is a parameter precisely so a test
2237/// can point it away from the operator's real history (see [`drive`]'s own
2238/// doc), and a scan that fell through to the global home anyway would walk
2239/// whichever directory some *other* process or test pinned into that
2240/// `OnceLock` first — mutating runs this call was never handed.
2241fn reclaim_abandoned_runs(home: &Path, now: Timestamp) -> Vec<String> {
2242    reclaim_abandoned_runs_with(
2243        home,
2244        now,
2245        crate::proc::pid_status,
2246        crate::proc::process_started_at,
2247    )
2248}
2249
2250/// [`reclaim_abandoned_runs`] with its `driver_pid` liveness/identity queries
2251/// supplied by the caller — mirrors [`sweep_stale_claims_with`], which exists
2252/// for the identical reason: this sweep's real damage (wiping a run's active
2253/// seats and failing it) has to be provable against an injected answer in a
2254/// test, not just the real process table.
2255fn reclaim_abandoned_runs_with<F, G>(
2256    home: &Path,
2257    now: Timestamp,
2258    query: F,
2259    identity: G,
2260) -> Vec<String>
2261where
2262    F: Fn(u32) -> Option<bool>,
2263    G: Fn(u32) -> Option<String>,
2264{
2265    let mut abandoned = Vec::new();
2266    for entry in std::fs::read_dir(home.join("runs"))
2267        .into_iter()
2268        .flatten()
2269        .flatten()
2270    {
2271        let id = entry.file_name().to_string_lossy().into_owned();
2272        if !crate::run::is_run_id(&id) {
2273            continue;
2274        }
2275        // Serialize the proof and save with claims and takeover: a new
2276        // execution must not be overwritten after the liveness observation.
2277        let Ok(_owner) = crate::pr_head::owner_lock(home, &id) else {
2278            continue;
2279        };
2280        // Unreadable is `clean::fold_due`'s problem, not this one's — see
2281        // that module's docs for why a run this cannot parse is left alone
2282        // rather than guessed at. A different schema number is not that: this
2283        // touches only `status` and `active`, never a field whose meaning a
2284        // schema bump changed, so an old record's values serve this exactly
2285        // as well as a current one's (see `clean::read_state`'s own doc for
2286        // the same reasoning applied to folding).
2287        let Ok(body) = std::fs::read_to_string(entry.path().join("run.json")) else {
2288            continue;
2289        };
2290        let Ok(mut state) = serde_json::from_str::<RunState>(&body) else {
2291            continue;
2292        };
2293        if state.driver_exited || state.status.done() || !state.active_all_overrun(now) {
2294            continue;
2295        }
2296        // Not `!is_working_on(..)` alone: that is only "no *daemon* claims
2297        // it", which is also the normal, healthy shape of a manual `magi
2298        // run` / `magi review` sharing this same home — this scan walks
2299        // every run on disk, not only ones this daemon itself started. Such
2300        // a run's active seats can legitimately sit past their own timeout
2301        // for a little while (the CLI finishing up, its result still being
2302        // collected) without the process driving it having died. `liveness`
2303        // is what actually tells the two apart, by corroborating
2304        // `driver_pid` against the process it names — see its own doc. Only
2305        // its strongest, provable answer licenses wiping this run's active
2306        // seats and failing it out from under whatever is still running it.
2307        let daemon_claims = is_working_on(home, &id, now);
2308        if state.liveness_under_with(home, daemon_claims, &query, &identity)
2309            != crate::run::Liveness::Dead
2310        {
2311            continue;
2312        }
2313        state.abandon("daemon");
2314        if let Err(e) = state.save_under(home) {
2315            tracing::warn!("could not persist abandoned run {id}: {e:#}");
2316            continue;
2317        }
2318        // This run never passes through `Runner::execute` again, so its end
2319        // is announced here.
2320        if let Some(notice) = notices::run_ended(&state) {
2321            notices::raise_in(home, notice);
2322        }
2323        // The seat that asked is gone for good now, exactly like any other
2324        // door `graph::Runner::settle_questions` closes the moment `status`
2325        // lands somewhere non-resumable - see that method's own doc. Nothing
2326        // else reaches this one before the next `janitor()` startup pass
2327        // (`clean::abandon_settled_questions`), and a daemon that stays up
2328        // for days must not leave an open question badging the operator
2329        // until it happens to restart.
2330        if let Err(e) = Questions::at(home.join("questions")).settle_run(&id, state.status) {
2331            tracing::warn!("abandon questions for {id}: {e:#}");
2332        }
2333        abandoned.push(id);
2334    }
2335    abandoned
2336}
2337
2338#[derive(Debug, Clone, Serialize, Deserialize)]
2339struct TaskWorker {
2340    opts: Opts,
2341    queue: PathBuf,
2342    task: String,
2343    #[serde(default)]
2344    lane: PermitKind,
2345    #[serde(default)]
2346    standalone: bool,
2347}
2348
2349fn task_jobs(home: &Path) -> Result<Vec<crate::worker::Job>> {
2350    Ok(crate::worker::active(home)?
2351        .into_iter()
2352        .filter(|job| job.kind == "task")
2353        .collect())
2354}
2355
2356fn owned_task_jobs(home: &Path) -> Result<Vec<crate::worker::Job>> {
2357    task_jobs(home)?
2358        .into_iter()
2359        .filter_map(
2360            |job| match serde_json::from_value::<TaskWorker>(job.payload.clone()) {
2361                Ok(request) if !request.standalone => Some(Ok(job)),
2362                Ok(_) => None,
2363                Err(error) => Some(Err(error.into())),
2364            },
2365        )
2366        .collect()
2367}
2368
2369/// Whether an independent scheduler can be controlled without its web parent.
2370#[must_use]
2371pub fn managed(home: &Path) -> bool {
2372    match crate::worker::active(home) {
2373        Err(error) => {
2374            tracing::error!("cannot determine scheduler ownership: {error:#}");
2375            true
2376        }
2377        Ok(jobs) if jobs.iter().any(|j| j.kind == "scheduler") => true,
2378        Ok(_) => {
2379            read_status(home).is_some_and(|r| r.worker_protocol == Some(1))
2380                && crate::worker::try_lease(home, "scheduler").map_or(true, |lease| lease.is_none())
2381        }
2382    }
2383}
2384
2385/// Start a detached, singleton scheduler using this executable generation.
2386pub async fn spawn_detached(home: &Path, opts: &Opts) -> Result<crate::worker::Job> {
2387    anyhow::ensure!(
2388        crate::worker::control(home, "scheduler")?.is_none() || !managed(home),
2389        "the scheduler is stopping; wait for retirement before starting it again"
2390    );
2391    crate::worker::spawn(
2392        home,
2393        "scheduler",
2394        "scheduler",
2395        serde_json::to_value(opts.absolute()?)?,
2396    )
2397    .await
2398}
2399
2400fn park_request_path(home: &Path, job: &crate::worker::Job) -> PathBuf {
2401    home.join("worker-parks").join(format!("{}.json", job.id))
2402}
2403
2404fn request_task_park(
2405    home: &Path,
2406    job: &crate::worker::Job,
2407    cause: crate::run::ParkCause,
2408) -> Result<()> {
2409    let path = park_request_path(home, job);
2410    std::fs::create_dir_all(path.parent().context("worker park directory")?)?;
2411    std::fs::write(path, serde_json::to_vec(&cause)?)?;
2412    crate::worker::request(home, &job.key, crate::worker::Control::Park)
2413}
2414
2415/// Stop dispatch immediately; only an explicit park reaches existing workers.
2416pub fn request_control(home: &Path, control: crate::worker::Control) -> Result<()> {
2417    if control == crate::worker::Control::Park {
2418        for job in owned_task_jobs(home)? {
2419            request_task_park(home, &job, crate::run::ParkCause::Operator)?;
2420        }
2421    }
2422    crate::worker::request(home, "scheduler", control)
2423}
2424
2425/// Replace the scheduler, leaving all active task and conversation jobs alone.
2426pub async fn restart_detached(home: &Path) -> Result<()> {
2427    let active = crate::worker::active(home)?;
2428    let prior = active.into_iter().find(|job| job.kind == "scheduler");
2429    if prior.is_none() && !managed(home) {
2430        return Ok(());
2431    }
2432    let opts: Opts = match prior {
2433        Some(job) => serde_json::from_value(job.payload)?,
2434        None => serde_json::from_slice(&std::fs::read(home.join("scheduler-options.json"))?)?,
2435    };
2436    request_control(home, crate::worker::Control::Stop)?;
2437    loop {
2438        let reserved = crate::worker::active(home)?
2439            .iter()
2440            .any(|job| job.kind == "scheduler");
2441        if !reserved && crate::worker::try_lease(home, "scheduler")?.is_some() {
2442            break;
2443        }
2444        tokio::time::sleep(Duration::from_millis(100)).await;
2445    }
2446    spawn_detached(home, &opts).await?;
2447    Ok(())
2448}
2449
2450/// Hidden worker dispatch entry. The worker infrastructure owns its job lease.
2451pub async fn run_scheduler_worker(home: &Path, payload: serde_json::Value) -> Result<()> {
2452    let opts: Opts = serde_json::from_value(payload)?;
2453    let _ = home;
2454    serve_until(opts, Stop::new()).await
2455}
2456
2457/// Execute and settle one task with the queue claim owned by this process.
2458pub async fn run_task_worker(home: &Path, payload: serde_json::Value) -> Result<()> {
2459    let request: TaskWorker = serde_json::from_value(payload)?;
2460    let queue = Queue::at(request.queue);
2461    let key = format!("task:{}", request.task);
2462    let job = task_jobs(home)?
2463        .into_iter()
2464        .find(|job| job.key == key)
2465        .context("task worker reservation is missing")?;
2466    let park_path = park_request_path(home, &job);
2467    // The spawning CLI/scheduler still owns the claim until spawn acknowledges
2468    // our durable reservation. Never execute before that ownership hand-off.
2469    let _claim = loop {
2470        match queue.claim(&request.task) {
2471            Ok(claim) => break claim,
2472            // Only contention (the claim file exists) is worth waiting out; an
2473            // I/O failure must end this worker instead of pinning its lane.
2474            Err(e) if e.chain().any(|c| c.is::<std::io::Error>()) => {
2475                return Err(e.context("claim the task for its worker"));
2476            }
2477            Err(_) => {
2478                sweep_stale_claims(&queue, STALE_CLAIM);
2479                tokio::time::sleep(Duration::from_millis(50)).await;
2480            }
2481        }
2482    };
2483    let mut task = queue.get(&request.task)?;
2484    if !task.status.runnable()
2485        || (!request.standalone
2486            && request.lane
2487                != permit_kind(land_resume_state(&task) == LandResume::Ready, task.urgent))
2488    {
2489        return Ok(());
2490    }
2491    let stop = Stop::new();
2492    let watcher = {
2493        let home = home.to_path_buf();
2494        let stop = stop.clone();
2495        let park_path = park_path.clone();
2496        tokio::spawn(async move {
2497            loop {
2498                if crate::worker::control(&home, &key).ok().flatten().is_some() {
2499                    let cause = std::fs::read(&park_path)
2500                        .ok()
2501                        .and_then(|body| serde_json::from_slice(&body).ok())
2502                        .unwrap_or(crate::run::ParkCause::Operator);
2503                    stop.park(cause);
2504                    return;
2505                }
2506                tokio::time::sleep(Duration::from_millis(200)).await;
2507            }
2508        })
2509    };
2510    let quota = if request.standalone {
2511        run_claimed_until(&request.opts, &queue, &mut task, &stop).await
2512    } else {
2513        let status = Arc::new(Mutex::new(Status::new()));
2514        Box::pin(attempt(
2515            &request.opts,
2516            &queue,
2517            &status,
2518            &stop,
2519            crate::graph::Pause::new(),
2520            &mut task,
2521        ))
2522        .await
2523    };
2524    watcher.abort();
2525    let _ = std::fs::remove_file(park_path);
2526    persist_cooldown(home, &quota).await?;
2527    Ok(())
2528}
2529
2530/// Launch the standalone task worker with a claim-to-reservation handoff.
2531/// Returns only after the worker owns a durable execution reservation.
2532pub async fn spawn_claimed(
2533    home: &Path,
2534    opts: &Opts,
2535    queue: &Queue,
2536    task: &Task,
2537    claim: crate::queue::Claim,
2538) -> Result<crate::worker::Job> {
2539    let request = TaskWorker {
2540        opts: opts.absolute()?,
2541        queue: std::path::absolute(queue.root())?,
2542        task: task.id.clone(),
2543        lane: permit_kind(land_resume_state(task) == LandResume::Ready, task.urgent),
2544        standalone: true,
2545    };
2546    let job = crate::worker::spawn(
2547        home,
2548        "task",
2549        &format!("task:{}", task.id),
2550        serde_json::to_value(request)?,
2551    )
2552    .await?;
2553    drop(claim);
2554    Ok(job)
2555}
2556
2557/// Transfer a standalone caller's claim only after the detached worker is live,
2558/// then follow that worker to its final (non-automatically-retrying) outcome.
2559pub async fn run_detached_claimed(
2560    opts: &Opts,
2561    queue: &Queue,
2562    task: &mut Task,
2563    claim: crate::queue::Claim,
2564) -> Result<()> {
2565    let home = crate::run::home();
2566    let job = spawn_claimed(&home, opts, queue, task, claim).await?;
2567    loop {
2568        // Reconcile execution leases even without a scheduler. A dead worker
2569        // must become a reported failure, never an infinite persisted wait.
2570        crate::worker::active(&home)?;
2571        let current = crate::worker::get(&home, &job.id)?;
2572        if matches!(
2573            current.status,
2574            crate::worker::Status::Finished | crate::worker::Status::Failed
2575        ) {
2576            *task = queue.get(&task.id)?;
2577            anyhow::ensure!(
2578                current.status != crate::worker::Status::Failed,
2579                "task worker failed: {}",
2580                current.result.as_deref().unwrap_or("unknown failure")
2581            );
2582            return Ok(());
2583        }
2584        tokio::time::sleep(Duration::from_millis(200)).await;
2585    }
2586}
2587
2588async fn persist_cooldown(home: &Path, quota: &[QuotaLoss]) -> Result<()> {
2589    if let Some(until) = cooldown_until(quota, Timestamp::now()) {
2590        let _lease = loop {
2591            if let Some(lease) = crate::worker::try_lease(home, "task-quota-cooldown")? {
2592                break lease;
2593            }
2594            tokio::time::sleep(Duration::from_millis(10)).await;
2595        };
2596        if read_cooldown(home)?.is_none_or(|previous| previous < until) {
2597            let path = home.join("task-quota-cooldown.json");
2598            let temporary = path.with_extension("json.tmp");
2599            std::fs::write(&temporary, serde_json::to_vec(&until)?)?;
2600            std::fs::rename(temporary, path)?;
2601        }
2602    }
2603    Ok(())
2604}
2605
2606fn read_cooldown(home: &Path) -> Result<Option<Timestamp>> {
2607    match std::fs::read(home.join("task-quota-cooldown.json")) {
2608        Ok(body) => Ok(Some(serde_json::from_slice(&body)?)),
2609        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
2610        Err(error) => Err(error.into()),
2611    }
2612}
2613
2614/// Run the loop until Ctrl-C, or until the queue drains with [`Opts::once`].
2615///
2616/// A thin wrapper over [`serve_until`] with a stop nothing but Ctrl-C ever
2617/// sets, so there is one loop body rather than two that drift apart the first
2618/// time the retry policy changes on only one of them.
2619pub async fn serve(opts: Opts) -> Result<()> {
2620    serve_until(opts, Stop::new()).await
2621}
2622
2623/// [`serve`], but stopping when `stop` is set as well as on Ctrl-C.
2624///
2625/// A stop retires dispatch promptly, including while a worker launch is being
2626/// acknowledged. Already reserved workers own their attempts independently and
2627/// finish with their started executable. Explicit park additionally forwards
2628/// durable park requests; neither scheduler retirement nor update does so.
2629///
2630/// While the queue is empty the stop is honoured within one wakeup rather than
2631/// one poll interval: the wait is a `select!` against [`Stop`]'s notify, so a
2632/// caller that taps stop does not sit through the remainder of a sleep.
2633pub async fn serve_until(opts: Opts, stop: Stop) -> Result<()> {
2634    let opts = opts.absolute()?;
2635    let home = crate::run::home();
2636    let _scheduler = crate::worker::try_lease(&home, "scheduler")?
2637        .context("another scheduler already owns this home")?;
2638    if let Some(reading) = read_status(&home)
2639        && legacy_scheduler_running(&reading, Timestamp::now(), std::process::id())
2640    {
2641        bail!("a legacy scheduler is still running; stop it before starting independent dispatch");
2642    }
2643    if !crate::worker::active(&home)?
2644        .iter()
2645        .any(|job| job.kind == "scheduler")
2646    {
2647        crate::worker::clear_control(&home, "scheduler")?;
2648    }
2649    std::fs::write(
2650        home.join("scheduler-options.json"),
2651        serde_json::to_vec(&opts)?,
2652    )?;
2653    let control = {
2654        let home = home.clone();
2655        let stop = stop.clone();
2656        tokio::spawn(async move {
2657            loop {
2658                match crate::worker::control(&home, "scheduler").ok().flatten() {
2659                    Some(crate::worker::Control::Park) => {
2660                        let _ = request_control(&home, crate::worker::Control::Park);
2661                        stop.park(crate::run::ParkCause::Operator);
2662                        break;
2663                    }
2664                    Some(crate::worker::Control::Stop) => {
2665                        stop.stop();
2666                        break;
2667                    }
2668                    None => {}
2669                }
2670                tokio::time::sleep(Duration::from_millis(100)).await;
2671            }
2672        })
2673    };
2674    let signal = {
2675        let stop = stop.clone();
2676        tokio::spawn(async move {
2677            if tokio::signal::ctrl_c().await.is_ok() {
2678                stop.stop();
2679                tracing::info!("scheduler shutdown requested; independent workers continue");
2680            }
2681        })
2682    };
2683
2684    let worktrees_root = opts
2685        .worktrees_root
2686        .clone()
2687        .unwrap_or_else(crate::run::default_worktree_root);
2688    let outcome = drive(
2689        &opts,
2690        &Queue::open(),
2691        &status_path(),
2692        &crate::run::home(),
2693        &worktrees_root,
2694        &stop,
2695    )
2696    .await;
2697
2698    signal.abort();
2699    control.abort();
2700    outcome
2701}
2702
2703/// The loop proper: setup, poll, teardown, with the queue and the status file
2704/// supplied rather than discovered.
2705///
2706/// All three of `home`, `worktrees_root` and the queue/status paths are
2707/// parameters rather than resolved here, for the same reason:
2708/// [`crate::run::home`] is process-global and its override is a `OnceLock`,
2709/// so a unit test that pinned it would fight every other test in the binary,
2710/// and a loop that resolved its own worktree bay could only be exercised
2711/// against the operator's real `~/wt/<repo>` - publishing over a live
2712/// daemon's status file, claiming tasks out of a live backlog, and, since
2713/// [`janitor`] runs on every idle tick, reclaiming worktrees out from under
2714/// whatever the operator actually has on disk.
2715async fn drive(
2716    opts: &Opts,
2717    queue: &Queue,
2718    status_file: &Path,
2719    home: &Path,
2720    worktrees_root: &Path,
2721    stop: &Stop,
2722) -> Result<()> {
2723    // The status file is a *snapshot*, not a stream of events: a reader only
2724    // ever wants the latest values, and every tick rewrites the whole file
2725    // anyway. A shared `Mutex<Status>` therefore says exactly what is meant,
2726    // while an mpsc channel would force the loop to re-send unchanged fields on
2727    // every heartbeat — or the heartbeat to keep its own shadow copy of them —
2728    // for no gain. The lock is only ever held across a field assignment, never
2729    // across an await.
2730    let recovery_cfg = prepare(&opts.repo, opts).ok();
2731    let daemon_cfg = recovery_cfg
2732        .as_ref()
2733        .map(|c| c.daemon.clone())
2734        .unwrap_or_default();
2735    let reconciled = crate::driver::reconcile(home);
2736    if !reconciled.is_empty() {
2737        tracing::info!(runs = ?reconciled, "reconciled stopped run drivers at startup");
2738    }
2739    let mut initial_status = Status::new();
2740    let owned = owned_task_jobs(home)?;
2741    initial_status.current = current_work(home, Timestamp::now())
2742        .into_iter()
2743        .filter(|current| {
2744            owned
2745                .iter()
2746                .any(|job| job.key.strip_prefix("task:") == Some(current.task.as_str()))
2747        })
2748        .collect();
2749    initial_status.idle = initial_status.current.is_empty();
2750    initial_status.question_recovery = Some(crate::question_delivery::Recovery::new(
2751        recovery_cfg.as_ref(),
2752        daemon_cfg.max_deputies,
2753    ));
2754    let status = Arc::new(Mutex::new(initial_status));
2755    write_status_to(status_file, &lock(&status)).context("publish the daemon status file")?;
2756    let beat = tokio::spawn(heartbeat(Arc::clone(&status), status_file.to_path_buf()));
2757
2758    // Read once at startup, not per task: how many runs this loop drives at
2759    // once is a property of the machine running it, not of whichever
2760    // repository a given task happens to name - see
2761    // `Config::daemon.max_concurrent_runs`'s doc for why that is a machine
2762    // fact in the same sense the agent roster is.
2763    let concurrency = max_concurrent(daemon_cfg.max_concurrent_runs);
2764
2765    tracing::info!(
2766        "magi serve: queue {} (poll {}s, {} attempts per task, {} run(s) at once{})",
2767        queue.root().display(),
2768        opts.poll.as_secs(),
2769        opts.max_attempts,
2770        concurrency,
2771        if daemon_cfg.pause_for_interrupts {
2772            ", interrupts enabled"
2773        } else {
2774            ""
2775        }
2776    );
2777
2778    let outcome = tokio::select! {
2779        outcome = poll(opts, queue, &status, home, worktrees_root, stop, DispatchLimits {
2780            max_concurrent: concurrency,
2781            pause_for_interrupts: daemon_cfg.pause_for_interrupts,
2782            recovery_cfg,
2783        }) => outcome,
2784        () = async {
2785            while !stop.stopped() {
2786                tokio::time::sleep(Duration::from_millis(25)).await;
2787            }
2788        } => {
2789            if stop.parking() {
2790                request_control(home, crate::worker::Control::Park)?;
2791            }
2792            Ok(())
2793        }
2794    };
2795
2796    stop.busy.store(0, Ordering::SeqCst);
2797    beat.abort();
2798    clear_status_at(status_file);
2799    outcome
2800}
2801
2802/// The checkouts the release watcher covers and `[daemon] release_stall_minutes`,
2803/// re-read each lap. An unreadable config yields `0` minutes: the watcher idles.
2804pub(crate) fn release_watch_settings_from_config(
2805    home: &Path,
2806    repo: &Path,
2807    cfg: &Config,
2808) -> (Vec<PathBuf>, u64) {
2809    let mut paths: Vec<PathBuf> = crate::repos::scan(&cfg.repos.roots)
2810        .into_iter()
2811        .map(|r| r.path)
2812        .collect();
2813    // Repositories the daemon has worked in, wherever they live: a task may
2814    // name one outside `[repos] roots`, and its release PR must be watched too.
2815    let mut known: Vec<PathBuf> = Queue::at(home.join("queue"))
2816        .list()
2817        .into_iter()
2818        .map(|t| t.repo)
2819        .collect();
2820    let runs = home.join("runs");
2821    for id in crate::run::list_ids_in(&runs) {
2822        let repo = std::fs::read_to_string(runs.join(&id).join("run.json"))
2823            .ok()
2824            .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
2825            .and_then(|v| v.get("repo")?.as_str().map(PathBuf::from));
2826        known.extend(repo);
2827    }
2828    for k in known {
2829        let k = k.canonicalize().unwrap_or(k);
2830        if k.join(".git").exists() && !paths.contains(&k) {
2831            paths.push(k);
2832        }
2833    }
2834    let own = repo.canonicalize().unwrap_or_else(|_| repo.to_path_buf());
2835    if !paths.contains(&own) {
2836        paths.push(own);
2837    }
2838    (paths, cfg.daemon.release_stall_minutes)
2839}
2840
2841/// Per-repo bound on one `git fetch`, so a dead remote cannot stall the pass.
2842const FETCH_TIMEOUT: Duration = Duration::from_secs(30);
2843
2844/// Refresh the status file on a fixed tick.
2845///
2846/// Separate from the loop because a run takes tens of minutes: a status file
2847/// written only between tasks would look stale for the whole of every run, and
2848/// a reader would report the daemon dead exactly while it was busiest.
2849async fn heartbeat(status: Arc<Mutex<Status>>, path: PathBuf) {
2850    loop {
2851        tokio::time::sleep(HEARTBEAT).await;
2852        let snapshot = {
2853            let mut guard = lock(&status);
2854            guard.updated_at = Timestamp::now();
2855            guard.clone()
2856        };
2857        if let Err(e) = write_status_to(&path, &snapshot) {
2858            // A failed heartbeat must not take the daemon down: the loop is the
2859            // product, the status file is only the window onto it.
2860            tracing::warn!("could not refresh the daemon status file: {e:#}");
2861        }
2862    }
2863}
2864
2865/// Whether a task's last run is sitting in `land`'s merge-approval wait, and
2866/// if so, whether that wait is over.
2867#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2868enum LandResume {
2869    /// The task's last run is not parked on a land approval; schedule it
2870    /// like any other candidate.
2871    NotLanding,
2872    /// Parked in `land`, waiting on a question nobody has answered yet.
2873    /// Left alone: attempting it now would only re-observe the same pull
2874    /// request and park again, spending a `gh` call on a decision that has
2875    /// not changed since the last time this was checked.
2876    StillWaiting,
2877    /// Parked in `land`, and the question is settled - answered or
2878    /// abandoned. Resuming this is the one kind of candidate that must not
2879    /// wait on a free [`Config::daemon`] concurrency slot: see [`poll`].
2880    Ready,
2881}
2882
2883/// Classify a runnable candidate by whether it is parked on a land-merge
2884/// approval. Read-only - no claim taken, nothing written - so it is cheap
2885/// enough to call on every candidate, every poll.
2886fn land_resume_state(task: &Task) -> LandResume {
2887    let Some(run_id) = task.runs.last() else {
2888        return LandResume::NotLanding;
2889    };
2890    let Ok(state) = RunState::load(run_id) else {
2891        return LandResume::NotLanding;
2892    };
2893    if !state.parked {
2894        return LandResume::NotLanding;
2895    }
2896    if state.status == RunStatus::Landing
2897        && state.land_approval.as_ref().is_some_and(|binding| {
2898            ask::Questions::open()
2899                .get(&binding.question)
2900                .is_ok_and(|q| {
2901                    q.run == state.id
2902                        && q.resolution()
2903                            .as_deref()
2904                            .is_some_and(crate::merge_decision::stops)
2905                })
2906        })
2907    {
2908        return LandResume::Ready;
2909    }
2910    if !implementers_match(task, &state) {
2911        return LandResume::NotLanding;
2912    }
2913    // Parked in `land` on the merge approval, or at `merge` on the owner's
2914    // word about a title/body the posting gate withheld. Told apart by the
2915    // run's own record, not by a status both can share.
2916    let node = if state.status == RunStatus::Landing {
2917        land::APPROVAL_NODE
2918    } else if state.github_text.as_ref().is_some_and(|g| !g.resolved) {
2919        crate::github_text::ASK_NODE
2920    } else {
2921        return LandResume::NotLanding;
2922    };
2923    let store = ask::Questions::open();
2924    let waiting = store
2925        .list()
2926        .into_iter()
2927        .filter(|q| &q.run == run_id && q.node == node)
2928        .max_by(|a, b| a.id.cmp(&b.id));
2929    let Some(mut q) = waiting else {
2930        return LandResume::Ready;
2931    };
2932    if !q.status.open() {
2933        // Repair spends agent resources and must obey concurrency and quota
2934        // waits; only a merge/hold decision may use the decision-only lane.
2935        if q.resolution().as_deref() == Some(land::REPAIR) {
2936            return if state.liveness(false) == Liveness::Live {
2937                LandResume::StillWaiting
2938            } else {
2939                LandResume::NotLanding
2940            };
2941        }
2942        return LandResume::Ready;
2943    }
2944    // `ask::ask_and_wait`'s own deadline is what used to retire a question
2945    // nobody ever answered; land's approval bypasses that wait entirely (see
2946    // `land::approval_gate`), so the same deadline has to be enforced here
2947    // instead, or `graph.answer_timeout` silently stops meaning anything for
2948    // a land approval and a run can sit `StillWaiting` forever with nobody
2949    // told to look at it.
2950    let timeout = Duration::from_secs(state.config.graph.answer_timeout);
2951    let elapsed = Timestamp::now().as_second() - q.asked_at.as_second();
2952    if elapsed >= 0 && elapsed as u64 >= timeout.as_secs() {
2953        q.abandon(format!(
2954            "no answer within {}s of asking",
2955            timeout.as_secs().max(1)
2956        ));
2957        // If this can't be persisted, do not treat the wait as settled on a
2958        // guess: fall through and try again next poll.
2959        if store.put(&mut q).is_ok() {
2960            return LandResume::Ready;
2961        }
2962    }
2963    LandResume::StillWaiting
2964}
2965
2966/// How often the loop rechecks for new work while something it already
2967/// started is still running, rather than sleeping out the whole
2968/// [`Opts::poll`] interval.
2969///
2970/// Short on purpose: this is what lets a land-merge approval that comes back
2971/// while another task is mid-competition be noticed and resumed within a
2972/// fraction of a second, not within the next multi-second poll.
2973const RECHECK_WHILE_BUSY: Duration = Duration::from_millis(200);
2974
2975/// How often [`poll`] rechecks the shared build cache against its cap at a
2976/// boundary between runs (see [`maybe_prune_cache_between_runs`]), instead of
2977/// waiting for the queue to run dry.
2978///
2979/// A queue that never empties means the `janitor` call at the bottom of this
2980/// loop's fully-idle branch can go unreached for as long as the backlog
2981/// lasts. Five minutes is far below a single gate's own 1200s timeout, so a
2982/// cache that started the day at its 10 GiB cap cannot grow anywhere near the
2983/// 81.8 GiB an idle-only check let it reach before this existed, and it is
2984/// well above the cost of a `dir_size` walk over a multi-gigabyte cache, so a
2985/// backlog of short tasks does not pay for that walk on every poll.
2986const CACHE_CHECK_INTERVAL_SECS: u64 = 5 * 60;
2987
2988/// State of [`poll`]'s own interrupt-scheduling sequence - see
2989/// [`crate::config::Daemon::pause_for_interrupts`]. Advanced once per tick by
2990/// [`advance_interrupt`] and consulted by [`interrupt_gate`], both pure and
2991/// both kept free of `Task`'s non-identity fields on purpose: every decision
2992/// here turns only on task ids and which ones are in flight, so the "never
2993/// more than one run at once" and "exactly one resume" invariants can be
2994/// pinned down with a plain `#[test]`, no `Runner`, no tokio, no fixture
2995/// queue - which is exactly the coverage this feature's first two attempts
2996/// were missing.
2997///
2998/// Persisted after every transition, so scheduler replacement preserves the
2999/// interrupt's exclusive turn and the interrupted task's priority resume.
3000#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
3001enum Interrupt {
3002    /// No interrupt sequence in progress. Ordinary dispatch applies.
3003    Idle,
3004    /// `interrupt_task` is runnable and exactly one other run is in flight;
3005    /// `parked` names that one task's id. Dispatch is withheld from
3006    /// everyone, including `interrupt_task` itself, until it has left
3007    /// flight - only then is `interrupt_task` let through.
3008    ///
3009    /// `parked` is a `Vec` rather than a bare id for symmetry with
3010    /// [`Interrupt::Running`] and [`Interrupt::Resuming`], but
3011    /// [`advance_interrupt`]'s own `Idle` branch only ever starts a sequence
3012    /// when exactly one run is in flight, so it is guaranteed to hold
3013    /// exactly one entry in practice - see that branch's own doc for why
3014    /// more than one is deliberately never attempted.
3015    Parking {
3016        parked: Vec<String>,
3017        interrupt_task: String,
3018    },
3019    /// `interrupt_task` is dispatched and in flight alone. Dispatch is
3020    /// withheld from everyone until it leaves flight - merged, failed, held,
3021    /// it makes no difference - at which point the sequence moves to
3022    /// [`Interrupt::Resuming`], never straight back to [`Interrupt::Idle`]:
3023    /// going straight to `Idle` would hand `parked` back to ordinary
3024    /// priority-order dispatch, where a higher-priority task filed in the
3025    /// meantime could start ahead of it.
3026    Running {
3027        parked: Vec<String>,
3028        interrupt_task: String,
3029    },
3030    /// `interrupt_task` left flight; `parked` still names the one run this
3031    /// sequence owes a resume. Dispatch is withheld from everyone except
3032    /// that task - see [`interrupt_gate`] - so the resume this feature
3033    /// promises is never raced by, or run alongside, an unrelated candidate.
3034    /// Ends the moment it is seen in flight, or - see `advance_interrupt`'s
3035    /// own doc on abandonment - the moment it is no longer runnable at all.
3036    Resuming { parked: Vec<String> },
3037}
3038
3039/// One tick of the interrupt scheduler's own state machine. Pure: `in_flight`
3040/// and `runnable` are read-only snapshots of this tick's reality, and the
3041/// only side effect the caller still owes the world is asking whichever
3042/// `Pause` handles `parked` names to actually park - see [`poll`]'s own call
3043/// site.
3044///
3045/// `runnable` only has to carry `id` and `interrupt`; the whole [`Task`] is
3046/// accepted rather than a narrower type because that is what [`poll`] already
3047/// has on hand from [`runnable`], and building a second, smaller list on
3048/// every tick just to satisfy this signature would cost more than it proves.
3049///
3050/// Abandonment: [`Interrupt::Parking`] and [`Interrupt::Resuming`] both fall
3051/// back to a task they are waiting on no longer being [`runnable`] - held,
3052/// blocked, deleted, or finished by some other means entirely, all of which
3053/// an operator can do to a task sitting in the queue with no claim on it at
3054/// all, at any moment, interrupt sequence or not. Without this check the
3055/// sequence would wait forever for a dispatch that can never come, and
3056/// `interrupt_gate` would withhold every other task in the queue right along
3057/// with it - a single `magi task hold` on the wrong id turning into a
3058/// daemon that never dispatches anything again.
3059fn advance_interrupt(state: Interrupt, in_flight: &[String], runnable: &[Task]) -> Interrupt {
3060    match state {
3061        Interrupt::Idle => {
3062            // Not just "something to interrupt": exactly one thing. More
3063            // than one run in flight only happens above the default
3064            // `max_concurrent_runs = 1`, and `parked` guarantees "exactly
3065            // one resume, never run alongside anything else" only because
3066            // it is only ever seeded with exactly one id - see
3067            // `Interrupt::Resuming`'s own doc on why releasing more than one
3068            // parked id back to ordinary dispatch cannot be made safe
3069            // against that same setting's own extra concurrency slots.
3070            // Waiting here for the herd to settle to one is the
3071            // simplification this feature's own constraints ask for rather
3072            // than a second concurrency model to reconcile with the first.
3073            if in_flight.len() != 1 {
3074                return Interrupt::Idle;
3075            }
3076            match runnable.iter().find(|t| t.interrupt) {
3077                Some(t) => Interrupt::Parking {
3078                    parked: in_flight.to_vec(),
3079                    interrupt_task: t.id.clone(),
3080                },
3081                None => Interrupt::Idle,
3082            }
3083        }
3084        Interrupt::Parking {
3085            parked,
3086            interrupt_task,
3087        } => {
3088            if in_flight.iter().any(|id| parked.contains(id)) {
3089                // Still waiting for what was in flight to actually stop.
3090                Interrupt::Parking {
3091                    parked,
3092                    interrupt_task,
3093                }
3094            } else if in_flight.contains(&interrupt_task) {
3095                Interrupt::Running {
3096                    parked,
3097                    interrupt_task,
3098                }
3099            } else if runnable.iter().any(|t| t.id == interrupt_task) {
3100                // The parked run(s) are gone, but the interrupt task has not
3101                // been dispatched yet on this tick - `interrupt_gate` is
3102                // what lets it through next.
3103                Interrupt::Parking {
3104                    parked,
3105                    interrupt_task,
3106                }
3107            } else {
3108                // The interrupt task itself is no longer runnable - see this
3109                // function's own doc on abandonment. The parked run(s) still
3110                // get their guaranteed resume; there is simply no interrupt
3111                // to run ahead of them any longer.
3112                Interrupt::Resuming { parked }
3113            }
3114        }
3115        Interrupt::Running {
3116            parked,
3117            interrupt_task,
3118        } => {
3119            if in_flight.contains(&interrupt_task) {
3120                Interrupt::Running {
3121                    parked,
3122                    interrupt_task,
3123                }
3124            } else {
3125                // The interrupt task's own run reached a terminal status,
3126                // whichever one - this is the *only* trigger that moves the
3127                // sequence on, driven straight off the same in-flight
3128                // bookkeeping `poll` already reaps every tick, not a second,
3129                // independent poll of anything.
3130                Interrupt::Resuming { parked }
3131            }
3132        }
3133        Interrupt::Resuming { parked } => {
3134            if in_flight.iter().any(|id| parked.contains(id)) {
3135                // One of the parked runs has been dispatched - the resume
3136                // this sequence owed is fulfilled. Whatever else is left in
3137                // `parked` (ordinarily nothing, at the default concurrency
3138                // of one) rejoins ordinary priority-order dispatch, same as
3139                // any other runnable task.
3140                Interrupt::Idle
3141            } else if runnable.iter().any(|t| parked.contains(&t.id)) {
3142                Interrupt::Resuming { parked }
3143            } else {
3144                // Abandonment (see this function's own doc): nothing left in
3145                // `parked` is even runnable any longer.
3146                Interrupt::Idle
3147            }
3148        }
3149    }
3150}
3151
3152/// [`Interrupt`], but with [`crate::config::Daemon::pause_for_interrupts`]
3153/// folded in: disabled, the sequence can never leave [`Interrupt::Idle`], so
3154/// a task marked [`Task::interrupt`] on a daemon that has not opted in is
3155/// indistinguishable from any other runnable task - exactly the "off does
3156/// nothing" this feature promises.
3157fn advance_interrupt_tick(
3158    enabled: bool,
3159    state: Interrupt,
3160    in_flight: &[String],
3161    runnable: &[Task],
3162) -> Interrupt {
3163    if !enabled {
3164        return Interrupt::Idle;
3165    }
3166    advance_interrupt(state, in_flight, runnable)
3167}
3168
3169/// Which of this tick's runnable candidates the interrupt sequence actually
3170/// allows to be dispatched. Pure, and separate from [`advance_interrupt`] so
3171/// each half is assertable on its own: this is the half that keeps a
3172/// competition and an interrupt from ever running at the same moment.
3173fn interrupt_gate(state: &Interrupt, in_flight: &[String], candidates: Vec<Task>) -> Vec<Task> {
3174    match state {
3175        Interrupt::Idle => candidates,
3176        Interrupt::Parking {
3177            parked,
3178            interrupt_task,
3179        } => {
3180            if in_flight.iter().any(|id| parked.contains(id)) {
3181                Vec::new()
3182            } else {
3183                candidates
3184                    .into_iter()
3185                    .filter(|t| &t.id == interrupt_task)
3186                    .collect()
3187            }
3188        }
3189        Interrupt::Running { .. } => Vec::new(),
3190        // At most one: even if `parked` names more than one id (more than
3191        // one run was in flight when the sequence began, only possible
3192        // above the default `max_concurrent_runs = 1`), only the first match
3193        // is offered. Capping this to a single candidate - not merely to
3194        // `parked`'s own ids - is what makes "exactly one resume, never two
3195        // dispatched together" true regardless of how many ordinary slots
3196        // happen to be free this tick.
3197        Interrupt::Resuming { parked } => candidates
3198            .into_iter()
3199            .find(|t| parked.contains(&t.id))
3200            .into_iter()
3201            .collect(),
3202    }
3203}
3204
3205/// The daemon-loop knobs [`poll`] needs from [`crate::config::Daemon`],
3206/// bundled into one parameter so `poll`'s own signature stays readable -
3207/// see [`drive`]'s call site for where these are actually read.
3208struct DispatchLimits {
3209    /// How many *ordinary* candidates run at once. See
3210    /// [`crate::config::Daemon::max_concurrent_runs`].
3211    max_concurrent: usize,
3212    /// See [`crate::config::Daemon::pause_for_interrupts`].
3213    pause_for_interrupts: bool,
3214    recovery_cfg: Option<Config>,
3215}
3216
3217/// Which semaphore, if any, dispatching a candidate should draw its permit
3218/// from.
3219///
3220/// Pure and separate from [`poll`]'s loop body for the same reason
3221/// [`advance_interrupt`] is: the choice between "skip the ordinary pool
3222/// entirely", "spend the one urgent slot" and "spend an ordinary slot" is
3223/// exactly the policy this feature adds, and a policy only exercisable by
3224/// running the whole loop is a policy nobody checks.
3225#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
3226enum PermitKind {
3227    /// A land-merge resume (see [`LandResume::Ready`]): bypasses every slot.
3228    /// Checked first - a task can be both a land resume and marked
3229    /// [`Task::urgent`], and the resume's own "must not queue behind
3230    /// anything" guarantee takes precedence.
3231    None,
3232    /// [`Task::urgent`]: the one extra slot in `urgent_sem`, spent instead of
3233    /// (never in addition to trying) the ordinary pool. This is what lets
3234    /// an urgent task dispatch while every ordinary slot is already checked
3235    /// out, and what stops a second urgent task from opening a third run: it
3236    /// waits on this same one-slot semaphore rather than falling through to
3237    /// the ordinary one.
3238    Urgent,
3239    /// The ordinary `max_concurrent_runs` pool - unaffected by either of the
3240    /// above.
3241    #[default]
3242    Ordinary,
3243}
3244
3245/// [`PermitKind`] for one candidate. `priority` is [`LandResume::Ready`]'s
3246/// own boolean, already computed by the caller from [`land_resume_state`].
3247fn permit_kind(priority: bool, urgent: bool) -> PermitKind {
3248    if priority {
3249        PermitKind::None
3250    } else if urgent {
3251        PermitKind::Urgent
3252    } else {
3253        PermitKind::Ordinary
3254    }
3255}
3256
3257fn available_slots(requests: &[TaskWorker], limit: usize) -> (usize, usize) {
3258    let (ordinary, urgent) = requests.iter().filter(|request| !request.standalone).fold(
3259        (0usize, 0usize),
3260        |(ordinary, urgent), request| match request.lane {
3261            PermitKind::Ordinary => (ordinary + 1, urgent),
3262            PermitKind::Urgent => (ordinary, urgent + 1),
3263            PermitKind::None => (ordinary, urgent),
3264        },
3265    );
3266    (
3267        limit.saturating_sub(ordinary),
3268        1usize.saturating_sub(urgent),
3269    )
3270}
3271
3272struct DispatchClaim {
3273    task: Task,
3274    claim: crate::queue::Claim,
3275    permit: Option<tokio::sync::OwnedSemaphorePermit>,
3276}
3277
3278/// Claim and re-read before choosing a lane: a queue listing may predate an
3279/// urgent toggle. All unsuccessful paths release the claim without an attempt.
3280fn claim_dispatch(
3281    queue: &Queue,
3282    candidate: &Task,
3283    cooling_down: bool,
3284    ordinary: &Arc<tokio::sync::Semaphore>,
3285    urgent: &Arc<tokio::sync::Semaphore>,
3286) -> Option<DispatchClaim> {
3287    let Ok(claim) = queue.claim(&candidate.id) else {
3288        tracing::info!("task {} is claimed elsewhere; skipping", candidate.short());
3289        return None;
3290    };
3291    let task = match queue.get(&candidate.id) {
3292        Ok(t) if t.status.runnable() => t,
3293        Ok(_) => return None,
3294        Err(e) => {
3295            tracing::warn!("could not re-read task {}: {e:#}", candidate.short());
3296            return None;
3297        }
3298    };
3299    let resume = land_resume_state(&task);
3300    if resume == LandResume::StillWaiting {
3301        return None;
3302    }
3303    let priority = resume == LandResume::Ready;
3304    if !priority && cooling_down {
3305        return None;
3306    }
3307    // Never borrow an ordinary slot when the single urgent slot is busy.
3308    // Keep scanning other candidates when either pool has no free slot.
3309    let permit = match permit_kind(priority, task.urgent) {
3310        PermitKind::None => None,
3311        PermitKind::Urgent => Some(Arc::clone(urgent).try_acquire_owned().ok()?),
3312        PermitKind::Ordinary => Some(Arc::clone(ordinary).try_acquire_owned().ok()?),
3313    };
3314    // Persist the dispatch order under the claim, before config discovery or
3315    // coordination waits can reorder concurrent starts.
3316    let stamp = Timestamp::now();
3317    let mut task = task;
3318    task.dispatched_at = Some(stamp);
3319    if let Err(e) = queue.modify(&task.id, |t| {
3320        t.dispatched_at = Some(stamp);
3321        true
3322    }) {
3323        tracing::warn!("could not record dispatch order of {}: {e:#}", task.short());
3324    }
3325    Some(DispatchClaim {
3326        task,
3327        claim,
3328        permit,
3329    })
3330}
3331
3332/// Poll the queue until stopped, factored out so [`drive`] owns only setup and
3333/// teardown and cannot skip the teardown on an early return.
3334///
3335/// `limits.max_concurrent` bounds how many *ordinary* candidates run at once,
3336/// see [`crate::config::Daemon::max_concurrent_runs`]. A run parked on a
3337/// land approval that has since been answered is dispatched outside that
3338/// bound the moment [`land_resume_state`] reports it [`LandResume::Ready`]:
3339/// the whole point of parking there is that it must not queue behind
3340/// whatever else the loop happens to be running, even at the default of one.
3341/// A [`Task::urgent`] candidate is exempt from the same bound the same way,
3342/// through its own one-slot `urgent_sem` (see [`permit_kind`]). Every one of
3343/// these exemptions is still subject to the interrupt gate below, urgent
3344/// included: a land-resume or urgent candidate is exactly as much "something
3345/// else running" as an ordinary one from the interrupt sequence's point of
3346/// view, and letting either slip through while a run is being parked, or
3347/// while the interrupt task itself has the floor, is precisely the second
3348/// run 75dd's own "at most one run, ever, at once" guarantee must never
3349/// allow - see [`Interrupt`]'s own doc.
3350#[derive(Serialize, Deserialize)]
3351struct ServiceWorker {
3352    opts: Opts,
3353    queue: PathBuf,
3354    worktrees_root: PathBuf,
3355    operation: String,
3356}
3357
3358fn load_conductor(home: &Path) -> Result<Conductor> {
3359    match std::fs::read(home.join("conductor-state.json")) {
3360        Ok(body) => serde_json::from_slice(&body).context("read conductor state"),
3361        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(Conductor::new()),
3362        Err(error) => Err(error.into()),
3363    }
3364}
3365
3366fn conductor_work(queue: &Queue, home: &Path) -> (Vec<Task>, Vec<Task>, Vec<Task>) {
3367    let live: std::collections::BTreeSet<_> = current_work(home, Timestamp::now())
3368        .into_iter()
3369        .map(|c| c.task)
3370        .collect();
3371    let stalled = stalled_tasks(queue, home, Timestamp::now());
3372    let stalled_ids: std::collections::BTreeSet<_> = stalled.iter().map(|t| t.id.clone()).collect();
3373    let finished = finished_tasks(queue)
3374        .into_iter()
3375        .filter(|t| !live.contains(&t.id) && !stalled_ids.contains(&t.id))
3376        .filter(|t| !awaiting_resume_with(t, |id| RunState::load_under(id, home)))
3377        .collect();
3378    let queued = queued_tasks(queue)
3379        .into_iter()
3380        .filter(|t| !live.contains(&t.id))
3381        .collect();
3382    (queued, stalled, finished)
3383}
3384
3385async fn start_service(
3386    home: &Path,
3387    opts: &Opts,
3388    queue: &Queue,
3389    worktrees_root: &Path,
3390    operation: &str,
3391    cfg: Option<&Config>,
3392    busy: bool,
3393) -> Result<()> {
3394    // Fetch policy is live configuration, unlike question recovery capacity.
3395    // Reload before both cadence and eligibility, including disabled -> enabled.
3396    let fetch_cfg = if operation == "fetch" {
3397        match prepare(&opts.repo, opts) {
3398            Ok(cfg) => Some(cfg),
3399            Err(error) => {
3400                tracing::warn!("could not reload periodic fetch configuration: {error:#}");
3401                return Ok(());
3402            }
3403        }
3404    } else {
3405        None
3406    };
3407    let cfg = fetch_cfg.as_ref().or(cfg);
3408    let ticks = home.join("service-ticks");
3409    std::fs::create_dir_all(&ticks)?;
3410    let receipt = ticks.join(format!("daemon-{operation}"));
3411    let last = std::fs::read(&receipt)
3412        .ok()
3413        .and_then(|body| serde_json::from_slice::<Timestamp>(&body).ok());
3414    let interval = match operation {
3415        "disk-pressure" => 30,
3416        "cache" => CACHE_CHECK_INTERVAL_SECS,
3417        "fetch" => cfg.map_or(60, |cfg| cfg.repos.fetch_interval.max(1)),
3418        _ => opts.poll.as_secs().max(1),
3419    };
3420    let now = Timestamp::now();
3421    if !cache_check_due(last, now, interval) {
3422        return Ok(());
3423    }
3424    let key = format!("service:daemon-{operation}");
3425    if crate::worker::active(home)?
3426        .iter()
3427        .any(|job| job.key == key)
3428    {
3429        return Ok(());
3430    }
3431    let eligible = match operation {
3432        "conductor" => {
3433            let (queued, stalled, finished) = conductor_work(queue, home);
3434            !(queued.is_empty() && stalled.is_empty() && finished.is_empty())
3435                && load_conductor(home)?.worth_a_look(queue, &stalled, &finished)
3436        }
3437        "maintenance" => Questions::at(home.join("questions"))
3438            .list()
3439            .iter()
3440            .any(|q| {
3441                q.chosen_action().is_some_and(|action| action.maintenance())
3442                    && !home
3443                        .join("choice-actions")
3444                        .join(format!("{}.json", q.id))
3445                        .exists()
3446            }),
3447        "housekeeping" => !busy && runnable(queue).is_empty(),
3448        "cache" => {
3449            !busy
3450                && !runnable(queue).is_empty()
3451                && cfg.is_some_and(|cfg| cfg.disk.cache_limit_bytes > 0)
3452        }
3453        "disk-pressure" => busy,
3454        "fetch" => {
3455            cfg.is_some_and(|cfg| cfg.repos.fetch_interval > 0 && !cfg.repos.roots.is_empty())
3456        }
3457        _ => false,
3458    };
3459    if !eligible {
3460        return Ok(());
3461    }
3462    let request = ServiceWorker {
3463        opts: opts.absolute()?,
3464        queue: std::path::absolute(queue.root())?,
3465        worktrees_root: std::path::absolute(worktrees_root)?,
3466        operation: operation.to_owned(),
3467    };
3468    if let Err(error) =
3469        crate::worker::spawn(home, "daemon-service", &key, serde_json::to_value(request)?).await
3470    {
3471        tracing::warn!(operation, "could not launch daemon service: {error:#}");
3472    }
3473    std::fs::write(receipt, serde_json::to_vec(&now)?)?;
3474    Ok(())
3475}
3476
3477/// Finite maintenance jobs survive scheduler replacement without duplicating
3478/// agent calls or cancelling an operation between its effect and receipt.
3479pub async fn run_service_worker(home: &Path, payload: serde_json::Value) -> Result<()> {
3480    let request: ServiceWorker = serde_json::from_value(payload)?;
3481    let opts = &request.opts;
3482    let queue = Queue::at(request.queue);
3483    let questions = Questions::at(home.join("questions"));
3484    match request.operation.as_str() {
3485        "maintenance" => apply_maintenance_choices(&queue, &questions, home).await,
3486        "conductor" => {
3487            let path = home.join("conductor-state.json");
3488            let mut conductor = load_conductor(home)?;
3489            let (queued, stalled, finished) = conductor_work(&queue, home);
3490            if !(queued.is_empty() && stalled.is_empty() && finished.is_empty())
3491                && conductor.worth_a_look(&queue, &stalled, &finished)
3492            {
3493                let cfg = prepare(&opts.repo, opts)?;
3494                conductor
3495                    .maybe_run(
3496                        &cfg,
3497                        &opts.repo,
3498                        &queue,
3499                        &questions,
3500                        home,
3501                        &queued,
3502                        &stalled,
3503                        &finished,
3504                        opts.max_attempts,
3505                    )
3506                    .await;
3507                let temporary = path.with_extension("json.tmp");
3508                std::fs::write(&temporary, serde_json::to_vec(&conductor)?)?;
3509                std::fs::rename(temporary, path)?;
3510            }
3511        }
3512        "fetch" => {
3513            let cfg = prepare(&opts.repo, opts)?;
3514            if cfg.repos.fetch_interval > 0 && !cfg.repos.roots.is_empty() {
3515                let outcome = clean::fetch_origins(&cfg.repos.roots, FETCH_TIMEOUT, || false).await;
3516                tracing::debug!("fetch origins: {outcome:?}");
3517            }
3518        }
3519        "cache" => {
3520            if task_jobs(home)?.is_empty() {
3521                let mut checked = None;
3522                maybe_prune_cache_between_runs(
3523                    &opts.repo,
3524                    opts,
3525                    home,
3526                    &Stop::new(),
3527                    &mut checked,
3528                    Timestamp::now(),
3529                )
3530                .await;
3531            }
3532        }
3533        "disk-pressure" => {
3534            if let Ok(cfg) = prepare(&opts.repo, opts) {
3535                crate::retention::sweep(home, &cfg.disk, Timestamp::now()).await;
3536                if let Err(e) = clean::prune_cache_if_over_limit(&cfg, home) {
3537                    tracing::warn!("disk pressure cache cleanup failed: {e:#}");
3538                }
3539                for current in current_work(home, Timestamp::now()) {
3540                    if let Ok(state) = RunState::load_under(&current.run, home)
3541                        && let Some(reason) = disk_gate(&state.repo, &state.config)
3542                    {
3543                        for job in owned_task_jobs(home)?.into_iter().filter(|job| {
3544                            job.key.strip_prefix("task:") == Some(current.task.as_str())
3545                        }) {
3546                            request_task_park(
3547                                home,
3548                                &job,
3549                                crate::run::ParkCause::Interrupt {
3550                                    reason: reason.clone(),
3551                                },
3552                            )?;
3553                        }
3554                        notices::raise_in(
3555                            home,
3556                            Notice::warn(
3557                                "disk:pressure",
3558                                "Disk pressure persists after cleanup; active workers were asked to checkpoint and park. New starts and resumes wait for free space.",
3559                            ),
3560                        );
3561                    }
3562                }
3563            }
3564        }
3565        "housekeeping" => {
3566            if task_jobs(home)?.is_empty() {
3567                if let Ok(cfg) = prepare(&opts.repo, opts) {
3568                    crate::retention::sweep(home, &cfg.disk, Timestamp::now()).await;
3569                }
3570                janitor(&opts.repo, opts, home, &request.worktrees_root).await;
3571                resweep_superseded_attempts(&queue, home);
3572                triage_held(&queue, home, opts).await;
3573            }
3574        }
3575        operation => bail!("unknown daemon service {operation}"),
3576    }
3577    Ok(())
3578}
3579
3580/// Only a different request under the same key is a dispatch handoff race.
3581/// Our own unacknowledged reservation is a startup failure, not competing work:
3582/// stop dispatch without fencing it, so an already detached child may still run.
3583fn task_worker_spawn_error(home: &Path, request: &TaskWorker, error: anyhow::Error) -> Result<()> {
3584    let key = format!("task:{}", request.task);
3585    let payload = serde_json::to_value(request)?;
3586    if crate::worker::active(home)?
3587        .iter()
3588        .any(|job| job.key == key && job.kind == "task" && job.payload != payload)
3589    {
3590        tracing::warn!(
3591            "task {} acquired a competing worker reservation: {error:#}",
3592            crate::run::short_of(&request.task)
3593        );
3594        return Ok(());
3595    }
3596    tracing::error!("task worker startup stopped dispatch: {error:#}");
3597    notices::raise_in(
3598        home,
3599        Notice::error(
3600            "loop:worker-startup",
3601            "A task worker could not be started or acknowledged. Scheduler dispatch stopped; already reserved workers may still complete. Startup details are kept in the local execution log.",
3602        ),
3603    );
3604    Err(error).with_context(|| {
3605        format!(
3606            "cannot start task {} worker; dispatcher stopped; any unacknowledged reservation remains occupied",
3607            crate::run::short_of(&request.task)
3608        )
3609    })
3610}
3611
3612async fn poll(
3613    opts: &Opts,
3614    queue: &Queue,
3615    status: &Arc<Mutex<Status>>,
3616    home: &Path,
3617    worktrees_root: &Path,
3618    stop: &Stop,
3619    limits: DispatchLimits,
3620) -> Result<()> {
3621    let mut attempted = Vec::new();
3622    let interrupt_path = home.join("scheduler-interrupt.json");
3623    let mut interrupt = match std::fs::read(&interrupt_path) {
3624        Ok(body) => serde_json::from_slice(&body).context("read scheduler interrupt state")?,
3625        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Interrupt::Idle,
3626        Err(error) => return Err(error.into()),
3627    };
3628    let mut previous = std::collections::BTreeSet::<String>::new();
3629    while !stop.stopped() {
3630        let jobs: Vec<_> = crate::worker::active(home)?
3631            .into_iter()
3632            .filter(|job| job.kind == "task")
3633            .collect();
3634        let requests: Vec<_> = jobs
3635            .iter()
3636            .map(|job| {
3637                serde_json::from_value::<TaskWorker>(job.payload.clone())
3638                    .with_context(|| format!("read live task worker {}", job.id))
3639            })
3640            .collect::<Result<_>>()?;
3641        let live: std::collections::BTreeSet<_> = requests.iter().map(|r| r.task.clone()).collect();
3642        let owned: std::collections::BTreeSet<_> = requests
3643            .iter()
3644            .filter(|r| !r.standalone)
3645            .map(|r| r.task.clone())
3646            .collect();
3647        let current: Vec<_> = requests
3648            .iter()
3649            .filter(|request| !request.standalone)
3650            .map(|request| Current {
3651                task: request.task.clone(),
3652                run: queue
3653                    .get(&request.task)
3654                    .ok()
3655                    .and_then(|task| task.runs.last().cloned())
3656                    .unwrap_or_default(),
3657            })
3658            .collect();
3659        {
3660            let mut snapshot = lock(status);
3661            snapshot.polls += 1;
3662            snapshot.completed += previous.difference(&owned).count();
3663            snapshot.current = current;
3664            snapshot.idle = owned.is_empty();
3665        }
3666        previous = owned.clone();
3667        stop.busy.store(owned.len(), Ordering::SeqCst);
3668
3669        // Slots are reconstructed from durable reservations and OS-held worker
3670        // leases, not this scheduler's lifetime or mutable task urgency.
3671        let (ordinary, urgent) = available_slots(&requests, limits.max_concurrent);
3672        let sem = Arc::new(tokio::sync::Semaphore::new(ordinary));
3673        let urgent_sem = Arc::new(tokio::sync::Semaphore::new(urgent));
3674        let mut permits = Vec::new();
3675        sweep_stale_claims(queue, STALE_CLAIM);
3676        reclaim_orphaned_running(queue, opts.max_attempts, &live);
3677        reclaim_abandoned_runs(home, Timestamp::now());
3678        let questions = Questions::at(home.join("questions"));
3679
3680        // Deterministic: no model, run before the conductor sees anything so
3681        // its input reflects the queue's current, already-resolved state.
3682        crate::task_chat::sweep(
3683            queue,
3684            &crate::talk::Talks::at(home.join("talks")),
3685            &questions,
3686            home,
3687        );
3688        repair_choice_offers(&questions);
3689        resolve_blockers(queue, &questions);
3690        apply_task_choice_actions(queue, &questions, home);
3691        triage::apply_answers(queue, &questions);
3692        reconcile_task_questions(queue, &questions);
3693        let talks = crate::talk::Talks::at(home.join("talks"));
3694        crate::chat_notify::sweep(queue, &talks, home);
3695        crate::chat_watch::sweep(home, &talks);
3696        let mut turns = tokio::task::JoinSet::new();
3697        crate::chat_notify::start_turns(&talks, stop.parking(), &mut turns);
3698        // start_turns dispatches detached turns; no paid work lives in this set.
3699        while turns.join_next().await.is_some() {}
3700
3701        crate::consult::start_services(
3702            home,
3703            &opts.repo,
3704            limits.recovery_cfg.as_ref(),
3705            opts.config.as_deref(),
3706        )
3707        .await?;
3708        for operation in [
3709            "conductor",
3710            "maintenance",
3711            "housekeeping",
3712            "disk-pressure",
3713            "cache",
3714            "fetch",
3715        ] {
3716            start_service(
3717                home,
3718                opts,
3719                queue,
3720                worktrees_root,
3721                operation,
3722                limits.recovery_cfg.as_ref(),
3723                !live.is_empty(),
3724            )
3725            .await?;
3726        }
3727        let latest = crate::worker::active(home)?;
3728        if latest.iter().any(|job| job.key == "service:daemon-cache")
3729            || latest
3730                .iter()
3731                .any(|job| job.kind == "task" && !jobs.iter().any(|known| known.id == job.id))
3732        {
3733            stop.idle(RECHECK_WHILE_BUSY.min(opts.poll)).await;
3734            continue;
3735        }
3736        let candidates: Vec<Task> = runnable(queue)
3737            .into_iter()
3738            .filter(|task| !live.contains(&task.id))
3739            .filter(|task| !opts.once || !attempted.contains(&task.id))
3740            .collect();
3741        let in_flight: Vec<_> = owned.iter().cloned().collect();
3742        let prior_interrupt = std::mem::discriminant(&interrupt);
3743        interrupt = advance_interrupt_tick(
3744            limits.pause_for_interrupts,
3745            interrupt,
3746            &in_flight,
3747            &candidates,
3748        );
3749        // Same-variant transitions preserve their identities. Only an actual
3750        // transition needs a durable write, atomically before sending controls.
3751        if prior_interrupt != std::mem::discriminant(&interrupt) {
3752            let temporary = interrupt_path.with_extension("json.tmp");
3753            std::fs::write(&temporary, serde_json::to_vec(&interrupt)?)?;
3754            std::fs::rename(temporary, &interrupt_path)?;
3755        }
3756        if let Interrupt::Parking {
3757            parked,
3758            interrupt_task,
3759        } = &interrupt
3760        {
3761            for job in jobs.iter().filter(|job| {
3762                parked
3763                    .iter()
3764                    .any(|id| job.key.strip_prefix("task:") == Some(id.as_str()))
3765            }) {
3766                if crate::worker::control(home, &job.key)?.is_none() {
3767                    request_task_park(
3768                        home,
3769                        job,
3770                        crate::run::ParkCause::Interrupt {
3771                            reason: format!(
3772                                "task {} asked to run first",
3773                                crate::run::short_of(interrupt_task)
3774                            ),
3775                        },
3776                    )?;
3777                }
3778            }
3779        }
3780        let candidates = interrupt_gate(&interrupt, &in_flight, candidates);
3781        let cooling = read_cooldown(home)?.is_some_and(|until| Timestamp::now() < until);
3782        let mut started = false;
3783        let mut resync = false;
3784        for candidate in candidates {
3785            if stop.stopped() {
3786                break;
3787            }
3788            let Some(DispatchClaim {
3789                task,
3790                claim,
3791                permit,
3792            }) = claim_dispatch(queue, &candidate, cooling, &sem, &urgent_sem)
3793            else {
3794                continue;
3795            };
3796            if crate::worker::active(home)?
3797                .iter()
3798                .any(|job| job.kind == "task" && !jobs.iter().any(|known| known.id == job.id))
3799            {
3800                drop(claim);
3801                resync = true;
3802                break;
3803            }
3804            let lane = permit_kind(land_resume_state(&task) == LandResume::Ready, task.urgent);
3805            let request = TaskWorker {
3806                opts: opts.absolute()?,
3807                queue: std::path::absolute(queue.root())?,
3808                task: task.id.clone(),
3809                lane,
3810                standalone: false,
3811            };
3812            // Keep the claim until spawn acknowledges the worker's durable
3813            // reservation. The worker then claims and re-reads before attempt.
3814            if let Err(error) = crate::worker::spawn(
3815                home,
3816                "task",
3817                &format!("task:{}", task.id),
3818                serde_json::to_value(&request)?,
3819            )
3820            .await
3821            {
3822                task_worker_spawn_error(home, &request, error)?;
3823                drop(claim);
3824                resync = true;
3825                break;
3826            }
3827            drop(claim);
3828            permits.extend(permit);
3829            attempted.push(task.id);
3830            started = true;
3831        }
3832        if started || resync {
3833            continue;
3834        }
3835        if owned.is_empty() && opts.once {
3836            while crate::worker::active(home)?
3837                .iter()
3838                .any(|job| job.key == "service:daemon-housekeeping")
3839            {
3840                stop.idle(RECHECK_WHILE_BUSY).await;
3841                if stop.stopped() {
3842                    return Ok(());
3843                }
3844            }
3845            break;
3846        }
3847        stop.idle(if live.is_empty() {
3848            opts.poll
3849        } else {
3850            RECHECK_WHILE_BUSY.min(opts.poll)
3851        })
3852        .await;
3853    }
3854    // Dropping a scheduler only drops dispatch bookkeeping. The workers own
3855    // every queue claim and Runner driver lease through outcome settlement.
3856    stop.busy.store(0, Ordering::SeqCst);
3857    Ok(())
3858}
3859
3860/// A maintenance pass may briefly own the same record while a worker starts.
3861/// Wait without blocking the executor or charging an agent attempt, then read
3862/// ownership again under the acquired lock. Other I/O failures still fail closed.
3863async fn resume_owner_lock(home: &Path, id: &str) -> Result<crate::pr_head::OwnerLock> {
3864    let busy = |error: &anyhow::Error| {
3865        error.downcast_ref::<std::io::Error>().is_some_and(|error| {
3866            error.kind() == std::io::ErrorKind::WouldBlock
3867                || cfg!(windows) && error.raw_os_error() == Some(33)
3868        })
3869    };
3870    match crate::pr_head::owner_lock(home, id) {
3871        Ok(lock) => return Ok(lock),
3872        Err(error) if busy(&error) => {}
3873        Err(error) => return Err(error),
3874    }
3875    let wait = RunState::load_under(id, home)?
3876        .config
3877        .graph
3878        .timeout_resource_wait;
3879    crate::pr_head::owner_lock_wait(home, id, Duration::from_secs(wait)).await
3880}
3881
3882/// Historical successors do not revoke a still-resumable public owner. Scan
3883/// under the same lock as publication/driver claims, and fail closed on unknown
3884/// history before spending another attempt.
3885async fn public_resume_owner(
3886    home: &Path,
3887    runs: &[String],
3888    repo: &Path,
3889) -> Result<Option<RunState>> {
3890    // This worker's reservation protects cleanup, but is not evidence that a
3891    // previous driver is still executing. Keep every other claim and the run's
3892    // own execution lease/process checks authoritative.
3893    let current_task = crate::worker::current_job(home)?
3894        .filter(|job| job.kind == "task" && job.pid == Some(std::process::id()))
3895        .map(|job| serde_json::from_value::<TaskWorker>(job.payload).map(|request| request.task))
3896        .transpose()?;
3897    let mut owner = None;
3898    for id in runs.iter().collect::<std::collections::BTreeSet<_>>() {
3899        let _lock = resume_owner_lock(home, id).await?;
3900        let state = RunState::load_under(id, home)?;
3901        if state.released()
3902            || state.retired_to.is_some()
3903            || state.pr_taken_over_by.is_some()
3904            || !crate::handover::landing_checkpoint(&state)
3905        {
3906            continue;
3907        }
3908        anyhow::ensure!(
3909            crate::git::toplevel(&state.repo).await?.canonicalize()?
3910                == crate::git::toplevel(repo).await?.canonicalize()?,
3911            "public owner repository differs; inspect run lineage"
3912        );
3913        anyhow::ensure!(
3914            owner.is_none(),
3915            "multiple retained public owners; inspect run lineage"
3916        );
3917        anyhow::ensure!(
3918            crate::handover::can_resume(&state),
3919            "public owner cannot resume"
3920        );
3921        let claimed = current_work(home, Timestamp::now())
3922            .iter()
3923            .any(|claim| claim.run == *id && current_task.as_deref() != Some(claim.task.as_str()));
3924        anyhow::ensure!(
3925            state.liveness_under(home, claimed) == crate::run::Liveness::Dead,
3926            "public owner's driver is live or unknown; wait before retrying"
3927        );
3928        owner = Some(state);
3929    }
3930    Ok(owner)
3931}
3932
3933/// Run one claimed task to a terminal status and record the outcome.
3934///
3935/// Every transition is flushed to the queue as it happens, so the state on disk
3936/// is what actually occurred rather than what this process still intends to
3937/// write.
3938async fn attempt(
3939    opts: &Opts,
3940    queue: &Queue,
3941    status: &Arc<Mutex<Status>>,
3942    stop: &Stop,
3943    interrupt_pause: crate::graph::Pause,
3944    task: &mut Task,
3945) -> Vec<QuotaLoss> {
3946    let repo = repo_for(task, &opts.repo);
3947    tracing::info!(
3948        "task {} — {} (repo {})",
3949        task.short(),
3950        task.title,
3951        repo.display()
3952    );
3953
3954    let public_owner = match public_resume_owner(&crate::run::home(), &task.runs, &repo).await {
3955        Ok(owner) => owner,
3956        Err(e) => {
3957            task.hold_machine(Some(format!(
3958                "Cannot verify existing public owner: {e:#}; no attempt started."
3959            )));
3960            record(queue, task);
3961            return Vec::new();
3962        }
3963    };
3964    let resume_runs = public_owner
3965        .as_ref()
3966        .map(|s| vec![s.id.clone()])
3967        .unwrap_or_else(|| task.runs.clone());
3968
3969    // A stop spends no agent resources and uses the saved landing owner.
3970    // Current roster edits, missing attachments or a failed config fetch must
3971    // never turn it into a new implementation, or prevent a PR from closing.
3972    if let Some(previous) = resume_runs.last().and_then(|id| RunState::load(id).ok())
3973        && previous
3974            .merge_decision
3975            .as_ref()
3976            .is_some_and(|decision| !decision.closed)
3977        && previous.status != RunStatus::Landing
3978        && !crate::merge_decision::resumable_hold(&previous)
3979    {
3980        task.hold_unrequested_retry(Some(format!("The existing owner stop decision remains held. Inspect run {} and its PR; request an explicit fresh review/decision before further work. No replacement or merge was started.", previous.id)));
3981        record(queue, task);
3982        return Vec::new();
3983    }
3984    let stop_owner = resume_runs
3985        .last()
3986        .and_then(|id| RunState::load(id).ok())
3987        .filter(|state| {
3988            crate::merge_decision::resumable_hold(state)
3989                || state.status == RunStatus::Landing
3990                    && state.land_approval.as_ref().is_some_and(|binding| {
3991                        Questions::open().get(&binding.question).is_ok_and(|q| {
3992                            q.run == state.id
3993                                && q.resolution()
3994                                    .as_deref()
3995                                    .is_some_and(crate::merge_decision::stops)
3996                        })
3997                    })
3998        });
3999    if let Some(owner) = stop_owner {
4000        let mut runner = match Runner::resume(&owner.id) {
4001            Ok(runner) => runner,
4002            Err(e) => {
4003                task.hold_manual(Some(format!("Cannot stop the existing landing owner: {e:#}. No replacement or merge was started.")));
4004                record(queue, task);
4005                return Vec::new();
4006            }
4007        };
4008        runner.on_pause(stop.pause());
4009        runner.watch_interrupt(interrupt_pause);
4010        let quota_before = runner.state.quota.clone();
4011        task.start(owner.id.clone());
4012        record(queue, task);
4013        lock(status).current.push(Current {
4014            task: task.id.clone(),
4015            run: owner.id,
4016        });
4017        runner.state.next_pass_task_entry = true;
4018        let result = runner.execute().await;
4019        return finish_attempt(
4020            opts.max_attempts,
4021            queue,
4022            task,
4023            &runner.state,
4024            &quota_before,
4025            result,
4026        );
4027    }
4028
4029    if let Some(owner) = &public_owner
4030        && (task.fresh_start || !may_resume_run(owner, task))
4031    {
4032        task.hold_machine(Some(format!("Existing PR belongs to run {}; resume its authorized instructions or explicitly resolve its owner decision before starting a successor. No attempt started.", owner.id)));
4033        record(queue, task);
4034        return Vec::new();
4035    }
4036
4037    let unfinished = (!task.fresh_start)
4038        .then(|| unfinished_run(&resume_runs, task.short()))
4039        .flatten()
4040        .filter(|id| RunState::load(id).is_ok_and(|run| may_resume_run(&run, task)));
4041    let previous = resume_runs.last().and_then(|id| RunState::load(id).ok());
4042    if !task.fresh_start
4043        && task.pr_recovery.is_none()
4044        && task.review_branch.is_none()
4045        && task.resume_override.is_none()
4046        && previous
4047            .as_ref()
4048            .is_some_and(|run| run.retired_to.is_some())
4049        && unfinished.is_none()
4050    {
4051        task.hold_unrequested_retry(Some("Recorded recovery authority is unavailable, no longer resumable or no longer authorized for these instructions. Inspect the retained lineage and select a recovery source explicitly; no replacement competition was started.".into()));
4052        record(queue, task);
4053        return Vec::new();
4054    }
4055
4056    let saved_recovery = previous.as_ref().filter(|run| {
4057        task.replacement.is_none()
4058            && task.pr_recovery.as_ref().is_some_and(|selected| {
4059                selected.check_task(&task.id, &task.instruction).is_ok()
4060                    && selected_starter(selected, Some(run), unfinished.as_deref())
4061                        == Starter::Resume(run.id.clone())
4062            })
4063    });
4064
4065    // The repository's magi.toml comes from `<remote>/<base>`, fetched now. A
4066    // failure here is the network's or the operator's, not the task's: hold
4067    // without spending an attempt, never fall back to a possibly stale copy.
4068    let explicit = match &task.overrides {
4069        Some(o) if !o.inherit_daemon_options => o.config.as_deref(),
4070        Some(_) => opts.config.as_deref(),
4071        None => opts.config.as_deref(),
4072    };
4073    let (mut config, layers) = if let Some(run) = saved_recovery {
4074        // A matching resume owns its resolved configuration and evidence;
4075        // current layers and inherited overrides only configure new work.
4076        let mut saved = run.clone();
4077        if let Err(e) = saved.configure_recovery_resume() {
4078            task.hold_manual(Some(format!("[recovery] saved run {} cannot resume: {e:#}. Inspect its saved configuration and evidence before requesting a new selection.", run.id)));
4079            record(queue, task);
4080            return Vec::new();
4081        }
4082        (saved.config, Vec::new())
4083    } else {
4084        let (mut config, layers) = match Config::discover_fetched(&repo, explicit).await {
4085            Ok(loaded) => loaded,
4086            Err(e) => {
4087                let reason = format!("[config] {e:#}");
4088                record_config_failure(task, &e);
4089                task.last_error = Some(reason.clone());
4090                task.hold_machine(Some(reason));
4091                record(queue, task);
4092                return Vec::new();
4093            }
4094        };
4095        if let Err(e) = apply_options(&mut config, opts, task) {
4096            task.hold_manual(Some(format!(
4097                "[config] {e:#}; correct the merge override before releasing the task"
4098            )));
4099            record(queue, task);
4100            return Vec::new();
4101        }
4102        if let Some(selected) = &task.pr_recovery
4103            && let Err(e) = selected.configure(&mut config)
4104        {
4105            task.hold_manual(Some(format!("[recovery] {e:#}. No run started; select the PR again with an explicit disposition and corrected configuration.")));
4106            record(queue, task);
4107            return Vec::new();
4108        }
4109        (config, layers)
4110    };
4111    let config_evidence = crate::pr_recovery::ConfigEvidence {
4112        layers,
4113        overrides: task.overrides.clone(),
4114        daemon_merge: task
4115            .overrides
4116            .as_ref()
4117            .is_none_or(|o| o.inherit_daemon_options)
4118            .then(|| opts.merge.clone())
4119            .flatten(),
4120    };
4121    apply_task_implementers(&mut config, task);
4122    if let Some(replacement) = &task.replacement {
4123        let safe = RunState::load(&replacement.run).is_ok_and(|old| {
4124            old.liveness(false) == Liveness::Dead
4125                && old.merge_decision.as_ref().is_some_and(|d| {
4126                    d.closed
4127                        && d.question == replacement.question
4128                        && d.replacement_run.as_ref().is_none_or(|run| {
4129                            task.runs.last() == Some(run)
4130                                && RunState::load(run).is_ok_and(|current| {
4131                                    current.replaces.as_ref().is_some_and(|pin| {
4132                                        pin.question == replacement.question
4133                                            && pin.agent == replacement.agent
4134                                    })
4135                                })
4136                        })
4137                })
4138        });
4139        if !safe {
4140            task.hold_manual(Some("Replacement ownership cannot be confirmed, or a replacement run was already minted before a crash. Inspect the task and its linked runs; no second replacement was started.".into()));
4141            record(queue, task);
4142            return Vec::new();
4143        }
4144        match config.agent(&replacement.agent) {
4145            Ok(agent) if crate::agent::installed(agent) => {}
4146            _ => {
4147                task.hold_manual(Some(format!("Selected implementer `{}` is unavailable or no longer configured. No substitute was dispatched. Restore it and explicitly release the task.", replacement.agent)));
4148                record(queue, task);
4149                return Vec::new();
4150            }
4151        }
4152        config.graph.implementers = 1;
4153        config.graph.land_approval = true;
4154        config.merge.mode = MergeMode::Pr;
4155        config.graph.selected_implementer = Some(replacement.agent.clone());
4156    }
4157    let p = phrases(&config.graph.language);
4158    let start_failed = p.could_not_start;
4159    // Resolve once for both admission and settlement. The inputs are the
4160    // task's explicit sources (never prose), plus an explicit review source's
4161    // current head; review-only runs may rebase later, and neither fact can
4162    // refund a failed automatic generation.
4163    if let Ok(base) =
4164        crate::git::merge_base_branch(&repo, &config.merge.remote, config.merge.base.as_deref())
4165            .await
4166        && let Ok(head) =
4167            crate::git::rev_parse(&repo, &format!("{}/{}", config.merge.remote, base)).await
4168    {
4169        let mut inputs = crate::queue::recovery_inputs(&task.sources);
4170        if let Some(branch) = task.review_of.as_ref().or(task.review_branch.as_ref()) {
4171            let source = crate::git::rev_parse(&repo, branch)
4172                .await
4173                .unwrap_or_default();
4174            inputs.push_str(&format!(";review:{branch}:{source}"));
4175        }
4176        if let Some(reason) = task.recovery_exhausted(
4177            &head,
4178            &inputs,
4179            task.attempt_limit
4180                .map_or(opts.max_attempts, std::num::NonZeroUsize::get),
4181        ) {
4182            task.hold_unrequested_retry(Some(reason));
4183            record(queue, task);
4184            return Vec::new();
4185        }
4186        task.recovery_admission = Some(crate::queue::RecoveryAdmission { base: head, inputs });
4187    }
4188
4189    // The free-space gate, checked *before* anything is minted: a task that
4190    // waits out a full disk costs nothing yet, and must not spend an attempt
4191    // or start a run the machine cannot finish. Held tasks stay in the list
4192    // for the human to see, and `magi task release` re-queues them when space
4193    // comes back - the same recovery as any other hold. A volume whose free
4194    // space cannot be measured closes the gate too: starting a run blind on a
4195    // disk that may be full is how the machine ends up with 6.7 GB free.
4196    if let Some(reason) = disk_gate(&repo, &config) {
4197        task.notice_cause("disk-space", None, None);
4198        task.last_error = Some(reason.clone());
4199        task.hold_machine(Some(reason.clone()));
4200        record(queue, task);
4201        tracing::warn!("holding {} for want of disk space: {reason}", task.short());
4202        // Stable wording - the free-space figures live in the task's hold
4203        // reason, and changing numbers would relight the bell every poll.
4204        notices::raise(
4205            Notice::warn(
4206                &format!("disk:{}", repo.display()),
4207                "A task was held for want of free disk space; free some, then release it from the queue.",
4208            )
4209            .link(Link::Task {
4210                id: task.id.clone(),
4211            }),
4212        );
4213        return Vec::new();
4214    }
4215
4216    // A resumable run of this task is carried on, never re-competed. The
4217    // candidates are built and paid for, and a fresh competition races a
4218    // second implementation against them.
4219    //
4220    // Two runs paid for that lesson. Run 01c2 was blocked and the loop
4221    // started 3cbf on the same task a moment later, duplicating two and a
4222    // half hours of agent work. Then b25f stalled on a judge that timed out
4223    // and one that answered with no JSON - `quota: 0`, so nothing the machine
4224    // was to blame for - and 4043 started **one second** later, buying three
4225    // fresh implementations to reach the same panel. `RunStatus::resumable`
4226    // rather than `!done()` is what catches the second case: a stall is
4227    // terminal, and its cheap recovery re-asks only the absent seats.
4228    //
4229    // A load failure is warned about rather than silently read as "not
4230    // resumable": the alternative is exactly what let a schema mismatch on
4231    // run `eba2` fall through to a full re-competition with nobody told why.
4232    // `crate::conduct` is what actually offers a better answer than
4233    // `Runner::start` here (see `Recovery::Review`), once this task's next
4234    // failure shows it up as `held`/`failed` with the run state unreadable.
4235    // `crate::conduct` chose `Review` for this task on an earlier cycle: its
4236    // branch survived, and this reopens exactly that branch as a
4237    // review-only pass rather than resuming or competing again. Consumed
4238    // (cleared) here whichever way this goes, so it never outlives this one
4239    // attempt - see `queue::Task::review_branch`.
4240    let review_branch = take_review_branch(task, previous.as_ref());
4241    let branch_exists = match &review_branch {
4242        Some(branch) => crate::git::branch_exists(&repo, branch)
4243            .await
4244            .unwrap_or(false),
4245        None => false,
4246    };
4247    // An explicit `magi review <branch>` is reviewed whatever happens to the
4248    // branch unless an explicit implementer change requires fresh work. An
4249    // unfinished run of the same task still wins, so a retry resumes.
4250    let proposed = match (&review_branch, &unfinished, &task.review_of) {
4251        (None, None, Some(branch)) => Starter::Review(branch.clone()),
4252        _ => choose_starter(
4253            review_branch.as_deref(),
4254            branch_exists,
4255            unfinished.as_deref(),
4256        ),
4257    };
4258    let retry_selected = task.replacement.is_some()
4259        && task.resumption.is_some()
4260        && !task.fresh_start
4261        && previous.as_ref().is_some_and(|run| {
4262            run.replaces.is_some()
4263                && ((run.status == RunStatus::Failed && run.viable().is_empty())
4264                    || (run.status == RunStatus::Blocked && run.tally.is_none()))
4265        });
4266    let stop_pin = task
4267        .resume_override
4268        .as_ref()
4269        .filter(|pin| {
4270            Questions::open().get(&pin.question_id).is_ok_and(|q| {
4271                q.node == land::APPROVAL_NODE
4272                    && q.resolution()
4273                        .as_deref()
4274                        .is_some_and(crate::merge_decision::stops)
4275            })
4276        })
4277        .and_then(|pin| pin.pinned_run.clone());
4278    let starter = if let Some(owner) = &public_owner {
4279        Starter::Resume(owner.id.clone())
4280    } else if let Some(run) = stop_pin {
4281        // Stop actions resume their exact owner even after seat-count edits;
4282        // they must never fall through to implementation or a takeover.
4283        Starter::Resume(run)
4284    } else if retry_selected {
4285        Starter::Resume(previous.as_ref().unwrap().id.clone())
4286    } else if let Some(selected) = &task.pr_recovery {
4287        if selected.check_task(&task.id, &task.instruction).is_ok() {
4288            selected_starter(selected, previous.as_ref(), unfinished.as_deref())
4289        } else {
4290            Starter::Review(selected.branch.clone())
4291        }
4292    } else {
4293        starter_with_implementers(task, previous.as_ref(), proposed)
4294    };
4295    // An explicit resume pin must never degrade into a new competition or
4296    // takeover, including when its run stops being eligible after the answer.
4297    if let Some(pin) = &task.resume_override
4298        && let Some(run) = &pin.pinned_run
4299        && starter != Starter::Resume(run.clone())
4300    {
4301        let run = run.clone();
4302        task.notice_cause("resume-owner", Some(&run), None);
4303        task.hold_machine(Some(format!(
4304            "pinned run cannot resume; inspect `magi show {run}` and restore the existing run before `magi task release {}`; no replacement task was started",
4305            task.id
4306        )));
4307        record(queue, task);
4308        return Vec::new();
4309    }
4310    if task.replacement.is_some()
4311        && previous.as_ref().is_some_and(|run| run.replaces.is_some())
4312        && starter != Starter::Resume(previous.as_ref().unwrap().id.clone())
4313    {
4314        task.hold_manual(Some("The selected replacement already has an attempt. It cannot be resumed safely; inspect its saved pin and restore the existing run. No additional replacement was started.".into()));
4315        record(queue, task);
4316        return Vec::new();
4317    }
4318    if let Some(reason) = retained_attempt_hold(
4319        task,
4320        &starter,
4321        config.disk.retained_attempt_limit,
4322        RunState::load,
4323    ) {
4324        // The review choice was consumed above. Preserve it across this hold
4325        // so a later release cannot silently become a fresh implementation.
4326        if let Starter::Review(branch) = &starter {
4327            task.review_branch = Some(branch.clone());
4328        }
4329        task.hold_unrequested_retry(Some(reason));
4330        record(queue, task);
4331        return Vec::new();
4332    }
4333    // Absolute paths of the files the task was filed with, re-read from the
4334    // task now so a file attached while it was held reaches a resumed run
4335    // too. A stored name whose file has gone is an error before any agent is
4336    // paid for, never a silent omission the implementer cannot notice.
4337    let attachments = match task_attachments(queue, task) {
4338        Ok(a) => a,
4339        Err(e) => {
4340            task.attempts += 1;
4341            task.fail_startup_target(
4342                "startup-attachments",
4343                e.name.clone(),
4344                format!("{start_failed}{e:#}"),
4345                opts.max_attempts,
4346            );
4347            record(queue, task);
4348            return Vec::new();
4349        }
4350    };
4351    let startup_pause = stop.pause();
4352    let startup_interrupt = interrupt_pause.clone();
4353    let wait = crate::repo_lock::WaitPolicy::new(
4354        Duration::from_secs(config.graph.timeout_resource_wait),
4355        move || startup_pause.parked() || startup_interrupt.parked(),
4356    );
4357    // Startup includes review takeover and recovery. Do not embed its large
4358    // future in both the wait scope and the surrounding attempt stack frame.
4359    let started = wait.scope(Box::pin(async { match &starter {
4360        Starter::Review(_) if task.pr_recovery.is_some() => {
4361            let selected = task.pr_recovery.clone().expect("selected recovery");
4362            match selected.check_task(&task.id, &task.instruction) {
4363                Ok(()) => Runner::recover_pr(&repo, &task.id, selected, config)
4364                    .await
4365                    .map(|mut r| {
4366                        r.state.attachments = attachments.clone();
4367                        r
4368                    }),
4369                Err(e) => Err(e),
4370            }
4371        }
4372        Starter::Review(branch) => {
4373            tracing::info!(
4374                "task {} reopens `{branch}` as a review-only pass",
4375                task.short()
4376            );
4377            // Every earlier attempt is superseded by the one being minted
4378            // now, so a branch one of them still holds may be taken over.
4379            let takeover = crate::handover::Takeover {
4380                earlier: task.earlier_attempts().to_vec(),
4381                home: crate::run::home(),
4382                choice: take_divergence_answer(branch, &config.merge.remote, task),
4383                task_instruction: Some(instruction_for(task)),
4384            };
4385            Runner::review_taking_over(
4386                &repo,
4387                branch,
4388                config,
4389                Some(takeover),
4390                crate::run::Origin::queue(&task.id),
4391            )
4392            .await
4393        }
4394        Starter::Resume(id) => {
4395            tracing::info!("resuming run {id} rather than competing again");
4396            Runner::resume(id).and_then(|mut r| {
4397                if retry_selected {
4398                    // The explicit queue release retries this same pinned
4399                    // implementation, preserving the attempt and worktrees.
4400                    for candidate in &mut r.state.candidates {
4401                        candidate.failed = None;
4402                        candidate.empty = false;
4403                        candidate.commits = 0;
4404                    }
4405                    r.state.status = RunStatus::Implementing;
4406                    r.state.event("implement", "owner released the held selected implementer; retrying the same attempt without fallback");
4407                }
4408                if r.state.prep_complete == Some(false) && r.state.candidates.is_empty() {
4409                    r.state.sources = task.sources.clone();
4410                }
4411                if r.state.prep_failure.is_some() {
4412                    let approval = task
4413                        .resumption
4414                        .as_ref()
4415                        .context("preparation retry requires an explicit task release")?;
4416                    r.retry_preparation(approval.at)?;
4417                }
4418                if let Some(instruction) =
4419                    prepare_instruction(&starter, Some(&r.state.instruction), task)
4420                {
4421                    r.state.instruction = instruction;
4422                }
4423                r.state.attachments = attachments.clone();
4424                if task.replacement.is_some() {
4425                    r.state.config.merge.mode = MergeMode::Pr;
4426                    r.state.config.graph.land_approval = true;
4427                }
4428                // Replacements publish a new PR and ask again. Selected PR
4429                // recoveries keep their saved disposition; ordinary runs can
4430                // retain the task's merge override.
4431                if task.replacement.is_none() && task.pr_recovery.is_none() && let Some(mode) = task
4432                    .overrides
4433                    .as_ref()
4434                    .and_then(|o| o.merge.as_deref())
4435                    .and_then(|m| merge_mode(m).ok())
4436                {
4437                    r.state.config.merge.mode = mode;
4438                }
4439                Ok(r)
4440            })
4441        }
4442        Starter::Start => {
4443            if let Some(branch) = &review_branch {
4444                tracing::warn!(
4445                    "conductor chose review for task {} but branch `{branch}` no longer \
4446                     exists; requeuing as a fresh competition instead",
4447                    task.short()
4448                );
4449            }
4450            let instruction = prepare_instruction(&starter, None, task)
4451                .unwrap_or_else(|| task.instruction.clone());
4452            Runner::start_naming(
4453                &repo,
4454                instruction,
4455                &task.title,
4456                config,
4457                crate::run::Origin::queue(&task.id),
4458            )
4459            .await
4460            .map(|mut r| {
4461                r.state.attachments = attachments.clone();
4462                r.state.sources = task.sources.clone();
4463                r
4464            })
4465        }
4466    }})).await;
4467    let mut runner = match started {
4468        Ok(r) => r,
4469        Err(e) if e.is::<crate::repo_lock::ResourceBlocked>() => {
4470            task.last_error = Some(e.to_string());
4471            if stop.parking() || interrupt_pause.parked() {
4472                let attempts = task.attempts;
4473                task.park(e.to_string());
4474                // Startup has not spent an attempt to refund.
4475                task.attempts = attempts;
4476            } else {
4477                task.hold_machine(Some(format!("{}{e}", crate::cache::RESOURCE_HOLD)));
4478            }
4479            record(queue, task);
4480            return Vec::new();
4481        }
4482        // A branch an earlier attempt still holds and magi will not release
4483        // by itself is the operator's call, not a failed attempt: hold the
4484        // task with the reason, spend nothing, and say so on the bell.
4485        Err(e) if task.pr_recovery.is_some() => {
4486            task.startup_notice_cause("startup-public-head");
4487            task.attempts += 1;
4488            if let Some(partial) = e.downcast_ref::<crate::pr_recovery::StartupFailed>() {
4489                task.link_run(&partial.run);
4490                task.record_pass(&partial.run, 0, task.attempts, None);
4491            }
4492            task.last_error = Some(format!("{start_failed}{e:#}"));
4493            task.hold_unrequested_retry(Some(format!(
4494                "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."
4495            )));
4496            record(queue, task);
4497            return Vec::new();
4498        }
4499        Err(e) if e.downcast_ref::<crate::handover::Refused>().is_some() => {
4500            let detail = match e.downcast_ref::<crate::handover::Refused>() {
4501                Some(r) => (p.handover_refused)(r),
4502                None => format!("{e:#}"),
4503            };
4504            task.startup_notice_cause("startup-handover");
4505            let reason = format!("{start_failed}{detail}");
4506            task.last_error = Some(reason.clone());
4507            // Consumed above by `take`; keep it so the released task retries
4508            // as a review of this same branch.
4509            let branch = match &starter {
4510                Starter::Review(branch) => Some(branch.clone()),
4511                _ => None,
4512            };
4513            // The hold's own `task:<id>` notice (raised by `Queue::put`) is the
4514            // one page; the guidance rides in the reason, fixed per state so a
4515            // re-put never changes the wording and relights it.
4516            task.hold_for_handover(branch, format!("{reason}{}", p.handover_hint));
4517            record(queue, task);
4518            tracing::warn!(
4519                "holding {} for a branch it cannot take over: {e:#}",
4520                task.short()
4521            );
4522            return Vec::new();
4523        }
4524        // A branch that differs between here and the remote in ways magi
4525        // cannot prove are the same change is a decision for the owner, not a
4526        // failed attempt: ask with both sides laid out, spend no attempt, and
4527        // let the answer requeue the task.
4528        Err(e) if e.downcast_ref::<crate::reconcile::Diverged>().is_some() => {
4529            let d = e
4530                .downcast_ref::<crate::reconcile::Diverged>()
4531                .expect("checked by the guard");
4532            let mut q = ask::Question::new(
4533                task.id.clone(),
4534                crate::reconcile::NODE.to_owned(),
4535                crate::reconcile::SEAT.to_owned(),
4536                d.summary(),
4537                d.detail(),
4538                d.choices(),
4539            );
4540            match Questions::open().put(&mut q) {
4541                Ok(()) => {
4542                    task.last_error = Some(format!("{e:#}"));
4543                    // Consumed above by `take`; the answer has to reach a review
4544                    // of this same branch, so keep pointing the task at it.
4545                    task.review_branch = Some(d.branch.clone());
4546                    task.review_instruction = Some(task.instruction.clone());
4547                    task.block(vec![q.id.clone()], Some(d.summary()));
4548                }
4549                Err(put) => {
4550                    tracing::warn!("could not file the divergence question: {put:#}");
4551                    task.attempts += 1;
4552                    task.fail_startup(
4553                        "startup-question-store",
4554                        format!("{start_failed}{e:#}"),
4555                        opts.max_attempts,
4556                    );
4557                }
4558            }
4559            record(queue, task);
4560            return Vec::new();
4561        }
4562        Err(e) => {
4563            // The answer was stale and nothing was applied: keep pointing at
4564            // the branch so the next attempt asks again against its new tips.
4565            // Nothing went wrong with the task, so no attempt is spent: a
4566            // budget one short of its limit must not hold it instead of
4567            // letting the question be asked again.
4568            if e.downcast_ref::<crate::reconcile::Stale>().is_some()
4569                && let Starter::Review(branch) = &starter
4570            {
4571                task.review_branch = Some(branch.clone());
4572                task.review_instruction = Some(task.instruction.clone());
4573                task.last_error = Some(format!("{start_failed}{e:#}"));
4574                task.status = crate::queue::TaskStatus::Failed;
4575                record(queue, task);
4576                return Vec::new();
4577            }
4578            task.attempts += 1;
4579            task.last_error = Some(format!("{start_failed}{e:#}"));
4580            task.startup_notice_cause("startup-source");
4581            task.hold_unrequested_retry(Some(format!(
4582                "Startup failed: {e:#}. Correct the startup source or configuration, then explicitly release the task; automatic recovery is stopped."
4583            )));
4584            record(queue, task);
4585            return Vec::new();
4586        }
4587    };
4588    if task.pr_recovery.is_some() && !matches!(starter, Starter::Resume(_)) {
4589        runner.state.recovery_config = Some(config_evidence);
4590    }
4591    if let Some(replacement) = &task.replacement {
4592        if matches!(starter, Starter::Start) {
4593            runner.state.instruction.push_str(&format!(
4594                "\n\n# Replacement review context\n{}",
4595                replacement.context
4596            ));
4597        }
4598        runner.state.replaces = Some(replacement.clone());
4599        runner.state.config.graph.selected_implementer = Some(replacement.agent.clone());
4600        if let Ok(mut old) = RunState::load(&replacement.run)
4601            && let Some(d) = &mut old.merge_decision
4602        {
4603            d.replacement_run = Some(runner.state.id.clone());
4604            old.event(
4605                "replacement",
4606                format!(
4607                    "replacement run {} owns the next attempt; old PR remains closed",
4608                    runner.state.id
4609                ),
4610            );
4611            if let Err(e) = old.save() {
4612                task.hold_manual(Some(format!(
4613                    "Could not link replacement history: {e}; no agents dispatched"
4614                )));
4615                record(queue, task);
4616                return Vec::new();
4617            }
4618        }
4619    }
4620    runner.state.task_instruction = Some(task.instruction.clone());
4621    // The follow-up depth is fixed now, while the task certainly exists:
4622    // deleting it later must not reset the cap (`crate::followup`).
4623    runner.state.followup_generation = Some(task.followup.as_ref().map_or(0, |f| f.generation));
4624    if runner.state.origin_chat.is_none() {
4625        runner.state.origin_chat = task.chat_talk().map(str::to_owned);
4626    }
4627    // A stop that means "park" reaches the graph through this handle.
4628    runner.on_pause(stop.pause());
4629    // `poll`'s interrupt scheduler reaches this one run - and no other -
4630    // through this handle. See `Pause`'s own doc for why these are never
4631    // the same one.
4632    runner.watch_interrupt(interrupt_pause);
4633
4634    // `start` has minted the run, so the task can now point at it. Persisting
4635    // `Running` before `execute` is what makes a crash mid-run legible.
4636    let run = runner.state.id.clone();
4637    task.start(run.clone());
4638    record(queue, task);
4639    lock(status).current.push(Current {
4640        task: task.id.clone(),
4641        run,
4642    });
4643
4644    // `RunState::quota` is the run's whole history across every resume, so
4645    // only what this execution added may arm the cooldown or earn a refund.
4646    let quota_before = runner.state.quota.clone();
4647    runner.state.next_pass_task_entry = true;
4648    let result = runner.execute().await;
4649    finish_attempt(
4650        opts.max_attempts,
4651        queue,
4652        task,
4653        &runner.state,
4654        &quota_before,
4655        result,
4656    )
4657}
4658
4659/// Settle `task` after its run executed: the verdict, the diagnosis, the
4660/// supersession of earlier attempts and the save. Shared by the loop and by
4661/// `magi run --resume`, so an attempt is counted one way wherever it ran.
4662/// Returns the quota losses this execution added to the run.
4663pub fn finish_attempt(
4664    max_attempts: usize,
4665    queue: &Queue,
4666    task: &mut Task,
4667    state: &RunState,
4668    quota_before: &[QuotaLoss],
4669    result: Result<()>,
4670) -> Vec<QuotaLoss> {
4671    let detail = match result {
4672        Ok(()) => describe(state),
4673        Err(e) => format!("{e:#}"),
4674    };
4675    let fresh = losses_this_attempt(quota_before, &state.quota);
4676    let verdict = Verdict {
4677        status: state.status,
4678        // A run that opened a pull request handed its work over, whatever the
4679        // gate then decided about merging it.
4680        left_pr: state.pr.is_some(),
4681        // Only a rate limit earns the task its attempt back - and only one
4682        // suffered now: a refund justified by a previous session's loss is
4683        // the same mistake as re-arming the cooldown from it. A stalled run
4684        // resumed without hitting quota again therefore spends its attempt,
4685        // like any other failure of the task's own.
4686        quota_hit: !fresh.is_empty(),
4687        // A run that parked was asked to stop; that is not a failure and must
4688        // not spend an attempt, or replacing the binary a few times would
4689        // exhaust a task's budget without an agent ever misbehaving.
4690        parked: state.parked,
4691        // A quota loss that left nothing viable is the same machine fact as a
4692        // `Stalled` quota loss; see `settle`'s doc table.
4693        no_viable_candidates: state.viable().is_empty(),
4694    };
4695    if let Some(replacement) = &state.replaces
4696        && let Some(pr) = &state.pr
4697        && let Ok(mut old) = RunState::load(&replacement.run)
4698        && let Some(d) = &mut old.merge_decision
4699    {
4700        d.replacement_pr = Some(pr.url.clone());
4701        let _ = old.save();
4702    }
4703    settle_and_diagnose(task, verdict, &detail, max_attempts, state);
4704    if state.status == RunStatus::Blocked && state.prep_failure.is_some() {
4705        task.hold_unrequested_retry(Some(format!(
4706            "Candidate setup failed: {detail}. Correct the explicit sources with `magi task sources {}` (README, \"Use\": implementation imports), or select a complete implementation with `magi task recover-pr`, then explicitly release the task.",
4707            task.short()
4708        )));
4709    }
4710    if task.status == TaskStatus::Done {
4711        supersede_prior_runs(task, &crate::run::home());
4712    }
4713    record(queue, task);
4714    tracing::info!(
4715        "task {} is {} after run {} ({})",
4716        task.short(),
4717        task.status.as_str(),
4718        state.short(),
4719        label(state.status)
4720    );
4721    fresh
4722}
4723
4724/// A task a hand-started run leaves runnable (failed with attempts to spare,
4725/// or requeued) is **held**: nobody asked for a retry, and a loop starting
4726/// later must not spend agent calls on it. `magi task release` retries.
4727pub fn hold_if_runnable(queue: &Queue, task: &mut Task) {
4728    // Parked work (including legacy Failed records with a parked run) is a
4729    // checkpoint or owner handoff, not an unrequested retry.
4730    if matches!(task.status, TaskStatus::Queued | TaskStatus::Failed)
4731        && !awaiting_resume_with(task, RunState::load)
4732        // A run parked on the owner's word about a withheld PR title/body
4733        // resumes via `land_resume_state`, including review-only tasks that
4734        // `awaiting_resume_with` does not consider.
4735        && withheld_text_wait(task).is_none()
4736        && !(task.fresh_start && task.replacement.is_some())
4737    {
4738        let why = task.last_error.clone().map_or_else(
4739            || "the run did not finish".to_owned(),
4740            |e| format!("the run did not finish: {e}"),
4741        );
4742        task.hold_unrequested_retry(Some(format!(
4743            "{why}. It was started by hand, so it is not retried \
4744             automatically; `magi task release` retries it."
4745        )));
4746        record(queue, task);
4747    }
4748}
4749
4750/// Whether an attempt at a task whose newest run is `run` would resume that
4751/// run rather than start a fresh competition. `magi run --resume` hands a run
4752/// to a live loop only when this holds.
4753#[must_use]
4754pub fn loop_would_resume(run: &str) -> bool {
4755    unfinished_run(&[run.to_owned()], crate::run::short_of(run)).is_some()
4756}
4757
4758/// A loop in another process that is alive right now: a fresh heartbeat in
4759/// `<home>/daemon.json` published by a pid that is not `own_pid`. `Some(pid)`
4760/// carries the pid it published (`None` inside when it published none - a
4761/// fresh heartbeat is still evidence of a live loop).
4762///
4763/// The one judgement of "is somebody else serving this queue", shared by the
4764/// web UI's refusal to start a second loop and by `magi run` / `magi review`
4765/// deciding whether to hand a task to the loop. A claim file proves nothing
4766/// here: whether a claim succeeds says nothing about whether its owner lives.
4767#[must_use]
4768pub fn foreign_loop(
4769    reading: Option<&Reading>,
4770    now: Timestamp,
4771    own_pid: u32,
4772) -> Option<Option<u32>> {
4773    let reading = reading.filter(|r| r.running(now))?;
4774    match reading.pid {
4775        Some(pid) if pid == own_pid => None,
4776        pid => Some(pid),
4777    }
4778}
4779
4780fn legacy_scheduler_running(reading: &Reading, now: Timestamp, own_pid: u32) -> bool {
4781    reading.worker_protocol != Some(1)
4782        && (foreign_loop(Some(reading), now, own_pid).is_some()
4783            || reading
4784                .pid
4785                .is_some_and(|pid| pid != own_pid && crate::proc::pid_alive(pid)))
4786}
4787
4788/// Low-level claimed-task execution shared by the detached standalone worker
4789/// and focused lifecycle fixtures. The caller must retain the queue claim for
4790/// this entire call. CLI callers transfer ownership through
4791/// [`run_detached_claimed`] instead of executing inside their own runtime.
4792///
4793/// The one deliberate difference from the loop: a task the attempt leaves
4794/// runnable (failed with attempts to spare, or requeued) is **held** instead.
4795/// Nobody asked for a retry, and a daemon starting later must not spend agent
4796/// calls re-running a hand-started task behind the operator's back;
4797/// `magi task release` is the explicit retry.
4798pub async fn run_claimed(opts: &Opts, queue: &Queue, task: &mut Task) {
4799    run_claimed_until(opts, queue, task, &Stop::new()).await;
4800}
4801
4802async fn run_claimed_until(
4803    opts: &Opts,
4804    queue: &Queue,
4805    task: &mut Task,
4806    stop: &Stop,
4807) -> Vec<QuotaLoss> {
4808    let status = Arc::new(Mutex::new(Status::new()));
4809    // An attempt embeds startup and graph futures. Keep it off the standalone
4810    // caller's stack, on both the initial execution and withheld-text resume.
4811    let mut quota = Box::pin(attempt(
4812        opts,
4813        queue,
4814        &status,
4815        stop,
4816        crate::graph::Pause::new(),
4817        task,
4818    ))
4819    .await;
4820    // A run parked on the owner's word about a withheld PR title/body has no
4821    // daemon to finish it. Nobody else will enforce `answer_timeout`, so wait
4822    // here with the claim still held, and resume once the question is settled
4823    // (answered, or abandoned at its deadline -> the neutral text).
4824    // A rejected reply files one more question (the gate asks at most twice),
4825    // so after a resume the run may be waiting again: keep going, with a fresh
4826    // limit for the new question. `resumes` bounds it even if a resume parks
4827    // without asking anything new.
4828    let mut resumes = 0;
4829    while resumes < MAX_WITHHELD_RESUMES && !stop.parking() {
4830        let Some(timeout) = withheld_text_wait(task) else {
4831            break;
4832        };
4833        eprintln!(
4834            "waiting for the owner's answer on a withheld pull request text \
4835             (up to {timeout}s; Ctrl-C leaves the run parked for a later `magi serve`)"
4836        );
4837        // The deadline abandons the question itself; the extra margin only
4838        // covers an abandon that could not be saved.
4839        let limit = std::time::Instant::now() + Duration::from_secs(timeout + 60);
4840        let ready = loop {
4841            match land_resume_state(task) {
4842                LandResume::StillWaiting
4843                    if !stop.parking() && std::time::Instant::now() < limit =>
4844                {
4845                    tokio::time::sleep(WITHHELD_POLL).await;
4846                }
4847                LandResume::Ready => break true,
4848                _ => break false,
4849            }
4850        };
4851        if !ready || stop.parking() {
4852            break;
4853        }
4854        resumes += 1;
4855        quota.extend(
4856            Box::pin(attempt(
4857                opts,
4858                queue,
4859                &status,
4860                stop,
4861                crate::graph::Pause::new(),
4862                task,
4863            ))
4864            .await,
4865        );
4866    }
4867    hold_if_runnable(queue, task);
4868    quota
4869}
4870
4871/// Resumes an in-process run makes for withheld-text questions: the gate asks
4872/// at most twice, plus one for a resume that asks nothing new.
4873const MAX_WITHHELD_RESUMES: u32 = 3;
4874
4875/// How often an in-process run rechecks its withheld-text question.
4876const WITHHELD_POLL: Duration = Duration::from_secs(15);
4877
4878/// The `answer_timeout` (seconds) of the task's last run when that run is
4879/// parked on an unresolved withheld-text question; `None` otherwise.
4880fn withheld_text_wait(task: &Task) -> Option<u64> {
4881    let id = task.runs.last()?;
4882    let s = RunState::load(id).ok()?;
4883    (s.parked && s.github_text.as_ref().is_some_and(|g| !g.resolved))
4884        .then_some(s.config.graph.answer_timeout)
4885}
4886
4887/// The quota losses `after` holds that `before` did not: what one execution
4888/// suffered, as opposed to the run's history.
4889///
4890/// Compared by value rather than by length or position because
4891/// `Runner::recover_stall` drops a `QuotaLoss` when its seat ranks again, so
4892/// the vector can shrink and shift under a resume. A retried seat that hits
4893/// quota again is a `push` with a new `at`, so it shows up as new here.
4894/// `reclaim` deliberately keeps reading the whole history: after a crash there
4895/// is no attempt boundary to diff against.
4896fn losses_this_attempt(before: &[QuotaLoss], after: &[QuotaLoss]) -> Vec<QuotaLoss> {
4897    after
4898        .iter()
4899        .filter(|q| !before.contains(q))
4900        .cloned()
4901        .collect()
4902}
4903
4904/// When the loop-wide quota cooldown should end, given this attempt's losses;
4905/// `None` when there were none. Reset-hint parsing and the cap live here.
4906fn cooldown_until(quota: &[QuotaLoss], now: Timestamp) -> Option<Timestamp> {
4907    if quota.is_empty() {
4908        return None;
4909    }
4910    let with_hint = quota.iter().find(|q| q.reset.is_some());
4911    let reset_at = with_hint.and_then(|q| parse_reset_hint(q.reset.as_deref()?, now, q.at));
4912    let wait = quota_wait(reset_at, now, QUOTA_WAIT_FALLBACK, QUOTA_WAIT_CAP);
4913    let secs = i64::try_from(wait.as_secs()).unwrap_or(i64::MAX);
4914    Some(
4915        now.checked_add(jiff::SignedDuration::from_secs(secs))
4916            .unwrap_or(Timestamp::MAX),
4917    )
4918}
4919
4920/// An explicit task count must agree with a run before any resume.
4921fn implementers_match(task: &Task, state: &RunState) -> bool {
4922    task.replacement.as_ref().map_or_else(
4923        || {
4924            task.implementers()
4925                .is_none_or(|n| n == state.config.graph.implementers)
4926        },
4927        |replacement| {
4928            state.config.graph.selected_implementer.as_deref() == Some(&replacement.agent)
4929                && state.config.graph.implementers == 1
4930        },
4931    )
4932}
4933
4934/// Never resume or take over candidate work built for a different seat count.
4935fn starter_with_implementers(
4936    task: &Task,
4937    previous: Option<&RunState>,
4938    proposed: Starter,
4939) -> Starter {
4940    if previous.is_some_and(|s| !implementers_match(task, s)) {
4941        task.review_of
4942            .as_ref()
4943            .map_or(Starter::Start, |branch| Starter::Review(branch.clone()))
4944    } else {
4945        proposed
4946    }
4947}
4948
4949/// Apply this task's explicit count to the new attempt, without changing defaults.
4950fn apply_task_implementers(config: &mut Config, task: &Task) {
4951    if let Some(n) = task.implementers() {
4952        config.graph.implementers = n;
4953    }
4954}
4955
4956/// [`prepare`] for one task: its own `--config` and command-line overrides
4957/// (`Task::overrides`, filed by `magi run` / `magi review`) take precedence
4958/// over the loop's, so handing a hand-started task to a daemon changes nothing
4959/// about what it does.
4960#[cfg(test)]
4961fn prepare_for(repo: &Path, opts: &Opts, task: &Task) -> Result<Config> {
4962    let explicit = match &task.overrides {
4963        Some(o) if !o.inherit_daemon_options => o.config.as_deref(),
4964        _ => opts.config.as_deref(),
4965    };
4966    let (mut config, _) = Config::discover(repo, explicit)?;
4967    apply_options(&mut config, opts, task)?;
4968    Ok(config)
4969}
4970
4971fn apply_options(config: &mut Config, opts: &Opts, task: &Task) -> Result<()> {
4972    if task
4973        .overrides
4974        .as_ref()
4975        .is_none_or(|o| o.inherit_daemon_options)
4976        && let Some(mode) = opts.merge.as_deref()
4977    {
4978        config.merge.mode = merge_mode(mode)?;
4979    }
4980    if let Some(o) = &task.overrides {
4981        o.apply(config);
4982    }
4983    Ok(())
4984}
4985
4986/// Load the config for a task's repository, with the merge override applied.
4987fn prepare(repo: &Path, opts: &Opts) -> Result<Config> {
4988    let (mut config, _layers) = Config::discover(repo, opts.config.as_deref())?;
4989    if let Some(mode) = &opts.merge {
4990        config.merge.mode = merge_mode(mode)?;
4991    }
4992    Ok(config)
4993}
4994
4995/// Prune the shared build cache at a between-runs boundary. The detached cache
4996/// service checks authoritative task jobs first; dispatch waits for that finite
4997/// service, so scheduler replacement cannot turn a compile into idle cleanup.
4998/// Its durable service cadence preserves [`CACHE_CHECK_INTERVAL_SECS`].
4999async fn maybe_prune_cache_between_runs(
5000    repo: &Path,
5001    opts: &Opts,
5002    home: &Path,
5003    stop: &Stop,
5004    last_checked: &mut Option<Timestamp>,
5005    now: Timestamp,
5006) {
5007    if stop.stopped() || !cache_check_due(*last_checked, now, CACHE_CHECK_INTERVAL_SECS) {
5008        return;
5009    }
5010    *last_checked = Some(now);
5011    let cfg = match prepare(repo, opts) {
5012        Ok(cfg) => cfg,
5013        Err(e) => {
5014            tracing::warn!("cache check: no config: {e:#}");
5015            return;
5016        }
5017    };
5018    match clean::prune_cache_if_over_limit(&cfg, home) {
5019        Ok(Some(pruned)) if pruned.files > 0 => tracing::info!(
5020            "housekeep: pruned {} file(s) ({} bytes) from the shared cache between runs",
5021            pruned.files,
5022            pruned.freed
5023        ),
5024        Ok(_) => {}
5025        Err(e) => {
5026            tracing::warn!("housekeep: prune cache: {e:#}");
5027            notices::raise_in(
5028                home,
5029                Notice::warn(
5030                    "housekeep:cache",
5031                    "Pruning the shared build cache failed; disk usage may keep growing.",
5032                ),
5033            );
5034        }
5035    }
5036}
5037
5038/// Whether [`maybe_prune_cache_between_runs`] should re-measure the cache
5039/// now, given when it last did (if ever). Pure, so the cadence is asserted
5040/// directly rather than by waiting out real minutes in a test.
5041fn cache_check_due(last_checked: Option<Timestamp>, now: Timestamp, interval_secs: u64) -> bool {
5042    last_checked.is_none_or(|last| clean::due(now, last, interval_secs))
5043}
5044
5045/// The disk janitor, with its housekeeping logged rather than fatal.
5046///
5047/// Called only at the loop's idle points, for the reason the caller documents:
5048/// a prune racing a live compile would delete files mid-build. The config is
5049/// re-read on every call because the repository that just ran may not be the
5050/// daemon's own default, and the cache directory is a repository fact.
5051///
5052/// `home` and `worktrees_root` are parameters rather than [`crate::run::home`]
5053/// and [`crate::run::default_worktree_root`] read here, for the same reason
5054/// [`drive`] takes its queue and status file rather than resolving them: a
5055/// test driving the loop must not reach through to the operator's real home
5056/// or worktree bay just because the janitor runs on every idle tick.
5057/// `worktrees_root` staying unread by [`clean::fold_due`] once made this easy
5058/// to get wrong silently - a test's `home` was already isolated, but nothing
5059/// exercised the parameter next to it, so a real worktree bay stayed wired in
5060/// underneath. The moment [`clean::fold_orphaned_worktrees`] started reading
5061/// it for real, every test in this file that drives the loop at all started
5062/// sweeping the operator's actual `~/wt/<repo>` instead of a fixture's.
5063async fn janitor(repo: &Path, opts: &Opts, home: &Path, worktrees_root: &Path) {
5064    let cfg = match prepare(repo, opts) {
5065        Ok(cfg) => cfg,
5066        Err(e) => {
5067            tracing::warn!("housekeep: no config: {e:#}");
5068            return;
5069        }
5070    };
5071    // A run's own worktree lives under `config.graph.worktree_root` when the
5072    // repository sets one - the same precedence `RunState::worktree_root`
5073    // uses - and `worktrees_root` only stands in for the *default* an
5074    // unconfigured repository resolves to (see this function's own
5075    // parameter, or the test fixture wiring one to a fake path). Housekeeping
5076    // that always swept the default regardless of this override would never
5077    // see, and so never reclaim, a single worktree for a repository that
5078    // relocated them elsewhere.
5079    let worktrees_root = cfg.graph.worktree_root.as_deref().unwrap_or(worktrees_root);
5080    let out = clean::housekeep(&cfg, home, worktrees_root, repo, Timestamp::now()).await;
5081    // Reported whenever there is anything to say, not only when `folded > 0`:
5082    // the incident this exists to prevent was 90 of 93 runs skipped and 0
5083    // folded, on every single pass, for months - a report gated on `folded`
5084    // would have stayed silent through every one of them.
5085    if out.folded > 0 || out.unreadable > 0 || out.orphaned_worktrees > 0 {
5086        let mut extra = Vec::new();
5087        if out.unreadable > 0 {
5088            extra.push(format!("{} unreadable", out.unreadable));
5089        }
5090        if out.orphaned_worktrees > 0 {
5091            extra.push(format!("{} orphaned worktree(s)", out.orphaned_worktrees));
5092        }
5093        let detail = if extra.is_empty() {
5094            String::new()
5095        } else {
5096            format!(" ({})", extra.join(", "))
5097        };
5098        tracing::info!("housekeep: folded {} run(s){detail}", out.folded);
5099    }
5100    if out.external_merges_recorded > 0 {
5101        tracing::info!(
5102            "housekeep: recorded {} run(s) as merged externally",
5103            out.external_merges_recorded
5104        );
5105    }
5106    if out.stale_pr_states_repaired > 0 {
5107        tracing::info!(
5108            "housekeep: rewrote {} run record(s) whose pull request had already settled",
5109            out.stale_pr_states_repaired
5110        );
5111    }
5112    if out.cache_files > 0 {
5113        tracing::info!(
5114            "housekeep: pruned {} file(s) ({} bytes) from the shared cache",
5115            out.cache_files,
5116            out.cache_freed
5117        );
5118    }
5119    if out.questions_abandoned > 0 {
5120        tracing::info!(
5121            "housekeep: abandoned {} question(s) left open by a finished run",
5122            out.questions_abandoned
5123        );
5124    }
5125}
5126
5127/// Run [`triage::run_once`] and log whatever it did, the same "only when
5128/// there is something to say" rule [`janitor`] follows for its own report.
5129///
5130/// Called at the same idle points as [`janitor`] - once per full poll
5131/// interval, never mid-attempt - for the same reason: it is not liveness
5132/// critical, and a task's own `hold_reason` string is the one thing this
5133/// would otherwise re-check (via [`crate::disk::free_bytes`]) on every busy
5134/// tick for no benefit.
5135async fn triage_held(queue: &Queue, home: &Path, opts: &Opts) {
5136    let questions = Questions::at(home.join("questions"));
5137    let report = triage::run_once(queue, &questions, opts.config.as_deref(), Timestamp::now());
5138    if report.is_empty() {
5139        return;
5140    }
5141    if !report.quarantined.is_empty() {
5142        tracing::info!(
5143            "triage: held {} blocked task(s) whose blocked-on task or \
5144             question no longer exists: {}",
5145            report.quarantined.len(),
5146            report.quarantined.join(", ")
5147        );
5148    }
5149    if !report.resumed.is_empty() {
5150        tracing::info!(
5151            "triage: resumed {} held task(s) whose machine hold had resolved: {}",
5152            report.resumed.len(),
5153            report.resumed.join(", ")
5154        );
5155    }
5156    if !report.asked.is_empty() {
5157        tracing::info!(
5158            "triage: asked about {} held task(s): {}",
5159            report.asked.len(),
5160            report.asked.join(", ")
5161        );
5162    }
5163    if !report.answered.is_empty() {
5164        tracing::info!(
5165            "triage: applied {} operator answer(s): {}",
5166            report.answered.len(),
5167            report.answered.join(", ")
5168        );
5169    }
5170}
5171
5172/// The free-space gate: what stands between this task and a new run, if
5173/// anything. `Some(reason)` holds the task; `None` lets it start.
5174///
5175/// A zero [`Config::disk::min_free_bytes`] opens the gate unconditionally -
5176/// the operator opted out. A measurement failure is a gate, not a pass: both
5177/// sides of "cannot tell" are served by not starting.
5178pub(crate) fn disk_gate(repo: &Path, config: &Config) -> Option<String> {
5179    disk_gate_with(repo, config, crate::disk::free_bytes)
5180}
5181
5182/// [`disk_gate`] with its free-space measurement supplied by the caller, so a
5183/// test can assert the exact wiring `attempt` runs - config's threshold in,
5184/// task-holding reason out - without asking the real machine's disk anything.
5185fn disk_gate_with<F: Fn(&Path) -> Result<u64>>(
5186    repo: &Path,
5187    config: &Config,
5188    free_bytes: F,
5189) -> Option<String> {
5190    let min = config.disk.min_free_bytes;
5191    if min == 0 {
5192        return None;
5193    }
5194    let mut paths = vec![repo.to_owned()];
5195    if let Some(cache) = config.cache_dir() {
5196        paths.push(cache);
5197    }
5198    if let Some(root) = &config.graph.worktree_root {
5199        paths.push(root.clone());
5200    }
5201    for path in paths {
5202        let probe = if path == repo {
5203            path.as_path()
5204        } else {
5205            path.ancestors().find(|p| p.exists()).unwrap_or(&path)
5206        };
5207        let reason = match free_bytes(probe) {
5208            Ok(free) => crate::disk::gate_in(free, min, &config.graph.language),
5209            Err(e) => Some(crate::disk::unmeasured_in(
5210                probe,
5211                &e,
5212                &config.graph.language,
5213            )),
5214        };
5215        if reason.is_some() {
5216            return reason;
5217        }
5218    }
5219    None
5220}
5221
5222/// How long to wait before offering another task when a run lost a seat to a
5223/// rate limit and its [`QuotaLoss::reset`] carried no hint [`parse_reset_hint`]
5224/// could read, or carried nothing at all. Long enough that a quota outage
5225/// cannot burn through a whole backlog in the few seconds each doomed attempt
5226/// takes to fail; short enough that a quota which clears early is not left
5227/// idle for the fallback's sake.
5228const QUOTA_WAIT_FALLBACK: Duration = Duration::from_secs(5 * 60);
5229
5230/// Longest a parsed reset hint may push the wait out to. The hint comes from
5231/// the CLI's own words, not a contract, so a parsing slip that lands a day
5232/// away must not leave the loop asleep for a day.
5233const QUOTA_WAIT_CAP: Duration = Duration::from_secs(30 * 60);
5234
5235/// How long [`poll`] should wait before offering the next task, after a run
5236/// lost at least one seat to a rate limit.
5237///
5238/// Pure and separate from the loop so the policy can be exercised without a
5239/// real quota outage. `reset_at` is the time [`parse_reset_hint`] made of the
5240/// CLI's free-text hint, if it could; `fallback` is what to wait when there is
5241/// nothing to parse, or the parsed time has already passed; `cap` bounds how
5242/// far a parsed hint is trusted to push the wait out.
5243fn quota_wait(
5244    reset_at: Option<Timestamp>,
5245    now: Timestamp,
5246    fallback: Duration,
5247    cap: Duration,
5248) -> Duration {
5249    match reset_at {
5250        Some(at) if at > now => {
5251            let secs = u64::try_from(at.as_second() - now.as_second()).unwrap_or(0);
5252            Duration::from_secs(secs).min(cap)
5253        }
5254        _ => fallback,
5255    }
5256}
5257
5258/// Best-effort reading of a [`QuotaLoss::reset`] hint into a concrete time.
5259///
5260/// `reset` is deliberately free text — see [`crate::agent::Quota`], which
5261/// explains why parsing it exactly "would be a bug factory" — so this only
5262/// recognises the shapes actually observed in the wild, and returns `None`
5263/// for anything else rather than guess at a format nobody has seen.
5264///
5265/// `recorded` is when the loss was noted ([`QuotaLoss::at`]). It anchors the
5266/// relative shape (`"in 1h2m49s"`, agy's), which counts from the moment the CLI
5267/// said it, not from whenever this loop happens to read it: anchoring on `now`
5268/// would push the reset later on every read and would read an already-elapsed
5269/// reset as still in the future. (A long hint is still clamped by
5270/// [`QUOTA_WAIT_CAP`]; the anchor matters for short hints and elapsed ones.)
5271fn parse_reset_hint(text: &str, now: Timestamp, recorded: Timestamp) -> Option<Timestamp> {
5272    parse_reset_hint_zoned(text, now)
5273        .or_else(|| parse_reset_hint_dated(text))
5274        .or_else(|| parse_reset_hint_relative(text, recorded))
5275}
5276
5277/// agy's shape: `"in 1h2m49s"` - `in`, then hours/minutes/seconds, each unit
5278/// optional but at least one required, in that order. A bare number or any
5279/// unknown unit is refused.
5280fn parse_reset_hint_relative(text: &str, recorded: Timestamp) -> Option<Timestamp> {
5281    let rest = text.trim().trim_end_matches('.').strip_prefix("in ")?;
5282    let mut rest = rest.trim();
5283    if rest.is_empty() {
5284        return None;
5285    }
5286    let mut total: i64 = 0;
5287    let mut matched = false;
5288    for (unit, secs) in [('h', 3600), ('m', 60), ('s', 1)] {
5289        if let Some((digits, tail)) = rest.split_once(unit)
5290            && !digits.is_empty()
5291            && digits.bytes().all(|b| b.is_ascii_digit())
5292        {
5293            total += digits.parse::<i64>().ok()?.checked_mul(secs)?;
5294            rest = tail;
5295            matched = true;
5296        }
5297    }
5298    if !rest.is_empty() || !matched {
5299        return None;
5300    }
5301    recorded
5302        .checked_add(jiff::SignedDuration::from_secs(total))
5303        .ok()
5304}
5305
5306/// Reads a 12-hour `"H:MMam/pm"` clock reading (whitespace trimmed,
5307/// case-insensitive) into a 24-hour hour and minute. Shared by every
5308/// reset-hint shape below.
5309fn parse_12h_clock(clock: &str) -> Option<(i8, i8)> {
5310    let clock = clock.trim().to_lowercase();
5311    let (digits, pm) = clock
5312        .strip_suffix("am")
5313        .map(|d| (d, false))
5314        .or_else(|| clock.strip_suffix("pm").map(|d| (d, true)))?;
5315    let (h, m) = digits.trim().split_once(':')?;
5316    let mut hour: i8 = h.trim().parse().ok()?;
5317    let minute: i8 = m.trim().parse().ok()?;
5318    if !(1..=12).contains(&hour) || !(0..=59).contains(&minute) {
5319        return None;
5320    }
5321    if pm && hour != 12 {
5322        hour += 12;
5323    } else if !pm && hour == 12 {
5324        hour = 0;
5325    }
5326    Some((hour, minute))
5327}
5328
5329/// The Claude CLI's shape: `"H:MMam/pm (Zone)"`, naming only a clock reading
5330/// and a zone, never a date. A clock reading already past today is read as
5331/// tomorrow's: a CLI naming a same-day reset that has already gone by means
5332/// the window rolled over while nothing was watching.
5333fn parse_reset_hint_zoned(text: &str, now: Timestamp) -> Option<Timestamp> {
5334    let open = text.find('(')?;
5335    let close = text.rfind(')')?;
5336    if close <= open {
5337        return None;
5338    }
5339    let zone = text[open + 1..close].trim();
5340    let (hour, minute) = parse_12h_clock(&text[..open])?;
5341    let tz = jiff::tz::TimeZone::get(zone).ok()?;
5342    let candidate = now
5343        .to_zoned(tz)
5344        .with()
5345        .hour(hour)
5346        .minute(minute)
5347        .second(0)
5348        .millisecond(0)
5349        .microsecond(0)
5350        .nanosecond(0)
5351        .build()
5352        .ok()?;
5353    let mut at = candidate.timestamp();
5354    if at <= now {
5355        at += jiff::SignedDuration::from_hours(24);
5356    }
5357    Some(at)
5358}
5359
5360/// The Codex CLI's shape: `"Mon DDth, YYYY H:MMam/pm"` (English month
5361/// abbreviation, an ordinal day, a 4-digit year, a 12-hour clock reading),
5362/// with no zone at all — unlike [`parse_reset_hint_zoned`], so there is no
5363/// "already past today" correction to make: the year already disambiguates
5364/// it. Scanned as a five-word window so it can be pulled out of the middle
5365/// of a full sentence, e.g. Codex's actual wording: "...or try again at Sep
5366/// 19th, 2026 5:10 PM." The result is read as UTC, same as this crate reads
5367/// any other timestamp with no zone attached.
5368fn parse_reset_hint_dated(text: &str) -> Option<Timestamp> {
5369    let words: Vec<&str> = text.split_whitespace().collect();
5370    if words.len() < 5 {
5371        return None;
5372    }
5373    (0..=words.len() - 5)
5374        .find_map(|start| parse_dated_window(&words[start..start + 5], words.get(start + 5)))
5375}
5376
5377/// One five-word window: month, `"DDth,"`, `"YYYY"`, `"H:MM"`, `"am/pm"`. A
5378/// parenthesis right after the window is refused rather than ignored — it
5379/// reads as an explicit zone annotation on a shape that otherwise carries
5380/// none, and guessing UTC anyway would be exactly the silent misread this
5381/// module's parsing otherwise avoids.
5382fn parse_dated_window(window: &[&str], trailing: Option<&&str>) -> Option<Timestamp> {
5383    if trailing.is_some_and(|next| next.starts_with('(')) {
5384        return None;
5385    }
5386    let month = month_number(window[0])?;
5387    let day_token = window[1].strip_suffix(',')?.to_lowercase();
5388    let day_digits = ["st", "nd", "rd", "th"]
5389        .iter()
5390        .find_map(|suffix| day_token.strip_suffix(*suffix))?;
5391    let day: i8 = day_digits.parse().ok()?;
5392    let year_token = window[2];
5393    if year_token.len() != 4 || !year_token.bytes().all(|b| b.is_ascii_digit()) {
5394        return None;
5395    }
5396    let year: i16 = year_token.parse().ok()?;
5397    // The am/pm word carries the sentence's own trailing punctuation, e.g.
5398    // the period ending "...at Sep 19th, 2026 5:10 PM." — strip it before
5399    // reusing the same 12-hour clock reader the bracketed shape uses.
5400    let ampm = window[4].trim_matches(|c: char| !c.is_ascii_alphabetic());
5401    let (hour, minute) = parse_12h_clock(&format!("{}{}", window[3], ampm))?;
5402    let date = jiff::civil::Date::new(year, month, day).ok()?;
5403    let candidate = date
5404        .at(hour, minute, 0, 0)
5405        .to_zoned(jiff::tz::TimeZone::UTC)
5406        .ok()?;
5407    Some(candidate.timestamp())
5408}
5409
5410/// The 3-letter English month abbreviation [`parse_reset_hint_dated`] reads,
5411/// case-insensitively, into a 1-based month number.
5412fn month_number(name: &str) -> Option<i8> {
5413    const NAMES: [&str; 12] = [
5414        "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
5415    ];
5416    let lower = name.to_lowercase();
5417    NAMES
5418        .iter()
5419        .position(|n| *n == lower.as_str())
5420        .map(|i| i as i8 + 1)
5421}
5422
5423/// Test probe for the shared resume policy's review-budget exclusion.
5424#[cfg(test)]
5425fn exhausted_review_budget(state: &RunState) -> bool {
5426    // A run whose task-head publication failed has nothing left to review:
5427    // resuming it only retries the publication, so it is never "exhausted".
5428    !crate::handover::can_resume(state)
5429        && state.status == RunStatus::Blocked
5430        && !state.released()
5431        && state.retired_to.is_none()
5432}
5433
5434/// This task's *most recent* run, if resuming it would actually make
5435/// progress. `short` is only for the warning's own message.
5436///
5437/// Only ever `runs.last()` — never a search back through older history.
5438/// `runs` accumulates one entry per fresh `Runner::start`/`Runner::review`
5439/// mint, oldest first, and every entry before the last one was already
5440/// superseded at the moment it was minted: the daemon only ever starts a new
5441/// run when the previous one was not worth resuming (unresumable, exhausted,
5442/// or unreadable), or when `crate::conduct::Recovery::Review` deliberately
5443/// opens a fresh review-only run alongside an older, already-failed
5444/// competition. Searching further back would let an old run that merely
5445/// *looks* resumable — a `Stalled` competition an earlier `Review` pass left
5446/// behind, say — get resumed instead of the fresh competition
5447/// `crate::conduct::Recovery::Requeue` actually promised, reviving history
5448/// nothing asked to revisit.
5449///
5450/// Two runs paid for the "prefer resuming over restarting" half of this
5451/// lesson, which is why this still checks `runs.last()` rather than always
5452/// restarting. Run 01c2 was blocked and the loop started 3cbf on the same
5453/// task a moment later, duplicating two and a half hours of agent work. Then
5454/// b25f stalled on a judge that timed out and one that answered with no JSON
5455/// — `quota: 0`, so nothing the machine was to blame for — and 4043 started
5456/// **one second** later, buying three fresh implementations to reach the
5457/// same panel. `RunStatus::resumable` rather than `!done()` is what catches
5458/// the second case: a stall is terminal, and its cheap recovery re-asks only
5459/// the absent seats. [`crate::handover::can_resume`] supplies the other half: a run
5460/// that is technically `resumable()` but provably cannot progress must not
5461/// count as "unfinished" either, or `Recovery::Requeue` becomes a silent
5462/// no-op instead of the fresh competition it promises.
5463///
5464/// A load failure is warned about rather than silently read as "not
5465/// resumable": the alternative is exactly what let a schema mismatch on run
5466/// `eba2` fall through to a full re-competition with nobody told why.
5467/// `crate::conduct` is what actually offers a better answer than
5468/// `Runner::start` here (see `Recovery::Review`), once this task's next
5469/// failure shows it up as `held`/`failed` with the run state unreadable.
5470fn unfinished_run(runs: &[String], short: &str) -> Option<String> {
5471    unfinished_run_with(runs, short, RunState::load)
5472}
5473
5474/// [`unfinished_run`] with an injected state reader. Tests provide their
5475/// fixtures directly rather than touching the process-global run home.
5476pub(crate) fn unfinished_run_with<F>(runs: &[String], short: &str, mut load: F) -> Option<String>
5477where
5478    F: FnMut(&str) -> Result<RunState>,
5479{
5480    let mut id = runs.last()?.clone();
5481    let mut visited = std::collections::BTreeSet::new();
5482    let mut repo = None;
5483    loop {
5484        if !runs.contains(&id) || !visited.insert(id.clone()) {
5485            tracing::warn!("invalid or cyclic recovery authority for task {short}");
5486            return None;
5487        }
5488        let state = match load(&id) {
5489            Ok(state) if state.id == id => state,
5490            Ok(_) => return None,
5491            Err(e) => {
5492                tracing::warn!("could not read run {id} for task {short}: {e:#}");
5493                return None;
5494            }
5495        };
5496        if repo.as_ref().is_some_and(|repo| *repo != state.repo) {
5497            return None;
5498        }
5499        repo = Some(state.repo.clone());
5500        if let Some(authority) = state.retired_to {
5501            id = authority;
5502            continue;
5503        }
5504        return (crate::handover::can_resume(&state)
5505            && state.liveness(false) != crate::run::Liveness::Live)
5506            .then_some(id);
5507    }
5508}
5509
5510/// Is `task` a parked (or, from an older build, failed) task whose last run was parked at a node boundary, so
5511/// that [`attempt`] will resume it on its own?
5512///
5513/// Mirrors [`unfinished_run_with`]'s own conditions (plus `parked`, and no
5514/// pending review choice, which [`choose_starter`] ranks first). Pure, with an
5515/// injected reader like its sibling. An unreadable run is not awaiting
5516/// anything: it keeps going to the conductor as before.
5517fn awaiting_resume_with<F>(task: &Task, load: F) -> bool
5518where
5519    F: FnOnce(&str) -> Result<RunState>,
5520{
5521    // `Failed` stays: a record an older build wrote for a parked run.
5522    if !matches!(task.status, TaskStatus::Parked | TaskStatus::Failed)
5523        || task.fresh_start
5524        || task.review_branch.is_some()
5525    {
5526        return false;
5527    }
5528    let Some(id) = task.runs.last() else {
5529        return false;
5530    };
5531    load(id).is_ok_and(|s| s.parked && crate::handover::can_resume(&s))
5532}
5533
5534/// Which of the three ways [`attempt`] can mint or continue a run this task
5535/// should use.
5536#[derive(Debug, Clone, PartialEq, Eq)]
5537enum Starter {
5538    /// `crate::graph::Runner::review` against a branch `crate::conduct` chose
5539    /// and that still exists.
5540    Review(String),
5541    /// `crate::graph::Runner::resume` on an unfinished run of this task.
5542    Resume(String),
5543    /// `crate::graph::Runner::start`: a fresh competition.
5544    Start,
5545}
5546
5547/// A second bound independent of inputs and the resettable failure counter.
5548/// Count recovery records, not history entries: resuming appends the same id.
5549/// Unknown history is not evidence that a new set of worktrees is affordable.
5550fn retained_attempt_hold(
5551    task: &Task,
5552    starter: &Starter,
5553    limit: usize,
5554    mut load: impl FnMut(&str) -> Result<RunState>,
5555) -> Option<String> {
5556    if limit == 0 || matches!(starter, Starter::Resume(_)) {
5557        return None;
5558    }
5559    let mut retained = 0;
5560    for id in task.runs.iter().collect::<std::collections::BTreeSet<_>>() {
5561        let state = match load(id) {
5562            Ok(state) if state.id == *id => state,
5563            _ => {
5564                return Some(format!(
5565                    "[recovery-backlog] Cannot verify historical run {id}. Restore its record and inspect the recovery lineage before starting another attempt. Existing history was retained."
5566                ));
5567            }
5568        };
5569        if state.retired_to.is_some() || state.released() {
5570            continue;
5571        }
5572        // Neither a completed status nor an empty candidate array proves that
5573        // all recovery sources (including unrecorded preparation) were retired.
5574        retained += 1;
5575    }
5576    (retained >= limit).then(|| format!(
5577        "[recovery-backlog] {retained} unretired attempts reach the limit of {limit}. Resume an existing recovery owner or inspect the lineage and retire proved-obsolete attempts before creating another run. Releases, refunds and changed upstream inputs do not reset this limit. No source or historical report was removed."
5578    ))
5579}
5580
5581/// A selected public implementation overrides every historical candidate and pin.
5582fn selected_starter(
5583    selected: &crate::pr_recovery::Selection,
5584    previous: Option<&RunState>,
5585    unfinished: Option<&str>,
5586) -> Starter {
5587    if let Some(id) = unfinished
5588        && previous.is_some_and(|r| {
5589            r.id == id && r.pr_recovery.as_ref() == Some(selected) && r.reviewed_commits.is_some()
5590        })
5591    {
5592        Starter::Resume(id.to_owned())
5593    } else {
5594        Starter::Review(selected.branch.clone())
5595    }
5596}
5597
5598/// The owner's answer to this branch's divergence question, consumed from the
5599/// task so a later, different divergence asks again. The runner applies it
5600/// (see `Runner::review_taking_over`), after the earlier worktree is released.
5601fn take_divergence_answer(
5602    branch: &str,
5603    remote: &str,
5604    task: &mut Task,
5605) -> Option<crate::reconcile::Choice> {
5606    let summary = crate::reconcile::summary_for(branch, remote);
5607    let (idx, choice) = task.answers.iter().enumerate().rev().find_map(|(i, a)| {
5608        (a.question == summary)
5609            .then(|| crate::reconcile::Choice::from_answer(&a.answer))
5610            .flatten()
5611            .map(|c| (i, c))
5612    })?;
5613    task.answers.remove(idx);
5614    Some(choice)
5615}
5616
5617/// Decide which of [`Runner::review`], [`Runner::resume`] or [`Runner::start`]
5618/// this attempt should use. Pure, and separate from [`attempt`], so the
5619/// routing itself is assertable without spawning a real graph or a git
5620/// process: `attempt`'s own `crate::git::branch_exists` call has already
5621/// happened by the time this is called.
5622///
5623/// `review_branch` wins whenever `branch_exists` confirms it; a `review_branch`
5624/// whose branch is gone falls all the way through to [`Starter::Start`], not
5625/// to [`Starter::Resume`] — `crate::conduct` chose review over resuming the
5626/// old (likely `Blocked`) run in the first place, and a branch that vanished
5627/// out from under that choice is not evidence resuming it would fare better.
5628fn choose_starter(
5629    review_branch: Option<&str>,
5630    branch_exists: bool,
5631    unfinished: Option<&str>,
5632) -> Starter {
5633    match review_branch {
5634        Some(branch) if branch_exists => Starter::Review(branch.to_owned()),
5635        Some(_) => Starter::Start,
5636        None => match unfinished {
5637            Some(id) => Starter::Resume(id.to_owned()),
5638            None => Starter::Start,
5639        },
5640    }
5641}
5642
5643/// Consume a retained review only while its authored criteria still match.
5644fn take_review_branch(task: &mut Task, previous: Option<&RunState>) -> Option<String> {
5645    let branch = task.review_branch.take();
5646    let instruction = task.review_instruction.take();
5647    branch.filter(|branch| match &instruction {
5648        Some(authored) => *authored == task.instruction,
5649        // Legacy retained branches must agree with their prior run; only an
5650        // explicitly pinned review can proceed without recoverable provenance.
5651        None => previous.map_or_else(
5652            || task.review_of.as_ref() == Some(branch),
5653            |run| compatible_run_instruction(run, task),
5654        ),
5655    })
5656}
5657
5658/// A current explicit pin can select an otherwise incompatible run. Edits
5659/// and subsequent approvals invalidate pins selected for earlier criteria.
5660fn may_resume_run(run: &RunState, task: &Task) -> bool {
5661    task.resume_override
5662        .as_ref()
5663        .is_some_and(|r| r.pinned_run.as_ref() == Some(&run.id))
5664        || compatible_run_instruction(run, task)
5665}
5666
5667/// Review scaffolding is execution context, not evidence of an operator edit.
5668pub(crate) fn compatible_run_instruction(run: &RunState, task: &Task) -> bool {
5669    if let Some(authored) = &run.task_instruction {
5670        return compatible_instruction(authored, task);
5671    }
5672    if compatible_instruction(&run.instruction, task) {
5673        return true;
5674    }
5675    // Recognize only the existing generated review shapes on legacy runs.
5676    // Ordinary authored text with a similar heading is left intact.
5677    if run.reviewed_commits.is_some() {
5678        if let Some((authored, _)) = run.instruction.split_once("\n\n# Work already on branch `") {
5679            return strip_answers_block(authored, task).trim() == task.instruction.trim();
5680        }
5681        if let Some(branch) = &task.review_of {
5682            let canonical = format!(
5683                "Review the branch `{branch}` (review-only: no implementation, no judging)."
5684            );
5685            let generated = format!(
5686                "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"
5687            );
5688            return task.instruction == canonical && run.instruction.starts_with(&generated);
5689        }
5690    }
5691    false
5692}
5693
5694/// Compare authored instructions, excluding answer blocks appended by the daemon.
5695pub(crate) fn compatible_instruction(old: &str, task: &Task) -> bool {
5696    strip_answers_block(old, task) == task.instruction
5697}
5698
5699/// Which repository a task runs in. A task that names none — the normal case
5700/// for one filed from a phone — runs in the daemon's own default.
5701fn repo_for(task: &Task, fallback: &Path) -> PathBuf {
5702    if task.repo.as_os_str().is_empty() || task.repo == Path::new(".") {
5703        return fallback.to_path_buf();
5704    }
5705    task.repo.clone()
5706}
5707
5708/// The header [`append_answers`] appends operator answers under. Shared with
5709/// [`strip_answers_block`] so a resumed run's instruction can be refreshed
5710/// rather than grown a new block on every resume.
5711const ANSWERS_HEADER: &str = "\n\n# Operator answers\n\n";
5712
5713/// Render the first `count` answers in the block appended to an instruction.
5714fn answers_block(task: &Task, count: usize) -> String {
5715    let mut s = ANSWERS_HEADER.to_owned();
5716    for a in &task.answers[..count] {
5717        s.push_str(&format!("- {}: {}\n", a.question, a.answer));
5718    }
5719    s
5720}
5721
5722/// Render the approval independently of transient task state, so historical
5723/// suffixes remain recognizable after another explicit release.
5724fn resumption_block(r: &crate::queue::Resumption) -> String {
5725    format!(
5726        "\n\n# Explicit operator resumption\n\nGeneration {} at {} approved this instruction:\n{}\n\
5727         Earlier answers (first {}) are audit history; their waiting premises are superseded by this approval.\n",
5728        r.generation, r.at, r.instruction, r.answers_through,
5729    )
5730}
5731
5732/// Append audit answers and the latest explicit approval to the instruction.
5733fn append_answers(base: &str, task: &Task) -> String {
5734    let mut s = base.to_owned();
5735    if !task.answers.is_empty() {
5736        s.push_str(&answers_block(task, task.answers.len()));
5737    }
5738    if let Some(r) = &task.resumption {
5739        s.push_str(&resumption_block(r));
5740    }
5741    s
5742}
5743
5744/// Drop the prior answer block only when it is exactly the suffix this task
5745/// could have appended on an earlier resume. An `ANSWERS_HEADER` written by
5746/// the task author is ordinary instruction text, not a block to remove.
5747fn strip_answers_block<'a>(instruction: &'a str, task: &Task) -> &'a str {
5748    let mut instruction = instruction;
5749    for approval in task
5750        .resumption
5751        .iter()
5752        .chain(task.resumption_history.iter().rev())
5753    {
5754        if let Some(base) = instruction.strip_suffix(&resumption_block(approval)) {
5755            instruction = base;
5756            break;
5757        }
5758    }
5759    for count in (1..=task.answers.len()).rev() {
5760        let block = answers_block(task, count);
5761        if let Some(base) = instruction.strip_suffix(&block) {
5762            return base;
5763        }
5764    }
5765    instruction
5766}
5767
5768/// The instruction handed to `Runner::start`: the task's own text, plus any
5769/// operator answers `crate::conduct` collected for it (see
5770/// [`Task::answers`]), so a decision the operator actually made reaches the
5771/// implementers rather than only clearing the block that was waiting on it.
5772///
5773/// Appended rather than merged into [`Task::instruction`] itself, so the
5774/// task's own record stays exactly what its author wrote.
5775fn instruction_for(task: &Task) -> String {
5776    append_answers(&task.instruction, task)
5777}
5778
5779/// The instruction a resumed run should carry on with: whatever it already
5780/// had, refreshed with the task's *current* operator answers.
5781///
5782/// A resumable run's own `RunState::instruction` predates any answer
5783/// `crate::conduct` collects after the run parks, so resuming it unchanged —
5784/// the behaviour before this function existed — silently drops the very
5785/// decision the operator made to unblock it. Re-stripping any block this
5786/// function appended on an earlier resume before re-appending the current
5787/// list (rather than blindly appending again) is what keeps a task resumed
5788/// three times over three answered questions from carrying the same answer
5789/// three times.
5790fn resumed_instruction(old_instruction: &str, task: &Task) -> String {
5791    append_answers(strip_answers_block(old_instruction, task), task)
5792}
5793
5794/// Preserve the producer's verified boundary and target, independent of prose.
5795fn record_config_failure(task: &mut Task, error: &anyhow::Error) {
5796    let failure = error.downcast_ref::<crate::config::ConfigFailure>();
5797    task.notice_cause(failure.map_or("config-load", |f| f.stage), None, None);
5798    if let Some(cause) = task.hold_notice_cause.as_mut() {
5799        cause.target = failure.map(|f| f.target.clone());
5800    }
5801}
5802
5803#[derive(Debug, thiserror::Error)]
5804#[error("attachment `{name}` is recorded on the task but {path} is missing")]
5805struct MissingAttachment {
5806    name: String,
5807    path: PathBuf,
5808}
5809
5810/// The task's attachments as absolute paths, each checked to still exist.
5811fn task_attachments(
5812    queue: &Queue,
5813    task: &Task,
5814) -> std::result::Result<Vec<PathBuf>, MissingAttachment> {
5815    let paths = queue.attachment_paths(task);
5816    for (name, path) in task.attachments.iter().zip(&paths) {
5817        if !path.is_file() {
5818            return Err(MissingAttachment {
5819                name: name.clone(),
5820                path: path.clone(),
5821            });
5822        }
5823    }
5824    Ok(paths)
5825}
5826
5827/// What [`attempt`] should tell a [`Starter`] about `task`'s current operator
5828/// answers before handing it to `Runner` — the actual boundary between
5829/// [`choose_starter`]'s routing and the graph, factored out so it is
5830/// assertable without a real repository, git branch, or agent CLI.
5831///
5832/// `Starter::Review` deliberately answers `None`: `Runner::review` builds its
5833/// instruction from the reviewed branch's own commit log because there is no
5834/// task statement to speak of for hand-written work, and splicing operator
5835/// answers into that text would contradict the very message it sends
5836/// reviewers ("there is no task statement").
5837fn prepare_instruction(
5838    starter: &Starter,
5839    old_instruction: Option<&str>,
5840    task: &Task,
5841) -> Option<String> {
5842    match starter {
5843        Starter::Start => Some(instruction_for(task)),
5844        Starter::Resume(_) => Some(resumed_instruction(
5845            old_instruction.expect("a resumed run always has a prior instruction"),
5846            task,
5847        )),
5848        Starter::Review(_) => None,
5849    }
5850}
5851
5852/// Persist a transition. A queue write failure is logged rather than fatal: the
5853/// run already happened, and taking the daemon down would only add a lost
5854/// backlog to a full disk.
5855fn record(queue: &Queue, task: &mut Task) {
5856    if let Err(e) = queue.put(task) {
5857        tracing::error!("could not record task {}: {e:#}", task.short());
5858        let notice = Notice::error(
5859            "loop:record",
5860            "The loop could not save a task's state; check the disk.",
5861        )
5862        .link(Link::Task {
5863            id: task.id.clone(),
5864        });
5865        // The queue lives at `<home>/queue`, so its parent is the home.
5866        match queue.root().parent().filter(|p| !p.as_os_str().is_empty()) {
5867            Some(home) => notices::raise_in(home, notice),
5868            None => notices::raise(notice),
5869        }
5870    }
5871}
5872
5873/// Every runnable task, in the order the loop should try them.
5874///
5875/// The head of this list is exactly what [`Queue::next_runnable`] offers; the
5876/// tail exists so that a claim somebody else holds costs the loop the next
5877/// candidate rather than a whole poll interval of idleness.
5878fn runnable(queue: &Queue) -> Vec<Task> {
5879    let mut tasks: Vec<Task> = queue
5880        .list()
5881        .into_iter()
5882        .filter(|t| t.status.runnable())
5883        .collect();
5884    tasks.sort_unstable_by(crate::queue::dispatch_order);
5885    tasks
5886}
5887
5888/// Why a run ended where it did, in one line, for [`Task::last_error`].
5889///
5890/// A stalled run names the seats the quota took out: "out of quota" is not
5891/// actionable, while "judge-2, judge-3 hit a limit" tells the operator which
5892/// agent to replace or which plan to top up.
5893///
5894/// Uses [`RunStatus::display_label`] rather than [`label`]/`as_str` on
5895/// purpose: unlike `label`'s other callers (an internal log line, an
5896/// already-a-bug fallback message), this string becomes `Task::last_error`
5897/// verbatim, which the phone renders in the same alarm-styled box an
5898/// ordinary failure gets — see `web::tests` and `assets/ui/app.js`'s
5899/// `.err` styling. A bare `verified_noop` there would read exactly like the
5900/// failure this whole feature exists to tell apart from one.
5901fn describe(state: &RunState) -> String {
5902    let p = phrases(&state.config.graph.language);
5903    let mut detail = if state.status == RunStatus::Stalled {
5904        let mut seats: Vec<&str> = state.quota.iter().map(|q| q.seat.as_str()).collect();
5905        seats.sort_unstable();
5906        seats.dedup();
5907        if seats.is_empty() {
5908            p.quorum_lost.to_owned()
5909        } else {
5910            format!("{}{}{}", p.quorum_lost, p.quota_took_out, seats.join(", "))
5911        }
5912    } else {
5913        format!("{}{}", p.run_ended, state.status.display_label())
5914    };
5915    if let Some(last) = state.events.last() {
5916        // The review loop's "never answered" stop is the one event worth
5917        // translating; it is recognised from structured state, not by
5918        // matching the English event text (which stays as recorded).
5919        let unanswered = state
5920            .reviews
5921            .last()
5922            .filter(|r| {
5923                state.status == RunStatus::Blocked
5924                    && last.node == "review"
5925                    && r.incomplete()
5926                    && r.blocking == 0
5927                    && r.round == state.config.graph.review_rounds
5928                    && r.e2e.iter().all(crate::run::CommandOutcome::ok)
5929            })
5930            .map(|r| (r.expected - r.answered, r.round));
5931        match unanswered {
5932            Some((missing, rounds)) if crate::lang::is_japanese(&state.config.graph.language) => {
5933                detail.push_str(&format!(
5934                    " ({}: {})",
5935                    last.node,
5936                    (p.reviewers_never_answered)(missing, rounds)
5937                ));
5938            }
5939            _ => detail.push_str(&format!(" ({}: {})", last.node, last.message)),
5940        }
5941    }
5942    detail.push_str(&format!(" [run {}]", state.id));
5943    detail
5944}
5945
5946/// Upper bound on [`Task::diagnostic`]'s length, in bytes.
5947///
5948/// The task file lives in the backlog indefinitely; a diagnostic is an
5949/// excerpt of the run's own `artifacts/`, not a copy of them, so this has to
5950/// stay small regardless of how much a gate command or a candidate printed.
5951const DIAGNOSTIC_MAX: usize = 4_000;
5952
5953/// Tail kept from a single failing command's output inside a diagnostic.
5954/// Smaller than [`crate::graph`]'s own `OUTPUT_TAIL` on purpose: this is a
5955/// pointer for a human deciding whether to go read the full artifact by hand,
5956/// not a replacement for reading it.
5957const DIAGNOSTIC_OUTPUT_TAIL: usize = 800;
5958
5959/// Assemble a bounded diagnostic excerpt from a held task's own run, so
5960/// `magi task show` says more than the one-line reason in [`describe`].
5961///
5962/// The one-liner answers "where did the run stop"; this answers "what would a
5963/// human have found opening `artifacts/` by hand" — the point of the whole
5964/// feature is the case that one-liner actively misleads on: a run held as "no
5965/// candidate produced a change" can mean the implementer actually finished
5966/// the task (opened a PR, merged it, tagged a release) and only left a clean
5967/// local worktree behind, which reads as "nothing happened" unless someone
5968/// goes and reads what the agent actually said. `None` when the run carries
5969/// none of the three shapes this recognises — an ordinary run held for
5970/// something not diagnosable from `RunState` alone still explains itself
5971/// through `Task::last_error`.
5972fn diagnostic(state: &RunState) -> Option<String> {
5973    let mut parts: Vec<String> = Vec::new();
5974
5975    // Gate failure: which check(s), and the tail of what each printed.
5976    for o in state.gate.iter().filter(|o| !o.ok()) {
5977        parts.push(format!(
5978            "gate `{}` failed ({}):\n{}",
5979            o.command,
5980            notices::exit_status(o.code),
5981            crate::run::tail(&o.output_tail, DIAGNOSTIC_OUTPUT_TAIL)
5982        ));
5983    }
5984
5985    // The land loop gave up because the fixer declined while checks were
5986    // still red: the message already names them (see `land::run`).
5987    if let Some(last) = state
5988        .events
5989        .iter()
5990        .rev()
5991        .find(|e| e.node == "land" && e.message.contains("fixer produced no commit"))
5992    {
5993        parts.push(last.message.clone());
5994    }
5995
5996    // No viable candidate: every implementer's own final word, sanitized the
5997    // same way a judge would have read it, so a run that actually finished
5998    // the job does not read as an unexplained failure. A verified no-op is
5999    // called out ahead of its own summary and apart from an ordinary
6000    // failure's `why` — this is the one candidate shape whose diagnostic a
6001    // human is expected to actually judge, not just skim.
6002    if state.viable().is_empty() {
6003        for c in &state.candidates {
6004            if let Some(evidence) = &c.verified_noop {
6005                parts.push(format!(
6006                    "candidate {} (agent-verified no-op, unconfirmed by magi): {evidence}",
6007                    c.label
6008                ));
6009            } else if !c.summary.trim().is_empty() {
6010                parts.push(format!("candidate {}: {}", c.label, c.summary.trim()));
6011            } else if let Some(why) = &c.failed {
6012                parts.push(format!("candidate {}: {why}", c.label));
6013            }
6014        }
6015    }
6016
6017    if parts.is_empty() {
6018        return None;
6019    }
6020    // `run::tail` prefixes an "N earlier bytes omitted" marker whose own
6021    // length depends on N, so asking it for exactly `DIAGNOSTIC_MAX` can come
6022    // back slightly over. Leave it enough room to always land under the
6023    // limit.
6024    Some(crate::run::tail(
6025        &parts.join("\n\n"),
6026        DIAGNOSTIC_MAX.saturating_sub(100),
6027    ))
6028}
6029
6030/// Stable lower-case name for a run status, for an internal log line and the
6031/// "graph stopped without reaching a terminal status" bug message in
6032/// [`settle`] — never for [`Task::last_error`] itself; see [`describe`]'s own
6033/// doc for why that one reads [`RunStatus::display_label`] instead. One
6034/// definition of a status's name, on the type that owns it: this table used
6035/// to live here as a second copy, and a status renamed in one place would
6036/// have gone on reading correctly in the other.
6037fn label(status: RunStatus) -> &'static str {
6038    status.as_str()
6039}
6040
6041/// Parse a merge mode override.
6042pub(crate) fn merge_mode(mode: &str) -> Result<MergeMode> {
6043    match mode {
6044        "none" => Ok(MergeMode::None),
6045        "local" => Ok(MergeMode::Local),
6046        "pr" => Ok(MergeMode::Pr),
6047        other => bail!("unknown merge mode `{other}`; expected none, local or pr"),
6048    }
6049}
6050
6051/// Take the status lock, recovering from a poisoned one.
6052///
6053/// A panic elsewhere must not silently stop the heartbeat: the status is plain
6054/// data, and the worst a poisoned lock can hold is a stale timestamp.
6055fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
6056    mutex
6057        .lock()
6058        .unwrap_or_else(std::sync::PoisonError::into_inner)
6059}
6060
6061#[cfg(test)]
6062mod tests {
6063    use super::*;
6064    use crate::queue::{Source, TaskStatus};
6065    use crate::run::{Candidate, CommandOutcome};
6066    use pretty_assertions::assert_eq;
6067
6068    fn task() -> Task {
6069        Task::new(
6070            "add retries".to_owned(),
6071            "add retries".to_owned(),
6072            PathBuf::from("/repo"),
6073            Source::Human,
6074        )
6075    }
6076
6077    fn starting_task_reservation(home: &Path, request: &TaskWorker) -> crate::worker::Job {
6078        let job = crate::worker::Job {
6079            id: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa".into(),
6080            sequence: 1,
6081            kind: "task".into(),
6082            key: format!("task:{}", request.task),
6083            payload: serde_json::to_value(request).unwrap(),
6084            generation: "a".repeat(64),
6085            version: None,
6086            status: crate::worker::Status::Starting,
6087            result: None,
6088            pid: None,
6089            created_at: u64::try_from(Timestamp::now().as_second()).unwrap(),
6090            finished_at: None,
6091        };
6092        let dir = home.join("workers/jobs").join(&job.id);
6093        std::fs::create_dir_all(&dir).unwrap();
6094        std::fs::write(dir.join("job.json"), serde_json::to_vec(&job).unwrap()).unwrap();
6095        job
6096    }
6097
6098    #[test]
6099    fn an_unacknowledged_own_reservation_stops_dispatch_without_cancelling_the_child() {
6100        let home = tempfile::tempdir().unwrap();
6101        let request = TaskWorker {
6102            opts: Opts::default(),
6103            queue: home.path().join("queue"),
6104            task: "startup".into(),
6105            lane: PermitKind::Ordinary,
6106            standalone: false,
6107        };
6108        let job = starting_task_reservation(home.path(), &request);
6109        task_worker_spawn_error(
6110            home.path(),
6111            &request,
6112            anyhow::anyhow!("worker startup is unacknowledged: private-error-detail"),
6113        )
6114        .expect_err("our own reservation must not turn a failed spawn into a resync");
6115        let active = crate::worker::active(home.path()).unwrap();
6116        assert_eq!(active.len(), 1);
6117        assert_eq!(active[0].id, job.id);
6118        assert_eq!(active[0].status, crate::worker::Status::Starting);
6119        assert!(
6120            crate::worker::control(home.path(), &job.key)
6121                .unwrap()
6122                .is_none()
6123        );
6124        let notices = notices::Notices::at(home.path().join("notifications")).list();
6125        assert_eq!(notices.len(), 1);
6126        assert_eq!(notices[0].key, "loop:worker-startup");
6127        assert!(!notices[0].message.contains("private-error-detail"));
6128    }
6129
6130    #[test]
6131    fn a_standalone_handoff_reservation_resyncs_dispatch_without_replacing_it() {
6132        let home = tempfile::tempdir().unwrap();
6133        let mut request = TaskWorker {
6134            opts: Opts::default(),
6135            queue: home.path().join("queue"),
6136            task: "handoff".into(),
6137            lane: PermitKind::Ordinary,
6138            standalone: true,
6139        };
6140        let job = starting_task_reservation(home.path(), &request);
6141        request.standalone = false;
6142        task_worker_spawn_error(
6143            home.path(),
6144            &request,
6145            anyhow::anyhow!("operation key is already reserved for a different request"),
6146        )
6147        .expect("a different standalone request is the handoff race, not our startup failure");
6148        let active = crate::worker::active(home.path()).unwrap();
6149        assert_eq!(active.len(), 1);
6150        assert_eq!(active[0].id, job.id);
6151        assert_eq!(active[0].payload, job.payload);
6152        assert!(
6153            notices::Notices::at(home.path().join("notifications"))
6154                .list()
6155                .is_empty()
6156        );
6157    }
6158
6159    #[test]
6160    fn a_live_worker_reservation_prevents_reclaim_before_its_task_claim() {
6161        let dir = tempfile::tempdir().unwrap();
6162        let queue = Queue::at(dir.path().join("queue"));
6163        let mut task = task();
6164        task.status = TaskStatus::Running;
6165        task.attempts = 1;
6166        queue.put(&mut task).unwrap();
6167        let live = std::collections::BTreeSet::from([task.id.clone()]);
6168        assert!(reclaim_orphaned_running(&queue, 2, &live).is_empty());
6169        drop(queue.claim(&task.id).unwrap());
6170        let saved = queue.get(&task.id).unwrap();
6171        assert_eq!(saved.status, TaskStatus::Running);
6172        assert_eq!(saved.attempts, 1);
6173        assert!(saved.last_error.is_none());
6174    }
6175
6176    #[test]
6177    fn standalone_workers_do_not_consume_scheduler_lanes() {
6178        let request = |task: &str, lane, standalone| TaskWorker {
6179            opts: Opts::default(),
6180            queue: PathBuf::from("/queue"),
6181            task: task.to_owned(),
6182            lane,
6183            standalone,
6184        };
6185        let mut jobs = vec![
6186            request("ordinary", PermitKind::Ordinary, false),
6187            request("standalone-urgent", PermitKind::Urgent, true),
6188            request("standalone-ordinary", PermitKind::Ordinary, true),
6189            request("decision", PermitKind::None, false),
6190        ];
6191        assert_eq!(available_slots(&jobs, 2), (1, 1));
6192        jobs.push(request("urgent", PermitKind::Urgent, false));
6193        assert_eq!(available_slots(&jobs, 2), (1, 0));
6194        assert_eq!(available_slots(&jobs, 0), (0, 0));
6195    }
6196
6197    #[test]
6198    fn a_live_legacy_scheduler_blocks_migration_even_with_a_stale_heartbeat() {
6199        let now = Timestamp::now();
6200        let mut reading = Reading {
6201            pid: Some(std::process::id()),
6202            updated_at: Some(now - jiff::SignedDuration::from_secs(STALE_SECS + 1)),
6203            ..Reading::default()
6204        };
6205        assert!(!reading.running(now));
6206        assert!(legacy_scheduler_running(&reading, now, 0));
6207        reading.worker_protocol = Some(1);
6208        assert!(!legacy_scheduler_running(&reading, now, 0));
6209    }
6210
6211    #[test]
6212    fn selected_public_head_overrides_historical_resume_and_pin() {
6213        let mut t = task();
6214        let mut old = run_state(RunStatus::Blocked);
6215        let selection = crate::pr_recovery::Selection {
6216            url: "url".into(),
6217            branch: format!("magi/task-{}", t.id),
6218            head: "a".repeat(40),
6219            source_head: None,
6220            source_run: None,
6221            source_proof: None,
6222            disposition: Some(crate::pr_recovery::Disposition::Review),
6223            instruction: t.instruction.clone(),
6224            approved_at: Timestamp::now(),
6225        };
6226        t.resume_override = Some(crate::queue::OperatorResume {
6227            pinned_run: Some(old.id.clone()),
6228            question_id: "choice".into(),
6229            at: Timestamp::now(),
6230            conductor_rehold: None,
6231            forced: false,
6232        });
6233        assert_eq!(
6234            selected_starter(&selection, Some(&old), Some(&old.id)),
6235            Starter::Review(selection.branch.clone())
6236        );
6237        old.pr_recovery = Some(selection.clone());
6238        assert_eq!(
6239            selected_starter(&selection, Some(&old), Some(&old.id)),
6240            Starter::Review(selection.branch.clone())
6241        );
6242        old.reviewed_commits = Some(vec!["implementation".into()]);
6243        assert_eq!(
6244            selected_starter(&selection, Some(&old), Some(&old.id)),
6245            Starter::Resume(old.id.clone())
6246        );
6247        let mut changed = selection.clone();
6248        changed.head = "b".repeat(40);
6249        assert_eq!(
6250            selected_starter(&changed, Some(&old), Some(&old.id)),
6251            Starter::Review(changed.branch)
6252        );
6253    }
6254
6255    #[test]
6256    fn candidate_transplant_failure_is_one_actionable_hold() {
6257        let dir = tempfile::tempdir().unwrap();
6258        let q = Queue::at(dir.path().join("queue"));
6259        let mut t = task();
6260        let mut state = run_state(RunStatus::Blocked);
6261        state.event("prep", "cannot apply referenced commit: conflict");
6262        state.prep_failure = Some("conflict".into());
6263        t.start(state.id.clone());
6264        finish_attempt(3, &q, &mut t, &state, &[], Err(anyhow::anyhow!("conflict")));
6265        assert_eq!(t.attempts, 1);
6266        assert_eq!(t.status, TaskStatus::Held);
6267        assert!(t.operator_held());
6268        assert!(!t.status.runnable());
6269        assert!(t.hold_reason.as_deref().unwrap().contains("recover-pr"));
6270        let first = t.chat_notice_key().unwrap();
6271        t.chat_notices.push(first.clone());
6272        t.release_operator("test", None);
6273        state.id = "another-unprepared-run".into();
6274        t.start(state.id.clone());
6275        finish_attempt(
6276            3,
6277            &q,
6278            &mut t,
6279            &state,
6280            &[],
6281            Err(anyhow::anyhow!("still conflicts")),
6282        );
6283        assert_eq!(
6284            t.chat_notice_key().as_ref(),
6285            Some(&first),
6286            "no work happened"
6287        );
6288        t.release_operator("test", None);
6289        state.prep_failure = None;
6290        state.status = RunStatus::Failed;
6291        state.begin_pass();
6292        t.start(state.id.clone());
6293        finish_attempt(
6294            1,
6295            &q,
6296            &mut t,
6297            &state,
6298            &[],
6299            Err(anyhow::anyhow!("new gate failure")),
6300        );
6301        let failure = t.chat_notice_key().unwrap();
6302        assert_ne!(failure, first, "approved recovery reached a real failure");
6303        t.release_operator("test", None);
6304        state.begin_pass();
6305        t.start(state.id.clone());
6306        finish_attempt(
6307            1,
6308            &q,
6309            &mut t,
6310            &state,
6311            &[],
6312            Err(anyhow::anyhow!("new failure on resume")),
6313        );
6314        assert_ne!(
6315            t.chat_notice_key().as_ref(),
6316            Some(&failure),
6317            "a new settled pass"
6318        );
6319    }
6320
6321    #[test]
6322    fn revised_resumption_invalidates_old_pins_and_preserves_unchanged_pins() {
6323        for source in ["cli", "web"] {
6324            let mut t = task();
6325            let mut run = run_state(RunStatus::Blocked);
6326            run.instruction = "Run A original criteria".into();
6327            run.task_instruction = Some(run.instruction.clone());
6328            t.start(run.id.clone());
6329            t.release_operator("choice", Some("resume-question"));
6330            t.resume_override = Some(crate::queue::OperatorResume {
6331                question_id: "resume-question".into(),
6332                at: Timestamp::now(),
6333                conductor_rehold: None,
6334                forced: true,
6335                pinned_run: Some(run.id.clone()),
6336            });
6337            t.hold_machine(Some("not enough free space to start a run".into()));
6338            let mut unchanged = t.clone();
6339            unchanged
6340                .edit("title changed".into(), unchanged.instruction.clone())
6341                .unwrap();
6342            unchanged.release_operator(source, None);
6343            assert_eq!(
6344                unchanged
6345                    .resume_override
6346                    .as_ref()
6347                    .unwrap()
6348                    .pinned_run
6349                    .as_deref(),
6350                Some(run.id.as_str())
6351            );
6352            assert!(!compatible_run_instruction(&run, &unchanged));
6353            assert!(may_resume_run(&run, &unchanged));
6354
6355            t.edit(
6356                "approved recovery".into(),
6357                "Reuse measured PR head B; fix Clippy; review".into(),
6358            )
6359            .unwrap();
6360            assert!(t.resume_override.is_none());
6361            t.release_operator(source, None);
6362            let reloaded: Task = serde_json::from_str(&serde_json::to_string(&t).unwrap()).unwrap();
6363            assert!(reloaded.resume_override.is_none());
6364            assert!(!may_resume_run(&run, &reloaded));
6365            let unfinished =
6366                unfinished_run_with(&reloaded.runs, reloaded.short(), |_| Ok(run.clone()))
6367                    .filter(|_| may_resume_run(&run, &reloaded));
6368            assert_eq!(
6369                choose_starter(None, false, unfinished.as_deref()),
6370                Starter::Start
6371            );
6372            assert!(instruction_for(&reloaded).starts_with("Reuse measured PR head B"));
6373            assert_eq!(
6374                reloaded.resumption_history[0].question_id.as_deref(),
6375                Some("resume-question")
6376            );
6377
6378            // A revision already persisted with the old approval/pin is also
6379            // invalidated when the new operator release is recorded.
6380            unchanged.instruction = "Reuse measured PR head B".into();
6381            unchanged.hold_machine(Some("disk hold".into()));
6382            unchanged.release_operator(source, None);
6383            assert!(unchanged.resume_override.is_none());
6384            assert!(!may_resume_run(&run, &unchanged));
6385        }
6386    }
6387
6388    #[test]
6389    fn resumption_preserves_generated_review_runs_and_rejects_revised_targets() {
6390        let mut t = task();
6391        let mut run = run_state(RunStatus::Blocked);
6392        t.review_of = Some("approved-head".into());
6393        t.instruction =
6394            "Review the branch `approved-head` (review-only: no implementation, no judging)."
6395                .into();
6396        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();
6397        run.reviewed_commits = Some(vec!["Commit log".into()]);
6398        t.start(run.id.clone());
6399        t.hold_machine(Some("interrupted review".into()));
6400        t.release_operator("cli", None);
6401        assert!(
6402            compatible_run_instruction(&run, &t),
6403            "legacy explicit review remains resumable"
6404        );
6405        let unfinished = unfinished_run_with(&t.runs, t.short(), |_| Ok(run.clone()))
6406            .filter(|_| compatible_run_instruction(&run, &t));
6407        assert_eq!(
6408            choose_starter(None, false, unfinished.as_deref()),
6409            Starter::Resume(run.id.clone())
6410        );
6411        run.task_instruction = Some(t.instruction.clone());
6412        assert!(compatible_run_instruction(&run, &t));
6413        let reloaded: RunState =
6414            serde_json::from_str(&serde_json::to_string(&run).unwrap()).unwrap();
6415        assert!(compatible_run_instruction(&reloaded, &t));
6416        t.edit(
6417            "new approved head".into(),
6418            "Reuse measured PR head B".into(),
6419        )
6420        .unwrap();
6421        assert!(!compatible_run_instruction(&run, &t));
6422
6423        // Legacy recovery reviews retain the task criteria ahead of the log.
6424        t.review_of = None;
6425        t.instruction = "Fix Clippy on approved head".into();
6426        run.task_instruction = None;
6427        run.instruction = format!(
6428            "{}\n\n# Work already on branch `approved-head` (commit subjects)\n\nCommit log",
6429            t.instruction
6430        );
6431        assert!(compatible_run_instruction(&run, &t));
6432        t.instruction.push('\n');
6433        assert!(
6434            compatible_run_instruction(&run, &t),
6435            "legacy generated review trimmed the original criteria"
6436        );
6437        t.hold_for_handover(Some("approved-head".into()), "refused".into());
6438        let mut unchanged = t.clone();
6439        assert_eq!(
6440            take_review_branch(&mut unchanged, Some(&run)).as_deref(),
6441            Some("approved-head")
6442        );
6443        t.edit("new plan".into(), "Reuse measured PR head B".into())
6444            .unwrap();
6445        assert!(take_review_branch(&mut t, Some(&run)).is_none());
6446        assert!(!compatible_run_instruction(&run, &t));
6447        t.review_branch = Some("approved-head".into());
6448        t.review_instruction = None;
6449        assert!(
6450            take_review_branch(&mut t, Some(&run)).is_none(),
6451            "legacy retained candidate cannot replace a revised approved head"
6452        );
6453    }
6454
6455    #[test]
6456    fn resumption_actions_survive_automatic_holds_of_hand_started_runs() {
6457        crate::run::pin_test_home();
6458        let dir = tempfile::tempdir().unwrap();
6459        let queue = Queue::at(dir.path().join("queue"));
6460        for resume in [true, false] {
6461            let state = run_state(RunStatus::Blocked);
6462            let mut t = task();
6463            t.start(state.id.clone());
6464            let action = if resume {
6465                ask::ChoiceAction::Resume {
6466                    run: state.id.clone(),
6467                }
6468            } else {
6469                ask::ChoiceAction::Requeue
6470            };
6471            let mut q = action_question(&state.id, action);
6472            q.status = ask::QuestionStatus::Open;
6473            q.answer = None;
6474            q.answered_at = None;
6475            t.fail("run stopped with a question", 3);
6476            hold_if_runnable(&queue, &mut t);
6477            assert!(t.operator_decided_at.is_none());
6478            assert!(t.operator_held());
6479            q.answer(ask::Answer::Choice(q.choices[0].clone())).unwrap();
6480            let expected = if resume {
6481                ActionDecision::Resume(state.id.clone())
6482            } else {
6483                ActionDecision::Requeue
6484            };
6485            assert_eq!(
6486                decide_action(&t, &q, phrases("en"), |_| Ok(state.clone())),
6487                expected
6488            );
6489            t.hold_manual(Some("later explicit hold".into()));
6490            assert_eq!(
6491                decide_action(&t, &q, phrases("en"), |_| Ok(state.clone())),
6492                ActionDecision::Stale
6493            );
6494        }
6495    }
6496
6497    #[test]
6498    fn explicit_resumption_keeps_revised_instruction_and_audit_answers() {
6499        let mut t = task();
6500        t.answers.push(crate::queue::AnsweredQuestion {
6501            question: "Inspect the remote?".into(),
6502            answer: "Inspect then wait".into(),
6503        });
6504        let old = instruction_for(&t);
6505        assert!(compatible_instruction(&old, &t));
6506        t.instruction = "Reuse approved PR head and review".into();
6507        t.release_operator("cli", None);
6508        assert!(!compatible_instruction(&old, &t));
6509        let approved = instruction_for(&t);
6510        assert!(approved.contains("waiting premises are superseded"));
6511        assert!(compatible_instruction(&approved, &t));
6512        assert_eq!(resumed_instruction(&approved, &t), approved);
6513        t.release_operator("web", None);
6514        assert!(compatible_instruction(&approved, &t));
6515        assert_eq!(t.resumption_history.len(), 1);
6516        t.start("run".into());
6517        assert!(instruction_for(&t).contains("Explicit operator resumption"));
6518    }
6519
6520    /// A run that stopped on an infrastructure wait asked no agent anything:
6521    /// the task is held by the machine under a `[resource]` reason (resumable,
6522    /// re-checked by triage), and the attempt it started is given back.
6523    #[test]
6524    fn a_resource_blocked_run_holds_its_task_and_spends_no_attempt() {
6525        let mut state = RunState::new(
6526            PathBuf::from("/repo"),
6527            "main".to_owned(),
6528            "abc1234".to_owned(),
6529            "task".to_owned(),
6530            Config::default(),
6531        );
6532        state.status = RunStatus::Blocked;
6533        state.resource_block = Some(crate::run::ResourceBlock {
6534            node: "graft".to_owned(),
6535            kind: "shared build cache".to_owned(),
6536            detail: "timed out".to_owned(),
6537            at: jiff::Timestamp::now(),
6538        });
6539        let mut t = task();
6540        t.start("20260902-000000-aaaa".to_owned());
6541        assert_eq!(t.attempts, 1);
6542        let verdict = Verdict {
6543            status: RunStatus::Blocked,
6544            left_pr: false,
6545            quota_hit: false,
6546            parked: false,
6547            no_viable_candidates: false,
6548        };
6549        settle_and_diagnose(&mut t, verdict, "blocked", 2, &state);
6550        assert_eq!(t.status, TaskStatus::Held);
6551        assert_eq!(t.attempts, 0, "the attempt is refunded");
6552        assert!(t.hold_reason.as_deref().unwrap().starts_with("[resource] "));
6553        assert!(
6554            !t.operator_held(),
6555            "a machine hold, released without a human"
6556        );
6557
6558        let mut selected = task();
6559        selected.start("20260902-000000-cccc".into());
6560        selected.replacement = Some(crate::merge_decision::Replacement {
6561            question: "fixture".into(),
6562            run: "earlier".into(),
6563            pr: "https://example.invalid/pr/1".into(),
6564            agent: "selected".into(),
6565            context: "fixture".into(),
6566        });
6567        state.resource_block.as_mut().unwrap().kind = "repository coordination".into();
6568        settle_and_diagnose(&mut selected, verdict, "blocked", 2, &state);
6569        assert_eq!(selected.attempts, 0);
6570        assert!(
6571            !selected.operator_held(),
6572            "a selected agent also retains a machine resource hold"
6573        );
6574        assert!(
6575            selected
6576                .hold_reason
6577                .as_deref()
6578                .unwrap()
6579                .starts_with("[resource] ")
6580        );
6581
6582        // A park request outranks it: still the operator's doing.
6583        let mut p = task();
6584        p.start("20260902-000000-bbbb".to_owned());
6585        settle_and_diagnose(
6586            &mut p,
6587            Verdict {
6588                parked: true,
6589                ..verdict
6590            },
6591            "parked",
6592            2,
6593            &state,
6594        );
6595        assert_eq!(p.status, TaskStatus::Parked);
6596    }
6597
6598    #[test]
6599    fn record_failure_notice_links_the_task() {
6600        let dir = tempfile::tempdir().unwrap();
6601        // A regular file where the queue directory should be makes every
6602        // `put` fail, on every platform and whoever runs the test.
6603        std::fs::write(dir.path().join("queue"), b"not a directory").unwrap();
6604        let queue = Queue::at(dir.path().join("queue"));
6605        let mut t = interrupt_task("rec-fail-1");
6606        record(&queue, &mut t);
6607
6608        let found = crate::notices::Notices::at(dir.path().join("notifications")).list();
6609        assert_eq!(found.len(), 1);
6610        assert_eq!(found[0].key, "loop:record");
6611        assert_eq!(
6612            found[0].link,
6613            Some(Link::Task {
6614                id: "rec-fail-1".to_owned()
6615            })
6616        );
6617    }
6618
6619    /// A runnable task marked to interrupt, with an id fixed for assertions
6620    /// rather than the random one [`Task::new`] mints.
6621    fn interrupt_task(id: &str) -> Task {
6622        let mut t = task();
6623        t.id = id.to_owned();
6624        t.interrupt = true;
6625        t
6626    }
6627
6628    /// An ordinary runnable task with an id fixed for assertions.
6629    fn task_with_id(id: &str) -> Task {
6630        let mut t = task();
6631        t.id = id.to_owned();
6632        t
6633    }
6634
6635    /// A runnable task marked `--urgent`, with an id fixed for assertions.
6636    fn urgent_task(id: &str) -> Task {
6637        let mut t = task();
6638        t.id = id.to_owned();
6639        t.urgent = true;
6640        t
6641    }
6642
6643    #[test]
6644    fn upgrade_resume_dispatch_keeps_claim_and_lane_limits() {
6645        let dir = tempfile::tempdir().unwrap();
6646        let queue = Queue::at(dir.path().join("queue"));
6647        let mut parked = task_with_id("parked");
6648        parked.start("run".into());
6649        parked.park("upgrade");
6650        parked.record_pass_at(
6651            "run",
6652            0,
6653            Some(Timestamp::now()),
6654            1,
6655            Some(crate::run::ParkCause::Upgrade {
6656                from: "old".into(),
6657                to: None,
6658            }),
6659        );
6660        let mut fresh = task_with_id("fresh");
6661        fresh.priority = 100;
6662        let mut urgent_task = urgent_task("urgent");
6663        for t in [&mut parked, &mut fresh, &mut urgent_task] {
6664            queue.put(t).unwrap();
6665        }
6666        let ordinary = Arc::new(tokio::sync::Semaphore::new(1));
6667        let urgent = Arc::new(tokio::sync::Semaphore::new(1));
6668        let candidates = runnable(&queue);
6669        assert_eq!(candidates[0].id, parked.id);
6670        let first = claim_dispatch(&queue, &candidates[0], false, &ordinary, &urgent).unwrap();
6671        assert!(claim_dispatch(&queue, &fresh, false, &ordinary, &urgent).is_none());
6672        assert!(claim_dispatch(&queue, &urgent_task, false, &ordinary, &urgent).is_some());
6673        drop(first);
6674        let claimed_elsewhere = queue.claim(&parked.id).unwrap();
6675        let next = runnable(&queue)
6676            .into_iter()
6677            .find_map(|t| claim_dispatch(&queue, &t, false, &ordinary, &urgent))
6678            .unwrap();
6679        assert_eq!(next.task.id, fresh.id);
6680        assert_eq!(queue.get(&parked.id).unwrap().attempts, 0);
6681        drop(claimed_elsewhere);
6682        drop(next);
6683        assert!(claim_dispatch(&queue, &parked, true, &ordinary, &urgent).is_none());
6684    }
6685
6686    #[test]
6687    fn urgent_dispatch_has_one_extra_slot_and_never_uses_a_stale_flag() {
6688        let dir = tempfile::tempdir().unwrap();
6689        let queue = Queue::at(dir.path().join("queue"));
6690        let ordinary = Arc::new(tokio::sync::Semaphore::new(1));
6691        let urgent = Arc::new(tokio::sync::Semaphore::new(1));
6692        let occupied = Arc::clone(&ordinary).try_acquire_owned().unwrap();
6693        let mut first = task();
6694        queue.put(&mut first).unwrap();
6695        let stale_plain = queue.get(&first.id).unwrap();
6696        first.set_urgent(true).unwrap();
6697        queue.put(&mut first).unwrap();
6698        let dispatched = claim_dispatch(&queue, &stale_plain, false, &ordinary, &urgent).unwrap();
6699        let mut second = urgent_task("second");
6700        queue.put(&mut second).unwrap();
6701        assert!(claim_dispatch(&queue, &second, false, &ordinary, &urgent).is_none());
6702        drop(queue.claim(&second.id).unwrap());
6703        assert_eq!(queue.get(&second.id).unwrap().attempts, 0);
6704        drop(dispatched);
6705        let stale_urgent = queue.get(&first.id).unwrap();
6706        first.set_urgent(false).unwrap();
6707        queue.put(&mut first).unwrap();
6708        assert!(claim_dispatch(&queue, &stale_urgent, false, &ordinary, &urgent).is_none());
6709        assert_eq!(urgent.available_permits(), 1);
6710        drop(queue.claim(&first.id).unwrap());
6711        drop(occupied);
6712    }
6713
6714    #[test]
6715    fn urgent_dispatch_uses_saved_lane_and_releases_unsuccessful_claims() {
6716        let dir = tempfile::tempdir().unwrap();
6717        let queue = Queue::at(dir.path().join("queue"));
6718        let ordinary = Arc::new(tokio::sync::Semaphore::new(1));
6719        let urgent = Arc::new(tokio::sync::Semaphore::new(1));
6720        let mut t = task();
6721        queue.put(&mut t).unwrap();
6722        for enabled in [true, false] {
6723            let stale = queue.get(&t.id).unwrap();
6724            t.set_urgent(enabled).unwrap();
6725            queue.put(&mut t).unwrap();
6726            assert_ne!(stale.urgent, enabled);
6727            let DispatchClaim {
6728                task: fresh,
6729                claim,
6730                permit,
6731            } = claim_dispatch(&queue, &stale, false, &ordinary, &urgent).unwrap();
6732            assert_eq!(fresh.urgent, enabled);
6733            assert_eq!(ordinary.available_permits(), usize::from(enabled));
6734            assert_eq!(urgent.available_permits(), usize::from(!enabled));
6735            assert!(queue.claim(&t.id).is_err());
6736            assert_eq!(fresh.attempts, stale.attempts);
6737            drop((claim, permit));
6738            assert_eq!(ordinary.available_permits(), 1);
6739            assert_eq!(urgent.available_permits(), 1);
6740        }
6741        t.set_urgent(true).unwrap();
6742        queue.put(&mut t).unwrap();
6743        let occupied = Arc::clone(&urgent).try_acquire_owned().unwrap();
6744        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
6745        assert_eq!(ordinary.available_permits(), 1, "no ordinary fallback");
6746        drop(queue.claim(&t.id).unwrap());
6747        assert_eq!(queue.get(&t.id).unwrap().attempts, t.attempts);
6748        drop(occupied);
6749        assert!(claim_dispatch(&queue, &t, true, &ordinary, &urgent).is_none());
6750        drop(queue.claim(&t.id).unwrap());
6751        let held = queue.claim(&t.id).unwrap();
6752        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
6753        drop(held);
6754        t.status = TaskStatus::Held;
6755        queue.put(&mut t).unwrap();
6756        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
6757        drop(queue.claim(&t.id).unwrap());
6758        std::fs::remove_file(queue.path_of(&t.id)).unwrap();
6759        assert!(claim_dispatch(&queue, &t, false, &ordinary, &urgent).is_none());
6760        drop(queue.claim(&t.id).unwrap());
6761        assert_eq!(urgent.available_permits(), 1);
6762    }
6763
6764    /// The land-resume exemption wins outright, whether or not the candidate
6765    /// also happens to be marked [`Task::urgent`]: a resume's own "must not
6766    /// queue behind anything" guarantee cannot be weaker just because the
6767    /// same task was also filed with `--urgent`.
6768    #[test]
6769    fn permit_kind_prefers_a_land_resume_over_the_urgent_slot() {
6770        assert_eq!(permit_kind(true, false), PermitKind::None);
6771        assert_eq!(permit_kind(true, true), PermitKind::None);
6772    }
6773
6774    /// The one property this whole feature exists for: `--urgent` draws from
6775    /// its own slot, never the ordinary `max_concurrent_runs` pool - and an
6776    /// ordinary candidate draws from the ordinary pool exactly as before,
6777    /// untouched by the urgent slot's existence.
6778    #[test]
6779    fn permit_kind_separates_urgent_from_ordinary() {
6780        assert_eq!(permit_kind(false, true), PermitKind::Urgent);
6781        assert_eq!(permit_kind(false, false), PermitKind::Ordinary);
6782    }
6783
6784    /// The exact wiring `attempt` runs before minting anything: a config's
6785    /// `min_free_bytes` in, a task-holding reason naming both numbers out.
6786    /// Free space is injected rather than asked of the real disk - the point
6787    /// of [`disk_gate_with`] existing separately from [`disk_gate`] - so this
6788    /// is deterministic on every machine this test runs on, never dependent
6789    /// on how full the CI runner's own disk happens to be.
6790    #[test]
6791    fn disk_gate_with_holds_a_task_below_the_threshold_and_names_both_numbers() {
6792        let cfg = Config::default();
6793        let repo = Path::new("/any/repo/path");
6794
6795        let reason =
6796            disk_gate_with(repo, &cfg, |_| Ok(1024)).expect("must hold below the threshold");
6797        assert!(reason.contains("1024"), "{reason}");
6798        assert!(
6799            reason.contains(&cfg.disk.min_free_bytes.to_string()),
6800            "{reason}"
6801        );
6802
6803        assert_eq!(
6804            disk_gate_with(repo, &cfg, |_| Ok(cfg.disk.min_free_bytes)),
6805            None,
6806            "exactly at the floor is open"
6807        );
6808        assert_eq!(
6809            disk_gate_with(repo, &cfg, |_| Ok(cfg.disk.min_free_bytes + 1)),
6810            None,
6811            "comfortably above the floor is open"
6812        );
6813    }
6814
6815    #[test]
6816    fn disk_gate_with_opens_unconditionally_when_the_operator_opted_out() {
6817        let mut cfg = Config::default();
6818        cfg.disk.min_free_bytes = 0;
6819        let repo = Path::new("/any/repo/path");
6820        assert_eq!(
6821            disk_gate_with(repo, &cfg, |_| Ok(0)),
6822            None,
6823            "a zero floor never measures at all"
6824        );
6825    }
6826
6827    #[test]
6828    fn disk_gate_with_closes_rather_than_starts_blind_when_it_cannot_measure() {
6829        let cfg = Config::default();
6830        let repo = Path::new("/any/repo/path");
6831        let reason = disk_gate_with(repo, &cfg, |_| Err(anyhow::anyhow!("no df on this box")))
6832            .expect("a measurement failure must close the gate, not open it");
6833        assert!(reason.contains("could not measure"), "{reason}");
6834    }
6835
6836    #[test]
6837    fn no_interrupt_task_leaves_the_sequence_idle_even_with_something_in_flight() {
6838        let ordinary = task();
6839        let next = advance_interrupt(
6840            Interrupt::Idle,
6841            std::slice::from_ref(&ordinary.id),
6842            std::slice::from_ref(&ordinary),
6843        );
6844        assert_eq!(next, Interrupt::Idle);
6845    }
6846
6847    #[test]
6848    fn an_interrupt_task_with_nothing_in_flight_never_starts_a_sequence() {
6849        // Nothing to interrupt - this is just an ordinary candidate, and the
6850        // loop's normal dispatch will pick it up like any other.
6851        let marked = interrupt_task("marked");
6852        let next = advance_interrupt(Interrupt::Idle, &[], std::slice::from_ref(&marked));
6853        assert_eq!(next, Interrupt::Idle);
6854    }
6855
6856    #[test]
6857    fn an_interrupt_task_with_something_in_flight_starts_parking_it() {
6858        let marked = interrupt_task("marked");
6859        let next = advance_interrupt(
6860            Interrupt::Idle,
6861            &["running".to_owned()],
6862            std::slice::from_ref(&marked),
6863        );
6864        assert_eq!(
6865            next,
6866            Interrupt::Parking {
6867                parked: vec!["running".to_owned()],
6868                interrupt_task: "marked".to_owned(),
6869            }
6870        );
6871    }
6872
6873    /// R1-1-2 / R2-1-2: above the default `max_concurrent_runs`, more than
6874    /// one run can be in flight when a task becomes runnable and marked.
6875    /// Parking all of them would mean `Resuming` later has more than one id
6876    /// to release back to ordinary dispatch, which cannot be made safe
6877    /// against that same setting's own extra concurrency slots letting two
6878    /// of them start together - see `advance_interrupt`'s own `Idle` branch.
6879    /// The simplification the task's own constraints ask for: do not begin
6880    /// a sequence at all until the herd settles back to exactly one.
6881    #[test]
6882    fn more_than_one_run_in_flight_never_starts_an_interrupt_sequence() {
6883        let marked = interrupt_task("marked");
6884
6885        let two = advance_interrupt(
6886            Interrupt::Idle,
6887            &["a".to_owned(), "b".to_owned()],
6888            std::slice::from_ref(&marked),
6889        );
6890        assert_eq!(two, Interrupt::Idle);
6891
6892        let none = advance_interrupt(Interrupt::Idle, &[], std::slice::from_ref(&marked));
6893        assert_eq!(none, Interrupt::Idle, "nothing to interrupt either");
6894    }
6895
6896    #[test]
6897    fn parking_holds_until_every_parked_id_has_actually_left_flight() {
6898        let state = Interrupt::Parking {
6899            parked: vec!["running".to_owned()],
6900            interrupt_task: "marked".to_owned(),
6901        };
6902        // Still in flight: no change.
6903        let still_going = advance_interrupt(state.clone(), &["running".to_owned()], &[]);
6904        assert_eq!(still_going, state);
6905
6906        // Left flight, but the interrupt task has not been dispatched yet on
6907        // this tick - stays `Parking` so `interrupt_gate` can let it through,
6908        // as long as it is still runnable.
6909        let stopped_but_not_yet_dispatched =
6910            advance_interrupt(state.clone(), &[], &[interrupt_task("marked")]);
6911        assert_eq!(stopped_but_not_yet_dispatched, state);
6912
6913        // Left flight, and the interrupt task is now in flight itself.
6914        let dispatched = advance_interrupt(state, &["marked".to_owned()], &[]);
6915        assert_eq!(
6916            dispatched,
6917            Interrupt::Running {
6918                parked: vec!["running".to_owned()],
6919                interrupt_task: "marked".to_owned(),
6920            }
6921        );
6922    }
6923
6924    #[test]
6925    fn the_sequence_moves_to_resuming_the_instant_the_interrupt_tasks_own_run_leaves_flight() {
6926        let state = Interrupt::Running {
6927            parked: vec!["running".to_owned()],
6928            interrupt_task: "marked".to_owned(),
6929        };
6930        let still_running = advance_interrupt(state.clone(), &["marked".to_owned()], &[]);
6931        assert_eq!(still_running, state);
6932
6933        // Whatever it ended as - merged, failed, held - is not this
6934        // function's concern: leaving flight is the only trigger, driven
6935        // straight off the same in-flight list `poll` already reaps. It does
6936        // not go straight to `Idle`: see `Interrupt::Running`'s own doc for
6937        // why that would let an unrelated task start ahead of, or alongside,
6938        // the guaranteed resume.
6939        let ended = advance_interrupt(state, &[], &[task_with_id("running")]);
6940        assert_eq!(
6941            ended,
6942            Interrupt::Resuming {
6943                parked: vec!["running".to_owned()]
6944            }
6945        );
6946    }
6947
6948    #[test]
6949    fn resuming_ends_the_instant_a_parked_task_is_seen_in_flight() {
6950        let state = Interrupt::Resuming {
6951            parked: vec!["running".to_owned()],
6952        };
6953        let still_waiting = advance_interrupt(state.clone(), &[], &[task_with_id("running")]);
6954        assert_eq!(still_waiting, state);
6955
6956        let dispatched = advance_interrupt(state, &["running".to_owned()], &[]);
6957        assert_eq!(dispatched, Interrupt::Idle);
6958    }
6959
6960    /// R1-2-1: an interrupt task that stops being runnable - held, blocked,
6961    /// or otherwise moved on by an operator with no claim standing in the
6962    /// way - must not wedge the sequence (and so the whole loop's dispatch,
6963    /// via `interrupt_gate`) waiting forever for a dispatch that can never
6964    /// come. The parked run still gets its resume.
6965    #[test]
6966    fn an_interrupt_task_that_stops_being_runnable_abandons_the_wait_without_losing_the_parked_run()
6967    {
6968        let state = Interrupt::Parking {
6969            parked: vec!["running".to_owned()],
6970            interrupt_task: "marked".to_owned(),
6971        };
6972        // `marked` has been held/blocked/deleted since the sequence began:
6973        // it no longer appears in `runnable` at all.
6974        let next = advance_interrupt(state, &[], &[]);
6975        assert_eq!(
6976            next,
6977            Interrupt::Resuming {
6978                parked: vec!["running".to_owned()]
6979            },
6980            "abandoning the interrupt must not abandon the resume it owes"
6981        );
6982    }
6983
6984    /// The same abandonment, one step later: `Resuming` itself must not wait
6985    /// forever for a parked task that has since become unrunnable.
6986    #[test]
6987    fn resuming_abandons_a_parked_task_that_stops_being_runnable() {
6988        let state = Interrupt::Resuming {
6989            parked: vec!["running".to_owned()],
6990        };
6991        let next = advance_interrupt(state, &[], &[]);
6992        assert_eq!(
6993            next,
6994            Interrupt::Idle,
6995            "nothing is left to wait for; the loop must not stay wedged"
6996        );
6997    }
6998
6999    #[test]
7000    fn disabled_by_config_the_sequence_can_never_leave_idle() {
7001        let marked = interrupt_task("marked");
7002        let next = advance_interrupt_tick(
7003            false,
7004            Interrupt::Idle,
7005            &["running".to_owned()],
7006            std::slice::from_ref(&marked),
7007        );
7008        assert_eq!(
7009            next,
7010            Interrupt::Idle,
7011            "an unmarked, unconfigured daemon must behave exactly as before"
7012        );
7013    }
7014
7015    #[test]
7016    fn the_gate_blocks_everyone_while_something_parked_is_still_in_flight() {
7017        let state = Interrupt::Parking {
7018            parked: vec!["running".to_owned()],
7019            interrupt_task: "marked".to_owned(),
7020        };
7021        let candidates = vec![interrupt_task("marked"), task()];
7022        let allowed = interrupt_gate(&state, &["running".to_owned()], candidates);
7023        assert!(
7024            allowed.is_empty(),
7025            "nothing may dispatch - not even the interrupt task itself - \
7026             until the parked run has actually stopped"
7027        );
7028    }
7029
7030    /// `interrupt_gate` knows nothing about [`Task::urgent`] and must not
7031    /// grow a special case for it: an urgent candidate that is not the
7032    /// interrupt task withholds exactly like an ordinary one for as long as
7033    /// the parked run it is racing has not actually left flight. A version
7034    /// of this feature once bypassed the gate for urgent candidates, on the
7035    /// theory that a genuinely separate concurrency lane could not collide
7036    /// with 75dd's own; it could, and did - the bypassed task dispatched
7037    /// alongside a run 75dd's own state machine had only *asked* to park,
7038    /// not yet confirmed gone, which is exactly the second run
7039    /// `Interrupt::Parking`'s own doc says must never happen. There is no
7040    /// tick during Parking/Running/Resuming where letting an extra
7041    /// candidate through is safe, urgent or not - the parked run may still
7042    /// be genuinely mid-call.
7043    #[test]
7044    fn urgent_gains_no_exemption_from_an_active_interrupt_sequence() {
7045        for state in [
7046            Interrupt::Parking {
7047                parked: vec!["running".to_owned()],
7048                interrupt_task: "marked".to_owned(),
7049            },
7050            Interrupt::Running {
7051                parked: vec!["running".to_owned()],
7052                interrupt_task: "marked".to_owned(),
7053            },
7054            Interrupt::Resuming {
7055                parked: vec!["running".to_owned()],
7056            },
7057        ] {
7058            let candidates = vec![
7059                interrupt_task("marked"),
7060                urgent_task("hot"),
7061                task_with_id("ordinary"),
7062            ];
7063            let allowed = interrupt_gate(&state, &["running".to_owned()], candidates);
7064            assert!(
7065                !allowed.iter().any(|t| t.id == "hot"),
7066                "an urgent candidate must wait out the same gate as anything \
7067                 else while the run it would run alongside has not actually \
7068                 left flight, for state {state:?}: {allowed:?}"
7069            );
7070        }
7071    }
7072
7073    /// The one case where marking a task `--urgent` and `interrupt` at once
7074    /// is not redundant: once the interrupt sequence's own guaranteed
7075    /// resume/dispatch actually admits the task, it is unaffected by also
7076    /// carrying `urgent` - `interrupt_gate` decides purely on identity,
7077    /// never on the urgent flag.
7078    #[test]
7079    fn a_task_marked_both_urgent_and_interrupt_is_admitted_once_the_gate_itself_says_so() {
7080        let state = Interrupt::Resuming {
7081            parked: vec!["hot".to_owned()],
7082        };
7083        let candidates = vec![urgent_task("hot"), task()];
7084        let allowed = interrupt_gate(&state, &[], candidates);
7085        assert_eq!(
7086            allowed.iter().filter(|t| t.id == "hot").count(),
7087            1,
7088            "the gate's own decision is unaffected by the urgent flag: {allowed:?}"
7089        );
7090    }
7091
7092    #[test]
7093    fn the_gate_lets_only_the_interrupt_task_through_once_parked_work_has_stopped() {
7094        let state = Interrupt::Parking {
7095            parked: vec!["running".to_owned()],
7096            interrupt_task: "marked".to_owned(),
7097        };
7098        let other = task();
7099        let candidates = vec![interrupt_task("marked"), other.clone()];
7100        let allowed = interrupt_gate(&state, &[], candidates);
7101        assert_eq!(allowed.len(), 1);
7102        assert_eq!(allowed[0].id, "marked");
7103    }
7104
7105    #[test]
7106    fn the_gate_blocks_everyone_while_the_interrupt_task_itself_is_in_flight() {
7107        let state = Interrupt::Running {
7108            parked: vec!["running".to_owned()],
7109            interrupt_task: "marked".to_owned(),
7110        };
7111        let candidates = vec![task(), task()];
7112        let allowed = interrupt_gate(&state, &["marked".to_owned()], candidates);
7113        assert!(allowed.is_empty());
7114    }
7115
7116    /// R1-1-1 / R1-1-2: even when more than one task was in flight when the
7117    /// sequence began (only reachable above the default
7118    /// `max_concurrent_runs = 1`), `Resuming` offers at most one of them -
7119    /// never both in the same tick, which is what "exactly one resume, no
7120    /// simultaneous run" actually requires structurally rather than by
7121    /// coincidence of how many ordinary slots happen to be free.
7122    #[test]
7123    fn the_gate_offers_at_most_one_candidate_while_resuming_even_with_two_parked() {
7124        let state = Interrupt::Resuming {
7125            parked: vec!["a".to_owned(), "c".to_owned()],
7126        };
7127        let candidates = vec![task_with_id("a"), task_with_id("c"), task_with_id("other")];
7128        let allowed = interrupt_gate(&state, &[], candidates);
7129        assert_eq!(
7130            allowed.len(),
7131            1,
7132            "at most one candidate may be offered while resuming: {allowed:?}"
7133        );
7134        assert_eq!(allowed[0].id, "a");
7135    }
7136
7137    #[test]
7138    fn the_gate_offers_nothing_while_resuming_if_no_parked_task_is_runnable() {
7139        let state = Interrupt::Resuming {
7140            parked: vec!["a".to_owned()],
7141        };
7142        let allowed = interrupt_gate(&state, &[], vec![task_with_id("other")]);
7143        assert!(allowed.is_empty());
7144    }
7145
7146    /// The invariant the completion criteria ask for by name: across a whole
7147    /// simulated sequence, there is never a tick where the gate would let
7148    /// through both the parked run's resume and the interrupt task, and
7149    /// exactly one candidate resumes the instant the interrupt task's run
7150    /// ends - never zero, never more than one.
7151    #[test]
7152    fn a_full_sequence_never_gates_two_runs_through_at_once_and_resumes_exactly_one() {
7153        let running = task(); // id: whatever `Task::new` minted
7154        let marked = interrupt_task("marked");
7155
7156        let mut state = Interrupt::Idle;
7157        // Tick 1: `running` is in flight, `marked` becomes runnable.
7158        let in_flight = vec![running.id.clone()];
7159        state = advance_interrupt_tick(true, state, &in_flight, std::slice::from_ref(&marked));
7160        let gated = interrupt_gate(&state, &in_flight, vec![marked.clone(), running.clone()]);
7161        assert!(gated.is_empty(), "still waiting on `running` to park");
7162
7163        // Tick 2: `running` parked and left flight; nothing dispatched yet.
7164        state = advance_interrupt_tick(true, state, &[], &[marked.clone(), running.clone()]);
7165        let gated = interrupt_gate(&state, &[], vec![marked.clone(), running.clone()]);
7166        assert_eq!(
7167            gated.iter().map(|t| t.id.as_str()).collect::<Vec<_>>(),
7168            vec!["marked"],
7169            "only the interrupt task may be offered to the dispatcher now"
7170        );
7171
7172        // Tick 3: `marked` is now in flight (dispatched from tick 2's gate).
7173        state = advance_interrupt_tick(
7174            true,
7175            state,
7176            &["marked".to_owned()],
7177            std::slice::from_ref(&running),
7178        );
7179        let gated = interrupt_gate(
7180            &state,
7181            &["marked".to_owned()],
7182            vec![marked.clone(), running.clone()],
7183        );
7184        assert!(
7185            gated.is_empty(),
7186            "the parked run must not be offered back while the interrupt \
7187             task is still running"
7188        );
7189
7190        // Tick 4: `marked`'s run reached a terminal status and left flight.
7191        // A higher-priority ordinary task `other` is also runnable now - it
7192        // must not be let through instead of, or alongside, `running`.
7193        let other = task_with_id("other");
7194        state = advance_interrupt_tick(true, state, &[], &[running.clone(), other.clone()]);
7195        assert_eq!(
7196            state,
7197            Interrupt::Resuming {
7198                parked: vec![running.id.clone()]
7199            }
7200        );
7201        let gated = interrupt_gate(&state, &[], vec![other.clone(), running.clone()]);
7202        assert_eq!(
7203            gated.iter().map(|t| t.id.as_str()).collect::<Vec<_>>(),
7204            vec![running.id.as_str()],
7205            "exactly the parked run resumes - not the unrelated task, even \
7206             though it was offered first"
7207        );
7208
7209        // Tick 5: `running` is now in flight (dispatched from tick 4's
7210        // gate). Only now does the sequence end and ordinary dispatch fully
7211        // resume.
7212        state = advance_interrupt_tick(
7213            true,
7214            state,
7215            std::slice::from_ref(&running.id),
7216            std::slice::from_ref(&other),
7217        );
7218        assert_eq!(state, Interrupt::Idle);
7219        let gated = interrupt_gate(
7220            &state,
7221            std::slice::from_ref(&running.id),
7222            vec![other.clone()],
7223        );
7224        assert_eq!(
7225            gated.iter().map(|t| t.id.as_str()).collect::<Vec<_>>(),
7226            vec![other.id.as_str()],
7227            "ordinary dispatch is unrestricted again"
7228        );
7229    }
7230
7231    #[test]
7232    fn every_run_status_settles_the_task_it_came_from() {
7233        // run status, resulting task status, attempts still standing after one
7234        let table = [
7235            (RunStatus::Merged, TaskStatus::Done, 1),
7236            (RunStatus::Ready, TaskStatus::Done, 1),
7237            (RunStatus::Stalled, TaskStatus::Failed, 0),
7238            (RunStatus::Blocked, TaskStatus::Failed, 1),
7239            (RunStatus::Failed, TaskStatus::Failed, 1),
7240            (RunStatus::VerifiedNoop, TaskStatus::Held, 1),
7241            (RunStatus::Prep, TaskStatus::Failed, 1),
7242            (RunStatus::Implementing, TaskStatus::Failed, 1),
7243            (RunStatus::Judging, TaskStatus::Failed, 1),
7244            (RunStatus::Deliberating, TaskStatus::Failed, 1),
7245            (RunStatus::Voting, TaskStatus::Failed, 1),
7246            (RunStatus::Reviewing, TaskStatus::Failed, 1),
7247            (RunStatus::Gating, TaskStatus::Failed, 1),
7248        ];
7249        for (run, want, attempts) in table {
7250            let mut t = task();
7251            t.start("20260902-000000-aaaa".to_owned());
7252            settle(
7253                &mut t,
7254                Verdict {
7255                    status: run,
7256                    left_pr: false,
7257                    parked: false,
7258                    quota_hit: matches!(run, RunStatus::Stalled),
7259                    no_viable_candidates: false,
7260                },
7261                "why",
7262                2,
7263            );
7264            assert_eq!(t.status, want, "task status after {}", label(run));
7265            assert_eq!(t.attempts, attempts, "attempts after {}", label(run));
7266        }
7267    }
7268
7269    #[test]
7270    fn a_quota_stall_costs_the_task_no_attempt_but_a_block_does() {
7271        let mut stalled = task();
7272        stalled.start("20260902-000000-aaaa".to_owned());
7273        settle(
7274            &mut stalled,
7275            Verdict {
7276                status: RunStatus::Stalled,
7277                left_pr: false,
7278                parked: false,
7279                quota_hit: true,
7280                no_viable_candidates: false,
7281            },
7282            "quota",
7283            1,
7284        );
7285        assert_eq!(stalled.attempts, 0);
7286        assert!(
7287            stalled.status.runnable(),
7288            "a machine problem must leave the task in line"
7289        );
7290
7291        let mut blocked = task();
7292        blocked.start("20260902-000000-aaaa".to_owned());
7293        settle(
7294            &mut blocked,
7295            Verdict {
7296                status: RunStatus::Blocked,
7297                left_pr: false,
7298                parked: false,
7299                quota_hit: false,
7300                no_viable_candidates: false,
7301            },
7302            "findings open",
7303            1,
7304        );
7305        assert_eq!(blocked.attempts, 1);
7306        assert_eq!(
7307            blocked.status,
7308            TaskStatus::Held,
7309            "the last attempt hands the task to a human"
7310        );
7311    }
7312
7313    #[test]
7314    fn a_run_that_opened_a_pull_request_is_never_re_competed() {
7315        // Attempts to spare: without the pull request this task would go
7316        // straight back in line and run the whole competition again.
7317        let mut delivered = task();
7318        delivered.start("20260903-080619-01c2".to_owned());
7319        settle(
7320            &mut delivered,
7321            Verdict {
7322                status: RunStatus::Blocked,
7323                left_pr: true,
7324                parked: false,
7325                quota_hit: false,
7326                no_viable_candidates: false,
7327            },
7328            "no check status",
7329            4,
7330        );
7331        assert_eq!(
7332            delivered.status,
7333            TaskStatus::Held,
7334            "a pull request waiting on CI or a person is not a retryable failure"
7335        );
7336        assert!(
7337            !delivered.status.runnable(),
7338            "the loop must not pick this task up again"
7339        );
7340        assert_eq!(
7341            delivered.last_error.as_deref(),
7342            Some("no check status"),
7343            "the operator needs to be told what the gate was waiting for"
7344        );
7345
7346        // The same status without a pull request is a plain failure, and with
7347        // attempts left it is retried.
7348        let mut empty_handed = task();
7349        empty_handed.start("20260903-080619-01c2".to_owned());
7350        settle(
7351            &mut empty_handed,
7352            Verdict {
7353                status: RunStatus::Blocked,
7354                left_pr: false,
7355                parked: false,
7356                quota_hit: false,
7357                no_viable_candidates: false,
7358            },
7359            "findings open",
7360            4,
7361        );
7362        assert_eq!(empty_handed.status, TaskStatus::Failed);
7363        assert!(empty_handed.status.runnable());
7364    }
7365
7366    #[test]
7367    fn a_verified_noop_run_hands_off_rather_than_closing_or_auto_retrying() {
7368        // Every candidate agreed, with evidence, that nothing belonged in the
7369        // worktree. That is not a confirmed success to close automatically -
7370        // a human still has to check the claim - and it is not an ordinary
7371        // failure either, so this settles exactly like a pull request nobody
7372        // merged yet: `Held`, same as `Blocked` with a PR.
7373        let mut noop = task();
7374        noop.start("20260912-131304-391f".to_owned());
7375        settle(
7376            &mut noop,
7377            Verdict {
7378                status: RunStatus::VerifiedNoop,
7379                left_pr: false,
7380                parked: false,
7381                quota_hit: false,
7382                no_viable_candidates: true,
7383            },
7384            "candidate A: already fixed by b32cfc4, on main",
7385            4,
7386        );
7387        assert_eq!(
7388            noop.status,
7389            TaskStatus::Held,
7390            "an unverified claim is a request for a human, not a failure"
7391        );
7392        assert!(
7393            !noop.status.runnable(),
7394            "the loop must not requeue this on the same unverified claim"
7395        );
7396        // `Task::release` resets attempts to zero the moment a human looks at
7397        // the evidence and lets it run again, so it does not matter here
7398        // whether the one attempt already spent stays spent - what matters is
7399        // that nothing retries this task unattended in the meantime.
7400        assert_eq!(noop.attempts, 1);
7401    }
7402
7403    #[test]
7404    fn parking_costs_the_task_no_attempt_and_leaves_it_in_line() {
7405        // Parking is the operator asking for the process back - to replace the
7406        // binary, most of all. The run's work is intact on disk, so this is
7407        // not a failed attempt, and charging for it would mean a few upgrades
7408        // could exhaust a budget meant for agents that misbehaved.
7409        let mut parked = task();
7410        parked.start("20260903-183634-2d98".to_owned());
7411        settle(
7412            &mut parked,
7413            Verdict {
7414                status: RunStatus::Implementing,
7415                left_pr: false,
7416                quota_hit: false,
7417                parked: true,
7418                no_viable_candidates: false,
7419            },
7420            "parked after `implementing`",
7421            2,
7422        );
7423        assert_eq!(parked.attempts, 0, "a park is refunded");
7424        assert!(
7425            parked.status.runnable(),
7426            "and the task stays in line so the next loop resumes its run"
7427        );
7428        assert_eq!(parked.status, TaskStatus::Parked);
7429        assert_eq!(
7430            parked.park_reason.as_deref(),
7431            Some("parked after `implementing`"),
7432            "the card says where it stopped"
7433        );
7434        assert_eq!(parked.last_error, None, "nothing failed");
7435
7436        // Without the park flag the same non-terminal status is what it always
7437        // was: `execute` returning mid-flight, which is a bug and spends an
7438        // attempt so a task cannot loop on it forever.
7439        let mut broken = task();
7440        broken.start("20260903-183634-2d98".to_owned());
7441        settle(
7442            &mut broken,
7443            Verdict {
7444                status: RunStatus::Implementing,
7445                left_pr: false,
7446                quota_hit: false,
7447                parked: false,
7448                no_viable_candidates: false,
7449            },
7450            "returned mid-flight",
7451            2,
7452        );
7453        assert_eq!(broken.attempts, 1);
7454    }
7455
7456    #[test]
7457    fn only_a_rate_limit_buys_the_task_its_attempt_back() {
7458        // Run e633: quorum lost because two judges answered with the wrong
7459        // JSON shape, `quota: []`. Refunding that takes the bound off the
7460        // retry loop, and each retry pays for a fresh hour-long implement
7461        // wave before it can fail the same way.
7462        let mut flaky = task();
7463        flaky.start("20260903-123023-e633".to_owned());
7464        settle(
7465            &mut flaky,
7466            Verdict {
7467                status: RunStatus::Stalled,
7468                left_pr: false,
7469                parked: false,
7470                quota_hit: false,
7471                no_viable_candidates: false,
7472            },
7473            "verdict rests on 1 of 3 judges",
7474            2,
7475        );
7476        assert_eq!(
7477            flaky.attempts, 1,
7478            "flakiness spends an attempt, so `max_attempts` still bounds it"
7479        );
7480        assert!(flaky.status.runnable(), "and it is still worth retrying");
7481
7482        // The same status, lost to a rate limit, is the machine's fault.
7483        let mut limited = task();
7484        limited.start("20260903-123023-e633".to_owned());
7485        settle(
7486            &mut limited,
7487            Verdict {
7488                status: RunStatus::Stalled,
7489                left_pr: false,
7490                parked: false,
7491                quota_hit: true,
7492                no_viable_candidates: false,
7493            },
7494            "judge-2, judge-3 out of quota",
7495            2,
7496        );
7497        assert_eq!(limited.attempts, 0, "a quota window is refunded");
7498        assert!(limited.status.runnable());
7499
7500        // And the bound really binds: a task that keeps stalling on flakiness
7501        // reaches a human instead of running the roster forever.
7502        let mut worn = task();
7503        for _ in 0..2 {
7504            worn.release();
7505        }
7506        worn.start("20260903-123023-e633".to_owned());
7507        worn.attempts = 2;
7508        settle(
7509            &mut worn,
7510            Verdict {
7511                status: RunStatus::Stalled,
7512                left_pr: false,
7513                parked: false,
7514                quota_hit: false,
7515                no_viable_candidates: false,
7516            },
7517            "no quorum again",
7518            2,
7519        );
7520        assert_eq!(worn.status, TaskStatus::Held);
7521        assert!(!worn.status.runnable());
7522    }
7523
7524    #[test]
7525    fn a_quota_wipeout_that_leaves_nothing_to_judge_also_costs_no_attempt() {
7526        // The implement wave loses every seat to the same rate limit and
7527        // `after_implement` bails with nothing viable, which surfaces as
7528        // `Failed` rather than `Stalled`. That is the same machine fact the
7529        // `Stalled`-quota row already refunds, and must be refunded the same
7530        // way, or a quota outage quietly holds every task it touches instead
7531        // of leaving them in line for the reset.
7532        let mut wiped_out = task();
7533        wiped_out.start("20260907-025000-a1b2".to_owned());
7534        settle(
7535            &mut wiped_out,
7536            Verdict {
7537                status: RunStatus::Failed,
7538                left_pr: false,
7539                parked: false,
7540                quota_hit: true,
7541                no_viable_candidates: true,
7542            },
7543            "no candidate produced a change; nothing to judge",
7544            2,
7545        );
7546        assert_eq!(wiped_out.attempts, 0, "a total quota wipeout is refunded");
7547        assert!(
7548            wiped_out.status.runnable(),
7549            "a machine problem must leave the task in line"
7550        );
7551
7552        // This is the exemption that must stay narrow: a candidate that did
7553        // produce a change, and then failed for some other reason, still
7554        // spends the attempt even though a seat elsewhere hit its quota.
7555        // Otherwise every ordinary failure that happens to share a run with
7556        // an unrelated rate limit would be refunded for free.
7557        let mut partial_progress = task();
7558        partial_progress.start("20260907-025500-c3d4".to_owned());
7559        settle(
7560            &mut partial_progress,
7561            Verdict {
7562                status: RunStatus::Failed,
7563                left_pr: false,
7564                parked: false,
7565                quota_hit: true,
7566                no_viable_candidates: false,
7567            },
7568            "gate failed on the winning candidate",
7569            2,
7570        );
7571        assert_eq!(
7572            partial_progress.attempts, 1,
7573            "a candidate that actually produced a change spends the attempt \
7574             even though some other seat hit its quota"
7575        );
7576        assert!(partial_progress.status.runnable());
7577    }
7578
7579    #[test]
7580    fn reclaim_refunds_a_recovered_quota_wipeout_the_same_way_a_live_settle_does() {
7581        // `reclaim` builds its own `Verdict` from a `RunState` it loads off
7582        // disk, and that construction must reach the same conclusion as the
7583        // one `attempt` builds from a live run, or a crash at exactly the
7584        // wrong moment gives a recovered task a different policy than one a
7585        // daemon finished settling itself.
7586        let mut t = task();
7587        t.start("20260907-025000-a1b2".to_owned());
7588        let mut state = run_state(RunStatus::Failed);
7589        state.quota.push(QuotaLoss {
7590            seat: "cand-a".to_owned(),
7591            node: "implement".to_owned(),
7592            at: Timestamp::now(),
7593            reset: None,
7594        });
7595        assert!(
7596            state.viable().is_empty(),
7597            "no candidate was added, so nothing is viable"
7598        );
7599        reclaim(&mut t, Some(state), 2, "en");
7600        assert_eq!(t.attempts, 0, "a recovered quota wipeout is refunded");
7601        assert!(t.status.runnable());
7602    }
7603
7604    #[test]
7605    fn a_held_task_is_never_offered_to_the_loop() {
7606        let dir = tempfile::tempdir().unwrap();
7607        let queue = Queue::at(dir.path().to_path_buf());
7608        for (n, priority) in [(1, 0), (2, 5), (3, 5)] {
7609            let mut t = task();
7610            t.id = format!("2026090{n}-000000-000{n}");
7611            t.priority = priority;
7612            queue.put(&mut t).unwrap();
7613        }
7614        let mut held = task();
7615        held.id = "20260909-000000-9999".to_owned();
7616        held.priority = 99;
7617        held.hold_machine(None);
7618        queue.put(&mut held).unwrap();
7619
7620        let order: Vec<String> = runnable(&queue).into_iter().map(|t| t.id).collect();
7621        assert_eq!(order.len(), 3);
7622        assert!(!order.contains(&held.id));
7623        assert_eq!(
7624            order.first().cloned(),
7625            queue.next_runnable().map(|t| t.id),
7626            "the loop's first candidate is exactly what the queue offers"
7627        );
7628        assert_eq!(
7629            order,
7630            vec![
7631                "20260902-000000-0002".to_owned(),
7632                "20260903-000000-0003".to_owned(),
7633                "20260901-000000-0001".to_owned(),
7634            ],
7635            "priority first, then oldest, so nothing starves"
7636        );
7637    }
7638
7639    #[test]
7640    fn sweep_removes_an_old_unparseable_lock_and_keeps_a_live_one() {
7641        let dir = tempfile::tempdir().unwrap();
7642        let queue = Queue::at(dir.path().to_path_buf());
7643        let mut old = task();
7644        old.id = "20260101-000000-old0".to_owned();
7645        queue.put(&mut old).unwrap();
7646        let mut fresh = task();
7647        fresh.id = "20260101-000000-new0".to_owned();
7648        queue.put(&mut fresh).unwrap();
7649
7650        // No parseable pid at all, so age is the only signal there is to
7651        // check - unlike a real `Queue::claim`, which always names a real,
7652        // and therefore alive, pid this test cannot fake as dead.
7653        std::fs::write(dir.path().join(format!("{}.lock", old.id)), "not a pid").unwrap();
7654        std::thread::sleep(Duration::from_millis(60));
7655        let live = queue.claim(&fresh.id).unwrap();
7656
7657        let swept = sweep_stale_claims(&queue, Duration::from_millis(50));
7658        assert_eq!(swept, vec![old.id.clone()]);
7659        assert!(
7660            queue.claim(&old.id).is_ok(),
7661            "an unparseable lock older than the threshold is swept"
7662        );
7663        assert!(
7664            queue.claim(&fresh.id).is_err(),
7665            "a live pid protects its lock regardless of age"
7666        );
7667        drop(live);
7668    }
7669
7670    #[test]
7671    fn an_old_lock_whose_pid_is_still_alive_is_never_swept_by_age_alone() {
7672        // A live worker's claim remains authoritative even beyond the sweep's
7673        // age limit. Sweeping it would permit a second competing attempt.
7674        let dir = tempfile::tempdir().unwrap();
7675        let queue = Queue::at(dir.path().to_path_buf());
7676        let mut t = task();
7677        t.id = "20260101-000000-live".to_owned();
7678        queue.put(&mut t).unwrap();
7679
7680        let claim = queue.claim(&t.id).unwrap();
7681        std::thread::sleep(Duration::from_millis(60));
7682
7683        let swept = sweep_stale_claims(&queue, Duration::from_millis(50));
7684        assert!(
7685            swept.is_empty(),
7686            "a lock naming a live pid must never be swept by age, no matter how old: {swept:?}"
7687        );
7688        assert!(
7689            queue.claim(&t.id).is_err(),
7690            "the lock still protects its task"
7691        );
7692        drop(claim);
7693    }
7694
7695    /// このテストプロセスにはなり得ない決定的なフィクスチャ PID。
7696    /// OS 上の状態は意図的に無関係で、各利用箇所が方針問い合わせを注入する。
7697    fn injected_dead_pid() -> u32 {
7698        std::process::id().checked_add(1).unwrap_or(1)
7699    }
7700
7701    #[test]
7702    fn a_lock_naming_a_dead_pid_is_swept_at_once_regardless_of_age() {
7703        let dir = tempfile::tempdir().unwrap();
7704        let queue = Queue::at(dir.path().to_path_buf());
7705        let mut t = task();
7706        t.id = "20260101-000000-dead".to_owned();
7707        queue.put(&mut t).unwrap();
7708        let dead_pid = injected_dead_pid();
7709
7710        // Written directly rather than through `Queue::claim`, which would
7711        // stamp this test process's own very much alive pid and defeat the
7712        // point: this is what a `.lock` left by a `SIGKILL`ed daemon looks
7713        // like moments after it died, not six hours later.
7714        std::fs::write(
7715            dir.path().join(format!("{}.lock", t.id)),
7716            dead_pid.to_string(),
7717        )
7718        .unwrap();
7719
7720        let swept = sweep_stale_claims_with(&queue, Duration::from_secs(6 * 60 * 60), |pid| {
7721            pid != dead_pid
7722        });
7723        assert_eq!(
7724            swept,
7725            vec![t.id.clone()],
7726            "a dead owner is reclaimed immediately, not after STALE_CLAIM"
7727        );
7728        assert!(queue.claim(&t.id).is_ok(), "the task is claimable again");
7729    }
7730
7731    #[test]
7732    fn sweeping_on_every_poll_catches_a_lock_that_appears_after_the_first_sweep() {
7733        let dir = tempfile::tempdir().unwrap();
7734        let queue = Queue::at(dir.path().to_path_buf());
7735        let mut t = task();
7736        t.id = "20260101-000000-late".to_owned();
7737        queue.put(&mut t).unwrap();
7738        let dead_pid = injected_dead_pid();
7739
7740        // Tick one, standing in for the sweep `poll` already runs at
7741        // startup: nothing to find yet.
7742        assert!(
7743            sweep_stale_claims(&queue, Duration::from_secs(6 * 60 * 60)).is_empty(),
7744            "nothing has claimed the task yet"
7745        );
7746
7747        // A second daemon claims the task and dies before it ever writes
7748        // `running`, well after this loop's own startup sweep already ran.
7749        std::fs::write(
7750            dir.path().join(format!("{}.lock", t.id)),
7751            dead_pid.to_string(),
7752        )
7753        .unwrap();
7754
7755        // Tick two, standing in for a poll long into this daemon's uptime:
7756        // the same function, called again, notices what only just appeared -
7757        // proving the sweep is not a one-shot startup check.
7758        let swept = sweep_stale_claims_with(&queue, Duration::from_secs(6 * 60 * 60), |pid| {
7759            pid != dead_pid
7760        });
7761        assert_eq!(swept, vec![t.id.clone()]);
7762    }
7763
7764    #[test]
7765    fn a_running_task_behind_a_dead_daemons_lock_recovers_once_swept_and_keeps_its_history() {
7766        // `reclaim_orphaned_running` looks up the task's last run, which
7767        // touches `run::home()`; the first call anywhere in this binary wins,
7768        // so this is a no-op if another test already pinned one, and either
7769        // way the run id below is never written under it.
7770        crate::run::pin_test_home();
7771        let dir = tempfile::tempdir().unwrap();
7772        let queue = Queue::at(dir.path().to_path_buf());
7773        let mut t = task();
7774        t.id = "20260101-000000-crsh".to_owned();
7775        t.status = TaskStatus::Running;
7776        t.attempts = 1;
7777        // No `run.json` behind this id: standing in for a run this test does
7778        // not need to make readable, since the point is the lock, not the
7779        // recovery table `reclaim` already has its own tests for.
7780        t.runs.push("20260904-000000-4043".to_owned());
7781        queue.put(&mut t).unwrap();
7782        let dead_pid = injected_dead_pid();
7783
7784        // The crashed daemon's own claim, naming a pid nothing on the
7785        // machine holds anymore.
7786        std::fs::write(
7787            dir.path().join(format!("{}.lock", t.id)),
7788            dead_pid.to_string(),
7789        )
7790        .unwrap();
7791
7792        // Before the lock is swept the task looks claimed, and
7793        // `reclaim_orphaned_running` must leave it alone - this is exactly
7794        // the bug: a `running` task stranded behind a dead daemon's lock,
7795        // invisible to the claim-as-proof check because the lock outlived
7796        // the process that wrote it.
7797        assert!(reclaim_orphaned_running(&queue, 2, &Default::default()).is_empty());
7798        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Running);
7799
7800        let swept = sweep_stale_claims_with(&queue, Duration::from_secs(6 * 60 * 60), |pid| {
7801            pid != dead_pid
7802        });
7803        assert_eq!(swept, vec![t.id.clone()]);
7804
7805        let reclaimed = reclaim_orphaned_running(&queue, 2, &Default::default());
7806        assert_eq!(reclaimed, vec![t.id.clone()]);
7807        let after = queue.get(&t.id).unwrap();
7808        assert_eq!(
7809            after.status,
7810            TaskStatus::Held,
7811            "no run.json to recover from, so a human is asked"
7812        );
7813        assert_eq!(
7814            after.runs,
7815            vec!["20260904-000000-4043".to_owned()],
7816            "the crashed run's id is kept as evidence, not discarded"
7817        );
7818    }
7819
7820    #[test]
7821    fn a_lock_is_kept_when_the_process_query_is_unavailable() {
7822        let dir = tempfile::tempdir().unwrap();
7823        let queue = Queue::at(dir.path().to_path_buf());
7824        let mut t = task();
7825        t.id = "20260101-000000-unknown".to_owned();
7826        queue.put(&mut t).unwrap();
7827        let dead_pid = injected_dead_pid();
7828        std::fs::write(
7829            dir.path().join(format!("{}.lock", t.id)),
7830            dead_pid.to_string(),
7831        )
7832        .unwrap();
7833
7834        let swept = sweep_stale_claims_with(&queue, Duration::ZERO, |_| true);
7835        assert!(swept.is_empty(), "an unknown pid must keep its lock");
7836        assert!(queue.claim(&t.id).is_err(), "the lock remains protective");
7837    }
7838
7839    fn run_state_in(status: RunStatus, language: &str) -> RunState {
7840        let mut s = run_state(status);
7841        s.config.graph.language = language.to_owned();
7842        s
7843    }
7844
7845    fn unstarted_verdict(status: RunStatus) -> Verdict {
7846        Verdict {
7847            status,
7848            left_pr: false,
7849            quota_hit: false,
7850            parked: false,
7851            no_viable_candidates: false,
7852        }
7853    }
7854
7855    #[test]
7856    fn an_already_in_base_run_finishes_the_task_without_spending_an_attempt() {
7857        let mut t = task();
7858        t.attempts = 1;
7859        settle_in(
7860            &mut t,
7861            unstarted_verdict(RunStatus::AlreadyInBase),
7862            "already in main",
7863            1,
7864            phrases("en"),
7865        );
7866        assert_eq!(t.status, TaskStatus::Done);
7867        assert_eq!(t.attempts, 0);
7868    }
7869
7870    #[test]
7871    fn settle_renders_the_non_terminal_reason_in_the_configured_language() {
7872        let reason = |language: &str| {
7873            let mut t = task();
7874            settle_in(
7875                &mut t,
7876                unstarted_verdict(RunStatus::Judging),
7877                "boom",
7878                1,
7879                phrases(language),
7880            );
7881            t.last_error.or(t.hold_reason).unwrap_or_default()
7882        };
7883        assert!(
7884            reason("en").starts_with("the graph stopped at `"),
7885            "{}",
7886            reason("en")
7887        );
7888        assert!(reason("ja").starts_with("グラフが終端状態に達しないまま"));
7889        assert!(reason("日本語").contains("boom"));
7890        assert_eq!(reason("fr"), reason("en"));
7891    }
7892
7893    #[test]
7894    fn describe_follows_the_run_language_and_keeps_the_run_id() {
7895        let en = describe(&run_state_in(RunStatus::Stalled, "en"));
7896        assert!(en.starts_with("the judging panel lost its quorum"), "{en}");
7897        let ja = describe(&run_state_in(RunStatus::Stalled, "ja"));
7898        assert!(ja.starts_with("審査パネルが定足数を失いました"), "{ja}");
7899        assert!(ja.contains("[run "), "{ja}");
7900        let ended = describe(&run_state_in(RunStatus::Failed, "jp"));
7901        assert!(ended.starts_with("run 終了: "), "{ended}");
7902        let mut de = run_state_in(RunStatus::Failed, "de");
7903        let mut en = run_state_in(RunStatus::Failed, "en");
7904        de.id = "same".to_owned();
7905        en.id = "same".to_owned();
7906        assert_eq!(describe(&de), describe(&en));
7907    }
7908
7909    #[test]
7910    fn handover_refusals_follow_the_language_and_keep_the_detail_apart() {
7911        use crate::handover::Refused;
7912        let cases = [
7913            Refused::Foreign {
7914                branch: "b".into(),
7915                path: "/w/x".into(),
7916                why: "made by hand".into(),
7917            },
7918            Refused::Unsafe {
7919                branch: "b".into(),
7920                path: "/w/x".into(),
7921                why: "its worktree has uncommitted changes (a.rs)".into(),
7922            },
7923            Refused::ReleaseFailed {
7924                branch: "b".into(),
7925                path: "/w/x".into(),
7926                run: "ab12".into(),
7927            },
7928        ];
7929        for r in &cases {
7930            let en = (phrases("en").handover_refused)(r);
7931            assert_eq!(en, r.to_string());
7932            let ja = (phrases("ja").handover_refused)(r);
7933            assert!(
7934                !ja.contains("is checked out") && !ja.contains("try again"),
7935                "{ja}"
7936            );
7937            assert!(ja.contains("`b`") && ja.contains("/w/x"), "{ja}");
7938            if let Refused::Foreign { why, .. } | Refused::Unsafe { why, .. } = r {
7939                assert!(ja.contains(&format!("(詳細: {why})")), "{ja}");
7940            }
7941        }
7942        assert!(phrases("en").handover_hint.contains("release the task"));
7943        assert!(phrases("ja").handover_hint.contains("解放"));
7944    }
7945
7946    #[test]
7947    fn describe_translates_the_unanswered_reviewer_stop_only_in_ja() {
7948        let build = |lang: &str| {
7949            let mut s = run_state_in(RunStatus::Blocked, lang);
7950            s.id = "same".to_owned();
7951            s.config.graph.review_rounds = 3;
7952            let mut r = review_round(3);
7953            r.expected = 3;
7954            r.answered = 1;
7955            s.reviews.push(r);
7956            s.event(
7957                "review",
7958                "2 reviewer seat(s) never answered after 3 rounds; refusing to call it clean",
7959            );
7960            s
7961        };
7962        let en = describe(&build("en"));
7963        assert!(en.contains("(review: 2 reviewer seat(s) never answered after 3 rounds; refusing to call it clean)"), "{en}");
7964        let ja = describe(&build("ja"));
7965        assert!(ja.contains("2 席のレビュアーが 3 ラウンド"), "{ja}");
7966        assert!(!ja.contains("never answered"), "{ja}");
7967        assert!(ja.contains("[run "), "{ja}");
7968
7969        // An incomplete panel whose verification failed is a different stop:
7970        // the real event must survive in ja too.
7971        let mut failed = build("ja");
7972        failed.reviews[0].e2e.push(crate::run::CommandOutcome {
7973            command: "cargo test".to_owned(),
7974            code: Some(1),
7975            output_tail: String::new(),
7976            duration_ms: 0,
7977            resource_blocked: false,
7978        });
7979        failed.event("review", "stopped; e2e failed: cargo test");
7980        let ja = describe(&failed);
7981        assert!(ja.contains("stopped; e2e failed: cargo test"), "{ja}");
7982        assert!(!ja.contains("席のレビュアー"), "{ja}");
7983    }
7984
7985    #[test]
7986    fn refusals_and_recovery_prose_follow_the_language() {
7987        let t = held_task_with("r1");
7988        let q = action_question(
7989            "r1",
7990            ask::ChoiceAction::Resume {
7991                run: "r1".to_owned(),
7992            },
7993        );
7994        let refuse = |p: &Phrases| match decide_action(&t, &q, p, |_: &str| bail!("gone")) {
7995            ActionDecision::Refuse(s) => s,
7996            other => panic!("{other:?}"),
7997        };
7998        assert!(refuse(phrases("en")).contains("could not be read: gone"));
7999        assert!(refuse(phrases("ja")).contains("読み込めませんでした: gone"));
8000        assert_eq!(refuse(phrases("fr")), refuse(phrases("en")));
8001
8002        let mut held = task();
8003        reclaim(&mut held, None, 2, "ja");
8004        assert!(held.hold_reason.unwrap().contains("保留にしました"));
8005        let mut held = task();
8006        reclaim(&mut held, None, 2, "xx");
8007        assert!(held.hold_reason.unwrap().contains("held for a human"));
8008    }
8009
8010    fn run_state(status: RunStatus) -> RunState {
8011        let mut state = RunState::new(
8012            PathBuf::from("/repo"),
8013            "main".to_owned(),
8014            "abc1234def".to_owned(),
8015            "add retries".to_owned(),
8016            Config::default(),
8017        );
8018        state.status = status;
8019        state
8020    }
8021
8022    fn candidate(label: char, summary: &str, empty: bool, failed: Option<&str>) -> Candidate {
8023        Candidate {
8024            index: 0,
8025            label,
8026            agent: "claude".to_owned(),
8027            branch: format!("magi/x/{label}"),
8028            worktree: PathBuf::from("/repo"),
8029            summary: summary.to_owned(),
8030            stat: String::new(),
8031            files: 0,
8032            commits: usize::from(!empty),
8033            empty,
8034            failed: failed.map(str::to_owned),
8035            verified_noop: None,
8036            duration_ms: 0,
8037            folded: false,
8038        }
8039    }
8040
8041    #[test]
8042    fn diagnostic_names_the_failing_gate_checks_and_their_output() {
8043        let mut state = run_state(RunStatus::Blocked);
8044        state.gate = vec![
8045            CommandOutcome {
8046                command: "cargo make check".to_owned(),
8047                code: Some(0),
8048                output_tail: "ok".to_owned(),
8049                duration_ms: 0,
8050                resource_blocked: false,
8051            },
8052            CommandOutcome {
8053                command: "cargo test".to_owned(),
8054                code: Some(101),
8055                output_tail: "thread 'x' panicked: assertion failed".to_owned(),
8056                duration_ms: 0,
8057                resource_blocked: false,
8058            },
8059        ];
8060        let d = diagnostic(&state).expect("a failing gate must produce a diagnostic");
8061        assert!(d.contains("cargo test"), "{d}");
8062        assert!(
8063            !d.contains("cargo make check"),
8064            "a passing check is not a diagnostic: {d}"
8065        );
8066        assert!(d.contains("assertion failed"), "{d}");
8067    }
8068
8069    #[test]
8070    fn diagnostic_names_the_checks_the_fixer_gave_up_in_front_of() {
8071        let mut state = run_state(RunStatus::Blocked);
8072        state.event(
8073            "land",
8074            "stopped: the fixer produced no commit while 2 check(s) were failing \
8075             (build, lint); stopping instead of looping on an unchanged tree",
8076        );
8077        let d = diagnostic(&state).expect("a stalled land loop must produce a diagnostic");
8078        assert!(d.contains("build"), "{d}");
8079        assert!(d.contains("lint"), "{d}");
8080        assert!(d.contains("fixer produced no commit"), "{d}");
8081    }
8082
8083    #[test]
8084    fn describe_never_leaves_a_verified_noop_reading_as_a_bare_status_code() {
8085        // `describe`'s output becomes `Task::last_error` verbatim, and the
8086        // phone renders that in the same alarm-styled box an ordinary
8087        // failure gets. A bare `verified_noop` there would read exactly like
8088        // the failure this status exists to be told apart from.
8089        let state = run_state(RunStatus::VerifiedNoop);
8090        let d = describe(&state);
8091        assert!(
8092            d.contains("agent-verified no-op"),
8093            "expected the display label, not the wire spelling: {d}"
8094        );
8095        assert!(!d.contains("verified_noop"), "{d}");
8096    }
8097
8098    #[test]
8099    fn diagnostic_carries_a_candidates_own_final_word_when_none_was_viable() {
8100        // The whole point of the feature: a run held as "no candidate produced
8101        // a change" can mean the implementer actually finished the task and
8102        // only left a clean local tree behind - see AGENTS.md on this exact
8103        // failure mode. The diagnostic has to carry what the agent actually
8104        // said, not just the fact that nothing was there to judge.
8105        let mut state = run_state(RunStatus::Failed);
8106        state.candidates = vec![candidate(
8107            'A',
8108            "opened pull request #42, merged it, tagged v1.2.3 and published the release",
8109            true,
8110            None,
8111        )];
8112        let d = diagnostic(&state).expect("an empty candidate with a summary must be surfaced");
8113        assert!(d.contains("candidate A"), "{d}");
8114        assert!(d.contains("tagged v1.2.3"), "{d}");
8115    }
8116
8117    #[test]
8118    fn diagnostic_falls_back_to_a_candidates_failure_reason_when_it_has_no_summary() {
8119        let mut state = run_state(RunStatus::Failed);
8120        state.candidates = vec![candidate('A', "", true, Some("agent timed out"))];
8121        let d = diagnostic(&state).expect("a candidate's own failure reason must be surfaced");
8122        assert!(d.contains("candidate A"), "{d}");
8123        assert!(d.contains("agent timed out"), "{d}");
8124    }
8125
8126    #[test]
8127    fn diagnostic_is_none_when_nothing_recognisable_explains_the_hold() {
8128        // A viable candidate existed, the gate never ran, and nothing land
8129        // said matches - `Task::last_error` is left to explain this one alone.
8130        let mut state = run_state(RunStatus::Failed);
8131        state.candidates = vec![candidate('A', "did the work", false, None)];
8132        assert!(diagnostic(&state).is_none());
8133    }
8134
8135    #[test]
8136    fn diagnostic_is_bounded_however_much_a_run_printed() {
8137        let mut state = run_state(RunStatus::Blocked);
8138        state.gate = vec![
8139            CommandOutcome {
8140                command: "cargo test".to_owned(),
8141                code: Some(101),
8142                output_tail: "x".repeat(50_000),
8143                duration_ms: 0,
8144                resource_blocked: false,
8145            },
8146            CommandOutcome {
8147                command: "cargo clippy".to_owned(),
8148                code: Some(1),
8149                output_tail: "y".repeat(50_000),
8150                duration_ms: 0,
8151                resource_blocked: false,
8152            },
8153        ];
8154        state.candidates = vec![
8155            candidate('A', &"z".repeat(50_000), true, None),
8156            candidate('B', &"w".repeat(50_000), true, None),
8157        ];
8158        let d = diagnostic(&state).expect("plenty here to diagnose");
8159        assert!(
8160            d.len() <= DIAGNOSTIC_MAX,
8161            "diagnostic grew to {} bytes, unbounded",
8162            d.len()
8163        );
8164    }
8165
8166    #[test]
8167    fn selected_delivery_guards_survive_recovery_failure_accounting() {
8168        for status in [RunStatus::Ready, RunStatus::Blocked, RunStatus::Failed] {
8169            let mut t = task();
8170            let mut state = run_state(status);
8171            t.pr_recovery = Some(crate::pr_recovery::Selection {
8172                url: "url".into(),
8173                branch: format!("magi/task-{}", t.id),
8174                head: "a".repeat(40),
8175                source_head: None,
8176                source_run: None,
8177                source_proof: None,
8178                disposition: Some(crate::pr_recovery::Disposition::Land),
8179                instruction: t.instruction.clone(),
8180                approved_at: Timestamp::now(),
8181            });
8182            state.pr_recovery = t.pr_recovery.clone();
8183            t.start(state.id.clone());
8184            settle_and_diagnose(
8185                &mut t,
8186                Verdict {
8187                    status,
8188                    left_pr: false,
8189                    quota_hit: false,
8190                    parked: false,
8191                    no_viable_candidates: false,
8192                },
8193                "delivery stopped",
8194                5,
8195                &state,
8196            );
8197            assert_eq!(t.status, TaskStatus::Held);
8198            let reason = t.hold_reason.as_deref().unwrap();
8199            if status == RunStatus::Ready {
8200                assert!(reason.contains("Selected PR delivery is incomplete"));
8201                assert!(t.recovery_budget.is_none());
8202            } else {
8203                assert!(reason.contains("Selected recovery stopped"));
8204                assert_eq!(t.recovery_budget.as_ref().unwrap().failures, 1);
8205            }
8206            assert!(t.pr_recovery.is_some());
8207        }
8208    }
8209
8210    #[test]
8211    fn settle_and_diagnose_attaches_a_diagnostic_only_once_the_task_is_held() {
8212        let mut state = run_state(RunStatus::Blocked);
8213        state.gate = vec![CommandOutcome {
8214            command: "cargo test".to_owned(),
8215            code: Some(101),
8216            output_tail: "assertion failed".to_owned(),
8217            duration_ms: 0,
8218            resource_blocked: false,
8219        }];
8220        let verdict = Verdict {
8221            status: RunStatus::Blocked,
8222            left_pr: false,
8223            quota_hit: false,
8224            parked: false,
8225            no_viable_candidates: false,
8226        };
8227
8228        // Attempt one of two still has a retry coming: no diagnostic yet, the
8229        // task is going to run again and this run's evidence would go stale.
8230        let mut t = task();
8231        t.start("run-1".to_owned());
8232        settle_and_diagnose(&mut t, verdict, "gate failed", 2, &state);
8233        assert_eq!(t.status, TaskStatus::Failed);
8234        assert!(t.diagnostic.is_none());
8235
8236        // Attempt two exhausts the budget: now it is held, and the
8237        // diagnostic is what `magi task show` has to say more than one line.
8238        t.start("run-2".to_owned());
8239        settle_and_diagnose(&mut t, verdict, "gate failed", 2, &state);
8240        assert_eq!(t.status, TaskStatus::Held);
8241        let d = t.diagnostic.expect("a held task must carry its diagnostic");
8242        assert!(d.contains("cargo test"), "{d}");
8243    }
8244
8245    #[test]
8246    fn a_held_task_names_the_open_question_it_is_waiting_on() {
8247        // Task 20260928-191407-05fd's own bug: a verified no-op that filed a
8248        // `magi ask` question settled Held with `hold_reason: null`, so the
8249        // notification said nothing actionable. `note_open_question` should
8250        // append the question id to whatever `handed_off` already wrote.
8251        crate::run::pin_test_home();
8252        let home = crate::run::home();
8253        let state = run_state(RunStatus::VerifiedNoop);
8254        let mut q = ask::Question::new(
8255            state.id.clone(),
8256            "implement".to_owned(),
8257            "impl-A".to_owned(),
8258            "is this really a no-op?".to_owned(),
8259            String::new(),
8260            Vec::new(),
8261        );
8262        Questions::at(home.join("questions")).put(&mut q).unwrap();
8263
8264        let verdict = Verdict {
8265            status: RunStatus::VerifiedNoop,
8266            left_pr: false,
8267            quota_hit: false,
8268            parked: false,
8269            no_viable_candidates: false,
8270        };
8271        let mut t = task();
8272        t.start(state.id.clone());
8273        settle_and_diagnose(&mut t, verdict, "run ended agent-verified no-op", 5, &state);
8274
8275        assert_eq!(t.status, TaskStatus::Held);
8276        let reason = t.hold_reason.expect("a held task must record why");
8277        assert!(
8278            reason.starts_with("run ended agent-verified no-op"),
8279            "the original settle reason must survive unchanged: {reason}"
8280        );
8281        assert!(
8282            reason.contains(q.short()),
8283            "the open question's id must be named so the notice is actionable: {reason}"
8284        );
8285    }
8286
8287    #[test]
8288    fn a_held_task_with_no_open_question_keeps_its_plain_reason() {
8289        crate::run::pin_test_home();
8290        let state = run_state(RunStatus::VerifiedNoop);
8291
8292        let verdict = Verdict {
8293            status: RunStatus::VerifiedNoop,
8294            left_pr: false,
8295            quota_hit: false,
8296            parked: false,
8297            no_viable_candidates: false,
8298        };
8299        let mut t = task();
8300        t.start(state.id.clone());
8301        settle_and_diagnose(&mut t, verdict, "run ended agent-verified no-op", 5, &state);
8302
8303        assert_eq!(t.status, TaskStatus::Held);
8304        assert_eq!(
8305            t.hold_reason.as_deref(),
8306            Some("run ended agent-verified no-op"),
8307            "nothing to append when the question was already answered or never asked"
8308        );
8309    }
8310
8311    #[test]
8312    fn supersede_prior_runs_rewrites_an_earlier_blocked_attempt_once_a_later_one_lands() {
8313        crate::run::pin_test_home();
8314        let mut first = run_state(RunStatus::Blocked);
8315        first.id = "20260101-000000-sup1".to_owned();
8316        first.save().unwrap();
8317        let mut second = run_state(RunStatus::Merged);
8318        second.id = "20260101-000000-sup2".to_owned();
8319        second.save().unwrap();
8320
8321        let mut t = task();
8322        t.runs = vec![first.id.clone(), second.id.clone()];
8323        t.status = TaskStatus::Done;
8324
8325        supersede_prior_runs(&t, &crate::run::home());
8326
8327        assert_eq!(
8328            RunState::load(&first.id).unwrap().status,
8329            RunStatus::Superseded,
8330            "the first attempt's Blocked no longer needs anyone's attention"
8331        );
8332        assert_eq!(
8333            RunState::load(&second.id).unwrap().status,
8334            RunStatus::Merged,
8335            "the run that actually succeeded is left exactly as it was"
8336        );
8337    }
8338
8339    #[test]
8340    fn supersede_prior_runs_leaves_a_manually_resumed_attempt_alone() {
8341        // `is_working_on` alone only proves a *daemon* claim; a `magi run
8342        // --resume` invoked by hand outside the daemon never touches
8343        // `daemon.json` at all, so it would look identical to a genuinely
8344        // idle run without also consulting `RunState::liveness`, which reads
8345        // this run's own recorded `driver_pid` instead.
8346        crate::run::pin_test_home();
8347        let mut first = run_state(RunStatus::Blocked);
8348        first.id = "20260101-000000-sup9".to_owned();
8349        // This test process's own pid: guaranteed alive without needing a
8350        // real second process or daemon.
8351        first.driver_pid = Some(std::process::id());
8352        first.driver_started_at = Some(
8353            crate::proc::process_started_at(std::process::id())
8354                .expect("this test process's own start time must be queryable"),
8355        );
8356        first.save().unwrap();
8357        let mut second = run_state(RunStatus::Merged);
8358        second.id = "20260101-000000-supa".to_owned();
8359        second.save().unwrap();
8360
8361        let mut t = task();
8362        t.runs = vec![first.id.clone(), second.id.clone()];
8363        t.status = TaskStatus::Done;
8364
8365        supersede_prior_runs(&t, &crate::run::home());
8366
8367        assert_eq!(
8368            RunState::load(&first.id).unwrap().status,
8369            RunStatus::Blocked,
8370            "a live driver_pid means something is still actually working this run, \
8371             even though no daemon claims it - rewriting under it would just be \
8372             undone the next time that process saves"
8373        );
8374    }
8375
8376    #[test]
8377    fn resweep_catches_up_a_run_left_live_once_its_manual_process_is_no_longer_driving_it() {
8378        // The first resweep must skip a still-live attempt exactly like
8379        // `supersede_prior_runs` itself does; a later resweep, once that
8380        // process is no longer actually driving it, is what closes the gap
8381        // `supersede_prior_runs` alone leaves - a task only ever reaches
8382        // `Done` once, so nothing else would ever look at this run again.
8383        let dir = tempfile::tempdir().unwrap();
8384        let home = dir.path().to_path_buf();
8385        let queue = Queue::at(dir.path().join("queue"));
8386
8387        let mut first = run_state(RunStatus::Blocked);
8388        first.id = "20260101-000000-supd".to_owned();
8389        first.driver_pid = Some(std::process::id());
8390        first.driver_started_at = Some(
8391            crate::proc::process_started_at(std::process::id())
8392                .expect("this test process's own start time must be queryable"),
8393        );
8394        first.save_under(&home).unwrap();
8395        let mut second = run_state(RunStatus::Merged);
8396        second.id = "20260101-000000-supe".to_owned();
8397        second.save_under(&home).unwrap();
8398
8399        let mut t = task();
8400        t.runs = vec![first.id.clone(), second.id.clone()];
8401        t.status = TaskStatus::Done;
8402        queue.put(&mut t).unwrap();
8403
8404        resweep_superseded_attempts(&queue, &home);
8405        assert_eq!(
8406            RunState::load_under(&first.id, &home).unwrap().status,
8407            RunStatus::Blocked,
8408            "still live on the first pass, so still untouched"
8409        );
8410
8411        // Stand in for the manual process no longer being the one driving
8412        // this run: same pid (this test process really is still alive), but
8413        // an identity marker that no longer matches it - see
8414        // `RunState::liveness`'s own doc for why a mismatched
8415        // `driver_started_at` reads as `Dead`, not merely `Unknown`.
8416        let mut stale = RunState::load_under(&first.id, &home).unwrap();
8417        stale.driver_started_at = Some("1".to_owned());
8418        stale.save_under(&home).unwrap();
8419
8420        resweep_superseded_attempts(&queue, &home);
8421        assert_eq!(
8422            RunState::load_under(&first.id, &home).unwrap().status,
8423            RunStatus::Superseded,
8424            "the second pass catches up what the first one correctly skipped"
8425        );
8426    }
8427
8428    #[test]
8429    fn supersede_prior_runs_leaves_concurrent_blocked_attempts_alone_while_the_task_is_not_done() {
8430        crate::run::pin_test_home();
8431        let mut first = run_state(RunStatus::Blocked);
8432        first.id = "20260101-000000-sup3".to_owned();
8433        first.save().unwrap();
8434        let mut second = run_state(RunStatus::Blocked);
8435        second.id = "20260101-000000-sup4".to_owned();
8436        second.save().unwrap();
8437
8438        let mut t = task();
8439        t.runs = vec![first.id.clone(), second.id.clone()];
8440        // Still retrying: nothing about this task's story is settled yet, so
8441        // neither Blocked run may be relabelled, even though a later attempt
8442        // already exists.
8443        t.status = TaskStatus::Failed;
8444
8445        supersede_prior_runs(&t, &crate::run::home());
8446
8447        assert_eq!(
8448            RunState::load(&first.id).unwrap().status,
8449            RunStatus::Blocked
8450        );
8451        assert_eq!(
8452            RunState::load(&second.id).unwrap().status,
8453            RunStatus::Blocked
8454        );
8455    }
8456
8457    #[test]
8458    fn supersede_prior_runs_does_nothing_when_the_task_was_closed_by_hand() {
8459        // `magi task done` (or the API's equivalent) can close a task with no
8460        // run of its own ever having succeeded - there is nothing here that
8461        // counts as "the attempt that finished it", so nothing is rewritten.
8462        crate::run::pin_test_home();
8463        let mut first = run_state(RunStatus::Blocked);
8464        first.id = "20260101-000000-sup5".to_owned();
8465        first.save().unwrap();
8466
8467        let mut t = task();
8468        t.runs = vec![first.id.clone()];
8469        t.status = TaskStatus::Done;
8470
8471        supersede_prior_runs(&t, &crate::run::home());
8472
8473        assert_eq!(
8474            RunState::load(&first.id).unwrap().status,
8475            RunStatus::Blocked,
8476            "a single-attempt task has no earlier run to supersede"
8477        );
8478    }
8479
8480    #[test]
8481    fn supersede_prior_runs_does_nothing_when_the_last_recorded_attempt_never_landed() {
8482        // `magi task done` (or the web/conductor equivalents) can close a
8483        // task in any status, including one whose *last* recorded attempt is
8484        // itself still `Blocked`/`Failed` - a manual merge magi's own loop
8485        // never watched, say. `runs.last()` being `Done`-adjacent is not
8486        // proof it actually succeeded, so nothing earlier may be relabelled
8487        // on its say-so alone.
8488        crate::run::pin_test_home();
8489        let mut first = run_state(RunStatus::Blocked);
8490        first.id = "20260101-000000-supb".to_owned();
8491        first.save().unwrap();
8492        let mut second = run_state(RunStatus::Failed);
8493        second.id = "20260101-000000-supc".to_owned();
8494        second.save().unwrap();
8495
8496        let mut t = task();
8497        t.runs = vec![first.id.clone(), second.id.clone()];
8498        t.status = TaskStatus::Done;
8499
8500        supersede_prior_runs(&t, &crate::run::home());
8501
8502        assert_eq!(
8503            RunState::load(&first.id).unwrap().status,
8504            RunStatus::Blocked,
8505            "the task's last attempt never landed, so there is nothing here \
8506             actually superseding it"
8507        );
8508    }
8509
8510    #[test]
8511    fn supersede_prior_runs_leaves_a_failed_or_verified_noop_attempt_as_is() {
8512        // Only `Blocked`/`Stalled` mean "sitting there waiting for a human
8513        // to look" - `Failed` and `VerifiedNoop` are already their own
8514        // terminal answers and must not be relabelled into a third one.
8515        crate::run::pin_test_home();
8516        let mut failed = run_state(RunStatus::Failed);
8517        failed.id = "20260101-000000-sup6".to_owned();
8518        failed.save().unwrap();
8519        let mut noop = run_state(RunStatus::VerifiedNoop);
8520        noop.id = "20260101-000000-sup7".to_owned();
8521        noop.save().unwrap();
8522        let mut winner = run_state(RunStatus::Ready);
8523        winner.id = "20260101-000000-sup8".to_owned();
8524        winner.save().unwrap();
8525
8526        let mut t = task();
8527        t.runs = vec![failed.id.clone(), noop.id.clone(), winner.id.clone()];
8528        t.status = TaskStatus::Done;
8529
8530        supersede_prior_runs(&t, &crate::run::home());
8531
8532        assert_eq!(
8533            RunState::load(&failed.id).unwrap().status,
8534            RunStatus::Failed
8535        );
8536        assert_eq!(
8537            RunState::load(&noop.id).unwrap().status,
8538            RunStatus::VerifiedNoop
8539        );
8540    }
8541
8542    fn approval_question(run: &str) -> ask::Question {
8543        ask::Question::new(
8544            run.to_owned(),
8545            land::APPROVAL_NODE.to_owned(),
8546            "land".to_owned(),
8547            "merge?".to_owned(),
8548            String::new(),
8549            vec!["merge".to_owned(), "hold".to_owned()],
8550        )
8551    }
8552
8553    #[test]
8554    fn land_resume_state_leaves_a_fresh_open_question_waiting() {
8555        crate::run::pin_test_home();
8556        let mut state = run_state(RunStatus::Landing);
8557        state.id = "20260101-000000-fre1".to_owned();
8558        state.parked = true;
8559        state.save().unwrap();
8560        ask::Questions::open()
8561            .put(&mut approval_question(&state.id))
8562            .unwrap();
8563
8564        let mut t = task();
8565        t.runs.push(state.id.clone());
8566        assert_eq!(
8567            land_resume_state(&t),
8568            LandResume::StillWaiting,
8569            "nobody has answered and the timeout has not passed"
8570        );
8571    }
8572
8573    #[test]
8574    fn land_resume_state_waits_on_a_withheld_text_question_then_resumes() {
8575        crate::run::pin_test_home();
8576        let mut state = run_state(RunStatus::Gating);
8577        state.id = "20260101-000000-gt01".to_owned();
8578        state.parked = true;
8579        state.config.graph.answer_timeout = 60;
8580        state.github_text = Some(crate::run::GithubTextAsk {
8581            rewrite: None,
8582            fingerprint: "f".into(),
8583            title: true,
8584            body: false,
8585            categories: vec!["title-language".into()],
8586            question: None,
8587            asks: 1,
8588            resolved: false,
8589            chosen_title: None,
8590            choice: None,
8591        });
8592        state.save().unwrap();
8593        let store = ask::Questions::open();
8594        let mut q = ask::Question::new(
8595            state.id.clone(),
8596            crate::github_text::ASK_NODE.to_owned(),
8597            crate::github_text::ASK_SEAT.to_owned(),
8598            "s".into(),
8599            String::new(),
8600            vec!["use fallback".into()],
8601        );
8602        store.put(&mut q).unwrap();
8603        let mut t = task();
8604        t.runs.push(state.id.clone());
8605        assert_eq!(land_resume_state(&t), LandResume::StillWaiting);
8606        q.asked_at = Timestamp::now() - jiff::SignedDuration::from_secs(120);
8607        store.put(&mut q).unwrap();
8608        assert_eq!(land_resume_state(&t), LandResume::Ready);
8609        assert!(!store.get(&q.id).unwrap().status.open());
8610        // Once decided, a parked run is an ordinary candidate again.
8611        state.github_text.as_mut().unwrap().resolved = true;
8612        state.save().unwrap();
8613        assert_eq!(land_resume_state(&t), LandResume::NotLanding);
8614    }
8615
8616    #[test]
8617    fn land_resume_state_abandons_a_question_that_outlived_answer_timeout() {
8618        // `ask::ask_and_wait`'s own deadline used to retire a question
8619        // nobody answered; land's approval bypasses that wait (see
8620        // `land::approval_gate`), so this is now the only place
8621        // `graph.answer_timeout` is enforced for a land approval at all.
8622        crate::run::pin_test_home();
8623        let mut state = run_state(RunStatus::Landing);
8624        state.id = "20260101-000000-exp1".to_owned();
8625        state.parked = true;
8626        state.config.graph.answer_timeout = 60;
8627        state.save().unwrap();
8628
8629        let store = ask::Questions::open();
8630        let mut q = approval_question(&state.id);
8631        q.asked_at = Timestamp::now() - jiff::SignedDuration::from_secs(120);
8632        store.put(&mut q).unwrap();
8633
8634        let mut t = task();
8635        t.runs.push(state.id.clone());
8636        assert_eq!(
8637            land_resume_state(&t),
8638            LandResume::Ready,
8639            "an expired question must not be waited on forever"
8640        );
8641
8642        let after = store.get(&q.id).unwrap();
8643        assert!(
8644            !after.status.open(),
8645            "the question is abandoned, not silently ignored"
8646        );
8647        assert!(
8648            after.resolution().is_none(),
8649            "an abandoned question is not read as a decision"
8650        );
8651    }
8652
8653    #[test]
8654    fn reclaim_settles_a_running_task_against_its_last_run() {
8655        let mut t = task();
8656        t.start("20260904-000000-4043".to_owned());
8657        reclaim(&mut t, Some(run_state(RunStatus::Ready)), 2, "en");
8658        assert_eq!(
8659            t.status,
8660            TaskStatus::Done,
8661            "a run that actually finished must not stay `running` forever"
8662        );
8663    }
8664
8665    #[test]
8666    fn reclaim_maps_a_parked_run_to_parked_without_touching_last_error() {
8667        let mut t = task();
8668        t.start("20260904-000000-4043".to_owned());
8669        t.last_error = Some("earlier trouble".to_owned());
8670        let mut state = run_state(RunStatus::Judging);
8671        state.parked = true;
8672        reclaim(&mut t, Some(state), 1, "en");
8673        assert_eq!(t.status, TaskStatus::Parked);
8674        assert_eq!(t.attempts, 0, "the park is refunded, even at max_attempts");
8675        assert_eq!(t.last_error.as_deref(), Some("earlier trouble"));
8676        assert!(t.park_reason.is_some());
8677        assert_eq!(t.runs, ["20260904-000000-4043"], "the same run is kept");
8678        assert!(!t.fresh_start);
8679        assert!(t.status.runnable());
8680    }
8681
8682    #[test]
8683    fn settle_parks_a_task_without_a_failure_and_a_new_start_clears_the_reason() {
8684        let mut t = task();
8685        t.start("20260904-000000-4043".to_owned());
8686        t.last_error = Some("earlier trouble".to_owned());
8687        settle(
8688            &mut t,
8689            Verdict {
8690                status: RunStatus::Judging,
8691                left_pr: false,
8692                quota_hit: false,
8693                parked: true,
8694                no_viable_candidates: false,
8695            },
8696            "parked after `judging`",
8697            1,
8698        );
8699        assert_eq!(t.status, TaskStatus::Parked);
8700        assert_eq!(t.attempts, 0);
8701        assert_eq!(t.last_error.as_deref(), Some("earlier trouble"));
8702        assert_eq!(t.park_reason.as_deref(), Some("parked after `judging`"));
8703        assert_eq!(t.schema, crate::queue::SCHEMA);
8704        assert!(
8705            crate::notices::task_held(&t).is_none(),
8706            "a park is not news"
8707        );
8708        t.start("20260904-000000-4043".to_owned());
8709        assert_eq!(t.park_reason, None);
8710    }
8711
8712    #[test]
8713    fn reclaim_reuses_the_same_retry_policy_as_a_live_settle() {
8714        // A blocked run with attempts left goes back to `Failed`, exactly as
8715        // it would from `attempt` itself - `reclaim` must not invent a second
8716        // policy for a task a daemon merely stopped without reporting.
8717        let mut t = task();
8718        t.start("20260904-000000-4043".to_owned());
8719        reclaim(&mut t, Some(run_state(RunStatus::Blocked)), 2, "en");
8720        assert_eq!(t.status, TaskStatus::Failed);
8721        assert!(t.status.runnable());
8722    }
8723
8724    #[test]
8725    fn a_settled_parked_pass_records_its_cause_and_the_refund() {
8726        let mut t = task();
8727        t.start("20260904-000000-4043".to_owned());
8728        assert_eq!(t.attempts, 1);
8729        let mut run = run_state(RunStatus::Implementing);
8730        run.id = "20260904-000000-4043".to_owned();
8731        run.begin_pass();
8732        let cause = crate::run::ParkCause::Upgrade {
8733            from: "0.121.0".to_owned(),
8734            to: Some("0.121.1".to_owned()),
8735        };
8736        run.end_pass_parked(cause.clone());
8737        run.parked = true;
8738        let verdict = Verdict {
8739            status: run.status,
8740            left_pr: false,
8741            quota_hit: false,
8742            parked: true,
8743            no_viable_candidates: false,
8744        };
8745        // A crash after the checkpoint but before queue settlement must
8746        // recover exactly the same scheduling evidence and refund.
8747        let mut orphan = t.clone();
8748        reclaim(&mut orphan, Some(run.clone()), 2, "en");
8749        assert_eq!(orphan.status, TaskStatus::Parked);
8750        assert_eq!(orphan.attempts, 0);
8751        assert_eq!(
8752            orphan.upgrade_resume_key(),
8753            Some((run.passes[0].started_at, orphan.id.as_str()))
8754        );
8755        settle_and_diagnose(&mut t, verdict, "parked", 2, &run);
8756        assert_eq!(t.attempts, 0);
8757        assert_eq!(t.passes.len(), 1);
8758        assert_eq!(t.passes[0].cost, crate::queue::PassCost::Refunded);
8759        assert_eq!(t.passes[0].parked, Some(cause));
8760        assert_eq!(t.passes[0].pass_index, 0);
8761        assert_eq!(t.passes[0].started_at, Some(run.passes[0].started_at));
8762        assert!(t.upgrade_resume_key().is_some());
8763
8764        // A resumed pass that then fails spends its attempt, and the earlier
8765        // pass's entry is untouched.
8766        t.start("20260904-000000-4043".to_owned());
8767        run.begin_pass();
8768        run.status = RunStatus::Failed;
8769        let verdict = Verdict {
8770            status: RunStatus::Failed,
8771            left_pr: false,
8772            quota_hit: false,
8773            parked: false,
8774            no_viable_candidates: false,
8775        };
8776        settle_and_diagnose(&mut t, verdict, "failed", 2, &run);
8777        assert_eq!(t.passes.len(), 2);
8778        assert_eq!(t.passes[0].cost, crate::queue::PassCost::Refunded);
8779        assert_eq!(t.passes[1].cost, crate::queue::PassCost::Spent);
8780        assert_eq!(t.passes[1].pass_index, 1);
8781        assert_eq!(t.attempts, 1);
8782    }
8783
8784    #[test]
8785    fn reclaim_holds_a_running_task_whose_run_cannot_be_found() {
8786        let mut t = task();
8787        t.start("20260904-000000-4043".to_owned());
8788        reclaim(&mut t, None, 2, "en");
8789        assert_eq!(t.status, TaskStatus::Held);
8790        assert!(
8791            t.last_error
8792                .as_deref()
8793                .is_some_and(|e| e.contains("running")),
8794            "the operator needs to know why this task was held"
8795        );
8796    }
8797
8798    #[test]
8799    fn orphaned_running_tasks_are_reclaimed_but_live_ones_are_left_alone() {
8800        let dir = tempfile::tempdir().unwrap();
8801        let queue = Queue::at(dir.path().to_path_buf());
8802
8803        // No run recorded, so this never has to touch `RunState::load`.
8804        let mut orphaned = task();
8805        orphaned.id = "20260904-000000-orph".to_owned();
8806        orphaned.status = TaskStatus::Running;
8807        orphaned.attempts = 1;
8808        queue.put(&mut orphaned).unwrap();
8809
8810        let mut alive = task();
8811        alive.id = "20260904-000000-live".to_owned();
8812        alive.status = TaskStatus::Running;
8813        alive.attempts = 1;
8814        queue.put(&mut alive).unwrap();
8815        let _held_by_a_live_daemon = queue.claim(&alive.id).unwrap();
8816
8817        let mut queued = task();
8818        queued.id = "20260904-000000-wait".to_owned();
8819        queue.put(&mut queued).unwrap();
8820
8821        let reclaimed = reclaim_orphaned_running(&queue, 2, &Default::default());
8822        assert_eq!(reclaimed, vec![orphaned.id.clone()]);
8823
8824        assert_eq!(
8825            queue.get(&orphaned.id).unwrap().status,
8826            TaskStatus::Held,
8827            "nothing was driving it and there was no run to recover"
8828        );
8829        assert_eq!(
8830            queue.get(&alive.id).unwrap().status,
8831            TaskStatus::Running,
8832            "a live claim must protect the task it belongs to"
8833        );
8834        assert_eq!(queue.get(&queued.id).unwrap().status, TaskStatus::Queued);
8835    }
8836
8837    /// Read a run.json back from an explicit `home`, the same way
8838    /// `reclaim_abandoned_runs` itself does - never through the
8839    /// process-global `RunState::load`, which this test's own `home` (an
8840    /// isolated tempdir, never pinned into the shared `OnceLock`) does not
8841    /// use at all.
8842    fn read_run_under(home: &Path, id: &str) -> RunState {
8843        let body = std::fs::read_to_string(home.join("runs").join(id).join("run.json")).unwrap();
8844        serde_json::from_str(&body).unwrap()
8845    }
8846
8847    #[test]
8848    fn reclaim_abandoned_runs_fails_a_run_whose_active_seats_are_all_provably_dead() {
8849        let dir = tempfile::tempdir().unwrap();
8850        let home = dir.path().to_path_buf();
8851        let now = Timestamp::now();
8852        let overrun_seat = || crate::run::ActiveSeat {
8853            node: "implement".to_owned(),
8854            started_at: now - jiff::SignedDuration::new(21_000, 0),
8855            timeout_secs: 3_600,
8856            attempt: 0,
8857            task: None,
8858            command: None,
8859            index: None,
8860            total: None,
8861        };
8862
8863        let mut dead = run_state(RunStatus::Implementing);
8864        dead.id = "20260101-000000-dead".to_owned();
8865        dead.active.insert("impl-A".to_owned(), overrun_seat());
8866        // A `driver_pid` the injected query below confirms gone outright —
8867        // `liveness` reads this as `Dead`, not merely "no daemon claims it".
8868        dead.driver_pid = Some(4242);
8869        dead.save_under(&home).unwrap();
8870
8871        // Same shape, but a live daemon's heartbeat names it: must be left
8872        // exactly alone, however far past its own timeout the seat sits.
8873        let mut alive = run_state(RunStatus::Implementing);
8874        alive.id = "20260101-000000-aliv".to_owned();
8875        alive.active.insert("impl-A".to_owned(), overrun_seat());
8876        alive.save_under(&home).unwrap();
8877        let mut status = Status::new();
8878        status.current = vec![Current {
8879            task: "20260101-000000-task".to_owned(),
8880            run: alive.id.clone(),
8881        }];
8882        write_status_to(&home.join("daemon.json"), &status).unwrap();
8883
8884        // The abandoned seat left an open question behind: nobody is left to
8885        // read an answer once the run is failed, and this must not wait for
8886        // some later daemon startup's own sweep to notice that.
8887        let questions = Questions::at(home.join("questions"));
8888        let mut q = ask::Question::new(
8889            dead.id.clone(),
8890            "implement".to_owned(),
8891            "impl-A".to_owned(),
8892            "Which storage backend?".to_owned(),
8893            String::new(),
8894            vec!["SQLite".to_owned(), "Redis".to_owned()],
8895        );
8896        questions.put(&mut q).unwrap();
8897
8898        let abandoned = reclaim_abandoned_runs_with(
8899            &home,
8900            now,
8901            |pid| if pid == 4242 { Some(false) } else { None },
8902            |_| panic!("a query answering Dead outright needs no identity corroboration"),
8903        );
8904        assert_eq!(abandoned, vec![dead.id.clone()]);
8905
8906        let reloaded = read_run_under(&home, &dead.id);
8907        assert_eq!(reloaded.status, RunStatus::Failed);
8908        assert!(reloaded.active.is_empty());
8909        assert!(
8910            !questions.get(&q.id).unwrap().status.open(),
8911            "the failed run's own open question must be settled in the same pass"
8912        );
8913
8914        let still_alive = read_run_under(&home, &alive.id);
8915        assert_eq!(
8916            still_alive.status,
8917            RunStatus::Implementing,
8918            "a live daemon's claim protects it"
8919        );
8920        assert!(!still_alive.active.is_empty());
8921    }
8922
8923    /// The exact shape a review round flagged as broken: `magi serve` running
8924    /// in this same `home` scans *every* run on disk, including a manual
8925    /// `magi review` / `magi run` this daemon never started and that
8926    /// therefore claims no heartbeat of its own. Before this scan asked
8927    /// `liveness` rather than just `is_working_on`, a manual run whose active
8928    /// seat merely ran a little past its own timeout — the CLI finishing up,
8929    /// its result still being collected — got wiped and failed by a daemon
8930    /// that had nothing to do with it, out from under a process that was
8931    /// still very much running.
8932    #[test]
8933    fn reclaim_abandoned_runs_leaves_a_live_manual_run_alone_even_though_no_daemon_claims_it() {
8934        let dir = tempfile::tempdir().unwrap();
8935        let home = dir.path().to_path_buf();
8936        let now = Timestamp::now();
8937
8938        let mut manual = run_state(RunStatus::Reviewing);
8939        manual.id = "20260101-000000-manl".to_owned();
8940        manual.active.insert(
8941            "review-1".to_owned(),
8942            crate::run::ActiveSeat {
8943                node: "review".to_owned(),
8944                started_at: now - jiff::SignedDuration::new(21_000, 0),
8945                timeout_secs: 3_600,
8946                attempt: 0,
8947                task: None,
8948                command: None,
8949                index: None,
8950                total: None,
8951            },
8952        );
8953        // Not claimed by any daemon (no `daemon.json` at all in this `home`),
8954        // but a real, still-running process: `liveness` must corroborate this
8955        // as `Live`, not read the missing daemon claim as death.
8956        manual.driver_pid = Some(4242);
8957        manual.driver_started_at = Some("1790000000".to_owned());
8958        manual.save_under(&home).unwrap();
8959
8960        let abandoned = reclaim_abandoned_runs_with(
8961            &home,
8962            now,
8963            |pid| if pid == 4242 { Some(true) } else { None },
8964            |pid| {
8965                if pid == 4242 {
8966                    Some("1790000000".to_owned())
8967                } else {
8968                    None
8969                }
8970            },
8971        );
8972        assert!(
8973            abandoned.is_empty(),
8974            "a manual run a real process is still driving must never be reclaimed: {abandoned:?}"
8975        );
8976
8977        let reloaded = read_run_under(&home, &manual.id);
8978        assert_eq!(reloaded.status, RunStatus::Reviewing);
8979        assert!(!reloaded.active.is_empty());
8980    }
8981
8982    #[test]
8983    fn an_already_claimed_task_is_skipped_rather_than_failed() {
8984        let dir = tempfile::tempdir().unwrap();
8985        let queue = Queue::at(dir.path().to_path_buf());
8986        let mut only = task();
8987        queue.put(&mut only).unwrap();
8988
8989        let _elsewhere = queue.claim(&only.id).unwrap();
8990        let candidates = runnable(&queue);
8991        assert_eq!(candidates.len(), 1, "the task is still runnable");
8992        assert!(
8993            queue.claim(&candidates[0].id).is_err(),
8994            "the loop cannot take a claim somebody else holds"
8995        );
8996
8997        let after = queue.get(&only.id).unwrap();
8998        assert_eq!(after.status, TaskStatus::Queued);
8999        assert_eq!(
9000            after.attempts, 0,
9001            "losing the race is not an attempt at the task"
9002        );
9003        assert_eq!(after.last_error, None);
9004    }
9005
9006    #[test]
9007    fn the_status_file_round_trips_and_its_heartbeat_advances() {
9008        let dir = tempfile::tempdir().unwrap();
9009        let path = dir.path().join("daemon.json");
9010
9011        let mut status = Status::new();
9012        status.idle = false;
9013        status.completed = 7;
9014        status.current = vec![Current {
9015            task: "20260902-000000-t111".to_owned(),
9016            run: "20260902-000001-r111".to_owned(),
9017        }];
9018        write_status_to(&path, &status).unwrap();
9019        let first: Status = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
9020        assert_eq!(first.schema, SCHEMA);
9021        assert_eq!(first.pid, std::process::id());
9022        assert!(!first.idle);
9023        assert_eq!(first.completed, 7);
9024        assert_eq!(first.current, status.current);
9025        assert!(
9026            !path.with_extension("json.tmp").exists(),
9027            "the temp file is renamed, not left behind"
9028        );
9029
9030        std::thread::sleep(Duration::from_millis(5));
9031        status.updated_at = Timestamp::now();
9032        status.polls = 3;
9033        write_status_to(&path, &status).unwrap();
9034        let second: Status =
9035            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
9036        assert!(
9037            second.updated_at > first.updated_at,
9038            "a reader can only detect staleness if the heartbeat moves"
9039        );
9040        assert_eq!(
9041            second.started_at, first.started_at,
9042            "the start time is not a heartbeat"
9043        );
9044        assert_eq!(second.polls, 3);
9045    }
9046
9047    #[test]
9048    fn reading_counts_as_running_only_while_its_heartbeat_is_fresh() {
9049        let dir = tempfile::tempdir().unwrap();
9050
9051        assert!(read_status(dir.path()).is_none(), "no file, no daemon");
9052
9053        let mut status = Status::new();
9054        status.updated_at = Timestamp::now() - jiff::SignedDuration::from_secs(60);
9055        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
9056        let stale = read_status(dir.path()).unwrap();
9057        assert!(
9058            !stale.running(Timestamp::now()),
9059            "a minute without a heartbeat is a dead daemon, not a busy one"
9060        );
9061        assert!(stale.age_secs(Timestamp::now()).is_some_and(|s| s >= 55));
9062
9063        status.updated_at = Timestamp::now();
9064        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
9065        let fresh = read_status(dir.path()).unwrap();
9066        assert!(fresh.running(Timestamp::now()));
9067    }
9068
9069    #[test]
9070    fn only_a_live_daemon_on_this_very_run_counts_as_working_on_it() {
9071        let dir = tempfile::tempdir().unwrap();
9072        let now = Timestamp::now();
9073        let mine = "20260903-080619-01c2";
9074
9075        assert!(
9076            !is_working_on(dir.path(), mine, now),
9077            "no status file means nobody is working on anything"
9078        );
9079
9080        let mut status = Status::new();
9081        status.current = vec![Current {
9082            task: "20260903-080340-0167".to_owned(),
9083            run: mine.to_owned(),
9084        }];
9085        status.updated_at = now;
9086        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
9087        assert!(is_working_on(dir.path(), mine, now));
9088        assert!(
9089            !is_working_on(dir.path(), "20260903-105039-3cbf", now),
9090            "a daemon busy with one run is not working on another"
9091        );
9092
9093        // A killed daemon stops writing heartbeats but leaves the file behind
9094        // naming the run it died in. That run must not be undeletable forever.
9095        status.updated_at = now - jiff::SignedDuration::from_secs(600);
9096        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
9097        assert!(
9098            !is_working_on(dir.path(), mine, now),
9099            "a stale heartbeat is a dead daemon, so its run is a leftover"
9100        );
9101    }
9102
9103    #[test]
9104    fn is_working_on_short_matches_by_the_worktree_bays_own_name() {
9105        let dir = tempfile::tempdir().unwrap();
9106        let now = Timestamp::now();
9107
9108        assert!(
9109            !is_working_on_short(dir.path(), "01c2", now),
9110            "no status file means nobody is working on anything"
9111        );
9112
9113        let mut status = Status::new();
9114        status.current = vec![Current {
9115            task: "20260903-080340-0167".to_owned(),
9116            run: "20260903-080619-01c2".to_owned(),
9117        }];
9118        status.updated_at = now;
9119        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
9120        assert!(
9121            is_working_on_short(dir.path(), "01c2", now),
9122            "the run's short id is the last block of its full id"
9123        );
9124        assert!(
9125            !is_working_on_short(dir.path(), "3cbf", now),
9126            "a daemon busy with one worktree bay is not working on another"
9127        );
9128    }
9129
9130    #[test]
9131    fn a_newer_status_file_still_yields_a_reading() {
9132        let dir = tempfile::tempdir().unwrap();
9133        // A field this build has never heard of must not turn the reading into
9134        // nothing at all; that is the whole reason the reader is permissive.
9135        std::fs::write(
9136            dir.path().join("daemon.json"),
9137            serde_json::json!({
9138                "schema": 2,
9139                "updated_at": Timestamp::now().to_string(),
9140                "idle": true,
9141                "surprise": { "nested": [1, 2, 3] },
9142            })
9143            .to_string(),
9144        )
9145        .unwrap();
9146
9147        let reading = read_status(dir.path()).expect("a forward-compatible read");
9148        assert!(reading.running(Timestamp::now()));
9149        assert!(reading.idle);
9150        assert!(reading.current.is_empty());
9151    }
9152
9153    #[test]
9154    fn an_older_daemons_single_object_current_still_reads_as_a_one_item_list() {
9155        // A daemon started before `current` became a list keeps writing this
9156        // shape on every heartbeat until it is restarted. A rolling upgrade
9157        // - a newer `magi web` or `magi doctor` reading an older `magi
9158        // serve`'s heartbeat - must still see the run it is on, not "no
9159        // daemon" from a type mismatch failing the whole struct.
9160        let dir = tempfile::tempdir().unwrap();
9161        std::fs::write(
9162            dir.path().join("daemon.json"),
9163            serde_json::json!({
9164                "schema": 1,
9165                "pid": 4242,
9166                "updated_at": Timestamp::now().to_string(),
9167                "idle": false,
9168                "current": {"task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb"},
9169                "completed": 3,
9170                "polls": 9,
9171            })
9172            .to_string(),
9173        )
9174        .unwrap();
9175
9176        let reading = read_status(dir.path()).expect("an older shape must still parse");
9177        assert!(reading.running(Timestamp::now()));
9178        assert_eq!(
9179            reading.current,
9180            vec![Current {
9181                task: "20260902-140501-aaaa".to_owned(),
9182                run: "20260902-140502-bbbb".to_owned(),
9183            }]
9184        );
9185    }
9186
9187    #[test]
9188    fn an_absent_or_null_current_reads_as_idle_not_a_parse_failure() {
9189        let dir = tempfile::tempdir().unwrap();
9190        std::fs::write(
9191            dir.path().join("daemon.json"),
9192            serde_json::json!({
9193                "schema": 1,
9194                "updated_at": Timestamp::now().to_string(),
9195                "idle": true,
9196                "current": null,
9197            })
9198            .to_string(),
9199        )
9200        .unwrap();
9201        let with_null = read_status(dir.path()).expect("null must still parse");
9202        assert!(with_null.current.is_empty());
9203
9204        std::fs::write(
9205            dir.path().join("daemon.json"),
9206            serde_json::json!({
9207                "schema": 1,
9208                "updated_at": Timestamp::now().to_string(),
9209                "idle": true,
9210            })
9211            .to_string(),
9212        )
9213        .unwrap();
9214        let absent = read_status(dir.path()).expect("a missing field must still parse");
9215        assert!(absent.current.is_empty());
9216    }
9217
9218    #[test]
9219    fn a_task_without_a_repository_runs_in_the_daemons_default() {
9220        let fallback = Path::new("/default");
9221        let mut blank = task();
9222        blank.repo = PathBuf::new();
9223        assert_eq!(repo_for(&blank, fallback), PathBuf::from("/default"));
9224        let mut dot = task();
9225        dot.repo = PathBuf::from(".");
9226        assert_eq!(repo_for(&dot, fallback), PathBuf::from("/default"));
9227        assert_eq!(
9228            repo_for(&task(), fallback),
9229            PathBuf::from("/repo"),
9230            "a task that names a repository keeps it"
9231        );
9232    }
9233
9234    #[test]
9235    fn task_implementers_edit_inherits_daemon_config_and_merge() {
9236        let dir = tempfile::tempdir().unwrap();
9237        let path = dir.path().join("explicit.toml");
9238        std::fs::write(&path, "[graph]\nimplementers = 3\njudges = 1\n").unwrap();
9239        let opts = Opts {
9240            config: Some(path),
9241            merge: Some("none".into()),
9242            ..Default::default()
9243        };
9244        let mut t = task();
9245        t.set_implementers(
9246            2,
9247            &toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
9248                .unwrap(),
9249        )
9250        .unwrap();
9251        let cfg = prepare_for(dir.path(), &opts, &t).unwrap();
9252        assert_eq!(cfg.graph.implementers, 2);
9253        assert_eq!(cfg.graph.judges, 1);
9254        assert_eq!(cfg.merge.mode, MergeMode::None);
9255        t.set_implementers(1, &cfg).unwrap();
9256        let mut cfg = prepare_for(dir.path(), &opts, &t).unwrap();
9257        apply_task_implementers(&mut cfg, &t);
9258        assert_eq!(cfg.graph.implementers, 1);
9259        assert_eq!(cfg.graph.judges, 1);
9260        assert_eq!(cfg.merge.mode, MergeMode::None);
9261    }
9262
9263    #[test]
9264    fn task_implementers_direct_overrides_stay_isolated_after_edits() {
9265        let dir = tempfile::tempdir().unwrap();
9266        let repo = dir.path().join("repo");
9267        std::fs::create_dir(&repo).unwrap();
9268        std::fs::write(
9269            repo.join("magi.toml"),
9270            "[graph]\njudges = 2\n[merge]\nmode = 'none'\n",
9271        )
9272        .unwrap();
9273        let daemon_config = dir.path().join("daemon.toml");
9274        std::fs::write(
9275            &daemon_config,
9276            "[graph]\njudges = 1\n[merge]\nmode = 'pr'\n",
9277        )
9278        .unwrap();
9279        let opts = Opts {
9280            config: Some(daemon_config),
9281            merge: Some("pr".into()),
9282            ..Default::default()
9283        };
9284        let q = Queue::at(dir.path().join("queue"));
9285        let validation: Config =
9286            toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
9287                .unwrap();
9288        // No provenance field on legacy direct tasks: both shapes stay isolated.
9289        for record in ["{}", r#"{"candidates":2}"#] {
9290            let overrides = serde_json::from_str(record).unwrap();
9291            let filing = crate::direct::Filing {
9292                instruction: "work".into(),
9293                title: "direct".into(),
9294                repo: repo.clone(),
9295                source: crate::queue::Source::Human,
9296                solo: false,
9297                overrides,
9298                review_of: None,
9299                sources: Default::default(),
9300            };
9301            let filed =
9302                crate::direct::file(&q, dir.path(), Timestamp::now(), 1, filing, true).unwrap();
9303            let crate::direct::Filed::Standalone {
9304                mut task,
9305                claim: _claim,
9306            } = filed
9307            else {
9308                panic!("local filing")
9309            };
9310            let before = prepare_for(&repo, &opts, &task).unwrap();
9311            assert_eq!(before.graph.judges, 2, "daemon --config must not leak");
9312            assert_eq!(
9313                before.merge.mode,
9314                MergeMode::None,
9315                "daemon --merge must not leak"
9316            );
9317            for n in [2, 1, 2] {
9318                task.set_implementers(n, &validation).unwrap();
9319                q.put(&mut task).unwrap();
9320                task = q.get(&task.id).unwrap();
9321                assert!(!task.overrides.as_ref().unwrap().inherit_daemon_options);
9322                let mut cfg = prepare_for(&repo, &opts, &task).unwrap();
9323                apply_task_implementers(&mut cfg, &task);
9324                assert_eq!(cfg.graph.implementers, usize::from(n));
9325                assert_eq!(cfg.graph.judges, 2);
9326                assert_eq!(cfg.merge.mode, MergeMode::None);
9327            }
9328        }
9329    }
9330
9331    #[test]
9332    fn task_implementers_mismatch_keeps_explicit_reviews_review_only() {
9333        let cfg: Config =
9334            toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
9335                .unwrap();
9336        let mut t = task();
9337        t.review_of = Some("review-target".into());
9338        t.hold_manual(Some("retry".into()));
9339        let mut previous = RunState::new(
9340            PathBuf::from("/repo"),
9341            "main".into(),
9342            "abc1234".into(),
9343            "review".into(),
9344            cfg.clone(),
9345        );
9346        previous.config.graph.implementers = 2;
9347        for count in [1, 3] {
9348            t.set_implementers(count, &cfg).unwrap();
9349            t.release();
9350            assert_eq!(t.review_of.as_deref(), Some("review-target"));
9351            for proposed in [
9352                Starter::Resume(previous.id.clone()),
9353                Starter::Review("recovery-target".into()),
9354            ] {
9355                assert_eq!(
9356                    starter_with_implementers(&t, Some(&previous), proposed),
9357                    Starter::Review("review-target".into())
9358                );
9359            }
9360            let mut configured = cfg.clone();
9361            apply_task_implementers(&mut configured, &t);
9362            assert_eq!(configured.graph.implementers, usize::from(count));
9363            assert_eq!(previous.config.graph.implementers, 2);
9364        }
9365    }
9366
9367    #[test]
9368    fn task_implementers_dispatch_and_resume_agree() {
9369        let mut t = task();
9370        let cfg: crate::config::Config =
9371            toml::from_str("[[agents]]\nid = 'mock'\nkind = 'command'\ncommand = ['true']\n")
9372                .unwrap();
9373        let mut run = RunState::new(
9374            PathBuf::from("/repo"),
9375            "main".into(),
9376            "abc1234".into(),
9377            "work".into(),
9378            cfg.clone(),
9379        );
9380        run.config.graph.implementers = 2;
9381        for n in [2, 1, 3, 2] {
9382            t.set_implementers(n, &cfg).unwrap();
9383            let mut dispatched = cfg.clone();
9384            apply_task_implementers(&mut dispatched, &t);
9385            assert_eq!(dispatched.graph.implementers, usize::from(n));
9386            assert_eq!(implementers_match(&t, &run), n == 2);
9387            for proposed in [
9388                Starter::Resume(run.id.clone()),
9389                Starter::Review("old-branch".into()),
9390            ] {
9391                let expected = if n == 2 {
9392                    proposed.clone()
9393                } else {
9394                    Starter::Start
9395                };
9396                assert_eq!(
9397                    starter_with_implementers(&t, Some(&run), proposed),
9398                    expected
9399                );
9400            }
9401            assert_eq!(
9402                run.config.graph.implementers, 2,
9403                "history must not be rewritten"
9404            );
9405        }
9406        t.solo = false;
9407        t.overrides = None;
9408        assert!(implementers_match(&t, &run));
9409    }
9410
9411    #[test]
9412    fn a_solo_task_runs_with_one_candidate_and_a_plain_task_keeps_the_configs() {
9413        // Three seats said out loud. What `solo` promises is one candidate
9414        // *whatever the config asks for*, so the contrast has to be a number
9415        // this test owns - it used to be `Config::default()`'s, which became
9416        // 1 when one implementation became the default and left the two
9417        // halves of this test asserting the same thing.
9418        let mut solo_cfg = Config::default();
9419        solo_cfg.graph.implementers = 3;
9420        let mut solo_task = task();
9421        solo_task.solo = true;
9422        apply_task_implementers(&mut solo_cfg, &solo_task);
9423        assert_eq!(solo_cfg.graph.implementers, 1);
9424
9425        let mut plain_cfg = Config::default();
9426        plain_cfg.graph.implementers = 3;
9427        let plain_task = task();
9428        assert!(!plain_task.solo);
9429        apply_task_implementers(&mut plain_cfg, &plain_task);
9430        assert_eq!(
9431            plain_cfg.graph.implementers, 3,
9432            "a task that did not ask to run alone keeps the config's candidates"
9433        );
9434    }
9435
9436    fn loss(seat: &str, at: &str, reset: Option<&str>) -> QuotaLoss {
9437        QuotaLoss {
9438            seat: seat.into(),
9439            node: "judge".into(),
9440            at: at.parse().unwrap(),
9441            reset: reset.map(str::to_string),
9442        }
9443    }
9444
9445    #[test]
9446    fn a_resumed_run_with_only_old_quota_losses_arms_no_cooldown() {
9447        let old: Vec<QuotaLoss> = (1..=4)
9448            .map(|i| {
9449                loss(
9450                    &format!("judge-{i}"),
9451                    "2026-09-23T05:23:00Z",
9452                    Some("2:40pm (Asia/Tokyo)"),
9453                )
9454            })
9455            .collect();
9456        let fresh = losses_this_attempt(&old, &old);
9457        assert!(fresh.is_empty());
9458        assert_eq!(cooldown_until(&fresh, Timestamp::now()), None);
9459        // And it is not a `quota_hit` either: that is `!fresh.is_empty()`.
9460    }
9461
9462    #[test]
9463    fn a_new_quota_loss_during_the_attempt_still_arms_the_cooldown() {
9464        let old = vec![loss("judge-1", "2026-09-23T05:23:00Z", None)];
9465        let now = Timestamp::now();
9466        let mut after = old.clone();
9467        after.push(loss("judge-2", &now.to_string(), None));
9468        let fresh = losses_this_attempt(&old, &after);
9469        assert_eq!(fresh, vec![after[1].clone()]);
9470        let until = cooldown_until(&fresh, now).expect("a fresh loss arms the cooldown");
9471        assert_eq!(
9472            until,
9473            now + jiff::SignedDuration::from_secs(QUOTA_WAIT_FALLBACK.as_secs() as i64)
9474        );
9475    }
9476
9477    #[test]
9478    fn a_recovered_seat_dropping_out_of_the_history_does_not_hide_a_new_loss() {
9479        // `recover_stall` removes judge-1's loss and the retry then hits quota
9480        // again: the vector is the same length, so an index diff sees nothing.
9481        let before = vec![
9482            loss("judge-1", "2026-09-23T05:23:00Z", None),
9483            loss("judge-2", "2026-09-23T05:24:00Z", None),
9484        ];
9485        let after = vec![
9486            loss("judge-2", "2026-09-23T05:24:00Z", None),
9487            loss("judge-1", "2026-09-24T01:00:00Z", None),
9488        ];
9489        assert_eq!(losses_this_attempt(&before, &after), vec![after[1].clone()]);
9490    }
9491
9492    #[test]
9493    fn merge_overrides_are_parsed_or_refused() {
9494        assert_eq!(merge_mode("none").unwrap(), MergeMode::None);
9495        assert_eq!(merge_mode("local").unwrap(), MergeMode::Local);
9496        assert_eq!(merge_mode("pr").unwrap(), MergeMode::Pr);
9497        assert!(merge_mode("squash").is_err());
9498    }
9499
9500    #[test]
9501    fn quota_wait_uses_a_future_reset_time_capped_and_falls_back_otherwise() {
9502        let now = Timestamp::now();
9503        let fallback = Duration::from_secs(300);
9504        let cap = Duration::from_secs(1800);
9505
9506        // No reset hint at all: the fallback.
9507        assert_eq!(quota_wait(None, now, fallback, cap), fallback);
9508
9509        // A reset ten minutes out, well inside the cap: waited for exactly.
9510        let soon = now + jiff::SignedDuration::from_secs(600);
9511        assert_eq!(
9512            quota_wait(Some(soon), now, fallback, cap),
9513            Duration::from_secs(600)
9514        );
9515
9516        // A reset already in the past is not trusted: the fallback, not a
9517        // zero or negative wait that would spin the loop right back around.
9518        let past = now - jiff::SignedDuration::from_secs(60);
9519        assert_eq!(quota_wait(Some(past), now, fallback, cap), fallback);
9520
9521        // A reset further out than the cap is trusted for direction but not
9522        // for magnitude: a parsing slip must not sleep the loop for a day.
9523        let far = now + jiff::SignedDuration::from_secs(3 * 3600);
9524        assert_eq!(quota_wait(Some(far), now, fallback, cap), cap);
9525    }
9526
9527    #[test]
9528    fn parse_reset_hint_reads_the_claude_cli_shape_and_rolls_a_past_clock_to_tomorrow() {
9529        let now = "2026-09-07T02:50:00Z".parse::<Timestamp>().unwrap();
9530
9531        let at = parse_reset_hint("4:50am (UTC)", now, now).expect("a recognised shape parses");
9532        assert_eq!(at.to_string(), "2026-09-07T04:50:00Z");
9533
9534        // Same clock reading, but it has already gone by today: read as
9535        // tomorrow's, since the CLI would not still be reporting a limit past
9536        // its own stated reset.
9537        let already_past =
9538            parse_reset_hint("1:00am (UTC)", now, now).expect("a recognised shape parses");
9539        assert_eq!(already_past.to_string(), "2026-09-08T01:00:00Z");
9540
9541        assert!(
9542            parse_reset_hint("session limit reached", now, now).is_none(),
9543            "free text with no recognised shape is not guessed at"
9544        );
9545        assert!(
9546            parse_reset_hint("4:50am (Nowhere/Fake)", now, now).is_none(),
9547            "an unresolvable zone name is not guessed at either"
9548        );
9549    }
9550
9551    #[test]
9552    fn parse_reset_hint_reads_the_codex_cli_shape_with_no_year_rollover_needed() {
9553        let now = "2026-09-07T02:50:00Z".parse::<Timestamp>().unwrap();
9554
9555        let at = parse_reset_hint(
9556            "You've hit your usage limit. Visit \
9557             https://chatgpt.com/codex/settings/usage to purchase more \
9558             credits or try again at Sep 19th, 2026 5:10 PM.",
9559            now,
9560            now,
9561        )
9562        .expect("the codex reset wording is a recognised shape");
9563        assert_eq!(at.to_string(), "2026-09-19T17:10:00Z");
9564
9565        // The month is explicit, so a date already earlier in the same
9566        // sentence-implied year than `now` is trusted as written rather than
9567        // rolled forward a year the way the bracketed shape rolls a
9568        // same-day clock reading to tomorrow.
9569        let earlier = parse_reset_hint("try again at Jan 2nd, 2026 1:00 AM.", now, now)
9570            .expect("an explicit year needs no rollover");
9571        assert_eq!(earlier.to_string(), "2026-01-02T01:00:00Z");
9572
9573        assert!(
9574            parse_reset_hint("try again at Sep 19th, 26 5:10 PM.", now, now).is_none(),
9575            "a two-digit year is not the documented shape and is not guessed at"
9576        );
9577        assert!(
9578            parse_reset_hint("try again at Sept 19th, 2026 5:10 PM.", now, now).is_none(),
9579            "a four-letter month name is not the documented three-letter abbreviation"
9580        );
9581        assert!(
9582            parse_reset_hint("try again at Sep 19th, 2026 5:10 PM (UTC).", now, now).is_none(),
9583            "an explicit zone on the dated shape is a format nobody has \
9584             documented, and is refused rather than guessed at as UTC"
9585        );
9586    }
9587
9588    #[test]
9589    fn parse_reset_hint_reads_agys_relative_shape_from_when_the_loss_was_recorded() {
9590        let now = "2026-09-24T12:00:00Z".parse::<Timestamp>().unwrap();
9591        let recorded = "2026-09-24T08:00:00Z".parse::<Timestamp>().unwrap();
9592
9593        let at = parse_reset_hint("in 1h2m49s", now, recorded).expect("agy's shape parses");
9594        assert_eq!(at.as_second() - recorded.as_second(), 3769);
9595
9596        let partial = parse_reset_hint("in 45m", now, recorded).expect("units are optional");
9597        assert_eq!(partial.as_second() - recorded.as_second(), 45 * 60);
9598
9599        for bad in ["in ", "in 45", "in 3x", "in m", "in 1h junk", "1h2m"] {
9600            assert!(
9601                parse_reset_hint(bad, now, recorded).is_none(),
9602                "{bad:?} must not be guessed at"
9603            );
9604        }
9605    }
9606
9607    /// A loop whose queue lives in a temp tree and whose poll interval is far
9608    /// longer than the test's patience, so anything that waits out a poll
9609    /// instead of noticing the stop fails rather than merely being slow.
9610    fn idle_loop(dir: &Path) -> (Opts, Queue, PathBuf, PathBuf, PathBuf) {
9611        let config = dir.join("magi.toml");
9612        std::fs::write(
9613            &config,
9614            "[disk]\nmin_free_bytes = 0\nauto_fold = false\ncache_limit_bytes = 0\n",
9615        )
9616        .unwrap();
9617        let opts = Opts {
9618            poll: Duration::from_secs(30),
9619            config: Some(config),
9620            // The explicit fixture config keeps startup cleanup from reading
9621            // machine configuration. This fictional repository likewise
9622            // keeps any best-effort git cleanup away from this checkout.
9623            repo: dir.join("repo"),
9624            ..Opts::default()
9625        };
9626        // The status file goes in a directory that does not exist yet, so its
9627        // creation is itself evidence the loop published one. `worktrees`
9628        // must be just as fictional: the janitor reclaims worktrees under it
9629        // for real, and a test that let it fall through to
9630        // `crate::run::default_worktree_root()` would have it reclaim
9631        // worktrees out of the operator's real `~/wt/<repo>`, not a fixture -
9632        // which is exactly what happened before this function took the
9633        // parameter at all.
9634        let home = dir.join("home");
9635        // These control-only fixtures do not launch native workers. Give the
9636        // independently tested maintenance services a fresh cadence receipt.
9637        let stamps = home.join("service-ticks");
9638        std::fs::create_dir_all(&stamps).unwrap();
9639        for service in ["waiter", "deputies", "release-watch"] {
9640            std::fs::write(
9641                stamps.join(service),
9642                Timestamp::now().as_second().to_string(),
9643            )
9644            .unwrap();
9645        }
9646        for operation in [
9647            "conductor",
9648            "maintenance",
9649            "housekeeping",
9650            "disk-pressure",
9651            "cache",
9652            "fetch",
9653        ] {
9654            std::fs::write(
9655                stamps.join(format!("daemon-{operation}")),
9656                serde_json::to_vec(&Timestamp::now()).unwrap(),
9657            )
9658            .unwrap();
9659        }
9660        let worktrees = dir.join("wt");
9661        (
9662            opts,
9663            Queue::at(dir.join("queue")),
9664            home.join("daemon.json"),
9665            home,
9666            worktrees,
9667        )
9668    }
9669
9670    #[test]
9671    fn a_stop_is_idempotent_and_once_set_stays_set() {
9672        let stop = Stop::new();
9673        assert!(!stop.stopped());
9674
9675        stop.stop();
9676        assert!(stop.stopped());
9677        stop.stop();
9678        assert!(stop.stopped(), "a second stop is not a toggle");
9679
9680        let shared = stop.clone();
9681        assert!(
9682            shared.stopped(),
9683            "a clone is the same stop; that is how the loop and its caller share one"
9684        );
9685    }
9686
9687    #[test]
9688    fn only_a_stop_with_a_run_in_flight_reads_as_finishing() {
9689        let stop = Stop::new();
9690        stop.busy.store(1, Ordering::SeqCst);
9691        assert!(
9692            !stop.finishing(),
9693            "a busy loop nobody has asked to stop is just running"
9694        );
9695
9696        stop.stop();
9697        assert!(
9698            stop.finishing(),
9699            "a stop asked for mid-run has not landed until the run is settled"
9700        );
9701
9702        stop.busy.store(0, Ordering::SeqCst);
9703        assert!(
9704            !stop.finishing(),
9705            "once the run is settled the stop has landed and there is nothing to finish"
9706        );
9707    }
9708
9709    #[test]
9710    fn finishing_stays_true_until_the_last_of_several_runs_exits() {
9711        let stop = Stop::new();
9712        stop.busy.store(2, Ordering::SeqCst);
9713        stop.stop();
9714        assert!(stop.finishing(), "two runs still in flight");
9715
9716        stop.busy.store(1, Ordering::SeqCst);
9717        assert!(
9718            stop.finishing(),
9719            "one run finished, but a sibling is still working"
9720        );
9721
9722        stop.busy.store(0, Ordering::SeqCst);
9723        assert!(
9724            !stop.finishing(),
9725            "the last run out is what actually lands the stop"
9726        );
9727    }
9728
9729    #[tokio::test]
9730    async fn a_loop_already_asked_to_stop_returns_without_waiting_out_a_poll() {
9731        let dir = tempfile::tempdir().unwrap();
9732        let (opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
9733        let stop = Stop::new();
9734        stop.stop();
9735
9736        let began = std::time::Instant::now();
9737        tokio::time::timeout(
9738            Duration::from_secs(2),
9739            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
9740        )
9741        .await
9742        .expect("a stopped loop must return, not sit out its poll interval")
9743        .expect("the loop's own setup and teardown must not fail");
9744        assert!(
9745            began.elapsed() < opts.poll,
9746            "returned only after {:?}, which is a poll interval, not a stop",
9747            began.elapsed()
9748        );
9749    }
9750
9751    #[tokio::test]
9752    async fn a_stop_while_idle_wakes_the_wait_instead_of_sleeping_it_out() {
9753        let dir = tempfile::tempdir().unwrap();
9754        let (opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
9755        let stop = Stop::new();
9756
9757        // Asked for after the loop is already parked on its empty queue, which
9758        // is the case an operator tapping stop on a phone actually hits.
9759        let asker = {
9760            let stop = stop.clone();
9761            tokio::spawn(async move {
9762                tokio::time::sleep(Duration::from_millis(20)).await;
9763                stop.stop();
9764            })
9765        };
9766
9767        let began = std::time::Instant::now();
9768        tokio::time::timeout(
9769            Duration::from_secs(2),
9770            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
9771        )
9772        .await
9773        .expect("a stop asked for while idle must wake the wait")
9774        .expect("the loop's own setup and teardown must not fail");
9775        asker.await.unwrap();
9776        assert!(
9777            began.elapsed() < opts.poll,
9778            "returned only after {:?}, so the stop waited on the sleep",
9779            began.elapsed()
9780        );
9781    }
9782
9783    #[tokio::test]
9784    async fn a_stopped_loop_leaves_no_status_file_claiming_it_is_running() {
9785        let dir = tempfile::tempdir().unwrap();
9786        let (opts, queue, status_file, home, worktrees) = idle_loop(dir.path());
9787        let stop = Stop::new();
9788        stop.stop();
9789
9790        tokio::time::timeout(
9791            Duration::from_secs(2),
9792            drive(&opts, &queue, &status_file, &home, &worktrees, &stop),
9793        )
9794        .await
9795        .expect("a stopped loop must return")
9796        .expect("the loop's own setup and teardown must not fail");
9797
9798        assert!(
9799            home.is_dir(),
9800            "the loop did publish a status file, so its removal is the teardown and not an absence"
9801        );
9802        assert!(
9803            !status_file.exists(),
9804            "a stopped loop clears its status file"
9805        );
9806        assert!(
9807            read_status(&home).is_none(),
9808            "a reader must see no daemon at all, not a heartbeat that merely stopped"
9809        );
9810    }
9811
9812    #[tokio::test]
9813    async fn housekeeping_worker_settles_questions_without_a_scheduler() {
9814        let dir = tempfile::tempdir().unwrap();
9815        let (opts, queue, _, home, worktrees) = idle_loop(dir.path());
9816
9817        let mut settled = RunState::new(
9818            dir.path().join("repo"),
9819            "main".to_owned(),
9820            "abc1234".to_owned(),
9821            "fixture".to_owned(),
9822            Config::default(),
9823        );
9824        settled.status = RunStatus::Ready;
9825        let run_dir = home.join("runs").join(&settled.id);
9826        std::fs::create_dir_all(&run_dir).unwrap();
9827        std::fs::write(
9828            run_dir.join("run.json"),
9829            serde_json::to_string_pretty(&settled).unwrap(),
9830        )
9831        .unwrap();
9832        let questions = Questions::at(home.join("questions"));
9833        let mut question = ask::Question::new(
9834            settled.id.clone(),
9835            "review".to_owned(),
9836            "reviewer-1".to_owned(),
9837            "Continue?".to_owned(),
9838            String::new(),
9839            Vec::new(),
9840        );
9841        questions.put(&mut question).unwrap();
9842
9843        run_service_worker(
9844            &home,
9845            serde_json::to_value(ServiceWorker {
9846                opts,
9847                queue: queue.root().to_path_buf(),
9848                worktrees_root: worktrees,
9849                operation: "housekeeping".to_owned(),
9850            })
9851            .unwrap(),
9852        )
9853        .await
9854        .unwrap();
9855
9856        assert_eq!(
9857            questions.get(&question.id).unwrap().status,
9858            ask::QuestionStatus::Abandoned,
9859            "the independent housekeeping worker settles ended-run questions"
9860        );
9861    }
9862
9863    #[test]
9864    fn cache_check_due_fires_immediately_then_waits_out_its_own_interval() {
9865        let t0 = "2026-09-15T00:00:00Z".parse::<Timestamp>().unwrap();
9866
9867        assert!(
9868            cache_check_due(None, t0, CACHE_CHECK_INTERVAL_SECS),
9869            "never checked before: due at once"
9870        );
9871
9872        let one_sec_later = t0 + jiff::SignedDuration::from_secs(1);
9873        assert!(
9874            !cache_check_due(Some(t0), one_sec_later, CACHE_CHECK_INTERVAL_SECS),
9875            "well inside the interval: not due yet"
9876        );
9877
9878        let at_the_edge = t0 + jiff::SignedDuration::from_secs(CACHE_CHECK_INTERVAL_SECS as i64);
9879        assert!(
9880            !cache_check_due(Some(t0), at_the_edge, CACHE_CHECK_INTERVAL_SECS),
9881            "exactly at the edge: not yet due, same convention as `clean::due`"
9882        );
9883
9884        let past_it = t0 + jiff::SignedDuration::from_secs(CACHE_CHECK_INTERVAL_SECS as i64 + 1);
9885        assert!(
9886            cache_check_due(Some(t0), past_it, CACHE_CHECK_INTERVAL_SECS),
9887            "past the interval: due again"
9888        );
9889    }
9890
9891    /// A `magi.toml` whose `[verify] gate` names `cache_dir` as its shared
9892    /// `CARGO_TARGET_DIR`, capped at `limit_bytes`, plus a repository path
9893    /// that is never created - the fixtures [`maybe_prune_cache_between_runs`]
9894    /// and the congestion test below both need, and must not drift apart.
9895    fn cache_check_opts(dir: &Path, cache_dir: &Path, limit_bytes: u64) -> Opts {
9896        let config = dir.join("magi.toml");
9897        // A literal (single-quoted) TOML string, not a basic one: the cache
9898        // path is a Windows path full of backslashes, and a basic string
9899        // would have TOML try to interpret `\U` (from `\Users\...`) as a
9900        // Unicode escape and fail to parse - the same trap `magi.toml`'s own
9901        // `{{ vars.cache }}` rendering documents.
9902        std::fs::write(
9903            &config,
9904            format!(
9905                "[disk]\nmin_free_bytes = 0\nauto_fold = false\ncache_limit_bytes = {limit_bytes}\n\n\
9906                 [verify]\ngate = ['CARGO_TARGET_DIR={} cargo make check']\n",
9907                cache_dir.display()
9908            ),
9909        )
9910        .unwrap();
9911        Opts {
9912            config: Some(config),
9913            repo: dir.join("repo"),
9914            ..Opts::default()
9915        }
9916    }
9917
9918    #[tokio::test]
9919    async fn maybe_prune_cache_between_runs_reprunes_only_once_its_own_interval_elapses() {
9920        let dir = tempfile::tempdir().unwrap();
9921        let home = dir.path().join("home");
9922        let cache_dir = dir.path().join("cache");
9923        std::fs::create_dir_all(&cache_dir).unwrap();
9924        std::fs::write(cache_dir.join("a"), vec![0u8; 10]).unwrap();
9925        let opts = cache_check_opts(dir.path(), &cache_dir, 1);
9926
9927        // Nobody has asked this daemon to stop, which is the ordinary case;
9928        // the skip that a stop buys is asserted by its own test below.
9929        let running = Stop::new();
9930        let mut last_checked = None;
9931        let t0 = "2026-09-15T00:00:00Z".parse::<Timestamp>().unwrap();
9932        maybe_prune_cache_between_runs(&opts.repo, &opts, &home, &running, &mut last_checked, t0)
9933            .await;
9934        assert_eq!(
9935            crate::disk::dir_size(&cache_dir),
9936            0,
9937            "over the cap on the first check ever: pruned at once, no idle queue required"
9938        );
9939        assert_eq!(last_checked, Some(t0));
9940
9941        // A fresh oversized file lands, but the next check is not due yet.
9942        std::fs::write(cache_dir.join("b"), vec![0u8; 10]).unwrap();
9943        let too_soon = t0 + jiff::SignedDuration::from_secs(1);
9944        maybe_prune_cache_between_runs(
9945            &opts.repo,
9946            &opts,
9947            &home,
9948            &running,
9949            &mut last_checked,
9950            too_soon,
9951        )
9952        .await;
9953        assert_eq!(
9954            crate::disk::dir_size(&cache_dir),
9955            10,
9956            "too soon since the last check: left alone rather than rescanned every call"
9957        );
9958        assert_eq!(
9959            last_checked,
9960            Some(t0),
9961            "an idle check does not reset the clock"
9962        );
9963
9964        // Once the interval elapses, the same oversized cache is caught again.
9965        let due_again = t0 + jiff::SignedDuration::from_secs(CACHE_CHECK_INTERVAL_SECS as i64 + 1);
9966        maybe_prune_cache_between_runs(
9967            &opts.repo,
9968            &opts,
9969            &home,
9970            &running,
9971            &mut last_checked,
9972            due_again,
9973        )
9974        .await;
9975        assert_eq!(
9976            crate::disk::dir_size(&cache_dir),
9977            0,
9978            "due again: pruned back under the cap"
9979        );
9980    }
9981
9982    /// A stop must not queue behind housekeeping. The prune below is a
9983    /// synchronous walk of the whole cache with no await point in it, so a
9984    /// loop that entered it could not get back to its own `stopped()` test
9985    /// until the walk finished - and because no run is in flight at this
9986    /// boundary, `Stop::finishing` would meanwhile tell the operator's screen
9987    /// the stop had already landed. The idle branch has always made this same
9988    /// check before reaching `janitor`; the between-runs path makes it too.
9989    #[tokio::test]
9990    async fn a_stop_already_asked_for_skips_the_between_runs_cache_walk() {
9991        let dir = tempfile::tempdir().unwrap();
9992        let home = dir.path().join("home");
9993        let cache_dir = dir.path().join("cache");
9994        std::fs::create_dir_all(&cache_dir).unwrap();
9995        std::fs::write(cache_dir.join("a"), vec![0u8; 10]).unwrap();
9996        let opts = cache_check_opts(dir.path(), &cache_dir, 1);
9997
9998        let stop = Stop::new();
9999        stop.stop();
10000        assert!(
10001            !stop.finishing(),
10002            "no run is in flight at a between-runs boundary, so nothing else \
10003             would tell the operator this stop had not taken effect yet"
10004        );
10005
10006        let mut last_checked = None;
10007        let t0 = "2026-09-15T00:00:00Z".parse::<Timestamp>().unwrap();
10008        maybe_prune_cache_between_runs(&opts.repo, &opts, &home, &stop, &mut last_checked, t0)
10009            .await;
10010        assert_eq!(
10011            crate::disk::dir_size(&cache_dir),
10012            10,
10013            "over its cap, and due for the first check ever, but a stop outranks \
10014             it: the cap is a standing policy the next start measures again"
10015        );
10016        assert_eq!(
10017            last_checked, None,
10018            "a check that never happened must not claim the interval"
10019        );
10020    }
10021
10022    /// A backlog must not starve cache pruning. The finite cache worker runs
10023    /// independently of queue drain and must not consume any task attempts.
10024    #[tokio::test]
10025    async fn cache_prune_reaches_a_queue_that_never_goes_idle() {
10026        let dir = tempfile::tempdir().unwrap();
10027        let cache_dir = dir.path().join("cache");
10028        std::fs::create_dir_all(&cache_dir).unwrap();
10029        std::fs::write(cache_dir.join("stale"), vec![0u8; 4096]).unwrap();
10030
10031        let mut opts = cache_check_opts(dir.path(), &cache_dir, 1);
10032        opts.poll = Duration::from_millis(20);
10033        opts.max_attempts = 1_000;
10034
10035        let queue = Queue::at(dir.path().join("queue"));
10036        for i in 0..128 {
10037            let mut task = Task::new(
10038                format!("queued work {i}"),
10039                format!("queued work {i}"),
10040                opts.repo.clone(),
10041                Source::Human,
10042            );
10043            queue.put(&mut task).unwrap();
10044        }
10045
10046        let home = dir.path().join("home");
10047        let worktrees = dir.path().join("wt");
10048        run_service_worker(
10049            &home,
10050            serde_json::to_value(ServiceWorker {
10051                opts,
10052                queue: queue.root().to_path_buf(),
10053                worktrees_root: worktrees,
10054                operation: "cache".to_owned(),
10055            })
10056            .unwrap(),
10057        )
10058        .await
10059        .unwrap();
10060        let after = queue.list();
10061        assert_eq!(after.len(), 128);
10062        assert!(
10063            after
10064                .iter()
10065                .all(|task| task.status.runnable() && task.attempts == 0)
10066        );
10067
10068        assert_eq!(
10069            crate::disk::dir_size(&cache_dir),
10070            0,
10071            "an oversized cache must not be left to grow unboundedly just because the \
10072             queue kept the loop busy the whole time"
10073        );
10074    }
10075
10076    #[test]
10077    fn task_question_reconciliation_keeps_references_and_retires_manual_releases() {
10078        let dir = tempfile::tempdir().unwrap();
10079        let queue = Queue::at(dir.path().join("queue"));
10080        let questions = Questions::at(dir.path().join("questions"));
10081        let mut task = task();
10082        queue.put(&mut task).unwrap();
10083
10084        let mut task_question = ask::Question::new(
10085            task.id.clone(),
10086            crate::conduct::NODE.to_owned(),
10087            "conduct".to_owned(),
10088            "Which backend?".to_owned(),
10089            String::new(),
10090            Vec::new(),
10091        );
10092        questions.put(&mut task_question).unwrap();
10093        task.block(vec![task_question.id.clone()], None);
10094        queue.put(&mut task).unwrap();
10095
10096        let mut run_question = ask::Question::new(
10097            "20260101-000000-run1".to_owned(),
10098            "review".to_owned(),
10099            "reviewer-1".to_owned(),
10100            "Run question".to_owned(),
10101            String::new(),
10102            Vec::new(),
10103        );
10104        questions.put(&mut run_question).unwrap();
10105
10106        // A question from another node whose `run` happens to equal this
10107        // task's id — the same field, filled in for an unrelated reason. Only
10108        // `crate::conduct::NODE` questions use `run` as a task id; this one
10109        // must never be touched by this reconciliation, even after release.
10110        let mut coincidental = ask::Question::new(
10111            task.id.clone(),
10112            "review".to_owned(),
10113            "reviewer-1".to_owned(),
10114            "Unrelated review question".to_owned(),
10115            String::new(),
10116            Vec::new(),
10117        );
10118        questions.put(&mut coincidental).unwrap();
10119
10120        reconcile_task_questions(&queue, &questions);
10121        assert!(questions.get(&task_question.id).unwrap().status.open());
10122        assert!(questions.get(&run_question.id).unwrap().status.open());
10123        assert!(questions.get(&coincidental.id).unwrap().status.open());
10124
10125        task.release();
10126        queue.put(&mut task).unwrap();
10127        reconcile_task_questions(&queue, &questions);
10128        assert_eq!(
10129            questions.get(&task_question.id).unwrap().status,
10130            ask::QuestionStatus::Abandoned
10131        );
10132        assert!(
10133            questions.get(&run_question.id).unwrap().status.open(),
10134            "run questions remain the run janitor's responsibility"
10135        );
10136        assert!(
10137            questions.get(&coincidental.id).unwrap().status.open(),
10138            "a non-conductor question must not be abandoned just because its \
10139             run id coincides with a task id"
10140        );
10141    }
10142
10143    #[test]
10144    fn a_freshly_started_running_task_is_never_stalled() {
10145        let dir = tempfile::tempdir().unwrap();
10146        let mut t = task();
10147        t.start("run-1".to_owned());
10148        // `updated_at` is `Timestamp::now()`, left alone: no live daemon
10149        // named in `dir`, but nowhere near `STALLED_RUNNING` yet.
10150        assert!(!is_stalled(&t, dir.path(), Timestamp::now()));
10151    }
10152
10153    #[test]
10154    fn a_long_running_task_with_no_live_daemon_is_stalled() {
10155        let dir = tempfile::tempdir().unwrap();
10156        let mut t = task();
10157        t.start("run-1".to_owned());
10158        t.updated_at = Timestamp::now()
10159            - jiff::SignedDuration::from_secs(STALLED_RUNNING.as_secs() as i64 + 60);
10160        assert!(is_stalled(&t, dir.path(), Timestamp::now()));
10161        assert_eq!(
10162            stalled_tasks(
10163                &Queue::at(dir.path().join("q")),
10164                dir.path(),
10165                Timestamp::now()
10166            )
10167            .len(),
10168            0,
10169            "the task was never written to this queue"
10170        );
10171    }
10172
10173    #[test]
10174    fn a_long_running_task_a_live_daemon_still_names_is_not_stalled() {
10175        let dir = tempfile::tempdir().unwrap();
10176        let mut t = task();
10177        t.id = "20260903-080340-0167".to_owned();
10178        t.start("20260903-080619-01c2".to_owned());
10179        t.updated_at = Timestamp::now()
10180            - jiff::SignedDuration::from_secs(STALLED_RUNNING.as_secs() as i64 + 60);
10181
10182        let mut status = Status::new();
10183        status.current = vec![Current {
10184            task: t.id.clone(),
10185            run: "20260903-080619-01c2".to_owned(),
10186        }];
10187        write_status_to(&dir.path().join("daemon.json"), &status).unwrap();
10188
10189        assert!(
10190            !is_stalled(&t, dir.path(), Timestamp::now()),
10191            "a live daemon's own heartbeat rules out stalled, however long the task has run"
10192        );
10193    }
10194
10195    /// Rewrite a task's `updated_at` on disk directly, bypassing
10196    /// `Queue::put`'s own `Timestamp::now()` stamping - the only way to make
10197    /// a fixture look like it has genuinely been `running` for a while.
10198    fn backdate_task(queue: &Queue, id: &str, seconds_ago: i64) {
10199        let path = queue.path_of(id);
10200        let body = std::fs::read_to_string(&path).unwrap();
10201        let mut v: serde_json::Value = serde_json::from_str(&body).unwrap();
10202        let old = Timestamp::now() - jiff::SignedDuration::from_secs(seconds_ago);
10203        v["updated_at"] = serde_json::Value::String(old.to_string());
10204        std::fs::write(&path, serde_json::to_string_pretty(&v).unwrap()).unwrap();
10205    }
10206
10207    #[test]
10208    fn stalled_tasks_still_reaches_a_task_reclaim_could_not_claim_yet() {
10209        // The realistic `poll()` ordering, not `is_stalled` in isolation:
10210        // `reclaim_orphaned_running` runs first, on every poll, and settles
10211        // any `running` task whose claim it can actually take. For most
10212        // crashes that is immediate - a dead pid is proof enough for
10213        // `sweep_stale_claims` to drop the lock the same tick, and the very
10214        // next claim attempt succeeds. But a lock whose pid cannot be parsed
10215        // at all falls back to `STALE_CLAIM`'s six-hour age instead (see
10216        // `sweep_stale_claims`'s own doc), so the lock - and the claim
10217        // failure behind it - can legitimately outlive many polls. This is
10218        // exactly the gap `stalled_tasks` exists to surface well before that
10219        // six-hour sweep would: reclaim leaves the task `running`, and it
10220        // must still reach the conductor as stalled.
10221        let dir = tempfile::tempdir().unwrap();
10222        let queue = Queue::at(dir.path().join("queue"));
10223        let home = dir.path().join("home");
10224
10225        let mut t = task();
10226        t.id = "20260101-000001-lock".to_owned();
10227        t.start("run-1".to_owned());
10228        queue.put(&mut t).unwrap();
10229        backdate_task(&queue, &t.id, STALLED_RUNNING.as_secs() as i64 + 60);
10230        std::fs::write(
10231            dir.path().join("queue").join(format!("{}.lock", t.id)),
10232            "not a pid",
10233        )
10234        .unwrap();
10235
10236        let now = Timestamp::now();
10237        assert!(
10238            reclaim_orphaned_running(&queue, 2, &Default::default()).is_empty(),
10239            "the unparseable lock is still well within STALE_CLAIM, so the claim fails \
10240             and reclaim must leave the task alone"
10241        );
10242        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Running);
10243
10244        let stalled = stalled_tasks(&queue, &home, now);
10245        assert_eq!(
10246            stalled.len(),
10247            1,
10248            "reclaim's inability to claim it yet must not hide it from the conductor"
10249        );
10250        assert_eq!(stalled[0].id, t.id);
10251    }
10252
10253    #[test]
10254    fn ordinary_dead_daemon_task_is_shown_stalled_before_reclaim_and_can_be_requeued() {
10255        let dir = tempfile::tempdir().unwrap();
10256        crate::run::set_home(dir.path().join("run-home"));
10257        let queue = Queue::at(dir.path().join("queue"));
10258        let home = dir.path().join("home");
10259        let questions = Questions::at(dir.path().join("questions"));
10260
10261        let mut t = task();
10262        t.id = "20260101-000003-dead".to_owned();
10263        t.start("missing-run".to_owned());
10264        queue.put(&mut t).unwrap();
10265        backdate_task(&queue, &t.id, STALLED_RUNNING.as_secs() as i64 + 60);
10266
10267        // This is the real poll ordering: retain the deterministic stalled
10268        // input before a claim proves the owner is gone and reclaims it.
10269        let stalled = stalled_tasks(&queue, &home, Timestamp::now());
10270        assert_eq!(
10271            stalled.iter().map(|task| &task.id).collect::<Vec<_>>(),
10272            [&t.id]
10273        );
10274        assert_eq!(
10275            reclaim_orphaned_running(&queue, 2, &Default::default()),
10276            [t.id.clone()]
10277        );
10278        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
10279
10280        // Reclaim drops its guard before conductor decisions are applied, so
10281        // the decision for the captured stalled input has a real write path.
10282        crate::conduct::apply(
10283            &queue,
10284            &questions,
10285            &crate::conduct::Verdict {
10286                decisions: vec![crate::conduct::Decision {
10287                    id: t.id.clone(),
10288                    recovery: Some(crate::conduct::Recovery::Requeue),
10289                    ..crate::conduct::Decision::default()
10290                }],
10291            },
10292        )
10293        .unwrap();
10294        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Queued);
10295    }
10296
10297    #[test]
10298    fn stalled_tasks_reports_exactly_the_tasks_is_stalled_agrees_on() {
10299        let dir = tempfile::tempdir().unwrap();
10300        let queue = Queue::at(dir.path().join("queue"));
10301        let home = dir.path().join("home");
10302
10303        let mut fresh = task();
10304        fresh.id = "20260101-000001-aaaa".to_owned();
10305        fresh.start("run-1".to_owned());
10306        queue.put(&mut fresh).unwrap();
10307
10308        let mut old = task();
10309        old.id = "20260101-000002-bbbb".to_owned();
10310        old.start("run-2".to_owned());
10311        queue.put(&mut old).unwrap();
10312        backdate_task(&queue, &old.id, STALLED_RUNNING.as_secs() as i64 + 60);
10313
10314        let stalled = stalled_tasks(&queue, &home, Timestamp::now());
10315        assert_eq!(stalled.len(), 1);
10316        assert_eq!(stalled[0].id, old.id);
10317    }
10318
10319    #[test]
10320    fn queued_and_finished_task_views_partition_by_status() {
10321        let dir = tempfile::tempdir().unwrap();
10322        let queue = Queue::at(dir.path().join("queue"));
10323
10324        let mut queued = task();
10325        queued.id = "20260101-000001-aaaa".to_owned();
10326        queue.put(&mut queued).unwrap();
10327
10328        let mut failed = task();
10329        failed.id = "20260101-000002-bbbb".to_owned();
10330        failed.start("run-1".to_owned());
10331        failed.fail("gate red", 5);
10332        queue.put(&mut failed).unwrap();
10333
10334        let mut held = task();
10335        held.id = "20260101-000003-cccc".to_owned();
10336        held.hold_machine(None);
10337        queue.put(&mut held).unwrap();
10338
10339        let mut running = task();
10340        running.id = "20260101-000004-dddd".to_owned();
10341        running.start("run-2".to_owned());
10342        queue.put(&mut running).unwrap();
10343
10344        let queued_ids: Vec<String> = queued_tasks(&queue).into_iter().map(|t| t.id).collect();
10345        assert_eq!(queued_ids, [queued.id.clone()]);
10346
10347        let mut finished_ids: Vec<String> =
10348            finished_tasks(&queue).into_iter().map(|t| t.id).collect();
10349        finished_ids.sort_unstable();
10350        let mut want = vec![failed.id.clone(), held.id.clone()];
10351        want.sort_unstable();
10352        assert_eq!(finished_ids, want);
10353    }
10354
10355    #[test]
10356    fn dependency_resolution_preserves_existing_short_and_full_running_chains() {
10357        for short in [false, true] {
10358            let dir = tempfile::tempdir().unwrap();
10359            let queue = Queue::at(dir.path().join("queue"));
10360            let questions = ask::Questions::at(dir.path().join("questions"));
10361            let mut running = task();
10362            running.id = "20261013-192213-feb2".into();
10363            running.start("existing-run".into());
10364            queue.put(&mut running).unwrap();
10365            let mut middle = task();
10366            middle.id = "20261013-193611-ca5b".into();
10367            middle.block(
10368                vec![if short {
10369                    "feb2".into()
10370                } else {
10371                    running.id.clone()
10372                }],
10373                None,
10374            );
10375            queue.put(&mut middle).unwrap();
10376            let mut waiting = task();
10377            waiting.id = "20261013-202849-ee37".into();
10378            waiting.block(
10379                vec![if short {
10380                    "ca5b".into()
10381                } else {
10382                    middle.id.clone()
10383                }],
10384                Some("wait for implementation".into()),
10385            );
10386            queue.put(&mut waiting).unwrap();
10387            assert_eq!(queue.get("ca5b").unwrap().id, middle.id);
10388            let inv = crate::blockers::Inventory::load(&queue, &questions);
10389            assert_eq!(inv.waits_on(&waiting), ["ca5b (blocked → feb2 running)"]);
10390            assert!(inv.stuck().is_empty());
10391            let report = crate::triage::run_once(&queue, &questions, None, Timestamp::now());
10392            assert!(report.quarantined.is_empty());
10393            assert!(report.asked.is_empty());
10394            resolve_blockers(&queue, &questions);
10395            assert_eq!(queue.get(&waiting.id).unwrap().status, TaskStatus::Blocked);
10396            assert!(questions.list().is_empty());
10397            // Finishing the transitive prerequisite alone does not satisfy
10398            // the task's direct dependency.
10399            running.succeed();
10400            queue.put(&mut running).unwrap();
10401            resolve_blockers(&queue, &questions);
10402            assert_eq!(queue.get(&middle.id).unwrap().status, TaskStatus::Queued);
10403            assert_eq!(queue.get(&waiting.id).unwrap().status, TaskStatus::Blocked);
10404            middle = queue.get(&middle.id).unwrap();
10405            middle.succeed();
10406            queue.put(&mut middle).unwrap();
10407            resolve_blockers(&queue, &questions);
10408            let after = queue.get(&waiting.id).unwrap();
10409            assert_eq!(after.status, TaskStatus::Queued);
10410            assert!(after.blocked_by.is_empty());
10411            assert_eq!(after.instruction, waiting.instruction);
10412        }
10413    }
10414
10415    #[test]
10416    fn dependency_resolution_unknown_records_never_hold_ask_or_release() {
10417        for future_schema in [false, true] {
10418            let name = "20261013-193611-ca5b";
10419            let mut future = task();
10420            future.id = name.into();
10421            future.schema = crate::queue::SCHEMA + 1;
10422            let contents = if future_schema {
10423                serde_json::to_string(&future).unwrap()
10424            } else {
10425                "corrupt".into()
10426            };
10427            let dir = tempfile::tempdir().unwrap();
10428            let queue = Queue::at(dir.path().join("queue"));
10429            let questions = ask::Questions::at(dir.path().join("questions"));
10430            let mut waiting = task();
10431            waiting.id = "20261013-202849-ee37".into();
10432            waiting.block(vec!["ca5b".into()], None);
10433            queue.put(&mut waiting).unwrap();
10434            std::fs::write(queue.path_of(name), contents).unwrap();
10435            assert_eq!(queue.resolve_id("ca5b").unwrap(), name);
10436            assert!(
10437                crate::queue::missing_blockers(&queue, &questions, &waiting.blocked_by).is_empty()
10438            );
10439            let report = crate::triage::run_once(&queue, &questions, None, Timestamp::now());
10440            assert!(report.asked.is_empty());
10441            resolve_blockers(&queue, &questions);
10442            assert_eq!(queue.get(&waiting.id).unwrap().status, TaskStatus::Blocked);
10443            assert!(questions.list().is_empty());
10444        }
10445        let dir = tempfile::tempdir().unwrap();
10446        let queue = Queue::at(dir.path().join("queue"));
10447        let questions = ask::Questions::at(dir.path().join("questions"));
10448        let mut waiting = task();
10449        waiting.id = "20261013-202849-ee37".into();
10450        waiting.block(vec!["ca5b".into()], None);
10451        queue.put(&mut waiting).unwrap();
10452        let mut done = task();
10453        done.id = "20261013-193611-ca5b".into();
10454        done.succeed();
10455        queue.put(&mut done).unwrap();
10456        // An unreadable second match must not disappear from uniqueness
10457        // checks just because Queue::list skips it.
10458        std::fs::write(queue.path_of("20261014-193611-ca5b"), "bad").unwrap();
10459        assert!(queue.get("ca5b").is_err());
10460        assert_eq!(
10461            crate::queue::dependency(&queue, &questions, "ca5b"),
10462            crate::queue::Dependency::Ambiguous
10463        );
10464        resolve_blockers(&queue, &questions);
10465        let report = crate::triage::run_once(&queue, &questions, None, Timestamp::now());
10466        assert!(report.asked.is_empty());
10467        assert_eq!(queue.get(&waiting.id).unwrap().status, TaskStatus::Blocked);
10468        assert_eq!(queue.get(&done.id).unwrap().status, TaskStatus::Done);
10469    }
10470
10471    #[test]
10472    fn dependency_resolution_preserves_independent_consent_and_holds_after_completion() {
10473        for held in [false, true] {
10474            for question in [false, true] {
10475                for reference in ["ca5b", "20261013-193611-ca5b"] {
10476                    let dir = tempfile::tempdir().unwrap();
10477                    let queue = Queue::at(dir.path().join("queue"));
10478                    let questions = ask::Questions::at(dir.path().join("questions"));
10479                    let mut prerequisite = task();
10480                    prerequisite.id = "20261013-193611-ca5b".into();
10481                    prerequisite.start("existing-run".into());
10482                    queue.put(&mut prerequisite).unwrap();
10483                    let mut waiting = task();
10484                    waiting.id = "20261013-202849-ee37".into();
10485                    if held {
10486                        waiting.hold_machine(Some("waiting for separate consent".into()));
10487                    }
10488                    queue.put(&mut waiting).unwrap();
10489                    let reason = "Deleting saved data requires separate owner consent";
10490                    crate::conduct::apply(
10491                        &queue,
10492                        &questions,
10493                        &crate::conduct::Verdict {
10494                            decisions: vec![crate::conduct::Decision {
10495                                id: waiting.id.clone(),
10496                                blocked_by: vec![reference.into()],
10497                                question: question.then(|| "May the saved data be deleted?".into()),
10498                                recovery: (!question).then_some(crate::conduct::Recovery::Hold),
10499                                reason: Some(reason.into()),
10500                                ..crate::conduct::Decision::default()
10501                            }],
10502                        },
10503                    )
10504                    .unwrap();
10505                    let before = queue.get(&waiting.id).unwrap();
10506                    if question {
10507                        assert_eq!(before.status, TaskStatus::Blocked);
10508                        let q = questions.get(&before.blocked_by[0]).unwrap();
10509                        assert_eq!(q.summary, "May the saved data be deleted?");
10510                        assert!(q.status.open());
10511                    } else {
10512                        assert_eq!(before.status, TaskStatus::Held);
10513                        let recorded_reason = before.hold_reason.as_deref().unwrap();
10514                        assert!(recorded_reason.starts_with(reason));
10515                        if held {
10516                            assert!(recorded_reason.contains("waiting for separate consent"));
10517                        }
10518                    }
10519                    prerequisite.succeed();
10520                    queue.put(&mut prerequisite).unwrap();
10521                    resolve_blockers(&queue, &questions);
10522                    let after = queue.get(&waiting.id).unwrap();
10523                    assert_eq!(after.status, before.status);
10524                    assert_eq!(after.blocked_by, before.blocked_by);
10525                    assert_eq!(after.hold_reason, before.hold_reason);
10526                    assert_eq!(after.instruction, waiting.instruction);
10527                    if question {
10528                        assert!(questions.get(&after.blocked_by[0]).unwrap().status.open());
10529                    }
10530                    assert!(
10531                        queue.next_runnable().is_none(),
10532                        "dependency completion is not consent"
10533                    );
10534                }
10535            }
10536        }
10537    }
10538
10539    #[test]
10540    fn resolve_blockers_clears_a_done_dependency_and_keeps_an_unresolved_one() {
10541        let dir = tempfile::tempdir().unwrap();
10542        let queue = Queue::at(dir.path().join("queue"));
10543        let questions = ask::Questions::at(dir.path().join("questions"));
10544
10545        let mut dep = task();
10546        dep.id = "20260101-000001-dep0".to_owned();
10547        dep.succeed();
10548        queue.put(&mut dep).unwrap();
10549
10550        let mut still_going = task();
10551        still_going.id = "20260101-000002-dep1".to_owned();
10552        queue.put(&mut still_going).unwrap();
10553
10554        let mut blocked = task();
10555        blocked.id = "20260101-000003-main".to_owned();
10556        blocked.block(
10557            vec![dep.id.clone(), still_going.id.clone()],
10558            Some("waits on both".to_owned()),
10559        );
10560        queue.put(&mut blocked).unwrap();
10561
10562        resolve_blockers(&queue, &questions);
10563
10564        let after = queue.get(&blocked.id).unwrap();
10565        assert_eq!(
10566            after.status,
10567            TaskStatus::Blocked,
10568            "one dependency is still outstanding"
10569        );
10570        assert_eq!(after.blocked_by, [still_going.id.clone()]);
10571    }
10572
10573    #[test]
10574    fn resolve_blockers_carries_an_answers_content_onto_the_task_and_unblocks_it() {
10575        let dir = tempfile::tempdir().unwrap();
10576        let queue = Queue::at(dir.path().join("queue"));
10577        let questions = ask::Questions::at(dir.path().join("questions"));
10578
10579        let mut q = crate::ask::Question::new(
10580            "20260101-000001-main".to_owned(),
10581            crate::conduct::NODE.to_owned(),
10582            "conduct".to_owned(),
10583            "Which backend?".to_owned(),
10584            String::new(),
10585            Vec::new(),
10586        );
10587        questions.put(&mut q).unwrap();
10588        q.answer(crate::ask::Answer::Text("SQLite".to_owned()))
10589            .unwrap();
10590        questions.put(&mut q).unwrap();
10591
10592        let mut blocked = task();
10593        blocked.id = "20260101-000001-main".to_owned();
10594        blocked.block(vec![q.id.clone()], Some("which backend?".to_owned()));
10595        queue.put(&mut blocked).unwrap();
10596
10597        resolve_blockers(&queue, &questions);
10598
10599        let after = queue.get(&blocked.id).unwrap();
10600        assert_eq!(
10601            after.status,
10602            TaskStatus::Queued,
10603            "the only blocker resolved"
10604        );
10605        assert_eq!(after.answers.len(), 1);
10606        assert_eq!(after.answers[0].question, "Which backend?");
10607        assert_eq!(after.answers[0].answer, "SQLite");
10608
10609        // And the run this task starts next is told about it.
10610        let instruction = instruction_for(&after);
10611        assert!(instruction.contains("Which backend?"));
10612        assert!(instruction.contains("SQLite"));
10613    }
10614
10615    #[test]
10616    fn resolve_blockers_holds_a_task_whose_conductor_question_was_abandoned() {
10617        let dir = tempfile::tempdir().unwrap();
10618        let queue = Queue::at(dir.path().join("queue"));
10619        let questions = ask::Questions::at(dir.path().join("questions"));
10620
10621        let mut q = crate::ask::Question::new(
10622            "20260101-000001-main".to_owned(),
10623            crate::conduct::NODE.to_owned(),
10624            "conduct".to_owned(),
10625            "Is the setup done?".to_owned(),
10626            String::new(),
10627            Vec::new(),
10628        );
10629        q.abandon("no answer within 60s of asking");
10630        questions.put(&mut q).unwrap();
10631
10632        let mut blocked = task();
10633        blocked.id = "20260101-000001-main".to_owned();
10634        blocked.block(vec![q.id.clone()], Some("setup?".to_owned()));
10635        queue.put(&mut blocked).unwrap();
10636
10637        resolve_blockers(&queue, &questions);
10638
10639        let after = queue.get(&blocked.id).unwrap();
10640        assert_eq!(
10641            after.status,
10642            TaskStatus::Held,
10643            "never left blocked on nothing"
10644        );
10645        assert!(!after.operator_held(), "a machine hold, for triage");
10646        assert!(
10647            after
10648                .hold_reason
10649                .as_deref()
10650                .unwrap_or_default()
10651                .contains("went unanswered")
10652        );
10653    }
10654
10655    #[test]
10656    fn resolve_blockers_restores_a_held_task_to_held_instead_of_queuing_it() {
10657        // Reproduces the reported bug (task 3958): a task held out of
10658        // attempts, blocked on a `crate::conduct` follow-up question, must
10659        // come back `held` once that question is answered - never `queued`,
10660        // whatever the answer said - or it silently re-enters the
10661        // competition queue with its attempts already exhausted.
10662        let dir = tempfile::tempdir().unwrap();
10663        let queue = Queue::at(dir.path().join("queue"));
10664        let questions = ask::Questions::at(dir.path().join("questions"));
10665
10666        let mut q = crate::ask::Question::new(
10667            "20260101-000001-main".to_owned(),
10668            crate::conduct::NODE.to_owned(),
10669            "conduct".to_owned(),
10670            "How should this be handled?".to_owned(),
10671            String::new(),
10672            Vec::new(),
10673        );
10674        questions.put(&mut q).unwrap();
10675        q.answer(crate::ask::Answer::Text(
10676            "leave it held, a human will look at it later".to_owned(),
10677        ))
10678        .unwrap();
10679        questions.put(&mut q).unwrap();
10680
10681        let mut held = task();
10682        held.id = "20260101-000001-main".to_owned();
10683        held.hold_machine(Some("out of attempts".to_owned()));
10684        held.block(vec![q.id.clone()], Some("what now?".to_owned()));
10685        queue.put(&mut held).unwrap();
10686
10687        resolve_blockers(&queue, &questions);
10688
10689        let after = queue.get(&held.id).unwrap();
10690        assert_eq!(after.status, TaskStatus::Held);
10691        assert_eq!(after.hold_reason.as_deref(), Some("out of attempts"));
10692        assert_eq!(
10693            after.answers[0].answer,
10694            "leave it held, a human will look at it later"
10695        );
10696    }
10697
10698    #[test]
10699    fn resolve_blockers_releases_a_task_whose_dependency_was_deleted_on_purpose() {
10700        let dir = tempfile::tempdir().unwrap();
10701        let queue = Queue::at(dir.path().join("queue"));
10702        let questions = ask::Questions::at(dir.path().join("questions"));
10703        let mut dep = task();
10704        dep.id = "20260101-000001-gone".to_owned();
10705        queue.put(&mut dep).unwrap();
10706        let mut blocked = task();
10707        blocked.id = "20260101-000003-main".to_owned();
10708        blocked.block(vec![dep.id.clone()], None);
10709        queue.put(&mut blocked).unwrap();
10710
10711        // Claimed, so `remove` cannot rewrite it and the resolver must.
10712        let claim = queue.claim(&blocked.id).unwrap();
10713        queue.remove(&dep.id, false, &questions).unwrap();
10714        drop(claim);
10715        resolve_blockers(&queue, &questions);
10716
10717        let after = queue.get(&blocked.id).unwrap();
10718        assert_eq!(after.status, TaskStatus::Queued);
10719        assert!(after.blocked_by.is_empty());
10720    }
10721
10722    #[test]
10723    fn resolve_blockers_holds_a_task_whose_dependency_was_deleted() {
10724        // Reproduces the reported bug: a task blocked on a task id that was
10725        // removed (`magi task rm`, or deleted by hand) can never see that id
10726        // reach `Done`, so the ordinary per-id loop has nothing to notice and
10727        // would otherwise leave the task `blocked` forever with no way for an
10728        // operator to find out why.
10729        let dir = tempfile::tempdir().unwrap();
10730        let queue = Queue::at(dir.path().join("queue"));
10731        let questions = ask::Questions::at(dir.path().join("questions"));
10732
10733        let mut still_going = task();
10734        still_going.id = "20260101-000002-dep1".to_owned();
10735        queue.put(&mut still_going).unwrap();
10736
10737        let mut blocked = task();
10738        blocked.id = "20260101-000003-main".to_owned();
10739        blocked.block(
10740            vec!["20260101-000001-gone".to_owned(), still_going.id.clone()],
10741            Some("waits on both".to_owned()),
10742        );
10743        queue.put(&mut blocked).unwrap();
10744
10745        resolve_blockers(&queue, &questions);
10746
10747        let after = queue.get(&blocked.id).unwrap();
10748        assert_eq!(
10749            after.status,
10750            TaskStatus::Held,
10751            "a missing dependency must not leave the task blocked forever"
10752        );
10753        assert_eq!(after.hold_source, Some(crate::queue::HoldSource::Machine));
10754        assert!(after.blocked_by.is_empty());
10755        let reason = after.hold_reason.as_deref().unwrap_or_default();
10756        assert!(
10757            reason.contains("20260101-000001-gone"),
10758            "the missing id must be named so an operator can tell what happened: {reason}"
10759        );
10760        assert!(
10761            reason.contains(&still_going.id),
10762            "the still-valid dependency must not silently vanish from the record: {reason}"
10763        );
10764    }
10765
10766    #[test]
10767    fn instruction_for_is_unchanged_without_any_answers() {
10768        let t = task();
10769        assert_eq!(instruction_for(&t), t.instruction);
10770    }
10771
10772    #[test]
10773    fn task_attachments_are_absolute_and_a_missing_file_is_an_error() {
10774        let dir = tempfile::tempdir().unwrap();
10775        let q = Queue::at(dir.path().join("queue"));
10776        let src = dir.path().join("shot.png");
10777        std::fs::write(&src, "x").unwrap();
10778        let mut t = task();
10779        q.attach(&mut t, &[src]).unwrap();
10780        let paths = task_attachments(&q, &t).unwrap();
10781        assert_eq!(paths.len(), 1);
10782        assert!(paths[0].is_absolute() && paths[0].is_file());
10783        std::fs::remove_file(&paths[0]).unwrap();
10784        let err = task_attachments(&q, &t).unwrap_err().to_string();
10785        assert!(err.contains("shot.png"), "{err}");
10786    }
10787
10788    #[tokio::test]
10789    async fn config_failure_notices_distinguish_boundaries_and_targets_after_restart() {
10790        let dir = tempfile::tempdir().unwrap();
10791        let queue_path = dir.path().join("queue");
10792        let queue = Queue::at(queue_path.clone());
10793        let mut t = task();
10794        t.source = Source::Agent {
10795            run: "originating-chat".into(),
10796            node: crate::queue::CHAT_NODE.into(),
10797        };
10798        let fetch = anyhow::anyhow!("network unavailable").context(crate::config::ConfigFailure {
10799            stage: "config-fetch",
10800            target: "origin/main".into(),
10801        });
10802        record_config_failure(&mut t, &fetch);
10803        t.hold_machine(Some(format!("{fetch:#}")));
10804        let fetch_key = t.chat_notice_due().unwrap();
10805        t.chat_notices.push(fetch_key.clone());
10806        queue.put(&mut t).unwrap();
10807
10808        let queue = Queue::at(queue_path);
10809        let mut t = queue.get(&t.id).unwrap();
10810        let a = dir.path().join("a.toml");
10811        let b = dir.path().join("b.toml");
10812        for (path, text, new_cause) in [
10813            (&a, "[graph", true),
10814            (&a, "[verify", false),
10815            (&a, "[graph]\nimplementers = 'invalid-type'", true),
10816            (&b, "[graph]\nimplementers = 'invalid-type'", true),
10817        ] {
10818            t.release_operator("test", None);
10819            std::fs::write(path, text).unwrap();
10820            let error = Config::discover_fetched(dir.path(), Some(path))
10821                .await
10822                .unwrap_err();
10823            record_config_failure(&mut t, &error);
10824            t.hold_machine(Some(format!("{error:#}")));
10825            let due = t.chat_notice_due();
10826            assert_eq!(due.is_some(), new_cause, "{error:#}");
10827            if let Some(key) = due {
10828                assert_ne!(key, fetch_key);
10829                t.chat_notices.push(key);
10830            }
10831        }
10832        t.succeed();
10833        assert_eq!(t.chat_notice_due().as_deref(), Some("done"));
10834    }
10835
10836    #[test]
10837    fn different_missing_attachments_have_distinct_persisted_notice_causes() {
10838        let dir = tempfile::tempdir().unwrap();
10839        let queue_path = dir.path().join("queue");
10840        let queue = Queue::at(queue_path.clone());
10841        let mut t = task();
10842        t.source = crate::queue::Source::Agent {
10843            run: "originating-chat".into(),
10844            node: crate::queue::CHAT_NODE.into(),
10845        };
10846        let a = dir.path().join("a.png");
10847        let b = dir.path().join("b.png");
10848        std::fs::write(&a, "a").unwrap();
10849        std::fs::write(&b, "b").unwrap();
10850        queue.attach(&mut t, &[a, b]).unwrap();
10851        let paths = task_attachments(&queue, &t).unwrap();
10852        for path in &paths {
10853            std::fs::remove_file(path).unwrap();
10854        }
10855        let missing = task_attachments(&queue, &t).unwrap_err();
10856        t.attempts += 1;
10857        let reason = missing.to_string();
10858        t.fail_startup_target("startup-attachments", missing.name, reason, 1);
10859        let first = t.chat_notice_due().expect("first attachment notice");
10860        t.chat_notices.push(first.clone());
10861        queue.put(&mut t).unwrap();
10862
10863        let queue = Queue::at(queue_path.clone());
10864        let mut t = queue.get(&t.id).unwrap();
10865        assert!(t.chat_notice_due().is_none(), "restart does not replay");
10866        t.release_operator("test", None);
10867        t.attempts += 1;
10868        let missing = task_attachments(&queue, &t).unwrap_err();
10869        t.fail_startup_target(
10870            "startup-attachments",
10871            missing.name,
10872            "same missing attachment with regenerated wording",
10873            1,
10874        );
10875        assert!(t.chat_notice_due().is_none(), "same object stays quiet");
10876
10877        std::fs::write(&paths[0], "restored").unwrap();
10878        t.release_operator("test", None);
10879        t.attempts += 1;
10880        let missing = task_attachments(&queue, &t).unwrap_err();
10881        let reason = missing.to_string();
10882        t.fail_startup_target("startup-attachments", missing.name, reason, 1);
10883        let second = t
10884            .chat_notice_due()
10885            .expect("different attachment must notify");
10886        assert_ne!(first, second);
10887        assert!(t.hold_reason.as_ref().unwrap().contains("b.png"));
10888    }
10889
10890    #[test]
10891    fn resumed_instruction_is_unchanged_without_any_answers() {
10892        let t = task();
10893        assert_eq!(resumed_instruction(&t.instruction, &t), t.instruction);
10894    }
10895
10896    #[test]
10897    fn resumed_instruction_carries_a_new_answer_onto_the_old_run() {
10898        let mut t = task();
10899        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
10900        // The run's own instruction on disk predates the answer: it is the
10901        // plain original text `Runner::start` saved before the operator was
10902        // ever asked anything.
10903        let old = t.instruction.clone();
10904
10905        let refreshed = resumed_instruction(&old, &t);
10906        assert!(refreshed.starts_with(&old), "the original text is kept");
10907        assert!(refreshed.contains("Which backend?"));
10908        assert!(refreshed.contains("SQLite"));
10909    }
10910
10911    #[test]
10912    fn resumed_instruction_keeps_an_original_answers_heading() {
10913        let mut t = task();
10914        t.instruction = "Context\n\n# Operator answers\n\nThis is part of the task.".to_owned();
10915        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
10916
10917        let refreshed = resumed_instruction(&t.instruction, &t);
10918
10919        assert!(
10920            refreshed.starts_with(&t.instruction),
10921            "an answers heading in the original instruction is not the appended block"
10922        );
10923        assert_eq!(refreshed.matches(ANSWERS_HEADER).count(), 2);
10924        assert!(refreshed.contains("Which backend?"));
10925        assert!(refreshed.contains("SQLite"));
10926
10927        let repeated = resumed_instruction(&refreshed, &t);
10928        assert_eq!(
10929            repeated, refreshed,
10930            "only the final appended block is refreshed"
10931        );
10932    }
10933
10934    #[test]
10935    fn resumed_instruction_does_not_duplicate_across_repeated_resumes() {
10936        let mut t = task();
10937        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
10938
10939        // A first resume appends the block; a second resume of the same run,
10940        // with no new answer in between, must reproduce exactly the same
10941        // text rather than appending the block a second time.
10942        let once = resumed_instruction(&t.instruction, &t);
10943        let twice = resumed_instruction(&once, &t);
10944        assert_eq!(once, twice);
10945        assert_eq!(once.matches("Which backend?").count(), 1);
10946
10947        // A later answer replaces the block wholesale rather than growing it.
10948        t.record_answer("Which cache?".to_owned(), "Redis".to_owned());
10949        let refreshed = resumed_instruction(&once, &t);
10950        assert_eq!(refreshed.matches(ANSWERS_HEADER).count(), 1);
10951        assert!(refreshed.contains("Which backend?"));
10952        assert!(refreshed.contains("Which cache?"));
10953    }
10954
10955    #[test]
10956    fn prepare_instruction_covers_all_three_starters() {
10957        let mut t = task();
10958        t.record_answer("Which backend?".to_owned(), "SQLite".to_owned());
10959
10960        // Start: a fresh run gets the task text plus every answer so far —
10961        // exactly `instruction_for`.
10962        assert_eq!(
10963            prepare_instruction(&Starter::Start, None, &t),
10964            Some(instruction_for(&t))
10965        );
10966
10967        // Resume: the run's prior instruction is refreshed with the answer,
10968        // not discarded and not left stale.
10969        let old = t.instruction.clone();
10970        assert_eq!(
10971            prepare_instruction(&Starter::Resume("some-run".to_owned()), Some(&old), &t),
10972            Some(resumed_instruction(&old, &t))
10973        );
10974
10975        // Review: a review-only pass builds its own instruction from the
10976        // branch's history in `crate::graph`, with no task statement at all -
10977        // this boundary must leave it alone.
10978        assert_eq!(
10979            prepare_instruction(&Starter::Review("magi/eba2/A".to_owned()), Some(&old), &t),
10980            None
10981        );
10982    }
10983
10984    #[test]
10985    fn choose_starter_prefers_review_over_resume_when_the_branch_survived() {
10986        assert_eq!(
10987            choose_starter(Some("magi/eba2/A"), true, Some("some-run")),
10988            Starter::Review("magi/eba2/A".to_owned())
10989        );
10990    }
10991
10992    #[test]
10993    fn choose_starter_falls_back_to_start_when_the_review_branch_is_gone() {
10994        assert_eq!(
10995            choose_starter(Some("magi/eba2/A"), false, Some("some-run")),
10996            Starter::Start,
10997            "a vanished review branch must not fall back to resuming the old run either"
10998        );
10999    }
11000
11001    #[test]
11002    fn a_refused_handover_retries_as_a_review_of_the_same_branch() {
11003        let mut t = task();
11004        t.start("old-run".to_owned());
11005        t.hold_for_handover(Some("magi/eba2/A".to_owned()), "checked out".to_owned());
11006        t.release();
11007        let branch = t.review_branch.take();
11008        assert_eq!(
11009            choose_starter(branch.as_deref(), true, Some("old-run")),
11010            Starter::Review("magi/eba2/A".to_owned()),
11011            "a review wins over resuming the old run"
11012        );
11013    }
11014
11015    #[test]
11016    fn choose_starter_resumes_or_starts_when_there_is_no_review_choice_at_all() {
11017        assert_eq!(
11018            choose_starter(None, false, Some("some-run")),
11019            Starter::Resume("some-run".to_owned())
11020        );
11021        assert_eq!(choose_starter(None, false, None), Starter::Start);
11022    }
11023
11024    #[test]
11025    fn an_explicit_release_forces_a_fresh_competition_even_with_a_resumable_run() {
11026        let mut released = task();
11027        released.start("stalled-run".to_owned());
11028        released.requeue();
11029        let unfinished = (!released.fresh_start)
11030            .then(|| Some("stalled-run".to_owned()))
11031            .flatten();
11032        assert_eq!(
11033            choose_starter(None, false, unfinished.as_deref()),
11034            Starter::Start,
11035            "release keeps run history but must not resume it"
11036        );
11037        assert_eq!(released.runs, ["stalled-run"]);
11038    }
11039
11040    fn stopped_public_run(status: RunStatus) -> RunState {
11041        let mut state = run_state(status);
11042        state.driver_exited = true;
11043        state.config.graph.land = true;
11044        state.config.graph.review_rounds = 1;
11045        state.reviews = vec![review_round(1)];
11046        state.merge = Some(crate::run::MergeOutcome {
11047            mode: MergeMode::Pr,
11048            ok: false,
11049            detail: "landing stopped".into(),
11050            empty: false,
11051        });
11052        state.pr = Some(crate::run::PrRecord {
11053            url: "https://example.test/pull/1".into(),
11054            number: 1,
11055            state: "open".into(),
11056            checks: "red".into(),
11057            round: 1,
11058            rounds: 1,
11059            red_at_merge: Vec::new(),
11060        });
11061        state
11062    }
11063
11064    #[test]
11065    fn stopped_public_owner_resumes_on_release_choice_and_control_after_review_exhaustion() {
11066        for status in [RunStatus::Blocked, RunStatus::Gating] {
11067            let home = tempfile::tempdir().unwrap();
11068            let mut state = stopped_public_run(status);
11069            state.save_under(home.path()).unwrap();
11070            assert!(crate::handover::has_resume_checkpoint(&state, home.path()));
11071            assert!(!exhausted_review_budget(&state));
11072            let mut task = held_task_with(&state.id);
11073            task.release();
11074            let unfinished = unfinished_run_with(&task.runs, task.short(), |_| Ok(state.clone()));
11075            assert_eq!(
11076                choose_starter(None, false, unfinished.as_deref()),
11077                Starter::Resume(state.id.clone())
11078            );
11079            let question = action_question(&state.id, resume_action(&state.id));
11080            assert_eq!(
11081                decide_action(&task, &question, &PHRASES_EN, |_| Ok(state.clone())),
11082                ActionDecision::Resume(state.id.clone())
11083            );
11084            // The control-plane command uses this same preflight before pinning.
11085            assert_eq!(
11086                crate::resume::validate(home.path(), &state.id).unwrap().id,
11087                state.id
11088            );
11089            let queue = Queue::at(home.path().join("queue"));
11090            let mut controlled = Task::new(
11091                "resume land".into(),
11092                state.instruction.clone(),
11093                state.repo.clone(),
11094                Source::Agent {
11095                    run: "talk".into(),
11096                    node: crate::queue::CHAT_NODE.into(),
11097                },
11098            );
11099            controlled.runs.push(state.id.clone());
11100            controlled.hold_machine(Some("land stopped".into()));
11101            queue.put(&mut controlled).unwrap();
11102            let resumed =
11103                crate::chat_control::resume(&queue, home.path(), "talk", &state.id).unwrap();
11104            assert_eq!(resumed.status, TaskStatus::Queued);
11105            assert_eq!(
11106                resumed.resume_override.unwrap().pinned_run,
11107                Some(state.id.clone())
11108            );
11109            assert_eq!(resumed.attempts, 0);
11110        }
11111    }
11112
11113    #[test]
11114    fn exhausted_gate_failure_with_clean_or_disabled_reviews_is_not_resumable() {
11115        for review_rounds in [0, 1] {
11116            let home = tempfile::tempdir().unwrap();
11117            let mut state = run_state(RunStatus::Blocked);
11118            state.config.graph.review_rounds = review_rounds;
11119            if review_rounds > 0 {
11120                let mut review = review_round(1);
11121                review.clean = true;
11122                state.reviews = vec![review];
11123            }
11124            state.gate_ran = true;
11125            state.gate = vec![crate::run::CommandOutcome {
11126                command: "verify".into(),
11127                code: Some(1),
11128                output_tail: "gate failed after repair budget exhausted".into(),
11129                duration_ms: 0,
11130                resource_blocked: false,
11131            }];
11132            state.save_under(home.path()).unwrap();
11133            assert!(!crate::handover::has_resume_checkpoint(&state, home.path()));
11134            assert!(exhausted_review_budget(&state));
11135            let mut task = held_task_with(&state.id);
11136            task.release();
11137            assert!(unfinished_run_with(&task.runs, task.short(), |_| Ok(state.clone())).is_none());
11138            let question = action_question(&state.id, resume_action(&state.id));
11139            assert_ne!(
11140                decide_action(&task, &question, &PHRASES_EN, |_| Ok(state.clone())),
11141                ActionDecision::Resume(state.id.clone())
11142            );
11143            assert!(crate::resume::validate(home.path(), &state.id).is_err());
11144
11145            // An unexecuted gate still has work to do with or without reviews.
11146            state.gate_ran = false;
11147            state.gate.clear();
11148            assert!(!exhausted_review_budget(&state));
11149            assert_eq!(
11150                unfinished_run_with(&task.runs, task.short(), |_| Ok(state.clone())),
11151                Some(state.id)
11152            );
11153        }
11154    }
11155
11156    #[test]
11157    fn resource_blocked_verification_resumes_the_same_run() {
11158        for (kind, stage) in [
11159            ("cache", "clean"),
11160            ("coordination", "clean"),
11161            ("cache", "review-e2e"),
11162            ("coordination", "no-reviews"),
11163            ("coordination", "handoff"),
11164        ] {
11165            let home = tempfile::tempdir().unwrap();
11166            let mut state = run_state(RunStatus::Blocked);
11167            state.config.graph.review_rounds = 1;
11168            let mut review = review_round(1);
11169            review.clean = true;
11170            state.reviews = vec![review];
11171            if stage == "review-e2e" {
11172                let review = &mut state.reviews[0];
11173                review.clean = false;
11174                review.blocking = 1;
11175                review.e2e = vec![crate::run::CommandOutcome {
11176                    command: "verify".into(),
11177                    code: None,
11178                    output_tail: "resource wait expired".into(),
11179                    duration_ms: 0,
11180                    resource_blocked: true,
11181                }];
11182            } else if stage == "no-reviews" {
11183                state.config.graph.review_rounds = 0;
11184                state.reviews.clear();
11185            } else if stage == "handoff" {
11186                // A completed final verification can hand off to the gate
11187                // even when the review still carries contested findings.
11188                let review = &mut state.reviews[0];
11189                review.clean = false;
11190                review.blocking = 1;
11191                review.e2e = vec![crate::run::CommandOutcome {
11192                    command: "verify".into(),
11193                    code: Some(0),
11194                    output_tail: String::new(),
11195                    duration_ms: 0,
11196                    resource_blocked: false,
11197                }];
11198            }
11199            state.gate_ran = false;
11200            state.resource_block = Some(crate::run::ResourceBlock {
11201                node: if stage == "review-e2e" {
11202                    "review"
11203                } else {
11204                    "gate"
11205                }
11206                .into(),
11207                kind: kind.into(),
11208                detail: "resource wait expired".into(),
11209                at: jiff::Timestamp::now(),
11210            });
11211            state.save_under(home.path()).unwrap();
11212            assert!(!crate::handover::has_resume_checkpoint(&state, home.path()));
11213            assert!(!exhausted_review_budget(&state));
11214            let mut task = held_task_with(&state.id);
11215            task.release();
11216            let unfinished = unfinished_run_with(&task.runs, task.short(), |_| Ok(state.clone()));
11217            assert_eq!(
11218                choose_starter(None, false, unfinished.as_deref()),
11219                Starter::Resume(state.id.clone())
11220            );
11221            let question = action_question(&state.id, resume_action(&state.id));
11222            assert_eq!(
11223                decide_action(&task, &question, &PHRASES_EN, |_| Ok(state.clone())),
11224                ActionDecision::Resume(state.id.clone())
11225            );
11226            assert_eq!(
11227                crate::resume::validate(home.path(), &state.id).unwrap().id,
11228                state.id
11229            );
11230            let queue = Queue::at(home.path().join("queue"));
11231            let mut controlled = Task::new(
11232                "resume verification".into(),
11233                state.instruction.clone(),
11234                state.repo.clone(),
11235                Source::Agent {
11236                    run: "talk".into(),
11237                    node: crate::queue::CHAT_NODE.into(),
11238                },
11239            );
11240            controlled.runs.push(state.id.clone());
11241            controlled.hold_machine(Some("verification resource unavailable".into()));
11242            queue.put(&mut controlled).unwrap();
11243            let resumed =
11244                crate::chat_control::resume(&queue, home.path(), "talk", &state.id).unwrap();
11245            assert_eq!(resumed.status, TaskStatus::Queued);
11246            assert_eq!(
11247                resumed.resume_override.unwrap().pinned_run,
11248                Some(state.id.clone())
11249            );
11250            assert_eq!(resumed.attempts, 0);
11251
11252            // A genuinely unresolved final review still exhausts the budget.
11253            state.config.graph.review_rounds = 1;
11254            let mut unresolved = review_round(1);
11255            unresolved.blocking = 1;
11256            state.reviews = vec![unresolved];
11257            state.resource_block = None;
11258            state.save_under(home.path()).unwrap();
11259            assert!(exhausted_review_budget(&state));
11260            assert!(crate::resume::validate(home.path(), &state.id).is_err());
11261        }
11262    }
11263
11264    #[tokio::test]
11265    async fn ownership_contention_waits_and_reads_the_settled_record_before_resuming() {
11266        let home = tempfile::tempdir().unwrap();
11267        let mut owner = stopped_public_run(RunStatus::Blocked);
11268        owner.save_under(home.path()).unwrap();
11269        let history = vec![owner.id.clone()];
11270        let repo = owner.repo.clone();
11271        let lock = crate::pr_head::owner_lock(home.path(), &owner.id).unwrap();
11272        let mut lookup = Box::pin(public_resume_owner(home.path(), &history, &repo));
11273        let initial =
11274            std::future::poll_fn(|cx| std::task::Poll::Ready(lookup.as_mut().poll(cx))).await;
11275        assert!(
11276            initial.is_pending(),
11277            "a busy record must not hold or resume the task"
11278        );
11279        owner.retired_to = Some("20260101-000001-bbbb".into());
11280        owner.save_under(home.path()).unwrap();
11281        drop(lock);
11282        let result = tokio::time::timeout(Duration::from_secs(10), lookup)
11283            .await
11284            .expect("ownership lock released")
11285            .expect("read settled owner");
11286        assert!(
11287            result.is_none(),
11288            "the retired owner must not be resumed from a stale read"
11289        );
11290    }
11291
11292    #[tokio::test]
11293    async fn retained_public_owner_wins_over_an_existing_successor_and_unknown_history_holds() {
11294        let home = tempfile::tempdir().unwrap();
11295        let mut owner = stopped_public_run(RunStatus::Blocked);
11296        let repo = tempfile::tempdir().unwrap();
11297        crate::git::git(repo.path(), &["init"]).await.unwrap();
11298        owner.repo = crate::git::toplevel(repo.path()).await.unwrap();
11299        owner.id = "20260101-000000-aaaa".into();
11300        owner.save_under(home.path()).unwrap();
11301        let mut successor = run_state(RunStatus::Blocked);
11302        successor.id = "20260101-000001-bbbb".into();
11303        successor.save_under(home.path()).unwrap();
11304        let runs = vec![owner.id.clone(), successor.id.clone()];
11305        assert_eq!(
11306            public_resume_owner(home.path(), &runs, &owner.repo)
11307                .await
11308                .unwrap()
11309                .unwrap()
11310                .id,
11311            owner.id
11312        );
11313        let subdir = owner.repo.join("nested");
11314        std::fs::create_dir(&subdir).unwrap();
11315        assert_eq!(
11316            public_resume_owner(home.path(), &runs, &subdir)
11317                .await
11318                .unwrap()
11319                .unwrap()
11320                .id,
11321            owner.id
11322        );
11323        #[cfg(unix)]
11324        {
11325            let alias = home.path().join("repo-alias");
11326            std::os::unix::fs::symlink(&owner.repo, &alias).unwrap();
11327            assert_eq!(
11328                public_resume_owner(home.path(), &runs, &alias.join("nested"))
11329                    .await
11330                    .unwrap()
11331                    .unwrap()
11332                    .id,
11333                owner.id
11334            );
11335        }
11336        let different = tempfile::tempdir().unwrap();
11337        crate::git::git(different.path(), &["init"]).await.unwrap();
11338        assert!(
11339            public_resume_owner(home.path(), &runs, different.path())
11340                .await
11341                .is_err()
11342        );
11343        std::fs::remove_file(
11344            home.path()
11345                .join("runs")
11346                .join(&successor.id)
11347                .join("run.json"),
11348        )
11349        .unwrap();
11350        assert!(
11351            public_resume_owner(home.path(), &runs, &owner.repo)
11352                .await
11353                .is_err()
11354        );
11355    }
11356
11357    #[test]
11358    fn an_ordinary_release_keeps_a_resumable_run_available() {
11359        let mut released = task();
11360        released.start("stalled-run".to_owned());
11361        released.release();
11362        let unfinished = (!released.fresh_start)
11363            .then(|| Some("stalled-run".to_owned()))
11364            .flatten();
11365        assert_eq!(
11366            choose_starter(None, false, unfinished.as_deref()),
11367            Starter::Resume("stalled-run".to_owned()),
11368            "manual release must preserve the normal resume path"
11369        );
11370    }
11371
11372    #[test]
11373    fn a_blocked_run_that_spent_every_review_round_has_exhausted_its_budget() {
11374        let mut state = run_state(RunStatus::Blocked);
11375        state.config.graph.review_rounds = 3;
11376        state.reviews = vec![review_round(1), review_round(2), review_round(3)];
11377        assert!(exhausted_review_budget(&state));
11378
11379        // One round still unused: resuming can still ask a reviewer something.
11380        state.reviews.pop();
11381        assert!(!exhausted_review_budget(&state));
11382
11383        // Exhausted rounds on a non-`Blocked` status (a stall, say) do not
11384        // count: only a `Blocked` run re-enters the review loop on resume.
11385        let mut stalled = run_state(RunStatus::Stalled);
11386        stalled.config.graph.review_rounds = 1;
11387        stalled.reviews = vec![review_round(1)];
11388        assert!(!exhausted_review_budget(&stalled));
11389    }
11390
11391    fn review_round(round: usize) -> crate::run::ReviewRound {
11392        crate::run::ReviewRound {
11393            round,
11394            head: "deadbeef".to_owned(),
11395            verified_head: None,
11396            verified_at: None,
11397            reviews: Vec::new(),
11398            e2e: Vec::new(),
11399            verify_retried: false,
11400            e2e_deferred: false,
11401            e2e_defer_reason: None,
11402            fix: None,
11403            blocking: 0,
11404            answered: 1,
11405            expected: 1,
11406            clean: false,
11407            progressed: true,
11408            vote_split: false,
11409            reconsideration: Vec::new(),
11410            verdict: None,
11411        }
11412    }
11413
11414    #[test]
11415    fn awaiting_resume_is_a_failed_task_whose_last_run_parked_and_can_be_resumed() {
11416        let mut run = RunState::new(
11417            PathBuf::from("/repo"),
11418            "main".to_owned(),
11419            "abc1234def".to_owned(),
11420            "add retries".to_owned(),
11421            Config::default(),
11422        );
11423        run.status = RunStatus::Judging;
11424        run.parked = true;
11425        let mut task = Task::new(
11426            "add retries".to_owned(),
11427            "add retries".to_owned(),
11428            PathBuf::from("/repo"),
11429            crate::queue::Source::Human,
11430        );
11431        task.status = TaskStatus::Failed;
11432        task.runs = vec![run.id.clone()];
11433        let with = |t: &Task, r: &RunState| awaiting_resume_with(t, |_| Ok(r.clone()));
11434        assert!(with(&task, &run), "parked after judging is the case");
11435        let mut parked_task = task.clone();
11436        parked_task.status = TaskStatus::Parked;
11437        assert!(with(&parked_task, &run), "the Parked status is the case");
11438
11439        let mut not_parked = run.clone();
11440        not_parked.parked = false;
11441        not_parked.status = RunStatus::Stalled;
11442        assert!(!with(&task, &not_parked), "a stall is the conductor's");
11443
11444        let mut fresh = task.clone();
11445        fresh.fresh_start = true;
11446        assert!(!with(&fresh, &run), "a requeue asked for a new competition");
11447
11448        let mut review = task.clone();
11449        review.review_branch = Some("magi/x/A".to_owned());
11450        assert!(!with(&review, &run), "review is ranked before resume");
11451
11452        let mut held = task.clone();
11453        held.status = TaskStatus::Held;
11454        assert!(!with(&held, &run), "a hold stays visible to the conductor");
11455
11456        let mut released = run.clone();
11457        released.released_to = Some("20260901-000000-new1".to_owned());
11458        assert!(!with(&task, &released), "nothing left to resume into");
11459
11460        assert!(!awaiting_resume_with(&task, |_| anyhow::bail!(
11461            "unreadable"
11462        )));
11463    }
11464
11465    #[test]
11466    fn unfinished_run_never_offers_a_run_whose_worktree_was_released() {
11467        let mut released = RunState::new(
11468            PathBuf::from("/repo"),
11469            "main".to_owned(),
11470            "abc1234def".to_owned(),
11471            "add retries".to_owned(),
11472            Config::default(),
11473        );
11474        released.status = RunStatus::Blocked;
11475        assert_eq!(
11476            unfinished_run_with(&[released.id.clone()], "t", |_| Ok(released.clone())),
11477            Some(released.id.clone())
11478        );
11479        released.released_to = Some("20260901-000000-new1".to_owned());
11480        assert_eq!(
11481            unfinished_run_with(&[released.id.clone()], "t", |_| Ok(released.clone())),
11482            None,
11483            "there is nothing left to resume it into"
11484        );
11485    }
11486
11487    #[test]
11488    fn unfinished_run_skips_a_round_exhausted_blocked_run_so_requeue_means_a_fresh_competition() {
11489        // Mirrors the failure this exists to close: a task's last run ended
11490        // `Blocked` with the review budget spent, `crate::conduct` chose
11491        // `Recovery::Requeue` (`Task::release`, which keeps `runs` as
11492        // evidence), and without this check `attempt` would go on treating
11493        // that exhausted run as "unfinished" and resume it - `graph::Runner`'s
11494        // review loop iterates zero times over an already-spent budget, so
11495        // the resumed run settles right back to `Blocked` having asked nobody
11496        // anything, and `Requeue`'s promised fresh competition never happens.
11497        let mut exhausted = RunState::new(
11498            PathBuf::from("/repo"),
11499            "main".to_owned(),
11500            "abc1234def".to_owned(),
11501            "add retries".to_owned(),
11502            Config::default(),
11503        );
11504        exhausted.status = RunStatus::Blocked;
11505        exhausted.config.graph.review_rounds = 1;
11506        exhausted.reviews = vec![review_round(1)];
11507
11508        assert_eq!(
11509            unfinished_run_with(&[exhausted.id.clone()], "t", |_| Ok(exhausted.clone())),
11510            None,
11511            "an exhausted `Blocked` run must not be offered as resumable"
11512        );
11513
11514        // A `Blocked` run with rounds still unused is genuinely worth
11515        // resuming, and must still be found.
11516        let mut has_budget_left = RunState::new(
11517            PathBuf::from("/repo"),
11518            "main".to_owned(),
11519            "abc1234def".to_owned(),
11520            "add retries".to_owned(),
11521            Config::default(),
11522        );
11523        has_budget_left.status = RunStatus::Blocked;
11524        has_budget_left.config.graph.review_rounds = 3;
11525        has_budget_left.reviews = vec![review_round(1)];
11526
11527        assert_eq!(
11528            unfinished_run_with(&[has_budget_left.id.clone()], "t", |_| {
11529                Ok(has_budget_left.clone())
11530            }),
11531            Some(has_budget_left.id.clone())
11532        );
11533    }
11534
11535    #[test]
11536    fn unfinished_run_resumes_a_run_whose_task_head_publication_is_pending() {
11537        let mut run = RunState::new(
11538            PathBuf::from("/repo"),
11539            "main".to_owned(),
11540            "abc1234def".to_owned(),
11541            "add retries".to_owned(),
11542            Config::default(),
11543        );
11544        run.status = RunStatus::Blocked;
11545        run.config.graph.review_rounds = 1;
11546        run.reviews = vec![review_round(1)];
11547        run.pr_publication_pending = true;
11548        assert_eq!(
11549            unfinished_run_with(&[run.id.clone()], "t", |_| Ok(run.clone())),
11550            Some(run.id.clone()),
11551            "a spent review budget must not turn a failed publication into a re-competition"
11552        );
11553    }
11554
11555    #[test]
11556    fn retained_attempt_bound_survives_changed_bases_releases_and_duplicate_history() {
11557        let mut task = Task::new(
11558            "recover work".into(),
11559            "recover work".into(),
11560            PathBuf::from("/repo"),
11561            crate::queue::Source::Human,
11562        );
11563        let mut history = std::collections::BTreeMap::new();
11564        for index in 0..8 {
11565            let mut run = run_state(RunStatus::Blocked);
11566            run.id = format!("20260101-000000-{index:04}");
11567            run.base_commit = format!("upstream-{index}");
11568            // Seven empty newer attempts must not erase the old recovery owner.
11569            if index > 0 {
11570                run.candidates.clear();
11571            }
11572            task.runs.extend([run.id.clone(), run.id.clone()]);
11573            history.insert(run.id.clone(), run);
11574            task.requeue();
11575        }
11576        let check = |task: &Task, starter: &Starter, limit| {
11577            retained_attempt_hold(task, starter, limit, |id| Ok(history[id].clone()))
11578        };
11579        assert!(
11580            check(&task, &Starter::Start, 8)
11581                .unwrap()
11582                .contains("8 unretired")
11583        );
11584        assert!(check(&task, &Starter::Start, 9).is_none());
11585        assert!(check(&task, &Starter::Review("existing".into()), 8).is_some());
11586        task.release_operator("owner", None);
11587        assert_eq!(task.attempts, 0);
11588        let restored: Task = serde_json::from_slice(&serde_json::to_vec(&task).unwrap()).unwrap();
11589        assert!(check(&restored, &Starter::Start, 8).is_some());
11590        assert!(check(&restored, &Starter::Resume(task.runs[0].clone()), 8).is_none());
11591        assert!(check(&restored, &Starter::Start, 0).is_none());
11592    }
11593
11594    #[test]
11595    fn retained_attempt_bound_requires_verified_retirement_not_completion_labels() {
11596        let mut task = Task::new(
11597            "recover work".into(),
11598            "recover work".into(),
11599            PathBuf::from("/repo"),
11600            crate::queue::Source::Human,
11601        );
11602        let mut run = run_state(RunStatus::Superseded);
11603        run.candidates.clear();
11604        task.runs.push(run.id.clone());
11605        assert!(retained_attempt_hold(&task, &Starter::Start, 1, |_| Ok(run.clone())).is_some());
11606        run.retired_to = Some("authority".into());
11607        assert!(retained_attempt_hold(&task, &Starter::Start, 1, |_| Ok(run.clone())).is_none());
11608        run.retired_to = None;
11609        run.released_to = Some("authority".into());
11610        assert!(retained_attempt_hold(&task, &Starter::Start, 1, |_| Ok(run.clone())).is_none());
11611        assert!(
11612            retained_attempt_hold(&task, &Starter::Start, 8, |_| anyhow::bail!("unreadable"))
11613                .unwrap()
11614                .contains("Cannot verify")
11615        );
11616        // Resuming an existing owner needs no new inventory or worktree budget.
11617        assert!(
11618            retained_attempt_hold(&task, &Starter::Resume(run.id), 1, |_| {
11619                panic!("resume must not load historical attempts")
11620            })
11621            .is_none()
11622        );
11623    }
11624
11625    #[test]
11626    fn retired_resume_requires_a_valid_same_repository_authority() {
11627        let mut source = run_state(RunStatus::Blocked);
11628        let mut empty = run_state(RunStatus::Blocked);
11629        empty.retired_to = Some(source.id.clone());
11630        let runs = vec![source.id.clone(), empty.id.clone()];
11631        let resolve = |source: &RunState, empty: &RunState| {
11632            unfinished_run_with(&runs, "fixture", |id| {
11633                if id == source.id {
11634                    Ok(source.clone())
11635                } else if id == empty.id {
11636                    Ok(empty.clone())
11637                } else {
11638                    bail!("unreadable authority")
11639                }
11640            })
11641        };
11642        assert_eq!(resolve(&source, &empty), Some(source.id.clone()));
11643        assert_eq!(
11644            choose_starter(None, false, resolve(&source, &empty).as_deref()),
11645            Starter::Resume(source.id.clone())
11646        );
11647        source.retired_to = Some(empty.id.clone());
11648        assert!(
11649            resolve(&source, &empty).is_none(),
11650            "cycles do not revive obsolete runs"
11651        );
11652        source.retired_to = None;
11653        source.repo = PathBuf::from("/another-repository");
11654        assert!(resolve(&source, &empty).is_none());
11655        source.repo = empty.repo.clone();
11656        source.released_to = Some("later".into());
11657        assert!(resolve(&source, &empty).is_none());
11658        empty.retired_to = Some("unlisted-run".into());
11659        assert!(resolve(&source, &empty).is_none());
11660    }
11661
11662    #[test]
11663    fn unfinished_run_never_falls_back_to_an_older_resumable_run() {
11664        // A task whose history holds an *older* run that still looks
11665        // resumable (say, a competition `Runner::review` was started
11666        // alongside after that older run went `Stalled`) and a *newest* run
11667        // that is `Blocked` with its review budget spent. `Recovery::Requeue`
11668        // on this task must mean a fresh competition — falling back to the
11669        // stale, superseded `Stalled` run instead would resurrect history
11670        // nothing asked to revisit and silently defeat the requeue.
11671        let mut older_stalled = RunState::new(
11672            PathBuf::from("/repo"),
11673            "main".to_owned(),
11674            "abc1234def".to_owned(),
11675            "add retries".to_owned(),
11676            Config::default(),
11677        );
11678        older_stalled.status = RunStatus::Stalled;
11679
11680        let mut newest_exhausted = RunState::new(
11681            PathBuf::from("/repo"),
11682            "main".to_owned(),
11683            "abc1234def".to_owned(),
11684            "add retries".to_owned(),
11685            Config::default(),
11686        );
11687        newest_exhausted.status = RunStatus::Blocked;
11688        newest_exhausted.config.graph.review_rounds = 1;
11689        newest_exhausted.reviews = vec![review_round(1)];
11690
11691        assert_eq!(
11692            unfinished_run_with(
11693                &[older_stalled.id.clone(), newest_exhausted.id.clone()],
11694                "t",
11695                |_| Ok(newest_exhausted.clone())
11696            ),
11697            None,
11698            "the newest run is exhausted, so nothing here is worth resuming - \
11699             least of all the older, already-superseded run"
11700        );
11701    }
11702
11703    #[test]
11704    fn unfinished_run_warns_and_skips_a_run_it_cannot_read() {
11705        assert_eq!(
11706            unfinished_run_with(&["20260101-000000-gone".to_owned()], "t", |_| {
11707                Err(anyhow::anyhow!("fixture is absent"))
11708            }),
11709            None
11710        );
11711    }
11712
11713    fn action_question(run: &str, action: ask::ChoiceAction) -> ask::Question {
11714        let mut q = ask::Question::new(
11715            run.to_owned(),
11716            "implement".to_owned(),
11717            "impl-A".to_owned(),
11718            "continue?".to_owned(),
11719            String::new(),
11720            vec!["resume で続行する".to_owned(), "other".to_owned()],
11721        );
11722        q.actions.insert("resume で続行する".to_owned(), action);
11723        q.answer(ask::Answer::Choice("resume で続行する".to_owned()))
11724            .unwrap();
11725        q
11726    }
11727
11728    fn held_task_with(run: &str) -> Task {
11729        let mut t = task();
11730        t.runs = vec![run.to_owned()];
11731        t.hold_machine(Some("waiting for magi resume to be executed".to_owned()));
11732        t
11733    }
11734
11735    fn resume_action(run: &str) -> ask::ChoiceAction {
11736        ask::ChoiceAction::Resume { run: run.into() }
11737    }
11738
11739    #[test]
11740    fn explicitly_resumed_older_run_remains_the_release_and_question_target() {
11741        for parked in [false, true] {
11742            let mut t = task();
11743            t.start("A".into());
11744            t.start("B".into());
11745            assert_eq!(t.runs, vec!["A", "B"]);
11746            t.start("A".into());
11747            let mut resumed = run_state(RunStatus::Blocked);
11748            resumed.id = "A".into();
11749            resumed.parked = parked;
11750            resumed.driver_exited = true;
11751            t.hold_machine(Some("resumed run stopped".into()));
11752            let q = action_question("A", resume_action("A"));
11753            assert_eq!(action_standing(&t, &q), ActionStanding::Pending);
11754            assert_eq!(
11755                decide_action(&t, &q, &PHRASES_EN, |id| {
11756                    assert_eq!(id, "A");
11757                    Ok(resumed.clone())
11758                }),
11759                ActionDecision::Resume("A".into())
11760            );
11761            assert_eq!(
11762                action_standing(&t, &action_question("B", resume_action("B"))),
11763                ActionStanding::Stale
11764            );
11765            t.release_operator("cli", None);
11766            let unfinished = unfinished_run_with(&t.runs, t.short(), |id| {
11767                assert_eq!(id, "A");
11768                Ok(resumed.clone())
11769            });
11770            assert_eq!(
11771                choose_starter(None, false, unfinished.as_deref()),
11772                Starter::Resume("A".into())
11773            );
11774            assert_eq!(t.runs, vec!["B", "A"]);
11775        }
11776    }
11777
11778    #[test]
11779    fn merge_repair_dispatch_refuses_obsolete_bindings_and_unsupported_owners() {
11780        let mut state = crate::land::tests::repair_state(true, true);
11781        state.status = RunStatus::Landing;
11782        state.parked = true;
11783        state.driver_exited = true;
11784        let mut q = ask::Question::new(
11785            state.id.clone(),
11786            land::APPROVAL_NODE.into(),
11787            "land".into(),
11788            "Merge?".into(),
11789            String::new(),
11790            vec![land::APPROVE.into(), land::HOLD.into()],
11791        );
11792        q.id = state.id.clone();
11793        state.land_approval = Some(crate::run::LandApproval {
11794            question: q.id.clone(),
11795            head: "current-head".into(),
11796        });
11797        assert!(land::upgrade_repair_choice(&mut q, &state));
11798        q.answer(ask::Answer::Choice(land::REPAIR.into())).unwrap();
11799        let mut task = held_task_with(&state.id);
11800        task.status = TaskStatus::Parked;
11801        assert_eq!(
11802            decide_action(&task, &q, &PHRASES_EN, |_| Ok(state.clone())),
11803            ActionDecision::Resume(state.id.clone())
11804        );
11805        q.asked_at = "2026-01-01T00:00:00Z".parse().unwrap();
11806        q.answered_at = Some("2026-01-01T00:00:20Z".parse().unwrap());
11807        task.operator_decided_at = Some("2026-01-01T00:00:10Z".parse().unwrap());
11808        assert_eq!(
11809            decide_action(&task, &q, &PHRASES_EN, |_| Ok(state.clone())),
11810            ActionDecision::Resume(state.id.clone()),
11811            "a repair answer newer than the manual hold resumes the existing owner"
11812        );
11813        task.operator_decided_at = Some("2026-01-01T00:00:30Z".parse().unwrap());
11814        for answered_at in [q.answered_at, None] {
11815            let mut older = q.clone();
11816            older.answered_at = answered_at;
11817            let refused = decide_action(&task, &older, &PHRASES_EN, |_| {
11818                panic!("a later manual hold must prevent dispatch")
11819            });
11820            let ActionDecision::Refuse(reason) = refused else {
11821                panic!("expected an actionable refusal, got {refused:?}");
11822            };
11823            assert!(reason.contains("later operator decision"));
11824            assert!(reason.contains(&format!("magi task release {}", task.id)));
11825        }
11826        task.operator_decided_at = None;
11827        let mut superseded = state.clone();
11828        superseded.land_approval.as_mut().unwrap().question = "fresh-decision".into();
11829        assert_eq!(
11830            decide_action(&task, &q, &PHRASES_EN, |_| Ok(superseded)),
11831            ActionDecision::Stale
11832        );
11833        for case in 1..4 {
11834            let mut changed = state.clone();
11835            match case {
11836                1 => changed.status = RunStatus::Blocked,
11837                2 => changed.released_to = Some("new-owner".into()),
11838                _ => changed.land_approval.as_mut().unwrap().head.clear(),
11839            }
11840            assert!(matches!(
11841                decide_action(&task, &q, &PHRASES_EN, |_| Ok(changed)),
11842                ActionDecision::Refuse(_)
11843            ));
11844        }
11845        assert!(matches!(
11846            decide_action(&task, &q, &PHRASES_EN, |_| anyhow::bail!("missing owner")),
11847            ActionDecision::Refuse(_)
11848        ));
11849        task.runs.push("replacement".into());
11850        task.operator_decided_at = Some("2026-01-01T00:00:30Z".parse().unwrap());
11851        assert_eq!(
11852            decide_action(&task, &q, &PHRASES_EN, |_| panic!(
11853                "old owner must not load"
11854            )),
11855            ActionDecision::Stale
11856        );
11857    }
11858
11859    #[test]
11860    fn merge_repair_dispatch_obeys_capacity_and_quota_while_hold_stays_decision_only() {
11861        let mut state = crate::land::tests::repair_state(true, true);
11862        state.status = RunStatus::Landing;
11863        state.parked = true;
11864        state.driver_exited = true;
11865        let mut q = ask::Question::new(
11866            state.id.clone(),
11867            land::APPROVAL_NODE.into(),
11868            "land".into(),
11869            "Merge?".into(),
11870            String::new(),
11871            vec![land::APPROVE.into(), land::HOLD.into()],
11872        );
11873        q.id = state.id.clone();
11874        state.land_approval = Some(crate::run::LandApproval {
11875            question: q.id.clone(),
11876            head: "current-head".into(),
11877        });
11878        state.save().unwrap();
11879        land::upgrade_repair_choice(&mut q, &state);
11880        q.answer(ask::Answer::Choice(land::REPAIR.into())).unwrap();
11881        Questions::open().put(&mut q).unwrap();
11882        let tmp = tempfile::tempdir().unwrap();
11883        let queue = Queue::at(tmp.path().join("queue"));
11884        let mut task = held_task_with(&state.id);
11885        task.status = TaskStatus::Parked;
11886        queue.put(&mut task).unwrap();
11887        let empty = Arc::new(tokio::sync::Semaphore::new(0));
11888        let free = Arc::new(tokio::sync::Semaphore::new(1));
11889        assert!(claim_dispatch(&queue, &task, false, &empty, &free).is_none());
11890        assert!(claim_dispatch(&queue, &task, true, &free, &free).is_none());
11891        let claim = claim_dispatch(&queue, &task, false, &free, &free).unwrap();
11892        assert!(claim.permit.is_some());
11893        assert!(claim_dispatch(&queue, &task, false, &free, &free).is_none());
11894        drop(claim);
11895        Questions::open()
11896            .update(&q.id, |q| {
11897                q.answer = Some(ask::Answer::Choice(land::HOLD.into()));
11898                Ok(())
11899            })
11900            .unwrap();
11901        let claim = claim_dispatch(&queue, &task, true, &empty, &empty).unwrap();
11902        assert!(claim.permit.is_none());
11903        assert!(RunState::load(&state.id).unwrap().merge_repairs.is_empty());
11904    }
11905
11906    #[test]
11907    fn decide_action_resumes_only_the_latest_resumable_run() {
11908        let t = held_task_with("r1");
11909        let q = action_question("r1", resume_action("r1"));
11910        let load = |s: RunState| move |_: &str| Ok(s);
11911        assert_eq!(
11912            decide_action(&t, &q, &PHRASES_EN, load(run_state(RunStatus::Blocked))),
11913            ActionDecision::Resume("r1".into())
11914        );
11915        // Not the latest run of the task.
11916        let q_other = action_question("r1", resume_action("r0"));
11917        assert!(matches!(
11918            decide_action(
11919                &t,
11920                &q_other,
11921                &PHRASES_EN,
11922                load(run_state(RunStatus::Blocked))
11923            ),
11924            ActionDecision::Refuse(_)
11925        ));
11926        // A finished run cannot make progress.
11927        assert!(matches!(
11928            decide_action(&t, &q, &PHRASES_EN, load(run_state(RunStatus::Ready))),
11929            ActionDecision::Refuse(_)
11930        ));
11931        // A released one either.
11932        let mut released = run_state(RunStatus::Blocked);
11933        released.released_to = Some("elsewhere".into());
11934        assert!(matches!(
11935            decide_action(&t, &q, &PHRASES_EN, load(released)),
11936            ActionDecision::Refuse(_)
11937        ));
11938        // Unreadable state is a refusal, never a fresh competition.
11939        assert!(matches!(
11940            decide_action(&t, &q, &PHRASES_EN, |_: &str| bail!("gone")),
11941            ActionDecision::Refuse(_)
11942        ));
11943    }
11944
11945    #[test]
11946    fn decide_action_ignores_a_question_about_an_earlier_run() {
11947        let mut t = held_task_with("r1");
11948        t.runs.push("r2".to_owned());
11949        let never = |_: &str| -> Result<RunState> { bail!("not read") };
11950        assert_eq!(
11951            decide_action(
11952                &t,
11953                &action_question("r1", ask::ChoiceAction::Done),
11954                &PHRASES_EN,
11955                never
11956            ),
11957            ActionDecision::Stale
11958        );
11959    }
11960
11961    #[test]
11962    fn decide_action_maps_requeue_and_done_and_never_acts_twice() {
11963        let mut t = held_task_with("r1");
11964        let never = |_: &str| -> Result<RunState> { bail!("not read") };
11965        assert_eq!(
11966            decide_action(
11967                &t,
11968                &action_question("r1", ask::ChoiceAction::Requeue),
11969                &PHRASES_EN,
11970                never
11971            ),
11972            ActionDecision::Requeue
11973        );
11974        let done_q = action_question("r1", ask::ChoiceAction::Done);
11975        assert_eq!(
11976            decide_action(&t, &done_q, &PHRASES_EN, never),
11977            ActionDecision::Done
11978        );
11979        t.mark_action_applied(&done_q.id);
11980        assert_eq!(
11981            decide_action(&t, &done_q, &PHRASES_EN, never),
11982            ActionDecision::Skip
11983        );
11984
11985        // A plain answer (no action for that label) does nothing.
11986        let mut plain = action_question("r1", ask::ChoiceAction::Done);
11987        plain.actions.clear();
11988        assert_eq!(
11989            decide_action(&held_task_with("r1"), &plain, &PHRASES_EN, never),
11990            ActionDecision::Skip
11991        );
11992        // A running task is left alone.
11993        let mut running = held_task_with("r1");
11994        running.status = TaskStatus::Running;
11995        assert_eq!(
11996            decide_action(
11997                &running,
11998                &action_question("r1", ask::ChoiceAction::Done),
11999                &PHRASES_EN,
12000                never
12001            ),
12002            ActionDecision::Skip
12003        );
12004    }
12005
12006    #[test]
12007    fn merge_repair_rejected_after_manual_hold_keeps_the_conductor_guard() {
12008        crate::run::pin_test_home();
12009        for legacy in [false, true] {
12010            let dir = tempfile::tempdir().unwrap();
12011            let queue = Queue::at(dir.path().join("queue"));
12012            let questions = Questions::at(dir.path().join("questions"));
12013            let run = "20260101-000000-hold";
12014            let mut task = held_task_with(run);
12015            task.status = TaskStatus::Parked;
12016            let mut q = ask::Question::new(
12017                run.into(),
12018                land::APPROVAL_NODE.into(),
12019                "land".into(),
12020                "Repair?".into(),
12021                String::new(),
12022                vec![land::REPAIR.into()],
12023            );
12024            q.actions.insert(land::REPAIR.into(), resume_action(run));
12025            q.answer(ask::Answer::Choice(land::REPAIR.into())).unwrap();
12026            // Deterministically order the answer before the actual manual hold.
12027            q.asked_at = "2026-01-01T00:00:00Z".parse().unwrap();
12028            q.answered_at = Some("2026-01-01T00:00:01Z".parse().unwrap());
12029            questions.put(&mut q).unwrap();
12030            task.hold_manual(Some("Keep this PR paused".into()));
12031            if legacy {
12032                task.hold_source = None;
12033            }
12034            queue.put(&mut task).unwrap();
12035
12036            apply_task_choice_actions(&queue, &questions, dir.path());
12037            let after = queue.get(&task.id).unwrap();
12038            assert!(after.operator_held());
12039            assert_eq!(after.hold_source, task.hold_source);
12040            assert_eq!(after.operator_decided_at, task.operator_decided_at);
12041            assert_eq!(after.runs, task.runs);
12042            assert_eq!(after.instruction, task.instruction);
12043            assert!(after.resume_override.is_none());
12044            assert!(after.action_applied(&q.id));
12045            let reason = after.hold_reason.as_deref().unwrap();
12046            assert!(reason.starts_with("Keep this PR paused"));
12047            assert!(reason.contains("later operator decision"));
12048            assert!(reason.contains(&format!("magi task release {}", task.id)));
12049
12050            for recovery in [
12051                crate::conduct::Recovery::Review,
12052                crate::conduct::Recovery::Requeue,
12053            ] {
12054                crate::conduct::apply(
12055                    &queue,
12056                    &questions,
12057                    &crate::conduct::Verdict {
12058                        decisions: vec![crate::conduct::Decision {
12059                            id: task.id.clone(),
12060                            recovery: Some(recovery),
12061                            ..Default::default()
12062                        }],
12063                    },
12064                )
12065                .unwrap();
12066            }
12067            // Reconciliation is idempotent, and automatic recovery cannot
12068            // reopen this owner after it observes the rejected repair answer.
12069            apply_task_choice_actions(&queue, &questions, dir.path());
12070            let still_held = queue.get(&task.id).unwrap();
12071            assert!(still_held.operator_held());
12072            assert_eq!(still_held.hold_reason, after.hold_reason);
12073            assert_eq!(still_held.runs, task.runs);
12074            assert!(still_held.review_branch.is_none());
12075            assert!(!still_held.fresh_start);
12076            assert!(queue.next_runnable().is_none());
12077        }
12078    }
12079
12080    #[test]
12081    fn conductor_driver_choices_apply_from_both_queued_and_held_tasks() {
12082        for held in [false, true] {
12083            for retry in [false, true] {
12084                let dir = tempfile::tempdir().unwrap();
12085                let queue = Queue::at(dir.path().join("queue"));
12086                let questions = Questions::at(dir.path().join("questions"));
12087                let home = dir.path().join("home");
12088                let mut t = task();
12089                if held {
12090                    t.hold_machine(Some("driver state unknown".into()));
12091                }
12092                queue.put(&mut t).unwrap();
12093                let verdict = serde_json::from_value(serde_json::json!({
12094                    "decisions": [{
12095                        "id": t.id,
12096                        "question": "Driver state unknown; retry the checks or hold?",
12097                        "choices": ["daemon: recheck and retry", "daemon: hold the task"],
12098                        "actions": {
12099                            "daemon: recheck and retry": {"do": "requeue"},
12100                            "daemon: hold the task": {"do": "hold"}
12101                        }
12102                    }]
12103                }))
12104                .unwrap();
12105                crate::conduct::apply(&queue, &questions, &verdict).unwrap();
12106                assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Blocked);
12107                let mut q = questions.list().pop().unwrap();
12108                let choice = if retry {
12109                    "daemon: recheck and retry"
12110                } else {
12111                    "daemon: hold the task"
12112                };
12113                assert_eq!(q.actions.len(), 2);
12114                q.answer(ask::Answer::Choice(choice.into())).unwrap();
12115                questions.put(&mut q).unwrap();
12116
12117                // A Web answer does not remove the deputy's fresh lease.
12118                questions.beat(&q.id, ask::WaiterKind::Deputy);
12119                for _ in 0..2 {
12120                    resolve_blockers(&queue, &questions);
12121                    apply_task_choice_actions(&queue, &questions, &home);
12122                    let waiting = queue.get(&t.id).unwrap();
12123                    assert_eq!(waiting.status, TaskStatus::Blocked);
12124                    assert_eq!(waiting.blocked_by, [q.id.clone()]);
12125                    assert!(waiting.answers.is_empty());
12126                    assert!(!waiting.action_applied(&q.id));
12127                    assert!(runnable(&queue).is_empty());
12128                    let free = Arc::new(tokio::sync::Semaphore::new(1));
12129                    assert!(claim_dispatch(&queue, &t, false, &free, &free).is_none());
12130                }
12131                std::fs::remove_file(questions.root().join(format!("{}.lease", q.id))).unwrap();
12132
12133                // Releasing the blocker and applying the operation share one save.
12134                resolve_blockers(&queue, &questions);
12135                assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Blocked);
12136                apply_task_choice_actions(&queue, &questions, &home);
12137                let after = queue.get(&t.id).unwrap();
12138                assert_eq!(
12139                    after.status,
12140                    if retry {
12141                        TaskStatus::Queued
12142                    } else {
12143                        TaskStatus::Held
12144                    }
12145                );
12146                assert!(after.action_applied(&q.id));
12147                assert_eq!(after.operator_held(), !retry);
12148                assert_eq!(after.answers.len(), 1);
12149
12150                // A later operator decision must not replay this answer.
12151                let mut changed = after;
12152                if retry {
12153                    changed.hold_manual(Some("later hold".into()));
12154                } else {
12155                    changed.release_operator("test", None);
12156                }
12157                let expected = changed.status;
12158                queue.put(&mut changed).unwrap();
12159                apply_task_choice_actions(&queue, &questions, &home);
12160                assert_eq!(queue.get(&t.id).unwrap().status, expected);
12161            }
12162        }
12163    }
12164
12165    #[test]
12166    fn apply_choice_actions_releases_the_task_pinned_to_its_run_and_only_once() {
12167        let dir = tempfile::tempdir().unwrap();
12168        let queue = Queue::at(dir.path().join("queue"));
12169        let questions = Questions::at(dir.path().join("questions"));
12170        let home = dir.path().join("home");
12171        let mut state = run_state(RunStatus::Blocked);
12172        state.id = "20260101-000000-act1".to_owned();
12173        state.save_under(&home).unwrap();
12174
12175        let mut t = held_task_with(&state.id);
12176        queue.put(&mut t).unwrap();
12177        let mut q = action_question(&state.id, resume_action(&state.id));
12178        questions.put(&mut q).unwrap();
12179
12180        apply_task_choice_actions(&queue, &questions, &home);
12181        let after = queue.get(&t.id).unwrap();
12182        assert_eq!(after.status, TaskStatus::Queued);
12183        assert!(!after.fresh_start);
12184        assert!(after.action_applied(&q.id));
12185        let pin = after.resume_override.clone().unwrap();
12186        assert_eq!(pin.pinned_run.as_deref(), Some(state.id.as_str()));
12187        assert!(pin.forced);
12188
12189        // Held again later: the same answer does not release it a second time.
12190        let mut again = queue.get(&t.id).unwrap();
12191        again.hold_machine(Some("later".into()));
12192        queue.put(&mut again).unwrap();
12193        apply_task_choice_actions(&queue, &questions, &home);
12194        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
12195    }
12196
12197    #[test]
12198    fn a_delivered_answer_is_still_acted_on_exactly_once() {
12199        let dir = tempfile::tempdir().unwrap();
12200        let queue = Queue::at(dir.path().join("queue"));
12201        let questions = Questions::at(dir.path().join("questions"));
12202        let home = dir.path().join("home");
12203        let mut state = run_state(RunStatus::Blocked);
12204        state.id = "20260101-000000-act2".to_owned();
12205        state.save_under(&home).unwrap();
12206
12207        let mut t = held_task_with(&state.id);
12208        queue.put(&mut t).unwrap();
12209        let mut q = action_question(&state.id, resume_action(&state.id));
12210        q.answer_delivered = true;
12211        questions.put(&mut q).unwrap();
12212
12213        // Delivered, and the agent then died: the task is still held, so the
12214        // chosen action applies.
12215        apply_task_choice_actions(&queue, &questions, &home);
12216        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Queued);
12217        assert!(queue.get(&t.id).unwrap().action_applied(&q.id));
12218
12219        // Held again: the same answer does nothing a second time.
12220        let mut again = queue.get(&t.id).unwrap();
12221        again.hold_machine(Some("later".into()));
12222        queue.put(&mut again).unwrap();
12223        apply_task_choice_actions(&queue, &questions, &home);
12224        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
12225    }
12226
12227    #[test]
12228    fn a_fresh_asker_defers_the_action_until_it_goes_quiet() {
12229        let dir = tempfile::tempdir().unwrap();
12230        let queue = Queue::at(dir.path().join("queue"));
12231        let questions = Questions::at(dir.path().join("questions"));
12232        let home = dir.path().join("home");
12233        let mut state = run_state(RunStatus::Blocked);
12234        state.id = "20260101-000000-act3".to_owned();
12235        state.save_under(&home).unwrap();
12236        let mut t = held_task_with(&state.id);
12237        queue.put(&mut t).unwrap();
12238        let mut q = action_question(&state.id, ask::ChoiceAction::Requeue);
12239        questions.put(&mut q).unwrap();
12240
12241        questions.beat(&q.id, crate::ask::WaiterKind::Asker);
12242        apply_task_choice_actions(&queue, &questions, &home);
12243        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
12244
12245        std::fs::remove_file(questions.root().join(format!("{}.lease", q.id))).unwrap();
12246        apply_task_choice_actions(&queue, &questions, &home);
12247        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Queued);
12248    }
12249
12250    #[test]
12251    fn action_standing_tells_the_reasons_apart() {
12252        let mut t = held_task_with("r1");
12253        let q = action_question("r1", ask::ChoiceAction::Requeue);
12254        assert_eq!(action_standing(&t, &q), ActionStanding::Pending);
12255        let mut plain = q.clone();
12256        plain.actions.clear();
12257        assert_eq!(action_standing(&t, &plain), ActionStanding::NoAction);
12258
12259        // Applied wins over everything, even a running task.
12260        t.mark_action_applied(&q.id);
12261        assert_eq!(action_standing(&t, &q), ActionStanding::Applied);
12262
12263        // A newer attempt running: the old question is Stale, not Busy.
12264        let mut t = held_task_with("r1");
12265        t.start("r2".to_owned());
12266        assert_eq!(action_standing(&t, &q), ActionStanding::Stale);
12267        // The same question about the latest run just waits.
12268        let q2 = action_question("r2", ask::ChoiceAction::Requeue);
12269        assert_eq!(action_standing(&t, &q2), ActionStanding::Busy);
12270        t.status = TaskStatus::Blocked;
12271        assert_eq!(action_standing(&t, &q2), ActionStanding::Busy);
12272
12273        // A conductor question is keyed by the task, never stale.
12274        let mut c = action_question("r0", ask::ChoiceAction::Requeue);
12275        c.node = crate::conduct::NODE.to_owned();
12276        assert_eq!(action_standing(&t, &c), ActionStanding::Busy);
12277        assert!(!ActionStanding::Busy.daemon_owns());
12278    }
12279
12280    #[test]
12281    fn a_running_task_waits_and_a_dead_asker_with_a_cwd_is_still_actioned() {
12282        let dir = tempfile::tempdir().unwrap();
12283        let queue = Queue::at(dir.path().join("queue"));
12284        let questions = Questions::at(dir.path().join("questions"));
12285        let home = dir.path().join("home");
12286        let mut state = run_state(RunStatus::Blocked);
12287        state.id = "20260101-000000-act4".to_owned();
12288        state.save_under(&home).unwrap();
12289        let mut t = held_task_with(&state.id);
12290        t.status = TaskStatus::Running;
12291        queue.put(&mut t).unwrap();
12292        let mut q = action_question(&state.id, ask::ChoiceAction::Done);
12293        q.cwd = Some(dir.path().display().to_string());
12294        questions.put(&mut q).unwrap();
12295
12296        // While the task runs the daemon will not act, so the waiter still
12297        // delivers the word to the dead asker's seat ...
12298        let running = queue.get(&t.id).unwrap();
12299        assert_eq!(action_standing(&running, &q), ActionStanding::Busy);
12300        assert!(matches!(
12301            crate::waiter::decide_owned(&q, None, false, 86_400, Timestamp::now(), false),
12302            crate::waiter::Action::Deliver(_)
12303        ));
12304        // ... and the daemon waits while the task runs, then applies once.
12305        apply_task_choice_actions(&queue, &questions, &home);
12306        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Running);
12307        let mut back = queue.get(&t.id).unwrap();
12308        back.hold_machine(Some("later".into()));
12309        queue.put(&mut back).unwrap();
12310        apply_task_choice_actions(&queue, &questions, &home);
12311        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Done);
12312        let mut again = queue.get(&t.id).unwrap();
12313        again.hold_machine(Some("again".into()));
12314        queue.put(&mut again).unwrap();
12315        apply_task_choice_actions(&queue, &questions, &home);
12316        assert_eq!(queue.get(&t.id).unwrap().status, TaskStatus::Held);
12317    }
12318}